~ubuntu-branches/ubuntu/breezy/malaga/breezy

« back to all changes in this revision

Viewing changes to source/debugger.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Bushnell, BSG
  • Date: 2005-01-10 11:52:04 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050110115204-hpgncw5pb0m1t8i6
Tags: 6.13-5
debian/control (malaga-doc Recommends): Suggest gv as a
postscript-viewer instead of ghostview.  (Closes: #289701).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Malaga, a system for Natural Language Analysis.
2
 
 * Copyright (C) 1995-1999 Bjoern Beutel
3
 
 *
4
 
 * Bjoern Beutel
5
 
 * Universitaet Erlangen-Nuernberg
6
 
 * Abteilung fuer Computerlinguistik
7
 
 * Bismarckstrasse 12
8
 
 * D-91054 Erlangen
9
 
 * e-mail: malaga@linguistik.uni-erlangen.de 
10
 
 *
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.
15
 
 *
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.
20
 
 *
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 */
24
 
 
25
 
/* description ==============================================================*/
26
 
 
27
 
/* Debugger functions. */
28
 
 
29
 
/* includes =================================================================*/
30
 
 
31
 
#include <ctype.h>
32
 
#include <stdio.h>
33
 
#include <setjmp.h>
34
 
#include <stdlib.h>
35
 
#include "basic.h"
36
 
#include "pools.h"
37
 
#include "values.h"
38
 
#include "symbols.h"
39
 
#include "files.h"
40
 
#include "input.h"
41
 
#include "commands.h"
42
 
#include "options.h"
43
 
#include "rule_type.h"
44
 
#include "rules.h"
45
 
#include "display.h"
46
 
#include "breakpoints.h"
47
 
 
48
 
#undef GLOBAL
49
 
#define GLOBAL
50
 
#include "debugger.h"
51
 
 
52
 
/* variables ================================================================*/
53
 
 
54
 
LOCAL debug_mode_t debug_mode = RUN_MODE; /* current debug mode */
55
 
 
56
 
LOCAL struct /* definition of the step properties */
57
 
{
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 */
63
 
} step;
64
 
 
65
 
LOCAL void (*print_rule) (void); 
66
 
/* pointer to a function that prints the current rule name */
67
 
 
68
 
LOCAL command_t **debugger_commands;
69
 
/* commands that can be executed when the debug mode is invoked */
70
 
 
71
 
/* functions ================================================================*/
72
 
 
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. */
76
 
{
77
 
  int_t line;
78
 
  string_t file;
79
 
  
80
 
  switch (debug_mode)
81
 
  {
82
 
  case NEXT_MODE:
83
 
    if (backup_top < step.backup_top)
84
 
      /* Current path is finished. Debug older path. */
85
 
    {
86
 
      step.nested_subrules = nested_subrules;
87
 
      step.backup_top = backup_top;
88
 
    }
89
 
    if (nested_subrules > step.nested_subrules)
90
 
      break;
91
 
    /* fall through to STEP_MODE */
92
 
  case 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))
96
 
      break;
97
 
    
98
 
    step.nested_subrules = nested_subrules;
99
 
    step.line = line;
100
 
    step.file = file;
101
 
    
102
 
    /* Ignore interactive mode while number of steps > 1. */
103
 
    if (step.repetitions > 1)
104
 
      step.repetitions--;
105
 
    else
106
 
      return TRUE;
107
 
    break;
108
 
  case WALK_MODE:
109
 
    if (rule_sys->rules[executed_rule_number].first_instr == pc)
110
 
      return TRUE;
111
 
    break;
112
 
  default:
113
 
    break;
114
 
  }
115
 
  return FALSE;
116
 
}
117
 
 
118
 
/*---------------------------------------------------------------------------*/
119
 
 
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>. */
122
 
{
123
 
  switch (new_debug_mode)
124
 
    {
125
 
    case RUN_MODE:
126
 
      debug_rule_sys = NULL;
127
 
      break;
128
 
    case STEP_MODE:
129
 
    case NEXT_MODE:
130
 
      step.backup_top = 0;
131
 
      step.nested_subrules = 0;
132
 
      step.line = -1;
133
 
      step.file = NULL;
134
 
      step.repetitions = 0;
135
 
      debug_rule_sys = rule_sys;
136
 
      break;
137
 
    case GO_MODE:
138
 
    case WALK_MODE:
139
 
      debug_rule_sys = rule_sys;
140
 
      break;
141
 
    }
142
 
  debug_mode = new_debug_mode;
143
 
}
144
 
 
145
 
