1 /* Tracing functionality for remote targets in custom GDB protocol
3 Copyright (C) 1997-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
25 #include "expression.h"
29 #include "target-dcache.h"
32 #include "breakpoint.h"
33 #include "tracepoint.h"
36 #include "completer.h"
38 #include "dictionary.h"
40 #include "user-regs.h"
44 #include "filenames.h"
45 #include "gdbthread.h"
52 #include "cli/cli-utils.h"
55 #include "filestuff.h"
57 #include "tracefile.h"
61 /* readline include files */
62 #include "readline/readline.h"
63 #include "readline/history.h"
65 /* readline defines this. */
70 /* Maximum length of an agent aexpression.
71 This accounts for the fact that packets are limited to 400 bytes
72 (which includes everything -- including the checksum), and assumes
73 the worst case of maximum length for each of the pieces of a
76 NOTE: expressions get mem2hex'ed otherwise this would be twice as
77 large. (400 - 31)/2 == 184 */
78 #define MAX_AGENT_EXPR_LEN 184
80 /* A hook used to notify the UI of tracepoint operations. */
82 void (*deprecated_trace_find_hook) (char *arg, int from_tty);
83 void (*deprecated_trace_start_stop_hook) (int start, int from_tty);
88 This module defines the following debugger commands:
89 trace : set a tracepoint on a function, line, or address.
90 info trace : list all debugger-defined tracepoints.
91 delete trace : delete one or more tracepoints.
92 enable trace : enable one or more tracepoints.
93 disable trace : disable one or more tracepoints.
94 actions : specify actions to be taken at a tracepoint.
95 passcount : specify a pass count for a tracepoint.
96 tstart : start a trace experiment.
97 tstop : stop a trace experiment.
98 tstatus : query the status of a trace experiment.
99 tfind : find a trace frame in the trace buffer.
100 tdump : print everything collected at the current tracepoint.
101 save-tracepoints : write tracepoint setup into a file.
103 This module defines the following user-visible debugger variables:
104 $trace_frame : sequence number of trace frame currently being debugged.
105 $trace_line : source line of trace frame currently being debugged.
106 $trace_file : source file of trace frame currently being debugged.
107 $tracepoint : tracepoint number of trace frame currently being debugged.
111 /* ======= Important global variables: ======= */
113 /* The list of all trace state variables. We don't retain pointers to
114 any of these for any reason - API is by name or number only - so it
115 works to have a vector of objects. */
117 typedef struct trace_state_variable tsv_s;
120 static VEC(tsv_s) *tvariables;
122 /* The next integer to assign to a variable. */
124 static int next_tsv_number = 1;
126 /* Number of last traceframe collected. */
127 static int traceframe_number;
129 /* Tracepoint for last traceframe collected. */
130 static int tracepoint_number;
132 /* The traceframe info of the current traceframe. NULL if we haven't
133 yet attempted to fetch it, or if the target does not support
134 fetching this object, or if we're not inspecting a traceframe
136 static struct traceframe_info *traceframe_info;
138 /* Tracing command lists. */
139 static struct cmd_list_element *tfindlist;
141 /* List of expressions to collect by default at each tracepoint hit. */
142 char *default_collect;
144 static int disconnected_tracing;
146 /* This variable controls whether we ask the target for a linear or
147 circular trace buffer. */
149 static int circular_trace_buffer;
151 /* This variable is the requested trace buffer size, or -1 to indicate
152 that we don't care and leave it up to the target to set a size. */
154 static int trace_buffer_size = -1;
156 /* Textual notes applying to the current and/or future trace runs. */
158 char *trace_user = NULL;
160 /* Textual notes applying to the current and/or future trace runs. */
162 char *trace_notes = NULL;
164 /* Textual notes applying to the stopping of a trace. */
166 char *trace_stop_notes = NULL;
168 /* ======= Important command functions: ======= */
169 static void actions_command (char *, int);
170 static void tstart_command (char *, int);
171 static void tstop_command (char *, int);
172 static void tstatus_command (char *, int);
173 static void tfind_pc_command (char *, int);
174 static void tfind_tracepoint_command (char *, int);
175 static void tfind_line_command (char *, int);
176 static void tfind_range_command (char *, int);
177 static void tfind_outside_command (char *, int);
178 static void tdump_command (char *, int);
180 /* support routines */
182 struct collection_list;
183 static char *mem2hex (gdb_byte *, char *, int);
185 static struct command_line *
186 all_tracepoint_actions_and_cleanup (struct breakpoint *t);
188 extern void _initialize_tracepoint (void);
190 static struct trace_status trace_status;
192 const char *stop_reason_names[] = {
202 struct trace_status *
203 current_trace_status (void)
205 return &trace_status;
211 free_traceframe_info (struct traceframe_info *info)
215 VEC_free (mem_range_s, info->memory);
216 VEC_free (int, info->tvars);
222 /* Free and clear the traceframe info cache of the current
226 clear_traceframe_info (void)
228 free_traceframe_info (traceframe_info);
229 traceframe_info = NULL;
232 /* Set traceframe number to NUM. */
234 set_traceframe_num (int num)
236 traceframe_number = num;
237 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
240 /* Set tracepoint number to NUM. */
242 set_tracepoint_num (int num)
244 tracepoint_number = num;
245 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
248 /* Set externally visible debug variables for querying/printing
249 the traceframe context (line, function, file). */
252 set_traceframe_context (struct frame_info *trace_frame)
255 struct symbol *traceframe_fun;
256 struct symtab_and_line traceframe_sal;
258 /* Save as globals for internal use. */
259 if (trace_frame != NULL
260 && get_frame_pc_if_available (trace_frame, &trace_pc))
262 traceframe_sal = find_pc_line (trace_pc, 0);
263 traceframe_fun = find_pc_function (trace_pc);
265 /* Save linenumber as "$trace_line", a debugger variable visible to
267 set_internalvar_integer (lookup_internalvar ("trace_line"),
268 traceframe_sal.line);
272 init_sal (&traceframe_sal);
273 traceframe_fun = NULL;
274 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
277 /* Save func name as "$trace_func", a debugger variable visible to
279 if (traceframe_fun == NULL
280 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
281 clear_internalvar (lookup_internalvar ("trace_func"));
283 set_internalvar_string (lookup_internalvar ("trace_func"),
284 SYMBOL_LINKAGE_NAME (traceframe_fun));
286 /* Save file name as "$trace_file", a debugger variable visible to
288 if (traceframe_sal.symtab == NULL)
289 clear_internalvar (lookup_internalvar ("trace_file"));
291 set_internalvar_string (lookup_internalvar ("trace_file"),
292 symtab_to_filename_for_display (traceframe_sal.symtab));
295 /* Create a new trace state variable with the given name. */
297 struct trace_state_variable *
298 create_trace_state_variable (const char *name)
300 struct trace_state_variable tsv;
302 memset (&tsv, 0, sizeof (tsv));
303 tsv.name = xstrdup (name);
304 tsv.number = next_tsv_number++;
305 return VEC_safe_push (tsv_s, tvariables, &tsv);
308 /* Look for a trace state variable of the given name. */
310 struct trace_state_variable *
311 find_trace_state_variable (const char *name)
313 struct trace_state_variable *tsv;
316 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
317 if (strcmp (name, tsv->name) == 0)
323 /* Look for a trace state variable of the given number. Return NULL if
326 struct trace_state_variable *
327 find_trace_state_variable_by_number (int number)
329 struct trace_state_variable *tsv;
332 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
333 if (tsv->number == number)
340 delete_trace_state_variable (const char *name)
342 struct trace_state_variable *tsv;
345 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
346 if (strcmp (name, tsv->name) == 0)
348 observer_notify_tsv_deleted (tsv);
350 xfree ((void *)tsv->name);
351 VEC_unordered_remove (tsv_s, tvariables, ix);
356 warning (_("No trace variable named \"$%s\", not deleting"), name);
359 /* Throws an error if NAME is not valid syntax for a trace state
363 validate_trace_state_variable_name (const char *name)
368 error (_("Must supply a non-empty variable name"));
370 /* All digits in the name is reserved for value history
372 for (p = name; isdigit (*p); p++)
375 error (_("$%s is not a valid trace state variable name"), name);
377 for (p = name; isalnum (*p) || *p == '_'; p++)
380 error (_("$%s is not a valid trace state variable name"), name);
383 /* The 'tvariable' command collects a name and optional expression to
384 evaluate into an initial value. */
387 trace_variable_command (char *args, int from_tty)
389 struct cleanup *old_chain;
391 struct trace_state_variable *tsv;
395 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
397 /* Only allow two syntaxes; "$name" and "$name=value". */
398 p = skip_spaces (args);
401 error (_("Name of trace variable should start with '$'"));
404 while (isalnum (*p) || *p == '_')
406 name = savestring (name, p - name);
407 old_chain = make_cleanup (xfree, name);
410 if (*p != '=' && *p != '\0')
411 error (_("Syntax must be $NAME [ = EXPR ]"));
413 validate_trace_state_variable_name (name);
416 initval = value_as_long (parse_and_eval (++p));
418 /* If the variable already exists, just change its initial value. */
419 tsv = find_trace_state_variable (name);
422 if (tsv->initial_value != initval)
424 tsv->initial_value = initval;
425 observer_notify_tsv_modified (tsv);
427 printf_filtered (_("Trace state variable $%s "
428 "now has initial value %s.\n"),
429 tsv->name, plongest (tsv->initial_value));
430 do_cleanups (old_chain);
434 /* Create a new variable. */
435 tsv = create_trace_state_variable (name);
436 tsv->initial_value = initval;
438 observer_notify_tsv_created (tsv);
440 printf_filtered (_("Trace state variable $%s "
441 "created, with initial value %s.\n"),
442 tsv->name, plongest (tsv->initial_value));
444 do_cleanups (old_chain);
448 delete_trace_variable_command (char *args, int from_tty)
452 struct cleanup *back_to;
456 if (query (_("Delete all trace state variables? ")))
457 VEC_free (tsv_s, tvariables);
459 observer_notify_tsv_deleted (NULL);
463 argv = gdb_buildargv (args);
464 back_to = make_cleanup_freeargv (argv);
466 for (ix = 0; argv[ix] != NULL; ix++)
468 if (*argv[ix] == '$')
469 delete_trace_state_variable (argv[ix] + 1);
471 warning (_("Name \"%s\" not prefixed with '$', ignoring"), argv[ix]);
474 do_cleanups (back_to);
480 tvariables_info_1 (void)
482 struct trace_state_variable *tsv;
485 struct cleanup *back_to;
486 struct ui_out *uiout = current_uiout;
488 if (VEC_length (tsv_s, tvariables) == 0 && !uiout->is_mi_like_p ())
490 printf_filtered (_("No trace state variables.\n"));
494 /* Try to acquire values from the target. */
495 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix, ++count)
496 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
499 back_to = make_cleanup_ui_out_table_begin_end (uiout, 3,
500 count, "trace-variables");
501 uiout->table_header (15, ui_left, "name", "Name");
502 uiout->table_header (11, ui_left, "initial", "Initial");
503 uiout->table_header (11, ui_left, "current", "Current");
505 uiout->table_body ();
507 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
509 struct cleanup *back_to2;
513 back_to2 = make_cleanup_ui_out_tuple_begin_end (uiout, "variable");
515 name = concat ("$", tsv->name, (char *) NULL);
516 make_cleanup (xfree, name);
517 uiout->field_string ("name", name);
518 uiout->field_string ("initial", plongest (tsv->initial_value));
520 if (tsv->value_known)
521 c = plongest (tsv->value);
522 else if (uiout->is_mi_like_p ())
523 /* For MI, we prefer not to use magic string constants, but rather
524 omit the field completely. The difference between unknown and
525 undefined does not seem important enough to represent. */
527 else if (current_trace_status ()->running || traceframe_number >= 0)
528 /* The value is/was defined, but we don't have it. */
531 /* It is not meaningful to ask about the value. */
534 uiout->field_string ("current", c);
537 do_cleanups (back_to2);
540 do_cleanups (back_to);
543 /* List all the trace state variables. */
546 tvariables_info (char *args, int from_tty)
548 tvariables_info_1 ();
551 /* Stash definitions of tsvs into the given file. */
554 save_trace_state_variables (struct ui_file *fp)
556 struct trace_state_variable *tsv;
559 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
561 fprintf_unfiltered (fp, "tvariable $%s", tsv->name);
562 if (tsv->initial_value)
563 fprintf_unfiltered (fp, " = %s", plongest (tsv->initial_value));
564 fprintf_unfiltered (fp, "\n");
568 /* ACTIONS functions: */
570 /* The three functions:
571 collect_pseudocommand,
572 while_stepping_pseudocommand, and
573 end_actions_pseudocommand
574 are placeholders for "commands" that are actually ONLY to be used
575 within a tracepoint action list. If the actual function is ever called,
576 it means that somebody issued the "command" at the top level,
577 which is always an error. */
580 end_actions_pseudocommand (char *args, int from_tty)
582 error (_("This command cannot be used at the top level."));
586 while_stepping_pseudocommand (char *args, int from_tty)
588 error (_("This command can only be used in a tracepoint actions list."));
592 collect_pseudocommand (char *args, int from_tty)
594 error (_("This command can only be used in a tracepoint actions list."));
598 teval_pseudocommand (char *args, int from_tty)
600 error (_("This command can only be used in a tracepoint actions list."));
603 /* Parse any collection options, such as /s for strings. */
606 decode_agent_options (const char *exp, int *trace_string)
608 struct value_print_options opts;
615 /* Call this to borrow the print elements default for collection
617 get_user_print_options (&opts);
622 if (target_supports_string_tracing ())
624 /* Allow an optional decimal number giving an explicit maximum
625 string length, defaulting it to the "print elements" value;
626 so "collect/s80 mystr" gets at most 80 bytes of string. */
627 *trace_string = opts.print_max;
629 if (*exp >= '0' && *exp <= '9')
630 *trace_string = atoi (exp);
631 while (*exp >= '0' && *exp <= '9')
635 error (_("Target does not support \"/s\" option for string tracing."));
638 error (_("Undefined collection format \"%c\"."), *exp);
640 exp = skip_spaces_const (exp);
645 /* Enter a list of actions for a tracepoint. */
647 actions_command (char *args, int from_tty)
649 struct tracepoint *t;
650 struct command_line *l;
652 t = get_tracepoint_by_number (&args, NULL);
656 xstrprintf ("Enter actions for tracepoint %d, one per line.",
658 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
660 l = read_command_lines (tmpbuf, from_tty, 1,
661 check_tracepoint_command, t);
662 do_cleanups (cleanups);
663 breakpoint_set_commands (&t->base, l);
665 /* else just return */
668 /* Report the results of checking the agent expression, as errors or
672 report_agent_reqs_errors (struct agent_expr *aexpr)
674 /* All of the "flaws" are serious bytecode generation issues that
675 should never occur. */
676 if (aexpr->flaw != agent_flaw_none)
677 internal_error (__FILE__, __LINE__, _("expression is malformed"));
679 /* If analysis shows a stack underflow, GDB must have done something
680 badly wrong in its bytecode generation. */
681 if (aexpr->min_height < 0)
682 internal_error (__FILE__, __LINE__,
683 _("expression has min height < 0"));
685 /* Issue this error if the stack is predicted to get too deep. The
686 limit is rather arbitrary; a better scheme might be for the
687 target to report how much stack it will have available. The
688 depth roughly corresponds to parenthesization, so a limit of 20
689 amounts to 20 levels of expression nesting, which is actually
690 a pretty big hairy expression. */
691 if (aexpr->max_height > 20)
692 error (_("Expression is too complicated."));
695 /* worker function */
697 validate_actionline (const char *line, struct breakpoint *b)
699 struct cmd_list_element *c;
700 struct cleanup *old_chain = NULL;
703 struct bp_location *loc;
704 struct tracepoint *t = (struct tracepoint *) b;
706 /* If EOF is typed, *line is NULL. */
710 p = skip_spaces_const (line);
712 /* Symbol lookup etc. */
713 if (*p == '\0') /* empty line: just prompt for another line. */
716 if (*p == '#') /* comment line */
719 c = lookup_cmd (&p, cmdlist, "", -1, 1);
721 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
723 if (cmd_cfunc_eq (c, collect_pseudocommand))
725 int trace_string = 0;
728 p = decode_agent_options (p, &trace_string);
731 { /* Repeat over a comma-separated list. */
732 QUIT; /* Allow user to bail out with ^C. */
733 p = skip_spaces_const (p);
735 if (*p == '$') /* Look for special pseudo-symbols. */
737 if (0 == strncasecmp ("reg", p + 1, 3)
738 || 0 == strncasecmp ("arg", p + 1, 3)
739 || 0 == strncasecmp ("loc", p + 1, 3)
740 || 0 == strncasecmp ("_ret", p + 1, 4)
741 || 0 == strncasecmp ("_sdata", p + 1, 6))
746 /* else fall thru, treat p as an expression and parse it! */
749 for (loc = t->base.loc; loc; loc = loc->next)
752 expression_up exp = parse_exp_1 (&p, loc->address,
753 block_for_pc (loc->address), 1);
755 if (exp->elts[0].opcode == OP_VAR_VALUE)
757 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
759 error (_("constant `%s' (value %s) "
760 "will not be collected."),
761 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
762 plongest (SYMBOL_VALUE (exp->elts[2].symbol)));
764 else if (SYMBOL_CLASS (exp->elts[2].symbol)
765 == LOC_OPTIMIZED_OUT)
767 error (_("`%s' is optimized away "
768 "and cannot be collected."),
769 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
773 /* We have something to collect, make sure that the expr to
774 bytecode translator can handle it and that it's not too
776 agent_expr_up aexpr = gen_trace_for_expr (loc->address,
780 if (aexpr->len > MAX_AGENT_EXPR_LEN)
781 error (_("Expression is too complicated."));
783 ax_reqs (aexpr.get ());
785 report_agent_reqs_errors (aexpr.get ());
788 while (p && *p++ == ',');
791 else if (cmd_cfunc_eq (c, teval_pseudocommand))
794 { /* Repeat over a comma-separated list. */
795 QUIT; /* Allow user to bail out with ^C. */
796 p = skip_spaces_const (p);
799 for (loc = t->base.loc; loc; loc = loc->next)
803 /* Only expressions are allowed for this action. */
804 expression_up exp = parse_exp_1 (&p, loc->address,
805 block_for_pc (loc->address), 1);
807 /* We have something to evaluate, make sure that the expr to
808 bytecode translator can handle it and that it's not too
810 agent_expr_up aexpr = gen_eval_for_expr (loc->address, exp.get ());
812 if (aexpr->len > MAX_AGENT_EXPR_LEN)
813 error (_("Expression is too complicated."));
815 ax_reqs (aexpr.get ());
816 report_agent_reqs_errors (aexpr.get ());
819 while (p && *p++ == ',');
822 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
826 p = skip_spaces_const (p);
827 t->step_count = strtol (p, &endp, 0);
828 if (endp == p || t->step_count == 0)
829 error (_("while-stepping step count `%s' is malformed."), line);
833 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
837 error (_("`%s' is not a supported tracepoint action."), line);
841 memrange_absolute = -1
844 /* MEMRANGE functions: */
846 /* Compare memranges for std::sort. */
849 memrange_comp (const memrange &a, const memrange &b)
851 if (a.type == b.type)
853 if (a.type == memrange_absolute)
854 return (bfd_vma) a.start < (bfd_vma) b.start;
856 return a.start < b.start;
859 return a.type < b.type;
862 /* Sort the memrange list using std::sort, and merge adjacent memranges. */
865 memrange_sortmerge (std::vector<memrange> &memranges)
867 if (!memranges.empty ())
871 std::sort (memranges.begin (), memranges.end (), memrange_comp);
873 for (a = 0, b = 1; b < memranges.size (); b++)
875 /* If memrange b overlaps or is adjacent to memrange a,
877 if (memranges[a].type == memranges[b].type
878 && memranges[b].start <= memranges[a].end)
880 if (memranges[b].end > memranges[a].end)
881 memranges[a].end = memranges[b].end;
882 continue; /* next b, same a */
886 memranges[a] = memranges[b];
888 memranges.resize (a + 1);
892 /* Add a register to a collection list. */
895 collection_list::add_register (unsigned int regno)
898 printf_filtered ("collect register %d\n", regno);
899 if (regno >= (8 * sizeof (m_regs_mask)))
900 error (_("Internal: register number %d too large for tracepoint"),
902 m_regs_mask[regno / 8] |= 1 << (regno % 8);
905 /* Add a memrange to a collection list. */
908 collection_list::add_memrange (struct gdbarch *gdbarch,
909 int type, bfd_signed_vma base,
913 printf_filtered ("(%d,%s,%ld)\n", type, paddress (gdbarch, base), len);
915 /* type: memrange_absolute == memory, other n == basereg */
916 /* base: addr if memory, offset if reg relative. */
917 /* len: we actually save end (base + len) for convenience */
918 m_memranges.emplace_back (type, base, base + len);
920 if (type != memrange_absolute) /* Better collect the base register! */
924 /* Add a symbol to a collection list. */
927 collection_list::collect_symbol (struct symbol *sym,
928 struct gdbarch *gdbarch,
929 long frame_regno, long frame_offset,
935 bfd_signed_vma offset;
936 int treat_as_expr = 0;
938 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
939 switch (SYMBOL_CLASS (sym))
942 printf_filtered ("%s: don't know symbol class %d\n",
943 SYMBOL_PRINT_NAME (sym),
947 printf_filtered ("constant %s (value %s) will not be collected.\n",
948 SYMBOL_PRINT_NAME (sym), plongest (SYMBOL_VALUE (sym)));
951 offset = SYMBOL_VALUE_ADDRESS (sym);
954 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
955 SYMBOL_PRINT_NAME (sym), len,
956 paddress (gdbarch, offset));
958 /* A struct may be a C++ class with static fields, go to general
959 expression handling. */
960 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT)
963 add_memrange (gdbarch, memrange_absolute, offset, len);
966 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
968 printf_filtered ("LOC_REG[parm] %s: ",
969 SYMBOL_PRINT_NAME (sym));
971 /* Check for doubles stored in two registers. */
972 /* FIXME: how about larger types stored in 3 or more regs? */
973 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
974 len > register_size (gdbarch, reg))
975 add_register (reg + 1);
978 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
979 printf_filtered (" (will not collect %s)\n",
980 SYMBOL_PRINT_NAME (sym));
984 offset = frame_offset + SYMBOL_VALUE (sym);
987 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
988 " from frame ptr reg %d\n",
989 SYMBOL_PRINT_NAME (sym), len,
990 paddress (gdbarch, offset), reg);
992 add_memrange (gdbarch, reg, offset, len);
994 case LOC_REGPARM_ADDR:
995 reg = SYMBOL_VALUE (sym);
999 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s"
1001 SYMBOL_PRINT_NAME (sym), len,
1002 paddress (gdbarch, offset), reg);
1004 add_memrange (gdbarch, reg, offset, len);
1008 offset = frame_offset + SYMBOL_VALUE (sym);
1011 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
1012 " from frame ptr reg %d\n",
1013 SYMBOL_PRINT_NAME (sym), len,
1014 paddress (gdbarch, offset), reg);
1016 add_memrange (gdbarch, reg, offset, len);
1019 case LOC_UNRESOLVED:
1023 case LOC_OPTIMIZED_OUT:
1024 printf_filtered ("%s has been optimized out of existence.\n",
1025 SYMBOL_PRINT_NAME (sym));
1033 /* Expressions are the most general case. */
1036 struct cleanup *old_chain1 = NULL;
1038 agent_expr_up aexpr = gen_trace_for_var (scope, gdbarch,
1041 /* It can happen that the symbol is recorded as a computed
1042 location, but it's been optimized away and doesn't actually
1043 have a location expression. */
1046 printf_filtered ("%s has been optimized out of existence.\n",
1047 SYMBOL_PRINT_NAME (sym));
1051 ax_reqs (aexpr.get ());
1053 report_agent_reqs_errors (aexpr.get ());
1055 /* Take care of the registers. */
1056 if (aexpr->reg_mask_len > 0)
1058 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1060 QUIT; /* Allow user to bail out with ^C. */
1061 if (aexpr->reg_mask[ndx1] != 0)
1063 /* Assume chars have 8 bits. */
1064 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1065 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1066 /* It's used -- record it. */
1067 add_register (ndx1 * 8 + ndx2);
1072 add_aexpr (std::move (aexpr));
1076 /* Data to be passed around in the calls to the locals and args
1079 struct add_local_symbols_data
1081 struct collection_list *collect;
1082 struct gdbarch *gdbarch;
1090 /* The callback for the locals and args iterators. */
1093 do_collect_symbol (const char *print_name,
1097 struct add_local_symbols_data *p = (struct add_local_symbols_data *) cb_data;
1099 p->collect->collect_symbol (sym, p->gdbarch, p->frame_regno,
1100 p->frame_offset, p->pc, p->trace_string);
1103 p->collect->add_wholly_collected (print_name);
1107 collection_list::add_wholly_collected (const char *print_name)
1109 m_wholly_collected.push_back (print_name);
1112 /* Add all locals (or args) symbols to collection list. */
1115 collection_list::add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc,
1116 long frame_regno, long frame_offset, int type,
1119 const struct block *block;
1120 struct add_local_symbols_data cb_data;
1122 cb_data.collect = this;
1123 cb_data.gdbarch = gdbarch;
1125 cb_data.frame_regno = frame_regno;
1126 cb_data.frame_offset = frame_offset;
1128 cb_data.trace_string = trace_string;
1132 block = block_for_pc (pc);
1135 warning (_("Can't collect locals; "
1136 "no symbol table info available.\n"));
1140 iterate_over_block_local_vars (block, do_collect_symbol, &cb_data);
1141 if (cb_data.count == 0)
1142 warning (_("No locals found in scope."));
1146 pc = get_pc_function_start (pc);
1147 block = block_for_pc (pc);
1150 warning (_("Can't collect args; no symbol table info available."));
1154 iterate_over_block_arg_vars (block, do_collect_symbol, &cb_data);
1155 if (cb_data.count == 0)
1156 warning (_("No args found in scope."));
1161 collection_list::add_static_trace_data ()
1164 printf_filtered ("collect static trace data\n");
1165 m_strace_data = true;
1168 collection_list::collection_list ()
1170 m_strace_data (false)
1172 m_memranges.reserve (128);
1173 m_aexprs.reserve (128);
1176 /* Reduce a collection list to string form (for gdb protocol). */
1179 collection_list::stringify ()
1181 char temp_buf[2048];
1184 char *(*str_list)[];
1188 count = 1 + 1 + m_memranges.size () + m_aexprs.size () + 1;
1189 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
1194 printf_filtered ("\nCollecting static trace data\n");
1197 (*str_list)[ndx] = savestring (temp_buf, end - temp_buf);
1201 for (i = sizeof (m_regs_mask) - 1; i > 0; i--)
1202 if (m_regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
1204 if (m_regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
1207 printf_filtered ("\nCollecting registers (mask): 0x");
1212 QUIT; /* Allow user to bail out with ^C. */
1214 printf_filtered ("%02X", m_regs_mask[i]);
1215 sprintf (end, "%02X", m_regs_mask[i]);
1218 (*str_list)[ndx] = xstrdup (temp_buf);
1222 printf_filtered ("\n");
1223 if (!m_memranges.empty () && info_verbose)
1224 printf_filtered ("Collecting memranges: \n");
1225 for (i = 0, count = 0, end = temp_buf; i < m_memranges.size (); i++)
1227 QUIT; /* Allow user to bail out with ^C. */
1230 printf_filtered ("(%d, %s, %ld)\n",
1231 m_memranges[i].type,
1232 paddress (target_gdbarch (),
1233 m_memranges[i].start),
1234 (long) (m_memranges[i].end
1235 - m_memranges[i].start));
1237 if (count + 27 > MAX_AGENT_EXPR_LEN)
1239 (*str_list)[ndx] = savestring (temp_buf, count);
1246 bfd_signed_vma length
1247 = m_memranges[i].end - m_memranges[i].start;
1249 /* The "%X" conversion specifier expects an unsigned argument,
1250 so passing -1 (memrange_absolute) to it directly gives you
1251 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1253 if (m_memranges[i].type == memrange_absolute)
1254 sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
1257 sprintf (end, "M%X,%s,%lX", m_memranges[i].type,
1258 phex_nz (m_memranges[i].start, 0), (long) length);
1261 count += strlen (end);
1262 end = temp_buf + count;
1265 for (i = 0; i < m_aexprs.size (); i++)
1267 QUIT; /* Allow user to bail out with ^C. */
1268 if ((count + 10 + 2 * m_aexprs[i]->len) > MAX_AGENT_EXPR_LEN)
1270 (*str_list)[ndx] = savestring (temp_buf, count);
1275 sprintf (end, "X%08X,", m_aexprs[i]->len);
1276 end += 10; /* 'X' + 8 hex digits + ',' */
1279 end = mem2hex (m_aexprs[i]->buf, end, m_aexprs[i]->len);
1280 count += 2 * m_aexprs[i]->len;
1285 (*str_list)[ndx] = savestring (temp_buf, count);
1290 (*str_list)[ndx] = NULL;
1301 /* Add the printed expression EXP to *LIST. */
1304 collection_list::append_exp (struct expression *exp)
1306 string_file tmp_stream;
1308 print_expression (exp, &tmp_stream);
1310 m_computed.push_back (std::move (tmp_stream.string ()));
1314 collection_list::finish ()
1316 memrange_sortmerge (m_memranges);
1320 encode_actions_1 (struct command_line *action,
1321 struct bp_location *tloc,
1323 LONGEST frame_offset,
1324 struct collection_list *collect,
1325 struct collection_list *stepping_list)
1327 const char *action_exp;
1329 struct value *tempval;
1330 struct cmd_list_element *cmd;
1332 for (; action; action = action->next)
1334 QUIT; /* Allow user to bail out with ^C. */
1335 action_exp = action->line;
1336 action_exp = skip_spaces_const (action_exp);
1338 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1340 error (_("Bad action list item: %s"), action_exp);
1342 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
1344 int trace_string = 0;
1346 if (*action_exp == '/')
1347 action_exp = decode_agent_options (action_exp, &trace_string);
1350 { /* Repeat over a comma-separated list. */
1351 QUIT; /* Allow user to bail out with ^C. */
1352 action_exp = skip_spaces_const (action_exp);
1354 if (0 == strncasecmp ("$reg", action_exp, 4))
1356 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
1357 collect->add_register (i);
1358 action_exp = strchr (action_exp, ','); /* more? */
1360 else if (0 == strncasecmp ("$arg", action_exp, 4))
1362 collect->add_local_symbols (target_gdbarch (),
1368 action_exp = strchr (action_exp, ','); /* more? */
1370 else if (0 == strncasecmp ("$loc", action_exp, 4))
1372 collect->add_local_symbols (target_gdbarch (),
1378 action_exp = strchr (action_exp, ','); /* more? */
1380 else if (0 == strncasecmp ("$_ret", action_exp, 5))
1383 = gen_trace_for_return_address (tloc->address,
1387 ax_reqs (aexpr.get ());
1388 report_agent_reqs_errors (aexpr.get ());
1390 /* take care of the registers */
1391 if (aexpr->reg_mask_len > 0)
1393 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1395 QUIT; /* allow user to bail out with ^C */
1396 if (aexpr->reg_mask[ndx1] != 0)
1398 /* assume chars have 8 bits */
1399 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1400 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1402 /* It's used -- record it. */
1403 collect->add_register (ndx1 * 8 + ndx2);
1409 collect->add_aexpr (std::move (aexpr));
1410 action_exp = strchr (action_exp, ','); /* more? */
1412 else if (0 == strncasecmp ("$_sdata", action_exp, 7))
1414 collect->add_static_trace_data ();
1415 action_exp = strchr (action_exp, ','); /* more? */
1420 struct cleanup *old_chain1 = NULL;
1422 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1423 block_for_pc (tloc->address),
1426 switch (exp->elts[0].opcode)
1430 const char *name = &exp->elts[2].string;
1432 i = user_reg_map_name_to_regnum (target_gdbarch (),
1433 name, strlen (name));
1435 internal_error (__FILE__, __LINE__,
1436 _("Register $%s not available"),
1439 printf_filtered ("OP_REGISTER: ");
1440 collect->add_register (i);
1445 /* Safe because we know it's a simple expression. */
1446 tempval = evaluate_expression (exp.get ());
1447 addr = value_address (tempval);
1448 /* Initialize the TYPE_LENGTH if it is a typedef. */
1449 check_typedef (exp->elts[1].type);
1450 collect->add_memrange (target_gdbarch (),
1451 memrange_absolute, addr,
1452 TYPE_LENGTH (exp->elts[1].type));
1453 collect->append_exp (exp.get ());
1458 struct symbol *sym = exp->elts[2].symbol;
1459 char_ptr name = (char_ptr) SYMBOL_NATURAL_NAME (sym);
1461 collect->collect_symbol (exp->elts[2].symbol,
1467 collect->add_wholly_collected (name);
1471 default: /* Full-fledged expression. */
1472 agent_expr_up aexpr = gen_trace_for_expr (tloc->address,
1476 ax_reqs (aexpr.get ());
1478 report_agent_reqs_errors (aexpr.get ());
1480 /* Take care of the registers. */
1481 if (aexpr->reg_mask_len > 0)
1483 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1485 QUIT; /* Allow user to bail out with ^C. */
1486 if (aexpr->reg_mask[ndx1] != 0)
1488 /* Assume chars have 8 bits. */
1489 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1490 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1492 /* It's used -- record it. */
1493 collect->add_register (ndx1 * 8 + ndx2);
1499 collect->add_aexpr (std::move (aexpr));
1500 collect->append_exp (exp.get ());
1505 while (action_exp && *action_exp++ == ',');
1507 else if (cmd_cfunc_eq (cmd, teval_pseudocommand))
1510 { /* Repeat over a comma-separated list. */
1511 QUIT; /* Allow user to bail out with ^C. */
1512 action_exp = skip_spaces_const (action_exp);
1515 struct cleanup *old_chain1 = NULL;
1517 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1518 block_for_pc (tloc->address),
1521 agent_expr_up aexpr = gen_eval_for_expr (tloc->address,
1524 ax_reqs (aexpr.get ());
1525 report_agent_reqs_errors (aexpr.get ());
1527 /* Even though we're not officially collecting, add
1528 to the collect list anyway. */
1529 collect->add_aexpr (std::move (aexpr));
1532 while (action_exp && *action_exp++ == ',');
1534 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
1536 /* We check against nested while-stepping when setting
1537 breakpoint action, so no way to run into nested
1539 gdb_assert (stepping_list);
1541 encode_actions_1 (action->body_list[0], tloc, frame_reg,
1542 frame_offset, stepping_list, NULL);
1545 error (_("Invalid tracepoint command '%s'"), action->line);
1549 /* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
1550 and STEPPING_LIST. */
1553 encode_actions (struct bp_location *tloc,
1554 struct collection_list *tracepoint_list,
1555 struct collection_list *stepping_list)
1557 struct command_line *actions;
1559 LONGEST frame_offset;
1561 gdbarch_virtual_frame_pointer (tloc->gdbarch,
1562 tloc->address, &frame_reg, &frame_offset);
1564 actions = all_tracepoint_actions_and_cleanup (tloc->owner);
1566 encode_actions_1 (actions, tloc, frame_reg, frame_offset,
1567 tracepoint_list, stepping_list);
1569 tracepoint_list->finish ();
1570 stepping_list->finish ();
1573 /* Render all actions into gdb protocol. */
1576 encode_actions_rsp (struct bp_location *tloc, char ***tdp_actions,
1577 char ***stepping_actions)
1579 struct collection_list tracepoint_list, stepping_list;
1581 *tdp_actions = NULL;
1582 *stepping_actions = NULL;
1584 encode_actions (tloc, &tracepoint_list, &stepping_list);
1586 *tdp_actions = tracepoint_list.stringify ();
1587 *stepping_actions = stepping_list.stringify ();
1591 collection_list::add_aexpr (agent_expr_up aexpr)
1593 m_aexprs.push_back (std::move (aexpr));
1597 process_tracepoint_on_disconnect (void)
1599 VEC(breakpoint_p) *tp_vec = NULL;
1601 struct breakpoint *b;
1602 int has_pending_p = 0;
1604 /* Check whether we still have pending tracepoint. If we have, warn the
1605 user that pending tracepoint will no longer work. */
1606 tp_vec = all_tracepoints ();
1607 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1616 struct bp_location *loc1;
1618 for (loc1 = b->loc; loc1; loc1 = loc1->next)
1620 if (loc1->shlib_disabled)
1631 VEC_free (breakpoint_p, tp_vec);
1634 warning (_("Pending tracepoints will not be resolved while"
1635 " GDB is disconnected\n"));
1638 /* Reset local state of tracing. */
1641 trace_reset_local_state (void)
1643 set_traceframe_num (-1);
1644 set_tracepoint_num (-1);
1645 set_traceframe_context (NULL);
1646 clear_traceframe_info ();
1650 start_tracing (char *notes)
1652 VEC(breakpoint_p) *tp_vec = NULL;
1654 struct breakpoint *b;
1655 struct trace_state_variable *tsv;
1656 int any_enabled = 0, num_to_download = 0;
1659 tp_vec = all_tracepoints ();
1661 /* No point in tracing without any tracepoints... */
1662 if (VEC_length (breakpoint_p, tp_vec) == 0)
1664 VEC_free (breakpoint_p, tp_vec);
1665 error (_("No tracepoints defined, not starting trace"));
1668 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1670 if (b->enable_state == bp_enabled)
1673 if ((b->type == bp_fast_tracepoint
1674 ? may_insert_fast_tracepoints
1675 : may_insert_tracepoints))
1678 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
1679 (b->type == bp_fast_tracepoint ? "fast " : ""), b->number);
1684 if (target_supports_enable_disable_tracepoint ())
1685 warning (_("No tracepoints enabled"));
1688 /* No point in tracing with only disabled tracepoints that
1689 cannot be re-enabled. */
1690 VEC_free (breakpoint_p, tp_vec);
1691 error (_("No tracepoints enabled, not starting trace"));
1695 if (num_to_download <= 0)
1697 VEC_free (breakpoint_p, tp_vec);
1698 error (_("No tracepoints that may be downloaded, not starting trace"));
1701 target_trace_init ();
1703 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1705 struct tracepoint *t = (struct tracepoint *) b;
1706 struct bp_location *loc;
1707 int bp_location_downloaded = 0;
1709 /* Clear `inserted' flag. */
1710 for (loc = b->loc; loc; loc = loc->next)
1713 if ((b->type == bp_fast_tracepoint
1714 ? !may_insert_fast_tracepoints
1715 : !may_insert_tracepoints))
1718 t->number_on_target = 0;
1720 for (loc = b->loc; loc; loc = loc->next)
1722 /* Since tracepoint locations are never duplicated, `inserted'
1723 flag should be zero. */
1724 gdb_assert (!loc->inserted);
1726 target_download_tracepoint (loc);
1729 bp_location_downloaded = 1;
1732 t->number_on_target = b->number;
1734 for (loc = b->loc; loc; loc = loc->next)
1735 if (loc->probe.probe != NULL
1736 && loc->probe.probe->pops->set_semaphore != NULL)
1737 loc->probe.probe->pops->set_semaphore (loc->probe.probe,
1741 if (bp_location_downloaded)
1742 observer_notify_breakpoint_modified (b);
1744 VEC_free (breakpoint_p, tp_vec);
1746 /* Send down all the trace state variables too. */
1747 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
1749 target_download_trace_state_variable (tsv);
1752 /* Tell target to treat text-like sections as transparent. */
1753 target_trace_set_readonly_regions ();
1754 /* Set some mode flags. */
1755 target_set_disconnected_tracing (disconnected_tracing);
1756 target_set_circular_trace_buffer (circular_trace_buffer);
1757 target_set_trace_buffer_size (trace_buffer_size);
1760 notes = trace_notes;
1761 ret = target_set_trace_notes (trace_user, notes, NULL);
1763 if (!ret && (trace_user || notes))
1764 warning (_("Target does not support trace user/notes, info ignored"));
1766 /* Now insert traps and begin collecting data. */
1767 target_trace_start ();
1769 /* Reset our local state. */
1770 trace_reset_local_state ();
1771 current_trace_status()->running = 1;
1774 /* The tstart command requests the target to start a new trace run.
1775 The command passes any arguments it has to the target verbatim, as
1776 an optional "trace note". This is useful as for instance a warning
1777 to other users if the trace runs disconnected, and you don't want
1778 anybody else messing with the target. */
1781 tstart_command (char *args, int from_tty)
1783 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1785 if (current_trace_status ()->running)
1788 && !query (_("A trace is running already. Start a new run? ")))
1789 error (_("New trace run not started."));
1792 start_tracing (args);
1795 /* The tstop command stops the tracing run. The command passes any
1796 supplied arguments to the target verbatim as a "stop note"; if the
1797 target supports trace notes, then it will be reported back as part
1798 of the trace run's status. */
1801 tstop_command (char *args, int from_tty)
1803 if (!current_trace_status ()->running)
1804 error (_("Trace is not running."));
1806 stop_tracing (args);
1810 stop_tracing (char *note)
1813 VEC(breakpoint_p) *tp_vec = NULL;
1815 struct breakpoint *t;
1817 target_trace_stop ();
1819 tp_vec = all_tracepoints ();
1820 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1822 struct bp_location *loc;
1824 if ((t->type == bp_fast_tracepoint
1825 ? !may_insert_fast_tracepoints
1826 : !may_insert_tracepoints))
1829 for (loc = t->loc; loc; loc = loc->next)
1831 /* GDB can be totally absent in some disconnected trace scenarios,
1832 but we don't really care if this semaphore goes out of sync.
1833 That's why we are decrementing it here, but not taking care
1835 if (loc->probe.probe != NULL
1836 && loc->probe.probe->pops->clear_semaphore != NULL)
1837 loc->probe.probe->pops->clear_semaphore (loc->probe.probe,
1843 VEC_free (breakpoint_p, tp_vec);
1846 note = trace_stop_notes;
1847 ret = target_set_trace_notes (NULL, NULL, note);
1850 warning (_("Target does not support trace notes, note ignored"));
1852 /* Should change in response to reply? */
1853 current_trace_status ()->running = 0;
1856 /* tstatus command */
1858 tstatus_command (char *args, int from_tty)
1860 struct trace_status *ts = current_trace_status ();
1862 VEC(breakpoint_p) *tp_vec = NULL;
1863 struct breakpoint *t;
1865 status = target_get_trace_status (ts);
1869 if (ts->filename != NULL)
1870 printf_filtered (_("Using a trace file.\n"));
1873 printf_filtered (_("Trace can not be run on this target.\n"));
1878 if (!ts->running_known)
1880 printf_filtered (_("Run/stop status is unknown.\n"));
1882 else if (ts->running)
1884 printf_filtered (_("Trace is running on the target.\n"));
1888 switch (ts->stop_reason)
1890 case trace_never_run:
1891 printf_filtered (_("No trace has been run on the target.\n"));
1893 case trace_stop_command:
1895 printf_filtered (_("Trace stopped by a tstop command (%s).\n"),
1898 printf_filtered (_("Trace stopped by a tstop command.\n"));
1900 case trace_buffer_full:
1901 printf_filtered (_("Trace stopped because the buffer was full.\n"));
1903 case trace_disconnected:
1904 printf_filtered (_("Trace stopped because of disconnection.\n"));
1906 case tracepoint_passcount:
1907 printf_filtered (_("Trace stopped by tracepoint %d.\n"),
1908 ts->stopping_tracepoint);
1910 case tracepoint_error:
1911 if (ts->stopping_tracepoint)
1912 printf_filtered (_("Trace stopped by an "
1913 "error (%s, tracepoint %d).\n"),
1914 ts->stop_desc, ts->stopping_tracepoint);
1916 printf_filtered (_("Trace stopped by an error (%s).\n"),
1919 case trace_stop_reason_unknown:
1920 printf_filtered (_("Trace stopped for an unknown reason.\n"));
1923 printf_filtered (_("Trace stopped for some other reason (%d).\n"),
1929 if (ts->traceframes_created >= 0
1930 && ts->traceframe_count != ts->traceframes_created)
1932 printf_filtered (_("Buffer contains %d trace "
1933 "frames (of %d created total).\n"),
1934 ts->traceframe_count, ts->traceframes_created);
1936 else if (ts->traceframe_count >= 0)
1938 printf_filtered (_("Collected %d trace frames.\n"),
1939 ts->traceframe_count);
1942 if (ts->buffer_free >= 0)
1944 if (ts->buffer_size >= 0)
1946 printf_filtered (_("Trace buffer has %d bytes of %d bytes free"),
1947 ts->buffer_free, ts->buffer_size);
1948 if (ts->buffer_size > 0)
1949 printf_filtered (_(" (%d%% full)"),
1950 ((int) ((((long long) (ts->buffer_size
1951 - ts->buffer_free)) * 100)
1952 / ts->buffer_size)));
1953 printf_filtered (_(".\n"));
1956 printf_filtered (_("Trace buffer has %d bytes free.\n"),
1960 if (ts->disconnected_tracing)
1961 printf_filtered (_("Trace will continue if GDB disconnects.\n"));
1963 printf_filtered (_("Trace will stop if GDB disconnects.\n"));
1965 if (ts->circular_buffer)
1966 printf_filtered (_("Trace buffer is circular.\n"));
1968 if (ts->user_name && strlen (ts->user_name) > 0)
1969 printf_filtered (_("Trace user is %s.\n"), ts->user_name);
1971 if (ts->notes && strlen (ts->notes) > 0)
1972 printf_filtered (_("Trace notes: %s.\n"), ts->notes);
1974 /* Now report on what we're doing with tfind. */
1975 if (traceframe_number >= 0)
1976 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
1977 traceframe_number, tracepoint_number);
1979 printf_filtered (_("Not looking at any trace frame.\n"));
1981 /* Report start/stop times if supplied. */
1986 LONGEST run_time = ts->stop_time - ts->start_time;
1988 /* Reporting a run time is more readable than two long numbers. */
1989 printf_filtered (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
1990 (long int) (ts->start_time / 1000000),
1991 (long int) (ts->start_time % 1000000),
1992 (long int) (run_time / 1000000),
1993 (long int) (run_time % 1000000));
1996 printf_filtered (_("Trace started at %ld.%06ld secs.\n"),
1997 (long int) (ts->start_time / 1000000),
1998 (long int) (ts->start_time % 1000000));
2000 else if (ts->stop_time)
2001 printf_filtered (_("Trace stopped at %ld.%06ld secs.\n"),
2002 (long int) (ts->stop_time / 1000000),
2003 (long int) (ts->stop_time % 1000000));
2005 /* Now report any per-tracepoint status available. */
2006 tp_vec = all_tracepoints ();
2008 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
2009 target_get_tracepoint_status (t, NULL);
2011 VEC_free (breakpoint_p, tp_vec);
2014 /* Report the trace status to uiout, in a way suitable for MI, and not
2015 suitable for CLI. If ON_STOP is true, suppress a few fields that
2016 are not meaningful in the -trace-stop response.
2018 The implementation is essentially parallel to trace_status_command, but
2019 merging them will result in unreadable code. */
2021 trace_status_mi (int on_stop)
2023 struct ui_out *uiout = current_uiout;
2024 struct trace_status *ts = current_trace_status ();
2027 status = target_get_trace_status (ts);
2029 if (status == -1 && ts->filename == NULL)
2031 uiout->field_string ("supported", "0");
2035 if (ts->filename != NULL)
2036 uiout->field_string ("supported", "file");
2038 uiout->field_string ("supported", "1");
2040 if (ts->filename != NULL)
2041 uiout->field_string ("trace-file", ts->filename);
2043 gdb_assert (ts->running_known);
2047 uiout->field_string ("running", "1");
2049 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
2050 Given that the frontend gets the status either on -trace-stop, or from
2051 -trace-status after re-connection, it does not seem like this
2052 information is necessary for anything. It is not necessary for either
2053 figuring the vital state of the target nor for navigation of trace
2054 frames. If the frontend wants to show the current state is some
2055 configure dialog, it can request the value when such dialog is
2056 invoked by the user. */
2060 const char *stop_reason = NULL;
2061 int stopping_tracepoint = -1;
2064 uiout->field_string ("running", "0");
2066 if (ts->stop_reason != trace_stop_reason_unknown)
2068 switch (ts->stop_reason)
2070 case trace_stop_command:
2071 stop_reason = "request";
2073 case trace_buffer_full:
2074 stop_reason = "overflow";
2076 case trace_disconnected:
2077 stop_reason = "disconnection";
2079 case tracepoint_passcount:
2080 stop_reason = "passcount";
2081 stopping_tracepoint = ts->stopping_tracepoint;
2083 case tracepoint_error:
2084 stop_reason = "error";
2085 stopping_tracepoint = ts->stopping_tracepoint;
2091 uiout->field_string ("stop-reason", stop_reason);
2092 if (stopping_tracepoint != -1)
2093 uiout->field_int ("stopping-tracepoint",
2094 stopping_tracepoint);
2095 if (ts->stop_reason == tracepoint_error)
2096 uiout->field_string ("error-description",
2102 if (ts->traceframe_count != -1)
2103 uiout->field_int ("frames", ts->traceframe_count);
2104 if (ts->traceframes_created != -1)
2105 uiout->field_int ("frames-created", ts->traceframes_created);
2106 if (ts->buffer_size != -1)
2107 uiout->field_int ("buffer-size", ts->buffer_size);
2108 if (ts->buffer_free != -1)
2109 uiout->field_int ("buffer-free", ts->buffer_free);
2111 uiout->field_int ("disconnected", ts->disconnected_tracing);
2112 uiout->field_int ("circular", ts->circular_buffer);
2114 uiout->field_string ("user-name", ts->user_name);
2115 uiout->field_string ("notes", ts->notes);
2120 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2121 (long int) (ts->start_time / 1000000),
2122 (long int) (ts->start_time % 1000000));
2123 uiout->field_string ("start-time", buf);
2124 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2125 (long int) (ts->stop_time / 1000000),
2126 (long int) (ts->stop_time % 1000000));
2127 uiout->field_string ("stop-time", buf);
2131 /* Check if a trace run is ongoing. If so, and FROM_TTY, query the
2132 user if she really wants to detach. */
2135 query_if_trace_running (int from_tty)
2140 /* It can happen that the target that was tracing went away on its
2141 own, and we didn't notice. Get a status update, and if the
2142 current target doesn't even do tracing, then assume it's not
2144 if (target_get_trace_status (current_trace_status ()) < 0)
2145 current_trace_status ()->running = 0;
2147 /* If running interactively, give the user the option to cancel and
2148 then decide what to do differently with the run. Scripts are
2149 just going to disconnect and let the target deal with it,
2150 according to how it's been instructed previously via
2151 disconnected-tracing. */
2152 if (current_trace_status ()->running)
2154 process_tracepoint_on_disconnect ();
2156 if (current_trace_status ()->disconnected_tracing)
2158 if (!query (_("Trace is running and will "
2159 "continue after detach; detach anyway? ")))
2160 error (_("Not confirmed."));
2164 if (!query (_("Trace is running but will "
2165 "stop on detach; detach anyway? ")))
2166 error (_("Not confirmed."));
2171 /* This function handles the details of what to do about an ongoing
2172 tracing run if the user has asked to detach or otherwise disconnect
2176 disconnect_tracing (void)
2178 /* Also we want to be out of tfind mode, otherwise things can get
2179 confusing upon reconnection. Just use these calls instead of
2180 full tfind_1 behavior because we're in the middle of detaching,
2181 and there's no point to updating current stack frame etc. */
2182 trace_reset_local_state ();
2185 /* Worker function for the various flavors of the tfind command. */
2187 tfind_1 (enum trace_find_type type, int num,
2188 CORE_ADDR addr1, CORE_ADDR addr2,
2191 int target_frameno = -1, target_tracept = -1;
2192 struct frame_id old_frame_id = null_frame_id;
2193 struct tracepoint *tp;
2194 struct ui_out *uiout = current_uiout;
2196 /* Only try to get the current stack frame if we have a chance of
2197 succeeding. In particular, if we're trying to get a first trace
2198 frame while all threads are running, it's not going to succeed,
2199 so leave it with a default value and let the frame comparison
2200 below (correctly) decide to print out the source location of the
2202 if (!(type == tfind_number && num == -1)
2203 && (has_stack_frames () || traceframe_number >= 0))
2204 old_frame_id = get_frame_id (get_current_frame ());
2206 target_frameno = target_trace_find (type, num, addr1, addr2,
2209 if (type == tfind_number
2211 && target_frameno == -1)
2213 /* We told the target to get out of tfind mode, and it did. */
2215 else if (target_frameno == -1)
2217 /* A request for a non-existent trace frame has failed.
2218 Our response will be different, depending on FROM_TTY:
2220 If FROM_TTY is true, meaning that this command was
2221 typed interactively by the user, then give an error
2222 and DO NOT change the state of traceframe_number etc.
2224 However if FROM_TTY is false, meaning that we're either
2225 in a script, a loop, or a user-defined command, then
2226 DON'T give an error, but DO change the state of
2227 traceframe_number etc. to invalid.
2229 The rationalle is that if you typed the command, you
2230 might just have committed a typo or something, and you'd
2231 like to NOT lose your current debugging state. However
2232 if you're in a user-defined command or especially in a
2233 loop, then you need a way to detect that the command
2234 failed WITHOUT aborting. This allows you to write
2235 scripts that search thru the trace buffer until the end,
2236 and then continue on to do something else. */
2239 error (_("Target failed to find requested trace frame."));
2243 printf_filtered ("End of trace buffer.\n");
2244 #if 0 /* dubious now? */
2245 /* The following will not recurse, since it's
2247 tfind_command ("-1", from_tty);
2252 tp = get_tracepoint_by_number_on_target (target_tracept);
2254 reinit_frame_cache ();
2255 target_dcache_invalidate ();
2257 set_tracepoint_num (tp ? tp->base.number : target_tracept);
2259 if (target_frameno != get_traceframe_number ())
2260 observer_notify_traceframe_changed (target_frameno, tracepoint_number);
2262 set_current_traceframe (target_frameno);
2264 if (target_frameno == -1)
2265 set_traceframe_context (NULL);
2267 set_traceframe_context (get_current_frame ());
2269 if (traceframe_number >= 0)
2271 /* Use different branches for MI and CLI to make CLI messages
2273 if (uiout->is_mi_like_p ())
2275 uiout->field_string ("found", "1");
2276 uiout->field_int ("tracepoint", tracepoint_number);
2277 uiout->field_int ("traceframe", traceframe_number);
2281 printf_unfiltered (_("Found trace frame %d, tracepoint %d\n"),
2282 traceframe_number, tracepoint_number);
2287 if (uiout->is_mi_like_p ())
2288 uiout->field_string ("found", "0");
2289 else if (type == tfind_number && num == -1)
2290 printf_unfiltered (_("No longer looking at any trace frame\n"));
2291 else /* This case may never occur, check. */
2292 printf_unfiltered (_("No trace frame found\n"));
2295 /* If we're in nonstop mode and getting out of looking at trace
2296 frames, there won't be any current frame to go back to and
2299 && (has_stack_frames () || traceframe_number >= 0))
2301 enum print_what print_what;
2303 /* NOTE: in imitation of the step command, try to determine
2304 whether we have made a transition from one function to
2305 another. If so, we'll print the "stack frame" (ie. the new
2306 function and it's arguments) -- otherwise we'll just show the
2309 if (frame_id_eq (old_frame_id,
2310 get_frame_id (get_current_frame ())))
2311 print_what = SRC_LINE;
2313 print_what = SRC_AND_LOC;
2315 print_stack_frame (get_selected_frame (NULL), 1, print_what, 1);
2320 /* Error on looking at traceframes while trace is running. */
2323 check_trace_running (struct trace_status *status)
2325 if (status->running && status->filename == NULL)
2326 error (_("May not look at trace frames while trace is running."));
2329 /* trace_find_command takes a trace frame number n,
2330 sends "QTFrame:<n>" to the target,
2331 and accepts a reply that may contain several optional pieces
2332 of information: a frame number, a tracepoint number, and an
2333 indication of whether this is a trap frame or a stepping frame.
2335 The minimal response is just "OK" (which indicates that the
2336 target does not give us a frame number or a tracepoint number).
2337 Instead of that, the target may send us a string containing
2339 F<hexnum> (gives the selected frame number)
2340 T<hexnum> (gives the selected tracepoint number)
2345 tfind_command_1 (const char *args, int from_tty)
2346 { /* This should only be called with a numeric argument. */
2349 check_trace_running (current_trace_status ());
2351 if (args == 0 || *args == 0)
2352 { /* TFIND with no args means find NEXT trace frame. */
2353 if (traceframe_number == -1)
2354 frameno = 0; /* "next" is first one. */
2356 frameno = traceframe_number + 1;
2358 else if (0 == strcmp (args, "-"))
2360 if (traceframe_number == -1)
2361 error (_("not debugging trace buffer"));
2362 else if (from_tty && traceframe_number == 0)
2363 error (_("already at start of trace buffer"));
2365 frameno = traceframe_number - 1;
2367 /* A hack to work around eval's need for fp to have been collected. */
2368 else if (0 == strcmp (args, "-1"))
2371 frameno = parse_and_eval_long (args);
2374 error (_("invalid input (%d is less than zero)"), frameno);
2376 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
2380 tfind_command (char *args, int from_tty)
2382 tfind_command_1 (const_cast<char *> (args), from_tty);
2387 tfind_end_command (char *args, int from_tty)
2389 tfind_command_1 ("-1", from_tty);
2394 tfind_start_command (char *args, int from_tty)
2396 tfind_command_1 ("0", from_tty);
2399 /* tfind pc command */
2401 tfind_pc_command (char *args, int from_tty)
2405 check_trace_running (current_trace_status ());
2407 if (args == 0 || *args == 0)
2408 pc = regcache_read_pc (get_current_regcache ());
2410 pc = parse_and_eval_address (args);
2412 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
2415 /* tfind tracepoint command */
2417 tfind_tracepoint_command (char *args, int from_tty)
2420 struct tracepoint *tp;
2422 check_trace_running (current_trace_status ());
2424 if (args == 0 || *args == 0)
2426 if (tracepoint_number == -1)
2427 error (_("No current tracepoint -- please supply an argument."));
2429 tdp = tracepoint_number; /* Default is current TDP. */
2432 tdp = parse_and_eval_long (args);
2434 /* If we have the tracepoint on hand, use the number that the
2435 target knows about (which may be different if we disconnected
2436 and reconnected). */
2437 tp = get_tracepoint (tdp);
2439 tdp = tp->number_on_target;
2441 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
2444 /* TFIND LINE command:
2446 This command will take a sourceline for argument, just like BREAK
2447 or TRACE (ie. anything that "decode_line_1" can handle).
2449 With no argument, this command will find the next trace frame
2450 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2453 tfind_line_command (char *args, int from_tty)
2455 static CORE_ADDR start_pc, end_pc;
2456 struct symtabs_and_lines sals;
2457 struct symtab_and_line sal;
2458 struct cleanup *old_chain;
2460 check_trace_running (current_trace_status ());
2462 if (args == 0 || *args == 0)
2464 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
2466 sals.sals = XNEW (struct symtab_and_line);
2471 sals = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
2475 old_chain = make_cleanup (xfree, sals.sals);
2476 if (sal.symtab == 0)
2477 error (_("No line number information available."));
2479 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2481 if (start_pc == end_pc)
2483 printf_filtered ("Line %d of \"%s\"",
2485 symtab_to_filename_for_display (sal.symtab));
2487 printf_filtered (" is at address ");
2488 print_address (get_current_arch (), start_pc, gdb_stdout);
2490 printf_filtered (" but contains no code.\n");
2491 sal = find_pc_line (start_pc, 0);
2493 && find_line_pc_range (sal, &start_pc, &end_pc)
2494 && start_pc != end_pc)
2495 printf_filtered ("Attempting to find line %d instead.\n",
2498 error (_("Cannot find a good line."));
2502 /* Is there any case in which we get here, and have an address
2503 which the user would want to see? If we have debugging
2504 symbols and no line numbers? */
2505 error (_("Line number %d is out of range for \"%s\"."),
2506 sal.line, symtab_to_filename_for_display (sal.symtab));
2508 /* Find within range of stated line. */
2510 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
2512 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
2513 do_cleanups (old_chain);
2516 /* tfind range command */
2518 tfind_range_command (char *args, int from_tty)
2520 static CORE_ADDR start, stop;
2523 check_trace_running (current_trace_status ());
2525 if (args == 0 || *args == 0)
2526 { /* XXX FIXME: what should default behavior be? */
2527 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2531 if (0 != (tmp = strchr (args, ',')))
2533 *tmp++ = '\0'; /* Terminate start address. */
2534 tmp = skip_spaces (tmp);
2535 start = parse_and_eval_address (args);
2536 stop = parse_and_eval_address (tmp);
2539 { /* No explicit end address? */
2540 start = parse_and_eval_address (args);
2541 stop = start + 1; /* ??? */
2544 tfind_1 (tfind_range, 0, start, stop, from_tty);
2547 /* tfind outside command */
2549 tfind_outside_command (char *args, int from_tty)
2551 CORE_ADDR start, stop;
2554 if (current_trace_status ()->running
2555 && current_trace_status ()->filename == NULL)
2556 error (_("May not look at trace frames while trace is running."));
2558 if (args == 0 || *args == 0)
2559 { /* XXX FIXME: what should default behavior be? */
2560 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2564 if (0 != (tmp = strchr (args, ',')))
2566 *tmp++ = '\0'; /* Terminate start address. */
2567 tmp = skip_spaces (tmp);
2568 start = parse_and_eval_address (args);
2569 stop = parse_and_eval_address (tmp);
2572 { /* No explicit end address? */
2573 start = parse_and_eval_address (args);
2574 stop = start + 1; /* ??? */
2577 tfind_1 (tfind_outside, 0, start, stop, from_tty);
2580 /* info scope command: list the locals for a scope. */
2582 scope_info (char *args, int from_tty)
2584 struct symtabs_and_lines sals;
2586 struct bound_minimal_symbol msym;
2587 const struct block *block;
2588 const char *symname;
2589 char *save_args = args;
2590 struct block_iterator iter;
2592 struct gdbarch *gdbarch;
2595 if (args == 0 || *args == 0)
2596 error (_("requires an argument (function, "
2597 "line or *addr) to define a scope"));
2599 event_location_up location = string_to_event_location (&args,
2601 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
2603 if (sals.nelts == 0)
2605 /* Presumably decode_line_1 has already warned. */
2609 /* Resolve line numbers to PC. */
2610 resolve_sal_pc (&sals.sals[0]);
2611 block = block_for_pc (sals.sals[0].pc);
2615 QUIT; /* Allow user to bail out with ^C. */
2616 ALL_BLOCK_SYMBOLS (block, iter, sym)
2618 QUIT; /* Allow user to bail out with ^C. */
2620 printf_filtered ("Scope for %s:\n", save_args);
2623 symname = SYMBOL_PRINT_NAME (sym);
2624 if (symname == NULL || *symname == '\0')
2625 continue; /* Probably botched, certainly useless. */
2627 gdbarch = symbol_arch (sym);
2629 printf_filtered ("Symbol %s is ", symname);
2631 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
2632 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym,
2633 BLOCK_START (block),
2637 switch (SYMBOL_CLASS (sym))
2640 case LOC_UNDEF: /* Messed up symbol? */
2641 printf_filtered ("a bogus symbol, class %d.\n",
2642 SYMBOL_CLASS (sym));
2643 count--; /* Don't count this one. */
2646 printf_filtered ("a constant with value %s (%s)",
2647 plongest (SYMBOL_VALUE (sym)),
2648 hex_string (SYMBOL_VALUE (sym)));
2650 case LOC_CONST_BYTES:
2651 printf_filtered ("constant bytes: ");
2652 if (SYMBOL_TYPE (sym))
2653 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2654 fprintf_filtered (gdb_stdout, " %02x",
2655 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2658 printf_filtered ("in static storage at address ");
2659 printf_filtered ("%s", paddress (gdbarch,
2660 SYMBOL_VALUE_ADDRESS (sym)));
2663 /* GDBARCH is the architecture associated with the objfile
2664 the symbol is defined in; the target architecture may be
2665 different, and may provide additional registers. However,
2666 we do not know the target architecture at this point.
2667 We assume the objfile architecture will contain all the
2668 standard registers that occur in debug info in that
2670 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2673 if (SYMBOL_IS_ARGUMENT (sym))
2674 printf_filtered ("an argument in register $%s",
2675 gdbarch_register_name (gdbarch, regno));
2677 printf_filtered ("a local variable in register $%s",
2678 gdbarch_register_name (gdbarch, regno));
2681 printf_filtered ("an argument at stack/frame offset %s",
2682 plongest (SYMBOL_VALUE (sym)));
2685 printf_filtered ("a local variable at frame offset %s",
2686 plongest (SYMBOL_VALUE (sym)));
2689 printf_filtered ("a reference argument at offset %s",
2690 plongest (SYMBOL_VALUE (sym)));
2692 case LOC_REGPARM_ADDR:
2693 /* Note comment at LOC_REGISTER. */
2694 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2696 printf_filtered ("the address of an argument, in register $%s",
2697 gdbarch_register_name (gdbarch, regno));
2700 printf_filtered ("a typedef.\n");
2703 printf_filtered ("a label at address ");
2704 printf_filtered ("%s", paddress (gdbarch,
2705 SYMBOL_VALUE_ADDRESS (sym)));
2708 printf_filtered ("a function at address ");
2709 printf_filtered ("%s",
2710 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
2712 case LOC_UNRESOLVED:
2713 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
2715 if (msym.minsym == NULL)
2716 printf_filtered ("Unresolved Static");
2719 printf_filtered ("static storage at address ");
2720 printf_filtered ("%s",
2722 BMSYMBOL_VALUE_ADDRESS (msym)));
2725 case LOC_OPTIMIZED_OUT:
2726 printf_filtered ("optimized out.\n");
2729 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
2732 if (SYMBOL_TYPE (sym))
2733 printf_filtered (", length %d.\n",
2734 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
2736 if (BLOCK_FUNCTION (block))
2739 block = BLOCK_SUPERBLOCK (block);
2742 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2746 /* Helper for trace_dump_command. Dump the action list starting at
2747 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2748 actions of the body of a while-stepping action. STEPPING_FRAME is
2749 set if the current traceframe was determined to be a while-stepping
2753 trace_dump_actions (struct command_line *action,
2754 int stepping_actions, int stepping_frame,
2757 const char *action_exp, *next_comma;
2759 for (; action != NULL; action = action->next)
2761 struct cmd_list_element *cmd;
2763 QUIT; /* Allow user to bail out with ^C. */
2764 action_exp = action->line;
2765 action_exp = skip_spaces_const (action_exp);
2767 /* The collection actions to be done while stepping are
2768 bracketed by the commands "while-stepping" and "end". */
2770 if (*action_exp == '#') /* comment line */
2773 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2775 error (_("Bad action list item: %s"), action_exp);
2777 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
2781 for (i = 0; i < action->body_count; ++i)
2782 trace_dump_actions (action->body_list[i],
2783 1, stepping_frame, from_tty);
2785 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
2787 /* Display the collected data.
2788 For the trap frame, display only what was collected at
2789 the trap. Likewise for stepping frames, display only
2790 what was collected while stepping. This means that the
2791 two boolean variables, STEPPING_FRAME and
2792 STEPPING_ACTIONS should be equal. */
2793 if (stepping_frame == stepping_actions)
2796 struct cleanup *old_chain
2797 = make_cleanup (free_current_contents, &cmd);
2798 int trace_string = 0;
2800 if (*action_exp == '/')
2801 action_exp = decode_agent_options (action_exp, &trace_string);
2804 { /* Repeat over a comma-separated list. */
2805 QUIT; /* Allow user to bail out with ^C. */
2806 if (*action_exp == ',')
2808 action_exp = skip_spaces_const (action_exp);
2810 next_comma = strchr (action_exp, ',');
2812 if (0 == strncasecmp (action_exp, "$reg", 4))
2813 registers_info (NULL, from_tty);
2814 else if (0 == strncasecmp (action_exp, "$_ret", 5))
2816 else if (0 == strncasecmp (action_exp, "$loc", 4))
2817 locals_info (NULL, from_tty);
2818 else if (0 == strncasecmp (action_exp, "$arg", 4))
2819 args_info (NULL, from_tty);
2822 if (next_comma != NULL)
2824 size_t len = next_comma - action_exp;
2826 cmd = (char *) xrealloc (cmd, len + 1);
2827 memcpy (cmd, action_exp, len);
2832 size_t len = strlen (action_exp);
2834 cmd = (char *) xrealloc (cmd, len + 1);
2835 memcpy (cmd, action_exp, len + 1);
2838 printf_filtered ("%s = ", cmd);
2839 output_command_const (cmd, from_tty);
2840 printf_filtered ("\n");
2842 action_exp = next_comma;
2844 while (action_exp && *action_exp == ',');
2846 do_cleanups (old_chain);
2852 /* Return bp_location of the tracepoint associated with the current
2853 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
2854 is a stepping traceframe. */
2856 struct bp_location *
2857 get_traceframe_location (int *stepping_frame_p)
2859 struct tracepoint *t;
2860 struct bp_location *tloc;
2861 struct regcache *regcache;
2863 if (tracepoint_number == -1)
2864 error (_("No current trace frame."));
2866 t = get_tracepoint (tracepoint_number);
2869 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2872 /* The current frame is a trap frame if the frame PC is equal to the
2873 tracepoint PC. If not, then the current frame was collected
2874 during single-stepping. */
2875 regcache = get_current_regcache ();
2877 /* If the traceframe's address matches any of the tracepoint's
2878 locations, assume it is a direct hit rather than a while-stepping
2879 frame. (FIXME this is not reliable, should record each frame's
2881 for (tloc = t->base.loc; tloc; tloc = tloc->next)
2882 if (tloc->address == regcache_read_pc (regcache))
2884 *stepping_frame_p = 0;
2888 /* If this is a stepping frame, we don't know which location
2889 triggered. The first is as good (or bad) a guess as any... */
2890 *stepping_frame_p = 1;
2894 /* Return all the actions, including default collect, of a tracepoint
2895 T. It constructs cleanups into the chain, and leaves the caller to
2896 handle them (call do_cleanups). */
2898 static struct command_line *
2899 all_tracepoint_actions_and_cleanup (struct breakpoint *t)
2901 struct command_line *actions;
2903 actions = breakpoint_commands (t);
2905 /* If there are default expressions to collect, make up a collect
2906 action and prepend to the action list to encode. Note that since
2907 validation is per-tracepoint (local var "xyz" might be valid for
2908 one tracepoint and not another, etc), we make up the action on
2909 the fly, and don't cache it. */
2910 if (*default_collect)
2912 struct command_line *default_collect_action;
2913 char *default_collect_line;
2915 default_collect_line = xstrprintf ("collect %s", default_collect);
2916 make_cleanup (xfree, default_collect_line);
2918 validate_actionline (default_collect_line, t);
2919 default_collect_action = XNEW (struct command_line);
2920 make_cleanup (xfree, default_collect_action);
2921 default_collect_action->next = actions;
2922 default_collect_action->line = default_collect_line;
2923 actions = default_collect_action;
2929 /* The tdump command. */
2932 tdump_command (char *args, int from_tty)
2934 int stepping_frame = 0;
2935 struct bp_location *loc;
2936 struct cleanup *old_chain;
2937 struct command_line *actions;
2939 /* This throws an error is not inspecting a trace frame. */
2940 loc = get_traceframe_location (&stepping_frame);
2942 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2943 tracepoint_number, traceframe_number);
2945 /* This command only makes sense for the current frame, not the
2947 old_chain = make_cleanup_restore_current_thread ();
2948 select_frame (get_current_frame ());
2950 actions = all_tracepoint_actions_and_cleanup (loc->owner);
2952 trace_dump_actions (actions, 0, stepping_frame, from_tty);
2954 do_cleanups (old_chain);
2957 /* Encode a piece of a tracepoint's source-level definition in a form
2958 that is suitable for both protocol and saving in files. */
2959 /* This version does not do multiple encodes for long strings; it should
2960 return an offset to the next piece to encode. FIXME */
2963 encode_source_string (int tpnum, ULONGEST addr,
2964 const char *srctype, const char *src,
2965 char *buf, int buf_size)
2967 if (80 + strlen (srctype) > buf_size)
2968 error (_("Buffer too small for source encoding"));
2969 sprintf (buf, "%x:%s:%s:%x:%x:",
2970 tpnum, phex_nz (addr, sizeof (addr)),
2971 srctype, 0, (int) strlen (src));
2972 if (strlen (buf) + strlen (src) * 2 >= buf_size)
2973 error (_("Source string too long for buffer"));
2974 bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src));
2978 /* Tell the target what to do with an ongoing tracing run if GDB
2979 disconnects for some reason. */
2982 set_disconnected_tracing (char *args, int from_tty,
2983 struct cmd_list_element *c)
2985 target_set_disconnected_tracing (disconnected_tracing);
2989 set_circular_trace_buffer (char *args, int from_tty,
2990 struct cmd_list_element *c)
2992 target_set_circular_trace_buffer (circular_trace_buffer);
2996 set_trace_buffer_size (char *args, int from_tty,
2997 struct cmd_list_element *c)
2999 target_set_trace_buffer_size (trace_buffer_size);
3003 set_trace_user (char *args, int from_tty,
3004 struct cmd_list_element *c)
3008 ret = target_set_trace_notes (trace_user, NULL, NULL);
3011 warning (_("Target does not support trace notes, user ignored"));
3015 set_trace_notes (char *args, int from_tty,
3016 struct cmd_list_element *c)
3020 ret = target_set_trace_notes (NULL, trace_notes, NULL);
3023 warning (_("Target does not support trace notes, note ignored"));
3027 set_trace_stop_notes (char *args, int from_tty,
3028 struct cmd_list_element *c)
3032 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes);
3035 warning (_("Target does not support trace notes, stop note ignored"));
3038 /* Convert the memory pointed to by mem into hex, placing result in buf.
3039 * Return a pointer to the last char put in buf (null)
3040 * "stolen" from sparc-stub.c
3043 static const char hexchars[] = "0123456789abcdef";
3046 mem2hex (gdb_byte *mem, char *buf, int count)
3054 *buf++ = hexchars[ch >> 4];
3055 *buf++ = hexchars[ch & 0xf];
3064 get_traceframe_number (void)
3066 return traceframe_number;
3070 get_tracepoint_number (void)
3072 return tracepoint_number;
3075 /* Make the traceframe NUM be the current trace frame. Does nothing
3076 if NUM is already current. */
3079 set_current_traceframe (int num)
3083 if (traceframe_number == num)
3085 /* Nothing to do. */
3089 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
3092 warning (_("could not change traceframe"));
3094 set_traceframe_num (newnum);
3096 /* Changing the traceframe changes our view of registers and of the
3098 registers_changed ();
3100 clear_traceframe_info ();
3103 /* A cleanup used when switching away and back from tfind mode. */
3105 struct current_traceframe_cleanup
3107 /* The traceframe we were inspecting. */
3108 int traceframe_number;
3112 do_restore_current_traceframe_cleanup (void *arg)
3114 struct current_traceframe_cleanup *old
3115 = (struct current_traceframe_cleanup *) arg;
3117 set_current_traceframe (old->traceframe_number);
3121 restore_current_traceframe_cleanup_dtor (void *arg)
3123 struct current_traceframe_cleanup *old
3124 = (struct current_traceframe_cleanup *) arg;
3130 make_cleanup_restore_current_traceframe (void)
3132 struct current_traceframe_cleanup *old =
3133 XNEW (struct current_traceframe_cleanup);
3135 old->traceframe_number = traceframe_number;
3137 return make_cleanup_dtor (do_restore_current_traceframe_cleanup, old,
3138 restore_current_traceframe_cleanup_dtor);
3141 /* Given a number and address, return an uploaded tracepoint with that
3142 number, creating if necessary. */
3144 struct uploaded_tp *
3145 get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
3147 struct uploaded_tp *utp;
3149 for (utp = *utpp; utp; utp = utp->next)
3150 if (utp->number == num && utp->addr == addr)
3153 utp = XCNEW (struct uploaded_tp);
3156 utp->actions = NULL;
3157 utp->step_actions = NULL;
3158 utp->cmd_strings = NULL;
3166 free_uploaded_tps (struct uploaded_tp **utpp)
3168 struct uploaded_tp *next_one;
3172 next_one = (*utpp)->next;
3178 /* Given a number and address, return an uploaded tracepoint with that
3179 number, creating if necessary. */
3181 struct uploaded_tsv *
3182 get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
3184 struct uploaded_tsv *utsv;
3186 for (utsv = *utsvp; utsv; utsv = utsv->next)
3187 if (utsv->number == num)
3190 utsv = XCNEW (struct uploaded_tsv);
3192 utsv->next = *utsvp;
3199 free_uploaded_tsvs (struct uploaded_tsv **utsvp)
3201 struct uploaded_tsv *next_one;
3205 next_one = (*utsvp)->next;
3211 /* FIXME this function is heuristic and will miss the cases where the
3212 conditional is semantically identical but differs in whitespace,
3213 such as "x == 0" vs "x==0". */
3216 cond_string_is_same (char *str1, char *str2)
3218 if (str1 == NULL || str2 == NULL)
3219 return (str1 == str2);
3221 return (strcmp (str1, str2) == 0);
3224 /* Look for an existing tracepoint that seems similar enough to the
3225 uploaded one. Enablement isn't compared, because the user can
3226 toggle that freely, and may have done so in anticipation of the
3227 next trace run. Return the location of matched tracepoint. */
3229 static struct bp_location *
3230 find_matching_tracepoint_location (struct uploaded_tp *utp)
3232 VEC(breakpoint_p) *tp_vec = all_tracepoints ();
3234 struct breakpoint *b;
3235 struct bp_location *loc;
3237 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
3239 struct tracepoint *t = (struct tracepoint *) b;
3241 if (b->type == utp->type
3242 && t->step_count == utp->step
3243 && t->pass_count == utp->pass
3244 && cond_string_is_same (t->base.cond_string, utp->cond_string)
3245 /* FIXME also test actions. */
3248 /* Scan the locations for an address match. */
3249 for (loc = b->loc; loc; loc = loc->next)
3251 if (loc->address == utp->addr)
3259 /* Given a list of tracepoints uploaded from a target, attempt to
3260 match them up with existing tracepoints, and create new ones if not
3264 merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
3266 struct uploaded_tp *utp;
3267 /* A set of tracepoints which are modified. */
3268 VEC(breakpoint_p) *modified_tp = NULL;
3270 struct breakpoint *b;
3272 /* Look for GDB tracepoints that match up with our uploaded versions. */
3273 for (utp = *uploaded_tps; utp; utp = utp->next)
3275 struct bp_location *loc;
3276 struct tracepoint *t;
3278 loc = find_matching_tracepoint_location (utp);
3283 /* Mark this location as already inserted. */
3285 t = (struct tracepoint *) loc->owner;
3286 printf_filtered (_("Assuming tracepoint %d is same "
3287 "as target's tracepoint %d at %s.\n"),
3288 loc->owner->number, utp->number,
3289 paddress (loc->gdbarch, utp->addr));
3291 /* The tracepoint LOC->owner was modified (the location LOC
3292 was marked as inserted in the target). Save it in
3293 MODIFIED_TP if not there yet. The 'breakpoint-modified'
3294 observers will be notified later once for each tracepoint
3295 saved in MODIFIED_TP. */
3297 VEC_iterate (breakpoint_p, modified_tp, ix, b);
3299 if (b == loc->owner)
3305 VEC_safe_push (breakpoint_p, modified_tp, loc->owner);
3309 t = create_tracepoint_from_upload (utp);
3311 printf_filtered (_("Created tracepoint %d for "
3312 "target's tracepoint %d at %s.\n"),
3313 t->base.number, utp->number,
3314 paddress (get_current_arch (), utp->addr));
3316 printf_filtered (_("Failed to create tracepoint for target's "
3317 "tracepoint %d at %s, skipping it.\n"),
3319 paddress (get_current_arch (), utp->addr));
3321 /* Whether found or created, record the number used by the
3322 target, to help with mapping target tracepoints back to their
3323 counterparts here. */
3325 t->number_on_target = utp->number;
3328 /* Notify 'breakpoint-modified' observer that at least one of B's
3329 locations was changed. */
3330 for (ix = 0; VEC_iterate (breakpoint_p, modified_tp, ix, b); ix++)
3331 observer_notify_breakpoint_modified (b);
3333 VEC_free (breakpoint_p, modified_tp);
3334 free_uploaded_tps (uploaded_tps);
3337 /* Trace state variables don't have much to identify them beyond their
3338 name, so just use that to detect matches. */
3340 static struct trace_state_variable *
3341 find_matching_tsv (struct uploaded_tsv *utsv)
3346 return find_trace_state_variable (utsv->name);
3349 static struct trace_state_variable *
3350 create_tsv_from_upload (struct uploaded_tsv *utsv)
3352 const char *namebase;
3355 struct trace_state_variable *tsv;
3356 struct cleanup *old_chain;
3360 namebase = utsv->name;
3361 buf = xstrprintf ("%s", namebase);
3366 buf = xstrprintf ("%s_%d", namebase, try_num++);
3369 /* Fish for a name that is not in use. */
3370 /* (should check against all internal vars?) */
3371 while (find_trace_state_variable (buf))
3374 buf = xstrprintf ("%s_%d", namebase, try_num++);
3377 old_chain = make_cleanup (xfree, buf);
3379 /* We have an available name, create the variable. */
3380 tsv = create_trace_state_variable (buf);
3381 tsv->initial_value = utsv->initial_value;
3382 tsv->builtin = utsv->builtin;
3384 observer_notify_tsv_created (tsv);
3386 do_cleanups (old_chain);
3391 /* Given a list of uploaded trace state variables, try to match them
3392 up with existing variables, or create additional ones. */
3395 merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
3398 struct uploaded_tsv *utsv;
3399 struct trace_state_variable *tsv;
3402 /* Most likely some numbers will have to be reassigned as part of
3403 the merge, so clear them all in anticipation. */
3404 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3407 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
3409 tsv = find_matching_tsv (utsv);
3413 printf_filtered (_("Assuming trace state variable $%s "
3414 "is same as target's variable %d.\n"),
3415 tsv->name, utsv->number);
3419 tsv = create_tsv_from_upload (utsv);
3421 printf_filtered (_("Created trace state variable "
3422 "$%s for target's variable %d.\n"),
3423 tsv->name, utsv->number);
3425 /* Give precedence to numberings that come from the target. */
3427 tsv->number = utsv->number;
3430 /* Renumber everything that didn't get a target-assigned number. */
3432 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3433 if (tsv->number > highest)
3434 highest = tsv->number;
3437 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3438 if (tsv->number == 0)
3439 tsv->number = highest++;
3441 free_uploaded_tsvs (uploaded_tsvs);
3444 /* Parse the part of trace status syntax that is shared between
3445 the remote protocol and the trace file reader. */
3448 parse_trace_status (char *line, struct trace_status *ts)
3450 char *p = line, *p1, *p2, *p3, *p_temp;
3454 ts->running_known = 1;
3455 ts->running = (*p++ == '1');
3456 ts->stop_reason = trace_stop_reason_unknown;
3457 xfree (ts->stop_desc);
3458 ts->stop_desc = NULL;
3459 ts->traceframe_count = -1;
3460 ts->traceframes_created = -1;
3461 ts->buffer_free = -1;
3462 ts->buffer_size = -1;
3463 ts->disconnected_tracing = 0;
3464 ts->circular_buffer = 0;
3465 xfree (ts->user_name);
3466 ts->user_name = NULL;
3469 ts->start_time = ts->stop_time = 0;
3473 p1 = strchr (p, ':');
3475 error (_("Malformed trace status, at %s\n\
3476 Status line: '%s'\n"), p, line);
3477 p3 = strchr (p, ';');
3479 p3 = p + strlen (p);
3480 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
3482 p = unpack_varlen_hex (++p1, &val);
3483 ts->stop_reason = trace_buffer_full;
3485 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
3487 p = unpack_varlen_hex (++p1, &val);
3488 ts->stop_reason = trace_never_run;
3490 else if (strncmp (p, stop_reason_names[tracepoint_passcount],
3493 p = unpack_varlen_hex (++p1, &val);
3494 ts->stop_reason = tracepoint_passcount;
3495 ts->stopping_tracepoint = val;
3497 else if (strncmp (p, stop_reason_names[trace_stop_command], p1 - p) == 0)
3499 p2 = strchr (++p1, ':');
3507 ts->stop_desc = (char *) xmalloc (strlen (line));
3508 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3509 ts->stop_desc[end] = '\0';
3512 ts->stop_desc = xstrdup ("");
3514 p = unpack_varlen_hex (++p2, &val);
3515 ts->stop_reason = trace_stop_command;
3517 else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0)
3519 p = unpack_varlen_hex (++p1, &val);
3520 ts->stop_reason = trace_disconnected;
3522 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
3524 p2 = strchr (++p1, ':');
3527 ts->stop_desc = (char *) xmalloc ((p2 - p1) / 2 + 1);
3528 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3529 ts->stop_desc[end] = '\0';
3532 ts->stop_desc = xstrdup ("");
3534 p = unpack_varlen_hex (++p2, &val);
3535 ts->stopping_tracepoint = val;
3536 ts->stop_reason = tracepoint_error;
3538 else if (strncmp (p, "tframes", p1 - p) == 0)
3540 p = unpack_varlen_hex (++p1, &val);
3541 ts->traceframe_count = val;
3543 else if (strncmp (p, "tcreated", p1 - p) == 0)
3545 p = unpack_varlen_hex (++p1, &val);
3546 ts->traceframes_created = val;
3548 else if (strncmp (p, "tfree", p1 - p) == 0)
3550 p = unpack_varlen_hex (++p1, &val);
3551 ts->buffer_free = val;
3553 else if (strncmp (p, "tsize", p1 - p) == 0)
3555 p = unpack_varlen_hex (++p1, &val);
3556 ts->buffer_size = val;
3558 else if (strncmp (p, "disconn", p1 - p) == 0)
3560 p = unpack_varlen_hex (++p1, &val);
3561 ts->disconnected_tracing = val;
3563 else if (strncmp (p, "circular", p1 - p) == 0)
3565 p = unpack_varlen_hex (++p1, &val);
3566 ts->circular_buffer = val;
3568 else if (strncmp (p, "starttime", p1 - p) == 0)
3570 p = unpack_varlen_hex (++p1, &val);
3571 ts->start_time = val;
3573 else if (strncmp (p, "stoptime", p1 - p) == 0)
3575 p = unpack_varlen_hex (++p1, &val);
3576 ts->stop_time = val;
3578 else if (strncmp (p, "username", p1 - p) == 0)
3581 ts->user_name = (char *) xmalloc (strlen (p) / 2);
3582 end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2);
3583 ts->user_name[end] = '\0';
3586 else if (strncmp (p, "notes", p1 - p) == 0)
3589 ts->notes = (char *) xmalloc (strlen (p) / 2);
3590 end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2);
3591 ts->notes[end] = '\0';
3596 /* Silently skip unknown optional info. */
3597 p_temp = strchr (p1 + 1, ';');
3601 /* Must be at the end. */
3608 parse_tracepoint_status (char *p, struct breakpoint *bp,
3609 struct uploaded_tp *utp)
3612 struct tracepoint *tp = (struct tracepoint *) bp;
3614 p = unpack_varlen_hex (p, &uval);
3616 tp->base.hit_count += uval;
3618 utp->hit_count += uval;
3619 p = unpack_varlen_hex (p + 1, &uval);
3621 tp->traceframe_usage += uval;
3623 utp->traceframe_usage += uval;
3624 /* Ignore any extra, allowing for future extensions. */
3627 /* Given a line of text defining a part of a tracepoint, parse it into
3628 an "uploaded tracepoint". */
3631 parse_tracepoint_definition (char *line, struct uploaded_tp **utpp)
3635 ULONGEST num, addr, step, pass, orig_size, xlen, start;
3638 char *cond, *srctype, *buf;
3639 struct uploaded_tp *utp = NULL;
3642 /* Both tracepoint and action definitions start with the same number
3643 and address sequence. */
3645 p = unpack_varlen_hex (p, &num);
3646 p++; /* skip a colon */
3647 p = unpack_varlen_hex (p, &addr);
3648 p++; /* skip a colon */
3651 enabled = (*p++ == 'E');
3652 p++; /* skip a colon */
3653 p = unpack_varlen_hex (p, &step);
3654 p++; /* skip a colon */
3655 p = unpack_varlen_hex (p, &pass);
3656 type = bp_tracepoint;
3658 /* Thumb through optional fields. */
3661 p++; /* skip a colon */
3664 type = bp_fast_tracepoint;
3666 p = unpack_varlen_hex (p, &orig_size);
3670 type = bp_static_tracepoint;
3676 p = unpack_varlen_hex (p, &xlen);
3677 p++; /* skip a comma */
3678 cond = (char *) xmalloc (2 * xlen + 1);
3679 strncpy (cond, p, 2 * xlen);
3680 cond[2 * xlen] = '\0';
3684 warning (_("Unrecognized char '%c' in tracepoint "
3685 "definition, skipping rest"), *p);
3687 utp = get_uploaded_tp (num, addr, utpp);
3689 utp->enabled = enabled;
3694 else if (piece == 'A')
3696 utp = get_uploaded_tp (num, addr, utpp);
3697 VEC_safe_push (char_ptr, utp->actions, xstrdup (p));
3699 else if (piece == 'S')
3701 utp = get_uploaded_tp (num, addr, utpp);
3702 VEC_safe_push (char_ptr, utp->step_actions, xstrdup (p));
3704 else if (piece == 'Z')
3706 /* Parse a chunk of source form definition. */
3707 utp = get_uploaded_tp (num, addr, utpp);
3709 p = strchr (p, ':');
3710 p++; /* skip a colon */
3711 p = unpack_varlen_hex (p, &start);
3712 p++; /* skip a colon */
3713 p = unpack_varlen_hex (p, &xlen);
3714 p++; /* skip a colon */
3716 buf = (char *) alloca (strlen (line));
3718 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3721 if (startswith (srctype, "at:"))
3722 utp->at_string = xstrdup (buf);
3723 else if (startswith (srctype, "cond:"))
3724 utp->cond_string = xstrdup (buf);
3725 else if (startswith (srctype, "cmd:"))
3726 VEC_safe_push (char_ptr, utp->cmd_strings, xstrdup (buf));
3728 else if (piece == 'V')
3730 utp = get_uploaded_tp (num, addr, utpp);
3732 parse_tracepoint_status (p, NULL, utp);
3736 /* Don't error out, the target might be sending us optional
3737 info that we don't care about. */
3738 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
3742 /* Convert a textual description of a trace state variable into an
3746 parse_tsv_definition (char *line, struct uploaded_tsv **utsvp)
3749 ULONGEST num, initval, builtin;
3751 struct uploaded_tsv *utsv = NULL;
3753 buf = (char *) alloca (strlen (line));
3756 p = unpack_varlen_hex (p, &num);
3757 p++; /* skip a colon */
3758 p = unpack_varlen_hex (p, &initval);
3759 p++; /* skip a colon */
3760 p = unpack_varlen_hex (p, &builtin);
3761 p++; /* skip a colon */
3762 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3765 utsv = get_uploaded_tsv (num, utsvp);
3766 utsv->initial_value = initval;
3767 utsv->builtin = builtin;
3768 utsv->name = xstrdup (buf);
3772 free_current_marker (void *arg)
3774 struct static_tracepoint_marker **marker_p
3775 = (struct static_tracepoint_marker **) arg;
3777 if (*marker_p != NULL)
3779 release_static_tracepoint_marker (*marker_p);
3786 /* Given a line of text defining a static tracepoint marker, parse it
3787 into a "static tracepoint marker" object. Throws an error is
3788 parsing fails. If PP is non-null, it points to one past the end of
3789 the parsed marker definition. */
3792 parse_static_tracepoint_marker_definition (char *line, char **pp,
3793 struct static_tracepoint_marker *marker)
3800 p = unpack_varlen_hex (p, &addr);
3801 p++; /* skip a colon */
3803 marker->gdbarch = target_gdbarch ();
3804 marker->address = (CORE_ADDR) addr;
3806 endp = strchr (p, ':');
3808 error (_("bad marker definition: %s"), line);
3810 marker->str_id = (char *) xmalloc (endp - p + 1);
3811 end = hex2bin (p, (gdb_byte *) marker->str_id, (endp - p + 1) / 2);
3812 marker->str_id[end] = '\0';
3815 p++; /* skip a colon */
3817 marker->extra = (char *) xmalloc (strlen (p) + 1);
3818 end = hex2bin (p, (gdb_byte *) marker->extra, strlen (p) / 2);
3819 marker->extra[end] = '\0';
3825 /* Release a static tracepoint marker's contents. Note that the
3826 object itself isn't released here. There objects are usually on
3830 release_static_tracepoint_marker (struct static_tracepoint_marker *marker)
3832 xfree (marker->str_id);
3833 marker->str_id = NULL;
3836 /* Print MARKER to gdb_stdout. */
3839 print_one_static_tracepoint_marker (int count,
3840 struct static_tracepoint_marker *marker)
3844 char wrap_indent[80];
3845 char extra_field_indent[80];
3846 struct ui_out *uiout = current_uiout;
3847 struct cleanup *bkpt_chain;
3848 VEC(breakpoint_p) *tracepoints;
3850 struct symtab_and_line sal;
3854 sal.pc = marker->address;
3856 tracepoints = static_tracepoints_here (marker->address);
3858 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "marker");
3860 /* A counter field to help readability. This is not a stable
3862 uiout->field_int ("count", count);
3864 uiout->field_string ("marker-id", marker->str_id);
3866 uiout->field_fmt ("enabled", "%c",
3867 !VEC_empty (breakpoint_p, tracepoints) ? 'y' : 'n');
3870 strcpy (wrap_indent, " ");
3872 if (gdbarch_addr_bit (marker->gdbarch) <= 32)
3873 strcat (wrap_indent, " ");
3875 strcat (wrap_indent, " ");
3877 strcpy (extra_field_indent, " ");
3879 uiout->field_core_addr ("addr", marker->gdbarch, marker->address);
3881 sal = find_pc_line (marker->address, 0);
3882 sym = find_pc_sect_function (marker->address, NULL);
3885 uiout->text ("in ");
3886 uiout->field_string ("func",
3887 SYMBOL_PRINT_NAME (sym));
3888 uiout->wrap_hint (wrap_indent);
3889 uiout->text (" at ");
3892 uiout->field_skip ("func");
3894 if (sal.symtab != NULL)
3896 uiout->field_string ("file",
3897 symtab_to_filename_for_display (sal.symtab));
3900 if (uiout->is_mi_like_p ())
3902 const char *fullname = symtab_to_fullname (sal.symtab);
3904 uiout->field_string ("fullname", fullname);
3907 uiout->field_skip ("fullname");
3909 uiout->field_int ("line", sal.line);
3913 uiout->field_skip ("fullname");
3914 uiout->field_skip ("line");
3918 uiout->text (extra_field_indent);
3919 uiout->text (_("Data: \""));
3920 uiout->field_string ("extra-data", marker->extra);
3921 uiout->text ("\"\n");
3923 if (!VEC_empty (breakpoint_p, tracepoints))
3925 struct cleanup *cleanup_chain;
3927 struct breakpoint *b;
3929 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout,
3932 uiout->text (extra_field_indent);
3933 uiout->text (_("Probed by static tracepoints: "));
3934 for (ix = 0; VEC_iterate(breakpoint_p, tracepoints, ix, b); ix++)
3939 uiout->field_int ("tracepoint-id", b->number);
3942 do_cleanups (cleanup_chain);
3944 if (uiout->is_mi_like_p ())
3945 uiout->field_int ("number-of-tracepoints",
3946 VEC_length(breakpoint_p, tracepoints));
3950 VEC_free (breakpoint_p, tracepoints);
3952 do_cleanups (bkpt_chain);
3956 info_static_tracepoint_markers_command (char *arg, int from_tty)
3958 VEC(static_tracepoint_marker_p) *markers;
3959 struct cleanup *old_chain;
3960 struct static_tracepoint_marker *marker;
3961 struct ui_out *uiout = current_uiout;
3964 /* We don't have to check target_can_use_agent and agent's capability on
3965 static tracepoint here, in order to be compatible with older GDBserver.
3966 We don't check USE_AGENT is true or not, because static tracepoints
3967 don't work without in-process agent, so we don't bother users to type
3968 `set agent on' when to use static tracepoint. */
3971 = make_cleanup_ui_out_table_begin_end (uiout, 5, -1,
3972 "StaticTracepointMarkersTable");
3974 uiout->table_header (7, ui_left, "counter", "Cnt");
3976 uiout->table_header (40, ui_left, "marker-id", "ID");
3978 uiout->table_header (3, ui_left, "enabled", "Enb");
3979 if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
3980 uiout->table_header (10, ui_left, "addr", "Address");
3982 uiout->table_header (18, ui_left, "addr", "Address");
3983 uiout->table_header (40, ui_noalign, "what", "What");
3985 uiout->table_body ();
3987 markers = target_static_tracepoint_markers_by_strid (NULL);
3988 make_cleanup (VEC_cleanup (static_tracepoint_marker_p), &markers);
3991 VEC_iterate (static_tracepoint_marker_p,
3992 markers, i, marker);
3995 print_one_static_tracepoint_marker (i + 1, marker);
3996 release_static_tracepoint_marker (marker);
3999 do_cleanups (old_chain);
4002 /* The $_sdata convenience variable is a bit special. We don't know
4003 for sure type of the value until we actually have a chance to fetch
4004 the data --- the size of the object depends on what has been
4005 collected. We solve this by making $_sdata be an internalvar that
4006 creates a new value on access. */
4008 /* Return a new value with the correct type for the sdata object of
4009 the current trace frame. Return a void value if there's no object
4012 static struct value *
4013 sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var,
4019 /* We need to read the whole object before we know its size. */
4020 size = target_read_alloc (¤t_target,
4021 TARGET_OBJECT_STATIC_TRACE_DATA,
4028 type = init_vector_type (builtin_type (gdbarch)->builtin_true_char,
4030 v = allocate_value (type);
4031 memcpy (value_contents_raw (v), buf, size);
4036 return allocate_value (builtin_type (gdbarch)->builtin_void);
4039 #if !defined(HAVE_LIBEXPAT)
4041 struct traceframe_info *
4042 parse_traceframe_info (const char *tframe_info)
4044 static int have_warned;
4049 warning (_("Can not parse XML trace frame info; XML support "
4050 "was disabled at compile time"));
4056 #else /* HAVE_LIBEXPAT */
4058 #include "xml-support.h"
4060 /* Handle the start of a <memory> element. */
4063 traceframe_info_start_memory (struct gdb_xml_parser *parser,
4064 const struct gdb_xml_element *element,
4065 void *user_data, VEC(gdb_xml_value_s) *attributes)
4067 struct traceframe_info *info = (struct traceframe_info *) user_data;
4068 struct mem_range *r = VEC_safe_push (mem_range_s, info->memory, NULL);
4069 ULONGEST *start_p, *length_p;
4072 = (ULONGEST *) xml_find_attribute (attributes, "start")->value;
4074 = (ULONGEST *) xml_find_attribute (attributes, "length")->value;
4076 r->start = *start_p;
4077 r->length = *length_p;
4080 /* Handle the start of a <tvar> element. */
4083 traceframe_info_start_tvar (struct gdb_xml_parser *parser,
4084 const struct gdb_xml_element *element,
4086 VEC(gdb_xml_value_s) *attributes)
4088 struct traceframe_info *info = (struct traceframe_info *) user_data;
4089 const char *id_attrib
4090 = (const char *) xml_find_attribute (attributes, "id")->value;
4091 int id = gdb_xml_parse_ulongest (parser, id_attrib);
4093 VEC_safe_push (int, info->tvars, id);
4096 /* Discard the constructed trace frame info (if an error occurs). */
4099 free_result (void *p)
4101 struct traceframe_info *result = (struct traceframe_info *) p;
4103 free_traceframe_info (result);
4106 /* The allowed elements and attributes for an XML memory map. */
4108 static const struct gdb_xml_attribute memory_attributes[] = {
4109 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
4110 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
4111 { NULL, GDB_XML_AF_NONE, NULL, NULL }
4114 static const struct gdb_xml_attribute tvar_attributes[] = {
4115 { "id", GDB_XML_AF_NONE, NULL, NULL },
4116 { NULL, GDB_XML_AF_NONE, NULL, NULL }
4119 static const struct gdb_xml_element traceframe_info_children[] = {
4120 { "memory", memory_attributes, NULL,
4121 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
4122 traceframe_info_start_memory, NULL },
4123 { "tvar", tvar_attributes, NULL,
4124 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
4125 traceframe_info_start_tvar, NULL },
4126 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
4129 static const struct gdb_xml_element traceframe_info_elements[] = {
4130 { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE,
4132 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
4135 /* Parse a traceframe-info XML document. */
4137 struct traceframe_info *
4138 parse_traceframe_info (const char *tframe_info)
4140 struct traceframe_info *result;
4141 struct cleanup *back_to;
4143 result = XCNEW (struct traceframe_info);
4144 back_to = make_cleanup (free_result, result);
4146 if (gdb_xml_parse_quick (_("trace frame info"),
4147 "traceframe-info.dtd", traceframe_info_elements,
4148 tframe_info, result) == 0)
4150 /* Parsed successfully, keep the result. */
4151 discard_cleanups (back_to);
4156 do_cleanups (back_to);
4160 #endif /* HAVE_LIBEXPAT */
4162 /* Returns the traceframe_info object for the current traceframe.
4163 This is where we avoid re-fetching the object from the target if we
4164 already have it cached. */
4166 struct traceframe_info *
4167 get_traceframe_info (void)
4169 if (traceframe_info == NULL)
4170 traceframe_info = target_traceframe_info ();
4172 return traceframe_info;
4175 /* If the target supports the query, return in RESULT the set of
4176 collected memory in the current traceframe, found within the LEN
4177 bytes range starting at MEMADDR. Returns true if the target
4178 supports the query, otherwise returns false, and RESULT is left
4182 traceframe_available_memory (VEC(mem_range_s) **result,
4183 CORE_ADDR memaddr, ULONGEST len)
4185 struct traceframe_info *info = get_traceframe_info ();
4189 struct mem_range *r;
4194 for (i = 0; VEC_iterate (mem_range_s, info->memory, i, r); i++)
4195 if (mem_ranges_overlap (r->start, r->length, memaddr, len))
4197 ULONGEST lo1, hi1, lo2, hi2;
4198 struct mem_range *nr;
4201 hi1 = memaddr + len;
4204 hi2 = r->start + r->length;
4206 nr = VEC_safe_push (mem_range_s, *result, NULL);
4208 nr->start = std::max (lo1, lo2);
4209 nr->length = std::min (hi1, hi2) - nr->start;
4212 normalize_mem_ranges (*result);
4219 /* Implementation of `sdata' variable. */
4221 static const struct internalvar_funcs sdata_funcs =
4228 /* module initialization */
4230 _initialize_tracepoint (void)
4232 struct cmd_list_element *c;
4234 /* Explicitly create without lookup, since that tries to create a
4235 value with a void typed value, and when we get here, gdbarch
4236 isn't initialized yet. At this point, we're quite sure there
4237 isn't another convenience variable of the same name. */
4238 create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL);
4240 traceframe_number = -1;
4241 tracepoint_number = -1;
4243 add_info ("scope", scope_info,
4244 _("List the variables local to a scope"));
4246 add_cmd ("tracepoints", class_trace, NULL,
4247 _("Tracing of program execution without stopping the program."),
4250 add_com ("tdump", class_trace, tdump_command,
4251 _("Print everything collected at the current tracepoint."));
4253 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
4254 Define a trace state variable.\n\
4255 Argument is a $-prefixed name, optionally followed\n\
4256 by '=' and an expression that sets the initial value\n\
4257 at the start of tracing."));
4258 set_cmd_completer (c, expression_completer);
4260 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
4261 Delete one or more trace state variables.\n\
4262 Arguments are the names of the variables to delete.\n\
4263 If no arguments are supplied, delete all variables."), &deletelist);
4264 /* FIXME add a trace variable completer. */
4266 add_info ("tvariables", tvariables_info, _("\
4267 Status of trace state variables and their values.\n\
4270 add_info ("static-tracepoint-markers",
4271 info_static_tracepoint_markers_command, _("\
4272 List target static tracepoints markers.\n\
4275 add_prefix_cmd ("tfind", class_trace, tfind_command, _("\
4276 Select a trace frame;\n\
4277 No argument means forward by one frame; '-' means backward by one frame."),
4278 &tfindlist, "tfind ", 1, &cmdlist);
4280 add_cmd ("outside", class_trace, tfind_outside_command, _("\
4281 Select a trace frame whose PC is outside the given range (exclusive).\n\
4282 Usage: tfind outside addr1, addr2"),
4285 add_cmd ("range", class_trace, tfind_range_command, _("\
4286 Select a trace frame whose PC is in the given range (inclusive).\n\
4287 Usage: tfind range addr1,addr2"),
4290 add_cmd ("line", class_trace, tfind_line_command, _("\
4291 Select a trace frame by source line.\n\
4292 Argument can be a line number (with optional source file),\n\
4293 a function name, or '*' followed by an address.\n\
4294 Default argument is 'the next source line that was traced'."),
4297 add_cmd ("tracepoint", class_trace, tfind_tracepoint_command, _("\
4298 Select a trace frame by tracepoint number.\n\
4299 Default is the tracepoint for the current trace frame."),
4302 add_cmd ("pc", class_trace, tfind_pc_command, _("\
4303 Select a trace frame by PC.\n\
4304 Default is the current PC, or the PC of the current trace frame."),
4307 add_cmd ("end", class_trace, tfind_end_command, _("\
4308 De-select any trace frame and resume 'live' debugging."),
4311 add_alias_cmd ("none", "end", class_trace, 0, &tfindlist);
4313 add_cmd ("start", class_trace, tfind_start_command,
4314 _("Select the first trace frame in the trace buffer."),
4317 add_com ("tstatus", class_trace, tstatus_command,
4318 _("Display the status of the current trace data collection."));
4320 add_com ("tstop", class_trace, tstop_command, _("\
4321 Stop trace data collection.\n\
4322 Usage: tstop [ <notes> ... ]\n\
4323 Any arguments supplied are recorded with the trace as a stop reason and\n\
4324 reported by tstatus (if the target supports trace notes)."));
4326 add_com ("tstart", class_trace, tstart_command, _("\
4327 Start trace data collection.\n\
4328 Usage: tstart [ <notes> ... ]\n\
4329 Any arguments supplied are recorded with the trace as a note and\n\
4330 reported by tstatus (if the target supports trace notes)."));
4332 add_com ("end", class_trace, end_actions_pseudocommand, _("\
4333 Ends a list of commands or actions.\n\
4334 Several GDB commands allow you to enter a list of commands or actions.\n\
4335 Entering \"end\" on a line by itself is the normal way to terminate\n\
4337 Note: the \"end\" command cannot be used at the gdb prompt."));
4339 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
4340 Specify single-stepping behavior at a tracepoint.\n\
4341 Argument is number of instructions to trace in single-step mode\n\
4342 following the tracepoint. This command is normally followed by\n\
4343 one or more \"collect\" commands, to specify what to collect\n\
4344 while single-stepping.\n\n\
4345 Note: this command can only be used in a tracepoint \"actions\" list."));
4347 add_com_alias ("ws", "while-stepping", class_alias, 0);
4348 add_com_alias ("stepping", "while-stepping", class_alias, 0);
4350 add_com ("collect", class_trace, collect_pseudocommand, _("\
4351 Specify one or more data items to be collected at a tracepoint.\n\
4352 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
4353 collect all data (variables, registers) referenced by that expression.\n\
4354 Also accepts the following special arguments:\n\
4355 $regs -- all registers.\n\
4356 $args -- all function arguments.\n\
4357 $locals -- all variables local to the block/function scope.\n\
4358 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
4359 Note: this command can only be used in a tracepoint \"actions\" list."));
4361 add_com ("teval", class_trace, teval_pseudocommand, _("\
4362 Specify one or more expressions to be evaluated at a tracepoint.\n\
4363 Accepts a comma-separated list of (one or more) expressions.\n\
4364 The result of each evaluation will be discarded.\n\
4365 Note: this command can only be used in a tracepoint \"actions\" list."));
4367 add_com ("actions", class_trace, actions_command, _("\
4368 Specify the actions to be taken at a tracepoint.\n\
4369 Tracepoint actions may include collecting of specified data,\n\
4370 single-stepping, or enabling/disabling other tracepoints,\n\
4371 depending on target's capabilities."));
4373 default_collect = xstrdup ("");
4374 add_setshow_string_cmd ("default-collect", class_trace,
4375 &default_collect, _("\
4376 Set the list of expressions to collect by default"), _("\
4377 Show the list of expressions to collect by default"), NULL,
4379 &setlist, &showlist);
4381 add_setshow_boolean_cmd ("disconnected-tracing", no_class,
4382 &disconnected_tracing, _("\
4383 Set whether tracing continues after GDB disconnects."), _("\
4384 Show whether tracing continues after GDB disconnects."), _("\
4385 Use this to continue a tracing run even if GDB disconnects\n\
4386 or detaches from the target. You can reconnect later and look at\n\
4387 trace data collected in the meantime."),
4388 set_disconnected_tracing,
4393 add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
4394 &circular_trace_buffer, _("\
4395 Set target's use of circular trace buffer."), _("\
4396 Show target's use of circular trace buffer."), _("\
4397 Use this to make the trace buffer into a circular buffer,\n\
4398 which will discard traceframes (oldest first) instead of filling\n\
4399 up and stopping the trace run."),
4400 set_circular_trace_buffer,
4405 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class,
4406 &trace_buffer_size, _("\
4407 Set requested size of trace buffer."), _("\
4408 Show requested size of trace buffer."), _("\
4409 Use this to choose a size for the trace buffer. Some targets\n\
4410 may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
4411 disables any attempt to set the buffer size and lets the target choose."),
4412 set_trace_buffer_size, NULL,
4413 &setlist, &showlist);
4415 add_setshow_string_cmd ("trace-user", class_trace,
4417 Set the user name to use for current and future trace runs"), _("\
4418 Show the user name to use for current and future trace runs"), NULL,
4419 set_trace_user, NULL,
4420 &setlist, &showlist);
4422 add_setshow_string_cmd ("trace-notes", class_trace,
4424 Set notes string to use for current and future trace runs"), _("\
4425 Show the notes string to use for current and future trace runs"), NULL,
4426 set_trace_notes, NULL,
4427 &setlist, &showlist);
4429 add_setshow_string_cmd ("trace-stop-notes", class_trace,
4430 &trace_stop_notes, _("\
4431 Set notes string to use for future tstop commands"), _("\
4432 Show the notes string to use for future tstop commands"), NULL,
4433 set_trace_stop_notes, NULL,
4434 &setlist, &showlist);