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 /* support routines */
170 struct collection_list;
171 static char *mem2hex (gdb_byte *, char *, int);
173 static struct command_line *
174 all_tracepoint_actions_and_cleanup (struct breakpoint *t);
176 static struct trace_status trace_status;
178 const char *stop_reason_names[] = {
188 struct trace_status *
189 current_trace_status (void)
191 return &trace_status;
197 free_traceframe_info (struct traceframe_info *info)
201 VEC_free (mem_range_s, info->memory);
202 VEC_free (int, info->tvars);
208 /* Free and clear the traceframe info cache of the current
212 clear_traceframe_info (void)
214 free_traceframe_info (traceframe_info);
215 traceframe_info = NULL;
218 /* Set traceframe number to NUM. */
220 set_traceframe_num (int num)
222 traceframe_number = num;
223 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
226 /* Set tracepoint number to NUM. */
228 set_tracepoint_num (int num)
230 tracepoint_number = num;
231 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
234 /* Set externally visible debug variables for querying/printing
235 the traceframe context (line, function, file). */
238 set_traceframe_context (struct frame_info *trace_frame)
241 struct symbol *traceframe_fun;
242 symtab_and_line traceframe_sal;
244 /* Save as globals for internal use. */
245 if (trace_frame != NULL
246 && get_frame_pc_if_available (trace_frame, &trace_pc))
248 traceframe_sal = find_pc_line (trace_pc, 0);
249 traceframe_fun = find_pc_function (trace_pc);
251 /* Save linenumber as "$trace_line", a debugger variable visible to
253 set_internalvar_integer (lookup_internalvar ("trace_line"),
254 traceframe_sal.line);
258 traceframe_fun = NULL;
259 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
262 /* Save func name as "$trace_func", a debugger variable visible to
264 if (traceframe_fun == NULL
265 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
266 clear_internalvar (lookup_internalvar ("trace_func"));
268 set_internalvar_string (lookup_internalvar ("trace_func"),
269 SYMBOL_LINKAGE_NAME (traceframe_fun));
271 /* Save file name as "$trace_file", a debugger variable visible to
273 if (traceframe_sal.symtab == NULL)
274 clear_internalvar (lookup_internalvar ("trace_file"));
276 set_internalvar_string (lookup_internalvar ("trace_file"),
277 symtab_to_filename_for_display (traceframe_sal.symtab));
280 /* Create a new trace state variable with the given name. */
282 struct trace_state_variable *
283 create_trace_state_variable (const char *name)
285 struct trace_state_variable tsv;
287 memset (&tsv, 0, sizeof (tsv));
288 tsv.name = xstrdup (name);
289 tsv.number = next_tsv_number++;
290 return VEC_safe_push (tsv_s, tvariables, &tsv);
293 /* Look for a trace state variable of the given name. */
295 struct trace_state_variable *
296 find_trace_state_variable (const char *name)
298 struct trace_state_variable *tsv;
301 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
302 if (strcmp (name, tsv->name) == 0)
308 /* Look for a trace state variable of the given number. Return NULL if
311 struct trace_state_variable *
312 find_trace_state_variable_by_number (int number)
314 struct trace_state_variable *tsv;
317 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
318 if (tsv->number == number)
325 delete_trace_state_variable (const char *name)
327 struct trace_state_variable *tsv;
330 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
331 if (strcmp (name, tsv->name) == 0)
333 observer_notify_tsv_deleted (tsv);
335 xfree ((void *)tsv->name);
336 VEC_unordered_remove (tsv_s, tvariables, ix);
341 warning (_("No trace variable named \"$%s\", not deleting"), name);
344 /* Throws an error if NAME is not valid syntax for a trace state
348 validate_trace_state_variable_name (const char *name)
353 error (_("Must supply a non-empty variable name"));
355 /* All digits in the name is reserved for value history
357 for (p = name; isdigit (*p); p++)
360 error (_("$%s is not a valid trace state variable name"), name);
362 for (p = name; isalnum (*p) || *p == '_'; p++)
365 error (_("$%s is not a valid trace state variable name"), name);
368 /* The 'tvariable' command collects a name and optional expression to
369 evaluate into an initial value. */
372 trace_variable_command (char *args, int from_tty)
375 struct trace_state_variable *tsv;
376 char *name_start, *p;
379 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
381 /* Only allow two syntaxes; "$name" and "$name=value". */
382 p = skip_spaces (args);
385 error (_("Name of trace variable should start with '$'"));
388 while (isalnum (*p) || *p == '_')
390 std::string name (name_start, p - name_start);
393 if (*p != '=' && *p != '\0')
394 error (_("Syntax must be $NAME [ = EXPR ]"));
396 validate_trace_state_variable_name (name.c_str ());
399 initval = value_as_long (parse_and_eval (++p));
401 /* If the variable already exists, just change its initial value. */
402 tsv = find_trace_state_variable (name.c_str ());
405 if (tsv->initial_value != initval)
407 tsv->initial_value = initval;
408 observer_notify_tsv_modified (tsv);
410 printf_filtered (_("Trace state variable $%s "
411 "now has initial value %s.\n"),
412 tsv->name, plongest (tsv->initial_value));
416 /* Create a new variable. */
417 tsv = create_trace_state_variable (name.c_str ());
418 tsv->initial_value = initval;
420 observer_notify_tsv_created (tsv);
422 printf_filtered (_("Trace state variable $%s "
423 "created, with initial value %s.\n"),
424 tsv->name, plongest (tsv->initial_value));
428 delete_trace_variable_command (const char *args, int from_tty)
432 if (query (_("Delete all trace state variables? ")))
433 VEC_free (tsv_s, tvariables);
435 observer_notify_tsv_deleted (NULL);
439 gdb_argv argv (args);
441 for (char *arg : argv)
444 delete_trace_state_variable (arg + 1);
446 warning (_("Name \"%s\" not prefixed with '$', ignoring"), arg);
453 tvariables_info_1 (void)
455 struct trace_state_variable *tsv;
458 struct ui_out *uiout = current_uiout;
460 if (VEC_length (tsv_s, tvariables) == 0 && !uiout->is_mi_like_p ())
462 printf_filtered (_("No trace state variables.\n"));
466 /* Try to acquire values from the target. */
467 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix, ++count)
468 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
471 ui_out_emit_table table_emitter (uiout, 3, count, "trace-variables");
472 uiout->table_header (15, ui_left, "name", "Name");
473 uiout->table_header (11, ui_left, "initial", "Initial");
474 uiout->table_header (11, ui_left, "current", "Current");
476 uiout->table_body ();
478 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
482 ui_out_emit_tuple tuple_emitter (uiout, "variable");
484 std::string name = std::string ("$") + tsv->name;
485 uiout->field_string ("name", name.c_str ());
486 uiout->field_string ("initial", plongest (tsv->initial_value));
488 if (tsv->value_known)
489 c = plongest (tsv->value);
490 else if (uiout->is_mi_like_p ())
491 /* For MI, we prefer not to use magic string constants, but rather
492 omit the field completely. The difference between unknown and
493 undefined does not seem important enough to represent. */
495 else if (current_trace_status ()->running || traceframe_number >= 0)
496 /* The value is/was defined, but we don't have it. */
499 /* It is not meaningful to ask about the value. */
502 uiout->field_string ("current", c);
507 /* List all the trace state variables. */
510 info_tvariables_command (char *args, int from_tty)
512 tvariables_info_1 ();
515 /* Stash definitions of tsvs into the given file. */
518 save_trace_state_variables (struct ui_file *fp)
520 struct trace_state_variable *tsv;
523 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
525 fprintf_unfiltered (fp, "tvariable $%s", tsv->name);
526 if (tsv->initial_value)
527 fprintf_unfiltered (fp, " = %s", plongest (tsv->initial_value));
528 fprintf_unfiltered (fp, "\n");
532 /* ACTIONS functions: */
534 /* The three functions:
535 collect_pseudocommand,
536 while_stepping_pseudocommand, and
537 end_actions_pseudocommand
538 are placeholders for "commands" that are actually ONLY to be used
539 within a tracepoint action list. If the actual function is ever called,
540 it means that somebody issued the "command" at the top level,
541 which is always an error. */
544 end_actions_pseudocommand (char *args, int from_tty)
546 error (_("This command cannot be used at the top level."));
550 while_stepping_pseudocommand (char *args, int from_tty)
552 error (_("This command can only be used in a tracepoint actions list."));
556 collect_pseudocommand (char *args, int from_tty)
558 error (_("This command can only be used in a tracepoint actions list."));
562 teval_pseudocommand (char *args, int from_tty)
564 error (_("This command can only be used in a tracepoint actions list."));
567 /* Parse any collection options, such as /s for strings. */
570 decode_agent_options (const char *exp, int *trace_string)
572 struct value_print_options opts;
579 /* Call this to borrow the print elements default for collection
581 get_user_print_options (&opts);
586 if (target_supports_string_tracing ())
588 /* Allow an optional decimal number giving an explicit maximum
589 string length, defaulting it to the "print elements" value;
590 so "collect/s80 mystr" gets at most 80 bytes of string. */
591 *trace_string = opts.print_max;
593 if (*exp >= '0' && *exp <= '9')
594 *trace_string = atoi (exp);
595 while (*exp >= '0' && *exp <= '9')
599 error (_("Target does not support \"/s\" option for string tracing."));
602 error (_("Undefined collection format \"%c\"."), *exp);
604 exp = skip_spaces (exp);
609 /* Enter a list of actions for a tracepoint. */
611 actions_command (char *args, int from_tty)
613 struct tracepoint *t;
615 t = get_tracepoint_by_number (&args, NULL);
619 string_printf ("Enter actions for tracepoint %d, one per line.",
622 command_line_up l = read_command_lines (&tmpbuf[0], from_tty, 1,
623 check_tracepoint_command, t);
624 breakpoint_set_commands (t, std::move (l));
626 /* else just return */
629 /* Report the results of checking the agent expression, as errors or
633 report_agent_reqs_errors (struct agent_expr *aexpr)
635 /* All of the "flaws" are serious bytecode generation issues that
636 should never occur. */
637 if (aexpr->flaw != agent_flaw_none)
638 internal_error (__FILE__, __LINE__, _("expression is malformed"));
640 /* If analysis shows a stack underflow, GDB must have done something
641 badly wrong in its bytecode generation. */
642 if (aexpr->min_height < 0)
643 internal_error (__FILE__, __LINE__,
644 _("expression has min height < 0"));
646 /* Issue this error if the stack is predicted to get too deep. The
647 limit is rather arbitrary; a better scheme might be for the
648 target to report how much stack it will have available. The
649 depth roughly corresponds to parenthesization, so a limit of 20
650 amounts to 20 levels of expression nesting, which is actually
651 a pretty big hairy expression. */
652 if (aexpr->max_height > 20)
653 error (_("Expression is too complicated."));
656 /* worker function */
658 validate_actionline (const char *line, struct breakpoint *b)
660 struct cmd_list_element *c;
663 struct bp_location *loc;
664 struct tracepoint *t = (struct tracepoint *) b;
666 /* If EOF is typed, *line is NULL. */
670 p = skip_spaces (line);
672 /* Symbol lookup etc. */
673 if (*p == '\0') /* empty line: just prompt for another line. */
676 if (*p == '#') /* comment line */
679 c = lookup_cmd (&p, cmdlist, "", -1, 1);
681 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
683 if (cmd_cfunc_eq (c, collect_pseudocommand))
685 int trace_string = 0;
688 p = decode_agent_options (p, &trace_string);
691 { /* Repeat over a comma-separated list. */
692 QUIT; /* Allow user to bail out with ^C. */
695 if (*p == '$') /* Look for special pseudo-symbols. */
697 if (0 == strncasecmp ("reg", p + 1, 3)
698 || 0 == strncasecmp ("arg", p + 1, 3)
699 || 0 == strncasecmp ("loc", p + 1, 3)
700 || 0 == strncasecmp ("_ret", p + 1, 4)
701 || 0 == strncasecmp ("_sdata", p + 1, 6))
706 /* else fall thru, treat p as an expression and parse it! */
709 for (loc = t->loc; loc; loc = loc->next)
712 expression_up exp = parse_exp_1 (&p, loc->address,
713 block_for_pc (loc->address), 1);
715 if (exp->elts[0].opcode == OP_VAR_VALUE)
717 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
719 error (_("constant `%s' (value %s) "
720 "will not be collected."),
721 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
722 plongest (SYMBOL_VALUE (exp->elts[2].symbol)));
724 else if (SYMBOL_CLASS (exp->elts[2].symbol)
725 == LOC_OPTIMIZED_OUT)
727 error (_("`%s' is optimized away "
728 "and cannot be collected."),
729 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
733 /* We have something to collect, make sure that the expr to
734 bytecode translator can handle it and that it's not too
736 agent_expr_up aexpr = gen_trace_for_expr (loc->address,
740 if (aexpr->len > MAX_AGENT_EXPR_LEN)
741 error (_("Expression is too complicated."));
743 ax_reqs (aexpr.get ());
745 report_agent_reqs_errors (aexpr.get ());
748 while (p && *p++ == ',');
751 else if (cmd_cfunc_eq (c, teval_pseudocommand))
754 { /* Repeat over a comma-separated list. */
755 QUIT; /* Allow user to bail out with ^C. */
759 for (loc = t->loc; loc; loc = loc->next)
763 /* Only expressions are allowed for this action. */
764 expression_up exp = parse_exp_1 (&p, loc->address,
765 block_for_pc (loc->address), 1);
767 /* We have something to evaluate, make sure that the expr to
768 bytecode translator can handle it and that it's not too
770 agent_expr_up aexpr = gen_eval_for_expr (loc->address, exp.get ());
772 if (aexpr->len > MAX_AGENT_EXPR_LEN)
773 error (_("Expression is too complicated."));
775 ax_reqs (aexpr.get ());
776 report_agent_reqs_errors (aexpr.get ());
779 while (p && *p++ == ',');
782 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
787 t->step_count = strtol (p, &endp, 0);
788 if (endp == p || t->step_count == 0)
789 error (_("while-stepping step count `%s' is malformed."), line);
793 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
797 error (_("`%s' is not a supported tracepoint action."), line);
801 memrange_absolute = -1
804 /* MEMRANGE functions: */
806 /* Compare memranges for std::sort. */
809 memrange_comp (const memrange &a, const memrange &b)
811 if (a.type == b.type)
813 if (a.type == memrange_absolute)
814 return (bfd_vma) a.start < (bfd_vma) b.start;
816 return a.start < b.start;
819 return a.type < b.type;
822 /* Sort the memrange list using std::sort, and merge adjacent memranges. */
825 memrange_sortmerge (std::vector<memrange> &memranges)
827 if (!memranges.empty ())
831 std::sort (memranges.begin (), memranges.end (), memrange_comp);
833 for (a = 0, b = 1; b < memranges.size (); b++)
835 /* If memrange b overlaps or is adjacent to memrange a,
837 if (memranges[a].type == memranges[b].type
838 && memranges[b].start <= memranges[a].end)
840 if (memranges[b].end > memranges[a].end)
841 memranges[a].end = memranges[b].end;
842 continue; /* next b, same a */
846 memranges[a] = memranges[b];
848 memranges.resize (a + 1);
852 /* Add a register to a collection list. */
855 collection_list::add_register (unsigned int regno)
858 printf_filtered ("collect register %d\n", regno);
859 if (regno >= (8 * sizeof (m_regs_mask)))
860 error (_("Internal: register number %d too large for tracepoint"),
862 m_regs_mask[regno / 8] |= 1 << (regno % 8);
865 /* Add a memrange to a collection list. */
868 collection_list::add_memrange (struct gdbarch *gdbarch,
869 int type, bfd_signed_vma base,
873 printf_filtered ("(%d,%s,%ld)\n", type, paddress (gdbarch, base), len);
875 /* type: memrange_absolute == memory, other n == basereg */
876 /* base: addr if memory, offset if reg relative. */
877 /* len: we actually save end (base + len) for convenience */
878 m_memranges.emplace_back (type, base, base + len);
880 if (type != memrange_absolute) /* Better collect the base register! */
884 /* Add a symbol to a collection list. */
887 collection_list::collect_symbol (struct symbol *sym,
888 struct gdbarch *gdbarch,
889 long frame_regno, long frame_offset,
895 bfd_signed_vma offset;
896 int treat_as_expr = 0;
898 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
899 switch (SYMBOL_CLASS (sym))
902 printf_filtered ("%s: don't know symbol class %d\n",
903 SYMBOL_PRINT_NAME (sym),
907 printf_filtered ("constant %s (value %s) will not be collected.\n",
908 SYMBOL_PRINT_NAME (sym), plongest (SYMBOL_VALUE (sym)));
911 offset = SYMBOL_VALUE_ADDRESS (sym);
914 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
915 SYMBOL_PRINT_NAME (sym), len,
916 paddress (gdbarch, offset));
918 /* A struct may be a C++ class with static fields, go to general
919 expression handling. */
920 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT)
923 add_memrange (gdbarch, memrange_absolute, offset, len);
926 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
928 printf_filtered ("LOC_REG[parm] %s: ",
929 SYMBOL_PRINT_NAME (sym));
931 /* Check for doubles stored in two registers. */
932 /* FIXME: how about larger types stored in 3 or more regs? */
933 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
934 len > register_size (gdbarch, reg))
935 add_register (reg + 1);
938 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
939 printf_filtered (" (will not collect %s)\n",
940 SYMBOL_PRINT_NAME (sym));
944 offset = frame_offset + SYMBOL_VALUE (sym);
947 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
948 " from frame ptr reg %d\n",
949 SYMBOL_PRINT_NAME (sym), len,
950 paddress (gdbarch, offset), reg);
952 add_memrange (gdbarch, reg, offset, len);
954 case LOC_REGPARM_ADDR:
955 reg = SYMBOL_VALUE (sym);
959 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s"
961 SYMBOL_PRINT_NAME (sym), len,
962 paddress (gdbarch, offset), reg);
964 add_memrange (gdbarch, reg, offset, len);
968 offset = frame_offset + SYMBOL_VALUE (sym);
971 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
972 " from frame ptr reg %d\n",
973 SYMBOL_PRINT_NAME (sym), len,
974 paddress (gdbarch, offset), reg);
976 add_memrange (gdbarch, reg, offset, len);
983 case LOC_OPTIMIZED_OUT:
984 printf_filtered ("%s has been optimized out of existence.\n",
985 SYMBOL_PRINT_NAME (sym));
993 /* Expressions are the most general case. */
996 agent_expr_up aexpr = gen_trace_for_var (scope, gdbarch,
999 /* It can happen that the symbol is recorded as a computed
1000 location, but it's been optimized away and doesn't actually
1001 have a location expression. */
1004 printf_filtered ("%s has been optimized out of existence.\n",
1005 SYMBOL_PRINT_NAME (sym));
1009 ax_reqs (aexpr.get ());
1011 report_agent_reqs_errors (aexpr.get ());
1013 /* Take care of the registers. */
1014 if (aexpr->reg_mask_len > 0)
1016 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1018 QUIT; /* Allow user to bail out with ^C. */
1019 if (aexpr->reg_mask[ndx1] != 0)
1021 /* Assume chars have 8 bits. */
1022 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1023 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1024 /* It's used -- record it. */
1025 add_register (ndx1 * 8 + ndx2);
1030 add_aexpr (std::move (aexpr));
1034 /* Data to be passed around in the calls to the locals and args
1037 struct add_local_symbols_data
1039 struct collection_list *collect;
1040 struct gdbarch *gdbarch;
1048 /* The callback for the locals and args iterators. */
1051 do_collect_symbol (const char *print_name,
1055 struct add_local_symbols_data *p = (struct add_local_symbols_data *) cb_data;
1057 p->collect->collect_symbol (sym, p->gdbarch, p->frame_regno,
1058 p->frame_offset, p->pc, p->trace_string);
1061 p->collect->add_wholly_collected (print_name);
1065 collection_list::add_wholly_collected (const char *print_name)
1067 m_wholly_collected.push_back (print_name);
1070 /* Add all locals (or args) symbols to collection list. */
1073 collection_list::add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc,
1074 long frame_regno, long frame_offset, int type,
1077 const struct block *block;
1078 struct add_local_symbols_data cb_data;
1080 cb_data.collect = this;
1081 cb_data.gdbarch = gdbarch;
1083 cb_data.frame_regno = frame_regno;
1084 cb_data.frame_offset = frame_offset;
1086 cb_data.trace_string = trace_string;
1090 block = block_for_pc (pc);
1093 warning (_("Can't collect locals; "
1094 "no symbol table info available.\n"));
1098 iterate_over_block_local_vars (block, do_collect_symbol, &cb_data);
1099 if (cb_data.count == 0)
1100 warning (_("No locals found in scope."));
1104 pc = get_pc_function_start (pc);
1105 block = block_for_pc (pc);
1108 warning (_("Can't collect args; no symbol table info available."));
1112 iterate_over_block_arg_vars (block, do_collect_symbol, &cb_data);
1113 if (cb_data.count == 0)
1114 warning (_("No args found in scope."));
1119 collection_list::add_static_trace_data ()
1122 printf_filtered ("collect static trace data\n");
1123 m_strace_data = true;
1126 collection_list::collection_list ()
1128 m_strace_data (false)
1130 m_memranges.reserve (128);
1131 m_aexprs.reserve (128);
1134 /* Reduce a collection list to string form (for gdb protocol). */
1137 collection_list::stringify ()
1139 char temp_buf[2048];
1142 char *(*str_list)[];
1146 count = 1 + 1 + m_memranges.size () + m_aexprs.size () + 1;
1147 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
1152 printf_filtered ("\nCollecting static trace data\n");
1155 (*str_list)[ndx] = savestring (temp_buf, end - temp_buf);
1159 for (i = sizeof (m_regs_mask) - 1; i > 0; i--)
1160 if (m_regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
1162 if (m_regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
1165 printf_filtered ("\nCollecting registers (mask): 0x");
1170 QUIT; /* Allow user to bail out with ^C. */
1172 printf_filtered ("%02X", m_regs_mask[i]);
1173 sprintf (end, "%02X", m_regs_mask[i]);
1176 (*str_list)[ndx] = xstrdup (temp_buf);
1180 printf_filtered ("\n");
1181 if (!m_memranges.empty () && info_verbose)
1182 printf_filtered ("Collecting memranges: \n");
1183 for (i = 0, count = 0, end = temp_buf; i < m_memranges.size (); i++)
1185 QUIT; /* Allow user to bail out with ^C. */
1188 printf_filtered ("(%d, %s, %ld)\n",
1189 m_memranges[i].type,
1190 paddress (target_gdbarch (),
1191 m_memranges[i].start),
1192 (long) (m_memranges[i].end
1193 - m_memranges[i].start));
1195 if (count + 27 > MAX_AGENT_EXPR_LEN)
1197 (*str_list)[ndx] = savestring (temp_buf, count);
1204 bfd_signed_vma length
1205 = m_memranges[i].end - m_memranges[i].start;
1207 /* The "%X" conversion specifier expects an unsigned argument,
1208 so passing -1 (memrange_absolute) to it directly gives you
1209 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1211 if (m_memranges[i].type == memrange_absolute)
1212 sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
1215 sprintf (end, "M%X,%s,%lX", m_memranges[i].type,
1216 phex_nz (m_memranges[i].start, 0), (long) length);
1219 count += strlen (end);
1220 end = temp_buf + count;
1223 for (i = 0; i < m_aexprs.size (); i++)
1225 QUIT; /* Allow user to bail out with ^C. */
1226 if ((count + 10 + 2 * m_aexprs[i]->len) > MAX_AGENT_EXPR_LEN)
1228 (*str_list)[ndx] = savestring (temp_buf, count);
1233 sprintf (end, "X%08X,", m_aexprs[i]->len);
1234 end += 10; /* 'X' + 8 hex digits + ',' */
1237 end = mem2hex (m_aexprs[i]->buf, end, m_aexprs[i]->len);
1238 count += 2 * m_aexprs[i]->len;
1243 (*str_list)[ndx] = savestring (temp_buf, count);
1248 (*str_list)[ndx] = NULL;
1259 /* Add the printed expression EXP to *LIST. */
1262 collection_list::append_exp (struct expression *exp)
1264 string_file tmp_stream;
1266 print_expression (exp, &tmp_stream);
1268 m_computed.push_back (std::move (tmp_stream.string ()));
1272 collection_list::finish ()
1274 memrange_sortmerge (m_memranges);
1278 encode_actions_1 (struct command_line *action,
1279 struct bp_location *tloc,
1281 LONGEST frame_offset,
1282 struct collection_list *collect,
1283 struct collection_list *stepping_list)
1285 const char *action_exp;
1287 struct value *tempval;
1288 struct cmd_list_element *cmd;
1290 for (; action; action = action->next)
1292 QUIT; /* Allow user to bail out with ^C. */
1293 action_exp = action->line;
1294 action_exp = skip_spaces (action_exp);
1296 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1298 error (_("Bad action list item: %s"), action_exp);
1300 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
1302 int trace_string = 0;
1304 if (*action_exp == '/')
1305 action_exp = decode_agent_options (action_exp, &trace_string);
1308 { /* Repeat over a comma-separated list. */
1309 QUIT; /* Allow user to bail out with ^C. */
1310 action_exp = skip_spaces (action_exp);
1312 if (0 == strncasecmp ("$reg", action_exp, 4))
1314 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
1315 collect->add_register (i);
1316 action_exp = strchr (action_exp, ','); /* more? */
1318 else if (0 == strncasecmp ("$arg", action_exp, 4))
1320 collect->add_local_symbols (target_gdbarch (),
1326 action_exp = strchr (action_exp, ','); /* more? */
1328 else if (0 == strncasecmp ("$loc", action_exp, 4))
1330 collect->add_local_symbols (target_gdbarch (),
1336 action_exp = strchr (action_exp, ','); /* more? */
1338 else if (0 == strncasecmp ("$_ret", action_exp, 5))
1341 = gen_trace_for_return_address (tloc->address,
1345 ax_reqs (aexpr.get ());
1346 report_agent_reqs_errors (aexpr.get ());
1348 /* take care of the registers */
1349 if (aexpr->reg_mask_len > 0)
1351 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1353 QUIT; /* allow user to bail out with ^C */
1354 if (aexpr->reg_mask[ndx1] != 0)
1356 /* assume chars have 8 bits */
1357 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1358 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1360 /* It's used -- record it. */
1361 collect->add_register (ndx1 * 8 + ndx2);
1367 collect->add_aexpr (std::move (aexpr));
1368 action_exp = strchr (action_exp, ','); /* more? */
1370 else if (0 == strncasecmp ("$_sdata", action_exp, 7))
1372 collect->add_static_trace_data ();
1373 action_exp = strchr (action_exp, ','); /* more? */
1379 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1380 block_for_pc (tloc->address),
1383 switch (exp->elts[0].opcode)
1387 const char *name = &exp->elts[2].string;
1389 i = user_reg_map_name_to_regnum (target_gdbarch (),
1390 name, strlen (name));
1392 internal_error (__FILE__, __LINE__,
1393 _("Register $%s not available"),
1396 printf_filtered ("OP_REGISTER: ");
1397 collect->add_register (i);
1402 /* Safe because we know it's a simple expression. */
1403 tempval = evaluate_expression (exp.get ());
1404 addr = value_address (tempval);
1405 /* Initialize the TYPE_LENGTH if it is a typedef. */
1406 check_typedef (exp->elts[1].type);
1407 collect->add_memrange (target_gdbarch (),
1408 memrange_absolute, addr,
1409 TYPE_LENGTH (exp->elts[1].type));
1410 collect->append_exp (exp.get ());
1415 struct symbol *sym = exp->elts[2].symbol;
1416 char_ptr name = (char_ptr) SYMBOL_NATURAL_NAME (sym);
1418 collect->collect_symbol (exp->elts[2].symbol,
1424 collect->add_wholly_collected (name);
1428 default: /* Full-fledged expression. */
1429 agent_expr_up aexpr = gen_trace_for_expr (tloc->address,
1433 ax_reqs (aexpr.get ());
1435 report_agent_reqs_errors (aexpr.get ());
1437 /* Take care of the registers. */
1438 if (aexpr->reg_mask_len > 0)
1440 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1442 QUIT; /* Allow user to bail out with ^C. */
1443 if (aexpr->reg_mask[ndx1] != 0)
1445 /* Assume chars have 8 bits. */
1446 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1447 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1449 /* It's used -- record it. */
1450 collect->add_register (ndx1 * 8 + ndx2);
1456 collect->add_aexpr (std::move (aexpr));
1457 collect->append_exp (exp.get ());
1462 while (action_exp && *action_exp++ == ',');
1464 else if (cmd_cfunc_eq (cmd, teval_pseudocommand))
1467 { /* Repeat over a comma-separated list. */
1468 QUIT; /* Allow user to bail out with ^C. */
1469 action_exp = skip_spaces (action_exp);
1472 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1473 block_for_pc (tloc->address),
1476 agent_expr_up aexpr = gen_eval_for_expr (tloc->address,
1479 ax_reqs (aexpr.get ());
1480 report_agent_reqs_errors (aexpr.get ());
1482 /* Even though we're not officially collecting, add
1483 to the collect list anyway. */
1484 collect->add_aexpr (std::move (aexpr));
1487 while (action_exp && *action_exp++ == ',');
1489 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
1491 /* We check against nested while-stepping when setting
1492 breakpoint action, so no way to run into nested
1494 gdb_assert (stepping_list);
1496 encode_actions_1 (action->body_list[0], tloc, frame_reg,
1497 frame_offset, stepping_list, NULL);
1500 error (_("Invalid tracepoint command '%s'"), action->line);
1504 /* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
1505 and STEPPING_LIST. */
1508 encode_actions (struct bp_location *tloc,
1509 struct collection_list *tracepoint_list,
1510 struct collection_list *stepping_list)
1512 struct command_line *actions;
1514 LONGEST frame_offset;
1516 gdbarch_virtual_frame_pointer (tloc->gdbarch,
1517 tloc->address, &frame_reg, &frame_offset);
1519 actions = all_tracepoint_actions_and_cleanup (tloc->owner);
1521 encode_actions_1 (actions, tloc, frame_reg, frame_offset,
1522 tracepoint_list, stepping_list);
1524 tracepoint_list->finish ();
1525 stepping_list->finish ();
1528 /* Render all actions into gdb protocol. */
1531 encode_actions_rsp (struct bp_location *tloc, char ***tdp_actions,
1532 char ***stepping_actions)
1534 struct collection_list tracepoint_list, stepping_list;
1536 *tdp_actions = NULL;
1537 *stepping_actions = NULL;
1539 encode_actions (tloc, &tracepoint_list, &stepping_list);
1541 *tdp_actions = tracepoint_list.stringify ();
1542 *stepping_actions = stepping_list.stringify ();
1546 collection_list::add_aexpr (agent_expr_up aexpr)
1548 m_aexprs.push_back (std::move (aexpr));
1552 process_tracepoint_on_disconnect (void)
1554 VEC(breakpoint_p) *tp_vec = NULL;
1556 struct breakpoint *b;
1557 int has_pending_p = 0;
1559 /* Check whether we still have pending tracepoint. If we have, warn the
1560 user that pending tracepoint will no longer work. */
1561 tp_vec = all_tracepoints ();
1562 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1571 struct bp_location *loc1;
1573 for (loc1 = b->loc; loc1; loc1 = loc1->next)
1575 if (loc1->shlib_disabled)
1586 VEC_free (breakpoint_p, tp_vec);
1589 warning (_("Pending tracepoints will not be resolved while"
1590 " GDB is disconnected\n"));
1593 /* Reset local state of tracing. */
1596 trace_reset_local_state (void)
1598 set_traceframe_num (-1);
1599 set_tracepoint_num (-1);
1600 set_traceframe_context (NULL);
1601 clear_traceframe_info ();
1605 start_tracing (char *notes)
1607 VEC(breakpoint_p) *tp_vec = NULL;
1609 struct breakpoint *b;
1610 struct trace_state_variable *tsv;
1611 int any_enabled = 0, num_to_download = 0;
1614 tp_vec = all_tracepoints ();
1616 /* No point in tracing without any tracepoints... */
1617 if (VEC_length (breakpoint_p, tp_vec) == 0)
1619 VEC_free (breakpoint_p, tp_vec);
1620 error (_("No tracepoints defined, not starting trace"));
1623 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1625 if (b->enable_state == bp_enabled)
1628 if ((b->type == bp_fast_tracepoint
1629 ? may_insert_fast_tracepoints
1630 : may_insert_tracepoints))
1633 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
1634 (b->type == bp_fast_tracepoint ? "fast " : ""), b->number);
1639 if (target_supports_enable_disable_tracepoint ())
1640 warning (_("No tracepoints enabled"));
1643 /* No point in tracing with only disabled tracepoints that
1644 cannot be re-enabled. */
1645 VEC_free (breakpoint_p, tp_vec);
1646 error (_("No tracepoints enabled, not starting trace"));
1650 if (num_to_download <= 0)
1652 VEC_free (breakpoint_p, tp_vec);
1653 error (_("No tracepoints that may be downloaded, not starting trace"));
1656 target_trace_init ();
1658 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1660 struct tracepoint *t = (struct tracepoint *) b;
1661 struct bp_location *loc;
1662 int bp_location_downloaded = 0;
1664 /* Clear `inserted' flag. */
1665 for (loc = b->loc; loc; loc = loc->next)
1668 if ((b->type == bp_fast_tracepoint
1669 ? !may_insert_fast_tracepoints
1670 : !may_insert_tracepoints))
1673 t->number_on_target = 0;
1675 for (loc = b->loc; loc; loc = loc->next)
1677 /* Since tracepoint locations are never duplicated, `inserted'
1678 flag should be zero. */
1679 gdb_assert (!loc->inserted);
1681 target_download_tracepoint (loc);
1684 bp_location_downloaded = 1;
1687 t->number_on_target = b->number;
1689 for (loc = b->loc; loc; loc = loc->next)
1690 if (loc->probe.probe != NULL
1691 && loc->probe.probe->pops->set_semaphore != NULL)
1692 loc->probe.probe->pops->set_semaphore (loc->probe.probe,
1696 if (bp_location_downloaded)
1697 observer_notify_breakpoint_modified (b);
1699 VEC_free (breakpoint_p, tp_vec);
1701 /* Send down all the trace state variables too. */
1702 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
1704 target_download_trace_state_variable (tsv);
1707 /* Tell target to treat text-like sections as transparent. */
1708 target_trace_set_readonly_regions ();
1709 /* Set some mode flags. */
1710 target_set_disconnected_tracing (disconnected_tracing);
1711 target_set_circular_trace_buffer (circular_trace_buffer);
1712 target_set_trace_buffer_size (trace_buffer_size);
1715 notes = trace_notes;
1716 ret = target_set_trace_notes (trace_user, notes, NULL);
1718 if (!ret && (trace_user || notes))
1719 warning (_("Target does not support trace user/notes, info ignored"));
1721 /* Now insert traps and begin collecting data. */
1722 target_trace_start ();
1724 /* Reset our local state. */
1725 trace_reset_local_state ();
1726 current_trace_status()->running = 1;
1729 /* The tstart command requests the target to start a new trace run.
1730 The command passes any arguments it has to the target verbatim, as
1731 an optional "trace note". This is useful as for instance a warning
1732 to other users if the trace runs disconnected, and you don't want
1733 anybody else messing with the target. */
1736 tstart_command (char *args, int from_tty)
1738 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1740 if (current_trace_status ()->running)
1743 && !query (_("A trace is running already. Start a new run? ")))
1744 error (_("New trace run not started."));
1747 start_tracing (args);
1750 /* The tstop command stops the tracing run. The command passes any
1751 supplied arguments to the target verbatim as a "stop note"; if the
1752 target supports trace notes, then it will be reported back as part
1753 of the trace run's status. */
1756 tstop_command (char *args, int from_tty)
1758 if (!current_trace_status ()->running)
1759 error (_("Trace is not running."));
1761 stop_tracing (args);
1765 stop_tracing (char *note)
1768 VEC(breakpoint_p) *tp_vec = NULL;
1770 struct breakpoint *t;
1772 target_trace_stop ();
1774 tp_vec = all_tracepoints ();
1775 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1777 struct bp_location *loc;
1779 if ((t->type == bp_fast_tracepoint
1780 ? !may_insert_fast_tracepoints
1781 : !may_insert_tracepoints))
1784 for (loc = t->loc; loc; loc = loc->next)
1786 /* GDB can be totally absent in some disconnected trace scenarios,
1787 but we don't really care if this semaphore goes out of sync.
1788 That's why we are decrementing it here, but not taking care
1790 if (loc->probe.probe != NULL
1791 && loc->probe.probe->pops->clear_semaphore != NULL)
1792 loc->probe.probe->pops->clear_semaphore (loc->probe.probe,
1798 VEC_free (breakpoint_p, tp_vec);
1801 note = trace_stop_notes;
1802 ret = target_set_trace_notes (NULL, NULL, note);
1805 warning (_("Target does not support trace notes, note ignored"));
1807 /* Should change in response to reply? */
1808 current_trace_status ()->running = 0;
1811 /* tstatus command */
1813 tstatus_command (char *args, int from_tty)
1815 struct trace_status *ts = current_trace_status ();
1817 VEC(breakpoint_p) *tp_vec = NULL;
1818 struct breakpoint *t;
1820 status = target_get_trace_status (ts);
1824 if (ts->filename != NULL)
1825 printf_filtered (_("Using a trace file.\n"));
1828 printf_filtered (_("Trace can not be run on this target.\n"));
1833 if (!ts->running_known)
1835 printf_filtered (_("Run/stop status is unknown.\n"));
1837 else if (ts->running)
1839 printf_filtered (_("Trace is running on the target.\n"));
1843 switch (ts->stop_reason)
1845 case trace_never_run:
1846 printf_filtered (_("No trace has been run on the target.\n"));
1848 case trace_stop_command:
1850 printf_filtered (_("Trace stopped by a tstop command (%s).\n"),
1853 printf_filtered (_("Trace stopped by a tstop command.\n"));
1855 case trace_buffer_full:
1856 printf_filtered (_("Trace stopped because the buffer was full.\n"));
1858 case trace_disconnected:
1859 printf_filtered (_("Trace stopped because of disconnection.\n"));
1861 case tracepoint_passcount:
1862 printf_filtered (_("Trace stopped by tracepoint %d.\n"),
1863 ts->stopping_tracepoint);
1865 case tracepoint_error:
1866 if (ts->stopping_tracepoint)
1867 printf_filtered (_("Trace stopped by an "
1868 "error (%s, tracepoint %d).\n"),
1869 ts->stop_desc, ts->stopping_tracepoint);
1871 printf_filtered (_("Trace stopped by an error (%s).\n"),
1874 case trace_stop_reason_unknown:
1875 printf_filtered (_("Trace stopped for an unknown reason.\n"));
1878 printf_filtered (_("Trace stopped for some other reason (%d).\n"),
1884 if (ts->traceframes_created >= 0
1885 && ts->traceframe_count != ts->traceframes_created)
1887 printf_filtered (_("Buffer contains %d trace "
1888 "frames (of %d created total).\n"),
1889 ts->traceframe_count, ts->traceframes_created);
1891 else if (ts->traceframe_count >= 0)
1893 printf_filtered (_("Collected %d trace frames.\n"),
1894 ts->traceframe_count);
1897 if (ts->buffer_free >= 0)
1899 if (ts->buffer_size >= 0)
1901 printf_filtered (_("Trace buffer has %d bytes of %d bytes free"),
1902 ts->buffer_free, ts->buffer_size);
1903 if (ts->buffer_size > 0)
1904 printf_filtered (_(" (%d%% full)"),
1905 ((int) ((((long long) (ts->buffer_size
1906 - ts->buffer_free)) * 100)
1907 / ts->buffer_size)));
1908 printf_filtered (_(".\n"));
1911 printf_filtered (_("Trace buffer has %d bytes free.\n"),
1915 if (ts->disconnected_tracing)
1916 printf_filtered (_("Trace will continue if GDB disconnects.\n"));
1918 printf_filtered (_("Trace will stop if GDB disconnects.\n"));
1920 if (ts->circular_buffer)
1921 printf_filtered (_("Trace buffer is circular.\n"));
1923 if (ts->user_name && strlen (ts->user_name) > 0)
1924 printf_filtered (_("Trace user is %s.\n"), ts->user_name);
1926 if (ts->notes && strlen (ts->notes) > 0)
1927 printf_filtered (_("Trace notes: %s.\n"), ts->notes);
1929 /* Now report on what we're doing with tfind. */
1930 if (traceframe_number >= 0)
1931 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
1932 traceframe_number, tracepoint_number);
1934 printf_filtered (_("Not looking at any trace frame.\n"));
1936 /* Report start/stop times if supplied. */
1941 LONGEST run_time = ts->stop_time - ts->start_time;
1943 /* Reporting a run time is more readable than two long numbers. */
1944 printf_filtered (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
1945 (long int) (ts->start_time / 1000000),
1946 (long int) (ts->start_time % 1000000),
1947 (long int) (run_time / 1000000),
1948 (long int) (run_time % 1000000));
1951 printf_filtered (_("Trace started at %ld.%06ld secs.\n"),
1952 (long int) (ts->start_time / 1000000),
1953 (long int) (ts->start_time % 1000000));
1955 else if (ts->stop_time)
1956 printf_filtered (_("Trace stopped at %ld.%06ld secs.\n"),
1957 (long int) (ts->stop_time / 1000000),
1958 (long int) (ts->stop_time % 1000000));
1960 /* Now report any per-tracepoint status available. */
1961 tp_vec = all_tracepoints ();
1963 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1964 target_get_tracepoint_status (t, NULL);
1966 VEC_free (breakpoint_p, tp_vec);
1969 /* Report the trace status to uiout, in a way suitable for MI, and not
1970 suitable for CLI. If ON_STOP is true, suppress a few fields that
1971 are not meaningful in the -trace-stop response.
1973 The implementation is essentially parallel to trace_status_command, but
1974 merging them will result in unreadable code. */
1976 trace_status_mi (int on_stop)
1978 struct ui_out *uiout = current_uiout;
1979 struct trace_status *ts = current_trace_status ();
1982 status = target_get_trace_status (ts);
1984 if (status == -1 && ts->filename == NULL)
1986 uiout->field_string ("supported", "0");
1990 if (ts->filename != NULL)
1991 uiout->field_string ("supported", "file");
1993 uiout->field_string ("supported", "1");
1995 if (ts->filename != NULL)
1996 uiout->field_string ("trace-file", ts->filename);
1998 gdb_assert (ts->running_known);
2002 uiout->field_string ("running", "1");
2004 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
2005 Given that the frontend gets the status either on -trace-stop, or from
2006 -trace-status after re-connection, it does not seem like this
2007 information is necessary for anything. It is not necessary for either
2008 figuring the vital state of the target nor for navigation of trace
2009 frames. If the frontend wants to show the current state is some
2010 configure dialog, it can request the value when such dialog is
2011 invoked by the user. */
2015 const char *stop_reason = NULL;
2016 int stopping_tracepoint = -1;
2019 uiout->field_string ("running", "0");
2021 if (ts->stop_reason != trace_stop_reason_unknown)
2023 switch (ts->stop_reason)
2025 case trace_stop_command:
2026 stop_reason = "request";
2028 case trace_buffer_full:
2029 stop_reason = "overflow";
2031 case trace_disconnected:
2032 stop_reason = "disconnection";
2034 case tracepoint_passcount:
2035 stop_reason = "passcount";
2036 stopping_tracepoint = ts->stopping_tracepoint;
2038 case tracepoint_error:
2039 stop_reason = "error";
2040 stopping_tracepoint = ts->stopping_tracepoint;
2046 uiout->field_string ("stop-reason", stop_reason);
2047 if (stopping_tracepoint != -1)
2048 uiout->field_int ("stopping-tracepoint",
2049 stopping_tracepoint);
2050 if (ts->stop_reason == tracepoint_error)
2051 uiout->field_string ("error-description",
2057 if (ts->traceframe_count != -1)
2058 uiout->field_int ("frames", ts->traceframe_count);
2059 if (ts->traceframes_created != -1)
2060 uiout->field_int ("frames-created", ts->traceframes_created);
2061 if (ts->buffer_size != -1)
2062 uiout->field_int ("buffer-size", ts->buffer_size);
2063 if (ts->buffer_free != -1)
2064 uiout->field_int ("buffer-free", ts->buffer_free);
2066 uiout->field_int ("disconnected", ts->disconnected_tracing);
2067 uiout->field_int ("circular", ts->circular_buffer);
2069 uiout->field_string ("user-name", ts->user_name);
2070 uiout->field_string ("notes", ts->notes);
2075 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2076 (long int) (ts->start_time / 1000000),
2077 (long int) (ts->start_time % 1000000));
2078 uiout->field_string ("start-time", buf);
2079 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2080 (long int) (ts->stop_time / 1000000),
2081 (long int) (ts->stop_time % 1000000));
2082 uiout->field_string ("stop-time", buf);
2086 /* Check if a trace run is ongoing. If so, and FROM_TTY, query the
2087 user if she really wants to detach. */
2090 query_if_trace_running (int from_tty)
2095 /* It can happen that the target that was tracing went away on its
2096 own, and we didn't notice. Get a status update, and if the
2097 current target doesn't even do tracing, then assume it's not
2099 if (target_get_trace_status (current_trace_status ()) < 0)
2100 current_trace_status ()->running = 0;
2102 /* If running interactively, give the user the option to cancel and
2103 then decide what to do differently with the run. Scripts are
2104 just going to disconnect and let the target deal with it,
2105 according to how it's been instructed previously via
2106 disconnected-tracing. */
2107 if (current_trace_status ()->running)
2109 process_tracepoint_on_disconnect ();
2111 if (current_trace_status ()->disconnected_tracing)
2113 if (!query (_("Trace is running and will "
2114 "continue after detach; detach anyway? ")))
2115 error (_("Not confirmed."));
2119 if (!query (_("Trace is running but will "
2120 "stop on detach; detach anyway? ")))
2121 error (_("Not confirmed."));
2126 /* This function handles the details of what to do about an ongoing
2127 tracing run if the user has asked to detach or otherwise disconnect
2131 disconnect_tracing (void)
2133 /* Also we want to be out of tfind mode, otherwise things can get
2134 confusing upon reconnection. Just use these calls instead of
2135 full tfind_1 behavior because we're in the middle of detaching,
2136 and there's no point to updating current stack frame etc. */
2137 trace_reset_local_state ();
2140 /* Worker function for the various flavors of the tfind command. */
2142 tfind_1 (enum trace_find_type type, int num,
2143 CORE_ADDR addr1, CORE_ADDR addr2,
2146 int target_frameno = -1, target_tracept = -1;
2147 struct frame_id old_frame_id = null_frame_id;
2148 struct tracepoint *tp;
2149 struct ui_out *uiout = current_uiout;
2151 /* Only try to get the current stack frame if we have a chance of
2152 succeeding. In particular, if we're trying to get a first trace
2153 frame while all threads are running, it's not going to succeed,
2154 so leave it with a default value and let the frame comparison
2155 below (correctly) decide to print out the source location of the
2157 if (!(type == tfind_number && num == -1)
2158 && (has_stack_frames () || traceframe_number >= 0))
2159 old_frame_id = get_frame_id (get_current_frame ());
2161 target_frameno = target_trace_find (type, num, addr1, addr2,
2164 if (type == tfind_number
2166 && target_frameno == -1)
2168 /* We told the target to get out of tfind mode, and it did. */
2170 else if (target_frameno == -1)
2172 /* A request for a non-existent trace frame has failed.
2173 Our response will be different, depending on FROM_TTY:
2175 If FROM_TTY is true, meaning that this command was
2176 typed interactively by the user, then give an error
2177 and DO NOT change the state of traceframe_number etc.
2179 However if FROM_TTY is false, meaning that we're either
2180 in a script, a loop, or a user-defined command, then
2181 DON'T give an error, but DO change the state of
2182 traceframe_number etc. to invalid.
2184 The rationalle is that if you typed the command, you
2185 might just have committed a typo or something, and you'd
2186 like to NOT lose your current debugging state. However
2187 if you're in a user-defined command or especially in a
2188 loop, then you need a way to detect that the command
2189 failed WITHOUT aborting. This allows you to write
2190 scripts that search thru the trace buffer until the end,
2191 and then continue on to do something else. */
2194 error (_("Target failed to find requested trace frame."));
2198 printf_filtered ("End of trace buffer.\n");
2199 #if 0 /* dubious now? */
2200 /* The following will not recurse, since it's
2202 tfind_command ("-1", from_tty);
2207 tp = get_tracepoint_by_number_on_target (target_tracept);
2209 reinit_frame_cache ();
2210 target_dcache_invalidate ();
2212 set_tracepoint_num (tp ? tp->number : target_tracept);
2214 if (target_frameno != get_traceframe_number ())
2215 observer_notify_traceframe_changed (target_frameno, tracepoint_number);
2217 set_current_traceframe (target_frameno);
2219 if (target_frameno == -1)
2220 set_traceframe_context (NULL);
2222 set_traceframe_context (get_current_frame ());
2224 if (traceframe_number >= 0)
2226 /* Use different branches for MI and CLI to make CLI messages
2228 if (uiout->is_mi_like_p ())
2230 uiout->field_string ("found", "1");
2231 uiout->field_int ("tracepoint", tracepoint_number);
2232 uiout->field_int ("traceframe", traceframe_number);
2236 printf_unfiltered (_("Found trace frame %d, tracepoint %d\n"),
2237 traceframe_number, tracepoint_number);
2242 if (uiout->is_mi_like_p ())
2243 uiout->field_string ("found", "0");
2244 else if (type == tfind_number && num == -1)
2245 printf_unfiltered (_("No longer looking at any trace frame\n"));
2246 else /* This case may never occur, check. */
2247 printf_unfiltered (_("No trace frame found\n"));
2250 /* If we're in nonstop mode and getting out of looking at trace
2251 frames, there won't be any current frame to go back to and
2254 && (has_stack_frames () || traceframe_number >= 0))
2256 enum print_what print_what;
2258 /* NOTE: in imitation of the step command, try to determine
2259 whether we have made a transition from one function to
2260 another. If so, we'll print the "stack frame" (ie. the new
2261 function and it's arguments) -- otherwise we'll just show the
2264 if (frame_id_eq (old_frame_id,
2265 get_frame_id (get_current_frame ())))
2266 print_what = SRC_LINE;
2268 print_what = SRC_AND_LOC;
2270 print_stack_frame (get_selected_frame (NULL), 1, print_what, 1);
2275 /* Error on looking at traceframes while trace is running. */
2278 check_trace_running (struct trace_status *status)
2280 if (status->running && status->filename == NULL)
2281 error (_("May not look at trace frames while trace is running."));
2284 /* trace_find_command takes a trace frame number n,
2285 sends "QTFrame:<n>" to the target,
2286 and accepts a reply that may contain several optional pieces
2287 of information: a frame number, a tracepoint number, and an
2288 indication of whether this is a trap frame or a stepping frame.
2290 The minimal response is just "OK" (which indicates that the
2291 target does not give us a frame number or a tracepoint number).
2292 Instead of that, the target may send us a string containing
2294 F<hexnum> (gives the selected frame number)
2295 T<hexnum> (gives the selected tracepoint number)
2300 tfind_command_1 (const char *args, int from_tty)
2301 { /* This should only be called with a numeric argument. */
2304 check_trace_running (current_trace_status ());
2306 if (args == 0 || *args == 0)
2307 { /* TFIND with no args means find NEXT trace frame. */
2308 if (traceframe_number == -1)
2309 frameno = 0; /* "next" is first one. */
2311 frameno = traceframe_number + 1;
2313 else if (0 == strcmp (args, "-"))
2315 if (traceframe_number == -1)
2316 error (_("not debugging trace buffer"));
2317 else if (from_tty && traceframe_number == 0)
2318 error (_("already at start of trace buffer"));
2320 frameno = traceframe_number - 1;
2322 /* A hack to work around eval's need for fp to have been collected. */
2323 else if (0 == strcmp (args, "-1"))
2326 frameno = parse_and_eval_long (args);
2329 error (_("invalid input (%d is less than zero)"), frameno);
2331 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
2335 tfind_command (const char *args, int from_tty)
2337 tfind_command_1 (const_cast<char *> (args), from_tty);
2342 tfind_end_command (const char *args, int from_tty)
2344 tfind_command_1 ("-1", from_tty);
2349 tfind_start_command (const char *args, int from_tty)
2351 tfind_command_1 ("0", from_tty);
2354 /* tfind pc command */
2356 tfind_pc_command (const char *args, int from_tty)
2360 check_trace_running (current_trace_status ());
2362 if (args == 0 || *args == 0)
2363 pc = regcache_read_pc (get_current_regcache ());
2365 pc = parse_and_eval_address (args);
2367 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
2370 /* tfind tracepoint command */
2372 tfind_tracepoint_command (const char *args, int from_tty)
2375 struct tracepoint *tp;
2377 check_trace_running (current_trace_status ());
2379 if (args == 0 || *args == 0)
2381 if (tracepoint_number == -1)
2382 error (_("No current tracepoint -- please supply an argument."));
2384 tdp = tracepoint_number; /* Default is current TDP. */
2387 tdp = parse_and_eval_long (args);
2389 /* If we have the tracepoint on hand, use the number that the
2390 target knows about (which may be different if we disconnected
2391 and reconnected). */
2392 tp = get_tracepoint (tdp);
2394 tdp = tp->number_on_target;
2396 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
2399 /* TFIND LINE command:
2401 This command will take a sourceline for argument, just like BREAK
2402 or TRACE (ie. anything that "decode_line_1" can handle).
2404 With no argument, this command will find the next trace frame
2405 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2408 tfind_line_command (const char *args, int from_tty)
2410 check_trace_running (current_trace_status ());
2412 symtab_and_line sal;
2413 if (args == 0 || *args == 0)
2415 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
2419 std::vector<symtab_and_line> sals
2420 = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
2424 if (sal.symtab == 0)
2425 error (_("No line number information available."));
2427 CORE_ADDR start_pc, end_pc;
2428 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2430 if (start_pc == end_pc)
2432 printf_filtered ("Line %d of \"%s\"",
2434 symtab_to_filename_for_display (sal.symtab));
2436 printf_filtered (" is at address ");
2437 print_address (get_current_arch (), start_pc, gdb_stdout);
2439 printf_filtered (" but contains no code.\n");
2440 sal = find_pc_line (start_pc, 0);
2442 && find_line_pc_range (sal, &start_pc, &end_pc)
2443 && start_pc != end_pc)
2444 printf_filtered ("Attempting to find line %d instead.\n",
2447 error (_("Cannot find a good line."));
2451 /* Is there any case in which we get here, and have an address
2452 which the user would want to see? If we have debugging
2453 symbols and no line numbers? */
2454 error (_("Line number %d is out of range for \"%s\"."),
2455 sal.line, symtab_to_filename_for_display (sal.symtab));
2457 /* Find within range of stated line. */
2459 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
2461 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
2464 /* tfind range command */
2466 tfind_range_command (const char *args, int from_tty)
2468 static CORE_ADDR start, stop;
2471 check_trace_running (current_trace_status ());
2473 if (args == 0 || *args == 0)
2474 { /* XXX FIXME: what should default behavior be? */
2475 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2479 if (0 != (tmp = strchr (args, ',')))
2481 std::string start_addr (args, tmp);
2483 tmp = skip_spaces (tmp);
2484 start = parse_and_eval_address (start_addr.c_str ());
2485 stop = parse_and_eval_address (tmp);
2488 { /* No explicit end address? */
2489 start = parse_and_eval_address (args);
2490 stop = start + 1; /* ??? */
2493 tfind_1 (tfind_range, 0, start, stop, from_tty);
2496 /* tfind outside command */
2498 tfind_outside_command (const char *args, int from_tty)
2500 CORE_ADDR start, stop;
2503 if (current_trace_status ()->running
2504 && current_trace_status ()->filename == NULL)
2505 error (_("May not look at trace frames while trace is running."));
2507 if (args == 0 || *args == 0)
2508 { /* XXX FIXME: what should default behavior be? */
2509 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2513 if (0 != (tmp = strchr (args, ',')))
2515 std::string start_addr (args, tmp);
2517 tmp = skip_spaces (tmp);
2518 start = parse_and_eval_address (start_addr.c_str ());
2519 stop = parse_and_eval_address (tmp);
2522 { /* No explicit end address? */
2523 start = parse_and_eval_address (args);
2524 stop = start + 1; /* ??? */
2527 tfind_1 (tfind_outside, 0, start, stop, from_tty);
2530 /* info scope command: list the locals for a scope. */
2532 info_scope_command (char *args_in, int from_tty)
2535 struct bound_minimal_symbol msym;
2536 const struct block *block;
2537 const char *symname;
2538 const char *save_args = args_in;
2539 struct block_iterator iter;
2541 struct gdbarch *gdbarch;
2543 const char *args = args_in;
2545 if (args == 0 || *args == 0)
2546 error (_("requires an argument (function, "
2547 "line or *addr) to define a scope"));
2549 event_location_up location = string_to_event_location (&args,
2551 std::vector<symtab_and_line> sals
2552 = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
2556 /* Presumably decode_line_1 has already warned. */
2560 /* Resolve line numbers to PC. */
2561 resolve_sal_pc (&sals[0]);
2562 block = block_for_pc (sals[0].pc);
2566 QUIT; /* Allow user to bail out with ^C. */
2567 ALL_BLOCK_SYMBOLS (block, iter, sym)
2569 QUIT; /* Allow user to bail out with ^C. */
2571 printf_filtered ("Scope for %s:\n", save_args);
2574 symname = SYMBOL_PRINT_NAME (sym);
2575 if (symname == NULL || *symname == '\0')
2576 continue; /* Probably botched, certainly useless. */
2578 gdbarch = symbol_arch (sym);
2580 printf_filtered ("Symbol %s is ", symname);
2582 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
2583 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym,
2584 BLOCK_START (block),
2588 switch (SYMBOL_CLASS (sym))
2591 case LOC_UNDEF: /* Messed up symbol? */
2592 printf_filtered ("a bogus symbol, class %d.\n",
2593 SYMBOL_CLASS (sym));
2594 count--; /* Don't count this one. */
2597 printf_filtered ("a constant with value %s (%s)",
2598 plongest (SYMBOL_VALUE (sym)),
2599 hex_string (SYMBOL_VALUE (sym)));
2601 case LOC_CONST_BYTES:
2602 printf_filtered ("constant bytes: ");
2603 if (SYMBOL_TYPE (sym))
2604 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2605 fprintf_filtered (gdb_stdout, " %02x",
2606 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2609 printf_filtered ("in static storage at address ");
2610 printf_filtered ("%s", paddress (gdbarch,
2611 SYMBOL_VALUE_ADDRESS (sym)));
2614 /* GDBARCH is the architecture associated with the objfile
2615 the symbol is defined in; the target architecture may be
2616 different, and may provide additional registers. However,
2617 we do not know the target architecture at this point.
2618 We assume the objfile architecture will contain all the
2619 standard registers that occur in debug info in that
2621 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2624 if (SYMBOL_IS_ARGUMENT (sym))
2625 printf_filtered ("an argument in register $%s",
2626 gdbarch_register_name (gdbarch, regno));
2628 printf_filtered ("a local variable in register $%s",
2629 gdbarch_register_name (gdbarch, regno));
2632 printf_filtered ("an argument at stack/frame offset %s",
2633 plongest (SYMBOL_VALUE (sym)));
2636 printf_filtered ("a local variable at frame offset %s",
2637 plongest (SYMBOL_VALUE (sym)));
2640 printf_filtered ("a reference argument at offset %s",
2641 plongest (SYMBOL_VALUE (sym)));
2643 case LOC_REGPARM_ADDR:
2644 /* Note comment at LOC_REGISTER. */
2645 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2647 printf_filtered ("the address of an argument, in register $%s",
2648 gdbarch_register_name (gdbarch, regno));
2651 printf_filtered ("a typedef.\n");
2654 printf_filtered ("a label at address ");
2655 printf_filtered ("%s", paddress (gdbarch,
2656 SYMBOL_VALUE_ADDRESS (sym)));
2659 printf_filtered ("a function at address ");
2660 printf_filtered ("%s",
2661 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
2663 case LOC_UNRESOLVED:
2664 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
2666 if (msym.minsym == NULL)
2667 printf_filtered ("Unresolved Static");
2670 printf_filtered ("static storage at address ");
2671 printf_filtered ("%s",
2673 BMSYMBOL_VALUE_ADDRESS (msym)));
2676 case LOC_OPTIMIZED_OUT:
2677 printf_filtered ("optimized out.\n");
2680 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
2683 if (SYMBOL_TYPE (sym))
2684 printf_filtered (", length %d.\n",
2685 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
2687 if (BLOCK_FUNCTION (block))
2690 block = BLOCK_SUPERBLOCK (block);
2693 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2697 /* Helper for trace_dump_command. Dump the action list starting at
2698 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2699 actions of the body of a while-stepping action. STEPPING_FRAME is
2700 set if the current traceframe was determined to be a while-stepping
2704 trace_dump_actions (struct command_line *action,
2705 int stepping_actions, int stepping_frame,
2708 const char *action_exp, *next_comma;
2710 for (; action != NULL; action = action->next)
2712 struct cmd_list_element *cmd;
2714 QUIT; /* Allow user to bail out with ^C. */
2715 action_exp = action->line;
2716 action_exp = skip_spaces (action_exp);
2718 /* The collection actions to be done while stepping are
2719 bracketed by the commands "while-stepping" and "end". */
2721 if (*action_exp == '#') /* comment line */
2724 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2726 error (_("Bad action list item: %s"), action_exp);
2728 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
2732 for (i = 0; i < action->body_count; ++i)
2733 trace_dump_actions (action->body_list[i],
2734 1, stepping_frame, from_tty);
2736 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
2738 /* Display the collected data.
2739 For the trap frame, display only what was collected at
2740 the trap. Likewise for stepping frames, display only
2741 what was collected while stepping. This means that the
2742 two boolean variables, STEPPING_FRAME and
2743 STEPPING_ACTIONS should be equal. */
2744 if (stepping_frame == stepping_actions)
2747 struct cleanup *old_chain
2748 = make_cleanup (free_current_contents, &cmd);
2749 int trace_string = 0;
2751 if (*action_exp == '/')
2752 action_exp = decode_agent_options (action_exp, &trace_string);
2755 { /* Repeat over a comma-separated list. */
2756 QUIT; /* Allow user to bail out with ^C. */
2757 if (*action_exp == ',')
2759 action_exp = skip_spaces (action_exp);
2761 next_comma = strchr (action_exp, ',');
2763 if (0 == strncasecmp (action_exp, "$reg", 4))
2764 registers_info (NULL, from_tty);
2765 else if (0 == strncasecmp (action_exp, "$_ret", 5))
2767 else if (0 == strncasecmp (action_exp, "$loc", 4))
2768 info_locals_command (NULL, from_tty);
2769 else if (0 == strncasecmp (action_exp, "$arg", 4))
2770 info_args_command (NULL, from_tty);
2773 if (next_comma != NULL)
2775 size_t len = next_comma - action_exp;
2777 cmd = (char *) xrealloc (cmd, len + 1);
2778 memcpy (cmd, action_exp, len);
2783 size_t len = strlen (action_exp);
2785 cmd = (char *) xrealloc (cmd, len + 1);
2786 memcpy (cmd, action_exp, len + 1);
2789 printf_filtered ("%s = ", cmd);
2790 output_command_const (cmd, from_tty);
2791 printf_filtered ("\n");
2793 action_exp = next_comma;
2795 while (action_exp && *action_exp == ',');
2797 do_cleanups (old_chain);
2803 /* Return bp_location of the tracepoint associated with the current
2804 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
2805 is a stepping traceframe. */
2807 struct bp_location *
2808 get_traceframe_location (int *stepping_frame_p)
2810 struct tracepoint *t;
2811 struct bp_location *tloc;
2812 struct regcache *regcache;
2814 if (tracepoint_number == -1)
2815 error (_("No current trace frame."));
2817 t = get_tracepoint (tracepoint_number);
2820 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2823 /* The current frame is a trap frame if the frame PC is equal to the
2824 tracepoint PC. If not, then the current frame was collected
2825 during single-stepping. */
2826 regcache = get_current_regcache ();
2828 /* If the traceframe's address matches any of the tracepoint's
2829 locations, assume it is a direct hit rather than a while-stepping
2830 frame. (FIXME this is not reliable, should record each frame's
2832 for (tloc = t->loc; tloc; tloc = tloc->next)
2833 if (tloc->address == regcache_read_pc (regcache))
2835 *stepping_frame_p = 0;
2839 /* If this is a stepping frame, we don't know which location
2840 triggered. The first is as good (or bad) a guess as any... */
2841 *stepping_frame_p = 1;
2845 /* Return all the actions, including default collect, of a tracepoint
2846 T. It constructs cleanups into the chain, and leaves the caller to
2847 handle them (call do_cleanups). */
2849 static struct command_line *
2850 all_tracepoint_actions_and_cleanup (struct breakpoint *t)
2852 struct command_line *actions;
2854 actions = breakpoint_commands (t);
2856 /* If there are default expressions to collect, make up a collect
2857 action and prepend to the action list to encode. Note that since
2858 validation is per-tracepoint (local var "xyz" might be valid for
2859 one tracepoint and not another, etc), we make up the action on
2860 the fly, and don't cache it. */
2861 if (*default_collect)
2863 struct command_line *default_collect_action;
2864 char *default_collect_line;
2866 default_collect_line = xstrprintf ("collect %s", default_collect);
2867 make_cleanup (xfree, default_collect_line);
2869 validate_actionline (default_collect_line, t);
2870 default_collect_action = XNEW (struct command_line);
2871 make_cleanup (xfree, default_collect_action);
2872 default_collect_action->next = actions;
2873 default_collect_action->line = default_collect_line;
2874 actions = default_collect_action;
2880 /* The tdump command. */
2883 tdump_command (char *args, int from_tty)
2885 int stepping_frame = 0;
2886 struct bp_location *loc;
2887 struct command_line *actions;
2889 /* This throws an error is not inspecting a trace frame. */
2890 loc = get_traceframe_location (&stepping_frame);
2892 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2893 tracepoint_number, traceframe_number);
2895 /* This command only makes sense for the current frame, not the
2897 scoped_restore_current_thread restore_thread;
2899 select_frame (get_current_frame ());
2901 actions = all_tracepoint_actions_and_cleanup (loc->owner);
2903 trace_dump_actions (actions, 0, stepping_frame, from_tty);
2906 /* Encode a piece of a tracepoint's source-level definition in a form
2907 that is suitable for both protocol and saving in files. */
2908 /* This version does not do multiple encodes for long strings; it should
2909 return an offset to the next piece to encode. FIXME */
2912 encode_source_string (int tpnum, ULONGEST addr,
2913 const char *srctype, const char *src,
2914 char *buf, int buf_size)
2916 if (80 + strlen (srctype) > buf_size)
2917 error (_("Buffer too small for source encoding"));
2918 sprintf (buf, "%x:%s:%s:%x:%x:",
2919 tpnum, phex_nz (addr, sizeof (addr)),
2920 srctype, 0, (int) strlen (src));
2921 if (strlen (buf) + strlen (src) * 2 >= buf_size)
2922 error (_("Source string too long for buffer"));
2923 bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src));
2927 /* Tell the target what to do with an ongoing tracing run if GDB
2928 disconnects for some reason. */
2931 set_disconnected_tracing (char *args, int from_tty,
2932 struct cmd_list_element *c)
2934 target_set_disconnected_tracing (disconnected_tracing);
2938 set_circular_trace_buffer (char *args, int from_tty,
2939 struct cmd_list_element *c)
2941 target_set_circular_trace_buffer (circular_trace_buffer);
2945 set_trace_buffer_size (char *args, int from_tty,
2946 struct cmd_list_element *c)
2948 target_set_trace_buffer_size (trace_buffer_size);
2952 set_trace_user (char *args, int from_tty,
2953 struct cmd_list_element *c)
2957 ret = target_set_trace_notes (trace_user, NULL, NULL);
2960 warning (_("Target does not support trace notes, user ignored"));
2964 set_trace_notes (char *args, int from_tty,
2965 struct cmd_list_element *c)
2969 ret = target_set_trace_notes (NULL, trace_notes, NULL);
2972 warning (_("Target does not support trace notes, note ignored"));
2976 set_trace_stop_notes (char *args, int from_tty,
2977 struct cmd_list_element *c)
2981 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes);
2984 warning (_("Target does not support trace notes, stop note ignored"));
2987 /* Convert the memory pointed to by mem into hex, placing result in buf.
2988 * Return a pointer to the last char put in buf (null)
2989 * "stolen" from sparc-stub.c
2992 static const char hexchars[] = "0123456789abcdef";
2995 mem2hex (gdb_byte *mem, char *buf, int count)
3003 *buf++ = hexchars[ch >> 4];
3004 *buf++ = hexchars[ch & 0xf];
3013 get_traceframe_number (void)
3015 return traceframe_number;
3019 get_tracepoint_number (void)
3021 return tracepoint_number;
3024 /* Make the traceframe NUM be the current trace frame. Does nothing
3025 if NUM is already current. */
3028 set_current_traceframe (int num)
3032 if (traceframe_number == num)
3034 /* Nothing to do. */
3038 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
3041 warning (_("could not change traceframe"));
3043 set_traceframe_num (newnum);
3045 /* Changing the traceframe changes our view of registers and of the
3047 registers_changed ();
3049 clear_traceframe_info ();
3052 /* A cleanup used when switching away and back from tfind mode. */
3054 struct current_traceframe_cleanup
3056 /* The traceframe we were inspecting. */
3057 int traceframe_number;
3061 do_restore_current_traceframe_cleanup (void *arg)
3063 struct current_traceframe_cleanup *old
3064 = (struct current_traceframe_cleanup *) arg;
3066 set_current_traceframe (old->traceframe_number);
3070 restore_current_traceframe_cleanup_dtor (void *arg)
3072 struct current_traceframe_cleanup *old
3073 = (struct current_traceframe_cleanup *) arg;
3079 make_cleanup_restore_current_traceframe (void)
3081 struct current_traceframe_cleanup *old =
3082 XNEW (struct current_traceframe_cleanup);
3084 old->traceframe_number = traceframe_number;
3086 return make_cleanup_dtor (do_restore_current_traceframe_cleanup, old,
3087 restore_current_traceframe_cleanup_dtor);
3090 /* Given a number and address, return an uploaded tracepoint with that
3091 number, creating if necessary. */
3093 struct uploaded_tp *
3094 get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
3096 struct uploaded_tp *utp;
3098 for (utp = *utpp; utp; utp = utp->next)
3099 if (utp->number == num && utp->addr == addr)
3102 utp = XCNEW (struct uploaded_tp);
3105 utp->actions = NULL;
3106 utp->step_actions = NULL;
3107 utp->cmd_strings = NULL;
3115 free_uploaded_tps (struct uploaded_tp **utpp)
3117 struct uploaded_tp *next_one;
3121 next_one = (*utpp)->next;
3127 /* Given a number and address, return an uploaded tracepoint with that
3128 number, creating if necessary. */
3130 struct uploaded_tsv *
3131 get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
3133 struct uploaded_tsv *utsv;
3135 for (utsv = *utsvp; utsv; utsv = utsv->next)
3136 if (utsv->number == num)
3139 utsv = XCNEW (struct uploaded_tsv);
3141 utsv->next = *utsvp;
3148 free_uploaded_tsvs (struct uploaded_tsv **utsvp)
3150 struct uploaded_tsv *next_one;
3154 next_one = (*utsvp)->next;
3160 /* FIXME this function is heuristic and will miss the cases where the
3161 conditional is semantically identical but differs in whitespace,
3162 such as "x == 0" vs "x==0". */
3165 cond_string_is_same (char *str1, char *str2)
3167 if (str1 == NULL || str2 == NULL)
3168 return (str1 == str2);
3170 return (strcmp (str1, str2) == 0);
3173 /* Look for an existing tracepoint that seems similar enough to the
3174 uploaded one. Enablement isn't compared, because the user can
3175 toggle that freely, and may have done so in anticipation of the
3176 next trace run. Return the location of matched tracepoint. */
3178 static struct bp_location *
3179 find_matching_tracepoint_location (struct uploaded_tp *utp)
3181 VEC(breakpoint_p) *tp_vec = all_tracepoints ();
3183 struct breakpoint *b;
3184 struct bp_location *loc;
3186 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
3188 struct tracepoint *t = (struct tracepoint *) b;
3190 if (b->type == utp->type
3191 && t->step_count == utp->step
3192 && t->pass_count == utp->pass
3193 && cond_string_is_same (t->cond_string, utp->cond_string)
3194 /* FIXME also test actions. */
3197 /* Scan the locations for an address match. */
3198 for (loc = b->loc; loc; loc = loc->next)
3200 if (loc->address == utp->addr)
3208 /* Given a list of tracepoints uploaded from a target, attempt to
3209 match them up with existing tracepoints, and create new ones if not
3213 merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
3215 struct uploaded_tp *utp;
3216 /* A set of tracepoints which are modified. */
3217 VEC(breakpoint_p) *modified_tp = NULL;
3219 struct breakpoint *b;
3221 /* Look for GDB tracepoints that match up with our uploaded versions. */
3222 for (utp = *uploaded_tps; utp; utp = utp->next)
3224 struct bp_location *loc;
3225 struct tracepoint *t;
3227 loc = find_matching_tracepoint_location (utp);
3232 /* Mark this location as already inserted. */
3234 t = (struct tracepoint *) loc->owner;
3235 printf_filtered (_("Assuming tracepoint %d is same "
3236 "as target's tracepoint %d at %s.\n"),
3237 loc->owner->number, utp->number,
3238 paddress (loc->gdbarch, utp->addr));
3240 /* The tracepoint LOC->owner was modified (the location LOC
3241 was marked as inserted in the target). Save it in
3242 MODIFIED_TP if not there yet. The 'breakpoint-modified'
3243 observers will be notified later once for each tracepoint
3244 saved in MODIFIED_TP. */
3246 VEC_iterate (breakpoint_p, modified_tp, ix, b);
3248 if (b == loc->owner)
3254 VEC_safe_push (breakpoint_p, modified_tp, loc->owner);
3258 t = create_tracepoint_from_upload (utp);
3260 printf_filtered (_("Created tracepoint %d for "
3261 "target's tracepoint %d at %s.\n"),
3262 t->number, utp->number,
3263 paddress (get_current_arch (), utp->addr));
3265 printf_filtered (_("Failed to create tracepoint for target's "
3266 "tracepoint %d at %s, skipping it.\n"),
3268 paddress (get_current_arch (), utp->addr));
3270 /* Whether found or created, record the number used by the
3271 target, to help with mapping target tracepoints back to their
3272 counterparts here. */
3274 t->number_on_target = utp->number;
3277 /* Notify 'breakpoint-modified' observer that at least one of B's
3278 locations was changed. */
3279 for (ix = 0; VEC_iterate (breakpoint_p, modified_tp, ix, b); ix++)
3280 observer_notify_breakpoint_modified (b);
3282 VEC_free (breakpoint_p, modified_tp);
3283 free_uploaded_tps (uploaded_tps);
3286 /* Trace state variables don't have much to identify them beyond their
3287 name, so just use that to detect matches. */
3289 static struct trace_state_variable *
3290 find_matching_tsv (struct uploaded_tsv *utsv)
3295 return find_trace_state_variable (utsv->name);
3298 static struct trace_state_variable *
3299 create_tsv_from_upload (struct uploaded_tsv *utsv)
3301 const char *namebase;
3304 struct trace_state_variable *tsv;
3305 struct cleanup *old_chain;
3309 namebase = utsv->name;
3315 buf = string_printf ("%s_%d", namebase, try_num++);
3318 /* Fish for a name that is not in use. */
3319 /* (should check against all internal vars?) */
3320 while (find_trace_state_variable (buf.c_str ()))
3321 buf = string_printf ("%s_%d", namebase, try_num++);
3323 /* We have an available name, create the variable. */
3324 tsv = create_trace_state_variable (buf.c_str ());
3325 tsv->initial_value = utsv->initial_value;
3326 tsv->builtin = utsv->builtin;
3328 observer_notify_tsv_created (tsv);
3333 /* Given a list of uploaded trace state variables, try to match them
3334 up with existing variables, or create additional ones. */
3337 merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
3340 struct uploaded_tsv *utsv;
3341 struct trace_state_variable *tsv;
3344 /* Most likely some numbers will have to be reassigned as part of
3345 the merge, so clear them all in anticipation. */
3346 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3349 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
3351 tsv = find_matching_tsv (utsv);
3355 printf_filtered (_("Assuming trace state variable $%s "
3356 "is same as target's variable %d.\n"),
3357 tsv->name, utsv->number);
3361 tsv = create_tsv_from_upload (utsv);
3363 printf_filtered (_("Created trace state variable "
3364 "$%s for target's variable %d.\n"),
3365 tsv->name, utsv->number);
3367 /* Give precedence to numberings that come from the target. */
3369 tsv->number = utsv->number;
3372 /* Renumber everything that didn't get a target-assigned number. */
3374 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3375 if (tsv->number > highest)
3376 highest = tsv->number;
3379 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3380 if (tsv->number == 0)
3381 tsv->number = highest++;
3383 free_uploaded_tsvs (uploaded_tsvs);
3386 /* Parse the part of trace status syntax that is shared between
3387 the remote protocol and the trace file reader. */
3390 parse_trace_status (const char *line, struct trace_status *ts)
3392 const char *p = line, *p1, *p2, *p3, *p_temp;
3396 ts->running_known = 1;
3397 ts->running = (*p++ == '1');
3398 ts->stop_reason = trace_stop_reason_unknown;
3399 xfree (ts->stop_desc);
3400 ts->stop_desc = NULL;
3401 ts->traceframe_count = -1;
3402 ts->traceframes_created = -1;
3403 ts->buffer_free = -1;
3404 ts->buffer_size = -1;
3405 ts->disconnected_tracing = 0;
3406 ts->circular_buffer = 0;
3407 xfree (ts->user_name);
3408 ts->user_name = NULL;
3411 ts->start_time = ts->stop_time = 0;
3415 p1 = strchr (p, ':');
3417 error (_("Malformed trace status, at %s\n\
3418 Status line: '%s'\n"), p, line);
3419 p3 = strchr (p, ';');
3421 p3 = p + strlen (p);
3422 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
3424 p = unpack_varlen_hex (++p1, &val);
3425 ts->stop_reason = trace_buffer_full;
3427 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
3429 p = unpack_varlen_hex (++p1, &val);
3430 ts->stop_reason = trace_never_run;
3432 else if (strncmp (p, stop_reason_names[tracepoint_passcount],
3435 p = unpack_varlen_hex (++p1, &val);
3436 ts->stop_reason = tracepoint_passcount;
3437 ts->stopping_tracepoint = val;
3439 else if (strncmp (p, stop_reason_names[trace_stop_command], p1 - p) == 0)
3441 p2 = strchr (++p1, ':');
3449 ts->stop_desc = (char *) xmalloc (strlen (line));
3450 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3451 ts->stop_desc[end] = '\0';
3454 ts->stop_desc = xstrdup ("");
3456 p = unpack_varlen_hex (++p2, &val);
3457 ts->stop_reason = trace_stop_command;
3459 else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0)
3461 p = unpack_varlen_hex (++p1, &val);
3462 ts->stop_reason = trace_disconnected;
3464 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
3466 p2 = strchr (++p1, ':');
3469 ts->stop_desc = (char *) xmalloc ((p2 - p1) / 2 + 1);
3470 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3471 ts->stop_desc[end] = '\0';
3474 ts->stop_desc = xstrdup ("");
3476 p = unpack_varlen_hex (++p2, &val);
3477 ts->stopping_tracepoint = val;
3478 ts->stop_reason = tracepoint_error;
3480 else if (strncmp (p, "tframes", p1 - p) == 0)
3482 p = unpack_varlen_hex (++p1, &val);
3483 ts->traceframe_count = val;
3485 else if (strncmp (p, "tcreated", p1 - p) == 0)
3487 p = unpack_varlen_hex (++p1, &val);
3488 ts->traceframes_created = val;
3490 else if (strncmp (p, "tfree", p1 - p) == 0)
3492 p = unpack_varlen_hex (++p1, &val);
3493 ts->buffer_free = val;
3495 else if (strncmp (p, "tsize", p1 - p) == 0)
3497 p = unpack_varlen_hex (++p1, &val);
3498 ts->buffer_size = val;
3500 else if (strncmp (p, "disconn", p1 - p) == 0)
3502 p = unpack_varlen_hex (++p1, &val);
3503 ts->disconnected_tracing = val;
3505 else if (strncmp (p, "circular", p1 - p) == 0)
3507 p = unpack_varlen_hex (++p1, &val);
3508 ts->circular_buffer = val;
3510 else if (strncmp (p, "starttime", p1 - p) == 0)
3512 p = unpack_varlen_hex (++p1, &val);
3513 ts->start_time = val;
3515 else if (strncmp (p, "stoptime", p1 - p) == 0)
3517 p = unpack_varlen_hex (++p1, &val);
3518 ts->stop_time = val;
3520 else if (strncmp (p, "username", p1 - p) == 0)
3523 ts->user_name = (char *) xmalloc (strlen (p) / 2);
3524 end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2);
3525 ts->user_name[end] = '\0';
3528 else if (strncmp (p, "notes", p1 - p) == 0)
3531 ts->notes = (char *) xmalloc (strlen (p) / 2);
3532 end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2);
3533 ts->notes[end] = '\0';
3538 /* Silently skip unknown optional info. */
3539 p_temp = strchr (p1 + 1, ';');
3543 /* Must be at the end. */
3550 parse_tracepoint_status (const char *p, struct breakpoint *bp,
3551 struct uploaded_tp *utp)
3554 struct tracepoint *tp = (struct tracepoint *) bp;
3556 p = unpack_varlen_hex (p, &uval);
3558 tp->hit_count += uval;
3560 utp->hit_count += uval;
3561 p = unpack_varlen_hex (p + 1, &uval);
3563 tp->traceframe_usage += uval;
3565 utp->traceframe_usage += uval;
3566 /* Ignore any extra, allowing for future extensions. */
3569 /* Given a line of text defining a part of a tracepoint, parse it into
3570 an "uploaded tracepoint". */
3573 parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
3577 ULONGEST num, addr, step, pass, orig_size, xlen, start;
3580 const char *srctype;
3582 struct uploaded_tp *utp = NULL;
3585 /* Both tracepoint and action definitions start with the same number
3586 and address sequence. */
3588 p = unpack_varlen_hex (p, &num);
3589 p++; /* skip a colon */
3590 p = unpack_varlen_hex (p, &addr);
3591 p++; /* skip a colon */
3594 enabled = (*p++ == 'E');
3595 p++; /* skip a colon */
3596 p = unpack_varlen_hex (p, &step);
3597 p++; /* skip a colon */
3598 p = unpack_varlen_hex (p, &pass);
3599 type = bp_tracepoint;
3601 /* Thumb through optional fields. */
3604 p++; /* skip a colon */
3607 type = bp_fast_tracepoint;
3609 p = unpack_varlen_hex (p, &orig_size);
3613 type = bp_static_tracepoint;
3619 p = unpack_varlen_hex (p, &xlen);
3620 p++; /* skip a comma */
3621 cond = (char *) xmalloc (2 * xlen + 1);
3622 strncpy (cond, p, 2 * xlen);
3623 cond[2 * xlen] = '\0';
3627 warning (_("Unrecognized char '%c' in tracepoint "
3628 "definition, skipping rest"), *p);
3630 utp = get_uploaded_tp (num, addr, utpp);
3632 utp->enabled = enabled;
3637 else if (piece == 'A')
3639 utp = get_uploaded_tp (num, addr, utpp);
3640 VEC_safe_push (char_ptr, utp->actions, xstrdup (p));
3642 else if (piece == 'S')
3644 utp = get_uploaded_tp (num, addr, utpp);
3645 VEC_safe_push (char_ptr, utp->step_actions, xstrdup (p));
3647 else if (piece == 'Z')
3649 /* Parse a chunk of source form definition. */
3650 utp = get_uploaded_tp (num, addr, utpp);
3652 p = strchr (p, ':');
3653 p++; /* skip a colon */
3654 p = unpack_varlen_hex (p, &start);
3655 p++; /* skip a colon */
3656 p = unpack_varlen_hex (p, &xlen);
3657 p++; /* skip a colon */
3659 buf = (char *) alloca (strlen (line));
3661 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3664 if (startswith (srctype, "at:"))
3665 utp->at_string = xstrdup (buf);
3666 else if (startswith (srctype, "cond:"))
3667 utp->cond_string = xstrdup (buf);
3668 else if (startswith (srctype, "cmd:"))
3669 VEC_safe_push (char_ptr, utp->cmd_strings, xstrdup (buf));
3671 else if (piece == 'V')
3673 utp = get_uploaded_tp (num, addr, utpp);
3675 parse_tracepoint_status (p, NULL, utp);
3679 /* Don't error out, the target might be sending us optional
3680 info that we don't care about. */
3681 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
3685 /* Convert a textual description of a trace state variable into an
3689 parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp)
3693 ULONGEST num, initval, builtin;
3695 struct uploaded_tsv *utsv = NULL;
3697 buf = (char *) alloca (strlen (line));
3700 p = unpack_varlen_hex (p, &num);
3701 p++; /* skip a colon */
3702 p = unpack_varlen_hex (p, &initval);
3703 p++; /* skip a colon */
3704 p = unpack_varlen_hex (p, &builtin);
3705 p++; /* skip a colon */
3706 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3709 utsv = get_uploaded_tsv (num, utsvp);
3710 utsv->initial_value = initval;
3711 utsv->builtin = builtin;
3712 utsv->name = xstrdup (buf);
3716 free_current_marker (void *arg)
3718 struct static_tracepoint_marker **marker_p
3719 = (struct static_tracepoint_marker **) arg;
3721 if (*marker_p != NULL)
3723 release_static_tracepoint_marker (*marker_p);
3730 /* Given a line of text defining a static tracepoint marker, parse it
3731 into a "static tracepoint marker" object. Throws an error is
3732 parsing fails. If PP is non-null, it points to one past the end of
3733 the parsed marker definition. */
3736 parse_static_tracepoint_marker_definition (const char *line, const char **pp,
3737 struct static_tracepoint_marker *marker)
3739 const char *p, *endp;
3744 p = unpack_varlen_hex (p, &addr);
3745 p++; /* skip a colon */
3747 marker->gdbarch = target_gdbarch ();
3748 marker->address = (CORE_ADDR) addr;
3750 endp = strchr (p, ':');
3752 error (_("bad marker definition: %s"), line);
3754 marker->str_id = (char *) xmalloc (endp - p + 1);
3755 end = hex2bin (p, (gdb_byte *) marker->str_id, (endp - p + 1) / 2);
3756 marker->str_id[end] = '\0';
3759 p++; /* skip a colon */
3761 marker->extra = (char *) xmalloc (strlen (p) + 1);
3762 end = hex2bin (p, (gdb_byte *) marker->extra, strlen (p) / 2);
3763 marker->extra[end] = '\0';
3769 /* Release a static tracepoint marker's contents. Note that the
3770 object itself isn't released here. There objects are usually on
3774 release_static_tracepoint_marker (struct static_tracepoint_marker *marker)
3776 xfree (marker->str_id);
3777 marker->str_id = NULL;
3780 /* Print MARKER to gdb_stdout. */
3783 print_one_static_tracepoint_marker (int count,
3784 struct static_tracepoint_marker *marker)
3788 char wrap_indent[80];
3789 char extra_field_indent[80];
3790 struct ui_out *uiout = current_uiout;
3791 VEC(breakpoint_p) *tracepoints;
3793 symtab_and_line sal;
3794 sal.pc = marker->address;
3796 tracepoints = static_tracepoints_here (marker->address);
3798 ui_out_emit_tuple tuple_emitter (uiout, "marker");
3800 /* A counter field to help readability. This is not a stable
3802 uiout->field_int ("count", count);
3804 uiout->field_string ("marker-id", marker->str_id);
3806 uiout->field_fmt ("enabled", "%c",
3807 !VEC_empty (breakpoint_p, tracepoints) ? 'y' : 'n');
3810 strcpy (wrap_indent, " ");
3812 if (gdbarch_addr_bit (marker->gdbarch) <= 32)
3813 strcat (wrap_indent, " ");
3815 strcat (wrap_indent, " ");
3817 strcpy (extra_field_indent, " ");
3819 uiout->field_core_addr ("addr", marker->gdbarch, marker->address);
3821 sal = find_pc_line (marker->address, 0);
3822 sym = find_pc_sect_function (marker->address, NULL);
3825 uiout->text ("in ");
3826 uiout->field_string ("func",
3827 SYMBOL_PRINT_NAME (sym));
3828 uiout->wrap_hint (wrap_indent);
3829 uiout->text (" at ");
3832 uiout->field_skip ("func");
3834 if (sal.symtab != NULL)
3836 uiout->field_string ("file",
3837 symtab_to_filename_for_display (sal.symtab));
3840 if (uiout->is_mi_like_p ())
3842 const char *fullname = symtab_to_fullname (sal.symtab);
3844 uiout->field_string ("fullname", fullname);
3847 uiout->field_skip ("fullname");
3849 uiout->field_int ("line", sal.line);
3853 uiout->field_skip ("fullname");
3854 uiout->field_skip ("line");
3858 uiout->text (extra_field_indent);
3859 uiout->text (_("Data: \""));
3860 uiout->field_string ("extra-data", marker->extra);
3861 uiout->text ("\"\n");
3863 if (!VEC_empty (breakpoint_p, tracepoints))
3866 struct breakpoint *b;
3869 ui_out_emit_tuple tuple_emitter (uiout, "tracepoints-at");
3871 uiout->text (extra_field_indent);
3872 uiout->text (_("Probed by static tracepoints: "));
3873 for (ix = 0; VEC_iterate(breakpoint_p, tracepoints, ix, b); ix++)
3878 uiout->field_int ("tracepoint-id", b->number);
3882 if (uiout->is_mi_like_p ())
3883 uiout->field_int ("number-of-tracepoints",
3884 VEC_length(breakpoint_p, tracepoints));
3888 VEC_free (breakpoint_p, tracepoints);
3892 info_static_tracepoint_markers_command (char *arg, int from_tty)
3894 VEC(static_tracepoint_marker_p) *markers;
3895 struct cleanup *old_chain;
3896 struct static_tracepoint_marker *marker;
3897 struct ui_out *uiout = current_uiout;
3900 /* We don't have to check target_can_use_agent and agent's capability on
3901 static tracepoint here, in order to be compatible with older GDBserver.
3902 We don't check USE_AGENT is true or not, because static tracepoints
3903 don't work without in-process agent, so we don't bother users to type
3904 `set agent on' when to use static tracepoint. */
3906 ui_out_emit_table table_emitter (uiout, 5, -1,
3907 "StaticTracepointMarkersTable");
3909 uiout->table_header (7, ui_left, "counter", "Cnt");
3911 uiout->table_header (40, ui_left, "marker-id", "ID");
3913 uiout->table_header (3, ui_left, "enabled", "Enb");
3914 if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
3915 uiout->table_header (10, ui_left, "addr", "Address");
3917 uiout->table_header (18, ui_left, "addr", "Address");
3918 uiout->table_header (40, ui_noalign, "what", "What");
3920 uiout->table_body ();
3922 markers = target_static_tracepoint_markers_by_strid (NULL);
3923 old_chain = make_cleanup (VEC_cleanup (static_tracepoint_marker_p), &markers);
3926 VEC_iterate (static_tracepoint_marker_p,
3927 markers, i, marker);
3930 print_one_static_tracepoint_marker (i + 1, marker);
3931 release_static_tracepoint_marker (marker);
3934 do_cleanups (old_chain);
3937 /* The $_sdata convenience variable is a bit special. We don't know
3938 for sure type of the value until we actually have a chance to fetch
3939 the data --- the size of the object depends on what has been
3940 collected. We solve this by making $_sdata be an internalvar that
3941 creates a new value on access. */
3943 /* Return a new value with the correct type for the sdata object of
3944 the current trace frame. Return a void value if there's no object
3947 static struct value *
3948 sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var,
3954 /* We need to read the whole object before we know its size. */
3955 size = target_read_alloc (¤t_target,
3956 TARGET_OBJECT_STATIC_TRACE_DATA,
3963 type = init_vector_type (builtin_type (gdbarch)->builtin_true_char,
3965 v = allocate_value (type);
3966 memcpy (value_contents_raw (v), buf, size);
3971 return allocate_value (builtin_type (gdbarch)->builtin_void);
3974 #if !defined(HAVE_LIBEXPAT)
3976 struct traceframe_info *
3977 parse_traceframe_info (const char *tframe_info)
3979 static int have_warned;
3984 warning (_("Can not parse XML trace frame info; XML support "
3985 "was disabled at compile time"));
3991 #else /* HAVE_LIBEXPAT */
3993 #include "xml-support.h"
3995 /* Handle the start of a <memory> element. */
3998 traceframe_info_start_memory (struct gdb_xml_parser *parser,
3999 const struct gdb_xml_element *element,
4000 void *user_data, VEC(gdb_xml_value_s) *attributes)
4002 struct traceframe_info *info = (struct traceframe_info *) user_data;
4003 struct mem_range *r = VEC_safe_push (mem_range_s, info->memory, NULL);
4004 ULONGEST *start_p, *length_p;
4007 = (ULONGEST *) xml_find_attribute (attributes, "start")->value;
4009 = (ULONGEST *) xml_find_attribute (attributes, "length")->value;
4011 r->start = *start_p;
4012 r->length = *length_p;
4015 /* Handle the start of a <tvar> element. */
4018 traceframe_info_start_tvar (struct gdb_xml_parser *parser,
4019 const struct gdb_xml_element *element,
4021 VEC(gdb_xml_value_s) *attributes)
4023 struct traceframe_info *info = (struct traceframe_info *) user_data;
4024 const char *id_attrib
4025 = (const char *) xml_find_attribute (attributes, "id")->value;
4026 int id = gdb_xml_parse_ulongest (parser, id_attrib);
4028 VEC_safe_push (int, info->tvars, id);
4031 /* Discard the constructed trace frame info (if an error occurs). */
4034 free_result (void *p)
4036 struct traceframe_info *result = (struct traceframe_info *) p;
4038 free_traceframe_info (result);
4041 /* The allowed elements and attributes for an XML memory map. */
4043 static const struct gdb_xml_attribute memory_attributes[] = {
4044 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
4045 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
4046 { NULL, GDB_XML_AF_NONE, NULL, NULL }
4049 static const struct gdb_xml_attribute tvar_attributes[] = {
4050 { "id", GDB_XML_AF_NONE, NULL, NULL },
4051 { NULL, GDB_XML_AF_NONE, NULL, NULL }
4054 static const struct gdb_xml_element traceframe_info_children[] = {
4055 { "memory", memory_attributes, NULL,
4056 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
4057 traceframe_info_start_memory, NULL },
4058 { "tvar", tvar_attributes, NULL,
4059 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
4060 traceframe_info_start_tvar, NULL },
4061 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
4064 static const struct gdb_xml_element traceframe_info_elements[] = {
4065 { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE,
4067 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
4070 /* Parse a traceframe-info XML document. */
4072 struct traceframe_info *
4073 parse_traceframe_info (const char *tframe_info)
4075 struct traceframe_info *result;
4076 struct cleanup *back_to;
4078 result = XCNEW (struct traceframe_info);
4079 back_to = make_cleanup (free_result, result);
4081 if (gdb_xml_parse_quick (_("trace frame info"),
4082 "traceframe-info.dtd", traceframe_info_elements,
4083 tframe_info, result) == 0)
4085 /* Parsed successfully, keep the result. */
4086 discard_cleanups (back_to);
4091 do_cleanups (back_to);
4095 #endif /* HAVE_LIBEXPAT */
4097 /* Returns the traceframe_info object for the current traceframe.
4098 This is where we avoid re-fetching the object from the target if we
4099 already have it cached. */
4101 struct traceframe_info *
4102 get_traceframe_info (void)
4104 if (traceframe_info == NULL)
4105 traceframe_info = target_traceframe_info ();
4107 return traceframe_info;
4110 /* If the target supports the query, return in RESULT the set of
4111 collected memory in the current traceframe, found within the LEN
4112 bytes range starting at MEMADDR. Returns true if the target
4113 supports the query, otherwise returns false, and RESULT is left
4117 traceframe_available_memory (VEC(mem_range_s) **result,
4118 CORE_ADDR memaddr, ULONGEST len)
4120 struct traceframe_info *info = get_traceframe_info ();
4124 struct mem_range *r;
4129 for (i = 0; VEC_iterate (mem_range_s, info->memory, i, r); i++)
4130 if (mem_ranges_overlap (r->start, r->length, memaddr, len))
4132 ULONGEST lo1, hi1, lo2, hi2;
4133 struct mem_range *nr;
4136 hi1 = memaddr + len;
4139 hi2 = r->start + r->length;
4141 nr = VEC_safe_push (mem_range_s, *result, NULL);
4143 nr->start = std::max (lo1, lo2);
4144 nr->length = std::min (hi1, hi2) - nr->start;
4147 normalize_mem_ranges (*result);
4154 /* Implementation of `sdata' variable. */
4156 static const struct internalvar_funcs sdata_funcs =
4163 /* module initialization */
4165 _initialize_tracepoint (void)
4167 struct cmd_list_element *c;
4169 /* Explicitly create without lookup, since that tries to create a
4170 value with a void typed value, and when we get here, gdbarch
4171 isn't initialized yet. At this point, we're quite sure there
4172 isn't another convenience variable of the same name. */
4173 create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL);
4175 traceframe_number = -1;
4176 tracepoint_number = -1;
4178 add_info ("scope", info_scope_command,
4179 _("List the variables local to a scope"));
4181 add_cmd ("tracepoints", class_trace,
4182 _("Tracing of program execution without stopping the program."),
4185 add_com ("tdump", class_trace, tdump_command,
4186 _("Print everything collected at the current tracepoint."));
4188 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
4189 Define a trace state variable.\n\
4190 Argument is a $-prefixed name, optionally followed\n\
4191 by '=' and an expression that sets the initial value\n\
4192 at the start of tracing."));
4193 set_cmd_completer (c, expression_completer);
4195 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
4196 Delete one or more trace state variables.\n\
4197 Arguments are the names of the variables to delete.\n\
4198 If no arguments are supplied, delete all variables."), &deletelist);
4199 /* FIXME add a trace variable completer. */
4201 add_info ("tvariables", info_tvariables_command, _("\
4202 Status of trace state variables and their values.\n\
4205 add_info ("static-tracepoint-markers",
4206 info_static_tracepoint_markers_command, _("\
4207 List target static tracepoints markers.\n\
4210 add_prefix_cmd ("tfind", class_trace, tfind_command, _("\
4211 Select a trace frame;\n\
4212 No argument means forward by one frame; '-' means backward by one frame."),
4213 &tfindlist, "tfind ", 1, &cmdlist);
4215 add_cmd ("outside", class_trace, tfind_outside_command, _("\
4216 Select a trace frame whose PC is outside the given range (exclusive).\n\
4217 Usage: tfind outside addr1, addr2"),
4220 add_cmd ("range", class_trace, tfind_range_command, _("\
4221 Select a trace frame whose PC is in the given range (inclusive).\n\
4222 Usage: tfind range addr1,addr2"),
4225 add_cmd ("line", class_trace, tfind_line_command, _("\
4226 Select a trace frame by source line.\n\
4227 Argument can be a line number (with optional source file),\n\
4228 a function name, or '*' followed by an address.\n\
4229 Default argument is 'the next source line that was traced'."),
4232 add_cmd ("tracepoint", class_trace, tfind_tracepoint_command, _("\
4233 Select a trace frame by tracepoint number.\n\
4234 Default is the tracepoint for the current trace frame."),
4237 add_cmd ("pc", class_trace, tfind_pc_command, _("\
4238 Select a trace frame by PC.\n\
4239 Default is the current PC, or the PC of the current trace frame."),
4242 add_cmd ("end", class_trace, tfind_end_command, _("\
4243 De-select any trace frame and resume 'live' debugging."),
4246 add_alias_cmd ("none", "end", class_trace, 0, &tfindlist);
4248 add_cmd ("start", class_trace, tfind_start_command,
4249 _("Select the first trace frame in the trace buffer."),
4252 add_com ("tstatus", class_trace, tstatus_command,
4253 _("Display the status of the current trace data collection."));
4255 add_com ("tstop", class_trace, tstop_command, _("\
4256 Stop trace data collection.\n\
4257 Usage: tstop [ <notes> ... ]\n\
4258 Any arguments supplied are recorded with the trace as a stop reason and\n\
4259 reported by tstatus (if the target supports trace notes)."));
4261 add_com ("tstart", class_trace, tstart_command, _("\
4262 Start trace data collection.\n\
4263 Usage: tstart [ <notes> ... ]\n\
4264 Any arguments supplied are recorded with the trace as a note and\n\
4265 reported by tstatus (if the target supports trace notes)."));
4267 add_com ("end", class_trace, end_actions_pseudocommand, _("\
4268 Ends a list of commands or actions.\n\
4269 Several GDB commands allow you to enter a list of commands or actions.\n\
4270 Entering \"end\" on a line by itself is the normal way to terminate\n\
4272 Note: the \"end\" command cannot be used at the gdb prompt."));
4274 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
4275 Specify single-stepping behavior at a tracepoint.\n\
4276 Argument is number of instructions to trace in single-step mode\n\
4277 following the tracepoint. This command is normally followed by\n\
4278 one or more \"collect\" commands, to specify what to collect\n\
4279 while single-stepping.\n\n\
4280 Note: this command can only be used in a tracepoint \"actions\" list."));
4282 add_com_alias ("ws", "while-stepping", class_alias, 0);
4283 add_com_alias ("stepping", "while-stepping", class_alias, 0);
4285 add_com ("collect", class_trace, collect_pseudocommand, _("\
4286 Specify one or more data items to be collected at a tracepoint.\n\
4287 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
4288 collect all data (variables, registers) referenced by that expression.\n\
4289 Also accepts the following special arguments:\n\
4290 $regs -- all registers.\n\
4291 $args -- all function arguments.\n\
4292 $locals -- all variables local to the block/function scope.\n\
4293 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
4294 Note: this command can only be used in a tracepoint \"actions\" list."));
4296 add_com ("teval", class_trace, teval_pseudocommand, _("\
4297 Specify one or more expressions to be evaluated at a tracepoint.\n\
4298 Accepts a comma-separated list of (one or more) expressions.\n\
4299 The result of each evaluation will be discarded.\n\
4300 Note: this command can only be used in a tracepoint \"actions\" list."));
4302 add_com ("actions", class_trace, actions_command, _("\
4303 Specify the actions to be taken at a tracepoint.\n\
4304 Tracepoint actions may include collecting of specified data,\n\
4305 single-stepping, or enabling/disabling other tracepoints,\n\
4306 depending on target's capabilities."));
4308 default_collect = xstrdup ("");
4309 add_setshow_string_cmd ("default-collect", class_trace,
4310 &default_collect, _("\
4311 Set the list of expressions to collect by default"), _("\
4312 Show the list of expressions to collect by default"), NULL,
4314 &setlist, &showlist);
4316 add_setshow_boolean_cmd ("disconnected-tracing", no_class,
4317 &disconnected_tracing, _("\
4318 Set whether tracing continues after GDB disconnects."), _("\
4319 Show whether tracing continues after GDB disconnects."), _("\
4320 Use this to continue a tracing run even if GDB disconnects\n\
4321 or detaches from the target. You can reconnect later and look at\n\
4322 trace data collected in the meantime."),
4323 set_disconnected_tracing,
4328 add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
4329 &circular_trace_buffer, _("\
4330 Set target's use of circular trace buffer."), _("\
4331 Show target's use of circular trace buffer."), _("\
4332 Use this to make the trace buffer into a circular buffer,\n\
4333 which will discard traceframes (oldest first) instead of filling\n\
4334 up and stopping the trace run."),
4335 set_circular_trace_buffer,
4340 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class,
4341 &trace_buffer_size, _("\
4342 Set requested size of trace buffer."), _("\
4343 Show requested size of trace buffer."), _("\
4344 Use this to choose a size for the trace buffer. Some targets\n\
4345 may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
4346 disables any attempt to set the buffer size and lets the target choose."),
4347 set_trace_buffer_size, NULL,
4348 &setlist, &showlist);
4350 add_setshow_string_cmd ("trace-user", class_trace,
4352 Set the user name to use for current and future trace runs"), _("\
4353 Show the user name to use for current and future trace runs"), NULL,
4354 set_trace_user, NULL,
4355 &setlist, &showlist);
4357 add_setshow_string_cmd ("trace-notes", class_trace,
4359 Set notes string to use for current and future trace runs"), _("\
4360 Show the notes string to use for current and future trace runs"), NULL,
4361 set_trace_notes, NULL,
4362 &setlist, &showlist);
4364 add_setshow_string_cmd ("trace-stop-notes", class_trace,
4365 &trace_stop_notes, _("\
4366 Set notes string to use for future tstop commands"), _("\
4367 Show the notes string to use for future tstop commands"), NULL,
4368 set_trace_stop_notes, NULL,
4369 &setlist, &showlist);