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