/*---------------------------------------------------------------------------*/
146
 
 
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. */
150
 
{
151
 
  rule_t *rule = executed_rule_sys->rules + executed_rule_number;
152
 
  
153
 
  if (rule->first_instr == pc) 
154
 
    print_rule ();
155
 
}
156
 
 
157
 
/*---------------------------------------------------------------------------*/
158
 
 
159
 
LOCAL void display_variables (void)
160
 
{
161
 
  string_t value_string;
162
 
  int_t stack_top = inspect_stack_pointer ();
163
 
  int_t i;
164
 
 
165
 
  start_display_process ();
166
 
 
167
 
  fprintf (display_stream, "variables\n");
168
 
 
169
 
  for (i = first_variable_index (executed_rule_sys, pc); i < stack_top; i++)
170
 
  {
171
 
    string_t var_name = variable_at_index (executed_rule_sys, i, pc);
172
 
    
173
 
    if (var_name != NULL && inspect_stack (i) != NULL)
174
 
    {
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);
178
 
    }
179
 
  }
180
 
  
181
 
  fprintf (display_stream, "end\n");
182
 
  fflush (display_stream);
183
 
}
184
 
 
185
 
/*---------------------------------------------------------------------------*/
186
 
 
187
 
LOCAL void debugger_debug_rule (void)
188
 
/* Called from "execute_rule" before instruction pointed to by
189
 
 * <pc> is executed. */
190
 
{
191
 
  int_t breakpoint_number;
192
 
 
193
 
  executing_rule = FALSE;
194
 
  in_debugger = TRUE;
195
 
  
196
 
  breakpoint_number = at_breakpoint (executed_rule_sys, pc);
197
 
  if (breakpoint_number != 0 || at_next_line (executed_rule_sys, pc))
198
 
  {
199
 
    int_t line;
200
 
    string_t file, rule;
201
 
    int_t old_top;
202
 
  
203
 
    source_of_instr (executed_rule_sys, pc, &line, NULL, &file, &rule);
204
 
    
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);
211
 
    
212
 
    if (getenv ("MALAGA_MODE") != NULL)
213
 
      printf ("SHOW \"%s\":%ld:0\n", file, line);
214
 
    
215
 
    /* check if we are at rule start and print an appropriate message */
216
 
    check_if_rule_start ();
217
 
 
218
 
    if (show_variables)
219
 
      display_variables ();
220
 
 
221
 
    old_top = top;
222
 
    command_loop ("debug", debugger_commands);
223
 
    top = old_top;
224
 
    if (quit_requested)
225
 
      set_debug_mode (RUN_MODE, NULL);
226
 
  }
227
 
  
228
 
  in_debugger = FALSE;
229
 
  executing_rule = TRUE;
230
 
}
231
 
 
232
 
/*---------------------------------------------------------------------------*/
233
 
 
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. */
239
 
{
240
 
  print_rule = my_print_rule;
241
 
  debugger_commands = my_debugger_commands;
242
 
  debug_rule = debugger_debug_rule;
243
 
}
244
 
 
245
 
/*---------------------------------------------------------------------------*/
246
 
 
247
 
GLOBAL void terminate_debugger (void)
248
 
/* Terminate the debugger module. */
249
 
{
250
 
  stop_display_process ();
251
 
  debug_rule = NULL;
252
 
}
253
 
 
254
 
/*---------------------------------------------------------------------------*/
255
 
 
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>. */
258
 
{
259
 
  int_t line;
260
 
  string_t file;
261
 
  int_t repetitions;
262
 
 
263
 
  if (! in_debugger)
264
 
    error ("not in debugging mode");
265
 
  
266
 
  source_of_instr (executed_rule_sys, pc, &line, NULL, &file, NULL);
267
 
  
268
 
  DB_ASSERT (line != -1);
269
 
  
270
 
  if (*argument == EOS)
271
 
    repetitions = 0;
272
 
  else
273
 
    repetitions = parse_int (&argument);
274
 
  parse_end (argument);
275
 
  
276
 
  set_debug_mode (debug_mode, executed_rule_sys);
277
 
  step.backup_top = backup_top;
278
 
  step.nested_subrules = nested_subrules;
279
 
  step.line = line;
280
 
  step.file = file;
281
 
  step.repetitions = repetitions;
282
 
}
283
 
 
284
 
