1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
10 * Baranyi, Botond – initial implementation
12 ******************************************************************************/
14 #include "Debugger.hh"
15 #include "DebugCommands.hh"
16 #include "Communication.hh"
17 #include "../common/pattern.hh"
18 #include "Param_Types.hh"
19 #include "DebuggerUI.hh"
23 //////////////////////////////////////////////////////
24 ////////////////// TTCN3_Debugger ////////////////////
25 //////////////////////////////////////////////////////
27 TTCN3_Debugger ttcn3_debugger
;
29 void TTCN3_Debugger::switch_state(const char* p_state_str
)
31 if (!strcmp(p_state_str
, "on")) {
33 print(DRET_NOTIFICATION
, "The debugger is already switched on.");
37 print(DRET_SETTING_CHANGE
, "Debugger switched on.");
40 else if(!strcmp(p_state_str
, "off")) {
42 print(DRET_NOTIFICATION
, "The debugger is already switched off.");
46 print(DRET_SETTING_CHANGE
, "Debugger switched off.");
50 print(DRET_NOTIFICATION
, "Argument 1 is invalid. Expected 'on' or 'off'.");
54 void TTCN3_Debugger::set_breakpoint(const char* p_module
, int p_line
,
55 const char* batch_file
)
57 size_t pos
= find_breakpoint(p_module
, p_line
);
58 if (pos
== breakpoints
.size()) {
60 bp
.module
= mcopystr(p_module
);
62 bp
.batch_file
= batch_file
!= NULL
? mcopystr(batch_file
) : NULL
;
63 breakpoints
.push_back(bp
);
64 print(DRET_SETTING_CHANGE
, "Breakpoint added in module '%s' at line %d%s%s%s.",
66 batch_file
!= NULL
? " with batch file '" : " with no batch file",
67 batch_file
!= NULL
? batch_file
: "", batch_file
!= NULL
? "'" : "");
70 if (breakpoints
[pos
].batch_file
!= NULL
) {
71 if (batch_file
!= NULL
) {
72 if (!strcmp(batch_file
, breakpoints
[pos
].batch_file
)) {
73 print(DRET_NOTIFICATION
, "Breakpoint already set in module '%s' at "
74 "line %d with batch file '%s'.",
75 p_module
, p_line
, batch_file
);
78 print(DRET_SETTING_CHANGE
, "Batch file was changed from '%s' to '%s' for "
79 "breakpoint in module '%s' at line %d.", breakpoints
[pos
].batch_file
,
80 batch_file
, p_module
, p_line
);
84 print(DRET_SETTING_CHANGE
, "Batch file '%s' removed from breakpoint in "
85 "module '%s' at line %d.", breakpoints
[pos
].batch_file
, p_module
, p_line
);
87 Free(breakpoints
[pos
].batch_file
);
90 if (batch_file
!= NULL
) {
91 print(DRET_SETTING_CHANGE
, "Batch file '%s' added to breakpoint in module "
92 "'%s' at line %d.", batch_file
, p_module
, p_line
);
95 print(DRET_NOTIFICATION
, "Breakpoint already set in module '%s' at line "
96 "%d with no batch file.", p_module
, p_line
);
99 breakpoints
[pos
].batch_file
= batch_file
!= NULL
? mcopystr(batch_file
) : NULL
;
103 void TTCN3_Debugger::remove_breakpoint(const char* p_module
, const char* p_line
)
105 bool all_breakpoints
= !strcmp(p_module
, "all");
106 if (p_line
!= NULL
) {
107 if (!strcmp(p_line
, "all")) {
109 for (size_t i
= breakpoints
.size(); i
> 0; --i
) {
110 if (!strcmp(breakpoints
[i
- 1].module
, p_module
)) {
112 Free(breakpoints
[i
- 1].module
);
113 Free(breakpoints
[i
- 1].batch_file
);
114 breakpoints
.erase_at(i
- 1);
118 print(DRET_SETTING_CHANGE
, "Removed all breakpoints in module '%s'.", p_module
);
121 print(DRET_NOTIFICATION
, "No breakpoints found in module '%s'.", p_module
);
126 if (all_breakpoints
) {
127 print(DRET_NOTIFICATION
, "Unexpected 2nd argument, when the first "
128 "argument is 'all'.");
131 size_t len
= strlen(p_line
);
132 for (size_t i
= 0; i
< len
; ++i
) {
133 if (p_line
[i
] < '0' || p_line
[i
] > '9') {
134 print(DRET_NOTIFICATION
, "Argument 2 is invalid. Expected 'all' or "
135 "integer value (line number).");
139 long line
= strtol(p_line
, NULL
, 10);
140 size_t pos
= find_breakpoint(p_module
, line
);
141 if (pos
!= breakpoints
.size()) {
142 Free(breakpoints
[pos
].module
);
143 Free(breakpoints
[pos
].batch_file
);
144 breakpoints
.erase_at(pos
);
145 print(DRET_SETTING_CHANGE
, "Breakpoint removed in module '%s' from line %d.",
149 print(DRET_NOTIFICATION
, "No breakpoint found in module '%s' at line %d.",
155 else if (!all_breakpoints
) {
156 print(DRET_NOTIFICATION
, "2 arguments expected, when the first argument is "
160 // delete all breakpoints if we got this far
161 if (breakpoints
.empty()) {
162 print(DRET_NOTIFICATION
, "No breakpoints found.");
165 for (size_t i
= 0; i
< breakpoints
.size(); ++i
) {
166 Free(breakpoints
[i
].module
);
167 Free(breakpoints
[i
].batch_file
);
170 print(DRET_SETTING_CHANGE
, "Removed all breakpoints.");
174 void TTCN3_Debugger::set_automatic_breakpoint(const char* p_event_str
,
175 const char* p_state_str
,
176 const char* p_batch_file
)
179 if (!strcmp(p_state_str
, "on")) {
182 else if(!strcmp(p_state_str
, "off")) {
186 print(DRET_NOTIFICATION
, "Argument 2 is invalid. Expected 'on' or 'off'.");
189 const char* sbp_type_str
;
191 char** old_batch_file_ptr
;
192 if (!strcmp(p_event_str
, "fail")) {
193 state_changed
= (fail_behavior
.trigger
!= new_state
);
194 fail_behavior
.trigger
= new_state
;
195 old_batch_file_ptr
= &fail_behavior
.batch_file
;
196 sbp_type_str
= "fail verdict";
198 else if (!strcmp(p_event_str
, "error")) {
199 state_changed
= (error_behavior
.trigger
!= new_state
);
200 error_behavior
.trigger
= new_state
;
201 old_batch_file_ptr
= &error_behavior
.batch_file
;
202 sbp_type_str
= "error verdict";
205 print(DRET_NOTIFICATION
, "Argument 1 is invalid. Expected 'error' or 'fail'.");
209 print(DRET_SETTING_CHANGE
, "Automatic breakpoint at %s switched %s%s%s%s.",
210 sbp_type_str
, new_state
? "on" : "off",
211 new_state
? (p_batch_file
!= NULL
? " with batch file '" : " with no batch file") : "",
212 (p_batch_file
!= NULL
&& new_state
) ? p_batch_file
: "",
213 (p_batch_file
!= NULL
&& new_state
) ? "'" : "");
217 if (*old_batch_file_ptr
!= NULL
) {
218 if (p_batch_file
!= NULL
) {
219 if (!strcmp(p_batch_file
, *old_batch_file_ptr
)) {
220 print(DRET_NOTIFICATION
, "Automatic breakpoint at %s was already "
221 "switched on with batch file '%s'.", sbp_type_str
, p_batch_file
);
224 print(DRET_SETTING_CHANGE
, "Batch file was changed from '%s' to '%s' "
225 "for automatic breakpoint at %s.", *old_batch_file_ptr
, p_batch_file
,
230 print(DRET_SETTING_CHANGE
, "Batch file '%s' removed from automatic "
231 "breakpoint at %s.", *old_batch_file_ptr
, sbp_type_str
);
235 if (p_batch_file
!= NULL
) {
236 print(DRET_SETTING_CHANGE
, "Batch file '%s' added to automatic breakpoint "
237 "at %s.", p_batch_file
, sbp_type_str
);
240 print(DRET_NOTIFICATION
, "Automatic breakpoint at %s was already "
241 "switched on with no batch file.", sbp_type_str
);
246 print(DRET_NOTIFICATION
, "Automatic breakpoint at %s was already switched off.");
249 Free(*old_batch_file_ptr
);
250 *old_batch_file_ptr
= p_batch_file
!= NULL
? mcopystr(p_batch_file
) : NULL
;
253 void TTCN3_Debugger::print_call_stack()
255 for (size_t i
= call_stack
.size(); i
!= 0; --i
) {
256 add_to_result("%d.\t", (int)call_stack
.size() - (int)i
+ 1);
257 call_stack
[i
- 1].function
->print_function();
264 void TTCN3_Debugger::set_stack_level(int new_level
)
267 print(DRET_NOTIFICATION
, "Stack level can only be set if test execution is halted.");
269 else if (new_level
<= 0 || (size_t)new_level
> call_stack
.size()) {
270 print(DRET_NOTIFICATION
, "Invalid new stack level. Expected 1 - %d.",
271 (int)call_stack
.size());
274 stack_level
= (int)call_stack
.size() - new_level
;
275 call_stack
[stack_level
].function
->print_function();
276 print(DRET_NOTIFICATION
, "Stack level set to:\n%d.\t%s", new_level
, command_result
);
277 Free(command_result
);
278 command_result
= NULL
;
282 #define STACK_LEVEL (stack_level >= 0) ? (size_t)stack_level : (call_stack.size() - 1)
284 void TTCN3_Debugger::print_variable(const char* p_var_name
)
286 const variable_t
* var
= call_stack
[STACK_LEVEL
].function
->find_variable(p_var_name
);
288 add_to_result("[%s] %s := %s", var
->type_name
, var
->name
,
289 (const char*)var
->print_function(*var
));
292 add_to_result("Variable '%s' not found.", p_var_name
);
296 void TTCN3_Debugger::overwrite_variable(const char* p_var_name
,
297 int p_value_element_count
,
298 char** p_value_elements
)
300 variable_t
* var
= call_stack
[STACK_LEVEL
].function
->find_variable(p_var_name
);
302 if (var
->set_function
== NULL
) {
303 print(DRET_NOTIFICATION
, "Constant variables cannot be overwritten.");
306 char* new_value_str
= NULL
;
307 for (int i
= 0; i
< p_value_element_count
; ++i
) {
309 new_value_str
= mputc(new_value_str
, ' ');
311 new_value_str
= mputstr(new_value_str
, p_value_elements
[i
]);
313 Module_Param
* parsed_mp
= process_config_debugger_value(new_value_str
);
314 // an error message has already been displayed if parsed_mp is NULL
315 if (parsed_mp
!= NULL
) {
317 Debugger_Value_Parsing debug_value_parsing
;
318 boolean handled
= var
->set_function(*var
, *parsed_mp
);
320 print(DRET_NOTIFICATION
, "Variables of type '%s' cannot be overwritten.",
324 add_to_result("[%s] %s := %s", var
->type_name
, var
->name
,
325 (const char*)var
->print_function(*var
));
328 catch (const TC_Error
&) {
329 // do nothing, an error message has already been displayed in this case
336 print(DRET_NOTIFICATION
, "Variable '%s' not found.", p_var_name
);
340 void TTCN3_Debugger::set_output(const char* p_output_type
, const char* p_file_name
)
344 bool same_file
= false;
345 char* final_file_name
= NULL
;
346 // check the command's parameters before actually changing anything
347 if (!strcmp(p_output_type
, "console")) {
351 else if (!strcmp(p_output_type
, "file")) {
355 else if (!strcmp(p_output_type
, "both")) {
360 print(DRET_NOTIFICATION
, "Argument 1 is invalid. Expected 'console', 'file' or 'both'.");
364 if (p_file_name
== NULL
) {
365 print(DRET_NOTIFICATION
, "Argument 2 (output file name) is missing.");
368 if (output_file_name
!= NULL
&& !strcmp(p_file_name
, output_file_name
)) {
369 // don't reopen it if it's the same file as before
372 else if (!TTCN_Runtime::is_hc()) {
373 // don't open any files on HCs, just store settings for future PTCs
374 final_file_name
= finalize_file_name(p_file_name
);
375 new_fp
= fopen(final_file_name
, TTCN_Runtime::is_mtc() ? "w" : "a");
376 if (new_fp
== NULL
) {
377 print(DRET_NOTIFICATION
, "Failed to open file '%s' for writing.", final_file_name
);
378 Free(final_file_name
);
383 // print the change notification to the old output
384 char* file_str
= file
? mprintf("file '%s'", final_file_name
) : NULL
;
385 Free(final_file_name
);
386 print(DRET_SETTING_CHANGE
, "Debugger set to print its output to %s%s%s.",
387 console
? "the console" : "", (console
&& file
) ? " and to " : "",
388 file
? file_str
: "");
392 if (!same_file
&& !TTCN_Runtime::is_hc()) {
393 if (output_file
!= NULL
) {
396 output_file
= new_fp
;
398 send_to_console
= console
;
399 Free(output_file_name
);
401 output_file_name
= mcopystr(p_file_name
);
405 void TTCN3_Debugger::set_global_batch_file(const char* p_state_str
,
406 const char* p_file_name
)
408 bool delete_old
= false;
409 bool copy_new
= false;
410 if (!strcmp(p_state_str
, "on")) {
411 if (p_file_name
!= NULL
) {
412 if (global_batch_file
!= NULL
) {
413 if (!strcmp(p_file_name
, global_batch_file
)) {
414 print(DRET_NOTIFICATION
, "Global batch file was already switched on "
415 "and set to '%s'.", p_file_name
);
418 print(DRET_SETTING_CHANGE
, "Global batch file changed from '%s' to '%s'.",
419 global_batch_file
, p_file_name
);
425 print(DRET_SETTING_CHANGE
, "Global batch file switched on and set to '%s'.",
431 print(DRET_NOTIFICATION
, "Missing batch file name argument.");
434 else if (!strcmp(p_state_str
, "off")) {
435 if (global_batch_file
!= NULL
) {
436 print(DRET_SETTING_CHANGE
, "Global batch file switched off.");
440 print(DRET_NOTIFICATION
, "Global batch file was already switched off.");
444 print(DRET_NOTIFICATION
, "Argument 1 is invalid. Expected 'on' or 'off'.");
447 Free(global_batch_file
);
448 global_batch_file
= NULL
;
451 global_batch_file
= mcopystr(p_file_name
);
455 void TTCN3_Debugger::step(stepping_t p_stepping_type
)
458 print(DRET_NOTIFICATION
, "Stepping commands can only be used when test "
459 "execution is halted.");
462 stepping_type
= p_stepping_type
;
463 stepping_stack_size
= call_stack
.size();
464 if (!TTCN_Runtime::is_single()) {
465 TTCN_Communication::send_debug_continue_req();
470 void TTCN3_Debugger::run_to_cursor(const char* p_module
, int p_line
)
472 // all processes receive this command, since the specified location might be
473 // reached in any process, even a PTC that hasn't been created yet
475 print(DRET_NOTIFICATION
, "The 'run to' command can only be used when test "
476 "execution is halted.");
479 temporary_breakpoint
.module
= mcopystr(p_module
);
480 temporary_breakpoint
.line
= p_line
;
484 void TTCN3_Debugger::halt(const char* p_batch_file
, bool p_run_global_batch
)
488 Free(temporary_breakpoint
.module
);
489 temporary_breakpoint
.module
= NULL
;
490 temporary_breakpoint
.line
= 0;
491 if (!TTCN_Runtime::is_hc()) {
492 stepping_type
= NOT_STEPPING
;
493 stack_level
= call_stack
.size() - 1;
494 print(DRET_NOTIFICATION
, "Test execution halted.");
495 if (p_batch_file
!= NULL
) {
496 if (TTCN_Runtime::is_single()) {
497 TTCN_Debugger_UI::execute_batch_file(p_batch_file
);
500 TTCN_Communication::send_debug_batch(p_batch_file
);
503 else if (p_run_global_batch
&& global_batch_file
!= NULL
) {
504 if (TTCN_Runtime::is_single()) {
505 TTCN_Debugger_UI::execute_batch_file(global_batch_file
);
508 TTCN_Communication::send_debug_batch(global_batch_file
);
511 if (TTCN_Runtime::is_single()) {
512 if (halted
&& !halt_at_start
) {
516 TTCN_Debugger_UI::read_loop();
520 TTCN_Communication::process_debug_messages();
525 print(DRET_NOTIFICATION
, "Test execution is already halted.");
529 void TTCN3_Debugger::resume()
534 print(DRET_NOTIFICATION
, "Test execution resumed.");
537 print(DRET_NOTIFICATION
, "Test execution is not halted.");
541 void TTCN3_Debugger::exit_(const char* p_what
)
543 if (!strcmp(p_what
, "test")) {
546 else if (!strcmp(p_what
, "all")) {
550 print(DRET_NOTIFICATION
, "Argument 1 is invalid. Expected 'test' or 'all'.");
554 if (!TTCN_Runtime::is_hc()) {
555 print((exiting
&& TTCN_Runtime::is_mtc()) ? DRET_EXIT_ALL
: DRET_NOTIFICATION
,
556 "Exiting %s.", exiting
? "test execution" : "current test");
557 TTCN_Runtime::stop_execution();
561 size_t TTCN3_Debugger::find_breakpoint(const char* p_module
, int p_line
) const
563 for (size_t i
= 0; i
< breakpoints
.size(); ++i
) {
564 if (!strcmp(breakpoints
[i
].module
, p_module
) && breakpoints
[i
].line
== p_line
) {
568 return breakpoints
.size();
571 TTCN3_Debugger::variable_t
* TTCN3_Debugger::find_variable(const void* p_value
) const
573 for (size_t i
= 0; i
< variables
.size(); ++i
) {
574 if (variables
[i
]->value
== p_value
) {
581 char* TTCN3_Debugger::finalize_file_name(const char* p_file_name_skeleton
)
583 if (p_file_name_skeleton
== NULL
) {
586 size_t len
= strlen(p_file_name_skeleton
);
588 char* ret_val
= NULL
;
589 for (size_t i
= 0; i
< len
- 1; ++i
) {
590 if (p_file_name_skeleton
[i
] == '%') {
591 ret_val
= mputstrn(ret_val
, p_file_name_skeleton
+ next_idx
, i
- next_idx
);
592 switch (p_file_name_skeleton
[i
+ 1]) {
593 case 'e': // %e -> executable name
594 ret_val
= mputstr(ret_val
, TTCN_Logger::get_executable_name());
596 case 'h': // %h -> host name
597 ret_val
= mputstr(ret_val
, TTCN_Runtime::get_host_name());
599 case 'p': // %p -> process ID
600 ret_val
= mputprintf(ret_val
, "%ld", (long)getpid());
602 case 'l': { // %l -> login name
604 struct passwd
*p
= getpwuid(getuid());
606 ret_val
= mputstr(ret_val
, p
->pw_name
);
610 case 'r': // %r -> component reference
611 if (TTCN_Runtime::is_single()) {
612 ret_val
= mputstr(ret_val
, "single");
614 else if (TTCN_Runtime::is_mtc()) {
615 ret_val
= mputstr(ret_val
, "mtc");
617 else if (TTCN_Runtime::is_ptc()) {
618 ret_val
= mputprintf(ret_val
, "%d", (component
)self
);
621 case 'n': // %n -> component name
622 if (TTCN_Runtime::is_mtc()) {
623 ret_val
= mputstr(ret_val
, "MTC");
625 else if (TTCN_Runtime::is_ptc()) {
626 ret_val
= mputstr(ret_val
, TTCN_Runtime::get_component_name());
629 case '%': // %% -> single %
630 ret_val
= mputc(ret_val
, '%');
632 default: // unknown sequence -> leave it as it is
633 ret_val
= mputstrn(ret_val
, p_file_name_skeleton
+ i
, 2);
640 if (next_idx
< len
) {
641 ret_val
= mputstr(ret_val
, p_file_name_skeleton
+ next_idx
);
646 void TTCN3_Debugger::test_execution_started()
651 if (TTCN_Runtime::is_single()) {
652 TTCN_Debugger_UI::init();
653 if (initial_batch_file
) {
654 halt(initial_batch_file
, false);
656 else if (halt_at_start
) {
660 halt_at_start
= true;
663 void TTCN3_Debugger::test_execution_finished()
665 stepping_type
= NOT_STEPPING
;
666 Free(temporary_breakpoint
.module
);
667 temporary_breakpoint
.module
= NULL
;
668 temporary_breakpoint
.line
= 0;
669 last_breakpoint_entry
.module
= NULL
;
670 last_breakpoint_entry
.line
= 0;
671 if (TTCN_Runtime::is_single()) {
672 TTCN_Debugger_UI::clean_up();
676 void TTCN3_Debugger::print(int return_type
, const char* fmt
, ...) const
678 if (TTCN_Runtime::is_hc()) {
679 // don't display anything while on the HC process
683 va_start(parameters
, fmt
);
684 char* str
= mprintf_va_list(fmt
, parameters
);
686 if (TTCN_Runtime::is_single()) {
687 if (send_to_console
) {
688 TTCN_Debugger_UI::print(str
);
692 TTCN_Communication::send_debug_return_value(return_type
, send_to_console
? str
: NULL
);
694 if (output_file
!= NULL
) {
695 fseek(output_file
, 0, SEEK_END
); // in case multiple processes are writing the same file
696 fputs(str
, output_file
);
697 fputc('\n', output_file
);
703 TTCN3_Debugger::TTCN3_Debugger()
709 output_file_name
= NULL
;
710 send_to_console
= true;
712 last_breakpoint_entry
.module
= NULL
;
713 last_breakpoint_entry
.line
= 0;
714 last_breakpoint_entry
.batch_file
= NULL
; // not used
716 fail_behavior
.trigger
= false;
717 fail_behavior
.batch_file
= NULL
;
718 error_behavior
.trigger
= false;
719 error_behavior
.batch_file
= NULL
;
720 global_batch_file
= NULL
;
721 command_result
= NULL
;
722 last_variable_list
= NULL
;
723 stepping_type
= NOT_STEPPING
;
724 stepping_stack_size
= 0;
725 temporary_breakpoint
.module
= NULL
;
726 temporary_breakpoint
.line
= 0;
727 temporary_breakpoint
.batch_file
= NULL
; // not used
729 halt_at_start
= false;
730 initial_batch_file
= NULL
;
733 TTCN3_Debugger::~TTCN3_Debugger()
735 if (output_file
!= NULL
) {
737 Free(output_file_name
);
739 for (size_t i
= 0; i
< breakpoints
.size(); ++i
) {
740 Free(breakpoints
[i
].module
);
741 Free(breakpoints
[i
].batch_file
);
743 for (size_t i
= 0; i
< global_scopes
.size(); ++i
) {
744 delete global_scopes
[i
].scope
;
746 for (size_t i
= 0; i
< component_scopes
.size(); ++i
) {
747 delete component_scopes
[i
].scope
;
749 for (size_t i
= 0; i
< variables
.size(); ++i
) {
752 Free(fail_behavior
.batch_file
);
753 Free(error_behavior
.batch_file
);
754 Free(global_batch_file
);
756 Free(last_variable_list
);
759 TTCN3_Debug_Scope
* TTCN3_Debugger::add_global_scope(const char* p_module
)
761 named_scope_t global_scope
;
762 global_scope
.name
= p_module
;
763 global_scope
.scope
= new TTCN3_Debug_Scope();
764 global_scopes
.push_back(global_scope
);
765 return global_scope
.scope
;
768 TTCN3_Debug_Scope
* TTCN3_Debugger::add_component_scope(const char* p_component
)
770 named_scope_t component_scope
;
771 component_scope
.name
= p_component
;
772 component_scope
.scope
= new TTCN3_Debug_Scope();
773 component_scopes
.push_back(component_scope
);
774 return component_scope
.scope
;
777 void TTCN3_Debugger::set_return_value(const CHARSTRING
& p_value
)
779 if (active
&& !call_stack
.empty()) {
780 call_stack
[call_stack
.size() - 1].function
->set_return_value(p_value
);
784 void TTCN3_Debugger::breakpoint_entry(int p_line
)
786 if (active
&& !call_stack
.empty()) {
787 const char* module_name
= call_stack
[call_stack
.size() - 1].function
->get_module_name();
788 bool trigger
= false;
789 const char* trigger_type
;
791 const char* batch_file
= NULL
;
793 case SBP_FAIL_VERDICT
:
794 trigger
= fail_behavior
.trigger
;
795 trigger_type
= "Automatic breakpoint (fail verdict) reached at";
796 actual_line
= TTCN_Location::get_line_number();
797 batch_file
= fail_behavior
.batch_file
;
799 case SBP_ERROR_VERDICT
:
800 trigger
= error_behavior
.trigger
;
801 trigger_type
= "Automatic breakpoint (error verdict) reached at";
802 actual_line
= TTCN_Location::get_line_number();
803 batch_file
= error_behavior
.batch_file
;
805 default: // code lines
806 // first: make sure it's not the same breakpoint entry as last time
807 if (p_line
== last_breakpoint_entry
.line
&&
808 module_name
== last_breakpoint_entry
.module
) {
811 actual_line
= p_line
;
812 // second: check if a stepping operation ends here
813 if (stepping_type
== STEP_INTO
||
814 (stepping_type
== STEP_OVER
&& call_stack
.size() <= stepping_stack_size
) ||
815 (stepping_type
== STEP_OUT
&& call_stack
.size() < stepping_stack_size
)) {
817 trigger_type
= "Stepped to";
820 // third: check if this is the destination of a 'run to cursor' operation
821 if (p_line
== temporary_breakpoint
.line
&&
822 !strcmp(module_name
, temporary_breakpoint
.module
)) {
824 trigger_type
= "Ran to";
827 // fourth: check if the location matches one of the breakpoints in the list
828 size_t pos
= find_breakpoint(module_name
, p_line
);
829 if (pos
!= breakpoints
.size()) {
831 batch_file
= breakpoints
[pos
].batch_file
;
833 trigger_type
= "User breakpoint reached at";
837 print(DRET_NOTIFICATION
, "%s line %d in module '%s'.",
838 trigger_type
, actual_line
, module_name
);
839 if (!TTCN_Runtime::is_single()) {
840 TTCN_Communication::send_debug_halt_req();
842 halt(batch_file
, true);
844 last_breakpoint_entry
.module
= (char*)module_name
;
845 last_breakpoint_entry
.line
= p_line
;
849 CHARSTRING
TTCN3_Debugger::print_base_var(const TTCN3_Debugger::variable_t
& p_var
)
851 const void* ptr
= p_var
.set_function
!= NULL
? p_var
.value
: p_var
.cvalue
;
852 TTCN_Logger::begin_event_log2str();
853 if (!strcmp(p_var
.type_name
, "bitstring")) {
854 ((const BITSTRING
*)ptr
)->log();
856 else if (!strcmp(p_var
.type_name
, "bitstring template")) {
857 ((const BITSTRING_template
*)ptr
)->log();
859 else if (!strcmp(p_var
.type_name
, "boolean")) {
860 ((const BOOLEAN
*)ptr
)->log();
862 else if (!strcmp(p_var
.type_name
, "boolean template")) {
863 ((const BOOLEAN_template
*)ptr
)->log();
865 else if (!strcmp(p_var
.type_name
, "charstring")) {
866 ((const CHARSTRING
*)ptr
)->log();
868 else if (!strcmp(p_var
.type_name
, "charstring template")) {
869 ((const CHARSTRING_template
*)ptr
)->log();
871 else if (!strcmp(p_var
.type_name
, "float")) {
872 ((const FLOAT
*)ptr
)->log();
874 else if (!strcmp(p_var
.type_name
, "float template")) {
875 ((const FLOAT_template
*)ptr
)->log();
877 else if (!strcmp(p_var
.type_name
, "hexstring")) {
878 ((const HEXSTRING
*)ptr
)->log();
880 else if (!strcmp(p_var
.type_name
, "hexstring template")) {
881 ((const HEXSTRING_template
*)ptr
)->log();
883 else if (!strcmp(p_var
.type_name
, "integer")) {
884 ((const INTEGER
*)ptr
)->log();
886 else if (!strcmp(p_var
.type_name
, "integer template")) {
887 ((const INTEGER_template
*)ptr
)->log();
889 else if (!strcmp(p_var
.type_name
, "objid")) {
890 ((const OBJID
*)ptr
)->log();
892 else if (!strcmp(p_var
.type_name
, "objid template")) {
893 ((const OBJID_template
*)ptr
)->log();
895 else if (!strcmp(p_var
.type_name
, "octetstring")) {
896 ((const OCTETSTRING
*)ptr
)->log();
898 else if (!strcmp(p_var
.type_name
, "octetstring template")) {
899 ((const OCTETSTRING_template
*)ptr
)->log();
901 else if (!strcmp(p_var
.type_name
, "universal charstring")) {
902 ((const UNIVERSAL_CHARSTRING
*)ptr
)->log();
904 else if (!strcmp(p_var
.type_name
, "universal charstring template")) {
905 ((const UNIVERSAL_CHARSTRING_template
*)ptr
)->log();
907 else if (!strcmp(p_var
.type_name
, "verdicttype")) {
908 ((const VERDICTTYPE
*)ptr
)->log();
910 else if (!strcmp(p_var
.type_name
, "verdicttype template")) {
911 ((const VERDICTTYPE_template
*)ptr
)->log();
913 else if (!strcmp(p_var
.type_name
, "component")) {
914 ((const COMPONENT
*)ptr
)->log();
916 else if (!strcmp(p_var
.type_name
, "component template")) {
917 ((const COMPONENT_template
*)ptr
)->log();
919 else if (!strcmp(p_var
.type_name
, "default")) {
920 ((const DEFAULT
*)ptr
)->log();
922 else if (!strcmp(p_var
.type_name
, "default template")) {
923 ((const DEFAULT_template
*)ptr
)->log();
925 else if (!strcmp(p_var
.type_name
, "timer")) {
926 ((const TIMER
*)ptr
)->log();
928 else if (!strcmp(p_var
.type_name
, "NULL")) {
929 ((const ASN_NULL
*)ptr
)->log();
931 else if (!strcmp(p_var
.type_name
, "NULL template")) {
932 ((const ASN_NULL_template
*)ptr
)->log();
934 else if (!strcmp(p_var
.type_name
, "CHARACTER STRING")) {
935 ((const CHARACTER_STRING
*)ptr
)->log();
937 else if (!strcmp(p_var
.type_name
, "CHARACTER STRING template")) {
938 ((const CHARACTER_STRING_template
*)ptr
)->log();
940 else if (!strcmp(p_var
.type_name
, "EMBEDDED PDV")) {
941 ((const EMBEDDED_PDV
*)ptr
)->log();
943 else if (!strcmp(p_var
.type_name
, "EMBEDDED PDV template")) {
944 ((const EMBEDDED_PDV_template
*)ptr
)->log();
946 else if (!strcmp(p_var
.type_name
, "EXTERNAL")) {
947 ((const EXTERNAL
*)ptr
)->log();
949 else if (!strcmp(p_var
.type_name
, "EXTERNAL template")) {
950 ((const EXTERNAL_template
*)ptr
)->log();
953 TTCN_Logger::log_event_str("<unrecognized value or template>");
955 return TTCN_Logger::end_event_log2str();
958 boolean
TTCN3_Debugger::set_base_var(variable_t
& p_var
, Module_Param
& p_new_value
)
960 if (!strcmp(p_var
.type_name
, "bitstring")) {
961 ((BITSTRING
*)p_var
.value
)->set_param(p_new_value
);
963 else if (!strcmp(p_var
.type_name
, "bitstring template")) {
964 ((BITSTRING_template
*)p_var
.value
)->set_param(p_new_value
);
966 else if (!strcmp(p_var
.type_name
, "boolean")) {
967 ((BOOLEAN
*)p_var
.value
)->set_param(p_new_value
);
969 else if (!strcmp(p_var
.type_name
, "boolean template")) {
970 ((BOOLEAN_template
*)p_var
.value
)->set_param(p_new_value
);
972 else if (!strcmp(p_var
.type_name
, "charstring")) {
973 ((CHARSTRING
*)p_var
.value
)->set_param(p_new_value
);
975 else if (!strcmp(p_var
.type_name
, "charstring template")) {
976 ((CHARSTRING_template
*)p_var
.value
)->set_param(p_new_value
);
978 else if (!strcmp(p_var
.type_name
, "float")) {
979 ((FLOAT
*)p_var
.value
)->set_param(p_new_value
);
981 else if (!strcmp(p_var
.type_name
, "float template")) {
982 ((FLOAT_template
*)p_var
.value
)->set_param(p_new_value
);
984 else if (!strcmp(p_var
.type_name
, "hexstring")) {
985 ((HEXSTRING
*)p_var
.value
)->set_param(p_new_value
);
987 else if (!strcmp(p_var
.type_name
, "hexstring template")) {
988 ((HEXSTRING_template
*)p_var
.value
)->set_param(p_new_value
);
990 else if (!strcmp(p_var
.type_name
, "integer")) {
991 ((INTEGER
*)p_var
.value
)->set_param(p_new_value
);
993 else if (!strcmp(p_var
.type_name
, "integer template")) {
994 ((INTEGER_template
*)p_var
.value
)->set_param(p_new_value
);
996 else if (!strcmp(p_var
.type_name
, "objid")) {
997 ((OBJID
*)p_var
.value
)->set_param(p_new_value
);
999 else if (!strcmp(p_var
.type_name
, "objid template")) {
1000 ((OBJID_template
*)p_var
.value
)->set_param(p_new_value
);
1002 else if (!strcmp(p_var
.type_name
, "octetstring")) {
1003 ((OCTETSTRING
*)p_var
.value
)->set_param(p_new_value
);
1005 else if (!strcmp(p_var
.type_name
, "octetstring template")) {
1006 ((OCTETSTRING_template
*)p_var
.value
)->set_param(p_new_value
);
1008 else if (!strcmp(p_var
.type_name
, "universal charstring")) {
1009 ((UNIVERSAL_CHARSTRING
*)p_var
.value
)->set_param(p_new_value
);
1011 else if (!strcmp(p_var
.type_name
, "universal charstring template")) {
1012 ((UNIVERSAL_CHARSTRING_template
*)p_var
.value
)->set_param(p_new_value
);
1014 else if (!strcmp(p_var
.type_name
, "verdicttype")) {
1015 ((VERDICTTYPE
*)p_var
.value
)->set_param(p_new_value
);
1017 else if (!strcmp(p_var
.type_name
, "verdicttype template")) {
1018 ((VERDICTTYPE_template
*)p_var
.value
)->set_param(p_new_value
);
1020 else if (!strcmp(p_var
.type_name
, "component")) {
1021 ((COMPONENT
*)p_var
.value
)->set_param(p_new_value
);
1023 else if (!strcmp(p_var
.type_name
, "component template")) {
1024 ((COMPONENT_template
*)p_var
.value
)->set_param(p_new_value
);
1026 else if (!strcmp(p_var
.type_name
, "default")) {
1027 ((DEFAULT
*)p_var
.value
)->set_param(p_new_value
);
1029 else if (!strcmp(p_var
.type_name
, "default template")) {
1030 ((DEFAULT_template
*)p_var
.value
)->set_param(p_new_value
);
1032 else if (!strcmp(p_var
.type_name
, "NULL")) {
1033 ((ASN_NULL
*)p_var
.value
)->set_param(p_new_value
);
1035 else if (!strcmp(p_var
.type_name
, "NULL template")) {
1036 ((ASN_NULL_template
*)p_var
.value
)->set_param(p_new_value
);
1038 else if (!strcmp(p_var
.type_name
, "CHARACTER STRING")) {
1039 ((CHARACTER_STRING
*)p_var
.value
)->set_param(p_new_value
);
1041 else if (!strcmp(p_var
.type_name
, "CHARACTER STRING template")) {
1042 ((CHARACTER_STRING_template
*)p_var
.value
)->set_param(p_new_value
);
1044 else if (!strcmp(p_var
.type_name
, "EMBEDDED PDV")) {
1045 ((EMBEDDED_PDV
*)p_var
.value
)->set_param(p_new_value
);
1047 else if (!strcmp(p_var
.type_name
, "EMBEDDED PDV template")) {
1048 ((EMBEDDED_PDV_template
*)p_var
.value
)->set_param(p_new_value
);
1050 else if (!strcmp(p_var
.type_name
, "EXTERNAL")) {
1051 ((EXTERNAL
*)p_var
.value
)->set_param(p_new_value
);
1053 else if (!strcmp(p_var
.type_name
, "EXTERNAL template")) {
1054 ((EXTERNAL_template
*)p_var
.value
)->set_param(p_new_value
);
1062 void TTCN3_Debugger::add_to_result(const char* fmt
, ...)
1065 va_start(parameters
, fmt
);
1066 command_result
= mputprintf_va_list(command_result
, fmt
, parameters
);
1070 void TTCN3_Debugger::add_function(TTCN3_Debug_Function
* p_function
)
1072 function_call_t function_call
;
1073 if (call_stack
.empty()) {
1074 test_execution_started();
1075 function_call
.caller_line
= 0;
1078 function_call
.caller_line
= last_breakpoint_entry
.line
;
1080 function_call
.function
= p_function
;
1081 call_stack
.push_back(function_call
);
1084 void TTCN3_Debugger::add_scope(TTCN3_Debug_Scope
* p_scope
)
1086 if (active
&& !call_stack
.empty()) {
1087 call_stack
[call_stack
.size() - 1].function
->add_scope(p_scope
);
1091 void TTCN3_Debugger::remove_function(TTCN3_Debug_Function
* p_function
)
1093 if (!call_stack
.empty() && call_stack
[call_stack
.size() - 1].function
== p_function
) {
1094 bool removing_test_case
= call_stack
[call_stack
.size() - 1].function
->is_test_case();
1095 int caller_line
= call_stack
[call_stack
.size() - 1].caller_line
;
1096 call_stack
.erase_at(call_stack
.size() - 1);
1097 if (call_stack
.empty()) {
1098 test_execution_finished();
1100 if (caller_line
!= 0 && (stepping_type
== STEP_INTO
|| stepping_type
== STEP_OUT
||
1101 (stepping_type
== STEP_OVER
&& call_stack
.size() != stepping_stack_size
))) {
1102 breakpoint_entry(caller_line
);
1104 if (exiting
&& TTCN_Runtime::is_single() && !call_stack
.empty() && removing_test_case
&&
1105 call_stack
[call_stack
.size() - 1].function
->is_control_part()) {
1106 // 'exit all' was requested while executing a test case called by a control
1107 // part, which means the test case caught the original TC_End exception;
1108 // another exception must be thrown to stop the control part, too
1114 void TTCN3_Debugger::remove_scope(TTCN3_Debug_Scope
* p_scope
)
1116 if (!call_stack
.empty()) {
1117 call_stack
[call_stack
.size() - 1].function
->remove_scope(p_scope
);
1121 TTCN3_Debugger::variable_t
* TTCN3_Debugger::add_variable(const void* p_value
,
1124 TTCN3_Debugger::print_function_t p_print_function
)
1126 if (call_stack
.empty()) {
1127 // no call stack yet, so this is a global or component variable
1128 variable_t
* var
= find_variable(p_value
);
1130 var
= new TTCN3_Debugger::variable_t
;
1131 var
->cvalue
= p_value
;
1133 var
->type_name
= p_type
;
1134 var
->print_function
= p_print_function
;
1135 var
->set_function
= NULL
;
1136 variables
.push_back(var
);
1141 // it's a local variable for the top-most function
1142 return call_stack
[call_stack
.size() - 1].function
->add_variable(
1143 p_value
, p_name
, p_type
, p_print_function
);
1148 TTCN3_Debugger::variable_t
* TTCN3_Debugger::add_variable(void* p_value
,
1151 TTCN3_Debugger::print_function_t p_print_function
,
1152 TTCN3_Debugger::set_function_t p_set_function
)
1154 if (call_stack
.empty()) {
1155 // no call stack yet, so this is a global or component variable
1156 variable_t
* var
= find_variable(p_value
);
1158 var
= new TTCN3_Debugger::variable_t
;
1159 var
->value
= p_value
;
1161 var
->type_name
= p_type
;
1162 var
->print_function
= p_print_function
;
1163 var
->set_function
= p_set_function
;
1164 variables
.push_back(var
);
1169 // it's a local variable for the top-most function
1170 return call_stack
[call_stack
.size() - 1].function
->add_variable(
1171 p_value
, p_name
, p_type
, p_print_function
);
1176 void TTCN3_Debugger::remove_variable(const variable_t
* p_var
)
1178 if (active
&& !call_stack
.empty()) {
1179 call_stack
[call_stack
.size() - 1].function
->remove_variable(p_var
);
1183 const TTCN3_Debug_Scope
* TTCN3_Debugger::get_global_scope(const char* p_module
) const
1185 for (size_t i
= 0; i
< global_scopes
.size(); ++i
) {
1186 if (strcmp(global_scopes
[i
].name
, p_module
) == 0) {
1187 return global_scopes
[i
].scope
;
1193 const TTCN3_Debug_Scope
* TTCN3_Debugger::get_component_scope(const char* p_component
) const
1195 for (size_t i
= 0; i
< component_scopes
.size(); ++i
) {
1196 if (strcmp(component_scopes
[i
].name
, p_component
) == 0) {
1197 return component_scopes
[i
].scope
;
1203 void TTCN3_Debugger::add_snapshot(const char* p_snapshot
)
1205 if (snapshots
!= NULL
) {
1206 snapshots
= mputc(snapshots
, '\n');
1208 snapshots
= mputstr(snapshots
, p_snapshot
);
1211 #define CHECK_NOF_ARGUMENTS(exp_num) \
1212 if (exp_num != p_argument_count) { \
1213 print(DRET_NOTIFICATION, "Invalid number of arguments. Expected %d, got %d.", \
1214 (int)exp_num, (int)p_argument_count); \
1218 #define CHECK_NOF_ARGUMENTS_RANGE(min, max) \
1219 if ((int)min > p_argument_count || (int)max < p_argument_count) { \
1220 print(DRET_NOTIFICATION, "Invalid number of arguments. Expected at least %d " \
1221 "and at most %d, got %d.", (int)min, (int)max, p_argument_count); \
1225 #define CHECK_NOF_ARGUMENTS_MIN(min) \
1226 if ((int)min > p_argument_count) { \
1227 print(DRET_NOTIFICATION, "Invalid number of arguments. Expected at least %d, got %d.", \
1228 (int)min, p_argument_count); \
1232 #define CHECK_INT_ARGUMENT(arg_idx) \
1234 size_t len = strlen(p_arguments[arg_idx]); \
1235 for (size_t i = 0; i < len; ++i) { \
1236 if (p_arguments[arg_idx][i] < '0' || p_arguments[arg_idx][i] > '9') { \
1237 print(DRET_NOTIFICATION, "Argument %d is not an integer.", (int)(arg_idx + 1)); \
1243 #define CHECK_CALL_STACK(print_msg) \
1246 print(DRET_NOTIFICATION, "This command can only be used if the debugger " \
1247 "is switched on."); \
1251 if (call_stack.empty()) { \
1253 print(DRET_NOTIFICATION, "This command can only be used if the debugger's " \
1254 "call stack is not empty."); \
1259 void TTCN3_Debugger::execute_command(int p_command
, int p_argument_count
,
1265 for (int i
= 0; i
< p_argument_count
; ++i
) {
1266 if (p_arguments
[i
] == NULL
) {
1267 print(DRET_NOTIFICATION
, "Argument %d is a null pointer.", i
+ 1);
1271 switch (p_command
) {
1273 CHECK_NOF_ARGUMENTS(1)
1274 switch_state(p_arguments
[0]);
1276 case D_SET_BREAKPOINT
:
1277 CHECK_NOF_ARGUMENTS_RANGE(2, 3)
1278 CHECK_INT_ARGUMENT(1)
1279 set_breakpoint(p_arguments
[0], str2int(p_arguments
[1]),
1280 (p_argument_count
== 3) ? p_arguments
[2] : NULL
);
1282 case D_REMOVE_BREAKPOINT
:
1283 CHECK_NOF_ARGUMENTS_RANGE(1, 2)
1284 remove_breakpoint(p_arguments
[0], (p_argument_count
== 2) ? p_arguments
[1] : NULL
);
1286 case D_SET_AUTOMATIC_BREAKPOINT
:
1287 CHECK_NOF_ARGUMENTS_RANGE(2, 3)
1288 set_automatic_breakpoint(p_arguments
[0], p_arguments
[1],
1289 (p_argument_count
== 3) ? p_arguments
[2] : NULL
);
1292 CHECK_NOF_ARGUMENTS_RANGE(1, 2)
1293 set_output(p_arguments
[0], (p_argument_count
== 2) ? p_arguments
[1] : NULL
);
1295 case D_SET_GLOBAL_BATCH_FILE
:
1296 CHECK_NOF_ARGUMENTS_RANGE(1, 2)
1297 set_global_batch_file(p_arguments
[0], (p_argument_count
== 2) ? p_arguments
[1] : NULL
);
1299 case D_SET_COMPONENT
:
1300 print(DRET_NOTIFICATION
, "Command " D_SET_COMPONENT_TEXT
" %s.",
1301 TTCN_Runtime::is_single() ? "is not available in single mode" :
1302 "should have been sent to the Main Controller");
1304 case D_PRINT_CALL_STACK
:
1305 CHECK_CALL_STACK(true)
1306 CHECK_NOF_ARGUMENTS(0)
1309 case D_SET_STACK_LEVEL
:
1310 CHECK_CALL_STACK(true)
1311 CHECK_NOF_ARGUMENTS(1)
1312 CHECK_INT_ARGUMENT(0)
1313 set_stack_level(str2int(p_arguments
[0]));
1315 case D_LIST_VARIABLES
:
1316 CHECK_CALL_STACK(true)
1317 CHECK_NOF_ARGUMENTS_RANGE(1, 2)
1318 call_stack
[STACK_LEVEL
].function
->list_variables(p_arguments
[0],
1319 (p_argument_count
== 2) ? p_arguments
[1] : NULL
);
1321 case D_PRINT_VARIABLE
:
1322 CHECK_CALL_STACK(true)
1323 CHECK_NOF_ARGUMENTS_MIN(1)
1324 for (int i
= 0; i
< p_argument_count
; ++i
) {
1326 add_to_result("\n");
1328 if (!strcmp(p_arguments
[i
], "$")) {
1329 // '$' refers to the result of the last D_LIST_VARIABLES command
1330 // these variable names are separated by spaces
1331 if (last_variable_list
!= NULL
) {
1332 size_t len
= mstrlen(last_variable_list
);
1334 for (size_t j
= 0; j
< len
; ++j
) {
1335 if (last_variable_list
[j
] == ' ') {
1336 // extract the variable name before this space
1337 char* var_name
= mcopystrn(last_variable_list
+ start
, j
- start
);
1338 print_variable(var_name
);
1340 add_to_result("\n");
1344 // extract the last (or only) variable name
1345 char* var_name
= mcopystrn(last_variable_list
+ start
, len
- start
);
1346 print_variable(var_name
);
1350 add_to_result("No previous " D_LIST_VARIABLES_TEXT
" result.");
1354 print_variable(p_arguments
[i
]);
1358 case D_OVERWRITE_VARIABLE
:
1359 CHECK_CALL_STACK(true)
1360 CHECK_NOF_ARGUMENTS_MIN(2)
1361 overwrite_variable(p_arguments
[0], p_argument_count
- 1, p_arguments
+ 1);
1363 case D_PRINT_SNAPSHOTS
:
1364 CHECK_NOF_ARGUMENTS(0)
1365 add_to_result("%s", snapshots
);
1368 CHECK_CALL_STACK(true)
1369 CHECK_NOF_ARGUMENTS(0)
1373 CHECK_CALL_STACK(true)
1374 CHECK_NOF_ARGUMENTS(0)
1378 CHECK_CALL_STACK(true)
1379 CHECK_NOF_ARGUMENTS(0)
1382 case D_RUN_TO_CURSOR
:
1383 if (!TTCN_Runtime::is_hc() && !TTCN_Runtime::is_single()) {
1384 CHECK_CALL_STACK(TTCN_Runtime::is_mtc())
1386 CHECK_NOF_ARGUMENTS(2)
1387 CHECK_INT_ARGUMENT(1)
1388 run_to_cursor(p_arguments
[0], str2int(p_arguments
[1]));
1391 if (!TTCN_Runtime::is_hc() && !TTCN_Runtime::is_single()) {
1392 CHECK_CALL_STACK(TTCN_Runtime::is_mtc())
1394 CHECK_NOF_ARGUMENTS(0)
1398 CHECK_NOF_ARGUMENTS(0)
1402 if (!TTCN_Runtime::is_hc() && !TTCN_Runtime::is_single()) {
1403 CHECK_CALL_STACK(TTCN_Runtime::is_mtc())
1405 CHECK_NOF_ARGUMENTS(1)
1406 exit_(p_arguments
[0]);
1409 CHECK_NOF_ARGUMENTS_MIN(5)
1410 if (strlen(p_arguments
[0]) > 0) {
1411 switch_state(p_arguments
[0]);
1413 if (strlen(p_arguments
[1]) > 0) {
1414 set_output(p_arguments
[1], p_arguments
[2]);
1416 if (strlen(p_arguments
[3]) > 0) {
1417 set_automatic_breakpoint("error", p_arguments
[3],
1418 strlen(p_arguments
[4]) > 0 ? p_arguments
[4] : NULL
);
1420 if (strlen(p_arguments
[5]) > 0) {
1421 set_automatic_breakpoint("fail", p_arguments
[5],
1422 strlen(p_arguments
[6]) > 0 ? p_arguments
[6] : NULL
);
1424 if (strlen(p_arguments
[7]) > 0) {
1425 set_global_batch_file(p_arguments
[7],
1426 strlen(p_arguments
[8]) > 0 ? p_arguments
[8] : NULL
);
1428 for (int i
= 9; i
< p_argument_count
; i
+= 3) {
1429 set_breakpoint(p_arguments
[i
], str2int(p_arguments
[i
+ 1]),
1430 strlen(p_arguments
[i
+ 2]) > 0 ? p_arguments
[i
+ 2] : NULL
);
1434 print(DRET_NOTIFICATION
, "Invalid command received (ID: %d).", p_command
);
1437 if (command_result
!= NULL
) {
1438 print(DRET_DATA
, command_result
);
1439 if (p_command
== D_LIST_VARIABLES
) {
1440 Free(last_variable_list
);
1441 last_variable_list
= command_result
;
1444 Free(command_result
);
1446 command_result
= NULL
;
1450 void TTCN3_Debugger::open_output_file()
1452 if (output_file
== NULL
&& output_file_name
!= NULL
) {
1453 char* final_file_name
= finalize_file_name(output_file_name
);
1454 output_file
= fopen(final_file_name
, TTCN_Runtime::is_mtc() ? "w" : "a");
1455 if (output_file
== NULL
) {
1456 print(DRET_NOTIFICATION
, "Failed to open file '%s' for writing.", final_file_name
);
1458 Free(final_file_name
);
1462 //////////////////////////////////////////////////////
1463 //////////////// TTCN3_Debug_Scope ///////////////////
1464 //////////////////////////////////////////////////////
1466 TTCN3_Debug_Scope::TTCN3_Debug_Scope()
1468 ttcn3_debugger
.add_scope(this);
1471 TTCN3_Debug_Scope::~TTCN3_Debug_Scope()
1473 for (size_t i
= 0; i
< variables
.size(); ++i
) {
1474 ttcn3_debugger
.remove_variable(variables
[i
]);
1476 ttcn3_debugger
.remove_scope(this);
1479 void TTCN3_Debug_Scope::add_variable(const void* p_value
,
1482 TTCN3_Debugger::print_function_t p_print_function
)
1484 TTCN3_Debugger::variable_t
* var
= ttcn3_debugger
.add_variable(p_value
, p_name
, p_type
, p_print_function
);
1486 variables
.push_back(var
);
1490 void TTCN3_Debug_Scope::add_variable(void* p_value
,
1493 TTCN3_Debugger::print_function_t p_print_function
,
1494 TTCN3_Debugger::set_function_t p_set_function
)
1496 TTCN3_Debugger::variable_t
* var
= ttcn3_debugger
.add_variable(p_value
, p_name
,
1497 p_type
, p_print_function
, p_set_function
);
1499 variables
.push_back(var
);
1503 TTCN3_Debugger::variable_t
* TTCN3_Debug_Scope::find_variable(const char* p_name
) const
1505 for (size_t i
= 0; i
< variables
.size(); ++i
) {
1506 if (strcmp(variables
[i
]->name
, p_name
) == 0) {
1507 return variables
[i
];
1513 void TTCN3_Debug_Scope::list_variables(regex_t
* p_posix_regexp
, bool& p_first
) const
1515 for (size_t i
= 0; i
< variables
.size(); ++i
) {
1516 if (p_posix_regexp
== NULL
||
1517 regexec(p_posix_regexp
, variables
[i
]->name
, 0, NULL
, 0) == 0) {
1518 ttcn3_debugger
.add_to_result("%s%s", p_first
? "" : " ", variables
[i
]->name
);
1524 //////////////////////////////////////////////////////
1525 /////////////// TTCN3_Debug_Function /////////////////
1526 //////////////////////////////////////////////////////
1528 TTCN3_Debug_Function::TTCN3_Debug_Function(const char* p_name
,
1530 const char* p_module
,
1531 const charstring_list
& p_parameter_names
,
1532 const charstring_list
& p_parameter_types
,
1533 const char* p_component_name
)
1534 : function_name(p_name
), function_type(p_type
), module_name(p_module
)
1535 , parameter_names(new charstring_list(p_parameter_names
))
1536 , parameter_types(new charstring_list(p_parameter_types
))
1538 ttcn3_debugger
.add_function(this);
1539 global_scope
= ttcn3_debugger
.get_global_scope(p_module
);
1540 component_scope
= (p_component_name
!= NULL
) ?
1541 ttcn3_debugger
.get_component_scope(p_component_name
) : NULL
;
1542 if (function_name
== NULL
) {
1543 function_name
= p_module
; // for control parts
1547 TTCN3_Debug_Function::~TTCN3_Debug_Function()
1549 if (ttcn3_debugger
.is_on()) {
1550 char* snapshot
= mprintf("[%s]\tfinished\t%s(", function_type
, function_name
);
1551 if (parameter_names
->size_of() > 0) {
1552 for (int i
= 0; i
< parameter_names
->size_of(); ++i
) {
1554 snapshot
= mputstr(snapshot
, ", ");
1556 snapshot
= mputprintf(snapshot
, "[%s] %s := ", (const char*)((*parameter_types
)[i
]),
1557 (const char*)((*parameter_names
)[i
]));
1558 if ((*parameter_types
)[i
] == "out" || (*parameter_types
)[i
] == "inout") {
1559 const TTCN3_Debugger::variable_t
* parameter
= find_variable((*parameter_names
)[i
]);
1560 snapshot
= mputstr(snapshot
, parameter
->print_function(*parameter
));
1563 snapshot
= mputc(snapshot
, '-');
1567 snapshot
= mputc(snapshot
, ')');
1568 if (return_value
.is_bound()) {
1569 snapshot
= mputprintf(snapshot
, " returned %s", (const char*)return_value
);
1571 ttcn3_debugger
.add_snapshot(snapshot
);
1574 for (size_t i
= 0; i
< variables
.size(); ++i
) {
1575 delete variables
[i
];
1577 delete parameter_names
;
1578 delete parameter_types
;
1579 ttcn3_debugger
.remove_function(this);
1582 TTCN3_Debugger::variable_t
* TTCN3_Debug_Function::add_variable(const void* p_value
,
1585 TTCN3_Debugger::print_function_t p_print_function
)
1587 if (ttcn3_debugger
.is_on()) {
1588 TTCN3_Debugger::variable_t
* var
= new TTCN3_Debugger::variable_t
;
1589 var
->cvalue
= p_value
;
1591 var
->type_name
= p_type
;
1592 var
->print_function
= p_print_function
;
1593 var
->set_function
= NULL
;
1594 variables
.push_back(var
);
1600 TTCN3_Debugger::variable_t
* TTCN3_Debug_Function::add_variable(void* p_value
,
1603 TTCN3_Debugger::print_function_t p_print_function
,
1604 TTCN3_Debugger::set_function_t p_set_function
)
1606 if (ttcn3_debugger
.is_on()) {
1607 TTCN3_Debugger::variable_t
* var
= new TTCN3_Debugger::variable_t
;
1608 var
->value
= p_value
;
1610 var
->type_name
= p_type
;
1611 var
->print_function
= p_print_function
;
1612 var
->set_function
= p_set_function
;
1613 variables
.push_back(var
);
1619 void TTCN3_Debug_Function::set_return_value(const CHARSTRING
& p_value
)
1621 return_value
= p_value
;
1624 void TTCN3_Debug_Function::initial_snapshot() const
1626 if (ttcn3_debugger
.is_on()) {
1627 char* snapshot
= mprintf("[%s]\tstarted \t%s(", function_type
, function_name
);
1628 if (parameter_names
->size_of() > 0) {
1629 for (int i
= 0; i
< parameter_names
->size_of(); ++i
) {
1631 snapshot
= mputstr(snapshot
, ", ");
1633 snapshot
= mputprintf(snapshot
, "[%s] %s := ", (const char*)((*parameter_types
)[i
]),
1634 (const char*)((*parameter_names
)[i
]));
1635 if ((*parameter_types
)[i
] == "in" || (*parameter_types
)[i
] == "inout") {
1636 const TTCN3_Debugger::variable_t
* parameter
= find_variable((*parameter_names
)[i
]);
1637 snapshot
= mputstr(snapshot
, parameter
->print_function(*parameter
));
1640 snapshot
= mputc(snapshot
, '-');
1644 snapshot
= mputstr(snapshot
, ")");
1645 ttcn3_debugger
.add_snapshot(snapshot
);
1650 void TTCN3_Debug_Function::add_scope(TTCN3_Debug_Scope
* p_scope
)
1652 scopes
.push_back(p_scope
);
1655 void TTCN3_Debug_Function::remove_scope(TTCN3_Debug_Scope
* p_scope
)
1657 if (!scopes
.empty() && scopes
[scopes
.size() - 1] == p_scope
) {
1658 scopes
.erase_at(scopes
.size() - 1);
1662 void TTCN3_Debug_Function::remove_variable(const TTCN3_Debugger::variable_t
* p_var
)
1664 for (size_t i
= 0; i
< variables
.size(); ++i
) {
1665 if (variables
[i
] == p_var
) {
1666 variables
.erase_at(i
);
1673 TTCN3_Debugger::variable_t
* TTCN3_Debug_Function::find_variable(const char* p_name
) const
1675 for (size_t i
= 0; i
< variables
.size(); ++i
) {
1676 if (strcmp(variables
[i
]->name
, p_name
) == 0) {
1677 return variables
[i
];
1680 // it's not a local variable, it might still be a global or component variable
1681 if (component_scope
!= NULL
) {
1682 TTCN3_Debugger::variable_t
* res
= component_scope
->find_variable(p_name
);
1687 return (global_scope
!= NULL
) ? global_scope
->find_variable(p_name
) : NULL
;
1690 void TTCN3_Debug_Function::print_function() const
1692 ttcn3_debugger
.add_to_result("[%s]\t%s(", function_type
, function_name
);
1693 if (parameter_names
->size_of() > 0) {
1694 for (int i
= 0; i
< parameter_names
->size_of(); ++i
) {
1696 ttcn3_debugger
.add_to_result(", ");
1698 const TTCN3_Debugger::variable_t
* parameter
= find_variable((*parameter_names
)[i
]);
1699 ttcn3_debugger
.add_to_result("[%s] %s := %s", (const char*)(*parameter_types
)[i
],
1700 (const char*)(*parameter_names
)[i
], (const char*)parameter
->print_function(*parameter
));
1703 ttcn3_debugger
.add_to_result(")");
1706 void TTCN3_Debug_Function::list_variables(const char* p_scope
, const char* p_filter
) const
1709 bool list_local
= false;
1710 bool list_global
= false;
1711 bool list_comp
= false;
1712 if (!strcmp(p_scope
, "local")) {
1715 else if (!strcmp(p_scope
, "global")) {
1718 else if (!strcmp(p_scope
, "comp")) {
1721 else if (!strcmp(p_scope
, "all")) {
1727 ttcn3_debugger
.print(DRET_NOTIFICATION
, "Argument 1 is invalid. "
1728 "Expected 'local', 'global', 'comp' or 'all'.");
1731 regex_t
* posix_regexp
= NULL
;
1732 if (p_filter
!= NULL
) {
1733 char* posix_str
= TTCN_pattern_to_regexp(p_filter
);
1734 if (posix_str
== NULL
) {
1735 ttcn3_debugger
.print(DRET_NOTIFICATION
, "Argument 2 is invalid. "
1736 "Expected a valid TTCN-3 character pattern.");
1739 posix_regexp
= new regex_t
;
1740 int ret_val
= regcomp(posix_regexp
, posix_str
, REG_EXTENDED
| REG_NOSUB
);
1744 regerror(ret_val
, posix_regexp
, msg
, sizeof(msg
));
1745 regfree(posix_regexp
);
1746 delete posix_regexp
;
1747 ttcn3_debugger
.print(DRET_NOTIFICATION
, "Compilation of POSIX regular "
1748 "expression failed.");
1753 for (size_t i
= 0; i
< variables
.size(); ++i
) {
1754 if (posix_regexp
== NULL
||
1755 regexec(posix_regexp
, variables
[i
]->name
, 0, NULL
, 0) == 0) {
1756 ttcn3_debugger
.add_to_result("%s%s", first
? "" : " ", variables
[i
]->name
);
1761 if (list_global
&& global_scope
!= NULL
&& global_scope
->has_variables()) {
1762 global_scope
->list_variables(posix_regexp
, first
);
1764 if (list_comp
&& component_scope
!= NULL
&& component_scope
->has_variables()) {
1765 component_scope
->list_variables(posix_regexp
, first
);
1768 ttcn3_debugger
.print(DRET_NOTIFICATION
, "No variables found.");
1770 if (posix_regexp
!= NULL
) {
1771 regfree(posix_regexp
);
1772 delete posix_regexp
;
1776 bool TTCN3_Debug_Function::is_control_part() const
1778 return !strcmp(function_type
, "control");
1781 bool TTCN3_Debug_Function::is_test_case() const
1783 return !strcmp(function_type
, "testcase");