1 // Copyright 2014, ARM Limited
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of ARM Limited nor the names of its contributors may be
13 //     used to endorse or promote products derived from this software without
14 //     specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef VIXL_A64_DEBUGGER_A64_H_
28 #define VIXL_A64_DEBUGGER_A64_H_
29 
30 #include <ctype.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include <vector>
34 
35 #include "vixl/globals.h"
36 #include "vixl/utils.h"
37 #include "vixl/a64/constants-a64.h"
38 #include "vixl/a64/simulator-a64.h"
39 
40 namespace vixl {
41 
42 // Flags that represent the debugger state.
43 enum DebugParameters {
44   DBG_INACTIVE = 0,
45   DBG_ACTIVE = 1 << 0,  // The debugger is active.
46   DBG_BREAK  = 1 << 1   // The debugger is at a breakpoint.
47 };
48 
49 // Forward declarations.
50 class DebugCommand;
51 class Token;
52 class FormatToken;
53 
54 class Debugger : public Simulator {
55  public:
56   explicit Debugger(Decoder* decoder, FILE* stream = stdout);
57   ~Debugger();
58 
59   virtual void Run();
60   virtual void VisitException(const Instruction* instr);
61 
debug_parameters()62   int debug_parameters() const { return debug_parameters_; }
set_debug_parameters(int parameters)63   void set_debug_parameters(int parameters) {
64     debug_parameters_ = parameters;
65 
66     update_pending_request();
67   }
68 
69   // Numbers of instructions to execute before the debugger shell is given
70   // back control.
steps()71   int steps() const { return steps_; }
set_steps(int value)72   void set_steps(int value) {
73     VIXL_ASSERT(value > 1);
74     steps_ = value;
75   }
76 
IsDebuggerRunning()77   bool IsDebuggerRunning() const {
78     return (debug_parameters_ & DBG_ACTIVE) != 0;
79   }
80 
pending_request()81   bool pending_request() const { return pending_request_; }
update_pending_request()82   void update_pending_request() {
83     pending_request_ = IsDebuggerRunning();
84   }
85 
86   void PrintInstructions(const void* address, int64_t count = 1);
87   void PrintMemory(const uint8_t* address,
88                    const FormatToken* format,
89                    int64_t count = 1);
90   void PrintRegister(const Register& target_reg,
91                      const char* name,
92                      const FormatToken* format);
93   void PrintFPRegister(const FPRegister& target_fpreg,
94                        const FormatToken* format);
95 
96  private:
97   char* ReadCommandLine(const char* prompt, char* buffer, int length);
98   void RunDebuggerShell();
99   void DoBreakpoint(const Instruction* instr);
100 
101   int debug_parameters_;
102   bool pending_request_;
103   int steps_;
104   DebugCommand* last_command_;
105   PrintDisassembler* disasm_;
106   Decoder* printer_;
107 
108   // Length of the biggest command line accepted by the debugger shell.
109   static const int kMaxDebugShellLine = 256;
110 };
111 
112 }  // namespace vixl
113 
114 #endif  // VIXL_A64_DEBUGGER_A64_H_
115