Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / tracepoint.c
blob1586a043af69a1c72d03ea06a2f634d43f7e8bc3
1 /* Tracing functionality for remote targets in custom GDB protocol
3 Copyright (C) 1997-2024 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/>. */
20 #include "arch-utils.h"
21 #include "symtab.h"
22 #include "frame.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "gdbcmd.h"
26 #include "value.h"
27 #include "target.h"
28 #include "target-dcache.h"
29 #include "language.h"
30 #include "inferior.h"
31 #include "breakpoint.h"
32 #include "tracepoint.h"
33 #include "linespec.h"
34 #include "regcache.h"
35 #include "completer.h"
36 #include "block.h"
37 #include "dictionary.h"
38 #include "observable.h"
39 #include "user-regs.h"
40 #include "valprint.h"
41 #include "gdbcore.h"
42 #include "objfiles.h"
43 #include "filenames.h"
44 #include "gdbthread.h"
45 #include "stack.h"
46 #include "remote.h"
47 #include "source.h"
48 #include "ax.h"
49 #include "ax-gdb.h"
50 #include "memrange.h"
51 #include "cli/cli-utils.h"
52 #include "probe.h"
53 #include "gdbsupport/filestuff.h"
54 #include "gdbsupport/rsp-low.h"
55 #include "tracefile.h"
56 #include "location.h"
57 #include <algorithm>
58 #include "cli/cli-style.h"
59 #include "expop.h"
60 #include "gdbsupport/buildargv.h"
61 #include "interps.h"
63 #include <unistd.h>
65 /* Maximum length of an agent aexpression.
66 This accounts for the fact that packets are limited to 400 bytes
67 (which includes everything -- including the checksum), and assumes
68 the worst case of maximum length for each of the pieces of a
69 continuation packet.
71 NOTE: expressions get bin2hex'ed otherwise this would be twice as
72 large. (400 - 31)/2 == 184 */
73 #define MAX_AGENT_EXPR_LEN 184
75 /*
76 Tracepoint.c:
78 This module defines the following debugger commands:
79 trace : set a tracepoint on a function, line, or address.
80 info trace : list all debugger-defined tracepoints.
81 delete trace : delete one or more tracepoints.
82 enable trace : enable one or more tracepoints.
83 disable trace : disable one or more tracepoints.
84 actions : specify actions to be taken at a tracepoint.
85 passcount : specify a pass count for a tracepoint.
86 tstart : start a trace experiment.
87 tstop : stop a trace experiment.
88 tstatus : query the status of a trace experiment.
89 tfind : find a trace frame in the trace buffer.
90 tdump : print everything collected at the current tracepoint.
91 save-tracepoints : write tracepoint setup into a file.
93 This module defines the following user-visible debugger variables:
94 $trace_frame : sequence number of trace frame currently being debugged.
95 $trace_line : source line of trace frame currently being debugged.
96 $trace_file : source file of trace frame currently being debugged.
97 $tracepoint : tracepoint number of trace frame currently being debugged.
101 /* ======= Important global variables: ======= */
103 /* The list of all trace state variables. We don't retain pointers to
104 any of these for any reason - API is by name or number only - so it
105 works to have a vector of objects. */
107 static std::vector<trace_state_variable> tvariables;
109 /* The next integer to assign to a variable. */
111 static int next_tsv_number = 1;
113 /* Number of last traceframe collected. */
114 static int traceframe_number;
116 /* Tracepoint for last traceframe collected. */
117 static int tracepoint_number;
119 /* The traceframe info of the current traceframe. NULL if we haven't
120 yet attempted to fetch it, or if the target does not support
121 fetching this object, or if we're not inspecting a traceframe
122 presently. */
123 static traceframe_info_up current_traceframe_info;
125 /* Tracing command lists. */
126 static struct cmd_list_element *tfindlist;
128 /* List of expressions to collect by default at each tracepoint hit. */
129 std::string default_collect;
131 static bool disconnected_tracing;
133 /* This variable controls whether we ask the target for a linear or
134 circular trace buffer. */
136 static bool circular_trace_buffer;
138 /* This variable is the requested trace buffer size, or -1 to indicate
139 that we don't care and leave it up to the target to set a size. */
141 static int trace_buffer_size = -1;
143 /* Textual notes applying to the current and/or future trace runs. */
145 static std::string trace_user;
147 /* Textual notes applying to the current and/or future trace runs. */
149 static std::string trace_notes;
151 /* Textual notes applying to the stopping of a trace. */
153 static std::string trace_stop_notes;
155 /* support routines */
157 struct collection_list;
159 static counted_command_line all_tracepoint_actions (tracepoint *);
161 static struct trace_status trace_status;
163 const char *stop_reason_names[] = {
164 "tunknown",
165 "tnotrun",
166 "tstop",
167 "tfull",
168 "tdisconnected",
169 "tpasscount",
170 "terror"
173 struct trace_status *
174 current_trace_status (void)
176 return &trace_status;
179 /* Free and clear the traceframe info cache of the current
180 traceframe. */
182 static void
183 clear_traceframe_info (void)
185 current_traceframe_info = NULL;
188 /* Set traceframe number to NUM. */
189 static void
190 set_traceframe_num (int num)
192 traceframe_number = num;
193 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
196 /* Set tracepoint number to NUM. */
197 static void
198 set_tracepoint_num (int num)
200 tracepoint_number = num;
201 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
204 /* Set externally visible debug variables for querying/printing
205 the traceframe context (line, function, file). */
207 static void
208 set_traceframe_context (const frame_info_ptr &trace_frame)
210 CORE_ADDR trace_pc;
211 struct symbol *traceframe_fun;
212 symtab_and_line traceframe_sal;
214 /* Save as globals for internal use. */
215 if (trace_frame != NULL
216 && get_frame_pc_if_available (trace_frame, &trace_pc))
218 traceframe_sal = find_pc_line (trace_pc, 0);
219 traceframe_fun = find_pc_function (trace_pc);
221 /* Save linenumber as "$trace_line", a debugger variable visible to
222 users. */
223 set_internalvar_integer (lookup_internalvar ("trace_line"),
224 traceframe_sal.line);
226 else
228 traceframe_fun = NULL;
229 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
232 /* Save func name as "$trace_func", a debugger variable visible to
233 users. */
234 if (traceframe_fun == NULL
235 || traceframe_fun->linkage_name () == NULL)
236 clear_internalvar (lookup_internalvar ("trace_func"));
237 else
238 set_internalvar_string (lookup_internalvar ("trace_func"),
239 traceframe_fun->linkage_name ());
241 /* Save file name as "$trace_file", a debugger variable visible to
242 users. */
243 if (traceframe_sal.symtab == NULL)
244 clear_internalvar (lookup_internalvar ("trace_file"));
245 else
246 set_internalvar_string (lookup_internalvar ("trace_file"),
247 symtab_to_filename_for_display (traceframe_sal.symtab));
250 /* Create a new trace state variable with the given name. */
252 struct trace_state_variable *
253 create_trace_state_variable (const char *name)
255 return &tvariables.emplace_back (name, next_tsv_number++);
258 /* Look for a trace state variable of the given name. */
260 struct trace_state_variable *
261 find_trace_state_variable (const char *name)
263 for (trace_state_variable &tsv : tvariables)
264 if (tsv.name == name)
265 return &tsv;
267 return NULL;
270 /* Look for a trace state variable of the given number. Return NULL if
271 not found. */
273 struct trace_state_variable *
274 find_trace_state_variable_by_number (int number)
276 for (trace_state_variable &tsv : tvariables)
277 if (tsv.number == number)
278 return &tsv;
280 return NULL;
283 static void
284 delete_trace_state_variable (const char *name)
286 for (auto it = tvariables.begin (); it != tvariables.end (); it++)
287 if (it->name == name)
289 interps_notify_tsv_deleted (&*it);
290 tvariables.erase (it);
291 return;
294 warning (_("No trace variable named \"$%s\", not deleting"), name);
297 /* Throws an error if NAME is not valid syntax for a trace state
298 variable's name. */
300 void
301 validate_trace_state_variable_name (const char *name)
303 const char *p;
305 if (*name == '\0')
306 error (_("Must supply a non-empty variable name"));
308 /* All digits in the name is reserved for value history
309 references. */
310 for (p = name; isdigit (*p); p++)
312 if (*p == '\0')
313 error (_("$%s is not a valid trace state variable name"), name);
315 for (p = name; isalnum (*p) || *p == '_'; p++)
317 if (*p != '\0')
318 error (_("$%s is not a valid trace state variable name"), name);
321 /* The 'tvariable' command collects a name and optional expression to
322 evaluate into an initial value. */
324 static void
325 trace_variable_command (const char *args, int from_tty)
327 LONGEST initval = 0;
328 struct trace_state_variable *tsv;
329 const char *name_start, *p;
331 if (!args || !*args)
332 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
334 /* Only allow two syntaxes; "$name" and "$name=value". */
335 p = skip_spaces (args);
337 if (*p++ != '$')
338 error (_("Name of trace variable should start with '$'"));
340 name_start = p;
341 while (isalnum (*p) || *p == '_')
342 p++;
343 std::string name (name_start, p - name_start);
345 p = skip_spaces (p);
346 if (*p != '=' && *p != '\0')
347 error (_("Syntax must be $NAME [ = EXPR ]"));
349 validate_trace_state_variable_name (name.c_str ());
351 if (*p == '=')
352 initval = value_as_long (parse_and_eval (++p));
354 /* If the variable already exists, just change its initial value. */
355 tsv = find_trace_state_variable (name.c_str ());
356 if (tsv)
358 if (tsv->initial_value != initval)
360 tsv->initial_value = initval;
361 interps_notify_tsv_modified (tsv);
363 gdb_printf (_("Trace state variable $%s "
364 "now has initial value %s.\n"),
365 tsv->name.c_str (), plongest (tsv->initial_value));
366 return;
369 /* Create a new variable. */
370 tsv = create_trace_state_variable (name.c_str ());
371 tsv->initial_value = initval;
373 interps_notify_tsv_created (tsv);
375 gdb_printf (_("Trace state variable $%s "
376 "created, with initial value %s.\n"),
377 tsv->name.c_str (), plongest (tsv->initial_value));
380 static void
381 delete_trace_variable_command (const char *args, int from_tty)
383 if (args == NULL)
385 if (query (_("Delete all trace state variables? ")))
386 tvariables.clear ();
387 dont_repeat ();
388 interps_notify_tsv_deleted (nullptr);
389 return;
392 gdb_argv argv (args);
394 for (char *arg : argv)
396 if (*arg == '$')
397 delete_trace_state_variable (arg + 1);
398 else
399 warning (_("Name \"%s\" not prefixed with '$', ignoring"), arg);
402 dont_repeat ();
405 void
406 tvariables_info_1 (void)
408 struct ui_out *uiout = current_uiout;
410 /* Try to acquire values from the target. */
411 for (trace_state_variable &tsv : tvariables)
412 tsv.value_known
413 = target_get_trace_state_variable_value (tsv.number, &tsv.value);
416 ui_out_emit_table table_emitter (uiout, 3, tvariables.size (),
417 "trace-variables");
418 uiout->table_header (15, ui_left, "name", "Name");
419 uiout->table_header (11, ui_left, "initial", "Initial");
420 uiout->table_header (11, ui_left, "current", "Current");
422 uiout->table_body ();
424 for (const trace_state_variable &tsv : tvariables)
426 const char *c;
428 ui_out_emit_tuple tuple_emitter (uiout, "variable");
430 uiout->field_string ("name", std::string ("$") + tsv.name);
431 uiout->field_string ("initial", plongest (tsv.initial_value));
433 ui_file_style style;
434 if (tsv.value_known)
435 c = plongest (tsv.value);
436 else if (uiout->is_mi_like_p ())
437 /* For MI, we prefer not to use magic string constants, but rather
438 omit the field completely. The difference between unknown and
439 undefined does not seem important enough to represent. */
440 c = NULL;
441 else if (current_trace_status ()->running || traceframe_number >= 0)
443 /* The value is/was defined, but we don't have it. */
444 c = "<unknown>";
445 style = metadata_style.style ();
447 else
449 /* It is not meaningful to ask about the value. */
450 c = "<undefined>";
451 style = metadata_style.style ();
453 if (c)
454 uiout->field_string ("current", c, style);
455 uiout->text ("\n");
459 if (tvariables.empty ())
460 uiout->text (_("No trace state variables.\n"));
463 /* List all the trace state variables. */
465 static void
466 info_tvariables_command (const char *args, int from_tty)
468 tvariables_info_1 ();
471 /* Stash definitions of tsvs into the given file. */
473 void
474 save_trace_state_variables (struct ui_file *fp)
476 for (const trace_state_variable &tsv : tvariables)
478 gdb_printf (fp, "tvariable $%s", tsv.name.c_str ());
479 if (tsv.initial_value)
480 gdb_printf (fp, " = %s", plongest (tsv.initial_value));
481 gdb_printf (fp, "\n");
485 /* ACTIONS functions: */
487 /* The three functions:
488 collect_pseudocommand,
489 while_stepping_pseudocommand, and
490 end_actions_pseudocommand
491 are placeholders for "commands" that are actually ONLY to be used
492 within a tracepoint action list. If the actual function is ever called,
493 it means that somebody issued the "command" at the top level,
494 which is always an error. */
496 static void
497 end_actions_pseudocommand (const char *args, int from_tty)
499 error (_("This command cannot be used at the top level."));
502 static void
503 while_stepping_pseudocommand (const char *args, int from_tty)
505 error (_("This command can only be used in a tracepoint actions list."));
508 static void
509 collect_pseudocommand (const char *args, int from_tty)
511 error (_("This command can only be used in a tracepoint actions list."));
514 static void
515 teval_pseudocommand (const char *args, int from_tty)
517 error (_("This command can only be used in a tracepoint actions list."));
520 /* Parse any collection options, such as /s for strings. */
522 const char *
523 decode_agent_options (const char *exp, int *trace_string)
525 struct value_print_options opts;
527 *trace_string = 0;
529 if (*exp != '/')
530 return exp;
532 /* Call this to borrow the print elements default for collection
533 size. */
534 get_user_print_options (&opts);
536 exp++;
537 if (*exp == 's')
539 if (target_supports_string_tracing ())
541 /* Allow an optional decimal number giving an explicit maximum
542 string length, defaulting it to the "print characters" value;
543 so "collect/s80 mystr" gets at most 80 bytes of string. */
544 *trace_string = get_print_max_chars (&opts);
545 exp++;
546 if (*exp >= '0' && *exp <= '9')
547 *trace_string = atoi (exp);
548 while (*exp >= '0' && *exp <= '9')
549 exp++;
551 else
552 error (_("Target does not support \"/s\" option for string tracing."));
554 else
555 error (_("Undefined collection format \"%c\"."), *exp);
557 exp = skip_spaces (exp);
559 return exp;
562 /* Enter a list of actions for a tracepoint. */
563 static void
564 actions_command (const char *args, int from_tty)
566 struct tracepoint *t;
568 t = get_tracepoint_by_number (&args, NULL);
569 if (t)
571 std::string tmpbuf =
572 string_printf ("Enter actions for tracepoint %d, one per line.",
573 t->number);
575 counted_command_line l = read_command_lines (tmpbuf.c_str (),
576 from_tty, 1,
577 [=] (const char *line)
579 validate_actionline (line, t);
581 breakpoint_set_commands (t, std::move (l));
583 /* else just return */
586 /* Report the results of checking the agent expression, as errors or
587 internal errors. */
589 static void
590 report_agent_reqs_errors (struct agent_expr *aexpr)
592 /* All of the "flaws" are serious bytecode generation issues that
593 should never occur. */
594 if (aexpr->flaw != agent_flaw_none)
595 internal_error (_("expression is malformed"));
597 /* If analysis shows a stack underflow, GDB must have done something
598 badly wrong in its bytecode generation. */
599 if (aexpr->min_height < 0)
600 internal_error (_("expression has min height < 0"));
602 /* Issue this error if the stack is predicted to get too deep. The
603 limit is rather arbitrary; a better scheme might be for the
604 target to report how much stack it will have available. The
605 depth roughly corresponds to parenthesization, so a limit of 20
606 amounts to 20 levels of expression nesting, which is actually
607 a pretty big hairy expression. */
608 if (aexpr->max_height > 20)
609 error (_("Expression is too complicated."));
612 /* Call ax_reqs on AEXPR and raise an error if something is wrong. */
614 static void
615 finalize_tracepoint_aexpr (struct agent_expr *aexpr)
617 ax_reqs (aexpr);
619 if (aexpr->buf.size () > MAX_AGENT_EXPR_LEN)
620 error (_("Expression is too complicated."));
622 report_agent_reqs_errors (aexpr);
625 /* worker function */
626 void
627 validate_actionline (const char *line, tracepoint *t)
629 struct cmd_list_element *c;
630 const char *tmp_p;
631 const char *p;
633 /* If EOF is typed, *line is NULL. */
634 if (line == NULL)
635 return;
637 p = skip_spaces (line);
639 /* Symbol lookup etc. */
640 if (*p == '\0') /* empty line: just prompt for another line. */
641 return;
643 if (*p == '#') /* comment line */
644 return;
646 c = lookup_cmd (&p, cmdlist, "", NULL, -1, 1);
647 if (c == 0)
648 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
650 if (cmd_simple_func_eq (c, collect_pseudocommand))
652 int trace_string = 0;
654 if (*p == '/')
655 p = decode_agent_options (p, &trace_string);
658 { /* Repeat over a comma-separated list. */
659 QUIT; /* Allow user to bail out with ^C. */
660 p = skip_spaces (p);
662 if (*p == '$') /* Look for special pseudo-symbols. */
664 if (0 == strncasecmp ("reg", p + 1, 3)
665 || 0 == strncasecmp ("arg", p + 1, 3)
666 || 0 == strncasecmp ("loc", p + 1, 3)
667 || 0 == strncasecmp ("_ret", p + 1, 4)
668 || 0 == strncasecmp ("_sdata", p + 1, 6))
670 p = strchr (p, ',');
671 continue;
673 /* else fall thru, treat p as an expression and parse it! */
675 tmp_p = p;
676 for (bp_location &loc : t->locations ())
678 p = tmp_p;
679 expression_up exp = parse_exp_1 (&p, loc.address,
680 block_for_pc (loc.address),
681 PARSER_COMMA_TERMINATES);
683 if (exp->first_opcode () == OP_VAR_VALUE)
685 symbol *sym;
686 expr::var_value_operation *vvop
687 = (gdb::checked_static_cast<expr::var_value_operation *>
688 (exp->op.get ()));
689 sym = vvop->get_symbol ();
691 if (sym->aclass () == LOC_CONST)
693 error (_("constant `%s' (value %s) "
694 "will not be collected."),
695 sym->print_name (),
696 plongest (sym->value_longest ()));
698 else if (sym->aclass () == LOC_OPTIMIZED_OUT)
700 error (_("`%s' is optimized away "
701 "and cannot be collected."),
702 sym->print_name ());
706 /* We have something to collect, make sure that the expr to
707 bytecode translator can handle it and that it's not too
708 long. */
709 agent_expr_up aexpr = gen_trace_for_expr (loc.address,
710 exp.get (),
711 trace_string);
713 finalize_tracepoint_aexpr (aexpr.get ());
716 while (p && *p++ == ',');
719 else if (cmd_simple_func_eq (c, teval_pseudocommand))
722 { /* Repeat over a comma-separated list. */
723 QUIT; /* Allow user to bail out with ^C. */
724 p = skip_spaces (p);
726 tmp_p = p;
727 for (bp_location &loc : t->locations ())
729 p = tmp_p;
731 /* Only expressions are allowed for this action. */
732 expression_up exp = parse_exp_1 (&p, loc.address,
733 block_for_pc (loc.address),
734 PARSER_COMMA_TERMINATES);
736 /* We have something to evaluate, make sure that the expr to
737 bytecode translator can handle it and that it's not too
738 long. */
739 agent_expr_up aexpr = gen_eval_for_expr (loc.address, exp.get ());
741 finalize_tracepoint_aexpr (aexpr.get ());
744 while (p && *p++ == ',');
747 else if (cmd_simple_func_eq (c, while_stepping_pseudocommand))
749 char *endp;
751 p = skip_spaces (p);
752 t->step_count = strtol (p, &endp, 0);
753 if (endp == p || t->step_count == 0)
754 error (_("while-stepping step count `%s' is malformed."), line);
755 p = endp;
758 else if (cmd_simple_func_eq (c, end_actions_pseudocommand))
761 else
762 error (_("`%s' is not a supported tracepoint action."), line);
765 enum {
766 memrange_absolute = -1
769 /* MEMRANGE functions: */
771 /* Compare memranges for std::sort. */
773 static bool
774 memrange_comp (const memrange &a, const memrange &b)
776 if (a.type == b.type)
778 if (a.type == memrange_absolute)
779 return (bfd_vma) a.start < (bfd_vma) b.start;
780 else
781 return a.start < b.start;
784 return a.type < b.type;
787 /* Sort the memrange list using std::sort, and merge adjacent memranges. */
789 static void
790 memrange_sortmerge (std::vector<memrange> &memranges)
792 if (!memranges.empty ())
794 int a, b;
796 std::sort (memranges.begin (), memranges.end (), memrange_comp);
798 for (a = 0, b = 1; b < memranges.size (); b++)
800 /* If memrange b overlaps or is adjacent to memrange a,
801 merge them. */
802 if (memranges[a].type == memranges[b].type
803 && memranges[b].start <= memranges[a].end)
805 if (memranges[b].end > memranges[a].end)
806 memranges[a].end = memranges[b].end;
807 continue; /* next b, same a */
809 a++; /* next a */
810 if (a != b)
811 memranges[a] = memranges[b];
813 memranges.resize (a + 1);
817 /* Add remote register number REGNO to the collection list mask. */
819 void
820 collection_list::add_remote_register (unsigned int regno)
822 if (info_verbose)
823 gdb_printf ("collect register %d\n", regno);
825 m_regs_mask.at (regno / 8) |= 1 << (regno % 8);
828 /* Add all the registers from the mask in AEXPR to the mask in the
829 collection list. Registers in the AEXPR mask are already remote
830 register numbers. */
832 void
833 collection_list::add_ax_registers (struct agent_expr *aexpr)
835 for (int ndx1 = 0; ndx1 < aexpr->reg_mask.size (); ndx1++)
837 QUIT; /* Allow user to bail out with ^C. */
838 if (aexpr->reg_mask[ndx1])
840 /* It's used -- record it. */
841 add_remote_register (ndx1);
846 /* If REGNO is raw, add its corresponding remote register number to
847 the mask. If REGNO is a pseudo-register, figure out the necessary
848 registers using a temporary agent expression, and add it to the
849 list if it needs more than just a mask. */
851 void
852 collection_list::add_local_register (struct gdbarch *gdbarch,
853 unsigned int regno,
854 CORE_ADDR scope)
856 if (regno < gdbarch_num_regs (gdbarch))
858 int remote_regno = gdbarch_remote_register_number (gdbarch, regno);
860 if (remote_regno < 0)
861 error (_("Can't collect register %d"), regno);
863 add_remote_register (remote_regno);
865 else
867 agent_expr_up aexpr (new agent_expr (gdbarch, scope));
869 ax_reg_mask (aexpr.get (), regno);
871 finalize_tracepoint_aexpr (aexpr.get ());
873 add_ax_registers (aexpr.get ());
875 /* Usually ax_reg_mask for a pseudo-regiser only sets the
876 corresponding raw registers in the ax mask, but if this isn't
877 the case add the expression that is generated to the
878 collection list. */
879 if (aexpr->buf.size () > 0)
880 add_aexpr (std::move (aexpr));
884 /* Add a memrange to a collection list. */
886 void
887 collection_list::add_memrange (struct gdbarch *gdbarch,
888 int type, bfd_signed_vma base,
889 unsigned long len, CORE_ADDR scope)
891 if (info_verbose)
892 gdb_printf ("(%d,%s,%ld)\n", type, paddress (gdbarch, base), len);
894 /* type: memrange_absolute == memory, other n == basereg */
895 /* base: addr if memory, offset if reg relative. */
896 /* len: we actually save end (base + len) for convenience */
897 m_memranges.emplace_back (type, base, base + len);
899 if (type != memrange_absolute) /* Better collect the base register! */
900 add_local_register (gdbarch, type, scope);
903 /* Add a symbol to a collection list. */
905 void
906 collection_list::collect_symbol (struct symbol *sym,
907 struct gdbarch *gdbarch,
908 long frame_regno, long frame_offset,
909 CORE_ADDR scope,
910 int trace_string)
912 unsigned long len;
913 unsigned int reg;
914 bfd_signed_vma offset;
915 int treat_as_expr = 0;
917 len = check_typedef (sym->type ())->length ();
918 switch (sym->aclass ())
920 default:
921 gdb_printf ("%s: don't know symbol class %d\n",
922 sym->print_name (), sym->aclass ());
923 break;
924 case LOC_CONST:
925 gdb_printf ("constant %s (value %s) will not be collected.\n",
926 sym->print_name (), plongest (sym->value_longest ()));
927 break;
928 case LOC_STATIC:
929 offset = sym->value_address ();
930 if (info_verbose)
932 gdb_printf ("LOC_STATIC %s: collect %ld bytes at %s.\n",
933 sym->print_name (), len,
934 paddress (gdbarch, offset));
936 /* A struct may be a C++ class with static fields, go to general
937 expression handling. */
938 if (sym->type ()->code () == TYPE_CODE_STRUCT)
939 treat_as_expr = 1;
940 else
941 add_memrange (gdbarch, memrange_absolute, offset, len, scope);
942 break;
943 case LOC_REGISTER:
944 reg = sym->register_ops ()->register_number (sym, gdbarch);
945 if (info_verbose)
946 gdb_printf ("LOC_REG[parm] %s: ", sym->print_name ());
947 add_local_register (gdbarch, reg, scope);
948 /* Check for doubles stored in two registers. */
949 /* FIXME: how about larger types stored in 3 or more regs? */
950 if (sym->type ()->code () == TYPE_CODE_FLT &&
951 len > register_size (gdbarch, reg))
952 add_local_register (gdbarch, reg + 1, scope);
953 break;
954 case LOC_REF_ARG:
955 gdb_printf ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
956 gdb_printf (" (will not collect %s)\n", sym->print_name ());
957 break;
958 case LOC_ARG:
959 reg = frame_regno;
960 offset = frame_offset + sym->value_longest ();
961 if (info_verbose)
963 gdb_printf ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
964 " from frame ptr reg %d\n", sym->print_name (), len,
965 paddress (gdbarch, offset), reg);
967 add_memrange (gdbarch, reg, offset, len, scope);
968 break;
969 case LOC_REGPARM_ADDR:
970 reg = sym->value_longest ();
971 offset = 0;
972 if (info_verbose)
974 gdb_printf ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s"
975 " from reg %d\n", sym->print_name (), len,
976 paddress (gdbarch, offset), reg);
978 add_memrange (gdbarch, reg, offset, len, scope);
979 break;
980 case LOC_LOCAL:
981 reg = frame_regno;
982 offset = frame_offset + sym->value_longest ();
983 if (info_verbose)
985 gdb_printf ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
986 " from frame ptr reg %d\n", sym->print_name (), len,
987 paddress (gdbarch, offset), reg);
989 add_memrange (gdbarch, reg, offset, len, scope);
990 break;
992 case LOC_UNRESOLVED:
993 treat_as_expr = 1;
994 break;
996 case LOC_OPTIMIZED_OUT:
997 gdb_printf ("%s has been optimized out of existence.\n",
998 sym->print_name ());
999 break;
1001 case LOC_COMPUTED:
1002 treat_as_expr = 1;
1003 break;
1006 /* Expressions are the most general case. */
1007 if (treat_as_expr)
1009 agent_expr_up aexpr = gen_trace_for_var (scope, gdbarch,
1010 sym, trace_string);
1012 /* It can happen that the symbol is recorded as a computed
1013 location, but it's been optimized away and doesn't actually
1014 have a location expression. */
1015 if (!aexpr)
1017 gdb_printf ("%s has been optimized out of existence.\n",
1018 sym->print_name ());
1019 return;
1022 finalize_tracepoint_aexpr (aexpr.get ());
1024 /* Take care of the registers. */
1025 add_ax_registers (aexpr.get ());
1027 add_aexpr (std::move (aexpr));
1031 void
1032 collection_list::add_wholly_collected (const char *print_name)
1034 m_wholly_collected.push_back (print_name);
1037 /* Add all locals (or args) symbols to collection list. */
1039 void
1040 collection_list::add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc,
1041 long frame_regno, long frame_offset, int type,
1042 int trace_string)
1044 const struct block *block;
1045 int count = 0;
1047 auto do_collect_symbol = [&] (const char *print_name,
1048 struct symbol *sym)
1050 collect_symbol (sym, gdbarch, frame_regno,
1051 frame_offset, pc, trace_string);
1052 count++;
1053 add_wholly_collected (print_name);
1056 if (type == 'L')
1058 block = block_for_pc (pc);
1059 if (block == NULL)
1061 warning (_("Can't collect locals; "
1062 "no symbol table info available.\n"));
1063 return;
1066 iterate_over_block_local_vars (block, do_collect_symbol);
1067 if (count == 0)
1068 warning (_("No locals found in scope."));
1070 else
1072 CORE_ADDR fn_pc = get_pc_function_start (pc);
1073 block = block_for_pc (fn_pc);
1074 if (block == NULL)
1076 warning (_("Can't collect args; no symbol table info available."));
1077 return;
1080 iterate_over_block_arg_vars (block, do_collect_symbol);
1081 if (count == 0)
1082 warning (_("No args found in scope."));
1086 void
1087 collection_list::add_static_trace_data ()
1089 if (info_verbose)
1090 gdb_printf ("collect static trace data\n");
1091 m_strace_data = true;
1094 collection_list::collection_list ()
1095 : m_strace_data (false)
1097 int max_remote_regno = 0;
1098 for (int i = 0; i < gdbarch_num_regs (current_inferior ()->arch ()); i++)
1100 int remote_regno = (gdbarch_remote_register_number
1101 (current_inferior ()->arch (), i));
1103 if (remote_regno >= 0 && remote_regno > max_remote_regno)
1104 max_remote_regno = remote_regno;
1107 m_regs_mask.resize ((max_remote_regno / 8) + 1);
1109 m_memranges.reserve (128);
1110 m_aexprs.reserve (128);
1113 /* Reduce a collection list to string form (for gdb protocol). */
1115 std::vector<std::string>
1116 collection_list::stringify ()
1118 gdb::char_vector temp_buf (2048);
1120 int count;
1121 char *end;
1122 long i;
1123 std::vector<std::string> str_list;
1125 if (m_strace_data)
1127 if (info_verbose)
1128 gdb_printf ("\nCollecting static trace data\n");
1129 end = temp_buf.data ();
1130 *end++ = 'L';
1131 str_list.emplace_back (temp_buf.data (), end - temp_buf.data ());
1134 for (i = m_regs_mask.size () - 1; i > 0; i--)
1135 if (m_regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
1136 break;
1137 if (m_regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
1139 if (info_verbose)
1140 gdb_printf ("\nCollecting registers (mask): 0x");
1142 /* One char for 'R', one for the null terminator and two per
1143 mask byte. */
1144 std::size_t new_size = (i + 1) * 2 + 2;
1145 if (new_size > temp_buf.size ())
1146 temp_buf.resize (new_size);
1148 end = temp_buf.data ();
1149 *end++ = 'R';
1150 for (; i >= 0; i--)
1152 QUIT; /* Allow user to bail out with ^C. */
1153 if (info_verbose)
1154 gdb_printf ("%02X", m_regs_mask[i]);
1156 end = pack_hex_byte (end, m_regs_mask[i]);
1158 *end = '\0';
1160 str_list.emplace_back (temp_buf.data ());
1162 if (info_verbose)
1163 gdb_printf ("\n");
1164 if (!m_memranges.empty () && info_verbose)
1165 gdb_printf ("Collecting memranges: \n");
1166 for (i = 0, count = 0, end = temp_buf.data ();
1167 i < m_memranges.size (); i++)
1169 QUIT; /* Allow user to bail out with ^C. */
1170 if (info_verbose)
1172 gdb_printf ("(%d, %s, %ld)\n",
1173 m_memranges[i].type,
1174 paddress (current_inferior ()->arch (),
1175 m_memranges[i].start),
1176 (long) (m_memranges[i].end
1177 - m_memranges[i].start));
1179 if (count + 27 > MAX_AGENT_EXPR_LEN)
1181 str_list.emplace_back (temp_buf.data (), count);
1182 count = 0;
1183 end = temp_buf.data ();
1187 bfd_signed_vma length
1188 = m_memranges[i].end - m_memranges[i].start;
1190 /* The "%X" conversion specifier expects an unsigned argument,
1191 so passing -1 (memrange_absolute) to it directly gives you
1192 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1193 Special-case it. */
1194 if (m_memranges[i].type == memrange_absolute)
1195 sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
1196 (long) length);
1197 else
1198 sprintf (end, "M%X,%s,%lX", m_memranges[i].type,
1199 phex_nz (m_memranges[i].start, 0), (long) length);
1202 count += strlen (end);
1203 end = temp_buf.data () + count;
1206 for (i = 0; i < m_aexprs.size (); i++)
1208 QUIT; /* Allow user to bail out with ^C. */
1209 if ((count + 10 + 2 * m_aexprs[i]->buf.size ()) > MAX_AGENT_EXPR_LEN)
1211 str_list.emplace_back (temp_buf.data (), count);
1212 count = 0;
1213 end = temp_buf.data ();
1215 sprintf (end, "X%08X,", (int) m_aexprs[i]->buf.size ());
1216 end += 10; /* 'X' + 8 hex digits + ',' */
1217 count += 10;
1219 end += 2 * bin2hex (m_aexprs[i]->buf.data (), end,
1220 m_aexprs[i]->buf.size ());
1221 count += 2 * m_aexprs[i]->buf.size ();
1224 if (count != 0)
1226 str_list.emplace_back (temp_buf.data (), count);
1227 count = 0;
1228 end = temp_buf.data ();
1231 return str_list;
1234 /* Add the expression STR to M_COMPUTED. */
1236 void
1237 collection_list::append_exp (std::string &&str)
1239 m_computed.push_back (std::move (str));
1242 void
1243 collection_list::finish ()
1245 memrange_sortmerge (m_memranges);
1248 static void
1249 encode_actions_1 (struct command_line *action,
1250 struct bp_location *tloc,
1251 int frame_reg,
1252 LONGEST frame_offset,
1253 struct collection_list *collect,
1254 struct collection_list *stepping_list)
1256 const char *action_exp;
1257 int i;
1258 struct value *tempval;
1259 struct cmd_list_element *cmd;
1261 for (; action; action = action->next)
1263 QUIT; /* Allow user to bail out with ^C. */
1264 action_exp = action->line;
1265 action_exp = skip_spaces (action_exp);
1267 cmd = lookup_cmd (&action_exp, cmdlist, "", NULL, -1, 1);
1268 if (cmd == 0)
1269 error (_("Bad action list item: %s"), action_exp);
1271 if (cmd_simple_func_eq (cmd, collect_pseudocommand))
1273 int trace_string = 0;
1275 if (*action_exp == '/')
1276 action_exp = decode_agent_options (action_exp, &trace_string);
1279 { /* Repeat over a comma-separated list. */
1280 QUIT; /* Allow user to bail out with ^C. */
1281 action_exp = skip_spaces (action_exp);
1282 gdbarch *arch = current_inferior ()->arch ();
1284 if (0 == strncasecmp ("$reg", action_exp, 4))
1286 for (i = 0; i < gdbarch_num_regs (arch);
1287 i++)
1289 int remote_regno = (gdbarch_remote_register_number
1290 (arch, i));
1292 /* Ignore arch regnos without a corresponding
1293 remote regno. This can happen for regnos not
1294 in the tdesc. */
1295 if (remote_regno >= 0)
1296 collect->add_remote_register (remote_regno);
1298 action_exp = strchr (action_exp, ','); /* more? */
1300 else if (0 == strncasecmp ("$arg", action_exp, 4))
1302 collect->add_local_symbols (arch,
1303 tloc->address,
1304 frame_reg,
1305 frame_offset,
1306 'A',
1307 trace_string);
1308 action_exp = strchr (action_exp, ','); /* more? */
1310 else if (0 == strncasecmp ("$loc", action_exp, 4))
1312 collect->add_local_symbols (arch,
1313 tloc->address,
1314 frame_reg,
1315 frame_offset,
1316 'L',
1317 trace_string);
1318 action_exp = strchr (action_exp, ','); /* more? */
1320 else if (0 == strncasecmp ("$_ret", action_exp, 5))
1322 agent_expr_up aexpr
1323 = gen_trace_for_return_address (tloc->address,
1324 arch, trace_string);
1326 finalize_tracepoint_aexpr (aexpr.get ());
1328 /* take care of the registers */
1329 collect->add_ax_registers (aexpr.get ());
1331 collect->add_aexpr (std::move (aexpr));
1332 action_exp = strchr (action_exp, ','); /* more? */
1334 else if (0 == strncasecmp ("$_sdata", action_exp, 7))
1336 collect->add_static_trace_data ();
1337 action_exp = strchr (action_exp, ','); /* more? */
1339 else
1341 unsigned long addr;
1343 const char *exp_start = action_exp;
1344 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1345 block_for_pc (tloc->address),
1346 PARSER_COMMA_TERMINATES);
1348 switch (exp->first_opcode ())
1350 case OP_REGISTER:
1352 expr::register_operation *regop
1353 = (gdb::checked_static_cast<expr::register_operation *>
1354 (exp->op.get ()));
1355 const char *name = regop->get_name ();
1357 i = user_reg_map_name_to_regnum (arch,
1358 name, strlen (name));
1359 if (i == -1)
1360 internal_error (_("Register $%s not available"),
1361 name);
1362 if (info_verbose)
1363 gdb_printf ("OP_REGISTER: ");
1364 collect->add_local_register (arch, i, tloc->address);
1365 break;
1368 case UNOP_MEMVAL:
1370 /* Safe because we know it's a simple expression. */
1371 tempval = exp->evaluate ();
1372 addr = tempval->address ();
1373 expr::unop_memval_operation *memop
1374 = (gdb::checked_static_cast<expr::unop_memval_operation *>
1375 (exp->op.get ()));
1376 struct type *type = memop->get_type ();
1377 /* Initialize the TYPE_LENGTH if it is a typedef. */
1378 check_typedef (type);
1379 collect->add_memrange (arch,
1380 memrange_absolute, addr,
1381 type->length (),
1382 tloc->address);
1383 collect->append_exp (std::string (exp_start,
1384 action_exp));
1386 break;
1388 case OP_VAR_VALUE:
1390 expr::var_value_operation *vvo
1391 = (gdb::checked_static_cast<expr::var_value_operation *>
1392 (exp->op.get ()));
1393 struct symbol *sym = vvo->get_symbol ();
1394 const char *name = sym->natural_name ();
1396 collect->collect_symbol (sym,
1397 arch,
1398 frame_reg,
1399 frame_offset,
1400 tloc->address,
1401 trace_string);
1402 collect->add_wholly_collected (name);
1404 break;
1406 default: /* Full-fledged expression. */
1407 agent_expr_up aexpr = gen_trace_for_expr (tloc->address,
1408 exp.get (),
1409 trace_string);
1411 finalize_tracepoint_aexpr (aexpr.get ());
1413 /* Take care of the registers. */
1414 collect->add_ax_registers (aexpr.get ());
1416 collect->add_aexpr (std::move (aexpr));
1417 collect->append_exp (std::string (exp_start,
1418 action_exp));
1419 break;
1420 } /* switch */
1421 } /* do */
1423 while (action_exp && *action_exp++ == ',');
1424 } /* if */
1425 else if (cmd_simple_func_eq (cmd, teval_pseudocommand))
1428 { /* Repeat over a comma-separated list. */
1429 QUIT; /* Allow user to bail out with ^C. */
1430 action_exp = skip_spaces (action_exp);
1433 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1434 block_for_pc (tloc->address),
1435 PARSER_COMMA_TERMINATES);
1437 agent_expr_up aexpr = gen_eval_for_expr (tloc->address,
1438 exp.get ());
1440 finalize_tracepoint_aexpr (aexpr.get ());
1442 /* Even though we're not officially collecting, add
1443 to the collect list anyway. */
1444 collect->add_aexpr (std::move (aexpr));
1445 } /* do */
1447 while (action_exp && *action_exp++ == ',');
1448 } /* if */
1449 else if (cmd_simple_func_eq (cmd, while_stepping_pseudocommand))
1451 /* We check against nested while-stepping when setting
1452 breakpoint action, so no way to run into nested
1453 here. */
1454 gdb_assert (stepping_list);
1456 encode_actions_1 (action->body_list_0.get (), tloc, frame_reg,
1457 frame_offset, stepping_list, NULL);
1459 else
1460 error (_("Invalid tracepoint command '%s'"), action->line);
1461 } /* for */
1464 /* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
1465 and STEPPING_LIST. */
1467 void
1468 encode_actions (struct bp_location *tloc,
1469 struct collection_list *tracepoint_list,
1470 struct collection_list *stepping_list)
1472 int frame_reg;
1473 LONGEST frame_offset;
1475 gdbarch_virtual_frame_pointer (tloc->gdbarch,
1476 tloc->address, &frame_reg, &frame_offset);
1478 tracepoint *t = gdb::checked_static_cast<tracepoint *> (tloc->owner);
1479 counted_command_line actions = all_tracepoint_actions (t);
1480 encode_actions_1 (actions.get (), tloc, frame_reg, frame_offset,
1481 tracepoint_list, stepping_list);
1482 encode_actions_1 (breakpoint_commands (tloc->owner), tloc,
1483 frame_reg, frame_offset, tracepoint_list, stepping_list);
1485 tracepoint_list->finish ();
1486 stepping_list->finish ();
1489 /* Render all actions into gdb protocol. */
1491 void
1492 encode_actions_rsp (struct bp_location *tloc,
1493 std::vector<std::string> *tdp_actions,
1494 std::vector<std::string> *stepping_actions)
1496 struct collection_list tracepoint_list, stepping_list;
1498 encode_actions (tloc, &tracepoint_list, &stepping_list);
1500 *tdp_actions = tracepoint_list.stringify ();
1501 *stepping_actions = stepping_list.stringify ();
1504 void
1505 collection_list::add_aexpr (agent_expr_up aexpr)
1507 m_aexprs.push_back (std::move (aexpr));
1510 static void
1511 process_tracepoint_on_disconnect (void)
1513 int has_pending_p = 0;
1515 /* Check whether we still have pending tracepoint. If we have, warn the
1516 user that pending tracepoint will no longer work. */
1517 for (breakpoint &b : all_tracepoints ())
1519 if (!b.has_locations ())
1521 has_pending_p = 1;
1522 break;
1524 else
1526 for (bp_location &loc1 : b.locations ())
1528 if (loc1.shlib_disabled)
1530 has_pending_p = 1;
1531 break;
1535 if (has_pending_p)
1536 break;
1540 if (has_pending_p)
1541 warning (_("Pending tracepoints will not be resolved while"
1542 " GDB is disconnected\n"));
1545 /* Reset local state of tracing. */
1547 void
1548 trace_reset_local_state (void)
1550 set_traceframe_num (-1);
1551 set_tracepoint_num (-1);
1552 set_traceframe_context (NULL);
1553 clear_traceframe_info ();
1556 void
1557 start_tracing (const char *notes)
1559 int any_enabled = 0, num_to_download = 0;
1560 int ret;
1562 auto tracepoint_range = all_tracepoints ();
1564 /* No point in tracing without any tracepoints... */
1565 if (tracepoint_range.begin () == tracepoint_range.end ())
1566 error (_("No tracepoints defined, not starting trace"));
1568 for (breakpoint &b : tracepoint_range)
1570 if (b.enable_state == bp_enabled)
1571 any_enabled = 1;
1573 if ((b.type == bp_fast_tracepoint
1574 ? may_insert_fast_tracepoints
1575 : may_insert_tracepoints))
1576 ++num_to_download;
1577 else
1578 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
1579 (b.type == bp_fast_tracepoint ? "fast " : ""), b.number);
1582 if (!any_enabled)
1584 if (target_supports_enable_disable_tracepoint ())
1585 warning (_("No tracepoints enabled"));
1586 else
1588 /* No point in tracing with only disabled tracepoints that
1589 cannot be re-enabled. */
1590 error (_("No tracepoints enabled, not starting trace"));
1594 if (num_to_download <= 0)
1595 error (_("No tracepoints that may be downloaded, not starting trace"));
1597 target_trace_init ();
1599 for (breakpoint &b : tracepoint_range)
1601 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
1602 int bp_location_downloaded = 0;
1604 /* Clear `inserted' flag. */
1605 for (bp_location &loc : b.locations ())
1606 loc.inserted = 0;
1608 if ((b.type == bp_fast_tracepoint
1609 ? !may_insert_fast_tracepoints
1610 : !may_insert_tracepoints))
1611 continue;
1613 t.number_on_target = 0;
1615 for (bp_location &loc : b.locations ())
1617 /* Since tracepoint locations are never duplicated, `inserted'
1618 flag should be zero. */
1619 gdb_assert (!loc.inserted);
1621 target_download_tracepoint (&loc);
1623 loc.inserted = 1;
1624 bp_location_downloaded = 1;
1627 t.number_on_target = b.number;
1629 for (bp_location &loc : b.locations ())
1630 if (loc.probe.prob != NULL)
1631 loc.probe.prob->set_semaphore (loc.probe.objfile, loc.gdbarch);
1633 if (bp_location_downloaded)
1634 notify_breakpoint_modified (&b);
1637 /* Send down all the trace state variables too. */
1638 for (const trace_state_variable &tsv : tvariables)
1639 target_download_trace_state_variable (tsv);
1641 /* Tell target to treat text-like sections as transparent. */
1642 target_trace_set_readonly_regions ();
1643 /* Set some mode flags. */
1644 target_set_disconnected_tracing (disconnected_tracing);
1645 target_set_circular_trace_buffer (circular_trace_buffer);
1646 target_set_trace_buffer_size (trace_buffer_size);
1648 if (!notes)
1649 notes = trace_notes.c_str ();
1651 ret = target_set_trace_notes (trace_user.c_str (), notes, NULL);
1653 if (!ret && (!trace_user.empty () || notes))
1654 warning (_("Target does not support trace user/notes, info ignored"));
1656 /* Now insert traps and begin collecting data. */
1657 target_trace_start ();
1659 /* Reset our local state. */
1660 trace_reset_local_state ();
1661 current_trace_status()->running = 1;
1664 /* The tstart command requests the target to start a new trace run.
1665 The command passes any arguments it has to the target verbatim, as
1666 an optional "trace note". This is useful as for instance a warning
1667 to other users if the trace runs disconnected, and you don't want
1668 anybody else messing with the target. */
1670 static void
1671 tstart_command (const char *args, int from_tty)
1673 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1675 if (current_trace_status ()->running)
1677 if (from_tty
1678 && !query (_("A trace is running already. Start a new run? ")))
1679 error (_("New trace run not started."));
1682 start_tracing (args);
1685 /* The tstop command stops the tracing run. The command passes any
1686 supplied arguments to the target verbatim as a "stop note"; if the
1687 target supports trace notes, then it will be reported back as part
1688 of the trace run's status. */
1690 static void
1691 tstop_command (const char *args, int from_tty)
1693 if (!current_trace_status ()->running)
1694 error (_("Trace is not running."));
1696 stop_tracing (args);
1699 void
1700 stop_tracing (const char *note)
1702 int ret;
1704 target_trace_stop ();
1706 for (breakpoint &t : all_tracepoints ())
1708 if ((t.type == bp_fast_tracepoint
1709 ? !may_insert_fast_tracepoints
1710 : !may_insert_tracepoints))
1711 continue;
1713 for (bp_location &loc : t.locations ())
1715 /* GDB can be totally absent in some disconnected trace scenarios,
1716 but we don't really care if this semaphore goes out of sync.
1717 That's why we are decrementing it here, but not taking care
1718 in other places. */
1719 if (loc.probe.prob != NULL)
1720 loc.probe.prob->clear_semaphore (loc.probe.objfile, loc.gdbarch);
1724 if (!note)
1725 note = trace_stop_notes.c_str ();
1727 ret = target_set_trace_notes (NULL, NULL, note);
1729 if (!ret && note)
1730 warning (_("Target does not support trace notes, note ignored"));
1732 /* Should change in response to reply? */
1733 current_trace_status ()->running = 0;
1736 /* tstatus command */
1737 static void
1738 tstatus_command (const char *args, int from_tty)
1740 struct trace_status *ts = current_trace_status ();
1741 int status;
1743 status = target_get_trace_status (ts);
1745 if (status == -1)
1747 if (ts->filename != NULL)
1748 gdb_printf (_("Using a trace file.\n"));
1749 else
1751 gdb_printf (_("Trace can not be run on this target.\n"));
1752 return;
1756 if (!ts->running_known)
1758 gdb_printf (_("Run/stop status is unknown.\n"));
1760 else if (ts->running)
1762 gdb_printf (_("Trace is running on the target.\n"));
1764 else
1766 switch (ts->stop_reason)
1768 case trace_never_run:
1769 gdb_printf (_("No trace has been run on the target.\n"));
1770 break;
1771 case trace_stop_command:
1772 if (ts->stop_desc)
1773 gdb_printf (_("Trace stopped by a tstop command (%s).\n"),
1774 ts->stop_desc);
1775 else
1776 gdb_printf (_("Trace stopped by a tstop command.\n"));
1777 break;
1778 case trace_buffer_full:
1779 gdb_printf (_("Trace stopped because the buffer was full.\n"));
1780 break;
1781 case trace_disconnected:
1782 gdb_printf (_("Trace stopped because of disconnection.\n"));
1783 break;
1784 case tracepoint_passcount:
1785 gdb_printf (_("Trace stopped by tracepoint %d.\n"),
1786 ts->stopping_tracepoint);
1787 break;
1788 case tracepoint_error:
1789 if (ts->stopping_tracepoint)
1790 gdb_printf (_("Trace stopped by an "
1791 "error (%s, tracepoint %d).\n"),
1792 ts->stop_desc, ts->stopping_tracepoint);
1793 else
1794 gdb_printf (_("Trace stopped by an error (%s).\n"),
1795 ts->stop_desc);
1796 break;
1797 case trace_stop_reason_unknown:
1798 gdb_printf (_("Trace stopped for an unknown reason.\n"));
1799 break;
1800 default:
1801 gdb_printf (_("Trace stopped for some other reason (%d).\n"),
1802 ts->stop_reason);
1803 break;
1807 if (ts->traceframes_created >= 0
1808 && ts->traceframe_count != ts->traceframes_created)
1810 gdb_printf (_("Buffer contains %d trace "
1811 "frames (of %d created total).\n"),
1812 ts->traceframe_count, ts->traceframes_created);
1814 else if (ts->traceframe_count >= 0)
1816 gdb_printf (_("Collected %d trace frames.\n"),
1817 ts->traceframe_count);
1820 if (ts->buffer_free >= 0)
1822 if (ts->buffer_size >= 0)
1824 gdb_printf (_("Trace buffer has %d bytes of %d bytes free"),
1825 ts->buffer_free, ts->buffer_size);
1826 if (ts->buffer_size > 0)
1827 gdb_printf (_(" (%d%% full)"),
1828 ((int) ((((long long) (ts->buffer_size
1829 - ts->buffer_free)) * 100)
1830 / ts->buffer_size)));
1831 gdb_printf (_(".\n"));
1833 else
1834 gdb_printf (_("Trace buffer has %d bytes free.\n"),
1835 ts->buffer_free);
1838 if (ts->disconnected_tracing)
1839 gdb_printf (_("Trace will continue if GDB disconnects.\n"));
1840 else
1841 gdb_printf (_("Trace will stop if GDB disconnects.\n"));
1843 if (ts->circular_buffer)
1844 gdb_printf (_("Trace buffer is circular.\n"));
1846 if (ts->user_name && strlen (ts->user_name) > 0)
1847 gdb_printf (_("Trace user is %s.\n"), ts->user_name);
1849 if (ts->notes && strlen (ts->notes) > 0)
1850 gdb_printf (_("Trace notes: %s.\n"), ts->notes);
1852 /* Now report on what we're doing with tfind. */
1853 if (traceframe_number >= 0)
1854 gdb_printf (_("Looking at trace frame %d, tracepoint %d.\n"),
1855 traceframe_number, tracepoint_number);
1856 else
1857 gdb_printf (_("Not looking at any trace frame.\n"));
1859 /* Report start/stop times if supplied. */
1860 if (ts->start_time)
1862 if (ts->stop_time)
1864 LONGEST run_time = ts->stop_time - ts->start_time;
1866 /* Reporting a run time is more readable than two long numbers. */
1867 gdb_printf (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
1868 (long int) (ts->start_time / 1000000),
1869 (long int) (ts->start_time % 1000000),
1870 (long int) (run_time / 1000000),
1871 (long int) (run_time % 1000000));
1873 else
1874 gdb_printf (_("Trace started at %ld.%06ld secs.\n"),
1875 (long int) (ts->start_time / 1000000),
1876 (long int) (ts->start_time % 1000000));
1878 else if (ts->stop_time)
1879 gdb_printf (_("Trace stopped at %ld.%06ld secs.\n"),
1880 (long int) (ts->stop_time / 1000000),
1881 (long int) (ts->stop_time % 1000000));
1883 /* Now report any per-tracepoint status available. */
1884 for (breakpoint &b : all_tracepoints ())
1886 tracepoint *t = gdb::checked_static_cast<tracepoint *> (&b);
1887 target_get_tracepoint_status (t, nullptr);
1891 /* Report the trace status to uiout, in a way suitable for MI, and not
1892 suitable for CLI. If ON_STOP is true, suppress a few fields that
1893 are not meaningful in the -trace-stop response.
1895 The implementation is essentially parallel to trace_status_command, but
1896 merging them will result in unreadable code. */
1897 void
1898 trace_status_mi (int on_stop)
1900 struct ui_out *uiout = current_uiout;
1901 struct trace_status *ts = current_trace_status ();
1902 int status;
1904 status = target_get_trace_status (ts);
1906 if (status == -1 && ts->filename == NULL)
1908 uiout->field_string ("supported", "0");
1909 return;
1912 if (ts->filename != NULL)
1913 uiout->field_string ("supported", "file");
1914 else if (!on_stop)
1915 uiout->field_string ("supported", "1");
1917 if (ts->filename != NULL)
1918 uiout->field_string ("trace-file", ts->filename);
1920 gdb_assert (ts->running_known);
1922 if (ts->running)
1924 uiout->field_string ("running", "1");
1926 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
1927 Given that the frontend gets the status either on -trace-stop, or from
1928 -trace-status after re-connection, it does not seem like this
1929 information is necessary for anything. It is not necessary for either
1930 figuring the vital state of the target nor for navigation of trace
1931 frames. If the frontend wants to show the current state is some
1932 configure dialog, it can request the value when such dialog is
1933 invoked by the user. */
1935 else
1937 const char *stop_reason = NULL;
1938 int stopping_tracepoint = -1;
1940 if (!on_stop)
1941 uiout->field_string ("running", "0");
1943 if (ts->stop_reason != trace_stop_reason_unknown)
1945 switch (ts->stop_reason)
1947 case trace_stop_command:
1948 stop_reason = "request";
1949 break;
1950 case trace_buffer_full:
1951 stop_reason = "overflow";
1952 break;
1953 case trace_disconnected:
1954 stop_reason = "disconnection";
1955 break;
1956 case tracepoint_passcount:
1957 stop_reason = "passcount";
1958 stopping_tracepoint = ts->stopping_tracepoint;
1959 break;
1960 case tracepoint_error:
1961 stop_reason = "error";
1962 stopping_tracepoint = ts->stopping_tracepoint;
1963 break;
1966 if (stop_reason)
1968 uiout->field_string ("stop-reason", stop_reason);
1969 if (stopping_tracepoint != -1)
1970 uiout->field_signed ("stopping-tracepoint",
1971 stopping_tracepoint);
1972 if (ts->stop_reason == tracepoint_error)
1973 uiout->field_string ("error-description",
1974 ts->stop_desc);
1979 if (ts->traceframe_count != -1)
1980 uiout->field_signed ("frames", ts->traceframe_count);
1981 if (ts->traceframes_created != -1)
1982 uiout->field_signed ("frames-created", ts->traceframes_created);
1983 if (ts->buffer_size != -1)
1984 uiout->field_signed ("buffer-size", ts->buffer_size);
1985 if (ts->buffer_free != -1)
1986 uiout->field_signed ("buffer-free", ts->buffer_free);
1988 uiout->field_signed ("disconnected", ts->disconnected_tracing);
1989 uiout->field_signed ("circular", ts->circular_buffer);
1991 uiout->field_string ("user-name", ts->user_name);
1992 uiout->field_string ("notes", ts->notes);
1995 char buf[100];
1997 xsnprintf (buf, sizeof buf, "%ld.%06ld",
1998 (long int) (ts->start_time / 1000000),
1999 (long int) (ts->start_time % 1000000));
2000 uiout->field_string ("start-time", buf);
2001 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2002 (long int) (ts->stop_time / 1000000),
2003 (long int) (ts->stop_time % 1000000));
2004 uiout->field_string ("stop-time", buf);
2008 /* Check if a trace run is ongoing. If so, and FROM_TTY, query the
2009 user if she really wants to detach. */
2011 void
2012 query_if_trace_running (int from_tty)
2014 if (!from_tty)
2015 return;
2017 /* It can happen that the target that was tracing went away on its
2018 own, and we didn't notice. Get a status update, and if the
2019 current target doesn't even do tracing, then assume it's not
2020 running anymore. */
2021 if (target_get_trace_status (current_trace_status ()) < 0)
2022 current_trace_status ()->running = 0;
2024 /* If running interactively, give the user the option to cancel and
2025 then decide what to do differently with the run. Scripts are
2026 just going to disconnect and let the target deal with it,
2027 according to how it's been instructed previously via
2028 disconnected-tracing. */
2029 if (current_trace_status ()->running)
2031 process_tracepoint_on_disconnect ();
2033 if (current_trace_status ()->disconnected_tracing)
2035 if (!query (_("Trace is running and will "
2036 "continue after detach; detach anyway? ")))
2037 error (_("Not confirmed."));
2039 else
2041 if (!query (_("Trace is running but will "
2042 "stop on detach; detach anyway? ")))
2043 error (_("Not confirmed."));
2048 /* This function handles the details of what to do about an ongoing
2049 tracing run if the user has asked to detach or otherwise disconnect
2050 from the target. */
2052 void
2053 disconnect_tracing (void)
2055 /* Also we want to be out of tfind mode, otherwise things can get
2056 confusing upon reconnection. Just use these calls instead of
2057 full tfind_1 behavior because we're in the middle of detaching,
2058 and there's no point to updating current stack frame etc. */
2059 trace_reset_local_state ();
2062 /* Worker function for the various flavors of the tfind command. */
2063 void
2064 tfind_1 (enum trace_find_type type, int num,
2065 CORE_ADDR addr1, CORE_ADDR addr2,
2066 int from_tty)
2068 int target_frameno = -1, target_tracept = -1;
2069 struct frame_id old_frame_id = null_frame_id;
2070 struct tracepoint *tp;
2071 struct ui_out *uiout = current_uiout;
2073 /* Only try to get the current stack frame if we have a chance of
2074 succeeding. In particular, if we're trying to get a first trace
2075 frame while all threads are running, it's not going to succeed,
2076 so leave it with a default value and let the frame comparison
2077 below (correctly) decide to print out the source location of the
2078 trace frame. */
2079 if (!(type == tfind_number && num == -1)
2080 && (has_stack_frames () || traceframe_number >= 0))
2081 old_frame_id = get_frame_id (get_current_frame ());
2083 target_frameno = target_trace_find (type, num, addr1, addr2,
2084 &target_tracept);
2086 if (type == tfind_number
2087 && num == -1
2088 && target_frameno == -1)
2090 /* We told the target to get out of tfind mode, and it did. */
2092 else if (target_frameno == -1)
2094 /* A request for a non-existent trace frame has failed.
2095 Our response will be different, depending on FROM_TTY:
2097 If FROM_TTY is true, meaning that this command was
2098 typed interactively by the user, then give an error
2099 and DO NOT change the state of traceframe_number etc.
2101 However if FROM_TTY is false, meaning that we're either
2102 in a script, a loop, or a user-defined command, then
2103 DON'T give an error, but DO change the state of
2104 traceframe_number etc. to invalid.
2106 The rationale is that if you typed the command, you
2107 might just have committed a typo or something, and you'd
2108 like to NOT lose your current debugging state. However
2109 if you're in a user-defined command or especially in a
2110 loop, then you need a way to detect that the command
2111 failed WITHOUT aborting. This allows you to write
2112 scripts that search thru the trace buffer until the end,
2113 and then continue on to do something else. */
2115 if (from_tty)
2116 error (_("Target failed to find requested trace frame."));
2117 else
2119 if (info_verbose)
2120 gdb_printf ("End of trace buffer.\n");
2121 #if 0 /* dubious now? */
2122 /* The following will not recurse, since it's
2123 special-cased. */
2124 tfind_command ("-1", from_tty);
2125 #endif
2129 tp = get_tracepoint_by_number_on_target (target_tracept);
2131 reinit_frame_cache ();
2132 target_dcache_invalidate (current_program_space->aspace);
2134 set_tracepoint_num (tp ? tp->number : target_tracept);
2136 if (target_frameno != get_traceframe_number ())
2137 interps_notify_traceframe_changed (target_frameno, tracepoint_number);
2139 set_current_traceframe (target_frameno);
2141 if (target_frameno == -1)
2142 set_traceframe_context (NULL);
2143 else
2144 set_traceframe_context (get_current_frame ());
2146 if (traceframe_number >= 0)
2148 /* Use different branches for MI and CLI to make CLI messages
2149 i18n-eable. */
2150 if (uiout->is_mi_like_p ())
2152 uiout->field_string ("found", "1");
2153 uiout->field_signed ("tracepoint", tracepoint_number);
2154 uiout->field_signed ("traceframe", traceframe_number);
2156 else
2158 gdb_printf (_("Found trace frame %d, tracepoint %d\n"),
2159 traceframe_number, tracepoint_number);
2162 else
2164 if (uiout->is_mi_like_p ())
2165 uiout->field_string ("found", "0");
2166 else if (type == tfind_number && num == -1)
2167 gdb_printf (_("No longer looking at any trace frame\n"));
2168 else /* This case may never occur, check. */
2169 gdb_printf (_("No trace frame found\n"));
2172 /* If we're in nonstop mode and getting out of looking at trace
2173 frames, there won't be any current frame to go back to and
2174 display. */
2175 if (from_tty
2176 && (has_stack_frames () || traceframe_number >= 0))
2178 enum print_what print_what;
2180 /* NOTE: in imitation of the step command, try to determine
2181 whether we have made a transition from one function to
2182 another. If so, we'll print the "stack frame" (ie. the new
2183 function and it's arguments) -- otherwise we'll just show the
2184 new source line. */
2186 if (old_frame_id == get_frame_id (get_current_frame ()))
2187 print_what = SRC_LINE;
2188 else
2189 print_what = SRC_AND_LOC;
2191 print_stack_frame (get_selected_frame (NULL), 1, print_what, 1);
2192 do_displays ();
2196 /* Error on looking at traceframes while trace is running. */
2198 void
2199 check_trace_running (struct trace_status *status)
2201 if (status->running && status->filename == NULL)
2202 error (_("May not look at trace frames while trace is running."));
2205 /* trace_find_command takes a trace frame number n,
2206 sends "QTFrame:<n>" to the target,
2207 and accepts a reply that may contain several optional pieces
2208 of information: a frame number, a tracepoint number, and an
2209 indication of whether this is a trap frame or a stepping frame.
2211 The minimal response is just "OK" (which indicates that the
2212 target does not give us a frame number or a tracepoint number).
2213 Instead of that, the target may send us a string containing
2214 any combination of:
2215 F<hexnum> (gives the selected frame number)
2216 T<hexnum> (gives the selected tracepoint number)
2219 /* tfind command */
2220 static void
2221 tfind_command_1 (const char *args, int from_tty)
2222 { /* This should only be called with a numeric argument. */
2223 int frameno = -1;
2225 check_trace_running (current_trace_status ());
2227 if (args == 0 || *args == 0)
2228 { /* TFIND with no args means find NEXT trace frame. */
2229 if (traceframe_number == -1)
2230 frameno = 0; /* "next" is first one. */
2231 else
2232 frameno = traceframe_number + 1;
2234 else if (0 == strcmp (args, "-"))
2236 if (traceframe_number == -1)
2237 error (_("not debugging trace buffer"));
2238 else if (from_tty && traceframe_number == 0)
2239 error (_("already at start of trace buffer"));
2241 frameno = traceframe_number - 1;
2243 /* A hack to work around eval's need for fp to have been collected. */
2244 else if (0 == strcmp (args, "-1"))
2245 frameno = -1;
2246 else
2247 frameno = parse_and_eval_long (args);
2249 if (frameno < -1)
2250 error (_("invalid input (%d is less than zero)"), frameno);
2252 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
2255 static void
2256 tfind_command (const char *args, int from_tty)
2258 tfind_command_1 (args, from_tty);
2261 /* tfind end */
2262 static void
2263 tfind_end_command (const char *args, int from_tty)
2265 tfind_command_1 ("-1", from_tty);
2268 /* tfind start */
2269 static void
2270 tfind_start_command (const char *args, int from_tty)
2272 tfind_command_1 ("0", from_tty);
2275 /* tfind pc command */
2276 static void
2277 tfind_pc_command (const char *args, int from_tty)
2279 CORE_ADDR pc;
2281 check_trace_running (current_trace_status ());
2283 if (args == 0 || *args == 0)
2284 pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
2285 else
2286 pc = parse_and_eval_address (args);
2288 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
2291 /* tfind tracepoint command */
2292 static void
2293 tfind_tracepoint_command (const char *args, int from_tty)
2295 int tdp;
2296 struct tracepoint *tp;
2298 check_trace_running (current_trace_status ());
2300 if (args == 0 || *args == 0)
2302 if (tracepoint_number == -1)
2303 error (_("No current tracepoint -- please supply an argument."));
2304 else
2305 tdp = tracepoint_number; /* Default is current TDP. */
2307 else
2308 tdp = parse_and_eval_long (args);
2310 /* If we have the tracepoint on hand, use the number that the
2311 target knows about (which may be different if we disconnected
2312 and reconnected). */
2313 tp = get_tracepoint (tdp);
2314 if (tp)
2315 tdp = tp->number_on_target;
2317 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
2320 /* TFIND LINE command:
2322 This command will take a sourceline for argument, just like BREAK
2323 or TRACE (ie. anything that "decode_line_1" can handle).
2325 With no argument, this command will find the next trace frame
2326 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2328 static void
2329 tfind_line_command (const char *args, int from_tty)
2331 check_trace_running (current_trace_status ());
2333 symtab_and_line sal;
2334 if (args == 0 || *args == 0)
2336 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
2338 else
2340 std::vector<symtab_and_line> sals
2341 = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
2342 sal = sals[0];
2345 if (sal.symtab == 0)
2346 error (_("No line number information available."));
2348 CORE_ADDR start_pc, end_pc;
2349 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2351 if (start_pc == end_pc)
2353 gdb_printf ("Line %d of \"%s\"",
2354 sal.line,
2355 symtab_to_filename_for_display (sal.symtab));
2356 gdb_stdout->wrap_here (2);
2357 gdb_printf (" is at address ");
2358 print_address (get_current_arch (), start_pc, gdb_stdout);
2359 gdb_stdout->wrap_here (2);
2360 gdb_printf (" but contains no code.\n");
2361 sal = find_pc_line (start_pc, 0);
2362 if (sal.line > 0
2363 && find_line_pc_range (sal, &start_pc, &end_pc)
2364 && start_pc != end_pc)
2365 gdb_printf ("Attempting to find line %d instead.\n",
2366 sal.line);
2367 else
2368 error (_("Cannot find a good line."));
2371 else
2373 /* Is there any case in which we get here, and have an address
2374 which the user would want to see? If we have debugging
2375 symbols and no line numbers? */
2376 error (_("Line number %d is out of range for \"%s\"."),
2377 sal.line, symtab_to_filename_for_display (sal.symtab));
2380 /* Find within range of stated line. */
2381 if (args && *args)
2382 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
2383 else
2384 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
2387 /* tfind range command */
2388 static void
2389 tfind_range_command (const char *args, int from_tty)
2391 static CORE_ADDR start, stop;
2392 const char *tmp;
2394 check_trace_running (current_trace_status ());
2396 if (args == 0 || *args == 0)
2397 { /* XXX FIXME: what should default behavior be? */
2398 gdb_printf ("Usage: tfind range STARTADDR, ENDADDR\n");
2399 return;
2402 if (0 != (tmp = strchr (args, ',')))
2404 std::string start_addr (args, tmp);
2405 ++tmp;
2406 tmp = skip_spaces (tmp);
2407 start = parse_and_eval_address (start_addr.c_str ());
2408 stop = parse_and_eval_address (tmp);
2410 else
2411 { /* No explicit end address? */
2412 start = parse_and_eval_address (args);
2413 stop = start + 1; /* ??? */
2416 tfind_1 (tfind_range, 0, start, stop, from_tty);
2419 /* tfind outside command */
2420 static void
2421 tfind_outside_command (const char *args, int from_tty)
2423 CORE_ADDR start, stop;
2424 const char *tmp;
2426 if (current_trace_status ()->running
2427 && current_trace_status ()->filename == NULL)
2428 error (_("May not look at trace frames while trace is running."));
2430 if (args == 0 || *args == 0)
2431 { /* XXX FIXME: what should default behavior be? */
2432 gdb_printf ("Usage: tfind outside STARTADDR, ENDADDR\n");
2433 return;
2436 if (0 != (tmp = strchr (args, ',')))
2438 std::string start_addr (args, tmp);
2439 ++tmp;
2440 tmp = skip_spaces (tmp);
2441 start = parse_and_eval_address (start_addr.c_str ());
2442 stop = parse_and_eval_address (tmp);
2444 else
2445 { /* No explicit end address? */
2446 start = parse_and_eval_address (args);
2447 stop = start + 1; /* ??? */
2450 tfind_1 (tfind_outside, 0, start, stop, from_tty);
2453 /* info scope command: list the locals for a scope. */
2454 static void
2455 info_scope_command (const char *args_in, int from_tty)
2457 struct bound_minimal_symbol msym;
2458 const struct block *block;
2459 const char *symname;
2460 const char *save_args = args_in;
2461 int j, count = 0;
2462 struct gdbarch *gdbarch;
2463 int regno;
2464 const char *args = args_in;
2466 if (args == 0 || *args == 0)
2467 error (_("requires an argument (function, "
2468 "line or *addr) to define a scope"));
2470 location_spec_up locspec = string_to_location_spec (&args,
2471 current_language);
2472 std::vector<symtab_and_line> sals
2473 = decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
2474 NULL, NULL, 0);
2475 if (sals.empty ())
2477 /* Presumably decode_line_1 has already warned. */
2478 return;
2481 /* Resolve line numbers to PC. */
2482 resolve_sal_pc (&sals[0]);
2483 block = block_for_pc (sals[0].pc);
2485 while (block != 0)
2487 QUIT; /* Allow user to bail out with ^C. */
2488 for (struct symbol *sym : block_iterator_range (block))
2490 QUIT; /* Allow user to bail out with ^C. */
2491 if (count == 0)
2492 gdb_printf ("Scope for %s:\n", save_args);
2493 count++;
2495 symname = sym->print_name ();
2496 if (symname == NULL || *symname == '\0')
2497 continue; /* Probably botched, certainly useless. */
2499 gdbarch = sym->arch ();
2501 gdb_printf ("Symbol %s is ", symname);
2503 if (const symbol_computed_ops *computed_ops = sym->computed_ops ();
2504 computed_ops != nullptr)
2505 computed_ops->describe_location (sym, block->entry_pc (),
2506 gdb_stdout);
2507 else
2509 switch (sym->aclass ())
2511 default:
2512 case LOC_UNDEF: /* Messed up symbol? */
2513 gdb_printf ("a bogus symbol, class %d.\n",
2514 sym->aclass ());
2515 count--; /* Don't count this one. */
2516 continue;
2517 case LOC_CONST:
2518 gdb_printf ("a constant with value %s (%s)",
2519 plongest (sym->value_longest ()),
2520 hex_string (sym->value_longest ()));
2521 break;
2522 case LOC_CONST_BYTES:
2523 gdb_printf ("constant bytes: ");
2524 if (sym->type ())
2525 for (j = 0; j < sym->type ()->length (); j++)
2526 gdb_printf (" %02x", (unsigned) sym->value_bytes ()[j]);
2527 break;
2528 case LOC_STATIC:
2529 gdb_printf ("in static storage at address ");
2530 gdb_printf ("%s", paddress (gdbarch, sym->value_address ()));
2531 break;
2532 case LOC_REGISTER:
2533 /* GDBARCH is the architecture associated with the objfile
2534 the symbol is defined in; the target architecture may be
2535 different, and may provide additional registers. However,
2536 we do not know the target architecture at this point.
2537 We assume the objfile architecture will contain all the
2538 standard registers that occur in debug info in that
2539 objfile. */
2540 regno = sym->register_ops ()->register_number (sym, gdbarch);
2542 if (sym->is_argument ())
2543 gdb_printf ("an argument in register $%s",
2544 gdbarch_register_name (gdbarch, regno));
2545 else
2546 gdb_printf ("a local variable in register $%s",
2547 gdbarch_register_name (gdbarch, regno));
2548 break;
2549 case LOC_ARG:
2550 gdb_printf ("an argument at stack/frame offset %s",
2551 plongest (sym->value_longest ()));
2552 break;
2553 case LOC_LOCAL:
2554 gdb_printf ("a local variable at frame offset %s",
2555 plongest (sym->value_longest ()));
2556 break;
2557 case LOC_REF_ARG:
2558 gdb_printf ("a reference argument at offset %s",
2559 plongest (sym->value_longest ()));
2560 break;
2561 case LOC_REGPARM_ADDR:
2562 /* Note comment at LOC_REGISTER. */
2563 regno = sym->register_ops ()->register_number (sym, gdbarch);
2564 gdb_printf ("the address of an argument, in register $%s",
2565 gdbarch_register_name (gdbarch, regno));
2566 break;
2567 case LOC_TYPEDEF:
2568 gdb_printf ("a typedef.\n");
2569 continue;
2570 case LOC_LABEL:
2571 gdb_printf ("a label at address ");
2572 gdb_printf ("%s", paddress (gdbarch, sym->value_address ()));
2573 break;
2574 case LOC_BLOCK:
2575 gdb_printf ("a function at address ");
2576 gdb_printf ("%s",
2577 paddress (gdbarch,
2578 sym->value_block ()->entry_pc ()));
2579 break;
2580 case LOC_UNRESOLVED:
2581 msym = lookup_minimal_symbol (sym->linkage_name (),
2582 NULL, NULL);
2583 if (msym.minsym == NULL)
2584 gdb_printf ("Unresolved Static");
2585 else
2587 gdb_printf ("static storage at address ");
2588 gdb_printf ("%s",
2589 paddress (gdbarch, msym.value_address ()));
2591 break;
2592 case LOC_OPTIMIZED_OUT:
2593 gdb_printf ("optimized out.\n");
2594 continue;
2595 case LOC_COMPUTED:
2596 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
2599 if (sym->type ())
2601 struct type *t = check_typedef (sym->type ());
2603 gdb_printf (", length %s.\n", pulongest (t->length ()));
2606 if (block->function ())
2607 break;
2608 else
2609 block = block->superblock ();
2611 if (count <= 0)
2612 gdb_printf ("Scope for %s contains no locals or arguments.\n",
2613 save_args);
2616 /* Helper for trace_dump_command. Dump the action list starting at
2617 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2618 actions of the body of a while-stepping action. STEPPING_FRAME is
2619 set if the current traceframe was determined to be a while-stepping
2620 traceframe. */
2622 static void
2623 trace_dump_actions (struct command_line *action,
2624 int stepping_actions, int stepping_frame,
2625 int from_tty)
2627 const char *action_exp, *next_comma;
2629 for (; action != NULL; action = action->next)
2631 struct cmd_list_element *cmd;
2633 QUIT; /* Allow user to bail out with ^C. */
2634 action_exp = action->line;
2635 action_exp = skip_spaces (action_exp);
2637 /* The collection actions to be done while stepping are
2638 bracketed by the commands "while-stepping" and "end". */
2640 if (*action_exp == '#') /* comment line */
2641 continue;
2643 cmd = lookup_cmd (&action_exp, cmdlist, "", NULL, -1, 1);
2644 if (cmd == 0)
2645 error (_("Bad action list item: %s"), action_exp);
2647 if (cmd_simple_func_eq (cmd, while_stepping_pseudocommand))
2649 gdb_assert (action->body_list_1 == nullptr);
2650 trace_dump_actions (action->body_list_0.get (),
2651 1, stepping_frame, from_tty);
2653 else if (cmd_simple_func_eq (cmd, collect_pseudocommand))
2655 /* Display the collected data.
2656 For the trap frame, display only what was collected at
2657 the trap. Likewise for stepping frames, display only
2658 what was collected while stepping. This means that the
2659 two boolean variables, STEPPING_FRAME and
2660 STEPPING_ACTIONS should be equal. */
2661 if (stepping_frame == stepping_actions)
2663 int trace_string = 0;
2665 if (*action_exp == '/')
2666 action_exp = decode_agent_options (action_exp, &trace_string);
2669 { /* Repeat over a comma-separated list. */
2670 QUIT; /* Allow user to bail out with ^C. */
2671 if (*action_exp == ',')
2672 action_exp++;
2673 action_exp = skip_spaces (action_exp);
2675 next_comma = strchr (action_exp, ',');
2677 if (0 == strncasecmp (action_exp, "$reg", 4))
2678 registers_info (NULL, from_tty);
2679 else if (0 == strncasecmp (action_exp, "$_ret", 5))
2681 else if (0 == strncasecmp (action_exp, "$loc", 4))
2682 info_locals_command (NULL, from_tty);
2683 else if (0 == strncasecmp (action_exp, "$arg", 4))
2684 info_args_command (NULL, from_tty);
2685 else
2686 { /* variable */
2687 std::string contents;
2688 const char *exp = action_exp;
2689 if (next_comma != NULL)
2691 size_t len = next_comma - action_exp;
2692 contents = std::string (action_exp, len);
2693 exp = contents.c_str ();
2696 gdb_printf ("%s = ", exp);
2697 output_command (exp, from_tty);
2698 gdb_printf ("\n");
2700 action_exp = next_comma;
2702 while (action_exp && *action_exp == ',');
2708 /* Return bp_location of the tracepoint associated with the current
2709 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
2710 is a stepping traceframe. */
2712 struct bp_location *
2713 get_traceframe_location (int *stepping_frame_p)
2715 struct tracepoint *t;
2716 struct regcache *regcache;
2718 if (tracepoint_number == -1)
2719 error (_("No current trace frame."));
2721 t = get_tracepoint (tracepoint_number);
2723 if (t == NULL)
2724 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2725 tracepoint_number);
2727 /* The current frame is a trap frame if the frame PC is equal to the
2728 tracepoint PC. If not, then the current frame was collected
2729 during single-stepping. */
2730 regcache = get_thread_regcache (inferior_thread ());
2732 /* If the traceframe's address matches any of the tracepoint's
2733 locations, assume it is a direct hit rather than a while-stepping
2734 frame. (FIXME this is not reliable, should record each frame's
2735 type.) */
2736 for (bp_location &tloc : t->locations ())
2737 if (tloc.address == regcache_read_pc (regcache))
2739 *stepping_frame_p = 0;
2740 return &tloc;
2743 /* If this is a stepping frame, we don't know which location
2744 triggered. The first is as good (or bad) a guess as any... */
2745 *stepping_frame_p = 1;
2746 return &t->first_loc ();
2749 /* Return the default collect actions of a tracepoint T. */
2751 static counted_command_line
2752 all_tracepoint_actions (tracepoint *t)
2754 counted_command_line actions (nullptr, command_lines_deleter ());
2756 /* If there are default expressions to collect, make up a collect
2757 action and prepend to the action list to encode. Note that since
2758 validation is per-tracepoint (local var "xyz" might be valid for
2759 one tracepoint and not another, etc), we make up the action on
2760 the fly, and don't cache it. */
2761 if (!default_collect.empty ())
2763 gdb::unique_xmalloc_ptr<char> default_collect_line
2764 = xstrprintf ("collect %s", default_collect.c_str ());
2766 validate_actionline (default_collect_line.get (), t);
2767 actions.reset (new struct command_line (simple_control,
2768 default_collect_line.release ()),
2769 command_lines_deleter ());
2772 return actions;
2775 /* The tdump command. */
2777 static void
2778 tdump_command (const char *args, int from_tty)
2780 int stepping_frame = 0;
2781 struct bp_location *loc;
2783 /* This throws an error is not inspecting a trace frame. */
2784 loc = get_traceframe_location (&stepping_frame);
2786 gdb_printf ("Data collected at tracepoint %d, trace frame %d:\n",
2787 tracepoint_number, traceframe_number);
2789 /* This command only makes sense for the current frame, not the
2790 selected frame. */
2791 scoped_restore_current_thread restore_thread;
2793 select_frame (get_current_frame ());
2795 tracepoint *t = gdb::checked_static_cast<tracepoint *> (loc->owner);
2796 counted_command_line actions = all_tracepoint_actions (t);
2798 trace_dump_actions (actions.get (), 0, stepping_frame, from_tty);
2799 trace_dump_actions (breakpoint_commands (loc->owner), 0, stepping_frame,
2800 from_tty);
2803 /* Encode a piece of a tracepoint's source-level definition in a form
2804 that is suitable for both protocol and saving in files. */
2805 /* This version does not do multiple encodes for long strings; it should
2806 return an offset to the next piece to encode. FIXME */
2809 encode_source_string (int tpnum, ULONGEST addr,
2810 const char *srctype, const char *src,
2811 char *buf, int buf_size)
2813 if (80 + strlen (srctype) > buf_size)
2814 error (_("Buffer too small for source encoding"));
2815 sprintf (buf, "%x:%s:%s:%x:%x:",
2816 tpnum, phex_nz (addr, sizeof (addr)),
2817 srctype, 0, (int) strlen (src));
2818 if (strlen (buf) + strlen (src) * 2 >= buf_size)
2819 error (_("Source string too long for buffer"));
2820 bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src));
2821 return -1;
2824 /* Tell the target what to do with an ongoing tracing run if GDB
2825 disconnects for some reason. */
2827 static void
2828 set_disconnected_tracing (const char *args, int from_tty,
2829 struct cmd_list_element *c)
2831 target_set_disconnected_tracing (disconnected_tracing);
2834 static void
2835 set_circular_trace_buffer (const char *args, int from_tty,
2836 struct cmd_list_element *c)
2838 target_set_circular_trace_buffer (circular_trace_buffer);
2841 static void
2842 set_trace_buffer_size (const char *args, int from_tty,
2843 struct cmd_list_element *c)
2845 target_set_trace_buffer_size (trace_buffer_size);
2848 static void
2849 set_trace_user (const char *args, int from_tty,
2850 struct cmd_list_element *c)
2852 int ret;
2854 ret = target_set_trace_notes (trace_user.c_str (), NULL, NULL);
2856 if (!ret)
2857 warning (_("Target does not support trace notes, user ignored"));
2860 static void
2861 set_trace_notes (const char *args, int from_tty,
2862 struct cmd_list_element *c)
2864 int ret;
2866 ret = target_set_trace_notes (NULL, trace_notes.c_str (), NULL);
2868 if (!ret)
2869 warning (_("Target does not support trace notes, note ignored"));
2872 static void
2873 set_trace_stop_notes (const char *args, int from_tty,
2874 struct cmd_list_element *c)
2876 int ret;
2878 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes.c_str ());
2880 if (!ret)
2881 warning (_("Target does not support trace notes, stop note ignored"));
2885 get_traceframe_number (void)
2887 return traceframe_number;
2891 get_tracepoint_number (void)
2893 return tracepoint_number;
2896 /* Make the traceframe NUM be the current trace frame. Does nothing
2897 if NUM is already current. */
2899 void
2900 set_current_traceframe (int num)
2902 int newnum;
2904 if (traceframe_number == num)
2906 /* Nothing to do. */
2907 return;
2910 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
2912 if (newnum != num)
2913 warning (_("could not change traceframe"));
2915 set_traceframe_num (newnum);
2917 /* Changing the traceframe changes our view of registers and of the
2918 frame chain. */
2919 registers_changed ();
2921 clear_traceframe_info ();
2924 scoped_restore_current_traceframe::scoped_restore_current_traceframe ()
2925 : m_traceframe_number (traceframe_number)
2928 /* Given a number and address, return an uploaded tracepoint with that
2929 number, creating if necessary. */
2931 struct uploaded_tp *
2932 get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
2934 struct uploaded_tp *utp;
2936 for (utp = *utpp; utp; utp = utp->next)
2937 if (utp->number == num && utp->addr == addr)
2938 return utp;
2940 utp = new uploaded_tp;
2941 utp->number = num;
2942 utp->addr = addr;
2943 utp->next = *utpp;
2944 *utpp = utp;
2946 return utp;
2949 void
2950 free_uploaded_tps (struct uploaded_tp **utpp)
2952 struct uploaded_tp *next_one;
2954 while (*utpp)
2956 next_one = (*utpp)->next;
2957 delete *utpp;
2958 *utpp = next_one;
2962 /* Given a number and address, return an uploaded tracepoint with that
2963 number, creating if necessary. */
2965 struct uploaded_tsv *
2966 get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
2968 struct uploaded_tsv *utsv;
2970 for (utsv = *utsvp; utsv; utsv = utsv->next)
2971 if (utsv->number == num)
2972 return utsv;
2974 utsv = XCNEW (struct uploaded_tsv);
2975 utsv->number = num;
2976 utsv->next = *utsvp;
2977 *utsvp = utsv;
2979 return utsv;
2982 void
2983 free_uploaded_tsvs (struct uploaded_tsv **utsvp)
2985 struct uploaded_tsv *next_one;
2987 while (*utsvp)
2989 next_one = (*utsvp)->next;
2990 xfree (*utsvp);
2991 *utsvp = next_one;
2995 /* FIXME this function is heuristic and will miss the cases where the
2996 conditional is semantically identical but differs in whitespace,
2997 such as "x == 0" vs "x==0". */
2999 static int
3000 cond_string_is_same (char *str1, char *str2)
3002 if (str1 == NULL || str2 == NULL)
3003 return (str1 == str2);
3005 return (strcmp (str1, str2) == 0);
3008 /* Look for an existing tracepoint that seems similar enough to the
3009 uploaded one. Enablement isn't compared, because the user can
3010 toggle that freely, and may have done so in anticipation of the
3011 next trace run. Return the location of matched tracepoint. */
3013 static struct bp_location *
3014 find_matching_tracepoint_location (struct uploaded_tp *utp)
3016 for (breakpoint &b : all_tracepoints ())
3018 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
3020 if (b.type == utp->type
3021 && t.step_count == utp->step
3022 && t.pass_count == utp->pass
3023 && cond_string_is_same (t.cond_string.get (),
3024 utp->cond_string.get ())
3025 /* FIXME also test actions. */
3028 /* Scan the locations for an address match. */
3029 for (bp_location &loc : b.locations ())
3030 if (loc.address == utp->addr)
3031 return &loc;
3034 return NULL;
3037 /* Given a list of tracepoints uploaded from a target, attempt to
3038 match them up with existing tracepoints, and create new ones if not
3039 found. */
3041 void
3042 merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
3044 struct uploaded_tp *utp;
3045 /* A set of tracepoints which are modified. */
3046 std::vector<breakpoint *> modified_tp;
3048 /* Look for GDB tracepoints that match up with our uploaded versions. */
3049 for (utp = *uploaded_tps; utp; utp = utp->next)
3051 struct bp_location *loc;
3052 struct tracepoint *t;
3054 loc = find_matching_tracepoint_location (utp);
3055 if (loc)
3057 int found = 0;
3059 /* Mark this location as already inserted. */
3060 loc->inserted = 1;
3061 t = gdb::checked_static_cast<tracepoint *> (loc->owner);
3062 gdb_printf (_("Assuming tracepoint %d is same "
3063 "as target's tracepoint %d at %s.\n"),
3064 loc->owner->number, utp->number,
3065 paddress (loc->gdbarch, utp->addr));
3067 /* The tracepoint LOC->owner was modified (the location LOC
3068 was marked as inserted in the target). Save it in
3069 MODIFIED_TP if not there yet. The 'breakpoint-modified'
3070 observers will be notified later once for each tracepoint
3071 saved in MODIFIED_TP. */
3072 for (breakpoint *b : modified_tp)
3073 if (b == loc->owner)
3075 found = 1;
3076 break;
3078 if (!found)
3079 modified_tp.push_back (loc->owner);
3081 else
3083 t = create_tracepoint_from_upload (utp);
3084 if (t)
3085 gdb_printf (_("Created tracepoint %d for "
3086 "target's tracepoint %d at %s.\n"),
3087 t->number, utp->number,
3088 paddress (get_current_arch (), utp->addr));
3089 else
3090 gdb_printf (_("Failed to create tracepoint for target's "
3091 "tracepoint %d at %s, skipping it.\n"),
3092 utp->number,
3093 paddress (get_current_arch (), utp->addr));
3095 /* Whether found or created, record the number used by the
3096 target, to help with mapping target tracepoints back to their
3097 counterparts here. */
3098 if (t)
3099 t->number_on_target = utp->number;
3102 /* Notify 'breakpoint-modified' observer that at least one of B's
3103 locations was changed. */
3104 for (breakpoint *b : modified_tp)
3105 notify_breakpoint_modified (b);
3107 free_uploaded_tps (uploaded_tps);
3110 /* Trace state variables don't have much to identify them beyond their
3111 name, so just use that to detect matches. */
3113 static struct trace_state_variable *
3114 find_matching_tsv (struct uploaded_tsv *utsv)
3116 if (!utsv->name)
3117 return NULL;
3119 return find_trace_state_variable (utsv->name);
3122 static struct trace_state_variable *
3123 create_tsv_from_upload (struct uploaded_tsv *utsv)
3125 const char *namebase;
3126 std::string buf;
3127 int try_num = 0;
3128 struct trace_state_variable *tsv;
3130 if (utsv->name)
3132 namebase = utsv->name;
3133 buf = namebase;
3135 else
3137 namebase = "__tsv";
3138 buf = string_printf ("%s_%d", namebase, try_num++);
3141 /* Fish for a name that is not in use. */
3142 /* (should check against all internal vars?) */
3143 while (find_trace_state_variable (buf.c_str ()))
3144 buf = string_printf ("%s_%d", namebase, try_num++);
3146 /* We have an available name, create the variable. */
3147 tsv = create_trace_state_variable (buf.c_str ());
3148 tsv->initial_value = utsv->initial_value;
3149 tsv->builtin = utsv->builtin;
3151 interps_notify_tsv_created (tsv);
3153 return tsv;
3156 /* Given a list of uploaded trace state variables, try to match them
3157 up with existing variables, or create additional ones. */
3159 void
3160 merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
3162 struct uploaded_tsv *utsv;
3163 int highest;
3165 /* Most likely some numbers will have to be reassigned as part of
3166 the merge, so clear them all in anticipation. */
3167 for (trace_state_variable &tsv : tvariables)
3168 tsv.number = 0;
3170 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
3172 struct trace_state_variable *tsv = find_matching_tsv (utsv);
3173 if (tsv)
3175 if (info_verbose)
3176 gdb_printf (_("Assuming trace state variable $%s "
3177 "is same as target's variable %d.\n"),
3178 tsv->name.c_str (), utsv->number);
3180 else
3182 tsv = create_tsv_from_upload (utsv);
3183 if (info_verbose)
3184 gdb_printf (_("Created trace state variable "
3185 "$%s for target's variable %d.\n"),
3186 tsv->name.c_str (), utsv->number);
3188 /* Give precedence to numberings that come from the target. */
3189 if (tsv)
3190 tsv->number = utsv->number;
3193 /* Renumber everything that didn't get a target-assigned number. */
3194 highest = 0;
3195 for (const trace_state_variable &tsv : tvariables)
3196 highest = std::max (tsv.number, highest);
3198 ++highest;
3199 for (trace_state_variable &tsv : tvariables)
3200 if (tsv.number == 0)
3201 tsv.number = highest++;
3203 free_uploaded_tsvs (uploaded_tsvs);
3206 /* Parse the part of trace status syntax that is shared between
3207 the remote protocol and the trace file reader. */
3209 void
3210 parse_trace_status (const char *line, struct trace_status *ts)
3212 const char *p = line, *p1, *p2, *p3, *p_temp;
3213 int end;
3214 ULONGEST val;
3216 ts->running_known = 1;
3217 ts->running = (*p++ == '1');
3218 ts->stop_reason = trace_stop_reason_unknown;
3219 xfree (ts->stop_desc);
3220 ts->stop_desc = NULL;
3221 ts->traceframe_count = -1;
3222 ts->traceframes_created = -1;
3223 ts->buffer_free = -1;
3224 ts->buffer_size = -1;
3225 ts->disconnected_tracing = 0;
3226 ts->circular_buffer = 0;
3227 xfree (ts->user_name);
3228 ts->user_name = NULL;
3229 xfree (ts->notes);
3230 ts->notes = NULL;
3231 ts->start_time = ts->stop_time = 0;
3233 while (*p++)
3235 p1 = strchr (p, ':');
3236 if (p1 == NULL)
3237 error (_("Malformed trace status, at %s\n\
3238 Status line: '%s'\n"), p, line);
3239 p3 = strchr (p, ';');
3240 if (p3 == NULL)
3241 p3 = p + strlen (p);
3242 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
3244 p = unpack_varlen_hex (++p1, &val);
3245 ts->stop_reason = trace_buffer_full;
3247 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
3249 p = unpack_varlen_hex (++p1, &val);
3250 ts->stop_reason = trace_never_run;
3252 else if (strncmp (p, stop_reason_names[tracepoint_passcount],
3253 p1 - p) == 0)
3255 p = unpack_varlen_hex (++p1, &val);
3256 ts->stop_reason = tracepoint_passcount;
3257 ts->stopping_tracepoint = val;
3259 else if (strncmp (p, stop_reason_names[trace_stop_command], p1 - p) == 0)
3261 p2 = strchr (++p1, ':');
3262 if (!p2 || p2 > p3)
3264 /*older style*/
3265 p2 = p1;
3267 else if (p2 != p1)
3269 ts->stop_desc = (char *) xmalloc (strlen (line));
3270 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3271 ts->stop_desc[end] = '\0';
3273 else
3274 ts->stop_desc = xstrdup ("");
3276 p = unpack_varlen_hex (++p2, &val);
3277 ts->stop_reason = trace_stop_command;
3279 else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0)
3281 p = unpack_varlen_hex (++p1, &val);
3282 ts->stop_reason = trace_disconnected;
3284 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
3286 p2 = strchr (++p1, ':');
3287 if (p2 != p1)
3289 ts->stop_desc = (char *) xmalloc ((p2 - p1) / 2 + 1);
3290 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
3291 ts->stop_desc[end] = '\0';
3293 else
3294 ts->stop_desc = xstrdup ("");
3296 p = unpack_varlen_hex (++p2, &val);
3297 ts->stopping_tracepoint = val;
3298 ts->stop_reason = tracepoint_error;
3300 else if (strncmp (p, "tframes", p1 - p) == 0)
3302 p = unpack_varlen_hex (++p1, &val);
3303 ts->traceframe_count = val;
3305 else if (strncmp (p, "tcreated", p1 - p) == 0)
3307 p = unpack_varlen_hex (++p1, &val);
3308 ts->traceframes_created = val;
3310 else if (strncmp (p, "tfree", p1 - p) == 0)
3312 p = unpack_varlen_hex (++p1, &val);
3313 ts->buffer_free = val;
3315 else if (strncmp (p, "tsize", p1 - p) == 0)
3317 p = unpack_varlen_hex (++p1, &val);
3318 ts->buffer_size = val;
3320 else if (strncmp (p, "disconn", p1 - p) == 0)
3322 p = unpack_varlen_hex (++p1, &val);
3323 ts->disconnected_tracing = val;
3325 else if (strncmp (p, "circular", p1 - p) == 0)
3327 p = unpack_varlen_hex (++p1, &val);
3328 ts->circular_buffer = val;
3330 else if (strncmp (p, "starttime", p1 - p) == 0)
3332 p = unpack_varlen_hex (++p1, &val);
3333 ts->start_time = val;
3335 else if (strncmp (p, "stoptime", p1 - p) == 0)
3337 p = unpack_varlen_hex (++p1, &val);
3338 ts->stop_time = val;
3340 else if (strncmp (p, "username", p1 - p) == 0)
3342 ++p1;
3343 ts->user_name = (char *) xmalloc (strlen (p) / 2);
3344 end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2);
3345 ts->user_name[end] = '\0';
3346 p = p3;
3348 else if (strncmp (p, "notes", p1 - p) == 0)
3350 ++p1;
3351 ts->notes = (char *) xmalloc (strlen (p) / 2);
3352 end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2);
3353 ts->notes[end] = '\0';
3354 p = p3;
3356 else
3358 /* Silently skip unknown optional info. */
3359 p_temp = strchr (p1 + 1, ';');
3360 if (p_temp)
3361 p = p_temp;
3362 else
3363 /* Must be at the end. */
3364 break;
3369 void
3370 parse_tracepoint_status (const char *p, tracepoint *tp,
3371 struct uploaded_tp *utp)
3373 ULONGEST uval;
3375 p = unpack_varlen_hex (p, &uval);
3376 if (tp)
3377 tp->hit_count += uval;
3378 else
3379 utp->hit_count += uval;
3380 p = unpack_varlen_hex (p + 1, &uval);
3381 if (tp)
3382 tp->traceframe_usage += uval;
3383 else
3384 utp->traceframe_usage += uval;
3385 /* Ignore any extra, allowing for future extensions. */
3388 /* Given a line of text defining a part of a tracepoint, parse it into
3389 an "uploaded tracepoint". */
3391 void
3392 parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
3394 const char *p;
3395 char piece;
3396 ULONGEST num, addr, step, pass, orig_size, xlen, start;
3397 int enabled, end;
3398 enum bptype type;
3399 const char *srctype;
3400 char *buf;
3401 struct uploaded_tp *utp = NULL;
3403 p = line;
3404 /* Both tracepoint and action definitions start with the same number
3405 and address sequence. */
3406 piece = *p++;
3407 p = unpack_varlen_hex (p, &num);
3408 p++; /* skip a colon */
3409 p = unpack_varlen_hex (p, &addr);
3410 p++; /* skip a colon */
3411 if (piece == 'T')
3413 gdb::unique_xmalloc_ptr<char[]> cond;
3415 enabled = (*p++ == 'E');
3416 p++; /* skip a colon */
3417 p = unpack_varlen_hex (p, &step);
3418 p++; /* skip a colon */
3419 p = unpack_varlen_hex (p, &pass);
3420 type = bp_tracepoint;
3421 /* Thumb through optional fields. */
3422 while (*p == ':')
3424 p++; /* skip a colon */
3425 if (*p == 'F')
3427 type = bp_fast_tracepoint;
3428 p++;
3429 p = unpack_varlen_hex (p, &orig_size);
3431 else if (*p == 'S')
3433 type = bp_static_tracepoint;
3434 p++;
3436 else if (*p == 'X')
3438 p++;
3439 p = unpack_varlen_hex (p, &xlen);
3440 p++; /* skip a comma */
3441 cond.reset ((char *) xmalloc (2 * xlen + 1));
3442 strncpy (&cond[0], p, 2 * xlen);
3443 cond[2 * xlen] = '\0';
3444 p += 2 * xlen;
3446 else
3447 warning (_("Unrecognized char '%c' in tracepoint "
3448 "definition, skipping rest"), *p);
3450 utp = get_uploaded_tp (num, addr, utpp);
3451 utp->type = type;
3452 utp->enabled = enabled;
3453 utp->step = step;
3454 utp->pass = pass;
3455 utp->cond = std::move (cond);
3457 else if (piece == 'A')
3459 utp = get_uploaded_tp (num, addr, utpp);
3460 utp->actions.emplace_back (xstrdup (p));
3462 else if (piece == 'S')
3464 utp = get_uploaded_tp (num, addr, utpp);
3465 utp->step_actions.emplace_back (xstrdup (p));
3467 else if (piece == 'Z')
3469 /* Parse a chunk of source form definition. */
3470 utp = get_uploaded_tp (num, addr, utpp);
3471 srctype = p;
3472 p = strchr (p, ':');
3473 p++; /* skip a colon */
3474 p = unpack_varlen_hex (p, &start);
3475 p++; /* skip a colon */
3476 p = unpack_varlen_hex (p, &xlen);
3477 p++; /* skip a colon */
3479 buf = (char *) alloca (strlen (line));
3481 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3482 buf[end] = '\0';
3484 if (startswith (srctype, "at:"))
3485 utp->at_string.reset (xstrdup (buf));
3486 else if (startswith (srctype, "cond:"))
3487 utp->cond_string.reset (xstrdup (buf));
3488 else if (startswith (srctype, "cmd:"))
3489 utp->cmd_strings.emplace_back (xstrdup (buf));
3491 else if (piece == 'V')
3493 utp = get_uploaded_tp (num, addr, utpp);
3495 parse_tracepoint_status (p, NULL, utp);
3497 else
3499 /* Don't error out, the target might be sending us optional
3500 info that we don't care about. */
3501 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
3505 /* Convert a textual description of a trace state variable into an
3506 uploaded object. */
3508 void
3509 parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp)
3511 const char *p;
3512 char *buf;
3513 ULONGEST num, initval, builtin;
3514 int end;
3515 struct uploaded_tsv *utsv = NULL;
3517 buf = (char *) alloca (strlen (line));
3519 p = line;
3520 p = unpack_varlen_hex (p, &num);
3521 p++; /* skip a colon */
3522 p = unpack_varlen_hex (p, &initval);
3523 p++; /* skip a colon */
3524 p = unpack_varlen_hex (p, &builtin);
3525 p++; /* skip a colon */
3526 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3527 buf[end] = '\0';
3529 utsv = get_uploaded_tsv (num, utsvp);
3530 utsv->initial_value = initval;
3531 utsv->builtin = builtin;
3532 utsv->name = xstrdup (buf);
3535 /* Given a line of text defining a static tracepoint marker, parse it
3536 into a "static tracepoint marker" object. Throws an error is
3537 parsing fails. If PP is non-null, it points to one past the end of
3538 the parsed marker definition. */
3540 void
3541 parse_static_tracepoint_marker_definition (const char *line, const char **pp,
3542 static_tracepoint_marker *marker)
3544 const char *p, *endp;
3545 ULONGEST addr;
3547 p = line;
3548 p = unpack_varlen_hex (p, &addr);
3549 p++; /* skip a colon */
3551 marker->gdbarch = current_inferior ()->arch ();
3552 marker->address = (CORE_ADDR) addr;
3554 endp = strchr (p, ':');
3555 if (endp == NULL)
3556 error (_("bad marker definition: %s"), line);
3558 marker->str_id = hex2str (p, (endp - p) / 2);
3560 p = endp;
3561 p++; /* skip a colon */
3563 /* This definition may be followed by another one, separated by a comma. */
3564 int hex_len;
3565 endp = strchr (p, ',');
3566 if (endp != nullptr)
3567 hex_len = endp - p;
3568 else
3569 hex_len = strlen (p);
3571 marker->extra = hex2str (p, hex_len / 2);
3573 if (pp != nullptr)
3574 *pp = p + hex_len;
3577 /* Print MARKER to gdb_stdout. */
3579 static void
3580 print_one_static_tracepoint_marker (int count,
3581 const static_tracepoint_marker &marker)
3583 struct symbol *sym;
3585 struct ui_out *uiout = current_uiout;
3587 symtab_and_line sal;
3588 sal.pc = marker.address;
3590 std::vector<breakpoint *> tracepoints
3591 = static_tracepoints_here (marker.address);
3593 ui_out_emit_tuple tuple_emitter (uiout, "marker");
3595 /* A counter field to help readability. This is not a stable
3596 identifier! */
3597 uiout->field_signed ("count", count);
3599 uiout->field_string ("marker-id", marker.str_id);
3601 uiout->field_fmt ("enabled", "%c",
3602 !tracepoints.empty () ? 'y' : 'n');
3603 uiout->spaces (2);
3605 int wrap_indent = 35;
3606 if (gdbarch_addr_bit (marker.gdbarch) <= 32)
3607 wrap_indent += 11;
3608 else
3609 wrap_indent += 19;
3611 const char *extra_field_indent = " ";
3613 uiout->field_core_addr ("addr", marker.gdbarch, marker.address);
3615 sal = find_pc_line (marker.address, 0);
3616 sym = find_pc_sect_function (marker.address, NULL);
3617 if (sym)
3619 uiout->text ("in ");
3620 uiout->field_string ("func", sym->print_name (),
3621 function_name_style.style ());
3622 uiout->wrap_hint (wrap_indent);
3623 uiout->text (" at ");
3625 else
3626 uiout->field_skip ("func");
3628 if (sal.symtab != NULL)
3630 uiout->field_string ("file",
3631 symtab_to_filename_for_display (sal.symtab),
3632 file_name_style.style ());
3633 uiout->text (":");
3635 if (uiout->is_mi_like_p ())
3637 const char *fullname = symtab_to_fullname (sal.symtab);
3639 uiout->field_string ("fullname", fullname);
3641 else
3642 uiout->field_skip ("fullname");
3644 uiout->field_signed ("line", sal.line);
3646 else
3648 uiout->field_skip ("fullname");
3649 uiout->field_skip ("line");
3652 uiout->text ("\n");
3653 uiout->text (extra_field_indent);
3654 uiout->text (_("Data: \""));
3655 uiout->field_string ("extra-data", marker.extra);
3656 uiout->text ("\"\n");
3658 if (!tracepoints.empty ())
3660 int ix;
3663 ui_out_emit_tuple inner_tuple_emitter (uiout, "tracepoints-at");
3665 uiout->text (extra_field_indent);
3666 uiout->text (_("Probed by static tracepoints: "));
3667 for (ix = 0; ix < tracepoints.size (); ix++)
3669 if (ix > 0)
3670 uiout->text (", ");
3671 uiout->text ("#");
3672 uiout->field_signed ("tracepoint-id", tracepoints[ix]->number);
3676 if (uiout->is_mi_like_p ())
3677 uiout->field_signed ("number-of-tracepoints", tracepoints.size ());
3678 else
3679 uiout->text ("\n");
3683 static void
3684 info_static_tracepoint_markers_command (const char *arg, int from_tty)
3686 struct ui_out *uiout = current_uiout;
3687 std::vector<static_tracepoint_marker> markers
3688 = target_static_tracepoint_markers_by_strid (NULL);
3690 /* We don't have to check target_can_use_agent and agent's capability on
3691 static tracepoint here, in order to be compatible with older GDBserver.
3692 We don't check USE_AGENT is true or not, because static tracepoints
3693 don't work without in-process agent, so we don't bother users to type
3694 `set agent on' when to use static tracepoint. */
3696 ui_out_emit_table table_emitter (uiout, 5, -1,
3697 "StaticTracepointMarkersTable");
3699 uiout->table_header (7, ui_left, "counter", "Cnt");
3701 uiout->table_header (40, ui_left, "marker-id", "ID");
3703 uiout->table_header (3, ui_left, "enabled", "Enb");
3704 if (gdbarch_addr_bit (current_inferior ()->arch ()) <= 32)
3705 uiout->table_header (10, ui_left, "addr", "Address");
3706 else
3707 uiout->table_header (18, ui_left, "addr", "Address");
3708 uiout->table_header (40, ui_noalign, "what", "What");
3710 uiout->table_body ();
3712 for (int i = 0; i < markers.size (); i++)
3713 print_one_static_tracepoint_marker (i + 1, markers[i]);
3716 /* The $_sdata convenience variable is a bit special. We don't know
3717 for sure type of the value until we actually have a chance to fetch
3718 the data --- the size of the object depends on what has been
3719 collected. We solve this by making $_sdata be an internalvar that
3720 creates a new value on access. */
3722 /* Return a new value with the correct type for the sdata object of
3723 the current trace frame. Return a void value if there's no object
3724 available. */
3726 static struct value *
3727 sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var,
3728 void *ignore)
3730 /* We need to read the whole object before we know its size. */
3731 std::optional<gdb::byte_vector> buf
3732 = target_read_alloc (current_inferior ()->top_target (),
3733 TARGET_OBJECT_STATIC_TRACE_DATA,
3734 NULL);
3735 if (buf)
3737 struct value *v;
3738 struct type *type;
3740 type = init_vector_type (builtin_type (gdbarch)->builtin_true_char,
3741 buf->size ());
3742 v = value::allocate (type);
3743 memcpy (v->contents_raw ().data (), buf->data (), buf->size ());
3744 return v;
3746 else
3747 return value::allocate (builtin_type (gdbarch)->builtin_void);
3750 #if !defined(HAVE_LIBEXPAT)
3752 struct std::unique_ptr<traceframe_info>
3753 parse_traceframe_info (const char *tframe_info)
3755 static int have_warned;
3757 if (!have_warned)
3759 have_warned = 1;
3760 warning (_("Can not parse XML trace frame info; XML support "
3761 "was disabled at compile time"));
3764 return NULL;
3767 #else /* HAVE_LIBEXPAT */
3769 #include "xml-support.h"
3771 /* Handle the start of a <memory> element. */
3773 static void
3774 traceframe_info_start_memory (struct gdb_xml_parser *parser,
3775 const struct gdb_xml_element *element,
3776 void *user_data,
3777 std::vector<gdb_xml_value> &attributes)
3779 struct traceframe_info *info = (struct traceframe_info *) user_data;
3780 ULONGEST *start_p, *length_p;
3782 start_p
3783 = (ULONGEST *) xml_find_attribute (attributes, "start")->value.get ();
3784 length_p
3785 = (ULONGEST *) xml_find_attribute (attributes, "length")->value.get ();
3787 info->memory.emplace_back (*start_p, *length_p);
3790 /* Handle the start of a <tvar> element. */
3792 static void
3793 traceframe_info_start_tvar (struct gdb_xml_parser *parser,
3794 const struct gdb_xml_element *element,
3795 void *user_data,
3796 std::vector<gdb_xml_value> &attributes)
3798 struct traceframe_info *info = (struct traceframe_info *) user_data;
3799 const char *id_attrib
3800 = (const char *) xml_find_attribute (attributes, "id")->value.get ();
3801 int id = gdb_xml_parse_ulongest (parser, id_attrib);
3803 info->tvars.push_back (id);
3806 /* The allowed elements and attributes for an XML memory map. */
3808 static const struct gdb_xml_attribute memory_attributes[] = {
3809 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
3810 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
3811 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3814 static const struct gdb_xml_attribute tvar_attributes[] = {
3815 { "id", GDB_XML_AF_NONE, NULL, NULL },
3816 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3819 static const struct gdb_xml_element traceframe_info_children[] = {
3820 { "memory", memory_attributes, NULL,
3821 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3822 traceframe_info_start_memory, NULL },
3823 { "tvar", tvar_attributes, NULL,
3824 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3825 traceframe_info_start_tvar, NULL },
3826 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3829 static const struct gdb_xml_element traceframe_info_elements[] = {
3830 { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE,
3831 NULL, NULL },
3832 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3835 /* Parse a traceframe-info XML document. */
3837 traceframe_info_up
3838 parse_traceframe_info (const char *tframe_info)
3840 traceframe_info_up result (new traceframe_info);
3842 if (gdb_xml_parse_quick (_("trace frame info"),
3843 "traceframe-info.dtd", traceframe_info_elements,
3844 tframe_info, result.get ()) == 0)
3845 return result;
3847 return NULL;
3850 #endif /* HAVE_LIBEXPAT */
3852 /* Returns the traceframe_info object for the current traceframe.
3853 This is where we avoid re-fetching the object from the target if we
3854 already have it cached. */
3856 struct traceframe_info *
3857 get_traceframe_info (void)
3859 if (current_traceframe_info == NULL)
3860 current_traceframe_info = target_traceframe_info ();
3862 return current_traceframe_info.get ();
3865 /* If the target supports the query, return in RESULT the set of
3866 collected memory in the current traceframe, found within the LEN
3867 bytes range starting at MEMADDR. Returns true if the target
3868 supports the query, otherwise returns false, and RESULT is left
3869 undefined. */
3872 traceframe_available_memory (std::vector<mem_range> *result,
3873 CORE_ADDR memaddr, ULONGEST len)
3875 struct traceframe_info *info = get_traceframe_info ();
3877 if (info != NULL)
3879 result->clear ();
3881 for (mem_range &r : info->memory)
3882 if (mem_ranges_overlap (r.start, r.length, memaddr, len))
3884 ULONGEST lo1, hi1, lo2, hi2;
3886 lo1 = memaddr;
3887 hi1 = memaddr + len;
3889 lo2 = r.start;
3890 hi2 = r.start + r.length;
3892 CORE_ADDR start = std::max (lo1, lo2);
3893 int length = std::min (hi1, hi2) - start;
3895 result->emplace_back (start, length);
3898 normalize_mem_ranges (result);
3899 return 1;
3902 return 0;
3905 /* Implementation of `sdata' variable. */
3907 static const struct internalvar_funcs sdata_funcs =
3909 sdata_make_value,
3910 NULL
3913 /* See tracepoint.h. */
3914 cmd_list_element *while_stepping_cmd_element = nullptr;
3916 /* module initialization */
3917 void _initialize_tracepoint ();
3918 void
3919 _initialize_tracepoint ()
3921 struct cmd_list_element *c;
3923 /* Explicitly create without lookup, since that tries to create a
3924 value with a void typed value, and when we get here, gdbarch
3925 isn't initialized yet. At this point, we're quite sure there
3926 isn't another convenience variable of the same name. */
3927 create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL);
3929 traceframe_number = -1;
3930 tracepoint_number = -1;
3932 add_info ("scope", info_scope_command,
3933 _("List the variables local to a scope."));
3935 add_cmd ("tracepoints", class_trace,
3936 _("Tracing of program execution without stopping the program."),
3937 &cmdlist);
3939 add_com ("tdump", class_trace, tdump_command,
3940 _("Print everything collected at the current tracepoint."));
3942 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
3943 Define a trace state variable.\n\
3944 Argument is a $-prefixed name, optionally followed\n\
3945 by '=' and an expression that sets the initial value\n\
3946 at the start of tracing."));
3947 set_cmd_completer (c, expression_completer);
3949 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
3950 Delete one or more trace state variables.\n\
3951 Arguments are the names of the variables to delete.\n\
3952 If no arguments are supplied, delete all variables."), &deletelist);
3953 /* FIXME add a trace variable completer. */
3955 add_info ("tvariables", info_tvariables_command, _("\
3956 Status of trace state variables and their values."));
3958 add_info ("static-tracepoint-markers",
3959 info_static_tracepoint_markers_command, _("\
3960 List target static tracepoints markers."));
3962 add_prefix_cmd ("tfind", class_trace, tfind_command, _("\
3963 Select a trace frame.\n\
3964 No argument means forward by one frame; '-' means backward by one frame."),
3965 &tfindlist, 1, &cmdlist);
3967 add_cmd ("outside", class_trace, tfind_outside_command, _("\
3968 Select a trace frame whose PC is outside the given range (exclusive).\n\
3969 Usage: tfind outside ADDR1, ADDR2"),
3970 &tfindlist);
3972 add_cmd ("range", class_trace, tfind_range_command, _("\
3973 Select a trace frame whose PC is in the given range (inclusive).\n\
3974 Usage: tfind range ADDR1, ADDR2"),
3975 &tfindlist);
3977 add_cmd ("line", class_trace, tfind_line_command, _("\
3978 Select a trace frame by source line.\n\
3979 Argument can be a line number (with optional source file),\n\
3980 a function name, or '*' followed by an address.\n\
3981 Default argument is 'the next source line that was traced'."),
3982 &tfindlist);
3984 add_cmd ("tracepoint", class_trace, tfind_tracepoint_command, _("\
3985 Select a trace frame by tracepoint number.\n\
3986 Default is the tracepoint for the current trace frame."),
3987 &tfindlist);
3989 add_cmd ("pc", class_trace, tfind_pc_command, _("\
3990 Select a trace frame by PC.\n\
3991 Default is the current PC, or the PC of the current trace frame."),
3992 &tfindlist);
3994 cmd_list_element *tfind_end_cmd
3995 = add_cmd ("end", class_trace, tfind_end_command, _("\
3996 De-select any trace frame and resume 'live' debugging."), &tfindlist);
3998 add_alias_cmd ("none", tfind_end_cmd, class_trace, 0, &tfindlist);
4000 add_cmd ("start", class_trace, tfind_start_command,
4001 _("Select the first trace frame in the trace buffer."),
4002 &tfindlist);
4004 add_com ("tstatus", class_trace, tstatus_command,
4005 _("Display the status of the current trace data collection."));
4007 add_com ("tstop", class_trace, tstop_command, _("\
4008 Stop trace data collection.\n\
4009 Usage: tstop [NOTES]...\n\
4010 Any arguments supplied are recorded with the trace as a stop reason and\n\
4011 reported by tstatus (if the target supports trace notes)."));
4013 add_com ("tstart", class_trace, tstart_command, _("\
4014 Start trace data collection.\n\
4015 Usage: tstart [NOTES]...\n\
4016 Any arguments supplied are recorded with the trace as a note and\n\
4017 reported by tstatus (if the target supports trace notes)."));
4019 add_com ("end", class_trace, end_actions_pseudocommand, _("\
4020 Ends a list of commands or actions.\n\
4021 Several GDB commands allow you to enter a list of commands or actions.\n\
4022 Entering \"end\" on a line by itself is the normal way to terminate\n\
4023 such a list.\n\n\
4024 Note: the \"end\" command cannot be used at the gdb prompt."));
4026 while_stepping_cmd_element = add_com ("while-stepping", class_trace,
4027 while_stepping_pseudocommand, _("\
4028 Specify single-stepping behavior at a tracepoint.\n\
4029 Argument is number of instructions to trace in single-step mode\n\
4030 following the tracepoint. This command is normally followed by\n\
4031 one or more \"collect\" commands, to specify what to collect\n\
4032 while single-stepping.\n\n\
4033 Note: this command can only be used in a tracepoint \"actions\" list."));
4035 add_com_alias ("ws", while_stepping_cmd_element, class_trace, 0);
4036 add_com_alias ("stepping", while_stepping_cmd_element, class_trace, 0);
4038 add_com ("collect", class_trace, collect_pseudocommand, _("\
4039 Specify one or more data items to be collected at a tracepoint.\n\
4040 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
4041 collect all data (variables, registers) referenced by that expression.\n\
4042 Also accepts the following special arguments:\n\
4043 $regs -- all registers.\n\
4044 $args -- all function arguments.\n\
4045 $locals -- all variables local to the block/function scope.\n\
4046 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
4047 Note: this command can only be used in a tracepoint \"actions\" list."));
4049 add_com ("teval", class_trace, teval_pseudocommand, _("\
4050 Specify one or more expressions to be evaluated at a tracepoint.\n\
4051 Accepts a comma-separated list of (one or more) expressions.\n\
4052 The result of each evaluation will be discarded.\n\
4053 Note: this command can only be used in a tracepoint \"actions\" list."));
4055 add_com ("actions", class_trace, actions_command, _("\
4056 Specify the actions to be taken at a tracepoint.\n\
4057 Tracepoint actions may include collecting of specified data,\n\
4058 single-stepping, or enabling/disabling other tracepoints,\n\
4059 depending on target's capabilities."));
4061 add_setshow_string_cmd ("default-collect", class_trace,
4062 &default_collect, _("\
4063 Set the list of expressions to collect by default."), _("\
4064 Show the list of expressions to collect by default."), NULL,
4065 NULL, NULL,
4066 &setlist, &showlist);
4068 add_setshow_boolean_cmd ("disconnected-tracing", no_class,
4069 &disconnected_tracing, _("\
4070 Set whether tracing continues after GDB disconnects."), _("\
4071 Show whether tracing continues after GDB disconnects."), _("\
4072 Use this to continue a tracing run even if GDB disconnects\n\
4073 or detaches from the target. You can reconnect later and look at\n\
4074 trace data collected in the meantime."),
4075 set_disconnected_tracing,
4076 NULL,
4077 &setlist,
4078 &showlist);
4080 add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
4081 &circular_trace_buffer, _("\
4082 Set target's use of circular trace buffer."), _("\
4083 Show target's use of circular trace buffer."), _("\
4084 Use this to make the trace buffer into a circular buffer,\n\
4085 which will discard traceframes (oldest first) instead of filling\n\
4086 up and stopping the trace run."),
4087 set_circular_trace_buffer,
4088 NULL,
4089 &setlist,
4090 &showlist);
4092 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class,
4093 &trace_buffer_size, _("\
4094 Set requested size of trace buffer."), _("\
4095 Show requested size of trace buffer."), _("\
4096 Use this to choose a size for the trace buffer. Some targets\n\
4097 may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
4098 disables any attempt to set the buffer size and lets the target choose."),
4099 set_trace_buffer_size, NULL,
4100 &setlist, &showlist);
4102 add_setshow_string_cmd ("trace-user", class_trace,
4103 &trace_user, _("\
4104 Set the user name to use for current and future trace runs."), _("\
4105 Show the user name to use for current and future trace runs."), NULL,
4106 set_trace_user, NULL,
4107 &setlist, &showlist);
4109 add_setshow_string_cmd ("trace-notes", class_trace,
4110 &trace_notes, _("\
4111 Set notes string to use for current and future trace runs."), _("\
4112 Show the notes string to use for current and future trace runs."), NULL,
4113 set_trace_notes, NULL,
4114 &setlist, &showlist);
4116 add_setshow_string_cmd ("trace-stop-notes", class_trace,
4117 &trace_stop_notes, _("\
4118 Set notes string to use for future tstop commands."), _("\
4119 Show the notes string to use for future tstop commands."), NULL,
4120 set_trace_stop_notes, NULL,
4121 &setlist, &showlist);