Go to the documentation of this file.
34 : args(args), log(log), gdb_state(
gdb_statet::NOT_CREATED)
63 const auto maybe_address_string =
get_value(pointer_expr);
83 if(pipe(pipe_input) == -1)
88 if(pipe(pipe_output) == -1)
103 close(pipe_input[1]);
104 close(pipe_output[0]);
106 dup2(pipe_input[0], STDIN_FILENO);
107 dup2(pipe_output[1], STDOUT_FILENO);
108 dup2(pipe_output[1], STDERR_FILENO);
110 dprintf(pipe_output[1],
"binary name: %s\n",
args.front().c_str());
112 std::vector<std::string> exec_cmd;
113 exec_cmd.reserve(
args.size() + 3);
114 exec_cmd.push_back(
"gdb");
115 exec_cmd.push_back(
"--interpreter=mi");
116 exec_cmd.push_back(
"--args");
117 exec_cmd.insert(exec_cmd.end(),
args.begin(),
args.end());
119 char **exec_cmd_ptr =
static_cast<char **
>(malloc(
120 sizeof(
char *) * (exec_cmd.size() + 1)));
121 exec_cmd_ptr[exec_cmd.size()] = NULL;
123 for(std::size_t i = 0; i < exec_cmd.size(); i++)
125 exec_cmd_ptr[i] =
static_cast<char *
>(malloc(
126 sizeof(
char) * (exec_cmd[i].length() + 1)));
127 strcpy(exec_cmd_ptr[i], exec_cmd[i].c_str());
130 dprintf(pipe_output[1],
"Loading gdb...\n");
131 execvp(
"gdb", exec_cmd_ptr);
134 int errno_value = errno;
135 dprintf(pipe_output[1],
"errno in child: %s\n", strerror(errno_value));
142 close(pipe_input[0]);
143 close(pipe_output[1]);
174 std::string line(command);
202 const size_t buf_size = 1024;
216 "EOF must have been reached when the error indicator on the stream "
217 "is not set and fgets returned NULL");
219 result.empty() || result.back() !=
'\n',
220 "when EOF is reached then either no characters were read or the string"
221 " read does not end in a newline");
226 std::string chunk(buf);
227 INVARIANT(!chunk.empty(),
"chunk cannot be empty when EOF was not reached");
230 }
while(result.back() !=
'\n');
244 }
while(line !=
"(gdb) \n");
264 std::string record =
strip_string(line.substr(line.find(
',') + 1));
280 const std::string command =
"core " + corefile;
316 if(frame_content.find(
"func=\"malloc\"") != std::string::npos)
340 std::string command(
"-break-insert");
341 command +=
" " + breakpoint;
381 const auto it = record.find(
"reason");
384 const std::string &reason = it->second;
386 if(reason ==
"breakpoint-hit")
391 else if(reason ==
"exited-normally")
398 "gdb stopped for unhandled reason `" + reason +
"`");
411 "could not create variable for expression `" + expr +
"`");
420 const auto it = record.find(
"value");
423 const std::string value = it->second;
426 value.back() !=
'"' ||
427 (value.length() >= 2 && value[value.length() - 2] ==
'\\'),
428 "quotes should have been stripped off from value");
429 INVARIANT(value.back() !=
'\n',
"value should not end in a newline");
452 const bool b = regex_match(value, result, regex);
457 const std::string
string = result[4];
461 const std::size_t len =
string.length();
465 "pointer-string should be: backslash, quotes, .., backslash, quotes");
468 "pointer-string should be: backslash, quotes, .., backslash, quotes");
471 "pointer-string should be: backslash, quotes, .., backslash, quotes");
473 string[len - 2] ==
'\\',
474 "pointer-string should be: backslash, quotes, .., backslash, quotes");
476 string[len - 1] ==
'"',
477 "pointer-string should be: backslash, quotes, .., backslash, quotes");
479 opt_string =
string.substr(2, len - 4);
482 return pointer_valuet(result[1], result[2], result[3], opt_string,
true);
502 std::regex regex(R
"([^ ]+ '([^']+)')");
505 const bool b = regex_match(value, result, regex);
509 return std::string{result[1]};
524 std::size_t depth = 0;
533 if(c ==
'{' || c ==
'[')
537 else if(c ==
'}' || c ==
']')
542 if(depth == 0 && (c ==
',' || i == n - 1))
544 const std::string item =
545 i == n - 1 ? s.substr(start) : s.substr(start, i - start);
553 const std::string key =
strip_string(item.substr(0, j));
556 const char first = value.front();
557 const char last = value.back();
559 INVARIANT(first ==
'"' || first ==
'{' || first ==
'[',
"");
560 INVARIANT(first !=
'"' || last ==
'"',
"");
561 INVARIANT(first !=
'{' || last ==
'}',
"");
562 INVARIANT(first !=
'[' || last ==
']',
"");
567 value = value.substr(1, value.length() - 2);
570 auto r = result.insert(std::make_pair(key, value));
593 return R
"((?:)" + regex + R"()?)";
599 return R
"((?:)" + regex_left + '|' + regex_right + R
"())";
604 const std::string &value_name)
606 const auto it = record.find(value_name);
608 const auto value = it->second;
611 value.back() !=
'"' ||
612 (value.length() >= 2 && value[value.length() - 2] ==
'\\'),
613 "quotes should have been stripped off from value");
614 INVARIANT(value.back() !=
'\n',
"value should not end in a newline");
621 const auto it = stopped_record.find(
"reason");
624 if(it->second !=
"breakpoint-hit")
636 std::string value_eq_quotes =
"value=\"";
637 auto value_eq_quotes_size = value_eq_quotes.size();
639 auto starting_pos = record_value.find(value_eq_quotes) + value_eq_quotes_size;
640 auto ending_pos = record_value.find(
'\"', starting_pos);
641 auto value_length = ending_pos - starting_pos;
642 return std::string{record_value, starting_pos, value_length};
#define UNREACHABLE
This should be used to mark dead code.
bool hit_malloc_breakpoint(const gdb_output_recordt &stopped_record)
Check if the breakpoint we hit is inside a malloc.
void collect_malloc_calls()
Intercepts the gdb-analysis at the malloc call-site to add the corresponding information into allocat...
size_t query_malloc_size(const std::string &pointer_expr)
Get the exact allocated size for a pointer pointer_expr.
Data associated with the value of a pointer, i.e.
std::vector< std::string > args
#define CHECK_RETURN(CONDITION)
std::forward_list< std::string > commandst
bool most_recent_line_has_tag(const std::string &tag)
optionalt< std::string > get_value(const std::string &expr)
Get the memory address pointed to by the given pointer expression.
const irept & find(const irep_namet &name) const
gdb_apit(const std::vector< std::string > &args, const bool log=false)
Create a gdb_apit object.
std::string eval_expr(const std::string &expr)
static gdb_output_recordt parse_gdb_output_record(const std::string &s)
std::size_t safe_string2size_t(const std::string &str, int base)
Low-level interface to gdb.
std::string strip_string(const std::string &s)
Remove all whitespace characters from either end of a string.
const std::string r_hex_addr
#define PRECONDITION(CONDITION)
gdb_output_recordt get_most_recent_record(const std::string &tag, const bool must_exist=false)
void write_to_gdb(const std::string &command)
~gdb_apit()
Terminate the gdb process and close open streams (for reading from and writing to gdb)
std::string read_next_line()
const commandst & get_command_log()
Return the vector of commands that have been written to gdb so far.
const std::string malloc_name
std::map< std::string, size_t > allocated_memory
track the allocated size for each malloc call maps hexadecimal address to the number of bytes
std::string get_register_value(const gdb_output_recordt &record)
Parse the record produced by listing register value.
nonstd::optional< T > optionalt
static std::string r_opt(const std::string ®ex)
bool was_command_accepted()
pointer_valuet get_memory(const std::string &expr)
Get the value of a pointer associated with expr.
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.
void create_gdb_process()
Create a new gdb process for analysing the binary indicated by the first element in args
const std::string r_string
void check_command_accepted()
bool run_gdb_to_breakpoint(const std::string &breakpoint)
Run gdb to the given breakpoint.
void run_gdb_from_core(const std::string &corefile)
Run gdb with the given core file.
static std::string r_or(const std::string ®ex_left, const std::string ®ex_right)
bool has_prefix(const std::string &s, const std::string &prefix)
unsignedbv_typet size_type()
std::string read_most_recent_line()
std::map< std::string, std::string > gdb_output_recordt