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