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