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