1
/* This file is part of Malaga, a system for Natural Language Analysis.
2
* Copyright (C) 1995-1999 Bjoern Beutel
5
* Universitaet Erlangen-Nuernberg
6
* Abteilung fuer Computerlinguistik
9
* e-mail: malaga@linguistik.uni-erlangen.de
11
* This program is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2 of the License, or
14
* (at your option) any later version.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
25
/* description ==============================================================*/
27
/* Debugger functions. */
29
/* includes =================================================================*/
43
#include "rule_type.h"
46
#include "breakpoints.h"
52
/* variables ================================================================*/
54
LOCAL debug_mode_t debug_mode = RUN_MODE; /* current debug mode */
56
LOCAL struct /* definition of the step properties */
58
int_t nested_subrules; /* depth of subrule nesting when stepping started */
59
int_t backup_top; /* number of inactive paths when stepping started */
60
int_t line; /* line where stepping started */
61
string_t file; /* file where stepping started */
62
int_t repetitions; /* repetitions of steps still to do */
65
LOCAL void (*print_rule) (void);
66
/* pointer to a function that prints the current rule name */
68
LOCAL command_t **debugger_commands;
69
/* commands that can be executed when the debug mode is invoked */
71
/* functions ================================================================*/
73
LOCAL bool_t at_next_line (rule_sys_t *rule_sys, int_t pc)
74
/* Compare current source file name and source line to previous values.
75
* If one of them has changed, return TRUE. */
83
if (backup_top < step.backup_top)
84
/* Current path is finished. Debug older path. */
86
step.nested_subrules = nested_subrules;
87
step.backup_top = backup_top;
89
if (nested_subrules > step.nested_subrules)
91
/* fall through to STEP_MODE */
93
source_of_instr (rule_sys, pc, &line, NULL, &file, NULL);
94
if ((line == -1 && file == NULL)
95
|| (step.line == line && step.file == file))
98
step.nested_subrules = nested_subrules;
102
/* Ignore interactive mode while number of steps > 1. */
103
if (step.repetitions > 1)
109
if (rule_sys->rules[executed_rule_number].first_instr == pc)
118
/*---------------------------------------------------------------------------*/
120
GLOBAL void set_debug_mode (debug_mode_t new_debug_mode, rule_sys_t *rule_sys)
121
/* Set debug mode <new_debug_mode> for <rule_sys>. */
123
switch (new_debug_mode)
126
debug_rule_sys = NULL;
131
step.nested_subrules = 0;
134
step.repetitions = 0;
135
debug_rule_sys = rule_sys;
139
debug_rule_sys = rule_sys;
142
debug_mode = new_debug_mode;
145
/*---------------------------------------------------------------------------*/
147
LOCAL void check_if_rule_start (void)
148
/* Look if <pc> points at rule start, and print an appropriate message.
149
* Called from "debug_rule" before entering the command loop. */
151
rule_t *rule = executed_rule_sys->rules + executed_rule_number;
153
if (rule->first_instr == pc)
157
/*---------------------------------------------------------------------------*/
159
LOCAL void display_variables (void)
161
string_t value_string;
162
int_t stack_top = inspect_stack_pointer ();
165
start_display_process ();
167
fprintf (display_stream, "variables\n");
169
for (i = first_variable_index (executed_rule_sys, pc); i < stack_top; i++)
171
string_t var_name = variable_at_index (executed_rule_sys, i, pc);
173
if (var_name != NULL && inspect_stack (i) != NULL)
175
value_string = value_to_readable (inspect_stack (i), FALSE);
176
fprintf (display_stream, "\"$%s\" {%s}\n", var_name, value_string);
177
free_mem (&value_string);
181
fprintf (display_stream, "end\n");
182
fflush (display_stream);
185
/*---------------------------------------------------------------------------*/
187
LOCAL void debugger_debug_rule (void)
188
/* Called from "execute_rule" before instruction pointed to by
189
* <pc> is executed. */
191
int_t breakpoint_number;
193
executing_rule = FALSE;
196
breakpoint_number = at_breakpoint (executed_rule_sys, pc);
197
if (breakpoint_number != 0 || at_next_line (executed_rule_sys, pc))
203
source_of_instr (executed_rule_sys, pc, &line, NULL, &file, &rule);
205
if (breakpoint_number != 0)
206
printf ("hit breakpoint %ld in file \"%s\", line %ld, rule \"%s\"\n",
207
breakpoint_number, name_in_path (file), line, rule);
208
else if (getenv ("MALAGA_MODE") == NULL)
209
printf ("step to file \"%s\", line %ld, rule \"%s\"\n",
210
name_in_path (file), line, rule);
212
if (getenv ("MALAGA_MODE") != NULL)
213
printf ("SHOW \"%s\":%ld:0\n", file, line);
215
/* check if we are at rule start and print an appropriate message */
216
check_if_rule_start ();
219
display_variables ();
222
command_loop ("debug", debugger_commands);
225
set_debug_mode (RUN_MODE, NULL);
229
executing_rule = TRUE;
232
/*---------------------------------------------------------------------------*/
234
GLOBAL void init_debugger (void (*my_print_rule) (void),
235
command_t *my_debugger_commands[])
236
/* Initialise the debugger module.
237
* Pass a function <my_print_rule> printing the current rule, and commands
238
* <my_debugger_commands> that can be invoked when debug mode is invoked. */
240
print_rule = my_print_rule;
241
debugger_commands = my_debugger_commands;
242
debug_rule = debugger_debug_rule;
245
/*---------------------------------------------------------------------------*/
247
GLOBAL void terminate_debugger (void)
248
/* Terminate the debugger module. */
250
stop_display_process ();
254
/*---------------------------------------------------------------------------*/
256
LOCAL void step_or_next (debug_mode_t debug_mode, string_t argument)
257
/* Execute "step" or "next" command (see <debug_mode>) with <argument>. */
264
error ("not in debugging mode");
266
source_of_instr (executed_rule_sys, pc, &line, NULL, &file, NULL);
268
DB_ASSERT (line != -1);
270
if (*argument == EOS)
273
repetitions = parse_int (&argument);
274
parse_end (argument);
276
set_debug_mode (debug_mode, executed_rule_sys);
277
step.backup_top = backup_top;
278
step.nested_subrules = nested_subrules;
281
step.repetitions = repetitions;
284
/*---------------------------------------------------------------------------*/
286
LOCAL void do_run (string_t arguments)
287
/* Continue rule execution in non-debugging mode. */
290
error ("not in debugging mode");
292
parse_end (arguments);
293
set_debug_mode (RUN_MODE, NULL);
294
leave_command_loop = TRUE;
297
GLOBAL command_t run_command =
300
"Return to non-debugging mode and complete rule execution.\n"
302
"\"run\" can only be used in debug mode.\n"
305
/*---------------------------------------------------------------------------*/
307
LOCAL void do_go (string_t arguments)
308
/* Continue rule execution until next breakpoint is met or end is reached. */
311
error ("not in debugging mode");
313
parse_end (arguments);
314
set_debug_mode (GO_MODE, executed_rule_sys);
315
leave_command_loop = TRUE;
318
GLOBAL command_t go_command =
321
"Execute rules until a breakpoint is hit or analysis is completed.\n"
323
"\"go\" can only be used in debug mode.\n"
326
/*---------------------------------------------------------------------------*/
328
LOCAL void do_walk (string_t arguments)
329
/* Continue rule execution until next rule or breakpoint is met
330
* or end is reached. */
333
error ("not in debugging mode");
335
parse_end (arguments);
336
set_debug_mode (WALK_MODE, executed_rule_sys);
337
leave_command_loop = TRUE;
340
GLOBAL command_t walk_command =
343
"Walk to the next rule.\n"
345
"\"walk\" can only be used in debug mode.\n"
348
/*---------------------------------------------------------------------------*/
350
LOCAL void do_step (string_t argument)
351
/* Continue until control reaches a different source line. */
353
step_or_next (STEP_MODE, argument);
354
leave_command_loop = TRUE;
357
GLOBAL command_t step_command =
360
"Step to the next source line.\n"
362
" step <repetitions> -- execute command <repetitions> time\n"
363
" step -- execute one step\n"
364
"\"step\" can only be used in debug mode.\n"
367
/*---------------------------------------------------------------------------*/
369
LOCAL void do_next (string_t argument)
370
/* Continue until control reaches a different source line, but jump over
373
step_or_next (NEXT_MODE, argument);
374
leave_command_loop = TRUE;
377
GLOBAL command_t next_command =
380
"Step to the next source line, but jump over subrules.\n"
382
" next <repetitions> -- execute <repetitions> steps\n"
383
" next -- execute one step\n"
384
"\"next\" can only be used in debug mode.\n"
387
/*---------------------------------------------------------------------------*/
389
LOCAL void do_trace (string_t arguments)
390
/* Print all active subrules and rules in reverse order. */
395
error ("no rule executed");
397
parse_end (arguments);
406
get_base_and_pc_indexes (index, &index, &pc_index);
407
source_of_instr (executed_rule_sys, pc_index, &line, NULL, &file, &rule);
408
printf ("line %ld in file \"%s\", rule \"%s\"\n",
409
line, name_in_path (file), rule);
410
} while (index != 0);
413
GLOBAL command_t trace_command =
416
"Print the active subrules in reverse order.\n"
418
"\"trace\" can only be used in debug mode or after a rule execution error.\n"
421
/*---------------------------------------------------------------------------*/
423
LOCAL void do_print (string_t argument)
424
/* Print content of variables. */
427
string_t value_string;
431
error ("no rule executed");
433
if (*argument != EOS)
435
/* If any arguments given, treat each argument as a variable. */
436
while (*argument != EOS)
438
string_t path = parse_word (&argument);
439
string_t section, sect_start, sect_end;
442
error ("variable names start with \"$\"");
443
sect_start = path + 1;
445
/* Find end of variable name. */
446
sect_end = sect_start;
447
while (*sect_end != EOS && *sect_end != '.')
450
/* Get variable name and its value. */
451
section = new_string (sect_start, sect_end);
452
stack_index = variable_index (executed_rule_sys, section, pc);
453
if (stack_index == -1)
454
error ("variable \"$%s\" is not defined here", section);
456
value = inspect_stack (stack_index);
458
while (*sect_end != EOS)
460
/* Skip "." and remember section start. */
462
sect_start = sect_end;
464
/* Find end of section. */
465
while (*sect_end != EOS && *sect_end != '.')
468
section = new_string (sect_start, sect_end);
469
if (isdigit (*section) || *section == '-')
475
index = parse_int (&s);
476
value = get_element (value, index);
479
value = get_attribute (value, find_symbol (section));
487
printf ("path \"%s\" doesn't exist\n", path);
490
value_string = value_to_readable (value, FALSE);
491
printf ("%s = %s\n", path, value_string);
492
free_mem (&value_string);
500
int_t stack_top = inspect_stack_pointer ();
502
/* Print all variables currently defined. */
503
for (stack_index = first_variable_index (executed_rule_sys, pc);
504
stack_index < stack_top;
509
var_name = variable_at_index (executed_rule_sys, stack_index, pc);
510
if (var_name != NULL)
512
value_string = value_to_readable (inspect_stack (stack_index), FALSE);
513
printf ("$%s = %s\n", var_name, value_string);
514
free_mem (&value_string);
520
GLOBAL command_t print_command =
523
"Print the values of variables.\n"
525
" print <variable> ... -- print the values of the specified variables\n"
526
" print -- print all currently defined variables\n"
527
"Each variable may include an attribute path.\n"
528
"\"print\" can only be used in debug mode or after a rule execution error.\n"
531
/*---------------------------------------------------------------------------*/
533
LOCAL void do_variables (string_t arguments)
534
/* Generate variables file and start TCL program to display variables. */
537
error ("no rule executed");
539
parse_end (arguments);
540
display_variables ();
543
GLOBAL command_t variables_command =
545
"variables v", do_variables,
546
"Display current variables in matrix form.\n"
548
"\"variables\" can only be used in debug mode or after a rule execution"
552
/* end of file ==============================================================*/