1 /* Tracing functionality for remote targets in custom GDB protocol
3 Copyright (C) 1997-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
25 #include "expression.h"
29 #include "target-dcache.h"
32 #include "breakpoint.h"
33 #include "tracepoint.h"
36 #include "completer.h"
38 #include "dictionary.h"
40 #include "user-regs.h"
44 #include "filenames.h"
45 #include "gdbthread.h"
52 #include "cli/cli-utils.h"
55 #include "filestuff.h"
57 #include "tracefile.h"
61 /* readline include files */
62 #include "readline/readline.h"
63 #include "readline/history.h"
65 /* readline defines this. */
70 /* Maximum length of an agent aexpression.
71 This accounts for the fact that packets are limited to 400 bytes
72 (which includes everything -- including the checksum), and assumes
73 the worst case of maximum length for each of the pieces of a
76 NOTE: expressions get mem2hex'ed otherwise this would be twice as
77 large. (400 - 31)/2 == 184 */
78 #define MAX_AGENT_EXPR_LEN 184
80 /* A hook used to notify the UI of tracepoint operations. */
82 void (*deprecated_trace_find_hook) (char *arg, int from_tty);
83 void (*deprecated_trace_start_stop_hook) (int start, int from_tty);
88 This module defines the following debugger commands:
89 trace : set a tracepoint on a function, line, or address.
90 info trace : list all debugger-defined tracepoints.
91 delete trace : delete one or more tracepoints.
92 enable trace : enable one or more tracepoints.
93 disable trace : disable one or more tracepoints.
94 actions : specify actions to be taken at a tracepoint.
95 passcount : specify a pass count for a tracepoint.
96 tstart : start a trace experiment.
97 tstop : stop a trace experiment.
98 tstatus : query the status of a trace experiment.
99 tfind : find a trace frame in the trace buffer.
100 tdump : print everything collected at the current tracepoint.
101 save-tracepoints : write tracepoint setup into a file.
103 This module defines the following user-visible debugger variables:
104 $trace_frame : sequence number of trace frame currently being debugged.
105 $trace_line : source line of trace frame currently being debugged.
106 $trace_file : source file of trace frame currently being debugged.
107 $tracepoint : tracepoint number of trace frame currently being debugged.
111 /* ======= Important global variables: ======= */
113 /* The list of all trace state variables. We don't retain pointers to
114 any of these for any reason - API is by name or number only - so it
115 works to have a vector of objects. */
117 typedef struct trace_state_variable tsv_s;
120 static VEC(tsv_s) *tvariables;
122 /* The next integer to assign to a variable. */
124 static int next_tsv_number = 1;
126 /* Number of last traceframe collected. */
127 static int traceframe_number;
129 /* Tracepoint for last traceframe collected. */
130 static int tracepoint_number;
132 /* The traceframe info of the current traceframe. NULL if we haven't
133 yet attempted to fetch it, or if the target does not support
134 fetching this object, or if we're not inspecting a traceframe
136 static struct traceframe_info *traceframe_info;
138 /* Tracing command lists. */
139 static struct cmd_list_element *tfindlist;
141 /* List of expressions to collect by default at each tracepoint hit. */
142 char *default_collect;
144 static int disconnected_tracing;
146 /* This variable controls whether we ask the target for a linear or
147 circular trace buffer. */
149 static int circular_trace_buffer;
151 /* This variable is the requested trace buffer size, or -1 to indicate
152 that we don't care and leave it up to the target to set a size. */
154 static int trace_buffer_size = -1;
156 /* Textual notes applying to the current and/or future trace runs. */
158 char *trace_user = NULL;
160 /* Textual notes applying to the current and/or future trace runs. */
162 char *trace_notes = NULL;
164 /* Textual notes applying to the stopping of a trace. */
166 char *trace_stop_notes = NULL;
168 /* ======= Important command functions: ======= */
169 static void actions_command (char *, int);
170 static void tstart_command (char *, int);
171 static void tstop_command (char *, int);
172 static void tstatus_command (char *, int);
173 static void tfind_pc_command (char *, int);
174 static void tfind_tracepoint_command (char *, int);
175 static void tfind_line_command (char *, int);
176 static void tfind_range_command (char *, int);
177 static void tfind_outside_command (char *, int);
178 static void tdump_command (char *, int);
180 /* support routines */
182 struct collection_list;
183 static char *mem2hex (gdb_byte *, char *, int);
185 static struct command_line *
186 all_tracepoint_actions_and_cleanup (struct breakpoint *t);
188 extern void _initialize_tracepoint (void);
190 static struct trace_status trace_status;
192 const char *stop_reason_names[] = {
202 struct trace_status *
203 current_trace_status (void)
205 return &trace_status;
211 free_traceframe_info (struct traceframe_info *info)
215 VEC_free (mem_range_s, info->memory);
216 VEC_free (int, info->tvars);
222 /* Free and clear the traceframe info cache of the current
226 clear_traceframe_info (void)
228 free_traceframe_info (traceframe_info);
229 traceframe_info = NULL;
232 /* Set traceframe number to NUM. */
234 set_traceframe_num (int num)
236 traceframe_number = num;
237 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
240 /* Set tracepoint number to NUM. */
242 set_tracepoint_num (int num)
244 tracepoint_number = num;
245 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
248 /* Set externally visible debug variables for querying/printing
249 the traceframe context (line, function, file). */
252 set_traceframe_context (struct frame_info *trace_frame)
255 struct symbol *traceframe_fun;
256 symtab_and_line traceframe_sal;
258 /* Save as globals for internal use. */
259 if (trace_frame != NULL
260 && get_frame_pc_if_available (trace_frame, &trace_pc))
262 traceframe_sal = find_pc_line (trace_pc, 0);
263 traceframe_fun = find_pc_function (trace_pc);
265 /* Save linenumber as "$trace_line", a debugger variable visible to
267 set_internalvar_integer (lookup_internalvar ("trace_line"),
268 traceframe_sal.line);
272 traceframe_fun = NULL;
273 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
276 /* Save func name as "$trace_func", a debugger variable visible to
278 if (traceframe_fun == NULL
279 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
280 clear_internalvar (lookup_internalvar ("trace_func"));
282 set_internalvar_string (lookup_internalvar ("trace_func"),
283 SYMBOL_LINKAGE_NAME (traceframe_fun));
285 /* Save file name as "$trace_file", a debugger variable visible to
287 if (traceframe_sal.symtab == NULL)
288 clear_internalvar (lookup_internalvar ("trace_file"));
290 set_internalvar_string (lookup_internalvar ("trace_file"),
291 symtab_to_filename_for_display (traceframe_sal.symtab));
294 /* Create a new trace state variable with the given name. */
296 struct trace_state_variable *
297 create_trace_state_variable (const char *name)
299 struct trace_state_variable tsv;
301 memset (&tsv, 0, sizeof (tsv));
302 tsv.name = xstrdup (name);
303 tsv.number = next_tsv_number++;
304 return VEC_safe_push (tsv_s, tvariables, &tsv);
307 /* Look for a trace state variable of the given name. */
309 struct trace_state_variable *
310 find_trace_state_variable (const char *name)
312 struct trace_state_variable *tsv;
315 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
316 if (strcmp (name, tsv->name) == 0)
322 /* Look for a trace state variable of the given number. Return NULL if
325 struct trace_state_variable *
326 find_trace_state_variable_by_number (int number)
328 struct trace_state_variable *tsv;
331 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
332 if (tsv->number == number)
339 delete_trace_state_variable (const char *name)
341 struct trace_state_variable *tsv;
344 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
345 if (strcmp (name, tsv->name) == 0)
347 observer_notify_tsv_deleted (tsv);
349 xfree ((void *)tsv->name);
350 VEC_unordered_remove (tsv_s, tvariables, ix);
355 warning (_("No trace variable named \"$%s\", not deleting"), name);
358 /* Throws an error if NAME is not valid syntax for a trace state
362 validate_trace_state_variable_name (const char *name)
367 error (_("Must supply a non-empty variable name"));
369 /* All digits in the name is reserved for value history
371 for (p = name; isdigit (*p); p++)
374 error (_("$%s is not a valid trace state variable name"), name);
376 for (p = name; isalnum (*p) || *p == '_'; p++)
379 error (_("$%s is not a valid trace state variable name"), name);
382 /* The 'tvariable' command collects a name and optional expression to
383 evaluate into an initial value. */
386 trace_variable_command (char *args, int from_tty)
388 struct cleanup *old_chain;
390 struct trace_state_variable *tsv;
394 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
396 /* Only allow two syntaxes; "$name" and "$name=value". */
397 p = skip_spaces (args);
400 error (_("Name of trace variable should start with '$'"));
403 while (isalnum (*p) || *p == '_')
405 name = savestring (name, p - name);
406 old_chain = make_cleanup (xfree, name);
409 if (*p != '=' && *p != '\0')
410 error (_("Syntax must be $NAME [ = EXPR ]"));
412 validate_trace_state_variable_name (name);
415 initval = value_as_long (parse_and_eval (++p));
417 /* If the variable already exists, just change its initial value. */
418 tsv = find_trace_state_variable (name);
421 if (tsv->initial_value != initval)
423 tsv->initial_value = initval;
424 observer_notify_tsv_modified (tsv);
426 printf_filtered (_("Trace state variable $%s "
427 "now has initial value %s.\n"),
428 tsv->name, plongest (tsv->initial_value));
429 do_cleanups (old_chain);
433 /* Create a new variable. */
434 tsv = create_trace_state_variable (name);
435 tsv->initial_value = initval;
437 observer_notify_tsv_created (tsv);
439 printf_filtered (_("Trace state variable $%s "
440 "created, with initial value %s.\n"),
441 tsv->name, plongest (tsv->initial_value));
443 do_cleanups (old_chain);
447 delete_trace_variable_command (char *args, int from_tty)
451 if (query (_("Delete all trace state variables? ")))
452 VEC_free (tsv_s, tvariables);
454 observer_notify_tsv_deleted (NULL);
458 gdb_argv argv (args);
460 for (char *arg : argv)
463 delete_trace_state_variable (arg + 1);
465 warning (_("Name \"%s\" not prefixed with '$', ignoring"), arg);
472 tvariables_info_1 (void)
474 struct trace_state_variable *tsv;
477 struct ui_out *uiout = current_uiout;
479 if (VEC_length (tsv_s, tvariables) == 0 && !uiout->is_mi_like_p ())
481 printf_filtered (_("No trace state variables.\n"));
485 /* Try to acquire values from the target. */
486 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix, ++count)
487 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
490 ui_out_emit_table table_emitter (uiout, 3, count, "trace-variables");
491 uiout->table_header (15, ui_left, "name", "Name");
492 uiout->table_header (11, ui_left, "initial", "Initial");
493 uiout->table_header (11, ui_left, "current", "Current");
495 uiout->table_body ();
497 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
501 ui_out_emit_tuple tuple_emitter (uiout, "variable");
503 std::string name = std::string ("$") + tsv->name;
504 uiout->field_string ("name", name.c_str ());
505 uiout->field_string ("initial", plongest (tsv->initial_value));
507 if (tsv->value_known)
508 c = plongest (tsv->value);
509 else if (uiout->is_mi_like_p ())
510 /* For MI, we prefer not to use magic string constants, but rather
511 omit the field completely. The difference between unknown and
512 undefined does not seem important enough to represent. */
514 else if (current_trace_status ()->running || traceframe_number >= 0)
515 /* The value is/was defined, but we don't have it. */
518 /* It is not meaningful to ask about the value. */
521 uiout->field_string ("current", c);
526 /* List all the trace state variables. */
529 info_tvariables_command (char *args, int from_tty)
531 tvariables_info_1 ();
534 /* Stash definitions of tsvs into the given file. */
537 save_trace_state_variables (struct ui_file *fp)
539 struct trace_state_variable *tsv;
542 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
544 fprintf_unfiltered (fp, "tvariable $%s", tsv->name);
545 if (tsv->initial_value)
546 fprintf_unfiltered (fp, " = %s", plongest (tsv->initial_value));
547 fprintf_unfiltered (fp, "\n");
551 /* ACTIONS functions: */
553 /* The three functions:
554 collect_pseudocommand,
555 while_stepping_pseudocommand, and
556 end_actions_pseudocommand
557 are placeholders for "commands" that are actually ONLY to be used
558 within a tracepoint action list. If the actual function is ever called,
559 it means that somebody issued the "command" at the top level,
560 which is always an error. */
563 end_actions_pseudocommand (char *args, int from_tty)
565 error (_("This command cannot be used at the top level."));
569 while_stepping_pseudocommand (char *args, int from_tty)
571 error (_("This command can only be used in a tracepoint actions list."));
575 collect_pseudocommand (char *args, int from_tty)
577 error (_("This command can only be used in a tracepoint actions list."));
581 teval_pseudocommand (char *args, int from_tty)
583 error (_("This command can only be used in a tracepoint actions list."));
586 /* Parse any collection options, such as /s for strings. */
589 decode_agent_options (const char *exp, int *trace_string)
591 struct value_print_options opts;
598 /* Call this to borrow the print elements default for collection
600 get_user_print_options (&opts);
605 if (target_supports_string_tracing ())
607 /* Allow an optional decimal number giving an explicit maximum
608 string length, defaulting it to the "print elements" value;
609 so "collect/s80 mystr" gets at most 80 bytes of string. */
610 *trace_string = opts.print_max;
612 if (*exp >= '0' && *exp <= '9')
613 *trace_string = atoi (exp);
614 while (*exp >= '0' && *exp <= '9')
618 error (_("Target does not support \"/s\" option for string tracing."));
621 error (_("Undefined collection format \"%c\"."), *exp);
623 exp = skip_spaces_const (exp);
628 /* Enter a list of actions for a tracepoint. */
630 actions_command (char *args, int from_tty)
632 struct tracepoint *t;
634 t = get_tracepoint_by_number (&args, NULL);
638 string_printf ("Enter actions for tracepoint %d, one per line.",
641 command_line_up l = read_command_lines (&tmpbuf[0], from_tty, 1,
642 check_tracepoint_command, t);
643 breakpoint_set_commands (t, std::move (l));
645 /* else just return */
648 /* Report the results of checking the agent expression, as errors or
652 report_agent_reqs_errors (struct agent_expr *aexpr)
654 /* All of the "flaws" are serious bytecode generation issues that
655 should never occur. */
656 if (aexpr->flaw != agent_flaw_none)
657 internal_error (__FILE__, __LINE__, _("expression is malformed"));
659 /* If analysis shows a stack underflow, GDB must have done something
660 badly wrong in its bytecode generation. */
661 if (aexpr->min_height < 0)
662 internal_error (__FILE__, __LINE__,
663 _("expression has min height < 0"));
665 /* Issue this error if the stack is predicted to get too deep. The
666 limit is rather arbitrary; a better scheme might be for the
667 target to report how much stack it will have available. The
668 depth roughly corresponds to parenthesization, so a limit of 20
669 amounts to 20 levels of expression nesting, which is actually
670 a pretty big hairy expression. */
671 if (aexpr->max_height > 20)
672 error (_("Expression is too complicated."));
675 /* worker function */
677 validate_actionline (const char *line, struct breakpoint *b)
679 struct cmd_list_element *c;
680 struct cleanup *old_chain = NULL;
683 struct bp_location *loc;
684 struct tracepoint *t = (struct tracepoint *) b;
686 /* If EOF is typed, *line is NULL. */
690 p = skip_spaces_const (line);
692 /* Symbol lookup etc. */
693 if (*p == '\0') /* empty line: just prompt for another line. */
696 if (*p == '#') /* comment line */
699 c = lookup_cmd (&p, cmdlist, "", -1, 1);
701 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
703 if (cmd_cfunc_eq (c, collect_pseudocommand))
705 int trace_string = 0;
708 p = decode_agent_options (p, &trace_string);
711 { /* Repeat over a comma-separated list. */
712 QUIT; /* Allow user to bail out with ^C. */
713 p = skip_spaces_const (p);
715 if (*p == '$') /* Look for special pseudo-symbols. */
717 if (0 == strncasecmp ("reg", p + 1, 3)
718 || 0 == strncasecmp ("arg", p + 1, 3)
719 || 0 == strncasecmp ("loc", p + 1, 3)
720 || 0 == strncasecmp ("_ret", p + 1, 4)
721 || 0 == strncasecmp ("_sdata", p + 1, 6))
726 /* else fall thru, treat p as an expression and parse it! */
729 for (loc = t->loc; loc; loc = loc->next)
732 expression_up exp = parse_exp_1 (&p, loc->address,
733 block_for_pc (loc->address), 1);
735 if (exp->elts[0].opcode == OP_VAR_VALUE)
737 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
739 error (_("constant `%s' (value %s) "
740 "will not be collected."),
741 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
742 plongest (SYMBOL_VALUE (exp->elts[2].symbol)));
744 else if (SYMBOL_CLASS (exp->elts[2].symbol)
745 == LOC_OPTIMIZED_OUT)
747 error (_("`%s' is optimized away "
748 "and cannot be collected."),
749 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
753 /* We have something to collect, make sure that the expr to
754 bytecode translator can handle it and that it's not too
756 agent_expr_up aexpr = gen_trace_for_expr (loc->address,
760 if (aexpr->len > MAX_AGENT_EXPR_LEN)
761 error (_("Expression is too complicated."));
763 ax_reqs (aexpr.get ());
765 report_agent_reqs_errors (aexpr.get ());
768 while (p && *p++ == ',');
771 else if (cmd_cfunc_eq (c, teval_pseudocommand))
774 { /* Repeat over a comma-separated list. */
775 QUIT; /* Allow user to bail out with ^C. */
776 p = skip_spaces_const (p);
779 for (loc = t->loc; loc; loc = loc->next)
783 /* Only expressions are allowed for this action. */
784 expression_up exp = parse_exp_1 (&p, loc->address,
785 block_for_pc (loc->address), 1);
787 /* We have something to evaluate, make sure that the expr to
788 bytecode translator can handle it and that it's not too
790 agent_expr_up aexpr = gen_eval_for_expr (loc->address, exp.get ());
792 if (aexpr->len > MAX_AGENT_EXPR_LEN)
793 error (_("Expression is too complicated."));
795 ax_reqs (aexpr.get ());
796 report_agent_reqs_errors (aexpr.get ());
799 while (p && *p++ == ',');
802 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
806 p = skip_spaces_const (p);
807 t->step_count = strtol (p, &endp, 0);
808 if (endp == p || t->step_count == 0)
809 error (_("while-stepping step count `%s' is malformed."), line);
813 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
817 error (_("`%s' is not a supported tracepoint action."), line);
821 memrange_absolute = -1
824 /* MEMRANGE functions: */
826 /* Compare memranges for std::sort. */
829 memrange_comp (const memrange &a, const memrange &b)
831 if (a.type == b.type)
833 if (a.type == memrange_absolute)
834 return (bfd_vma) a.start < (bfd_vma) b.start;
836 return a.start < b.start;
839 return a.type < b.type;
842 /* Sort the memrange list using std::sort, and merge adjacent memranges. */
845 memrange_sortmerge (std::vector<memrange> &memranges)
847 if (!memranges.empty ())
851 std::sort (memranges.begin (), memranges.end (), memrange_comp);
853 for (a = 0, b = 1; b < memranges.size (); b++)
855 /* If memrange b overlaps or is adjacent to memrange a,
857 if (memranges[a].type == memranges[b].type
858 && memranges[b].start <= memranges[a].end)
860 if (memranges[b].end > memranges[a].end)
861 memranges[a].end = memranges[b].end;
862 continue; /* next b, same a */
866 memranges[a] = memranges[b];
868 memranges.resize (a + 1);
872 /* Add a register to a collection list. */
875 collection_list::add_register (unsigned int regno)
878 printf_filtered ("collect register %d\n", regno);
879 if (regno >= (8 * sizeof (m_regs_mask)))
880 error (_("Internal: register number %d too large for tracepoint"),
882 m_regs_mask[regno / 8] |= 1 << (regno % 8);
885 /* Add a memrange to a collection list. */
888 collection_list::add_memrange (struct gdbarch *gdbarch,
889 int type, bfd_signed_vma base,
893 printf_filtered ("(%d,%s,%ld)\n", type, paddress (gdbarch, base), len);
895 /* type: memrange_absolute == memory, other n == basereg */
896 /* base: addr if memory, offset if reg relative. */
897 /* len: we actually save end (base + len) for convenience */
898 m_memranges.emplace_back (type, base, base + len);
900 if (type != memrange_absolute) /* Better collect the base register! */
904 /* Add a symbol to a collection list. */
907 collection_list::collect_symbol (struct symbol *sym,
908 struct gdbarch *gdbarch,
909 long frame_regno, long frame_offset,
915 bfd_signed_vma offset;
916 int treat_as_expr = 0;
918 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
919 switch (SYMBOL_CLASS (sym))
922 printf_filtered ("%s: don't know symbol class %d\n",
923 SYMBOL_PRINT_NAME (sym),
927 printf_filtered ("constant %s (value %s) will not be collected.\n",
928 SYMBOL_PRINT_NAME (sym), plongest (SYMBOL_VALUE (sym)));
931 offset = SYMBOL_VALUE_ADDRESS (sym);
934 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
935 SYMBOL_PRINT_NAME (sym), len,
936 paddress (gdbarch, offset));
938 /* A struct may be a C++ class with static fields, go to general
939 expression handling. */
940 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT)
943 add_memrange (gdbarch, memrange_absolute, offset, len);
946 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
948 printf_filtered ("LOC_REG[parm] %s: ",
949 SYMBOL_PRINT_NAME (sym));
951 /* Check for doubles stored in two registers. */
952 /* FIXME: how about larger types stored in 3 or more regs? */
953 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
954 len > register_size (gdbarch, reg))
955 add_register (reg + 1);
958 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
959 printf_filtered (" (will not collect %s)\n",
960 SYMBOL_PRINT_NAME (sym));
964 offset = frame_offset + SYMBOL_VALUE (sym);
967 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
968 " from frame ptr reg %d\n",
969 SYMBOL_PRINT_NAME (sym), len,
970 paddress (gdbarch, offset), reg);
972 add_memrange (gdbarch, reg, offset, len);
974 case LOC_REGPARM_ADDR:
975 reg = SYMBOL_VALUE (sym);
979 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s"
981 SYMBOL_PRINT_NAME (sym), len,
982 paddress (gdbarch, offset), reg);
984 add_memrange (gdbarch, reg, offset, len);
988 offset = frame_offset + SYMBOL_VALUE (sym);
991 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
992 " from frame ptr reg %d\n",
993 SYMBOL_PRINT_NAME (sym), len,
994 paddress (gdbarch, offset), reg);
996 add_memrange (gdbarch, reg, offset, len);
1003 case LOC_OPTIMIZED_OUT:
1004 printf_filtered ("%s has been optimized out of existence.\n",
1005 SYMBOL_PRINT_NAME (sym));
1013 /* Expressions are the most general case. */
1016 struct cleanup *old_chain1 = NULL;
1018 agent_expr_up aexpr = gen_trace_for_var (scope, gdbarch,
1021 /* It can happen that the symbol is recorded as a computed
1022 location, but it's been optimized away and doesn't actually
1023 have a location expression. */
1026 printf_filtered ("%s has been optimized out of existence.\n",
1027 SYMBOL_PRINT_NAME (sym));
1031 ax_reqs (aexpr.get ());
1033 report_agent_reqs_errors (aexpr.get ());
1035 /* Take care of the registers. */
1036 if (aexpr->reg_mask_len > 0)
1038 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1040 QUIT; /* Allow user to bail out with ^C. */
1041 if (aexpr->reg_mask[ndx1] != 0)
1043 /* Assume chars have 8 bits. */
1044 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1045 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1046 /* It's used -- record it. */
1047 add_register (ndx1 * 8 + ndx2);
1052 add_aexpr (std::move (aexpr));
1056 /* Data to be passed around in the calls to the locals and args
1059 struct add_local_symbols_data
1061 struct collection_list *collect;
1062 struct gdbarch *gdbarch;
1070 /* The callback for the locals and args iterators. */
1073 do_collect_symbol (const char *print_name,
1077 struct add_local_symbols_data *p = (struct add_local_symbols_data *) cb_data;
1079 p->collect->collect_symbol (sym, p->gdbarch, p->frame_regno,
1080 p->frame_offset, p->pc, p->trace_string);
1083 p->collect->add_wholly_collected (print_name);
1087 collection_list::add_wholly_collected (const char *print_name)
1089 m_wholly_collected.push_back (print_name);
1092 /* Add all locals (or args) symbols to collection list. */
1095 collection_list::add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc,
1096 long frame_regno, long frame_offset, int type,
1099 const struct block *block;
1100 struct add_local_symbols_data cb_data;
1102 cb_data.collect = this;
1103 cb_data.gdbarch = gdbarch;
1105 cb_data.frame_regno = frame_regno;
1106 cb_data.frame_offset = frame_offset;
1108 cb_data.trace_string = trace_string;
1112 block = block_for_pc (pc);
1115 warning (_("Can't collect locals; "
1116 "no symbol table info available.\n"));
1120 iterate_over_block_local_vars (block, do_collect_symbol, &cb_data);
1121 if (cb_data.count == 0)
1122 warning (_("No locals found in scope."));
1126 pc = get_pc_function_start (pc);
1127 block = block_for_pc (pc);
1130 warning (_("Can't collect args; no symbol table info available."));
1134 iterate_over_block_arg_vars (block, do_collect_symbol, &cb_data);
1135 if (cb_data.count == 0)
1136 warning (_("No args found in scope."));
1141 collection_list::add_static_trace_data ()
1144 printf_filtered ("collect static trace data\n");
1145 m_strace_data = true;
1148 collection_list::collection_list ()
1150 m_strace_data (false)
1152 m_memranges.reserve (128);
1153 m_aexprs.reserve (128);
1156 /* Reduce a collection list to string form (for gdb protocol). */
1159 collection_list::stringify ()
1161 char temp_buf[2048];
1164 char *(*str_list)[];
1168 count = 1 + 1 + m_memranges.size () + m_aexprs.size () + 1;
1169 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
1174 printf_filtered ("\nCollecting static trace data\n");
1177 (*str_list)[ndx] = savestring (temp_buf, end - temp_buf);
1181 for (i = sizeof (m_regs_mask) - 1; i > 0; i--)
1182 if (m_regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
1184 if (m_regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
1187 printf_filtered ("\nCollecting registers (mask): 0x");
1192 QUIT; /* Allow user to bail out with ^C. */
1194 printf_filtered ("%02X", m_regs_mask[i]);
1195 sprintf (end, "%02X", m_regs_mask[i]);
1198 (*str_list)[ndx] = xstrdup (temp_buf);
1202 printf_filtered ("\n");
1203 if (!m_memranges.empty () && info_verbose)
1204 printf_filtered ("Collecting memranges: \n");
1205 for (i = 0, count = 0, end = temp_buf; i < m_memranges.size (); i++)
1207 QUIT; /* Allow user to bail out with ^C. */
1210 printf_filtered ("(%d, %s, %ld)\n",
1211 m_memranges[i].type,
1212 paddress (target_gdbarch (),
1213 m_memranges[i].start),
1214 (long) (m_memranges[i].end
1215 - m_memranges[i].start));
1217 if (count + 27 > MAX_AGENT_EXPR_LEN)
1219 (*str_list)[ndx] = savestring (temp_buf, count);
1226 bfd_signed_vma length
1227 = m_memranges[i].end - m_memranges[i].start;
1229 /* The "%X" conversion specifier expects an unsigned argument,
1230 so passing -1 (memrange_absolute) to it directly gives you
1231 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1233 if (m_memranges[i].type == memrange_absolute)
1234 sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
1237 sprintf (end, "M%X,%s,%lX", m_memranges[i].type,
1238 phex_nz (m_memranges[i].start, 0), (long) length);
1241 count += strlen (end);
1242 end = temp_buf + count;
1245 for (i = 0; i < m_aexprs.size (); i++)
1247 QUIT; /* Allow user to bail out with ^C. */
1248 if ((count + 10 + 2 * m_aexprs[i]->len) > MAX_AGENT_EXPR_LEN)
1250 (*str_list)[ndx] = savestring (temp_buf, count);
1255 sprintf (end, "X%08X,", m_aexprs[i]->len);
1256 end += 10; /* 'X' + 8 hex digits + ',' */
1259 end = mem2hex (m_aexprs[i]->buf, end, m_aexprs[i]->len);
1260 count += 2 * m_aexprs[i]->len;
1265 (*str_list)[ndx] = savestring (temp_buf, count);
1270 (*str_list)[ndx] = NULL;
1281 /* Add the printed expression EXP to *LIST. */
1284 collection_list::append_exp (struct expression *exp)
1286 string_file tmp_stream;
1288 print_expression (exp, &tmp_stream);
1290 m_computed.push_back (std::move (tmp_stream.string ()));
1294 collection_list::finish ()
1296 memrange_sortmerge (m_memranges);
1300 encode_actions_1 (struct command_line *action,
1301 struct bp_location *tloc,
1303 LONGEST frame_offset,
1304 struct collection_list *collect,
1305 struct collection_list *stepping_list)
1307 const char *action_exp;
1309 struct value *tempval;
1310 struct cmd_list_element *cmd;
1312 for (; action; action = action->next)
1314 QUIT; /* Allow user to bail out with ^C. */
1315 action_exp = action->line;
1316 action_exp = skip_spaces_const (action_exp);
1318 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1320 error (_("Bad action list item: %s"), action_exp);
1322 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
1324 int trace_string = 0;
1326 if (*action_exp == '/')
1327 action_exp = decode_agent_options (action_exp, &trace_string);
1330 { /* Repeat over a comma-separated list. */
1331 QUIT; /* Allow user to bail out with ^C. */
1332 action_exp = skip_spaces_const (action_exp);
1334 if (0 == strncasecmp ("$reg", action_exp, 4))
1336 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
1337 collect->add_register (i);
1338 action_exp = strchr (action_exp, ','); /* more? */
1340 else if (0 == strncasecmp ("$arg", action_exp, 4))
1342 collect->add_local_symbols (target_gdbarch (),
1348 action_exp = strchr (action_exp, ','); /* more? */
1350 else if (0 == strncasecmp ("$loc", action_exp, 4))
1352 collect->add_local_symbols (target_gdbarch (),
1358 action_exp = strchr (action_exp, ','); /* more? */
1360 else if (0 == strncasecmp ("$_ret", action_exp, 5))
1363 = gen_trace_for_return_address (tloc->address,
1367 ax_reqs (aexpr.get ());
1368 report_agent_reqs_errors (aexpr.get ());
1370 /* take care of the registers */
1371 if (aexpr->reg_mask_len > 0)
1373 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1375 QUIT; /* allow user to bail out with ^C */
1376 if (aexpr->reg_mask[ndx1] != 0)
1378 /* assume chars have 8 bits */
1379 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1380 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1382 /* It's used -- record it. */
1383 collect->add_register (ndx1 * 8 + ndx2);
1389 collect->add_aexpr (std::move (aexpr));
1390 action_exp = strchr (action_exp, ','); /* more? */
1392 else if (0 == strncasecmp ("$_sdata", action_exp, 7))
1394 collect->add_static_trace_data ();
1395 action_exp = strchr (action_exp, ','); /* more? */
1400 struct cleanup *old_chain1 = NULL;
1402 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1403 block_for_pc (tloc->address),
1406 switch (exp->elts[0].opcode)
1410 const char *name = &exp->elts[2].string;
1412 i = user_reg_map_name_to_regnum (target_gdbarch (),
1413 name, strlen (name));
1415 internal_error (__FILE__, __LINE__,
1416 _("Register $%s not available"),
1419 printf_filtered ("OP_REGISTER: ");
1420 collect->add_register (i);
1425 /* Safe because we know it's a simple expression. */
1426 tempval = evaluate_expression (exp.get ());
1427 addr = value_address (tempval);
1428 /* Initialize the TYPE_LENGTH if it is a typedef. */
1429 check_typedef (exp->elts[1].type);
1430 collect->add_memrange (target_gdbarch (),
1431 memrange_absolute, addr,
1432 TYPE_LENGTH (exp->elts[1].type));
1433 collect->append_exp (exp.get ());
1438 struct symbol *sym = exp->elts[2].symbol;
1439 char_ptr name = (char_ptr) SYMBOL_NATURAL_NAME (sym);
1441 collect->collect_symbol (exp->elts[2].symbol,
1447 collect->add_wholly_collected (name);
1451 default: /* Full-fledged expression. */
1452 agent_expr_up aexpr = gen_trace_for_expr (tloc->address,
1456 ax_reqs (aexpr.get ());
1458 report_agent_reqs_errors (aexpr.get ());
1460 /* Take care of the registers. */
1461 if (aexpr->reg_mask_len > 0)
1463 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1465 QUIT; /* Allow user to bail out with ^C. */
1466 if (aexpr->reg_mask[ndx1] != 0)
1468 /* Assume chars have 8 bits. */
1469 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1470 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1472 /* It's used -- record it. */
1473 collect->add_register (ndx1 * 8 + ndx2);
1479 collect->add_aexpr (std::move (aexpr));
1480 collect->append_exp (exp.get ());
1485 while (action_exp && *action_exp++ == ',');
1487 else if (cmd_cfunc_eq (cmd, teval_pseudocommand))
1490 { /* Repeat over a comma-separated list. */
1491 QUIT; /* Allow user to bail out with ^C. */
1492 action_exp = skip_spaces_const (action_exp);
1495 struct cleanup *old_chain1 = NULL;
1497 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1498 block_for_pc (tloc->address),
1501 agent_expr_up aexpr = gen_eval_for_expr (tloc->address,
1504 ax_reqs (aexpr.get ());
1505 report_agent_reqs_errors (aexpr.get ());
1507 /* Even though we're not officially collecting, add
1508 to the collect list anyway. */
1509 collect->add_aexpr (std::move (aexpr));
1512 while (action_exp && *action_exp++ == ',');
1514 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
1516 /* We check against nested while-stepping when setting
1517 breakpoint action, so no way to run into nested
1519 gdb_assert (stepping_list);
1521 encode_actions_1 (action->body_list[0], tloc, frame_reg,
1522 frame_offset, stepping_list, NULL);
1525 error (_("Invalid tracepoint command '%s'"), action->line);
1529 /* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
1530 and STEPPING_LIST. */
1533 encode_actions (struct bp_location *tloc,
1534 struct collection_list *tracepoint_list,
1535 struct collection_list *stepping_list)
1537 struct command_line *actions;
1539 LONGEST frame_offset;
1541 gdbarch_virtual_frame_pointer (tloc->gdbarch,
1542 tloc->address, &frame_reg, &frame_offset);
1544 actions = all_tracepoint_actions_and_cleanup (tloc->owner);
1546 encode_actions_1 (actions, tloc, frame_reg, frame_offset,
1547 tracepoint_list, stepping_list);
1549 tracepoint_list->finish ();
1550 stepping_list->finish ();
1553 /* Render all actions into gdb protocol. */
1556 encode_actions_rsp (struct bp_location *tloc, char ***tdp_actions,
1557 char ***stepping_actions)
1559 struct collection_list tracepoint_list, stepping_list;
1561 *tdp_actions = NULL;
1562 *stepping_actions = NULL;
1564 encode_actions (tloc, &tracepoint_list, &stepping_list);
1566 *tdp_actions = tracepoint_list.stringify ();
1567 *stepping_actions = stepping_list.stringify ();
1571 collection_list::add_aexpr (agent_expr_up aexpr)
1573 m_aexprs.push_back (std::move (aexpr));
1577 process_tracepoint_on_disconnect (void)
1579 VEC(breakpoint_p) *tp_vec = NULL;
1581 struct breakpoint *b;
1582 int has_pending_p = 0;
1584 /* Check whether we still have pending tracepoint. If we have, warn the
1585 user that pending tracepoint will no longer work. */
1586 tp_vec = all_tracepoints ();
1587 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1596 struct bp_location *loc1;
1598 for (loc1 = b->loc; loc1; loc1 = loc1->next)
1600 if (loc1->shlib_disabled)
1611 VEC_free (breakpoint_p, tp_vec);
1614 warning (_("Pending tracepoints will not be resolved while"
1615 " GDB is disconnected\n"));
1618 /* Reset local state of tracing. */
1621 trace_reset_local_state (void)
1623 set_traceframe_num (-1);
1624 set_tracepoint_num (-1);
1625 set_traceframe_context (NULL);
1626 clear_traceframe_info ();
1630 start_tracing (char *notes)
1632 VEC(breakpoint_p) *tp_vec = NULL;
1634 struct breakpoint *b;
1635 struct trace_state_variable *tsv;
1636 int any_enabled = 0, num_to_download = 0;
1639 tp_vec = all_tracepoints ();
1641 /* No point in tracing without any tracepoints... */
1642 if (VEC_length (breakpoint_p, tp_vec) == 0)
1644 VEC_free (breakpoint_p, tp_vec);
1645 error (_("No tracepoints defined, not starting trace"));
1648 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1650 if (b->enable_state == bp_enabled)
1653 if ((b->type == bp_fast_tracepoint
1654 ? may_insert_fast_tracepoints
1655 : may_insert_tracepoints))
1658 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
1659 (b->type == bp_fast_tracepoint ? "fast " : ""), b->number);
1664 if (target_supports_enable_disable_tracepoint ())
1665 warning (_("No tracepoints enabled"));
1668 /* No point in tracing with only disabled tracepoints that
1669 cannot be re-enabled. */
1670 VEC_free (breakpoint_p, tp_vec);
1671 error (_("No tracepoints enabled, not starting trace"));
1675 if (num_to_download <= 0)
1677 VEC_free (breakpoint_p, tp_vec);
1678 error (_("No tracepoints that may be downloaded, not starting trace"));
1681 target_trace_init ();
1683 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1685 struct tracepoint *t = (struct tracepoint *) b;
1686 struct bp_location *loc;
1687 int bp_location_downloaded = 0;
1689 /* Clear `inserted' flag. */
1690 for (loc = b->loc; loc; loc = loc->next)
1693 if ((b->type == bp_fast_tracepoint
1694 ? !may_insert_fast_tracepoints
1695 : !may_insert_tracepoints))
1698 t->number_on_target = 0;
1700 for (loc = b->loc; loc; loc = loc->next)
1702 /* Since tracepoint locations are never duplicated, `inserted'
1703 flag should be zero. */
1704 gdb_assert (!loc->inserted);
1706 target_download_tracepoint (loc);
1709 bp_location_downloaded = 1;
1712 t->number_on_target = b->number;
1714 for (loc = b->loc; loc; loc = loc->next)
1715 if (loc->probe.probe != NULL
1716 && loc->probe.probe->pops->set_semaphore != NULL)
1717 loc->probe.probe->pops->set_semaphore (loc->probe.probe,
1721 if (bp_location_downloaded)
1722 observer_notify_breakpoint_modified (b);
1724 VEC_free (breakpoint_p, tp_vec);
1726 /* Send down all the trace state variables too. */
1727 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
1729 target_download_trace_state_variable (tsv);
1732 /* Tell target to treat text-like sections as transparent. */
1733 target_trace_set_readonly_regions ();
1734 /* Set some mode flags. */
1735 target_set_disconnected_tracing (disconnected_tracing);
1736 target_set_circular_trace_buffer (circular_trace_buffer);
1737 target_set_trace_buffer_size (trace_buffer_size);
1740 notes = trace_notes;
1741 ret = target_set_trace_notes (trace_user, notes, NULL);
1743 if (!ret && (trace_user || notes))
1744 warning (_("Target does not support trace user/notes, info ignored"));
1746 /* Now insert traps and begin collecting data. */
1747 target_trace_start ();
1749 /* Reset our local state. */
1750 trace_reset_local_state ();
1751 current_trace_status()->running = 1;
1754 /* The tstart command requests the target to start a new trace run.
1755 The command passes any arguments it has to the target verbatim, as
1756 an optional "trace note". This is useful as for instance a warning
1757 to other users if the trace runs disconnected, and you don't want
1758 anybody else messing with the target. */
1761 tstart_command (char *args, int from_tty)
1763 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1765 if (current_trace_status ()->running)
1768 && !query (_("A trace is running already. Start a new run? ")))
1769 error (_("New trace run not started."));
1772 start_tracing (args);
1775 /* The tstop command stops the tracing run. The command passes any
1776 supplied arguments to the target verbatim as a "stop note"; if the
1777 target supports trace notes, then it will be reported back as part
1778 of the trace run's status. */
1781 tstop_command (char *args, int from_tty)
1783 if (!current_trace_status ()->running)
1784 error (_("Trace is not running."));
1786 stop_tracing (args);
1790 stop_tracing (char *note)
1793 VEC(breakpoint_p) *tp_vec = NULL;
1795 struct breakpoint *t;
1797 target_trace_stop ();
1799 tp_vec = all_tracepoints ();
1800 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1802 struct bp_location *loc;
1804 if ((t->type == bp_fast_tracepoint
1805 ? !may_insert_fast_tracepoints
1806 : !may_insert_tracepoints))
1809 for (loc = t->loc; loc; loc = loc->next)
1811 /* GDB can be totally absent in some disconnected trace scenarios,
1812 but we don't really care if this semaphore goes out of sync.
1813 That's why we are decrementing it here, but not taking care
1815 if (loc->probe.probe != NULL
1816 && loc->probe.probe->pops->clear_semaphore != NULL)
1817 loc->probe.probe->pops->clear_semaphore (loc->probe.probe,
1823 VEC_free (breakpoint_p, tp_vec);
1826 note = trace_stop_notes;
1827 ret = target_set_trace_notes (NULL, NULL, note);
1830 warning (_("Target does not support trace notes, note ignored"));
1832 /* Should change in response to reply? */
1833 current_trace_status ()->running = 0;
1836 /* tstatus command */
1838 tstatus_command (char *args, int from_tty)
1840 struct trace_status *ts = current_trace_status ();
1842 VEC(breakpoint_p) *tp_vec = NULL;
1843 struct breakpoint *t;
1845 status = target_get_trace_status (ts);
1849 if (ts->filename != NULL)
1850 printf_filtered (_("Using a trace file.\n"));
1853 printf_filtered (_("Trace can not be run on this target.\n"));
1858 if (!ts->running_known)
1860 printf_filtered (_("Run/stop status is unknown.\n"));
1862 else if (ts->running)
1864 printf_filtered (_("Trace is running on the target.\n"));
1868 switch (ts->stop_reason)
1870 case trace_never_run:
1871 printf_filtered (_("No trace has been run on the target.\n"));
1873 case trace_stop_command:
1875 printf_filtered (_("Trace stopped by a tstop command (%s).\n"),
1878 printf_filtered (_("Trace stopped by a tstop command.\n"));
1880 case trace_buffer_full:
1881 printf_filtered (_("Trace stopped because the buffer was full.\n"));
1883 case trace_disconnected:
1884 printf_filtered (_("Trace stopped because of disconnection.\n"));
1886 case tracepoint_passcount:
1887 printf_filtered (_("Trace stopped by tracepoint %d.\n"),
1888 ts->stopping_tracepoint);
1890 case tracepoint_error:
1891 if (ts->stopping_tracepoint)
1892 printf_filtered (_("Trace stopped by an "
1893 "error (%s, tracepoint %d).\n"),
1894 ts->stop_desc, ts->stopping_tracepoint);
1896 printf_filtered (_("Trace stopped by an error (%s).\n"),
1899 case trace_stop_reason_unknown:
1900 printf_filtered (_("Trace stopped for an unknown reason.\n"));
1903 printf_filtered (_("Trace stopped for some other reason (%d).\n"),
1909 if (ts->traceframes_created >= 0
1910 && ts->traceframe_count != ts->traceframes_created)
1912 printf_filtered (_("Buffer contains %d trace "
1913 "frames (of %d created total).\n"),
1914 ts->traceframe_count, ts->traceframes_created);
1916 else if (ts->traceframe_count >= 0)
1918 printf_filtered (_("Collected %d trace frames.\n"),
1919 ts->traceframe_count);
1922 if (ts->buffer_free >= 0)
1924 if (ts->buffer_size >= 0)
1926 printf_filtered (_("Trace buffer has %d bytes of %d bytes free"),
1927 ts->buffer_free, ts->buffer_size);
1928 if (ts->buffer_size > 0)
1929 printf_filtered (_(" (%d%% full)"),
1930 ((int) ((((long long) (ts->buffer_size
1931 - ts->buffer_free)) * 100)
1932 / ts->buffer_size)));
1933 printf_filtered (_(".\n"));
1936 printf_filtered (_("Trace buffer has %d bytes free.\n"),
1940 if (ts->disconnected_tracing)
1941 printf_filtered (_("Trace will continue if GDB disconnects.\n"));
1943 printf_filtered (_("Trace will stop if GDB disconnects.\n"));
1945 if (ts->circular_buffer)
1946 printf_filtered (_("Trace buffer is circular.\n"));
1948 if (ts->user_name && strlen (ts->user_name) > 0)
1949 printf_filtered (_("Trace user is %s.\n"), ts->user_name);
1951 if (ts->notes && strlen (ts->notes) > 0)
1952 printf_filtered (_("Trace notes: %s.\n"), ts->notes);
1954 /* Now report on what we're doing with tfind. */
1955 if (traceframe_number >= 0)
1956 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
1957 traceframe_number, tracepoint_number);
1959 printf_filtered (_("Not looking at any trace frame.\n"));
1961 /* Report start/stop times if supplied. */
1966 LONGEST run_time = ts->stop_time - ts->start_time;
1968 /* Reporting a run time is more readable than two long numbers. */
1969 printf_filtered (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
1970 (long int) (ts->start_time / 1000000),
1971 (long int) (ts->start_time % 1000000),
1972 (long int) (run_time / 1000000),
1973 (long int) (run_time % 1000000));
1976 printf_filtered (_("Trace started at %ld.%06ld secs.\n"),
1977 (long int) (ts->start_time / 1000000),
1978 (long int) (ts->start_time % 1000000));
1980 else if (ts->stop_time)
1981 printf_filtered (_("Trace stopped at %ld.%06ld secs.\n"),
1982 (long int) (ts->stop_time / 1000000),
1983 (long int) (ts->stop_time % 1000000));
1985 /* Now report any per-tracepoint status available. */
1986 tp_vec = all_tracepoints ();
1988 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1989 target_get_tracepoint_status (t, NULL);
1991 VEC_free (breakpoint_p, tp_vec);
1994 /* Report the trace status to uiout, in a way suitable for MI, and not
1995 suitable for CLI. If ON_STOP is true, suppress a few fields that
1996 are not meaningful in the -trace-stop response.
1998 The implementation is essentially parallel to trace_status_command, but
1999 merging them will result in unreadable code. */
2001 trace_status_mi (int on_stop)
2003 struct ui_out *uiout = current_uiout;
2004 struct trace_status *ts = current_trace_status ();
2007 status = target_get_trace_status (ts);
2009 if (status == -1 && ts->filename == NULL)
2011 uiout->field_string ("supported", "0");
2015 if (ts->filename != NULL)
2016 uiout->field_string ("supported", "file");
2018 uiout->field_string ("supported", "1");
2020 if (ts->filename != NULL)
2021 uiout->field_string ("trace-file", ts->filename);
2023 gdb_assert (ts->running_known);
2027 uiout->field_string ("running", "1");
2029 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
2030 Given that the frontend gets the status either on -trace-stop, or from
2031 -trace-status after re-connection, it does not seem like this
2032 information is necessary for anything. It is not necessary for either
2033 figuring the vital state of the target nor for navigation of trace
2034 frames. If the frontend wants to show the current state is some
2035 configure dialog, it can request the value when such dialog is
2036 invoked by the user. */
2040 const char *stop_reason = NULL;
2041 int stopping_tracepoint = -1;
2044 uiout->field_string ("running", "0");
2046 if (ts->stop_reason != trace_stop_reason_unknown)
2048 switch (ts->stop_reason)
2050 case trace_stop_command:
2051 stop_reason = "request";
2053 case trace_buffer_full:
2054 stop_reason = "overflow";
2056 case trace_disconnected:
2057 stop_reason = "disconnection";
2059 case tracepoint_passcount:
2060 stop_reason = "passcount";
2061 stopping_tracepoint = ts->stopping_tracepoint;
2063 case tracepoint_error:
2064 stop_reason = "error";
2065 stopping_tracepoint = ts->stopping_tracepoint;
2071 uiout->field_string ("stop-reason", stop_reason);
2072 if (stopping_tracepoint != -1)
2073 uiout->field_int ("stopping-tracepoint",
2074 stopping_tracepoint);
2075 if (ts->stop_reason == tracepoint_error)
2076 uiout->field_string ("error-description",
2082 if (ts->traceframe_count != -1)
2083 uiout->field_int ("frames", ts->traceframe_count);
2084 if (ts->traceframes_created != -1)
2085 uiout->field_int ("frames-created", ts->traceframes_created);
2086 if (ts->buffer_size != -1)
2087 uiout->field_int ("buffer-size", ts->buffer_size);
2088 if (ts->buffer_free != -1)
2089 uiout->field_int ("buffer-free", ts->buffer_free);
2091 uiout->field_int ("disconnected", ts->disconnected_tracing);
2092 uiout->field_int ("circular", ts->circular_buffer);
2094 uiout->field_string ("user-name", ts->user_name);
2095 uiout->field_string ("notes", ts->notes);
2100 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2101 (long int) (ts->start_time / 1000000),
2102 (long int) (ts->start_time % 1000000));
2103 uiout->field_string ("start-time", buf);
2104 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2105 (long int) (ts->stop_time / 1000000),
2106 (long int) (ts->stop_time % 1000000));
2107 uiout->field_string ("stop-time", buf);
2111 /* Check if a trace run is ongoing. If so, and FROM_TTY, query the
2112 user if she really wants to detach. */
2115 query_if_trace_running (int from_tty)
2120 /* It can happen that the target that was tracing went away on its
2121 own, and we didn't notice. Get a status update, and if the
2122 current target doesn't even do tracing, then assume it's not
2124 if (target_get_trace_status (current_trace_status ()) < 0)
2125 current_trace_status ()->running = 0;
2127 /* If running interactively, give the user the option to cancel and
2128 then decide what to do differently with the run. Scripts are
2129 just going to disconnect and let the target deal with it,
2130 according to how it's been instructed previously via
2131 disconnected-tracing. */
2132 if (current_trace_status ()->running)
2134 process_tracepoint_on_disconnect ();
2136 if (current_trace_status ()->disconnected_tracing)
2138 if (!query (_("Trace is running and will "
2139 "continue after detach; detach anyway? ")))
2140 error (_("Not confirmed."));
2144 if (!query (_("Trace is running but will "
2145 "stop on detach; detach anyway? ")))
2146 error (_("Not confirmed."));
2151 /* This function handles the details of what to do about an ongoing
2152 tracing run if the user has asked to detach or otherwise disconnect
2156 disconnect_tracing (void)
2158 /* Also we want to be out of tfind mode, otherwise things can get
2159 confusing upon reconnection. Just use these calls instead of
2160 full tfind_1 behavior because we're in the middle of detaching,
2161 and there's no point to updating current stack frame etc. */
2162 trace_reset_local_state ();
2165 /* Worker function for the various flavors of the tfind command. */
2167 tfind_1 (enum trace_find_type type, int num,
2168 CORE_ADDR addr1, CORE_ADDR addr2,
2171 int target_frameno = -1, target_tracept = -1;
2172 struct frame_id old_frame_id = null_frame_id;
2173 struct tracepoint *tp;
2174 struct ui_out *uiout = current_uiout;
2176 /* Only try to get the current stack frame if we have a chance of
2177 succeeding. In particular, if we're trying to get a first trace
2178 frame while all threads are running, it's not going to succeed,
2179 so leave it with a default value and let the frame comparison
2180 below (correctly) decide to print out the source location of the
2182 if (!(type == tfind_number && num == -1)
2183 && (has_stack_frames () || traceframe_number >= 0))
2184 old_frame_id = get_frame_id (get_current_frame ());
2186 target_frameno = target_trace_find (type, num, addr1, addr2,
2189 if (type == tfind_number
2191 && target_frameno == -1)
2193 /* We told the target to get out of tfind mode, and it did. */
2195 else if (target_frameno == -1)
2197 /* A request for a non-existent trace frame has failed.
2198 Our response will be different, depending on FROM_TTY:
2200 If FROM_TTY is true, meaning that this command was
2201 typed interactively by the user, then give an error
2202 and DO NOT change the state of traceframe_number etc.
2204 However if FROM_TTY is false, meaning that we're either
2205 in a script, a loop, or a user-defined command, then
2206 DON'T give an error, but DO change the state of
2207 traceframe_number etc. to invalid.
2209 The rationalle is that if you typed the command, you
2210 might just have committed a typo or something, and you'd
2211 like to NOT lose your current debugging state. However
2212 if you're in a user-defined command or especially in a
2213 loop, then you need a way to detect that the command
2214 failed WITHOUT aborting. This allows you to write
2215 scripts that search thru the trace buffer until the end,
2216 and then continue on to do something else. */
2219 error (_("Target failed to find requested trace frame."));
2223 printf_filtered ("End of trace buffer.\n");
2224 #if 0 /* dubious now? */
2225 /* The following will not recurse, since it's
2227 tfind_command ("-1", from_tty);
2232 tp = get_tracepoint_by_number_on_target (target_tracept);
2234 reinit_frame_cache ();
2235 target_dcache_invalidate ();
2237 set_tracepoint_num (tp ? tp->number : target_tracept);
2239 if (target_frameno != get_traceframe_number ())
2240 observer_notify_traceframe_changed (target_frameno, tracepoint_number);
2242 set_current_traceframe (target_frameno);
2244 if (target_frameno == -1)
2245 set_traceframe_context (NULL);
2247 set_traceframe_context (get_current_frame ());
2249 if (traceframe_number >= 0)
2251 /* Use different branches for MI and CLI to make CLI messages
2253 if (uiout->is_mi_like_p ())
2255 uiout->field_string ("found", "1");
2256 uiout->field_int ("tracepoint", tracepoint_number);
2257 uiout->field_int ("traceframe", traceframe_number);
2261 printf_unfiltered (_("Found trace frame %d, tracepoint %d\n"),
2262 traceframe_number, tracepoint_number);
2267 if (uiout->is_mi_like_p ())
2268 uiout->field_string ("found", "0");
2269 else if (type == tfind_number && num == -1)
2270 printf_unfiltered (_("No longer looking at any trace frame\n"));
2271 else /* This case may never occur, check. */
2272 printf_unfiltered (_("No trace frame found\n"));
2275 /* If we're in nonstop mode and getting out of looking at trace
2276 frames, there won't be any current frame to go back to and
2279 && (has_stack_frames () || traceframe_number >= 0))
2281 enum print_what print_what;
2283 /* NOTE: in imitation of the step command, try to determine
2284 whether we have made a transition from one function to
2285 another. If so, we'll print the "stack frame" (ie. the new
2286 function and it's arguments) -- otherwise we'll just show the
2289 if (frame_id_eq (old_frame_id,
2290 get_frame_id (get_current_frame ())))
2291 print_what = SRC_LINE;
2293 print_what = SRC_AND_LOC;
2295 print_stack_frame (get_selected_frame (NULL), 1, print_what, 1);
2300 /* Error on looking at traceframes while trace is running. */
2303 check_trace_running (struct trace_status *status)
2305 if (status->running && status->filename == NULL)
2306 error (_("May not look at trace frames while trace is running."));
2309 /* trace_find_command takes a trace frame number n,
2310 sends "QTFrame:<n>" to the target,
2311 and accepts a reply that may contain several optional pieces
2312 of information: a frame number, a tracepoint number, and an
2313 indication of whether this is a trap frame or a stepping frame.
2315 The minimal response is just "OK" (which indicates that the
2316 target does not give us a frame number or a tracepoint number).
2317 Instead of that, the target may send us a string containing
2319 F<hexnum> (gives the selected frame number)
2320 T<hexnum> (gives the selected tracepoint number)
2325 tfind_command_1 (const char *args, int from_tty)
2326 { /* This should only be called with a numeric argument. */
2329 check_trace_running (current_trace_status ());
2331 if (args == 0 || *args == 0)
2332 { /* TFIND with no args means find NEXT trace frame. */
2333 if (traceframe_number == -1)
2334 frameno = 0; /* "next" is first one. */
2336 frameno = traceframe_number + 1;
2338 else if (0 == strcmp (args, "-"))
2340 if (traceframe_number == -1)
2341 error (_("not debugging trace buffer"));
2342 else if (from_tty && traceframe_number == 0)
2343 error (_("already at start of trace buffer"));
2345 frameno = traceframe_number - 1;
2347 /* A hack to work around eval's need for fp to have been collected. */
2348 else if (0 == strcmp (args, "-1"))
2351 frameno = parse_and_eval_long (args);
2354 error (_("invalid input (%d is less than zero)"), frameno);
2356 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
2360 tfind_command (char *args, int from_tty)
2362 tfind_command_1 (const_cast<char *> (args), from_tty);
2367 tfind_end_command (char *args, int from_tty)
2369 tfind_command_1 ("-1", from_tty);
2374 tfind_start_command (char *args, int from_tty)
2376 tfind_command_1 ("0", from_tty);
2379 /* tfind pc command */
2381 tfind_pc_command (char *args, int from_tty)
2385 check_trace_running (current_trace_status ());
2387 if (args == 0 || *args == 0)
2388 pc = regcache_read_pc (get_current_regcache ());
2390 pc = parse_and_eval_address (args);
2392 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
2395 /* tfind tracepoint command */
2397 tfind_tracepoint_command (char *args, int from_tty)
2400 struct tracepoint *tp;
2402 check_trace_running (current_trace_status ());
2404 if (args == 0 || *args == 0)
2406 if (tracepoint_number == -1)
2407 error (_("No current tracepoint -- please supply an argument."));
2409 tdp = tracepoint_number; /* Default is current TDP. */
2412 tdp = parse_and_eval_long (args);
2414 /* If we have the tracepoint on hand, use the number that the
2415 target knows about (which may be different if we disconnected
2416 and reconnected). */
2417 tp = get_tracepoint (tdp);
2419 tdp = tp->number_on_target;
2421 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
2424 /* TFIND LINE command:
2426 This command will take a sourceline for argument, just like BREAK
2427 or TRACE (ie. anything that "decode_line_1" can handle).
2429 With no argument, this command will find the next trace frame
2430 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2433 tfind_line_command (char *args, int from_tty)
2435 check_trace_running (current_trace_status ());
2437 symtab_and_line sal;
2438 if (args == 0 || *args == 0)
2440 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
2444 std::vector<symtab_and_line> sals
2445 = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
2449 if (sal.symtab == 0)
2450 error (_("No line number information available."));
2452 CORE_ADDR start_pc, end_pc;
2453 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2455 if (start_pc == end_pc)
2457 printf_filtered ("Line %d of \"%s\"",
2459 symtab_to_filename_for_display (sal.symtab));
2461 printf_filtered (" is at address ");
2462 print_address (get_current_arch (), start_pc, gdb_stdout);
2464 printf_filtered (" but contains no code.\n");
2465 sal = find_pc_line (start_pc, 0);
2467 && find_line_pc_range (sal, &start_pc, &end_pc)
2468 && start_pc != end_pc)
2469 printf_filtered ("Attempting to find line %d instead.\n",
2472 error (_("Cannot find a good line."));
2476 /* Is there any case in which we get here, and have an address
2477 which the user would want to see? If we have debugging
2478 symbols and no line numbers? */
2479 error (_("Line number %d is out of range for \"%s\"."),
2480 sal.line, symtab_to_filename_for_display (sal.symtab));
2482 /* Find within range of stated line. */
2484 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
2486 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
2489 /* tfind range command */
2491 tfind_range_command (char *args, int from_tty)
2493 static CORE_ADDR start, stop;
2496 check_trace_running (current_trace_status ());
2498 if (args == 0 || *args == 0)
2499 { /* XXX FIXME: what should default behavior be? */
2500 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2504 if (0 != (tmp = strchr (args, ',')))
2506 *tmp++ = '\0'; /* Terminate start address. */
2507 tmp = skip_spaces (tmp);
2508 start = parse_and_eval_address (args);
2509 stop = parse_and_eval_address (tmp);
2512 { /* No explicit end address? */
2513 start = parse_and_eval_address (args);
2514 stop = start + 1; /* ??? */
2517 tfind_1 (tfind_range, 0, start, stop, from_tty);
2520 /* tfind outside command */
2522 tfind_outside_command (char *args, int from_tty)
2524 CORE_ADDR start, stop;
2527 if (current_trace_status ()->running
2528 && current_trace_status ()->filename == NULL)
2529 error (_("May not look at trace frames while trace is running."));
2531 if (args == 0 || *args == 0)
2532 { /* XXX FIXME: what should default behavior be? */
2533 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2537 if (0 != (tmp = strchr (args, ',')))
2539 *tmp++ = '\0'; /* Terminate start address. */
2540 tmp = skip_spaces (tmp);
2541 start = parse_and_eval_address (args);
2542 stop = parse_and_eval_address (tmp);
2545 { /* No explicit end address? */
2546 start = parse_and_eval_address (args);
2547 stop = start + 1; /* ??? */
2550 tfind_1 (tfind_outside, 0, start, stop, from_tty);
2553 /* info scope command: list the locals for a scope. */
2555 info_scope_command (char *args, int from_tty)
2558 struct bound_minimal_symbol msym;
2559 const struct block *block;
2560 const char *symname;
2561 char *save_args = args;
2562 struct block_iterator iter;
2564 struct gdbarch *gdbarch;
2567 if (args == 0 || *args == 0)
2568 error (_("requires an argument (function, "
2569 "line or *addr) to define a scope"));
2571 event_location_up location = string_to_event_location (&args,
2573 std::vector<symtab_and_line> sals
2574 = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
2578 /* Presumably decode_line_1 has already warned. */
2582 /* Resolve line numbers to PC. */
2583 resolve_sal_pc (&sals[0]);
2584 block = block_for_pc (sals[0].pc);
2588 QUIT; /* Allow user to bail out with ^C. */
2589 ALL_BLOCK_SYMBOLS (block, iter, sym)
2591 QUIT; /* Allow user to bail out with ^C. */
2593 printf_filtered ("Scope for %s:\n", save_args);
2596 symname = SYMBOL_PRINT_NAME (sym);
2597 if (symname == NULL || *symname == '\0')
2598 continue; /* Probably botched, certainly useless. */
2600 gdbarch = symbol_arch (sym);
2602 printf_filtered ("Symbol %s is ", symname);
2604 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
2605 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym,
2606 BLOCK_START (block),
2610 switch (SYMBOL_CLASS (sym))
2613 case LOC_UNDEF: /* Messed up symbol? */
2614 printf_filtered ("a bogus symbol, class %d.\n",
2615 SYMBOL_CLASS (sym));
2616 count--; /* Don't count this one. */
2619 printf_filtered ("a constant with value %s (%s)",
2620 plongest (SYMBOL_VALUE (sym)),
2621 hex_string (SYMBOL_VALUE (sym)));
2623 case LOC_CONST_BYTES:
2624 printf_filtered ("constant bytes: ");
2625 if (SYMBOL_TYPE (sym))
2626 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2627 fprintf_filtered (gdb_stdout, " %02x",
2628 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2631 printf_filtered ("in static storage at address ");
2632 printf_filtered ("%s", paddress (gdbarch,
2633 SYMBOL_VALUE_ADDRESS (sym)));
2636 /* GDBARCH is the architecture associated with the objfile
2637 the symbol is defined in; the target architecture may be
2638 different, and may provide additional registers. However,
2639 we do not know the target architecture at this point.
2640 We assume the objfile architecture will contain all the
2641 standard registers that occur in debug info in that
2643 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2646 if (SYMBOL_IS_ARGUMENT (sym))
2647 printf_filtered ("an argument in register $%s",
2648 gdbarch_register_name (gdbarch, regno));
2650 printf_filtered ("a local variable in register $%s",
2651 gdbarch_register_name (gdbarch, regno));
2654 printf_filtered ("an argument at stack/frame offset %s",
2655 plongest (SYMBOL_VALUE (sym)));
2658 printf_filtered ("a local variable at frame offset %s",
2659 plongest (SYMBOL_VALUE (sym)));
2662 printf_filtered ("a reference argument at offset %s",
2663 plongest (SYMBOL_VALUE (sym)));
2665 case LOC_REGPARM_ADDR:
2666 /* Note comment at LOC_REGISTER. */
2667 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2669 printf_filtered ("the address of an argument, in register $%s",
2670 gdbarch_register_name (gdbarch, regno));
2673 printf_filtered ("a typedef.\n");
2676 printf_filtered ("a label at address ");
2677 printf_filtered ("%s", paddress (gdbarch,
2678 SYMBOL_VALUE_ADDRESS (sym)));
2681 printf_filtered ("a function at address ");
2682 printf_filtered ("%s",
2683 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
2685 case LOC_UNRESOLVED:
2686 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
2688 if (msym.minsym == NULL)
2689 printf_filtered ("Unresolved Static");
2692 printf_filtered ("static storage at address ");
2693 printf_filtered ("%s",
2695 BMSYMBOL_VALUE_ADDRESS (msym)));
2698 case LOC_OPTIMIZED_OUT:
2699 printf_filtered ("optimized out.\n");
2702 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
2705 if (SYMBOL_TYPE (sym))
2706 printf_filtered (", length %d.\n",
2707 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
2709 if (BLOCK_FUNCTION (block))
2712 block = BLOCK_SUPERBLOCK (block);
2715 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2719 /* Helper for trace_dump_command. Dump the action list starting at
2720 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2721 actions of the body of a while-stepping action. STEPPING_FRAME is
2722 set if the current traceframe was determined to be a while-stepping
2726 trace_dump_actions (struct command_line *action,
2727 int stepping_actions, int stepping_frame,
2730 const char *action_exp, *next_comma;
2732 for (; action != NULL; action = action->next)
2734 struct cmd_list_element *cmd;
2736 QUIT; /* Allow user to bail out with ^C. */
2737 action_exp = action->line;
2738 action_exp = skip_spaces_const (action_exp);
2740 /* The collection actions to be done while stepping are
2741 bracketed by the commands "while-stepping" and "end". */
2743 if (*action_exp == '#') /* comment line */
2746 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2748 error (_("Bad action list item: %s"), action_exp);
2750 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
2754 for (i = 0; i < action->body_count; ++i)
2755 trace_dump_actions (action->body_list[i],
2756 1, stepping_frame, from_tty);
2758 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
2760 /* Display the collected data.
2761 For the trap frame, display only what was collected at
2762 the trap. Likewise for stepping frames, display only
2763 what was collected while stepping. This means that the
2764 two boolean variables, STEPPING_FRAME and
2765 STEPPING_ACTIONS should be equal. */
2766 if (stepping_frame == stepping_actions)
2769 struct cleanup *old_chain
2770 = make_cleanup (free_current_contents, &cmd);
2771 int trace_string = 0;
2773 if (*action_exp == '/')
2774 action_exp = decode_agent_options (action_exp, &trace_string);
2777 { /* Repeat over a comma-separated list. */
2778 QUIT; /* Allow user to bail out with ^C. */
2779 if (*action_exp == ',')
2781 action_exp = skip_spaces_const (action_exp);
2783 next_comma = strchr (action_exp, ',');
2785 if (0 == strncasecmp (action_exp, "$reg", 4))
2786 registers_info (NULL, from_tty);
2787 else if (0 == strncasecmp (action_exp, "$_ret", 5))
2789 else if (0 == strncasecmp (action_exp, "$loc", 4))
2790 info_locals_command (NULL, from_tty);
2791 else if (0 == strncasecmp (action_exp, "$arg", 4))
2792 info_args_command (NULL, from_tty);
2795 if (next_comma != NULL)
2797 size_t len = next_comma - action_exp;
2799 cmd = (char *) xrealloc (cmd, len + 1);
2800 memcpy (cmd, action_exp, len);
2805 size_t len = strlen (action_exp);
2807 cmd = (char *) xrealloc (cmd, len + 1);
2808 memcpy (cmd, action_exp, len + 1);
2811 printf_filtered ("%s = ", cmd);
2812 output_command_const (cmd, from_tty);
2813 printf_filtered ("\n");
2815 action_exp = next_comma;
2817 while (action_exp && *action_exp == ',');
2819 do_cleanups (old_chain);
2825 /* Return bp_location of the tracepoint associated with the current
2826 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
2827 is a stepping traceframe. */
2829 struct bp_location *
2830 get_traceframe_location (int *stepping_frame_p)
2832 struct tracepoint *t;
2833 struct bp_location *tloc;
2834 struct regcache *regcache;
2836 if (tracepoint_number == -1)
2837 error (_("No current trace frame."));
2839 t = get_tracepoint (tracepoint_number);
2842 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2845 /* The current frame is a trap frame if the frame PC is equal to the
2846 tracepoint PC. If not, then the current frame was collected
2847 during single-stepping. */
2848 regcache = get_current_regcache ();
2850 /* If the traceframe's address matches any of the tracepoint's
2851 locations, assume it is a direct hit rather than a while-stepping
2852 frame. (FIXME this is not reliable, should record each frame's
2854 for (tloc = t->loc; tloc; tloc = tloc->next)
2855 if (tloc->address == regcache_read_pc (regcache))
2857 *stepping_frame_p = 0;
2861 /* If this is a stepping frame, we don't know which location
2862 triggered. The first is as good (or bad) a guess as any... */
2863 *stepping_frame_p = 1;
2867 /* Return all the actions, including default collect, of a tracepoint
2868 T. It constructs cleanups into the chain, and leaves the caller to
2869 handle them (call do_cleanups). */
2871 static struct command_line *
2872 all_tracepoint_actions_and_cleanup (struct breakpoint *t)
2874 struct command_line *actions;
2876 actions = breakpoint_commands (t);
2878 /* If there are default expressions to collect, make up a collect
2879 action and prepend to the action list to encode. Note that since
2880 validation is per-tracepoint (local var "xyz" might be valid for
2881 one tracepoint and not another, etc), we make up the action on
2882 the fly, and don't cache it. */
2883 if (*default_collect)
2885 struct command_line *default_collect_action;
2886 char *default_collect_line;
2888 default_collect_line = xstrprintf ("collect %s", default_collect);
2889 make_cleanup (xfree, default_collect_line);
2891 validate_actionline (default_collect_line, t);
2892 default_collect_action = XNEW (struct command_line);
2893 make_cleanup (xfree, default_collect_action);
2894 default_collect_action->next = actions;
2895 default_collect_action->line = default_collect_line;
2896 actions = default_collect_action;
2902 /* The tdump command. */
2905 tdump_command (char *args, int from_tty)
2907 int stepping_frame = 0;
2908 struct bp_location *loc;
2909 struct command_line *actions;
2911 /* This throws an error is not inspecting a trace frame. */
2912 loc = get_traceframe_location (&stepping_frame);
2914 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2915 tracepoint_number, traceframe_number);
2917 /* This command only makes sense for the current frame, not the
2919 scoped_restore_current_thread restore_thread;
2921 select_frame (get_current_frame ());
2923 actions = all_tracepoint_actions_and_cleanup (loc->owner);
2925 trace_dump_actions (actions, 0, stepping_frame, from_tty);
2928 /* Encode a piece of a tracepoint's source-level definition in a form
2929 that is suitable for both protocol and saving in files. */
2930 /* This version does not do multiple encodes for long strings; it should
2931 return an offset to the next piece to encode. FIXME */
2934 encode_source_string (int tpnum, ULONGEST addr,
2935 const char *srctype, const char *src,
2936 char *buf, int buf_size)
2938 if (80 + strlen (srctype) > buf_size)
2939 error (_("Buffer too small for source encoding"));
2940 sprintf (buf, "%x:%s:%s:%x:%x:",
2941 tpnum, phex_nz (addr, sizeof (addr)),
2942 srctype, 0, (int) strlen (src));
2943 if (strlen (buf) + strlen (src) * 2 >= buf_size)
2944 error (_("Source string too long for buffer"));
2945 bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src));
2949 /* Tell the target what to do with an ongoing tracing run if GDB
2950 disconnects for some reason. */
2953 set_disconnected_tracing (char *args, int from_tty,
2954 struct cmd_list_element *c)
2956 target_set_disconnected_tracing (disconnected_tracing);
2960 set_circular_trace_buffer (char *args, int from_tty,
2961 struct cmd_list_element *c)
2963 target_set_circular_trace_buffer (circular_trace_buffer);
2967 set_trace_buffer_size (char *args, int from_tty,
2968 struct cmd_list_element *c)
2970 target_set_trace_buffer_size (trace_buffer_size);
2974 set_trace_user (char *args, int from_tty,
2975 struct cmd_list_element *c)
2979 ret = target_set_trace_notes (trace_user, NULL, NULL);
2982 warning (_("Target does not support trace notes, user ignored"));
2986 set_trace_notes (char *args, int from_tty,
2987 struct cmd_list_element *c)
2991 ret = target_set_trace_notes (NULL, trace_notes, NULL);
2994 warning (_("Target does not support trace notes, note ignored"));
2998 set_trace_stop_notes (char *args, int from_tty,
2999 struct cmd_list_element *c)
3003 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes);
3006 warning (_("Target does not support trace notes, stop note ignored"));
3009 /* Convert the memory pointed to by mem into hex, placing result in buf.
3010 * Return a pointer to the last char put in buf (null)
3011 * "stolen" from sparc-stub.c
3014 static const char hexchars[] = "0123456789abcdef";
3017 mem2hex (gdb_byte *mem, char *buf, int count)
3025 *buf++ = hexchars[ch >> 4];
3026 *buf++ = hexchars[ch & 0xf];
3035 get_traceframe_number (void)
3037 return traceframe_number;
3041 get_tracepoint_number (void)
3043 return tracepoint_number;
3046 /* Make the traceframe NUM be the current trace frame. Does nothing
3047 if NUM is already current. */
3050 set_current_traceframe (int num)
3054 if (traceframe_number == num)
3056 /* Nothing to do. */
3060 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
3063 warning (_("could not change traceframe"));
3065 set_traceframe_num (newnum);
3067 /* Changing the traceframe changes our view of registers and of the
3069 registers_changed ();
3071 clear_traceframe_info ();
3074 /* A cleanup used when switching away and back from tfind mode. */
3076 struct current_traceframe_cleanup
3078 /* The traceframe we were inspecting. */
3079 int traceframe_number;
3083 do_restore_current_traceframe_cleanup (void *arg)
3085 struct current_traceframe_cleanup *old
3086 = (struct current_traceframe_cleanup *) arg;
3088 set_current_traceframe (old->traceframe_number);
3092 restore_current_traceframe_cleanup_dtor (void *arg)
3094 struct current_traceframe_cleanup *old
3095 = (struct current_traceframe_cleanup *) arg;
3101 make_cleanup_restore_current_traceframe (void)
3103 struct current_traceframe_cleanup *old =
3104 XNEW (struct current_traceframe_cleanup);
3106 old->traceframe_number = traceframe_number;
3108 return make_cleanup_dtor (do_restore_current_traceframe_cleanup, old,
3109 restore_current_traceframe_cleanup_dtor);
3112 /* Given a number and address, return an uploaded tracepoint with that
3113 number, creating if necessary. */
3115 struct uploaded_tp *
3116 get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
3118 struct uploaded_tp *utp;
3120 for (utp = *utpp; utp; utp = utp->next)
3121 if (utp->number == num && utp->addr == addr)
3124 utp = XCNEW (struct uploaded_tp);
3127 utp->actions = NULL;
3128 utp->step_actions = NULL;
3129 utp->cmd_strings = NULL;
3137 free_uploaded_tps (struct uploaded_tp **utpp)
3139 struct uploaded_tp *next_one;
3143 next_one = (*utpp)->next;
3149 /* Given a number and address, return an uploaded tracepoint with that
3150 number, creating if necessary. */
3152 struct uploaded_tsv *
3153 get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
3155 struct uploaded_tsv *utsv;
3157 for (utsv = *utsvp; utsv; utsv = utsv->next)
3158 if (utsv->number == num)
3161 utsv = XCNEW (struct uploaded_tsv);
3163 utsv->next = *utsvp;
3170 free_uploaded_tsvs (struct uploaded_tsv **utsvp)
3172 struct uploaded_tsv *next_one;
3176 next_one = (*utsvp)->next;
3182 /* FIXME this function is heuristic and will miss the cases where the
3183 conditional is semantically identical but differs in whitespace,
3184 such as "x == 0" vs "x==0". */
3187 cond_string_is_same (char *str1, char *str2)
3189 if (str1 == NULL || str2 == NULL)
3190 return (str1 == str2);
3192 return (strcmp (str1, str2) == 0);
3195 /* Look for an existing tracepoint that seems similar enough to the
3196 uploaded one. Enablement isn't compared, because the user can
3197 toggle that freely, and may have done so in anticipation of the
3198 next trace run. Return the location of matched tracepoint. */
3200 static struct bp_location *
3201 find_matching_tracepoint_location (struct uploaded_tp *utp)
3203 VEC(breakpoint_p) *tp_vec = all_tracepoints ();
3205 struct breakpoint *b;
3206 struct bp_location *loc;
3208 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
3210 struct tracepoint *t = (struct tracepoint *) b;
3212 if (b->type == utp->type
3213 && t->step_count == utp->step
3214 && t->pass_count == utp->pass
3215 && cond_string_is_same (t->cond_string, utp->cond_string)
3216 /* FIXME also test actions. */
3219 /* Scan the locations for an address match. */
3220 for (loc = b->loc; loc; loc = loc->next)
3222 if (loc->address == utp->addr)
3230 /* Given a list of tracepoints uploaded from a target, attempt to
3231 match them up with existing tracepoints, and create new ones if not
3235 merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
3237 struct uploaded_tp *utp;
3238 /* A set of tracepoints which are modified. */
3239 VEC(breakpoint_p) *modified_tp = NULL;
3241 struct breakpoint *b;
3243 /* Look for GDB tracepoints that match up with our uploaded versions. */
3244 for (utp = *uploaded_tps; utp; utp = utp->next)
3246 struct bp_location *loc;
3247 struct tracepoint *t;
3249 loc = find_matching_tracepoint_location (utp);
3254 /* Mark this location as already inserted. */
3256 t = (struct tracepoint *) loc->owner;
3257 printf_filtered (_("Assuming tracepoint %d is same "
3258 "as target's tracepoint %d at %s.\n"),
3259 loc->owner->number, utp->number,
3260 paddress (loc->gdbarch, utp->addr));
3262 /* The tracepoint LOC->owner was modified (the location LOC
3263 was marked as inserted in the target). Save it in
3264 MODIFIED_TP if not there yet. The 'breakpoint-modified'
3265 observers will be notified later once for each tracepoint
3266 saved in MODIFIED_TP. */
3268 VEC_iterate (breakpoint_p, modified_tp, ix, b);
3270 if (b == loc->owner)
3276 VEC_safe_push (breakpoint_p, modified_tp, loc->owner);
3280 t = create_tracepoint_from_upload (utp);
3282 printf_filtered (_("Created tracepoint %d for "
3283 "target's tracepoint %d at %s.\n"),
3284 t->number, utp->number,
3285 paddress (get_current_arch (), utp->addr));
3287 printf_filtered (_("Failed to create tracepoint for target's "
3288 "tracepoint %d at %s, skipping it.\n"),
3290 paddress (get_current_arch (), utp->addr));
3292 /* Whether found or created, record the number used by the
3293 target, to help with mapping target tracepoints back to their
3294 counterparts here. */
3296 t->number_on_target = utp->number;
3299 /* Notify 'breakpoint-modified' observer that at least one of B's
3300 locations was changed. */
3301 for (ix = 0; VEC_iterate (breakpoint_p, modified_tp, ix, b); ix++)
3302 observer_notify_breakpoint_modified (b);
3304 VEC_free (breakpoint_p, modified_tp);
3305 free_uploaded_tps (uploaded_tps);
3308 /* Trace state variables don't have much to identify them beyond their
3309 name, so just use that to detect matches. */
3311 static struct trace_state_variable *
3312 find_matching_tsv (struct uploaded_tsv *utsv)
3317 return find_trace_state_variable (utsv->name);
3320 static struct trace_state_variable *
3321 create_tsv_from_upload (struct uploaded_tsv *utsv)
3323 const char *namebase;
3326 struct trace_state_variable *tsv;
3327 struct cleanup *old_chain;
3331 namebase = utsv->name;
3332 buf = xstrprintf ("%s", namebase);
3337 buf = xstrprintf ("%s_%d", namebase, try_num++);
3340 /* Fish for a name that is not in use. */
3341 /* (should check against all internal vars?) */
3342 while (find_trace_state_variable (buf))
3345 buf = xstrprintf ("%s_%d", namebase, try_num++);
3348 old_chain = make_cleanup (xfree, buf);
3350 /* We have an available name, create the variable. */
3351 tsv = create_trace_state_variable (buf);
3352 tsv->initial_value = utsv->initial_value;
3353 tsv->builtin = utsv->builtin;
3355 observer_notify_tsv_created (tsv);
3357 do_cleanups (old_chain);
3362 /* Given a list of uploaded trace state variables, try to match them
3363 up with existing variables, or create additional ones. */
3366 merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
3369 struct uploaded_tsv *utsv;
3370 struct trace_state_variable *tsv;
3373 /* Most likely some numbers will have to be reassigned as part of
3374 the merge, so clear them all in anticipation. */
3375 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3378 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
3380 tsv = find_matching_tsv (utsv);
3384 printf_filtered (_("Assuming trace state variable $%s "
3385 "is same as target's variable %d.\n"),
3386 tsv->name, utsv->number);
3390 tsv = create_tsv_from_upload (utsv);
3392 printf_filtered (_("Created trace state variable "
3393 "$%s for target's variable %d.\n"),
3394 tsv->name, utsv->number);
3396 /* Give precedence to numberings that come from the target. */
3398 tsv->number = utsv->number;
3401 /* Renumber everything that didn't get a target-assigned number. */
3403 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3404 if (tsv->number > highest)
3405 highest = tsv->number;
3408 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3409 if (tsv->number == 0)
3410 tsv->number = highest++;
3412 free_uploaded_tsvs (uploaded_tsvs);
3415 /* Parse the part of trace status syntax that is shared between
3416 the remote protocol and the trace file reader. */
3419 parse_trace_status (char *line, struct trace_status *ts)
3421 char *p = line, *p1, *p2, *p3, *p_temp;
3425 ts->running_known = 1;
3426 ts->running = (*p++ == '1');
3427 ts->stop_reason = trace_stop_reason_unknown;
3428 xfree (ts->stop_desc);
3429 ts->stop_desc = NULL;
3430 ts->traceframe_count = -1;
3431 ts->traceframes_created = -1;
3432 ts->buffer_free = -1;
3433 ts->buffer_size = -1;
3434 ts->disconnected_tracing = 0;
3435 ts->circular_buffer = 0;
3436 xfree (ts->user_name);
3437 ts->user_name = NULL;
3440 ts->start_time = ts->stop_time = 0;
3444 p1 = strchr (p, ':');
3446 error (_("Malformed trace status, at %s\n\
3447 Status line: '%s'\n"), p, line);
3448 p3 = strchr (p, ';');
3450 p3 = p + strlen (p);
3451 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
3453 p = unpack_varlen_hex (++p1, &val);
3454 ts->stop_reason = trace_buffer_full;
3456 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
3458 p = unpack_varlen_hex (++p1, &val);
3459 ts->stop_reason = trace_never_run;
3461 else if (strncmp (p, stop_reason_names[tracepoint_passcount],
3464 p = unpack_varlen_hex (++p1, &val);
3465 ts->stop_reason = tracepoint_passcount;
3466 ts->stopping_tracepoint = val;
3468 else if (strncmp (p, stop_reason_names[trace_stop_command], p1 - p) == 0)
3470 p2 = strchr (++p1, ':');
3478 ts->stop_desc = (char *) xmalloc (strlen (line));
3479 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3480 ts->stop_desc[end] = '\0';
3483 ts->stop_desc = xstrdup ("");
3485 p = unpack_varlen_hex (++p2, &val);
3486 ts->stop_reason = trace_stop_command;
3488 else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0)
3490 p = unpack_varlen_hex (++p1, &val);
3491 ts->stop_reason = trace_disconnected;
3493 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
3495 p2 = strchr (++p1, ':');
3498 ts->stop_desc = (char *) xmalloc ((p2 - p1) / 2 + 1);
3499 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3500 ts->stop_desc[end] = '\0';
3503 ts->stop_desc = xstrdup ("");
3505 p = unpack_varlen_hex (++p2, &val);
3506 ts->stopping_tracepoint = val;
3507 ts->stop_reason = tracepoint_error;
3509 else if (strncmp (p, "tframes", p1 - p) == 0)
3511 p = unpack_varlen_hex (++p1, &val);
3512 ts->traceframe_count = val;
3514 else if (strncmp (p, "tcreated", p1 - p) == 0)
3516 p = unpack_varlen_hex (++p1, &val);
3517 ts->traceframes_created = val;
3519 else if (strncmp (p, "tfree", p1 - p) == 0)
3521 p = unpack_varlen_hex (++p1, &val);
3522 ts->buffer_free = val;
3524 else if (strncmp (p, "tsize", p1 - p) == 0)
3526 p = unpack_varlen_hex (++p1, &val);
3527 ts->buffer_size = val;
3529 else if (strncmp (p, "disconn", p1 - p) == 0)
3531 p = unpack_varlen_hex (++p1, &val);
3532 ts->disconnected_tracing = val;
3534 else if (strncmp (p, "circular", p1 - p) == 0)
3536 p = unpack_varlen_hex (++p1, &val);
3537 ts->circular_buffer = val;
3539 else if (strncmp (p, "starttime", p1 - p) == 0)
3541 p = unpack_varlen_hex (++p1, &val);
3542 ts->start_time = val;
3544 else if (strncmp (p, "stoptime", p1 - p) == 0)
3546 p = unpack_varlen_hex (++p1, &val);
3547 ts->stop_time = val;
3549 else if (strncmp (p, "username", p1 - p) == 0)
3552 ts->user_name = (char *) xmalloc (strlen (p) / 2);
3553 end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2);
3554 ts->user_name[end] = '\0';
3557 else if (strncmp (p, "notes", p1 - p) == 0)
3560 ts->notes = (char *) xmalloc (strlen (p) / 2);
3561 end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2);
3562 ts->notes[end] = '\0';
3567 /* Silently skip unknown optional info. */
3568 p_temp = strchr (p1 + 1, ';');
3572 /* Must be at the end. */
3579 parse_tracepoint_status (char *p, struct breakpoint *bp,
3580 struct uploaded_tp *utp)
3583 struct tracepoint *tp = (struct tracepoint *) bp;
3585 p = unpack_varlen_hex (p, &uval);
3587 tp->hit_count += uval;
3589 utp->hit_count += uval;
3590 p = unpack_varlen_hex (p + 1, &uval);
3592 tp->traceframe_usage += uval;
3594 utp->traceframe_usage += uval;
3595 /* Ignore any extra, allowing for future extensions. */
3598 /* Given a line of text defining a part of a tracepoint, parse it into
3599 an "uploaded tracepoint". */
3602 parse_tracepoint_definition (char *line, struct uploaded_tp **utpp)
3606 ULONGEST num, addr, step, pass, orig_size, xlen, start;
3609 char *cond, *srctype, *buf;
3610 struct uploaded_tp *utp = NULL;
3613 /* Both tracepoint and action definitions start with the same number
3614 and address sequence. */
3616 p = unpack_varlen_hex (p, &num);
3617 p++; /* skip a colon */
3618 p = unpack_varlen_hex (p, &addr);
3619 p++; /* skip a colon */
3622 enabled = (*p++ == 'E');
3623 p++; /* skip a colon */
3624 p = unpack_varlen_hex (p, &step);
3625 p++; /* skip a colon */
3626 p = unpack_varlen_hex (p, &pass);
3627 type = bp_tracepoint;
3629 /* Thumb through optional fields. */
3632 p++; /* skip a colon */
3635 type = bp_fast_tracepoint;
3637 p = unpack_varlen_hex (p, &orig_size);
3641 type = bp_static_tracepoint;
3647 p = unpack_varlen_hex (p, &xlen);
3648 p++; /* skip a comma */
3649 cond = (char *) xmalloc (2 * xlen + 1);
3650 strncpy (cond, p, 2 * xlen);
3651 cond[2 * xlen] = '\0';
3655 warning (_("Unrecognized char '%c' in tracepoint "
3656 "definition, skipping rest"), *p);
3658 utp = get_uploaded_tp (num, addr, utpp);
3660 utp->enabled = enabled;
3665 else if (piece == 'A')
3667 utp = get_uploaded_tp (num, addr, utpp);
3668 VEC_safe_push (char_ptr, utp->actions, xstrdup (p));
3670 else if (piece == 'S')
3672 utp = get_uploaded_tp (num, addr, utpp);
3673 VEC_safe_push (char_ptr, utp->step_actions, xstrdup (p));
3675 else if (piece == 'Z')
3677 /* Parse a chunk of source form definition. */
3678 utp = get_uploaded_tp (num, addr, utpp);
3680 p = strchr (p, ':');
3681 p++; /* skip a colon */
3682 p = unpack_varlen_hex (p, &start);
3683 p++; /* skip a colon */
3684 p = unpack_varlen_hex (p, &xlen);
3685 p++; /* skip a colon */
3687 buf = (char *) alloca (strlen (line));
3689 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3692 if (startswith (srctype, "at:"))
3693 utp->at_string = xstrdup (buf);
3694 else if (startswith (srctype, "cond:"))
3695 utp->cond_string = xstrdup (buf);
3696 else if (startswith (srctype, "cmd:"))
3697 VEC_safe_push (char_ptr, utp->cmd_strings, xstrdup (buf));
3699 else if (piece == 'V')
3701 utp = get_uploaded_tp (num, addr, utpp);
3703 parse_tracepoint_status (p, NULL, utp);
3707 /* Don't error out, the target might be sending us optional
3708 info that we don't care about. */
3709 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
3713 /* Convert a textual description of a trace state variable into an
3717 parse_tsv_definition (char *line, struct uploaded_tsv **utsvp)
3720 ULONGEST num, initval, builtin;
3722 struct uploaded_tsv *utsv = NULL;
3724 buf = (char *) alloca (strlen (line));
3727 p = unpack_varlen_hex (p, &num);
3728 p++; /* skip a colon */
3729 p = unpack_varlen_hex (p, &initval);
3730 p++; /* skip a colon */
3731 p = unpack_varlen_hex (p, &builtin);
3732 p++; /* skip a colon */
3733 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3736 utsv = get_uploaded_tsv (num, utsvp);
3737 utsv->initial_value = initval;
3738 utsv->builtin = builtin;
3739 utsv->name = xstrdup (buf);
3743 free_current_marker (void *arg)
3745 struct static_tracepoint_marker **marker_p
3746 = (struct static_tracepoint_marker **) arg;
3748 if (*marker_p != NULL)
3750 release_static_tracepoint_marker (*marker_p);
3757 /* Given a line of text defining a static tracepoint marker, parse it
3758 into a "static tracepoint marker" object. Throws an error is
3759 parsing fails. If PP is non-null, it points to one past the end of
3760 the parsed marker definition. */
3763 parse_static_tracepoint_marker_definition (char *line, char **pp,
3764 struct static_tracepoint_marker *marker)
3771 p = unpack_varlen_hex (p, &addr);
3772 p++; /* skip a colon */
3774 marker->gdbarch = target_gdbarch ();
3775 marker->address = (CORE_ADDR) addr;
3777 endp = strchr (p, ':');
3779 error (_("bad marker definition: %s"), line);
3781 marker->str_id = (char *) xmalloc (endp - p + 1);
3782 end = hex2bin (p, (gdb_byte *) marker->str_id, (endp - p + 1) / 2);
3783 marker->str_id[end] = '\0';
3786 p++; /* skip a colon */
3788 marker->extra = (char *) xmalloc (strlen (p) + 1);
3789 end = hex2bin (p, (gdb_byte *) marker->extra, strlen (p) / 2);
3790 marker->extra[end] = '\0';
3796 /* Release a static tracepoint marker's contents. Note that the
3797 object itself isn't released here. There objects are usually on
3801 release_static_tracepoint_marker (struct static_tracepoint_marker *marker)
3803 xfree (marker->str_id);
3804 marker->str_id = NULL;
3807 /* Print MARKER to gdb_stdout. */
3810 print_one_static_tracepoint_marker (int count,
3811 struct static_tracepoint_marker *marker)
3815 char wrap_indent[80];
3816 char extra_field_indent[80];
3817 struct ui_out *uiout = current_uiout;
3818 VEC(breakpoint_p) *tracepoints;
3820 symtab_and_line sal;
3821 sal.pc = marker->address;
3823 tracepoints = static_tracepoints_here (marker->address);
3825 ui_out_emit_tuple tuple_emitter (uiout, "marker");
3827 /* A counter field to help readability. This is not a stable
3829 uiout->field_int ("count", count);
3831 uiout->field_string ("marker-id", marker->str_id);
3833 uiout->field_fmt ("enabled", "%c",
3834 !VEC_empty (breakpoint_p, tracepoints) ? 'y' : 'n');
3837 strcpy (wrap_indent, " ");
3839 if (gdbarch_addr_bit (marker->gdbarch) <= 32)
3840 strcat (wrap_indent, " ");
3842 strcat (wrap_indent, " ");
3844 strcpy (extra_field_indent, " ");
3846 uiout->field_core_addr ("addr", marker->gdbarch, marker->address);
3848 sal = find_pc_line (marker->address, 0);
3849 sym = find_pc_sect_function (marker->address, NULL);
3852 uiout->text ("in ");
3853 uiout->field_string ("func",
3854 SYMBOL_PRINT_NAME (sym));
3855 uiout->wrap_hint (wrap_indent);
3856 uiout->text (" at ");
3859 uiout->field_skip ("func");
3861 if (sal.symtab != NULL)
3863 uiout->field_string ("file",
3864 symtab_to_filename_for_display (sal.symtab));
3867 if (uiout->is_mi_like_p ())
3869 const char *fullname = symtab_to_fullname (sal.symtab);
3871 uiout->field_string ("fullname", fullname);
3874 uiout->field_skip ("fullname");
3876 uiout->field_int ("line", sal.line);
3880 uiout->field_skip ("fullname");
3881 uiout->field_skip ("line");
3885 uiout->text (extra_field_indent);
3886 uiout->text (_("Data: \""));
3887 uiout->field_string ("extra-data", marker->extra);
3888 uiout->text ("\"\n");
3890 if (!VEC_empty (breakpoint_p, tracepoints))
3893 struct breakpoint *b;
3896 ui_out_emit_tuple tuple_emitter (uiout, "tracepoints-at");
3898 uiout->text (extra_field_indent);
3899 uiout->text (_("Probed by static tracepoints: "));
3900 for (ix = 0; VEC_iterate(breakpoint_p, tracepoints, ix, b); ix++)
3905 uiout->field_int ("tracepoint-id", b->number);
3909 if (uiout->is_mi_like_p ())
3910 uiout->field_int ("number-of-tracepoints",
3911 VEC_length(breakpoint_p, tracepoints));
3915 VEC_free (breakpoint_p, tracepoints);
3919 info_static_tracepoint_markers_command (char *arg, int from_tty)
3921 VEC(static_tracepoint_marker_p) *markers;
3922 struct cleanup *old_chain;
3923 struct static_tracepoint_marker *marker;
3924 struct ui_out *uiout = current_uiout;
3927 /* We don't have to check target_can_use_agent and agent's capability on
3928 static tracepoint here, in order to be compatible with older GDBserver.
3929 We don't check USE_AGENT is true or not, because static tracepoints
3930 don't work without in-process agent, so we don't bother users to type
3931 `set agent on' when to use static tracepoint. */
3933 ui_out_emit_table table_emitter (uiout, 5, -1,
3934 "StaticTracepointMarkersTable");
3936 uiout->table_header (7, ui_left, "counter", "Cnt");
3938 uiout->table_header (40, ui_left, "marker-id", "ID");
3940 uiout->table_header (3, ui_left, "enabled", "Enb");
3941 if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
3942 uiout->table_header (10, ui_left, "addr", "Address");
3944 uiout->table_header (18, ui_left, "addr", "Address");
3945 uiout->table_header (40, ui_noalign, "what", "What");
3947 uiout->table_body ();
3949 markers = target_static_tracepoint_markers_by_strid (NULL);
3950 old_chain = make_cleanup (VEC_cleanup (static_tracepoint_marker_p), &markers);
3953 VEC_iterate (static_tracepoint_marker_p,
3954 markers, i, marker);
3957 print_one_static_tracepoint_marker (i + 1, marker);
3958 release_static_tracepoint_marker (marker);
3961 do_cleanups (old_chain);
3964 /* The $_sdata convenience variable is a bit special. We don't know
3965 for sure type of the value until we actually have a chance to fetch
3966 the data --- the size of the object depends on what has been
3967 collected. We solve this by making $_sdata be an internalvar that
3968 creates a new value on access. */
3970 /* Return a new value with the correct type for the sdata object of
3971 the current trace frame. Return a void value if there's no object
3974 static struct value *
3975 sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var,
3981 /* We need to read the whole object before we know its size. */
3982 size = target_read_alloc (¤t_target,
3983 TARGET_OBJECT_STATIC_TRACE_DATA,
3990 type = init_vector_type (builtin_type (gdbarch)->builtin_true_char,
3992 v = allocate_value (type);
3993 memcpy (value_contents_raw (v), buf, size);
3998 return allocate_value (builtin_type (gdbarch)->builtin_void);
4001 #if !defined(HAVE_LIBEXPAT)
4003 struct traceframe_info *
4004 parse_traceframe_info (const char *tframe_info)
4006 static int have_warned;
4011 warning (_("Can not parse XML trace frame info; XML support "
4012 "was disabled at compile time"));
4018 #else /* HAVE_LIBEXPAT */
4020 #include "xml-support.h"
4022 /* Handle the start of a <memory> element. */
4025 traceframe_info_start_memory (struct gdb_xml_parser *parser,
4026 const struct gdb_xml_element *element,
4027 void *user_data, VEC(gdb_xml_value_s) *attributes)
4029 struct traceframe_info *info = (struct traceframe_info *) user_data;
4030 struct mem_range *r = VEC_safe_push (mem_range_s, info->memory, NULL);
4031 ULONGEST *start_p, *length_p;
4034 = (ULONGEST *) xml_find_attribute (attributes, "start")->value;
4036 = (ULONGEST *) xml_find_attribute (attributes, "length")->value;
4038 r->start = *start_p;
4039 r->length = *length_p;
4042 /* Handle the start of a <tvar> element. */
4045 traceframe_info_start_tvar (struct gdb_xml_parser *parser,
4046 const struct gdb_xml_element *element,
4048 VEC(gdb_xml_value_s) *attributes)
4050 struct traceframe_info *info = (struct traceframe_info *) user_data;
4051 const char *id_attrib
4052 = (const char *) xml_find_attribute (attributes, "id")->value;
4053 int id = gdb_xml_parse_ulongest (parser, id_attrib);
4055 VEC_safe_push (int, info->tvars, id);
4058 /* Discard the constructed trace frame info (if an error occurs). */
4061 free_result (void *p)
4063 struct traceframe_info *result = (struct traceframe_info *) p;
4065 free_traceframe_info (result);
4068 /* The allowed elements and attributes for an XML memory map. */
4070 static const struct gdb_xml_attribute memory_attributes[] = {
4071 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
4072 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
4073 { NULL, GDB_XML_AF_NONE, NULL, NULL }
4076 static const struct gdb_xml_attribute tvar_attributes[] = {
4077 { "id", GDB_XML_AF_NONE, NULL, NULL },
4078 { NULL, GDB_XML_AF_NONE, NULL, NULL }
4081 static const struct gdb_xml_element traceframe_info_children[] = {
4082 { "memory", memory_attributes, NULL,
4083 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
4084 traceframe_info_start_memory, NULL },
4085 { "tvar", tvar_attributes, NULL,
4086 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
4087 traceframe_info_start_tvar, NULL },
4088 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
4091 static const struct gdb_xml_element traceframe_info_elements[] = {
4092 { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE,
4094 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
4097 /* Parse a traceframe-info XML document. */
4099 struct traceframe_info *
4100 parse_traceframe_info (const char *tframe_info)
4102 struct traceframe_info *result;
4103 struct cleanup *back_to;
4105 result = XCNEW (struct traceframe_info);
4106 back_to = make_cleanup (free_result, result);
4108 if (gdb_xml_parse_quick (_("trace frame info"),
4109 "traceframe-info.dtd", traceframe_info_elements,
4110 tframe_info, result) == 0)
4112 /* Parsed successfully, keep the result. */
4113 discard_cleanups (back_to);
4118 do_cleanups (back_to);
4122 #endif /* HAVE_LIBEXPAT */
4124 /* Returns the traceframe_info object for the current traceframe.
4125 This is where we avoid re-fetching the object from the target if we
4126 already have it cached. */
4128 struct traceframe_info *
4129 get_traceframe_info (void)
4131 if (traceframe_info == NULL)
4132 traceframe_info = target_traceframe_info ();
4134 return traceframe_info;
4137 /* If the target supports the query, return in RESULT the set of
4138 collected memory in the current traceframe, found within the LEN
4139 bytes range starting at MEMADDR. Returns true if the target
4140 supports the query, otherwise returns false, and RESULT is left
4144 traceframe_available_memory (VEC(mem_range_s) **result,
4145 CORE_ADDR memaddr, ULONGEST len)
4147 struct traceframe_info *info = get_traceframe_info ();
4151 struct mem_range *r;
4156 for (i = 0; VEC_iterate (mem_range_s, info->memory, i, r); i++)
4157 if (mem_ranges_overlap (r->start, r->length, memaddr, len))
4159 ULONGEST lo1, hi1, lo2, hi2;
4160 struct mem_range *nr;
4163 hi1 = memaddr + len;
4166 hi2 = r->start + r->length;
4168 nr = VEC_safe_push (mem_range_s, *result, NULL);
4170 nr->start = std::max (lo1, lo2);
4171 nr->length = std::min (hi1, hi2) - nr->start;
4174 normalize_mem_ranges (*result);
4181 /* Implementation of `sdata' variable. */
4183 static const struct internalvar_funcs sdata_funcs =
4190 /* module initialization */
4192 _initialize_tracepoint (void)
4194 struct cmd_list_element *c;
4196 /* Explicitly create without lookup, since that tries to create a
4197 value with a void typed value, and when we get here, gdbarch
4198 isn't initialized yet. At this point, we're quite sure there
4199 isn't another convenience variable of the same name. */
4200 create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL);
4202 traceframe_number = -1;
4203 tracepoint_number = -1;
4205 add_info ("scope", info_scope_command,
4206 _("List the variables local to a scope"));
4208 add_cmd ("tracepoints", class_trace, NULL,
4209 _("Tracing of program execution without stopping the program."),
4212 add_com ("tdump", class_trace, tdump_command,
4213 _("Print everything collected at the current tracepoint."));
4215 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
4216 Define a trace state variable.\n\
4217 Argument is a $-prefixed name, optionally followed\n\
4218 by '=' and an expression that sets the initial value\n\
4219 at the start of tracing."));
4220 set_cmd_completer (c, expression_completer);
4222 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
4223 Delete one or more trace state variables.\n\
4224 Arguments are the names of the variables to delete.\n\
4225 If no arguments are supplied, delete all variables."), &deletelist);
4226 /* FIXME add a trace variable completer. */
4228 add_info ("tvariables", info_tvariables_command, _("\
4229 Status of trace state variables and their values.\n\
4232 add_info ("static-tracepoint-markers",
4233 info_static_tracepoint_markers_command, _("\
4234 List target static tracepoints markers.\n\
4237 add_prefix_cmd ("tfind", class_trace, tfind_command, _("\
4238 Select a trace frame;\n\
4239 No argument means forward by one frame; '-' means backward by one frame."),
4240 &tfindlist, "tfind ", 1, &cmdlist);
4242 add_cmd ("outside", class_trace, tfind_outside_command, _("\
4243 Select a trace frame whose PC is outside the given range (exclusive).\n\
4244 Usage: tfind outside addr1, addr2"),
4247 add_cmd ("range", class_trace, tfind_range_command, _("\
4248 Select a trace frame whose PC is in the given range (inclusive).\n\
4249 Usage: tfind range addr1,addr2"),
4252 add_cmd ("line", class_trace, tfind_line_command, _("\
4253 Select a trace frame by source line.\n\
4254 Argument can be a line number (with optional source file),\n\
4255 a function name, or '*' followed by an address.\n\
4256 Default argument is 'the next source line that was traced'."),
4259 add_cmd ("tracepoint", class_trace, tfind_tracepoint_command, _("\
4260 Select a trace frame by tracepoint number.\n\
4261 Default is the tracepoint for the current trace frame."),
4264 add_cmd ("pc", class_trace, tfind_pc_command, _("\
4265 Select a trace frame by PC.\n\
4266 Default is the current PC, or the PC of the current trace frame."),
4269 add_cmd ("end", class_trace, tfind_end_command, _("\
4270 De-select any trace frame and resume 'live' debugging."),
4273 add_alias_cmd ("none", "end", class_trace, 0, &tfindlist);
4275 add_cmd ("start", class_trace, tfind_start_command,
4276 _("Select the first trace frame in the trace buffer."),
4279 add_com ("tstatus", class_trace, tstatus_command,
4280 _("Display the status of the current trace data collection."));
4282 add_com ("tstop", class_trace, tstop_command, _("\
4283 Stop trace data collection.\n\
4284 Usage: tstop [ <notes> ... ]\n\
4285 Any arguments supplied are recorded with the trace as a stop reason and\n\
4286 reported by tstatus (if the target supports trace notes)."));
4288 add_com ("tstart", class_trace, tstart_command, _("\
4289 Start trace data collection.\n\
4290 Usage: tstart [ <notes> ... ]\n\
4291 Any arguments supplied are recorded with the trace as a note and\n\
4292 reported by tstatus (if the target supports trace notes)."));
4294 add_com ("end", class_trace, end_actions_pseudocommand, _("\
4295 Ends a list of commands or actions.\n\
4296 Several GDB commands allow you to enter a list of commands or actions.\n\
4297 Entering \"end\" on a line by itself is the normal way to terminate\n\
4299 Note: the \"end\" command cannot be used at the gdb prompt."));
4301 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
4302 Specify single-stepping behavior at a tracepoint.\n\
4303 Argument is number of instructions to trace in single-step mode\n\
4304 following the tracepoint. This command is normally followed by\n\
4305 one or more \"collect\" commands, to specify what to collect\n\
4306 while single-stepping.\n\n\
4307 Note: this command can only be used in a tracepoint \"actions\" list."));
4309 add_com_alias ("ws", "while-stepping", class_alias, 0);
4310 add_com_alias ("stepping", "while-stepping", class_alias, 0);
4312 add_com ("collect", class_trace, collect_pseudocommand, _("\
4313 Specify one or more data items to be collected at a tracepoint.\n\
4314 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
4315 collect all data (variables, registers) referenced by that expression.\n\
4316 Also accepts the following special arguments:\n\
4317 $regs -- all registers.\n\
4318 $args -- all function arguments.\n\
4319 $locals -- all variables local to the block/function scope.\n\
4320 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
4321 Note: this command can only be used in a tracepoint \"actions\" list."));
4323 add_com ("teval", class_trace, teval_pseudocommand, _("\
4324 Specify one or more expressions to be evaluated at a tracepoint.\n\
4325 Accepts a comma-separated list of (one or more) expressions.\n\
4326 The result of each evaluation will be discarded.\n\
4327 Note: this command can only be used in a tracepoint \"actions\" list."));
4329 add_com ("actions", class_trace, actions_command, _("\
4330 Specify the actions to be taken at a tracepoint.\n\
4331 Tracepoint actions may include collecting of specified data,\n\
4332 single-stepping, or enabling/disabling other tracepoints,\n\
4333 depending on target's capabilities."));
4335 default_collect = xstrdup ("");
4336 add_setshow_string_cmd ("default-collect", class_trace,
4337 &default_collect, _("\
4338 Set the list of expressions to collect by default"), _("\
4339 Show the list of expressions to collect by default"), NULL,
4341 &setlist, &showlist);
4343 add_setshow_boolean_cmd ("disconnected-tracing", no_class,
4344 &disconnected_tracing, _("\
4345 Set whether tracing continues after GDB disconnects."), _("\
4346 Show whether tracing continues after GDB disconnects."), _("\
4347 Use this to continue a tracing run even if GDB disconnects\n\
4348 or detaches from the target. You can reconnect later and look at\n\
4349 trace data collected in the meantime."),
4350 set_disconnected_tracing,
4355 add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
4356 &circular_trace_buffer, _("\
4357 Set target's use of circular trace buffer."), _("\
4358 Show target's use of circular trace buffer."), _("\
4359 Use this to make the trace buffer into a circular buffer,\n\
4360 which will discard traceframes (oldest first) instead of filling\n\
4361 up and stopping the trace run."),
4362 set_circular_trace_buffer,
4367 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class,
4368 &trace_buffer_size, _("\
4369 Set requested size of trace buffer."), _("\
4370 Show requested size of trace buffer."), _("\
4371 Use this to choose a size for the trace buffer. Some targets\n\
4372 may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
4373 disables any attempt to set the buffer size and lets the target choose."),
4374 set_trace_buffer_size, NULL,
4375 &setlist, &showlist);
4377 add_setshow_string_cmd ("trace-user", class_trace,
4379 Set the user name to use for current and future trace runs"), _("\
4380 Show the user name to use for current and future trace runs"), NULL,
4381 set_trace_user, NULL,
4382 &setlist, &showlist);
4384 add_setshow_string_cmd ("trace-notes", class_trace,
4386 Set notes string to use for current and future trace runs"), _("\
4387 Show the notes string to use for current and future trace runs"), NULL,
4388 set_trace_notes, NULL,
4389 &setlist, &showlist);
4391 add_setshow_string_cmd ("trace-stop-notes", class_trace,
4392 &trace_stop_notes, _("\
4393 Set notes string to use for future tstop commands"), _("\
4394 Show the notes string to use for future tstop commands"), NULL,
4395 set_trace_stop_notes, NULL,
4396 &setlist, &showlist);