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