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