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