/*---------------------------------------------------------------------------*/
285
 
 
286
 
LOCAL void do_run (string_t arguments)
287
 
/* Continue rule execution in non-debugging mode. */
288
 
{
289
 
  if (! in_debugger)
290
 
    error ("not in debugging mode");
291
 
 
292
 
  parse_end (arguments);
293
 
  set_debug_mode (RUN_MODE, NULL);
294
 
  leave_command_loop = TRUE;
295
 
}
296
 
 
297
 
GLOBAL command_t run_command =
298
 
{
299
 
  "run r", do_run,
300
 
  "Return to non-debugging mode and complete rule execution.\n"
301
 
  "Usage: run\n"
302
 
  "\"run\" can only be used in debug mode.\n"
303
 
};
304
 
 
305
 
/*---------------------------------------------------------------------------*/
306
 
 
307
 
LOCAL void do_go (string_t arguments)
308
 
/* Continue rule execution until next breakpoint is met or end is reached. */
309
 
{
310
 
  if (! in_debugger)
311
 
    error ("not in debugging mode");
312
 
 
313
 
  parse_end (arguments);
314
 
  set_debug_mode (GO_MODE, executed_rule_sys);
315
 
  leave_command_loop = TRUE;
316
 
}
317
 
 
318
 
GLOBAL command_t go_command =
319
 
{
320
 
  "go g", do_go,
321
 
  "Execute rules until a breakpoint is hit or analysis is completed.\n"
322
 
  "Usage: go\n"
323
 
  "\"go\" can only be used in debug mode.\n"
324
 
};
325
 
 
326
 
/*---------------------------------------------------------------------------*/
327
 
 
328
 
LOCAL void do_walk (string_t arguments)
329
 
/* Continue rule execution until next rule or breakpoint is met 
330
 
 * or end is reached. */
331
 
{
332
 
  if (! in_debugger)
333
 
    error ("not in debugging mode");
334
 
 
335
 
  parse_end (arguments);
336
 
  set_debug_mode (WALK_MODE, executed_rule_sys);
337
 
  leave_command_loop = TRUE;
338
 
}
339
 
 
340
 
GLOBAL command_t walk_command =
341
 
{
342
 
  "walk w", do_walk,
343
 
  "Walk to the next rule.\n"
344
 
  "Usage: walk\n"
345
 
  "\"walk\" can only be used in debug mode.\n"
346
 
};
347
 
 
348
 
/*---------------------------------------------------------------------------*/
349
 
 
350
 
LOCAL void do_step (string_t argument)
351
 
/* Continue until control reaches a different source line. */
352
 
{
353
 
  step_or_next (STEP_MODE, argument);
354
 
  leave_command_loop = TRUE;
355
 
}
356
 
 
357
 
GLOBAL command_t step_command =
358
 
{
359
 
  "step s", do_step,
360
 
  "Step to the next source line.\n"
361
 
  "Usage:\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"
365
 
};
366
 
 
367
 
/*---------------------------------------------------------------------------*/
368
 
 
369
 
LOCAL void do_next (string_t argument)
370
 
/* Continue until control reaches a different source line, but jump over
371
 
 * subrules. */
372
 
{
373
 
  step_or_next (NEXT_MODE, argument);
374
 
  leave_command_loop = TRUE;
375
 
}
376
 
 
377
 
GLOBAL command_t next_command =
378
 
{
379
 
  "next n", do_next,
380
 
  "Step to the next source line, but jump over subrules.\n"
381
 
  "Usage:\n"
382
 
  "  next <repetitions> -- execute <repetitions> steps\n"
383
 
  "  next -- execute one step\n"
384
 
  "\"next\" can only be used in debug mode.\n"
385
 
};
386
 
 
387
 
/*---------------------------------------------------------------------------*/
388
 
 
389
 
LOCAL void do_trace (string_t arguments)
390
 
/* Print all active subrules and rules in reverse order. */
391
 
{
392
 
  int_t index;
393
 
  
394
 
  if (pc == -1)
395
 
    error ("no rule executed");
396
 
 
397
 
  parse_end (arguments);
398
 
 
399
 
  index = 0;
400
 
  do
401
 
  {
402
 
    string_t file, rule;
403
 
    int_t line;
404
 
    int_t pc_index;
405
 
 
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);
411
 
}
412
 
 
413
 
