Skip to content

Commit 554a166

Browse files
petr-bauchdanpoe
authored andcommitted
Refactor analyze_symbols
and add documentation.
1 parent 2fddd15 commit 554a166

File tree

2 files changed

+151
-81
lines changed

2 files changed

+151
-81
lines changed

src/memory-analyzer/analyze_symbol.cpp

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,36 @@ Author: Malte Mues <[email protected]>
99

1010
#include "analyze_symbol.h"
1111

12-
#include <algorithm>
13-
#include <regex>
14-
15-
#include <iostream>
16-
17-
#include <goto-programs/goto_model.h>
18-
#include <goto-programs/read_goto_binary.h>
19-
20-
#include <util/arith_tools.h>
2112
#include <util/c_types.h>
2213
#include <util/c_types_util.h>
2314
#include <util/config.h>
24-
#include <util/cout_message.h>
25-
#include <util/expr.h>
2615
#include <util/expr_initializer.h>
27-
#include <util/irep.h>
28-
#include <util/mp_arith.h>
29-
#include <util/std_code.h>
30-
#include <util/std_expr.h>
31-
#include <util/std_types.h>
3216
#include <util/string_constant.h>
3317

34-
symbol_analyzert::symbol_analyzert(
18+
gdb_value_extractort::gdb_value_extractort(
3519
const symbol_tablet &symbol_table,
36-
gdb_apit &gdb_api)
37-
: gdb_api(gdb_api),
20+
const char *binary)
21+
: gdb_api(binary),
3822
symbol_table(symbol_table),
3923
ns(symbol_table),
4024
c_converter(ns, expr2c_configurationt::clean_configuration),
41-
allocate_objects(ID_C, source_locationt(), "", this->symbol_table)
25+
allocate_objects(ID_C, source_locationt(), irep_idt{}, this->symbol_table)
4226
{
4327
}
4428

45-
void symbol_analyzert::analyze_symbols(const std::vector<std::string> &symbols)
29+
void gdb_value_extractort::analyze_symbols(const std::vector<irep_idt> &symbols)
4630
{
4731
// record addresses of given symbols
4832
for(const auto &id : symbols)
4933
{
50-
const symbol_exprt symbol_expr(id, typet());
34+
const symbol_exprt &symbol_expr = ns.lookup(id).symbol_expr();
5135
const address_of_exprt aoe(symbol_expr);
5236

5337
const std::string c_expr = c_converter.convert(aoe);
5438
const pointer_valuet &value = gdb_api.get_memory(c_expr);
55-
CHECK_RETURN(value.pointee.empty() || (value.pointee == id));
39+
CHECK_RETURN(value.pointee.empty() || (id == value.pointee));
5640

57-
values.insert(std::make_pair(value.address, symbol_expr));
41+
values.insert({value.address, symbol_expr});
5842
}
5943

6044
for(const auto &id : symbols)
@@ -63,7 +47,7 @@ void symbol_analyzert::analyze_symbols(const std::vector<std::string> &symbols)
6347
}
6448
}
6549

66-
void symbol_analyzert::analyze_symbol(const std::string &symbol_name)
50+
void gdb_value_extractort::analyze_symbol(const irep_idt &symbol_name)
6751
{
6852
const symbolt &symbol = ns.lookup(symbol_name);
6953
const symbol_exprt symbol_expr = symbol.symbol_expr();
@@ -89,7 +73,7 @@ void symbol_analyzert::analyze_symbol(const std::string &symbol_name)
8973
}
9074

9175
/// Get memory snapshot as C code
92-
std::string symbol_analyzert::get_snapshot_as_c_code()
76+
std::string gdb_value_extractort::get_snapshot_as_c_code()
9377
{
9478
code_blockt generated_code;
9579

@@ -104,7 +88,7 @@ std::string symbol_analyzert::get_snapshot_as_c_code()
10488
}
10589

10690
/// Get memory snapshot as symbol table
107-
symbol_tablet symbol_analyzert::get_snapshot_as_symbol_table()
91+
symbol_tablet gdb_value_extractort::get_snapshot_as_symbol_table()
10892
{
10993
symbol_tablet snapshot;
11094

@@ -137,19 +121,19 @@ symbol_tablet symbol_analyzert::get_snapshot_as_symbol_table()
137121
return snapshot;
138122
}
139123

