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