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],
"Starting gdb failed: %s\n", strerror(errno_value));
136 dprintf(pipe_output[1],
"(gdb) \n");
142 close(pipe_input[0]);
143 close(pipe_output[1]);
179 std::string line(command);
207 const size_t buf_size = 1024;
221 "EOF must have been reached when the error indicator on the stream "
222 "is not set and fgets returned NULL");
224 result.empty() || result.back() !=
'\n',
225 "when EOF is reached then either no characters were read or the string"
226 " read does not end in a newline");
231 std::string chunk(buf);
232 INVARIANT(!chunk.empty(),
"chunk cannot be empty when EOF was not reached");
235 }
while(result.back() !=
'\n');
249 }
while(line !=
"(gdb) \n");
269 std::string record =
strip_string(line.substr(line.find(
',') + 1));
285 const std::string command =
"core " + corefile;
321 if(frame_content.find(
"func=\"malloc\"") != std::string::npos)
345 std::string command(
"-break-insert");
346 command +=
" " + breakpoint;
386 const auto it = record.find(
"reason");
389 const std::string &reason = it->second;
391 if(reason ==
"breakpoint-hit")
396 else if(reason ==
"exited-normally")
403 "gdb stopped for unhandled reason `" + reason +
"`");
416 "could not create variable for expression `" + expr +
"`");
425 const auto it = record.find(
"value");
428 const std::string value = it->second;
431 value.back() !=
'"' ||
432 (value.length() >= 2 && value[value.length() - 2] ==
'\\'),
433 "quotes should have been stripped off from value");
434 INVARIANT(value.back() !=
'\n',
"value should not end in a newline");
457 const bool b = regex_match(value, result, regex);
462 const std::string
string = result[4];
466 const std::size_t len =
string.length();
470 "pointer-string should be: backslash, quotes, .., backslash, quotes");
473 "pointer-string should be: backslash, quotes, .., backslash, quotes");
476 "pointer-string should be: backslash, quotes, .., backslash, quotes");
478 string[len - 2] ==
'\\',
479 "pointer-string should be: backslash, quotes, .., backslash, quotes");
481 string[len - 1] ==
'"',
482 "pointer-string should be: backslash, quotes, .., backslash, quotes");
484 opt_string =
string.substr(2, len - 4);
487 return pointer_valuet(result[1], result[2], result[3], opt_string,
true);
507 std::regex regex(R
"([^ ]+ '([^']+)')");
510 const bool b = regex_match(value, result, regex);
514 return std::string{result[1]};
529 std::size_t depth = 0;
538 if(c ==
'{' || c ==
'[')
542 else if(c ==
'}' || c ==
']')
547 if(depth == 0 && (c ==
',' || i == n - 1))
549 const std::string item =
550 i == n - 1 ? s.substr(start) : s.substr(start, i - start);
558 const std::string key =
strip_string(item.substr(0, j));
561 const char first = value.front();
562 const char last = value.back();
564 INVARIANT(first ==
'"' || first ==
'{' || first ==
'[',
"");
565 INVARIANT(first !=
'"' || last ==
'"',
"");
566 INVARIANT(first !=
'{' || last ==
'}',
"");
567 INVARIANT(first !=
'[' || last ==
']',
"");
572 value = value.substr(1, value.length() - 2);
575 auto r = result.insert(std::make_pair(key, value));
598 return R
"((?:)" + regex + R"()?)";
604 return R
"((?:)" + regex_left + '|' + regex_right + R
"())";
609 const std::string &value_name)
611 const auto it = record.find(value_name);
613 const auto value = it->second;
616 value.back() !=
'"' ||
617 (value.length() >= 2 && value[value.length() - 2] ==
'\\'),
618 "quotes should have been stripped off from value");
619 INVARIANT(value.back() !=
'\n',
"value should not end in a newline");
626 const auto it = stopped_record.find(
"reason");
629 if(it->second !=
"breakpoint-hit")
641 std::string value_eq_quotes =
"value=\"";
642 auto value_eq_quotes_size = value_eq_quotes.size();
644 auto starting_pos = record_value.find(value_eq_quotes) + value_eq_quotes_size;
645 auto ending_pos = record_value.find(
'\"', starting_pos);
646 auto value_length = ending_pos - starting_pos;
647 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
bool has_prefix(const std::string &s, const std::string &prefix)
#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)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
unsignedbv_typet size_type()
std::string read_most_recent_line()
std::map< std::string, std::string > gdb_output_recordt