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