1 /* Tracing functionality for remote targets in custom GDB protocol
3 Copyright (C) 1997-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "arch-utils.h"
25 #include "expression.h"
29 #include "target-dcache.h"
32 #include "breakpoint.h"
33 #include "tracepoint.h"
36 #include "completer.h"
38 #include "dictionary.h"
39 #include "observable.h"
40 #include "user-regs.h"
44 #include "filenames.h"
45 #include "gdbthread.h"
52 #include "cli/cli-utils.h"
54 #include "gdbsupport/filestuff.h"
55 #include "gdbsupport/rsp-low.h"
56 #include "tracefile.h"
59 #include "cli/cli-style.h"
64 /* Maximum length of an agent aexpression.
65 This accounts for the fact that packets are limited to 400 bytes
66 (which includes everything -- including the checksum), and assumes
67 the worst case of maximum length for each of the pieces of a
70 NOTE: expressions get mem2hex'ed otherwise this would be twice as
71 large. (400 - 31)/2 == 184 */
72 #define MAX_AGENT_EXPR_LEN 184
74 /* A hook used to notify the UI of tracepoint operations. */
76 void (*deprecated_trace_find_hook
) (char *arg
, int from_tty
);
77 void (*deprecated_trace_start_stop_hook
) (int start
, int from_tty
);
82 This module defines the following debugger commands:
83 trace : set a tracepoint on a function, line, or address.
84 info trace : list all debugger-defined tracepoints.
85 delete trace : delete one or more tracepoints.
86 enable trace : enable one or more tracepoints.
87 disable trace : disable one or more tracepoints.
88 actions : specify actions to be taken at a tracepoint.
89 passcount : specify a pass count for a tracepoint.
90 tstart : start a trace experiment.
91 tstop : stop a trace experiment.
92 tstatus : query the status of a trace experiment.
93 tfind : find a trace frame in the trace buffer.
94 tdump : print everything collected at the current tracepoint.
95 save-tracepoints : write tracepoint setup into a file.
97 This module defines the following user-visible debugger variables:
98 $trace_frame : sequence number of trace frame currently being debugged.
99 $trace_line : source line of trace frame currently being debugged.
100 $trace_file : source file of trace frame currently being debugged.
101 $tracepoint : tracepoint number of trace frame currently being debugged.
105 /* ======= Important global variables: ======= */
107 /* The list of all trace state variables. We don't retain pointers to
108 any of these for any reason - API is by name or number only - so it
109 works to have a vector of objects. */
111 static std::vector
<trace_state_variable
> tvariables
;
113 /* The next integer to assign to a variable. */
115 static int next_tsv_number
= 1;
117 /* Number of last traceframe collected. */
118 static int traceframe_number
;
120 /* Tracepoint for last traceframe collected. */
121 static int tracepoint_number
;
123 /* The traceframe info of the current traceframe. NULL if we haven't
124 yet attempted to fetch it, or if the target does not support
125 fetching this object, or if we're not inspecting a traceframe
127 static traceframe_info_up current_traceframe_info
;
129 /* Tracing command lists. */
130 static struct cmd_list_element
*tfindlist
;
132 /* List of expressions to collect by default at each tracepoint hit. */
133 char *default_collect
;
135 static bool disconnected_tracing
;
137 /* This variable controls whether we ask the target for a linear or
138 circular trace buffer. */
140 static bool circular_trace_buffer
;
142 /* This variable is the requested trace buffer size, or -1 to indicate
143 that we don't care and leave it up to the target to set a size. */
145 static int trace_buffer_size
= -1;
147 /* Textual notes applying to the current and/or future trace runs. */
149 static char *trace_user
= NULL
;
151 /* Textual notes applying to the current and/or future trace runs. */
153 static char *trace_notes
= NULL
;
155 /* Textual notes applying to the stopping of a trace. */
157 static char *trace_stop_notes
= NULL
;
159 /* support routines */
161 struct collection_list
;
162 static char *mem2hex (gdb_byte
*, char *, int);
164 static counted_command_line
all_tracepoint_actions (struct breakpoint
*);
166 static struct trace_status trace_status
;
168 const char *stop_reason_names
[] = {
178 struct trace_status
*
179 current_trace_status (void)
181 return &trace_status
;
184 /* Free and clear the traceframe info cache of the current
188 clear_traceframe_info (void)
190 current_traceframe_info
= NULL
;
193 /* Set traceframe number to NUM. */
195 set_traceframe_num (int num
)
197 traceframe_number
= num
;
198 set_internalvar_integer (lookup_internalvar ("trace_frame"), num
);
201 /* Set tracepoint number to NUM. */
203 set_tracepoint_num (int num
)
205 tracepoint_number
= num
;
206 set_internalvar_integer (lookup_internalvar ("tracepoint"), num
);
209 /* Set externally visible debug variables for querying/printing
210 the traceframe context (line, function, file). */
213 set_traceframe_context (struct frame_info
*trace_frame
)
216 struct symbol
*traceframe_fun
;
217 symtab_and_line traceframe_sal
;
219 /* Save as globals for internal use. */
220 if (trace_frame
!= NULL
221 && get_frame_pc_if_available (trace_frame
, &trace_pc
))
223 traceframe_sal
= find_pc_line (trace_pc
, 0);
224 traceframe_fun
= find_pc_function (trace_pc
);
226 /* Save linenumber as "$trace_line", a debugger variable visible to
228 set_internalvar_integer (lookup_internalvar ("trace_line"),
229 traceframe_sal
.line
);
233 traceframe_fun
= NULL
;
234 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
237 /* Save func name as "$trace_func", a debugger variable visible to
239 if (traceframe_fun
== NULL
240 || traceframe_fun
->linkage_name () == NULL
)
241 clear_internalvar (lookup_internalvar ("trace_func"));
243 set_internalvar_string (lookup_internalvar ("trace_func"),
244 traceframe_fun
->linkage_name ());
246 /* Save file name as "$trace_file", a debugger variable visible to
248 if (traceframe_sal
.symtab
== NULL
)
249 clear_internalvar (lookup_internalvar ("trace_file"));
251 set_internalvar_string (lookup_internalvar ("trace_file"),
252 symtab_to_filename_for_display (traceframe_sal
.symtab
));
255 /* Create a new trace state variable with the given name. */
257 struct trace_state_variable
*
258 create_trace_state_variable (const char *name
)
260 tvariables
.emplace_back (name
, next_tsv_number
++);
261 return &tvariables
.back ();
264 /* Look for a trace state variable of the given name. */
266 struct trace_state_variable
*
267 find_trace_state_variable (const char *name
)
269 for (trace_state_variable
&tsv
: tvariables
)
270 if (tsv
.name
== name
)
276 /* Look for a trace state variable of the given number. Return NULL if
279 struct trace_state_variable
*
280 find_trace_state_variable_by_number (int number
)
282 for (trace_state_variable
&tsv
: tvariables
)
283 if (tsv
.number
== number
)
290 delete_trace_state_variable (const char *name
)
292 for (auto it
= tvariables
.begin (); it
!= tvariables
.end (); it
++)
293 if (it
->name
== name
)
295 gdb::observers::tsv_deleted
.notify (&*it
);
296 tvariables
.erase (it
);
300 warning (_("No trace variable named \"$%s\", not deleting"), name
);
303 /* Throws an error if NAME is not valid syntax for a trace state
307 validate_trace_state_variable_name (const char *name
)
312 error (_("Must supply a non-empty variable name"));
314 /* All digits in the name is reserved for value history
316 for (p
= name
; isdigit (*p
); p
++)
319 error (_("$%s is not a valid trace state variable name"), name
);
321 for (p
= name
; isalnum (*p
) || *p
== '_'; p
++)
324 error (_("$%s is not a valid trace state variable name"), name
);
327 /* The 'tvariable' command collects a name and optional expression to
328 evaluate into an initial value. */
331 trace_variable_command (const char *args
, int from_tty
)
334 struct trace_state_variable
*tsv
;
335 const char *name_start
, *p
;
338 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
340 /* Only allow two syntaxes; "$name" and "$name=value". */
341 p
= skip_spaces (args
);
344 error (_("Name of trace variable should start with '$'"));
347 while (isalnum (*p
) || *p
== '_')
349 std::string
name (name_start
, p
- name_start
);
352 if (*p
!= '=' && *p
!= '\0')
353 error (_("Syntax must be $NAME [ = EXPR ]"));
355 validate_trace_state_variable_name (name
.c_str ());
358 initval
= value_as_long (parse_and_eval (++p
));
360 /* If the variable already exists, just change its initial value. */
361 tsv
= find_trace_state_variable (name
.c_str ());
364 if (tsv
->initial_value
!= initval
)
366 tsv
->initial_value
= initval
;
367 gdb::observers::tsv_modified
.notify (tsv
);
369 printf_filtered (_("Trace state variable $%s "
370 "now has initial value %s.\n"),
371 tsv
->name
.c_str (), plongest (tsv
->initial_value
));
375 /* Create a new variable. */
376 tsv
= create_trace_state_variable (name
.c_str ());
377 tsv
->initial_value
= initval
;
379 gdb::observers::tsv_created
.notify (tsv
);
381 printf_filtered (_("Trace state variable $%s "
382 "created, with initial value %s.\n"),
383 tsv
->name
.c_str (), plongest (tsv
->initial_value
));
387 delete_trace_variable_command (const char *args
, int from_tty
)
391 if (query (_("Delete all trace state variables? ")))
394 gdb::observers::tsv_deleted
.notify (NULL
);
398 gdb_argv
argv (args
);
400 for (char *arg
: argv
)
403 delete_trace_state_variable (arg
+ 1);
405 warning (_("Name \"%s\" not prefixed with '$', ignoring"), arg
);
412 tvariables_info_1 (void)
414 struct ui_out
*uiout
= current_uiout
;
416 /* Try to acquire values from the target. */
417 for (trace_state_variable
&tsv
: tvariables
)
419 = target_get_trace_state_variable_value (tsv
.number
, &tsv
.value
);
422 ui_out_emit_table
table_emitter (uiout
, 3, tvariables
.size (),
424 uiout
->table_header (15, ui_left
, "name", "Name");
425 uiout
->table_header (11, ui_left
, "initial", "Initial");
426 uiout
->table_header (11, ui_left
, "current", "Current");
428 uiout
->table_body ();
430 for (const trace_state_variable
&tsv
: tvariables
)
434 ui_out_emit_tuple
tuple_emitter (uiout
, "variable");
436 uiout
->field_string ("name", std::string ("$") + tsv
.name
);
437 uiout
->field_string ("initial", plongest (tsv
.initial_value
));
441 c
= plongest (tsv
.value
);
442 else if (uiout
->is_mi_like_p ())
443 /* For MI, we prefer not to use magic string constants, but rather
444 omit the field completely. The difference between unknown and
445 undefined does not seem important enough to represent. */
447 else if (current_trace_status ()->running
|| traceframe_number
>= 0)
449 /* The value is/was defined, but we don't have it. */
451 style
= metadata_style
.style ();
455 /* It is not meaningful to ask about the value. */
457 style
= metadata_style
.style ();
460 uiout
->field_string ("current", c
, style
);
465 if (tvariables
.empty ())
466 uiout
->text (_("No trace state variables.\n"));
469 /* List all the trace state variables. */
472 info_tvariables_command (const char *args
, int from_tty
)
474 tvariables_info_1 ();
477 /* Stash definitions of tsvs into the given file. */
480 save_trace_state_variables (struct ui_file
*fp
)
482 for (const trace_state_variable
&tsv
: tvariables
)
484 fprintf_unfiltered (fp
, "tvariable $%s", tsv
.name
.c_str ());
485 if (tsv
.initial_value
)
486 fprintf_unfiltered (fp
, " = %s", plongest (tsv
.initial_value
));
487 fprintf_unfiltered (fp
, "\n");
491 /* ACTIONS functions: */
493 /* The three functions:
494 collect_pseudocommand,
495 while_stepping_pseudocommand, and
496 end_actions_pseudocommand
497 are placeholders for "commands" that are actually ONLY to be used
498 within a tracepoint action list. If the actual function is ever called,
499 it means that somebody issued the "command" at the top level,
500 which is always an error. */
503 end_actions_pseudocommand (const char *args
, int from_tty
)
505 error (_("This command cannot be used at the top level."));
509 while_stepping_pseudocommand (const char *args
, int from_tty
)
511 error (_("This command can only be used in a tracepoint actions list."));
515 collect_pseudocommand (const char *args
, int from_tty
)
517 error (_("This command can only be used in a tracepoint actions list."));
521 teval_pseudocommand (const char *args
, int from_tty
)
523 error (_("This command can only be used in a tracepoint actions list."));
526 /* Parse any collection options, such as /s for strings. */
529 decode_agent_options (const char *exp
, int *trace_string
)
531 struct value_print_options opts
;
538 /* Call this to borrow the print elements default for collection
540 get_user_print_options (&opts
);
545 if (target_supports_string_tracing ())
547 /* Allow an optional decimal number giving an explicit maximum
548 string length, defaulting it to the "print elements" value;
549 so "collect/s80 mystr" gets at most 80 bytes of string. */
550 *trace_string
= opts
.print_max
;
552 if (*exp
>= '0' && *exp
<= '9')
553 *trace_string
= atoi (exp
);
554 while (*exp
>= '0' && *exp
<= '9')
558 error (_("Target does not support \"/s\" option for string tracing."));
561 error (_("Undefined collection format \"%c\"."), *exp
);
563 exp
= skip_spaces (exp
);
568 /* Enter a list of actions for a tracepoint. */
570 actions_command (const char *args
, int from_tty
)
572 struct tracepoint
*t
;
574 t
= get_tracepoint_by_number (&args
, NULL
);
578 string_printf ("Enter actions for tracepoint %d, one per line.",
581 counted_command_line l
= read_command_lines (tmpbuf
.c_str (),
583 [=] (const char *line
)
585 validate_actionline (line
, t
);
587 breakpoint_set_commands (t
, std::move (l
));
589 /* else just return */
592 /* Report the results of checking the agent expression, as errors or
596 report_agent_reqs_errors (struct agent_expr
*aexpr
)
598 /* All of the "flaws" are serious bytecode generation issues that
599 should never occur. */
600 if (aexpr
->flaw
!= agent_flaw_none
)
601 internal_error (__FILE__
, __LINE__
, _("expression is malformed"));
603 /* If analysis shows a stack underflow, GDB must have done something
604 badly wrong in its bytecode generation. */
605 if (aexpr
->min_height
< 0)
606 internal_error (__FILE__
, __LINE__
,
607 _("expression has min height < 0"));
609 /* Issue this error if the stack is predicted to get too deep. The
610 limit is rather arbitrary; a better scheme might be for the
611 target to report how much stack it will have available. The
612 depth roughly corresponds to parenthesization, so a limit of 20
613 amounts to 20 levels of expression nesting, which is actually
614 a pretty big hairy expression. */
615 if (aexpr
->max_height
> 20)
616 error (_("Expression is too complicated."));
619 /* Call ax_reqs on AEXPR and raise an error if something is wrong. */
622 finalize_tracepoint_aexpr (struct agent_expr
*aexpr
)
626 if (aexpr
->len
> MAX_AGENT_EXPR_LEN
)
627 error (_("Expression is too complicated."));
629 report_agent_reqs_errors (aexpr
);
632 /* worker function */
634 validate_actionline (const char *line
, struct breakpoint
*b
)
636 struct cmd_list_element
*c
;
639 struct tracepoint
*t
= (struct tracepoint
*) b
;
641 /* If EOF is typed, *line is NULL. */
645 p
= skip_spaces (line
);
647 /* Symbol lookup etc. */
648 if (*p
== '\0') /* empty line: just prompt for another line. */
651 if (*p
== '#') /* comment line */
654 c
= lookup_cmd (&p
, cmdlist
, "", NULL
, -1, 1);
656 error (_("`%s' is not a tracepoint action, or is ambiguous."), p
);
658 if (cmd_cfunc_eq (c
, collect_pseudocommand
))
660 int trace_string
= 0;
663 p
= decode_agent_options (p
, &trace_string
);
666 { /* Repeat over a comma-separated list. */
667 QUIT
; /* Allow user to bail out with ^C. */
670 if (*p
== '$') /* Look for special pseudo-symbols. */
672 if (0 == strncasecmp ("reg", p
+ 1, 3)
673 || 0 == strncasecmp ("arg", p
+ 1, 3)
674 || 0 == strncasecmp ("loc", p
+ 1, 3)
675 || 0 == strncasecmp ("_ret", p
+ 1, 4)
676 || 0 == strncasecmp ("_sdata", p
+ 1, 6))
681 /* else fall thru, treat p as an expression and parse it! */
684 for (bp_location
*loc
: t
->locations ())
687 expression_up exp
= parse_exp_1 (&p
, loc
->address
,
688 block_for_pc (loc
->address
), 1);
690 if (exp
->first_opcode () == OP_VAR_VALUE
)
693 expr::var_value_operation
*vvop
694 = (dynamic_cast<expr::var_value_operation
*>
696 sym
= vvop
->get_symbol ();
698 if (SYMBOL_CLASS (sym
) == LOC_CONST
)
700 error (_("constant `%s' (value %s) "
701 "will not be collected."),
703 plongest (SYMBOL_VALUE (sym
)));
705 else if (SYMBOL_CLASS (sym
) == LOC_OPTIMIZED_OUT
)
707 error (_("`%s' is optimized away "
708 "and cannot be collected."),
713 /* We have something to collect, make sure that the expr to
714 bytecode translator can handle it and that it's not too
716 agent_expr_up aexpr
= gen_trace_for_expr (loc
->address
,
720 finalize_tracepoint_aexpr (aexpr
.get ());
723 while (p
&& *p
++ == ',');
726 else if (cmd_cfunc_eq (c
, teval_pseudocommand
))
729 { /* Repeat over a comma-separated list. */
730 QUIT
; /* Allow user to bail out with ^C. */
734 for (bp_location
*loc
: t
->locations ())
738 /* Only expressions are allowed for this action. */
739 expression_up exp
= parse_exp_1 (&p
, loc
->address
,
740 block_for_pc (loc
->address
), 1);
742 /* We have something to evaluate, make sure that the expr to
743 bytecode translator can handle it and that it's not too
745 agent_expr_up aexpr
= gen_eval_for_expr (loc
->address
, exp
.get ());
747 finalize_tracepoint_aexpr (aexpr
.get ());
750 while (p
&& *p
++ == ',');
753 else if (cmd_cfunc_eq (c
, while_stepping_pseudocommand
))
758 t
->step_count
= strtol (p
, &endp
, 0);
759 if (endp
== p
|| t
->step_count
== 0)
760 error (_("while-stepping step count `%s' is malformed."), line
);
764 else if (cmd_cfunc_eq (c
, end_actions_pseudocommand
))
768 error (_("`%s' is not a supported tracepoint action."), line
);
772 memrange_absolute
= -1
775 /* MEMRANGE functions: */
777 /* Compare memranges for std::sort. */
780 memrange_comp (const memrange
&a
, const memrange
&b
)
782 if (a
.type
== b
.type
)
784 if (a
.type
== memrange_absolute
)
785 return (bfd_vma
) a
.start
< (bfd_vma
) b
.start
;
787 return a
.start
< b
.start
;
790 return a
.type
< b
.type
;
793 /* Sort the memrange list using std::sort, and merge adjacent memranges. */
796 memrange_sortmerge (std::vector
<memrange
> &memranges
)
798 if (!memranges
.empty ())
802 std::sort (memranges
.begin (), memranges
.end (), memrange_comp
);
804 for (a
= 0, b
= 1; b
< memranges
.size (); b
++)
806 /* If memrange b overlaps or is adjacent to memrange a,
808 if (memranges
[a
].type
== memranges
[b
].type
809 && memranges
[b
].start
<= memranges
[a
].end
)
811 if (memranges
[b
].end
> memranges
[a
].end
)
812 memranges
[a
].end
= memranges
[b
].end
;
813 continue; /* next b, same a */
817 memranges
[a
] = memranges
[b
];
819 memranges
.resize (a
+ 1);
823 /* Add remote register number REGNO to the collection list mask. */
826 collection_list::add_remote_register (unsigned int regno
)
829 printf_filtered ("collect register %d\n", regno
);
831 m_regs_mask
.at (regno
/ 8) |= 1 << (regno
% 8);
834 /* Add all the registers from the mask in AEXPR to the mask in the
835 collection list. Registers in the AEXPR mask are already remote
839 collection_list::add_ax_registers (struct agent_expr
*aexpr
)
841 if (aexpr
->reg_mask_len
> 0)
843 for (int ndx1
= 0; ndx1
< aexpr
->reg_mask_len
; ndx1
++)
845 QUIT
; /* Allow user to bail out with ^C. */
846 if (aexpr
->reg_mask
[ndx1
] != 0)
848 /* Assume chars have 8 bits. */
849 for (int ndx2
= 0; ndx2
< 8; ndx2
++)
850 if (aexpr
->reg_mask
[ndx1
] & (1 << ndx2
))
851 /* It's used -- record it. */
852 add_remote_register (ndx1
* 8 + ndx2
);
858 /* If REGNO is raw, add its corresponding remote register number to
859 the mask. If REGNO is a pseudo-register, figure out the necessary
860 registers using a temporary agent expression, and add it to the
861 list if it needs more than just a mask. */
864 collection_list::add_local_register (struct gdbarch
*gdbarch
,
868 if (regno
< gdbarch_num_regs (gdbarch
))
870 int remote_regno
= gdbarch_remote_register_number (gdbarch
, regno
);
872 if (remote_regno
< 0)
873 error (_("Can't collect register %d"), regno
);
875 add_remote_register (remote_regno
);
879 agent_expr_up
aexpr (new agent_expr (gdbarch
, scope
));
881 ax_reg_mask (aexpr
.get (), regno
);
883 finalize_tracepoint_aexpr (aexpr
.get ());
885 add_ax_registers (aexpr
.get ());
887 /* Usually ax_reg_mask for a pseudo-regiser only sets the
888 corresponding raw registers in the ax mask, but if this isn't
889 the case add the expression that is generated to the
892 add_aexpr (std::move (aexpr
));
896 /* Add a memrange to a collection list. */
899 collection_list::add_memrange (struct gdbarch
*gdbarch
,
900 int type
, bfd_signed_vma base
,
901 unsigned long len
, CORE_ADDR scope
)
904 printf_filtered ("(%d,%s,%ld)\n", type
, paddress (gdbarch
, base
), len
);
906 /* type: memrange_absolute == memory, other n == basereg */
907 /* base: addr if memory, offset if reg relative. */
908 /* len: we actually save end (base + len) for convenience */
909 m_memranges
.emplace_back (type
, base
, base
+ len
);
911 if (type
!= memrange_absolute
) /* Better collect the base register! */
912 add_local_register (gdbarch
, type
, scope
);
915 /* Add a symbol to a collection list. */
918 collection_list::collect_symbol (struct symbol
*sym
,
919 struct gdbarch
*gdbarch
,
920 long frame_regno
, long frame_offset
,
926 bfd_signed_vma offset
;
927 int treat_as_expr
= 0;
929 len
= TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym
)));
930 switch (SYMBOL_CLASS (sym
))
933 printf_filtered ("%s: don't know symbol class %d\n",
934 sym
->print_name (), SYMBOL_CLASS (sym
));
937 printf_filtered ("constant %s (value %s) will not be collected.\n",
938 sym
->print_name (), plongest (SYMBOL_VALUE (sym
)));
941 offset
= SYMBOL_VALUE_ADDRESS (sym
);
944 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
945 sym
->print_name (), len
,
946 paddress (gdbarch
, offset
));
948 /* A struct may be a C++ class with static fields, go to general
949 expression handling. */
950 if (SYMBOL_TYPE (sym
)->code () == TYPE_CODE_STRUCT
)
953 add_memrange (gdbarch
, memrange_absolute
, offset
, len
, scope
);
956 reg
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
, gdbarch
);
958 printf_filtered ("LOC_REG[parm] %s: ", sym
->print_name ());
959 add_local_register (gdbarch
, reg
, scope
);
960 /* Check for doubles stored in two registers. */
961 /* FIXME: how about larger types stored in 3 or more regs? */
962 if (SYMBOL_TYPE (sym
)->code () == TYPE_CODE_FLT
&&
963 len
> register_size (gdbarch
, reg
))
964 add_local_register (gdbarch
, reg
+ 1, scope
);
967 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
968 printf_filtered (" (will not collect %s)\n", sym
->print_name ());
972 offset
= frame_offset
+ SYMBOL_VALUE (sym
);
975 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
976 " from frame ptr reg %d\n", sym
->print_name (), len
,
977 paddress (gdbarch
, offset
), reg
);
979 add_memrange (gdbarch
, reg
, offset
, len
, scope
);
981 case LOC_REGPARM_ADDR
:
982 reg
= SYMBOL_VALUE (sym
);
986 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s"
987 " from reg %d\n", sym
->print_name (), len
,
988 paddress (gdbarch
, offset
), reg
);
990 add_memrange (gdbarch
, reg
, offset
, len
, scope
);
994 offset
= frame_offset
+ SYMBOL_VALUE (sym
);
997 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
998 " from frame ptr reg %d\n", sym
->print_name (), len
,
999 paddress (gdbarch
, offset
), reg
);
1001 add_memrange (gdbarch
, reg
, offset
, len
, scope
);
1004 case LOC_UNRESOLVED
:
1008 case LOC_OPTIMIZED_OUT
:
1009 printf_filtered ("%s has been optimized out of existence.\n",
1010 sym
->print_name ());
1018 /* Expressions are the most general case. */
1021 agent_expr_up aexpr
= gen_trace_for_var (scope
, gdbarch
,
1024 /* It can happen that the symbol is recorded as a computed
1025 location, but it's been optimized away and doesn't actually
1026 have a location expression. */
1029 printf_filtered ("%s has been optimized out of existence.\n",
1030 sym
->print_name ());
1034 finalize_tracepoint_aexpr (aexpr
.get ());
1036 /* Take care of the registers. */
1037 add_ax_registers (aexpr
.get ());
1039 add_aexpr (std::move (aexpr
));
1043 /* Data to be passed around in the calls to the locals and args
1046 struct add_local_symbols_data
1048 struct collection_list
*collect
;
1049 struct gdbarch
*gdbarch
;
1057 /* The callback for the locals and args iterators. */
1060 do_collect_symbol (const char *print_name
,
1064 struct add_local_symbols_data
*p
= (struct add_local_symbols_data
*) cb_data
;
1066 p
->collect
->collect_symbol (sym
, p
->gdbarch
, p
->frame_regno
,
1067 p
->frame_offset
, p
->pc
, p
->trace_string
);
1070 p
->collect
->add_wholly_collected (print_name
);
1074 collection_list::add_wholly_collected (const char *print_name
)
1076 m_wholly_collected
.push_back (print_name
);
1079 /* Add all locals (or args) symbols to collection list. */
1082 collection_list::add_local_symbols (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
1083 long frame_regno
, long frame_offset
, int type
,
1086 const struct block
*block
;
1087 struct add_local_symbols_data cb_data
;
1089 cb_data
.collect
= this;
1090 cb_data
.gdbarch
= gdbarch
;
1092 cb_data
.frame_regno
= frame_regno
;
1093 cb_data
.frame_offset
= frame_offset
;
1095 cb_data
.trace_string
= trace_string
;
1099 block
= block_for_pc (pc
);
1102 warning (_("Can't collect locals; "
1103 "no symbol table info available.\n"));
1107 iterate_over_block_local_vars (block
, do_collect_symbol
, &cb_data
);
1108 if (cb_data
.count
== 0)
1109 warning (_("No locals found in scope."));
1113 pc
= get_pc_function_start (pc
);
1114 block
= block_for_pc (pc
);
1117 warning (_("Can't collect args; no symbol table info available."));
1121 iterate_over_block_arg_vars (block
, do_collect_symbol
, &cb_data
);
1122 if (cb_data
.count
== 0)
1123 warning (_("No args found in scope."));
1128 collection_list::add_static_trace_data ()
1131 printf_filtered ("collect static trace data\n");
1132 m_strace_data
= true;
1135 collection_list::collection_list ()
1136 : m_strace_data (false)
1138 int max_remote_regno
= 0;
1139 for (int i
= 0; i
< gdbarch_num_regs (target_gdbarch ()); i
++)
1141 int remote_regno
= (gdbarch_remote_register_number
1142 (target_gdbarch (), i
));
1144 if (remote_regno
>= 0 && remote_regno
> max_remote_regno
)
1145 max_remote_regno
= remote_regno
;
1148 m_regs_mask
.resize ((max_remote_regno
/ 8) + 1);
1150 m_memranges
.reserve (128);
1151 m_aexprs
.reserve (128);
1154 /* Reduce a collection list to string form (for gdb protocol). */
1156 std::vector
<std::string
>
1157 collection_list::stringify ()
1159 gdb::char_vector
temp_buf (2048);
1164 std::vector
<std::string
> str_list
;
1169 printf_filtered ("\nCollecting static trace data\n");
1170 end
= temp_buf
.data ();
1172 str_list
.emplace_back (temp_buf
.data (), end
- temp_buf
.data ());
1175 for (i
= m_regs_mask
.size () - 1; i
> 0; i
--)
1176 if (m_regs_mask
[i
] != 0) /* Skip leading zeroes in regs_mask. */
1178 if (m_regs_mask
[i
] != 0) /* Prepare to send regs_mask to the stub. */
1181 printf_filtered ("\nCollecting registers (mask): 0x");
1183 /* One char for 'R', one for the null terminator and two per
1185 std::size_t new_size
= (i
+ 1) * 2 + 2;
1186 if (new_size
> temp_buf
.size ())
1187 temp_buf
.resize (new_size
);
1189 end
= temp_buf
.data ();
1193 QUIT
; /* Allow user to bail out with ^C. */
1195 printf_filtered ("%02X", m_regs_mask
[i
]);
1197 end
= pack_hex_byte (end
, m_regs_mask
[i
]);
1201 str_list
.emplace_back (temp_buf
.data ());
1204 printf_filtered ("\n");
1205 if (!m_memranges
.empty () && info_verbose
)
1206 printf_filtered ("Collecting memranges: \n");
1207 for (i
= 0, count
= 0, end
= temp_buf
.data ();
1208 i
< m_memranges
.size (); i
++)
1210 QUIT
; /* Allow user to bail out with ^C. */
1213 printf_filtered ("(%d, %s, %ld)\n",
1214 m_memranges
[i
].type
,
1215 paddress (target_gdbarch (),
1216 m_memranges
[i
].start
),
1217 (long) (m_memranges
[i
].end
1218 - m_memranges
[i
].start
));
1220 if (count
+ 27 > MAX_AGENT_EXPR_LEN
)
1222 str_list
.emplace_back (temp_buf
.data (), count
);
1224 end
= temp_buf
.data ();
1228 bfd_signed_vma length
1229 = m_memranges
[i
].end
- m_memranges
[i
].start
;
1231 /* The "%X" conversion specifier expects an unsigned argument,
1232 so passing -1 (memrange_absolute) to it directly gives you
1233 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1235 if (m_memranges
[i
].type
== memrange_absolute
)
1236 sprintf (end
, "M-1,%s,%lX", phex_nz (m_memranges
[i
].start
, 0),
1239 sprintf (end
, "M%X,%s,%lX", m_memranges
[i
].type
,
1240 phex_nz (m_memranges
[i
].start
, 0), (long) length
);
1243 count
+= strlen (end
);
1244 end
= temp_buf
.data () + count
;
1247 for (i
= 0; i
< m_aexprs
.size (); i
++)
1249 QUIT
; /* Allow user to bail out with ^C. */
1250 if ((count
+ 10 + 2 * m_aexprs
[i
]->len
) > MAX_AGENT_EXPR_LEN
)
1252 str_list
.emplace_back (temp_buf
.data (), count
);
1254 end
= temp_buf
.data ();
1256 sprintf (end
, "X%08X,", m_aexprs
[i
]->len
);
1257 end
+= 10; /* 'X' + 8 hex digits + ',' */
1260 end
= mem2hex (m_aexprs
[i
]->buf
, end
, m_aexprs
[i
]->len
);
1261 count
+= 2 * m_aexprs
[i
]->len
;
1266 str_list
.emplace_back (temp_buf
.data (), count
);
1268 end
= temp_buf
.data ();
1274 /* Add the expression STR to M_COMPUTED. */
1277 collection_list::append_exp (std::string
&&str
)
1279 m_computed
.push_back (std::move (str
));
1283 collection_list::finish ()
1285 memrange_sortmerge (m_memranges
);
1289 encode_actions_1 (struct command_line
*action
,
1290 struct bp_location
*tloc
,
1292 LONGEST frame_offset
,
1293 struct collection_list
*collect
,
1294 struct collection_list
*stepping_list
)
1296 const char *action_exp
;
1298 struct value
*tempval
;
1299 struct cmd_list_element
*cmd
;
1301 for (; action
; action
= action
->next
)
1303 QUIT
; /* Allow user to bail out with ^C. */
1304 action_exp
= action
->line
;
1305 action_exp
= skip_spaces (action_exp
);
1307 cmd
= lookup_cmd (&action_exp
, cmdlist
, "", NULL
, -1, 1);
1309 error (_("Bad action list item: %s"), action_exp
);
1311 if (cmd_cfunc_eq (cmd
, collect_pseudocommand
))
1313 int trace_string
= 0;
1315 if (*action_exp
== '/')
1316 action_exp
= decode_agent_options (action_exp
, &trace_string
);
1319 { /* Repeat over a comma-separated list. */
1320 QUIT
; /* Allow user to bail out with ^C. */
1321 action_exp
= skip_spaces (action_exp
);
1323 if (0 == strncasecmp ("$reg", action_exp
, 4))
1325 for (i
= 0; i
< gdbarch_num_regs (target_gdbarch ());
1328 int remote_regno
= (gdbarch_remote_register_number
1329 (target_gdbarch (), i
));
1331 /* Ignore arch regnos without a corresponding
1332 remote regno. This can happen for regnos not
1334 if (remote_regno
>= 0)
1335 collect
->add_remote_register (remote_regno
);
1337 action_exp
= strchr (action_exp
, ','); /* more? */
1339 else if (0 == strncasecmp ("$arg", action_exp
, 4))
1341 collect
->add_local_symbols (target_gdbarch (),
1347 action_exp
= strchr (action_exp
, ','); /* more? */
1349 else if (0 == strncasecmp ("$loc", action_exp
, 4))
1351 collect
->add_local_symbols (target_gdbarch (),
1357 action_exp
= strchr (action_exp
, ','); /* more? */
1359 else if (0 == strncasecmp ("$_ret", action_exp
, 5))
1362 = gen_trace_for_return_address (tloc
->address
,
1366 finalize_tracepoint_aexpr (aexpr
.get ());
1368 /* take care of the registers */
1369 collect
->add_ax_registers (aexpr
.get ());
1371 collect
->add_aexpr (std::move (aexpr
));
1372 action_exp
= strchr (action_exp
, ','); /* more? */
1374 else if (0 == strncasecmp ("$_sdata", action_exp
, 7))
1376 collect
->add_static_trace_data ();
1377 action_exp
= strchr (action_exp
, ','); /* more? */
1383 const char *exp_start
= action_exp
;
1384 expression_up exp
= parse_exp_1 (&action_exp
, tloc
->address
,
1385 block_for_pc (tloc
->address
),
1388 switch (exp
->first_opcode ())
1392 expr::register_operation
*regop
1393 = (dynamic_cast<expr::register_operation
*>
1395 const char *name
= regop
->get_name ();
1397 i
= user_reg_map_name_to_regnum (target_gdbarch (),
1398 name
, strlen (name
));
1400 internal_error (__FILE__
, __LINE__
,
1401 _("Register $%s not available"),
1404 printf_filtered ("OP_REGISTER: ");
1405 collect
->add_local_register (target_gdbarch (),
1412 /* Safe because we know it's a simple expression. */
1413 tempval
= evaluate_expression (exp
.get ());
1414 addr
= value_address (tempval
);
1415 expr::unop_memval_operation
*memop
1416 = (dynamic_cast<expr::unop_memval_operation
*>
1418 struct type
*type
= memop
->get_type ();
1419 /* Initialize the TYPE_LENGTH if it is a typedef. */
1420 check_typedef (type
);
1421 collect
->add_memrange (target_gdbarch (),
1422 memrange_absolute
, addr
,
1425 collect
->append_exp (std::string (exp_start
,
1432 expr::var_value_operation
*vvo
1433 = (dynamic_cast<expr::var_value_operation
*>
1435 struct symbol
*sym
= vvo
->get_symbol ();
1436 const char *name
= sym
->natural_name ();
1438 collect
->collect_symbol (sym
,
1444 collect
->add_wholly_collected (name
);
1448 default: /* Full-fledged expression. */
1449 agent_expr_up aexpr
= gen_trace_for_expr (tloc
->address
,
1453 finalize_tracepoint_aexpr (aexpr
.get ());
1455 /* Take care of the registers. */
1456 collect
->add_ax_registers (aexpr
.get ());
1458 collect
->add_aexpr (std::move (aexpr
));
1459 collect
->append_exp (std::string (exp_start
,
1465 while (action_exp
&& *action_exp
++ == ',');
1467 else if (cmd_cfunc_eq (cmd
, teval_pseudocommand
))
1470 { /* Repeat over a comma-separated list. */
1471 QUIT
; /* Allow user to bail out with ^C. */
1472 action_exp
= skip_spaces (action_exp
);
1475 expression_up exp
= parse_exp_1 (&action_exp
, tloc
->address
,
1476 block_for_pc (tloc
->address
),
1479 agent_expr_up aexpr
= gen_eval_for_expr (tloc
->address
,
1482 finalize_tracepoint_aexpr (aexpr
.get ());
1484 /* Even though we're not officially collecting, add
1485 to the collect list anyway. */
1486 collect
->add_aexpr (std::move (aexpr
));
1489 while (action_exp
&& *action_exp
++ == ',');
1491 else if (cmd_cfunc_eq (cmd
, while_stepping_pseudocommand
))
1493 /* We check against nested while-stepping when setting
1494 breakpoint action, so no way to run into nested
1496 gdb_assert (stepping_list
);
1498 encode_actions_1 (action
->body_list_0
.get (), tloc
, frame_reg
,
1499 frame_offset
, stepping_list
, NULL
);
1502 error (_("Invalid tracepoint command '%s'"), action
->line
);
1506 /* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
1507 and STEPPING_LIST. */
1510 encode_actions (struct bp_location
*tloc
,
1511 struct collection_list
*tracepoint_list
,
1512 struct collection_list
*stepping_list
)
1515 LONGEST frame_offset
;
1517 gdbarch_virtual_frame_pointer (tloc
->gdbarch
,
1518 tloc
->address
, &frame_reg
, &frame_offset
);
1520 counted_command_line actions
= all_tracepoint_actions (tloc
->owner
);
1521 encode_actions_1 (actions
.get (), tloc
, frame_reg
, frame_offset
,
1522 tracepoint_list
, stepping_list
);
1523 encode_actions_1 (breakpoint_commands (tloc
->owner
), tloc
,
1524 frame_reg
, frame_offset
, tracepoint_list
, stepping_list
);
1526 tracepoint_list
->finish ();
1527 stepping_list
->finish ();
1530 /* Render all actions into gdb protocol. */
1533 encode_actions_rsp (struct bp_location
*tloc
,
1534 std::vector
<std::string
> *tdp_actions
,
1535 std::vector
<std::string
> *stepping_actions
)
1537 struct collection_list tracepoint_list
, stepping_list
;
1539 encode_actions (tloc
, &tracepoint_list
, &stepping_list
);
1541 *tdp_actions
= tracepoint_list
.stringify ();
1542 *stepping_actions
= stepping_list
.stringify ();
1546 collection_list::add_aexpr (agent_expr_up aexpr
)
1548 m_aexprs
.push_back (std::move (aexpr
));
1552 process_tracepoint_on_disconnect (void)
1554 int has_pending_p
= 0;
1556 /* Check whether we still have pending tracepoint. If we have, warn the
1557 user that pending tracepoint will no longer work. */
1558 for (breakpoint
*b
: all_tracepoints ())
1567 for (bp_location
*loc1
: b
->locations ())
1569 if (loc1
->shlib_disabled
)
1582 warning (_("Pending tracepoints will not be resolved while"
1583 " GDB is disconnected\n"));
1586 /* Reset local state of tracing. */
1589 trace_reset_local_state (void)
1591 set_traceframe_num (-1);
1592 set_tracepoint_num (-1);
1593 set_traceframe_context (NULL
);
1594 clear_traceframe_info ();
1598 start_tracing (const char *notes
)
1600 int any_enabled
= 0, num_to_download
= 0;
1603 auto tracepoint_range
= all_tracepoints ();
1605 /* No point in tracing without any tracepoints... */
1606 if (tracepoint_range
.begin () == tracepoint_range
.end ())
1607 error (_("No tracepoints defined, not starting trace"));
1609 for (breakpoint
*b
: tracepoint_range
)
1611 if (b
->enable_state
== bp_enabled
)
1614 if ((b
->type
== bp_fast_tracepoint
1615 ? may_insert_fast_tracepoints
1616 : may_insert_tracepoints
))
1619 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
1620 (b
->type
== bp_fast_tracepoint
? "fast " : ""), b
->number
);
1625 if (target_supports_enable_disable_tracepoint ())
1626 warning (_("No tracepoints enabled"));
1629 /* No point in tracing with only disabled tracepoints that
1630 cannot be re-enabled. */
1631 error (_("No tracepoints enabled, not starting trace"));
1635 if (num_to_download
<= 0)
1636 error (_("No tracepoints that may be downloaded, not starting trace"));
1638 target_trace_init ();
1640 for (breakpoint
*b
: tracepoint_range
)
1642 struct tracepoint
*t
= (struct tracepoint
*) b
;
1643 int bp_location_downloaded
= 0;
1645 /* Clear `inserted' flag. */
1646 for (bp_location
*loc
: b
->locations ())
1649 if ((b
->type
== bp_fast_tracepoint
1650 ? !may_insert_fast_tracepoints
1651 : !may_insert_tracepoints
))
1654 t
->number_on_target
= 0;
1656 for (bp_location
*loc
: b
->locations ())
1658 /* Since tracepoint locations are never duplicated, `inserted'
1659 flag should be zero. */
1660 gdb_assert (!loc
->inserted
);
1662 target_download_tracepoint (loc
);
1665 bp_location_downloaded
= 1;
1668 t
->number_on_target
= b
->number
;
1670 for (bp_location
*loc
: b
->locations ())
1671 if (loc
->probe
.prob
!= NULL
)
1672 loc
->probe
.prob
->set_semaphore (loc
->probe
.objfile
,
1675 if (bp_location_downloaded
)
1676 gdb::observers::breakpoint_modified
.notify (b
);
1679 /* Send down all the trace state variables too. */
1680 for (const trace_state_variable
&tsv
: tvariables
)
1681 target_download_trace_state_variable (tsv
);
1683 /* Tell target to treat text-like sections as transparent. */
1684 target_trace_set_readonly_regions ();
1685 /* Set some mode flags. */
1686 target_set_disconnected_tracing (disconnected_tracing
);
1687 target_set_circular_trace_buffer (circular_trace_buffer
);
1688 target_set_trace_buffer_size (trace_buffer_size
);
1691 notes
= trace_notes
;
1692 ret
= target_set_trace_notes (trace_user
, notes
, NULL
);
1694 if (!ret
&& (trace_user
|| notes
))
1695 warning (_("Target does not support trace user/notes, info ignored"));
1697 /* Now insert traps and begin collecting data. */
1698 target_trace_start ();
1700 /* Reset our local state. */
1701 trace_reset_local_state ();
1702 current_trace_status()->running
= 1;
1705 /* The tstart command requests the target to start a new trace run.
1706 The command passes any arguments it has to the target verbatim, as
1707 an optional "trace note". This is useful as for instance a warning
1708 to other users if the trace runs disconnected, and you don't want
1709 anybody else messing with the target. */
1712 tstart_command (const char *args
, int from_tty
)
1714 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1716 if (current_trace_status ()->running
)
1719 && !query (_("A trace is running already. Start a new run? ")))
1720 error (_("New trace run not started."));
1723 start_tracing (args
);
1726 /* The tstop command stops the tracing run. The command passes any
1727 supplied arguments to the target verbatim as a "stop note"; if the
1728 target supports trace notes, then it will be reported back as part
1729 of the trace run's status. */
1732 tstop_command (const char *args
, int from_tty
)
1734 if (!current_trace_status ()->running
)
1735 error (_("Trace is not running."));
1737 stop_tracing (args
);
1741 stop_tracing (const char *note
)
1745 target_trace_stop ();
1747 for (breakpoint
*t
: all_tracepoints ())
1749 if ((t
->type
== bp_fast_tracepoint
1750 ? !may_insert_fast_tracepoints
1751 : !may_insert_tracepoints
))
1754 for (bp_location
*loc
: t
->locations ())
1756 /* GDB can be totally absent in some disconnected trace scenarios,
1757 but we don't really care if this semaphore goes out of sync.
1758 That's why we are decrementing it here, but not taking care
1760 if (loc
->probe
.prob
!= NULL
)
1761 loc
->probe
.prob
->clear_semaphore (loc
->probe
.objfile
,
1767 note
= trace_stop_notes
;
1768 ret
= target_set_trace_notes (NULL
, NULL
, note
);
1771 warning (_("Target does not support trace notes, note ignored"));
1773 /* Should change in response to reply? */
1774 current_trace_status ()->running
= 0;
1777 /* tstatus command */
1779 tstatus_command (const char *args
, int from_tty
)
1781 struct trace_status
*ts
= current_trace_status ();
1784 status
= target_get_trace_status (ts
);
1788 if (ts
->filename
!= NULL
)
1789 printf_filtered (_("Using a trace file.\n"));
1792 printf_filtered (_("Trace can not be run on this target.\n"));
1797 if (!ts
->running_known
)
1799 printf_filtered (_("Run/stop status is unknown.\n"));
1801 else if (ts
->running
)
1803 printf_filtered (_("Trace is running on the target.\n"));
1807 switch (ts
->stop_reason
)
1809 case trace_never_run
:
1810 printf_filtered (_("No trace has been run on the target.\n"));
1812 case trace_stop_command
:
1814 printf_filtered (_("Trace stopped by a tstop command (%s).\n"),
1817 printf_filtered (_("Trace stopped by a tstop command.\n"));
1819 case trace_buffer_full
:
1820 printf_filtered (_("Trace stopped because the buffer was full.\n"));
1822 case trace_disconnected
:
1823 printf_filtered (_("Trace stopped because of disconnection.\n"));
1825 case tracepoint_passcount
:
1826 printf_filtered (_("Trace stopped by tracepoint %d.\n"),
1827 ts
->stopping_tracepoint
);
1829 case tracepoint_error
:
1830 if (ts
->stopping_tracepoint
)
1831 printf_filtered (_("Trace stopped by an "
1832 "error (%s, tracepoint %d).\n"),
1833 ts
->stop_desc
, ts
->stopping_tracepoint
);
1835 printf_filtered (_("Trace stopped by an error (%s).\n"),
1838 case trace_stop_reason_unknown
:
1839 printf_filtered (_("Trace stopped for an unknown reason.\n"));
1842 printf_filtered (_("Trace stopped for some other reason (%d).\n"),
1848 if (ts
->traceframes_created
>= 0
1849 && ts
->traceframe_count
!= ts
->traceframes_created
)
1851 printf_filtered (_("Buffer contains %d trace "
1852 "frames (of %d created total).\n"),
1853 ts
->traceframe_count
, ts
->traceframes_created
);
1855 else if (ts
->traceframe_count
>= 0)
1857 printf_filtered (_("Collected %d trace frames.\n"),
1858 ts
->traceframe_count
);
1861 if (ts
->buffer_free
>= 0)
1863 if (ts
->buffer_size
>= 0)
1865 printf_filtered (_("Trace buffer has %d bytes of %d bytes free"),
1866 ts
->buffer_free
, ts
->buffer_size
);
1867 if (ts
->buffer_size
> 0)
1868 printf_filtered (_(" (%d%% full)"),
1869 ((int) ((((long long) (ts
->buffer_size
1870 - ts
->buffer_free
)) * 100)
1871 / ts
->buffer_size
)));
1872 printf_filtered (_(".\n"));
1875 printf_filtered (_("Trace buffer has %d bytes free.\n"),
1879 if (ts
->disconnected_tracing
)
1880 printf_filtered (_("Trace will continue if GDB disconnects.\n"));
1882 printf_filtered (_("Trace will stop if GDB disconnects.\n"));
1884 if (ts
->circular_buffer
)
1885 printf_filtered (_("Trace buffer is circular.\n"));
1887 if (ts
->user_name
&& strlen (ts
->user_name
) > 0)
1888 printf_filtered (_("Trace user is %s.\n"), ts
->user_name
);
1890 if (ts
->notes
&& strlen (ts
->notes
) > 0)
1891 printf_filtered (_("Trace notes: %s.\n"), ts
->notes
);
1893 /* Now report on what we're doing with tfind. */
1894 if (traceframe_number
>= 0)
1895 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
1896 traceframe_number
, tracepoint_number
);
1898 printf_filtered (_("Not looking at any trace frame.\n"));
1900 /* Report start/stop times if supplied. */
1905 LONGEST run_time
= ts
->stop_time
- ts
->start_time
;
1907 /* Reporting a run time is more readable than two long numbers. */
1908 printf_filtered (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
1909 (long int) (ts
->start_time
/ 1000000),
1910 (long int) (ts
->start_time
% 1000000),
1911 (long int) (run_time
/ 1000000),
1912 (long int) (run_time
% 1000000));
1915 printf_filtered (_("Trace started at %ld.%06ld secs.\n"),
1916 (long int) (ts
->start_time
/ 1000000),
1917 (long int) (ts
->start_time
% 1000000));
1919 else if (ts
->stop_time
)
1920 printf_filtered (_("Trace stopped at %ld.%06ld secs.\n"),
1921 (long int) (ts
->stop_time
/ 1000000),
1922 (long int) (ts
->stop_time
% 1000000));
1924 /* Now report any per-tracepoint status available. */
1925 for (breakpoint
*t
: all_tracepoints ())
1926 target_get_tracepoint_status (t
, NULL
);
1929 /* Report the trace status to uiout, in a way suitable for MI, and not
1930 suitable for CLI. If ON_STOP is true, suppress a few fields that
1931 are not meaningful in the -trace-stop response.
1933 The implementation is essentially parallel to trace_status_command, but
1934 merging them will result in unreadable code. */
1936 trace_status_mi (int on_stop
)
1938 struct ui_out
*uiout
= current_uiout
;
1939 struct trace_status
*ts
= current_trace_status ();
1942 status
= target_get_trace_status (ts
);
1944 if (status
== -1 && ts
->filename
== NULL
)
1946 uiout
->field_string ("supported", "0");
1950 if (ts
->filename
!= NULL
)
1951 uiout
->field_string ("supported", "file");
1953 uiout
->field_string ("supported", "1");
1955 if (ts
->filename
!= NULL
)
1956 uiout
->field_string ("trace-file", ts
->filename
);
1958 gdb_assert (ts
->running_known
);
1962 uiout
->field_string ("running", "1");
1964 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
1965 Given that the frontend gets the status either on -trace-stop, or from
1966 -trace-status after re-connection, it does not seem like this
1967 information is necessary for anything. It is not necessary for either
1968 figuring the vital state of the target nor for navigation of trace
1969 frames. If the frontend wants to show the current state is some
1970 configure dialog, it can request the value when such dialog is
1971 invoked by the user. */
1975 const char *stop_reason
= NULL
;
1976 int stopping_tracepoint
= -1;
1979 uiout
->field_string ("running", "0");
1981 if (ts
->stop_reason
!= trace_stop_reason_unknown
)
1983 switch (ts
->stop_reason
)
1985 case trace_stop_command
:
1986 stop_reason
= "request";
1988 case trace_buffer_full
:
1989 stop_reason
= "overflow";
1991 case trace_disconnected
:
1992 stop_reason
= "disconnection";
1994 case tracepoint_passcount
:
1995 stop_reason
= "passcount";
1996 stopping_tracepoint
= ts
->stopping_tracepoint
;
1998 case tracepoint_error
:
1999 stop_reason
= "error";
2000 stopping_tracepoint
= ts
->stopping_tracepoint
;
2006 uiout
->field_string ("stop-reason", stop_reason
);
2007 if (stopping_tracepoint
!= -1)
2008 uiout
->field_signed ("stopping-tracepoint",
2009 stopping_tracepoint
);
2010 if (ts
->stop_reason
== tracepoint_error
)
2011 uiout
->field_string ("error-description",
2017 if (ts
->traceframe_count
!= -1)
2018 uiout
->field_signed ("frames", ts
->traceframe_count
);
2019 if (ts
->traceframes_created
!= -1)
2020 uiout
->field_signed ("frames-created", ts
->traceframes_created
);
2021 if (ts
->buffer_size
!= -1)
2022 uiout
->field_signed ("buffer-size", ts
->buffer_size
);
2023 if (ts
->buffer_free
!= -1)
2024 uiout
->field_signed ("buffer-free", ts
->buffer_free
);
2026 uiout
->field_signed ("disconnected", ts
->disconnected_tracing
);
2027 uiout
->field_signed ("circular", ts
->circular_buffer
);
2029 uiout
->field_string ("user-name", ts
->user_name
);
2030 uiout
->field_string ("notes", ts
->notes
);
2035 xsnprintf (buf
, sizeof buf
, "%ld.%06ld",
2036 (long int) (ts
->start_time
/ 1000000),
2037 (long int) (ts
->start_time
% 1000000));
2038 uiout
->field_string ("start-time", buf
);
2039 xsnprintf (buf
, sizeof buf
, "%ld.%06ld",
2040 (long int) (ts
->stop_time
/ 1000000),
2041 (long int) (ts
->stop_time
% 1000000));
2042 uiout
->field_string ("stop-time", buf
);
2046 /* Check if a trace run is ongoing. If so, and FROM_TTY, query the
2047 user if she really wants to detach. */
2050 query_if_trace_running (int from_tty
)
2055 /* It can happen that the target that was tracing went away on its
2056 own, and we didn't notice. Get a status update, and if the
2057 current target doesn't even do tracing, then assume it's not
2059 if (target_get_trace_status (current_trace_status ()) < 0)
2060 current_trace_status ()->running
= 0;
2062 /* If running interactively, give the user the option to cancel and
2063 then decide what to do differently with the run. Scripts are
2064 just going to disconnect and let the target deal with it,
2065 according to how it's been instructed previously via
2066 disconnected-tracing. */
2067 if (current_trace_status ()->running
)
2069 process_tracepoint_on_disconnect ();
2071 if (current_trace_status ()->disconnected_tracing
)
2073 if (!query (_("Trace is running and will "
2074 "continue after detach; detach anyway? ")))
2075 error (_("Not confirmed."));
2079 if (!query (_("Trace is running but will "
2080 "stop on detach; detach anyway? ")))
2081 error (_("Not confirmed."));
2086 /* This function handles the details of what to do about an ongoing
2087 tracing run if the user has asked to detach or otherwise disconnect
2091 disconnect_tracing (void)
2093 /* Also we want to be out of tfind mode, otherwise things can get
2094 confusing upon reconnection. Just use these calls instead of
2095 full tfind_1 behavior because we're in the middle of detaching,
2096 and there's no point to updating current stack frame etc. */
2097 trace_reset_local_state ();
2100 /* Worker function for the various flavors of the tfind command. */
2102 tfind_1 (enum trace_find_type type
, int num
,
2103 CORE_ADDR addr1
, CORE_ADDR addr2
,
2106 int target_frameno
= -1, target_tracept
= -1;
2107 struct frame_id old_frame_id
= null_frame_id
;
2108 struct tracepoint
*tp
;
2109 struct ui_out
*uiout
= current_uiout
;
2111 /* Only try to get the current stack frame if we have a chance of
2112 succeeding. In particular, if we're trying to get a first trace
2113 frame while all threads are running, it's not going to succeed,
2114 so leave it with a default value and let the frame comparison
2115 below (correctly) decide to print out the source location of the
2117 if (!(type
== tfind_number
&& num
== -1)
2118 && (has_stack_frames () || traceframe_number
>= 0))
2119 old_frame_id
= get_frame_id (get_current_frame ());
2121 target_frameno
= target_trace_find (type
, num
, addr1
, addr2
,
2124 if (type
== tfind_number
2126 && target_frameno
== -1)
2128 /* We told the target to get out of tfind mode, and it did. */
2130 else if (target_frameno
== -1)
2132 /* A request for a non-existent trace frame has failed.
2133 Our response will be different, depending on FROM_TTY:
2135 If FROM_TTY is true, meaning that this command was
2136 typed interactively by the user, then give an error
2137 and DO NOT change the state of traceframe_number etc.
2139 However if FROM_TTY is false, meaning that we're either
2140 in a script, a loop, or a user-defined command, then
2141 DON'T give an error, but DO change the state of
2142 traceframe_number etc. to invalid.
2144 The rationale is that if you typed the command, you
2145 might just have committed a typo or something, and you'd
2146 like to NOT lose your current debugging state. However
2147 if you're in a user-defined command or especially in a
2148 loop, then you need a way to detect that the command
2149 failed WITHOUT aborting. This allows you to write
2150 scripts that search thru the trace buffer until the end,
2151 and then continue on to do something else. */
2154 error (_("Target failed to find requested trace frame."));
2158 printf_filtered ("End of trace buffer.\n");
2159 #if 0 /* dubious now? */
2160 /* The following will not recurse, since it's
2162 tfind_command ("-1", from_tty
);
2167 tp
= get_tracepoint_by_number_on_target (target_tracept
);
2169 reinit_frame_cache ();
2170 target_dcache_invalidate ();
2172 set_tracepoint_num (tp
? tp
->number
: target_tracept
);
2174 if (target_frameno
!= get_traceframe_number ())
2175 gdb::observers::traceframe_changed
.notify (target_frameno
, tracepoint_number
);
2177 set_current_traceframe (target_frameno
);
2179 if (target_frameno
== -1)
2180 set_traceframe_context (NULL
);
2182 set_traceframe_context (get_current_frame ());
2184 if (traceframe_number
>= 0)
2186 /* Use different branches for MI and CLI to make CLI messages
2188 if (uiout
->is_mi_like_p ())
2190 uiout
->field_string ("found", "1");
2191 uiout
->field_signed ("tracepoint", tracepoint_number
);
2192 uiout
->field_signed ("traceframe", traceframe_number
);
2196 printf_unfiltered (_("Found trace frame %d, tracepoint %d\n"),
2197 traceframe_number
, tracepoint_number
);
2202 if (uiout
->is_mi_like_p ())
2203 uiout
->field_string ("found", "0");
2204 else if (type
== tfind_number
&& num
== -1)
2205 printf_unfiltered (_("No longer looking at any trace frame\n"));
2206 else /* This case may never occur, check. */
2207 printf_unfiltered (_("No trace frame found\n"));
2210 /* If we're in nonstop mode and getting out of looking at trace
2211 frames, there won't be any current frame to go back to and
2214 && (has_stack_frames () || traceframe_number
>= 0))
2216 enum print_what print_what
;
2218 /* NOTE: in imitation of the step command, try to determine
2219 whether we have made a transition from one function to
2220 another. If so, we'll print the "stack frame" (ie. the new
2221 function and it's arguments) -- otherwise we'll just show the
2224 if (frame_id_eq (old_frame_id
,
2225 get_frame_id (get_current_frame ())))
2226 print_what
= SRC_LINE
;
2228 print_what
= SRC_AND_LOC
;
2230 print_stack_frame (get_selected_frame (NULL
), 1, print_what
, 1);
2235 /* Error on looking at traceframes while trace is running. */
2238 check_trace_running (struct trace_status
*status
)
2240 if (status
->running
&& status
->filename
== NULL
)
2241 error (_("May not look at trace frames while trace is running."));
2244 /* trace_find_command takes a trace frame number n,
2245 sends "QTFrame:<n>" to the target,
2246 and accepts a reply that may contain several optional pieces
2247 of information: a frame number, a tracepoint number, and an
2248 indication of whether this is a trap frame or a stepping frame.
2250 The minimal response is just "OK" (which indicates that the
2251 target does not give us a frame number or a tracepoint number).
2252 Instead of that, the target may send us a string containing
2254 F<hexnum> (gives the selected frame number)
2255 T<hexnum> (gives the selected tracepoint number)
2260 tfind_command_1 (const char *args
, int from_tty
)
2261 { /* This should only be called with a numeric argument. */
2264 check_trace_running (current_trace_status ());
2266 if (args
== 0 || *args
== 0)
2267 { /* TFIND with no args means find NEXT trace frame. */
2268 if (traceframe_number
== -1)
2269 frameno
= 0; /* "next" is first one. */
2271 frameno
= traceframe_number
+ 1;
2273 else if (0 == strcmp (args
, "-"))
2275 if (traceframe_number
== -1)
2276 error (_("not debugging trace buffer"));
2277 else if (from_tty
&& traceframe_number
== 0)
2278 error (_("already at start of trace buffer"));
2280 frameno
= traceframe_number
- 1;
2282 /* A hack to work around eval's need for fp to have been collected. */
2283 else if (0 == strcmp (args
, "-1"))
2286 frameno
= parse_and_eval_long (args
);
2289 error (_("invalid input (%d is less than zero)"), frameno
);
2291 tfind_1 (tfind_number
, frameno
, 0, 0, from_tty
);
2295 tfind_command (const char *args
, int from_tty
)
2297 tfind_command_1 (args
, from_tty
);
2302 tfind_end_command (const char *args
, int from_tty
)
2304 tfind_command_1 ("-1", from_tty
);
2309 tfind_start_command (const char *args
, int from_tty
)
2311 tfind_command_1 ("0", from_tty
);
2314 /* tfind pc command */
2316 tfind_pc_command (const char *args
, int from_tty
)
2320 check_trace_running (current_trace_status ());
2322 if (args
== 0 || *args
== 0)
2323 pc
= regcache_read_pc (get_current_regcache ());
2325 pc
= parse_and_eval_address (args
);
2327 tfind_1 (tfind_pc
, 0, pc
, 0, from_tty
);
2330 /* tfind tracepoint command */
2332 tfind_tracepoint_command (const char *args
, int from_tty
)
2335 struct tracepoint
*tp
;
2337 check_trace_running (current_trace_status ());
2339 if (args
== 0 || *args
== 0)
2341 if (tracepoint_number
== -1)
2342 error (_("No current tracepoint -- please supply an argument."));
2344 tdp
= tracepoint_number
; /* Default is current TDP. */
2347 tdp
= parse_and_eval_long (args
);
2349 /* If we have the tracepoint on hand, use the number that the
2350 target knows about (which may be different if we disconnected
2351 and reconnected). */
2352 tp
= get_tracepoint (tdp
);
2354 tdp
= tp
->number_on_target
;
2356 tfind_1 (tfind_tp
, tdp
, 0, 0, from_tty
);
2359 /* TFIND LINE command:
2361 This command will take a sourceline for argument, just like BREAK
2362 or TRACE (ie. anything that "decode_line_1" can handle).
2364 With no argument, this command will find the next trace frame
2365 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2368 tfind_line_command (const char *args
, int from_tty
)
2370 check_trace_running (current_trace_status ());
2372 symtab_and_line sal
;
2373 if (args
== 0 || *args
== 0)
2375 sal
= find_pc_line (get_frame_pc (get_current_frame ()), 0);
2379 std::vector
<symtab_and_line
> sals
2380 = decode_line_with_current_source (args
, DECODE_LINE_FUNFIRSTLINE
);
2384 if (sal
.symtab
== 0)
2385 error (_("No line number information available."));
2387 CORE_ADDR start_pc
, end_pc
;
2388 if (sal
.line
> 0 && find_line_pc_range (sal
, &start_pc
, &end_pc
))
2390 if (start_pc
== end_pc
)
2392 printf_filtered ("Line %d of \"%s\"",
2394 symtab_to_filename_for_display (sal
.symtab
));
2396 printf_filtered (" is at address ");
2397 print_address (get_current_arch (), start_pc
, gdb_stdout
);
2399 printf_filtered (" but contains no code.\n");
2400 sal
= find_pc_line (start_pc
, 0);
2402 && find_line_pc_range (sal
, &start_pc
, &end_pc
)
2403 && start_pc
!= end_pc
)
2404 printf_filtered ("Attempting to find line %d instead.\n",
2407 error (_("Cannot find a good line."));
2412 /* Is there any case in which we get here, and have an address
2413 which the user would want to see? If we have debugging
2414 symbols and no line numbers? */
2415 error (_("Line number %d is out of range for \"%s\"."),
2416 sal
.line
, symtab_to_filename_for_display (sal
.symtab
));
2419 /* Find within range of stated line. */
2421 tfind_1 (tfind_range
, 0, start_pc
, end_pc
- 1, from_tty
);
2423 tfind_1 (tfind_outside
, 0, start_pc
, end_pc
- 1, from_tty
);
2426 /* tfind range command */
2428 tfind_range_command (const char *args
, int from_tty
)
2430 static CORE_ADDR start
, stop
;
2433 check_trace_running (current_trace_status ());
2435 if (args
== 0 || *args
== 0)
2436 { /* XXX FIXME: what should default behavior be? */
2437 printf_filtered ("Usage: tfind range STARTADDR, ENDADDR\n");
2441 if (0 != (tmp
= strchr (args
, ',')))
2443 std::string
start_addr (args
, tmp
);
2445 tmp
= skip_spaces (tmp
);
2446 start
= parse_and_eval_address (start_addr
.c_str ());
2447 stop
= parse_and_eval_address (tmp
);
2450 { /* No explicit end address? */
2451 start
= parse_and_eval_address (args
);
2452 stop
= start
+ 1; /* ??? */
2455 tfind_1 (tfind_range
, 0, start
, stop
, from_tty
);
2458 /* tfind outside command */
2460 tfind_outside_command (const char *args
, int from_tty
)
2462 CORE_ADDR start
, stop
;
2465 if (current_trace_status ()->running
2466 && current_trace_status ()->filename
== NULL
)
2467 error (_("May not look at trace frames while trace is running."));
2469 if (args
== 0 || *args
== 0)
2470 { /* XXX FIXME: what should default behavior be? */
2471 printf_filtered ("Usage: tfind outside STARTADDR, ENDADDR\n");
2475 if (0 != (tmp
= strchr (args
, ',')))
2477 std::string
start_addr (args
, tmp
);
2479 tmp
= skip_spaces (tmp
);
2480 start
= parse_and_eval_address (start_addr
.c_str ());
2481 stop
= parse_and_eval_address (tmp
);
2484 { /* No explicit end address? */
2485 start
= parse_and_eval_address (args
);
2486 stop
= start
+ 1; /* ??? */
2489 tfind_1 (tfind_outside
, 0, start
, stop
, from_tty
);
2492 /* info scope command: list the locals for a scope. */
2494 info_scope_command (const char *args_in
, int from_tty
)
2497 struct bound_minimal_symbol msym
;
2498 const struct block
*block
;
2499 const char *symname
;
2500 const char *save_args
= args_in
;
2501 struct block_iterator iter
;
2503 struct gdbarch
*gdbarch
;
2505 const char *args
= args_in
;
2507 if (args
== 0 || *args
== 0)
2508 error (_("requires an argument (function, "
2509 "line or *addr) to define a scope"));
2511 event_location_up location
= string_to_event_location (&args
,
2513 std::vector
<symtab_and_line
> sals
2514 = decode_line_1 (location
.get (), DECODE_LINE_FUNFIRSTLINE
,
2518 /* Presumably decode_line_1 has already warned. */
2522 /* Resolve line numbers to PC. */
2523 resolve_sal_pc (&sals
[0]);
2524 block
= block_for_pc (sals
[0].pc
);
2528 QUIT
; /* Allow user to bail out with ^C. */
2529 ALL_BLOCK_SYMBOLS (block
, iter
, sym
)
2531 QUIT
; /* Allow user to bail out with ^C. */
2533 printf_filtered ("Scope for %s:\n", save_args
);
2536 symname
= sym
->print_name ();
2537 if (symname
== NULL
|| *symname
== '\0')
2538 continue; /* Probably botched, certainly useless. */
2540 gdbarch
= symbol_arch (sym
);
2542 printf_filtered ("Symbol %s is ", symname
);
2544 if (SYMBOL_COMPUTED_OPS (sym
) != NULL
)
2545 SYMBOL_COMPUTED_OPS (sym
)->describe_location (sym
,
2546 BLOCK_ENTRY_PC (block
),
2550 switch (SYMBOL_CLASS (sym
))
2553 case LOC_UNDEF
: /* Messed up symbol? */
2554 printf_filtered ("a bogus symbol, class %d.\n",
2555 SYMBOL_CLASS (sym
));
2556 count
--; /* Don't count this one. */
2559 printf_filtered ("a constant with value %s (%s)",
2560 plongest (SYMBOL_VALUE (sym
)),
2561 hex_string (SYMBOL_VALUE (sym
)));
2563 case LOC_CONST_BYTES
:
2564 printf_filtered ("constant bytes: ");
2565 if (SYMBOL_TYPE (sym
))
2566 for (j
= 0; j
< TYPE_LENGTH (SYMBOL_TYPE (sym
)); j
++)
2567 fprintf_filtered (gdb_stdout
, " %02x",
2568 (unsigned) SYMBOL_VALUE_BYTES (sym
)[j
]);
2571 printf_filtered ("in static storage at address ");
2572 printf_filtered ("%s", paddress (gdbarch
,
2573 SYMBOL_VALUE_ADDRESS (sym
)));
2576 /* GDBARCH is the architecture associated with the objfile
2577 the symbol is defined in; the target architecture may be
2578 different, and may provide additional registers. However,
2579 we do not know the target architecture at this point.
2580 We assume the objfile architecture will contain all the
2581 standard registers that occur in debug info in that
2583 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
,
2586 if (SYMBOL_IS_ARGUMENT (sym
))
2587 printf_filtered ("an argument in register $%s",
2588 gdbarch_register_name (gdbarch
, regno
));
2590 printf_filtered ("a local variable in register $%s",
2591 gdbarch_register_name (gdbarch
, regno
));
2594 printf_filtered ("an argument at stack/frame offset %s",
2595 plongest (SYMBOL_VALUE (sym
)));
2598 printf_filtered ("a local variable at frame offset %s",
2599 plongest (SYMBOL_VALUE (sym
)));
2602 printf_filtered ("a reference argument at offset %s",
2603 plongest (SYMBOL_VALUE (sym
)));
2605 case LOC_REGPARM_ADDR
:
2606 /* Note comment at LOC_REGISTER. */
2607 regno
= SYMBOL_REGISTER_OPS (sym
)->register_number (sym
,
2609 printf_filtered ("the address of an argument, in register $%s",
2610 gdbarch_register_name (gdbarch
, regno
));
2613 printf_filtered ("a typedef.\n");
2616 printf_filtered ("a label at address ");
2617 printf_filtered ("%s", paddress (gdbarch
,
2618 SYMBOL_VALUE_ADDRESS (sym
)));
2621 printf_filtered ("a function at address ");
2622 printf_filtered ("%s",
2623 paddress (gdbarch
, BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym
))));
2625 case LOC_UNRESOLVED
:
2626 msym
= lookup_minimal_symbol (sym
->linkage_name (),
2628 if (msym
.minsym
== NULL
)
2629 printf_filtered ("Unresolved Static");
2632 printf_filtered ("static storage at address ");
2633 printf_filtered ("%s",
2635 BMSYMBOL_VALUE_ADDRESS (msym
)));
2638 case LOC_OPTIMIZED_OUT
:
2639 printf_filtered ("optimized out.\n");
2642 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
2645 if (SYMBOL_TYPE (sym
))
2647 struct type
*t
= check_typedef (SYMBOL_TYPE (sym
));
2649 printf_filtered (", length %s.\n", pulongest (TYPE_LENGTH (t
)));
2652 if (BLOCK_FUNCTION (block
))
2655 block
= BLOCK_SUPERBLOCK (block
);
2658 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2662 /* Helper for trace_dump_command. Dump the action list starting at
2663 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2664 actions of the body of a while-stepping action. STEPPING_FRAME is
2665 set if the current traceframe was determined to be a while-stepping
2669 trace_dump_actions (struct command_line
*action
,
2670 int stepping_actions
, int stepping_frame
,
2673 const char *action_exp
, *next_comma
;
2675 for (; action
!= NULL
; action
= action
->next
)
2677 struct cmd_list_element
*cmd
;
2679 QUIT
; /* Allow user to bail out with ^C. */
2680 action_exp
= action
->line
;
2681 action_exp
= skip_spaces (action_exp
);
2683 /* The collection actions to be done while stepping are
2684 bracketed by the commands "while-stepping" and "end". */
2686 if (*action_exp
== '#') /* comment line */
2689 cmd
= lookup_cmd (&action_exp
, cmdlist
, "", NULL
, -1, 1);
2691 error (_("Bad action list item: %s"), action_exp
);
2693 if (cmd_cfunc_eq (cmd
, while_stepping_pseudocommand
))
2695 gdb_assert (action
->body_list_1
== nullptr);
2696 trace_dump_actions (action
->body_list_0
.get (),
2697 1, stepping_frame
, from_tty
);
2699 else if (cmd_cfunc_eq (cmd
, collect_pseudocommand
))
2701 /* Display the collected data.
2702 For the trap frame, display only what was collected at
2703 the trap. Likewise for stepping frames, display only
2704 what was collected while stepping. This means that the
2705 two boolean variables, STEPPING_FRAME and
2706 STEPPING_ACTIONS should be equal. */
2707 if (stepping_frame
== stepping_actions
)
2709 int trace_string
= 0;
2711 if (*action_exp
== '/')
2712 action_exp
= decode_agent_options (action_exp
, &trace_string
);
2715 { /* Repeat over a comma-separated list. */
2716 QUIT
; /* Allow user to bail out with ^C. */
2717 if (*action_exp
== ',')
2719 action_exp
= skip_spaces (action_exp
);
2721 next_comma
= strchr (action_exp
, ',');
2723 if (0 == strncasecmp (action_exp
, "$reg", 4))
2724 registers_info (NULL
, from_tty
);
2725 else if (0 == strncasecmp (action_exp
, "$_ret", 5))
2727 else if (0 == strncasecmp (action_exp
, "$loc", 4))
2728 info_locals_command (NULL
, from_tty
);
2729 else if (0 == strncasecmp (action_exp
, "$arg", 4))
2730 info_args_command (NULL
, from_tty
);
2733 std::string contents
;
2734 const char *exp
= action_exp
;
2735 if (next_comma
!= NULL
)
2737 size_t len
= next_comma
- action_exp
;
2738 contents
= std::string (action_exp
, len
);
2739 exp
= contents
.c_str ();
2742 printf_filtered ("%s = ", exp
);
2743 output_command (exp
, from_tty
);
2744 printf_filtered ("\n");
2746 action_exp
= next_comma
;
2748 while (action_exp
&& *action_exp
== ',');
2754 /* Return bp_location of the tracepoint associated with the current
2755 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
2756 is a stepping traceframe. */
2758 struct bp_location
*
2759 get_traceframe_location (int *stepping_frame_p
)
2761 struct tracepoint
*t
;
2762 struct regcache
*regcache
;
2764 if (tracepoint_number
== -1)
2765 error (_("No current trace frame."));
2767 t
= get_tracepoint (tracepoint_number
);
2770 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2773 /* The current frame is a trap frame if the frame PC is equal to the
2774 tracepoint PC. If not, then the current frame was collected
2775 during single-stepping. */
2776 regcache
= get_current_regcache ();
2778 /* If the traceframe's address matches any of the tracepoint's
2779 locations, assume it is a direct hit rather than a while-stepping
2780 frame. (FIXME this is not reliable, should record each frame's
2782 for (bp_location
*tloc
: t
->locations ())
2783 if (tloc
->address
== regcache_read_pc (regcache
))
2785 *stepping_frame_p
= 0;
2789 /* If this is a stepping frame, we don't know which location
2790 triggered. The first is as good (or bad) a guess as any... */
2791 *stepping_frame_p
= 1;
2795 /* Return the default collect actions of a tracepoint T. */
2797 static counted_command_line
2798 all_tracepoint_actions (struct breakpoint
*t
)
2800 counted_command_line
actions (nullptr, command_lines_deleter ());
2802 /* If there are default expressions to collect, make up a collect
2803 action and prepend to the action list to encode. Note that since
2804 validation is per-tracepoint (local var "xyz" might be valid for
2805 one tracepoint and not another, etc), we make up the action on
2806 the fly, and don't cache it. */
2807 if (*default_collect
)
2809 gdb::unique_xmalloc_ptr
<char> default_collect_line
2810 (xstrprintf ("collect %s", default_collect
));
2812 validate_actionline (default_collect_line
.get (), t
);
2813 actions
.reset (new struct command_line (simple_control
,
2814 default_collect_line
.release ()),
2815 command_lines_deleter ());
2821 /* The tdump command. */
2824 tdump_command (const char *args
, int from_tty
)
2826 int stepping_frame
= 0;
2827 struct bp_location
*loc
;
2829 /* This throws an error is not inspecting a trace frame. */
2830 loc
= get_traceframe_location (&stepping_frame
);
2832 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2833 tracepoint_number
, traceframe_number
);
2835 /* This command only makes sense for the current frame, not the
2837 scoped_restore_current_thread restore_thread
;
2839 select_frame (get_current_frame ());
2841 counted_command_line actions
= all_tracepoint_actions (loc
->owner
);
2843 trace_dump_actions (actions
.get (), 0, stepping_frame
, from_tty
);
2844 trace_dump_actions (breakpoint_commands (loc
->owner
), 0, stepping_frame
,
2848 /* Encode a piece of a tracepoint's source-level definition in a form
2849 that is suitable for both protocol and saving in files. */
2850 /* This version does not do multiple encodes for long strings; it should
2851 return an offset to the next piece to encode. FIXME */
2854 encode_source_string (int tpnum
, ULONGEST addr
,
2855 const char *srctype
, const char *src
,
2856 char *buf
, int buf_size
)
2858 if (80 + strlen (srctype
) > buf_size
)
2859 error (_("Buffer too small for source encoding"));
2860 sprintf (buf
, "%x:%s:%s:%x:%x:",
2861 tpnum
, phex_nz (addr
, sizeof (addr
)),
2862 srctype
, 0, (int) strlen (src
));
2863 if (strlen (buf
) + strlen (src
) * 2 >= buf_size
)
2864 error (_("Source string too long for buffer"));
2865 bin2hex ((gdb_byte
*) src
, buf
+ strlen (buf
), strlen (src
));
2869 /* Tell the target what to do with an ongoing tracing run if GDB
2870 disconnects for some reason. */
2873 set_disconnected_tracing (const char *args
, int from_tty
,
2874 struct cmd_list_element
*c
)
2876 target_set_disconnected_tracing (disconnected_tracing
);
2880 set_circular_trace_buffer (const char *args
, int from_tty
,
2881 struct cmd_list_element
*c
)
2883 target_set_circular_trace_buffer (circular_trace_buffer
);
2887 set_trace_buffer_size (const char *args
, int from_tty
,
2888 struct cmd_list_element
*c
)
2890 target_set_trace_buffer_size (trace_buffer_size
);
2894 set_trace_user (const char *args
, int from_tty
,
2895 struct cmd_list_element
*c
)
2899 ret
= target_set_trace_notes (trace_user
, NULL
, NULL
);
2902 warning (_("Target does not support trace notes, user ignored"));
2906 set_trace_notes (const char *args
, int from_tty
,
2907 struct cmd_list_element
*c
)
2911 ret
= target_set_trace_notes (NULL
, trace_notes
, NULL
);
2914 warning (_("Target does not support trace notes, note ignored"));
2918 set_trace_stop_notes (const char *args
, int from_tty
,
2919 struct cmd_list_element
*c
)
2923 ret
= target_set_trace_notes (NULL
, NULL
, trace_stop_notes
);
2926 warning (_("Target does not support trace notes, stop note ignored"));
2929 /* Convert the memory pointed to by mem into hex, placing result in buf.
2930 * Return a pointer to the last char put in buf (null)
2931 * "stolen" from sparc-stub.c
2934 static const char hexchars
[] = "0123456789abcdef";
2937 mem2hex (gdb_byte
*mem
, char *buf
, int count
)
2945 *buf
++ = hexchars
[ch
>> 4];
2946 *buf
++ = hexchars
[ch
& 0xf];
2955 get_traceframe_number (void)
2957 return traceframe_number
;
2961 get_tracepoint_number (void)
2963 return tracepoint_number
;
2966 /* Make the traceframe NUM be the current trace frame. Does nothing
2967 if NUM is already current. */
2970 set_current_traceframe (int num
)
2974 if (traceframe_number
== num
)
2976 /* Nothing to do. */
2980 newnum
= target_trace_find (tfind_number
, num
, 0, 0, NULL
);
2983 warning (_("could not change traceframe"));
2985 set_traceframe_num (newnum
);
2987 /* Changing the traceframe changes our view of registers and of the
2989 registers_changed ();
2991 clear_traceframe_info ();
2994 scoped_restore_current_traceframe::scoped_restore_current_traceframe ()
2995 : m_traceframe_number (traceframe_number
)
2998 /* Given a number and address, return an uploaded tracepoint with that
2999 number, creating if necessary. */
3001 struct uploaded_tp
*
3002 get_uploaded_tp (int num
, ULONGEST addr
, struct uploaded_tp
**utpp
)
3004 struct uploaded_tp
*utp
;
3006 for (utp
= *utpp
; utp
; utp
= utp
->next
)
3007 if (utp
->number
== num
&& utp
->addr
== addr
)
3010 utp
= new uploaded_tp
;
3020 free_uploaded_tps (struct uploaded_tp
**utpp
)
3022 struct uploaded_tp
*next_one
;
3026 next_one
= (*utpp
)->next
;
3032 /* Given a number and address, return an uploaded tracepoint with that
3033 number, creating if necessary. */
3035 struct uploaded_tsv
*
3036 get_uploaded_tsv (int num
, struct uploaded_tsv
**utsvp
)
3038 struct uploaded_tsv
*utsv
;
3040 for (utsv
= *utsvp
; utsv
; utsv
= utsv
->next
)
3041 if (utsv
->number
== num
)
3044 utsv
= XCNEW (struct uploaded_tsv
);
3046 utsv
->next
= *utsvp
;
3053 free_uploaded_tsvs (struct uploaded_tsv
**utsvp
)
3055 struct uploaded_tsv
*next_one
;
3059 next_one
= (*utsvp
)->next
;
3065 /* FIXME this function is heuristic and will miss the cases where the
3066 conditional is semantically identical but differs in whitespace,
3067 such as "x == 0" vs "x==0". */
3070 cond_string_is_same (char *str1
, char *str2
)
3072 if (str1
== NULL
|| str2
== NULL
)
3073 return (str1
== str2
);
3075 return (strcmp (str1
, str2
) == 0);
3078 /* Look for an existing tracepoint that seems similar enough to the
3079 uploaded one. Enablement isn't compared, because the user can
3080 toggle that freely, and may have done so in anticipation of the
3081 next trace run. Return the location of matched tracepoint. */
3083 static struct bp_location
*
3084 find_matching_tracepoint_location (struct uploaded_tp
*utp
)
3086 struct bp_location
*loc
;
3088 for (breakpoint
*b
: all_tracepoints ())
3090 struct tracepoint
*t
= (struct tracepoint
*) b
;
3092 if (b
->type
== utp
->type
3093 && t
->step_count
== utp
->step
3094 && t
->pass_count
== utp
->pass
3095 && cond_string_is_same (t
->cond_string
,
3096 utp
->cond_string
.get ())
3097 /* FIXME also test actions. */
3100 /* Scan the locations for an address match. */
3101 for (loc
= b
->loc
; loc
; loc
= loc
->next
)
3103 if (loc
->address
== utp
->addr
)
3111 /* Given a list of tracepoints uploaded from a target, attempt to
3112 match them up with existing tracepoints, and create new ones if not
3116 merge_uploaded_tracepoints (struct uploaded_tp
**uploaded_tps
)
3118 struct uploaded_tp
*utp
;
3119 /* A set of tracepoints which are modified. */
3120 std::vector
<breakpoint
*> modified_tp
;
3122 /* Look for GDB tracepoints that match up with our uploaded versions. */
3123 for (utp
= *uploaded_tps
; utp
; utp
= utp
->next
)
3125 struct bp_location
*loc
;
3126 struct tracepoint
*t
;
3128 loc
= find_matching_tracepoint_location (utp
);
3133 /* Mark this location as already inserted. */
3135 t
= (struct tracepoint
*) loc
->owner
;
3136 printf_filtered (_("Assuming tracepoint %d is same "
3137 "as target's tracepoint %d at %s.\n"),
3138 loc
->owner
->number
, utp
->number
,
3139 paddress (loc
->gdbarch
, utp
->addr
));
3141 /* The tracepoint LOC->owner was modified (the location LOC
3142 was marked as inserted in the target). Save it in
3143 MODIFIED_TP if not there yet. The 'breakpoint-modified'
3144 observers will be notified later once for each tracepoint
3145 saved in MODIFIED_TP. */
3146 for (breakpoint
*b
: modified_tp
)
3147 if (b
== loc
->owner
)
3153 modified_tp
.push_back (loc
->owner
);
3157 t
= create_tracepoint_from_upload (utp
);
3159 printf_filtered (_("Created tracepoint %d for "
3160 "target's tracepoint %d at %s.\n"),
3161 t
->number
, utp
->number
,
3162 paddress (get_current_arch (), utp
->addr
));
3164 printf_filtered (_("Failed to create tracepoint for target's "
3165 "tracepoint %d at %s, skipping it.\n"),
3167 paddress (get_current_arch (), utp
->addr
));
3169 /* Whether found or created, record the number used by the
3170 target, to help with mapping target tracepoints back to their
3171 counterparts here. */
3173 t
->number_on_target
= utp
->number
;
3176 /* Notify 'breakpoint-modified' observer that at least one of B's
3177 locations was changed. */
3178 for (breakpoint
*b
: modified_tp
)
3179 gdb::observers::breakpoint_modified
.notify (b
);
3181 free_uploaded_tps (uploaded_tps
);
3184 /* Trace state variables don't have much to identify them beyond their
3185 name, so just use that to detect matches. */
3187 static struct trace_state_variable
*
3188 find_matching_tsv (struct uploaded_tsv
*utsv
)
3193 return find_trace_state_variable (utsv
->name
);
3196 static struct trace_state_variable
*
3197 create_tsv_from_upload (struct uploaded_tsv
*utsv
)
3199 const char *namebase
;
3202 struct trace_state_variable
*tsv
;
3206 namebase
= utsv
->name
;
3212 buf
= string_printf ("%s_%d", namebase
, try_num
++);
3215 /* Fish for a name that is not in use. */
3216 /* (should check against all internal vars?) */
3217 while (find_trace_state_variable (buf
.c_str ()))
3218 buf
= string_printf ("%s_%d", namebase
, try_num
++);
3220 /* We have an available name, create the variable. */
3221 tsv
= create_trace_state_variable (buf
.c_str ());
3222 tsv
->initial_value
= utsv
->initial_value
;
3223 tsv
->builtin
= utsv
->builtin
;
3225 gdb::observers::tsv_created
.notify (tsv
);
3230 /* Given a list of uploaded trace state variables, try to match them
3231 up with existing variables, or create additional ones. */
3234 merge_uploaded_trace_state_variables (struct uploaded_tsv
**uploaded_tsvs
)
3236 struct uploaded_tsv
*utsv
;
3239 /* Most likely some numbers will have to be reassigned as part of
3240 the merge, so clear them all in anticipation. */
3241 for (trace_state_variable
&tsv
: tvariables
)
3244 for (utsv
= *uploaded_tsvs
; utsv
; utsv
= utsv
->next
)
3246 struct trace_state_variable
*tsv
= find_matching_tsv (utsv
);
3250 printf_filtered (_("Assuming trace state variable $%s "
3251 "is same as target's variable %d.\n"),
3252 tsv
->name
.c_str (), utsv
->number
);
3256 tsv
= create_tsv_from_upload (utsv
);
3258 printf_filtered (_("Created trace state variable "
3259 "$%s for target's variable %d.\n"),
3260 tsv
->name
.c_str (), utsv
->number
);
3262 /* Give precedence to numberings that come from the target. */
3264 tsv
->number
= utsv
->number
;
3267 /* Renumber everything that didn't get a target-assigned number. */
3269 for (const trace_state_variable
&tsv
: tvariables
)
3270 highest
= std::max (tsv
.number
, highest
);
3273 for (trace_state_variable
&tsv
: tvariables
)
3274 if (tsv
.number
== 0)
3275 tsv
.number
= highest
++;
3277 free_uploaded_tsvs (uploaded_tsvs
);
3280 /* Parse the part of trace status syntax that is shared between
3281 the remote protocol and the trace file reader. */
3284 parse_trace_status (const char *line
, struct trace_status
*ts
)
3286 const char *p
= line
, *p1
, *p2
, *p3
, *p_temp
;
3290 ts
->running_known
= 1;
3291 ts
->running
= (*p
++ == '1');
3292 ts
->stop_reason
= trace_stop_reason_unknown
;
3293 xfree (ts
->stop_desc
);
3294 ts
->stop_desc
= NULL
;
3295 ts
->traceframe_count
= -1;
3296 ts
->traceframes_created
= -1;
3297 ts
->buffer_free
= -1;
3298 ts
->buffer_size
= -1;
3299 ts
->disconnected_tracing
= 0;
3300 ts
->circular_buffer
= 0;
3301 xfree (ts
->user_name
);
3302 ts
->user_name
= NULL
;
3305 ts
->start_time
= ts
->stop_time
= 0;
3309 p1
= strchr (p
, ':');
3311 error (_("Malformed trace status, at %s\n\
3312 Status line: '%s'\n"), p
, line
);
3313 p3
= strchr (p
, ';');
3315 p3
= p
+ strlen (p
);
3316 if (strncmp (p
, stop_reason_names
[trace_buffer_full
], p1
- p
) == 0)
3318 p
= unpack_varlen_hex (++p1
, &val
);
3319 ts
->stop_reason
= trace_buffer_full
;
3321 else if (strncmp (p
, stop_reason_names
[trace_never_run
], p1
- p
) == 0)
3323 p
= unpack_varlen_hex (++p1
, &val
);
3324 ts
->stop_reason
= trace_never_run
;
3326 else if (strncmp (p
, stop_reason_names
[tracepoint_passcount
],
3329 p
= unpack_varlen_hex (++p1
, &val
);
3330 ts
->stop_reason
= tracepoint_passcount
;
3331 ts
->stopping_tracepoint
= val
;
3333 else if (strncmp (p
, stop_reason_names
[trace_stop_command
], p1
- p
) == 0)
3335 p2
= strchr (++p1
, ':');
3343 ts
->stop_desc
= (char *) xmalloc (strlen (line
));
3344 end
= hex2bin (p1
, (gdb_byte
*) ts
->stop_desc
, (p2
- p1
) / 2);
3345 ts
->stop_desc
[end
] = '\0';
3348 ts
->stop_desc
= xstrdup ("");
3350 p
= unpack_varlen_hex (++p2
, &val
);
3351 ts
->stop_reason
= trace_stop_command
;
3353 else if (strncmp (p
, stop_reason_names
[trace_disconnected
], p1
- p
) == 0)
3355 p
= unpack_varlen_hex (++p1
, &val
);
3356 ts
->stop_reason
= trace_disconnected
;
3358 else if (strncmp (p
, stop_reason_names
[tracepoint_error
], p1
- p
) == 0)
3360 p2
= strchr (++p1
, ':');
3363 ts
->stop_desc
= (char *) xmalloc ((p2
- p1
) / 2 + 1);
3364 end
= hex2bin (p1
, (gdb_byte
*) ts
->stop_desc
, (p2
- p1
) / 2);
3365 ts
->stop_desc
[end
] = '\0';
3368 ts
->stop_desc
= xstrdup ("");
3370 p
= unpack_varlen_hex (++p2
, &val
);
3371 ts
->stopping_tracepoint
= val
;
3372 ts
->stop_reason
= tracepoint_error
;
3374 else if (strncmp (p
, "tframes", p1
- p
) == 0)
3376 p
= unpack_varlen_hex (++p1
, &val
);
3377 ts
->traceframe_count
= val
;
3379 else if (strncmp (p
, "tcreated", p1
- p
) == 0)
3381 p
= unpack_varlen_hex (++p1
, &val
);
3382 ts
->traceframes_created
= val
;
3384 else if (strncmp (p
, "tfree", p1
- p
) == 0)
3386 p
= unpack_varlen_hex (++p1
, &val
);
3387 ts
->buffer_free
= val
;
3389 else if (strncmp (p
, "tsize", p1
- p
) == 0)
3391 p
= unpack_varlen_hex (++p1
, &val
);
3392 ts
->buffer_size
= val
;
3394 else if (strncmp (p
, "disconn", p1
- p
) == 0)
3396 p
= unpack_varlen_hex (++p1
, &val
);
3397 ts
->disconnected_tracing
= val
;
3399 else if (strncmp (p
, "circular", p1
- p
) == 0)
3401 p
= unpack_varlen_hex (++p1
, &val
);
3402 ts
->circular_buffer
= val
;
3404 else if (strncmp (p
, "starttime", p1
- p
) == 0)
3406 p
= unpack_varlen_hex (++p1
, &val
);
3407 ts
->start_time
= val
;
3409 else if (strncmp (p
, "stoptime", p1
- p
) == 0)
3411 p
= unpack_varlen_hex (++p1
, &val
);
3412 ts
->stop_time
= val
;
3414 else if (strncmp (p
, "username", p1
- p
) == 0)
3417 ts
->user_name
= (char *) xmalloc (strlen (p
) / 2);
3418 end
= hex2bin (p1
, (gdb_byte
*) ts
->user_name
, (p3
- p1
) / 2);
3419 ts
->user_name
[end
] = '\0';
3422 else if (strncmp (p
, "notes", p1
- p
) == 0)
3425 ts
->notes
= (char *) xmalloc (strlen (p
) / 2);
3426 end
= hex2bin (p1
, (gdb_byte
*) ts
->notes
, (p3
- p1
) / 2);
3427 ts
->notes
[end
] = '\0';
3432 /* Silently skip unknown optional info. */
3433 p_temp
= strchr (p1
+ 1, ';');
3437 /* Must be at the end. */
3444 parse_tracepoint_status (const char *p
, struct breakpoint
*bp
,
3445 struct uploaded_tp
*utp
)
3448 struct tracepoint
*tp
= (struct tracepoint
*) bp
;
3450 p
= unpack_varlen_hex (p
, &uval
);
3452 tp
->hit_count
+= uval
;
3454 utp
->hit_count
+= uval
;
3455 p
= unpack_varlen_hex (p
+ 1, &uval
);
3457 tp
->traceframe_usage
+= uval
;
3459 utp
->traceframe_usage
+= uval
;
3460 /* Ignore any extra, allowing for future extensions. */
3463 /* Given a line of text defining a part of a tracepoint, parse it into
3464 an "uploaded tracepoint". */
3467 parse_tracepoint_definition (const char *line
, struct uploaded_tp
**utpp
)
3471 ULONGEST num
, addr
, step
, pass
, orig_size
, xlen
, start
;
3474 const char *srctype
;
3476 struct uploaded_tp
*utp
= NULL
;
3479 /* Both tracepoint and action definitions start with the same number
3480 and address sequence. */
3482 p
= unpack_varlen_hex (p
, &num
);
3483 p
++; /* skip a colon */
3484 p
= unpack_varlen_hex (p
, &addr
);
3485 p
++; /* skip a colon */
3488 gdb::unique_xmalloc_ptr
<char[]> cond
;
3490 enabled
= (*p
++ == 'E');
3491 p
++; /* skip a colon */
3492 p
= unpack_varlen_hex (p
, &step
);
3493 p
++; /* skip a colon */
3494 p
= unpack_varlen_hex (p
, &pass
);
3495 type
= bp_tracepoint
;
3496 /* Thumb through optional fields. */
3499 p
++; /* skip a colon */
3502 type
= bp_fast_tracepoint
;
3504 p
= unpack_varlen_hex (p
, &orig_size
);
3508 type
= bp_static_tracepoint
;
3514 p
= unpack_varlen_hex (p
, &xlen
);
3515 p
++; /* skip a comma */
3516 cond
.reset ((char *) xmalloc (2 * xlen
+ 1));
3517 strncpy (&cond
[0], p
, 2 * xlen
);
3518 cond
[2 * xlen
] = '\0';
3522 warning (_("Unrecognized char '%c' in tracepoint "
3523 "definition, skipping rest"), *p
);
3525 utp
= get_uploaded_tp (num
, addr
, utpp
);
3527 utp
->enabled
= enabled
;
3530 utp
->cond
= std::move (cond
);
3532 else if (piece
== 'A')
3534 utp
= get_uploaded_tp (num
, addr
, utpp
);
3535 utp
->actions
.emplace_back (xstrdup (p
));
3537 else if (piece
== 'S')
3539 utp
= get_uploaded_tp (num
, addr
, utpp
);
3540 utp
->step_actions
.emplace_back (xstrdup (p
));
3542 else if (piece
== 'Z')
3544 /* Parse a chunk of source form definition. */
3545 utp
= get_uploaded_tp (num
, addr
, utpp
);
3547 p
= strchr (p
, ':');
3548 p
++; /* skip a colon */
3549 p
= unpack_varlen_hex (p
, &start
);
3550 p
++; /* skip a colon */
3551 p
= unpack_varlen_hex (p
, &xlen
);
3552 p
++; /* skip a colon */
3554 buf
= (char *) alloca (strlen (line
));
3556 end
= hex2bin (p
, (gdb_byte
*) buf
, strlen (p
) / 2);
3559 if (startswith (srctype
, "at:"))
3560 utp
->at_string
.reset (xstrdup (buf
));
3561 else if (startswith (srctype
, "cond:"))
3562 utp
->cond_string
.reset (xstrdup (buf
));
3563 else if (startswith (srctype
, "cmd:"))
3564 utp
->cmd_strings
.emplace_back (xstrdup (buf
));
3566 else if (piece
== 'V')
3568 utp
= get_uploaded_tp (num
, addr
, utpp
);
3570 parse_tracepoint_status (p
, NULL
, utp
);
3574 /* Don't error out, the target might be sending us optional
3575 info that we don't care about. */
3576 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece
);
3580 /* Convert a textual description of a trace state variable into an
3584 parse_tsv_definition (const char *line
, struct uploaded_tsv
**utsvp
)
3588 ULONGEST num
, initval
, builtin
;
3590 struct uploaded_tsv
*utsv
= NULL
;
3592 buf
= (char *) alloca (strlen (line
));
3595 p
= unpack_varlen_hex (p
, &num
);
3596 p
++; /* skip a colon */
3597 p
= unpack_varlen_hex (p
, &initval
);
3598 p
++; /* skip a colon */
3599 p
= unpack_varlen_hex (p
, &builtin
);
3600 p
++; /* skip a colon */
3601 end
= hex2bin (p
, (gdb_byte
*) buf
, strlen (p
) / 2);
3604 utsv
= get_uploaded_tsv (num
, utsvp
);
3605 utsv
->initial_value
= initval
;
3606 utsv
->builtin
= builtin
;
3607 utsv
->name
= xstrdup (buf
);
3610 /* Given a line of text defining a static tracepoint marker, parse it
3611 into a "static tracepoint marker" object. Throws an error is
3612 parsing fails. If PP is non-null, it points to one past the end of
3613 the parsed marker definition. */
3616 parse_static_tracepoint_marker_definition (const char *line
, const char **pp
,
3617 static_tracepoint_marker
*marker
)
3619 const char *p
, *endp
;
3623 p
= unpack_varlen_hex (p
, &addr
);
3624 p
++; /* skip a colon */
3626 marker
->gdbarch
= target_gdbarch ();
3627 marker
->address
= (CORE_ADDR
) addr
;
3629 endp
= strchr (p
, ':');
3631 error (_("bad marker definition: %s"), line
);
3633 marker
->str_id
= hex2str (p
, (endp
- p
) / 2);
3636 p
++; /* skip a colon */
3638 /* This definition may be followed by another one, separated by a comma. */
3640 endp
= strchr (p
, ',');
3641 if (endp
!= nullptr)
3644 hex_len
= strlen (p
);
3646 marker
->extra
= hex2str (p
, hex_len
/ 2);
3652 /* Print MARKER to gdb_stdout. */
3655 print_one_static_tracepoint_marker (int count
,
3656 const static_tracepoint_marker
&marker
)
3660 char wrap_indent
[80];
3661 char extra_field_indent
[80];
3662 struct ui_out
*uiout
= current_uiout
;
3664 symtab_and_line sal
;
3665 sal
.pc
= marker
.address
;
3667 std::vector
<breakpoint
*> tracepoints
3668 = static_tracepoints_here (marker
.address
);
3670 ui_out_emit_tuple
tuple_emitter (uiout
, "marker");
3672 /* A counter field to help readability. This is not a stable
3674 uiout
->field_signed ("count", count
);
3676 uiout
->field_string ("marker-id", marker
.str_id
);
3678 uiout
->field_fmt ("enabled", "%c",
3679 !tracepoints
.empty () ? 'y' : 'n');
3682 strcpy (wrap_indent
, " ");
3684 if (gdbarch_addr_bit (marker
.gdbarch
) <= 32)
3685 strcat (wrap_indent
, " ");
3687 strcat (wrap_indent
, " ");
3689 strcpy (extra_field_indent
, " ");
3691 uiout
->field_core_addr ("addr", marker
.gdbarch
, marker
.address
);
3693 sal
= find_pc_line (marker
.address
, 0);
3694 sym
= find_pc_sect_function (marker
.address
, NULL
);
3697 uiout
->text ("in ");
3698 uiout
->field_string ("func", sym
->print_name (),
3699 function_name_style
.style ());
3700 uiout
->wrap_hint (wrap_indent
);
3701 uiout
->text (" at ");
3704 uiout
->field_skip ("func");
3706 if (sal
.symtab
!= NULL
)
3708 uiout
->field_string ("file",
3709 symtab_to_filename_for_display (sal
.symtab
),
3710 file_name_style
.style ());
3713 if (uiout
->is_mi_like_p ())
3715 const char *fullname
= symtab_to_fullname (sal
.symtab
);
3717 uiout
->field_string ("fullname", fullname
);
3720 uiout
->field_skip ("fullname");
3722 uiout
->field_signed ("line", sal
.line
);
3726 uiout
->field_skip ("fullname");
3727 uiout
->field_skip ("line");
3731 uiout
->text (extra_field_indent
);
3732 uiout
->text (_("Data: \""));
3733 uiout
->field_string ("extra-data", marker
.extra
);
3734 uiout
->text ("\"\n");
3736 if (!tracepoints
.empty ())
3741 ui_out_emit_tuple
inner_tuple_emitter (uiout
, "tracepoints-at");
3743 uiout
->text (extra_field_indent
);
3744 uiout
->text (_("Probed by static tracepoints: "));
3745 for (ix
= 0; ix
< tracepoints
.size (); ix
++)
3750 uiout
->field_signed ("tracepoint-id", tracepoints
[ix
]->number
);
3754 if (uiout
->is_mi_like_p ())
3755 uiout
->field_signed ("number-of-tracepoints", tracepoints
.size ());
3762 info_static_tracepoint_markers_command (const char *arg
, int from_tty
)
3764 struct ui_out
*uiout
= current_uiout
;
3765 std::vector
<static_tracepoint_marker
> markers
3766 = target_static_tracepoint_markers_by_strid (NULL
);
3768 /* We don't have to check target_can_use_agent and agent's capability on
3769 static tracepoint here, in order to be compatible with older GDBserver.
3770 We don't check USE_AGENT is true or not, because static tracepoints
3771 don't work without in-process agent, so we don't bother users to type
3772 `set agent on' when to use static tracepoint. */
3774 ui_out_emit_table
table_emitter (uiout
, 5, -1,
3775 "StaticTracepointMarkersTable");
3777 uiout
->table_header (7, ui_left
, "counter", "Cnt");
3779 uiout
->table_header (40, ui_left
, "marker-id", "ID");
3781 uiout
->table_header (3, ui_left
, "enabled", "Enb");
3782 if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
3783 uiout
->table_header (10, ui_left
, "addr", "Address");
3785 uiout
->table_header (18, ui_left
, "addr", "Address");
3786 uiout
->table_header (40, ui_noalign
, "what", "What");
3788 uiout
->table_body ();
3790 for (int i
= 0; i
< markers
.size (); i
++)
3791 print_one_static_tracepoint_marker (i
+ 1, markers
[i
]);
3794 /* The $_sdata convenience variable is a bit special. We don't know
3795 for sure type of the value until we actually have a chance to fetch
3796 the data --- the size of the object depends on what has been
3797 collected. We solve this by making $_sdata be an internalvar that
3798 creates a new value on access. */
3800 /* Return a new value with the correct type for the sdata object of
3801 the current trace frame. Return a void value if there's no object
3804 static struct value
*
3805 sdata_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
3808 /* We need to read the whole object before we know its size. */
3809 gdb::optional
<gdb::byte_vector
> buf
3810 = target_read_alloc (current_inferior ()->top_target (),
3811 TARGET_OBJECT_STATIC_TRACE_DATA
,
3818 type
= init_vector_type (builtin_type (gdbarch
)->builtin_true_char
,
3820 v
= allocate_value (type
);
3821 memcpy (value_contents_raw (v
), buf
->data (), buf
->size ());
3825 return allocate_value (builtin_type (gdbarch
)->builtin_void
);
3828 #if !defined(HAVE_LIBEXPAT)
3830 struct std::unique_ptr
<traceframe_info
>
3831 parse_traceframe_info (const char *tframe_info
)
3833 static int have_warned
;
3838 warning (_("Can not parse XML trace frame info; XML support "
3839 "was disabled at compile time"));
3845 #else /* HAVE_LIBEXPAT */
3847 #include "xml-support.h"
3849 /* Handle the start of a <memory> element. */
3852 traceframe_info_start_memory (struct gdb_xml_parser
*parser
,
3853 const struct gdb_xml_element
*element
,
3855 std::vector
<gdb_xml_value
> &attributes
)
3857 struct traceframe_info
*info
= (struct traceframe_info
*) user_data
;
3858 ULONGEST
*start_p
, *length_p
;
3861 = (ULONGEST
*) xml_find_attribute (attributes
, "start")->value
.get ();
3863 = (ULONGEST
*) xml_find_attribute (attributes
, "length")->value
.get ();
3865 info
->memory
.emplace_back (*start_p
, *length_p
);
3868 /* Handle the start of a <tvar> element. */
3871 traceframe_info_start_tvar (struct gdb_xml_parser
*parser
,
3872 const struct gdb_xml_element
*element
,
3874 std::vector
<gdb_xml_value
> &attributes
)
3876 struct traceframe_info
*info
= (struct traceframe_info
*) user_data
;
3877 const char *id_attrib
3878 = (const char *) xml_find_attribute (attributes
, "id")->value
.get ();
3879 int id
= gdb_xml_parse_ulongest (parser
, id_attrib
);
3881 info
->tvars
.push_back (id
);
3884 /* The allowed elements and attributes for an XML memory map. */
3886 static const struct gdb_xml_attribute memory_attributes
[] = {
3887 { "start", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
3888 { "length", GDB_XML_AF_NONE
, gdb_xml_parse_attr_ulongest
, NULL
},
3889 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
3892 static const struct gdb_xml_attribute tvar_attributes
[] = {
3893 { "id", GDB_XML_AF_NONE
, NULL
, NULL
},
3894 { NULL
, GDB_XML_AF_NONE
, NULL
, NULL
}
3897 static const struct gdb_xml_element traceframe_info_children
[] = {
3898 { "memory", memory_attributes
, NULL
,
3899 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
3900 traceframe_info_start_memory
, NULL
},
3901 { "tvar", tvar_attributes
, NULL
,
3902 GDB_XML_EF_REPEATABLE
| GDB_XML_EF_OPTIONAL
,
3903 traceframe_info_start_tvar
, NULL
},
3904 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
3907 static const struct gdb_xml_element traceframe_info_elements
[] = {
3908 { "traceframe-info", NULL
, traceframe_info_children
, GDB_XML_EF_NONE
,
3910 { NULL
, NULL
, NULL
, GDB_XML_EF_NONE
, NULL
, NULL
}
3913 /* Parse a traceframe-info XML document. */
3916 parse_traceframe_info (const char *tframe_info
)
3918 traceframe_info_up
result (new traceframe_info
);
3920 if (gdb_xml_parse_quick (_("trace frame info"),
3921 "traceframe-info.dtd", traceframe_info_elements
,
3922 tframe_info
, result
.get ()) == 0)
3928 #endif /* HAVE_LIBEXPAT */
3930 /* Returns the traceframe_info object for the current traceframe.
3931 This is where we avoid re-fetching the object from the target if we
3932 already have it cached. */
3934 struct traceframe_info
*
3935 get_traceframe_info (void)
3937 if (current_traceframe_info
== NULL
)
3938 current_traceframe_info
= target_traceframe_info ();
3940 return current_traceframe_info
.get ();
3943 /* If the target supports the query, return in RESULT the set of
3944 collected memory in the current traceframe, found within the LEN
3945 bytes range starting at MEMADDR. Returns true if the target
3946 supports the query, otherwise returns false, and RESULT is left
3950 traceframe_available_memory (std::vector
<mem_range
> *result
,
3951 CORE_ADDR memaddr
, ULONGEST len
)
3953 struct traceframe_info
*info
= get_traceframe_info ();
3959 for (mem_range
&r
: info
->memory
)
3960 if (mem_ranges_overlap (r
.start
, r
.length
, memaddr
, len
))
3962 ULONGEST lo1
, hi1
, lo2
, hi2
;
3965 hi1
= memaddr
+ len
;
3968 hi2
= r
.start
+ r
.length
;
3970 CORE_ADDR start
= std::max (lo1
, lo2
);
3971 int length
= std::min (hi1
, hi2
) - start
;
3973 result
->emplace_back (start
, length
);
3976 normalize_mem_ranges (result
);
3983 /* Implementation of `sdata' variable. */
3985 static const struct internalvar_funcs sdata_funcs
=
3992 /* See tracepoint.h. */
3993 cmd_list_element
*while_stepping_cmd_element
= nullptr;
3995 /* module initialization */
3996 void _initialize_tracepoint ();
3998 _initialize_tracepoint ()
4000 struct cmd_list_element
*c
;
4002 /* Explicitly create without lookup, since that tries to create a
4003 value with a void typed value, and when we get here, gdbarch
4004 isn't initialized yet. At this point, we're quite sure there
4005 isn't another convenience variable of the same name. */
4006 create_internalvar_type_lazy ("_sdata", &sdata_funcs
, NULL
);
4008 traceframe_number
= -1;
4009 tracepoint_number
= -1;
4011 add_info ("scope", info_scope_command
,
4012 _("List the variables local to a scope."));
4014 add_cmd ("tracepoints", class_trace
,
4015 _("Tracing of program execution without stopping the program."),
4018 add_com ("tdump", class_trace
, tdump_command
,
4019 _("Print everything collected at the current tracepoint."));
4021 c
= add_com ("tvariable", class_trace
, trace_variable_command
,_("\
4022 Define a trace state variable.\n\
4023 Argument is a $-prefixed name, optionally followed\n\
4024 by '=' and an expression that sets the initial value\n\
4025 at the start of tracing."));
4026 set_cmd_completer (c
, expression_completer
);
4028 add_cmd ("tvariable", class_trace
, delete_trace_variable_command
, _("\
4029 Delete one or more trace state variables.\n\
4030 Arguments are the names of the variables to delete.\n\
4031 If no arguments are supplied, delete all variables."), &deletelist
);
4032 /* FIXME add a trace variable completer. */
4034 add_info ("tvariables", info_tvariables_command
, _("\
4035 Status of trace state variables and their values."));
4037 add_info ("static-tracepoint-markers",
4038 info_static_tracepoint_markers_command
, _("\
4039 List target static tracepoints markers."));
4041 add_prefix_cmd ("tfind", class_trace
, tfind_command
, _("\
4042 Select a trace frame.\n\
4043 No argument means forward by one frame; '-' means backward by one frame."),
4044 &tfindlist
, 1, &cmdlist
);
4046 add_cmd ("outside", class_trace
, tfind_outside_command
, _("\
4047 Select a trace frame whose PC is outside the given range (exclusive).\n\
4048 Usage: tfind outside ADDR1, ADDR2"),
4051 add_cmd ("range", class_trace
, tfind_range_command
, _("\
4052 Select a trace frame whose PC is in the given range (inclusive).\n\
4053 Usage: tfind range ADDR1, ADDR2"),
4056 add_cmd ("line", class_trace
, tfind_line_command
, _("\
4057 Select a trace frame by source line.\n\
4058 Argument can be a line number (with optional source file),\n\
4059 a function name, or '*' followed by an address.\n\
4060 Default argument is 'the next source line that was traced'."),
4063 add_cmd ("tracepoint", class_trace
, tfind_tracepoint_command
, _("\
4064 Select a trace frame by tracepoint number.\n\
4065 Default is the tracepoint for the current trace frame."),
4068 add_cmd ("pc", class_trace
, tfind_pc_command
, _("\
4069 Select a trace frame by PC.\n\
4070 Default is the current PC, or the PC of the current trace frame."),
4073 cmd_list_element
*tfind_end_cmd
4074 = add_cmd ("end", class_trace
, tfind_end_command
, _("\
4075 De-select any trace frame and resume 'live' debugging."), &tfindlist
);
4077 add_alias_cmd ("none", tfind_end_cmd
, class_trace
, 0, &tfindlist
);
4079 add_cmd ("start", class_trace
, tfind_start_command
,
4080 _("Select the first trace frame in the trace buffer."),
4083 add_com ("tstatus", class_trace
, tstatus_command
,
4084 _("Display the status of the current trace data collection."));
4086 add_com ("tstop", class_trace
, tstop_command
, _("\
4087 Stop trace data collection.\n\
4088 Usage: tstop [NOTES]...\n\
4089 Any arguments supplied are recorded with the trace as a stop reason and\n\
4090 reported by tstatus (if the target supports trace notes)."));
4092 add_com ("tstart", class_trace
, tstart_command
, _("\
4093 Start trace data collection.\n\
4094 Usage: tstart [NOTES]...\n\
4095 Any arguments supplied are recorded with the trace as a note and\n\
4096 reported by tstatus (if the target supports trace notes)."));
4098 add_com ("end", class_trace
, end_actions_pseudocommand
, _("\
4099 Ends a list of commands or actions.\n\
4100 Several GDB commands allow you to enter a list of commands or actions.\n\
4101 Entering \"end\" on a line by itself is the normal way to terminate\n\
4103 Note: the \"end\" command cannot be used at the gdb prompt."));
4105 while_stepping_cmd_element
= add_com ("while-stepping", class_trace
,
4106 while_stepping_pseudocommand
, _("\
4107 Specify single-stepping behavior at a tracepoint.\n\
4108 Argument is number of instructions to trace in single-step mode\n\
4109 following the tracepoint. This command is normally followed by\n\
4110 one or more \"collect\" commands, to specify what to collect\n\
4111 while single-stepping.\n\n\
4112 Note: this command can only be used in a tracepoint \"actions\" list."));
4114 add_com_alias ("ws", while_stepping_cmd_element
, class_trace
, 0);
4115 add_com_alias ("stepping", while_stepping_cmd_element
, class_trace
, 0);
4117 add_com ("collect", class_trace
, collect_pseudocommand
, _("\
4118 Specify one or more data items to be collected at a tracepoint.\n\
4119 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
4120 collect all data (variables, registers) referenced by that expression.\n\
4121 Also accepts the following special arguments:\n\
4122 $regs -- all registers.\n\
4123 $args -- all function arguments.\n\
4124 $locals -- all variables local to the block/function scope.\n\
4125 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
4126 Note: this command can only be used in a tracepoint \"actions\" list."));
4128 add_com ("teval", class_trace
, teval_pseudocommand
, _("\
4129 Specify one or more expressions to be evaluated at a tracepoint.\n\
4130 Accepts a comma-separated list of (one or more) expressions.\n\
4131 The result of each evaluation will be discarded.\n\
4132 Note: this command can only be used in a tracepoint \"actions\" list."));
4134 add_com ("actions", class_trace
, actions_command
, _("\
4135 Specify the actions to be taken at a tracepoint.\n\
4136 Tracepoint actions may include collecting of specified data,\n\
4137 single-stepping, or enabling/disabling other tracepoints,\n\
4138 depending on target's capabilities."));
4140 default_collect
= xstrdup ("");
4141 add_setshow_string_cmd ("default-collect", class_trace
,
4142 &default_collect
, _("\
4143 Set the list of expressions to collect by default."), _("\
4144 Show the list of expressions to collect by default."), NULL
,
4146 &setlist
, &showlist
);
4148 add_setshow_boolean_cmd ("disconnected-tracing", no_class
,
4149 &disconnected_tracing
, _("\
4150 Set whether tracing continues after GDB disconnects."), _("\
4151 Show whether tracing continues after GDB disconnects."), _("\
4152 Use this to continue a tracing run even if GDB disconnects\n\
4153 or detaches from the target. You can reconnect later and look at\n\
4154 trace data collected in the meantime."),
4155 set_disconnected_tracing
,
4160 add_setshow_boolean_cmd ("circular-trace-buffer", no_class
,
4161 &circular_trace_buffer
, _("\
4162 Set target's use of circular trace buffer."), _("\
4163 Show target's use of circular trace buffer."), _("\
4164 Use this to make the trace buffer into a circular buffer,\n\
4165 which will discard traceframes (oldest first) instead of filling\n\
4166 up and stopping the trace run."),
4167 set_circular_trace_buffer
,
4172 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class
,
4173 &trace_buffer_size
, _("\
4174 Set requested size of trace buffer."), _("\
4175 Show requested size of trace buffer."), _("\
4176 Use this to choose a size for the trace buffer. Some targets\n\
4177 may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
4178 disables any attempt to set the buffer size and lets the target choose."),
4179 set_trace_buffer_size
, NULL
,
4180 &setlist
, &showlist
);
4182 add_setshow_string_cmd ("trace-user", class_trace
,
4184 Set the user name to use for current and future trace runs."), _("\
4185 Show the user name to use for current and future trace runs."), NULL
,
4186 set_trace_user
, NULL
,
4187 &setlist
, &showlist
);
4189 add_setshow_string_cmd ("trace-notes", class_trace
,
4191 Set notes string to use for current and future trace runs."), _("\
4192 Show the notes string to use for current and future trace runs."), NULL
,
4193 set_trace_notes
, NULL
,
4194 &setlist
, &showlist
);
4196 add_setshow_string_cmd ("trace-stop-notes", class_trace
,
4197 &trace_stop_notes
, _("\
4198 Set notes string to use for future tstop commands."), _("\
4199 Show the notes string to use for future tstop commands."), NULL
,
4200 set_trace_stop_notes
, NULL
,
4201 &setlist
, &showlist
);