CBMC
gdb_apit Class Reference

Interface for running and querying GDB. More...

#include <gdb_api.h>

+ Collaboration diagram for gdb_apit:

Classes

struct  memory_addresst
 Memory address imbued with the explicit boolean data indicating if the address is null or not. More...
 
struct  pointer_valuet
 Data associated with the value of a pointer, i.e. More...
 

Public Types

using commandst = std::forward_list< std::string >
 

Public Member Functions

 gdb_apit (const std::vector< std::string > &args, const bool log=false)
 Create a gdb_apit object. More...
 
 ~gdb_apit ()
 Terminate the gdb process and close open streams (for reading from and writing to gdb) More...
 
size_t query_malloc_size (const std::string &pointer_expr)
 Get the exact allocated size for a pointer pointer_expr. More...
 
void create_gdb_process ()
 Create a new gdb process for analysing the binary indicated by the first element in args More...
 
bool run_gdb_to_breakpoint (const std::string &breakpoint)
 Run gdb to the given breakpoint. More...
 
void run_gdb_from_core (const std::string &corefile)
 Run gdb with the given core file. More...
 
std::optional< std::string > get_value (const std::string &expr)
 Get the memory address pointed to by the given pointer expression. More...
 
pointer_valuet get_memory (const std::string &expr)
 Get the value of a pointer associated with expr. More...
 
const commandstget_command_log ()
 Return the vector of commands that have been written to gdb so far. More...
 

Protected Types

enum class  gdb_statet { NOT_CREATED , CREATED , STOPPED }
 
typedef std::map< std::string, std::string > gdb_output_recordt
 

Protected Member Functions

void write_to_gdb (const std::string &command)
 
std::string read_next_line ()
 
std::string read_most_recent_line ()
 
std::string eval_expr (const std::string &expr)
 
gdb_output_recordt get_most_recent_record (const std::string &tag, const bool must_exist=false)
 
bool most_recent_line_has_tag (const std::string &tag)
 
bool was_command_accepted ()
 
void check_command_accepted ()
 
void collect_malloc_calls ()
 Intercepts the gdb-analysis at the malloc call-site to add the corresponding information into allocated_memory. More...
 
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. More...
 
bool hit_malloc_breakpoint (const gdb_output_recordt &stopped_record)
 Check if the breakpoint we hit is inside a malloc. More...
 
std::string get_register_value (const gdb_output_recordt &record)
 Parse the record produced by listing register value. More...
 

Static Protected Member Functions

static gdb_output_recordt parse_gdb_output_record (const std::string &s)
 
static std::string r_opt (const std::string &regex)
 
static std::string r_or (const std::string &regex_left, const std::string &regex_right)
 

Protected Attributes

std::vector< std::string > args
 
FILE * response_stream
 
FILE * command_stream
 
const bool log
 
commandst command_log
 
gdb_statet gdb_state
 
std::map< std::string, size_t > allocated_memory
 track the allocated size for each malloc call maps hexadecimal address to the number of bytes More...
 
const std::string r_hex_addr = R"((0x(?:0|[1-9a-f][0-9a-f]*)))"
 
const std::string r_id = R"(<([^<>]+)>)"
 
const std::string r_char = R"(\\"(\\\\[0-7]{3})\\")"
 
const std::string r_string = R"((\\".*\\"))"
 
const std::string malloc_name = "malloc"
 

Detailed Description

Interface for running and querying GDB.

Definition at line 29 of file gdb_api.h.

Member Typedef Documentation

◆ commandst

using gdb_apit::commandst = std::forward_list<std::string>

Definition at line 32 of file gdb_api.h.

◆ gdb_output_recordt

typedef std::map<std::string, std::string> gdb_apit::gdb_output_recordt
protected

Definition at line 164 of file gdb_api.h.

Member Enumeration Documentation

◆ gdb_statet

enum gdb_apit::gdb_statet
strongprotected
Enumerator
NOT_CREATED 
CREATED 
STOPPED 

Definition at line 151 of file gdb_api.h.

Constructor & Destructor Documentation

◆ gdb_apit()

gdb_apit::gdb_apit ( const std::vector< std::string > &  args,
const bool  log = false 
)
explicit

Create a gdb_apit object.

Parameters
argsarguments to pass to gdb, the first argument is the command to execute
logboolean indicating whether gdb input and output should be logged

Definition at line 28 of file gdb_api.cpp.

◆ ~gdb_apit()

gdb_apit::~gdb_apit ( )

Terminate the gdb process and close open streams (for reading from and writing to gdb)

Definition at line 33 of file gdb_api.cpp.

Member Function Documentation

◆ check_command_accepted()

void gdb_apit::check_command_accepted ( )
protected

◆ collect_malloc_calls()

void gdb_apit::collect_malloc_calls ( )
protected

Intercepts the gdb-analysis at the malloc call-site to add the corresponding information into allocated_memory.

Definition at line 288 of file gdb_api.cpp.

◆ create_gdb_process()

void gdb_apit::create_gdb_process ( )

Create a new gdb process for analysing the binary indicated by the first element in args

Definition at line 67 of file gdb_api.cpp.

◆ eval_expr()

std::string gdb_apit::eval_expr ( const std::string &  expr)
protected

Definition at line 416 of file gdb_api.cpp.

◆ get_command_log()

const gdb_apit::commandst & gdb_apit::get_command_log ( )

Return the vector of commands that have been written to gdb so far.

Returns
the list of commands

