1 /* Tracing functionality for remote targets in custom GDB protocol
3 Copyright (C) 1997-2018 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"
39 #include "observable.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 static std::vector<trace_state_variable> tvariables;
119 /* The next integer to assign to a variable. */
121 static int next_tsv_number = 1;
123 /* Number of last traceframe collected. */
124 static int traceframe_number;
126 /* Tracepoint for last traceframe collected. */
127 static int tracepoint_number;
129 /* The traceframe info of the current traceframe. NULL if we haven't
130 yet attempted to fetch it, or if the target does not support
131 fetching this object, or if we're not inspecting a traceframe
133 static traceframe_info_up current_traceframe_info;
135 /* Tracing command lists. */
136 static struct cmd_list_element *tfindlist;
138 /* List of expressions to collect by default at each tracepoint hit. */
139 char *default_collect;
141 static int disconnected_tracing;
143 /* This variable controls whether we ask the target for a linear or
144 circular trace buffer. */
146 static int circular_trace_buffer;
148 /* This variable is the requested trace buffer size, or -1 to indicate
149 that we don't care and leave it up to the target to set a size. */
151 static int trace_buffer_size = -1;
153 /* Textual notes applying to the current and/or future trace runs. */
155 char *trace_user = NULL;
157 /* Textual notes applying to the current and/or future trace runs. */
159 char *trace_notes = NULL;
161 /* Textual notes applying to the stopping of a trace. */
163 char *trace_stop_notes = NULL;
165 /* support routines */
167 struct collection_list;
168 static char *mem2hex (gdb_byte *, char *, int);
170 static counted_command_line all_tracepoint_actions (struct breakpoint *);
172 static struct trace_status trace_status;
174 const char *stop_reason_names[] = {
184 struct trace_status *
185 current_trace_status (void)
187 return &trace_status;
190 /* Free and clear the traceframe info cache of the current
194 clear_traceframe_info (void)
196 current_traceframe_info = NULL;
199 /* Set traceframe number to NUM. */
201 set_traceframe_num (int num)
203 traceframe_number = num;
204 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
207 /* Set tracepoint number to NUM. */
209 set_tracepoint_num (int num)
211 tracepoint_number = num;
212 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
215 /* Set externally visible debug variables for querying/printing
216 the traceframe context (line, function, file). */
219 set_traceframe_context (struct frame_info *trace_frame)
222 struct symbol *traceframe_fun;
223 symtab_and_line traceframe_sal;
225 /* Save as globals for internal use. */
226 if (trace_frame != NULL
227 && get_frame_pc_if_available (trace_frame, &trace_pc))
229 traceframe_sal = find_pc_line (trace_pc, 0);
230 traceframe_fun = find_pc_function (trace_pc);
232 /* Save linenumber as "$trace_line", a debugger variable visible to
234 set_internalvar_integer (lookup_internalvar ("trace_line"),
235 traceframe_sal.line);
239 traceframe_fun = NULL;
240 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
243 /* Save func name as "$trace_func", a debugger variable visible to
245 if (traceframe_fun == NULL
246 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
247 clear_internalvar (lookup_internalvar ("trace_func"));
249 set_internalvar_string (lookup_internalvar ("trace_func"),
250 SYMBOL_LINKAGE_NAME (traceframe_fun));
252 /* Save file name as "$trace_file", a debugger variable visible to
254 if (traceframe_sal.symtab == NULL)
255 clear_internalvar (lookup_internalvar ("trace_file"));
257 set_internalvar_string (lookup_internalvar ("trace_file"),
258 symtab_to_filename_for_display (traceframe_sal.symtab));
261 /* Create a new trace state variable with the given name. */
263 struct trace_state_variable *
264 create_trace_state_variable (const char *name)
266 tvariables.emplace_back (name, next_tsv_number++);
267 return &tvariables.back ();
270 /* Look for a trace state variable of the given name. */
272 struct trace_state_variable *
273 find_trace_state_variable (const char *name)
275 for (trace_state_variable &tsv : tvariables)
276 if (tsv.name == name)
282 /* Look for a trace state variable of the given number. Return NULL if
285 struct trace_state_variable *
286 find_trace_state_variable_by_number (int number)
288 for (trace_state_variable &tsv : tvariables)
289 if (tsv.number == number)
296 delete_trace_state_variable (const char *name)
298 for (auto it = tvariables.begin (); it != tvariables.end (); it++)
299 if (it->name == name)
301 gdb::observers::tsv_deleted.notify (&*it);
302 tvariables.erase (it);
306 warning (_("No trace variable named \"$%s\", not deleting"), name);
309 /* Throws an error if NAME is not valid syntax for a trace state
313 validate_trace_state_variable_name (const char *name)
318 error (_("Must supply a non-empty variable name"));
320 /* All digits in the name is reserved for value history
322 for (p = name; isdigit (*p); p++)
325 error (_("$%s is not a valid trace state variable name"), name);
327 for (p = name; isalnum (*p) || *p == '_'; p++)
330 error (_("$%s is not a valid trace state variable name"), name);
333 /* The 'tvariable' command collects a name and optional expression to
334 evaluate into an initial value. */
337 trace_variable_command (const char *args, int from_tty)
340 struct trace_state_variable *tsv;
341 const char *name_start, *p;
344 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
346 /* Only allow two syntaxes; "$name" and "$name=value". */
347 p = skip_spaces (args);
350 error (_("Name of trace variable should start with '$'"));
353 while (isalnum (*p) || *p == '_')
355 std::string name (name_start, p - name_start);
358 if (*p != '=' && *p != '\0')
359 error (_("Syntax must be $NAME [ = EXPR ]"));
361 validate_trace_state_variable_name (name.c_str ());
364 initval = value_as_long (parse_and_eval (++p));
366 /* If the variable already exists, just change its initial value. */
367 tsv = find_trace_state_variable (name.c_str ());
370 if (tsv->initial_value != initval)
372 tsv->initial_value = initval;
373 gdb::observers::tsv_modified.notify (tsv);
375 printf_filtered (_("Trace state variable $%s "
376 "now has initial value %s.\n"),
377 tsv->name.c_str (), plongest (tsv->initial_value));
381 /* Create a new variable. */
382 tsv = create_trace_state_variable (name.c_str ());
383 tsv->initial_value = initval;
385 gdb::observers::tsv_created.notify (tsv);
387 printf_filtered (_("Trace state variable $%s "
388 "created, with initial value %s.\n"),
389 tsv->name.c_str (), plongest (tsv->initial_value));
393 delete_trace_variable_command (const char *args, int from_tty)
397 if (query (_("Delete all trace state variables? ")))
400 gdb::observers::tsv_deleted.notify (NULL);
404 gdb_argv argv (args);
406 for (char *arg : argv)
409 delete_trace_state_variable (arg + 1);
411 warning (_("Name \"%s\" not prefixed with '$', ignoring"), arg);
418 tvariables_info_1 (void)
420 struct ui_out *uiout = current_uiout;
422 /* Try to acquire values from the target. */
423 for (trace_state_variable &tsv : tvariables)
425 = target_get_trace_state_variable_value (tsv.number, &tsv.value);
428 ui_out_emit_table table_emitter (uiout, 3, tvariables.size (),
430 uiout->table_header (15, ui_left, "name", "Name");
431 uiout->table_header (11, ui_left, "initial", "Initial");
432 uiout->table_header (11, ui_left, "current", "Current");
434 uiout->table_body ();
436 for (const trace_state_variable &tsv : tvariables)
440 ui_out_emit_tuple tuple_emitter (uiout, "variable");
442 uiout->field_string ("name", std::string ("$") + tsv.name);
443 uiout->field_string ("initial", plongest (tsv.initial_value));
446 c = plongest (tsv.value);
447 else if (uiout->is_mi_like_p ())
448 /* For MI, we prefer not to use magic string constants, but rather
449 omit the field completely. The difference between unknown and
450 undefined does not seem important enough to represent. */
452 else if (current_trace_status ()->running || traceframe_number >= 0)
453 /* The value is/was defined, but we don't have it. */
456 /* It is not meaningful to ask about the value. */
459 uiout->field_string ("current", c);
464 if (tvariables.empty ())
465 uiout->text (_("No trace state variables.\n"));
468 /* List all the trace state variables. */
471 info_tvariables_command (const char *args, int from_tty)
473 tvariables_info_1 ();
476 /* Stash definitions of tsvs into the given file. */
479 save_trace_state_variables (struct ui_file *fp)
481 for (const trace_state_variable &tsv : tvariables)
483 fprintf_unfiltered (fp, "tvariable $%s", tsv.name.c_str ());
484 if (tsv.initial_value)
485 fprintf_unfiltered (fp, " = %s", plongest (tsv.initial_value));
486 fprintf_unfiltered (fp, "\n");
490 /* ACTIONS functions: */
492 /* The three functions:
493 collect_pseudocommand,
494 while_stepping_pseudocommand, and
495 end_actions_pseudocommand
496 are placeholders for "commands" that are actually ONLY to be used
497 within a tracepoint action list. If the actual function is ever called,
498 it means that somebody issued the "command" at the top level,
499 which is always an error. */
502 end_actions_pseudocommand (const char *args, int from_tty)
504 error (_("This command cannot be used at the top level."));
508 while_stepping_pseudocommand (const char *args, int from_tty)
510 error (_("This command can only be used in a tracepoint actions list."));
514 collect_pseudocommand (const char *args, int from_tty)
516 error (_("This command can only be used in a tracepoint actions list."));
520 teval_pseudocommand (const char *args, int from_tty)
522 error (_("This command can only be used in a tracepoint actions list."));
525 /* Parse any collection options, such as /s for strings. */
528 decode_agent_options (const char *exp, int *trace_string)
530 struct value_print_options opts;
537 /* Call this to borrow the print elements default for collection
539 get_user_print_options (&opts);
544 if (target_supports_string_tracing ())
546 /* Allow an optional decimal number giving an explicit maximum
547 string length, defaulting it to the "print elements" value;
548 so "collect/s80 mystr" gets at most 80 bytes of string. */
549 *trace_string = opts.print_max;
551 if (*exp >= '0' && *exp <= '9')
552 *trace_string = atoi (exp);
553 while (*exp >= '0' && *exp <= '9')
557 error (_("Target does not support \"/s\" option for string tracing."));
560 error (_("Undefined collection format \"%c\"."), *exp);
562 exp = skip_spaces (exp);
567 /* Enter a list of actions for a tracepoint. */
569 actions_command (const char *args, int from_tty)
571 struct tracepoint *t;
573 t = get_tracepoint_by_number (&args, NULL);
577 string_printf ("Enter actions for tracepoint %d, one per line.",
580 counted_command_line l = read_command_lines (&tmpbuf[0], from_tty, 1,
581 check_tracepoint_command,
583 breakpoint_set_commands (t, std::move (l));
585 /* else just return */
588 /* Report the results of checking the agent expression, as errors or
592 report_agent_reqs_errors (struct agent_expr *aexpr)
594 /* All of the "flaws" are serious bytecode generation issues that
595 should never occur. */
596 if (aexpr->flaw != agent_flaw_none)
597 internal_error (__FILE__, __LINE__, _("expression is malformed"));
599 /* If analysis shows a stack underflow, GDB must have done something
600 badly wrong in its bytecode generation. */
601 if (aexpr->min_height < 0)
602 internal_error (__FILE__, __LINE__,
603 _("expression has min height < 0"));
605 /* Issue this error if the stack is predicted to get too deep. The
606 limit is rather arbitrary; a better scheme might be for the
607 target to report how much stack it will have available. The
608 depth roughly corresponds to parenthesization, so a limit of 20
609 amounts to 20 levels of expression nesting, which is actually
610 a pretty big hairy expression. */
611 if (aexpr->max_height > 20)
612 error (_("Expression is too complicated."));
615 /* worker function */
617 validate_actionline (const char *line, struct breakpoint *b)
619 struct cmd_list_element *c;
622 struct bp_location *loc;
623 struct tracepoint *t = (struct tracepoint *) b;
625 /* If EOF is typed, *line is NULL. */
629 p = skip_spaces (line);
631 /* Symbol lookup etc. */
632 if (*p == '\0') /* empty line: just prompt for another line. */
635 if (*p == '#') /* comment line */
638 c = lookup_cmd (&p, cmdlist, "", -1, 1);
640 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
642 if (cmd_cfunc_eq (c, collect_pseudocommand))
644 int trace_string = 0;
647 p = decode_agent_options (p, &trace_string);
650 { /* Repeat over a comma-separated list. */
651 QUIT; /* Allow user to bail out with ^C. */
654 if (*p == '$') /* Look for special pseudo-symbols. */
656 if (0 == strncasecmp ("reg", p + 1, 3)
657 || 0 == strncasecmp ("arg", p + 1, 3)
658 || 0 == strncasecmp ("loc", p + 1, 3)
659 || 0 == strncasecmp ("_ret", p + 1, 4)
660 || 0 == strncasecmp ("_sdata", p + 1, 6))
665 /* else fall thru, treat p as an expression and parse it! */
668 for (loc = t->loc; loc; loc = loc->next)
671 expression_up exp = parse_exp_1 (&p, loc->address,
672 block_for_pc (loc->address), 1);
674 if (exp->elts[0].opcode == OP_VAR_VALUE)
676 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
678 error (_("constant `%s' (value %s) "
679 "will not be collected."),
680 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
681 plongest (SYMBOL_VALUE (exp->elts[2].symbol)));
683 else if (SYMBOL_CLASS (exp->elts[2].symbol)
684 == LOC_OPTIMIZED_OUT)
686 error (_("`%s' is optimized away "
687 "and cannot be collected."),
688 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
692 /* We have something to collect, make sure that the expr to
693 bytecode translator can handle it and that it's not too
695 agent_expr_up aexpr = gen_trace_for_expr (loc->address,
699 if (aexpr->len > MAX_AGENT_EXPR_LEN)
700 error (_("Expression is too complicated."));
702 ax_reqs (aexpr.get ());
704 report_agent_reqs_errors (aexpr.get ());
707 while (p && *p++ == ',');
710 else if (cmd_cfunc_eq (c, teval_pseudocommand))
713 { /* Repeat over a comma-separated list. */
714 QUIT; /* Allow user to bail out with ^C. */
718 for (loc = t->loc; loc; loc = loc->next)
722 /* Only expressions are allowed for this action. */
723 expression_up exp = parse_exp_1 (&p, loc->address,
724 block_for_pc (loc->address), 1);
726 /* We have something to evaluate, make sure that the expr to
727 bytecode translator can handle it and that it's not too
729 agent_expr_up aexpr = gen_eval_for_expr (loc->address, exp.get ());
731 if (aexpr->len > MAX_AGENT_EXPR_LEN)
732 error (_("Expression is too complicated."));
734 ax_reqs (aexpr.get ());
735 report_agent_reqs_errors (aexpr.get ());
738 while (p && *p++ == ',');
741 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
746 t->step_count = strtol (p, &endp, 0);
747 if (endp == p || t->step_count == 0)
748 error (_("while-stepping step count `%s' is malformed."), line);
752 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
756 error (_("`%s' is not a supported tracepoint action."), line);
760 memrange_absolute = -1
763 /* MEMRANGE functions: */
765 /* Compare memranges for std::sort. */
768 memrange_comp (const memrange &a, const memrange &b)
770 if (a.type == b.type)
772 if (a.type == memrange_absolute)
773 return (bfd_vma) a.start < (bfd_vma) b.start;
775 return a.start < b.start;
778 return a.type < b.type;
781 /* Sort the memrange list using std::sort, and merge adjacent memranges. */
784 memrange_sortmerge (std::vector<memrange> &memranges)
786 if (!memranges.empty ())
790 std::sort (memranges.begin (), memranges.end (), memrange_comp);
792 for (a = 0, b = 1; b < memranges.size (); b++)
794 /* If memrange b overlaps or is adjacent to memrange a,
796 if (memranges[a].type == memranges[b].type
797 && memranges[b].start <= memranges[a].end)
799 if (memranges[b].end > memranges[a].end)
800 memranges[a].end = memranges[b].end;
801 continue; /* next b, same a */
805 memranges[a] = memranges[b];
807 memranges.resize (a + 1);
811 /* Add a register to a collection list. */
814 collection_list::add_register (unsigned int regno)
817 printf_filtered ("collect register %d\n", regno);
818 if (regno >= (8 * sizeof (m_regs_mask)))
819 error (_("Internal: register number %d too large for tracepoint"),
821 m_regs_mask[regno / 8] |= 1 << (regno % 8);
824 /* Add a memrange to a collection list. */
827 collection_list::add_memrange (struct gdbarch *gdbarch,
828 int type, bfd_signed_vma base,
832 printf_filtered ("(%d,%s,%ld)\n", type, paddress (gdbarch, base), len);
834 /* type: memrange_absolute == memory, other n == basereg */
835 /* base: addr if memory, offset if reg relative. */
836 /* len: we actually save end (base + len) for convenience */
837 m_memranges.emplace_back (type, base, base + len);
839 if (type != memrange_absolute) /* Better collect the base register! */
843 /* Add a symbol to a collection list. */
846 collection_list::collect_symbol (struct symbol *sym,
847 struct gdbarch *gdbarch,
848 long frame_regno, long frame_offset,
854 bfd_signed_vma offset;
855 int treat_as_expr = 0;
857 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
858 switch (SYMBOL_CLASS (sym))
861 printf_filtered ("%s: don't know symbol class %d\n",
862 SYMBOL_PRINT_NAME (sym),
866 printf_filtered ("constant %s (value %s) will not be collected.\n",
867 SYMBOL_PRINT_NAME (sym), plongest (SYMBOL_VALUE (sym)));
870 offset = SYMBOL_VALUE_ADDRESS (sym);
873 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
874 SYMBOL_PRINT_NAME (sym), len,
875 paddress (gdbarch, offset));
877 /* A struct may be a C++ class with static fields, go to general
878 expression handling. */
879 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT)
882 add_memrange (gdbarch, memrange_absolute, offset, len);
885 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
887 printf_filtered ("LOC_REG[parm] %s: ",
888 SYMBOL_PRINT_NAME (sym));
890 /* Check for doubles stored in two registers. */
891 /* FIXME: how about larger types stored in 3 or more regs? */
892 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
893 len > register_size (gdbarch, reg))
894 add_register (reg + 1);
897 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
898 printf_filtered (" (will not collect %s)\n",
899 SYMBOL_PRINT_NAME (sym));
903 offset = frame_offset + SYMBOL_VALUE (sym);
906 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
907 " from frame ptr reg %d\n",
908 SYMBOL_PRINT_NAME (sym), len,
909 paddress (gdbarch, offset), reg);
911 add_memrange (gdbarch, reg, offset, len);
913 case LOC_REGPARM_ADDR:
914 reg = SYMBOL_VALUE (sym);
918 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s"
920 SYMBOL_PRINT_NAME (sym), len,
921 paddress (gdbarch, offset), reg);
923 add_memrange (gdbarch, reg, offset, len);
927 offset = frame_offset + SYMBOL_VALUE (sym);
930 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
931 " from frame ptr reg %d\n",
932 SYMBOL_PRINT_NAME (sym), len,
933 paddress (gdbarch, offset), reg);
935 add_memrange (gdbarch, reg, offset, len);
942 case LOC_OPTIMIZED_OUT:
943 printf_filtered ("%s has been optimized out of existence.\n",
944 SYMBOL_PRINT_NAME (sym));
952 /* Expressions are the most general case. */
955 agent_expr_up aexpr = gen_trace_for_var (scope, gdbarch,
958 /* It can happen that the symbol is recorded as a computed
959 location, but it's been optimized away and doesn't actually
960 have a location expression. */
963 printf_filtered ("%s has been optimized out of existence.\n",
964 SYMBOL_PRINT_NAME (sym));
968 ax_reqs (aexpr.get ());
970 report_agent_reqs_errors (aexpr.get ());
972 /* Take care of the registers. */
973 if (aexpr->reg_mask_len > 0)
975 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
977 QUIT; /* Allow user to bail out with ^C. */
978 if (aexpr->reg_mask[ndx1] != 0)
980 /* Assume chars have 8 bits. */
981 for (int ndx2 = 0; ndx2 < 8; ndx2++)
982 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
983 /* It's used -- record it. */
984 add_register (ndx1 * 8 + ndx2);
989 add_aexpr (std::move (aexpr));
993 /* Data to be passed around in the calls to the locals and args
996 struct add_local_symbols_data
998 struct collection_list *collect;
999 struct gdbarch *gdbarch;
1007 /* The callback for the locals and args iterators. */
1010 do_collect_symbol (const char *print_name,
1014 struct add_local_symbols_data *p = (struct add_local_symbols_data *) cb_data;
1016 p->collect->collect_symbol (sym, p->gdbarch, p->frame_regno,
1017 p->frame_offset, p->pc, p->trace_string);
1020 p->collect->add_wholly_collected (print_name);
1024 collection_list::add_wholly_collected (const char *print_name)
1026 m_wholly_collected.push_back (print_name);
1029 /* Add all locals (or args) symbols to collection list. */
1032 collection_list::add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc,
1033 long frame_regno, long frame_offset, int type,
1036 const struct block *block;
1037 struct add_local_symbols_data cb_data;
1039 cb_data.collect = this;
1040 cb_data.gdbarch = gdbarch;
1042 cb_data.frame_regno = frame_regno;
1043 cb_data.frame_offset = frame_offset;
1045 cb_data.trace_string = trace_string;
1049 block = block_for_pc (pc);
1052 warning (_("Can't collect locals; "
1053 "no symbol table info available.\n"));
1057 iterate_over_block_local_vars (block, do_collect_symbol, &cb_data);
1058 if (cb_data.count == 0)
1059 warning (_("No locals found in scope."));
1063 pc = get_pc_function_start (pc);
1064 block = block_for_pc (pc);
1067 warning (_("Can't collect args; no symbol table info available."));
1071 iterate_over_block_arg_vars (block, do_collect_symbol, &cb_data);
1072 if (cb_data.count == 0)
1073 warning (_("No args found in scope."));
1078 collection_list::add_static_trace_data ()
1081 printf_filtered ("collect static trace data\n");
1082 m_strace_data = true;
1085 collection_list::collection_list ()
1087 m_strace_data (false)
1089 m_memranges.reserve (128);
1090 m_aexprs.reserve (128);
1093 /* Reduce a collection list to string form (for gdb protocol). */
1095 std::vector<std::string>
1096 collection_list::stringify ()
1098 char temp_buf[2048];
1102 std::vector<std::string> str_list;
1107 printf_filtered ("\nCollecting static trace data\n");
1110 str_list.emplace_back (temp_buf, end - temp_buf);
1113 for (i = sizeof (m_regs_mask) - 1; i > 0; i--)
1114 if (m_regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
1116 if (m_regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
1119 printf_filtered ("\nCollecting registers (mask): 0x");
1124 QUIT; /* Allow user to bail out with ^C. */
1126 printf_filtered ("%02X", m_regs_mask[i]);
1127 sprintf (end, "%02X", m_regs_mask[i]);
1130 str_list.emplace_back (temp_buf);
1133 printf_filtered ("\n");
1134 if (!m_memranges.empty () && info_verbose)
1135 printf_filtered ("Collecting memranges: \n");
1136 for (i = 0, count = 0, end = temp_buf; i < m_memranges.size (); i++)
1138 QUIT; /* Allow user to bail out with ^C. */
1141 printf_filtered ("(%d, %s, %ld)\n",
1142 m_memranges[i].type,
1143 paddress (target_gdbarch (),
1144 m_memranges[i].start),
1145 (long) (m_memranges[i].end
1146 - m_memranges[i].start));
1148 if (count + 27 > MAX_AGENT_EXPR_LEN)
1150 str_list.emplace_back (temp_buf, count);
1156 bfd_signed_vma length
1157 = m_memranges[i].end - m_memranges[i].start;
1159 /* The "%X" conversion specifier expects an unsigned argument,
1160 so passing -1 (memrange_absolute) to it directly gives you
1161 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1163 if (m_memranges[i].type == memrange_absolute)
1164 sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
1167 sprintf (end, "M%X,%s,%lX", m_memranges[i].type,
1168 phex_nz (m_memranges[i].start, 0), (long) length);
1171 count += strlen (end);
1172 end = temp_buf + count;
1175 for (i = 0; i < m_aexprs.size (); i++)
1177 QUIT; /* Allow user to bail out with ^C. */
1178 if ((count + 10 + 2 * m_aexprs[i]->len) > MAX_AGENT_EXPR_LEN)
1180 str_list.emplace_back (temp_buf, count);
1184 sprintf (end, "X%08X,", m_aexprs[i]->len);
1185 end += 10; /* 'X' + 8 hex digits + ',' */
1188 end = mem2hex (m_aexprs[i]->buf, end, m_aexprs[i]->len);
1189 count += 2 * m_aexprs[i]->len;
1194 str_list.emplace_back (temp_buf, count);
1202 /* Add the printed expression EXP to *LIST. */
1205 collection_list::append_exp (struct expression *exp)
1207 string_file tmp_stream;
1209 print_expression (exp, &tmp_stream);
1211 m_computed.push_back (std::move (tmp_stream.string ()));
1215 collection_list::finish ()
1217 memrange_sortmerge (m_memranges);
1221 encode_actions_1 (struct command_line *action,
1222 struct bp_location *tloc,
1224 LONGEST frame_offset,
1225 struct collection_list *collect,
1226 struct collection_list *stepping_list)
1228 const char *action_exp;
1230 struct value *tempval;
1231 struct cmd_list_element *cmd;
1233 for (; action; action = action->next)
1235 QUIT; /* Allow user to bail out with ^C. */
1236 action_exp = action->line;
1237 action_exp = skip_spaces (action_exp);
1239 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1241 error (_("Bad action list item: %s"), action_exp);
1243 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
1245 int trace_string = 0;
1247 if (*action_exp == '/')
1248 action_exp = decode_agent_options (action_exp, &trace_string);
1251 { /* Repeat over a comma-separated list. */
1252 QUIT; /* Allow user to bail out with ^C. */
1253 action_exp = skip_spaces (action_exp);
1255 if (0 == strncasecmp ("$reg", action_exp, 4))
1257 for (i = 0; i < gdbarch_num_regs (target_gdbarch ()); i++)
1258 collect->add_register (i);
1259 action_exp = strchr (action_exp, ','); /* more? */
1261 else if (0 == strncasecmp ("$arg", action_exp, 4))
1263 collect->add_local_symbols (target_gdbarch (),
1269 action_exp = strchr (action_exp, ','); /* more? */
1271 else if (0 == strncasecmp ("$loc", action_exp, 4))
1273 collect->add_local_symbols (target_gdbarch (),
1279 action_exp = strchr (action_exp, ','); /* more? */
1281 else if (0 == strncasecmp ("$_ret", action_exp, 5))
1284 = gen_trace_for_return_address (tloc->address,
1288 ax_reqs (aexpr.get ());
1289 report_agent_reqs_errors (aexpr.get ());
1291 /* take care of the registers */
1292 if (aexpr->reg_mask_len > 0)
1294 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1296 QUIT; /* allow user to bail out with ^C */
1297 if (aexpr->reg_mask[ndx1] != 0)
1299 /* assume chars have 8 bits */
1300 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1301 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1303 /* It's used -- record it. */
1304 collect->add_register (ndx1 * 8 + ndx2);
1310 collect->add_aexpr (std::move (aexpr));
1311 action_exp = strchr (action_exp, ','); /* more? */
1313 else if (0 == strncasecmp ("$_sdata", action_exp, 7))
1315 collect->add_static_trace_data ();
1316 action_exp = strchr (action_exp, ','); /* more? */
1322 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1323 block_for_pc (tloc->address),
1326 switch (exp->elts[0].opcode)
1330 const char *name = &exp->elts[2].string;
1332 i = user_reg_map_name_to_regnum (target_gdbarch (),
1333 name, strlen (name));
1335 internal_error (__FILE__, __LINE__,
1336 _("Register $%s not available"),
1339 printf_filtered ("OP_REGISTER: ");
1340 collect->add_register (i);
1345 /* Safe because we know it's a simple expression. */
1346 tempval = evaluate_expression (exp.get ());
1347 addr = value_address (tempval);
1348 /* Initialize the TYPE_LENGTH if it is a typedef. */
1349 check_typedef (exp->elts[1].type);
1350 collect->add_memrange (target_gdbarch (),
1351 memrange_absolute, addr,
1352 TYPE_LENGTH (exp->elts[1].type));
1353 collect->append_exp (exp.get ());
1358 struct symbol *sym = exp->elts[2].symbol;
1359 const char *name = SYMBOL_NATURAL_NAME (sym);
1361 collect->collect_symbol (exp->elts[2].symbol,
1367 collect->add_wholly_collected (name);
1371 default: /* Full-fledged expression. */
1372 agent_expr_up aexpr = gen_trace_for_expr (tloc->address,
1376 ax_reqs (aexpr.get ());
1378 report_agent_reqs_errors (aexpr.get ());
1380 /* Take care of the registers. */
1381 if (aexpr->reg_mask_len > 0)
1383 for (int ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1385 QUIT; /* Allow user to bail out with ^C. */
1386 if (aexpr->reg_mask[ndx1] != 0)
1388 /* Assume chars have 8 bits. */
1389 for (int ndx2 = 0; ndx2 < 8; ndx2++)
1390 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1392 /* It's used -- record it. */
1393 collect->add_register (ndx1 * 8 + ndx2);
1399 collect->add_aexpr (std::move (aexpr));
1400 collect->append_exp (exp.get ());
1405 while (action_exp && *action_exp++ == ',');
1407 else if (cmd_cfunc_eq (cmd, teval_pseudocommand))
1410 { /* Repeat over a comma-separated list. */
1411 QUIT; /* Allow user to bail out with ^C. */
1412 action_exp = skip_spaces (action_exp);
1415 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1416 block_for_pc (tloc->address),
1419 agent_expr_up aexpr = gen_eval_for_expr (tloc->address,
1422 ax_reqs (aexpr.get ());
1423 report_agent_reqs_errors (aexpr.get ());
1425 /* Even though we're not officially collecting, add
1426 to the collect list anyway. */
1427 collect->add_aexpr (std::move (aexpr));
1430 while (action_exp && *action_exp++ == ',');
1432 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
1434 /* We check against nested while-stepping when setting
1435 breakpoint action, so no way to run into nested
1437 gdb_assert (stepping_list);
1439 encode_actions_1 (action->body_list_0.get (), tloc, frame_reg,
1440 frame_offset, stepping_list, NULL);
1443 error (_("Invalid tracepoint command '%s'"), action->line);
1447 /* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
1448 and STEPPING_LIST. */
1451 encode_actions (struct bp_location *tloc,
1452 struct collection_list *tracepoint_list,
1453 struct collection_list *stepping_list)
1456 LONGEST frame_offset;
1458 gdbarch_virtual_frame_pointer (tloc->gdbarch,
1459 tloc->address, &frame_reg, &frame_offset);
1461 counted_command_line actions = all_tracepoint_actions (tloc->owner);
1462 encode_actions_1 (actions.get (), tloc, frame_reg, frame_offset,
1463 tracepoint_list, stepping_list);
1464 encode_actions_1 (breakpoint_commands (tloc->owner), tloc,
1465 frame_reg, frame_offset, tracepoint_list, stepping_list);
1467 tracepoint_list->finish ();
1468 stepping_list->finish ();
1471 /* Render all actions into gdb protocol. */
1474 encode_actions_rsp (struct bp_location *tloc,
1475 std::vector<std::string> *tdp_actions,
1476 std::vector<std::string> *stepping_actions)
1478 struct collection_list tracepoint_list, stepping_list;
1480 encode_actions (tloc, &tracepoint_list, &stepping_list);
1482 *tdp_actions = tracepoint_list.stringify ();
1483 *stepping_actions = stepping_list.stringify ();
1487 collection_list::add_aexpr (agent_expr_up aexpr)
1489 m_aexprs.push_back (std::move (aexpr));
1493 process_tracepoint_on_disconnect (void)
1495 VEC(breakpoint_p) *tp_vec = NULL;
1497 struct breakpoint *b;
1498 int has_pending_p = 0;
1500 /* Check whether we still have pending tracepoint. If we have, warn the
1501 user that pending tracepoint will no longer work. */
1502 tp_vec = all_tracepoints ();
1503 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1512 struct bp_location *loc1;
1514 for (loc1 = b->loc; loc1; loc1 = loc1->next)
1516 if (loc1->shlib_disabled)
1527 VEC_free (breakpoint_p, tp_vec);
1530 warning (_("Pending tracepoints will not be resolved while"
1531 " GDB is disconnected\n"));
1534 /* Reset local state of tracing. */
1537 trace_reset_local_state (void)
1539 set_traceframe_num (-1);
1540 set_tracepoint_num (-1);
1541 set_traceframe_context (NULL);
1542 clear_traceframe_info ();
1546 start_tracing (const char *notes)
1548 VEC(breakpoint_p) *tp_vec = NULL;
1550 struct breakpoint *b;
1551 struct trace_state_variable *tsv;
1552 int any_enabled = 0, num_to_download = 0;
1555 tp_vec = all_tracepoints ();
1557 /* No point in tracing without any tracepoints... */
1558 if (VEC_length (breakpoint_p, tp_vec) == 0)
1560 VEC_free (breakpoint_p, tp_vec);
1561 error (_("No tracepoints defined, not starting trace"));
1564 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1566 if (b->enable_state == bp_enabled)
1569 if ((b->type == bp_fast_tracepoint
1570 ? may_insert_fast_tracepoints
1571 : may_insert_tracepoints))
1574 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
1575 (b->type == bp_fast_tracepoint ? "fast " : ""), b->number);
1580 if (target_supports_enable_disable_tracepoint ())
1581 warning (_("No tracepoints enabled"));
1584 /* No point in tracing with only disabled tracepoints that
1585 cannot be re-enabled. */
1586 VEC_free (breakpoint_p, tp_vec);
1587 error (_("No tracepoints enabled, not starting trace"));
1591 if (num_to_download <= 0)
1593 VEC_free (breakpoint_p, tp_vec);
1594 error (_("No tracepoints that may be downloaded, not starting trace"));
1597 target_trace_init ();
1599 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1601 struct tracepoint *t = (struct tracepoint *) b;
1602 struct bp_location *loc;
1603 int bp_location_downloaded = 0;
1605 /* Clear `inserted' flag. */
1606 for (loc = b->loc; loc; loc = loc->next)
1609 if ((b->type == bp_fast_tracepoint
1610 ? !may_insert_fast_tracepoints
1611 : !may_insert_tracepoints))
1614 t->number_on_target = 0;
1616 for (loc = b->loc; loc; loc = loc->next)
1618 /* Since tracepoint locations are never duplicated, `inserted'
1619 flag should be zero. */
1620 gdb_assert (!loc->inserted);
1622 target_download_tracepoint (loc);
1625 bp_location_downloaded = 1;
1628 t->number_on_target = b->number;
1630 for (loc = b->loc; loc; loc = loc->next)
1631 if (loc->probe.prob != NULL)
1632 loc->probe.prob->set_semaphore (loc->probe.objfile,
1635 if (bp_location_downloaded)
1636 gdb::observers::breakpoint_modified.notify (b);
1638 VEC_free (breakpoint_p, tp_vec);
1640 /* Send down all the trace state variables too. */
1641 for (const trace_state_variable &tsv : tvariables)
1642 target_download_trace_state_variable (tsv);
1644 /* Tell target to treat text-like sections as transparent. */
1645 target_trace_set_readonly_regions ();
1646 /* Set some mode flags. */
1647 target_set_disconnected_tracing (disconnected_tracing);
1648 target_set_circular_trace_buffer (circular_trace_buffer);
1649 target_set_trace_buffer_size (trace_buffer_size);
1652 notes = trace_notes;
1653 ret = target_set_trace_notes (trace_user, notes, NULL);
1655 if (!ret && (trace_user || notes))
1656 warning (_("Target does not support trace user/notes, info ignored"));
1658 /* Now insert traps and begin collecting data. */
1659 target_trace_start ();
1661 /* Reset our local state. */
1662 trace_reset_local_state ();
1663 current_trace_status()->running = 1;
1666 /* The tstart command requests the target to start a new trace run.
1667 The command passes any arguments it has to the target verbatim, as
1668 an optional "trace note". This is useful as for instance a warning
1669 to other users if the trace runs disconnected, and you don't want
1670 anybody else messing with the target. */
1673 tstart_command (const char *args, int from_tty)
1675 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1677 if (current_trace_status ()->running)
1680 && !query (_("A trace is running already. Start a new run? ")))
1681 error (_("New trace run not started."));
1684 start_tracing (args);
1687 /* The tstop command stops the tracing run. The command passes any
1688 supplied arguments to the target verbatim as a "stop note"; if the
1689 target supports trace notes, then it will be reported back as part
1690 of the trace run's status. */
1693 tstop_command (const char *args, int from_tty)
1695 if (!current_trace_status ()->running)
1696 error (_("Trace is not running."));
1698 stop_tracing (args);
1702 stop_tracing (const char *note)
1705 VEC(breakpoint_p) *tp_vec = NULL;
1707 struct breakpoint *t;
1709 target_trace_stop ();
1711 tp_vec = all_tracepoints ();
1712 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1714 struct bp_location *loc;
1716 if ((t->type == bp_fast_tracepoint
1717 ? !may_insert_fast_tracepoints
1718 : !may_insert_tracepoints))
1721 for (loc = t->loc; loc; loc = loc->next)
1723 /* GDB can be totally absent in some disconnected trace scenarios,
1724 but we don't really care if this semaphore goes out of sync.
1725 That's why we are decrementing it here, but not taking care
1727 if (loc->probe.prob != NULL)
1728 loc->probe.prob->clear_semaphore (loc->probe.objfile,
1733 VEC_free (breakpoint_p, tp_vec);
1736 note = trace_stop_notes;
1737 ret = target_set_trace_notes (NULL, NULL, note);
1740 warning (_("Target does not support trace notes, note ignored"));
1742 /* Should change in response to reply? */
1743 current_trace_status ()->running = 0;
1746 /* tstatus command */
1748 tstatus_command (const char *args, int from_tty)
1750 struct trace_status *ts = current_trace_status ();
1752 VEC(breakpoint_p) *tp_vec = NULL;
1753 struct breakpoint *t;
1755 status = target_get_trace_status (ts);
1759 if (ts->filename != NULL)
1760 printf_filtered (_("Using a trace file.\n"));
1763 printf_filtered (_("Trace can not be run on this target.\n"));
1768 if (!ts->running_known)
1770 printf_filtered (_("Run/stop status is unknown.\n"));
1772 else if (ts->running)
1774 printf_filtered (_("Trace is running on the target.\n"));
1778 switch (ts->stop_reason)
1780 case trace_never_run:
1781 printf_filtered (_("No trace has been run on the target.\n"));
1783 case trace_stop_command:
1785 printf_filtered (_("Trace stopped by a tstop command (%s).\n"),
1788 printf_filtered (_("Trace stopped by a tstop command.\n"));
1790 case trace_buffer_full:
1791 printf_filtered (_("Trace stopped because the buffer was full.\n"));
1793 case trace_disconnected:
1794 printf_filtered (_("Trace stopped because of disconnection.\n"));
1796 case tracepoint_passcount:
1797 printf_filtered (_("Trace stopped by tracepoint %d.\n"),
1798 ts->stopping_tracepoint);
1800 case tracepoint_error:
1801 if (ts->stopping_tracepoint)
1802 printf_filtered (_("Trace stopped by an "
1803 "error (%s, tracepoint %d).\n"),
1804 ts->stop_desc, ts->stopping_tracepoint);
1806 printf_filtered (_("Trace stopped by an error (%s).\n"),
1809 case trace_stop_reason_unknown:
1810 printf_filtered (_("Trace stopped for an unknown reason.\n"));
1813 printf_filtered (_("Trace stopped for some other reason (%d).\n"),
1819 if (ts->traceframes_created >= 0
1820 && ts->traceframe_count != ts->traceframes_created)
1822 printf_filtered (_("Buffer contains %d trace "
1823 "frames (of %d created total).\n"),
1824 ts->traceframe_count, ts->traceframes_created);
1826 else if (ts->traceframe_count >= 0)
1828 printf_filtered (_("Collected %d trace frames.\n"),
1829 ts->traceframe_count);
1832 if (ts->buffer_free >= 0)
1834 if (ts->buffer_size >= 0)
1836 printf_filtered (_("Trace buffer has %d bytes of %d bytes free"),
1837 ts->buffer_free, ts->buffer_size);
1838 if (ts->buffer_size > 0)
1839 printf_filtered (_(" (%d%% full)"),
1840 ((int) ((((long long) (ts->buffer_size
1841 - ts->buffer_free)) * 100)
1842 / ts->buffer_size)));
1843 printf_filtered (_(".\n"));
1846 printf_filtered (_("Trace buffer has %d bytes free.\n"),
1850 if (ts->disconnected_tracing)
1851 printf_filtered (_("Trace will continue if GDB disconnects.\n"));
1853 printf_filtered (_("Trace will stop if GDB disconnects.\n"));
1855 if (ts->circular_buffer)
1856 printf_filtered (_("Trace buffer is circular.\n"));
1858 if (ts->user_name && strlen (ts->user_name) > 0)
1859 printf_filtered (_("Trace user is %s.\n"), ts->user_name);
1861 if (ts->notes && strlen (ts->notes) > 0)
1862 printf_filtered (_("Trace notes: %s.\n"), ts->notes);
1864 /* Now report on what we're doing with tfind. */
1865 if (traceframe_number >= 0)
1866 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
1867 traceframe_number, tracepoint_number);
1869 printf_filtered (_("Not looking at any trace frame.\n"));
1871 /* Report start/stop times if supplied. */
1876 LONGEST run_time = ts->stop_time - ts->start_time;
1878 /* Reporting a run time is more readable than two long numbers. */
1879 printf_filtered (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
1880 (long int) (ts->start_time / 1000000),
1881 (long int) (ts->start_time % 1000000),
1882 (long int) (run_time / 1000000),
1883 (long int) (run_time % 1000000));
1886 printf_filtered (_("Trace started at %ld.%06ld secs.\n"),
1887 (long int) (ts->start_time / 1000000),
1888 (long int) (ts->start_time % 1000000));
1890 else if (ts->stop_time)
1891 printf_filtered (_("Trace stopped at %ld.%06ld secs.\n"),
1892 (long int) (ts->stop_time / 1000000),
1893 (long int) (ts->stop_time % 1000000));
1895 /* Now report any per-tracepoint status available. */
1896 tp_vec = all_tracepoints ();
1898 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1899 target_get_tracepoint_status (t, NULL);
1901 VEC_free (breakpoint_p, tp_vec);
1904 /* Report the trace status to uiout, in a way suitable for MI, and not
1905 suitable for CLI. If ON_STOP is true, suppress a few fields that
1906 are not meaningful in the -trace-stop response.
1908 The implementation is essentially parallel to trace_status_command, but
1909 merging them will result in unreadable code. */
1911 trace_status_mi (int on_stop)
1913 struct ui_out *uiout = current_uiout;
1914 struct trace_status *ts = current_trace_status ();
1917 status = target_get_trace_status (ts);
1919 if (status == -1 && ts->filename == NULL)
1921 uiout->field_string ("supported", "0");
1925 if (ts->filename != NULL)
1926 uiout->field_string ("supported", "file");
1928 uiout->field_string ("supported", "1");
1930 if (ts->filename != NULL)
1931 uiout->field_string ("trace-file", ts->filename);
1933 gdb_assert (ts->running_known);
1937 uiout->field_string ("running", "1");
1939 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
1940 Given that the frontend gets the status either on -trace-stop, or from
1941 -trace-status after re-connection, it does not seem like this
1942 information is necessary for anything. It is not necessary for either
1943 figuring the vital state of the target nor for navigation of trace
1944 frames. If the frontend wants to show the current state is some
1945 configure dialog, it can request the value when such dialog is
1946 invoked by the user. */
1950 const char *stop_reason = NULL;
1951 int stopping_tracepoint = -1;
1954 uiout->field_string ("running", "0");
1956 if (ts->stop_reason != trace_stop_reason_unknown)
1958 switch (ts->stop_reason)
1960 case trace_stop_command:
1961 stop_reason = "request";
1963 case trace_buffer_full:
1964 stop_reason = "overflow";
1966 case trace_disconnected:
1967 stop_reason = "disconnection";
1969 case tracepoint_passcount:
1970 stop_reason = "passcount";
1971 stopping_tracepoint = ts->stopping_tracepoint;
1973 case tracepoint_error:
1974 stop_reason = "error";
1975 stopping_tracepoint = ts->stopping_tracepoint;
1981 uiout->field_string ("stop-reason", stop_reason);
1982 if (stopping_tracepoint != -1)
1983 uiout->field_int ("stopping-tracepoint",
1984 stopping_tracepoint);
1985 if (ts->stop_reason == tracepoint_error)
1986 uiout->field_string ("error-description",
1992 if (ts->traceframe_count != -1)
1993 uiout->field_int ("frames", ts->traceframe_count);
1994 if (ts->traceframes_created != -1)
1995 uiout->field_int ("frames-created", ts->traceframes_created);
1996 if (ts->buffer_size != -1)
1997 uiout->field_int ("buffer-size", ts->buffer_size);
1998 if (ts->buffer_free != -1)
1999 uiout->field_int ("buffer-free", ts->buffer_free);
2001 uiout->field_int ("disconnected", ts->disconnected_tracing);
2002 uiout->field_int ("circular", ts->circular_buffer);
2004 uiout->field_string ("user-name", ts->user_name);
2005 uiout->field_string ("notes", ts->notes);
2010 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2011 (long int) (ts->start_time / 1000000),
2012 (long int) (ts->start_time % 1000000));
2013 uiout->field_string ("start-time", buf);
2014 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2015 (long int) (ts->stop_time / 1000000),
2016 (long int) (ts->stop_time % 1000000));
2017 uiout->field_string ("stop-time", buf);
2021 /* Check if a trace run is ongoing. If so, and FROM_TTY, query the
2022 user if she really wants to detach. */
2025 query_if_trace_running (int from_tty)
2030 /* It can happen that the target that was tracing went away on its
2031 own, and we didn't notice. Get a status update, and if the
2032 current target doesn't even do tracing, then assume it's not
2034 if (target_get_trace_status (current_trace_status ()) < 0)
2035 current_trace_status ()->running = 0;
2037 /* If running interactively, give the user the option to cancel and
2038 then decide what to do differently with the run. Scripts are
2039 just going to disconnect and let the target deal with it,
2040 according to how it's been instructed previously via
2041 disconnected-tracing. */
2042 if (current_trace_status ()->running)
2044 process_tracepoint_on_disconnect ();
2046 if (current_trace_status ()->disconnected_tracing)
2048 if (!query (_("Trace is running and will "
2049 "continue after detach; detach anyway? ")))
2050 error (_("Not confirmed."));
2054 if (!query (_("Trace is running but will "
2055 "stop on detach; detach anyway? ")))
2056 error (_("Not confirmed."));
2061 /* This function handles the details of what to do about an ongoing
2062 tracing run if the user has asked to detach or otherwise disconnect
2066 disconnect_tracing (void)
2068 /* Also we want to be out of tfind mode, otherwise things can get
2069 confusing upon reconnection. Just use these calls instead of
2070 full tfind_1 behavior because we're in the middle of detaching,
2071 and there's no point to updating current stack frame etc. */
2072 trace_reset_local_state ();
2075 /* Worker function for the various flavors of the tfind command. */
2077 tfind_1 (enum trace_find_type type, int num,
2078 CORE_ADDR addr1, CORE_ADDR addr2,
2081 int target_frameno = -1, target_tracept = -1;
2082 struct frame_id old_frame_id = null_frame_id;
2083 struct tracepoint *tp;
2084 struct ui_out *uiout = current_uiout;
2086 /* Only try to get the current stack frame if we have a chance of
2087 succeeding. In particular, if we're trying to get a first trace
2088 frame while all threads are running, it's not going to succeed,
2089 so leave it with a default value and let the frame comparison
2090 below (correctly) decide to print out the source location of the
2092 if (!(type == tfind_number && num == -1)
2093 && (has_stack_frames () || traceframe_number >= 0))
2094 old_frame_id = get_frame_id (get_current_frame ());
2096 target_frameno = target_trace_find (type, num, addr1, addr2,
2099 if (type == tfind_number
2101 && target_frameno == -1)
2103 /* We told the target to get out of tfind mode, and it did. */
2105 else if (target_frameno == -1)
2107 /* A request for a non-existent trace frame has failed.
2108 Our response will be different, depending on FROM_TTY:
2110 If FROM_TTY is true, meaning that this command was
2111 typed interactively by the user, then give an error
2112 and DO NOT change the state of traceframe_number etc.
2114 However if FROM_TTY is false, meaning that we're either
2115 in a script, a loop, or a user-defined command, then
2116 DON'T give an error, but DO change the state of
2117 traceframe_number etc. to invalid.
2119 The rationalle is that if you typed the command, you
2120 might just have committed a typo or something, and you'd
2121 like to NOT lose your current debugging state. However
2122 if you're in a user-defined command or especially in a
2123 loop, then you need a way to detect that the command
2124 failed WITHOUT aborting. This allows you to write
2125 scripts that search thru the trace buffer until the end,
2126 and then continue on to do something else. */
2129 error (_("Target failed to find requested trace frame."));
2133 printf_filtered ("End of trace buffer.\n");
2134 #if 0 /* dubious now? */
2135 /* The following will not recurse, since it's
2137 tfind_command ("-1", from_tty);
2142 tp = get_tracepoint_by_number_on_target (target_tracept);
2144 reinit_frame_cache ();
2145 target_dcache_invalidate ();
2147 set_tracepoint_num (tp ? tp->number : target_tracept);
2149 if (target_frameno != get_traceframe_number ())
2150 gdb::observers::traceframe_changed.notify (target_frameno, tracepoint_number);
2152 set_current_traceframe (target_frameno);
2154 if (target_frameno == -1)
2155 set_traceframe_context (NULL);
2157 set_traceframe_context (get_current_frame ());
2159 if (traceframe_number >= 0)
2161 /* Use different branches for MI and CLI to make CLI messages
2163 if (uiout->is_mi_like_p ())
2165 uiout->field_string ("found", "1");
2166 uiout->field_int ("tracepoint", tracepoint_number);
2167 uiout->field_int ("traceframe", traceframe_number);
2171 printf_unfiltered (_("Found trace frame %d, tracepoint %d\n"),
2172 traceframe_number, tracepoint_number);
2177 if (uiout->is_mi_like_p ())
2178 uiout->field_string ("found", "0");
2179 else if (type == tfind_number && num == -1)
2180 printf_unfiltered (_("No longer looking at any trace frame\n"));
2181 else /* This case may never occur, check. */
2182 printf_unfiltered (_("No trace frame found\n"));
2185 /* If we're in nonstop mode and getting out of looking at trace
2186 frames, there won't be any current frame to go back to and
2189 && (has_stack_frames () || traceframe_number >= 0))
2191 enum print_what print_what;
2193 /* NOTE: in imitation of the step command, try to determine
2194 whether we have made a transition from one function to
2195 another. If so, we'll print the "stack frame" (ie. the new
2196 function and it's arguments) -- otherwise we'll just show the
2199 if (frame_id_eq (old_frame_id,
2200 get_frame_id (get_current_frame ())))
2201 print_what = SRC_LINE;
2203 print_what = SRC_AND_LOC;
2205 print_stack_frame (get_selected_frame (NULL), 1, print_what, 1);
2210 /* Error on looking at traceframes while trace is running. */
2213 check_trace_running (struct trace_status *status)
2215 if (status->running && status->filename == NULL)
2216 error (_("May not look at trace frames while trace is running."));
2219 /* trace_find_command takes a trace frame number n,
2220 sends "QTFrame:<n>" to the target,
2221 and accepts a reply that may contain several optional pieces
2222 of information: a frame number, a tracepoint number, and an
2223 indication of whether this is a trap frame or a stepping frame.
2225 The minimal response is just "OK" (which indicates that the
2226 target does not give us a frame number or a tracepoint number).
2227 Instead of that, the target may send us a string containing
2229 F<hexnum> (gives the selected frame number)
2230 T<hexnum> (gives the selected tracepoint number)
2235 tfind_command_1 (const char *args, int from_tty)
2236 { /* This should only be called with a numeric argument. */
2239 check_trace_running (current_trace_status ());
2241 if (args == 0 || *args == 0)
2242 { /* TFIND with no args means find NEXT trace frame. */
2243 if (traceframe_number == -1)
2244 frameno = 0; /* "next" is first one. */
2246 frameno = traceframe_number + 1;
2248 else if (0 == strcmp (args, "-"))
2250 if (traceframe_number == -1)
2251 error (_("not debugging trace buffer"));
2252 else if (from_tty && traceframe_number == 0)
2253 error (_("already at start of trace buffer"));
2255 frameno = traceframe_number - 1;
2257 /* A hack to work around eval's need for fp to have been collected. */
2258 else if (0 == strcmp (args, "-1"))
2261 frameno = parse_and_eval_long (args);
2264 error (_("invalid input (%d is less than zero)"), frameno);
2266 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
2270 tfind_command (const char *args, int from_tty)
2272 tfind_command_1 (args, from_tty);
2277 tfind_end_command (const char *args, int from_tty)
2279 tfind_command_1 ("-1", from_tty);
2284 tfind_start_command (const char *args, int from_tty)
2286 tfind_command_1 ("0", from_tty);
2289 /* tfind pc command */
2291 tfind_pc_command (const char *args, int from_tty)
2295 check_trace_running (current_trace_status ());
2297 if (args == 0 || *args == 0)
2298 pc = regcache_read_pc (get_current_regcache ());
2300 pc = parse_and_eval_address (args);
2302 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
2305 /* tfind tracepoint command */
2307 tfind_tracepoint_command (const char *args, int from_tty)
2310 struct tracepoint *tp;
2312 check_trace_running (current_trace_status ());
2314 if (args == 0 || *args == 0)
2316 if (tracepoint_number == -1)
2317 error (_("No current tracepoint -- please supply an argument."));
2319 tdp = tracepoint_number; /* Default is current TDP. */
2322 tdp = parse_and_eval_long (args);
2324 /* If we have the tracepoint on hand, use the number that the
2325 target knows about (which may be different if we disconnected
2326 and reconnected). */
2327 tp = get_tracepoint (tdp);
2329 tdp = tp->number_on_target;
2331 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
2334 /* TFIND LINE command:
2336 This command will take a sourceline for argument, just like BREAK
2337 or TRACE (ie. anything that "decode_line_1" can handle).
2339 With no argument, this command will find the next trace frame
2340 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2343 tfind_line_command (const char *args, int from_tty)
2345 check_trace_running (current_trace_status ());
2347 symtab_and_line sal;
2348 if (args == 0 || *args == 0)
2350 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
2354 std::vector<symtab_and_line> sals
2355 = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
2359 if (sal.symtab == 0)
2360 error (_("No line number information available."));
2362 CORE_ADDR start_pc, end_pc;
2363 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2365 if (start_pc == end_pc)
2367 printf_filtered ("Line %d of \"%s\"",
2369 symtab_to_filename_for_display (sal.symtab));
2371 printf_filtered (" is at address ");
2372 print_address (get_current_arch (), start_pc, gdb_stdout);
2374 printf_filtered (" but contains no code.\n");
2375 sal = find_pc_line (start_pc, 0);
2377 && find_line_pc_range (sal, &start_pc, &end_pc)
2378 && start_pc != end_pc)
2379 printf_filtered ("Attempting to find line %d instead.\n",
2382 error (_("Cannot find a good line."));
2386 /* Is there any case in which we get here, and have an address
2387 which the user would want to see? If we have debugging
2388 symbols and no line numbers? */
2389 error (_("Line number %d is out of range for \"%s\"."),
2390 sal.line, symtab_to_filename_for_display (sal.symtab));
2392 /* Find within range of stated line. */
2394 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
2396 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
2399 /* tfind range command */
2401 tfind_range_command (const char *args, int from_tty)
2403 static CORE_ADDR start, stop;
2406 check_trace_running (current_trace_status ());
2408 if (args == 0 || *args == 0)
2409 { /* XXX FIXME: what should default behavior be? */
2410 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2414 if (0 != (tmp = strchr (args, ',')))
2416 std::string start_addr (args, tmp);
2418 tmp = skip_spaces (tmp);
2419 start = parse_and_eval_address (start_addr.c_str ());
2420 stop = parse_and_eval_address (tmp);
2423 { /* No explicit end address? */
2424 start = parse_and_eval_address (args);
2425 stop = start + 1; /* ??? */
2428 tfind_1 (tfind_range, 0, start, stop, from_tty);
2431 /* tfind outside command */
2433 tfind_outside_command (const char *args, int from_tty)
2435 CORE_ADDR start, stop;
2438 if (current_trace_status ()->running
2439 && current_trace_status ()->filename == NULL)
2440 error (_("May not look at trace frames while trace is running."));
2442 if (args == 0 || *args == 0)
2443 { /* XXX FIXME: what should default behavior be? */
2444 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2448 if (0 != (tmp = strchr (args, ',')))
2450 std::string start_addr (args, tmp);
2452 tmp = skip_spaces (tmp);
2453 start = parse_and_eval_address (start_addr.c_str ());
2454 stop = parse_and_eval_address (tmp);
2457 { /* No explicit end address? */
2458 start = parse_and_eval_address (args);
2459 stop = start + 1; /* ??? */
2462 tfind_1 (tfind_outside, 0, start, stop, from_tty);
2465 /* info scope command: list the locals for a scope. */
2467 info_scope_command (const char *args_in, int from_tty)
2470 struct bound_minimal_symbol msym;
2471 const struct block *block;
2472 const char *symname;
2473 const char *save_args = args_in;
2474 struct block_iterator iter;
2476 struct gdbarch *gdbarch;
2478 const char *args = args_in;
2480 if (args == 0 || *args == 0)
2481 error (_("requires an argument (function, "
2482 "line or *addr) to define a scope"));
2484 event_location_up location = string_to_event_location (&args,
2486 std::vector<symtab_and_line> sals
2487 = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
2491 /* Presumably decode_line_1 has already warned. */
2495 /* Resolve line numbers to PC. */
2496 resolve_sal_pc (&sals[0]);
2497 block = block_for_pc (sals[0].pc);
2501 QUIT; /* Allow user to bail out with ^C. */
2502 ALL_BLOCK_SYMBOLS (block, iter, sym)
2504 QUIT; /* Allow user to bail out with ^C. */
2506 printf_filtered ("Scope for %s:\n", save_args);
2509 symname = SYMBOL_PRINT_NAME (sym);
2510 if (symname == NULL || *symname == '\0')
2511 continue; /* Probably botched, certainly useless. */
2513 gdbarch = symbol_arch (sym);
2515 printf_filtered ("Symbol %s is ", symname);
2517 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
2518 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym,
2519 BLOCK_START (block),
2523 switch (SYMBOL_CLASS (sym))
2526 case LOC_UNDEF: /* Messed up symbol? */
2527 printf_filtered ("a bogus symbol, class %d.\n",
2528 SYMBOL_CLASS (sym));
2529 count--; /* Don't count this one. */
2532 printf_filtered ("a constant with value %s (%s)",
2533 plongest (SYMBOL_VALUE (sym)),
2534 hex_string (SYMBOL_VALUE (sym)));
2536 case LOC_CONST_BYTES:
2537 printf_filtered ("constant bytes: ");
2538 if (SYMBOL_TYPE (sym))
2539 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2540 fprintf_filtered (gdb_stdout, " %02x",
2541 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2544 printf_filtered ("in static storage at address ");
2545 printf_filtered ("%s", paddress (gdbarch,
2546 SYMBOL_VALUE_ADDRESS (sym)));
2549 /* GDBARCH is the architecture associated with the objfile
2550 the symbol is defined in; the target architecture may be
2551 different, and may provide additional registers. However,
2552 we do not know the target architecture at this point.
2553 We assume the objfile architecture will contain all the
2554 standard registers that occur in debug info in that
2556 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2559 if (SYMBOL_IS_ARGUMENT (sym))
2560 printf_filtered ("an argument in register $%s",
2561 gdbarch_register_name (gdbarch, regno));
2563 printf_filtered ("a local variable in register $%s",
2564 gdbarch_register_name (gdbarch, regno));
2567 printf_filtered ("an argument at stack/frame offset %s",
2568 plongest (SYMBOL_VALUE (sym)));
2571 printf_filtered ("a local variable at frame offset %s",
2572 plongest (SYMBOL_VALUE (sym)));
2575 printf_filtered ("a reference argument at offset %s",
2576 plongest (SYMBOL_VALUE (sym)));
2578 case LOC_REGPARM_ADDR:
2579 /* Note comment at LOC_REGISTER. */
2580 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2582 printf_filtered ("the address of an argument, in register $%s",
2583 gdbarch_register_name (gdbarch, regno));
2586 printf_filtered ("a typedef.\n");
2589 printf_filtered ("a label at address ");
2590 printf_filtered ("%s", paddress (gdbarch,
2591 SYMBOL_VALUE_ADDRESS (sym)));
2594 printf_filtered ("a function at address ");
2595 printf_filtered ("%s",
2596 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
2598 case LOC_UNRESOLVED:
2599 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
2601 if (msym.minsym == NULL)
2602 printf_filtered ("Unresolved Static");
2605 printf_filtered ("static storage at address ");
2606 printf_filtered ("%s",
2608 BMSYMBOL_VALUE_ADDRESS (msym)));
2611 case LOC_OPTIMIZED_OUT:
2612 printf_filtered ("optimized out.\n");
2615 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
2618 if (SYMBOL_TYPE (sym))
2619 printf_filtered (", length %d.\n",
2620 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
2622 if (BLOCK_FUNCTION (block))
2625 block = BLOCK_SUPERBLOCK (block);
2628 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2632 /* Helper for trace_dump_command. Dump the action list starting at
2633 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2634 actions of the body of a while-stepping action. STEPPING_FRAME is
2635 set if the current traceframe was determined to be a while-stepping
2639 trace_dump_actions (struct command_line *action,
2640 int stepping_actions, int stepping_frame,
2643 const char *action_exp, *next_comma;
2645 for (; action != NULL; action = action->next)
2647 struct cmd_list_element *cmd;
2649 QUIT; /* Allow user to bail out with ^C. */
2650 action_exp = action->line;
2651 action_exp = skip_spaces (action_exp);
2653 /* The collection actions to be done while stepping are
2654 bracketed by the commands "while-stepping" and "end". */
2656 if (*action_exp == '#') /* comment line */
2659 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2661 error (_("Bad action list item: %s"), action_exp);
2663 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
2665 gdb_assert (action->body_list_1 == nullptr);
2666 trace_dump_actions (action->body_list_0.get (),
2667 1, stepping_frame, from_tty);
2669 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
2671 /* Display the collected data.
2672 For the trap frame, display only what was collected at
2673 the trap. Likewise for stepping frames, display only
2674 what was collected while stepping. This means that the
2675 two boolean variables, STEPPING_FRAME and
2676 STEPPING_ACTIONS should be equal. */
2677 if (stepping_frame == stepping_actions)
2680 struct cleanup *old_chain
2681 = make_cleanup (free_current_contents, &cmd);
2682 int trace_string = 0;
2684 if (*action_exp == '/')
2685 action_exp = decode_agent_options (action_exp, &trace_string);
2688 { /* Repeat over a comma-separated list. */
2689 QUIT; /* Allow user to bail out with ^C. */
2690 if (*action_exp == ',')
2692 action_exp = skip_spaces (action_exp);
2694 next_comma = strchr (action_exp, ',');
2696 if (0 == strncasecmp (action_exp, "$reg", 4))
2697 registers_info (NULL, from_tty);
2698 else if (0 == strncasecmp (action_exp, "$_ret", 5))
2700 else if (0 == strncasecmp (action_exp, "$loc", 4))
2701 info_locals_command (NULL, from_tty);
2702 else if (0 == strncasecmp (action_exp, "$arg", 4))
2703 info_args_command (NULL, from_tty);
2706 if (next_comma != NULL)
2708 size_t len = next_comma - action_exp;
2710 cmd = (char *) xrealloc (cmd, len + 1);
2711 memcpy (cmd, action_exp, len);
2716 size_t len = strlen (action_exp);
2718 cmd = (char *) xrealloc (cmd, len + 1);
2719 memcpy (cmd, action_exp, len + 1);
2722 printf_filtered ("%s = ", cmd);
2723 output_command_const (cmd, from_tty);
2724 printf_filtered ("\n");
2726 action_exp = next_comma;
2728 while (action_exp && *action_exp == ',');
2730 do_cleanups (old_chain);
2736 /* Return bp_location of the tracepoint associated with the current
2737 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
2738 is a stepping traceframe. */
2740 struct bp_location *
2741 get_traceframe_location (int *stepping_frame_p)
2743 struct tracepoint *t;
2744 struct bp_location *tloc;
2745 struct regcache *regcache;
2747 if (tracepoint_number == -1)
2748 error (_("No current trace frame."));
2750 t = get_tracepoint (tracepoint_number);
2753 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2756 /* The current frame is a trap frame if the frame PC is equal to the
2757 tracepoint PC. If not, then the current frame was collected
2758 during single-stepping. */
2759 regcache = get_current_regcache ();
2761 /* If the traceframe's address matches any of the tracepoint's
2762 locations, assume it is a direct hit rather than a while-stepping
2763 frame. (FIXME this is not reliable, should record each frame's
2765 for (tloc = t->loc; tloc; tloc = tloc->next)
2766 if (tloc->address == regcache_read_pc (regcache))
2768 *stepping_frame_p = 0;
2772 /* If this is a stepping frame, we don't know which location
2773 triggered. The first is as good (or bad) a guess as any... */
2774 *stepping_frame_p = 1;
2778 /* Return the default collect actions of a tracepoint T. */
2780 static counted_command_line
2781 all_tracepoint_actions (struct breakpoint *t)
2783 counted_command_line actions (nullptr, command_lines_deleter ());
2785 /* If there are default expressions to collect, make up a collect
2786 action and prepend to the action list to encode. Note that since
2787 validation is per-tracepoint (local var "xyz" might be valid for
2788 one tracepoint and not another, etc), we make up the action on
2789 the fly, and don't cache it. */
2790 if (*default_collect)
2792 struct command_line *default_collect_action;
2793 gdb::unique_xmalloc_ptr<char> default_collect_line
2794 (xstrprintf ("collect %s", default_collect));
2796 validate_actionline (default_collect_line.get (), t);
2797 actions.reset (new struct command_line (simple_control,
2798 default_collect_line.release ()),
2799 command_lines_deleter ());
2805 /* The tdump command. */
2808 tdump_command (const char *args, int from_tty)
2810 int stepping_frame = 0;
2811 struct bp_location *loc;
2813 /* This throws an error is not inspecting a trace frame. */
2814 loc = get_traceframe_location (&stepping_frame);
2816 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2817 tracepoint_number, traceframe_number);
2819 /* This command only makes sense for the current frame, not the
2821 scoped_restore_current_thread restore_thread;
2823 select_frame (get_current_frame ());
2825 counted_command_line actions = all_tracepoint_actions (loc->owner);
2827 trace_dump_actions (actions.get (), 0, stepping_frame, from_tty);
2828 trace_dump_actions (breakpoint_commands (loc->owner), 0, stepping_frame,
2832 /* Encode a piece of a tracepoint's source-level definition in a form
2833 that is suitable for both protocol and saving in files. */
2834 /* This version does not do multiple encodes for long strings; it should
2835 return an offset to the next piece to encode. FIXME */
2838 encode_source_string (int tpnum, ULONGEST addr,
2839 const char *srctype, const char *src,
2840 char *buf, int buf_size)
2842 if (80 + strlen (srctype) > buf_size)
2843 error (_("Buffer too small for source encoding"));
2844 sprintf (buf, "%x:%s:%s:%x:%x:",
2845 tpnum, phex_nz (addr, sizeof (addr)),
2846 srctype, 0, (int) strlen (src));
2847 if (strlen (buf) + strlen (src) * 2 >= buf_size)
2848 error (_("Source string too long for buffer"));
2849 bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src));
2853 /* Tell the target what to do with an ongoing tracing run if GDB
2854 disconnects for some reason. */
2857 set_disconnected_tracing (const char *args, int from_tty,
2858 struct cmd_list_element *c)
2860 target_set_disconnected_tracing (disconnected_tracing);
2864 set_circular_trace_buffer (const char *args, int from_tty,
2865 struct cmd_list_element *c)
2867 target_set_circular_trace_buffer (circular_trace_buffer);
2871 set_trace_buffer_size (const char *args, int from_tty,
2872 struct cmd_list_element *c)
2874 target_set_trace_buffer_size (trace_buffer_size);
2878 set_trace_user (const char *args, int from_tty,
2879 struct cmd_list_element *c)
2883 ret = target_set_trace_notes (trace_user, NULL, NULL);
2886 warning (_("Target does not support trace notes, user ignored"));
2890 set_trace_notes (const char *args, int from_tty,
2891 struct cmd_list_element *c)
2895 ret = target_set_trace_notes (NULL, trace_notes, NULL);
2898 warning (_("Target does not support trace notes, note ignored"));
2902 set_trace_stop_notes (const char *args, int from_tty,
2903 struct cmd_list_element *c)
2907 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes);
2910 warning (_("Target does not support trace notes, stop note ignored"));
2913 /* Convert the memory pointed to by mem into hex, placing result in buf.
2914 * Return a pointer to the last char put in buf (null)
2915 * "stolen" from sparc-stub.c
2918 static const char hexchars[] = "0123456789abcdef";
2921 mem2hex (gdb_byte *mem, char *buf, int count)
2929 *buf++ = hexchars[ch >> 4];
2930 *buf++ = hexchars[ch & 0xf];
2939 get_traceframe_number (void)
2941 return traceframe_number;
2945 get_tracepoint_number (void)
2947 return tracepoint_number;
2950 /* Make the traceframe NUM be the current trace frame. Does nothing
2951 if NUM is already current. */
2954 set_current_traceframe (int num)
2958 if (traceframe_number == num)
2960 /* Nothing to do. */
2964 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
2967 warning (_("could not change traceframe"));
2969 set_traceframe_num (newnum);
2971 /* Changing the traceframe changes our view of registers and of the
2973 registers_changed ();
2975 clear_traceframe_info ();
2978 scoped_restore_current_traceframe::scoped_restore_current_traceframe ()
2979 : m_traceframe_number (traceframe_number)
2982 /* Given a number and address, return an uploaded tracepoint with that
2983 number, creating if necessary. */
2985 struct uploaded_tp *
2986 get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
2988 struct uploaded_tp *utp;
2990 for (utp = *utpp; utp; utp = utp->next)
2991 if (utp->number == num && utp->addr == addr)
2994 utp = new uploaded_tp;
3004 free_uploaded_tps (struct uploaded_tp **utpp)
3006 struct uploaded_tp *next_one;
3010 next_one = (*utpp)->next;
3016 /* Given a number and address, return an uploaded tracepoint with that
3017 number, creating if necessary. */
3019 struct uploaded_tsv *
3020 get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
3022 struct uploaded_tsv *utsv;
3024 for (utsv = *utsvp; utsv; utsv = utsv->next)
3025 if (utsv->number == num)
3028 utsv = XCNEW (struct uploaded_tsv);
3030 utsv->next = *utsvp;
3037 free_uploaded_tsvs (struct uploaded_tsv **utsvp)
3039 struct uploaded_tsv *next_one;
3043 next_one = (*utsvp)->next;
3049 /* FIXME this function is heuristic and will miss the cases where the
3050 conditional is semantically identical but differs in whitespace,
3051 such as "x == 0" vs "x==0". */
3054 cond_string_is_same (char *str1, char *str2)
3056 if (str1 == NULL || str2 == NULL)
3057 return (str1 == str2);
3059 return (strcmp (str1, str2) == 0);
3062 /* Look for an existing tracepoint that seems similar enough to the
3063 uploaded one. Enablement isn't compared, because the user can
3064 toggle that freely, and may have done so in anticipation of the
3065 next trace run. Return the location of matched tracepoint. */
3067 static struct bp_location *
3068 find_matching_tracepoint_location (struct uploaded_tp *utp)
3070 VEC(breakpoint_p) *tp_vec = all_tracepoints ();
3072 struct breakpoint *b;
3073 struct bp_location *loc;
3075 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
3077 struct tracepoint *t = (struct tracepoint *) b;
3079 if (b->type == utp->type
3080 && t->step_count == utp->step
3081 && t->pass_count == utp->pass
3082 && cond_string_is_same (t->cond_string, utp->cond_string)
3083 /* FIXME also test actions. */
3086 /* Scan the locations for an address match. */
3087 for (loc = b->loc; loc; loc = loc->next)
3089 if (loc->address == utp->addr)
3097 /* Given a list of tracepoints uploaded from a target, attempt to
3098 match them up with existing tracepoints, and create new ones if not
3102 merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
3104 struct uploaded_tp *utp;
3105 /* A set of tracepoints which are modified. */
3106 VEC(breakpoint_p) *modified_tp = NULL;
3108 struct breakpoint *b;
3110 /* Look for GDB tracepoints that match up with our uploaded versions. */
3111 for (utp = *uploaded_tps; utp; utp = utp->next)
3113 struct bp_location *loc;
3114 struct tracepoint *t;
3116 loc = find_matching_tracepoint_location (utp);
3121 /* Mark this location as already inserted. */
3123 t = (struct tracepoint *) loc->owner;
3124 printf_filtered (_("Assuming tracepoint %d is same "
3125 "as target's tracepoint %d at %s.\n"),
3126 loc->owner->number, utp->number,
3127 paddress (loc->gdbarch, utp->addr));
3129 /* The tracepoint LOC->owner was modified (the location LOC
3130 was marked as inserted in the target). Save it in
3131 MODIFIED_TP if not there yet. The 'breakpoint-modified'
3132 observers will be notified later once for each tracepoint
3133 saved in MODIFIED_TP. */
3135 VEC_iterate (breakpoint_p, modified_tp, ix, b);
3137 if (b == loc->owner)
3143 VEC_safe_push (breakpoint_p, modified_tp, loc->owner);
3147 t = create_tracepoint_from_upload (utp);
3149 printf_filtered (_("Created tracepoint %d for "
3150 "target's tracepoint %d at %s.\n"),
3151 t->number, utp->number,
3152 paddress (get_current_arch (), utp->addr));
3154 printf_filtered (_("Failed to create tracepoint for target's "
3155 "tracepoint %d at %s, skipping it.\n"),
3157 paddress (get_current_arch (), utp->addr));
3159 /* Whether found or created, record the number used by the
3160 target, to help with mapping target tracepoints back to their
3161 counterparts here. */
3163 t->number_on_target = utp->number;
3166 /* Notify 'breakpoint-modified' observer that at least one of B's
3167 locations was changed. */
3168 for (ix = 0; VEC_iterate (breakpoint_p, modified_tp, ix, b); ix++)
3169 gdb::observers::breakpoint_modified.notify (b);
3171 VEC_free (breakpoint_p, modified_tp);
3172 free_uploaded_tps (uploaded_tps);
3175 /* Trace state variables don't have much to identify them beyond their
3176 name, so just use that to detect matches. */
3178 static struct trace_state_variable *
3179 find_matching_tsv (struct uploaded_tsv *utsv)
3184 return find_trace_state_variable (utsv->name);
3187 static struct trace_state_variable *
3188 create_tsv_from_upload (struct uploaded_tsv *utsv)
3190 const char *namebase;
3193 struct trace_state_variable *tsv;
3197 namebase = utsv->name;
3203 buf = string_printf ("%s_%d", namebase, try_num++);
3206 /* Fish for a name that is not in use. */
3207 /* (should check against all internal vars?) */
3208 while (find_trace_state_variable (buf.c_str ()))
3209 buf = string_printf ("%s_%d", namebase, try_num++);
3211 /* We have an available name, create the variable. */
3212 tsv = create_trace_state_variable (buf.c_str ());
3213 tsv->initial_value = utsv->initial_value;
3214 tsv->builtin = utsv->builtin;
3216 gdb::observers::tsv_created.notify (tsv);
3221 /* Given a list of uploaded trace state variables, try to match them
3222 up with existing variables, or create additional ones. */
3225 merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
3228 struct uploaded_tsv *utsv;
3229 struct trace_state_variable *tsv;
3232 /* Most likely some numbers will have to be reassigned as part of
3233 the merge, so clear them all in anticipation. */
3234 for (trace_state_variable &tsv : tvariables)
3237 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
3239 tsv = find_matching_tsv (utsv);
3243 printf_filtered (_("Assuming trace state variable $%s "
3244 "is same as target's variable %d.\n"),
3245 tsv->name.c_str (), utsv->number);
3249 tsv = create_tsv_from_upload (utsv);
3251 printf_filtered (_("Created trace state variable "
3252 "$%s for target's variable %d.\n"),
3253 tsv->name.c_str (), utsv->number);
3255 /* Give precedence to numberings that come from the target. */
3257 tsv->number = utsv->number;
3260 /* Renumber everything that didn't get a target-assigned number. */
3262 for (const trace_state_variable &tsv : tvariables)
3263 highest = std::max (tsv.number, highest);
3266 for (trace_state_variable &tsv : tvariables)
3267 if (tsv.number == 0)
3268 tsv.number = highest++;
3270 free_uploaded_tsvs (uploaded_tsvs);
3273 /* Parse the part of trace status syntax that is shared between
3274 the remote protocol and the trace file reader. */
3277 parse_trace_status (const char *line, struct trace_status *ts)
3279 const char *p = line, *p1, *p2, *p3, *p_temp;
3283 ts->running_known = 1;
3284 ts->running = (*p++ == '1');
3285 ts->stop_reason = trace_stop_reason_unknown;
3286 xfree (ts->stop_desc);
3287 ts->stop_desc = NULL;
3288 ts->traceframe_count = -1;
3289 ts->traceframes_created = -1;
3290 ts->buffer_free = -1;
3291 ts->buffer_size = -1;
3292 ts->disconnected_tracing = 0;
3293 ts->circular_buffer = 0;
3294 xfree (ts->user_name);
3295 ts->user_name = NULL;
3298 ts->start_time = ts->stop_time = 0;
3302 p1 = strchr (p, ':');
3304 error (_("Malformed trace status, at %s\n\
3305 Status line: '%s'\n"), p, line);
3306 p3 = strchr (p, ';');
3308 p3 = p + strlen (p);
3309 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
3311 p = unpack_varlen_hex (++p1, &val);
3312 ts->stop_reason = trace_buffer_full;
3314 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
3316 p = unpack_varlen_hex (++p1, &val);
3317 ts->stop_reason = trace_never_run;
3319 else if (strncmp (p, stop_reason_names[tracepoint_passcount],
3322 p = unpack_varlen_hex (++p1, &val);
3323 ts->stop_reason = tracepoint_passcount;
3324 ts->stopping_tracepoint = val;
3326 else if (strncmp (p, stop_reason_names[trace_stop_command], p1 - p) == 0)
3328 p2 = strchr (++p1, ':');
3336 ts->stop_desc = (char *) xmalloc (strlen (line));
3337 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3338 ts->stop_desc[end] = '\0';
3341 ts->stop_desc = xstrdup ("");
3343 p = unpack_varlen_hex (++p2, &val);
3344 ts->stop_reason = trace_stop_command;
3346 else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0)
3348 p = unpack_varlen_hex (++p1, &val);
3349 ts->stop_reason = trace_disconnected;
3351 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
3353 p2 = strchr (++p1, ':');
3356 ts->stop_desc = (char *) xmalloc ((p2 - p1) / 2 + 1);
3357 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3358 ts->stop_desc[end] = '\0';
3361 ts->stop_desc = xstrdup ("");
3363 p = unpack_varlen_hex (++p2, &val);
3364 ts->stopping_tracepoint = val;
3365 ts->stop_reason = tracepoint_error;
3367 else if (strncmp (p, "tframes", p1 - p) == 0)
3369 p = unpack_varlen_hex (++p1, &val);
3370 ts->traceframe_count = val;
3372 else if (strncmp (p, "tcreated", p1 - p) == 0)
3374 p = unpack_varlen_hex (++p1, &val);
3375 ts->traceframes_created = val;
3377 else if (strncmp (p, "tfree", p1 - p) == 0)
3379 p = unpack_varlen_hex (++p1, &val);
3380 ts->buffer_free = val;
3382 else if (strncmp (p, "tsize", p1 - p) == 0)
3384 p = unpack_varlen_hex (++p1, &val);
3385 ts->buffer_size = val;
3387 else if (strncmp (p, "disconn", p1 - p) == 0)
3389 p = unpack_varlen_hex (++p1, &val);
3390 ts->disconnected_tracing = val;
3392 else if (strncmp (p, "circular", p1 - p) == 0)
3394 p = unpack_varlen_hex (++p1, &val);
3395 ts->circular_buffer = val;
3397 else if (strncmp (p, "starttime", p1 - p) == 0)
3399 p = unpack_varlen_hex (++p1, &val);
3400 ts->start_time = val;
3402 else if (strncmp (p, "stoptime", p1 - p) == 0)
3404 p = unpack_varlen_hex (++p1, &val);
3405 ts->stop_time = val;
3407 else if (strncmp (p, "username", p1 - p) == 0)
3410 ts->user_name = (char *) xmalloc (strlen (p) / 2);
3411 end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2);
3412 ts->user_name[end] = '\0';
3415 else if (strncmp (p, "notes", p1 - p) == 0)
3418 ts->notes = (char *) xmalloc (strlen (p) / 2);
3419 end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2);
3420 ts->notes[end] = '\0';
3425 /* Silently skip unknown optional info. */
3426 p_temp = strchr (p1 + 1, ';');
3430 /* Must be at the end. */
3437 parse_tracepoint_status (const char *p, struct breakpoint *bp,
3438 struct uploaded_tp *utp)
3441 struct tracepoint *tp = (struct tracepoint *) bp;
3443 p = unpack_varlen_hex (p, &uval);
3445 tp->hit_count += uval;
3447 utp->hit_count += uval;
3448 p = unpack_varlen_hex (p + 1, &uval);
3450 tp->traceframe_usage += uval;
3452 utp->traceframe_usage += uval;
3453 /* Ignore any extra, allowing for future extensions. */
3456 /* Given a line of text defining a part of a tracepoint, parse it into
3457 an "uploaded tracepoint". */
3460 parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
3464 ULONGEST num, addr, step, pass, orig_size, xlen, start;
3467 const char *srctype;
3469 struct uploaded_tp *utp = NULL;
3472 /* Both tracepoint and action definitions start with the same number
3473 and address sequence. */
3475 p = unpack_varlen_hex (p, &num);
3476 p++; /* skip a colon */
3477 p = unpack_varlen_hex (p, &addr);
3478 p++; /* skip a colon */
3481 enabled = (*p++ == 'E');
3482 p++; /* skip a colon */
3483 p = unpack_varlen_hex (p, &step);
3484 p++; /* skip a colon */
3485 p = unpack_varlen_hex (p, &pass);
3486 type = bp_tracepoint;
3488 /* Thumb through optional fields. */
3491 p++; /* skip a colon */
3494 type = bp_fast_tracepoint;
3496 p = unpack_varlen_hex (p, &orig_size);
3500 type = bp_static_tracepoint;
3506 p = unpack_varlen_hex (p, &xlen);
3507 p++; /* skip a comma */
3508 cond = (char *) xmalloc (2 * xlen + 1);
3509 strncpy (cond, p, 2 * xlen);
3510 cond[2 * xlen] = '\0';
3514 warning (_("Unrecognized char '%c' in tracepoint "
3515 "definition, skipping rest"), *p);
3517 utp = get_uploaded_tp (num, addr, utpp);
3519 utp->enabled = enabled;
3524 else if (piece == 'A')
3526 utp = get_uploaded_tp (num, addr, utpp);
3527 utp->actions.push_back (xstrdup (p));
3529 else if (piece == 'S')
3531 utp = get_uploaded_tp (num, addr, utpp);
3532 utp->step_actions.push_back (xstrdup (p));
3534 else if (piece == 'Z')
3536 /* Parse a chunk of source form definition. */
3537 utp = get_uploaded_tp (num, addr, utpp);
3539 p = strchr (p, ':');
3540 p++; /* skip a colon */
3541 p = unpack_varlen_hex (p, &start);
3542 p++; /* skip a colon */
3543 p = unpack_varlen_hex (p, &xlen);
3544 p++; /* skip a colon */
3546 buf = (char *) alloca (strlen (line));
3548 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3551 if (startswith (srctype, "at:"))
3552 utp->at_string = xstrdup (buf);
3553 else if (startswith (srctype, "cond:"))
3554 utp->cond_string = xstrdup (buf);
3555 else if (startswith (srctype, "cmd:"))
3556 utp->cmd_strings.push_back (xstrdup (buf));
3558 else if (piece == 'V')
3560 utp = get_uploaded_tp (num, addr, utpp);
3562 parse_tracepoint_status (p, NULL, utp);
3566 /* Don't error out, the target might be sending us optional
3567 info that we don't care about. */
3568 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
3572 /* Convert a textual description of a trace state variable into an
3576 parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp)
3580 ULONGEST num, initval, builtin;
3582 struct uploaded_tsv *utsv = NULL;
3584 buf = (char *) alloca (strlen (line));
3587 p = unpack_varlen_hex (p, &num);
3588 p++; /* skip a colon */
3589 p = unpack_varlen_hex (p, &initval);
3590 p++; /* skip a colon */
3591 p = unpack_varlen_hex (p, &builtin);
3592 p++; /* skip a colon */
3593 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3596 utsv = get_uploaded_tsv (num, utsvp);
3597 utsv->initial_value = initval;
3598 utsv->builtin = builtin;
3599 utsv->name = xstrdup (buf);
3602 /* Given a line of text defining a static tracepoint marker, parse it
3603 into a "static tracepoint marker" object. Throws an error is
3604 parsing fails. If PP is non-null, it points to one past the end of
3605 the parsed marker definition. */
3608 parse_static_tracepoint_marker_definition (const char *line, const char **pp,
3609 static_tracepoint_marker *marker)
3611 const char *p, *endp;
3615 p = unpack_varlen_hex (p, &addr);
3616 p++; /* skip a colon */
3618 marker->gdbarch = target_gdbarch ();
3619 marker->address = (CORE_ADDR) addr;
3621 endp = strchr (p, ':');
3623 error (_("bad marker definition: %s"), line);
3625 marker->str_id = hex2str (p, (endp - p) / 2);
3628 p++; /* skip a colon */
3630 /* This definition may be followed by another one, separated by a comma. */
3632 endp = strchr (p, ',');
3633 if (endp != nullptr)
3636 hex_len = strlen (p);
3638 marker->extra = hex2str (p, hex_len / 2);
3644 /* Print MARKER to gdb_stdout. */
3647 print_one_static_tracepoint_marker (int count,
3648 const static_tracepoint_marker &marker)
3652 char wrap_indent[80];
3653 char extra_field_indent[80];
3654 struct ui_out *uiout = current_uiout;
3655 VEC(breakpoint_p) *tracepoints;
3657 symtab_and_line sal;
3658 sal.pc = marker.address;
3660 tracepoints = static_tracepoints_here (marker.address);
3662 ui_out_emit_tuple tuple_emitter (uiout, "marker");
3664 /* A counter field to help readability. This is not a stable
3666 uiout->field_int ("count", count);
3668 uiout->field_string ("marker-id", marker.str_id.c_str ());
3670 uiout->field_fmt ("enabled", "%c",
3671 !VEC_empty (breakpoint_p, tracepoints) ? 'y' : 'n');
3674 strcpy (wrap_indent, " ");
3676 if (gdbarch_addr_bit (marker.gdbarch) <= 32)
3677 strcat (wrap_indent, " ");
3679 strcat (wrap_indent, " ");
3681 strcpy (extra_field_indent, " ");
3683 uiout->field_core_addr ("addr", marker.gdbarch, marker.address);
3685 sal = find_pc_line (marker.address, 0);
3686 sym = find_pc_sect_function (marker.address, NULL);
3689 uiout->text ("in ");
3690 uiout->field_string ("func",
3691 SYMBOL_PRINT_NAME (sym));
3692 uiout->wrap_hint (wrap_indent);
3693 uiout->text (" at ");
3696 uiout->field_skip ("func");
3698 if (sal.symtab != NULL)
3700 uiout->field_string ("file",
3701 symtab_to_filename_for_display (sal.symtab));
3704 if (uiout->is_mi_like_p ())
3706 const char *fullname = symtab_to_fullname (sal.symtab);
3708 uiout->field_string ("fullname", fullname);
3711 uiout->field_skip ("fullname");
3713 uiout->field_int ("line", sal.line);
3717 uiout->field_skip ("fullname");
3718 uiout->field_skip ("line");
3722 uiout->text (extra_field_indent);
3723 uiout->text (_("Data: \""));
3724 uiout->field_string ("extra-data", marker.extra.c_str ());
3725 uiout->text ("\"\n");
3727 if (!VEC_empty (breakpoint_p, tracepoints))
3730 struct breakpoint *b;
3733 ui_out_emit_tuple tuple_emitter (uiout, "tracepoints-at");
3735 uiout->text (extra_field_indent);
3736 uiout->text (_("Probed by static tracepoints: "));
3737 for (ix = 0; VEC_iterate(breakpoint_p, tracepoints, ix, b); ix++)
3742 uiout->field_int ("tracepoint-id", b->number);
3746 if (uiout->is_mi_like_p ())
3747 uiout->field_int ("number-of-tracepoints",
3748 VEC_length(breakpoint_p, tracepoints));
3752 VEC_free (breakpoint_p, tracepoints);
3756 info_static_tracepoint_markers_command (const char *arg, int from_tty)
3758 struct ui_out *uiout = current_uiout;
3759 std::vector<static_tracepoint_marker> markers
3760 = target_static_tracepoint_markers_by_strid (NULL);
3762 /* We don't have to check target_can_use_agent and agent's capability on
3763 static tracepoint here, in order to be compatible with older GDBserver.
3764 We don't check USE_AGENT is true or not, because static tracepoints
3765 don't work without in-process agent, so we don't bother users to type
3766 `set agent on' when to use static tracepoint. */
3768 ui_out_emit_table table_emitter (uiout, 5, -1,
3769 "StaticTracepointMarkersTable");
3771 uiout->table_header (7, ui_left, "counter", "Cnt");
3773 uiout->table_header (40, ui_left, "marker-id", "ID");
3775 uiout->table_header (3, ui_left, "enabled", "Enb");
3776 if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
3777 uiout->table_header (10, ui_left, "addr", "Address");
3779 uiout->table_header (18, ui_left, "addr", "Address");
3780 uiout->table_header (40, ui_noalign, "what", "What");
3782 uiout->table_body ();
3784 for (int i = 0; i < markers.size (); i++)
3785 print_one_static_tracepoint_marker (i + 1, markers[i]);
3788 /* The $_sdata convenience variable is a bit special. We don't know
3789 for sure type of the value until we actually have a chance to fetch
3790 the data --- the size of the object depends on what has been
3791 collected. We solve this by making $_sdata be an internalvar that
3792 creates a new value on access. */
3794 /* Return a new value with the correct type for the sdata object of
3795 the current trace frame. Return a void value if there's no object
3798 static struct value *
3799 sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var,
3802 /* We need to read the whole object before we know its size. */
3803 gdb::optional<gdb::byte_vector> buf
3804 = target_read_alloc (target_stack, TARGET_OBJECT_STATIC_TRACE_DATA,
3811 type = init_vector_type (builtin_type (gdbarch)->builtin_true_char,
3813 v = allocate_value (type);
3814 memcpy (value_contents_raw (v), buf->data (), buf->size ());
3818 return allocate_value (builtin_type (gdbarch)->builtin_void);
3821 #if !defined(HAVE_LIBEXPAT)
3823 struct std::unique_ptr<traceframe_info>
3824 parse_traceframe_info (const char *tframe_info)
3826 static int have_warned;
3831 warning (_("Can not parse XML trace frame info; XML support "
3832 "was disabled at compile time"));
3838 #else /* HAVE_LIBEXPAT */
3840 #include "xml-support.h"
3842 /* Handle the start of a <memory> element. */
3845 traceframe_info_start_memory (struct gdb_xml_parser *parser,
3846 const struct gdb_xml_element *element,
3848 std::vector<gdb_xml_value> &attributes)
3850 struct traceframe_info *info = (struct traceframe_info *) user_data;
3851 ULONGEST *start_p, *length_p;
3854 = (ULONGEST *) xml_find_attribute (attributes, "start")->value.get ();
3856 = (ULONGEST *) xml_find_attribute (attributes, "length")->value.get ();
3858 info->memory.emplace_back (*start_p, *length_p);
3861 /* Handle the start of a <tvar> element. */
3864 traceframe_info_start_tvar (struct gdb_xml_parser *parser,
3865 const struct gdb_xml_element *element,
3867 std::vector<gdb_xml_value> &attributes)
3869 struct traceframe_info *info = (struct traceframe_info *) user_data;
3870 const char *id_attrib
3871 = (const char *) xml_find_attribute (attributes, "id")->value.get ();
3872 int id = gdb_xml_parse_ulongest (parser, id_attrib);
3874 info->tvars.push_back (id);
3877 /* The allowed elements and attributes for an XML memory map. */
3879 static const struct gdb_xml_attribute memory_attributes[] = {
3880 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
3881 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
3882 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3885 static const struct gdb_xml_attribute tvar_attributes[] = {
3886 { "id", GDB_XML_AF_NONE, NULL, NULL },
3887 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3890 static const struct gdb_xml_element traceframe_info_children[] = {
3891 { "memory", memory_attributes, NULL,
3892 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3893 traceframe_info_start_memory, NULL },
3894 { "tvar", tvar_attributes, NULL,
3895 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3896 traceframe_info_start_tvar, NULL },
3897 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3900 static const struct gdb_xml_element traceframe_info_elements[] = {
3901 { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE,
3903 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3906 /* Parse a traceframe-info XML document. */
3909 parse_traceframe_info (const char *tframe_info)
3911 traceframe_info_up result (new traceframe_info);
3913 if (gdb_xml_parse_quick (_("trace frame info"),
3914 "traceframe-info.dtd", traceframe_info_elements,
3915 tframe_info, result.get ()) == 0)
3921 #endif /* HAVE_LIBEXPAT */
3923 /* Returns the traceframe_info object for the current traceframe.
3924 This is where we avoid re-fetching the object from the target if we
3925 already have it cached. */
3927 struct traceframe_info *
3928 get_traceframe_info (void)
3930 if (current_traceframe_info == NULL)
3931 current_traceframe_info = target_traceframe_info ();
3933 return current_traceframe_info.get ();
3936 /* If the target supports the query, return in RESULT the set of
3937 collected memory in the current traceframe, found within the LEN
3938 bytes range starting at MEMADDR. Returns true if the target
3939 supports the query, otherwise returns false, and RESULT is left
3943 traceframe_available_memory (std::vector<mem_range> *result,
3944 CORE_ADDR memaddr, ULONGEST len)
3946 struct traceframe_info *info = get_traceframe_info ();
3952 for (mem_range &r : info->memory)
3953 if (mem_ranges_overlap (r.start, r.length, memaddr, len))
3955 ULONGEST lo1, hi1, lo2, hi2;
3958 hi1 = memaddr + len;
3961 hi2 = r.start + r.length;
3963 CORE_ADDR start = std::max (lo1, lo2);
3964 int length = std::min (hi1, hi2) - start;
3966 result->emplace_back (start, length);
3969 normalize_mem_ranges (result);
3976 /* Implementation of `sdata' variable. */
3978 static const struct internalvar_funcs sdata_funcs =
3985 /* module initialization */
3987 _initialize_tracepoint (void)
3989 struct cmd_list_element *c;
3991 /* Explicitly create without lookup, since that tries to create a
3992 value with a void typed value, and when we get here, gdbarch
3993 isn't initialized yet. At this point, we're quite sure there
3994 isn't another convenience variable of the same name. */
3995 create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL);
3997 traceframe_number = -1;
3998 tracepoint_number = -1;
4000 add_info ("scope", info_scope_command,
4001 _("List the variables local to a scope"));
4003 add_cmd ("tracepoints", class_trace,
4004 _("Tracing of program execution without stopping the program."),
4007 add_com ("tdump", class_trace, tdump_command,
4008 _("Print everything collected at the current tracepoint."));
4010 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
4011 Define a trace state variable.\n\
4012 Argument is a $-prefixed name, optionally followed\n\
4013 by '=' and an expression that sets the initial value\n\
4014 at the start of tracing."));
4015 set_cmd_completer (c, expression_completer);
4017 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
4018 Delete one or more trace state variables.\n\
4019 Arguments are the names of the variables to delete.\n\
4020 If no arguments are supplied, delete all variables."), &deletelist);
4021 /* FIXME add a trace variable completer. */
4023 add_info ("tvariables", info_tvariables_command, _("\
4024 Status of trace state variables and their values.\n\
4027 add_info ("static-tracepoint-markers",
4028 info_static_tracepoint_markers_command, _("\
4029 List target static tracepoints markers.\n\
4032 add_prefix_cmd ("tfind", class_trace, tfind_command, _("\
4033 Select a trace frame;\n\
4034 No argument means forward by one frame; '-' means backward by one frame."),
4035 &tfindlist, "tfind ", 1, &cmdlist);
4037 add_cmd ("outside", class_trace, tfind_outside_command, _("\
4038 Select a trace frame whose PC is outside the given range (exclusive).\n\
4039 Usage: tfind outside addr1, addr2"),
4042 add_cmd ("range", class_trace, tfind_range_command, _("\
4043 Select a trace frame whose PC is in the given range (inclusive).\n\
4044 Usage: tfind range addr1,addr2"),
4047 add_cmd ("line", class_trace, tfind_line_command, _("\
4048 Select a trace frame by source line.\n\
4049 Argument can be a line number (with optional source file),\n\
4050 a function name, or '*' followed by an address.\n\
4051 Default argument is 'the next source line that was traced'."),
4054 add_cmd ("tracepoint", class_trace, tfind_tracepoint_command, _("\
4055 Select a trace frame by tracepoint number.\n\
4056 Default is the tracepoint for the current trace frame."),
4059 add_cmd ("pc", class_trace, tfind_pc_command, _("\
4060 Select a trace frame by PC.\n\
4061 Default is the current PC, or the PC of the current trace frame."),
4064 add_cmd ("end", class_trace, tfind_end_command, _("\
4065 De-select any trace frame and resume 'live' debugging."),
4068 add_alias_cmd ("none", "end", class_trace, 0, &tfindlist);
4070 add_cmd ("start", class_trace, tfind_start_command,
4071 _("Select the first trace frame in the trace buffer."),
4074 add_com ("tstatus", class_trace, tstatus_command,
4075 _("Display the status of the current trace data collection."));
4077 add_com ("tstop", class_trace, tstop_command, _("\
4078 Stop trace data collection.\n\
4079 Usage: tstop [ <notes> ... ]\n\
4080 Any arguments supplied are recorded with the trace as a stop reason and\n\
4081 reported by tstatus (if the target supports trace notes)."));
4083 add_com ("tstart", class_trace, tstart_command, _("\
4084 Start trace data collection.\n\
4085 Usage: tstart [ <notes> ... ]\n\
4086 Any arguments supplied are recorded with the trace as a note and\n\
4087 reported by tstatus (if the target supports trace notes)."));
4089 add_com ("end", class_trace, end_actions_pseudocommand, _("\
4090 Ends a list of commands or actions.\n\
4091 Several GDB commands allow you to enter a list of commands or actions.\n\
4092 Entering \"end\" on a line by itself is the normal way to terminate\n\
4094 Note: the \"end\" command cannot be used at the gdb prompt."));
4096 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
4097 Specify single-stepping behavior at a tracepoint.\n\
4098 Argument is number of instructions to trace in single-step mode\n\
4099 following the tracepoint. This command is normally followed by\n\
4100 one or more \"collect\" commands, to specify what to collect\n\
4101 while single-stepping.\n\n\
4102 Note: this command can only be used in a tracepoint \"actions\" list."));
4104 add_com_alias ("ws", "while-stepping", class_alias, 0);
4105 add_com_alias ("stepping", "while-stepping", class_alias, 0);
4107 add_com ("collect", class_trace, collect_pseudocommand, _("\
4108 Specify one or more data items to be collected at a tracepoint.\n\
4109 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
4110 collect all data (variables, registers) referenced by that expression.\n\
4111 Also accepts the following special arguments:\n\
4112 $regs -- all registers.\n\
4113 $args -- all function arguments.\n\
4114 $locals -- all variables local to the block/function scope.\n\
4115 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
4116 Note: this command can only be used in a tracepoint \"actions\" list."));
4118 add_com ("teval", class_trace, teval_pseudocommand, _("\
4119 Specify one or more expressions to be evaluated at a tracepoint.\n\
4120 Accepts a comma-separated list of (one or more) expressions.\n\
4121 The result of each evaluation will be discarded.\n\
4122 Note: this command can only be used in a tracepoint \"actions\" list."));
4124 add_com ("actions", class_trace, actions_command, _("\
4125 Specify the actions to be taken at a tracepoint.\n\
4126 Tracepoint actions may include collecting of specified data,\n\
4127 single-stepping, or enabling/disabling other tracepoints,\n\
4128 depending on target's capabilities."));
4130 default_collect = xstrdup ("");
4131 add_setshow_string_cmd ("default-collect", class_trace,
4132 &default_collect, _("\
4133 Set the list of expressions to collect by default"), _("\
4134 Show the list of expressions to collect by default"), NULL,
4136 &setlist, &showlist);
4138 add_setshow_boolean_cmd ("disconnected-tracing", no_class,
4139 &disconnected_tracing, _("\
4140 Set whether tracing continues after GDB disconnects."), _("\
4141 Show whether tracing continues after GDB disconnects."), _("\
4142 Use this to continue a tracing run even if GDB disconnects\n\
4143 or detaches from the target. You can reconnect later and look at\n\
4144 trace data collected in the meantime."),
4145 set_disconnected_tracing,
4150 add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
4151 &circular_trace_buffer, _("\
4152 Set target's use of circular trace buffer."), _("\
4153 Show target's use of circular trace buffer."), _("\
4154 Use this to make the trace buffer into a circular buffer,\n\
4155 which will discard traceframes (oldest first) instead of filling\n\
4156 up and stopping the trace run."),
4157 set_circular_trace_buffer,
4162 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class,
4163 &trace_buffer_size, _("\
4164 Set requested size of trace buffer."), _("\
4165 Show requested size of trace buffer."), _("\
4166 Use this to choose a size for the trace buffer. Some targets\n\
4167 may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
4168 disables any attempt to set the buffer size and lets the target choose."),
4169 set_trace_buffer_size, NULL,
4170 &setlist, &showlist);
4172 add_setshow_string_cmd ("trace-user", class_trace,
4174 Set the user name to use for current and future trace runs"), _("\
4175 Show the user name to use for current and future trace runs"), NULL,
4176 set_trace_user, NULL,
4177 &setlist, &showlist);
4179 add_setshow_string_cmd ("trace-notes", class_trace,
4181 Set notes string to use for current and future trace runs"), _("\
4182 Show the notes string to use for current and future trace runs"), NULL,
4183 set_trace_notes, NULL,
4184 &setlist, &showlist);
4186 add_setshow_string_cmd ("trace-stop-notes", class_trace,
4187 &trace_stop_notes, _("\
4188 Set notes string to use for future tstop commands"), _("\
4189 Show the notes string to use for future tstop commands"), NULL,
4190 set_trace_stop_notes, NULL,
4191 &setlist, &showlist);