ROSE  0.11.145.0
GdbResponse.h
1 #ifndef ROSE_BinaryAnalysis_Debugger_GdbResponse_H
2 #define ROSE_BinaryAnalysis_Debugger_GdbResponse_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_DEBUGGER_GDB
5 
6 #include <Rose/BinaryAnalysis/Debugger/Exception.h>
7 #include <Rose/Yaml.h>
8 
9 namespace Rose {
10 namespace BinaryAnalysis {
11 namespace Debugger {
12 
16 class GdbResponse {
17 
18  // Token types from the lexical analyzer
19  enum TokenType {
20  TOK_EOF, // end of file
21  TOK_LEFT, // '{' or '['
22  TOK_RIGHT, // '}' or ']'
23  TOK_DIGITS, // sequence of digits
24  TOK_SYMBOL, // sequence of letters
25  TOK_STRING, // C-style string literal
26  TOK_GDB, // the string "(gdb)"
27  TOK_OTHER // anything else
28  };
29 
30  // One token from the GDB MI output langauge
31  class Token {
32  friend class TokenStream;
33  TokenType type_ = TOK_EOF;
34  size_t begin_ = 0;
35  size_t end_ = 0;
36 
37  public:
38  Token() {}
39  Token(TokenType type, size_t begin, size_t end)
40  : type_(type), begin_(begin), end_(end) {
41  ASSERT_require(end >= begin);
42  }
43 
44  TokenType type() const {
45  return type_;
46  }
47 
48  size_t begin() const {
49  return begin_;
50  }
51 
52  size_t end() const {
53  return end_;
54  }
55 
56  size_t size() const {
57  return end_ - begin_;
58  }
59  };
60 
61  // Lexical analysis and the resulting sequence of tokens.
62  class TokenStream {
63  std::string content_;
64  size_t at_ = 0;
65  std::vector<Token> tokens_;
66 
67  public:
68  explicit TokenStream(const std::string &sentence)
69  : content_(sentence) {}
70 
71  const Token& operator[](size_t lookahead);
72  void consume(size_t n = 1);
73  std::string lexeme(size_t lookahead) const;
74  const std::string& content() const;
75 
76  private:
77  void makeNextToken();
78  };
79 
80 public:
82  enum class ResultClass { DONE, RUNNING, CONNECTED, ERROR, EXIT, UNSPECIFIED };
83 
85  enum class AsyncClass {
86  STOPPED,
87  RUNNING,
88  THREAD_GROUP_ADDED,
89  THREAD_GROUP_STARTED,
90  THREAD_GROUP_EXITED,
91  THREAD_CREATED,
92  THREAD_EXITED,
93  BREAKPOINT_MODIFIED,
94  OTHER,
96  };
97 
99  struct ResultRecord {
100  ResultClass rclass = ResultClass::UNSPECIFIED;
101  Yaml::Node results;
104  explicit operator bool() const {
105  return rclass != ResultClass::UNSPECIFIED;
106  }
107  };
108 
110  struct AsyncRecord {
111  AsyncClass aclass = AsyncClass::UNSPECIFIED;
112  Yaml::Node results;
115  explicit operator bool() const {
116  return aclass != AsyncClass::UNSPECIFIED;
117  }
118  };
119 
120 public:
121  std::string raw;
122  bool atEnd = false;
123  std::string token;
124  ResultRecord result;
125  AsyncRecord exec;
126  AsyncRecord status;
127  AsyncRecord notify;
128  std::string console;
129  std::string target;
130  std::string log;
132 public:
134  static GdbResponse parse(const std::string &line);
135 
137  void print(std::ostream&) const;
138 
139 private:
140  // Construct an exception for a syntax error with information about the error and where it occurs.
141  static Exception syntaxError(std::string mesg, TokenStream&, size_t);
142 
143  // Parsers
144  static void parseResultRecord(TokenStream&, GdbResponse&);
145  static std::pair<AsyncClass, Yaml::Node> parseAsyncOutput(TokenStream&);
146  static void parseExecAsyncOutput(TokenStream&, GdbResponse&);
147  static void parseStatusAsyncOutput(TokenStream&, GdbResponse&);
148  static void parseNotifyAsyncOutput(TokenStream&, GdbResponse&);
149  static void parseConsoleStreamOutput(TokenStream&, GdbResponse&);
150  static void parseTargetStreamOutput(TokenStream&, GdbResponse&);
151  static void parseLogStreamOutput(TokenStream&, GdbResponse&);
152  static std::pair<std::string, Yaml::Node> parseResult(TokenStream&);
153  static Yaml::Node parseValue(TokenStream&);
154 
155  // Returns indented YAML unparsed from the specified node tree.
156  static std::string nodeToString(const Yaml::Node&, const std::string&);
157 };
158 
160 std::ostream& operator<<(std::ostream&, const GdbResponse&);
161 
162 } // namespace
163 } // namespace
164 } // namespace
165 
166 #endif
167 #endif
Sawyer::Language::Clexer::Token Token
A token parsed from the input file.
Definition: ud/BasicTypes.h:10
Stack parameter order is unknown or unspecified.
Error messages that indicate an abnormal situation from which the program was able to at least partia...
Definition: Message.h:330
Main namespace for the ROSE library.
Sawyer::Language::Clexer::TokenStream TokenStream
A stream of tokens from the input file.
Definition: ud/BasicTypes.h:13
void print(const StackVariables &, const Partitioner2::PartitionerConstPtr &, std::ostream &out, const std::string &prefix="")
Print info about multiple local variables.
const char * AsyncClass(int64_t)
Convert Rose::BinaryAnalysis::Debugger::GdbResponse::AsyncClass enum constant to a string...
const char * ResultClass(int64_t)
Convert Rose::BinaryAnalysis::Debugger::Gdb::Response::ResultRecord::ResultClass enum constant to a s...
void notify(variables_map &)
Transfer map to C++ variables.
void parse(SgAsmGenericFile *)
Main function to parse DWARF information.