-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.c
5754 lines (4940 loc) · 133 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* debug.c - gawk debugger
*/
/*
* Copyright (C) 2004, 2010-2013 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GAWK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "awk.h"
#include "cmd.h"
#ifndef O_RDONLY
#include <fcntl.h> /* open() */
#endif
extern bool exiting;
extern SRCFILE *srcfiles;
extern INSTRUCTION *rule_list;
extern INSTRUCTION *code_block;
extern NODE **fcall_list;
extern long fcall_count;
extern FILE *output_fp;
extern IOBUF *curfile;
extern const char *command_file;
extern const char *get_spec_varname(Func_ptr fptr);
extern int zzparse(void);
#define read_command() (void) zzparse()
extern const char *redir2str(int redirtype);
static char *linebuf = NULL; /* used to print a single line of source */
static size_t linebuf_len;
FILE *out_fp;
char *dbg_prompt;
char *commands_prompt = "> "; /* breakpoint or watchpoint commands list */
char *eval_prompt = "@> "; /* awk statement(s) */
bool input_from_tty = false;
int input_fd;
static SRCFILE *cur_srcfile;
static long cur_frame = 0;
static INSTRUCTION *cur_pc;
int cur_rule = 0;
static bool prog_running = false;
struct condition {
INSTRUCTION *code;
AWK_CONTEXT *ctxt;
char *expr;
};
struct commands_item {
struct commands_item *next;
struct commands_item *prev;
int cmd;
char *cmd_string;
CMDARG *arg;
};
/* breakpoint structure */
typedef struct break_point {
struct break_point *next;
struct break_point *prev;
int number;
long ignore_count;
long hit_count;
char *src;
INSTRUCTION *bpi; /* Op_breakpoint */
struct commands_item commands; /* list of commands to run */
bool silent;
struct condition cndn;
short flags;
#define BP_ENABLE 1
#define BP_ENABLE_ONCE 2 /* enable once */
#define BP_TEMP 4
#define BP_IGNORE 8
} BREAKPOINT;
static BREAKPOINT breakpoints = { &breakpoints, &breakpoints, 0 };
#ifdef HAVE_LIBREADLINE
/* do_save -- save command */
static int sess_history_base = 0;
#endif
/* 'list' command */
static int last_printed_line = 0;
static int last_print_count; /* # of lines printed */
/* watch or display item */
struct list_item {
struct list_item *next;
struct list_item *prev;
int number; /* item number */
NODE *symbol; /* variable or function param */
NODE **subs; /* subscripts */
int num_subs; /* subscript(dimension) count */
char *sname; /* symbol or param name */
long fcall_count;
struct commands_item commands;
int silent;
struct condition cndn;
/* This is for the value of the watched item */
union {
NODE *n;
long l;
} value[2];
#define cur_value value[0].n
#define cur_size value[0].l
#define old_value value[1].n
#define old_size value[1].l
int flags;
#define PARAM 1
#define SUBSCRIPT 2
#define FIELD_NUM 4
#define OLD_IS_ARRAY 8 /* old item is array */
#define CUR_IS_ARRAY 16 /* current item is array */
};
#define IS_PARAM(d) (((d)->flags & PARAM) != 0)
#define IS_SUBSCRIPT(d) (((d)->flags & SUBSCRIPT) != 0)
#define IS_FIELD(d) (((d)->flags & FIELD_NUM) != 0)
#define WATCHING_ARRAY(d) (((d)->flags & CUR_IS_ARRAY) != 0)
static struct list_item display_list = { &display_list, &display_list, 0 };
static struct list_item watch_list = { &watch_list, &watch_list, 0 };
/* Structure to maintain data for processing debugger commands */
static struct {
long fcall_count; /* 'finish', 'until', 'next', 'step', 'nexti' commands */
int sourceline; /* source line number last
* time we stopped execution,
* used by next, until and step commands
*/
char *source; /* next, until and step */
INSTRUCTION *pc; /* 'until' and 'return' commands */
int repeat_count; /* 'step', 'next', 'stepi', 'nexti' commands */
bool print_frame; /* print frame info, 'finish' and 'until' */
bool print_ret; /* print returned value, 'finish' */
int break_point; /* non-zero (breakpoint number) if stopped at break point */
int watch_point; /* non-zero (watchpoint number) if stopped at watch point */
int (*check_func)(INSTRUCTION **); /* function to decide when to suspend
* awk interpreter and return control
* to debugger command interpreter.
*/
enum argtype command; /* command type */
} stop;
/* restart related stuff */
extern char **d_argv; /* copy of argv array */
static bool need_restart = false;
enum { BREAK=1, WATCH, DISPLAY, HISTORY, OPTION };
static const char *const env_variable[] = {
"",
"DGAWK_BREAK",
"DGAWK_WATCH",
"DGAWK_DISPLAY",
"DGAWK_HISTORY",
"DGAWK_OPTION",
};
static void serialize(int );
static void unserialize(int );
static const char *commands_string = NULL;
static int commands_string_len = 0;
static char line_sep;
#define FSEP (char)'\037'
#define RSEP (char)'\036'
#define CSEP (char)'\035'
/* debugger option */
struct dbg_option {
const char *name;
int *num_val;
char **str_val;
void (*assign)(const char *);
const char *help_txt;
};
#define DEFAULT_HISTFILE "./.gawk_history"
#define DEFAULT_OPTFILE "./.gawkrc"
#define DEFAULT_PROMPT "gawk> "
#define DEFAULT_LISTSIZE 15
#define DEFAULT_HISTSIZE 100
static void set_gawk_output(const char *file);
static void set_prompt(const char *value);
static void set_listsize(const char *value);
static void set_trace(const char *value);
static void set_save_history(const char *value);
static void set_save_options(const char *value);
static void set_history_size(const char *value);
static const char *options_file = DEFAULT_OPTFILE;
#ifdef HAVE_LIBREADLINE
static const char *history_file = DEFAULT_HISTFILE;
#endif
/* debugger option related variables */
static char *output_file = "/dev/stdout"; /* gawk output redirection */
char *dgawk_prompt = NULL; /* initialized in interpret */
static int list_size = DEFAULT_LISTSIZE; /* # of lines that 'list' prints */
static int do_trace = false;
static int do_save_history = true;
static int do_save_options = true;
static int history_size = DEFAULT_HISTSIZE; /* max # of lines in history file */
static const struct dbg_option option_list[] = {
{"history_size", &history_size, NULL, &set_history_size,
gettext_noop("set or show the number of lines to keep in history file.") },
{"listsize", &list_size, NULL, &set_listsize,
gettext_noop("set or show the list command window size.") },
{"outfile", NULL, &output_file, &set_gawk_output,
gettext_noop("set or show gawk output file.") },
{"prompt", NULL, &dgawk_prompt, &set_prompt,
gettext_noop("set or show debugger prompt."), },
{"save_history", &do_save_history, NULL, &set_save_history,
gettext_noop("(un)set or show saving of command history (value=on|off).") },
{"save_options", &do_save_options, NULL, &set_save_options,
gettext_noop("(un)set or show saving of options (value=on|off).") },
{"trace", &do_trace, NULL, &set_trace,
gettext_noop("(un)set or show instruction tracing (value=on|off).") },
{0, NULL, NULL, NULL, 0},
};
static void save_options(const char *file);
/* pager */
jmp_buf pager_quit_tag;
bool pager_quit_tag_valid = false;
static int screen_width = INT_MAX; /* no of columns */
static int screen_height = INT_MAX; /* no of rows */
static int pager_lines_printed = 0; /* no of lines printed so far */
static void restart(bool run) ATTRIBUTE_NORETURN;
static void close_all(void);
static int open_readfd(const char *file);
static int find_lines(SRCFILE *s);
static SRCFILE *source_find(char *src);
static int print_lines(char *src, int start_line, int nlines);
static void print_symbol(NODE *r, bool isparam);
static NODE *find_frame(long num);
static NODE *find_param(const char *name, long num, char **pname);
static NODE *find_symbol(const char *name, char **pname);
static NODE *find_array(const char *name);
static void print_field(long field_num);
static int print_function(INSTRUCTION *pc, void *);
static void print_frame(NODE *func, char *src, int srcline);
static void print_numbered_frame(long num);
static void print_cur_frame_and_sourceline(void);
static INSTRUCTION *find_rule(char *src, long lineno);
static INSTRUCTION *mk_breakpoint(char *src, int srcline);
static int execute_commands(struct commands_item *commands);
static void delete_commands_item(struct commands_item *c);
static NODE *execute_code(volatile INSTRUCTION *code);
static int pre_execute_code(INSTRUCTION **pi);
static int parse_condition(int type, int num, char *expr);
static BREAKPOINT *add_breakpoint(INSTRUCTION *prevp, INSTRUCTION *ip, char *src, bool silent);
static BREAKPOINT *set_breakpoint_next(INSTRUCTION *rp, INSTRUCTION *ip);
static BREAKPOINT *set_breakpoint_at(INSTRUCTION *rp, int lineno, bool silent);
static void delete_breakpoint(BREAKPOINT *b);
static BREAKPOINT *find_breakpoint(long num);
static void display(struct list_item *d);
static struct list_item *find_item(struct list_item *list, long num);
static struct list_item *add_item(struct list_item *list, int type, NODE *symbol, char *pname);
static void delete_item(struct list_item *d);
static int breakpoint_triggered(BREAKPOINT *b);
static int watchpoint_triggered(struct list_item *w);
static void print_instruction(INSTRUCTION *pc, Func_print print_func, FILE *fp, int in_dump);
static int print_code(INSTRUCTION *pc, void *x);
static void next_command();
static void debug_post_execute(INSTRUCTION *pc);
static int debug_pre_execute(INSTRUCTION **pi);
static char *g_readline(const char *prompt);
static int prompt_yes_no(const char *, char , int , FILE *);
static struct pf_data {
Func_print print_func;
bool defn;
FILE *fp;
} pf_data;
char * (*read_a_line)(const char *) = 0; /* reads a line of input */
struct command_source
{
int fd;
int is_tty;
char * (*read_func)(const char *);
int (*close_func)(int);
int eof_status; /* see push_cmd_src */
int cmd; /* D_source or 0 */
char *str; /* sourced file */
struct command_source *next;
};
static struct command_source *cmd_src = NULL;
#define CHECK_PROG_RUNNING() \
do { \
if (! prog_running) { \
d_error(_("program not running.")); \
return false; \
} \
} while (false)
/* g_readline -- read a line of text; the interface is like 'readline' but
* without any command-line editing; used when not compiled with
* readline support and/or input is not from terminal (prompt set to NULL).
*/
static char *
g_readline(const char *prompt)
{
char *line;
size_t line_size = 100;
static char buf[2];
char *p, *end;
int n;
if (input_from_tty && prompt && *prompt)
fprintf(out_fp, "%s", prompt);
emalloc(line, char *, line_size + 1, "g_readline");
p = line;
end = line + line_size;
while ((n = read(input_fd, buf, 1)) > 0) {
if (buf[0] == '\n') {
if (p > line && p[-1] == '\r')
p--;
break;
}
if (p == end) {
erealloc(line, char *, 2 * line_size + 1, "g_readline");
p = line + line_size;
line_size *= 2;
end = line + line_size;
}
*p++ = buf[0];
}
if (n == -1 || (n == 0 && p == line)) {
efree(line);
return NULL;
}
*p = '\0';
return line;
}
/* d_error --- print an error message */
void
d_error(const char *mesg, ...)
{
va_list args;
va_start(args, mesg);
fprintf(out_fp, _("error: "));
vfprintf(out_fp, mesg, args);
fprintf(out_fp, "\n");
va_end(args);
}
/* find_lines --- find the positions of the lines in the source file. */
static int
find_lines(SRCFILE *s)
{
char *buf, *p, *end;
int n;
int ofs = 0;
int *pos;
int pos_size;
int maxlen = 0;
int numlines = 0;
char lastchar = '\0';
emalloc(buf, char *, s->bufsize, "find_lines");
pos_size = s->srclines;
emalloc(s->line_offset, int *, (pos_size + 2) * sizeof(int), "find_lines");
pos = s->line_offset;
pos[0] = 0;
while ((n = read(s->fd, buf, s->bufsize)) > 0) {
end = buf + n;
lastchar = buf[n - 1];
p = buf;
while (p < end) {
if (*p++ == '\n') {
if (++numlines > pos_size) {
erealloc(s->line_offset, int *, (2 * pos_size + 2) * sizeof(int), "find_lines");
pos = s->line_offset + pos_size;
pos_size *= 2;
}
*++pos = ofs + (p - buf);
if ((pos[0] - pos[-1]) > maxlen)
maxlen = pos[0] - pos[-1]; /* length including NEWLINE */
}
}
ofs += n;
}
efree(buf);
if (n == -1) {
d_error(_("can't read source file `%s' (%s)"),
s->src, strerror(errno));
return -1;
}
if (ofs <= 0) {
fprintf(out_fp, _("source file `%s' is empty.\n"), s->src);
return -1;
}
if (lastchar != '\n') {
/* fake a NEWLINE at end */
*++pos = ofs + 1;
numlines++;
if ((pos[0] - pos[-1]) > maxlen)
maxlen = pos[0] - pos[-1];
}
s->maxlen = maxlen;
s->srclines = numlines;
return 0;
}
/* source_find --- return the SRCFILE struct for the source 'src' */
static SRCFILE *
source_find(char *src)
{
SRCFILE *s;
struct stat sbuf;
char *path;
int errno_val = 0;
if (src == NULL || *src == '\0') {
d_error(_("no current source file."));
return NULL;
}
if (cur_srcfile->src == src) /* strcmp(cur_srcfile->src, src) == 0 */
return cur_srcfile;
for (s = srcfiles->next; s != srcfiles; s = s->next) {
if ((s->stype == SRC_FILE || s->stype == SRC_INC)
&& strcmp(s->src, src) == 0)
return s;
}
path = find_source(src, & sbuf, & errno_val, false);
if (path != NULL) {
for (s = srcfiles->next; s != srcfiles; s = s->next) {
if ((s->stype == SRC_FILE || s->stype == SRC_INC)
&& files_are_same(path, s)) {
efree(path);
return s;
}
}
efree(path);
}
d_error(_("cannot find source file named `%s' (%s)"), src, strerror(errno_val));
return NULL;
}
/* print_lines --- print source lines, and update 'cur_srcfile' */
static int
print_lines(char *src, int start_line, int nlines)
{
SRCFILE *s;
int *pos;
int i;
struct stat sbuf;
s = source_find(src);
if (s == NULL)
return -1;
if (s->fd <= INVALID_HANDLE && (s->fd = srcopen(s)) <= INVALID_HANDLE) {
d_error(_("can't open source file `%s' for reading (%s)"),
src, strerror(errno));
return -1;
}
if (fstat(s->fd, &sbuf) == 0 && s->mtime < sbuf.st_mtime) {
fprintf(out_fp, _("WARNING: source file `%s' modified since program compilation.\n"),
src);
efree(s->line_offset);
s->line_offset = NULL;
s->mtime = sbuf.st_mtime;
/* reopen source file */
close(s->fd);
s->fd = INVALID_HANDLE;
if ((s->fd = srcopen(s)) <= INVALID_HANDLE) {
d_error(_("can't open source file `%s' for reading (%s)"),
src, strerror(errno));
return -1;
}
}
if (s->line_offset == NULL && find_lines(s) != 0)
return -1;
if (start_line < 1 || start_line > s->srclines) {
d_error(_("line number %d out of range; `%s' has %d lines"),
start_line, src, s->srclines);
return -1;
}
assert(nlines > 0);
if ((start_line + nlines - 1) > s->srclines)
nlines = s->srclines - start_line + 1;
pos = s->line_offset;
if (lseek(s->fd, (off_t) pos[start_line - 1], SEEK_SET) < 0) {
d_error("%s: %s", src, strerror(errno));
return -1;
}
if (linebuf == NULL) {
emalloc(linebuf, char *, s->maxlen + 20, "print_lines"); /* 19 for line # */
linebuf_len = s->maxlen;
} else if (linebuf_len < s->maxlen) {
erealloc(linebuf, char *, s->maxlen + 20, "print_lines");
linebuf_len = s->maxlen;
}
for (i = start_line; i < start_line + nlines; i++) {
int supposed_len, len;
char *p;
sprintf(linebuf, "%-8d", i);
/* mark the line about to be executed with =>; nlines > 1
* condition makes sure that we are in list command
*/
if (nlines > 1) {
BREAKPOINT *b;
bool has_bpt = false;
for (b = breakpoints.prev; b != &breakpoints; b = b->prev) {
if (src == b->src && i == b->bpi->source_line) {
has_bpt = true;
break;
}
}
if (prog_running && src == source && i == sourceline) {
if (has_bpt)
sprintf(linebuf, "%-4d:b=>", i);
else
sprintf(linebuf, "%-4d =>", i);
} else if (has_bpt)
sprintf(linebuf, "%-4d:b ", i);
}
p = linebuf + strlen(linebuf);
supposed_len = pos[i] - pos[i - 1];
len = read(s->fd, p, supposed_len);
switch (len) {
case -1:
d_error(_("can't read source file `%s' (%s)"),
src, strerror(errno));
return -1;
case 0:
d_error(_("unexpected eof while reading file `%s', line %d"),
src, i);
return -1;
default:
if (i == s->srclines && p[len - 1] != '\n')
p[len++] = '\n';
#if 0
if (len != supposed_len || p[len - 1] != '\n') {
d_error(_("source file `%s' modified since start of program execution"),
src);
return -1;
}
#endif
len += (p - linebuf);
if (fwrite(linebuf, sizeof(char), len, out_fp) != len)
return -1;
}
}
if (cur_srcfile != s) {
if (cur_srcfile->fd != INVALID_HANDLE) {
close(cur_srcfile->fd);
cur_srcfile->fd = INVALID_HANDLE;
}
cur_srcfile = s;
}
return (i - 1); /* no of lines printed */
}
/* do_list --- list command */
int
do_list(CMDARG *arg, int cmd ATTRIBUTE_UNUSED)
{
long line_first, line_last;
long count = list_size;
INSTRUCTION *rp;
char *src = cur_srcfile->src;
line_first = last_printed_line + 1; /* default or no arg */
if (arg == NULL) /* list or list + */
goto list;
switch (arg->type) {
case D_int: /* list n or list - */
if (arg->a_int < 0) { /* list - */
line_first = last_printed_line - last_print_count - list_size + 1;
if (line_first < 1) {
if (last_printed_line != last_print_count)
line_first = 1;
else
return false;
}
} else {
line:
line_first = arg->a_int - list_size / 2;
if (line_first < 1)
line_first = 1;
}
break;
case D_range: /* list m-n */
range:
line_first = arg->a_int;
arg = arg->next;
assert(arg != NULL);
assert(arg->type == D_int);
count = arg->a_int - line_first + 1;
break;
case D_string:
src = arg->a_string;
if (arg->next != NULL) {
arg = arg->next;
if (arg->type == D_int) /* list file:n */
goto line;
else if (arg->type == D_range) /* list file:m-n */
goto range;
else if (arg->type == D_func) /* list file:function */
goto func;
else
line_first = 1;
} else
line_first = 1;
break;
case D_func: /* list function */
func:
rp = arg->a_node->code_ptr;
src = rp->source_file;
line_first = rp->source_line - list_size / 2;
if (line_first < 1)
line_first = 1;
break;
default:
break;
}
list:
line_last = print_lines(src, line_first, count);
if (line_last != -1) {
last_printed_line = line_last;
last_print_count = line_last - line_first + 1;
}
return false;
}
/* do_info --- info command */
int
do_info(CMDARG *arg, int cmd ATTRIBUTE_UNUSED)
{
NODE **table;
if (arg == NULL || arg->type != D_argument)
return false;
switch (arg->a_argument) {
case A_SOURCE:
fprintf(out_fp, _("Current source file: %s\n"), cur_srcfile->src);
fprintf(out_fp, _("Number of lines: %d\n"), cur_srcfile->srclines);
break;
case A_SOURCES:
{
SRCFILE *s;
for (s = srcfiles->next; s != srcfiles; s = s->next) {
fprintf(out_fp, _("Source file (lines): %s (%d)\n"),
(s->stype == SRC_FILE || s->stype == SRC_INC) ? s->src
: "cmd. line",
s->srclines);
}
}
break;
case A_BREAK:
initialize_pager(out_fp);
if (setjmp(pager_quit_tag) == 0) {
BREAKPOINT *b;
struct commands_item *c;
gprintf(out_fp, _("Number Disp Enabled Location\n\n"));
for (b = breakpoints.prev; b != &breakpoints; b = b->prev) {
char *disp = "keep";
if ((b->flags & BP_ENABLE_ONCE) != 0)
disp = "dis";
else if ((b->flags & BP_TEMP) != 0)
disp = "del";
gprintf(out_fp, "%-6d %-4.4s %-7.7s file %s, line #%d\n",
b->number, disp, (b->flags & BP_ENABLE) != 0 ? "yes" : "no",
b->src, b->bpi->source_line);
if (b->hit_count > 0)
gprintf(out_fp, _("\tno of hits = %ld\n"), b->hit_count);
if ((b->flags & BP_IGNORE) != 0)
gprintf(out_fp, _("\tignore next %ld hit(s)\n"), b->ignore_count);
if (b->cndn.code != NULL)
gprintf(out_fp, _("\tstop condition: %s\n"), b->cndn.expr);
if (b->commands.next != &b->commands)
gprintf(out_fp, _("\tcommands:\n"));
for (c = b->commands.next; c != &b->commands; c = c->next) {
gprintf(out_fp, "\t%s\n", c->cmd_string);
if (c->cmd == D_eval) {
char *start, *end;
CMDARG *a = c->arg;
start = strchr(a->a_string, '{');
end = strrchr(a->a_string, '}');
if (start == NULL || end == NULL)
continue;
start++;
*end = '\0';
gprintf(out_fp, "%s", start); /* FIXME: translate ? */
*end = '}';
}
}
}
}
break;
case A_FRAME:
CHECK_PROG_RUNNING();
fprintf(out_fp, _("Current frame: "));
print_numbered_frame(cur_frame);
if (cur_frame < fcall_count) {
fprintf(out_fp, _("Called by frame: "));
print_numbered_frame(cur_frame + 1);
}
if (cur_frame > 0) {
fprintf(out_fp, _("Caller of frame: "));
print_numbered_frame(cur_frame - 1);
}
break;
case A_ARGS:
case A_LOCALS:
{
NODE *f, *func;
INSTRUCTION *pc;
int arg_count, pcount;
int i, from, to;
CHECK_PROG_RUNNING();
f = find_frame(cur_frame);
func = f->func_node;
if (func == NULL) {
/* print ARGV ? */
fprintf(out_fp, _("None in main().\n"));
return false;
}
pcount = func->param_cnt; /* # of defined params */
pc = (INSTRUCTION *) f->reti; /* Op_func_call instruction */
arg_count = (pc + 1)->expr_count; /* # of arguments supplied */
if (arg_count > pcount) /* extra args */
arg_count = pcount;
if (arg->a_argument == A_ARGS) {
from = 0;
to = arg_count - 1;
} else {
from = arg_count;
to = pcount - 1;
}
for (i = from; i <= to; i++) {
NODE *r;
r = f->stack[i];
if (r->type == Node_array_ref)
r = r->orig_array;
fprintf(out_fp, "%s = ", func->fparms[i].param);
print_symbol(r, true);
}
if (to < from)
fprintf(out_fp, "%s",
arg->a_argument == A_ARGS ?
_("No arguments.\n") :
_("No locals.\n"));
}
break;
case A_VARIABLES:
table = variable_list();
initialize_pager(out_fp);
if (setjmp(pager_quit_tag) == 0) {
gprintf(out_fp, _("All defined variables:\n\n"));
print_vars(table, gprintf, out_fp);
}
efree(table);
break;
case A_FUNCTIONS:
table = function_list(true);
initialize_pager(out_fp);
if (setjmp(pager_quit_tag) == 0) {
gprintf(out_fp, _("All defined functions:\n\n"));
pf_data.print_func = gprintf;
pf_data.fp = out_fp;
pf_data.defn = true;
(void) foreach_func(table,
(int (*)(INSTRUCTION *, void *)) print_function,
&pf_data);
}
efree(table);
break;
case A_DISPLAY:
case A_WATCH:
initialize_pager(out_fp);
if (setjmp(pager_quit_tag) == 0) {
struct list_item *d, *list;
if (arg->a_argument == A_DISPLAY) {
list = &display_list;
gprintf(out_fp, _("Auto-display variables:\n\n"));
} else {
list = &watch_list;
gprintf(out_fp, _("Watch variables:\n\n"));
}
for (d = list->prev; d != list; d = d->prev) {
int i;
struct commands_item *c;
NODE *symbol = d->symbol;
if (IS_SUBSCRIPT(d)) {
gprintf(out_fp, "%d:\t%s", d->number, d->sname);
for (i = 0; i < d->num_subs; i++) {
NODE *sub;
sub = d->subs[i];
gprintf(out_fp, "[\"%s\"]", sub->stptr);
}
gprintf(out_fp, "\n");
} else if (IS_FIELD(d))
gprintf(out_fp, "%d:\t$%ld\n", d->number, get_number_si(symbol));
else
gprintf(out_fp, "%d:\t%s\n", d->number, d->sname);
if (d->cndn.code != NULL)
gprintf(out_fp, _("\tstop condition: %s\n"), d->cndn.expr);
if (d->commands.next != &d->commands)
gprintf(out_fp, _("\tcommands:\n"));
for (c = d->commands.next; c != &d->commands; c = c->next) {
gprintf(out_fp, "\t%s\n", c->cmd_string);
if (c->cmd == D_eval) {
char *start, *end;
CMDARG *a = c->arg;
start = strchr(a->a_string, '{');
end = strrchr(a->a_string, '}');
if (start == NULL || end == NULL)
continue;
start++;
*end = '\0';
gprintf(out_fp, "%s", start); /* FIXME: translate ? */
*end = '}';
}
}
}
}
break;
default:
break;
}
return false;
}
/* print_symbol --- print a symbol table entry */
static void
print_symbol(NODE *r, bool isparam)
{
switch (r->type) {
case Node_var_new:
fprintf(out_fp, "untyped variable\n");
break;
case Node_var:
if (! isparam && r->var_update)
r->var_update();
valinfo(r->var_value, fprintf, out_fp);
break;
case Node_var_array:
fprintf(out_fp, "array, %ld elements\n", assoc_length(r));
break;
case Node_func:
fprintf(out_fp, "`function'\n");
break;
default:
break;
}
}
/* find_frame --- find frame given a frame number */
static NODE *
find_frame(long num)
{
assert(num >= 0);
if (num == 0)
return frame_ptr;
assert(prog_running == true);
assert(num <= fcall_count);
assert(fcall_list[num] != NULL);
return fcall_list[num];
}
/* find_param --- find a function parameter in a given frame number */
static NODE *
find_param(const char *name, long num, char **pname)
{
NODE *r = NULL;
NODE *f;
char *fparam;
if (pname)
*pname = NULL;
if (num < 0 || num > fcall_count || name == NULL)
return NULL;
f = find_frame(num);
if (f->func_node != NULL) { /* in function */
NODE *func;
int i, pcount;
func = f->func_node;
pcount = func->param_cnt;
for (i = 0; i < pcount; i++) {
fparam = func->fparms[i].param;
if (strcmp(name, fparam) == 0) {
r = f->stack[i];
if (r->type == Node_array_ref)
r = r->orig_array;
if (pname)
*pname = fparam;
break;
}