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