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