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