GLOBAL command_t trace_command =
414
 
{
415
 
  "trace", do_trace,
416
 
  "Print the active subrules in reverse order.\n"
417
 
  "Usage: trace\n"
418
 
  "\"trace\" can only be used in debug mode or after a rule execution error.\n"
419
 
};
420
 
 
421
 
/*---------------------------------------------------------------------------*/
422
 
 
423
 
LOCAL void do_print (string_t argument)
424
 
/* Print content of variables. */
425
 
{
426
 
  int_t stack_index;
427
 
  string_t value_string;
428
 
  value_t value;
429
 
 
430
 
  if (pc == -1)
431
 
    error ("no rule executed");
432
 
 
433
 
  if (*argument != EOS) 
434
 
  {
435
 
    /* If any arguments given, treat each argument as a variable. */
436
 
    while (*argument != EOS) 
437
 
    {
438
 
      string_t path = parse_word (&argument);
439
 
      string_t section, sect_start, sect_end;
440
 
      
441
 
      if (*path != '$')
442
 
        error ("variable names start with \"$\"");
443
 
      sect_start = path + 1;
444
 
      
445
 
      /* Find end of variable name. */
446
 
      sect_end = sect_start;
447
 
      while (*sect_end != EOS && *sect_end != '.')
448
 
        sect_end++;
449
 
      
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);
455
 
      free_mem (&section);
456
 
      value = inspect_stack (stack_index);
457
 
      
458
 
      while (*sect_end != EOS)
459
 
      {
460
 
        /* Skip "." and remember section start. */
461
 
        sect_end++;
462
 
        sect_start = sect_end;
463
 
        
464
 
        /* Find end of section. */
465
 
        while (*sect_end != EOS && *sect_end != '.')
466
 
          sect_end++;
467
 
        
468
 
        section = new_string (sect_start, sect_end);
469
 
        if (isdigit (*section) || *section == '-')
470
 
        {
471
 
          int_t index;
472
 
          string_t s;
473
 
 
474
 
          s = section;
475
 
          index = parse_int (&s);
476
 
          value = get_element (value, index);
477
 
        }
478
 
        else
479
 
          value = get_attribute (value, find_symbol (section));
480
 
 
481
 
        free_mem (&section);
482
 
        if (value == NULL) 
483
 
          break;
484
 
      }   
485
 
      
486
 
      if (value == NULL)
487
 
        printf ("path \"%s\" doesn't exist\n", path);
488
 
      else
489
 
      {
490
 
        value_string = value_to_readable (value, FALSE);
491
 
        printf ("%s = %s\n", path, value_string);
492
 
        free_mem (&value_string);
493
 
      }
494
 
      
495
 
      free_mem (&path);
496
 
    }
497
 
  } 
498
 
  else 
499
 
  {
500
 
    int_t stack_top = inspect_stack_pointer ();
501
 
    
502
 
    /* Print all variables currently defined. */
503
 
    for (stack_index = first_variable_index (executed_rule_sys, pc); 
504
 
         stack_index < stack_top; 
505
 
         stack_index++) 
506
 
    {
507
 
      string_t var_name;
508
 
      
509
 
      var_name = variable_at_index (executed_rule_sys, stack_index, pc);
510
 
      if (var_name != NULL)
511
 
      {
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);
515
 
      }
516
 
    }
517
 
  }
518
 
}
519
 
 
520
 
GLOBAL command_t print_command = 
521
 
{
522
 
  "print p", do_print,
523
 
  "Print the values of variables.\n"
524
 
  "Usage:\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"
529
 
};
530
 
 
531
 
/*---------------------------------------------------------------------------*/
532
 
 
533
 
LOCAL void do_variables (string_t arguments)
534
 
/* Generate variables file and start TCL program to display variables. */
535
 
{
536
 
  if (pc == -1)
537
 
    error ("no rule executed");
538
 
  
539
 
  parse_end (arguments);
540
 
  display_variables ();
541
 
}
542
 
 
543
 
GLOBAL command_t variables_command = 
544
 
{
545
 
  "variables v", do_variables,
546
 
  "Display current variables in matrix form.\n"
547
 
  "Usage: variables\n"
548
 
  "\"variables\" can only be used in debug mode or after a rule execution"
549
 
  "error.\n"
550
 
};
551
 
 
552
 
/* end of file ==============================================================*/