Definition at line 190 of file gdb_api.cpp.

◆ get_memory()

pointer_valuet gdb_apit::get_memory ( const std::string &  expr)

Get the value of a pointer associated with expr.

Parameters
exprthe expression to be analyzed
Returns
the pointer_valuet filled with data gdb produced for expr

◆ get_most_recent_record()

gdb_apit::gdb_output_recordt gdb_apit::get_most_recent_record ( const std::string &  tag,
const bool  must_exist = false 
)
protected

Definition at line 250 of file gdb_api.cpp.

◆ get_register_value()

std::string gdb_apit::get_register_value ( const gdb_output_recordt record)
protected

Parse the record produced by listing register value.

Parameters
recordgdb record for one register value
Returns
get the value associated with some register value

◆ get_value()

std::optional<std::string> gdb_apit::get_value ( const std::string &  expr)

Get the memory address pointed to by the given pointer expression.

Parameters
expran expression of pointer type (e.g., &x with x being of type int or p with p being of type int *)
Returns
memory address in hex format

◆ get_value_from_record()

std::string gdb_apit::get_value_from_record ( const gdb_output_recordt record,
const std::string &  value_name 
)
protected

Locate and return the value for a given name.

Parameters
recordgdb record to search
value_namename of the value to be extracted
Returns
the value associated with value_name

◆ hit_malloc_breakpoint()

bool gdb_apit::hit_malloc_breakpoint ( const gdb_output_recordt stopped_record)
protected

Check if the breakpoint we hit is inside a malloc.

Parameters
stopped_recordgdb record pertaining to a breakpoint being hit
Returns
true if the breakpoint the gdb stopped at was malloc

◆ most_recent_line_has_tag()

bool gdb_apit::most_recent_line_has_tag ( const std::string &  tag)
protected

Definition at line 269 of file gdb_api.cpp.

◆ parse_gdb_output_record()

static gdb_output_recordt gdb_apit::parse_gdb_output_record ( const std::string &  s)
staticprotected

◆ query_malloc_size()

size_t gdb_apit::query_malloc_size ( const std::string &  pointer_expr)

Get the exact allocated size for a pointer pointer_expr.

Parameters
pointer_exprexpression with a pointer name
Returns
1 if the pointer was not allocated with malloc otherwise return the number of allocated bytes

Definition at line 56 of file gdb_api.cpp.

◆ r_opt()

static std::string gdb_apit::r_opt ( const std::string &  regex)
staticprotected

◆ r_or()

static std::string gdb_apit::r_or ( const std::string &  regex_left,
const std::string &  regex_right 
)
staticprotected

◆ read_most_recent_line()

std::string gdb_apit::read_most_recent_line ( )
protected

Definition at line 235 of file gdb_api.cpp.

◆ read_next_line()

std::string gdb_apit::read_next_line ( )
protected

Definition at line 196 of file gdb_api.cpp.

◆ run_gdb_from_core()

void gdb_apit::run_gdb_from_core ( const std::string &  corefile)

Run gdb with the given core file.

Parameters
corefilecore dump

Definition at line 275 of file gdb_api.cpp.

◆ run_gdb_to_breakpoint()

bool gdb_apit::run_gdb_to_breakpoint ( const std::string &  breakpoint)

Run gdb to the given breakpoint.

Parameters
breakpointthe breakpoint to set (can be e.g. a line number or a function name)
Returns
true if something failed

Definition at line 345 of file gdb_api.cpp.

◆ was_command_accepted()

bool gdb_apit::was_command_accepted ( )
protected

◆ write_to_gdb()

void gdb_apit::write_to_gdb ( const std::string &  command)
protected

Definition at line 169 of file gdb_api.cpp.

Member Data Documentation

◆ allocated_memory

std::map<std::string, size_t> gdb_apit::allocated_memory
protected

track the allocated size for each malloc call maps hexadecimal address to the number of bytes

Definition at line 162 of file gdb_api.h.

◆ args

std::vector<std::string> gdb_apit::args
protected

Definition at line 143 of file gdb_api.h.

◆ command_log

commandst gdb_apit::command_log
protected

Definition at line 149 of file gdb_api.h.

◆ command_stream

FILE* gdb_apit::command_stream
protected

Definition at line 146 of file gdb_api.h.

◆ gdb_state

gdb_statet gdb_apit::gdb_state
protected

Definition at line 158 of file gdb_api.h.

◆ log

const bool gdb_apit::log
protected

Definition at line 148 of file gdb_api.h.

◆ malloc_name

const std::string gdb_apit::malloc_name = "malloc"
protected

Definition at line 225 of file gdb_api.h.

◆ r_char

const std::string gdb_apit::r_char = R"(\\"(\\\\[0-7]{3})\\")"
protected

Definition at line 218 of file gdb_api.h.

◆ r_hex_addr

const std::string gdb_apit::r_hex_addr = R"((0x(?:0|[1-9a-f][0-9a-f]*)))"
protected

Definition at line 210 of file gdb_api.h.

◆ r_id

const std::string gdb_apit::r_id = R"(<([^<>]+)>)"
protected

Definition at line 214 of file gdb_api.h.

◆ r_string

const std::string gdb_apit::r_string = R"((\\".*\\"))"
protected

Definition at line 222 of file gdb_api.h.

◆ response_stream

FILE* gdb_apit::response_stream
protected

Definition at line 145 of file gdb_api.h.


The documentation for this class was generated from the following files: