Skip to content

Commit 79d6d09

Browse files
Petr Bauchpetr-bauch
authored andcommitted
Optionalize the results of querying gdb
Not all queried values are sensible if we ask past allocated memory (malloc_usable_size doesn't return exact values).
1 parent df72e59 commit 79d6d09

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/memory-analyzer/gdb_api.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,23 @@ gdb_apit::pointer_valuet gdb_apit::get_memory(const std::string &expr)
369369
{
370370
PRECONDITION(gdb_state == gdb_statet::STOPPED);
371371

372-
std::string value = eval_expr(expr);
372+
std::string value;
373+
try
374+
{
375+
value = eval_expr(expr);
376+
}
377+
catch(gdb_interaction_exceptiont &e)
378+
{
379+
return pointer_valuet{};
380+
}
373381

374382
std::regex regex(
375383
r_hex_addr + r_opt(' ' + r_id) + r_opt(' ' + r_or(r_char, r_string)));
376384

377385
std::smatch result;
378386
const bool b = regex_match(value, result, regex);
379-
CHECK_RETURN(b);
387+
if(!b)
388+
return pointer_valuet{};
380389

381390
optionalt<std::string> opt_string;
382391
const std::string string = result[4];
@@ -404,14 +413,22 @@ gdb_apit::pointer_valuet gdb_apit::get_memory(const std::string &expr)
404413
opt_string = string.substr(2, len - 4);
405414
}
406415

407-
return pointer_valuet(result[1], result[2], result[3], opt_string);
416+
return pointer_valuet(result[1], result[2], result[3], opt_string, true);
408417
}
409418

410-
std::string gdb_apit::get_value(const std::string &expr)
419+
optionalt<std::string> gdb_apit::get_value(const std::string &expr)
411420
{
412421
PRECONDITION(gdb_state == gdb_statet::STOPPED);
413422

414-
const std::string value = eval_expr(expr);
423+
std::string value;
424+
try
425+
{
426+
value = eval_expr(expr);
427+
}
428+
catch(gdb_interaction_exceptiont &e)
429+
{
430+
return {};
431+
}
415432

416433
// Get char value
417434
{
@@ -423,7 +440,7 @@ std::string gdb_apit::get_value(const std::string &expr)
423440

424441
if(b)
425442
{
426-
return result[1];
443+
return std::string{result[1]};
427444
}
428445
}
429446

src/memory-analyzer/gdb_api.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,24 @@ class gdb_apit
6969
{
7070
pointer_valuet() = delete;
7171
pointer_valuet(
72-
const std::string &address,
73-
const std::string &pointee,
74-
const std::string &character,
75-
const optionalt<std::string> &string)
76-
: address(address), pointee(pointee), character(character), string(string)
72+
const std::string &address = "",
73+
const std::string &pointee = "",
74+
const std::string &character = "",
75+
const optionalt<std::string> &string = nullopt,
76+
const bool valid = false)
77+
: address(address),
78+
pointee(pointee),
79+
character(character),
80+
string(string),
81+
valid(valid)
7782
{
7883
}
7984

8085
const memory_addresst address;
8186
const std::string pointee;
8287
const std::string character;
8388
const optionalt<std::string> string;
89+
bool valid;
8490
};
8591

8692
/// Get the allocated size estimate for a pointer
@@ -103,17 +109,12 @@ class gdb_apit
103109
/// \param corefile: core dump
104110
void run_gdb_from_core(const std::string &corefile);
105111

106-
/// Get value of the given value expression
107-
/// \param expr: an expression of non-pointer type or pointer to char
108-
/// \return value of the expression; if the expression is of type pointer to
109-
/// char and represents a string, the string value is returned; otherwise
110-
/// the value is returned just as it is printed by gdb
111-
std::string get_value(const std::string &expr);
112-
113112
/// Get the memory address pointed to by the given pointer expression
114113
/// \param expr: an expression of pointer type (e.g., `&x` with `x` being of
115114
/// type `int` or `p` with `p` being of type `int *`)
116115
/// \return memory address in hex format
116+
optionalt<std::string> get_value(const std::string &expr);
117+
117118
pointer_valuet get_memory(const std::string &expr);
118119

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

0 commit comments

Comments
 (0)