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