]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/tracepoint.c
gdb: move a bunch of quit-related things to event-top.{c,h}
[thirdparty/binutils-gdb.git] / gdb / tracepoint.c
CommitLineData
c906108c 1/* Tracing functionality for remote targets in custom GDB protocol
9f60d481 2
1d506c26 3 Copyright (C) 1997-2024 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
5af949e3 20#include "arch-utils.h"
e5dc0d5d 21#include "event-top.h"
c906108c
SS
22#include "symtab.h"
23#include "frame.h"
c906108c
SS
24#include "gdbtypes.h"
25#include "expression.h"
26#include "gdbcmd.h"
27#include "value.h"
28#include "target.h"
68c765e2 29#include "target-dcache.h"
c906108c 30#include "language.h"
104c1213 31#include "inferior.h"
1042e4c0 32#include "breakpoint.h"
104c1213 33#include "tracepoint.h"
c5f0f3d0 34#include "linespec.h"
4e052eda 35#include "regcache.h"
c94fdfd0 36#include "completer.h"
fe898f56 37#include "block.h"
de4f826b 38#include "dictionary.h"
76727919 39#include "observable.h"
029a67e4 40#include "user-regs.h"
79a45b7d 41#include "valprint.h"
41575630 42#include "gdbcore.h"
768a979c 43#include "objfiles.h"
35b1e5cc 44#include "filenames.h"
00bf0b85 45#include "gdbthread.h"
2c58c0a9 46#include "stack.h"
176a6961 47#include "remote.h"
0fb4aa4b 48#include "source.h"
c906108c
SS
49#include "ax.h"
50#include "ax-gdb.h"
2a7498d8 51#include "memrange.h"
3065dfb6 52#include "cli/cli-utils.h"
55aa24fb 53#include "probe.h"
268a13a5
TT
54#include "gdbsupport/filestuff.h"
55#include "gdbsupport/rsp-low.h"
7951c4eb 56#include "tracefile.h"
f00aae0f 57#include "location.h"
325fac50 58#include <algorithm>
e43b10e1 59#include "cli/cli-style.h"
413403fc 60#include "expop.h"
7904e961 61#include "gdbsupport/buildargv.h"
0bc845fc 62#include "interps.h"
c906108c 63
c906108c 64#include <unistd.h>
c906108c 65
d183932d
MS
66/* Maximum length of an agent aexpression.
67 This accounts for the fact that packets are limited to 400 bytes
c906108c
SS
68 (which includes everything -- including the checksum), and assumes
69 the worst case of maximum length for each of the pieces of a
70 continuation packet.
c5aa993b 71
90db289d 72 NOTE: expressions get bin2hex'ed otherwise this would be twice as
c906108c
SS
73 large. (400 - 31)/2 == 184 */
74#define MAX_AGENT_EXPR_LEN 184
75
c906108c
SS
76/*
77 Tracepoint.c:
78
79 This module defines the following debugger commands:
80 trace : set a tracepoint on a function, line, or address.
81 info trace : list all debugger-defined tracepoints.
82 delete trace : delete one or more tracepoints.
83 enable trace : enable one or more tracepoints.
84 disable trace : disable one or more tracepoints.
85 actions : specify actions to be taken at a tracepoint.
86 passcount : specify a pass count for a tracepoint.
87 tstart : start a trace experiment.
88 tstop : stop a trace experiment.
89 tstatus : query the status of a trace experiment.
90 tfind : find a trace frame in the trace buffer.
91 tdump : print everything collected at the current tracepoint.
92 save-tracepoints : write tracepoint setup into a file.
93
94 This module defines the following user-visible debugger variables:
95 $trace_frame : sequence number of trace frame currently being debugged.
96 $trace_line : source line of trace frame currently being debugged.
97 $trace_file : source file of trace frame currently being debugged.
98 $tracepoint : tracepoint number of trace frame currently being debugged.
c5aa993b 99 */
c906108c
SS
100
101
102/* ======= Important global variables: ======= */
103
f61e138d
SS
104/* The list of all trace state variables. We don't retain pointers to
105 any of these for any reason - API is by name or number only - so it
106 works to have a vector of objects. */
107
c252925c 108static std::vector<trace_state_variable> tvariables;
f61e138d
SS
109
110/* The next integer to assign to a variable. */
111
112static int next_tsv_number = 1;
113
c906108c
SS
114/* Number of last traceframe collected. */
115static int traceframe_number;
116
117/* Tracepoint for last traceframe collected. */
118static int tracepoint_number;
119
b3b9301e
PA
120/* The traceframe info of the current traceframe. NULL if we haven't
121 yet attempted to fetch it, or if the target does not support
122 fetching this object, or if we're not inspecting a traceframe
123 presently. */
2098b393 124static traceframe_info_up current_traceframe_info;
b3b9301e 125
c378eb4e 126/* Tracing command lists. */
c906108c
SS
127static struct cmd_list_element *tfindlist;
128
236f1d4d 129/* List of expressions to collect by default at each tracepoint hit. */
e0700ba4 130std::string default_collect;
236f1d4d 131
491144b5 132static bool disconnected_tracing;
d5551862 133
4daf5ac0
SS
134/* This variable controls whether we ask the target for a linear or
135 circular trace buffer. */
136
491144b5 137static bool circular_trace_buffer;
4daf5ac0 138
f6f899bf
HAQ
139/* This variable is the requested trace buffer size, or -1 to indicate
140 that we don't care and leave it up to the target to set a size. */
141
142static int trace_buffer_size = -1;
143
f196051f
SS
144/* Textual notes applying to the current and/or future trace runs. */
145
e0700ba4 146static std::string trace_user;
f196051f
SS
147
148/* Textual notes applying to the current and/or future trace runs. */
149
e0700ba4 150static std::string trace_notes;
f196051f
SS
151
152/* Textual notes applying to the stopping of a trace. */
153
e0700ba4 154static std::string trace_stop_notes;
f196051f 155
c906108c 156/* support routines */
c906108c
SS
157
158struct collection_list;
392a587b 159
01bccc56 160static counted_command_line all_tracepoint_actions (tracepoint *);
00bf0b85 161
00bf0b85
SS
162static struct trace_status trace_status;
163
7951c4eb 164const char *stop_reason_names[] = {
00bf0b85
SS
165 "tunknown",
166 "tnotrun",
167 "tstop",
168 "tfull",
169 "tdisconnected",
6c28cbf2
SS
170 "tpasscount",
171 "terror"
00bf0b85
SS
172};
173
174struct trace_status *
eeae04df 175current_trace_status (void)
00bf0b85
SS
176{
177 return &trace_status;
178}
179
7a9dd1b2 180/* Free and clear the traceframe info cache of the current
b3b9301e
PA
181 traceframe. */
182
183static void
184clear_traceframe_info (void)
185{
8d3c73ef 186 current_traceframe_info = NULL;
b3b9301e
PA
187}
188
c906108c
SS
189/* Set traceframe number to NUM. */
190static void
fba45db2 191set_traceframe_num (int num)
c906108c
SS
192{
193 traceframe_number = num;
4fa62494 194 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
c906108c
SS
195}
196
197/* Set tracepoint number to NUM. */
198static void
fba45db2 199set_tracepoint_num (int num)
c906108c
SS
200{
201 tracepoint_number = num;
4fa62494 202 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
c906108c
SS
203}
204
205/* Set externally visible debug variables for querying/printing
c378eb4e 206 the traceframe context (line, function, file). */
c906108c
SS
207
208static void
8480a37e 209set_traceframe_context (const frame_info_ptr &trace_frame)
c906108c 210{
fb14de7b 211 CORE_ADDR trace_pc;
24a00813 212 struct symbol *traceframe_fun;
51abb421 213 symtab_and_line traceframe_sal;
fb14de7b 214
dba09041
PA
215 /* Save as globals for internal use. */
216 if (trace_frame != NULL
217 && get_frame_pc_if_available (trace_frame, &trace_pc))
218 {
219 traceframe_sal = find_pc_line (trace_pc, 0);
220 traceframe_fun = find_pc_function (trace_pc);
221
222 /* Save linenumber as "$trace_line", a debugger variable visible to
223 users. */
224 set_internalvar_integer (lookup_internalvar ("trace_line"),
225 traceframe_sal.line);
226 }
227 else
c906108c 228 {
dba09041 229 traceframe_fun = NULL;
4fa62494 230 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
c906108c
SS
231 }
232
d183932d
MS
233 /* Save func name as "$trace_func", a debugger variable visible to
234 users. */
4fa62494 235 if (traceframe_fun == NULL
987012b8 236 || traceframe_fun->linkage_name () == NULL)
4fa62494 237 clear_internalvar (lookup_internalvar ("trace_func"));
c906108c 238 else
78267919 239 set_internalvar_string (lookup_internalvar ("trace_func"),
987012b8 240 traceframe_fun->linkage_name ());
c906108c 241
d183932d
MS
242 /* Save file name as "$trace_file", a debugger variable visible to
243 users. */
4e04028d 244 if (traceframe_sal.symtab == NULL)
4fa62494 245 clear_internalvar (lookup_internalvar ("trace_file"));
c906108c 246 else
78267919 247 set_internalvar_string (lookup_internalvar ("trace_file"),
05cba821 248 symtab_to_filename_for_display (traceframe_sal.symtab));
c906108c
SS
249}
250
f61e138d
SS
251/* Create a new trace state variable with the given name. */
252
253struct trace_state_variable *
254create_trace_state_variable (const char *name)
255{
b36a2634 256 return &tvariables.emplace_back (name, next_tsv_number++);
f61e138d
SS
257}
258
259/* Look for a trace state variable of the given name. */
260
261struct trace_state_variable *
262find_trace_state_variable (const char *name)
263{
c252925c
SM
264 for (trace_state_variable &tsv : tvariables)
265 if (tsv.name == name)
266 return &tsv;
f61e138d
SS
267
268 return NULL;
269}
270
dc673c81
YQ
271/* Look for a trace state variable of the given number. Return NULL if
272 not found. */
273
274struct trace_state_variable *
275find_trace_state_variable_by_number (int number)
276{
c252925c
SM
277 for (trace_state_variable &tsv : tvariables)
278 if (tsv.number == number)
279 return &tsv;
dc673c81
YQ
280
281 return NULL;
282}
283
70221824 284static void
f61e138d
SS
285delete_trace_state_variable (const char *name)
286{
c252925c
SM
287 for (auto it = tvariables.begin (); it != tvariables.end (); it++)
288 if (it->name == name)
f61e138d 289 {
f0dffaff 290 interps_notify_tsv_deleted (&*it);
c252925c 291 tvariables.erase (it);
f61e138d
SS
292 return;
293 }
294
295 warning (_("No trace variable named \"$%s\", not deleting"), name);
296}
297
1773c82c
HAQ
298/* Throws an error if NAME is not valid syntax for a trace state
299 variable's name. */
300
301void
302validate_trace_state_variable_name (const char *name)
303{
304 const char *p;
305
306 if (*name == '\0')
307 error (_("Must supply a non-empty variable name"));
308
309 /* All digits in the name is reserved for value history
310 references. */
311 for (p = name; isdigit (*p); p++)
312 ;
313 if (*p == '\0')
314 error (_("$%s is not a valid trace state variable name"), name);
315
316 for (p = name; isalnum (*p) || *p == '_'; p++)
317 ;
318 if (*p != '\0')
319 error (_("$%s is not a valid trace state variable name"), name);
320}
321
f61e138d
SS
322/* The 'tvariable' command collects a name and optional expression to
323 evaluate into an initial value. */
324
70221824 325static void
0b39b52e 326trace_variable_command (const char *args, int from_tty)
f61e138d 327{
f61e138d
SS
328 LONGEST initval = 0;
329 struct trace_state_variable *tsv;
0b39b52e 330 const char *name_start, *p;
f61e138d
SS
331
332 if (!args || !*args)
1773c82c
HAQ
333 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
334
335 /* Only allow two syntaxes; "$name" and "$name=value". */
336 p = skip_spaces (args);
f61e138d 337
1773c82c
HAQ
338 if (*p++ != '$')
339 error (_("Name of trace variable should start with '$'"));
f61e138d 340
8abcee91 341 name_start = p;
1773c82c
HAQ
342 while (isalnum (*p) || *p == '_')
343 p++;
8abcee91 344 std::string name (name_start, p - name_start);
f61e138d 345
1773c82c
HAQ
346 p = skip_spaces (p);
347 if (*p != '=' && *p != '\0')
f61e138d
SS
348 error (_("Syntax must be $NAME [ = EXPR ]"));
349
8abcee91 350 validate_trace_state_variable_name (name.c_str ());
f61e138d 351
1773c82c
HAQ
352 if (*p == '=')
353 initval = value_as_long (parse_and_eval (++p));
f61e138d
SS
354
355 /* If the variable already exists, just change its initial value. */
8abcee91 356 tsv = find_trace_state_variable (name.c_str ());
f61e138d
SS
357 if (tsv)
358 {
134a2066
YQ
359 if (tsv->initial_value != initval)
360 {
361 tsv->initial_value = initval;
c27ec5c0 362 interps_notify_tsv_modified (tsv);
134a2066 363 }
6cb06a8c
TT
364 gdb_printf (_("Trace state variable $%s "
365 "now has initial value %s.\n"),
366 tsv->name.c_str (), plongest (tsv->initial_value));
f61e138d
SS
367 return;
368 }
369
370 /* Create a new variable. */
8abcee91 371 tsv = create_trace_state_variable (name.c_str ());
f61e138d
SS
372 tsv->initial_value = initval;
373
bf506f27 374 interps_notify_tsv_created (tsv);
bb25a15c 375
6cb06a8c
TT
376 gdb_printf (_("Trace state variable $%s "
377 "created, with initial value %s.\n"),
378 tsv->name.c_str (), plongest (tsv->initial_value));
f61e138d
SS
379}
380
70221824 381static void
2983f7cb 382delete_trace_variable_command (const char *args, int from_tty)
f61e138d 383{
f61e138d
SS
384 if (args == NULL)
385 {
386 if (query (_("Delete all trace state variables? ")))
c252925c 387 tvariables.clear ();
f61e138d 388 dont_repeat ();
f0dffaff 389 interps_notify_tsv_deleted (nullptr);
f61e138d
SS
390 return;
391 }
392
773a1edc 393 gdb_argv argv (args);
f61e138d 394
773a1edc 395 for (char *arg : argv)
f61e138d 396 {
773a1edc
TT
397 if (*arg == '$')
398 delete_trace_state_variable (arg + 1);
f61e138d 399 else
773a1edc 400 warning (_("Name \"%s\" not prefixed with '$', ignoring"), arg);
f61e138d
SS
401 }
402
f61e138d
SS
403 dont_repeat ();
404}
405
40e1c229
VP
406void
407tvariables_info_1 (void)
f61e138d 408{
79a45e25 409 struct ui_out *uiout = current_uiout;
f61e138d 410
35b1e5cc 411 /* Try to acquire values from the target. */
c252925c
SM
412 for (trace_state_variable &tsv : tvariables)
413 tsv.value_known
414 = target_get_trace_state_variable_value (tsv.number, &tsv.value);
35b1e5cc 415
f3c6abab
TT
416 {
417 ui_out_emit_table table_emitter (uiout, 3, tvariables.size (),
418 "trace-variables");
419 uiout->table_header (15, ui_left, "name", "Name");
420 uiout->table_header (11, ui_left, "initial", "Initial");
421 uiout->table_header (11, ui_left, "current", "Current");
40e1c229 422
f3c6abab 423 uiout->table_body ();
f61e138d 424
f3c6abab
TT
425 for (const trace_state_variable &tsv : tvariables)
426 {
427 const char *c;
428
429 ui_out_emit_tuple tuple_emitter (uiout, "variable");
430
431 uiout->field_string ("name", std::string ("$") + tsv.name);
432 uiout->field_string ("initial", plongest (tsv.initial_value));
433
7f6aba03 434 ui_file_style style;
f3c6abab
TT
435 if (tsv.value_known)
436 c = plongest (tsv.value);
437 else if (uiout->is_mi_like_p ())
438 /* For MI, we prefer not to use magic string constants, but rather
439 omit the field completely. The difference between unknown and
440 undefined does not seem important enough to represent. */
441 c = NULL;
442 else if (current_trace_status ()->running || traceframe_number >= 0)
7f6aba03
TT
443 {
444 /* The value is/was defined, but we don't have it. */
445 c = "<unknown>";
446 style = metadata_style.style ();
447 }
f3c6abab 448 else
7f6aba03
TT
449 {
450 /* It is not meaningful to ask about the value. */
451 c = "<undefined>";
452 style = metadata_style.style ();
453 }
f3c6abab 454 if (c)
7f6aba03 455 uiout->field_string ("current", c, style);
f3c6abab
TT
456 uiout->text ("\n");
457 }
458 }
459
460 if (tvariables.empty ())
461 uiout->text (_("No trace state variables.\n"));
40e1c229
VP
462}
463
464/* List all the trace state variables. */
465
466static void
1d12d88f 467info_tvariables_command (const char *args, int from_tty)
40e1c229
VP
468{
469 tvariables_info_1 ();
f61e138d
SS
470}
471
8bf6485c
SS
472/* Stash definitions of tsvs into the given file. */
473
474void
475save_trace_state_variables (struct ui_file *fp)
476{
c252925c 477 for (const trace_state_variable &tsv : tvariables)
8bf6485c 478 {
6cb06a8c 479 gdb_printf (fp, "tvariable $%s", tsv.name.c_str ());
c252925c 480 if (tsv.initial_value)
6cb06a8c
TT
481 gdb_printf (fp, " = %s", plongest (tsv.initial_value));
482 gdb_printf (fp, "\n");
8bf6485c
SS
483 }
484}
485
c906108c
SS
486/* ACTIONS functions: */
487
c906108c 488/* The three functions:
c5aa993b
JM
489 collect_pseudocommand,
490 while_stepping_pseudocommand, and
491 end_actions_pseudocommand
c906108c
SS
492 are placeholders for "commands" that are actually ONLY to be used
493 within a tracepoint action list. If the actual function is ever called,
494 it means that somebody issued the "command" at the top level,
495 which is always an error. */
496
7b3ae3a6 497static void
0b39b52e 498end_actions_pseudocommand (const char *args, int from_tty)
c906108c 499{
8a3fe4f8 500 error (_("This command cannot be used at the top level."));
c906108c
SS
501}
502
7b3ae3a6 503static void
0b39b52e 504while_stepping_pseudocommand (const char *args, int from_tty)
c906108c 505{
8a3fe4f8 506 error (_("This command can only be used in a tracepoint actions list."));
c906108c
SS
507}
508
509static void
0b39b52e 510collect_pseudocommand (const char *args, int from_tty)
c906108c 511{
8a3fe4f8 512 error (_("This command can only be used in a tracepoint actions list."));
c906108c
SS
513}
514
6da95a67 515static void
0b39b52e 516teval_pseudocommand (const char *args, int from_tty)
6da95a67
SS
517{
518 error (_("This command can only be used in a tracepoint actions list."));
519}
520
3065dfb6
SS
521/* Parse any collection options, such as /s for strings. */
522
6f937416 523const char *
92bc6a20 524decode_agent_options (const char *exp, int *trace_string)
3065dfb6
SS
525{
526 struct value_print_options opts;
527
92bc6a20
TT
528 *trace_string = 0;
529
3065dfb6
SS
530 if (*exp != '/')
531 return exp;
532
533 /* Call this to borrow the print elements default for collection
534 size. */
535 get_user_print_options (&opts);
536
537 exp++;
538 if (*exp == 's')
539 {
540 if (target_supports_string_tracing ())
541 {
542 /* Allow an optional decimal number giving an explicit maximum
76b58849 543 string length, defaulting it to the "print characters" value;
3065dfb6 544 so "collect/s80 mystr" gets at most 80 bytes of string. */
76b58849 545 *trace_string = get_print_max_chars (&opts);
3065dfb6
SS
546 exp++;
547 if (*exp >= '0' && *exp <= '9')
92bc6a20 548 *trace_string = atoi (exp);
3065dfb6
SS
549 while (*exp >= '0' && *exp <= '9')
550 exp++;
551 }
552 else
553 error (_("Target does not support \"/s\" option for string tracing."));
554 }
555 else
556 error (_("Undefined collection format \"%c\"."), *exp);
557
f1735a53 558 exp = skip_spaces (exp);
3065dfb6
SS
559
560 return exp;
561}
562
c906108c
SS
563/* Enter a list of actions for a tracepoint. */
564static void
0b39b52e 565actions_command (const char *args, int from_tty)
c906108c 566{
d9b3f62e 567 struct tracepoint *t;
c906108c 568
5fa1d40e 569 t = get_tracepoint_by_number (&args, NULL);
7a292a7a 570 if (t)
c906108c 571 {
93921405
TT
572 std::string tmpbuf =
573 string_printf ("Enter actions for tracepoint %d, one per line.",
c1fc2657 574 t->number);
93921405 575
295dc222
TT
576 counted_command_line l = read_command_lines (tmpbuf.c_str (),
577 from_tty, 1,
60b3cef2
TT
578 [=] (const char *line)
579 {
580 validate_actionline (line, t);
581 });
c1fc2657 582 breakpoint_set_commands (t, std::move (l));
c906108c 583 }
5c44784c 584 /* else just return */
c906108c
SS
585}
586
fff87407
SS
587/* Report the results of checking the agent expression, as errors or
588 internal errors. */
589
590static void
35c9c7ba 591report_agent_reqs_errors (struct agent_expr *aexpr)
fff87407
SS
592{
593 /* All of the "flaws" are serious bytecode generation issues that
594 should never occur. */
35c9c7ba 595 if (aexpr->flaw != agent_flaw_none)
f34652de 596 internal_error (_("expression is malformed"));
fff87407
SS
597
598 /* If analysis shows a stack underflow, GDB must have done something
599 badly wrong in its bytecode generation. */
35c9c7ba 600 if (aexpr->min_height < 0)
f34652de 601 internal_error (_("expression has min height < 0"));
fff87407
SS
602
603 /* Issue this error if the stack is predicted to get too deep. The
604 limit is rather arbitrary; a better scheme might be for the
605 target to report how much stack it will have available. The
606 depth roughly corresponds to parenthesization, so a limit of 20
607 amounts to 20 levels of expression nesting, which is actually
608 a pretty big hairy expression. */
35c9c7ba 609 if (aexpr->max_height > 20)
fff87407
SS
610 error (_("Expression is too complicated."));
611}
612
4277c4b8
PFC
613/* Call ax_reqs on AEXPR and raise an error if something is wrong. */
614
615static void
616finalize_tracepoint_aexpr (struct agent_expr *aexpr)
617{
618 ax_reqs (aexpr);
619
6f96f485 620 if (aexpr->buf.size () > MAX_AGENT_EXPR_LEN)
4277c4b8
PFC
621 error (_("Expression is too complicated."));
622
623 report_agent_reqs_errors (aexpr);
624}
625
c906108c 626/* worker function */
fff87407 627void
01bccc56 628validate_actionline (const char *line, tracepoint *t)
c906108c
SS
629{
630 struct cmd_list_element *c;
6f937416
PA
631 const char *tmp_p;
632 const char *p;
c906108c 633
c378eb4e 634 /* If EOF is typed, *line is NULL. */
6f937416 635 if (line == NULL)
fff87407 636 return;
15255275 637
f1735a53 638 p = skip_spaces (line);
c906108c 639
d183932d
MS
640 /* Symbol lookup etc. */
641 if (*p == '\0') /* empty line: just prompt for another line. */
fff87407 642 return;
c906108c 643
c5aa993b 644 if (*p == '#') /* comment line */
fff87407 645 return;
c906108c 646
cf00cd6f 647 c = lookup_cmd (&p, cmdlist, "", NULL, -1, 1);
c906108c 648 if (c == 0)
fff87407 649 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
c5aa993b 650
3a553c80 651 if (cmd_simple_func_eq (c, collect_pseudocommand))
c906108c 652 {
92bc6a20
TT
653 int trace_string = 0;
654
3065dfb6 655 if (*p == '/')
92bc6a20 656 p = decode_agent_options (p, &trace_string);
3065dfb6 657
c5aa993b 658 do
c378eb4e
MS
659 { /* Repeat over a comma-separated list. */
660 QUIT; /* Allow user to bail out with ^C. */
f1735a53 661 p = skip_spaces (p);
c906108c 662
c378eb4e 663 if (*p == '$') /* Look for special pseudo-symbols. */
c5aa993b 664 {
0fb4aa4b
PA
665 if (0 == strncasecmp ("reg", p + 1, 3)
666 || 0 == strncasecmp ("arg", p + 1, 3)
667 || 0 == strncasecmp ("loc", p + 1, 3)
6710bf39 668 || 0 == strncasecmp ("_ret", p + 1, 4)
0fb4aa4b 669 || 0 == strncasecmp ("_sdata", p + 1, 6))
c5aa993b
JM
670 {
671 p = strchr (p, ',');
672 continue;
673 }
d183932d 674 /* else fall thru, treat p as an expression and parse it! */
c5aa993b 675 }
9355b391 676 tmp_p = p;
b00b30b2 677 for (bp_location &loc : t->locations ())
c5aa993b 678 {
6f937416 679 p = tmp_p;
b00b30b2
SM
680 expression_up exp = parse_exp_1 (&p, loc.address,
681 block_for_pc (loc.address),
b8c03634 682 PARSER_COMMA_TERMINATES);
9355b391 683
2adab65c 684 if (exp->first_opcode () == OP_VAR_VALUE)
c5aa993b 685 {
413403fc 686 symbol *sym;
1eaebe02 687 expr::var_value_operation *vvop
5f48d886 688 = (gdb::checked_static_cast<expr::var_value_operation *>
1eaebe02
TT
689 (exp->op.get ()));
690 sym = vvop->get_symbol ();
413403fc 691
66d7f48f 692 if (sym->aclass () == LOC_CONST)
9355b391 693 {
12df843f 694 error (_("constant `%s' (value %s) "
3e43a32a 695 "will not be collected."),
413403fc 696 sym->print_name (),
4aeddc50 697 plongest (sym->value_longest ()));
9355b391 698 }
66d7f48f 699 else if (sym->aclass () == LOC_OPTIMIZED_OUT)
9355b391 700 {
3e43a32a
MS
701 error (_("`%s' is optimized away "
702 "and cannot be collected."),
413403fc 703 sym->print_name ());
9355b391 704 }
c5aa993b 705 }
c906108c 706
9355b391
SS
707 /* We have something to collect, make sure that the expr to
708 bytecode translator can handle it and that it's not too
709 long. */
b00b30b2 710 agent_expr_up aexpr = gen_trace_for_expr (loc.address,
833177a4
PA
711 exp.get (),
712 trace_string);
c906108c 713
4277c4b8 714 finalize_tracepoint_aexpr (aexpr.get ());
9355b391 715 }
c5aa993b
JM
716 }
717 while (p && *p++ == ',');
c906108c 718 }
fff87407 719
3a553c80 720 else if (cmd_simple_func_eq (c, teval_pseudocommand))
6da95a67 721 {
6da95a67 722 do
c378eb4e
MS
723 { /* Repeat over a comma-separated list. */
724 QUIT; /* Allow user to bail out with ^C. */
f1735a53 725 p = skip_spaces (p);
6da95a67 726
9355b391 727 tmp_p = p;
b00b30b2 728 for (bp_location &loc : t->locations ())
9355b391 729 {
6f937416 730 p = tmp_p;
bbc13ae3 731
9355b391 732 /* Only expressions are allowed for this action. */
b00b30b2
SM
733 expression_up exp = parse_exp_1 (&p, loc.address,
734 block_for_pc (loc.address),
b8c03634 735 PARSER_COMMA_TERMINATES);
6da95a67 736
9355b391
SS
737 /* We have something to evaluate, make sure that the expr to
738 bytecode translator can handle it and that it's not too
739 long. */
b00b30b2 740 agent_expr_up aexpr = gen_eval_for_expr (loc.address, exp.get ());
6da95a67 741
4277c4b8 742 finalize_tracepoint_aexpr (aexpr.get ());
9355b391 743 }
6da95a67
SS
744 }
745 while (p && *p++ == ',');
6da95a67 746 }
fff87407 747
3a553c80 748 else if (cmd_simple_func_eq (c, while_stepping_pseudocommand))
c906108c 749 {
6f937416 750 char *endp;
c906108c 751
f1735a53 752 p = skip_spaces (p);
6f937416
PA
753 t->step_count = strtol (p, &endp, 0);
754 if (endp == p || t->step_count == 0)
755 error (_("while-stepping step count `%s' is malformed."), line);
756 p = endp;
c906108c 757 }
fff87407 758
3a553c80 759 else if (cmd_simple_func_eq (c, end_actions_pseudocommand))
fff87407
SS
760 ;
761
c906108c 762 else
6f937416 763 error (_("`%s' is not a supported tracepoint action."), line);
74b7792f
AC
764}
765
f50e79a4
JB
766enum {
767 memrange_absolute = -1
768};
769
c906108c
SS
770/* MEMRANGE functions: */
771
1f45808e 772/* Compare memranges for std::sort. */
c906108c 773
1f45808e
PA
774static bool
775memrange_comp (const memrange &a, const memrange &b)
c906108c 776{
1f45808e 777 if (a.type == b.type)
c906108c 778 {
1f45808e
PA
779 if (a.type == memrange_absolute)
780 return (bfd_vma) a.start < (bfd_vma) b.start;
781 else
782 return a.start < b.start;
c906108c 783 }
1f45808e
PA
784
785 return a.type < b.type;
c906108c
SS
786}
787
1f45808e
PA
788/* Sort the memrange list using std::sort, and merge adjacent memranges. */
789
c906108c 790static void
1f45808e 791memrange_sortmerge (std::vector<memrange> &memranges)
c906108c 792{
1f45808e 793 if (!memranges.empty ())
c906108c 794 {
1f45808e
PA
795 int a, b;
796
797 std::sort (memranges.begin (), memranges.end (), memrange_comp);
798
799 for (a = 0, b = 1; b < memranges.size (); b++)
c906108c 800 {
1b28d0b3
PA
801 /* If memrange b overlaps or is adjacent to memrange a,
802 merge them. */
1f45808e
PA
803 if (memranges[a].type == memranges[b].type
804 && memranges[b].start <= memranges[a].end)
c906108c 805 {
1f45808e
PA
806 if (memranges[b].end > memranges[a].end)
807 memranges[a].end = memranges[b].end;
c906108c
SS
808 continue; /* next b, same a */
809 }
810 a++; /* next a */
811 if (a != b)
1f45808e 812 memranges[a] = memranges[b];
c906108c 813 }
1f45808e 814 memranges.resize (a + 1);
c906108c
SS
815 }
816}
817
4277c4b8 818/* Add remote register number REGNO to the collection list mask. */
1f45808e
PA
819
820void
4277c4b8 821collection_list::add_remote_register (unsigned int regno)
c906108c
SS
822{
823 if (info_verbose)
6cb06a8c 824 gdb_printf ("collect register %d\n", regno);
a04b9d62
PFC
825
826 m_regs_mask.at (regno / 8) |= 1 << (regno % 8);
c906108c
SS
827}
828
4277c4b8
PFC
829/* Add all the registers from the mask in AEXPR to the mask in the
830 collection list. Registers in the AEXPR mask are already remote
831 register numbers. */
832
833void
834collection_list::add_ax_registers (struct agent_expr *aexpr)
835{
a2bbca9f 836 for (int ndx1 = 0; ndx1 < aexpr->reg_mask.size (); ndx1++)
4277c4b8 837 {
a2bbca9f
TT
838 QUIT; /* Allow user to bail out with ^C. */
839 if (aexpr->reg_mask[ndx1])
4277c4b8 840 {
a2bbca9f
TT
841 /* It's used -- record it. */
842 add_remote_register (ndx1);
4277c4b8
PFC
843 }
844 }
845}
846
847/* If REGNO is raw, add its corresponding remote register number to
848 the mask. If REGNO is a pseudo-register, figure out the necessary
849 registers using a temporary agent expression, and add it to the
850 list if it needs more than just a mask. */
851
852void
853collection_list::add_local_register (struct gdbarch *gdbarch,
854 unsigned int regno,
855 CORE_ADDR scope)
856{
857 if (regno < gdbarch_num_regs (gdbarch))
858 {
859 int remote_regno = gdbarch_remote_register_number (gdbarch, regno);
860
861 if (remote_regno < 0)
862 error (_("Can't collect register %d"), regno);
863
864 add_remote_register (remote_regno);
865 }
866 else
867 {
868 agent_expr_up aexpr (new agent_expr (gdbarch, scope));
869
870 ax_reg_mask (aexpr.get (), regno);
871
872 finalize_tracepoint_aexpr (aexpr.get ());
873
874 add_ax_registers (aexpr.get ());
875
876 /* Usually ax_reg_mask for a pseudo-regiser only sets the
877 corresponding raw registers in the ax mask, but if this isn't
878 the case add the expression that is generated to the
879 collection list. */
6f96f485 880 if (aexpr->buf.size () > 0)
4277c4b8
PFC
881 add_aexpr (std::move (aexpr));
882 }
883}
884
c378eb4e 885/* Add a memrange to a collection list. */
1f45808e
PA
886
887void
19f1935d
PA
888collection_list::add_memrange (struct gdbarch *gdbarch,
889 int type, bfd_signed_vma base,
4277c4b8 890 unsigned long len, CORE_ADDR scope)
c906108c
SS
891{
892 if (info_verbose)
6cb06a8c 893 gdb_printf ("(%d,%s,%ld)\n", type, paddress (gdbarch, base), len);
104c1213 894
f50e79a4 895 /* type: memrange_absolute == memory, other n == basereg */
d183932d 896 /* base: addr if memory, offset if reg relative. */
c906108c 897 /* len: we actually save end (base + len) for convenience */
7a63494a 898 m_memranges.emplace_back (type, base, base + len);
c906108c 899
3e43a32a 900 if (type != memrange_absolute) /* Better collect the base register! */
4277c4b8 901 add_local_register (gdbarch, type, scope);
c906108c
SS
902}
903
d183932d 904/* Add a symbol to a collection list. */
1f45808e
PA
905
906void
907collection_list::collect_symbol (struct symbol *sym,
908 struct gdbarch *gdbarch,
909 long frame_regno, long frame_offset,
910 CORE_ADDR scope,
911 int trace_string)
c906108c 912{
c5aa993b 913 unsigned long len;
104c1213 914 unsigned int reg;
c906108c 915 bfd_signed_vma offset;
400c6af0 916 int treat_as_expr = 0;
c906108c 917
df86565b 918 len = check_typedef (sym->type ())->length ();
66d7f48f 919 switch (sym->aclass ())
c5aa993b
JM
920 {
921 default:
6cb06a8c
TT
922 gdb_printf ("%s: don't know symbol class %d\n",
923 sym->print_name (), sym->aclass ());
c5aa993b
JM
924 break;
925 case LOC_CONST:
6cb06a8c 926 gdb_printf ("constant %s (value %s) will not be collected.\n",
4aeddc50 927 sym->print_name (), plongest (sym->value_longest ()));
c5aa993b
JM
928 break;
929 case LOC_STATIC:
4aeddc50 930 offset = sym->value_address ();
c5aa993b 931 if (info_verbose)
104c1213 932 {
6cb06a8c
TT
933 gdb_printf ("LOC_STATIC %s: collect %ld bytes at %s.\n",
934 sym->print_name (), len,
935 paddress (gdbarch, offset));
104c1213 936 }
400c6af0
SS
937 /* A struct may be a C++ class with static fields, go to general
938 expression handling. */
5f9c5a63 939 if (sym->type ()->code () == TYPE_CODE_STRUCT)
400c6af0
SS
940 treat_as_expr = 1;
941 else
4277c4b8 942 add_memrange (gdbarch, memrange_absolute, offset, len, scope);
c5aa993b
JM
943 break;
944 case LOC_REGISTER:
7ae24327 945 reg = sym->register_ops ()->register_number (sym, gdbarch);
c5aa993b 946 if (info_verbose)
6cb06a8c 947 gdb_printf ("LOC_REG[parm] %s: ", sym->print_name ());
4277c4b8 948 add_local_register (gdbarch, reg, scope);
d183932d
MS
949 /* Check for doubles stored in two registers. */
950 /* FIXME: how about larger types stored in 3 or more regs? */
5f9c5a63 951 if (sym->type ()->code () == TYPE_CODE_FLT &&
a6d9a66e 952 len > register_size (gdbarch, reg))
4277c4b8 953 add_local_register (gdbarch, reg + 1, scope);
c5aa993b
JM
954 break;
955 case LOC_REF_ARG:
6cb06a8c
TT
956 gdb_printf ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
957 gdb_printf (" (will not collect %s)\n", sym->print_name ());
c5aa993b
JM
958 break;
959 case LOC_ARG:
960 reg = frame_regno;
4aeddc50 961 offset = frame_offset + sym->value_longest ();
c5aa993b
JM
962 if (info_verbose)
963 {
6cb06a8c
TT
964 gdb_printf ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
965 " from frame ptr reg %d\n", sym->print_name (), len,
966 paddress (gdbarch, offset), reg);
c5aa993b 967 }
4277c4b8 968 add_memrange (gdbarch, reg, offset, len, scope);
c5aa993b
JM
969 break;
970 case LOC_REGPARM_ADDR:
4aeddc50 971 reg = sym->value_longest ();
c5aa993b
JM
972 offset = 0;
973 if (info_verbose)
974 {
6cb06a8c
TT
975 gdb_printf ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset %s"
976 " from reg %d\n", sym->print_name (), len,
977 paddress (gdbarch, offset), reg);
c5aa993b 978 }
4277c4b8 979 add_memrange (gdbarch, reg, offset, len, scope);
c5aa993b
JM
980 break;
981 case LOC_LOCAL:
c5aa993b 982 reg = frame_regno;
4aeddc50 983 offset = frame_offset + sym->value_longest ();
c5aa993b
JM
984 if (info_verbose)
985 {
6cb06a8c
TT
986 gdb_printf ("LOC_LOCAL %s: Collect %ld bytes at offset %s"
987 " from frame ptr reg %d\n", sym->print_name (), len,
988 paddress (gdbarch, offset), reg);
c5aa993b 989 }
4277c4b8 990 add_memrange (gdbarch, reg, offset, len, scope);
c5aa993b 991 break;
a0405854 992
c5aa993b 993 case LOC_UNRESOLVED:
a0405854 994 treat_as_expr = 1;
c5aa993b 995 break;
a0405854 996
c5aa993b 997 case LOC_OPTIMIZED_OUT:
6cb06a8c
TT
998 gdb_printf ("%s has been optimized out of existence.\n",
999 sym->print_name ());
c5aa993b 1000 break;
0936ad1d
SS
1001
1002 case LOC_COMPUTED:
400c6af0 1003 treat_as_expr = 1;
0936ad1d 1004 break;
c5aa993b 1005 }
400c6af0
SS
1006
1007 /* Expressions are the most general case. */
1008 if (treat_as_expr)
1009 {
833177a4
PA
1010 agent_expr_up aexpr = gen_trace_for_var (scope, gdbarch,
1011 sym, trace_string);
400c6af0
SS
1012
1013 /* It can happen that the symbol is recorded as a computed
1014 location, but it's been optimized away and doesn't actually
1015 have a location expression. */
1016 if (!aexpr)
1017 {
6cb06a8c
TT
1018 gdb_printf ("%s has been optimized out of existence.\n",
1019 sym->print_name ());
400c6af0
SS
1020 return;
1021 }
1022
4277c4b8 1023 finalize_tracepoint_aexpr (aexpr.get ());
400c6af0 1024
c378eb4e 1025 /* Take care of the registers. */
4277c4b8 1026 add_ax_registers (aexpr.get ());
833177a4 1027
6c73cd95 1028 add_aexpr (std::move (aexpr));
400c6af0 1029 }
c906108c
SS
1030}
1031
1f45808e
PA
1032void
1033collection_list::add_wholly_collected (const char *print_name)
1034{
1035 m_wholly_collected.push_back (print_name);
2c58c0a9
PA
1036}
1037
c378eb4e 1038/* Add all locals (or args) symbols to collection list. */
1f45808e
PA
1039
1040void
1041collection_list::add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc,
1042 long frame_regno, long frame_offset, int type,
1043 int trace_string)
c906108c 1044{
3977b71f 1045 const struct block *block;
13835d88 1046 int count = 0;
2c58c0a9 1047
13835d88
TT
1048 auto do_collect_symbol = [&] (const char *print_name,
1049 struct symbol *sym)
1050 {
1051 collect_symbol (sym, gdbarch, frame_regno,
1052 frame_offset, pc, trace_string);
1053 count++;
1054 add_wholly_collected (print_name);
1055 };
c906108c 1056
2c58c0a9 1057 if (type == 'L')
c906108c 1058 {
2c58c0a9
PA
1059 block = block_for_pc (pc);
1060 if (block == NULL)
c906108c 1061 {
2c58c0a9
PA
1062 warning (_("Can't collect locals; "
1063 "no symbol table info available.\n"));
1064 return;
c906108c 1065 }
2c58c0a9 1066
13835d88
TT
1067 iterate_over_block_local_vars (block, do_collect_symbol);
1068 if (count == 0)
2c58c0a9
PA
1069 warning (_("No locals found in scope."));
1070 }
1071 else
1072 {
8f50b4b5
TT
1073 CORE_ADDR fn_pc = get_pc_function_start (pc);
1074 block = block_for_pc (fn_pc);
2c58c0a9
PA
1075 if (block == NULL)
1076 {
b37520b6 1077 warning (_("Can't collect args; no symbol table info available."));
2c58c0a9
PA
1078 return;
1079 }
1080
13835d88
TT
1081 iterate_over_block_arg_vars (block, do_collect_symbol);
1082 if (count == 0)
2c58c0a9 1083 warning (_("No args found in scope."));
c906108c 1084 }
c906108c
SS
1085}
1086
1f45808e
PA
1087void
1088collection_list::add_static_trace_data ()
0fb4aa4b
PA
1089{
1090 if (info_verbose)
6cb06a8c 1091 gdb_printf ("collect static trace data\n");
1f45808e 1092 m_strace_data = true;
cbfa3b61
YQ
1093}
1094
1f45808e 1095collection_list::collection_list ()
a04b9d62 1096 : m_strace_data (false)
cbfa3b61 1097{
a04b9d62 1098 int max_remote_regno = 0;
99d9c3b9 1099 for (int i = 0; i < gdbarch_num_regs (current_inferior ()->arch ()); i++)
a04b9d62
PFC
1100 {
1101 int remote_regno = (gdbarch_remote_register_number
99d9c3b9 1102 (current_inferior ()->arch (), i));
a04b9d62
PFC
1103
1104 if (remote_regno >= 0 && remote_regno > max_remote_regno)
1105 max_remote_regno = remote_regno;
1106 }
1107
1108 m_regs_mask.resize ((max_remote_regno / 8) + 1);
1109
1f45808e
PA
1110 m_memranges.reserve (128);
1111 m_aexprs.reserve (128);
cbfa3b61
YQ
1112}
1113
c378eb4e 1114/* Reduce a collection list to string form (for gdb protocol). */
1f45808e 1115
b44ec619 1116std::vector<std::string>
1f45808e 1117collection_list::stringify ()
c906108c 1118{
a04b9d62
PFC
1119 gdb::char_vector temp_buf (2048);
1120
c906108c 1121 int count;
c906108c 1122 char *end;
c5aa993b 1123 long i;
b44ec619 1124 std::vector<std::string> str_list;
c906108c 1125
1f45808e 1126 if (m_strace_data)
0fb4aa4b
PA
1127 {
1128 if (info_verbose)
6cb06a8c 1129 gdb_printf ("\nCollecting static trace data\n");
a04b9d62 1130 end = temp_buf.data ();
0fb4aa4b 1131 *end++ = 'L';
a04b9d62 1132 str_list.emplace_back (temp_buf.data (), end - temp_buf.data ());
0fb4aa4b
PA
1133 }
1134
a04b9d62 1135 for (i = m_regs_mask.size () - 1; i > 0; i--)
1f45808e 1136 if (m_regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
c906108c 1137 break;
1f45808e 1138 if (m_regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
c906108c
SS
1139 {
1140 if (info_verbose)
6cb06a8c 1141 gdb_printf ("\nCollecting registers (mask): 0x");
a04b9d62
PFC
1142
1143 /* One char for 'R', one for the null terminator and two per
1144 mask byte. */
1145 std::size_t new_size = (i + 1) * 2 + 2;
1146 if (new_size > temp_buf.size ())
1147 temp_buf.resize (new_size);
1148
1149 end = temp_buf.data ();
c5aa993b 1150 *end++ = 'R';
c906108c
SS
1151 for (; i >= 0; i--)
1152 {
c378eb4e 1153 QUIT; /* Allow user to bail out with ^C. */
c906108c 1154 if (info_verbose)
6cb06a8c 1155 gdb_printf ("%02X", m_regs_mask[i]);
a04b9d62
PFC
1156
1157 end = pack_hex_byte (end, m_regs_mask[i]);
c906108c 1158 }
a04b9d62
PFC
1159 *end = '\0';
1160
1161 str_list.emplace_back (temp_buf.data ());
c906108c
SS
1162 }
1163 if (info_verbose)
6cb06a8c 1164 gdb_printf ("\n");
1f45808e 1165 if (!m_memranges.empty () && info_verbose)
6cb06a8c 1166 gdb_printf ("Collecting memranges: \n");
a04b9d62
PFC
1167 for (i = 0, count = 0, end = temp_buf.data ();
1168 i < m_memranges.size (); i++)
c906108c 1169 {
c378eb4e 1170 QUIT; /* Allow user to bail out with ^C. */
c906108c 1171 if (info_verbose)
104c1213 1172 {
6cb06a8c
TT
1173 gdb_printf ("(%d, %s, %ld)\n",
1174 m_memranges[i].type,
99d9c3b9 1175 paddress (current_inferior ()->arch (),
6cb06a8c
TT
1176 m_memranges[i].start),
1177 (long) (m_memranges[i].end
1178 - m_memranges[i].start));
104c1213 1179 }
c906108c
SS
1180 if (count + 27 > MAX_AGENT_EXPR_LEN)
1181 {
a04b9d62 1182 str_list.emplace_back (temp_buf.data (), count);
c906108c 1183 count = 0;
a04b9d62 1184 end = temp_buf.data ();
c906108c 1185 }
104c1213 1186
d1948716 1187 {
dda83cd7 1188 bfd_signed_vma length
1f45808e 1189 = m_memranges[i].end - m_memranges[i].start;
d1948716 1190
dda83cd7
SM
1191 /* The "%X" conversion specifier expects an unsigned argument,
1192 so passing -1 (memrange_absolute) to it directly gives you
1193 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1194 Special-case it. */
1195 if (m_memranges[i].type == memrange_absolute)
1196 sprintf (end, "M-1,%s,%lX", phex_nz (m_memranges[i].start, 0),
19f1935d 1197 (long) length);
dda83cd7
SM
1198 else
1199 sprintf (end, "M%X,%s,%lX", m_memranges[i].type,
19f1935d 1200 phex_nz (m_memranges[i].start, 0), (long) length);
d1948716 1201 }
104c1213 1202
c906108c 1203 count += strlen (end);
a04b9d62 1204 end = temp_buf.data () + count;
c906108c
SS
1205 }
1206
1f45808e 1207 for (i = 0; i < m_aexprs.size (); i++)
c906108c 1208 {
c378eb4e 1209 QUIT; /* Allow user to bail out with ^C. */
6f96f485 1210 if ((count + 10 + 2 * m_aexprs[i]->buf.size ()) > MAX_AGENT_EXPR_LEN)
c906108c 1211 {
a04b9d62 1212 str_list.emplace_back (temp_buf.data (), count);
c906108c 1213 count = 0;
a04b9d62 1214 end = temp_buf.data ();
c906108c 1215 }
6f96f485 1216 sprintf (end, "X%08X,", (int) m_aexprs[i]->buf.size ());
c906108c
SS
1217 end += 10; /* 'X' + 8 hex digits + ',' */
1218 count += 10;
1219
6f96f485
TT
1220 end += 2 * bin2hex (m_aexprs[i]->buf.data (), end,
1221 m_aexprs[i]->buf.size ());
1222 count += 2 * m_aexprs[i]->buf.size ();
c906108c
SS
1223 }
1224
1225 if (count != 0)
1226 {
a04b9d62 1227 str_list.emplace_back (temp_buf.data (), count);
c906108c 1228 count = 0;
a04b9d62 1229 end = temp_buf.data ();
c906108c 1230 }
c906108c 1231
b44ec619 1232 return str_list;
c906108c
SS
1233}
1234
bc167b6b 1235/* Add the expression STR to M_COMPUTED. */
dc673c81 1236
1f45808e 1237void
bc167b6b 1238collection_list::append_exp (std::string &&str)
dc673c81 1239{
bc167b6b 1240 m_computed.push_back (std::move (str));
dc673c81 1241}
a7bdde9e 1242
1f45808e
PA
1243void
1244collection_list::finish ()
1245{
1246 memrange_sortmerge (m_memranges);
1247}
1248
a7bdde9e
VP
1249static void
1250encode_actions_1 (struct command_line *action,
a7bdde9e
VP
1251 struct bp_location *tloc,
1252 int frame_reg,
1253 LONGEST frame_offset,
1254 struct collection_list *collect,
1255 struct collection_list *stepping_list)
c906108c 1256{
6f937416 1257 const char *action_exp;
104c1213 1258 int i;
f976f6d4 1259 struct value *tempval;
c906108c 1260 struct cmd_list_element *cmd;
236f1d4d
SS
1261
1262 for (; action; action = action->next)
c906108c 1263 {
c378eb4e 1264 QUIT; /* Allow user to bail out with ^C. */
a7bdde9e 1265 action_exp = action->line;
f1735a53 1266 action_exp = skip_spaces (action_exp);
c906108c 1267
cf00cd6f 1268 cmd = lookup_cmd (&action_exp, cmdlist, "", NULL, -1, 1);
c906108c 1269 if (cmd == 0)
8a3fe4f8 1270 error (_("Bad action list item: %s"), action_exp);
c906108c 1271
3a553c80 1272 if (cmd_simple_func_eq (cmd, collect_pseudocommand))
c906108c 1273 {
92bc6a20
TT
1274 int trace_string = 0;
1275
3065dfb6 1276 if (*action_exp == '/')
92bc6a20 1277 action_exp = decode_agent_options (action_exp, &trace_string);
3065dfb6 1278
c5aa993b 1279 do
c378eb4e
MS
1280 { /* Repeat over a comma-separated list. */
1281 QUIT; /* Allow user to bail out with ^C. */
f1735a53 1282 action_exp = skip_spaces (action_exp);
99d9c3b9 1283 gdbarch *arch = current_inferior ()->arch ();
c906108c 1284
c5aa993b
JM
1285 if (0 == strncasecmp ("$reg", action_exp, 4))
1286 {
99d9c3b9 1287 for (i = 0; i < gdbarch_num_regs (arch);
4277c4b8
PFC
1288 i++)
1289 {
1290 int remote_regno = (gdbarch_remote_register_number
99d9c3b9 1291 (arch, i));
4277c4b8
PFC
1292
1293 /* Ignore arch regnos without a corresponding
1294 remote regno. This can happen for regnos not
1295 in the tdesc. */
1296 if (remote_regno >= 0)
1297 collect->add_remote_register (remote_regno);
1298 }
c5aa993b
JM
1299 action_exp = strchr (action_exp, ','); /* more? */
1300 }
1301 else if (0 == strncasecmp ("$arg", action_exp, 4))
1302 {
99d9c3b9 1303 collect->add_local_symbols (arch,
1f45808e
PA
1304 tloc->address,
1305 frame_reg,
1306 frame_offset,
1307 'A',
1308 trace_string);
c5aa993b
JM
1309 action_exp = strchr (action_exp, ','); /* more? */
1310 }
1311 else if (0 == strncasecmp ("$loc", action_exp, 4))
1312 {
99d9c3b9 1313 collect->add_local_symbols (arch,
1f45808e
PA
1314 tloc->address,
1315 frame_reg,
1316 frame_offset,
1317 'L',
1318 trace_string);
c5aa993b
JM
1319 action_exp = strchr (action_exp, ','); /* more? */
1320 }
6710bf39
SS
1321 else if (0 == strncasecmp ("$_ret", action_exp, 5))
1322 {
833177a4
PA
1323 agent_expr_up aexpr
1324 = gen_trace_for_return_address (tloc->address,
99d9c3b9 1325 arch, trace_string);
6710bf39 1326
4277c4b8 1327 finalize_tracepoint_aexpr (aexpr.get ());
6710bf39
SS
1328
1329 /* take care of the registers */
4277c4b8 1330 collect->add_ax_registers (aexpr.get ());
6710bf39 1331
6c73cd95 1332 collect->add_aexpr (std::move (aexpr));
6710bf39
SS
1333 action_exp = strchr (action_exp, ','); /* more? */
1334 }
0fb4aa4b
PA
1335 else if (0 == strncasecmp ("$_sdata", action_exp, 7))
1336 {
1f45808e 1337 collect->add_static_trace_data ();
0fb4aa4b
PA
1338 action_exp = strchr (action_exp, ','); /* more? */
1339 }
c5aa993b
JM
1340 else
1341 {
744a8059 1342 unsigned long addr;
c5aa993b 1343
bc167b6b 1344 const char *exp_start = action_exp;
4d01a485
PA
1345 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1346 block_for_pc (tloc->address),
b8c03634 1347 PARSER_COMMA_TERMINATES);
c906108c 1348
2adab65c 1349 switch (exp->first_opcode ())
c5aa993b
JM
1350 {
1351 case OP_REGISTER:
67f3407f 1352 {
1eaebe02 1353 expr::register_operation *regop
5f48d886 1354 = (gdb::checked_static_cast<expr::register_operation *>
1eaebe02
TT
1355 (exp->op.get ()));
1356 const char *name = regop->get_name ();
67f3407f 1357
99d9c3b9 1358 i = user_reg_map_name_to_regnum (arch,
029a67e4 1359 name, strlen (name));
67f3407f 1360 if (i == -1)
f34652de 1361 internal_error (_("Register $%s not available"),
67f3407f
DJ
1362 name);
1363 if (info_verbose)
6cb06a8c 1364 gdb_printf ("OP_REGISTER: ");
99d9c3b9 1365 collect->add_local_register (arch, i, tloc->address);
67f3407f
DJ
1366 break;
1367 }
c5aa993b
JM
1368
1369 case UNOP_MEMVAL:
413403fc
TT
1370 {
1371 /* Safe because we know it's a simple expression. */
43048e46 1372 tempval = exp->evaluate ();
9feb2d07 1373 addr = tempval->address ();
1eaebe02 1374 expr::unop_memval_operation *memop
5f48d886 1375 = (gdb::checked_static_cast<expr::unop_memval_operation *>
1eaebe02
TT
1376 (exp->op.get ()));
1377 struct type *type = memop->get_type ();
413403fc
TT
1378 /* Initialize the TYPE_LENGTH if it is a typedef. */
1379 check_typedef (type);
99d9c3b9 1380 collect->add_memrange (arch,
413403fc 1381 memrange_absolute, addr,
df86565b 1382 type->length (),
413403fc
TT
1383 tloc->address);
1384 collect->append_exp (std::string (exp_start,
1385 action_exp));
1386 }
c5aa993b
JM
1387 break;
1388
1389 case OP_VAR_VALUE:
dc673c81 1390 {
1eaebe02 1391 expr::var_value_operation *vvo
5f48d886 1392 = (gdb::checked_static_cast<expr::var_value_operation *>
1eaebe02
TT
1393 (exp->op.get ()));
1394 struct symbol *sym = vvo->get_symbol ();
987012b8 1395 const char *name = sym->natural_name ();
dc673c81 1396
413403fc 1397 collect->collect_symbol (sym,
99d9c3b9 1398 arch,
1f45808e
PA
1399 frame_reg,
1400 frame_offset,
1401 tloc->address,
1402 trace_string);
1403 collect->add_wholly_collected (name);
dc673c81 1404 }
c5aa993b
JM
1405 break;
1406
c378eb4e 1407 default: /* Full-fledged expression. */
833177a4
PA
1408 agent_expr_up aexpr = gen_trace_for_expr (tloc->address,
1409 exp.get (),
1410 trace_string);
c5aa993b 1411
4277c4b8 1412 finalize_tracepoint_aexpr (aexpr.get ());
c5aa993b 1413
c378eb4e 1414 /* Take care of the registers. */
4277c4b8 1415 collect->add_ax_registers (aexpr.get ());
dc673c81 1416
6c73cd95 1417 collect->add_aexpr (std::move (aexpr));
bc167b6b
TT
1418 collect->append_exp (std::string (exp_start,
1419 action_exp));
c5aa993b
JM
1420 break;
1421 } /* switch */
c5aa993b
JM
1422 } /* do */
1423 }
1424 while (action_exp && *action_exp++ == ',');
1425 } /* if */
3a553c80 1426 else if (cmd_simple_func_eq (cmd, teval_pseudocommand))
6da95a67
SS
1427 {
1428 do
c378eb4e
MS
1429 { /* Repeat over a comma-separated list. */
1430 QUIT; /* Allow user to bail out with ^C. */
f1735a53 1431 action_exp = skip_spaces (action_exp);
6da95a67
SS
1432
1433 {
4d01a485
PA
1434 expression_up exp = parse_exp_1 (&action_exp, tloc->address,
1435 block_for_pc (tloc->address),
b8c03634 1436 PARSER_COMMA_TERMINATES);
6da95a67 1437
833177a4
PA
1438 agent_expr_up aexpr = gen_eval_for_expr (tloc->address,
1439 exp.get ());
6da95a67 1440
4277c4b8 1441 finalize_tracepoint_aexpr (aexpr.get ());
6da95a67 1442
6da95a67
SS
1443 /* Even though we're not officially collecting, add
1444 to the collect list anyway. */
6c73cd95 1445 collect->add_aexpr (std::move (aexpr));
6da95a67
SS
1446 } /* do */
1447 }
1448 while (action_exp && *action_exp++ == ',');
1449 } /* if */
3a553c80 1450 else if (cmd_simple_func_eq (cmd, while_stepping_pseudocommand))
c906108c 1451 {
a7bdde9e
VP
1452 /* We check against nested while-stepping when setting
1453 breakpoint action, so no way to run into nested
1454 here. */
1455 gdb_assert (stepping_list);
1456
12973681 1457 encode_actions_1 (action->body_list_0.get (), tloc, frame_reg,
2a2287c7 1458 frame_offset, stepping_list, NULL);
c906108c 1459 }
a7bdde9e
VP
1460 else
1461 error (_("Invalid tracepoint command '%s'"), action->line);
1462 } /* for */
1463}
1464
dc673c81 1465/* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
1f45808e 1466 and STEPPING_LIST. */
5fbce5df 1467
1f45808e
PA
1468void
1469encode_actions (struct bp_location *tloc,
1470 struct collection_list *tracepoint_list,
1471 struct collection_list *stepping_list)
a7bdde9e 1472{
a7bdde9e
VP
1473 int frame_reg;
1474 LONGEST frame_offset;
cbfa3b61 1475
3e05895e
TT
1476 gdbarch_virtual_frame_pointer (tloc->gdbarch,
1477 tloc->address, &frame_reg, &frame_offset);
a7bdde9e 1478
01bccc56
TT
1479 tracepoint *t = gdb::checked_static_cast<tracepoint *> (tloc->owner);
1480 counted_command_line actions = all_tracepoint_actions (t);
12973681 1481 encode_actions_1 (actions.get (), tloc, frame_reg, frame_offset,
dc673c81 1482 tracepoint_list, stepping_list);
12973681
TT
1483 encode_actions_1 (breakpoint_commands (tloc->owner), tloc,
1484 frame_reg, frame_offset, tracepoint_list, stepping_list);
dc673c81 1485
1f45808e
PA
1486 tracepoint_list->finish ();
1487 stepping_list->finish ();
dc673c81
YQ
1488}
1489
1490/* Render all actions into gdb protocol. */
1491
1492void
b44ec619
SM
1493encode_actions_rsp (struct bp_location *tloc,
1494 std::vector<std::string> *tdp_actions,
1495 std::vector<std::string> *stepping_actions)
dc673c81
YQ
1496{
1497 struct collection_list tracepoint_list, stepping_list;
a7bdde9e 1498
1f45808e 1499 encode_actions (tloc, &tracepoint_list, &stepping_list);
236f1d4d 1500
1f45808e
PA
1501 *tdp_actions = tracepoint_list.stringify ();
1502 *stepping_actions = stepping_list.stringify ();
c906108c
SS
1503}
1504
1f45808e 1505void
833177a4 1506collection_list::add_aexpr (agent_expr_up aexpr)
c906108c 1507{
6c73cd95 1508 m_aexprs.push_back (std::move (aexpr));
c906108c
SS
1509}
1510
bfccc43c
YQ
1511static void
1512process_tracepoint_on_disconnect (void)
1513{
bfccc43c
YQ
1514 int has_pending_p = 0;
1515
1516 /* Check whether we still have pending tracepoint. If we have, warn the
1517 user that pending tracepoint will no longer work. */
a1decfc1 1518 for (breakpoint &b : all_tracepoints ())
bfccc43c 1519 {
a1decfc1 1520 if (!b.has_locations ())
bfccc43c
YQ
1521 {
1522 has_pending_p = 1;
1523 break;
1524 }
1525 else
1526 {
a1decfc1 1527 for (bp_location &loc1 : b.locations ())
bfccc43c 1528 {
b00b30b2 1529 if (loc1.shlib_disabled)
bfccc43c
YQ
1530 {
1531 has_pending_p = 1;
1532 break;
1533 }
1534 }
1535
1536 if (has_pending_p)
1537 break;
1538 }
1539 }
bfccc43c
YQ
1540
1541 if (has_pending_p)
1542 warning (_("Pending tracepoints will not be resolved while"
1543 " GDB is disconnected\n"));
1544}
1545
aef525cb
YQ
1546/* Reset local state of tracing. */
1547
1548void
1549trace_reset_local_state (void)
1550{
1551 set_traceframe_num (-1);
1552 set_tracepoint_num (-1);
1553 set_traceframe_context (NULL);
1554 clear_traceframe_info ();
1555}
c5aa993b 1556
f224b49d 1557void
0b39b52e 1558start_tracing (const char *notes)
d183932d 1559{
d914c394 1560 int any_enabled = 0, num_to_download = 0;
f196051f
SS
1561 int ret;
1562
f6d17b2b 1563 auto tracepoint_range = all_tracepoints ();
76a2b958 1564
c378eb4e 1565 /* No point in tracing without any tracepoints... */
f6d17b2b 1566 if (tracepoint_range.begin () == tracepoint_range.end ())
f51e0e20 1567 error (_("No tracepoints defined, not starting trace"));
76a2b958 1568
a1decfc1 1569 for (breakpoint &b : tracepoint_range)
76a2b958 1570 {
a1decfc1 1571 if (b.enable_state == bp_enabled)
d914c394
SS
1572 any_enabled = 1;
1573
a1decfc1 1574 if ((b.type == bp_fast_tracepoint
d914c394
SS
1575 ? may_insert_fast_tracepoints
1576 : may_insert_tracepoints))
1577 ++num_to_download;
1578 else
1579 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
a1decfc1 1580 (b.type == bp_fast_tracepoint ? "fast " : ""), b.number);
76a2b958
SS
1581 }
1582
76a2b958
SS
1583 if (!any_enabled)
1584 {
d248b706
KY
1585 if (target_supports_enable_disable_tracepoint ())
1586 warning (_("No tracepoints enabled"));
1587 else
1588 {
1589 /* No point in tracing with only disabled tracepoints that
1590 cannot be re-enabled. */
d248b706
KY
1591 error (_("No tracepoints enabled, not starting trace"));
1592 }
76a2b958
SS
1593 }
1594
d914c394 1595 if (num_to_download <= 0)
f51e0e20 1596 error (_("No tracepoints that may be downloaded, not starting trace"));
d914c394 1597
76a2b958
SS
1598 target_trace_init ();
1599
a1decfc1 1600 for (breakpoint &b : tracepoint_range)
7a697b8d 1601 {
a1decfc1 1602 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
f2a8bc8a 1603 int bp_location_downloaded = 0;
d9b3f62e 1604
4511b1ba 1605 /* Clear `inserted' flag. */
a1decfc1 1606 for (bp_location &loc : b.locations ())
b00b30b2 1607 loc.inserted = 0;
4511b1ba 1608
a1decfc1 1609 if ((b.type == bp_fast_tracepoint
d914c394
SS
1610 ? !may_insert_fast_tracepoints
1611 : !may_insert_tracepoints))
1612 continue;
1613
a1decfc1 1614 t.number_on_target = 0;
e8ba3115 1615
a1decfc1 1616 for (bp_location &loc : b.locations ())
1e4d1764
YQ
1617 {
1618 /* Since tracepoint locations are never duplicated, `inserted'
1619 flag should be zero. */
b00b30b2 1620 gdb_assert (!loc.inserted);
1e4d1764 1621
b00b30b2 1622 target_download_tracepoint (&loc);
1e4d1764 1623
b00b30b2 1624 loc.inserted = 1;
f2a8bc8a 1625 bp_location_downloaded = 1;
1e4d1764 1626 }
e8ba3115 1627
a1decfc1 1628 t.number_on_target = b.number;
55aa24fb 1629
a1decfc1 1630 for (bp_location &loc : b.locations ())
b00b30b2
SM
1631 if (loc.probe.prob != NULL)
1632 loc.probe.prob->set_semaphore (loc.probe.objfile, loc.gdbarch);
f2a8bc8a
YQ
1633
1634 if (bp_location_downloaded)
19081eb5 1635 notify_breakpoint_modified (&b);
7a697b8d 1636 }
76a2b958 1637
00bf0b85 1638 /* Send down all the trace state variables too. */
c252925c
SM
1639 for (const trace_state_variable &tsv : tvariables)
1640 target_download_trace_state_variable (tsv);
35b1e5cc
SS
1641
1642 /* Tell target to treat text-like sections as transparent. */
1643 target_trace_set_readonly_regions ();
4daf5ac0
SS
1644 /* Set some mode flags. */
1645 target_set_disconnected_tracing (disconnected_tracing);
1646 target_set_circular_trace_buffer (circular_trace_buffer);
f6f899bf 1647 target_set_trace_buffer_size (trace_buffer_size);
1042e4c0 1648
f196051f 1649 if (!notes)
e0700ba4 1650 notes = trace_notes.c_str ();
f196051f 1651
e0700ba4
SM
1652 ret = target_set_trace_notes (trace_user.c_str (), notes, NULL);
1653
1654 if (!ret && (!trace_user.empty () || notes))
43011e52 1655 warning (_("Target does not support trace user/notes, info ignored"));
f196051f 1656
35b1e5cc
SS
1657 /* Now insert traps and begin collecting data. */
1658 target_trace_start ();
1042e4c0 1659
35b1e5cc 1660 /* Reset our local state. */
aef525cb 1661 trace_reset_local_state ();
00bf0b85 1662 current_trace_status()->running = 1;
1042e4c0
SS
1663}
1664
f196051f
SS
1665/* The tstart command requests the target to start a new trace run.
1666 The command passes any arguments it has to the target verbatim, as
1667 an optional "trace note". This is useful as for instance a warning
1668 to other users if the trace runs disconnected, and you don't want
1669 anybody else messing with the target. */
f224b49d
VP
1670
1671static void
0b39b52e 1672tstart_command (const char *args, int from_tty)
f224b49d
VP
1673{
1674 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1675
615bcdef
SS
1676 if (current_trace_status ()->running)
1677 {
1678 if (from_tty
1679 && !query (_("A trace is running already. Start a new run? ")))
1680 error (_("New trace run not started."));
1681 }
1682
f196051f 1683 start_tracing (args);
f224b49d
VP
1684}
1685
f196051f
SS
1686/* The tstop command stops the tracing run. The command passes any
1687 supplied arguments to the target verbatim as a "stop note"; if the
1688 target supports trace notes, then it will be reported back as part
1689 of the trace run's status. */
1690
c906108c 1691static void
0b39b52e 1692tstop_command (const char *args, int from_tty)
d183932d 1693{
615bcdef
SS
1694 if (!current_trace_status ()->running)
1695 error (_("Trace is not running."));
1696
f196051f 1697 stop_tracing (args);
c906108c
SS
1698}
1699
d5551862 1700void
0b39b52e 1701stop_tracing (const char *note)
d5551862 1702{
f196051f
SS
1703 int ret;
1704
35b1e5cc 1705 target_trace_stop ();
f196051f 1706
a1decfc1 1707 for (breakpoint &t : all_tracepoints ())
55aa24fb 1708 {
a1decfc1 1709 if ((t.type == bp_fast_tracepoint
55aa24fb
SDJ
1710 ? !may_insert_fast_tracepoints
1711 : !may_insert_tracepoints))
1712 continue;
1713
a1decfc1 1714 for (bp_location &loc : t.locations ())
55aa24fb
SDJ
1715 {
1716 /* GDB can be totally absent in some disconnected trace scenarios,
1717 but we don't really care if this semaphore goes out of sync.
1718 That's why we are decrementing it here, but not taking care
1719 in other places. */
b00b30b2
SM
1720 if (loc.probe.prob != NULL)
1721 loc.probe.prob->clear_semaphore (loc.probe.objfile, loc.gdbarch);
55aa24fb
SDJ
1722 }
1723 }
1724
f196051f 1725 if (!note)
e0700ba4
SM
1726 note = trace_stop_notes.c_str ();
1727
f196051f
SS
1728 ret = target_set_trace_notes (NULL, NULL, note);
1729
1730 if (!ret && note)
43011e52 1731 warning (_("Target does not support trace notes, note ignored"));
f196051f 1732
c378eb4e 1733 /* Should change in response to reply? */
00bf0b85 1734 current_trace_status ()->running = 0;
d5551862
SS
1735}
1736
c906108c
SS
1737/* tstatus command */
1738static void
0b39b52e 1739tstatus_command (const char *args, int from_tty)
d183932d 1740{
00bf0b85 1741 struct trace_status *ts = current_trace_status ();
f51e0e20 1742 int status;
35b1e5cc 1743
00bf0b85
SS
1744 status = target_get_trace_status (ts);
1745
1746 if (status == -1)
1747 {
f5911ea1 1748 if (ts->filename != NULL)
6cb06a8c 1749 gdb_printf (_("Using a trace file.\n"));
00bf0b85
SS
1750 else
1751 {
6cb06a8c 1752 gdb_printf (_("Trace can not be run on this target.\n"));
00bf0b85
SS
1753 return;
1754 }
1755 }
1756
1757 if (!ts->running_known)
1758 {
6cb06a8c 1759 gdb_printf (_("Run/stop status is unknown.\n"));
00bf0b85
SS
1760 }
1761 else if (ts->running)
c906108c 1762 {
6cb06a8c 1763 gdb_printf (_("Trace is running on the target.\n"));
c906108c
SS
1764 }
1765 else
00bf0b85
SS
1766 {
1767 switch (ts->stop_reason)
1768 {
1769 case trace_never_run:
6cb06a8c 1770 gdb_printf (_("No trace has been run on the target.\n"));
00bf0b85 1771 break;
e5a873b7 1772 case trace_stop_command:
f196051f 1773 if (ts->stop_desc)
6cb06a8c
TT
1774 gdb_printf (_("Trace stopped by a tstop command (%s).\n"),
1775 ts->stop_desc);
f196051f 1776 else
6cb06a8c 1777 gdb_printf (_("Trace stopped by a tstop command.\n"));
00bf0b85
SS
1778 break;
1779 case trace_buffer_full:
6cb06a8c 1780 gdb_printf (_("Trace stopped because the buffer was full.\n"));
00bf0b85
SS
1781 break;
1782 case trace_disconnected:
6cb06a8c 1783 gdb_printf (_("Trace stopped because of disconnection.\n"));
00bf0b85
SS
1784 break;
1785 case tracepoint_passcount:
6cb06a8c
TT
1786 gdb_printf (_("Trace stopped by tracepoint %d.\n"),
1787 ts->stopping_tracepoint);
00bf0b85 1788 break;
6c28cbf2
SS
1789 case tracepoint_error:
1790 if (ts->stopping_tracepoint)
6cb06a8c
TT
1791 gdb_printf (_("Trace stopped by an "
1792 "error (%s, tracepoint %d).\n"),
1793 ts->stop_desc, ts->stopping_tracepoint);
6c28cbf2 1794 else
6cb06a8c
TT
1795 gdb_printf (_("Trace stopped by an error (%s).\n"),
1796 ts->stop_desc);
6c28cbf2 1797 break;
00bf0b85 1798 case trace_stop_reason_unknown:
6cb06a8c 1799 gdb_printf (_("Trace stopped for an unknown reason.\n"));
00bf0b85
SS
1800 break;
1801 default:
6cb06a8c
TT
1802 gdb_printf (_("Trace stopped for some other reason (%d).\n"),
1803 ts->stop_reason);
00bf0b85
SS
1804 break;
1805 }
1806 }
1807
4daf5ac0
SS
1808 if (ts->traceframes_created >= 0
1809 && ts->traceframe_count != ts->traceframes_created)
1810 {
6cb06a8c
TT
1811 gdb_printf (_("Buffer contains %d trace "
1812 "frames (of %d created total).\n"),
1813 ts->traceframe_count, ts->traceframes_created);
4daf5ac0
SS
1814 }
1815 else if (ts->traceframe_count >= 0)
00bf0b85 1816 {
6cb06a8c
TT
1817 gdb_printf (_("Collected %d trace frames.\n"),
1818 ts->traceframe_count);
00bf0b85
SS
1819 }
1820
4daf5ac0 1821 if (ts->buffer_free >= 0)
00bf0b85 1822 {
4daf5ac0
SS
1823 if (ts->buffer_size >= 0)
1824 {
6cb06a8c
TT
1825 gdb_printf (_("Trace buffer has %d bytes of %d bytes free"),
1826 ts->buffer_free, ts->buffer_size);
4daf5ac0 1827 if (ts->buffer_size > 0)
6cb06a8c
TT
1828 gdb_printf (_(" (%d%% full)"),
1829 ((int) ((((long long) (ts->buffer_size
1830 - ts->buffer_free)) * 100)
1831 / ts->buffer_size)));
1832 gdb_printf (_(".\n"));
4daf5ac0
SS
1833 }
1834 else
6cb06a8c
TT
1835 gdb_printf (_("Trace buffer has %d bytes free.\n"),
1836 ts->buffer_free);
00bf0b85 1837 }
35b1e5cc 1838
33da3f1c 1839 if (ts->disconnected_tracing)
6cb06a8c 1840 gdb_printf (_("Trace will continue if GDB disconnects.\n"));
33da3f1c 1841 else
6cb06a8c 1842 gdb_printf (_("Trace will stop if GDB disconnects.\n"));
33da3f1c
SS
1843
1844 if (ts->circular_buffer)
6cb06a8c 1845 gdb_printf (_("Trace buffer is circular.\n"));
33da3f1c 1846
f196051f 1847 if (ts->user_name && strlen (ts->user_name) > 0)
6cb06a8c 1848 gdb_printf (_("Trace user is %s.\n"), ts->user_name);
f196051f
SS
1849
1850 if (ts->notes && strlen (ts->notes) > 0)
6cb06a8c 1851 gdb_printf (_("Trace notes: %s.\n"), ts->notes);
f196051f 1852
00bf0b85 1853 /* Now report on what we're doing with tfind. */
35b1e5cc 1854 if (traceframe_number >= 0)
6cb06a8c
TT
1855 gdb_printf (_("Looking at trace frame %d, tracepoint %d.\n"),
1856 traceframe_number, tracepoint_number);
35b1e5cc 1857 else
6cb06a8c 1858 gdb_printf (_("Not looking at any trace frame.\n"));
f196051f
SS
1859
1860 /* Report start/stop times if supplied. */
1861 if (ts->start_time)
1862 {
1863 if (ts->stop_time)
1864 {
1865 LONGEST run_time = ts->stop_time - ts->start_time;
1866
1867 /* Reporting a run time is more readable than two long numbers. */
6cb06a8c
TT
1868 gdb_printf (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
1869 (long int) (ts->start_time / 1000000),
1870 (long int) (ts->start_time % 1000000),
1871 (long int) (run_time / 1000000),
1872 (long int) (run_time % 1000000));
f196051f
SS
1873 }
1874 else
6cb06a8c
TT
1875 gdb_printf (_("Trace started at %ld.%06ld secs.\n"),
1876 (long int) (ts->start_time / 1000000),
1877 (long int) (ts->start_time % 1000000));
f196051f
SS
1878 }
1879 else if (ts->stop_time)
6cb06a8c
TT
1880 gdb_printf (_("Trace stopped at %ld.%06ld secs.\n"),
1881 (long int) (ts->stop_time / 1000000),
1882 (long int) (ts->stop_time % 1000000));
f196051f
SS
1883
1884 /* Now report any per-tracepoint status available. */
01bccc56
TT
1885 for (breakpoint &b : all_tracepoints ())
1886 {
1887 tracepoint *t = gdb::checked_static_cast<tracepoint *> (&b);
1888 target_get_tracepoint_status (t, nullptr);
1889 }
c906108c
SS
1890}
1891
f224b49d
VP
1892/* Report the trace status to uiout, in a way suitable for MI, and not
1893 suitable for CLI. If ON_STOP is true, suppress a few fields that
1894 are not meaningful in the -trace-stop response.
1895
1896 The implementation is essentially parallel to trace_status_command, but
1897 merging them will result in unreadable code. */
1898void
1899trace_status_mi (int on_stop)
1900{
79a45e25 1901 struct ui_out *uiout = current_uiout;
f224b49d
VP
1902 struct trace_status *ts = current_trace_status ();
1903 int status;
f224b49d
VP
1904
1905 status = target_get_trace_status (ts);
1906
f5911ea1 1907 if (status == -1 && ts->filename == NULL)
f224b49d 1908 {
112e8700 1909 uiout->field_string ("supported", "0");
f224b49d
VP
1910 return;
1911 }
1912
f5911ea1 1913 if (ts->filename != NULL)
112e8700 1914 uiout->field_string ("supported", "file");
f224b49d 1915 else if (!on_stop)
112e8700 1916 uiout->field_string ("supported", "1");
f224b49d 1917
f5911ea1 1918 if (ts->filename != NULL)
112e8700 1919 uiout->field_string ("trace-file", ts->filename);
f5911ea1 1920
f224b49d
VP
1921 gdb_assert (ts->running_known);
1922
1923 if (ts->running)
1924 {
112e8700 1925 uiout->field_string ("running", "1");
f224b49d
VP
1926
1927 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
1928 Given that the frontend gets the status either on -trace-stop, or from
1929 -trace-status after re-connection, it does not seem like this
1930 information is necessary for anything. It is not necessary for either
1931 figuring the vital state of the target nor for navigation of trace
1932 frames. If the frontend wants to show the current state is some
1933 configure dialog, it can request the value when such dialog is
1934 invoked by the user. */
1935 }
1936 else
1937 {
a121b7c1 1938 const char *stop_reason = NULL;
f224b49d
VP
1939 int stopping_tracepoint = -1;
1940
1941 if (!on_stop)
112e8700 1942 uiout->field_string ("running", "0");
f224b49d
VP
1943
1944 if (ts->stop_reason != trace_stop_reason_unknown)
1945 {
1946 switch (ts->stop_reason)
1947 {
e5a873b7 1948 case trace_stop_command:
f224b49d
VP
1949 stop_reason = "request";
1950 break;
1951 case trace_buffer_full:
1952 stop_reason = "overflow";
1953 break;
1954 case trace_disconnected:
1955 stop_reason = "disconnection";
1956 break;
1957 case tracepoint_passcount:
1958 stop_reason = "passcount";
1959 stopping_tracepoint = ts->stopping_tracepoint;
1960 break;
6c28cbf2
SS
1961 case tracepoint_error:
1962 stop_reason = "error";
1963 stopping_tracepoint = ts->stopping_tracepoint;
1964 break;
f224b49d
VP
1965 }
1966
1967 if (stop_reason)
1968 {
112e8700 1969 uiout->field_string ("stop-reason", stop_reason);
f224b49d 1970 if (stopping_tracepoint != -1)
381befee
TT
1971 uiout->field_signed ("stopping-tracepoint",
1972 stopping_tracepoint);
6c28cbf2 1973 if (ts->stop_reason == tracepoint_error)
112e8700 1974 uiout->field_string ("error-description",
f196051f 1975 ts->stop_desc);
f224b49d
VP
1976 }
1977 }
1978 }
1979
a97153c7 1980 if (ts->traceframe_count != -1)
381befee 1981 uiout->field_signed ("frames", ts->traceframe_count);
87290684 1982 if (ts->traceframes_created != -1)
381befee 1983 uiout->field_signed ("frames-created", ts->traceframes_created);
a97153c7 1984 if (ts->buffer_size != -1)
381befee 1985 uiout->field_signed ("buffer-size", ts->buffer_size);
a97153c7 1986 if (ts->buffer_free != -1)
381befee 1987 uiout->field_signed ("buffer-free", ts->buffer_free);
a97153c7 1988
381befee
TT
1989 uiout->field_signed ("disconnected", ts->disconnected_tracing);
1990 uiout->field_signed ("circular", ts->circular_buffer);
f196051f 1991
112e8700
SM
1992 uiout->field_string ("user-name", ts->user_name);
1993 uiout->field_string ("notes", ts->notes);
f196051f
SS
1994
1995 {
1996 char buf[100];
1997
1998 xsnprintf (buf, sizeof buf, "%ld.%06ld",
f30aa5af
DK
1999 (long int) (ts->start_time / 1000000),
2000 (long int) (ts->start_time % 1000000));
112e8700 2001 uiout->field_string ("start-time", buf);
f196051f 2002 xsnprintf (buf, sizeof buf, "%ld.%06ld",
f30aa5af
DK
2003 (long int) (ts->stop_time / 1000000),
2004 (long int) (ts->stop_time % 1000000));
112e8700 2005 uiout->field_string ("stop-time", buf);
f196051f 2006 }
f224b49d
VP
2007}
2008
2f9d54cf
PA
2009/* Check if a trace run is ongoing. If so, and FROM_TTY, query the
2010 user if she really wants to detach. */
2011
d5551862 2012void
2f9d54cf 2013query_if_trace_running (int from_tty)
d5551862 2014{
2f9d54cf
PA
2015 if (!from_tty)
2016 return;
2017
00bf0b85
SS
2018 /* It can happen that the target that was tracing went away on its
2019 own, and we didn't notice. Get a status update, and if the
2020 current target doesn't even do tracing, then assume it's not
2021 running anymore. */
26afc0d7 2022 if (target_get_trace_status (current_trace_status ()) < 0)
00bf0b85
SS
2023 current_trace_status ()->running = 0;
2024
33da3f1c
SS
2025 /* If running interactively, give the user the option to cancel and
2026 then decide what to do differently with the run. Scripts are
2027 just going to disconnect and let the target deal with it,
2028 according to how it's been instructed previously via
2029 disconnected-tracing. */
2f9d54cf 2030 if (current_trace_status ()->running)
d5551862 2031 {
bfccc43c
YQ
2032 process_tracepoint_on_disconnect ();
2033
33da3f1c
SS
2034 if (current_trace_status ()->disconnected_tracing)
2035 {
3e43a32a
MS
2036 if (!query (_("Trace is running and will "
2037 "continue after detach; detach anyway? ")))
33da3f1c
SS
2038 error (_("Not confirmed."));
2039 }
2040 else
2041 {
3e43a32a
MS
2042 if (!query (_("Trace is running but will "
2043 "stop on detach; detach anyway? ")))
33da3f1c
SS
2044 error (_("Not confirmed."));
2045 }
d5551862 2046 }
2f9d54cf 2047}
8b9b7ef8 2048
2f9d54cf
PA
2049/* This function handles the details of what to do about an ongoing
2050 tracing run if the user has asked to detach or otherwise disconnect
2051 from the target. */
2052
2053void
2054disconnect_tracing (void)
2055{
8b9b7ef8
SS
2056 /* Also we want to be out of tfind mode, otherwise things can get
2057 confusing upon reconnection. Just use these calls instead of
2058 full tfind_1 behavior because we're in the middle of detaching,
2059 and there's no point to updating current stack frame etc. */
aef525cb 2060 trace_reset_local_state ();
d5551862
SS
2061}
2062
d183932d 2063/* Worker function for the various flavors of the tfind command. */
f197e0f1
VP
2064void
2065tfind_1 (enum trace_find_type type, int num,
cc5925ad 2066 CORE_ADDR addr1, CORE_ADDR addr2,
f197e0f1 2067 int from_tty)
c906108c
SS
2068{
2069 int target_frameno = -1, target_tracept = -1;
2ce6d6bf 2070 struct frame_id old_frame_id = null_frame_id;
d9b3f62e 2071 struct tracepoint *tp;
79a45e25 2072 struct ui_out *uiout = current_uiout;
c906108c 2073
2ce6d6bf
SS
2074 /* Only try to get the current stack frame if we have a chance of
2075 succeeding. In particular, if we're trying to get a first trace
2076 frame while all threads are running, it's not going to succeed,
2077 so leave it with a default value and let the frame comparison
2078 below (correctly) decide to print out the source location of the
2079 trace frame. */
2080 if (!(type == tfind_number && num == -1)
2081 && (has_stack_frames () || traceframe_number >= 0))
2082 old_frame_id = get_frame_id (get_current_frame ());
c906108c 2083
35b1e5cc
SS
2084 target_frameno = target_trace_find (type, num, addr1, addr2,
2085 &target_tracept);
2086
2087 if (type == tfind_number
2088 && num == -1
2089 && target_frameno == -1)
2090 {
2091 /* We told the target to get out of tfind mode, and it did. */
2092 }
2093 else if (target_frameno == -1)
2094 {
2ce6d6bf 2095 /* A request for a non-existent trace frame has failed.
35b1e5cc
SS
2096 Our response will be different, depending on FROM_TTY:
2097
2098 If FROM_TTY is true, meaning that this command was
2099 typed interactively by the user, then give an error
2100 and DO NOT change the state of traceframe_number etc.
2101
2102 However if FROM_TTY is false, meaning that we're either
2103 in a script, a loop, or a user-defined command, then
2104 DON'T give an error, but DO change the state of
2105 traceframe_number etc. to invalid.
2106
30baf67b 2107 The rationale is that if you typed the command, you
35b1e5cc
SS
2108 might just have committed a typo or something, and you'd
2109 like to NOT lose your current debugging state. However
2110 if you're in a user-defined command or especially in a
2111 loop, then you need a way to detect that the command
2112 failed WITHOUT aborting. This allows you to write
2113 scripts that search thru the trace buffer until the end,
2114 and then continue on to do something else. */
2115
2116 if (from_tty)
2117 error (_("Target failed to find requested trace frame."));
2118 else
2119 {
2120 if (info_verbose)
6cb06a8c 2121 gdb_printf ("End of trace buffer.\n");
c378eb4e 2122#if 0 /* dubious now? */
35b1e5cc
SS
2123 /* The following will not recurse, since it's
2124 special-cased. */
e5a873b7 2125 tfind_command ("-1", from_tty);
35b1e5cc
SS
2126#endif
2127 }
2128 }
2129
d5551862
SS
2130 tp = get_tracepoint_by_number_on_target (target_tracept);
2131
35f196d9 2132 reinit_frame_cache ();
41336620 2133 target_dcache_invalidate (current_program_space->aspace);
8d735b87 2134
c1fc2657 2135 set_tracepoint_num (tp ? tp->number : target_tracept);
201b4506
YQ
2136
2137 if (target_frameno != get_traceframe_number ())
0bc845fc 2138 interps_notify_traceframe_changed (target_frameno, tracepoint_number);
201b4506 2139
8d735b87
YQ
2140 set_current_traceframe (target_frameno);
2141
c906108c 2142 if (target_frameno == -1)
fb14de7b 2143 set_traceframe_context (NULL);
c906108c 2144 else
fb14de7b 2145 set_traceframe_context (get_current_frame ());
c906108c 2146
f197e0f1
VP
2147 if (traceframe_number >= 0)
2148 {
2149 /* Use different branches for MI and CLI to make CLI messages
2150 i18n-eable. */
112e8700 2151 if (uiout->is_mi_like_p ())
f197e0f1 2152 {
112e8700 2153 uiout->field_string ("found", "1");
381befee
TT
2154 uiout->field_signed ("tracepoint", tracepoint_number);
2155 uiout->field_signed ("traceframe", traceframe_number);
f197e0f1
VP
2156 }
2157 else
2158 {
6cb06a8c
TT
2159 gdb_printf (_("Found trace frame %d, tracepoint %d\n"),
2160 traceframe_number, tracepoint_number);
f197e0f1
VP
2161 }
2162 }
2163 else
2164 {
112e8700
SM
2165 if (uiout->is_mi_like_p ())
2166 uiout->field_string ("found", "0");
4136fdd2 2167 else if (type == tfind_number && num == -1)
6cb06a8c 2168 gdb_printf (_("No longer looking at any trace frame\n"));
c378eb4e 2169 else /* This case may never occur, check. */
6cb06a8c 2170 gdb_printf (_("No trace frame found\n"));
f197e0f1
VP
2171 }
2172
00bf0b85
SS
2173 /* If we're in nonstop mode and getting out of looking at trace
2174 frames, there won't be any current frame to go back to and
2175 display. */
2176 if (from_tty
2177 && (has_stack_frames () || traceframe_number >= 0))
c906108c 2178 {
0faf0076 2179 enum print_what print_what;
c906108c 2180
2ce6d6bf 2181 /* NOTE: in imitation of the step command, try to determine
dda83cd7
SM
2182 whether we have made a transition from one function to
2183 another. If so, we'll print the "stack frame" (ie. the new
2184 function and it's arguments) -- otherwise we'll just show the
2185 new source line. */
fb14de7b 2186
a0cbd650 2187 if (old_frame_id == get_frame_id (get_current_frame ()))
0faf0076 2188 print_what = SRC_LINE;
c906108c 2189 else
0faf0076 2190 print_what = SRC_AND_LOC;
c906108c 2191
08d72866 2192 print_stack_frame (get_selected_frame (NULL), 1, print_what, 1);
c906108c
SS
2193 do_displays ();
2194 }
2195}
2196
cc3da688
YQ
2197/* Error on looking at traceframes while trace is running. */
2198
2199void
2200check_trace_running (struct trace_status *status)
2201{
2202 if (status->running && status->filename == NULL)
2203 error (_("May not look at trace frames while trace is running."));
2204}
2205
c906108c
SS
2206/* trace_find_command takes a trace frame number n,
2207 sends "QTFrame:<n>" to the target,
2208 and accepts a reply that may contain several optional pieces
2209 of information: a frame number, a tracepoint number, and an
2210 indication of whether this is a trap frame or a stepping frame.
2211
2212 The minimal response is just "OK" (which indicates that the
2213 target does not give us a frame number or a tracepoint number).
2214 Instead of that, the target may send us a string containing
2215 any combination of:
c5aa993b
JM
2216 F<hexnum> (gives the selected frame number)
2217 T<hexnum> (gives the selected tracepoint number)
2218 */
c906108c
SS
2219
2220/* tfind command */
2221static void
a121b7c1 2222tfind_command_1 (const char *args, int from_tty)
c378eb4e 2223{ /* This should only be called with a numeric argument. */
c906108c 2224 int frameno = -1;
c906108c 2225
cc3da688 2226 check_trace_running (current_trace_status ());
35b1e5cc
SS
2227
2228 if (args == 0 || *args == 0)
2229 { /* TFIND with no args means find NEXT trace frame. */
2230 if (traceframe_number == -1)
c378eb4e 2231 frameno = 0; /* "next" is first one. */
01add95b 2232 else
35b1e5cc
SS
2233 frameno = traceframe_number + 1;
2234 }
2235 else if (0 == strcmp (args, "-"))
c906108c 2236 {
35b1e5cc
SS
2237 if (traceframe_number == -1)
2238 error (_("not debugging trace buffer"));
2239 else if (from_tty && traceframe_number == 0)
2240 error (_("already at start of trace buffer"));
2241
2242 frameno = traceframe_number - 1;
2243 }
2244 /* A hack to work around eval's need for fp to have been collected. */
2245 else if (0 == strcmp (args, "-1"))
2246 frameno = -1;
2247 else
2248 frameno = parse_and_eval_long (args);
c906108c 2249
35b1e5cc
SS
2250 if (frameno < -1)
2251 error (_("invalid input (%d is less than zero)"), frameno);
c906108c 2252
f197e0f1 2253 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
c906108c
SS
2254}
2255
a121b7c1 2256static void
981a3fb3 2257tfind_command (const char *args, int from_tty)
a121b7c1 2258{
09b847f3 2259 tfind_command_1 (args, from_tty);
a121b7c1
PA
2260}
2261
c906108c
SS
2262/* tfind end */
2263static void
2983f7cb 2264tfind_end_command (const char *args, int from_tty)
c906108c 2265{
a121b7c1 2266 tfind_command_1 ("-1", from_tty);
c906108c
SS
2267}
2268
c906108c
SS
2269/* tfind start */
2270static void
2983f7cb 2271tfind_start_command (const char *args, int from_tty)
c906108c 2272{
a121b7c1 2273 tfind_command_1 ("0", from_tty);
c906108c
SS
2274}
2275
2276/* tfind pc command */
2277static void
2983f7cb 2278tfind_pc_command (const char *args, int from_tty)
d183932d 2279{
c906108c 2280 CORE_ADDR pc;
c906108c 2281
cc3da688 2282 check_trace_running (current_trace_status ());
c906108c 2283
35b1e5cc 2284 if (args == 0 || *args == 0)
9c742269 2285 pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
c906108c 2286 else
35b1e5cc
SS
2287 pc = parse_and_eval_address (args);
2288
f197e0f1 2289 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
c906108c
SS
2290}
2291
2292/* tfind tracepoint command */
2293static void
2983f7cb 2294tfind_tracepoint_command (const char *args, int from_tty)
d183932d 2295{
c906108c 2296 int tdp;
d9b3f62e 2297 struct tracepoint *tp;
c906108c 2298
cc3da688 2299 check_trace_running (current_trace_status ());
383e5f85 2300
35b1e5cc
SS
2301 if (args == 0 || *args == 0)
2302 {
2303 if (tracepoint_number == -1)
2304 error (_("No current tracepoint -- please supply an argument."));
c906108c 2305 else
c378eb4e 2306 tdp = tracepoint_number; /* Default is current TDP. */
c906108c
SS
2307 }
2308 else
35b1e5cc
SS
2309 tdp = parse_and_eval_long (args);
2310
2311 /* If we have the tracepoint on hand, use the number that the
2312 target knows about (which may be different if we disconnected
2313 and reconnected). */
2314 tp = get_tracepoint (tdp);
2315 if (tp)
2316 tdp = tp->number_on_target;
2317
f197e0f1 2318 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
c906108c
SS
2319}
2320
2321/* TFIND LINE command:
c5aa993b 2322
c906108c 2323 This command will take a sourceline for argument, just like BREAK
d183932d 2324 or TRACE (ie. anything that "decode_line_1" can handle).
c5aa993b 2325
c906108c
SS
2326 With no argument, this command will find the next trace frame
2327 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2328
2329static void
2983f7cb 2330tfind_line_command (const char *args, int from_tty)
d183932d 2331{
cc3da688 2332 check_trace_running (current_trace_status ());
5af949e3 2333
6c5b2ebe 2334 symtab_and_line sal;
35b1e5cc
SS
2335 if (args == 0 || *args == 0)
2336 {
2337 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
35b1e5cc
SS
2338 }
2339 else
42e08e69 2340 {
6c5b2ebe
PA
2341 std::vector<symtab_and_line> sals
2342 = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
2343 sal = sals[0];
35b1e5cc 2344 }
6c5b2ebe 2345
35b1e5cc 2346 if (sal.symtab == 0)
42e08e69
SS
2347 error (_("No line number information available."));
2348
6c5b2ebe 2349 CORE_ADDR start_pc, end_pc;
42e08e69 2350 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
35b1e5cc
SS
2351 {
2352 if (start_pc == end_pc)
24b21115 2353 {
6cb06a8c
TT
2354 gdb_printf ("Line %d of \"%s\"",
2355 sal.line,
2356 symtab_to_filename_for_display (sal.symtab));
1285ce86 2357 gdb_stdout->wrap_here (2);
6cb06a8c 2358 gdb_printf (" is at address ");
35b1e5cc 2359 print_address (get_current_arch (), start_pc, gdb_stdout);
1285ce86 2360 gdb_stdout->wrap_here (2);
6cb06a8c 2361 gdb_printf (" but contains no code.\n");
35b1e5cc
SS
2362 sal = find_pc_line (start_pc, 0);
2363 if (sal.line > 0
2364 && find_line_pc_range (sal, &start_pc, &end_pc)
2365 && start_pc != end_pc)
6cb06a8c
TT
2366 gdb_printf ("Attempting to find line %d instead.\n",
2367 sal.line);
24b21115 2368 else
35b1e5cc 2369 error (_("Cannot find a good line."));
24b21115 2370 }
35b1e5cc 2371 }
492325c4
SM
2372 else
2373 {
2374 /* Is there any case in which we get here, and have an address
2375 which the user would want to see? If we have debugging
2376 symbols and no line numbers? */
2377 error (_("Line number %d is out of range for \"%s\"."),
2378 sal.line, symtab_to_filename_for_display (sal.symtab));
2379 }
35b1e5cc
SS
2380
2381 /* Find within range of stated line. */
2382 if (args && *args)
f197e0f1 2383 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
c906108c 2384 else
f197e0f1 2385 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
c906108c
SS
2386}
2387
2388/* tfind range command */
2389static void
2983f7cb 2390tfind_range_command (const char *args, int from_tty)
104c1213 2391{
c906108c 2392 static CORE_ADDR start, stop;
2983f7cb 2393 const char *tmp;
c906108c 2394
cc3da688 2395 check_trace_running (current_trace_status ());
c906108c 2396
35b1e5cc
SS
2397 if (args == 0 || *args == 0)
2398 { /* XXX FIXME: what should default behavior be? */
6cb06a8c 2399 gdb_printf ("Usage: tfind range STARTADDR, ENDADDR\n");
35b1e5cc
SS
2400 return;
2401 }
c906108c 2402
35b1e5cc
SS
2403 if (0 != (tmp = strchr (args, ',')))
2404 {
2983f7cb
TT
2405 std::string start_addr (args, tmp);
2406 ++tmp;
529480d0 2407 tmp = skip_spaces (tmp);
2983f7cb 2408 start = parse_and_eval_address (start_addr.c_str ());
35b1e5cc 2409 stop = parse_and_eval_address (tmp);
c906108c
SS
2410 }
2411 else
c378eb4e 2412 { /* No explicit end address? */
35b1e5cc
SS
2413 start = parse_and_eval_address (args);
2414 stop = start + 1; /* ??? */
2415 }
2416
f197e0f1 2417 tfind_1 (tfind_range, 0, start, stop, from_tty);
c906108c
SS
2418}
2419
2420/* tfind outside command */
2421static void
2983f7cb 2422tfind_outside_command (const char *args, int from_tty)
104c1213 2423{
c906108c 2424 CORE_ADDR start, stop;
2983f7cb 2425 const char *tmp;
c906108c 2426
f5911ea1
HAQ
2427 if (current_trace_status ()->running
2428 && current_trace_status ()->filename == NULL)
a73c6dcd 2429 error (_("May not look at trace frames while trace is running."));
c906108c 2430
35b1e5cc 2431 if (args == 0 || *args == 0)
c378eb4e 2432 { /* XXX FIXME: what should default behavior be? */
6cb06a8c 2433 gdb_printf ("Usage: tfind outside STARTADDR, ENDADDR\n");
35b1e5cc
SS
2434 return;
2435 }
c906108c 2436
35b1e5cc
SS
2437 if (0 != (tmp = strchr (args, ',')))
2438 {
2983f7cb
TT
2439 std::string start_addr (args, tmp);
2440 ++tmp;
529480d0 2441 tmp = skip_spaces (tmp);
2983f7cb 2442 start = parse_and_eval_address (start_addr.c_str ());
35b1e5cc 2443 stop = parse_and_eval_address (tmp);
c906108c
SS
2444 }
2445 else
c378eb4e 2446 { /* No explicit end address? */
35b1e5cc
SS
2447 start = parse_and_eval_address (args);
2448 stop = start + 1; /* ??? */
2449 }
2450
f197e0f1 2451 tfind_1 (tfind_outside, 0, start, stop, from_tty);
c906108c
SS
2452}
2453
c906108c
SS
2454/* info scope command: list the locals for a scope. */
2455static void
1d12d88f 2456info_scope_command (const char *args_in, int from_tty)
c906108c 2457{
3b7344d5 2458 struct bound_minimal_symbol msym;
3977b71f 2459 const struct block *block;
0d5cff50 2460 const char *symname;
f2fc3015 2461 const char *save_args = args_in;
de4f826b 2462 int j, count = 0;
768a979c
UW
2463 struct gdbarch *gdbarch;
2464 int regno;
f2fc3015 2465 const char *args = args_in;
c906108c
SS
2466
2467 if (args == 0 || *args == 0)
3e43a32a
MS
2468 error (_("requires an argument (function, "
2469 "line or *addr) to define a scope"));
c906108c 2470
264f9890
PA
2471 location_spec_up locspec = string_to_location_spec (&args,
2472 current_language);
6c5b2ebe 2473 std::vector<symtab_and_line> sals
264f9890 2474 = decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
6c5b2ebe
PA
2475 NULL, NULL, 0);
2476 if (sals.empty ())
f00aae0f
KS
2477 {
2478 /* Presumably decode_line_1 has already warned. */
f00aae0f
KS
2479 return;
2480 }
c906108c 2481
c378eb4e 2482 /* Resolve line numbers to PC. */
6c5b2ebe
PA
2483 resolve_sal_pc (&sals[0]);
2484 block = block_for_pc (sals[0].pc);
c906108c
SS
2485
2486 while (block != 0)
2487 {
c378eb4e 2488 QUIT; /* Allow user to bail out with ^C. */
548a89df 2489 for (struct symbol *sym : block_iterator_range (block))
c906108c 2490 {
c378eb4e 2491 QUIT; /* Allow user to bail out with ^C. */
c906108c 2492 if (count == 0)
6cb06a8c 2493 gdb_printf ("Scope for %s:\n", save_args);
c906108c 2494 count++;
e88c90f2 2495
987012b8 2496 symname = sym->print_name ();
c906108c 2497 if (symname == NULL || *symname == '\0')
c378eb4e 2498 continue; /* Probably botched, certainly useless. */
c906108c 2499
bcd6845e 2500 gdbarch = sym->arch ();
768a979c 2501
6cb06a8c 2502 gdb_printf ("Symbol %s is ", symname);
24d6c2a0 2503
7ae24327
SM
2504 if (const symbol_computed_ops *computed_ops = sym->computed_ops ();
2505 computed_ops != nullptr)
2506 computed_ops->describe_location (sym, block->entry_pc (),
2507 gdb_stdout);
24d6c2a0 2508 else
c5aa993b 2509 {
66d7f48f 2510 switch (sym->aclass ())
c5aa993b 2511 {
24d6c2a0
TT
2512 default:
2513 case LOC_UNDEF: /* Messed up symbol? */
6cb06a8c
TT
2514 gdb_printf ("a bogus symbol, class %d.\n",
2515 sym->aclass ());
24d6c2a0
TT
2516 count--; /* Don't count this one. */
2517 continue;
2518 case LOC_CONST:
6cb06a8c 2519 gdb_printf ("a constant with value %s (%s)",
4aeddc50
SM
2520 plongest (sym->value_longest ()),
2521 hex_string (sym->value_longest ()));
24d6c2a0
TT
2522 break;
2523 case LOC_CONST_BYTES:
6cb06a8c 2524 gdb_printf ("constant bytes: ");
5f9c5a63 2525 if (sym->type ())
df86565b 2526 for (j = 0; j < sym->type ()->length (); j++)
4aeddc50 2527 gdb_printf (" %02x", (unsigned) sym->value_bytes ()[j]);
24d6c2a0
TT
2528 break;
2529 case LOC_STATIC:
6cb06a8c 2530 gdb_printf ("in static storage at address ");
4aeddc50 2531 gdb_printf ("%s", paddress (gdbarch, sym->value_address ()));
24d6c2a0
TT
2532 break;
2533 case LOC_REGISTER:
2534 /* GDBARCH is the architecture associated with the objfile
2535 the symbol is defined in; the target architecture may be
2536 different, and may provide additional registers. However,
2537 we do not know the target architecture at this point.
2538 We assume the objfile architecture will contain all the
2539 standard registers that occur in debug info in that
2540 objfile. */
7ae24327 2541 regno = sym->register_ops ()->register_number (sym, gdbarch);
24d6c2a0 2542
d9743061 2543 if (sym->is_argument ())
6cb06a8c
TT
2544 gdb_printf ("an argument in register $%s",
2545 gdbarch_register_name (gdbarch, regno));
24d6c2a0 2546 else
6cb06a8c
TT
2547 gdb_printf ("a local variable in register $%s",
2548 gdbarch_register_name (gdbarch, regno));
24d6c2a0
TT
2549 break;
2550 case LOC_ARG:
6cb06a8c 2551 gdb_printf ("an argument at stack/frame offset %s",
4aeddc50 2552 plongest (sym->value_longest ()));
24d6c2a0
TT
2553 break;
2554 case LOC_LOCAL:
6cb06a8c 2555 gdb_printf ("a local variable at frame offset %s",
4aeddc50 2556 plongest (sym->value_longest ()));
24d6c2a0
TT
2557 break;
2558 case LOC_REF_ARG:
6cb06a8c 2559 gdb_printf ("a reference argument at offset %s",
4aeddc50 2560 plongest (sym->value_longest ()));
24d6c2a0
TT
2561 break;
2562 case LOC_REGPARM_ADDR:
2563 /* Note comment at LOC_REGISTER. */
7ae24327 2564 regno = sym->register_ops ()->register_number (sym, gdbarch);
6cb06a8c
TT
2565 gdb_printf ("the address of an argument, in register $%s",
2566 gdbarch_register_name (gdbarch, regno));
24d6c2a0
TT
2567 break;
2568 case LOC_TYPEDEF:
6cb06a8c 2569 gdb_printf ("a typedef.\n");
24d6c2a0
TT
2570 continue;
2571 case LOC_LABEL:
6cb06a8c 2572 gdb_printf ("a label at address ");
4aeddc50 2573 gdb_printf ("%s", paddress (gdbarch, sym->value_address ()));
24d6c2a0
TT
2574 break;
2575 case LOC_BLOCK:
6cb06a8c
TT
2576 gdb_printf ("a function at address ");
2577 gdb_printf ("%s",
4aeddc50 2578 paddress (gdbarch,
6395b628 2579 sym->value_block ()->entry_pc ()));
24d6c2a0
TT
2580 break;
2581 case LOC_UNRESOLVED:
987012b8 2582 msym = lookup_minimal_symbol (sym->linkage_name (),
24d6c2a0 2583 NULL, NULL);
3b7344d5 2584 if (msym.minsym == NULL)
6cb06a8c 2585 gdb_printf ("Unresolved Static");
24d6c2a0
TT
2586 else
2587 {
6cb06a8c
TT
2588 gdb_printf ("static storage at address ");
2589 gdb_printf ("%s",
4aeddc50 2590 paddress (gdbarch, msym.value_address ()));
24d6c2a0
TT
2591 }
2592 break;
2593 case LOC_OPTIMIZED_OUT:
6cb06a8c 2594 gdb_printf ("optimized out.\n");
24d6c2a0
TT
2595 continue;
2596 case LOC_COMPUTED:
557b4d76 2597 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
c5aa993b 2598 }
c5aa993b 2599 }
5f9c5a63 2600 if (sym->type ())
cc1defb1 2601 {
5f9c5a63 2602 struct type *t = check_typedef (sym->type ());
cc1defb1 2603
df86565b 2604 gdb_printf (", length %s.\n", pulongest (t->length ()));
cc1defb1 2605 }
c906108c 2606 }
6c00f721 2607 if (block->function ())
c906108c
SS
2608 break;
2609 else
f135fe72 2610 block = block->superblock ();
c906108c
SS
2611 }
2612 if (count <= 0)
6cb06a8c
TT
2613 gdb_printf ("Scope for %s contains no locals or arguments.\n",
2614 save_args);
c906108c
SS
2615}
2616
afd02f27
PA
2617/* Helper for trace_dump_command. Dump the action list starting at
2618 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2619 actions of the body of a while-stepping action. STEPPING_FRAME is
2620 set if the current traceframe was determined to be a while-stepping
2621 traceframe. */
2622
c906108c 2623static void
afd02f27
PA
2624trace_dump_actions (struct command_line *action,
2625 int stepping_actions, int stepping_frame,
2626 int from_tty)
c906108c 2627{
6f937416 2628 const char *action_exp, *next_comma;
c906108c 2629
afd02f27 2630 for (; action != NULL; action = action->next)
c906108c
SS
2631 {
2632 struct cmd_list_element *cmd;
2633
c378eb4e 2634 QUIT; /* Allow user to bail out with ^C. */
a7bdde9e 2635 action_exp = action->line;
f1735a53 2636 action_exp = skip_spaces (action_exp);
c906108c
SS
2637
2638 /* The collection actions to be done while stepping are
dda83cd7 2639 bracketed by the commands "while-stepping" and "end". */
c906108c
SS
2640
2641 if (*action_exp == '#') /* comment line */
2642 continue;
2643
cf00cd6f 2644 cmd = lookup_cmd (&action_exp, cmdlist, "", NULL, -1, 1);
c906108c 2645 if (cmd == 0)
8a3fe4f8 2646 error (_("Bad action list item: %s"), action_exp);
c906108c 2647
3a553c80 2648 if (cmd_simple_func_eq (cmd, while_stepping_pseudocommand))
afd02f27 2649 {
12973681
TT
2650 gdb_assert (action->body_list_1 == nullptr);
2651 trace_dump_actions (action->body_list_0.get (),
2652 1, stepping_frame, from_tty);
afd02f27 2653 }
3a553c80 2654 else if (cmd_simple_func_eq (cmd, collect_pseudocommand))
c906108c
SS
2655 {
2656 /* Display the collected data.
d183932d
MS
2657 For the trap frame, display only what was collected at
2658 the trap. Likewise for stepping frames, display only
2659 what was collected while stepping. This means that the
2660 two boolean variables, STEPPING_FRAME and
2661 STEPPING_ACTIONS should be equal. */
c906108c
SS
2662 if (stepping_frame == stepping_actions)
2663 {
92bc6a20 2664 int trace_string = 0;
6f937416 2665
3065dfb6 2666 if (*action_exp == '/')
92bc6a20 2667 action_exp = decode_agent_options (action_exp, &trace_string);
3065dfb6 2668
c5aa993b 2669 do
c378eb4e
MS
2670 { /* Repeat over a comma-separated list. */
2671 QUIT; /* Allow user to bail out with ^C. */
c5aa993b
JM
2672 if (*action_exp == ',')
2673 action_exp++;
f1735a53 2674 action_exp = skip_spaces (action_exp);
c5aa993b
JM
2675
2676 next_comma = strchr (action_exp, ',');
2677
2678 if (0 == strncasecmp (action_exp, "$reg", 4))
2679 registers_info (NULL, from_tty);
6710bf39
SS
2680 else if (0 == strncasecmp (action_exp, "$_ret", 5))
2681 ;
c5aa993b 2682 else if (0 == strncasecmp (action_exp, "$loc", 4))
11db9430 2683 info_locals_command (NULL, from_tty);
c5aa993b 2684 else if (0 == strncasecmp (action_exp, "$arg", 4))
11db9430 2685 info_args_command (NULL, from_tty);
c5aa993b
JM
2686 else
2687 { /* variable */
15b6611c
TT
2688 std::string contents;
2689 const char *exp = action_exp;
6f937416 2690 if (next_comma != NULL)
c5aa993b 2691 {
6f937416 2692 size_t len = next_comma - action_exp;
15b6611c
TT
2693 contents = std::string (action_exp, len);
2694 exp = contents.c_str ();
6f937416 2695 }
6f937416 2696
6cb06a8c 2697 gdb_printf ("%s = ", exp);
122b53ea 2698 output_command (exp, from_tty);
6cb06a8c 2699 gdb_printf ("\n");
c5aa993b 2700 }
c5aa993b
JM
2701 action_exp = next_comma;
2702 }
2703 while (action_exp && *action_exp == ',');
c906108c
SS
2704 }
2705 }
2706 }
afd02f27
PA
2707}
2708
ddacd3c8
YQ
2709/* Return bp_location of the tracepoint associated with the current
2710 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
2711 is a stepping traceframe. */
2712
dc673c81 2713struct bp_location *
ddacd3c8
YQ
2714get_traceframe_location (int *stepping_frame_p)
2715{
2716 struct tracepoint *t;
ddacd3c8
YQ
2717 struct regcache *regcache;
2718
2719 if (tracepoint_number == -1)
2720 error (_("No current trace frame."));
2721
2722 t = get_tracepoint (tracepoint_number);
2723
2724 if (t == NULL)
2725 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2726 tracepoint_number);
2727
2728 /* The current frame is a trap frame if the frame PC is equal to the
2729 tracepoint PC. If not, then the current frame was collected
2730 during single-stepping. */
9c742269 2731 regcache = get_thread_regcache (inferior_thread ());
ddacd3c8
YQ
2732
2733 /* If the traceframe's address matches any of the tracepoint's
2734 locations, assume it is a direct hit rather than a while-stepping
2735 frame. (FIXME this is not reliable, should record each frame's
2736 type.) */
b00b30b2
SM
2737 for (bp_location &tloc : t->locations ())
2738 if (tloc.address == regcache_read_pc (regcache))
ddacd3c8
YQ
2739 {
2740 *stepping_frame_p = 0;
b00b30b2 2741 return &tloc;
ddacd3c8
YQ
2742 }
2743
2744 /* If this is a stepping frame, we don't know which location
2745 triggered. The first is as good (or bad) a guess as any... */
2746 *stepping_frame_p = 1;
f5951b9f 2747 return &t->first_loc ();
ddacd3c8
YQ
2748}
2749
12973681 2750/* Return the default collect actions of a tracepoint T. */
3ca73e0c 2751
12973681 2752static counted_command_line
01bccc56 2753all_tracepoint_actions (tracepoint *t)
3ca73e0c 2754{
12973681 2755 counted_command_line actions (nullptr, command_lines_deleter ());
3ca73e0c
YQ
2756
2757 /* If there are default expressions to collect, make up a collect
2758 action and prepend to the action list to encode. Note that since
2759 validation is per-tracepoint (local var "xyz" might be valid for
2760 one tracepoint and not another, etc), we make up the action on
2761 the fly, and don't cache it. */
e0700ba4 2762 if (!default_collect.empty ())
3ca73e0c 2763 {
12973681 2764 gdb::unique_xmalloc_ptr<char> default_collect_line
8579fd13 2765 = xstrprintf ("collect %s", default_collect.c_str ());
3ca73e0c 2766
12973681
TT
2767 validate_actionline (default_collect_line.get (), t);
2768 actions.reset (new struct command_line (simple_control,
2769 default_collect_line.release ()),
2770 command_lines_deleter ());
3ca73e0c
YQ
2771 }
2772
2773 return actions;
2774}
2775
afd02f27
PA
2776/* The tdump command. */
2777
2778static void
0b39b52e 2779tdump_command (const char *args, int from_tty)
afd02f27 2780{
afd02f27
PA
2781 int stepping_frame = 0;
2782 struct bp_location *loc;
2783
ddacd3c8
YQ
2784 /* This throws an error is not inspecting a trace frame. */
2785 loc = get_traceframe_location (&stepping_frame);
afd02f27 2786
6cb06a8c
TT
2787 gdb_printf ("Data collected at tracepoint %d, trace frame %d:\n",
2788 tracepoint_number, traceframe_number);
afd02f27 2789
de74e63a
YQ
2790 /* This command only makes sense for the current frame, not the
2791 selected frame. */
5ed8105e
PA
2792 scoped_restore_current_thread restore_thread;
2793
de74e63a
YQ
2794 select_frame (get_current_frame ());
2795
01bccc56
TT
2796 tracepoint *t = gdb::checked_static_cast<tracepoint *> (loc->owner);
2797 counted_command_line actions = all_tracepoint_actions (t);
2114d44c 2798
12973681
TT
2799 trace_dump_actions (actions.get (), 0, stepping_frame, from_tty);
2800 trace_dump_actions (breakpoint_commands (loc->owner), 0, stepping_frame,
2801 from_tty);
c906108c
SS
2802}
2803
409873ef
SS
2804/* Encode a piece of a tracepoint's source-level definition in a form
2805 that is suitable for both protocol and saving in files. */
2806/* This version does not do multiple encodes for long strings; it should
2807 return an offset to the next piece to encode. FIXME */
2808
a121b7c1 2809int
409873ef 2810encode_source_string (int tpnum, ULONGEST addr,
a121b7c1
PA
2811 const char *srctype, const char *src,
2812 char *buf, int buf_size)
409873ef
SS
2813{
2814 if (80 + strlen (srctype) > buf_size)
2815 error (_("Buffer too small for source encoding"));
2816 sprintf (buf, "%x:%s:%s:%x:%x:",
3e43a32a
MS
2817 tpnum, phex_nz (addr, sizeof (addr)),
2818 srctype, 0, (int) strlen (src));
409873ef
SS
2819 if (strlen (buf) + strlen (src) * 2 >= buf_size)
2820 error (_("Source string too long for buffer"));
9f1b45b0 2821 bin2hex ((gdb_byte *) src, buf + strlen (buf), strlen (src));
409873ef
SS
2822 return -1;
2823}
2824
d5551862
SS
2825/* Tell the target what to do with an ongoing tracing run if GDB
2826 disconnects for some reason. */
2827
d5551862 2828static void
eb4c3f4a 2829set_disconnected_tracing (const char *args, int from_tty,
d5551862
SS
2830 struct cmd_list_element *c)
2831{
f196051f 2832 target_set_disconnected_tracing (disconnected_tracing);
d5551862
SS
2833}
2834
4daf5ac0 2835static void
eb4c3f4a 2836set_circular_trace_buffer (const char *args, int from_tty,
4daf5ac0
SS
2837 struct cmd_list_element *c)
2838{
2839 target_set_circular_trace_buffer (circular_trace_buffer);
2840}
2841
f6f899bf 2842static void
eb4c3f4a 2843set_trace_buffer_size (const char *args, int from_tty,
f6f899bf
HAQ
2844 struct cmd_list_element *c)
2845{
2846 target_set_trace_buffer_size (trace_buffer_size);
2847}
2848
f196051f 2849static void
eb4c3f4a 2850set_trace_user (const char *args, int from_tty,
f196051f
SS
2851 struct cmd_list_element *c)
2852{
2853 int ret;
2854
e0700ba4 2855 ret = target_set_trace_notes (trace_user.c_str (), NULL, NULL);
f196051f
SS
2856
2857 if (!ret)
43011e52 2858 warning (_("Target does not support trace notes, user ignored"));
f196051f
SS
2859}
2860
2861static void
eb4c3f4a 2862set_trace_notes (const char *args, int from_tty,
f196051f
SS
2863 struct cmd_list_element *c)
2864{
2865 int ret;
2866
e0700ba4 2867 ret = target_set_trace_notes (NULL, trace_notes.c_str (), NULL);
f196051f
SS
2868
2869 if (!ret)
43011e52 2870 warning (_("Target does not support trace notes, note ignored"));
f196051f
SS
2871}
2872
2873static void
eb4c3f4a 2874set_trace_stop_notes (const char *args, int from_tty,
f196051f
SS
2875 struct cmd_list_element *c)
2876{
2877 int ret;
2878
e0700ba4 2879 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes.c_str ());
f196051f
SS
2880
2881 if (!ret)
43011e52 2882 warning (_("Target does not support trace notes, stop note ignored"));
f196051f
SS
2883}
2884
c5aa993b 2885int
fba45db2 2886get_traceframe_number (void)
c906108c 2887{
c5aa993b 2888 return traceframe_number;
c906108c
SS
2889}
2890
393fd4c3
YQ
2891int
2892get_tracepoint_number (void)
2893{
2894 return tracepoint_number;
2895}
2896
06cd862c
PA
2897/* Make the traceframe NUM be the current trace frame. Does nothing
2898 if NUM is already current. */
2899
2900void
e6e4e701 2901set_current_traceframe (int num)
06cd862c
PA
2902{
2903 int newnum;
2904
2905 if (traceframe_number == num)
2906 {
2907 /* Nothing to do. */
2908 return;
2909 }
2910
2911 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
2912
2913 if (newnum != num)
2914 warning (_("could not change traceframe"));
2915
8d735b87 2916 set_traceframe_num (newnum);
06cd862c
PA
2917
2918 /* Changing the traceframe changes our view of registers and of the
2919 frame chain. */
2920 registers_changed ();
b3b9301e
PA
2921
2922 clear_traceframe_info ();
06cd862c
PA
2923}
2924
6f14adc5
SM
2925scoped_restore_current_traceframe::scoped_restore_current_traceframe ()
2926: m_traceframe_number (traceframe_number)
2927{}
00bf0b85
SS
2928
2929/* Given a number and address, return an uploaded tracepoint with that
2930 number, creating if necessary. */
2931
2932struct uploaded_tp *
2933get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
2934{
2935 struct uploaded_tp *utp;
2936
2937 for (utp = *utpp; utp; utp = utp->next)
2938 if (utp->number == num && utp->addr == addr)
2939 return utp;
8d749320 2940
a18ba4e4 2941 utp = new uploaded_tp;
00bf0b85
SS
2942 utp->number = num;
2943 utp->addr = addr;
2944 utp->next = *utpp;
2945 *utpp = utp;
8d749320 2946
00bf0b85
SS
2947 return utp;
2948}
2949
7951c4eb 2950void
00bf0b85
SS
2951free_uploaded_tps (struct uploaded_tp **utpp)
2952{
2953 struct uploaded_tp *next_one;
2954
2955 while (*utpp)
2956 {
2957 next_one = (*utpp)->next;
a18ba4e4 2958 delete *utpp;
00bf0b85
SS
2959 *utpp = next_one;
2960 }
2961}
2962
2963/* Given a number and address, return an uploaded tracepoint with that
2964 number, creating if necessary. */
2965
393fd4c3 2966struct uploaded_tsv *
00bf0b85
SS
2967get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
2968{
2969 struct uploaded_tsv *utsv;
2970
2971 for (utsv = *utsvp; utsv; utsv = utsv->next)
2972 if (utsv->number == num)
2973 return utsv;
8d749320
SM
2974
2975 utsv = XCNEW (struct uploaded_tsv);
00bf0b85
SS
2976 utsv->number = num;
2977 utsv->next = *utsvp;
2978 *utsvp = utsv;
8d749320 2979
00bf0b85
SS
2980 return utsv;
2981}
2982
7951c4eb 2983void
00bf0b85
SS
2984free_uploaded_tsvs (struct uploaded_tsv **utsvp)
2985{
2986 struct uploaded_tsv *next_one;
2987
2988 while (*utsvp)
2989 {
2990 next_one = (*utsvp)->next;
2991 xfree (*utsvp);
2992 *utsvp = next_one;
2993 }
2994}
2995
4e5c165d
HZ
2996/* FIXME this function is heuristic and will miss the cases where the
2997 conditional is semantically identical but differs in whitespace,
2998 such as "x == 0" vs "x==0". */
2999
3000static int
3001cond_string_is_same (char *str1, char *str2)
3002{
3003 if (str1 == NULL || str2 == NULL)
3004 return (str1 == str2);
3005
3006 return (strcmp (str1, str2) == 0);
3007}
3008
00bf0b85
SS
3009/* Look for an existing tracepoint that seems similar enough to the
3010 uploaded one. Enablement isn't compared, because the user can
3011 toggle that freely, and may have done so in anticipation of the
1e4d1764 3012 next trace run. Return the location of matched tracepoint. */
00bf0b85 3013
70221824 3014static struct bp_location *
1e4d1764 3015find_matching_tracepoint_location (struct uploaded_tp *utp)
00bf0b85 3016{
a1decfc1 3017 for (breakpoint &b : all_tracepoints ())
00bf0b85 3018 {
a1decfc1 3019 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
d9b3f62e 3020
a1decfc1
SM
3021 if (b.type == utp->type
3022 && t.step_count == utp->step
3023 && t.pass_count == utp->pass
3024 && cond_string_is_same (t.cond_string.get (),
67aa1f3c 3025 utp->cond_string.get ())
4e5c165d 3026 /* FIXME also test actions. */
00bf0b85
SS
3027 )
3028 {
3029 /* Scan the locations for an address match. */
a1decfc1 3030 for (bp_location &loc : b.locations ())
b00b30b2
SM
3031 if (loc.address == utp->addr)
3032 return &loc;
00bf0b85
SS
3033 }
3034 }
3035 return NULL;
3036}
3037
3038/* Given a list of tracepoints uploaded from a target, attempt to
3039 match them up with existing tracepoints, and create new ones if not
3040 found. */
3041
3042void
3043merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
3044{
3045 struct uploaded_tp *utp;
f2a8bc8a 3046 /* A set of tracepoints which are modified. */
f51e0e20 3047 std::vector<breakpoint *> modified_tp;
00bf0b85
SS
3048
3049 /* Look for GDB tracepoints that match up with our uploaded versions. */
3050 for (utp = *uploaded_tps; utp; utp = utp->next)
3051 {
1e4d1764
YQ
3052 struct bp_location *loc;
3053 struct tracepoint *t;
3054
3055 loc = find_matching_tracepoint_location (utp);
3056 if (loc)
3057 {
f2a8bc8a
YQ
3058 int found = 0;
3059
1e4d1764
YQ
3060 /* Mark this location as already inserted. */
3061 loc->inserted = 1;
01bccc56 3062 t = gdb::checked_static_cast<tracepoint *> (loc->owner);
6cb06a8c
TT
3063 gdb_printf (_("Assuming tracepoint %d is same "
3064 "as target's tracepoint %d at %s.\n"),
3065 loc->owner->number, utp->number,
3066 paddress (loc->gdbarch, utp->addr));
f2a8bc8a
YQ
3067
3068 /* The tracepoint LOC->owner was modified (the location LOC
3069 was marked as inserted in the target). Save it in
3070 MODIFIED_TP if not there yet. The 'breakpoint-modified'
3071 observers will be notified later once for each tracepoint
3072 saved in MODIFIED_TP. */
f51e0e20 3073 for (breakpoint *b : modified_tp)
f2a8bc8a
YQ
3074 if (b == loc->owner)
3075 {
3076 found = 1;
3077 break;
3078 }
3079 if (!found)
f51e0e20 3080 modified_tp.push_back (loc->owner);
1e4d1764 3081 }
00bf0b85
SS
3082 else
3083 {
3084 t = create_tracepoint_from_upload (utp);
3085 if (t)
6cb06a8c
TT
3086 gdb_printf (_("Created tracepoint %d for "
3087 "target's tracepoint %d at %s.\n"),
3088 t->number, utp->number,
3089 paddress (get_current_arch (), utp->addr));
00bf0b85 3090 else
6cb06a8c
TT
3091 gdb_printf (_("Failed to create tracepoint for target's "
3092 "tracepoint %d at %s, skipping it.\n"),
3093 utp->number,
3094 paddress (get_current_arch (), utp->addr));
00bf0b85
SS
3095 }
3096 /* Whether found or created, record the number used by the
3097 target, to help with mapping target tracepoints back to their
3098 counterparts here. */
3099 if (t)
3100 t->number_on_target = utp->number;
3101 }
3102
f2a8bc8a
YQ
3103 /* Notify 'breakpoint-modified' observer that at least one of B's
3104 locations was changed. */
f51e0e20 3105 for (breakpoint *b : modified_tp)
19081eb5 3106 notify_breakpoint_modified (b);
f2a8bc8a 3107
00bf0b85
SS
3108 free_uploaded_tps (uploaded_tps);
3109}
3110
3111/* Trace state variables don't have much to identify them beyond their
3112 name, so just use that to detect matches. */
3113
70221824 3114static struct trace_state_variable *
00bf0b85
SS
3115find_matching_tsv (struct uploaded_tsv *utsv)
3116{
3117 if (!utsv->name)
3118 return NULL;
3119
3120 return find_trace_state_variable (utsv->name);
3121}
3122
70221824 3123static struct trace_state_variable *
00bf0b85
SS
3124create_tsv_from_upload (struct uploaded_tsv *utsv)
3125{
3126 const char *namebase;
8abcee91 3127 std::string buf;
00bf0b85
SS
3128 int try_num = 0;
3129 struct trace_state_variable *tsv;
3130
3131 if (utsv->name)
3132 {
3133 namebase = utsv->name;
8abcee91 3134 buf = namebase;
00bf0b85
SS
3135 }
3136 else
3137 {
3138 namebase = "__tsv";
8abcee91 3139 buf = string_printf ("%s_%d", namebase, try_num++);
00bf0b85
SS
3140 }
3141
3142 /* Fish for a name that is not in use. */
c378eb4e 3143 /* (should check against all internal vars?) */
8abcee91
TT
3144 while (find_trace_state_variable (buf.c_str ()))
3145 buf = string_printf ("%s_%d", namebase, try_num++);
00bf0b85
SS
3146
3147 /* We have an available name, create the variable. */
8abcee91 3148 tsv = create_trace_state_variable (buf.c_str ());
00bf0b85
SS
3149 tsv->initial_value = utsv->initial_value;
3150 tsv->builtin = utsv->builtin;
3151
bf506f27 3152 interps_notify_tsv_created (tsv);
bb25a15c 3153
00bf0b85
SS
3154 return tsv;
3155}
3156
3157/* Given a list of uploaded trace state variables, try to match them
3158 up with existing variables, or create additional ones. */
3159
3160void
3161merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
3162{
00bf0b85 3163 struct uploaded_tsv *utsv;
00bf0b85
SS
3164 int highest;
3165
3166 /* Most likely some numbers will have to be reassigned as part of
3167 the merge, so clear them all in anticipation. */
c252925c
SM
3168 for (trace_state_variable &tsv : tvariables)
3169 tsv.number = 0;
00bf0b85
SS
3170
3171 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
3172 {
b926417a 3173 struct trace_state_variable *tsv = find_matching_tsv (utsv);
00bf0b85 3174 if (tsv)
417b5110
DJ
3175 {
3176 if (info_verbose)
6cb06a8c
TT
3177 gdb_printf (_("Assuming trace state variable $%s "
3178 "is same as target's variable %d.\n"),
3179 tsv->name.c_str (), utsv->number);
417b5110 3180 }
00bf0b85
SS
3181 else
3182 {
3183 tsv = create_tsv_from_upload (utsv);
417b5110 3184 if (info_verbose)
6cb06a8c
TT
3185 gdb_printf (_("Created trace state variable "
3186 "$%s for target's variable %d.\n"),
3187 tsv->name.c_str (), utsv->number);
00bf0b85
SS
3188 }
3189 /* Give precedence to numberings that come from the target. */
3190 if (tsv)
3191 tsv->number = utsv->number;
3192 }
3193
3194 /* Renumber everything that didn't get a target-assigned number. */
3195 highest = 0;
c252925c
SM
3196 for (const trace_state_variable &tsv : tvariables)
3197 highest = std::max (tsv.number, highest);
00bf0b85
SS
3198
3199 ++highest;
c252925c
SM
3200 for (trace_state_variable &tsv : tvariables)
3201 if (tsv.number == 0)
3202 tsv.number = highest++;
00bf0b85
SS
3203
3204 free_uploaded_tsvs (uploaded_tsvs);
3205}
3206
00bf0b85
SS
3207/* Parse the part of trace status syntax that is shared between
3208 the remote protocol and the trace file reader. */
3209
00bf0b85 3210void
256642e8 3211parse_trace_status (const char *line, struct trace_status *ts)
00bf0b85 3212{
256642e8 3213 const char *p = line, *p1, *p2, *p3, *p_temp;
f196051f 3214 int end;
00bf0b85
SS
3215 ULONGEST val;
3216
3217 ts->running_known = 1;
3218 ts->running = (*p++ == '1');
3219 ts->stop_reason = trace_stop_reason_unknown;
f196051f
SS
3220 xfree (ts->stop_desc);
3221 ts->stop_desc = NULL;
4daf5ac0
SS
3222 ts->traceframe_count = -1;
3223 ts->traceframes_created = -1;
3224 ts->buffer_free = -1;
3225 ts->buffer_size = -1;
33da3f1c
SS
3226 ts->disconnected_tracing = 0;
3227 ts->circular_buffer = 0;
f196051f
SS
3228 xfree (ts->user_name);
3229 ts->user_name = NULL;
3230 xfree (ts->notes);
3231 ts->notes = NULL;
3232 ts->start_time = ts->stop_time = 0;
4daf5ac0 3233
00bf0b85
SS
3234 while (*p++)
3235 {
3236 p1 = strchr (p, ':');
3237 if (p1 == NULL)
3238 error (_("Malformed trace status, at %s\n\
3239Status line: '%s'\n"), p, line);
f196051f
SS
3240 p3 = strchr (p, ';');
3241 if (p3 == NULL)
3242 p3 = p + strlen (p);
00bf0b85
SS
3243 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
3244 {
3245 p = unpack_varlen_hex (++p1, &val);
3246 ts->stop_reason = trace_buffer_full;
3247 }
3248 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
3249 {
3250 p = unpack_varlen_hex (++p1, &val);
3251 ts->stop_reason = trace_never_run;
3252 }
3e43a32a
MS
3253 else if (strncmp (p, stop_reason_names[tracepoint_passcount],
3254 p1 - p) == 0)
00bf0b85
SS
3255 {
3256 p = unpack_varlen_hex (++p1, &val);
3257 ts->stop_reason = tracepoint_passcount;
3258 ts->stopping_tracepoint = val;
3259 }
e5a873b7 3260 else if (strncmp (p, stop_reason_names[trace_stop_command], p1 - p) == 0)
00bf0b85 3261 {
f196051f
SS
3262 p2 = strchr (++p1, ':');
3263 if (!p2 || p2 > p3)
3264 {
3265 /*older style*/
3266 p2 = p1;
3267 }
3268 else if (p2 != p1)
3269 {
224c3ddb 3270 ts->stop_desc = (char *) xmalloc (strlen (line));
bc20a4af 3271 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
f196051f
SS
3272 ts->stop_desc[end] = '\0';
3273 }
3274 else
3275 ts->stop_desc = xstrdup ("");
3276
3277 p = unpack_varlen_hex (++p2, &val);
e5a873b7 3278 ts->stop_reason = trace_stop_command;
00bf0b85 3279 }
33da3f1c
SS
3280 else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0)
3281 {
3282 p = unpack_varlen_hex (++p1, &val);
3283 ts->stop_reason = trace_disconnected;
3284 }
6c28cbf2
SS
3285 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
3286 {
3287 p2 = strchr (++p1, ':');
3288 if (p2 != p1)
3289 {
224c3ddb 3290 ts->stop_desc = (char *) xmalloc ((p2 - p1) / 2 + 1);
bc20a4af 3291 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
f196051f 3292 ts->stop_desc[end] = '\0';
6c28cbf2 3293 }
a609a0c8 3294 else
f196051f 3295 ts->stop_desc = xstrdup ("");
a609a0c8 3296
6c28cbf2
SS
3297 p = unpack_varlen_hex (++p2, &val);
3298 ts->stopping_tracepoint = val;
3299 ts->stop_reason = tracepoint_error;
3300 }
4daf5ac0 3301 else if (strncmp (p, "tframes", p1 - p) == 0)
00bf0b85
SS
3302 {
3303 p = unpack_varlen_hex (++p1, &val);
3304 ts->traceframe_count = val;
3305 }
4daf5ac0
SS
3306 else if (strncmp (p, "tcreated", p1 - p) == 0)
3307 {
3308 p = unpack_varlen_hex (++p1, &val);
3309 ts->traceframes_created = val;
3310 }
3311 else if (strncmp (p, "tfree", p1 - p) == 0)
00bf0b85
SS
3312 {
3313 p = unpack_varlen_hex (++p1, &val);
3314 ts->buffer_free = val;
3315 }
4daf5ac0
SS
3316 else if (strncmp (p, "tsize", p1 - p) == 0)
3317 {
3318 p = unpack_varlen_hex (++p1, &val);
3319 ts->buffer_size = val;
3320 }
33da3f1c
SS
3321 else if (strncmp (p, "disconn", p1 - p) == 0)
3322 {
3323 p = unpack_varlen_hex (++p1, &val);
3324 ts->disconnected_tracing = val;
3325 }
3326 else if (strncmp (p, "circular", p1 - p) == 0)
3327 {
3328 p = unpack_varlen_hex (++p1, &val);
3329 ts->circular_buffer = val;
3330 }
f196051f
SS
3331 else if (strncmp (p, "starttime", p1 - p) == 0)
3332 {
3333 p = unpack_varlen_hex (++p1, &val);
3334 ts->start_time = val;
3335 }
3336 else if (strncmp (p, "stoptime", p1 - p) == 0)
3337 {
3338 p = unpack_varlen_hex (++p1, &val);
3339 ts->stop_time = val;
3340 }
3341 else if (strncmp (p, "username", p1 - p) == 0)
3342 {
3343 ++p1;
224c3ddb 3344 ts->user_name = (char *) xmalloc (strlen (p) / 2);
bc20a4af 3345 end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2);
f196051f
SS
3346 ts->user_name[end] = '\0';
3347 p = p3;
3348 }
3349 else if (strncmp (p, "notes", p1 - p) == 0)
3350 {
3351 ++p1;
224c3ddb 3352 ts->notes = (char *) xmalloc (strlen (p) / 2);
bc20a4af 3353 end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2);
f196051f
SS
3354 ts->notes[end] = '\0';
3355 p = p3;
3356 }
00bf0b85
SS
3357 else
3358 {
3359 /* Silently skip unknown optional info. */
3360 p_temp = strchr (p1 + 1, ';');
3361 if (p_temp)
3362 p = p_temp;
3363 else
3364 /* Must be at the end. */
3365 break;
3366 }
3367 }
3368}
3369
f196051f 3370void
01bccc56 3371parse_tracepoint_status (const char *p, tracepoint *tp,
f196051f
SS
3372 struct uploaded_tp *utp)
3373{
3374 ULONGEST uval;
f196051f
SS
3375
3376 p = unpack_varlen_hex (p, &uval);
3377 if (tp)
c1fc2657 3378 tp->hit_count += uval;
f196051f
SS
3379 else
3380 utp->hit_count += uval;
3381 p = unpack_varlen_hex (p + 1, &uval);
3382 if (tp)
3383 tp->traceframe_usage += uval;
3384 else
3385 utp->traceframe_usage += uval;
3386 /* Ignore any extra, allowing for future extensions. */
3387}
3388
409873ef
SS
3389/* Given a line of text defining a part of a tracepoint, parse it into
3390 an "uploaded tracepoint". */
00bf0b85
SS
3391
3392void
256642e8 3393parse_tracepoint_definition (const char *line, struct uploaded_tp **utpp)
00bf0b85 3394{
256642e8 3395 const char *p;
00bf0b85 3396 char piece;
409873ef 3397 ULONGEST num, addr, step, pass, orig_size, xlen, start;
2a2287c7 3398 int enabled, end;
00bf0b85 3399 enum bptype type;
256642e8 3400 const char *srctype;
67aa1f3c 3401 char *buf;
00bf0b85
SS
3402 struct uploaded_tp *utp = NULL;
3403
3404 p = line;
3405 /* Both tracepoint and action definitions start with the same number
3406 and address sequence. */
3407 piece = *p++;
3408 p = unpack_varlen_hex (p, &num);
3409 p++; /* skip a colon */
3410 p = unpack_varlen_hex (p, &addr);
3411 p++; /* skip a colon */
3412 if (piece == 'T')
3413 {
67aa1f3c
PA
3414 gdb::unique_xmalloc_ptr<char[]> cond;
3415
00bf0b85
SS
3416 enabled = (*p++ == 'E');
3417 p++; /* skip a colon */
3418 p = unpack_varlen_hex (p, &step);
3419 p++; /* skip a colon */
3420 p = unpack_varlen_hex (p, &pass);
3421 type = bp_tracepoint;
00bf0b85
SS
3422 /* Thumb through optional fields. */
3423 while (*p == ':')
3424 {
3425 p++; /* skip a colon */
3426 if (*p == 'F')
3427 {
3428 type = bp_fast_tracepoint;
3429 p++;
3430 p = unpack_varlen_hex (p, &orig_size);
3431 }
0fb4aa4b
PA
3432 else if (*p == 'S')
3433 {
3434 type = bp_static_tracepoint;
3435 p++;
3436 }
00bf0b85
SS
3437 else if (*p == 'X')
3438 {
3439 p++;
3440 p = unpack_varlen_hex (p, &xlen);
3441 p++; /* skip a comma */
67aa1f3c
PA
3442 cond.reset ((char *) xmalloc (2 * xlen + 1));
3443 strncpy (&cond[0], p, 2 * xlen);
00bf0b85
SS
3444 cond[2 * xlen] = '\0';
3445 p += 2 * xlen;
3446 }
3447 else
3e43a32a
MS
3448 warning (_("Unrecognized char '%c' in tracepoint "
3449 "definition, skipping rest"), *p);
00bf0b85
SS
3450 }
3451 utp = get_uploaded_tp (num, addr, utpp);
3452 utp->type = type;
3453 utp->enabled = enabled;
3454 utp->step = step;
3455 utp->pass = pass;
67aa1f3c 3456 utp->cond = std::move (cond);
00bf0b85
SS
3457 }
3458 else if (piece == 'A')
3459 {
3460 utp = get_uploaded_tp (num, addr, utpp);
67aa1f3c 3461 utp->actions.emplace_back (xstrdup (p));
00bf0b85
SS
3462 }
3463 else if (piece == 'S')
3464 {
3465 utp = get_uploaded_tp (num, addr, utpp);
67aa1f3c 3466 utp->step_actions.emplace_back (xstrdup (p));
00bf0b85 3467 }
409873ef
SS
3468 else if (piece == 'Z')
3469 {
3470 /* Parse a chunk of source form definition. */
3471 utp = get_uploaded_tp (num, addr, utpp);
3472 srctype = p;
3473 p = strchr (p, ':');
3474 p++; /* skip a colon */
3475 p = unpack_varlen_hex (p, &start);
3476 p++; /* skip a colon */
3477 p = unpack_varlen_hex (p, &xlen);
3478 p++; /* skip a colon */
3479
224c3ddb 3480 buf = (char *) alloca (strlen (line));
409873ef
SS
3481
3482 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3483 buf[end] = '\0';
3484
61012eef 3485 if (startswith (srctype, "at:"))
67aa1f3c 3486 utp->at_string.reset (xstrdup (buf));
61012eef 3487 else if (startswith (srctype, "cond:"))
67aa1f3c 3488 utp->cond_string.reset (xstrdup (buf));
61012eef 3489 else if (startswith (srctype, "cmd:"))
67aa1f3c 3490 utp->cmd_strings.emplace_back (xstrdup (buf));
409873ef 3491 }
f196051f
SS
3492 else if (piece == 'V')
3493 {
3494 utp = get_uploaded_tp (num, addr, utpp);
3495
3496 parse_tracepoint_status (p, NULL, utp);
3497 }
00bf0b85
SS
3498 else
3499 {
409873ef
SS
3500 /* Don't error out, the target might be sending us optional
3501 info that we don't care about. */
3502 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
00bf0b85
SS
3503 }
3504}
3505
3506/* Convert a textual description of a trace state variable into an
3507 uploaded object. */
3508
3509void
256642e8 3510parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp)
00bf0b85 3511{
256642e8
PA
3512 const char *p;
3513 char *buf;
00bf0b85
SS
3514 ULONGEST num, initval, builtin;
3515 int end;
3516 struct uploaded_tsv *utsv = NULL;
3517
224c3ddb 3518 buf = (char *) alloca (strlen (line));
00bf0b85
SS
3519
3520 p = line;
3521 p = unpack_varlen_hex (p, &num);
3522 p++; /* skip a colon */
3523 p = unpack_varlen_hex (p, &initval);
3524 p++; /* skip a colon */
3525 p = unpack_varlen_hex (p, &builtin);
3526 p++; /* skip a colon */
3527 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3528 buf[end] = '\0';
3529
3530 utsv = get_uploaded_tsv (num, utsvp);
3531 utsv->initial_value = initval;
3532 utsv->builtin = builtin;
3533 utsv->name = xstrdup (buf);
3534}
3535
0fb4aa4b
PA
3536/* Given a line of text defining a static tracepoint marker, parse it
3537 into a "static tracepoint marker" object. Throws an error is
3538 parsing fails. If PP is non-null, it points to one past the end of
3539 the parsed marker definition. */
3540
3541void
256642e8 3542parse_static_tracepoint_marker_definition (const char *line, const char **pp,
5d9310c4 3543 static_tracepoint_marker *marker)
0fb4aa4b 3544{
256642e8 3545 const char *p, *endp;
0fb4aa4b 3546 ULONGEST addr;
0fb4aa4b
PA
3547
3548 p = line;
3549 p = unpack_varlen_hex (p, &addr);
3550 p++; /* skip a colon */
3551
99d9c3b9 3552 marker->gdbarch = current_inferior ()->arch ();
0fb4aa4b
PA
3553 marker->address = (CORE_ADDR) addr;
3554
3555 endp = strchr (p, ':');
3556 if (endp == NULL)
74232302 3557 error (_("bad marker definition: %s"), line);
0fb4aa4b 3558
5d9310c4 3559 marker->str_id = hex2str (p, (endp - p) / 2);
0fb4aa4b 3560
5d9310c4
SM
3561 p = endp;
3562 p++; /* skip a colon */
0fb4aa4b 3563
62c222b6
SM
3564 /* This definition may be followed by another one, separated by a comma. */
3565 int hex_len;
3566 endp = strchr (p, ',');
3567 if (endp != nullptr)
3568 hex_len = endp - p;
3569 else
3570 hex_len = strlen (p);
3571
5d9310c4 3572 marker->extra = hex2str (p, hex_len / 2);
0fb4aa4b 3573
5d9310c4 3574 if (pp != nullptr)
62c222b6 3575 *pp = p + hex_len;
0fb4aa4b
PA
3576}
3577
0fb4aa4b
PA
3578/* Print MARKER to gdb_stdout. */
3579
3580static void
3581print_one_static_tracepoint_marker (int count,
5d9310c4 3582 const static_tracepoint_marker &marker)
0fb4aa4b 3583{
0fb4aa4b
PA
3584 struct symbol *sym;
3585
79a45e25 3586 struct ui_out *uiout = current_uiout;
0fb4aa4b 3587
51abb421 3588 symtab_and_line sal;
5d9310c4 3589 sal.pc = marker.address;
0fb4aa4b 3590
f51e0e20
TT
3591 std::vector<breakpoint *> tracepoints
3592 = static_tracepoints_here (marker.address);
0fb4aa4b 3593
a14a62dd 3594 ui_out_emit_tuple tuple_emitter (uiout, "marker");
0fb4aa4b
PA
3595
3596 /* A counter field to help readability. This is not a stable
3597 identifier! */
381befee 3598 uiout->field_signed ("count", count);
0fb4aa4b 3599
8dd8c8d4 3600 uiout->field_string ("marker-id", marker.str_id);
0fb4aa4b 3601
112e8700 3602 uiout->field_fmt ("enabled", "%c",
f51e0e20 3603 !tracepoints.empty () ? 'y' : 'n');
112e8700 3604 uiout->spaces (2);
0fb4aa4b 3605
6c92c339 3606 int wrap_indent = 35;
5d9310c4 3607 if (gdbarch_addr_bit (marker.gdbarch) <= 32)
6c92c339 3608 wrap_indent += 11;
0fb4aa4b 3609 else
6c92c339 3610 wrap_indent += 19;
0fb4aa4b 3611
6c92c339 3612 const char *extra_field_indent = " ";
0fb4aa4b 3613
5d9310c4 3614 uiout->field_core_addr ("addr", marker.gdbarch, marker.address);
0fb4aa4b 3615
5d9310c4
SM
3616 sal = find_pc_line (marker.address, 0);
3617 sym = find_pc_sect_function (marker.address, NULL);
0fb4aa4b
PA
3618 if (sym)
3619 {
112e8700 3620 uiout->text ("in ");
987012b8 3621 uiout->field_string ("func", sym->print_name (),
e43b10e1 3622 function_name_style.style ());
112e8700
SM
3623 uiout->wrap_hint (wrap_indent);
3624 uiout->text (" at ");
0fb4aa4b
PA
3625 }
3626 else
112e8700 3627 uiout->field_skip ("func");
0fb4aa4b
PA
3628
3629 if (sal.symtab != NULL)
3630 {
112e8700 3631 uiout->field_string ("file",
cbe56571 3632 symtab_to_filename_for_display (sal.symtab),
e43b10e1 3633 file_name_style.style ());
112e8700 3634 uiout->text (":");
0fb4aa4b 3635
112e8700 3636 if (uiout->is_mi_like_p ())
0fb4aa4b 3637 {
0b0865da 3638 const char *fullname = symtab_to_fullname (sal.symtab);
0fb4aa4b 3639
112e8700 3640 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
3641 }
3642 else
112e8700 3643 uiout->field_skip ("fullname");
0fb4aa4b 3644
381befee 3645 uiout->field_signed ("line", sal.line);
0fb4aa4b
PA
3646 }
3647 else
3648 {
112e8700
SM
3649 uiout->field_skip ("fullname");
3650 uiout->field_skip ("line");
0fb4aa4b
PA
3651 }
3652
112e8700
SM
3653 uiout->text ("\n");
3654 uiout->text (extra_field_indent);
3655 uiout->text (_("Data: \""));
8dd8c8d4 3656 uiout->field_string ("extra-data", marker.extra);
112e8700 3657 uiout->text ("\"\n");
0fb4aa4b 3658
f51e0e20 3659 if (!tracepoints.empty ())
0fb4aa4b 3660 {
0fb4aa4b 3661 int ix;
0fb4aa4b 3662
a14a62dd 3663 {
b926417a 3664 ui_out_emit_tuple inner_tuple_emitter (uiout, "tracepoints-at");
a14a62dd
TT
3665
3666 uiout->text (extra_field_indent);
3667 uiout->text (_("Probed by static tracepoints: "));
f51e0e20 3668 for (ix = 0; ix < tracepoints.size (); ix++)
a14a62dd
TT
3669 {
3670 if (ix > 0)
3671 uiout->text (", ");
3672 uiout->text ("#");
381befee 3673 uiout->field_signed ("tracepoint-id", tracepoints[ix]->number);
a14a62dd
TT
3674 }
3675 }
0fb4aa4b 3676
112e8700 3677 if (uiout->is_mi_like_p ())
381befee 3678 uiout->field_signed ("number-of-tracepoints", tracepoints.size ());
0fb4aa4b 3679 else
112e8700 3680 uiout->text ("\n");
0fb4aa4b 3681 }
0fb4aa4b
PA
3682}
3683
3684static void
1d12d88f 3685info_static_tracepoint_markers_command (const char *arg, int from_tty)
0fb4aa4b 3686{
79a45e25 3687 struct ui_out *uiout = current_uiout;
5d9310c4
SM
3688 std::vector<static_tracepoint_marker> markers
3689 = target_static_tracepoint_markers_by_strid (NULL);
0fb4aa4b 3690
d1feda86
YQ
3691 /* We don't have to check target_can_use_agent and agent's capability on
3692 static tracepoint here, in order to be compatible with older GDBserver.
3693 We don't check USE_AGENT is true or not, because static tracepoints
3694 don't work without in-process agent, so we don't bother users to type
3695 `set agent on' when to use static tracepoint. */
3696
4a2b031d
TT
3697 ui_out_emit_table table_emitter (uiout, 5, -1,
3698 "StaticTracepointMarkersTable");
0fb4aa4b 3699
112e8700 3700 uiout->table_header (7, ui_left, "counter", "Cnt");
0fb4aa4b 3701
112e8700 3702 uiout->table_header (40, ui_left, "marker-id", "ID");
0fb4aa4b 3703
112e8700 3704 uiout->table_header (3, ui_left, "enabled", "Enb");
99d9c3b9 3705 if (gdbarch_addr_bit (current_inferior ()->arch ()) <= 32)
112e8700 3706 uiout->table_header (10, ui_left, "addr", "Address");
0fb4aa4b 3707 else
112e8700
SM
3708 uiout->table_header (18, ui_left, "addr", "Address");
3709 uiout->table_header (40, ui_noalign, "what", "What");
0fb4aa4b 3710
112e8700 3711 uiout->table_body ();
0fb4aa4b 3712
5d9310c4
SM
3713 for (int i = 0; i < markers.size (); i++)
3714 print_one_static_tracepoint_marker (i + 1, markers[i]);
0fb4aa4b
PA
3715}
3716
3717/* The $_sdata convenience variable is a bit special. We don't know
3718 for sure type of the value until we actually have a chance to fetch
3719 the data --- the size of the object depends on what has been
3720 collected. We solve this by making $_sdata be an internalvar that
3721 creates a new value on access. */
3722
3723/* Return a new value with the correct type for the sdata object of
3724 the current trace frame. Return a void value if there's no object
3725 available. */
3726
3727static struct value *
22d2b532
SDJ
3728sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var,
3729 void *ignore)
0fb4aa4b 3730{
0fb4aa4b 3731 /* We need to read the whole object before we know its size. */
6b09f134 3732 std::optional<gdb::byte_vector> buf
328d42d8
SM
3733 = target_read_alloc (current_inferior ()->top_target (),
3734 TARGET_OBJECT_STATIC_TRACE_DATA,
9018be22
SM
3735 NULL);
3736 if (buf)
0fb4aa4b
PA
3737 {
3738 struct value *v;
3739 struct type *type;
3740
3741 type = init_vector_type (builtin_type (gdbarch)->builtin_true_char,
9018be22 3742 buf->size ());
317c3ed9 3743 v = value::allocate (type);
bbe912ba 3744 memcpy (v->contents_raw ().data (), buf->data (), buf->size ());
0fb4aa4b
PA
3745 return v;
3746 }
3747 else
317c3ed9 3748 return value::allocate (builtin_type (gdbarch)->builtin_void);
0fb4aa4b
PA
3749}
3750
b3b9301e
PA
3751#if !defined(HAVE_LIBEXPAT)
3752
86766165 3753struct std::unique_ptr<traceframe_info>
b3b9301e
PA
3754parse_traceframe_info (const char *tframe_info)
3755{
3756 static int have_warned;
3757
3758 if (!have_warned)
3759 {
3760 have_warned = 1;
3761 warning (_("Can not parse XML trace frame info; XML support "
3762 "was disabled at compile time"));
3763 }
3764
3765 return NULL;
3766}
3767
3768#else /* HAVE_LIBEXPAT */
3769
3770#include "xml-support.h"
3771
3772/* Handle the start of a <memory> element. */
3773
3774static void
3775traceframe_info_start_memory (struct gdb_xml_parser *parser,
3776 const struct gdb_xml_element *element,
4d0fdd9b
SM
3777 void *user_data,
3778 std::vector<gdb_xml_value> &attributes)
b3b9301e 3779{
19ba03f4 3780 struct traceframe_info *info = (struct traceframe_info *) user_data;
b3b9301e
PA
3781 ULONGEST *start_p, *length_p;
3782
19ba03f4 3783 start_p
4d0fdd9b 3784 = (ULONGEST *) xml_find_attribute (attributes, "start")->value.get ();
19ba03f4 3785 length_p
4d0fdd9b 3786 = (ULONGEST *) xml_find_attribute (attributes, "length")->value.get ();
b3b9301e 3787
4cdd21a8 3788 info->memory.emplace_back (*start_p, *length_p);
b3b9301e
PA
3789}
3790
28a93511
YQ
3791/* Handle the start of a <tvar> element. */
3792
3793static void
3794traceframe_info_start_tvar (struct gdb_xml_parser *parser,
3795 const struct gdb_xml_element *element,
3796 void *user_data,
4d0fdd9b 3797 std::vector<gdb_xml_value> &attributes)
28a93511 3798{
19ba03f4
SM
3799 struct traceframe_info *info = (struct traceframe_info *) user_data;
3800 const char *id_attrib
4d0fdd9b 3801 = (const char *) xml_find_attribute (attributes, "id")->value.get ();
28a93511
YQ
3802 int id = gdb_xml_parse_ulongest (parser, id_attrib);
3803
d0d292a2 3804 info->tvars.push_back (id);
28a93511
YQ
3805}
3806
b3b9301e
PA
3807/* The allowed elements and attributes for an XML memory map. */
3808
3809static const struct gdb_xml_attribute memory_attributes[] = {
3810 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
3811 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
3812 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3813};
3814
28a93511
YQ
3815static const struct gdb_xml_attribute tvar_attributes[] = {
3816 { "id", GDB_XML_AF_NONE, NULL, NULL },
3817 { NULL, GDB_XML_AF_NONE, NULL, NULL }
3818};
3819
b3b9301e
PA
3820static const struct gdb_xml_element traceframe_info_children[] = {
3821 { "memory", memory_attributes, NULL,
3822 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3823 traceframe_info_start_memory, NULL },
28a93511
YQ
3824 { "tvar", tvar_attributes, NULL,
3825 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
3826 traceframe_info_start_tvar, NULL },
b3b9301e
PA
3827 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3828};
3829
3830static const struct gdb_xml_element traceframe_info_elements[] = {
3831 { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE,
3832 NULL, NULL },
3833 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
3834};
3835
3836/* Parse a traceframe-info XML document. */
3837
2098b393 3838traceframe_info_up
b3b9301e
PA
3839parse_traceframe_info (const char *tframe_info)
3840{
2098b393 3841 traceframe_info_up result (new traceframe_info);
b3b9301e
PA
3842
3843 if (gdb_xml_parse_quick (_("trace frame info"),
3844 "traceframe-info.dtd", traceframe_info_elements,
2098b393
SM
3845 tframe_info, result.get ()) == 0)
3846 return result;
b3b9301e 3847
b3b9301e
PA
3848 return NULL;
3849}
3850
3851#endif /* HAVE_LIBEXPAT */
3852
3853/* Returns the traceframe_info object for the current traceframe.
3854 This is where we avoid re-fetching the object from the target if we
3855 already have it cached. */
3856
dc673c81 3857struct traceframe_info *
b3b9301e
PA
3858get_traceframe_info (void)
3859{
8d3c73ef
SM
3860 if (current_traceframe_info == NULL)
3861 current_traceframe_info = target_traceframe_info ();
b3b9301e 3862
2098b393 3863 return current_traceframe_info.get ();
b3b9301e
PA
3864}
3865
c0f61f9c
PA
3866/* If the target supports the query, return in RESULT the set of
3867 collected memory in the current traceframe, found within the LEN
3868 bytes range starting at MEMADDR. Returns true if the target
3869 supports the query, otherwise returns false, and RESULT is left
3870 undefined. */
2a7498d8
PA
3871
3872int
a79b1bc6 3873traceframe_available_memory (std::vector<mem_range> *result,
2a7498d8
PA
3874 CORE_ADDR memaddr, ULONGEST len)
3875{
3876 struct traceframe_info *info = get_traceframe_info ();
3877
3878 if (info != NULL)
3879 {
a79b1bc6 3880 result->clear ();
2a7498d8 3881
4cdd21a8
SM
3882 for (mem_range &r : info->memory)
3883 if (mem_ranges_overlap (r.start, r.length, memaddr, len))
2a7498d8
PA
3884 {
3885 ULONGEST lo1, hi1, lo2, hi2;
2a7498d8
PA
3886
3887 lo1 = memaddr;
3888 hi1 = memaddr + len;
3889
4cdd21a8
SM
3890 lo2 = r.start;
3891 hi2 = r.start + r.length;
2a7498d8 3892
a79b1bc6
SM
3893 CORE_ADDR start = std::max (lo1, lo2);
3894 int length = std::min (hi1, hi2) - start;
2a7498d8 3895
a79b1bc6 3896 result->emplace_back (start, length);
2a7498d8
PA
3897 }
3898
a79b1bc6 3899 normalize_mem_ranges (result);
2a7498d8
PA
3900 return 1;
3901 }
3902
3903 return 0;
3904}
3905
22d2b532
SDJ
3906/* Implementation of `sdata' variable. */
3907
3908static const struct internalvar_funcs sdata_funcs =
3909{
3910 sdata_make_value,
22d2b532
SDJ
3911 NULL
3912};
3913
8588b356
SM
3914/* See tracepoint.h. */
3915cmd_list_element *while_stepping_cmd_element = nullptr;
3916
c906108c 3917/* module initialization */
6c265988 3918void _initialize_tracepoint ();
c906108c 3919void
6c265988 3920_initialize_tracepoint ()
c906108c 3921{
fa58ee11
EZ
3922 struct cmd_list_element *c;
3923
0fb4aa4b
PA
3924 /* Explicitly create without lookup, since that tries to create a
3925 value with a void typed value, and when we get here, gdbarch
3926 isn't initialized yet. At this point, we're quite sure there
3927 isn't another convenience variable of the same name. */
22d2b532 3928 create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL);
0fb4aa4b 3929
c906108c
SS
3930 traceframe_number = -1;
3931 tracepoint_number = -1;
3932
11db9430 3933 add_info ("scope", info_scope_command,
590042fc 3934 _("List the variables local to a scope."));
c906108c 3935
0450cc4c 3936 add_cmd ("tracepoints", class_trace,
1a966eab 3937 _("Tracing of program execution without stopping the program."),
c906108c
SS
3938 &cmdlist);
3939
e5a873b7 3940 add_com ("tdump", class_trace, tdump_command,
1bedd215 3941 _("Print everything collected at the current tracepoint."));
c906108c 3942
f61e138d
SS
3943 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
3944Define a trace state variable.\n\
3945Argument is a $-prefixed name, optionally followed\n\
3946by '=' and an expression that sets the initial value\n\
3947at the start of tracing."));
3948 set_cmd_completer (c, expression_completer);
3949
3950 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
3951Delete one or more trace state variables.\n\
3952Arguments are the names of the variables to delete.\n\
3953If no arguments are supplied, delete all variables."), &deletelist);
c378eb4e 3954 /* FIXME add a trace variable completer. */
f61e138d 3955
11db9430 3956 add_info ("tvariables", info_tvariables_command, _("\
89549d7f 3957Status of trace state variables and their values."));
0fb4aa4b
PA
3958
3959 add_info ("static-tracepoint-markers",
3960 info_static_tracepoint_markers_command, _("\
89549d7f 3961List target static tracepoints markers."));
f61e138d 3962
e5a873b7 3963 add_prefix_cmd ("tfind", class_trace, tfind_command, _("\
590042fc 3964Select a trace frame.\n\
1bedd215 3965No argument means forward by one frame; '-' means backward by one frame."),
2f822da5 3966 &tfindlist, 1, &cmdlist);
c906108c 3967
e5a873b7 3968 add_cmd ("outside", class_trace, tfind_outside_command, _("\
081dfbf7 3969Select a trace frame whose PC is outside the given range (exclusive).\n\
65e65158 3970Usage: tfind outside ADDR1, ADDR2"),
c906108c
SS
3971 &tfindlist);
3972
e5a873b7 3973 add_cmd ("range", class_trace, tfind_range_command, _("\
081dfbf7 3974Select a trace frame whose PC is in the given range (inclusive).\n\
65e65158 3975Usage: tfind range ADDR1, ADDR2"),
c906108c
SS
3976 &tfindlist);
3977
e5a873b7 3978 add_cmd ("line", class_trace, tfind_line_command, _("\
1a966eab 3979Select a trace frame by source line.\n\
cce7e648 3980Argument can be a line number (with optional source file),\n\
c906108c 3981a function name, or '*' followed by an address.\n\
1a966eab 3982Default argument is 'the next source line that was traced'."),
c906108c
SS
3983 &tfindlist);
3984
e5a873b7 3985 add_cmd ("tracepoint", class_trace, tfind_tracepoint_command, _("\
1a966eab
AC
3986Select a trace frame by tracepoint number.\n\
3987Default is the tracepoint for the current trace frame."),
c906108c
SS
3988 &tfindlist);
3989
e5a873b7 3990 add_cmd ("pc", class_trace, tfind_pc_command, _("\
1a966eab
AC
3991Select a trace frame by PC.\n\
3992Default is the current PC, or the PC of the current trace frame."),
c906108c
SS
3993 &tfindlist);
3994
5e84b7ee
SM
3995 cmd_list_element *tfind_end_cmd
3996 = add_cmd ("end", class_trace, tfind_end_command, _("\
3997De-select any trace frame and resume 'live' debugging."), &tfindlist);
c906108c 3998
5e84b7ee 3999 add_alias_cmd ("none", tfind_end_cmd, class_trace, 0, &tfindlist);
c906108c 4000
e5a873b7 4001 add_cmd ("start", class_trace, tfind_start_command,
1a966eab 4002 _("Select the first trace frame in the trace buffer."),
c906108c
SS
4003 &tfindlist);
4004
e5a873b7 4005 add_com ("tstatus", class_trace, tstatus_command,
1bedd215 4006 _("Display the status of the current trace data collection."));
c906108c 4007
e5a873b7 4008 add_com ("tstop", class_trace, tstop_command, _("\
f196051f 4009Stop trace data collection.\n\
02d016b7 4010Usage: tstop [NOTES]...\n\
f196051f
SS
4011Any arguments supplied are recorded with the trace as a stop reason and\n\
4012reported by tstatus (if the target supports trace notes)."));
c906108c 4013
e5a873b7 4014 add_com ("tstart", class_trace, tstart_command, _("\
f196051f 4015Start trace data collection.\n\
02d016b7 4016Usage: tstart [NOTES]...\n\
f196051f
SS
4017Any arguments supplied are recorded with the trace as a note and\n\
4018reported by tstatus (if the target supports trace notes)."));
c906108c 4019
1bedd215
AC
4020 add_com ("end", class_trace, end_actions_pseudocommand, _("\
4021Ends a list of commands or actions.\n\
c906108c
SS
4022Several GDB commands allow you to enter a list of commands or actions.\n\
4023Entering \"end\" on a line by itself is the normal way to terminate\n\
4024such a list.\n\n\
1bedd215 4025Note: the \"end\" command cannot be used at the gdb prompt."));
c906108c 4026
8588b356
SM
4027 while_stepping_cmd_element = add_com ("while-stepping", class_trace,
4028 while_stepping_pseudocommand, _("\
1bedd215 4029Specify single-stepping behavior at a tracepoint.\n\
c906108c
SS
4030Argument is number of instructions to trace in single-step mode\n\
4031following the tracepoint. This command is normally followed by\n\
4032one or more \"collect\" commands, to specify what to collect\n\
4033while single-stepping.\n\n\
1bedd215 4034Note: this command can only be used in a tracepoint \"actions\" list."));
c906108c 4035
3947f654
SM
4036 add_com_alias ("ws", while_stepping_cmd_element, class_trace, 0);
4037 add_com_alias ("stepping", while_stepping_cmd_element, class_trace, 0);
c906108c 4038
1bedd215
AC
4039 add_com ("collect", class_trace, collect_pseudocommand, _("\
4040Specify one or more data items to be collected at a tracepoint.\n\
c906108c
SS
4041Accepts a comma-separated list of (one or more) expressions. GDB will\n\
4042collect all data (variables, registers) referenced by that expression.\n\
4043Also accepts the following special arguments:\n\
4044 $regs -- all registers.\n\
4045 $args -- all function arguments.\n\
4046 $locals -- all variables local to the block/function scope.\n\
0fb4aa4b 4047 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
1bedd215 4048Note: this command can only be used in a tracepoint \"actions\" list."));
c906108c 4049
6da95a67
SS
4050 add_com ("teval", class_trace, teval_pseudocommand, _("\
4051Specify one or more expressions to be evaluated at a tracepoint.\n\
4052Accepts a comma-separated list of (one or more) expressions.\n\
4053The result of each evaluation will be discarded.\n\
4054Note: this command can only be used in a tracepoint \"actions\" list."));
4055
e5a873b7 4056 add_com ("actions", class_trace, actions_command, _("\
1bedd215 4057Specify the actions to be taken at a tracepoint.\n\
cce7e648
PA
4058Tracepoint actions may include collecting of specified data,\n\
4059single-stepping, or enabling/disabling other tracepoints,\n\
1bedd215 4060depending on target's capabilities."));
c906108c 4061
236f1d4d
SS
4062 add_setshow_string_cmd ("default-collect", class_trace,
4063 &default_collect, _("\
590042fc
PW
4064Set the list of expressions to collect by default."), _("\
4065Show the list of expressions to collect by default."), NULL,
236f1d4d
SS
4066 NULL, NULL,
4067 &setlist, &showlist);
4068
d5551862
SS
4069 add_setshow_boolean_cmd ("disconnected-tracing", no_class,
4070 &disconnected_tracing, _("\
4071Set whether tracing continues after GDB disconnects."), _("\
4072Show whether tracing continues after GDB disconnects."), _("\
4073Use this to continue a tracing run even if GDB disconnects\n\
4074or detaches from the target. You can reconnect later and look at\n\
4075trace data collected in the meantime."),
4076 set_disconnected_tracing,
4077 NULL,
4078 &setlist,
4079 &showlist);
00bf0b85 4080
4daf5ac0
SS
4081 add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
4082 &circular_trace_buffer, _("\
4083Set target's use of circular trace buffer."), _("\
4084Show target's use of circular trace buffer."), _("\
4085Use this to make the trace buffer into a circular buffer,\n\
4086which will discard traceframes (oldest first) instead of filling\n\
4087up and stopping the trace run."),
4088 set_circular_trace_buffer,
4089 NULL,
4090 &setlist,
4091 &showlist);
4092
f6f899bf 4093 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class,
9b67fcec 4094 &trace_buffer_size, _("\
f6f899bf
HAQ
4095Set requested size of trace buffer."), _("\
4096Show requested size of trace buffer."), _("\
4097Use this to choose a size for the trace buffer. Some targets\n\
f81d1120
PA
4098may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
4099disables any attempt to set the buffer size and lets the target choose."),
9b67fcec
YQ
4100 set_trace_buffer_size, NULL,
4101 &setlist, &showlist);
f6f899bf 4102
f196051f
SS
4103 add_setshow_string_cmd ("trace-user", class_trace,
4104 &trace_user, _("\
590042fc
PW
4105Set the user name to use for current and future trace runs."), _("\
4106Show the user name to use for current and future trace runs."), NULL,
f196051f
SS
4107 set_trace_user, NULL,
4108 &setlist, &showlist);
4109
4110 add_setshow_string_cmd ("trace-notes", class_trace,
4111 &trace_notes, _("\
590042fc
PW
4112Set notes string to use for current and future trace runs."), _("\
4113Show the notes string to use for current and future trace runs."), NULL,
f196051f
SS
4114 set_trace_notes, NULL,
4115 &setlist, &showlist);
4116
4117 add_setshow_string_cmd ("trace-stop-notes", class_trace,
4118 &trace_stop_notes, _("\
590042fc
PW
4119Set notes string to use for future tstop commands."), _("\
4120Show the notes string to use for future tstop commands."), NULL,
f196051f
SS
4121 set_trace_stop_notes, NULL,
4122 &setlist, &showlist);
c906108c 4123}