1 /* Tracing functionality for remote targets in custom GDB protocol
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
22 #include "arch-utils.h"
26 #include "expression.h"
31 #include "gdb_string.h"
33 #include "breakpoint.h"
34 #include "tracepoint.h"
36 extern int remote_supports_cond_tracepoints (void);
37 extern char *unpack_varlen_hex (char *buff, ULONGEST *result);
40 #include "completer.h"
42 #include "dictionary.h"
44 #include "user-regs.h"
52 /* readline include files */
53 #include "readline/readline.h"
54 #include "readline/history.h"
56 /* readline defines this. */
63 /* Maximum length of an agent aexpression.
64 This accounts for the fact that packets are limited to 400 bytes
65 (which includes everything -- including the checksum), and assumes
66 the worst case of maximum length for each of the pieces of a
69 NOTE: expressions get mem2hex'ed otherwise this would be twice as
70 large. (400 - 31)/2 == 184 */
71 #define MAX_AGENT_EXPR_LEN 184
73 /* A hook used to notify the UI of tracepoint operations. */
75 void (*deprecated_trace_find_hook) (char *arg, int from_tty);
76 void (*deprecated_trace_start_stop_hook) (int start, int from_tty);
78 extern void (*deprecated_readline_begin_hook) (char *, ...);
79 extern char *(*deprecated_readline_hook) (char *);
80 extern void (*deprecated_readline_end_hook) (void);
82 /* GDB commands implemented in other modules:
85 extern void output_command (char *, int);
90 This module defines the following debugger commands:
91 trace : set a tracepoint on a function, line, or address.
92 info trace : list all debugger-defined tracepoints.
93 delete trace : delete one or more tracepoints.
94 enable trace : enable one or more tracepoints.
95 disable trace : disable one or more tracepoints.
96 actions : specify actions to be taken at a tracepoint.
97 passcount : specify a pass count for a tracepoint.
98 tstart : start a trace experiment.
99 tstop : stop a trace experiment.
100 tstatus : query the status of a trace experiment.
101 tfind : find a trace frame in the trace buffer.
102 tdump : print everything collected at the current tracepoint.
103 save-tracepoints : write tracepoint setup into a file.
105 This module defines the following user-visible debugger variables:
106 $trace_frame : sequence number of trace frame currently being debugged.
107 $trace_line : source line of trace frame currently being debugged.
108 $trace_file : source file of trace frame currently being debugged.
109 $tracepoint : tracepoint number of trace frame currently being debugged.
113 /* ======= Important global variables: ======= */
115 /* The list of all trace state variables. We don't retain pointers to
116 any of these for any reason - API is by name or number only - so it
117 works to have a vector of objects. */
119 typedef struct trace_state_variable tsv_s;
122 static VEC(tsv_s) *tvariables;
124 /* The next integer to assign to a variable. */
126 static int next_tsv_number = 1;
128 /* Number of last traceframe collected. */
129 static int traceframe_number;
131 /* Tracepoint for last traceframe collected. */
132 static int tracepoint_number;
134 /* Symbol for function for last traceframe collected */
135 static struct symbol *traceframe_fun;
137 /* Symtab and line for last traceframe collected */
138 static struct symtab_and_line traceframe_sal;
140 /* Tracing command lists */
141 static struct cmd_list_element *tfindlist;
143 /* List of expressions to collect by default at each tracepoint hit. */
144 static char *default_collect = "";
146 static char *target_buf;
147 static long target_buf_size;
149 /* ======= Important command functions: ======= */
150 static void trace_actions_command (char *, int);
151 static void trace_start_command (char *, int);
152 static void trace_stop_command (char *, int);
153 static void trace_status_command (char *, int);
154 static void trace_find_command (char *, int);
155 static void trace_find_pc_command (char *, int);
156 static void trace_find_tracepoint_command (char *, int);
157 static void trace_find_line_command (char *, int);
158 static void trace_find_range_command (char *, int);
159 static void trace_find_outside_command (char *, int);
160 static void tracepoint_save_command (char *, int);
161 static void trace_dump_command (char *, int);
163 /* support routines */
165 struct collection_list;
166 static void add_aexpr (struct collection_list *, struct agent_expr *);
167 static char *mem2hex (gdb_byte *, char *, int);
168 static void add_register (struct collection_list *collection,
170 static struct cleanup *make_cleanup_free_actions (struct breakpoint *t);
171 static void free_actions_list (char **actions_list);
172 static void free_actions_list_cleanup_wrapper (void *);
174 extern void _initialize_tracepoint (void);
176 /* Utility: returns true if "target remote" */
178 target_is_remote (void)
180 if (current_target.to_shortname &&
181 (strcmp (current_target.to_shortname, "remote") == 0
182 || strcmp (current_target.to_shortname, "extended-remote") == 0))
188 /* Utility: generate error from an incoming stub packet. */
190 trace_error (char *buf)
193 return; /* not an error msg */
196 case '1': /* malformed packet error */
197 if (*++buf == '0') /* general case: */
198 error (_("tracepoint.c: error in outgoing packet."));
200 error (_("tracepoint.c: error in outgoing packet at field #%ld."),
201 strtol (buf, NULL, 16));
203 error (_("trace API error 0x%s."), ++buf);
205 error (_("Target returns error code '%s'."), buf);
209 /* Utility: wait for reply from stub, while accepting "O" packets. */
211 remote_get_noisy_reply (char **buf_p,
214 do /* Loop on reply from remote stub. */
217 QUIT; /* allow user to bail out with ^C */
218 getpkt (buf_p, sizeof_buf, 0);
221 error (_("Target does not support this command."));
222 else if (buf[0] == 'E')
224 else if (buf[0] == 'O' &&
226 remote_console_output (buf + 1); /* 'O' message from stub */
228 return buf; /* here's the actual reply */
233 /* Set traceframe number to NUM. */
235 set_traceframe_num (int num)
237 traceframe_number = num;
238 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
241 /* Set tracepoint number to NUM. */
243 set_tracepoint_num (int num)
245 tracepoint_number = num;
246 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
249 /* Set externally visible debug variables for querying/printing
250 the traceframe context (line, function, file) */
253 set_traceframe_context (struct frame_info *trace_frame)
257 if (trace_frame == NULL) /* Cease debugging any trace buffers. */
260 traceframe_sal.pc = traceframe_sal.line = 0;
261 traceframe_sal.symtab = NULL;
262 clear_internalvar (lookup_internalvar ("trace_func"));
263 clear_internalvar (lookup_internalvar ("trace_file"));
264 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
268 /* Save as globals for internal use. */
269 trace_pc = get_frame_pc (trace_frame);
270 traceframe_sal = find_pc_line (trace_pc, 0);
271 traceframe_fun = find_pc_function (trace_pc);
273 /* Save linenumber as "$trace_line", a debugger variable visible to
275 set_internalvar_integer (lookup_internalvar ("trace_line"),
276 traceframe_sal.line);
278 /* Save func name as "$trace_func", a debugger variable visible to
280 if (traceframe_fun == NULL
281 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
282 clear_internalvar (lookup_internalvar ("trace_func"));
284 set_internalvar_string (lookup_internalvar ("trace_func"),
285 SYMBOL_LINKAGE_NAME (traceframe_fun));
287 /* Save file name as "$trace_file", a debugger variable visible to
289 if (traceframe_sal.symtab == NULL
290 || traceframe_sal.symtab->filename == NULL)
291 clear_internalvar (lookup_internalvar ("trace_file"));
293 set_internalvar_string (lookup_internalvar ("trace_file"),
294 traceframe_sal.symtab->filename);
297 /* Create a new trace state variable with the given name. */
299 struct trace_state_variable *
300 create_trace_state_variable (const char *name)
302 struct trace_state_variable tsv;
304 memset (&tsv, 0, sizeof (tsv));
306 tsv.number = next_tsv_number++;
307 return VEC_safe_push (tsv_s, tvariables, &tsv);
310 /* Look for a trace state variable of the given name. */
312 struct trace_state_variable *
313 find_trace_state_variable (const char *name)
315 struct trace_state_variable *tsv;
318 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
319 if (strcmp (name, tsv->name) == 0)
326 delete_trace_state_variable (const char *name)
328 struct trace_state_variable *tsv;
331 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
332 if (strcmp (name, tsv->name) == 0)
334 VEC_unordered_remove (tsv_s, tvariables, ix);
338 warning (_("No trace variable named \"$%s\", not deleting"), name);
341 /* The 'tvariable' command collects a name and optional expression to
342 evaluate into an initial value. */
345 trace_variable_command (char *args, int from_tty)
347 struct expression *expr;
348 struct cleanup *old_chain;
349 struct internalvar *intvar = NULL;
351 struct trace_state_variable *tsv;
354 error_no_arg (_("trace state variable name"));
356 /* All the possible valid arguments are expressions. */
357 expr = parse_expression (args);
358 old_chain = make_cleanup (free_current_contents, &expr);
360 if (expr->nelts == 0)
361 error (_("No expression?"));
363 /* Only allow two syntaxes; "$name" and "$name=value". */
364 if (expr->elts[0].opcode == OP_INTERNALVAR)
366 intvar = expr->elts[1].internalvar;
368 else if (expr->elts[0].opcode == BINOP_ASSIGN
369 && expr->elts[1].opcode == OP_INTERNALVAR)
371 intvar = expr->elts[2].internalvar;
372 initval = value_as_long (evaluate_subexpression_type (expr, 4));
375 error (_("Syntax must be $NAME [ = EXPR ]"));
378 error (_("No name given"));
380 if (strlen (internalvar_name (intvar)) <= 0)
381 error (_("Must supply a non-empty variable name"));
383 /* If the variable already exists, just change its initial value. */
384 tsv = find_trace_state_variable (internalvar_name (intvar));
387 tsv->initial_value = initval;
388 printf_filtered (_("Trace state variable $%s now has initial value %s.\n"),
389 tsv->name, plongest (tsv->initial_value));
393 /* Create a new variable. */
394 tsv = create_trace_state_variable (internalvar_name (intvar));
395 tsv->initial_value = initval;
397 printf_filtered (_("Trace state variable $%s created, with initial value %s.\n"),
398 tsv->name, plongest (tsv->initial_value));
400 do_cleanups (old_chain);
404 delete_trace_variable_command (char *args, int from_tty)
408 struct cleanup *back_to;
409 struct trace_state_variable *tsv;
413 if (query (_("Delete all trace state variables? ")))
414 VEC_free (tsv_s, tvariables);
419 argv = gdb_buildargv (args);
420 back_to = make_cleanup_freeargv (argv);
422 for (i = 0; argv[i] != NULL; i++)
425 delete_trace_state_variable (argv[i] + 1);
427 warning (_("Name \"%s\" not prefixed with '$', ignoring"), argv[i]);
430 do_cleanups (back_to);
435 /* List all the trace state variables. */
438 tvariables_info (char *args, int from_tty)
440 struct trace_state_variable *tsv;
445 if (target_is_remote ())
449 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
451 /* We don't know anything about the value until we get a
453 tsv->value_known = 0;
454 sprintf (buf, "qTV:%x", tsv->number);
456 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
461 unpack_varlen_hex (reply + 1, &tval);
462 tsv->value = (LONGEST) tval;
463 tsv->value_known = 1;
465 /* FIXME say anything about oddball replies? */
470 if (VEC_length (tsv_s, tvariables) == 0)
472 printf_filtered (_("No trace state variables.\n"));
476 printf_filtered (_("Name\t\t Initial\tCurrent\n"));
478 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
480 printf_filtered ("$%s", tsv->name);
481 print_spaces_filtered (17 - strlen (tsv->name), gdb_stdout);
482 printf_filtered ("%s ", plongest (tsv->initial_value));
483 print_spaces_filtered (11 - strlen (plongest (tsv->initial_value)), gdb_stdout);
484 if (tsv->value_known)
485 printf_filtered (" %s", plongest (tsv->value));
486 else if (trace_running_p || traceframe_number >= 0)
487 /* The value is/was defined, but we don't have it. */
488 printf_filtered (_(" <unknown>"));
490 /* It is not meaningful to ask about the value. */
491 printf_filtered (_(" <undefined>"));
492 printf_filtered ("\n");
496 /* ACTIONS functions: */
498 /* Prototypes for action-parsing utility commands */
499 static void read_actions (struct breakpoint *);
501 /* The three functions:
502 collect_pseudocommand,
503 while_stepping_pseudocommand, and
504 end_actions_pseudocommand
505 are placeholders for "commands" that are actually ONLY to be used
506 within a tracepoint action list. If the actual function is ever called,
507 it means that somebody issued the "command" at the top level,
508 which is always an error. */
511 end_actions_pseudocommand (char *args, int from_tty)
513 error (_("This command cannot be used at the top level."));
517 while_stepping_pseudocommand (char *args, int from_tty)
519 error (_("This command can only be used in a tracepoint actions list."));
523 collect_pseudocommand (char *args, int from_tty)
525 error (_("This command can only be used in a tracepoint actions list."));
528 /* Enter a list of actions for a tracepoint. */
530 trace_actions_command (char *args, int from_tty)
532 struct breakpoint *t;
534 char *end_msg = "End with a line saying just \"end\".";
536 t = get_tracepoint_by_number (&args, 0, 1);
539 sprintf (tmpbuf, "Enter actions for tracepoint %d, one per line.",
544 if (deprecated_readline_begin_hook)
545 (*deprecated_readline_begin_hook) ("%s %s\n", tmpbuf, end_msg);
546 else if (input_from_terminal_p ())
547 printf_filtered ("%s\n%s\n", tmpbuf, end_msg);
551 t->step_count = 0; /* read_actions may set this */
554 if (deprecated_readline_end_hook)
555 (*deprecated_readline_end_hook) ();
556 /* tracepoints_changed () */
558 /* else just return */
561 /* worker function */
563 read_actions (struct breakpoint *t)
566 char *prompt1 = "> ", *prompt2 = " > ";
567 char *prompt = prompt1;
568 enum actionline_type linetype;
569 extern FILE *instream;
570 struct action_line *next = NULL, *temp;
571 struct cleanup *old_chain;
573 /* Control-C quits instantly if typed while in this loop
574 since it should not wait until the user types a newline. */
576 /* FIXME: kettenis/20010823: Something is wrong here. In this file
577 STOP_SIGNAL is never defined. So this code has been left out, at
578 least for quite a while now. Replacing STOP_SIGNAL with SIGTSTP
579 leads to compilation failures since the variable job_control
580 isn't declared. Leave this alone for now. */
583 signal (STOP_SIGNAL, handle_stop_sig);
585 old_chain = make_cleanup_free_actions (t);
588 /* Make sure that all output has been output. Some machines may
589 let you get away with leaving out some of the gdb_flush, but
592 gdb_flush (gdb_stdout);
593 gdb_flush (gdb_stderr);
595 if (deprecated_readline_hook && instream == NULL)
596 line = (*deprecated_readline_hook) (prompt);
597 else if (instream == stdin && ISATTY (instream))
599 line = gdb_readline_wrapper (prompt);
600 if (line && *line) /* add it to command history */
604 line = gdb_readline (0);
608 line = xstrdup ("end");
609 printf_filtered ("end\n");
612 linetype = validate_actionline (&line, t);
613 if (linetype == BADLINE)
614 continue; /* already warned -- collect another line */
616 temp = xmalloc (sizeof (struct action_line));
620 if (next == NULL) /* first action for this tracepoint? */
621 t->actions = next = temp;
628 if (linetype == STEPPING) /* begin "while-stepping" */
630 if (prompt == prompt2)
632 warning (_("Already processing 'while-stepping'"));
636 prompt = prompt2; /* change prompt for stepping actions */
638 else if (linetype == END)
640 if (prompt == prompt2)
642 prompt = prompt1; /* end of single-stepping actions */
645 { /* end of actions */
646 if (t->actions->next == NULL)
648 /* An "end" all by itself with no other actions
649 means this tracepoint has no actions.
650 Discard empty list. */
659 signal (STOP_SIGNAL, SIG_DFL);
662 discard_cleanups (old_chain);
665 /* worker function */
667 validate_actionline (char **line, struct breakpoint *t)
669 struct cmd_list_element *c;
670 struct expression *exp = NULL;
671 struct cleanup *old_chain = NULL;
674 /* if EOF is typed, *line is NULL */
678 for (p = *line; isspace ((int) *p);)
681 /* Symbol lookup etc. */
682 if (*p == '\0') /* empty line: just prompt for another line. */
685 if (*p == '#') /* comment line */
688 c = lookup_cmd (&p, cmdlist, "", -1, 1);
691 warning (_("'%s' is not an action that I know, or is ambiguous."),
696 if (cmd_cfunc_eq (c, collect_pseudocommand))
698 struct agent_expr *aexpr;
699 struct agent_reqs areqs;
702 { /* repeat over a comma-separated list */
703 QUIT; /* allow user to bail out with ^C */
704 while (isspace ((int) *p))
707 if (*p == '$') /* look for special pseudo-symbols */
709 if ((0 == strncasecmp ("reg", p + 1, 3)) ||
710 (0 == strncasecmp ("arg", p + 1, 3)) ||
711 (0 == strncasecmp ("loc", p + 1, 3)))
716 /* else fall thru, treat p as an expression and parse it! */
718 exp = parse_exp_1 (&p, block_for_pc (t->loc->address), 1);
719 old_chain = make_cleanup (free_current_contents, &exp);
721 if (exp->elts[0].opcode == OP_VAR_VALUE)
723 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
725 warning (_("constant %s (value %ld) will not be collected."),
726 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
727 SYMBOL_VALUE (exp->elts[2].symbol));
730 else if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_OPTIMIZED_OUT)
732 warning (_("%s is optimized away and cannot be collected."),
733 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
738 /* We have something to collect, make sure that the expr to
739 bytecode translator can handle it and that it's not too
741 aexpr = gen_trace_for_expr (t->loc->address, exp);
742 make_cleanup_free_agent_expr (aexpr);
744 if (aexpr->len > MAX_AGENT_EXPR_LEN)
745 error (_("expression too complicated, try simplifying"));
747 ax_reqs (aexpr, &areqs);
748 (void) make_cleanup (xfree, areqs.reg_mask);
750 if (areqs.flaw != agent_flaw_none)
751 error (_("malformed expression"));
753 if (areqs.min_height < 0)
754 error (_("gdb: Internal error: expression has min height < 0"));
756 if (areqs.max_height > 20)
757 error (_("expression too complicated, try simplifying"));
759 do_cleanups (old_chain);
761 while (p && *p++ == ',');
764 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
766 char *steparg; /* in case warning is necessary */
768 while (isspace ((int) *p))
773 (t->step_count = strtol (p, &p, 0)) == 0)
775 warning (_("'%s': bad step-count; command ignored."), *line);
780 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
784 warning (_("'%s' is not a supported tracepoint action."), *line);
789 /* worker function */
791 free_actions (struct breakpoint *t)
793 struct action_line *line, *next;
795 for (line = t->actions; line; line = next)
799 xfree (line->action);
806 do_free_actions_cleanup (void *t)
811 static struct cleanup *
812 make_cleanup_free_actions (struct breakpoint *t)
814 return make_cleanup (do_free_actions_cleanup, t);
818 memrange_absolute = -1
823 int type; /* memrange_absolute for absolute memory range,
824 else basereg number */
825 bfd_signed_vma start;
829 struct collection_list
831 unsigned char regs_mask[32]; /* room for up to 256 regs */
834 struct memrange *list;
835 long aexpr_listsize; /* size of array pointed to by expr_list elt */
837 struct agent_expr **aexpr_list;
840 tracepoint_list, stepping_list;
842 /* MEMRANGE functions: */
844 static int memrange_cmp (const void *, const void *);
846 /* compare memranges for qsort */
848 memrange_cmp (const void *va, const void *vb)
850 const struct memrange *a = va, *b = vb;
852 if (a->type < b->type)
854 if (a->type > b->type)
856 if (a->type == memrange_absolute)
858 if ((bfd_vma) a->start < (bfd_vma) b->start)
860 if ((bfd_vma) a->start > (bfd_vma) b->start)
865 if (a->start < b->start)
867 if (a->start > b->start)
873 /* Sort the memrange list using qsort, and merge adjacent memranges. */
875 memrange_sortmerge (struct collection_list *memranges)
879 qsort (memranges->list, memranges->next_memrange,
880 sizeof (struct memrange), memrange_cmp);
881 if (memranges->next_memrange > 0)
883 for (a = 0, b = 1; b < memranges->next_memrange; b++)
885 if (memranges->list[a].type == memranges->list[b].type &&
886 memranges->list[b].start - memranges->list[a].end <=
889 /* memrange b starts before memrange a ends; merge them. */
890 if (memranges->list[b].end > memranges->list[a].end)
891 memranges->list[a].end = memranges->list[b].end;
892 continue; /* next b, same a */
896 memcpy (&memranges->list[a], &memranges->list[b],
897 sizeof (struct memrange));
899 memranges->next_memrange = a + 1;
903 /* Add a register to a collection list. */
905 add_register (struct collection_list *collection, unsigned int regno)
908 printf_filtered ("collect register %d\n", regno);
909 if (regno >= (8 * sizeof (collection->regs_mask)))
910 error (_("Internal: register number %d too large for tracepoint"),
912 collection->regs_mask[regno / 8] |= 1 << (regno % 8);
915 /* Add a memrange to a collection list */
917 add_memrange (struct collection_list *memranges,
918 int type, bfd_signed_vma base,
923 printf_filtered ("(%d,", type);
925 printf_filtered (",%ld)\n", len);
928 /* type: memrange_absolute == memory, other n == basereg */
929 memranges->list[memranges->next_memrange].type = type;
930 /* base: addr if memory, offset if reg relative. */
931 memranges->list[memranges->next_memrange].start = base;
932 /* len: we actually save end (base + len) for convenience */
933 memranges->list[memranges->next_memrange].end = base + len;
934 memranges->next_memrange++;
935 if (memranges->next_memrange >= memranges->listsize)
937 memranges->listsize *= 2;
938 memranges->list = xrealloc (memranges->list,
939 memranges->listsize);
942 if (type != memrange_absolute) /* Better collect the base register! */
943 add_register (memranges, type);
946 /* Add a symbol to a collection list. */
948 collect_symbol (struct collection_list *collect,
950 struct gdbarch *gdbarch,
951 long frame_regno, long frame_offset,
956 bfd_signed_vma offset;
958 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
959 switch (SYMBOL_CLASS (sym))
962 printf_filtered ("%s: don't know symbol class %d\n",
963 SYMBOL_PRINT_NAME (sym),
967 printf_filtered ("constant %s (value %ld) will not be collected.\n",
968 SYMBOL_PRINT_NAME (sym), SYMBOL_VALUE (sym));
971 offset = SYMBOL_VALUE_ADDRESS (sym);
976 sprintf_vma (tmp, offset);
977 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
978 SYMBOL_PRINT_NAME (sym), len,
981 add_memrange (collect, memrange_absolute, offset, len);
984 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
986 printf_filtered ("LOC_REG[parm] %s: ",
987 SYMBOL_PRINT_NAME (sym));
988 add_register (collect, reg);
989 /* Check for doubles stored in two registers. */
990 /* FIXME: how about larger types stored in 3 or more regs? */
991 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
992 len > register_size (gdbarch, reg))
993 add_register (collect, reg + 1);
996 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
997 printf_filtered (" (will not collect %s)\n",
998 SYMBOL_PRINT_NAME (sym));
1002 offset = frame_offset + SYMBOL_VALUE (sym);
1005 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
1006 SYMBOL_PRINT_NAME (sym), len);
1007 printf_vma (offset);
1008 printf_filtered (" from frame ptr reg %d\n", reg);
1010 add_memrange (collect, reg, offset, len);
1012 case LOC_REGPARM_ADDR:
1013 reg = SYMBOL_VALUE (sym);
1017 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset ",
1018 SYMBOL_PRINT_NAME (sym), len);
1019 printf_vma (offset);
1020 printf_filtered (" from reg %d\n", reg);
1022 add_memrange (collect, reg, offset, len);
1026 offset = frame_offset + SYMBOL_VALUE (sym);
1029 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
1030 SYMBOL_PRINT_NAME (sym), len);
1031 printf_vma (offset);
1032 printf_filtered (" from frame ptr reg %d\n", reg);
1034 add_memrange (collect, reg, offset, len);
1036 case LOC_UNRESOLVED:
1037 printf_filtered ("Don't know LOC_UNRESOLVED %s\n",
1038 SYMBOL_PRINT_NAME (sym));
1040 case LOC_OPTIMIZED_OUT:
1041 printf_filtered ("%s has been optimized out of existence.\n",
1042 SYMBOL_PRINT_NAME (sym));
1047 struct agent_expr *aexpr;
1048 struct cleanup *old_chain1 = NULL;
1049 struct agent_reqs areqs;
1051 aexpr = gen_trace_for_var (scope, sym);
1053 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1055 ax_reqs (aexpr, &areqs);
1056 if (areqs.flaw != agent_flaw_none)
1057 error (_("malformed expression"));
1059 if (areqs.min_height < 0)
1060 error (_("gdb: Internal error: expression has min height < 0"));
1061 if (areqs.max_height > 20)
1062 error (_("expression too complicated, try simplifying"));
1064 discard_cleanups (old_chain1);
1065 add_aexpr (collect, aexpr);
1067 /* take care of the registers */
1068 if (areqs.reg_mask_len > 0)
1072 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
1074 QUIT; /* allow user to bail out with ^C */
1075 if (areqs.reg_mask[ndx1] != 0)
1077 /* assume chars have 8 bits */
1078 for (ndx2 = 0; ndx2 < 8; ndx2++)
1079 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1080 /* it's used -- record it */
1081 add_register (collect,
1091 /* Add all locals (or args) symbols to collection list */
1093 add_local_symbols (struct collection_list *collect,
1094 struct gdbarch *gdbarch, CORE_ADDR pc,
1095 long frame_regno, long frame_offset, int type)
1098 struct block *block;
1099 struct dict_iterator iter;
1102 block = block_for_pc (pc);
1105 QUIT; /* allow user to bail out with ^C */
1106 ALL_BLOCK_SYMBOLS (block, iter, sym)
1108 if (SYMBOL_IS_ARGUMENT (sym)
1109 ? type == 'A' /* collecting Arguments */
1110 : type == 'L') /* collecting Locals */
1113 collect_symbol (collect, sym, gdbarch,
1114 frame_regno, frame_offset, pc);
1117 if (BLOCK_FUNCTION (block))
1120 block = BLOCK_SUPERBLOCK (block);
1123 warning (_("No %s found in scope."),
1124 type == 'L' ? "locals" : "args");
1127 /* worker function */
1129 clear_collection_list (struct collection_list *list)
1133 list->next_memrange = 0;
1134 for (ndx = 0; ndx < list->next_aexpr_elt; ndx++)
1136 free_agent_expr (list->aexpr_list[ndx]);
1137 list->aexpr_list[ndx] = NULL;
1139 list->next_aexpr_elt = 0;
1140 memset (list->regs_mask, 0, sizeof (list->regs_mask));
1143 /* reduce a collection list to string form (for gdb protocol) */
1145 stringify_collection_list (struct collection_list *list, char *string)
1147 char temp_buf[2048];
1151 char *(*str_list)[];
1155 count = 1 + list->next_memrange + list->next_aexpr_elt + 1;
1156 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
1158 for (i = sizeof (list->regs_mask) - 1; i > 0; i--)
1159 if (list->regs_mask[i] != 0) /* skip leading zeroes in regs_mask */
1161 if (list->regs_mask[i] != 0) /* prepare to send regs_mask to the stub */
1164 printf_filtered ("\nCollecting registers (mask): 0x");
1169 QUIT; /* allow user to bail out with ^C */
1171 printf_filtered ("%02X", list->regs_mask[i]);
1172 sprintf (end, "%02X", list->regs_mask[i]);
1175 (*str_list)[ndx] = xstrdup (temp_buf);
1179 printf_filtered ("\n");
1180 if (list->next_memrange > 0 && info_verbose)
1181 printf_filtered ("Collecting memranges: \n");
1182 for (i = 0, count = 0, end = temp_buf; i < list->next_memrange; i++)
1184 QUIT; /* allow user to bail out with ^C */
1185 sprintf_vma (tmp2, list->list[i].start);
1188 printf_filtered ("(%d, %s, %ld)\n",
1191 (long) (list->list[i].end - list->list[i].start));
1193 if (count + 27 > MAX_AGENT_EXPR_LEN)
1195 (*str_list)[ndx] = savestring (temp_buf, count);
1202 bfd_signed_vma length = list->list[i].end - list->list[i].start;
1204 /* The "%X" conversion specifier expects an unsigned argument,
1205 so passing -1 (memrange_absolute) to it directly gives you
1206 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1208 if (list->list[i].type == memrange_absolute)
1209 sprintf (end, "M-1,%s,%lX", tmp2, (long) length);
1211 sprintf (end, "M%X,%s,%lX", list->list[i].type, tmp2, (long) length);
1214 count += strlen (end);
1215 end = temp_buf + count;
1218 for (i = 0; i < list->next_aexpr_elt; i++)
1220 QUIT; /* allow user to bail out with ^C */
1221 if ((count + 10 + 2 * list->aexpr_list[i]->len) > MAX_AGENT_EXPR_LEN)
1223 (*str_list)[ndx] = savestring (temp_buf, count);
1228 sprintf (end, "X%08X,", list->aexpr_list[i]->len);
1229 end += 10; /* 'X' + 8 hex digits + ',' */
1232 end = mem2hex (list->aexpr_list[i]->buf,
1233 end, list->aexpr_list[i]->len);
1234 count += 2 * list->aexpr_list[i]->len;
1239 (*str_list)[ndx] = savestring (temp_buf, count);
1244 (*str_list)[ndx] = NULL;
1256 free_actions_list_cleanup_wrapper (void *al)
1258 free_actions_list (al);
1262 free_actions_list (char **actions_list)
1266 if (actions_list == 0)
1269 for (ndx = 0; actions_list[ndx]; ndx++)
1270 xfree (actions_list[ndx]);
1272 xfree (actions_list);
1275 /* Render all actions into gdb protocol. */
1277 encode_actions (struct breakpoint *t, char ***tdp_actions,
1278 char ***stepping_actions)
1280 static char tdp_buff[2048], step_buff[2048];
1282 struct expression *exp = NULL;
1283 struct action_line *action;
1285 struct value *tempval;
1286 struct collection_list *collect;
1287 struct cmd_list_element *cmd;
1288 struct agent_expr *aexpr;
1290 LONGEST frame_offset;
1291 char *default_collect_line = NULL;
1292 struct action_line *default_collect_action = NULL;
1294 clear_collection_list (&tracepoint_list);
1295 clear_collection_list (&stepping_list);
1296 collect = &tracepoint_list;
1298 *tdp_actions = NULL;
1299 *stepping_actions = NULL;
1301 gdbarch_virtual_frame_pointer (t->gdbarch,
1302 t->loc->address, &frame_reg, &frame_offset);
1304 action = t->actions;
1306 /* If there are default expressions to collect, make up a collect
1307 action and prepend to the action list to encode. Note that since
1308 validation is per-tracepoint (local var "xyz" might be valid for
1309 one tracepoint and not another, etc), we make up the action on
1310 the fly, and don't cache it. */
1311 if (*default_collect)
1314 enum actionline_type linetype;
1316 default_collect_line = xmalloc (12 + strlen (default_collect));
1317 sprintf (default_collect_line, "collect %s", default_collect);
1318 line = default_collect_line;
1319 linetype = validate_actionline (&line, t);
1320 if (linetype != BADLINE)
1322 default_collect_action = xmalloc (sizeof (struct action_line));
1323 default_collect_action->next = t->actions;
1324 default_collect_action->action = line;
1325 action = default_collect_action;
1329 for (; action; action = action->next)
1331 QUIT; /* allow user to bail out with ^C */
1332 action_exp = action->action;
1333 while (isspace ((int) *action_exp))
1336 if (*action_exp == '#') /* comment line */
1339 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1341 error (_("Bad action list item: %s"), action_exp);
1343 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
1346 { /* repeat over a comma-separated list */
1347 QUIT; /* allow user to bail out with ^C */
1348 while (isspace ((int) *action_exp))
1351 if (0 == strncasecmp ("$reg", action_exp, 4))
1353 for (i = 0; i < gdbarch_num_regs (t->gdbarch); i++)
1354 add_register (collect, i);
1355 action_exp = strchr (action_exp, ','); /* more? */
1357 else if (0 == strncasecmp ("$arg", action_exp, 4))
1359 add_local_symbols (collect,
1365 action_exp = strchr (action_exp, ','); /* more? */
1367 else if (0 == strncasecmp ("$loc", action_exp, 4))
1369 add_local_symbols (collect,
1375 action_exp = strchr (action_exp, ','); /* more? */
1379 unsigned long addr, len;
1380 struct cleanup *old_chain = NULL;
1381 struct cleanup *old_chain1 = NULL;
1382 struct agent_reqs areqs;
1384 exp = parse_exp_1 (&action_exp,
1385 block_for_pc (t->loc->address), 1);
1386 old_chain = make_cleanup (free_current_contents, &exp);
1388 switch (exp->elts[0].opcode)
1392 const char *name = &exp->elts[2].string;
1394 i = user_reg_map_name_to_regnum (t->gdbarch,
1395 name, strlen (name));
1397 internal_error (__FILE__, __LINE__,
1398 _("Register $%s not available"),
1401 printf_filtered ("OP_REGISTER: ");
1402 add_register (collect, i);
1407 /* safe because we know it's a simple expression */
1408 tempval = evaluate_expression (exp);
1409 addr = value_address (tempval);
1410 len = TYPE_LENGTH (check_typedef (exp->elts[1].type));
1411 add_memrange (collect, memrange_absolute, addr, len);
1415 collect_symbol (collect,
1416 exp->elts[2].symbol,
1423 default: /* full-fledged expression */
1424 aexpr = gen_trace_for_expr (t->loc->address, exp);
1426 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1428 ax_reqs (aexpr, &areqs);
1429 if (areqs.flaw != agent_flaw_none)
1430 error (_("malformed expression"));
1432 if (areqs.min_height < 0)
1433 error (_("gdb: Internal error: expression has min height < 0"));
1434 if (areqs.max_height > 20)
1435 error (_("expression too complicated, try simplifying"));
1437 discard_cleanups (old_chain1);
1438 add_aexpr (collect, aexpr);
1440 /* take care of the registers */
1441 if (areqs.reg_mask_len > 0)
1446 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
1448 QUIT; /* allow user to bail out with ^C */
1449 if (areqs.reg_mask[ndx1] != 0)
1451 /* assume chars have 8 bits */
1452 for (ndx2 = 0; ndx2 < 8; ndx2++)
1453 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1454 /* it's used -- record it */
1455 add_register (collect,
1462 do_cleanups (old_chain);
1465 while (action_exp && *action_exp++ == ',');
1467 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
1469 collect = &stepping_list;
1471 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
1473 if (collect == &stepping_list) /* end stepping actions */
1474 collect = &tracepoint_list;
1476 break; /* end tracepoint actions */
1479 memrange_sortmerge (&tracepoint_list);
1480 memrange_sortmerge (&stepping_list);
1482 *tdp_actions = stringify_collection_list (&tracepoint_list,
1484 *stepping_actions = stringify_collection_list (&stepping_list,
1487 xfree (default_collect_line);
1488 xfree (default_collect_action);
1492 add_aexpr (struct collection_list *collect, struct agent_expr *aexpr)
1494 if (collect->next_aexpr_elt >= collect->aexpr_listsize)
1496 collect->aexpr_list =
1497 xrealloc (collect->aexpr_list,
1498 2 * collect->aexpr_listsize * sizeof (struct agent_expr *));
1499 collect->aexpr_listsize *= 2;
1501 collect->aexpr_list[collect->next_aexpr_elt] = aexpr;
1502 collect->next_aexpr_elt++;
1505 /* Set "transparent" memory ranges
1507 Allow trace mechanism to treat text-like sections
1508 (and perhaps all read-only sections) transparently,
1509 i.e. don't reject memory requests from these address ranges
1510 just because they haven't been collected. */
1513 remote_set_transparent_ranges (void)
1521 return; /* No information to give. */
1523 strcpy (target_buf, "QTro");
1524 for (s = exec_bfd->sections; s; s = s->next)
1526 char tmp1[40], tmp2[40];
1528 if ((s->flags & SEC_LOAD) == 0 ||
1529 /* (s->flags & SEC_CODE) == 0 || */
1530 (s->flags & SEC_READONLY) == 0)
1535 size = bfd_get_section_size (s);
1536 sprintf_vma (tmp1, lma);
1537 sprintf_vma (tmp2, lma + size);
1538 sprintf (target_buf + strlen (target_buf),
1539 ":%s,%s", tmp1, tmp2);
1543 putpkt (target_buf);
1544 getpkt (&target_buf, &target_buf_size, 0);
1550 Tell target to clear any previous trace experiment.
1551 Walk the list of tracepoints, and send them (and their actions)
1552 to the target. If no errors,
1553 Tell target to start a new trace experiment. */
1555 void download_tracepoint (struct breakpoint *t);
1558 trace_start_command (char *args, int from_tty)
1561 VEC(breakpoint_p) *tp_vec = NULL;
1563 struct breakpoint *t;
1564 struct trace_state_variable *tsv;
1566 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1568 if (target_is_remote ())
1571 remote_get_noisy_reply (&target_buf, &target_buf_size);
1572 if (strcmp (target_buf, "OK"))
1573 error (_("Target does not support this command."));
1575 tp_vec = all_tracepoints ();
1576 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1578 download_tracepoint (t);
1580 VEC_free (breakpoint_p, tp_vec);
1582 /* Init any trace state variables that start with nonzero values. */
1584 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
1586 if (tsv->initial_value != 0)
1588 sprintf (buf, "QTDV:%x:%s",
1589 tsv->number, phex ((ULONGEST) tsv->initial_value, 8));
1591 remote_get_noisy_reply (&target_buf, &target_buf_size);
1595 /* Tell target to treat text-like sections as transparent. */
1596 remote_set_transparent_ranges ();
1597 /* Now insert traps and begin collecting data. */
1599 remote_get_noisy_reply (&target_buf, &target_buf_size);
1600 if (strcmp (target_buf, "OK"))
1601 error (_("Bogus reply from target: %s"), target_buf);
1602 set_traceframe_num (-1); /* All old traceframes invalidated. */
1603 set_tracepoint_num (-1);
1604 set_traceframe_context (NULL);
1605 trace_running_p = 1;
1606 if (deprecated_trace_start_stop_hook)
1607 deprecated_trace_start_stop_hook (1, from_tty);
1611 error (_("Trace can only be run on remote targets."));
1614 /* Send the definition of a single tracepoint to the target. */
1617 download_tracepoint (struct breakpoint *t)
1622 char **stepping_actions;
1624 struct cleanup *old_chain = NULL;
1625 struct agent_expr *aexpr;
1626 struct cleanup *aexpr_chain = NULL;
1628 sprintf_vma (tmp, (t->loc ? t->loc->address : 0));
1629 sprintf (buf, "QTDP:%x:%s:%c:%lx:%x", t->number,
1631 (t->enable_state == bp_enabled ? 'E' : 'D'),
1632 t->step_count, t->pass_count);
1633 /* If the tracepoint has a conditional, make it into an agent
1634 expression and append to the definition. */
1637 /* Only test support at download time, we may not know target
1638 capabilities at definition time. */
1639 if (remote_supports_cond_tracepoints ())
1641 aexpr = gen_eval_for_expr (t->loc->address, t->loc->cond);
1642 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
1643 sprintf (buf + strlen (buf), ":X%x,", aexpr->len);
1644 mem2hex (aexpr->buf, buf + strlen (buf), aexpr->len);
1645 do_cleanups (aexpr_chain);
1648 warning (_("Target does not support conditional tracepoints, ignoring tp %d cond"), t->number);
1651 if (t->actions || *default_collect)
1654 remote_get_noisy_reply (&target_buf, &target_buf_size);
1655 if (strcmp (target_buf, "OK"))
1656 error (_("Target does not support tracepoints."));
1658 if (!t->actions && !*default_collect)
1661 encode_actions (t, &tdp_actions, &stepping_actions);
1662 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
1664 (void) make_cleanup (free_actions_list_cleanup_wrapper, stepping_actions);
1666 /* do_single_steps (t); */
1669 for (ndx = 0; tdp_actions[ndx]; ndx++)
1671 QUIT; /* allow user to bail out with ^C */
1672 sprintf (buf, "QTDP:-%x:%s:%s%c",
1673 t->number, tmp, /* address */
1675 ((tdp_actions[ndx + 1] || stepping_actions)
1678 remote_get_noisy_reply (&target_buf,
1680 if (strcmp (target_buf, "OK"))
1681 error (_("Error on target while setting tracepoints."));
1684 if (stepping_actions)
1686 for (ndx = 0; stepping_actions[ndx]; ndx++)
1688 QUIT; /* allow user to bail out with ^C */
1689 sprintf (buf, "QTDP:-%x:%s:%s%s%s",
1690 t->number, tmp, /* address */
1691 ((ndx == 0) ? "S" : ""),
1692 stepping_actions[ndx],
1693 (stepping_actions[ndx + 1] ? "-" : ""));
1695 remote_get_noisy_reply (&target_buf,
1697 if (strcmp (target_buf, "OK"))
1698 error (_("Error on target while setting tracepoints."));
1701 do_cleanups (old_chain);
1706 trace_stop_command (char *args, int from_tty)
1708 if (target_is_remote ())
1711 remote_get_noisy_reply (&target_buf, &target_buf_size);
1712 if (strcmp (target_buf, "OK"))
1713 error (_("Bogus reply from target: %s"), target_buf);
1714 trace_running_p = 0;
1715 if (deprecated_trace_start_stop_hook)
1716 deprecated_trace_start_stop_hook (0, from_tty);
1719 error (_("Trace can only be run on remote targets."));
1722 unsigned long trace_running_p;
1724 /* tstatus command */
1726 trace_status_command (char *args, int from_tty)
1728 if (target_is_remote ())
1730 putpkt ("qTStatus");
1731 remote_get_noisy_reply (&target_buf, &target_buf_size);
1733 if (target_buf[0] != 'T' ||
1734 (target_buf[1] != '0' && target_buf[1] != '1'))
1735 error (_("Bogus reply from target: %s"), target_buf);
1737 /* exported for use by the GUI */
1738 trace_running_p = (target_buf[1] == '1');
1741 error (_("Trace can only be run on remote targets."));
1744 /* Worker function for the various flavors of the tfind command. */
1746 finish_tfind_command (char **msg,
1750 int target_frameno = -1, target_tracept = -1;
1751 struct frame_id old_frame_id;
1754 old_frame_id = get_frame_id (get_current_frame ());
1757 reply = remote_get_noisy_reply (msg, sizeof_msg);
1759 while (reply && *reply)
1763 if ((target_frameno = (int) strtol (++reply, &reply, 16)) == -1)
1765 /* A request for a non-existant trace frame has failed.
1766 Our response will be different, depending on FROM_TTY:
1768 If FROM_TTY is true, meaning that this command was
1769 typed interactively by the user, then give an error
1770 and DO NOT change the state of traceframe_number etc.
1772 However if FROM_TTY is false, meaning that we're either
1773 in a script, a loop, or a user-defined command, then
1774 DON'T give an error, but DO change the state of
1775 traceframe_number etc. to invalid.
1777 The rationalle is that if you typed the command, you
1778 might just have committed a typo or something, and you'd
1779 like to NOT lose your current debugging state. However
1780 if you're in a user-defined command or especially in a
1781 loop, then you need a way to detect that the command
1782 failed WITHOUT aborting. This allows you to write
1783 scripts that search thru the trace buffer until the end,
1784 and then continue on to do something else. */
1787 error (_("Target failed to find requested trace frame."));
1791 printf_filtered ("End of trace buffer.\n");
1792 /* The following will not recurse, since it's
1794 trace_find_command ("-1", from_tty);
1795 reply = NULL; /* Break out of loop
1796 (avoid recursive nonsense). */
1801 if ((target_tracept = (int) strtol (++reply, &reply, 16)) == -1)
1802 error (_("Target failed to find requested trace frame."));
1804 case 'O': /* "OK"? */
1805 if (reply[1] == 'K' && reply[2] == '\0')
1808 error (_("Bogus reply from target: %s"), reply);
1811 error (_("Bogus reply from target: %s"), reply);
1814 reinit_frame_cache ();
1815 registers_changed ();
1816 set_traceframe_num (target_frameno);
1817 set_tracepoint_num (target_tracept);
1818 if (target_frameno == -1)
1819 set_traceframe_context (NULL);
1821 set_traceframe_context (get_current_frame ());
1825 enum print_what print_what;
1827 /* NOTE: in immitation of the step command, try to determine
1828 whether we have made a transition from one function to
1829 another. If so, we'll print the "stack frame" (ie. the new
1830 function and it's arguments) -- otherwise we'll just show the
1833 if (frame_id_eq (old_frame_id,
1834 get_frame_id (get_current_frame ())))
1835 print_what = SRC_LINE;
1837 print_what = SRC_AND_LOC;
1839 print_stack_frame (get_selected_frame (NULL), 1, print_what);
1844 /* trace_find_command takes a trace frame number n,
1845 sends "QTFrame:<n>" to the target,
1846 and accepts a reply that may contain several optional pieces
1847 of information: a frame number, a tracepoint number, and an
1848 indication of whether this is a trap frame or a stepping frame.
1850 The minimal response is just "OK" (which indicates that the
1851 target does not give us a frame number or a tracepoint number).
1852 Instead of that, the target may send us a string containing
1854 F<hexnum> (gives the selected frame number)
1855 T<hexnum> (gives the selected tracepoint number)
1860 trace_find_command (char *args, int from_tty)
1861 { /* this should only be called with a numeric argument */
1864 if (target_is_remote ())
1866 if (deprecated_trace_find_hook)
1867 deprecated_trace_find_hook (args, from_tty);
1869 if (args == 0 || *args == 0)
1870 { /* TFIND with no args means find NEXT trace frame. */
1871 if (traceframe_number == -1)
1872 frameno = 0; /* "next" is first one */
1874 frameno = traceframe_number + 1;
1876 else if (0 == strcmp (args, "-"))
1878 if (traceframe_number == -1)
1879 error (_("not debugging trace buffer"));
1880 else if (from_tty && traceframe_number == 0)
1881 error (_("already at start of trace buffer"));
1883 frameno = traceframe_number - 1;
1886 frameno = parse_and_eval_long (args);
1889 error (_("invalid input (%d is less than zero)"), frameno);
1891 sprintf (target_buf, "QTFrame:%x", frameno);
1892 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
1895 error (_("Trace can only be run on remote targets."));
1900 trace_find_end_command (char *args, int from_tty)
1902 trace_find_command ("-1", from_tty);
1907 trace_find_none_command (char *args, int from_tty)
1909 trace_find_command ("-1", from_tty);
1914 trace_find_start_command (char *args, int from_tty)
1916 trace_find_command ("0", from_tty);
1919 /* tfind pc command */
1921 trace_find_pc_command (char *args, int from_tty)
1926 if (target_is_remote ())
1928 if (args == 0 || *args == 0)
1929 pc = regcache_read_pc (get_current_regcache ());
1931 pc = parse_and_eval_address (args);
1933 sprintf_vma (tmp, pc);
1934 sprintf (target_buf, "QTFrame:pc:%s", tmp);
1935 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
1938 error (_("Trace can only be run on remote targets."));
1941 /* tfind tracepoint command */
1943 trace_find_tracepoint_command (char *args, int from_tty)
1947 if (target_is_remote ())
1949 if (args == 0 || *args == 0)
1951 if (tracepoint_number == -1)
1952 error (_("No current tracepoint -- please supply an argument."));
1954 tdp = tracepoint_number; /* default is current TDP */
1957 tdp = parse_and_eval_long (args);
1959 sprintf (target_buf, "QTFrame:tdp:%x", tdp);
1960 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
1963 error (_("Trace can only be run on remote targets."));
1966 /* TFIND LINE command:
1968 This command will take a sourceline for argument, just like BREAK
1969 or TRACE (ie. anything that "decode_line_1" can handle).
1971 With no argument, this command will find the next trace frame
1972 corresponding to a source line OTHER THAN THE CURRENT ONE. */
1975 trace_find_line_command (char *args, int from_tty)
1977 static CORE_ADDR start_pc, end_pc;
1978 struct symtabs_and_lines sals;
1979 struct symtab_and_line sal;
1980 struct cleanup *old_chain;
1981 char startpc_str[40], endpc_str[40];
1983 if (target_is_remote ())
1985 if (args == 0 || *args == 0)
1987 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
1989 sals.sals = (struct symtab_and_line *)
1990 xmalloc (sizeof (struct symtab_and_line));
1995 sals = decode_line_spec (args, 1);
1999 old_chain = make_cleanup (xfree, sals.sals);
2000 if (sal.symtab == 0)
2002 struct gdbarch *gdbarch = get_current_arch ();
2004 printf_filtered ("TFIND: No line number information available");
2007 /* This is useful for "info line *0x7f34". If we can't
2008 tell the user about a source line, at least let them
2009 have the symbolic address. */
2010 printf_filtered (" for address ");
2012 print_address (gdbarch, sal.pc, gdb_stdout);
2013 printf_filtered (";\n -- will attempt to find by PC. \n");
2017 printf_filtered (".\n");
2018 return; /* No line, no PC; what can we do? */
2021 else if (sal.line > 0
2022 && find_line_pc_range (sal, &start_pc, &end_pc))
2024 struct gdbarch *gdbarch = get_objfile_arch (sal.symtab->objfile);
2026 if (start_pc == end_pc)
2028 printf_filtered ("Line %d of \"%s\"",
2029 sal.line, sal.symtab->filename);
2031 printf_filtered (" is at address ");
2032 print_address (gdbarch, start_pc, gdb_stdout);
2034 printf_filtered (" but contains no code.\n");
2035 sal = find_pc_line (start_pc, 0);
2037 find_line_pc_range (sal, &start_pc, &end_pc) &&
2039 printf_filtered ("Attempting to find line %d instead.\n",
2042 error (_("Cannot find a good line."));
2046 /* Is there any case in which we get here, and have an address
2047 which the user would want to see? If we have debugging
2048 symbols and no line numbers? */
2049 error (_("Line number %d is out of range for \"%s\"."),
2050 sal.line, sal.symtab->filename);
2052 sprintf_vma (startpc_str, start_pc);
2053 sprintf_vma (endpc_str, end_pc - 1);
2054 /* Find within range of stated line. */
2056 sprintf (target_buf, "QTFrame:range:%s:%s",
2057 startpc_str, endpc_str);
2058 /* Find OUTSIDE OF range of CURRENT line. */
2060 sprintf (target_buf, "QTFrame:outside:%s:%s",
2061 startpc_str, endpc_str);
2062 finish_tfind_command (&target_buf, &target_buf_size,
2064 do_cleanups (old_chain);
2067 error (_("Trace can only be run on remote targets."));
2070 /* tfind range command */
2072 trace_find_range_command (char *args, int from_tty)
2074 static CORE_ADDR start, stop;
2075 char start_str[40], stop_str[40];
2078 if (target_is_remote ())
2080 if (args == 0 || *args == 0)
2081 { /* XXX FIXME: what should default behavior be? */
2082 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2086 if (0 != (tmp = strchr (args, ',')))
2088 *tmp++ = '\0'; /* terminate start address */
2089 while (isspace ((int) *tmp))
2091 start = parse_and_eval_address (args);
2092 stop = parse_and_eval_address (tmp);
2095 { /* no explicit end address? */
2096 start = parse_and_eval_address (args);
2097 stop = start + 1; /* ??? */
2100 sprintf_vma (start_str, start);
2101 sprintf_vma (stop_str, stop);
2102 sprintf (target_buf, "QTFrame:range:%s:%s", start_str, stop_str);
2103 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
2106 error (_("Trace can only be run on remote targets."));
2109 /* tfind outside command */
2111 trace_find_outside_command (char *args, int from_tty)
2113 CORE_ADDR start, stop;
2114 char start_str[40], stop_str[40];
2117 if (target_is_remote ())
2119 if (args == 0 || *args == 0)
2120 { /* XXX FIXME: what should default behavior be? */
2121 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2125 if (0 != (tmp = strchr (args, ',')))
2127 *tmp++ = '\0'; /* terminate start address */
2128 while (isspace ((int) *tmp))
2130 start = parse_and_eval_address (args);
2131 stop = parse_and_eval_address (tmp);
2134 { /* no explicit end address? */
2135 start = parse_and_eval_address (args);
2136 stop = start + 1; /* ??? */
2139 sprintf_vma (start_str, start);
2140 sprintf_vma (stop_str, stop);
2141 sprintf (target_buf, "QTFrame:outside:%s:%s", start_str, stop_str);
2142 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
2145 error (_("Trace can only be run on remote targets."));
2148 /* info scope command: list the locals for a scope. */
2150 scope_info (char *args, int from_tty)
2152 struct symtabs_and_lines sals;
2154 struct minimal_symbol *msym;
2155 struct block *block;
2156 char **canonical, *symname, *save_args = args;
2157 struct dict_iterator iter;
2159 struct gdbarch *gdbarch;
2162 if (args == 0 || *args == 0)
2163 error (_("requires an argument (function, line or *addr) to define a scope"));
2165 sals = decode_line_1 (&args, 1, NULL, 0, &canonical, NULL);
2166 if (sals.nelts == 0)
2167 return; /* presumably decode_line_1 has already warned */
2169 /* Resolve line numbers to PC */
2170 resolve_sal_pc (&sals.sals[0]);
2171 block = block_for_pc (sals.sals[0].pc);
2175 QUIT; /* allow user to bail out with ^C */
2176 ALL_BLOCK_SYMBOLS (block, iter, sym)
2178 QUIT; /* allow user to bail out with ^C */
2180 printf_filtered ("Scope for %s:\n", save_args);
2183 symname = SYMBOL_PRINT_NAME (sym);
2184 if (symname == NULL || *symname == '\0')
2185 continue; /* probably botched, certainly useless */
2187 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
2189 printf_filtered ("Symbol %s is ", symname);
2190 switch (SYMBOL_CLASS (sym))
2193 case LOC_UNDEF: /* messed up symbol? */
2194 printf_filtered ("a bogus symbol, class %d.\n",
2195 SYMBOL_CLASS (sym));
2196 count--; /* don't count this one */
2199 printf_filtered ("a constant with value %ld (0x%lx)",
2200 SYMBOL_VALUE (sym), SYMBOL_VALUE (sym));
2202 case LOC_CONST_BYTES:
2203 printf_filtered ("constant bytes: ");
2204 if (SYMBOL_TYPE (sym))
2205 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2206 fprintf_filtered (gdb_stdout, " %02x",
2207 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2210 printf_filtered ("in static storage at address ");
2211 printf_filtered ("%s", paddress (gdbarch,
2212 SYMBOL_VALUE_ADDRESS (sym)));
2215 /* GDBARCH is the architecture associated with the objfile
2216 the symbol is defined in; the target architecture may be
2217 different, and may provide additional registers. However,
2218 we do not know the target architecture at this point.
2219 We assume the objfile architecture will contain all the
2220 standard registers that occur in debug info in that
2222 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
2224 if (SYMBOL_IS_ARGUMENT (sym))
2225 printf_filtered ("an argument in register $%s",
2226 gdbarch_register_name (gdbarch, regno));
2228 printf_filtered ("a local variable in register $%s",
2229 gdbarch_register_name (gdbarch, regno));
2232 printf_filtered ("an argument at stack/frame offset %ld",
2233 SYMBOL_VALUE (sym));
2236 printf_filtered ("a local variable at frame offset %ld",
2237 SYMBOL_VALUE (sym));
2240 printf_filtered ("a reference argument at offset %ld",
2241 SYMBOL_VALUE (sym));
2243 case LOC_REGPARM_ADDR:
2244 /* Note comment at LOC_REGISTER. */
2245 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
2246 printf_filtered ("the address of an argument, in register $%s",
2247 gdbarch_register_name (gdbarch, regno));
2250 printf_filtered ("a typedef.\n");
2253 printf_filtered ("a label at address ");
2254 printf_filtered ("%s", paddress (gdbarch,
2255 SYMBOL_VALUE_ADDRESS (sym)));
2258 printf_filtered ("a function at address ");
2259 printf_filtered ("%s",
2260 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
2262 case LOC_UNRESOLVED:
2263 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
2266 printf_filtered ("Unresolved Static");
2269 printf_filtered ("static storage at address ");
2270 printf_filtered ("%s",
2271 paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msym)));
2274 case LOC_OPTIMIZED_OUT:
2275 printf_filtered ("optimized out.\n");
2278 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, gdb_stdout);
2281 if (SYMBOL_TYPE (sym))
2282 printf_filtered (", length %d.\n",
2283 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
2285 if (BLOCK_FUNCTION (block))
2288 block = BLOCK_SUPERBLOCK (block);
2291 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2295 /* worker function (cleanup) */
2297 replace_comma (void *data)
2305 trace_dump_command (char *args, int from_tty)
2307 struct regcache *regcache;
2308 struct gdbarch *gdbarch;
2309 struct breakpoint *t;
2310 struct action_line *action;
2311 char *action_exp, *next_comma;
2312 struct cleanup *old_cleanups;
2313 int stepping_actions = 0;
2314 int stepping_frame = 0;
2316 if (!target_is_remote ())
2318 error (_("Trace can only be run on remote targets."));
2322 if (tracepoint_number == -1)
2324 warning (_("No current trace frame."));
2328 t = get_tracepoint (tracepoint_number);
2331 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2334 old_cleanups = make_cleanup (null_cleanup, NULL);
2336 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2337 tracepoint_number, traceframe_number);
2339 /* The current frame is a trap frame if the frame PC is equal
2340 to the tracepoint PC. If not, then the current frame was
2341 collected during single-stepping. */
2343 regcache = get_current_regcache ();
2344 gdbarch = get_regcache_arch (regcache);
2346 stepping_frame = (t->loc->address != (regcache_read_pc (regcache)
2347 - gdbarch_decr_pc_after_break (gdbarch)));
2349 for (action = t->actions; action; action = action->next)
2351 struct cmd_list_element *cmd;
2353 QUIT; /* allow user to bail out with ^C */
2354 action_exp = action->action;
2355 while (isspace ((int) *action_exp))
2358 /* The collection actions to be done while stepping are
2359 bracketed by the commands "while-stepping" and "end". */
2361 if (*action_exp == '#') /* comment line */
2364 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2366 error (_("Bad action list item: %s"), action_exp);
2368 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
2369 stepping_actions = 1;
2370 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
2371 stepping_actions = 0;
2372 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
2374 /* Display the collected data.
2375 For the trap frame, display only what was collected at
2376 the trap. Likewise for stepping frames, display only
2377 what was collected while stepping. This means that the
2378 two boolean variables, STEPPING_FRAME and
2379 STEPPING_ACTIONS should be equal. */
2380 if (stepping_frame == stepping_actions)
2383 { /* repeat over a comma-separated list */
2384 QUIT; /* allow user to bail out with ^C */
2385 if (*action_exp == ',')
2387 while (isspace ((int) *action_exp))
2390 next_comma = strchr (action_exp, ',');
2392 if (0 == strncasecmp (action_exp, "$reg", 4))
2393 registers_info (NULL, from_tty);
2394 else if (0 == strncasecmp (action_exp, "$loc", 4))
2395 locals_info (NULL, from_tty);
2396 else if (0 == strncasecmp (action_exp, "$arg", 4))
2397 args_info (NULL, from_tty);
2402 make_cleanup (replace_comma, next_comma);
2405 printf_filtered ("%s = ", action_exp);
2406 output_command (action_exp, from_tty);
2407 printf_filtered ("\n");
2411 action_exp = next_comma;
2413 while (action_exp && *action_exp == ',');
2417 discard_cleanups (old_cleanups);
2420 /* Convert the memory pointed to by mem into hex, placing result in buf.
2421 * Return a pointer to the last char put in buf (null)
2422 * "stolen" from sparc-stub.c
2425 static const char hexchars[] = "0123456789abcdef";
2428 mem2hex (gdb_byte *mem, char *buf, int count)
2436 *buf++ = hexchars[ch >> 4];
2437 *buf++ = hexchars[ch & 0xf];
2446 get_traceframe_number (void)
2448 return traceframe_number;
2452 /* module initialization */
2454 _initialize_tracepoint (void)
2456 struct cmd_list_element *c;
2458 traceframe_number = -1;
2459 tracepoint_number = -1;
2461 if (tracepoint_list.list == NULL)
2463 tracepoint_list.listsize = 128;
2464 tracepoint_list.list = xmalloc
2465 (tracepoint_list.listsize * sizeof (struct memrange));
2467 if (tracepoint_list.aexpr_list == NULL)
2469 tracepoint_list.aexpr_listsize = 128;
2470 tracepoint_list.aexpr_list = xmalloc
2471 (tracepoint_list.aexpr_listsize * sizeof (struct agent_expr *));
2474 if (stepping_list.list == NULL)
2476 stepping_list.listsize = 128;
2477 stepping_list.list = xmalloc
2478 (stepping_list.listsize * sizeof (struct memrange));
2481 if (stepping_list.aexpr_list == NULL)
2483 stepping_list.aexpr_listsize = 128;
2484 stepping_list.aexpr_list = xmalloc
2485 (stepping_list.aexpr_listsize * sizeof (struct agent_expr *));
2488 add_info ("scope", scope_info,
2489 _("List the variables local to a scope"));
2491 add_cmd ("tracepoints", class_trace, NULL,
2492 _("Tracing of program execution without stopping the program."),
2495 add_com ("tdump", class_trace, trace_dump_command,
2496 _("Print everything collected at the current tracepoint."));
2498 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
2499 Define a trace state variable.\n\
2500 Argument is a $-prefixed name, optionally followed\n\
2501 by '=' and an expression that sets the initial value\n\
2502 at the start of tracing."));
2503 set_cmd_completer (c, expression_completer);
2505 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
2506 Delete one or more trace state variables.\n\
2507 Arguments are the names of the variables to delete.\n\
2508 If no arguments are supplied, delete all variables."), &deletelist);
2509 /* FIXME add a trace variable completer */
2511 add_info ("tvariables", tvariables_info, _("\
2512 Status of trace state variables and their values.\n\
2515 add_prefix_cmd ("tfind", class_trace, trace_find_command, _("\
2516 Select a trace frame;\n\
2517 No argument means forward by one frame; '-' means backward by one frame."),
2518 &tfindlist, "tfind ", 1, &cmdlist);
2520 add_cmd ("outside", class_trace, trace_find_outside_command, _("\
2521 Select a trace frame whose PC is outside the given range.\n\
2522 Usage: tfind outside addr1, addr2"),
2525 add_cmd ("range", class_trace, trace_find_range_command, _("\
2526 Select a trace frame whose PC is in the given range.\n\
2527 Usage: tfind range addr1,addr2"),
2530 add_cmd ("line", class_trace, trace_find_line_command, _("\
2531 Select a trace frame by source line.\n\
2532 Argument can be a line number (with optional source file), \n\
2533 a function name, or '*' followed by an address.\n\
2534 Default argument is 'the next source line that was traced'."),
2537 add_cmd ("tracepoint", class_trace, trace_find_tracepoint_command, _("\
2538 Select a trace frame by tracepoint number.\n\
2539 Default is the tracepoint for the current trace frame."),
2542 add_cmd ("pc", class_trace, trace_find_pc_command, _("\
2543 Select a trace frame by PC.\n\
2544 Default is the current PC, or the PC of the current trace frame."),
2547 add_cmd ("end", class_trace, trace_find_end_command, _("\
2548 Synonym for 'none'.\n\
2549 De-select any trace frame and resume 'live' debugging."),
2552 add_cmd ("none", class_trace, trace_find_none_command,
2553 _("De-select any trace frame and resume 'live' debugging."),
2556 add_cmd ("start", class_trace, trace_find_start_command,
2557 _("Select the first trace frame in the trace buffer."),
2560 add_com ("tstatus", class_trace, trace_status_command,
2561 _("Display the status of the current trace data collection."));
2563 add_com ("tstop", class_trace, trace_stop_command,
2564 _("Stop trace data collection."));
2566 add_com ("tstart", class_trace, trace_start_command,
2567 _("Start trace data collection."));
2569 add_com ("end", class_trace, end_actions_pseudocommand, _("\
2570 Ends a list of commands or actions.\n\
2571 Several GDB commands allow you to enter a list of commands or actions.\n\
2572 Entering \"end\" on a line by itself is the normal way to terminate\n\
2574 Note: the \"end\" command cannot be used at the gdb prompt."));
2576 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
2577 Specify single-stepping behavior at a tracepoint.\n\
2578 Argument is number of instructions to trace in single-step mode\n\
2579 following the tracepoint. This command is normally followed by\n\
2580 one or more \"collect\" commands, to specify what to collect\n\
2581 while single-stepping.\n\n\
2582 Note: this command can only be used in a tracepoint \"actions\" list."));
2584 add_com_alias ("ws", "while-stepping", class_alias, 0);
2585 add_com_alias ("stepping", "while-stepping", class_alias, 0);
2587 add_com ("collect", class_trace, collect_pseudocommand, _("\
2588 Specify one or more data items to be collected at a tracepoint.\n\
2589 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
2590 collect all data (variables, registers) referenced by that expression.\n\
2591 Also accepts the following special arguments:\n\
2592 $regs -- all registers.\n\
2593 $args -- all function arguments.\n\
2594 $locals -- all variables local to the block/function scope.\n\
2595 Note: this command can only be used in a tracepoint \"actions\" list."));
2597 add_com ("actions", class_trace, trace_actions_command, _("\
2598 Specify the actions to be taken at a tracepoint.\n\
2599 Tracepoint actions may include collecting of specified data, \n\
2600 single-stepping, or enabling/disabling other tracepoints, \n\
2601 depending on target's capabilities."));
2603 default_collect = xstrdup ("");
2604 add_setshow_string_cmd ("default-collect", class_trace,
2605 &default_collect, _("\
2606 Set the list of expressions to collect by default"), _("\
2607 Show the list of expressions to collect by default"), NULL,
2609 &setlist, &showlist);
2611 target_buf_size = 2048;
2612 target_buf = xmalloc (target_buf_size);