CBMC
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 
14 
15 #include <util/config.h>
16 #include <util/exit_codes.h>
17 #include <util/message.h>
18 #include <util/version.h>
19 
23 
24 #include <ansi-c/ansi_c_language.h>
25 #include <langapi/mode.h>
26 
27 #include "analyze_symbol.h"
28 
29 #include <algorithm>
30 #include <fstream>
31 #include <iostream>
32 
34  int argc,
35  const char *argv[])
38  argc,
39  argv,
40  std::string("MEMORY-ANALYZER ") + CBMC_VERSION),
41  message(ui_message_handler)
42 {
43 }
44 
46 {
47  // For now only C is supported due to the additional challenges of
48  // mapping source code to debugging symbols in other languages.
50 }
51 
53 {
54  if(cmdline.isset("version"))
55  {
56  std::cout << CBMC_VERSION << '\n';
57  return CPROVER_EXIT_SUCCESS;
58  }
59 
61 
62  if(cmdline.args.size() < 1)
63  {
65  "no binary provided for analysis", "<binary> <args>");
66  }
67 
68  if(!cmdline.isset("symbols"))
69  {
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  {
80  "cannot start gdb from both core-file and breakpoint",
81  "--core-file/--breakpoint");
82  }
83 
84  if(!core_file && !breakpoint)
85  {
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  {
97  "printing to a file is not supported for symbol table snapshot output",
98  "--symtab-snapshot");
99  }
100 
102 
103  std::string binary = cmdline.args.front();
104 
106 
107  if(!opt.has_value())
108  {
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(
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();
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  {
161  }
162 
163  return CPROVER_EXIT_SUCCESS;
164 }
165 
167 {
168  std::cout
169  << '\n'
170  << banner_string("Memory-Analyzer", CBMC_VERSION) << '\n'
171  << align_center_with_border("Copyright (C) 2019") << '\n'
172  << align_center_with_border("Malte Mues, Diffblue Ltd.") << '\n'
173  << align_center_with_border("info@diffblue.com") << '\n'
174  << '\n'
175  << "Usage: Purpose:\n"
176  << '\n'
177  << " memory-analyzer [-?] [-h] [--help] show help\n"
178  << " memory-analyzer --version show"
179  << " version\n"
180  << " memory-analyzer --symbols <symbol-list> <options> <binary> analyze"
181  << " binary\n"
182  << "\n"
183  << " --core-file <file> analyze from core file\n"
184  << " --breakpoint <breakpoint> analyze from breakpoint\n"
185  << " --symbols <symbol-list> list of symbols to analyze\n"
186  << " --symtab-snapshot output snapshot as symbol table\n"
187  << " --output-file <file> write snapshot to file\n"
188  << " --json-ui output snapshot in JSON format\n"
189  << '\n';
190 }
cmdlinet::args
argst args
Definition: cmdline.h:145
symbol_tablet
The symbol table.
Definition: symbol_table.h:13
parse_options_baset::ui_message_handler
ui_message_handlert ui_message_handler
Definition: parse_options.h:45
parse_options_baset
Definition: parse_options.h:19
cmdlinet::isset
virtual bool isset(char option) const
Definition: cmdline.cpp:30
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
deserialization_exceptiont
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
Definition: exception_utils.h:79
memory_analyzer_parse_optionst::message
messaget message
Definition: memory_analyzer_parse_options.h:39
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
goto_model.h
goto_modelt
Definition: goto_model.h:25
mode.h
messaget::eom
static eomt eom
Definition: message.h:297
show_symbol_table
void show_symbol_table(const symbol_tablet &symbol_table, ui_message_handlert &ui)
Definition: show_symbol_table.cpp:242
version.h
cmdlinet::get_comma_separated_values
std::list< std::string > get_comma_separated_values(const char *option) const
Collect all occurrences of option option and split their values on each comma, merging them into a si...
Definition: cmdline.cpp:121
gdb_value_extractort::create_gdb_process
void create_gdb_process()
Definition: analyze_symbol.h:64
CBMC_VERSION
const char * CBMC_VERSION
messaget::result
mstreamt & result() const
Definition: message.h:409
banner_string
std::string banner_string(const std::string &front_end, const std::string &version)
Definition: parse_options.cpp:174
gdb_value_extractort
Interface for extracting values from GDB (building on gdb_apit)
Definition: analyze_symbol.h:36
cmdlinet::get_value
std::string get_value(char option) const
Definition: cmdline.cpp:48
show_symbol_table.h
gdb_value_extractort::run_gdb_to_breakpoint
bool run_gdb_to_breakpoint(const std::string &breakpoint)
Definition: analyze_symbol.h:68
read_goto_binary.h
analyze_symbol.h
config
configt config
Definition: config.cpp:25
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
MEMORY_ANALYZER_OPTIONS
#define MEMORY_ANALYZER_OPTIONS
Definition: memory_analyzer_parse_options.h:20
ansi_c_language.h
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
configt::set
bool set(const cmdlinet &cmdline)
Definition: config.cpp:798
exit_codes.h
memory_analyzer_parse_optionst::help
void help() override
Definition: memory_analyzer_parse_options.cpp:166
new_ansi_c_language
std::unique_ptr< languaget > new_ansi_c_language()
Definition: ansi_c_language.cpp:150
binary
static std::string binary(const constant_exprt &src)
Definition: json_expr.cpp:189
config.h
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:61
memory_analyzer_parse_options.h
CPROVER_EXIT_SUCCESS
#define CPROVER_EXIT_SUCCESS
Success indicates the required analysis has been performed without error.
Definition: exit_codes.h:16
message.h
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:50
parse_options_baset::cmdline
cmdlinet cmdline
Definition: parse_options.h:28
goto_modelt::symbol_table
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:30
memory_analyzer_parse_optionst::register_languages
void register_languages() override
Definition: memory_analyzer_parse_options.cpp:45
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
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:161
gdb_value_extractort::run_gdb_from_core
void run_gdb_from_core(const std::string &corefile)
Definition: analyze_symbol.h:72