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