]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/tracepoint.c
Create new file regcache.h. Update all uses.
[thirdparty/binutils-gdb.git] / gdb / tracepoint.c
1 /* Tracing functionality for remote targets in custom GDB protocol
2 Copyright 1997, 1998, 2001 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "frame.h"
24 #include "gdbtypes.h"
25 #include "expression.h"
26 #include "gdbcmd.h"
27 #include "value.h"
28 #include "target.h"
29 #include "language.h"
30 #include "gdb_string.h"
31 #include "inferior.h"
32 #include "tracepoint.h"
33 #include "remote.h"
34 #include "linespec.h"
35 #include "completer.h"
36 #include "regcache.h"
37
38 #include "ax.h"
39 #include "ax-gdb.h"
40
41 /* readline include files */
42 #include <readline/readline.h>
43 #include <readline/history.h>
44
45 /* readline defines this. */
46 #undef savestring
47
48 #ifdef HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51
52 /* maximum length of an agent aexpression.
53 this accounts for the fact that packets are limited to 400 bytes
54 (which includes everything -- including the checksum), and assumes
55 the worst case of maximum length for each of the pieces of a
56 continuation packet.
57
58 NOTE: expressions get mem2hex'ed otherwise this would be twice as
59 large. (400 - 31)/2 == 184 */
60 #define MAX_AGENT_EXPR_LEN 184
61
62
63 extern int info_verbose;
64 extern void (*readline_begin_hook) (char *, ...);
65 extern char *(*readline_hook) (char *);
66 extern void (*readline_end_hook) (void);
67 extern void x_command (char *, int);
68 extern int addressprint; /* Print machine addresses? */
69
70 /* GDB commands implemented in other modules:
71 */
72
73 extern void output_command (char *, int);
74 extern void registers_info (char *, int);
75 extern void args_info (char *, int);
76 extern void locals_info (char *, int);
77
78
79 /* If this definition isn't overridden by the header files, assume
80 that isatty and fileno exist on this system. */
81 #ifndef ISATTY
82 #define ISATTY(FP) (isatty (fileno (FP)))
83 #endif
84
85 /*
86 Tracepoint.c:
87
88 This module defines the following debugger commands:
89 trace : set a tracepoint on a function, line, or address.
90 info trace : list all debugger-defined tracepoints.
91 delete trace : delete one or more tracepoints.
92 enable trace : enable one or more tracepoints.
93 disable trace : disable one or more tracepoints.
94 actions : specify actions to be taken at a tracepoint.
95 passcount : specify a pass count for a tracepoint.
96 tstart : start a trace experiment.
97 tstop : stop a trace experiment.
98 tstatus : query the status of a trace experiment.
99 tfind : find a trace frame in the trace buffer.
100 tdump : print everything collected at the current tracepoint.
101 save-tracepoints : write tracepoint setup into a file.
102
103 This module defines the following user-visible debugger variables:
104 $trace_frame : sequence number of trace frame currently being debugged.
105 $trace_line : source line of trace frame currently being debugged.
106 $trace_file : source file of trace frame currently being debugged.
107 $tracepoint : tracepoint number of trace frame currently being debugged.
108 */
109
110
111 /* ======= Important global variables: ======= */
112
113 /* Chain of all tracepoints defined. */
114 struct tracepoint *tracepoint_chain;
115
116 /* Number of last tracepoint made. */
117 static int tracepoint_count;
118
119 /* Number of last traceframe collected. */
120 static int traceframe_number;
121
122 /* Tracepoint for last traceframe collected. */
123 static int tracepoint_number;
124
125 /* Symbol for function for last traceframe collected */
126 static struct symbol *traceframe_fun;
127
128 /* Symtab and line for last traceframe collected */
129 static struct symtab_and_line traceframe_sal;
130
131 /* Tracing command lists */
132 static struct cmd_list_element *tfindlist;
133
134 /* ======= Important command functions: ======= */
135 static void trace_command (char *, int);
136 static void tracepoints_info (char *, int);
137 static void delete_trace_command (char *, int);
138 static void enable_trace_command (char *, int);
139 static void disable_trace_command (char *, int);
140 static void trace_pass_command (char *, int);
141 static void trace_actions_command (char *, int);
142 static void trace_start_command (char *, int);
143 static void trace_stop_command (char *, int);
144 static void trace_status_command (char *, int);
145 static void trace_find_command (char *, int);
146 static void trace_find_pc_command (char *, int);
147 static void trace_find_tracepoint_command (char *, int);
148 static void trace_find_line_command (char *, int);
149 static void trace_find_range_command (char *, int);
150 static void trace_find_outside_command (char *, int);
151 static void tracepoint_save_command (char *, int);
152 static void trace_dump_command (char *, int);
153
154 /* support routines */
155 static void trace_mention (struct tracepoint *);
156
157 struct collection_list;
158 static void add_aexpr (struct collection_list *, struct agent_expr *);
159 static unsigned char *mem2hex (unsigned char *, unsigned char *, int);
160 static void add_register (struct collection_list *collection,
161 unsigned int regno);
162 static struct cleanup *make_cleanup_free_actions (struct tracepoint *t);
163 static void free_actions_list (char **actions_list);
164 static void free_actions_list_cleanup_wrapper (void *);
165
166 extern void _initialize_tracepoint (void);
167
168 /* Utility: returns true if "target remote" */
169 static int
170 target_is_remote (void)
171 {
172 if (current_target.to_shortname &&
173 strcmp (current_target.to_shortname, "remote") == 0)
174 return 1;
175 else
176 return 0;
177 }
178
179 /* Utility: generate error from an incoming stub packet. */
180 static void
181 trace_error (char *buf)
182 {
183 if (*buf++ != 'E')
184 return; /* not an error msg */
185 switch (*buf)
186 {
187 case '1': /* malformed packet error */
188 if (*++buf == '0') /* general case: */
189 error ("tracepoint.c: error in outgoing packet.");
190 else
191 error ("tracepoint.c: error in outgoing packet at field #%d.",
192 strtol (buf, NULL, 16));
193 case '2':
194 error ("trace API error 0x%s.", ++buf);
195 default:
196 error ("Target returns error code '%s'.", buf);
197 }
198 }
199
200 /* Utility: wait for reply from stub, while accepting "O" packets */
201 static char *
202 remote_get_noisy_reply (char *buf,
203 long sizeof_buf)
204 {
205 do /* loop on reply from remote stub */
206 {
207 QUIT; /* allow user to bail out with ^C */
208 getpkt (buf, sizeof_buf, 0);
209 if (buf[0] == 0)
210 error ("Target does not support this command.");
211 else if (buf[0] == 'E')
212 trace_error (buf);
213 else if (buf[0] == 'O' &&
214 buf[1] != 'K')
215 remote_console_output (buf + 1); /* 'O' message from stub */
216 else
217 return buf; /* here's the actual reply */
218 }
219 while (1);
220 }
221
222 /* Set tracepoint count to NUM. */
223 static void
224 set_tracepoint_count (int num)
225 {
226 tracepoint_count = num;
227 set_internalvar (lookup_internalvar ("tpnum"),
228 value_from_longest (builtin_type_int, (LONGEST) num));
229 }
230
231 /* Set traceframe number to NUM. */
232 static void
233 set_traceframe_num (int num)
234 {
235 traceframe_number = num;
236 set_internalvar (lookup_internalvar ("trace_frame"),
237 value_from_longest (builtin_type_int, (LONGEST) num));
238 }
239
240 /* Set tracepoint number to NUM. */
241 static void
242 set_tracepoint_num (int num)
243 {
244 tracepoint_number = num;
245 set_internalvar (lookup_internalvar ("tracepoint"),
246 value_from_longest (builtin_type_int, (LONGEST) num));
247 }
248
249 /* Set externally visible debug variables for querying/printing
250 the traceframe context (line, function, file) */
251
252 static void
253 set_traceframe_context (CORE_ADDR trace_pc)
254 {
255 static struct type *func_string, *file_string;
256 static struct type *func_range, *file_range;
257 static value_ptr func_val, file_val;
258 static struct type *charstar;
259 int len;
260
261 if (charstar == (struct type *) NULL)
262 charstar = lookup_pointer_type (builtin_type_char);
263
264 if (trace_pc == -1) /* cease debugging any trace buffers */
265 {
266 traceframe_fun = 0;
267 traceframe_sal.pc = traceframe_sal.line = 0;
268 traceframe_sal.symtab = NULL;
269 set_internalvar (lookup_internalvar ("trace_func"),
270 value_from_pointer (charstar, (LONGEST) 0));
271 set_internalvar (lookup_internalvar ("trace_file"),
272 value_from_pointer (charstar, (LONGEST) 0));
273 set_internalvar (lookup_internalvar ("trace_line"),
274 value_from_pointer (builtin_type_int, (LONGEST) - 1));
275 return;
276 }
277
278 /* save as globals for internal use */
279 traceframe_sal = find_pc_line (trace_pc, 0);
280 traceframe_fun = find_pc_function (trace_pc);
281
282 /* save linenumber as "$trace_line", a debugger variable visible to users */
283 set_internalvar (lookup_internalvar ("trace_line"),
284 value_from_longest (builtin_type_int,
285 (LONGEST) traceframe_sal.line));
286
287 /* save func name as "$trace_func", a debugger variable visible to users */
288 if (traceframe_fun == NULL ||
289 SYMBOL_NAME (traceframe_fun) == NULL)
290 set_internalvar (lookup_internalvar ("trace_func"),
291 value_from_pointer (charstar, (LONGEST) 0));
292 else
293 {
294 len = strlen (SYMBOL_NAME (traceframe_fun));
295 func_range = create_range_type (func_range,
296 builtin_type_int, 0, len - 1);
297 func_string = create_array_type (func_string,
298 builtin_type_char, func_range);
299 func_val = allocate_value (func_string);
300 VALUE_TYPE (func_val) = func_string;
301 memcpy (VALUE_CONTENTS_RAW (func_val),
302 SYMBOL_NAME (traceframe_fun),
303 len);
304 func_val->modifiable = 0;
305 set_internalvar (lookup_internalvar ("trace_func"), func_val);
306 }
307
308 /* save file name as "$trace_file", a debugger variable visible to users */
309 if (traceframe_sal.symtab == NULL ||
310 traceframe_sal.symtab->filename == NULL)
311 set_internalvar (lookup_internalvar ("trace_file"),
312 value_from_pointer (charstar, (LONGEST) 0));
313 else
314 {
315 len = strlen (traceframe_sal.symtab->filename);
316 file_range = create_range_type (file_range,
317 builtin_type_int, 0, len - 1);
318 file_string = create_array_type (file_string,
319 builtin_type_char, file_range);
320 file_val = allocate_value (file_string);
321 VALUE_TYPE (file_val) = file_string;
322 memcpy (VALUE_CONTENTS_RAW (file_val),
323 traceframe_sal.symtab->filename,
324 len);
325 file_val->modifiable = 0;
326 set_internalvar (lookup_internalvar ("trace_file"), file_val);
327 }
328 }
329
330 /* Low level routine to set a tracepoint.
331 Returns the tracepoint object so caller can set other things.
332 Does not set the tracepoint number!
333 Does not print anything.
334
335 ==> This routine should not be called if there is a chance of later
336 error(); otherwise it leaves a bogus tracepoint on the chain. Validate
337 your arguments BEFORE calling this routine! */
338
339 static struct tracepoint *
340 set_raw_tracepoint (struct symtab_and_line sal)
341 {
342 register struct tracepoint *t, *tc;
343 struct cleanup *old_chain;
344
345 t = (struct tracepoint *) xmalloc (sizeof (struct tracepoint));
346 old_chain = make_cleanup (xfree, t);
347 memset (t, 0, sizeof (*t));
348 t->address = sal.pc;
349 if (sal.symtab == NULL)
350 t->source_file = NULL;
351 else
352 t->source_file = savestring (sal.symtab->filename,
353 strlen (sal.symtab->filename));
354
355 t->section = sal.section;
356 t->language = current_language->la_language;
357 t->input_radix = input_radix;
358 t->line_number = sal.line;
359 t->enabled = enabled;
360 t->next = 0;
361 t->step_count = 0;
362 t->pass_count = 0;
363 t->addr_string = NULL;
364
365 /* Add this tracepoint to the end of the chain
366 so that a list of tracepoints will come out in order
367 of increasing numbers. */
368
369 tc = tracepoint_chain;
370 if (tc == 0)
371 tracepoint_chain = t;
372 else
373 {
374 while (tc->next)
375 tc = tc->next;
376 tc->next = t;
377 }
378 discard_cleanups (old_chain);
379 return t;
380 }
381
382 /* Set a tracepoint according to ARG (function, linenum or *address) */
383 static void
384 trace_command (char *arg, int from_tty)
385 {
386 char **canonical = (char **) NULL;
387 struct symtabs_and_lines sals;
388 struct symtab_and_line sal;
389 struct tracepoint *t;
390 char *addr_start = 0, *addr_end = 0;
391 int i;
392
393 if (!arg || !*arg)
394 error ("trace command requires an argument");
395
396 if (from_tty && info_verbose)
397 printf_filtered ("TRACE %s\n", arg);
398
399 addr_start = arg;
400 sals = decode_line_1 (&arg, 1, (struct symtab *) NULL, 0, &canonical);
401 addr_end = arg;
402 if (!sals.nelts)
403 return; /* ??? Presumably decode_line_1 has already warned? */
404
405 /* Resolve all line numbers to PC's */
406 for (i = 0; i < sals.nelts; i++)
407 resolve_sal_pc (&sals.sals[i]);
408
409 /* Now set all the tracepoints. */
410 for (i = 0; i < sals.nelts; i++)
411 {
412 sal = sals.sals[i];
413
414 t = set_raw_tracepoint (sal);
415 set_tracepoint_count (tracepoint_count + 1);
416 t->number = tracepoint_count;
417
418 /* If a canonical line spec is needed use that instead of the
419 command string. */
420 if (canonical != (char **) NULL && canonical[i] != NULL)
421 t->addr_string = canonical[i];
422 else if (addr_start)
423 t->addr_string = savestring (addr_start, addr_end - addr_start);
424
425 trace_mention (t);
426
427 /* Let the UI know of any additions */
428 if (create_tracepoint_hook)
429 create_tracepoint_hook (t);
430 }
431
432 if (sals.nelts > 1)
433 {
434 printf_filtered ("Multiple tracepoints were set.\n");
435 printf_filtered ("Use 'delete trace' to delete unwanted tracepoints.\n");
436 }
437 }
438
439 /* Tell the user we have just set a tracepoint TP. */
440
441 static void
442 trace_mention (struct tracepoint *tp)
443 {
444 printf_filtered ("Tracepoint %d", tp->number);
445
446 if (addressprint || (tp->source_file == NULL))
447 {
448 printf_filtered (" at ");
449 print_address_numeric (tp->address, 1, gdb_stdout);
450 }
451 if (tp->source_file)
452 printf_filtered (": file %s, line %d.",
453 tp->source_file, tp->line_number);
454
455 printf_filtered ("\n");
456 }
457
458 /* Print information on tracepoint number TPNUM_EXP, or all if omitted. */
459
460 static void
461 tracepoints_info (char *tpnum_exp, int from_tty)
462 {
463 struct tracepoint *t;
464 struct action_line *action;
465 int found_a_tracepoint = 0;
466 char wrap_indent[80];
467 struct symbol *sym;
468 int tpnum = -1;
469
470 if (tpnum_exp)
471 tpnum = parse_and_eval_long (tpnum_exp);
472
473 ALL_TRACEPOINTS (t)
474 if (tpnum == -1 || tpnum == t->number)
475 {
476 extern int addressprint; /* print machine addresses? */
477
478 if (!found_a_tracepoint++)
479 {
480 printf_filtered ("Num Enb ");
481 if (addressprint)
482 printf_filtered ("Address ");
483 printf_filtered ("PassC StepC What\n");
484 }
485 strcpy (wrap_indent, " ");
486 if (addressprint)
487 strcat (wrap_indent, " ");
488
489 printf_filtered ("%-3d %-3s ", t->number,
490 t->enabled == enabled ? "y" : "n");
491 if (addressprint)
492 printf_filtered ("%s ",
493 local_hex_string_custom ((unsigned long) t->address,
494 "08l"));
495 printf_filtered ("%-5d %-5ld ", t->pass_count, t->step_count);
496
497 if (t->source_file)
498 {
499 sym = find_pc_sect_function (t->address, t->section);
500 if (sym)
501 {
502 fputs_filtered ("in ", gdb_stdout);
503 fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
504 wrap_here (wrap_indent);
505 fputs_filtered (" at ", gdb_stdout);
506 }
507 fputs_filtered (t->source_file, gdb_stdout);
508 printf_filtered (":%d", t->line_number);
509 }
510 else
511 print_address_symbolic (t->address, gdb_stdout, demangle, " ");
512
513 printf_filtered ("\n");
514 if (t->actions)
515 {
516 printf_filtered (" Actions for tracepoint %d: \n", t->number);
517 for (action = t->actions; action; action = action->next)
518 {
519 printf_filtered ("\t%s\n", action->action);
520 }
521 }
522 }
523 if (!found_a_tracepoint)
524 {
525 if (tpnum == -1)
526 printf_filtered ("No tracepoints.\n");
527 else
528 printf_filtered ("No tracepoint number %d.\n", tpnum);
529 }
530 }
531
532 /* Optimization: the code to parse an enable, disable, or delete TP command
533 is virtually identical except for whether it performs an enable, disable,
534 or delete. Therefore I've combined them into one function with an opcode.
535 */
536 enum tracepoint_opcode
537 {
538 enable_op,
539 disable_op,
540 delete_op
541 };
542
543 /* This function implements enable, disable and delete commands. */
544 static void
545 tracepoint_operation (struct tracepoint *t, int from_tty,
546 enum tracepoint_opcode opcode)
547 {
548 struct tracepoint *t2;
549
550 if (t == NULL) /* no tracepoint operand */
551 return;
552
553 switch (opcode)
554 {
555 case enable_op:
556 t->enabled = enabled;
557 if (modify_tracepoint_hook)
558 modify_tracepoint_hook (t);
559 break;
560 case disable_op:
561 t->enabled = disabled;
562 if (modify_tracepoint_hook)
563 modify_tracepoint_hook (t);
564 break;
565 case delete_op:
566 if (tracepoint_chain == t)
567 tracepoint_chain = t->next;
568
569 ALL_TRACEPOINTS (t2)
570 if (t2->next == t)
571 {
572 t2->next = t->next;
573 break;
574 }
575
576 /* Let the UI know of any deletions */
577 if (delete_tracepoint_hook)
578 delete_tracepoint_hook (t);
579
580 if (t->addr_string)
581 xfree (t->addr_string);
582 if (t->source_file)
583 xfree (t->source_file);
584 if (t->actions)
585 free_actions (t);
586
587 xfree (t);
588 break;
589 }
590 }
591
592 /* Utility: parse a tracepoint number and look it up in the list.
593 If MULTI_P is true, there might be a range of tracepoints in ARG.
594 if OPTIONAL_P is true, then if the argument is missing, the most
595 recent tracepoint (tracepoint_count) is returned. */
596 struct tracepoint *
597 get_tracepoint_by_number (char **arg, int multi_p, int optional_p)
598 {
599 struct tracepoint *t;
600 int tpnum;
601 char *instring = arg == NULL ? NULL : *arg;
602
603 if (arg == NULL || *arg == NULL || ! **arg)
604 {
605 if (optional_p)
606 tpnum = tracepoint_count;
607 else
608 error_no_arg ("tracepoint number");
609 }
610 else
611 tpnum = multi_p ? get_number_or_range (arg) : get_number (arg);
612
613 if (tpnum <= 0)
614 {
615 if (instring && *instring)
616 printf_filtered ("bad tracepoint number at or near '%s'\n", instring);
617 else
618 printf_filtered ("Tracepoint argument missing and no previous tracepoint\n");
619 return NULL;
620 }
621
622 ALL_TRACEPOINTS (t)
623 if (t->number == tpnum)
624 {
625 return t;
626 }
627
628 /* FIXME: if we are in the middle of a range we don't want to give
629 a message. The current interface to get_number_or_range doesn't
630 allow us to discover this. */
631 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
632 return NULL;
633 }
634
635 /* Utility: parse a list of tracepoint numbers, and call a func for each. */
636 static void
637 map_args_over_tracepoints (char *args, int from_tty,
638 enum tracepoint_opcode opcode)
639 {
640 struct tracepoint *t, *tmp;
641
642 if (args == 0 || *args == 0) /* do them all */
643 ALL_TRACEPOINTS_SAFE (t, tmp)
644 tracepoint_operation (t, from_tty, opcode);
645 else
646 while (*args)
647 {
648 QUIT; /* give user option to bail out with ^C */
649 t = get_tracepoint_by_number (&args, 1, 0);
650 tracepoint_operation (t, from_tty, opcode);
651 while (*args == ' ' || *args == '\t')
652 args++;
653 }
654 }
655
656 /* The 'enable trace' command enables tracepoints. Not supported by all targets. */
657 static void
658 enable_trace_command (char *args, int from_tty)
659 {
660 dont_repeat ();
661 map_args_over_tracepoints (args, from_tty, enable_op);
662 }
663
664 /* The 'disable trace' command enables tracepoints. Not supported by all targets. */
665 static void
666 disable_trace_command (char *args, int from_tty)
667 {
668 dont_repeat ();
669 map_args_over_tracepoints (args, from_tty, disable_op);
670 }
671
672 /* Remove a tracepoint (or all if no argument) */
673 static void
674 delete_trace_command (char *args, int from_tty)
675 {
676 dont_repeat ();
677 if (!args || !*args) /* No args implies all tracepoints; */
678 if (from_tty) /* confirm only if from_tty... */
679 if (tracepoint_chain) /* and if there are tracepoints to delete! */
680 if (!query ("Delete all tracepoints? "))
681 return;
682
683 map_args_over_tracepoints (args, from_tty, delete_op);
684 }
685
686 /* Set passcount for tracepoint.
687
688 First command argument is passcount, second is tracepoint number.
689 If tracepoint number omitted, apply to most recently defined.
690 Also accepts special argument "all". */
691
692 static void
693 trace_pass_command (char *args, int from_tty)
694 {
695 struct tracepoint *t1 = (struct tracepoint *) -1, *t2;
696 unsigned int count;
697 int all = 0;
698
699 if (args == 0 || *args == 0)
700 error ("passcount command requires an argument (count + optional TP num)");
701
702 count = strtoul (args, &args, 10); /* count comes first, then TP num */
703
704 while (*args && isspace ((int) *args))
705 args++;
706
707 if (*args && strncasecmp (args, "all", 3) == 0)
708 {
709 args += 3; /* skip special argument "all" */
710 all = 1;
711 if (*args)
712 error ("Junk at end of arguments.");
713 }
714 else
715 t1 = get_tracepoint_by_number (&args, 1, 1);
716
717 do
718 {
719 if (t1)
720 {
721 ALL_TRACEPOINTS (t2)
722 if (t1 == (struct tracepoint *) -1 || t1 == t2)
723 {
724 t2->pass_count = count;
725 if (modify_tracepoint_hook)
726 modify_tracepoint_hook (t2);
727 if (from_tty)
728 printf_filtered ("Setting tracepoint %d's passcount to %d\n",
729 t2->number, count);
730 }
731 if (! all && *args)
732 t1 = get_tracepoint_by_number (&args, 1, 0);
733 }
734 }
735 while (*args);
736 }
737
738 /* ACTIONS functions: */
739
740 /* Prototypes for action-parsing utility commands */
741 static void read_actions (struct tracepoint *);
742
743 /* The three functions:
744 collect_pseudocommand,
745 while_stepping_pseudocommand, and
746 end_actions_pseudocommand
747 are placeholders for "commands" that are actually ONLY to be used
748 within a tracepoint action list. If the actual function is ever called,
749 it means that somebody issued the "command" at the top level,
750 which is always an error. */
751
752 static void
753 end_actions_pseudocommand (char *args, int from_tty)
754 {
755 error ("This command cannot be used at the top level.");
756 }
757
758 static void
759 while_stepping_pseudocommand (char *args, int from_tty)
760 {
761 error ("This command can only be used in a tracepoint actions list.");
762 }
763
764 static void
765 collect_pseudocommand (char *args, int from_tty)
766 {
767 error ("This command can only be used in a tracepoint actions list.");
768 }
769
770 /* Enter a list of actions for a tracepoint. */
771 static void
772 trace_actions_command (char *args, int from_tty)
773 {
774 struct tracepoint *t;
775 char tmpbuf[128];
776 char *end_msg = "End with a line saying just \"end\".";
777
778 t = get_tracepoint_by_number (&args, 0, 1);
779 if (t)
780 {
781 sprintf (tmpbuf, "Enter actions for tracepoint %d, one per line.",
782 t->number);
783
784 if (from_tty)
785 {
786 if (readline_begin_hook)
787 (*readline_begin_hook) ("%s %s\n", tmpbuf, end_msg);
788 else if (input_from_terminal_p ())
789 printf_filtered ("%s\n%s\n", tmpbuf, end_msg);
790 }
791
792 free_actions (t);
793 t->step_count = 0; /* read_actions may set this */
794 read_actions (t);
795
796 if (readline_end_hook)
797 (*readline_end_hook) ();
798 /* tracepoints_changed () */
799 }
800 /* else just return */
801 }
802
803 /* worker function */
804 static void
805 read_actions (struct tracepoint *t)
806 {
807 char *line;
808 char *prompt1 = "> ", *prompt2 = " > ";
809 char *prompt = prompt1;
810 enum actionline_type linetype;
811 extern FILE *instream;
812 struct action_line *next = NULL, *temp;
813 struct cleanup *old_chain;
814
815 /* Control-C quits instantly if typed while in this loop
816 since it should not wait until the user types a newline. */
817 immediate_quit++;
818 #ifdef STOP_SIGNAL
819 if (job_control)
820 {
821 if (event_loop_p)
822 signal (STOP_SIGNAL, handle_stop_sig);
823 else
824 signal (STOP_SIGNAL, stop_sig);
825 }
826 #endif
827 old_chain = make_cleanup_free_actions (t);
828 while (1)
829 {
830 /* Make sure that all output has been output. Some machines may let
831 you get away with leaving out some of the gdb_flush, but not all. */
832 wrap_here ("");
833 gdb_flush (gdb_stdout);
834 gdb_flush (gdb_stderr);
835
836 if (readline_hook && instream == NULL)
837 line = (*readline_hook) (prompt);
838 else if (instream == stdin && ISATTY (instream))
839 {
840 line = readline (prompt);
841 if (line && *line) /* add it to command history */
842 add_history (line);
843 }
844 else
845 line = gdb_readline (0);
846
847 linetype = validate_actionline (&line, t);
848 if (linetype == BADLINE)
849 continue; /* already warned -- collect another line */
850
851 temp = xmalloc (sizeof (struct action_line));
852 temp->next = NULL;
853 temp->action = line;
854
855 if (next == NULL) /* first action for this tracepoint? */
856 t->actions = next = temp;
857 else
858 {
859 next->next = temp;
860 next = temp;
861 }
862
863 if (linetype == STEPPING) /* begin "while-stepping" */
864 {
865 if (prompt == prompt2)
866 {
867 warning ("Already processing 'while-stepping'");
868 continue;
869 }
870 else
871 prompt = prompt2; /* change prompt for stepping actions */
872 }
873 else if (linetype == END)
874 {
875 if (prompt == prompt2)
876 {
877 prompt = prompt1; /* end of single-stepping actions */
878 }
879 else
880 { /* end of actions */
881 if (t->actions->next == NULL)
882 {
883 /* an "end" all by itself with no other actions means
884 this tracepoint has no actions. Discard empty list. */
885 free_actions (t);
886 }
887 break;
888 }
889 }
890 }
891 #ifdef STOP_SIGNAL
892 if (job_control)
893 signal (STOP_SIGNAL, SIG_DFL);
894 #endif
895 immediate_quit--;
896 discard_cleanups (old_chain);
897 }
898
899 /* worker function */
900 enum actionline_type
901 validate_actionline (char **line, struct tracepoint *t)
902 {
903 struct cmd_list_element *c;
904 struct expression *exp = NULL;
905 struct cleanup *old_chain = NULL;
906 char *p;
907
908 for (p = *line; isspace ((int) *p);)
909 p++;
910
911 /* symbol lookup etc. */
912 if (*p == '\0') /* empty line: just prompt for another line. */
913 return BADLINE;
914
915 if (*p == '#') /* comment line */
916 return GENERIC;
917
918 c = lookup_cmd (&p, cmdlist, "", -1, 1);
919 if (c == 0)
920 {
921 warning ("'%s' is not an action that I know, or is ambiguous.", p);
922 return BADLINE;
923 }
924
925 if (c->function.cfunc == collect_pseudocommand)
926 {
927 struct agent_expr *aexpr;
928 struct agent_reqs areqs;
929
930 do
931 { /* repeat over a comma-separated list */
932 QUIT; /* allow user to bail out with ^C */
933 while (isspace ((int) *p))
934 p++;
935
936 if (*p == '$') /* look for special pseudo-symbols */
937 {
938 if ((0 == strncasecmp ("reg", p + 1, 3)) ||
939 (0 == strncasecmp ("arg", p + 1, 3)) ||
940 (0 == strncasecmp ("loc", p + 1, 3)))
941 {
942 p = strchr (p, ',');
943 continue;
944 }
945 /* else fall thru, treat p as an expression and parse it! */
946 }
947 exp = parse_exp_1 (&p, block_for_pc (t->address), 1);
948 old_chain = make_cleanup (free_current_contents, &exp);
949
950 if (exp->elts[0].opcode == OP_VAR_VALUE)
951 {
952 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
953 {
954 warning ("constant %s (value %ld) will not be collected.",
955 SYMBOL_NAME (exp->elts[2].symbol),
956 SYMBOL_VALUE (exp->elts[2].symbol));
957 return BADLINE;
958 }
959 else if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_OPTIMIZED_OUT)
960 {
961 warning ("%s is optimized away and cannot be collected.",
962 SYMBOL_NAME (exp->elts[2].symbol));
963 return BADLINE;
964 }
965 }
966
967 /* we have something to collect, make sure that the expr to
968 bytecode translator can handle it and that it's not too long */
969 aexpr = gen_trace_for_expr (t->address, exp);
970 make_cleanup_free_agent_expr (aexpr);
971
972 if (aexpr->len > MAX_AGENT_EXPR_LEN)
973 error ("expression too complicated, try simplifying");
974
975 ax_reqs (aexpr, &areqs);
976 (void) make_cleanup (xfree, areqs.reg_mask);
977
978 if (areqs.flaw != agent_flaw_none)
979 error ("malformed expression");
980
981 if (areqs.min_height < 0)
982 error ("gdb: Internal error: expression has min height < 0");
983
984 if (areqs.max_height > 20)
985 error ("expression too complicated, try simplifying");
986
987 do_cleanups (old_chain);
988 }
989 while (p && *p++ == ',');
990 return GENERIC;
991 }
992 else if (c->function.cfunc == while_stepping_pseudocommand)
993 {
994 char *steparg; /* in case warning is necessary */
995
996 while (isspace ((int) *p))
997 p++;
998 steparg = p;
999
1000 if (*p == '\0' ||
1001 (t->step_count = strtol (p, &p, 0)) == 0)
1002 {
1003 warning ("'%s': bad step-count; command ignored.", *line);
1004 return BADLINE;
1005 }
1006 return STEPPING;
1007 }
1008 else if (c->function.cfunc == end_actions_pseudocommand)
1009 return END;
1010 else
1011 {
1012 warning ("'%s' is not a supported tracepoint action.", *line);
1013 return BADLINE;
1014 }
1015 }
1016
1017 /* worker function */
1018 void
1019 free_actions (struct tracepoint *t)
1020 {
1021 struct action_line *line, *next;
1022
1023 for (line = t->actions; line; line = next)
1024 {
1025 next = line->next;
1026 if (line->action)
1027 xfree (line->action);
1028 xfree (line);
1029 }
1030 t->actions = NULL;
1031 }
1032
1033 static void
1034 do_free_actions_cleanup (void *t)
1035 {
1036 free_actions (t);
1037 }
1038
1039 static struct cleanup *
1040 make_cleanup_free_actions (struct tracepoint *t)
1041 {
1042 return make_cleanup (do_free_actions_cleanup, t);
1043 }
1044
1045 struct memrange
1046 {
1047 int type; /* 0 for absolute memory range, else basereg number */
1048 bfd_signed_vma start;
1049 bfd_signed_vma end;
1050 };
1051
1052 struct collection_list
1053 {
1054 unsigned char regs_mask[8]; /* room for up to 256 regs */
1055 long listsize;
1056 long next_memrange;
1057 struct memrange *list;
1058 long aexpr_listsize; /* size of array pointed to by expr_list elt */
1059 long next_aexpr_elt;
1060 struct agent_expr **aexpr_list;
1061
1062 }
1063 tracepoint_list, stepping_list;
1064
1065 /* MEMRANGE functions: */
1066
1067 static int memrange_cmp (const void *, const void *);
1068
1069 /* compare memranges for qsort */
1070 static int
1071 memrange_cmp (const void *va, const void *vb)
1072 {
1073 const struct memrange *a = va, *b = vb;
1074
1075 if (a->type < b->type)
1076 return -1;
1077 if (a->type > b->type)
1078 return 1;
1079 if (a->type == 0)
1080 {
1081 if ((bfd_vma) a->start < (bfd_vma) b->start)
1082 return -1;
1083 if ((bfd_vma) a->start > (bfd_vma) b->start)
1084 return 1;
1085 }
1086 else
1087 {
1088 if (a->start < b->start)
1089 return -1;
1090 if (a->start > b->start)
1091 return 1;
1092 }
1093 return 0;
1094 }
1095
1096 /* Sort the memrange list using qsort, and merge adjacent memranges */
1097 static void
1098 memrange_sortmerge (struct collection_list *memranges)
1099 {
1100 int a, b;
1101
1102 qsort (memranges->list, memranges->next_memrange,
1103 sizeof (struct memrange), memrange_cmp);
1104 if (memranges->next_memrange > 0)
1105 {
1106 for (a = 0, b = 1; b < memranges->next_memrange; b++)
1107 {
1108 if (memranges->list[a].type == memranges->list[b].type &&
1109 memranges->list[b].start - memranges->list[a].end <=
1110 MAX_REGISTER_VIRTUAL_SIZE)
1111 {
1112 /* memrange b starts before memrange a ends; merge them. */
1113 if (memranges->list[b].end > memranges->list[a].end)
1114 memranges->list[a].end = memranges->list[b].end;
1115 continue; /* next b, same a */
1116 }
1117 a++; /* next a */
1118 if (a != b)
1119 memcpy (&memranges->list[a], &memranges->list[b],
1120 sizeof (struct memrange));
1121 }
1122 memranges->next_memrange = a + 1;
1123 }
1124 }
1125
1126 /* Add a register to a collection list */
1127 static void
1128 add_register (struct collection_list *collection, unsigned int regno)
1129 {
1130 if (info_verbose)
1131 printf_filtered ("collect register %d\n", regno);
1132 if (regno > (8 * sizeof (collection->regs_mask)))
1133 error ("Internal: register number %d too large for tracepoint",
1134 regno);
1135 collection->regs_mask[regno / 8] |= 1 << (regno % 8);
1136 }
1137
1138 /* Add a memrange to a collection list */
1139 static void
1140 add_memrange (struct collection_list *memranges, int type, bfd_signed_vma base,
1141 unsigned long len)
1142 {
1143 if (info_verbose)
1144 {
1145 printf_filtered ("(%d,", type);
1146 printf_vma (base);
1147 printf_filtered (",%ld)\n", len);
1148 }
1149
1150 /* type: 0 == memory, n == basereg */
1151 memranges->list[memranges->next_memrange].type = type;
1152 /* base: addr if memory, offset if reg relative. */
1153 memranges->list[memranges->next_memrange].start = base;
1154 /* len: we actually save end (base + len) for convenience */
1155 memranges->list[memranges->next_memrange].end = base + len;
1156 memranges->next_memrange++;
1157 if (memranges->next_memrange >= memranges->listsize)
1158 {
1159 memranges->listsize *= 2;
1160 memranges->list = xrealloc (memranges->list,
1161 memranges->listsize);
1162 }
1163
1164 if (type != -1) /* better collect the base register! */
1165 add_register (memranges, type);
1166 }
1167
1168 /* Add a symbol to a collection list */
1169 static void
1170 collect_symbol (struct collection_list *collect, struct symbol *sym,
1171 long frame_regno, long frame_offset)
1172 {
1173 unsigned long len;
1174 unsigned int reg;
1175 bfd_signed_vma offset;
1176
1177 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
1178 switch (SYMBOL_CLASS (sym))
1179 {
1180 default:
1181 printf_filtered ("%s: don't know symbol class %d\n",
1182 SYMBOL_NAME (sym), SYMBOL_CLASS (sym));
1183 break;
1184 case LOC_CONST:
1185 printf_filtered ("constant %s (value %ld) will not be collected.\n",
1186 SYMBOL_NAME (sym), SYMBOL_VALUE (sym));
1187 break;
1188 case LOC_STATIC:
1189 offset = SYMBOL_VALUE_ADDRESS (sym);
1190 if (info_verbose)
1191 {
1192 char tmp[40];
1193
1194 sprintf_vma (tmp, offset);
1195 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
1196 SYMBOL_NAME (sym), len, tmp /* address */);
1197 }
1198 add_memrange (collect, -1, offset, len); /* 0 == memory */
1199 break;
1200 case LOC_REGISTER:
1201 case LOC_REGPARM:
1202 reg = SYMBOL_VALUE (sym);
1203 if (info_verbose)
1204 printf_filtered ("LOC_REG[parm] %s: ", SYMBOL_NAME (sym));
1205 add_register (collect, reg);
1206 /* check for doubles stored in two registers */
1207 /* FIXME: how about larger types stored in 3 or more regs? */
1208 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
1209 len > REGISTER_RAW_SIZE (reg))
1210 add_register (collect, reg + 1);
1211 break;
1212 case LOC_REF_ARG:
1213 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
1214 printf_filtered (" (will not collect %s)\n",
1215 SYMBOL_NAME (sym));
1216 break;
1217 case LOC_ARG:
1218 reg = frame_regno;
1219 offset = frame_offset + SYMBOL_VALUE (sym);
1220 if (info_verbose)
1221 {
1222 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
1223 SYMBOL_NAME (sym), len);
1224 printf_vma (offset);
1225 printf_filtered (" from frame ptr reg %d\n", reg);
1226 }
1227 add_memrange (collect, reg, offset, len);
1228 break;
1229 case LOC_REGPARM_ADDR:
1230 reg = SYMBOL_VALUE (sym);
1231 offset = 0;
1232 if (info_verbose)
1233 {
1234 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset ",
1235 SYMBOL_NAME (sym), len);
1236 printf_vma (offset);
1237 printf_filtered (" from reg %d\n", reg);
1238 }
1239 add_memrange (collect, reg, offset, len);
1240 break;
1241 case LOC_LOCAL:
1242 case LOC_LOCAL_ARG:
1243 reg = frame_regno;
1244 offset = frame_offset + SYMBOL_VALUE (sym);
1245 if (info_verbose)
1246 {
1247 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
1248 SYMBOL_NAME (sym), len);
1249 printf_vma (offset);
1250 printf_filtered (" from frame ptr reg %d\n", reg);
1251 }
1252 add_memrange (collect, reg, offset, len);
1253 break;
1254 case LOC_BASEREG:
1255 case LOC_BASEREG_ARG:
1256 reg = SYMBOL_BASEREG (sym);
1257 offset = SYMBOL_VALUE (sym);
1258 if (info_verbose)
1259 {
1260 printf_filtered ("LOC_BASEREG %s: collect %ld bytes at offset ",
1261 SYMBOL_NAME (sym), len);
1262 printf_vma (offset);
1263 printf_filtered (" from basereg %d\n", reg);
1264 }
1265 add_memrange (collect, reg, offset, len);
1266 break;
1267 case LOC_UNRESOLVED:
1268 printf_filtered ("Don't know LOC_UNRESOLVED %s\n", SYMBOL_NAME (sym));
1269 break;
1270 case LOC_OPTIMIZED_OUT:
1271 printf_filtered ("%s has been optimized out of existence.\n",
1272 SYMBOL_NAME (sym));
1273 break;
1274 }
1275 }
1276
1277 /* Add all locals (or args) symbols to collection list */
1278 static void
1279 add_local_symbols (struct collection_list *collect, CORE_ADDR pc,
1280 long frame_regno, long frame_offset, int type)
1281 {
1282 struct symbol *sym;
1283 struct block *block;
1284 int i, nsyms, count = 0;
1285
1286 block = block_for_pc (pc);
1287 while (block != 0)
1288 {
1289 QUIT; /* allow user to bail out with ^C */
1290 nsyms = BLOCK_NSYMS (block);
1291 for (i = 0; i < nsyms; i++)
1292 {
1293 sym = BLOCK_SYM (block, i);
1294 switch (SYMBOL_CLASS (sym))
1295 {
1296 default:
1297 warning ("don't know how to trace local symbol %s",
1298 SYMBOL_NAME (sym));
1299 case LOC_LOCAL:
1300 case LOC_STATIC:
1301 case LOC_REGISTER:
1302 case LOC_BASEREG:
1303 if (type == 'L') /* collecting Locals */
1304 {
1305 count++;
1306 collect_symbol (collect, sym, frame_regno, frame_offset);
1307 }
1308 break;
1309 case LOC_ARG:
1310 case LOC_LOCAL_ARG:
1311 case LOC_REF_ARG:
1312 case LOC_REGPARM:
1313 case LOC_REGPARM_ADDR:
1314 case LOC_BASEREG_ARG:
1315 if (type == 'A') /* collecting Arguments */
1316 {
1317 count++;
1318 collect_symbol (collect, sym, frame_regno, frame_offset);
1319 }
1320 }
1321 }
1322 if (BLOCK_FUNCTION (block))
1323 break;
1324 else
1325 block = BLOCK_SUPERBLOCK (block);
1326 }
1327 if (count == 0)
1328 warning ("No %s found in scope.", type == 'L' ? "locals" : "args");
1329 }
1330
1331 /* worker function */
1332 static void
1333 clear_collection_list (struct collection_list *list)
1334 {
1335 int ndx;
1336
1337 list->next_memrange = 0;
1338 for (ndx = 0; ndx < list->next_aexpr_elt; ndx++)
1339 {
1340 free_agent_expr (list->aexpr_list[ndx]);
1341 list->aexpr_list[ndx] = NULL;
1342 }
1343 list->next_aexpr_elt = 0;
1344 memset (list->regs_mask, 0, sizeof (list->regs_mask));
1345 }
1346
1347 /* reduce a collection list to string form (for gdb protocol) */
1348 static char **
1349 stringify_collection_list (struct collection_list *list, char *string)
1350 {
1351 char temp_buf[2048];
1352 char tmp2[40];
1353 int count;
1354 int ndx = 0;
1355 char *(*str_list)[];
1356 char *end;
1357 long i;
1358
1359 count = 1 + list->next_memrange + list->next_aexpr_elt + 1;
1360 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
1361
1362 for (i = sizeof (list->regs_mask) - 1; i > 0; i--)
1363 if (list->regs_mask[i] != 0) /* skip leading zeroes in regs_mask */
1364 break;
1365 if (list->regs_mask[i] != 0) /* prepare to send regs_mask to the stub */
1366 {
1367 if (info_verbose)
1368 printf_filtered ("\nCollecting registers (mask): 0x");
1369 end = temp_buf;
1370 *end++ = 'R';
1371 for (; i >= 0; i--)
1372 {
1373 QUIT; /* allow user to bail out with ^C */
1374 if (info_verbose)
1375 printf_filtered ("%02X", list->regs_mask[i]);
1376 sprintf (end, "%02X", list->regs_mask[i]);
1377 end += 2;
1378 }
1379 (*str_list)[ndx] = savestring (temp_buf, end - temp_buf);
1380 ndx++;
1381 }
1382 if (info_verbose)
1383 printf_filtered ("\n");
1384 if (list->next_memrange > 0 && info_verbose)
1385 printf_filtered ("Collecting memranges: \n");
1386 for (i = 0, count = 0, end = temp_buf; i < list->next_memrange; i++)
1387 {
1388 QUIT; /* allow user to bail out with ^C */
1389 sprintf_vma (tmp2, list->list[i].start);
1390 if (info_verbose)
1391 {
1392 printf_filtered ("(%d, %s, %ld)\n",
1393 list->list[i].type,
1394 tmp2,
1395 (long) (list->list[i].end - list->list[i].start));
1396 }
1397 if (count + 27 > MAX_AGENT_EXPR_LEN)
1398 {
1399 (*str_list)[ndx] = savestring (temp_buf, count);
1400 ndx++;
1401 count = 0;
1402 end = temp_buf;
1403 }
1404
1405 sprintf (end, "M%X,%s,%lX",
1406 list->list[i].type,
1407 tmp2,
1408 (long) (list->list[i].end - list->list[i].start));
1409
1410 count += strlen (end);
1411 end += count;
1412 }
1413
1414 for (i = 0; i < list->next_aexpr_elt; i++)
1415 {
1416 QUIT; /* allow user to bail out with ^C */
1417 if ((count + 10 + 2 * list->aexpr_list[i]->len) > MAX_AGENT_EXPR_LEN)
1418 {
1419 (*str_list)[ndx] = savestring (temp_buf, count);
1420 ndx++;
1421 count = 0;
1422 end = temp_buf;
1423 }
1424 sprintf (end, "X%08X,", list->aexpr_list[i]->len);
1425 end += 10; /* 'X' + 8 hex digits + ',' */
1426 count += 10;
1427
1428 end = mem2hex (list->aexpr_list[i]->buf, end, list->aexpr_list[i]->len);
1429 count += 2 * list->aexpr_list[i]->len;
1430 }
1431
1432 if (count != 0)
1433 {
1434 (*str_list)[ndx] = savestring (temp_buf, count);
1435 ndx++;
1436 count = 0;
1437 end = temp_buf;
1438 }
1439 (*str_list)[ndx] = NULL;
1440
1441 if (ndx == 0)
1442 return NULL;
1443 else
1444 return *str_list;
1445 }
1446
1447 static void
1448 free_actions_list_cleanup_wrapper (void *al)
1449 {
1450 free_actions_list (al);
1451 }
1452
1453 static void
1454 free_actions_list (char **actions_list)
1455 {
1456 int ndx;
1457
1458 if (actions_list == 0)
1459 return;
1460
1461 for (ndx = 0; actions_list[ndx]; ndx++)
1462 xfree (actions_list[ndx]);
1463
1464 xfree (actions_list);
1465 }
1466
1467 /* render all actions into gdb protocol */
1468 static void
1469 encode_actions (struct tracepoint *t, char ***tdp_actions,
1470 char ***stepping_actions)
1471 {
1472 static char tdp_buff[2048], step_buff[2048];
1473 char *action_exp;
1474 struct expression *exp = NULL;
1475 struct action_line *action;
1476 int i;
1477 value_ptr tempval;
1478 struct collection_list *collect;
1479 struct cmd_list_element *cmd;
1480 struct agent_expr *aexpr;
1481 long frame_reg, frame_offset;
1482
1483
1484 clear_collection_list (&tracepoint_list);
1485 clear_collection_list (&stepping_list);
1486 collect = &tracepoint_list;
1487
1488 *tdp_actions = NULL;
1489 *stepping_actions = NULL;
1490
1491 TARGET_VIRTUAL_FRAME_POINTER (t->address, &frame_reg, &frame_offset);
1492
1493 for (action = t->actions; action; action = action->next)
1494 {
1495 QUIT; /* allow user to bail out with ^C */
1496 action_exp = action->action;
1497 while (isspace ((int) *action_exp))
1498 action_exp++;
1499
1500 if (*action_exp == '#') /* comment line */
1501 return;
1502
1503 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1504 if (cmd == 0)
1505 error ("Bad action list item: %s", action_exp);
1506
1507 if (cmd->function.cfunc == collect_pseudocommand)
1508 {
1509 do
1510 { /* repeat over a comma-separated list */
1511 QUIT; /* allow user to bail out with ^C */
1512 while (isspace ((int) *action_exp))
1513 action_exp++;
1514
1515 if (0 == strncasecmp ("$reg", action_exp, 4))
1516 {
1517 for (i = 0; i < NUM_REGS; i++)
1518 add_register (collect, i);
1519 action_exp = strchr (action_exp, ','); /* more? */
1520 }
1521 else if (0 == strncasecmp ("$arg", action_exp, 4))
1522 {
1523 add_local_symbols (collect,
1524 t->address,
1525 frame_reg,
1526 frame_offset,
1527 'A');
1528 action_exp = strchr (action_exp, ','); /* more? */
1529 }
1530 else if (0 == strncasecmp ("$loc", action_exp, 4))
1531 {
1532 add_local_symbols (collect,
1533 t->address,
1534 frame_reg,
1535 frame_offset,
1536 'L');
1537 action_exp = strchr (action_exp, ','); /* more? */
1538 }
1539 else
1540 {
1541 unsigned long addr, len;
1542 struct cleanup *old_chain = NULL;
1543 struct cleanup *old_chain1 = NULL;
1544 struct agent_reqs areqs;
1545
1546 exp = parse_exp_1 (&action_exp, block_for_pc (t->address), 1);
1547 old_chain = make_cleanup (free_current_contents, &exp);
1548
1549 switch (exp->elts[0].opcode)
1550 {
1551 case OP_REGISTER:
1552 i = exp->elts[1].longconst;
1553 if (info_verbose)
1554 printf_filtered ("OP_REGISTER: ");
1555 add_register (collect, i);
1556 break;
1557
1558 case UNOP_MEMVAL:
1559 /* safe because we know it's a simple expression */
1560 tempval = evaluate_expression (exp);
1561 addr = VALUE_ADDRESS (tempval) + VALUE_OFFSET (tempval);
1562 len = TYPE_LENGTH (check_typedef (exp->elts[1].type));
1563 add_memrange (collect, -1, addr, len);
1564 break;
1565
1566 case OP_VAR_VALUE:
1567 collect_symbol (collect,
1568 exp->elts[2].symbol,
1569 frame_reg,
1570 frame_offset);
1571 break;
1572
1573 default: /* full-fledged expression */
1574 aexpr = gen_trace_for_expr (t->address, exp);
1575
1576 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1577
1578 ax_reqs (aexpr, &areqs);
1579 if (areqs.flaw != agent_flaw_none)
1580 error ("malformed expression");
1581
1582 if (areqs.min_height < 0)
1583 error ("gdb: Internal error: expression has min height < 0");
1584 if (areqs.max_height > 20)
1585 error ("expression too complicated, try simplifying");
1586
1587 discard_cleanups (old_chain1);
1588 add_aexpr (collect, aexpr);
1589
1590 /* take care of the registers */
1591 if (areqs.reg_mask_len > 0)
1592 {
1593 int ndx1;
1594 int ndx2;
1595
1596 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
1597 {
1598 QUIT; /* allow user to bail out with ^C */
1599 if (areqs.reg_mask[ndx1] != 0)
1600 {
1601 /* assume chars have 8 bits */
1602 for (ndx2 = 0; ndx2 < 8; ndx2++)
1603 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1604 /* it's used -- record it */
1605 add_register (collect, ndx1 * 8 + ndx2);
1606 }
1607 }
1608 }
1609 break;
1610 } /* switch */
1611 do_cleanups (old_chain);
1612 } /* do */
1613 }
1614 while (action_exp && *action_exp++ == ',');
1615 } /* if */
1616 else if (cmd->function.cfunc == while_stepping_pseudocommand)
1617 {
1618 collect = &stepping_list;
1619 }
1620 else if (cmd->function.cfunc == end_actions_pseudocommand)
1621 {
1622 if (collect == &stepping_list) /* end stepping actions */
1623 collect = &tracepoint_list;
1624 else
1625 break; /* end tracepoint actions */
1626 }
1627 } /* for */
1628 memrange_sortmerge (&tracepoint_list);
1629 memrange_sortmerge (&stepping_list);
1630
1631 *tdp_actions = stringify_collection_list (&tracepoint_list, tdp_buff);
1632 *stepping_actions = stringify_collection_list (&stepping_list, step_buff);
1633 }
1634
1635 static void
1636 add_aexpr (struct collection_list *collect, struct agent_expr *aexpr)
1637 {
1638 if (collect->next_aexpr_elt >= collect->aexpr_listsize)
1639 {
1640 collect->aexpr_list =
1641 xrealloc (collect->aexpr_list,
1642 2 * collect->aexpr_listsize * sizeof (struct agent_expr *));
1643 collect->aexpr_listsize *= 2;
1644 }
1645 collect->aexpr_list[collect->next_aexpr_elt] = aexpr;
1646 collect->next_aexpr_elt++;
1647 }
1648
1649 static char target_buf[2048];
1650
1651 /* Set "transparent" memory ranges
1652
1653 Allow trace mechanism to treat text-like sections
1654 (and perhaps all read-only sections) transparently,
1655 i.e. don't reject memory requests from these address ranges
1656 just because they haven't been collected. */
1657
1658 static void
1659 remote_set_transparent_ranges (void)
1660 {
1661 extern bfd *exec_bfd;
1662 asection *s;
1663 bfd_size_type size;
1664 bfd_vma lma;
1665 int anysecs = 0;
1666
1667 if (!exec_bfd)
1668 return; /* no information to give. */
1669
1670 strcpy (target_buf, "QTro");
1671 for (s = exec_bfd->sections; s; s = s->next)
1672 {
1673 char tmp1[40], tmp2[40];
1674
1675 if ((s->flags & SEC_LOAD) == 0 ||
1676 /* (s->flags & SEC_CODE) == 0 || */
1677 (s->flags & SEC_READONLY) == 0)
1678 continue;
1679
1680 anysecs = 1;
1681 lma = s->lma;
1682 size = bfd_get_section_size_before_reloc (s);
1683 sprintf_vma (tmp1, lma);
1684 sprintf_vma (tmp2, lma + size);
1685 sprintf (target_buf + strlen (target_buf),
1686 ":%s,%s", tmp1, tmp2);
1687 }
1688 if (anysecs)
1689 {
1690 putpkt (target_buf);
1691 getpkt (target_buf, sizeof (target_buf), 0);
1692 }
1693 }
1694
1695 /* tstart command:
1696
1697 Tell target to clear any previous trace experiment.
1698 Walk the list of tracepoints, and send them (and their actions)
1699 to the target. If no errors,
1700 Tell target to start a new trace experiment. */
1701
1702 static void
1703 trace_start_command (char *args, int from_tty)
1704 { /* STUB_COMM MOSTLY_IMPLEMENTED */
1705 struct tracepoint *t;
1706 char buf[2048];
1707 char **tdp_actions;
1708 char **stepping_actions;
1709 int ndx;
1710 struct cleanup *old_chain = NULL;
1711
1712 dont_repeat (); /* like "run", dangerous to repeat accidentally */
1713
1714 if (target_is_remote ())
1715 {
1716 putpkt ("QTinit");
1717 remote_get_noisy_reply (target_buf, sizeof (target_buf));
1718 if (strcmp (target_buf, "OK"))
1719 error ("Target does not support this command.");
1720
1721 ALL_TRACEPOINTS (t)
1722 {
1723 char tmp[40];
1724
1725 sprintf_vma (tmp, t->address);
1726 sprintf (buf, "QTDP:%x:%s:%c:%lx:%x", t->number, tmp, /* address */
1727 t->enabled == enabled ? 'E' : 'D',
1728 t->step_count, t->pass_count);
1729
1730 if (t->actions)
1731 strcat (buf, "-");
1732 putpkt (buf);
1733 remote_get_noisy_reply (target_buf, sizeof (target_buf));
1734 if (strcmp (target_buf, "OK"))
1735 error ("Target does not support tracepoints.");
1736
1737 if (t->actions)
1738 {
1739 encode_actions (t, &tdp_actions, &stepping_actions);
1740 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
1741 tdp_actions);
1742 (void) make_cleanup (free_actions_list_cleanup_wrapper,
1743 stepping_actions);
1744
1745 /* do_single_steps (t); */
1746 if (tdp_actions)
1747 {
1748 for (ndx = 0; tdp_actions[ndx]; ndx++)
1749 {
1750 QUIT; /* allow user to bail out with ^C */
1751 sprintf (buf, "QTDP:-%x:%s:%s%c",
1752 t->number, tmp, /* address */
1753 tdp_actions[ndx],
1754 ((tdp_actions[ndx + 1] || stepping_actions)
1755 ? '-' : 0));
1756 putpkt (buf);
1757 remote_get_noisy_reply (target_buf, sizeof (target_buf));
1758 if (strcmp (target_buf, "OK"))
1759 error ("Error on target while setting tracepoints.");
1760 }
1761 }
1762 if (stepping_actions)
1763 {
1764 for (ndx = 0; stepping_actions[ndx]; ndx++)
1765 {
1766 QUIT; /* allow user to bail out with ^C */
1767 sprintf (buf, "QTDP:-%x:%s:%s%s%s",
1768 t->number, tmp, /* address */
1769 ((ndx == 0) ? "S" : ""),
1770 stepping_actions[ndx],
1771 (stepping_actions[ndx + 1] ? "-" : ""));
1772 putpkt (buf);
1773 remote_get_noisy_reply (target_buf, sizeof (target_buf));
1774 if (strcmp (target_buf, "OK"))
1775 error ("Error on target while setting tracepoints.");
1776 }
1777 }
1778
1779 do_cleanups (old_chain);
1780 }
1781 }
1782 /* Tell target to treat text-like sections as transparent */
1783 remote_set_transparent_ranges ();
1784 /* Now insert traps and begin collecting data */
1785 putpkt ("QTStart");
1786 remote_get_noisy_reply (target_buf, sizeof (target_buf));
1787 if (strcmp (target_buf, "OK"))
1788 error ("Bogus reply from target: %s", target_buf);
1789 set_traceframe_num (-1); /* all old traceframes invalidated */
1790 set_tracepoint_num (-1);
1791 set_traceframe_context (-1);
1792 trace_running_p = 1;
1793 if (trace_start_stop_hook)
1794 trace_start_stop_hook (1, from_tty);
1795
1796 }
1797 else
1798 error ("Trace can only be run on remote targets.");
1799 }
1800
1801 /* tstop command */
1802 static void
1803 trace_stop_command (char *args, int from_tty)
1804 { /* STUB_COMM IS_IMPLEMENTED */
1805 if (target_is_remote ())
1806 {
1807 putpkt ("QTStop");
1808 remote_get_noisy_reply (target_buf, sizeof (target_buf));
1809 if (strcmp (target_buf, "OK"))
1810 error ("Bogus reply from target: %s", target_buf);
1811 trace_running_p = 0;
1812 if (trace_start_stop_hook)
1813 trace_start_stop_hook (0, from_tty);
1814 }
1815 else
1816 error ("Trace can only be run on remote targets.");
1817 }
1818
1819 unsigned long trace_running_p;
1820
1821 /* tstatus command */
1822 static void
1823 trace_status_command (char *args, int from_tty)
1824 { /* STUB_COMM IS_IMPLEMENTED */
1825 if (target_is_remote ())
1826 {
1827 putpkt ("qTStatus");
1828 remote_get_noisy_reply (target_buf, sizeof (target_buf));
1829
1830 if (target_buf[0] != 'T' ||
1831 (target_buf[1] != '0' && target_buf[1] != '1'))
1832 error ("Bogus reply from target: %s", target_buf);
1833
1834 /* exported for use by the GUI */
1835 trace_running_p = (target_buf[1] == '1');
1836 }
1837 else
1838 error ("Trace can only be run on remote targets.");
1839 }
1840
1841 /* Worker function for the various flavors of the tfind command */
1842 static void
1843 finish_tfind_command (char *msg,
1844 long sizeof_msg,
1845 int from_tty)
1846 {
1847 int target_frameno = -1, target_tracept = -1;
1848 CORE_ADDR old_frame_addr;
1849 struct symbol *old_func;
1850 char *reply;
1851
1852 old_frame_addr = FRAME_FP (get_current_frame ());
1853 old_func = find_pc_function (read_pc ());
1854
1855 putpkt (msg);
1856 reply = remote_get_noisy_reply (msg, sizeof_msg);
1857
1858 while (reply && *reply)
1859 switch (*reply)
1860 {
1861 case 'F':
1862 if ((target_frameno = (int) strtol (++reply, &reply, 16)) == -1)
1863 {
1864 /* A request for a non-existant trace frame has failed.
1865 Our response will be different, depending on FROM_TTY:
1866
1867 If FROM_TTY is true, meaning that this command was
1868 typed interactively by the user, then give an error
1869 and DO NOT change the state of traceframe_number etc.
1870
1871 However if FROM_TTY is false, meaning that we're either
1872 in a script, a loop, or a user-defined command, then
1873 DON'T give an error, but DO change the state of
1874 traceframe_number etc. to invalid.
1875
1876 The rationalle is that if you typed the command, you
1877 might just have committed a typo or something, and you'd
1878 like to NOT lose your current debugging state. However
1879 if you're in a user-defined command or especially in a
1880 loop, then you need a way to detect that the command
1881 failed WITHOUT aborting. This allows you to write
1882 scripts that search thru the trace buffer until the end,
1883 and then continue on to do something else. */
1884
1885 if (from_tty)
1886 error ("Target failed to find requested trace frame.");
1887 else
1888 {
1889 if (info_verbose)
1890 printf_filtered ("End of trace buffer.\n");
1891 /* The following will not recurse, since it's special-cased */
1892 trace_find_command ("-1", from_tty);
1893 reply = NULL; /* break out of loop,
1894 (avoid recursive nonsense) */
1895 }
1896 }
1897 break;
1898 case 'T':
1899 if ((target_tracept = (int) strtol (++reply, &reply, 16)) == -1)
1900 error ("Target failed to find requested trace frame.");
1901 break;
1902 case 'O': /* "OK"? */
1903 if (reply[1] == 'K' && reply[2] == '\0')
1904 reply += 2;
1905 else
1906 error ("Bogus reply from target: %s", reply);
1907 break;
1908 default:
1909 error ("Bogus reply from target: %s", reply);
1910 }
1911
1912 flush_cached_frames ();
1913 registers_changed ();
1914 select_frame (get_current_frame (), 0);
1915 set_traceframe_num (target_frameno);
1916 set_tracepoint_num (target_tracept);
1917 if (target_frameno == -1)
1918 set_traceframe_context (-1);
1919 else
1920 set_traceframe_context (read_pc ());
1921
1922 if (from_tty)
1923 {
1924 int source_only;
1925
1926 /* NOTE: in immitation of the step command, try to determine
1927 whether we have made a transition from one function to another.
1928 If so, we'll print the "stack frame" (ie. the new function and
1929 it's arguments) -- otherwise we'll just show the new source line.
1930
1931 This determination is made by checking (1) whether the current
1932 function has changed, and (2) whether the current FP has changed.
1933 Hack: if the FP wasn't collected, either at the current or the
1934 previous frame, assume that the FP has NOT changed. */
1935
1936 if (old_func == find_pc_function (read_pc ()) &&
1937 (old_frame_addr == 0 ||
1938 FRAME_FP (get_current_frame ()) == 0 ||
1939 old_frame_addr == FRAME_FP (get_current_frame ())))
1940 source_only = -1;
1941 else
1942 source_only = 1;
1943
1944 print_stack_frame (selected_frame, selected_frame_level, source_only);
1945 do_displays ();
1946 }
1947 }
1948
1949 /* trace_find_command takes a trace frame number n,
1950 sends "QTFrame:<n>" to the target,
1951 and accepts a reply that may contain several optional pieces
1952 of information: a frame number, a tracepoint number, and an
1953 indication of whether this is a trap frame or a stepping frame.
1954
1955 The minimal response is just "OK" (which indicates that the
1956 target does not give us a frame number or a tracepoint number).
1957 Instead of that, the target may send us a string containing
1958 any combination of:
1959 F<hexnum> (gives the selected frame number)
1960 T<hexnum> (gives the selected tracepoint number)
1961 */
1962
1963 /* tfind command */
1964 static void
1965 trace_find_command (char *args, int from_tty)
1966 { /* STUB_COMM PART_IMPLEMENTED */
1967 /* this should only be called with a numeric argument */
1968 int frameno = -1;
1969
1970 if (target_is_remote ())
1971 {
1972 if (trace_find_hook)
1973 trace_find_hook (args, from_tty);
1974
1975 if (args == 0 || *args == 0)
1976 { /* TFIND with no args means find NEXT trace frame. */
1977 if (traceframe_number == -1)
1978 frameno = 0; /* "next" is first one */
1979 else
1980 frameno = traceframe_number + 1;
1981 }
1982 else if (0 == strcmp (args, "-"))
1983 {
1984 if (traceframe_number == -1)
1985 error ("not debugging trace buffer");
1986 else if (from_tty && traceframe_number == 0)
1987 error ("already at start of trace buffer");
1988
1989 frameno = traceframe_number - 1;
1990 }
1991 else
1992 frameno = parse_and_eval_long (args);
1993
1994 if (frameno < -1)
1995 error ("invalid input (%d is less than zero)", frameno);
1996
1997 sprintf (target_buf, "QTFrame:%x", frameno);
1998 finish_tfind_command (target_buf, sizeof (target_buf), from_tty);
1999 }
2000 else
2001 error ("Trace can only be run on remote targets.");
2002 }
2003
2004 /* tfind end */
2005 static void
2006 trace_find_end_command (char *args, int from_tty)
2007 {
2008 trace_find_command ("-1", from_tty);
2009 }
2010
2011 /* tfind none */
2012 static void
2013 trace_find_none_command (char *args, int from_tty)
2014 {
2015 trace_find_command ("-1", from_tty);
2016 }
2017
2018 /* tfind start */
2019 static void
2020 trace_find_start_command (char *args, int from_tty)
2021 {
2022 trace_find_command ("0", from_tty);
2023 }
2024
2025 /* tfind pc command */
2026 static void
2027 trace_find_pc_command (char *args, int from_tty)
2028 { /* STUB_COMM PART_IMPLEMENTED */
2029 CORE_ADDR pc;
2030 char tmp[40];
2031
2032 if (target_is_remote ())
2033 {
2034 if (args == 0 || *args == 0)
2035 pc = read_pc (); /* default is current pc */
2036 else
2037 pc = parse_and_eval_address (args);
2038
2039 sprintf_vma (tmp, pc);
2040 sprintf (target_buf, "QTFrame:pc:%s", tmp);
2041 finish_tfind_command (target_buf, sizeof (target_buf), from_tty);
2042 }
2043 else
2044 error ("Trace can only be run on remote targets.");
2045 }
2046
2047 /* tfind tracepoint command */
2048 static void
2049 trace_find_tracepoint_command (char *args, int from_tty)
2050 { /* STUB_COMM PART_IMPLEMENTED */
2051 int tdp;
2052
2053 if (target_is_remote ())
2054 {
2055 if (args == 0 || *args == 0)
2056 if (tracepoint_number == -1)
2057 error ("No current tracepoint -- please supply an argument.");
2058 else
2059 tdp = tracepoint_number; /* default is current TDP */
2060 else
2061 tdp = parse_and_eval_long (args);
2062
2063 sprintf (target_buf, "QTFrame:tdp:%x", tdp);
2064 finish_tfind_command (target_buf, sizeof (target_buf), from_tty);
2065 }
2066 else
2067 error ("Trace can only be run on remote targets.");
2068 }
2069
2070 /* TFIND LINE command:
2071
2072 This command will take a sourceline for argument, just like BREAK
2073 or TRACE (ie. anything that "decode_line_1" can handle).
2074
2075 With no argument, this command will find the next trace frame
2076 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2077
2078 static void
2079 trace_find_line_command (char *args, int from_tty)
2080 { /* STUB_COMM PART_IMPLEMENTED */
2081 static CORE_ADDR start_pc, end_pc;
2082 struct symtabs_and_lines sals;
2083 struct symtab_and_line sal;
2084 struct cleanup *old_chain;
2085 char startpc_str[40], endpc_str[40];
2086
2087 if (target_is_remote ())
2088 {
2089 if (args == 0 || *args == 0)
2090 {
2091 sal = find_pc_line ((get_current_frame ())->pc, 0);
2092 sals.nelts = 1;
2093 sals.sals = (struct symtab_and_line *)
2094 xmalloc (sizeof (struct symtab_and_line));
2095 sals.sals[0] = sal;
2096 }
2097 else
2098 {
2099 sals = decode_line_spec (args, 1);
2100 sal = sals.sals[0];
2101 }
2102
2103 old_chain = make_cleanup (xfree, sals.sals);
2104 if (sal.symtab == 0)
2105 {
2106 printf_filtered ("TFIND: No line number information available");
2107 if (sal.pc != 0)
2108 {
2109 /* This is useful for "info line *0x7f34". If we can't tell the
2110 user about a source line, at least let them have the symbolic
2111 address. */
2112 printf_filtered (" for address ");
2113 wrap_here (" ");
2114 print_address (sal.pc, gdb_stdout);
2115 printf_filtered (";\n -- will attempt to find by PC. \n");
2116 }
2117 else
2118 {
2119 printf_filtered (".\n");
2120 return; /* no line, no PC; what can we do? */
2121 }
2122 }
2123 else if (sal.line > 0
2124 && find_line_pc_range (sal, &start_pc, &end_pc))
2125 {
2126 if (start_pc == end_pc)
2127 {
2128 printf_filtered ("Line %d of \"%s\"",
2129 sal.line, sal.symtab->filename);
2130 wrap_here (" ");
2131 printf_filtered (" is at address ");
2132 print_address (start_pc, gdb_stdout);
2133 wrap_here (" ");
2134 printf_filtered (" but contains no code.\n");
2135 sal = find_pc_line (start_pc, 0);
2136 if (sal.line > 0 &&
2137 find_line_pc_range (sal, &start_pc, &end_pc) &&
2138 start_pc != end_pc)
2139 printf_filtered ("Attempting to find line %d instead.\n",
2140 sal.line);
2141 else
2142 error ("Cannot find a good line.");
2143 }
2144 }
2145 else
2146 /* Is there any case in which we get here, and have an address
2147 which the user would want to see? If we have debugging symbols
2148 and no line numbers? */
2149 error ("Line number %d is out of range for \"%s\".\n",
2150 sal.line, sal.symtab->filename);
2151
2152 sprintf_vma (startpc_str, start_pc);
2153 sprintf_vma (endpc_str, end_pc - 1);
2154 if (args && *args) /* find within range of stated line */
2155 sprintf (target_buf, "QTFrame:range:%s:%s", startpc_str, endpc_str);
2156 else /* find OUTSIDE OF range of CURRENT line */
2157 sprintf (target_buf, "QTFrame:outside:%s:%s", startpc_str, endpc_str);
2158 finish_tfind_command (target_buf, sizeof (target_buf), from_tty);
2159 do_cleanups (old_chain);
2160 }
2161 else
2162 error ("Trace can only be run on remote targets.");
2163 }
2164
2165 /* tfind range command */
2166 static void
2167 trace_find_range_command (char *args, int from_tty)
2168 {
2169 static CORE_ADDR start, stop;
2170 char start_str[40], stop_str[40];
2171 char *tmp;
2172
2173 if (target_is_remote ())
2174 {
2175 if (args == 0 || *args == 0)
2176 { /* XXX FIXME: what should default behavior be? */
2177 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2178 return;
2179 }
2180
2181 if (0 != (tmp = strchr (args, ',')))
2182 {
2183 *tmp++ = '\0'; /* terminate start address */
2184 while (isspace ((int) *tmp))
2185 tmp++;
2186 start = parse_and_eval_address (args);
2187 stop = parse_and_eval_address (tmp);
2188 }
2189 else
2190 { /* no explicit end address? */
2191 start = parse_and_eval_address (args);
2192 stop = start + 1; /* ??? */
2193 }
2194
2195 sprintf_vma (start_str, start);
2196 sprintf_vma (stop_str, stop);
2197 sprintf (target_buf, "QTFrame:range:%s:%s", start_str, stop_str);
2198 finish_tfind_command (target_buf, sizeof (target_buf), from_tty);
2199 }
2200 else
2201 error ("Trace can only be run on remote targets.");
2202 }
2203
2204 /* tfind outside command */
2205 static void
2206 trace_find_outside_command (char *args, int from_tty)
2207 {
2208 CORE_ADDR start, stop;
2209 char start_str[40], stop_str[40];
2210 char *tmp;
2211
2212 if (target_is_remote ())
2213 {
2214 if (args == 0 || *args == 0)
2215 { /* XXX FIXME: what should default behavior be? */
2216 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2217 return;
2218 }
2219
2220 if (0 != (tmp = strchr (args, ',')))
2221 {
2222 *tmp++ = '\0'; /* terminate start address */
2223 while (isspace ((int) *tmp))
2224 tmp++;
2225 start = parse_and_eval_address (args);
2226 stop = parse_and_eval_address (tmp);
2227 }
2228 else
2229 { /* no explicit end address? */
2230 start = parse_and_eval_address (args);
2231 stop = start + 1; /* ??? */
2232 }
2233
2234 sprintf_vma (start_str, start);
2235 sprintf_vma (stop_str, stop);
2236 sprintf (target_buf, "QTFrame:outside:%s:%s", start_str, stop_str);
2237 finish_tfind_command (target_buf, sizeof (target_buf), from_tty);
2238 }
2239 else
2240 error ("Trace can only be run on remote targets.");
2241 }
2242
2243 /* save-tracepoints command */
2244 static void
2245 tracepoint_save_command (char *args, int from_tty)
2246 {
2247 struct tracepoint *tp;
2248 struct action_line *line;
2249 FILE *fp;
2250 char *i1 = " ", *i2 = " ";
2251 char *indent, *actionline;
2252 char tmp[40];
2253
2254 if (args == 0 || *args == 0)
2255 error ("Argument required (file name in which to save tracepoints");
2256
2257 if (tracepoint_chain == 0)
2258 {
2259 warning ("save-tracepoints: no tracepoints to save.\n");
2260 return;
2261 }
2262
2263 if (!(fp = fopen (args, "w")))
2264 error ("Unable to open file '%s' for saving tracepoints");
2265
2266 ALL_TRACEPOINTS (tp)
2267 {
2268 if (tp->addr_string)
2269 fprintf (fp, "trace %s\n", tp->addr_string);
2270 else
2271 {
2272 sprintf_vma (tmp, tp->address);
2273 fprintf (fp, "trace *0x%s\n", tmp);
2274 }
2275
2276 if (tp->pass_count)
2277 fprintf (fp, " passcount %d\n", tp->pass_count);
2278
2279 if (tp->actions)
2280 {
2281 fprintf (fp, " actions\n");
2282 indent = i1;
2283 for (line = tp->actions; line; line = line->next)
2284 {
2285 struct cmd_list_element *cmd;
2286
2287 QUIT; /* allow user to bail out with ^C */
2288 actionline = line->action;
2289 while (isspace ((int) *actionline))
2290 actionline++;
2291
2292 fprintf (fp, "%s%s\n", indent, actionline);
2293 if (*actionline != '#') /* skip for comment lines */
2294 {
2295 cmd = lookup_cmd (&actionline, cmdlist, "", -1, 1);
2296 if (cmd == 0)
2297 error ("Bad action list item: %s", actionline);
2298 if (cmd->function.cfunc == while_stepping_pseudocommand)
2299 indent = i2;
2300 else if (cmd->function.cfunc == end_actions_pseudocommand)
2301 indent = i1;
2302 }
2303 }
2304 }
2305 }
2306 fclose (fp);
2307 if (from_tty)
2308 printf_filtered ("Tracepoints saved to file '%s'.\n", args);
2309 return;
2310 }
2311
2312 /* info scope command: list the locals for a scope. */
2313 static void
2314 scope_info (char *args, int from_tty)
2315 {
2316 struct symtabs_and_lines sals;
2317 struct symbol *sym;
2318 struct minimal_symbol *msym;
2319 struct block *block;
2320 char **canonical, *symname, *save_args = args;
2321 int i, j, nsyms, count = 0;
2322
2323 if (args == 0 || *args == 0)
2324 error ("requires an argument (function, line or *addr) to define a scope");
2325
2326 sals = decode_line_1 (&args, 1, NULL, 0, &canonical);
2327 if (sals.nelts == 0)
2328 return; /* presumably decode_line_1 has already warned */
2329
2330 /* Resolve line numbers to PC */
2331 resolve_sal_pc (&sals.sals[0]);
2332 block = block_for_pc (sals.sals[0].pc);
2333
2334 while (block != 0)
2335 {
2336 QUIT; /* allow user to bail out with ^C */
2337 nsyms = BLOCK_NSYMS (block);
2338 for (i = 0; i < nsyms; i++)
2339 {
2340 QUIT; /* allow user to bail out with ^C */
2341 if (count == 0)
2342 printf_filtered ("Scope for %s:\n", save_args);
2343 count++;
2344 sym = BLOCK_SYM (block, i);
2345 symname = SYMBOL_NAME (sym);
2346 if (symname == NULL || *symname == '\0')
2347 continue; /* probably botched, certainly useless */
2348
2349 printf_filtered ("Symbol %s is ", symname);
2350 switch (SYMBOL_CLASS (sym))
2351 {
2352 default:
2353 case LOC_UNDEF: /* messed up symbol? */
2354 printf_filtered ("a bogus symbol, class %d.\n",
2355 SYMBOL_CLASS (sym));
2356 count--; /* don't count this one */
2357 continue;
2358 case LOC_CONST:
2359 printf_filtered ("a constant with value %ld (0x%lx)",
2360 SYMBOL_VALUE (sym), SYMBOL_VALUE (sym));
2361 break;
2362 case LOC_CONST_BYTES:
2363 printf_filtered ("constant bytes: ");
2364 if (SYMBOL_TYPE (sym))
2365 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2366 fprintf_filtered (gdb_stdout, " %02x",
2367 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2368 break;
2369 case LOC_STATIC:
2370 printf_filtered ("in static storage at address ");
2371 print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, gdb_stdout);
2372 break;
2373 case LOC_REGISTER:
2374 printf_filtered ("a local variable in register $%s",
2375 REGISTER_NAME (SYMBOL_VALUE (sym)));
2376 break;
2377 case LOC_ARG:
2378 case LOC_LOCAL_ARG:
2379 printf_filtered ("an argument at stack/frame offset %ld",
2380 SYMBOL_VALUE (sym));
2381 break;
2382 case LOC_LOCAL:
2383 printf_filtered ("a local variable at frame offset %ld",
2384 SYMBOL_VALUE (sym));
2385 break;
2386 case LOC_REF_ARG:
2387 printf_filtered ("a reference argument at offset %ld",
2388 SYMBOL_VALUE (sym));
2389 break;
2390 case LOC_REGPARM:
2391 printf_filtered ("an argument in register $%s",
2392 REGISTER_NAME (SYMBOL_VALUE (sym)));
2393 break;
2394 case LOC_REGPARM_ADDR:
2395 printf_filtered ("the address of an argument, in register $%s",
2396 REGISTER_NAME (SYMBOL_VALUE (sym)));
2397 break;
2398 case LOC_TYPEDEF:
2399 printf_filtered ("a typedef.\n");
2400 continue;
2401 case LOC_LABEL:
2402 printf_filtered ("a label at address ");
2403 print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, gdb_stdout);
2404 break;
2405 case LOC_BLOCK:
2406 printf_filtered ("a function at address ");
2407 print_address_numeric (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)), 1,
2408 gdb_stdout);
2409 break;
2410 case LOC_BASEREG:
2411 printf_filtered ("a variable at offset %ld from register $%s",
2412 SYMBOL_VALUE (sym),
2413 REGISTER_NAME (SYMBOL_BASEREG (sym)));
2414 break;
2415 case LOC_BASEREG_ARG:
2416 printf_filtered ("an argument at offset %ld from register $%s",
2417 SYMBOL_VALUE (sym),
2418 REGISTER_NAME (SYMBOL_BASEREG (sym)));
2419 break;
2420 case LOC_UNRESOLVED:
2421 msym = lookup_minimal_symbol (SYMBOL_NAME (sym), NULL, NULL);
2422 if (msym == NULL)
2423 printf_filtered ("Unresolved Static");
2424 else
2425 {
2426 printf_filtered ("static storage at address ");
2427 print_address_numeric (SYMBOL_VALUE_ADDRESS (msym), 1,
2428 gdb_stdout);
2429 }
2430 break;
2431 case LOC_OPTIMIZED_OUT:
2432 printf_filtered ("optimized out.\n");
2433 continue;
2434 }
2435 if (SYMBOL_TYPE (sym))
2436 printf_filtered (", length %d.\n",
2437 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
2438 }
2439 if (BLOCK_FUNCTION (block))
2440 break;
2441 else
2442 block = BLOCK_SUPERBLOCK (block);
2443 }
2444 if (count <= 0)
2445 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2446 save_args);
2447 }
2448
2449 /* worker function (cleanup) */
2450 static void
2451 replace_comma (void *data)
2452 {
2453 char *comma = data;
2454 *comma = ',';
2455 }
2456
2457 /* tdump command */
2458 static void
2459 trace_dump_command (char *args, int from_tty)
2460 {
2461 struct tracepoint *t;
2462 struct action_line *action;
2463 char *action_exp, *next_comma;
2464 struct cleanup *old_cleanups;
2465 int stepping_actions = 0;
2466 int stepping_frame = 0;
2467
2468 if (!target_is_remote ())
2469 {
2470 error ("Trace can only be run on remote targets.");
2471 return;
2472 }
2473
2474 if (tracepoint_number == -1)
2475 {
2476 warning ("No current trace frame.");
2477 return;
2478 }
2479
2480 ALL_TRACEPOINTS (t)
2481 if (t->number == tracepoint_number)
2482 break;
2483
2484 if (t == NULL)
2485 error ("No known tracepoint matches 'current' tracepoint #%d.",
2486 tracepoint_number);
2487
2488 old_cleanups = make_cleanup (null_cleanup, NULL);
2489
2490 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2491 tracepoint_number, traceframe_number);
2492
2493 /* The current frame is a trap frame if the frame PC is equal
2494 to the tracepoint PC. If not, then the current frame was
2495 collected during single-stepping. */
2496
2497 stepping_frame = (t->address != read_pc ());
2498
2499 for (action = t->actions; action; action = action->next)
2500 {
2501 struct cmd_list_element *cmd;
2502
2503 QUIT; /* allow user to bail out with ^C */
2504 action_exp = action->action;
2505 while (isspace ((int) *action_exp))
2506 action_exp++;
2507
2508 /* The collection actions to be done while stepping are
2509 bracketed by the commands "while-stepping" and "end". */
2510
2511 if (*action_exp == '#') /* comment line */
2512 continue;
2513
2514 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2515 if (cmd == 0)
2516 error ("Bad action list item: %s", action_exp);
2517
2518 if (cmd->function.cfunc == while_stepping_pseudocommand)
2519 stepping_actions = 1;
2520 else if (cmd->function.cfunc == end_actions_pseudocommand)
2521 stepping_actions = 0;
2522 else if (cmd->function.cfunc == collect_pseudocommand)
2523 {
2524 /* Display the collected data.
2525 For the trap frame, display only what was collected at the trap.
2526 Likewise for stepping frames, display only what was collected
2527 while stepping. This means that the two boolean variables,
2528 STEPPING_FRAME and STEPPING_ACTIONS should be equal. */
2529 if (stepping_frame == stepping_actions)
2530 {
2531 do
2532 { /* repeat over a comma-separated list */
2533 QUIT; /* allow user to bail out with ^C */
2534 if (*action_exp == ',')
2535 action_exp++;
2536 while (isspace ((int) *action_exp))
2537 action_exp++;
2538
2539 next_comma = strchr (action_exp, ',');
2540
2541 if (0 == strncasecmp (action_exp, "$reg", 4))
2542 registers_info (NULL, from_tty);
2543 else if (0 == strncasecmp (action_exp, "$loc", 4))
2544 locals_info (NULL, from_tty);
2545 else if (0 == strncasecmp (action_exp, "$arg", 4))
2546 args_info (NULL, from_tty);
2547 else
2548 { /* variable */
2549 if (next_comma)
2550 {
2551 make_cleanup (replace_comma, next_comma);
2552 *next_comma = '\0';
2553 }
2554 printf_filtered ("%s = ", action_exp);
2555 output_command (action_exp, from_tty);
2556 printf_filtered ("\n");
2557 }
2558 if (next_comma)
2559 *next_comma = ',';
2560 action_exp = next_comma;
2561 }
2562 while (action_exp && *action_exp == ',');
2563 }
2564 }
2565 }
2566 discard_cleanups (old_cleanups);
2567 }
2568
2569 /* Convert the memory pointed to by mem into hex, placing result in buf.
2570 * Return a pointer to the last char put in buf (null)
2571 * "stolen" from sparc-stub.c
2572 */
2573
2574 static const char hexchars[] = "0123456789abcdef";
2575
2576 static unsigned char *
2577 mem2hex (unsigned char *mem, unsigned char *buf, int count)
2578 {
2579 unsigned char ch;
2580
2581 while (count-- > 0)
2582 {
2583 ch = *mem++;
2584
2585 *buf++ = hexchars[ch >> 4];
2586 *buf++ = hexchars[ch & 0xf];
2587 }
2588
2589 *buf = 0;
2590
2591 return buf;
2592 }
2593
2594 int
2595 get_traceframe_number (void)
2596 {
2597 return traceframe_number;
2598 }
2599
2600
2601 /* module initialization */
2602 void
2603 _initialize_tracepoint (void)
2604 {
2605 struct cmd_list_element *c;
2606
2607 tracepoint_chain = 0;
2608 tracepoint_count = 0;
2609 traceframe_number = -1;
2610 tracepoint_number = -1;
2611
2612 set_internalvar (lookup_internalvar ("tpnum"),
2613 value_from_longest (builtin_type_int, (LONGEST) 0));
2614 set_internalvar (lookup_internalvar ("trace_frame"),
2615 value_from_longest (builtin_type_int, (LONGEST) - 1));
2616
2617 if (tracepoint_list.list == NULL)
2618 {
2619 tracepoint_list.listsize = 128;
2620 tracepoint_list.list = xmalloc
2621 (tracepoint_list.listsize * sizeof (struct memrange));
2622 }
2623 if (tracepoint_list.aexpr_list == NULL)
2624 {
2625 tracepoint_list.aexpr_listsize = 128;
2626 tracepoint_list.aexpr_list = xmalloc
2627 (tracepoint_list.aexpr_listsize * sizeof (struct agent_expr *));
2628 }
2629
2630 if (stepping_list.list == NULL)
2631 {
2632 stepping_list.listsize = 128;
2633 stepping_list.list = xmalloc
2634 (stepping_list.listsize * sizeof (struct memrange));
2635 }
2636
2637 if (stepping_list.aexpr_list == NULL)
2638 {
2639 stepping_list.aexpr_listsize = 128;
2640 stepping_list.aexpr_list = xmalloc
2641 (stepping_list.aexpr_listsize * sizeof (struct agent_expr *));
2642 }
2643
2644 add_info ("scope", scope_info,
2645 "List the variables local to a scope");
2646
2647 add_cmd ("tracepoints", class_trace, NO_FUNCTION,
2648 "Tracing of program execution without stopping the program.",
2649 &cmdlist);
2650
2651 add_info ("tracepoints", tracepoints_info,
2652 "Status of tracepoints, or tracepoint number NUMBER.\n\
2653 Convenience variable \"$tpnum\" contains the number of the\n\
2654 last tracepoint set.");
2655
2656 add_info_alias ("tp", "tracepoints", 1);
2657
2658 c = add_com ("save-tracepoints", class_trace, tracepoint_save_command,
2659 "Save current tracepoint definitions as a script.\n\
2660 Use the 'source' command in another debug session to restore them.");
2661 c->completer = filename_completer;
2662
2663 add_com ("tdump", class_trace, trace_dump_command,
2664 "Print everything collected at the current tracepoint.");
2665
2666 add_prefix_cmd ("tfind", class_trace, trace_find_command,
2667 "Select a trace frame;\n\
2668 No argument means forward by one frame; '-' meand backward by one frame.",
2669 &tfindlist, "tfind ", 1, &cmdlist);
2670
2671 add_cmd ("outside", class_trace, trace_find_outside_command,
2672 "Select a trace frame whose PC is outside the given \
2673 range.\nUsage: tfind outside addr1, addr2",
2674 &tfindlist);
2675
2676 add_cmd ("range", class_trace, trace_find_range_command,
2677 "Select a trace frame whose PC is in the given range.\n\
2678 Usage: tfind range addr1,addr2",
2679 &tfindlist);
2680
2681 add_cmd ("line", class_trace, trace_find_line_command,
2682 "Select a trace frame by source line.\n\
2683 Argument can be a line number (with optional source file), \n\
2684 a function name, or '*' followed by an address.\n\
2685 Default argument is 'the next source line that was traced'.",
2686 &tfindlist);
2687
2688 add_cmd ("tracepoint", class_trace, trace_find_tracepoint_command,
2689 "Select a trace frame by tracepoint number.\n\
2690 Default is the tracepoint for the current trace frame.",
2691 &tfindlist);
2692
2693 add_cmd ("pc", class_trace, trace_find_pc_command,
2694 "Select a trace frame by PC.\n\
2695 Default is the current PC, or the PC of the current trace frame.",
2696 &tfindlist);
2697
2698 add_cmd ("end", class_trace, trace_find_end_command,
2699 "Synonym for 'none'.\n\
2700 De-select any trace frame and resume 'live' debugging.",
2701 &tfindlist);
2702
2703 add_cmd ("none", class_trace, trace_find_none_command,
2704 "De-select any trace frame and resume 'live' debugging.",
2705 &tfindlist);
2706
2707 add_cmd ("start", class_trace, trace_find_start_command,
2708 "Select the first trace frame in the trace buffer.",
2709 &tfindlist);
2710
2711 add_com ("tstatus", class_trace, trace_status_command,
2712 "Display the status of the current trace data collection.");
2713
2714 add_com ("tstop", class_trace, trace_stop_command,
2715 "Stop trace data collection.");
2716
2717 add_com ("tstart", class_trace, trace_start_command,
2718 "Start trace data collection.");
2719
2720 add_com ("passcount", class_trace, trace_pass_command,
2721 "Set the passcount for a tracepoint.\n\
2722 The trace will end when the tracepoint has been passed 'count' times.\n\
2723 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
2724 if TPNUM is omitted, passcount refers to the last tracepoint defined.");
2725
2726 add_com ("end", class_trace, end_actions_pseudocommand,
2727 "Ends a list of commands or actions.\n\
2728 Several GDB commands allow you to enter a list of commands or actions.\n\
2729 Entering \"end\" on a line by itself is the normal way to terminate\n\
2730 such a list.\n\n\
2731 Note: the \"end\" command cannot be used at the gdb prompt.");
2732
2733 add_com ("while-stepping", class_trace, while_stepping_pseudocommand,
2734 "Specify single-stepping behavior at a tracepoint.\n\
2735 Argument is number of instructions to trace in single-step mode\n\
2736 following the tracepoint. This command is normally followed by\n\
2737 one or more \"collect\" commands, to specify what to collect\n\
2738 while single-stepping.\n\n\
2739 Note: this command can only be used in a tracepoint \"actions\" list.");
2740
2741 add_com_alias ("ws", "while-stepping", class_alias, 0);
2742 add_com_alias ("stepping", "while-stepping", class_alias, 0);
2743
2744 add_com ("collect", class_trace, collect_pseudocommand,
2745 "Specify one or more data items to be collected at a tracepoint.\n\
2746 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
2747 collect all data (variables, registers) referenced by that expression.\n\
2748 Also accepts the following special arguments:\n\
2749 $regs -- all registers.\n\
2750 $args -- all function arguments.\n\
2751 $locals -- all variables local to the block/function scope.\n\
2752 Note: this command can only be used in a tracepoint \"actions\" list.");
2753
2754 add_com ("actions", class_trace, trace_actions_command,
2755 "Specify the actions to be taken at a tracepoint.\n\
2756 Tracepoint actions may include collecting of specified data, \n\
2757 single-stepping, or enabling/disabling other tracepoints, \n\
2758 depending on target's capabilities.");
2759
2760 add_cmd ("tracepoints", class_trace, delete_trace_command,
2761 "Delete specified tracepoints.\n\
2762 Arguments are tracepoint numbers, separated by spaces.\n\
2763 No argument means delete all tracepoints.",
2764 &deletelist);
2765
2766 add_cmd ("tracepoints", class_trace, disable_trace_command,
2767 "Disable specified tracepoints.\n\
2768 Arguments are tracepoint numbers, separated by spaces.\n\
2769 No argument means disable all tracepoints.",
2770 &disablelist);
2771
2772 add_cmd ("tracepoints", class_trace, enable_trace_command,
2773 "Enable specified tracepoints.\n\
2774 Arguments are tracepoint numbers, separated by spaces.\n\
2775 No argument means enable all tracepoints.",
2776 &enablelist);
2777
2778 add_com ("trace", class_trace, trace_command,
2779 "Set a tracepoint at a specified line or function or address.\n\
2780 Argument may be a line number, function name, or '*' plus an address.\n\
2781 For a line number or function, trace at the start of its code.\n\
2782 If an address is specified, trace at that exact address.\n\n\
2783 Do \"help tracepoints\" for info on other tracepoint commands.");
2784
2785 add_com_alias ("tp", "trace", class_alias, 0);
2786 add_com_alias ("tr", "trace", class_alias, 1);
2787 add_com_alias ("tra", "trace", class_alias, 1);
2788 add_com_alias ("trac", "trace", class_alias, 1);
2789 }