140-
void symbol_analyzert::add_assignment(const exprt &lhs, const exprt &value)
124+
void gdb_value_extractort::add_assignment(const exprt &lhs, const exprt &value)
141125
{
142126
assignments.push_back(std::make_pair(lhs, value));
143127
}
144128

145-
exprt symbol_analyzert::get_char_pointer_value(
129+
exprt gdb_value_extractort::get_char_pointer_value(
146130
const exprt &expr,
147-
const std::string &memory_location,
131+
const memory_addresst &memory_location,
148132
const source_locationt &location)
149133
{
150134
PRECONDITION(expr.type().id() == ID_pointer);
151-
PRECONDITION(is_c_char(expr.type().subtype()));
152-
PRECONDITION(memory_location != "0x0");
135+
PRECONDITION(is_c_char_type(expr.type().subtype()));
136+
PRECONDITION(!memory_location.is_null());
153137

154138
auto it = values.find(memory_location);
155139

@@ -158,12 +142,11 @@ exprt symbol_analyzert::get_char_pointer_value(
158142
std::string c_expr = c_converter.convert(expr);
159143
pointer_valuet value = gdb_api.get_memory(c_expr);
160144
CHECK_RETURN(value.string);
161-
std::string string = *value.string;
162145

163-
string_constantt init(string);
146+
string_constantt init(*value.string);
164147
CHECK_RETURN(to_array_type(init.type()).is_complete());
165148

166-
symbol_exprt dummy(pointer_typet(init.type(), config.ansi_c.pointer_width));
149+
symbol_exprt dummy("tmp", pointer_type(init.type()));
167150
code_blockt assignments;
168151

169152
const symbol_exprt new_symbol =
@@ -182,14 +165,14 @@ exprt symbol_analyzert::get_char_pointer_value(
182165
}
183166
}
184167

185-
exprt symbol_analyzert::get_non_char_pointer_value(
168+
exprt gdb_value_extractort::get_non_char_pointer_value(
186169
const exprt &expr,
187-
const std::string memory_location,
170+
const memory_addresst &memory_location,
188171
const source_locationt &location)
189172
{
190173
PRECONDITION(expr.type().id() == ID_pointer);
191-
PRECONDITION(!is_c_char(expr.type().subtype()));
192-
PRECONDITION(memory_location != "0x0");
174+
PRECONDITION(!is_c_char_type(expr.type().subtype()));
175+
PRECONDITION(!memory_location.is_null());
193176

194177
auto it = values.find(memory_location);
195178

@@ -199,7 +182,7 @@ exprt symbol_analyzert::get_non_char_pointer_value(
199182

200183
const typet target_type = expr.type().subtype();
201184

202-
symbol_exprt dummy(expr.type());
185+
symbol_exprt dummy("tmp", expr.type());
203186
code_blockt assignments;
204187

205188
const symbol_exprt new_symbol =
@@ -227,7 +210,7 @@ exprt symbol_analyzert::get_non_char_pointer_value(
227210
}
228211
}
229212

230-
exprt symbol_analyzert::get_pointer_value(
213+
exprt gdb_value_extractort::get_pointer_value(
231214
const exprt &expr,
232215
const exprt &zero_expr,
233216
const source_locationt &location)
@@ -240,11 +223,11 @@ exprt symbol_analyzert::get_pointer_value(
240223
std::string c_expr = c_converter.convert(expr);
241224
const pointer_valuet value = gdb_api.get_memory(c_expr);
242225

243-
const std::string memory_location = value.address;
226+
const auto memory_location = value.address;
244227

245-
if(memory_location != "0x0")
228+
if(!memory_location.is_null())
246229
{
247-
if(is_c_char(expr.type().subtype()))
230+
if(is_c_char_type(expr.type().subtype()))
248231
{
249232
return get_char_pointer_value(expr, memory_location, location);
250233
}
@@ -267,7 +250,7 @@ exprt symbol_analyzert::get_pointer_value(
267250
return zero_expr;
268251
}
269252

270-
exprt symbol_analyzert::get_array_value(
253+
exprt gdb_value_extractort::get_array_value(
271254
const exprt &expr,
272255
const exprt &array,
273256
const source_locationt &location)
@@ -291,27 +274,23 @@ exprt symbol_analyzert::get_array_value(
291274
return new_array;
292275
}
293276

294-
exprt symbol_analyzert::get_expr_value(
277+
exprt gdb_value_extractort::get_expr_value(
295278
const exprt &expr,
296279
const exprt &zero_expr,
297280
const source_locationt &location)
298281
{
299282
PRECONDITION(expr.type() == zero_expr.type());
300283

301284
const typet &type = expr.type();
285+
PRECONDITION(type.id() != ID_struct);
302286

303-
if(is_c_int_derivate(type))
287+
if(is_c_integral_type(type))
304288
{
305289
INVARIANT(zero_expr.is_constant(), "zero initializer is a constant");
306290

307-
std::string c_expr = c_converter.convert(expr);
308-
const std::string value = gdb_api.get_value(c_expr);
309-
310-
const mp_integer int_rep = string2integer(value);
311-
312-
return from_integer(int_rep, type);
291+
return from_integer(string2integer(get_gdb_value(expr)), expr.type());
313292
}
314-
else if(is_c_char(type))
293+
else if(is_c_char_type(type))
315294
{
316295
INVARIANT(zero_expr.is_constant(), "zero initializer is a constant");
317296

@@ -321,19 +300,14 @@ exprt symbol_analyzert::get_expr_value(
321300
{
322301
INVARIANT(zero_expr.is_constant(), "zero initializer is a constant");
323302

324-
std::string c_expr = c_converter.convert(expr);
325-
const std::string value = gdb_api.get_value(c_expr);
326-
327-
return from_c_boolean_value(value, type);
303+
return from_c_boolean_value(id2boolean(get_gdb_value(expr)), type);
328304
}
329305
else if(type.id() == ID_c_enum)
330306
{
331307
INVARIANT(zero_expr.is_constant(), "zero initializer is a constant");
332308

333-
std::string c_expr = c_converter.convert(expr);
334-
const std::string value = gdb_api.get_value(c_expr);
335-
336-
return convert_member_name_to_enum_value(value, to_c_enum_type(type));
309+
return convert_member_name_to_enum_value(
310+
get_gdb_value(expr), to_c_enum_type(type));
337311
}
338312
else if(type.id() == ID_struct_tag)
339313
{
@@ -349,15 +323,10 @@ exprt symbol_analyzert::get_expr_value(
349323

350324
return get_pointer_value(expr, zero_expr, location);
351325
}
352-
else
353-
{
354-
throw analysis_exceptiont("unexpected expression:\n" + expr.pretty());
355-
}
356-
357-
return zero_expr;
326+
UNIMPLEMENTED;
358327
}
359328

360-
exprt symbol_analyzert::get_struct_value(
329+
exprt gdb_value_extractort::get_struct_value(
361330
const exprt &expr,
362331
const exprt &zero_expr,
363332
const source_locationt &location)
@@ -370,13 +339,13 @@ exprt symbol_analyzert::get_struct_value(
370339
exprt new_expr(zero_expr);
371340

372341
const struct_tag_typet &struct_tag_type = to_struct_tag_type(expr.type());
373-
const struct_typet struct_type = ns.follow_tag(struct_tag_type);
342+
const struct_typet &struct_type = ns.follow_tag(struct_tag_type);
374343

375344
for(size_t i = 0; i < new_expr.operands().size(); ++i)
376345
{
377346
const struct_typet::componentt &component = struct_type.components()[i];
378347

379-
if(component.get_is_padding())
348+
if(component.get_is_padding() || component.type().id() == ID_code)
380349
{
381350
continue;
382351
}
@@ -390,7 +359,7 @@ exprt symbol_analyzert::get_struct_value(
390359
return new_expr;
391360
}
392361

393-
void symbol_analyzert::process_outstanding_assignments()
362+
void gdb_value_extractort::process_outstanding_assignments()
394363
{
395364
for(const auto &pair : outstanding_assignments)
396365
{
@@ -399,3 +368,7 @@ void symbol_analyzert::process_outstanding_assignments()
399368
}
400369
}
401370

371+
std::string gdb_value_extractort::get_gdb_value(const exprt &expr)
372+
{
373+
return gdb_api.get_value(c_converter.convert(expr));
374+
}

0 commit comments

Comments
 (0)