cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
memory_analyzer_parse_options.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: Memory Analyzer
4
5
Author: Malte Mues <mail.mues@gmail.com>
6
Daniel Poetzl
7
8
\*******************************************************************/
9
12
13
#include "
memory_analyzer_parse_options.h
"
14
15
#include <
util/config.h
>
16
#include <
util/exit_codes.h
>
17
#include <
util/help_formatter.h
>
18
#include <
util/message.h
>
19
#include <
util/version.h
>
20
21
#include <
goto-programs/goto_model.h
>
22
#include <
goto-programs/read_goto_binary.h
>
23
#include <
goto-programs/show_symbol_table.h
>
24
25
#include <
ansi-c/ansi_c_language.h
>
26
#include <
langapi/mode.h
>
27
28
#include "
analyze_symbol.h
"
29
30
#include <fstream>
// IWYU pragma: keep
31
#include <iostream>
32
33
memory_analyzer_parse_optionst::memory_analyzer_parse_optionst
(
34
int
argc,
35
const
char
*argv[])
36
:
parse_options_baset
(
37
MEMORY_ANALYZER_OPTIONS
,
38
argc,
39
argv,
40
std
::string(
"MEMORY-ANALYZER "
) +
CBMC_VERSION
),
41
message
(
ui_message_handler
)
42
{
43
}
44
45
void
memory_analyzer_parse_optionst::register_languages
()
46
{
47
// For now only C is supported due to the additional challenges of
48
// mapping source code to debugging symbols in other languages.
49
register_language
(
new_ansi_c_language
);
50
}
51
52
int
memory_analyzer_parse_optionst::doit
()
53
{
54
if
(
cmdline
.isset(
"version"
))
55
{
56
std::cout <<
CBMC_VERSION
<<
'\n'
;
57
return
CPROVER_EXIT_SUCCESS
;
58
}
59
60
config
.set(
cmdline
);
61
62
if
(
cmdline
.args.size() < 1)
63
{
64
throw
invalid_command_line_argument_exceptiont
(
65
"no binary provided for analysis"
,
"<binary> <args>"
);
66
}
67
68
if
(!
cmdline
.isset(
"symbols"
))
69
{
70
throw
invalid_command_line_argument_exceptiont
(
71
"need to provide symbols to analyse via --symbols"
,
"--symbols"
);
72
}
73
74
const
bool
core_file =
cmdline
.isset(
"core-file"
);
75
const
bool
breakpoint =
cmdline
.isset(
"breakpoint"
);
76
77
if
(core_file && breakpoint)
78
{
79
throw
invalid_command_line_argument_exceptiont
(
80
"cannot start gdb from both core-file and breakpoint"
,
81
"--core-file/--breakpoint"
);
82
}
83
84
if
(!core_file && !breakpoint)
85
{
86
throw
invalid_command_line_argument_exceptiont
(
87
"need to provide either core-file or breakpoint for gdb"
,
88
"--core-file/--breakpoint"
);
89
}
90
91
const
bool
output_file =
cmdline
.isset(
"output-file"
);
92
const
bool
symtab_snapshot =
cmdline
.isset(
"symtab-snapshot"
);
93
94
if
(symtab_snapshot && output_file)
95
{
96
throw
invalid_command_line_argument_exceptiont
(
97
"printing to a file is not supported for symbol table snapshot output"
,
98
"--symtab-snapshot"
);
99
}
100
101
register_languages
();
102
103
std::string
binary
=
cmdline
.args.front();
104
105
auto
opt =
read_goto_binary
(
binary
,
ui_message_handler
);
106
107
if
(!opt.has_value())
108
{
109
throw
deserialization_exceptiont
(
110
"cannot read goto binary '"
+
binary
+
"'"
);
111
}
112
113
const
goto_modelt
goto_model(std::move(opt.value()));
114
115
gdb_value_extractort
gdb_value_extractor(
116
goto_model.
symbol_table
,
cmdline
.args);
117
gdb_value_extractor.
create_gdb_process
();
118
119
if
(core_file)
120
{
121
std::string core_file =
cmdline
.get_value(
"core-file"
);
122
gdb_value_extractor.
run_gdb_from_core
(core_file);
123
}
124
else
if
(breakpoint)
125
{
126
std::string breakpoint =
cmdline
.get_value(
"breakpoint"
);
127
gdb_value_extractor.
run_gdb_to_breakpoint
(breakpoint);
128
}
129
130
gdb_value_extractor.
analyze_symbols
(
131
cmdline
.get_comma_separated_values(
"symbols"
));
132
133
std::ofstream
file
;
134
135
if
(output_file)
136
{
137
file
.open(
cmdline
.get_value(
"output-file"
));
138
}
139
140
std::ostream &out =
141
output_file ? (std::ostream &)
file
: (std::ostream &)
message
.result();
142
143
if
(symtab_snapshot)
144
{
145
symbol_tablet
snapshot = gdb_value_extractor.
get_snapshot_as_symbol_table
();
146
show_symbol_table
(snapshot,
ui_message_handler
);
147
}
148
else
149
{
150
std::string snapshot = gdb_value_extractor.
get_snapshot_as_c_code
();
151
out << snapshot;
152
}
153
154
if
(output_file)
155
{
156
file
.close();
157
}
158
else
159
{
160
message
.result() <<
messaget::eom
;
161
}
162
163
return
CPROVER_EXIT_SUCCESS
;
164
}
165
166
void
memory_analyzer_parse_optionst::help
()
167
{
168
std::cout <<
'\n'
169
<<
banner_string
(
"Memory-Analyzer"
,
CBMC_VERSION
) <<
'\n'
170
<<
align_center_with_border
(
"Copyright (C) 2019"
) <<
'\n'
171
<<
align_center_with_border
(
"Malte Mues, Diffblue Ltd."
) <<
'\n'
172
<<
align_center_with_border
(
"info@diffblue.com"
) <<
'\n'
;
173
174
// clang-format off
175
std::cout <<
help_formatter
(
176
"\n"
177
"Usage: \tPurpose:\n"
178
"\n"
179
" {bmemory-analyzer} [{y-?}] [{y-h}] [{y--help}] \t show this help\n"
180
" {bmemory-analyzer} {y--version} \t show version\n"
181
" {bmemory-analyzer} [options] {y--symbols} {usymbol-list} {ubinary} \t"
182
" analyze {ubinary}\n"
183
"\n"
184
"Main options:\n"
185
" {y--core-file} {ufile_name} \t analyze from core file {ufile_name}\n"
186
" {y--breakpoint} {uname} \t analyze from breakpoint {uname}\n"
187
" {y--symbols} {usymbol-list} \t list of symbols to analyze\n"
188
" {y--symtab-snapshot} \t output snapshot as symbol table\n"
189
" {y--output-file} {ufile_name} \t write snapshot to file {ufile_name}\n"
190
" {y--json-ui} \t output snapshot in JSON format\n"
191
"\n"
);
192
// clang-format on
193
}
analyze_symbol.h
High-level interface to gdb.
new_ansi_c_language
std::unique_ptr< languaget > new_ansi_c_language()
Definition
ansi_c_language.cpp:155
ansi_c_language.h
config
configt config
Definition
config.cpp:25
deserialization_exceptiont
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
Definition
exception_utils.h:80
gdb_value_extractort
Interface for extracting values from GDB (building on gdb_apit).
Definition
analyze_symbol.h:33
gdb_value_extractort::run_gdb_to_breakpoint
bool run_gdb_to_breakpoint(const std::string &breakpoint)
Definition
analyze_symbol.h:64
gdb_value_extractort::run_gdb_from_core
void run_gdb_from_core(const std::string &corefile)
Definition
analyze_symbol.h:68
gdb_value_extractort::get_snapshot_as_c_code
std::string get_snapshot_as_c_code()
Get memory snapshot as C code.
Definition
analyze_symbol.cpp:174
gdb_value_extractort::analyze_symbols
void analyze_symbols(const std::list< std::string > &symbols)
For each input symbol in symbols: map its value address to its symbol_exprt (via the values map) and ...
Definition
analyze_symbol.cpp:109
gdb_value_extractort::create_gdb_process
void create_gdb_process()
Definition
analyze_symbol.h:60
gdb_value_extractort::get_snapshot_as_symbol_table
symbol_tablet get_snapshot_as_symbol_table()
Get memory snapshot as symbol table Build a new symbol_tablet and for each lhs symbol from collected ...
Definition
analyze_symbol.cpp:189
goto_modelt
Definition
goto_model.h:27
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition
goto_model.h:31
invalid_command_line_argument_exceptiont
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
Definition
exception_utils.h:51
memory_analyzer_parse_optionst::help
void help() override
Definition
memory_analyzer_parse_options.cpp:166
memory_analyzer_parse_optionst::doit
int doit() override
Definition
memory_analyzer_parse_options.cpp:52
memory_analyzer_parse_optionst::memory_analyzer_parse_optionst
memory_analyzer_parse_optionst(int argc, const char *argv[])
Definition
memory_analyzer_parse_options.cpp:33
memory_analyzer_parse_optionst::register_languages
void register_languages() override
Definition
memory_analyzer_parse_options.cpp:45
memory_analyzer_parse_optionst::message
messaget message
Definition
memory_analyzer_parse_options.h:39
messaget::eom
static eomt eom
Definition
message.h:289
parse_options_baset::cmdline
cmdlinet cmdline
Definition
parse_options.h:28
parse_options_baset::parse_options_baset
parse_options_baset(const std::string &optstring, int argc, const char **argv, const std::string &program)
Definition
parse_options.cpp:28
parse_options_baset::ui_message_handler
ui_message_handlert ui_message_handler
Definition
parse_options.h:45
symbol_tablet
The symbol table.
Definition
symbol_table.h:14
config.h
exit_codes.h
Document and give macros for the exit codes of CPROVER binaries.
CPROVER_EXIT_SUCCESS
#define CPROVER_EXIT_SUCCESS
Success indicates the required analysis has been performed without error.
Definition
exit_codes.h:16
goto_model.h
Symbol Table + CFG.
help_formatter.h
Help Formatter.
help_formatter
static help_formattert help_formatter(const std::string &s)
Definition
help_formatter.h:40
binary
static std::string binary(const constant_exprt &src)
Definition
json_expr.cpp:185
memory_analyzer_parse_options.h
This code does the command line parsing for the memory-analyzer tool.
MEMORY_ANALYZER_OPTIONS
#define MEMORY_ANALYZER_OPTIONS
Definition
memory_analyzer_parse_options.h:20
register_language
void register_language(language_factoryt factory)
Register a language Note: registering a language is required for using the functions in language_util...
Definition
mode.cpp:39
mode.h
std
STL namespace.
align_center_with_border
std::string align_center_with_border(const std::string &text)
Utility for displaying help centered messages borderered by "* *".
Definition
parse_options.cpp:158
banner_string
std::string banner_string(const std::string &front_end, const std::string &version)
Definition
parse_options.cpp:171
read_goto_binary
static bool read_goto_binary(const std::string &filename, symbol_tablet &, goto_functionst &, message_handlert &)
Read a goto binary from a file, but do not update config.
Definition
read_goto_binary.cpp:58
read_goto_binary.h
Read Goto Programs.
show_symbol_table
void show_symbol_table(const symbol_table_baset &symbol_table, ui_message_handlert &ui)
Definition
show_symbol_table.cpp:241
show_symbol_table.h
Show the symbol table.
message.h
file
Definition
kdev_t.h:19
version.h
CBMC_VERSION
const char * CBMC_VERSION
memory-analyzer
memory_analyzer_parse_options.cpp
Generated by
1.17.0