CBMC
gdb_api.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: GDB Machine Interface API
4 
5 Author: Malte Mues <mail.mues@gmail.com>
6  Daniel Poetzl
7 
8 \*******************************************************************/
9 
17 
18 #ifndef CPROVER_MEMORY_ANALYZER_GDB_API_H
19 #define CPROVER_MEMORY_ANALYZER_GDB_API_H
20 #include <unistd.h>
21 
22 #include <algorithm>
23 #include <forward_list>
24 #include <map>
25 
26 #include <util/exception_utils.h>
27 
29 class gdb_apit
30 {
31 public:
32  using commandst = std::forward_list<std::string>;
33 
37  {
39  std::string address_string;
41  {
42  }
43  explicit memory_addresst(const std::string &address_string)
45  {
46  }
47 
48  bool is_null() const
49  {
50  return null_address;
51  }
52  bool operator<(const memory_addresst &other) const
53  {
54  return address_string < other.address_string;
55  }
56  std::string string() const
57  {
58  return address_string;
59  }
60  };
61 
67  explicit gdb_apit(
68  const std::vector<std::string> &args, const bool log = false);
69 
72  ~gdb_apit();
73 
77  {
79  const std::string &address = "",
80  const std::string &pointee = "",
81  const std::string &character = "",
82  const std::optional<std::string> &string = {},
83  const bool valid = false)
84  : address(address),
87  string(string),
88  valid(valid)
89  {
90  }
91 
93  std::string pointee;
94  std::string character;
95  std::optional<std::string> string;
96 
97  bool has_known_offset() const
98  {
99  return std::any_of(
100  pointee.begin(), pointee.end(), [](char c) { return c == '+'; });
101  }
102 
103  bool valid;
104  };
105 
110  size_t query_malloc_size(const std::string &pointer_expr);
111 
114  void create_gdb_process();
115 
120  bool run_gdb_to_breakpoint(const std::string &breakpoint);
121 
124  void run_gdb_from_core(const std::string &corefile);
125 
130  std::optional<std::string> get_value(const std::string &expr);
131 
135  pointer_valuet get_memory(const std::string &expr);
136 
139  const commandst &get_command_log();
140 
141 protected:
142  // arguments passed to gdb, first argument is the command to execute
143  std::vector<std::string> args;
144 
147 
148  const bool log;
150 
151  enum class gdb_statet
152  {
153  NOT_CREATED,
154  CREATED,
155  STOPPED // valid state, reached e.g. after breakpoint was hit
156  };
157 
159 
162  std::map<std::string, size_t> allocated_memory;
163 
164  typedef std::map<std::string, std::string> gdb_output_recordt;
165  static gdb_output_recordt parse_gdb_output_record(const std::string &s);
166 
167  void write_to_gdb(const std::string &command);
168 
169  std::string read_next_line();
170  std::string read_most_recent_line();
171 
172  std::string eval_expr(const std::string &expr);
173 
175  get_most_recent_record(const std::string &tag, const bool must_exist = false);
176 
177  bool most_recent_line_has_tag(const std::string &tag);
180 
183  void collect_malloc_calls();
184 
190  const gdb_output_recordt &record,
191  const std::string &value_name);
192 
196  bool hit_malloc_breakpoint(const gdb_output_recordt &stopped_record);
197 
201  std::string get_register_value(const gdb_output_recordt &record);
202 
203  static std::string r_opt(const std::string &regex);
204 
205  static std::string
206  r_or(const std::string &regex_left, const std::string &regex_right);
207 
208  // regex group for hex memory address (part of the output of gdb when printing
209  // a pointer), matches e.g. 0x601040 and extracts 0x601040
210  const std::string r_hex_addr = R"((0x(?:0|[1-9a-f][0-9a-f]*)))";
211 
212  // regex group for identifier (optional part of the output of gdb when
213  // printing a pointer), matches e.g. <abc> and extracts abc
214  const std::string r_id = R"(<([^<>]+)>)";
215 
216  // regex group for octal encoded char (optional part of the output of gdb when
217  // printing a pointer), matches e.g. \"\\003\" and extracts \\003
218  const std::string r_char = R"(\\"(\\\\[0-7]{3})\\")";
219 
220  // regex group for string (optional part of the output of gdb when printing a
221  // pointer), matches e.g. \"abc\" and extracts \"abc\"
222  const std::string r_string = R"((\\".*\\"))";
223 
224  // name of malloc function
225  const std::string malloc_name = "malloc";
226 };
227 
229 {
230 public:
231  explicit gdb_interaction_exceptiont(std::string reason)
232  : cprover_exception_baset(std::move(reason))
233  {
234  }
235 };
236 
237 #endif // CPROVER_MEMORY_ANALYZER_GDB_API_H
Base class for exceptions thrown in the cprover project.
Definition: c_errors.h:64
std::string reason
The reason this exception was generated.
Definition: c_errors.h:83
Interface for running and querying GDB.
Definition: gdb_api.h:30
static std::string r_opt(const std::string &regex)
bool was_command_accepted()
void check_command_accepted()
bool run_gdb_to_breakpoint(const std::string &breakpoint)
Run gdb to the given breakpoint.
Definition: gdb_api.cpp:345
void collect_malloc_calls()
Intercepts the gdb-analysis at the malloc call-site to add the corresponding information into allocat...
Definition: gdb_api.cpp:288
FILE * command_stream
Definition: gdb_api.h:146
void create_gdb_process()
Create a new gdb process for analysing the binary indicated by the first element in args
Definition: gdb_api.cpp:67
std::map< std::string, size_t > allocated_memory
track the allocated size for each malloc call maps hexadecimal address to the number of bytes
Definition: gdb_api.h:162
gdb_statet gdb_state
Definition: gdb_api.h:158
const std::string r_char
Definition: gdb_api.h:218
const std::string r_hex_addr
Definition: gdb_api.h:210
void write_to_gdb(const std::string &command)
Definition: gdb_api.cpp:169
std::map< std::string, std::string > gdb_output_recordt
Definition: gdb_api.h:164
void run_gdb_from_core(const std::string &corefile)
Run gdb with the given core file.
Definition: gdb_api.cpp:275
const commandst & get_command_log()
Return the vector of commands that have been written to gdb so far.
Definition: gdb_api.cpp:190
std::optional< std::string > get_value(const std::string &expr)
Get the memory address pointed to by the given pointer expression.
gdb_statet
Definition: gdb_api.h:152
std::string eval_expr(const std::string &expr)
Definition: gdb_api.cpp:416
const std::string r_id
Definition: gdb_api.h:214
std::string read_next_line()
Definition: gdb_api.cpp:196
const std::string r_string
Definition: gdb_api.h:222
const bool log
Definition: gdb_api.h:148
std::forward_list< std::string > commandst
Definition: gdb_api.h:32
FILE * response_stream
Definition: gdb_api.h:145
std::string get_register_value(const gdb_output_recordt &record)
Parse the record produced by listing register value.
gdb_output_recordt get_most_recent_record(const std::string &tag, const bool must_exist=false)
Definition: gdb_api.cpp:250
const std::string malloc_name
Definition: gdb_api.h:225
static std::string r_or(const std::string &regex_left, const std::string &regex_right)
std::string get_value_from_record(const gdb_output_recordt &record, const std::string &value_name)
Locate and return the value for a given name.
static gdb_output_recordt parse_gdb_output_record(const std::string &s)
std::vector< std::string > args
Definition: gdb_api.h:143
pointer_valuet get_memory(const std::string &expr)
Get the value of a pointer associated with expr.
size_t query_malloc_size(const std::string &pointer_expr)
Get the exact allocated size for a pointer pointer_expr.
Definition: gdb_api.cpp:56
gdb_apit(const std::vector< std::string > &args, const bool log=false)
Create a gdb_apit object.
Definition: gdb_api.cpp:28
std::string read_most_recent_line()
Definition: gdb_api.cpp:235
commandst command_log
Definition: gdb_api.h:149
bool most_recent_line_has_tag(const std::string &tag)
Definition: gdb_api.cpp:269
~gdb_apit()
Terminate the gdb process and close open streams (for reading from and writing to gdb)
Definition: gdb_api.cpp:33
bool hit_malloc_breakpoint(const gdb_output_recordt &stopped_record)
Check if the breakpoint we hit is inside a malloc.
gdb_interaction_exceptiont(std::string reason)
Definition: gdb_api.h:231
Memory address imbued with the explicit boolean data indicating if the address is null or not.
Definition: gdb_api.h:37
std::string string() const
Definition: gdb_api.h:56
memory_addresst(const std::string &address_string)
Definition: gdb_api.h:43
std::string address_string
Definition: gdb_api.h:39
bool is_null() const
Definition: gdb_api.h:48
bool operator<(const memory_addresst &other) const
Definition: gdb_api.h:52
Data associated with the value of a pointer, i.e.
Definition: gdb_api.h:77
memory_addresst address
Definition: gdb_api.h:92
std::optional< std::string > string
Definition: gdb_api.h:95
bool has_known_offset() const
Definition: gdb_api.h:97
std::string character
Definition: gdb_api.h:94
std::string pointee
Definition: gdb_api.h:93
pointer_valuet(const std::string &address="", const std::string &pointee="", const std::string &character="", const std::optional< std::string > &string={}, const bool valid=false)
Definition: gdb_api.h:78