Skip to content

Commit 6565c4b

Browse files
petr-bauchdanpoe
authored andcommitted
Improve memory_analyzer parser and main
Remove ui_message_handler dependence, fix header, etc.
1 parent 554a166 commit 6565c4b

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

src/memory-analyzer/memory_analyzer_main.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
// Copyright 2018 Author: Malte Mues <[email protected]>
1+
/*******************************************************************\
22
3+
Module: Symbol Analyzer
4+
5+
Author: Malte Mues <[email protected]>
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Memory analyzer interface
311

412
#include "memory_analyzer_parse_options.h"
513

src/memory-analyzer/memory_analyzer_parse_options.cpp

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Author: Malte Mues <[email protected]>
1414
#include "analyze_symbol.h"
1515
#include "gdb_api.h"
1616

17+
#include <algorithm>
1718
#include <fstream>
1819

1920
#include <ansi-c/ansi_c_language.h>
@@ -67,10 +68,18 @@ int memory_analyzer_parse_optionst::doit()
6768
const bool core_file = cmdline.isset("core-file");
6869
const bool breakpoint = cmdline.isset("breakpoint");
6970

70-
if(!(core_file ^ breakpoint))
71+
if(core_file && breakpoint)
7172
{
7273
throw invalid_command_line_argument_exceptiont(
73-
"need to provide either option --core-file or option --breakpoint", "");
74+
"cannot start gdb from both core-file and breakpoint",
75+
"--core-file/--breakpoint");
76+
}
77+
78+
if(!core_file && !breakpoint)
79+
{
80+
throw invalid_command_line_argument_exceptiont(
81+
"need to provide either core-file or breakpoint for gdb",
82+
"--core-file/--breakpoint");
7483
}
7584

7685
const bool output_file = cmdline.isset("output-file");
@@ -87,20 +96,6 @@ int memory_analyzer_parse_optionst::doit()
8796

8897
std::string binary = cmdline.args.front();
8998

90-
gdb_apit gdb_api(binary.c_str());
91-
gdb_api.create_gdb_process();
92-
93-
if(core_file)
94-
{
95-
std::string core_file = cmdline.get_value("core-file");
96-
gdb_api.run_gdb_from_core(core_file);
97-
}
98-
else if(breakpoint)
99-
{
100-
std::string breakpoint = cmdline.get_value("breakpoint");
101-
gdb_api.run_gdb_to_breakpoint(breakpoint);
102-
}
103-
10499
const std::string symbol_list(cmdline.get_value("symbols"));
105100
std::vector<std::string> result;
106101
split_string(symbol_list, ',', result, true, true);
@@ -110,14 +105,32 @@ int memory_analyzer_parse_optionst::doit()
110105
if(!opt.has_value())
111106
{
112107
throw deserialization_exceptiont(
113-
"cannot read goto binary `" + binary + "`");
108+
"cannot read goto binary `" + binary + "'");
114109
}
115110

116111
const goto_modelt goto_model(std::move(opt.value()));
117112

118-
symbol_analyzert analyzer(goto_model.symbol_table, gdb_api);
113+
gdb_value_extractort gdb_value_extractor(
114+
goto_model.symbol_table, binary.c_str());
115+
gdb_value_extractor.create_gdb_process();
116+
117+
if(core_file)
118+
{
119+
std::string core_file = cmdline.get_value("core-file");
120+
gdb_value_extractor.run_gdb_from_core(core_file);
121+
}
122+
else if(breakpoint)
123+
{
124+
std::string breakpoint = cmdline.get_value("breakpoint");
125+
gdb_value_extractor.run_gdb_to_breakpoint(breakpoint);
126+
}
119127

120-
analyzer.analyze_symbols(result);
128+
std::vector<irep_idt> result_ids(result.size());
129+
std::transform(
130+
result.begin(), result.end(), result_ids.begin(), [](std::string &name) {
131+
return irep_idt{name};
132+
});
133+
gdb_value_extractor.analyze_symbols(result_ids);
121134

122135
std::ofstream file;
123136

@@ -131,12 +144,12 @@ int memory_analyzer_parse_optionst::doit()
131144

132145
if(symtab_snapshot)
133146
{
134-
symbol_tablet snapshot = analyzer.get_snapshot_as_symbol_table();
147+
symbol_tablet snapshot = gdb_value_extractor.get_snapshot_as_symbol_table();
135148
show_symbol_table(snapshot, ui_message_handler);
136149
}
137150
else
138151
{
139-
std::string snapshot = analyzer.get_snapshot_as_c_code();
152+
std::string snapshot = gdb_value_extractor.get_snapshot_as_c_code();
140153
out << snapshot;
141154
}
142155

@@ -163,14 +176,17 @@ void memory_analyzer_parse_optionst::help()
163176
<< '\n'
164177
<< "Usage: Purpose:\n"
165178
<< '\n'
166-
<< " memory-analyzer [-?] [-h] [--help] show help\n"
167-
<< " memory-analyzer --version show version\n"
168-
<< " memory-analyzer <options> <binary> analyze binary"
179+
<< " memory-analyzer [-?] [-h] [--help] show help\n"
180+
<< " memory-analyzer --version show"
181+
<< " version\n"
182+
<< " memory-analyzer --symbols <symbol-list> <options> <binary> analyze"
183+
<< " binary\n"
169184
<< "\n"
170185
<< " --core-file <file> analyze from core file\n"
171186
<< " --breakpoint <breakpoint> analyze from breakpoint\n"
172187
<< " --symbols <symbol-list> list of symbols to analyze\n"
173188
<< " --symtab-snapshot output snapshot as symbol table\n"
174189
<< " --output-file <file> write snapshot to file\n"
190+
<< " --json-ui output snapshot in JSON format\n"
175191
<< messaget::eom;
176192
}

0 commit comments

Comments
 (0)