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