Skip to content

Commit 0e72433

Browse files
author
Daniel Kroening
authored
Merge pull request diffblue#2516 from polgreen/cbmc_trace_options
extend plain trace to show function calls and returns
2 parents 2e90035 + c98c9df commit 0e72433

File tree

6 files changed

+124
-10
lines changed

6 files changed

+124
-10
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
int function_to_call(int a)
2+
{
3+
int local = 1;
4+
return a+local;
5+
}
6+
7+
8+
int main()
9+
{
10+
int a, c;
11+
unsigned int b;
12+
a = 0;
13+
c = -100;
14+
a = function_to_call(a) + function_to_call(c);
15+
if(a < 0)
16+
b = function_to_call(b);
17+
18+
if(c < 0)
19+
b = -function_to_call(b);
20+
21+
__CPROVER_assert(0,"");
22+
23+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CORE
2+
main.c
3+
--trace --trace-show-code
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
local = 1
7+
int a
8+
int c
9+
unsigned int b
10+
a = 0;
11+
c = -100;
12+
function_to_call\(a\)
13+
function_to_call\(c\)
14+
a = return_value_function_to_call \+ return_value_function_to_call
15+
function_to_call\(\(signed int\)b\)
16+
b = \(unsigned int\)return_value_function_to_call
17+
^VERIFICATION FAILED$
18+
--
19+
^warning: ignoring
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int function_to_call(int a)
2+
{
3+
int local = 1;
4+
return a+local;
5+
}
6+
7+
8+
int main()
9+
{
10+
int a;
11+
unsigned int b;
12+
a = 0;
13+
a = -100;
14+
a = 2147483647;
15+
b = a*2;
16+
a = -2147483647;
17+
a = function_to_call(a);
18+
b = function_to_call(b);
19+
20+
__CPROVER_assert(0,"");
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--trace --trace-show-function-calls
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
Function call: function_to_call \(depth 2\)
7+
Function return: function_to_call \(depth 1\)
8+
Function call: function_to_call \(depth 2\)
9+
^VERIFICATION FAILED$
10+
--
11+
^warning: ignoring

src/goto-programs/goto_trace.cpp

+34-9
Original file line numberDiff line numberDiff line change
@@ -247,20 +247,30 @@ void trace_value(
247247

248248
void show_state_header(
249249
std::ostream &out,
250+
const namespacet &ns,
250251
const goto_trace_stept &state,
251252
const source_locationt &source_location,
252-
unsigned step_nr)
253+
unsigned step_nr,
254+
const trace_optionst &options)
253255
{
254256
out << "\n";
255257

256-
if(step_nr==0)
258+
if(step_nr == 0)
257259
out << "Initial State";
258260
else
259261
out << "State " << step_nr;
260262

261-
out << " " << source_location
262-
<< " thread " << state.thread_nr << "\n";
263-
out << "----------------------------------------------------" << "\n";
263+
out << " " << source_location << " thread " << state.thread_nr << "\n";
264+
out << "----------------------------------------------------"
265+
<< "\n";
266+
267+
if(options.show_code)
268+
{
269+
out << as_string(ns, *state.pc)
270+
<< "\n";
271+
out << "----------------------------------------------------"
272+
<< "\n";
273+
}
264274
}
265275

266276
bool is_index_member_symbol(const exprt &src)
@@ -283,6 +293,7 @@ void show_goto_trace(
283293
{
284294
unsigned prev_step_nr=0;
285295
bool first_step=true;
296+
std::size_t function_depth=0;
286297

287298
for(const auto &step : goto_trace.steps)
288299
{
@@ -341,7 +352,8 @@ void show_goto_trace(
341352
{
342353
first_step=false;
343354
prev_step_nr=step.step_nr;
344-
show_state_header(out, step, step.pc->source_location, step.step_nr);
355+
show_state_header(
356+
out, ns, step, step.pc->source_location, step.step_nr, options);
345357
}
346358

347359
// see if the full lhs is something clean
@@ -369,7 +381,8 @@ void show_goto_trace(
369381
{
370382
first_step=false;
371383
prev_step_nr=step.step_nr;
372-
show_state_header(out, step, step.pc->source_location, step.step_nr);
384+
show_state_header(
385+
out, ns, step, step.pc->source_location, step.step_nr, options);
373386
}
374387

375388
trace_value(
@@ -386,7 +399,8 @@ void show_goto_trace(
386399
}
387400
else
388401
{
389-
show_state_header(out, step, step.pc->source_location, step.step_nr);
402+
show_state_header(
403+
out, ns, step, step.pc->source_location, step.step_nr, options);
390404
out << " OUTPUT " << step.io_id << ":";
391405

392406
for(std::list<exprt>::const_iterator
@@ -407,7 +421,8 @@ void show_goto_trace(
407421
break;
408422

409423
case goto_trace_stept::typet::INPUT:
410-
show_state_header(out, step, step.pc->source_location, step.step_nr);
424+
show_state_header(
425+
out, ns, step, step.pc->source_location, step.step_nr, options);
411426
out << " INPUT " << step.io_id << ":";
412427

413428
for(std::list<exprt>::const_iterator
@@ -427,7 +442,17 @@ void show_goto_trace(
427442
break;
428443

429444
case goto_trace_stept::typet::FUNCTION_CALL:
445+
function_depth++;
446+
if(options.show_function_calls)
447+
out << "\n#### Function call: " << step.identifier << " (depth "
448+
<< function_depth << ") ####\n";
449+
break;
430450
case goto_trace_stept::typet::FUNCTION_RETURN:
451+
function_depth--;
452+
if(options.show_function_calls)
453+
out << "\n#### Function return: " << step.identifier << " (depth "
454+
<< function_depth << ") ####\n";
455+
break;
431456
case goto_trace_stept::typet::SPAWN:
432457
case goto_trace_stept::typet::MEMORY_BARRIER:
433458
case goto_trace_stept::typet::ATOMIC_BEGIN:

src/goto-programs/goto_trace.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ struct trace_optionst
200200
bool json_full_lhs;
201201
bool hex_representation;
202202
bool base_prefix;
203+
bool show_function_calls;
204+
bool show_code;
203205

204206
static const trace_optionst default_options;
205207

@@ -208,6 +210,8 @@ struct trace_optionst
208210
json_full_lhs = options.get_bool_option("trace-json-extended");
209211
hex_representation = options.get_bool_option("trace-hex");
210212
base_prefix = hex_representation;
213+
show_function_calls = options.get_bool_option("trace-show-function-calls");
214+
show_code = options.get_bool_option("trace-show-code");
211215
};
212216

213217
private:
@@ -216,6 +220,8 @@ struct trace_optionst
216220
json_full_lhs = false;
217221
hex_representation = false;
218222
base_prefix = false;
223+
show_function_calls = false;
224+
show_code = false;
219225
};
220226
};
221227

@@ -239,15 +245,23 @@ void trace_value(
239245

240246

241247
#define OPT_GOTO_TRACE "(trace-json-extended)" \
248+
"(trace-show-function-calls)" \
249+
"(trace-show-code)" \
242250
"(trace-hex)"
243251

244252
#define HELP_GOTO_TRACE \
245-
" --trace-json-extended add rawLhs property to trace\n" \
253+
" --trace-json-extended add rawLhs property to trace\n" \
254+
" --trace-show-function-calls show function calls in plain trace\n" \
255+
" --trace-show-code show original code in plain trace\n" \
246256
" --trace-hex represent plain trace values in hex\n"
247257

248258
#define PARSE_OPTIONS_GOTO_TRACE(cmdline, options) \
249259
if(cmdline.isset("trace-json-extended")) \
250260
options.set_option("trace-json-extended", true); \
261+
if(cmdline.isset("trace-show-function-calls")) \
262+
options.set_option("trace-show-function-calls", true); \
263+
if(cmdline.isset("trace-show-code")) \
264+
options.set_option("trace-show-code", true); \
251265
if(cmdline.isset("trace-hex")) \
252266
options.set_option("trace-hex", true);
253267

0 commit comments

Comments
 (0)