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