Don't update target_dcache if it is not initialized
[deliverable/binutils-gdb.git] / gdb / tracepoint.c
CommitLineData
c906108c 1/* Tracing functionality for remote targets in custom GDB protocol
9f60d481 2
8acc9f48 3 Copyright (C) 1997-2013 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"
29#include "language.h"
0e9f083f 30#include <string.h>
104c1213 31#include "inferior.h"
1042e4c0 32#include "breakpoint.h"
104c1213 33#include "tracepoint.h"
c5f0f3d0 34#include "linespec.h"
4e052eda 35#include "regcache.h"
c94fdfd0 36#include "completer.h"
fe898f56 37#include "block.h"
de4f826b 38#include "dictionary.h"
383f836e 39#include "observer.h"
029a67e4 40#include "user-regs.h"
79a45b7d 41#include "valprint.h"
41575630 42#include "gdbcore.h"
768a979c 43#include "objfiles.h"
35b1e5cc 44#include "filenames.h"
00bf0b85 45#include "gdbthread.h"
2c58c0a9 46#include "stack.h"
fce3c1f0 47#include "gdbcore.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"
9852c492 57#include "completer.h"
614c279d 58#include "filestuff.h"
c906108c
SS
59
60/* readline include files */
dbda9972
AC
61#include "readline/readline.h"
62#include "readline/history.h"
c906108c
SS
63
64/* readline defines this. */
65#undef savestring
66
c906108c 67#include <unistd.h>
c906108c 68
00bf0b85
SS
69#ifndef O_LARGEFILE
70#define O_LARGEFILE 0
71#endif
72
d183932d
MS
73/* Maximum length of an agent aexpression.
74 This accounts for the fact that packets are limited to 400 bytes
c906108c
SS
75 (which includes everything -- including the checksum), and assumes
76 the worst case of maximum length for each of the pieces of a
77 continuation packet.
c5aa993b 78
c906108c
SS
79 NOTE: expressions get mem2hex'ed otherwise this would be twice as
80 large. (400 - 31)/2 == 184 */
81#define MAX_AGENT_EXPR_LEN 184
82
98c5b216
TT
83/* A hook used to notify the UI of tracepoint operations. */
84
85void (*deprecated_trace_find_hook) (char *arg, int from_tty);
86void (*deprecated_trace_start_stop_hook) (int start, int from_tty);
c906108c 87
c906108c
SS
88/*
89 Tracepoint.c:
90
91 This module defines the following debugger commands:
92 trace : set a tracepoint on a function, line, or address.
93 info trace : list all debugger-defined tracepoints.
94 delete trace : delete one or more tracepoints.
95 enable trace : enable one or more tracepoints.
96 disable trace : disable one or more tracepoints.
97 actions : specify actions to be taken at a tracepoint.
98 passcount : specify a pass count for a tracepoint.
99 tstart : start a trace experiment.
100 tstop : stop a trace experiment.
101 tstatus : query the status of a trace experiment.
102 tfind : find a trace frame in the trace buffer.
103 tdump : print everything collected at the current tracepoint.
104 save-tracepoints : write tracepoint setup into a file.
105
106 This module defines the following user-visible debugger variables:
107 $trace_frame : sequence number of trace frame currently being debugged.
108 $trace_line : source line of trace frame currently being debugged.
109 $trace_file : source file of trace frame currently being debugged.
110 $tracepoint : tracepoint number of trace frame currently being debugged.
c5aa993b 111 */
c906108c
SS
112
113
114/* ======= Important global variables: ======= */
115
f61e138d
SS
116/* The list of all trace state variables. We don't retain pointers to
117 any of these for any reason - API is by name or number only - so it
118 works to have a vector of objects. */
119
120typedef struct trace_state_variable tsv_s;
121DEF_VEC_O(tsv_s);
122
123static VEC(tsv_s) *tvariables;
124
125/* The next integer to assign to a variable. */
126
127static int next_tsv_number = 1;
128
c906108c
SS
129/* Number of last traceframe collected. */
130static int traceframe_number;
131
132/* Tracepoint for last traceframe collected. */
133static int tracepoint_number;
134
b3b9301e
PA
135/* The traceframe info of the current traceframe. NULL if we haven't
136 yet attempted to fetch it, or if the target does not support
137 fetching this object, or if we're not inspecting a traceframe
138 presently. */
139static struct traceframe_info *traceframe_info;
140
c378eb4e 141/* Tracing command lists. */
c906108c
SS
142static struct cmd_list_element *tfindlist;
143
236f1d4d 144/* List of expressions to collect by default at each tracepoint hit. */
35b1e5cc 145char *default_collect = "";
236f1d4d 146
d5551862
SS
147static int disconnected_tracing;
148
4daf5ac0
SS
149/* This variable controls whether we ask the target for a linear or
150 circular trace buffer. */
151
152static int circular_trace_buffer;
153
f6f899bf
HAQ
154/* This variable is the requested trace buffer size, or -1 to indicate
155 that we don't care and leave it up to the target to set a size. */
156
157static int trace_buffer_size = -1;
158
f196051f
SS
159/* Textual notes applying to the current and/or future trace runs. */
160
161char *trace_user = NULL;
162
163/* Textual notes applying to the current and/or future trace runs. */
164
165char *trace_notes = NULL;
166
167/* Textual notes applying to the stopping of a trace. */
168
169char *trace_stop_notes = NULL;
170
c906108c 171/* ======= Important command functions: ======= */
a14ed312
KB
172static void trace_actions_command (char *, int);
173static void trace_start_command (char *, int);
174static void trace_stop_command (char *, int);
175static void trace_status_command (char *, int);
176static void trace_find_command (char *, int);
177static void trace_find_pc_command (char *, int);
178static void trace_find_tracepoint_command (char *, int);
179static void trace_find_line_command (char *, int);
180static void trace_find_range_command (char *, int);
181static void trace_find_outside_command (char *, int);
a14ed312 182static void trace_dump_command (char *, int);
c906108c
SS
183
184/* support routines */
c906108c
SS
185
186struct collection_list;
a14ed312 187static void add_aexpr (struct collection_list *, struct agent_expr *);
47b667de 188static char *mem2hex (gdb_byte *, char *, int);
a14ed312
KB
189static void add_register (struct collection_list *collection,
190 unsigned int regno);
392a587b 191
00bf0b85
SS
192static void free_uploaded_tps (struct uploaded_tp **utpp);
193static void free_uploaded_tsvs (struct uploaded_tsv **utsvp);
194
3ca73e0c
YQ
195static struct command_line *
196 all_tracepoint_actions_and_cleanup (struct breakpoint *t);
00bf0b85 197
a14ed312 198extern void _initialize_tracepoint (void);
c906108c 199
00bf0b85
SS
200static struct trace_status trace_status;
201
202char *stop_reason_names[] = {
203 "tunknown",
204 "tnotrun",
205 "tstop",
206 "tfull",
207 "tdisconnected",
6c28cbf2
SS
208 "tpasscount",
209 "terror"
00bf0b85
SS
210};
211
212struct trace_status *
eeae04df 213current_trace_status (void)
00bf0b85
SS
214{
215 return &trace_status;
216}
217
b3b9301e
PA
218/* Destroy INFO. */
219
220static void
221free_traceframe_info (struct traceframe_info *info)
222{
223 if (info != NULL)
224 {
225 VEC_free (mem_range_s, info->memory);
28a93511 226 VEC_free (int, info->tvars);
b3b9301e
PA
227
228 xfree (info);
229 }
230}
231
7a9dd1b2 232/* Free and clear the traceframe info cache of the current
b3b9301e
PA
233 traceframe. */
234
235static void
236clear_traceframe_info (void)
237{
238 free_traceframe_info (traceframe_info);
239 traceframe_info = NULL;
240}
241
c906108c
SS
242/* Set traceframe number to NUM. */
243static void
fba45db2 244set_traceframe_num (int num)
c906108c
SS
245{
246 traceframe_number = num;
4fa62494 247 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
c906108c
SS
248}
249
250/* Set tracepoint number to NUM. */
251static void
fba45db2 252set_tracepoint_num (int num)
c906108c
SS
253{
254 tracepoint_number = num;
4fa62494 255 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
c906108c
SS
256}
257
258/* Set externally visible debug variables for querying/printing
c378eb4e 259 the traceframe context (line, function, file). */
c906108c
SS
260
261static void
fb14de7b 262set_traceframe_context (struct frame_info *trace_frame)
c906108c 263{
fb14de7b 264 CORE_ADDR trace_pc;
24a00813
YQ
265 struct symbol *traceframe_fun;
266 struct symtab_and_line traceframe_sal;
fb14de7b 267
dba09041
PA
268 /* Save as globals for internal use. */
269 if (trace_frame != NULL
270 && get_frame_pc_if_available (trace_frame, &trace_pc))
271 {
272 traceframe_sal = find_pc_line (trace_pc, 0);
273 traceframe_fun = find_pc_function (trace_pc);
274
275 /* Save linenumber as "$trace_line", a debugger variable visible to
276 users. */
277 set_internalvar_integer (lookup_internalvar ("trace_line"),
278 traceframe_sal.line);
279 }
280 else
c906108c 281 {
dba09041
PA
282 init_sal (&traceframe_sal);
283 traceframe_fun = NULL;
4fa62494 284 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
c906108c
SS
285 }
286
d183932d
MS
287 /* Save func name as "$trace_func", a debugger variable visible to
288 users. */
4fa62494
UW
289 if (traceframe_fun == NULL
290 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
291 clear_internalvar (lookup_internalvar ("trace_func"));
c906108c 292 else
78267919
UW
293 set_internalvar_string (lookup_internalvar ("trace_func"),
294 SYMBOL_LINKAGE_NAME (traceframe_fun));
c906108c 295
d183932d
MS
296 /* Save file name as "$trace_file", a debugger variable visible to
297 users. */
4e04028d 298 if (traceframe_sal.symtab == NULL)
4fa62494 299 clear_internalvar (lookup_internalvar ("trace_file"));
c906108c 300 else
78267919 301 set_internalvar_string (lookup_internalvar ("trace_file"),
05cba821 302 symtab_to_filename_for_display (traceframe_sal.symtab));
c906108c
SS
303}
304
f61e138d
SS
305/* Create a new trace state variable with the given name. */
306
307struct trace_state_variable *
308create_trace_state_variable (const char *name)
309{
310 struct trace_state_variable tsv;
311
312 memset (&tsv, 0, sizeof (tsv));
40e1c229 313 tsv.name = xstrdup (name);
f61e138d
SS
314 tsv.number = next_tsv_number++;
315 return VEC_safe_push (tsv_s, tvariables, &tsv);
316}
317
318/* Look for a trace state variable of the given name. */
319
320struct trace_state_variable *
321find_trace_state_variable (const char *name)
322{
323 struct trace_state_variable *tsv;
324 int ix;
325
326 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
327 if (strcmp (name, tsv->name) == 0)
328 return tsv;
329
330 return NULL;
331}
332
dc673c81
YQ
333/* Look for a trace state variable of the given number. Return NULL if
334 not found. */
335
336struct trace_state_variable *
337find_trace_state_variable_by_number (int number)
338{
339 struct trace_state_variable *tsv;
340 int ix;
341
342 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
343 if (tsv->number == number)
344 return tsv;
345
346 return NULL;
347}
348
70221824 349static void
f61e138d
SS
350delete_trace_state_variable (const char *name)
351{
352 struct trace_state_variable *tsv;
353 int ix;
354
355 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
356 if (strcmp (name, tsv->name) == 0)
357 {
134a2066
YQ
358 observer_notify_tsv_deleted (tsv);
359
40e1c229 360 xfree ((void *)tsv->name);
f61e138d 361 VEC_unordered_remove (tsv_s, tvariables, ix);
bb25a15c 362
f61e138d
SS
363 return;
364 }
365
366 warning (_("No trace variable named \"$%s\", not deleting"), name);
367}
368
1773c82c
HAQ
369/* Throws an error if NAME is not valid syntax for a trace state
370 variable's name. */
371
372void
373validate_trace_state_variable_name (const char *name)
374{
375 const char *p;
376
377 if (*name == '\0')
378 error (_("Must supply a non-empty variable name"));
379
380 /* All digits in the name is reserved for value history
381 references. */
382 for (p = name; isdigit (*p); p++)
383 ;
384 if (*p == '\0')
385 error (_("$%s is not a valid trace state variable name"), name);
386
387 for (p = name; isalnum (*p) || *p == '_'; p++)
388 ;
389 if (*p != '\0')
390 error (_("$%s is not a valid trace state variable name"), name);
391}
392
f61e138d
SS
393/* The 'tvariable' command collects a name and optional expression to
394 evaluate into an initial value. */
395
70221824 396static void
f61e138d
SS
397trace_variable_command (char *args, int from_tty)
398{
f61e138d 399 struct cleanup *old_chain;
f61e138d
SS
400 LONGEST initval = 0;
401 struct trace_state_variable *tsv;
1773c82c 402 char *name, *p;
f61e138d
SS
403
404 if (!args || !*args)
1773c82c
HAQ
405 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
406
407 /* Only allow two syntaxes; "$name" and "$name=value". */
408 p = skip_spaces (args);
f61e138d 409
1773c82c
HAQ
410 if (*p++ != '$')
411 error (_("Name of trace variable should start with '$'"));
f61e138d 412
1773c82c
HAQ
413 name = p;
414 while (isalnum (*p) || *p == '_')
415 p++;
416 name = savestring (name, p - name);
417 old_chain = make_cleanup (xfree, name);
f61e138d 418
1773c82c
HAQ
419 p = skip_spaces (p);
420 if (*p != '=' && *p != '\0')
f61e138d
SS
421 error (_("Syntax must be $NAME [ = EXPR ]"));
422
1773c82c 423 validate_trace_state_variable_name (name);
f61e138d 424
1773c82c
HAQ
425 if (*p == '=')
426 initval = value_as_long (parse_and_eval (++p));
f61e138d
SS
427
428 /* If the variable already exists, just change its initial value. */
1773c82c 429 tsv = find_trace_state_variable (name);
f61e138d
SS
430 if (tsv)
431 {
134a2066
YQ
432 if (tsv->initial_value != initval)
433 {
434 tsv->initial_value = initval;
435 observer_notify_tsv_modified (tsv);
436 }
3e43a32a
MS
437 printf_filtered (_("Trace state variable $%s "
438 "now has initial value %s.\n"),
f61e138d 439 tsv->name, plongest (tsv->initial_value));
f90824dc 440 do_cleanups (old_chain);
f61e138d
SS
441 return;
442 }
443
444 /* Create a new variable. */
1773c82c 445 tsv = create_trace_state_variable (name);
f61e138d
SS
446 tsv->initial_value = initval;
447
134a2066 448 observer_notify_tsv_created (tsv);
bb25a15c 449
3e43a32a
MS
450 printf_filtered (_("Trace state variable $%s "
451 "created, with initial value %s.\n"),
f61e138d
SS
452 tsv->name, plongest (tsv->initial_value));
453
454 do_cleanups (old_chain);
455}
456
70221824 457static void
f61e138d
SS
458delete_trace_variable_command (char *args, int from_tty)
459{
2a2287c7 460 int ix;
f61e138d
SS
461 char **argv;
462 struct cleanup *back_to;
f61e138d
SS
463
464 if (args == NULL)
465 {
466 if (query (_("Delete all trace state variables? ")))
467 VEC_free (tsv_s, tvariables);
468 dont_repeat ();
bb25a15c 469 observer_notify_tsv_deleted (NULL);
f61e138d
SS
470 return;
471 }
472
473 argv = gdb_buildargv (args);
474 back_to = make_cleanup_freeargv (argv);
475
2a2287c7 476 for (ix = 0; argv[ix] != NULL; ix++)
f61e138d 477 {
2a2287c7
MS
478 if (*argv[ix] == '$')
479 delete_trace_state_variable (argv[ix] + 1);
f61e138d 480 else
2a2287c7 481 warning (_("Name \"%s\" not prefixed with '$', ignoring"), argv[ix]);
f61e138d
SS
482 }
483
484 do_cleanups (back_to);
485
486 dont_repeat ();
487}
488
40e1c229
VP
489void
490tvariables_info_1 (void)
f61e138d
SS
491{
492 struct trace_state_variable *tsv;
493 int ix;
40e1c229
VP
494 int count = 0;
495 struct cleanup *back_to;
79a45e25 496 struct ui_out *uiout = current_uiout;
f61e138d 497
40e1c229 498 if (VEC_length (tsv_s, tvariables) == 0 && !ui_out_is_mi_like_p (uiout))
f61e138d
SS
499 {
500 printf_filtered (_("No trace state variables.\n"));
501 return;
502 }
503
35b1e5cc 504 /* Try to acquire values from the target. */
4baf5cf4 505 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix, ++count)
35b1e5cc
SS
506 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
507 &(tsv->value));
508
40e1c229
VP
509 back_to = make_cleanup_ui_out_table_begin_end (uiout, 3,
510 count, "trace-variables");
511 ui_out_table_header (uiout, 15, ui_left, "name", "Name");
512 ui_out_table_header (uiout, 11, ui_left, "initial", "Initial");
513 ui_out_table_header (uiout, 11, ui_left, "current", "Current");
514
515 ui_out_table_body (uiout);
f61e138d
SS
516
517 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
518 {
40e1c229
VP
519 struct cleanup *back_to2;
520 char *c;
521 char *name;
522
523 back_to2 = make_cleanup_ui_out_tuple_begin_end (uiout, "variable");
524
c4f7c687 525 name = concat ("$", tsv->name, (char *) NULL);
40e1c229
VP
526 make_cleanup (xfree, name);
527 ui_out_field_string (uiout, "name", name);
528 ui_out_field_string (uiout, "initial", plongest (tsv->initial_value));
529
f61e138d 530 if (tsv->value_known)
40e1c229
VP
531 c = plongest (tsv->value);
532 else if (ui_out_is_mi_like_p (uiout))
533 /* For MI, we prefer not to use magic string constants, but rather
534 omit the field completely. The difference between unknown and
535 undefined does not seem important enough to represent. */
536 c = NULL;
00bf0b85 537 else if (current_trace_status ()->running || traceframe_number >= 0)
f61e138d 538 /* The value is/was defined, but we don't have it. */
40e1c229 539 c = "<unknown>";
f61e138d
SS
540 else
541 /* It is not meaningful to ask about the value. */
40e1c229
VP
542 c = "<undefined>";
543 if (c)
544 ui_out_field_string (uiout, "current", c);
545 ui_out_text (uiout, "\n");
546
547 do_cleanups (back_to2);
f61e138d 548 }
40e1c229
VP
549
550 do_cleanups (back_to);
551}
552
553/* List all the trace state variables. */
554
555static void
556tvariables_info (char *args, int from_tty)
557{
558 tvariables_info_1 ();
f61e138d
SS
559}
560
8bf6485c
SS
561/* Stash definitions of tsvs into the given file. */
562
563void
564save_trace_state_variables (struct ui_file *fp)
565{
566 struct trace_state_variable *tsv;
567 int ix;
568
569 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
570 {
571 fprintf_unfiltered (fp, "tvariable $%s", tsv->name);
572 if (tsv->initial_value)
573 fprintf_unfiltered (fp, " = %s", plongest (tsv->initial_value));
574 fprintf_unfiltered (fp, "\n");
575 }
576}
577
c906108c
SS
578/* ACTIONS functions: */
579
c906108c 580/* The three functions:
c5aa993b
JM
581 collect_pseudocommand,
582 while_stepping_pseudocommand, and
583 end_actions_pseudocommand
c906108c
SS
584 are placeholders for "commands" that are actually ONLY to be used
585 within a tracepoint action list. If the actual function is ever called,
586 it means that somebody issued the "command" at the top level,
587 which is always an error. */
588
7b3ae3a6 589static void
fba45db2 590end_actions_pseudocommand (char *args, int from_tty)
c906108c 591{
8a3fe4f8 592 error (_("This command cannot be used at the top level."));
c906108c
SS
593}
594
7b3ae3a6 595static void
fba45db2 596while_stepping_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
601static void
fba45db2 602collect_pseudocommand (char *args, int from_tty)
c906108c 603{
8a3fe4f8 604 error (_("This command can only be used in a tracepoint actions list."));
c906108c
SS
605}
606
6da95a67
SS
607static void
608teval_pseudocommand (char *args, int from_tty)
609{
610 error (_("This command can only be used in a tracepoint actions list."));
611}
612
3065dfb6
SS
613/* Parse any collection options, such as /s for strings. */
614
6f937416 615const char *
92bc6a20 616decode_agent_options (const char *exp, int *trace_string)
3065dfb6
SS
617{
618 struct value_print_options opts;
619
92bc6a20
TT
620 *trace_string = 0;
621
3065dfb6
SS
622 if (*exp != '/')
623 return exp;
624
625 /* Call this to borrow the print elements default for collection
626 size. */
627 get_user_print_options (&opts);
628
629 exp++;
630 if (*exp == 's')
631 {
632 if (target_supports_string_tracing ())
633 {
634 /* Allow an optional decimal number giving an explicit maximum
635 string length, defaulting it to the "print elements" value;
636 so "collect/s80 mystr" gets at most 80 bytes of string. */
92bc6a20 637 *trace_string = opts.print_max;
3065dfb6
SS
638 exp++;
639 if (*exp >= '0' && *exp <= '9')
92bc6a20 640 *trace_string = atoi (exp);
3065dfb6
SS
641 while (*exp >= '0' && *exp <= '9')
642 exp++;
643 }
644 else
645 error (_("Target does not support \"/s\" option for string tracing."));
646 }
647 else
648 error (_("Undefined collection format \"%c\"."), *exp);
649
6f937416 650 exp = skip_spaces_const (exp);
3065dfb6
SS
651
652 return exp;
653}
654
c906108c
SS
655/* Enter a list of actions for a tracepoint. */
656static void
fba45db2 657trace_actions_command (char *args, int from_tty)
c906108c 658{
d9b3f62e 659 struct tracepoint *t;
a7bdde9e 660 struct command_line *l;
c906108c 661
197f0a60 662 t = get_tracepoint_by_number (&args, NULL, 1);
7a292a7a 663 if (t)
c906108c 664 {
a7bdde9e
VP
665 char *tmpbuf =
666 xstrprintf ("Enter actions for tracepoint %d, one per line.",
d9b3f62e 667 t->base.number);
a7bdde9e
VP
668 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
669
3e43a32a
MS
670 l = read_command_lines (tmpbuf, from_tty, 1,
671 check_tracepoint_command, t);
a7bdde9e 672 do_cleanups (cleanups);
d9b3f62e 673 breakpoint_set_commands (&t->base, l);
c906108c 674 }
5c44784c 675 /* else just return */
c906108c
SS
676}
677
fff87407
SS
678/* Report the results of checking the agent expression, as errors or
679 internal errors. */
680
681static void
35c9c7ba 682report_agent_reqs_errors (struct agent_expr *aexpr)
fff87407
SS
683{
684 /* All of the "flaws" are serious bytecode generation issues that
685 should never occur. */
35c9c7ba 686 if (aexpr->flaw != agent_flaw_none)
fff87407
SS
687 internal_error (__FILE__, __LINE__, _("expression is malformed"));
688
689 /* If analysis shows a stack underflow, GDB must have done something
690 badly wrong in its bytecode generation. */
35c9c7ba 691 if (aexpr->min_height < 0)
fff87407
SS
692 internal_error (__FILE__, __LINE__,
693 _("expression has min height < 0"));
694
695 /* Issue this error if the stack is predicted to get too deep. The
696 limit is rather arbitrary; a better scheme might be for the
697 target to report how much stack it will have available. The
698 depth roughly corresponds to parenthesization, so a limit of 20
699 amounts to 20 levels of expression nesting, which is actually
700 a pretty big hairy expression. */
35c9c7ba 701 if (aexpr->max_height > 20)
fff87407
SS
702 error (_("Expression is too complicated."));
703}
704
c906108c 705/* worker function */
fff87407 706void
6f937416 707validate_actionline (const char *line, struct breakpoint *b)
c906108c
SS
708{
709 struct cmd_list_element *c;
710 struct expression *exp = NULL;
c906108c 711 struct cleanup *old_chain = NULL;
6f937416
PA
712 const char *tmp_p;
713 const char *p;
9355b391 714 struct bp_location *loc;
fff87407 715 struct agent_expr *aexpr;
d9b3f62e 716 struct tracepoint *t = (struct tracepoint *) b;
c906108c 717
c378eb4e 718 /* If EOF is typed, *line is NULL. */
6f937416 719 if (line == NULL)
fff87407 720 return;
15255275 721
6f937416 722 p = skip_spaces_const (line);
c906108c 723
d183932d
MS
724 /* Symbol lookup etc. */
725 if (*p == '\0') /* empty line: just prompt for another line. */
fff87407 726 return;
c906108c 727
c5aa993b 728 if (*p == '#') /* comment line */
fff87407 729 return;
c906108c
SS
730
731 c = lookup_cmd (&p, cmdlist, "", -1, 1);
732 if (c == 0)
fff87407 733 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
c5aa993b 734
bbaca940 735 if (cmd_cfunc_eq (c, collect_pseudocommand))
c906108c 736 {
92bc6a20
TT
737 int trace_string = 0;
738
3065dfb6 739 if (*p == '/')
92bc6a20 740 p = decode_agent_options (p, &trace_string);
3065dfb6 741
c5aa993b 742 do
c378eb4e
MS
743 { /* Repeat over a comma-separated list. */
744 QUIT; /* Allow user to bail out with ^C. */
6f937416 745 p = skip_spaces_const (p);
c906108c 746
c378eb4e 747 if (*p == '$') /* Look for special pseudo-symbols. */
c5aa993b 748 {
0fb4aa4b
PA
749 if (0 == strncasecmp ("reg", p + 1, 3)
750 || 0 == strncasecmp ("arg", p + 1, 3)
751 || 0 == strncasecmp ("loc", p + 1, 3)
6710bf39 752 || 0 == strncasecmp ("_ret", p + 1, 4)
0fb4aa4b 753 || 0 == strncasecmp ("_sdata", p + 1, 6))
c5aa993b
JM
754 {
755 p = strchr (p, ',');
756 continue;
757 }
d183932d 758 /* else fall thru, treat p as an expression and parse it! */
c5aa993b 759 }
9355b391 760 tmp_p = p;
d9b3f62e 761 for (loc = t->base.loc; loc; loc = loc->next)
c5aa993b 762 {
6f937416
PA
763 p = tmp_p;
764 exp = parse_exp_1 (&p, loc->address,
1bb9788d 765 block_for_pc (loc->address), 1);
9355b391
SS
766 old_chain = make_cleanup (free_current_contents, &exp);
767
768 if (exp->elts[0].opcode == OP_VAR_VALUE)
c5aa993b 769 {
9355b391
SS
770 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
771 {
12df843f 772 error (_("constant `%s' (value %s) "
3e43a32a 773 "will not be collected."),
fff87407 774 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
12df843f 775 plongest (SYMBOL_VALUE (exp->elts[2].symbol)));
9355b391 776 }
3e43a32a
MS
777 else if (SYMBOL_CLASS (exp->elts[2].symbol)
778 == LOC_OPTIMIZED_OUT)
9355b391 779 {
3e43a32a
MS
780 error (_("`%s' is optimized away "
781 "and cannot be collected."),
fff87407 782 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
9355b391 783 }
c5aa993b 784 }
c906108c 785
9355b391
SS
786 /* We have something to collect, make sure that the expr to
787 bytecode translator can handle it and that it's not too
788 long. */
92bc6a20 789 aexpr = gen_trace_for_expr (loc->address, exp, trace_string);
9355b391 790 make_cleanup_free_agent_expr (aexpr);
c906108c 791
9355b391 792 if (aexpr->len > MAX_AGENT_EXPR_LEN)
fff87407 793 error (_("Expression is too complicated."));
c906108c 794
35c9c7ba 795 ax_reqs (aexpr);
c906108c 796
35c9c7ba 797 report_agent_reqs_errors (aexpr);
c906108c 798
9355b391
SS
799 do_cleanups (old_chain);
800 }
c5aa993b
JM
801 }
802 while (p && *p++ == ',');
c906108c 803 }
fff87407 804
6da95a67
SS
805 else if (cmd_cfunc_eq (c, teval_pseudocommand))
806 {
6da95a67 807 do
c378eb4e
MS
808 { /* Repeat over a comma-separated list. */
809 QUIT; /* Allow user to bail out with ^C. */
6f937416 810 p = skip_spaces_const (p);
6da95a67 811
9355b391 812 tmp_p = p;
d9b3f62e 813 for (loc = t->base.loc; loc; loc = loc->next)
9355b391 814 {
6f937416 815 p = tmp_p;
bbc13ae3 816
9355b391 817 /* Only expressions are allowed for this action. */
6f937416 818 exp = parse_exp_1 (&p, loc->address,
1bb9788d 819 block_for_pc (loc->address), 1);
9355b391 820 old_chain = make_cleanup (free_current_contents, &exp);
6da95a67 821
9355b391
SS
822 /* We have something to evaluate, make sure that the expr to
823 bytecode translator can handle it and that it's not too
824 long. */
825 aexpr = gen_eval_for_expr (loc->address, exp);
826 make_cleanup_free_agent_expr (aexpr);
6da95a67 827
9355b391 828 if (aexpr->len > MAX_AGENT_EXPR_LEN)
fff87407
SS
829 error (_("Expression is too complicated."));
830
35c9c7ba
SS
831 ax_reqs (aexpr);
832 report_agent_reqs_errors (aexpr);
6da95a67 833
9355b391
SS
834 do_cleanups (old_chain);
835 }
6da95a67
SS
836 }
837 while (p && *p++ == ',');
6da95a67 838 }
fff87407 839
bbaca940 840 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
c906108c 841 {
6f937416 842 char *endp;
c906108c 843
6f937416
PA
844 p = skip_spaces_const (p);
845 t->step_count = strtol (p, &endp, 0);
846 if (endp == p || t->step_count == 0)
847 error (_("while-stepping step count `%s' is malformed."), line);
848 p = endp;
c906108c 849 }
fff87407 850
bbaca940 851 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
fff87407
SS
852 ;
853
c906108c 854 else
6f937416 855 error (_("`%s' is not a supported tracepoint action."), line);
74b7792f
AC
856}
857
f50e79a4
JB
858enum {
859 memrange_absolute = -1
860};
861
c906108c
SS
862/* MEMRANGE functions: */
863
a14ed312 864static int memrange_cmp (const void *, const void *);
c906108c 865
c378eb4e 866/* Compare memranges for qsort. */
c906108c 867static int
fba45db2 868memrange_cmp (const void *va, const void *vb)
c906108c
SS
869{
870 const struct memrange *a = va, *b = vb;
871
872 if (a->type < b->type)
873 return -1;
874 if (a->type > b->type)
c5aa993b 875 return 1;
f50e79a4 876 if (a->type == memrange_absolute)
c906108c 877 {
c5aa993b
JM
878 if ((bfd_vma) a->start < (bfd_vma) b->start)
879 return -1;
880 if ((bfd_vma) a->start > (bfd_vma) b->start)
881 return 1;
c906108c
SS
882 }
883 else
884 {
c5aa993b 885 if (a->start < b->start)
c906108c 886 return -1;
c5aa993b
JM
887 if (a->start > b->start)
888 return 1;
c906108c
SS
889 }
890 return 0;
891}
892
d183932d 893/* Sort the memrange list using qsort, and merge adjacent memranges. */
c906108c 894static void
fba45db2 895memrange_sortmerge (struct collection_list *memranges)
c906108c
SS
896{
897 int a, b;
898
c5aa993b 899 qsort (memranges->list, memranges->next_memrange,
c906108c
SS
900 sizeof (struct memrange), memrange_cmp);
901 if (memranges->next_memrange > 0)
902 {
903 for (a = 0, b = 1; b < memranges->next_memrange; b++)
904 {
1b28d0b3
PA
905 /* If memrange b overlaps or is adjacent to memrange a,
906 merge them. */
907 if (memranges->list[a].type == memranges->list[b].type
908 && memranges->list[b].start <= memranges->list[a].end)
c906108c 909 {
08807d5a
PA
910 if (memranges->list[b].end > memranges->list[a].end)
911 memranges->list[a].end = memranges->list[b].end;
c906108c
SS
912 continue; /* next b, same a */
913 }
914 a++; /* next a */
915 if (a != b)
c5aa993b 916 memcpy (&memranges->list[a], &memranges->list[b],
c906108c
SS
917 sizeof (struct memrange));
918 }
919 memranges->next_memrange = a + 1;
920 }
921}
922
d183932d 923/* Add a register to a collection list. */
392a587b 924static void
fba45db2 925add_register (struct collection_list *collection, unsigned int regno)
c906108c
SS
926{
927 if (info_verbose)
928 printf_filtered ("collect register %d\n", regno);
27e06d3e 929 if (regno >= (8 * sizeof (collection->regs_mask)))
8a3fe4f8 930 error (_("Internal: register number %d too large for tracepoint"),
c906108c 931 regno);
c5aa993b 932 collection->regs_mask[regno / 8] |= 1 << (regno % 8);
c906108c
SS
933}
934
c378eb4e 935/* Add a memrange to a collection list. */
c906108c 936static void
d183932d
MS
937add_memrange (struct collection_list *memranges,
938 int type, bfd_signed_vma base,
fba45db2 939 unsigned long len)
c906108c
SS
940{
941 if (info_verbose)
104c1213
JM
942 {
943 printf_filtered ("(%d,", type);
944 printf_vma (base);
945 printf_filtered (",%ld)\n", len);
946 }
947
f50e79a4 948 /* type: memrange_absolute == memory, other n == basereg */
c5aa993b 949 memranges->list[memranges->next_memrange].type = type;
d183932d 950 /* base: addr if memory, offset if reg relative. */
c906108c
SS
951 memranges->list[memranges->next_memrange].start = base;
952 /* len: we actually save end (base + len) for convenience */
c5aa993b 953 memranges->list[memranges->next_memrange].end = base + len;
c906108c
SS
954 memranges->next_memrange++;
955 if (memranges->next_memrange >= memranges->listsize)
956 {
957 memranges->listsize *= 2;
c5aa993b 958 memranges->list = xrealloc (memranges->list,
c906108c
SS
959 memranges->listsize);
960 }
961
3e43a32a 962 if (type != memrange_absolute) /* Better collect the base register! */
c906108c
SS
963 add_register (memranges, type);
964}
965
d183932d 966/* Add a symbol to a collection list. */
c906108c 967static void
d183932d
MS
968collect_symbol (struct collection_list *collect,
969 struct symbol *sym,
a6d9a66e 970 struct gdbarch *gdbarch,
0936ad1d 971 long frame_regno, long frame_offset,
92bc6a20
TT
972 CORE_ADDR scope,
973 int trace_string)
c906108c 974{
c5aa993b 975 unsigned long len;
104c1213 976 unsigned int reg;
c906108c 977 bfd_signed_vma offset;
400c6af0 978 int treat_as_expr = 0;
c906108c 979
c5aa993b
JM
980 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
981 switch (SYMBOL_CLASS (sym))
982 {
983 default:
984 printf_filtered ("%s: don't know symbol class %d\n",
3567439c 985 SYMBOL_PRINT_NAME (sym),
d183932d 986 SYMBOL_CLASS (sym));
c5aa993b
JM
987 break;
988 case LOC_CONST:
12df843f
JK
989 printf_filtered ("constant %s (value %s) will not be collected.\n",
990 SYMBOL_PRINT_NAME (sym), plongest (SYMBOL_VALUE (sym)));
c5aa993b
JM
991 break;
992 case LOC_STATIC:
993 offset = SYMBOL_VALUE_ADDRESS (sym);
994 if (info_verbose)
104c1213
JM
995 {
996 char tmp[40];
997
998 sprintf_vma (tmp, offset);
999 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
3567439c 1000 SYMBOL_PRINT_NAME (sym), len,
d183932d 1001 tmp /* address */);
104c1213 1002 }
400c6af0
SS
1003 /* A struct may be a C++ class with static fields, go to general
1004 expression handling. */
1005 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT)
1006 treat_as_expr = 1;
1007 else
1008 add_memrange (collect, memrange_absolute, offset, len);
c5aa993b
JM
1009 break;
1010 case LOC_REGISTER:
a6d9a66e 1011 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
c5aa993b 1012 if (info_verbose)
d183932d 1013 printf_filtered ("LOC_REG[parm] %s: ",
3567439c 1014 SYMBOL_PRINT_NAME (sym));
c5aa993b 1015 add_register (collect, reg);
d183932d
MS
1016 /* Check for doubles stored in two registers. */
1017 /* FIXME: how about larger types stored in 3 or more regs? */
c5aa993b 1018 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
a6d9a66e 1019 len > register_size (gdbarch, reg))
c5aa993b
JM
1020 add_register (collect, reg + 1);
1021 break;
1022 case LOC_REF_ARG:
1023 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
1024 printf_filtered (" (will not collect %s)\n",
3567439c 1025 SYMBOL_PRINT_NAME (sym));
c5aa993b
JM
1026 break;
1027 case LOC_ARG:
1028 reg = frame_regno;
1029 offset = frame_offset + SYMBOL_VALUE (sym);
1030 if (info_verbose)
1031 {
104c1213 1032 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
3567439c 1033 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1034 printf_vma (offset);
1035 printf_filtered (" from frame ptr reg %d\n", reg);
c5aa993b
JM
1036 }
1037 add_memrange (collect, reg, offset, len);
1038 break;
1039 case LOC_REGPARM_ADDR:
1040 reg = SYMBOL_VALUE (sym);
1041 offset = 0;
1042 if (info_verbose)
1043 {
104c1213 1044 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset ",
3567439c 1045 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1046 printf_vma (offset);
1047 printf_filtered (" from reg %d\n", reg);
c5aa993b
JM
1048 }
1049 add_memrange (collect, reg, offset, len);
1050 break;
1051 case LOC_LOCAL:
c5aa993b
JM
1052 reg = frame_regno;
1053 offset = frame_offset + SYMBOL_VALUE (sym);
1054 if (info_verbose)
1055 {
104c1213 1056 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
3567439c 1057 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1058 printf_vma (offset);
1059 printf_filtered (" from frame ptr reg %d\n", reg);
c5aa993b
JM
1060 }
1061 add_memrange (collect, reg, offset, len);
1062 break;
a0405854 1063
c5aa993b 1064 case LOC_UNRESOLVED:
a0405854 1065 treat_as_expr = 1;
c5aa993b 1066 break;
a0405854 1067
c5aa993b 1068 case LOC_OPTIMIZED_OUT:
8e1a459b 1069 printf_filtered ("%s has been optimized out of existence.\n",
3567439c 1070 SYMBOL_PRINT_NAME (sym));
c5aa993b 1071 break;
0936ad1d
SS
1072
1073 case LOC_COMPUTED:
400c6af0 1074 treat_as_expr = 1;
0936ad1d 1075 break;
c5aa993b 1076 }
400c6af0
SS
1077
1078 /* Expressions are the most general case. */
1079 if (treat_as_expr)
1080 {
1081 struct agent_expr *aexpr;
1082 struct cleanup *old_chain1 = NULL;
400c6af0 1083
92bc6a20 1084 aexpr = gen_trace_for_var (scope, gdbarch, sym, trace_string);
400c6af0
SS
1085
1086 /* It can happen that the symbol is recorded as a computed
1087 location, but it's been optimized away and doesn't actually
1088 have a location expression. */
1089 if (!aexpr)
1090 {
1091 printf_filtered ("%s has been optimized out of existence.\n",
1092 SYMBOL_PRINT_NAME (sym));
1093 return;
1094 }
1095
1096 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1097
35c9c7ba 1098 ax_reqs (aexpr);
fff87407 1099
35c9c7ba 1100 report_agent_reqs_errors (aexpr);
400c6af0
SS
1101
1102 discard_cleanups (old_chain1);
1103 add_aexpr (collect, aexpr);
1104
c378eb4e 1105 /* Take care of the registers. */
35c9c7ba 1106 if (aexpr->reg_mask_len > 0)
400c6af0
SS
1107 {
1108 int ndx1, ndx2;
1109
35c9c7ba 1110 for (ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
400c6af0 1111 {
c378eb4e 1112 QUIT; /* Allow user to bail out with ^C. */
35c9c7ba 1113 if (aexpr->reg_mask[ndx1] != 0)
400c6af0 1114 {
c378eb4e 1115 /* Assume chars have 8 bits. */
400c6af0 1116 for (ndx2 = 0; ndx2 < 8; ndx2++)
35c9c7ba 1117 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
c378eb4e 1118 /* It's used -- record it. */
400c6af0
SS
1119 add_register (collect, ndx1 * 8 + ndx2);
1120 }
1121 }
1122 }
1123 }
c906108c
SS
1124}
1125
2c58c0a9
PA
1126/* Data to be passed around in the calls to the locals and args
1127 iterators. */
1128
1129struct add_local_symbols_data
1130{
1131 struct collection_list *collect;
1132 struct gdbarch *gdbarch;
1133 CORE_ADDR pc;
1134 long frame_regno;
1135 long frame_offset;
1136 int count;
92bc6a20 1137 int trace_string;
2c58c0a9
PA
1138};
1139
c378eb4e 1140/* The callback for the locals and args iterators. */
2c58c0a9
PA
1141
1142static void
1143do_collect_symbol (const char *print_name,
1144 struct symbol *sym,
1145 void *cb_data)
1146{
1147 struct add_local_symbols_data *p = cb_data;
1148
1149 collect_symbol (p->collect, sym, p->gdbarch, p->frame_regno,
92bc6a20 1150 p->frame_offset, p->pc, p->trace_string);
2c58c0a9 1151 p->count++;
dc673c81
YQ
1152
1153 VEC_safe_push (char_ptr, p->collect->wholly_collected,
1154 xstrdup (print_name));
2c58c0a9
PA
1155}
1156
c378eb4e 1157/* Add all locals (or args) symbols to collection list. */
c906108c 1158static void
a6d9a66e
UW
1159add_local_symbols (struct collection_list *collect,
1160 struct gdbarch *gdbarch, CORE_ADDR pc,
92bc6a20
TT
1161 long frame_regno, long frame_offset, int type,
1162 int trace_string)
c906108c 1163{
c5aa993b 1164 struct block *block;
2c58c0a9
PA
1165 struct add_local_symbols_data cb_data;
1166
1167 cb_data.collect = collect;
1168 cb_data.gdbarch = gdbarch;
1169 cb_data.pc = pc;
1170 cb_data.frame_regno = frame_regno;
1171 cb_data.frame_offset = frame_offset;
1172 cb_data.count = 0;
92bc6a20 1173 cb_data.trace_string = trace_string;
c906108c 1174
2c58c0a9 1175 if (type == 'L')
c906108c 1176 {
2c58c0a9
PA
1177 block = block_for_pc (pc);
1178 if (block == NULL)
c906108c 1179 {
2c58c0a9
PA
1180 warning (_("Can't collect locals; "
1181 "no symbol table info available.\n"));
1182 return;
c906108c 1183 }
2c58c0a9
PA
1184
1185 iterate_over_block_local_vars (block, do_collect_symbol, &cb_data);
1186 if (cb_data.count == 0)
1187 warning (_("No locals found in scope."));
1188 }
1189 else
1190 {
1191 pc = get_pc_function_start (pc);
1192 block = block_for_pc (pc);
1193 if (block == NULL)
1194 {
b37520b6 1195 warning (_("Can't collect args; no symbol table info available."));
2c58c0a9
PA
1196 return;
1197 }
1198
1199 iterate_over_block_arg_vars (block, do_collect_symbol, &cb_data);
1200 if (cb_data.count == 0)
1201 warning (_("No args found in scope."));
c906108c 1202 }
c906108c
SS
1203}
1204
0fb4aa4b
PA
1205static void
1206add_static_trace_data (struct collection_list *collection)
1207{
1208 if (info_verbose)
1209 printf_filtered ("collect static trace data\n");
1210 collection->strace_data = 1;
1211}
1212
c906108c
SS
1213/* worker function */
1214static void
fba45db2 1215clear_collection_list (struct collection_list *list)
c906108c
SS
1216{
1217 int ndx;
1218
1219 list->next_memrange = 0;
1220 for (ndx = 0; ndx < list->next_aexpr_elt; ndx++)
1221 {
c5aa993b 1222 free_agent_expr (list->aexpr_list[ndx]);
c906108c
SS
1223 list->aexpr_list[ndx] = NULL;
1224 }
1225 list->next_aexpr_elt = 0;
1226 memset (list->regs_mask, 0, sizeof (list->regs_mask));
0fb4aa4b 1227 list->strace_data = 0;
cbfa3b61
YQ
1228
1229 xfree (list->aexpr_list);
1230 xfree (list->list);
dc673c81
YQ
1231
1232 VEC_free (char_ptr, list->wholly_collected);
1233 VEC_free (char_ptr, list->computed);
cbfa3b61
YQ
1234}
1235
1236/* A cleanup wrapper for function clear_collection_list. */
1237
1238static void
1239do_clear_collection_list (void *list)
1240{
1241 struct collection_list *l = list;
1242
1243 clear_collection_list (l);
1244}
1245
1246/* Initialize collection_list CLIST. */
1247
1248static void
1249init_collection_list (struct collection_list *clist)
1250{
1251 memset (clist, 0, sizeof *clist);
1252
1253 clist->listsize = 128;
1254 clist->list = xcalloc (clist->listsize,
1255 sizeof (struct memrange));
1256
1257 clist->aexpr_listsize = 128;
1258 clist->aexpr_list = xcalloc (clist->aexpr_listsize,
1259 sizeof (struct agent_expr *));
c906108c
SS
1260}
1261
c378eb4e 1262/* Reduce a collection list to string form (for gdb protocol). */
c906108c 1263static char **
a73e3634 1264stringify_collection_list (struct collection_list *list)
c906108c
SS
1265{
1266 char temp_buf[2048];
104c1213 1267 char tmp2[40];
c906108c
SS
1268 int count;
1269 int ndx = 0;
1270 char *(*str_list)[];
1271 char *end;
c5aa993b 1272 long i;
c906108c 1273
0fb4aa4b 1274 count = 1 + 1 + list->next_memrange + list->next_aexpr_elt + 1;
c5aa993b 1275 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
c906108c 1276
0fb4aa4b
PA
1277 if (list->strace_data)
1278 {
1279 if (info_verbose)
1280 printf_filtered ("\nCollecting static trace data\n");
1281 end = temp_buf;
1282 *end++ = 'L';
1283 (*str_list)[ndx] = savestring (temp_buf, end - temp_buf);
1284 ndx++;
1285 }
1286
c906108c 1287 for (i = sizeof (list->regs_mask) - 1; i > 0; i--)
c378eb4e 1288 if (list->regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
c906108c 1289 break;
c378eb4e 1290 if (list->regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
c906108c
SS
1291 {
1292 if (info_verbose)
1293 printf_filtered ("\nCollecting registers (mask): 0x");
1294 end = temp_buf;
c5aa993b 1295 *end++ = 'R';
c906108c
SS
1296 for (; i >= 0; i--)
1297 {
c378eb4e 1298 QUIT; /* Allow user to bail out with ^C. */
c906108c
SS
1299 if (info_verbose)
1300 printf_filtered ("%02X", list->regs_mask[i]);
c5aa993b 1301 sprintf (end, "%02X", list->regs_mask[i]);
c906108c
SS
1302 end += 2;
1303 }
1b36a34b 1304 (*str_list)[ndx] = xstrdup (temp_buf);
c906108c
SS
1305 ndx++;
1306 }
1307 if (info_verbose)
1308 printf_filtered ("\n");
1309 if (list->next_memrange > 0 && info_verbose)
1310 printf_filtered ("Collecting memranges: \n");
1311 for (i = 0, count = 0, end = temp_buf; i < list->next_memrange; i++)
1312 {
c378eb4e 1313 QUIT; /* Allow user to bail out with ^C. */
104c1213 1314 sprintf_vma (tmp2, list->list[i].start);
c906108c 1315 if (info_verbose)
104c1213
JM
1316 {
1317 printf_filtered ("(%d, %s, %ld)\n",
1318 list->list[i].type,
1319 tmp2,
1320 (long) (list->list[i].end - list->list[i].start));
1321 }
c906108c
SS
1322 if (count + 27 > MAX_AGENT_EXPR_LEN)
1323 {
c5aa993b 1324 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1325 ndx++;
1326 count = 0;
1327 end = temp_buf;
1328 }
104c1213 1329
d1948716
JB
1330 {
1331 bfd_signed_vma length = list->list[i].end - list->list[i].start;
1332
1333 /* The "%X" conversion specifier expects an unsigned argument,
f50e79a4
JB
1334 so passing -1 (memrange_absolute) to it directly gives you
1335 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1336 Special-case it. */
1337 if (list->list[i].type == memrange_absolute)
d1948716
JB
1338 sprintf (end, "M-1,%s,%lX", tmp2, (long) length);
1339 else
1340 sprintf (end, "M%X,%s,%lX", list->list[i].type, tmp2, (long) length);
1341 }
104c1213 1342
c906108c 1343 count += strlen (end);
3ffbc0a5 1344 end = temp_buf + count;
c906108c
SS
1345 }
1346
1347 for (i = 0; i < list->next_aexpr_elt; i++)
1348 {
c378eb4e 1349 QUIT; /* Allow user to bail out with ^C. */
c906108c
SS
1350 if ((count + 10 + 2 * list->aexpr_list[i]->len) > MAX_AGENT_EXPR_LEN)
1351 {
c5aa993b 1352 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1353 ndx++;
1354 count = 0;
1355 end = temp_buf;
1356 }
1357 sprintf (end, "X%08X,", list->aexpr_list[i]->len);
1358 end += 10; /* 'X' + 8 hex digits + ',' */
1359 count += 10;
1360
d183932d
MS
1361 end = mem2hex (list->aexpr_list[i]->buf,
1362 end, list->aexpr_list[i]->len);
c906108c
SS
1363 count += 2 * list->aexpr_list[i]->len;
1364 }
1365
1366 if (count != 0)
1367 {
c5aa993b 1368 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1369 ndx++;
1370 count = 0;
1371 end = temp_buf;
1372 }
1373 (*str_list)[ndx] = NULL;
1374
1375 if (ndx == 0)
27e06d3e 1376 {
6c761d9c 1377 xfree (str_list);
27e06d3e
MS
1378 return NULL;
1379 }
c906108c
SS
1380 else
1381 return *str_list;
1382}
1383
dc673c81
YQ
1384/* Add the printed expression EXP to *LIST. */
1385
1386static void
1387append_exp (struct expression *exp, VEC(char_ptr) **list)
1388{
1389 struct ui_file *tmp_stream = mem_fileopen ();
1390 char *text;
1391
1392 print_expression (exp, tmp_stream);
1393
1394 text = ui_file_xstrdup (tmp_stream, NULL);
1395
1396 VEC_safe_push (char_ptr, *list, text);
1397 ui_file_delete (tmp_stream);
1398}
a7bdde9e
VP
1399
1400static void
1401encode_actions_1 (struct command_line *action,
a7bdde9e
VP
1402 struct bp_location *tloc,
1403 int frame_reg,
1404 LONGEST frame_offset,
1405 struct collection_list *collect,
1406 struct collection_list *stepping_list)
c906108c 1407{
6f937416 1408 const char *action_exp;
c5aa993b 1409 struct expression *exp = NULL;
104c1213 1410 int i;
f976f6d4 1411 struct value *tempval;
c906108c
SS
1412 struct cmd_list_element *cmd;
1413 struct agent_expr *aexpr;
236f1d4d
SS
1414
1415 for (; action; action = action->next)
c906108c 1416 {
c378eb4e 1417 QUIT; /* Allow user to bail out with ^C. */
a7bdde9e 1418 action_exp = action->line;
6f937416 1419 action_exp = skip_spaces_const (action_exp);
c906108c 1420
c906108c
SS
1421 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1422 if (cmd == 0)
8a3fe4f8 1423 error (_("Bad action list item: %s"), action_exp);
c906108c 1424
bbaca940 1425 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
c906108c 1426 {
92bc6a20
TT
1427 int trace_string = 0;
1428
3065dfb6 1429 if (*action_exp == '/')
92bc6a20 1430 action_exp = decode_agent_options (action_exp, &trace_string);
3065dfb6 1431
c5aa993b 1432 do
c378eb4e
MS
1433 { /* Repeat over a comma-separated list. */
1434 QUIT; /* Allow user to bail out with ^C. */
6f937416 1435 action_exp = skip_spaces_const (action_exp);
c906108c 1436
c5aa993b
JM
1437 if (0 == strncasecmp ("$reg", action_exp, 4))
1438 {
3e05895e 1439 for (i = 0; i < gdbarch_num_regs (tloc->gdbarch); i++)
c5aa993b
JM
1440 add_register (collect, i);
1441 action_exp = strchr (action_exp, ','); /* more? */
1442 }
1443 else if (0 == strncasecmp ("$arg", action_exp, 4))
1444 {
1445 add_local_symbols (collect,
3e05895e 1446 tloc->gdbarch,
9355b391 1447 tloc->address,
c5aa993b
JM
1448 frame_reg,
1449 frame_offset,
92bc6a20
TT
1450 'A',
1451 trace_string);
c5aa993b
JM
1452 action_exp = strchr (action_exp, ','); /* more? */
1453 }
1454 else if (0 == strncasecmp ("$loc", action_exp, 4))
1455 {
1456 add_local_symbols (collect,
3e05895e 1457 tloc->gdbarch,
9355b391 1458 tloc->address,
c5aa993b
JM
1459 frame_reg,
1460 frame_offset,
92bc6a20
TT
1461 'L',
1462 trace_string);
c5aa993b
JM
1463 action_exp = strchr (action_exp, ','); /* more? */
1464 }
6710bf39
SS
1465 else if (0 == strncasecmp ("$_ret", action_exp, 5))
1466 {
1467 struct cleanup *old_chain1 = NULL;
1468
1469 aexpr = gen_trace_for_return_address (tloc->address,
92bc6a20
TT
1470 tloc->gdbarch,
1471 trace_string);
6710bf39
SS
1472
1473 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1474
1475 ax_reqs (aexpr);
1476 report_agent_reqs_errors (aexpr);
1477
1478 discard_cleanups (old_chain1);
1479 add_aexpr (collect, aexpr);
1480
1481 /* take care of the registers */
1482 if (aexpr->reg_mask_len > 0)
1483 {
1484 int ndx1, ndx2;
1485
1486 for (ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1487 {
1488 QUIT; /* allow user to bail out with ^C */
1489 if (aexpr->reg_mask[ndx1] != 0)
1490 {
1491 /* assume chars have 8 bits */
1492 for (ndx2 = 0; ndx2 < 8; ndx2++)
1493 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1494 /* it's used -- record it */
1495 add_register (collect,
1496 ndx1 * 8 + ndx2);
1497 }
1498 }
1499 }
1500
1501 action_exp = strchr (action_exp, ','); /* more? */
1502 }
0fb4aa4b
PA
1503 else if (0 == strncasecmp ("$_sdata", action_exp, 7))
1504 {
1505 add_static_trace_data (collect);
1506 action_exp = strchr (action_exp, ','); /* more? */
1507 }
c5aa993b
JM
1508 else
1509 {
744a8059 1510 unsigned long addr;
c5aa993b
JM
1511 struct cleanup *old_chain = NULL;
1512 struct cleanup *old_chain1 = NULL;
c5aa993b 1513
6f937416 1514 exp = parse_exp_1 (&action_exp, tloc->address,
9355b391 1515 block_for_pc (tloc->address), 1);
74b7792f 1516 old_chain = make_cleanup (free_current_contents, &exp);
c906108c 1517
c5aa993b
JM
1518 switch (exp->elts[0].opcode)
1519 {
1520 case OP_REGISTER:
67f3407f
DJ
1521 {
1522 const char *name = &exp->elts[2].string;
1523
3e05895e 1524 i = user_reg_map_name_to_regnum (tloc->gdbarch,
029a67e4 1525 name, strlen (name));
67f3407f
DJ
1526 if (i == -1)
1527 internal_error (__FILE__, __LINE__,
1528 _("Register $%s not available"),
1529 name);
1530 if (info_verbose)
1531 printf_filtered ("OP_REGISTER: ");
1532 add_register (collect, i);
1533 break;
1534 }
c5aa993b
JM
1535
1536 case UNOP_MEMVAL:
c378eb4e 1537 /* Safe because we know it's a simple expression. */
c5aa993b 1538 tempval = evaluate_expression (exp);
42ae5230 1539 addr = value_address (tempval);
744a8059
SP
1540 /* Initialize the TYPE_LENGTH if it is a typedef. */
1541 check_typedef (exp->elts[1].type);
1542 add_memrange (collect, memrange_absolute, addr,
1543 TYPE_LENGTH (exp->elts[1].type));
dc673c81 1544 append_exp (exp, &collect->computed);
c5aa993b
JM
1545 break;
1546
1547 case OP_VAR_VALUE:
dc673c81
YQ
1548 {
1549 struct symbol *sym = exp->elts[2].symbol;
1550 char_ptr name = (char_ptr) SYMBOL_NATURAL_NAME (sym);
1551
1552 collect_symbol (collect,
1553 exp->elts[2].symbol,
1554 tloc->gdbarch,
1555 frame_reg,
1556 frame_offset,
1557 tloc->address,
1558 trace_string);
1559 VEC_safe_push (char_ptr,
1560 collect->wholly_collected,
1561 name);
1562 }
c5aa993b
JM
1563 break;
1564
c378eb4e 1565 default: /* Full-fledged expression. */
92bc6a20
TT
1566 aexpr = gen_trace_for_expr (tloc->address, exp,
1567 trace_string);
c5aa993b 1568
f23d52e0 1569 old_chain1 = make_cleanup_free_agent_expr (aexpr);
c5aa993b 1570
35c9c7ba 1571 ax_reqs (aexpr);
c5aa993b 1572
35c9c7ba 1573 report_agent_reqs_errors (aexpr);
c5aa993b
JM
1574
1575 discard_cleanups (old_chain1);
1576 add_aexpr (collect, aexpr);
1577
c378eb4e 1578 /* Take care of the registers. */
35c9c7ba 1579 if (aexpr->reg_mask_len > 0)
c906108c 1580 {
c5aa993b
JM
1581 int ndx1;
1582 int ndx2;
1583
35c9c7ba 1584 for (ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
c906108c 1585 {
c378eb4e 1586 QUIT; /* Allow user to bail out with ^C. */
35c9c7ba 1587 if (aexpr->reg_mask[ndx1] != 0)
c5aa993b 1588 {
c378eb4e 1589 /* Assume chars have 8 bits. */
c5aa993b 1590 for (ndx2 = 0; ndx2 < 8; ndx2++)
35c9c7ba 1591 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
c378eb4e 1592 /* It's used -- record it. */
d183932d
MS
1593 add_register (collect,
1594 ndx1 * 8 + ndx2);
c5aa993b 1595 }
c906108c
SS
1596 }
1597 }
dc673c81
YQ
1598
1599 append_exp (exp, &collect->computed);
c5aa993b
JM
1600 break;
1601 } /* switch */
1602 do_cleanups (old_chain);
1603 } /* do */
1604 }
1605 while (action_exp && *action_exp++ == ',');
1606 } /* if */
6da95a67
SS
1607 else if (cmd_cfunc_eq (cmd, teval_pseudocommand))
1608 {
1609 do
c378eb4e
MS
1610 { /* Repeat over a comma-separated list. */
1611 QUIT; /* Allow user to bail out with ^C. */
6f937416 1612 action_exp = skip_spaces_const (action_exp);
6da95a67
SS
1613
1614 {
6da95a67
SS
1615 struct cleanup *old_chain = NULL;
1616 struct cleanup *old_chain1 = NULL;
6da95a67 1617
6f937416 1618 exp = parse_exp_1 (&action_exp, tloc->address,
9355b391 1619 block_for_pc (tloc->address), 1);
6da95a67
SS
1620 old_chain = make_cleanup (free_current_contents, &exp);
1621
9355b391 1622 aexpr = gen_eval_for_expr (tloc->address, exp);
6da95a67
SS
1623 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1624
35c9c7ba
SS
1625 ax_reqs (aexpr);
1626 report_agent_reqs_errors (aexpr);
6da95a67
SS
1627
1628 discard_cleanups (old_chain1);
1629 /* Even though we're not officially collecting, add
1630 to the collect list anyway. */
1631 add_aexpr (collect, aexpr);
1632
1633 do_cleanups (old_chain);
1634 } /* do */
1635 }
1636 while (action_exp && *action_exp++ == ',');
1637 } /* if */
bbaca940 1638 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
c906108c 1639 {
a7bdde9e
VP
1640 /* We check against nested while-stepping when setting
1641 breakpoint action, so no way to run into nested
1642 here. */
1643 gdb_assert (stepping_list);
1644
23da373a 1645 encode_actions_1 (action->body_list[0], tloc, frame_reg,
2a2287c7 1646 frame_offset, stepping_list, NULL);
c906108c 1647 }
a7bdde9e
VP
1648 else
1649 error (_("Invalid tracepoint command '%s'"), action->line);
1650 } /* for */
1651}
1652
dc673c81
YQ
1653/* Encode actions of tracepoint TLOC->owner and fill TRACEPOINT_LIST
1654 and STEPPING_LIST. Return a cleanup pointer to clean up both
1655 TRACEPOINT_LIST and STEPPING_LIST. */
5fbce5df 1656
dc673c81
YQ
1657struct cleanup *
1658encode_actions_and_make_cleanup (struct bp_location *tloc,
1659 struct collection_list *tracepoint_list,
1660 struct collection_list *stepping_list)
a7bdde9e 1661{
a7bdde9e
VP
1662 char *default_collect_line = NULL;
1663 struct command_line *actions;
1664 struct command_line *default_collect_action = NULL;
1665 int frame_reg;
1666 LONGEST frame_offset;
dc673c81 1667 struct cleanup *back_to, *return_chain;
a7bdde9e 1668
dc673c81
YQ
1669 return_chain = make_cleanup (null_cleanup, NULL);
1670 init_collection_list (tracepoint_list);
1671 init_collection_list (stepping_list);
cbfa3b61 1672
dc673c81
YQ
1673 make_cleanup (do_clear_collection_list, tracepoint_list);
1674 make_cleanup (do_clear_collection_list, stepping_list);
a7bdde9e 1675
dc673c81 1676 back_to = make_cleanup (null_cleanup, NULL);
3e05895e
TT
1677 gdbarch_virtual_frame_pointer (tloc->gdbarch,
1678 tloc->address, &frame_reg, &frame_offset);
a7bdde9e 1679
3ca73e0c 1680 actions = all_tracepoint_actions_and_cleanup (tloc->owner);
a7bdde9e 1681
23da373a 1682 encode_actions_1 (actions, tloc, frame_reg, frame_offset,
dc673c81
YQ
1683 tracepoint_list, stepping_list);
1684
1685 memrange_sortmerge (tracepoint_list);
1686 memrange_sortmerge (stepping_list);
1687
1688 do_cleanups (back_to);
1689 return return_chain;
1690}
1691
1692/* Render all actions into gdb protocol. */
1693
1694void
1695encode_actions_rsp (struct bp_location *tloc, char ***tdp_actions,
1696 char ***stepping_actions)
1697{
1698 struct collection_list tracepoint_list, stepping_list;
1699 struct cleanup *cleanup;
a7bdde9e 1700
dc673c81
YQ
1701 *tdp_actions = NULL;
1702 *stepping_actions = NULL;
1703
1704 cleanup = encode_actions_and_make_cleanup (tloc, &tracepoint_list,
1705 &stepping_list);
c906108c 1706
a73e3634
YQ
1707 *tdp_actions = stringify_collection_list (&tracepoint_list);
1708 *stepping_actions = stringify_collection_list (&stepping_list);
236f1d4d 1709
dc673c81 1710 do_cleanups (cleanup);
c906108c
SS
1711}
1712
1713static void
fba45db2 1714add_aexpr (struct collection_list *collect, struct agent_expr *aexpr)
c906108c
SS
1715{
1716 if (collect->next_aexpr_elt >= collect->aexpr_listsize)
1717 {
1718 collect->aexpr_list =
1719 xrealloc (collect->aexpr_list,
5d502164 1720 2 * collect->aexpr_listsize * sizeof (struct agent_expr *));
c906108c
SS
1721 collect->aexpr_listsize *= 2;
1722 }
1723 collect->aexpr_list[collect->next_aexpr_elt] = aexpr;
1724 collect->next_aexpr_elt++;
1725}
1726
bfccc43c
YQ
1727static void
1728process_tracepoint_on_disconnect (void)
1729{
1730 VEC(breakpoint_p) *tp_vec = NULL;
1731 int ix;
1732 struct breakpoint *b;
1733 int has_pending_p = 0;
1734
1735 /* Check whether we still have pending tracepoint. If we have, warn the
1736 user that pending tracepoint will no longer work. */
1737 tp_vec = all_tracepoints ();
1738 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1739 {
1740 if (b->loc == NULL)
1741 {
1742 has_pending_p = 1;
1743 break;
1744 }
1745 else
1746 {
1747 struct bp_location *loc1;
1748
1749 for (loc1 = b->loc; loc1; loc1 = loc1->next)
1750 {
1751 if (loc1->shlib_disabled)
1752 {
1753 has_pending_p = 1;
1754 break;
1755 }
1756 }
1757
1758 if (has_pending_p)
1759 break;
1760 }
1761 }
1762 VEC_free (breakpoint_p, tp_vec);
1763
1764 if (has_pending_p)
1765 warning (_("Pending tracepoints will not be resolved while"
1766 " GDB is disconnected\n"));
1767}
1768
aef525cb
YQ
1769/* Reset local state of tracing. */
1770
1771void
1772trace_reset_local_state (void)
1773{
1774 set_traceframe_num (-1);
1775 set_tracepoint_num (-1);
1776 set_traceframe_context (NULL);
1777 clear_traceframe_info ();
1778}
c5aa993b 1779
f224b49d 1780void
f196051f 1781start_tracing (char *notes)
d183932d 1782{
1042e4c0
SS
1783 VEC(breakpoint_p) *tp_vec = NULL;
1784 int ix;
d9b3f62e 1785 struct breakpoint *b;
f61e138d 1786 struct trace_state_variable *tsv;
d914c394 1787 int any_enabled = 0, num_to_download = 0;
f196051f
SS
1788 int ret;
1789
35b1e5cc 1790 tp_vec = all_tracepoints ();
76a2b958 1791
c378eb4e 1792 /* No point in tracing without any tracepoints... */
76a2b958
SS
1793 if (VEC_length (breakpoint_p, tp_vec) == 0)
1794 {
1795 VEC_free (breakpoint_p, tp_vec);
1796 error (_("No tracepoints defined, not starting trace"));
1797 }
1798
d9b3f62e 1799 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
76a2b958 1800 {
d9b3f62e 1801 struct tracepoint *t = (struct tracepoint *) b;
55aa24fb 1802 struct bp_location *loc;
d9b3f62e
PA
1803
1804 if (b->enable_state == bp_enabled)
d914c394
SS
1805 any_enabled = 1;
1806
d9b3f62e 1807 if ((b->type == bp_fast_tracepoint
d914c394
SS
1808 ? may_insert_fast_tracepoints
1809 : may_insert_tracepoints))
1810 ++num_to_download;
1811 else
1812 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
d9b3f62e 1813 (b->type == bp_fast_tracepoint ? "fast " : ""), b->number);
76a2b958
SS
1814 }
1815
76a2b958
SS
1816 if (!any_enabled)
1817 {
d248b706
KY
1818 if (target_supports_enable_disable_tracepoint ())
1819 warning (_("No tracepoints enabled"));
1820 else
1821 {
1822 /* No point in tracing with only disabled tracepoints that
1823 cannot be re-enabled. */
1824 VEC_free (breakpoint_p, tp_vec);
1825 error (_("No tracepoints enabled, not starting trace"));
1826 }
76a2b958
SS
1827 }
1828
d914c394
SS
1829 if (num_to_download <= 0)
1830 {
1831 VEC_free (breakpoint_p, tp_vec);
1832 error (_("No tracepoints that may be downloaded, not starting trace"));
1833 }
1834
76a2b958
SS
1835 target_trace_init ();
1836
d9b3f62e 1837 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
7a697b8d 1838 {
d9b3f62e 1839 struct tracepoint *t = (struct tracepoint *) b;
e8ba3115 1840 struct bp_location *loc;
f2a8bc8a 1841 int bp_location_downloaded = 0;
d9b3f62e 1842
4511b1ba
YQ
1843 /* Clear `inserted' flag. */
1844 for (loc = b->loc; loc; loc = loc->next)
1845 loc->inserted = 0;
1846
d9b3f62e 1847 if ((b->type == bp_fast_tracepoint
d914c394
SS
1848 ? !may_insert_fast_tracepoints
1849 : !may_insert_tracepoints))
1850 continue;
1851
35b1e5cc 1852 t->number_on_target = 0;
e8ba3115
YQ
1853
1854 for (loc = b->loc; loc; loc = loc->next)
1e4d1764
YQ
1855 {
1856 /* Since tracepoint locations are never duplicated, `inserted'
1857 flag should be zero. */
1858 gdb_assert (!loc->inserted);
1859
1860 target_download_tracepoint (loc);
1861
1862 loc->inserted = 1;
f2a8bc8a 1863 bp_location_downloaded = 1;
1e4d1764 1864 }
e8ba3115 1865
d9b3f62e 1866 t->number_on_target = b->number;
55aa24fb
SDJ
1867
1868 for (loc = b->loc; loc; loc = loc->next)
311fe7e1
SDJ
1869 if (loc->probe != NULL)
1870 loc->probe->pops->set_semaphore (loc->probe, loc->gdbarch);
f2a8bc8a
YQ
1871
1872 if (bp_location_downloaded)
1873 observer_notify_breakpoint_modified (b);
7a697b8d 1874 }
35b1e5cc 1875 VEC_free (breakpoint_p, tp_vec);
76a2b958 1876
00bf0b85 1877 /* Send down all the trace state variables too. */
35b1e5cc 1878 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
782b2b07 1879 {
00bf0b85 1880 target_download_trace_state_variable (tsv);
782b2b07 1881 }
35b1e5cc
SS
1882
1883 /* Tell target to treat text-like sections as transparent. */
1884 target_trace_set_readonly_regions ();
4daf5ac0
SS
1885 /* Set some mode flags. */
1886 target_set_disconnected_tracing (disconnected_tracing);
1887 target_set_circular_trace_buffer (circular_trace_buffer);
f6f899bf 1888 target_set_trace_buffer_size (trace_buffer_size);
1042e4c0 1889
f196051f
SS
1890 if (!notes)
1891 notes = trace_notes;
1892 ret = target_set_trace_notes (trace_user, notes, NULL);
1893
1894 if (!ret && (trace_user || notes))
43011e52 1895 warning (_("Target does not support trace user/notes, info ignored"));
f196051f 1896
35b1e5cc
SS
1897 /* Now insert traps and begin collecting data. */
1898 target_trace_start ();
1042e4c0 1899
35b1e5cc 1900 /* Reset our local state. */
aef525cb 1901 trace_reset_local_state ();
00bf0b85 1902 current_trace_status()->running = 1;
1042e4c0
SS
1903}
1904
f196051f
SS
1905/* The tstart command requests the target to start a new trace run.
1906 The command passes any arguments it has to the target verbatim, as
1907 an optional "trace note". This is useful as for instance a warning
1908 to other users if the trace runs disconnected, and you don't want
1909 anybody else messing with the target. */
f224b49d
VP
1910
1911static void
1912trace_start_command (char *args, int from_tty)
1913{
1914 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1915
615bcdef
SS
1916 if (current_trace_status ()->running)
1917 {
1918 if (from_tty
1919 && !query (_("A trace is running already. Start a new run? ")))
1920 error (_("New trace run not started."));
1921 }
1922
f196051f 1923 start_tracing (args);
f224b49d
VP
1924}
1925
f196051f
SS
1926/* The tstop command stops the tracing run. The command passes any
1927 supplied arguments to the target verbatim as a "stop note"; if the
1928 target supports trace notes, then it will be reported back as part
1929 of the trace run's status. */
1930
c906108c 1931static void
fba45db2 1932trace_stop_command (char *args, int from_tty)
d183932d 1933{
615bcdef
SS
1934 if (!current_trace_status ()->running)
1935 error (_("Trace is not running."));
1936
f196051f 1937 stop_tracing (args);
c906108c
SS
1938}
1939
d5551862 1940void
f196051f 1941stop_tracing (char *note)
d5551862 1942{
f196051f 1943 int ret;
55aa24fb
SDJ
1944 VEC(breakpoint_p) *tp_vec = NULL;
1945 int ix;
1946 struct breakpoint *t;
f196051f 1947
35b1e5cc 1948 target_trace_stop ();
f196051f 1949
55aa24fb
SDJ
1950 tp_vec = all_tracepoints ();
1951 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1952 {
1953 struct bp_location *loc;
1954
1955 if ((t->type == bp_fast_tracepoint
1956 ? !may_insert_fast_tracepoints
1957 : !may_insert_tracepoints))
1958 continue;
1959
1960 for (loc = t->loc; loc; loc = loc->next)
1961 {
1962 /* GDB can be totally absent in some disconnected trace scenarios,
1963 but we don't really care if this semaphore goes out of sync.
1964 That's why we are decrementing it here, but not taking care
1965 in other places. */
311fe7e1
SDJ
1966 if (loc->probe != NULL)
1967 loc->probe->pops->clear_semaphore (loc->probe, loc->gdbarch);
55aa24fb
SDJ
1968 }
1969 }
1970
1971 VEC_free (breakpoint_p, tp_vec);
1972
f196051f
SS
1973 if (!note)
1974 note = trace_stop_notes;
1975 ret = target_set_trace_notes (NULL, NULL, note);
1976
1977 if (!ret && note)
43011e52 1978 warning (_("Target does not support trace notes, note ignored"));
f196051f 1979
c378eb4e 1980 /* Should change in response to reply? */
00bf0b85 1981 current_trace_status ()->running = 0;
d5551862
SS
1982}
1983
c906108c
SS
1984/* tstatus command */
1985static void
fba45db2 1986trace_status_command (char *args, int from_tty)
d183932d 1987{
00bf0b85 1988 struct trace_status *ts = current_trace_status ();
f196051f
SS
1989 int status, ix;
1990 VEC(breakpoint_p) *tp_vec = NULL;
1991 struct breakpoint *t;
35b1e5cc 1992
00bf0b85
SS
1993 status = target_get_trace_status (ts);
1994
1995 if (status == -1)
1996 {
f5911ea1 1997 if (ts->filename != NULL)
00bf0b85
SS
1998 printf_filtered (_("Using a trace file.\n"));
1999 else
2000 {
2001 printf_filtered (_("Trace can not be run on this target.\n"));
2002 return;
2003 }
2004 }
2005
2006 if (!ts->running_known)
2007 {
2008 printf_filtered (_("Run/stop status is unknown.\n"));
2009 }
2010 else if (ts->running)
c906108c 2011 {
35b1e5cc 2012 printf_filtered (_("Trace is running on the target.\n"));
c906108c
SS
2013 }
2014 else
00bf0b85
SS
2015 {
2016 switch (ts->stop_reason)
2017 {
2018 case trace_never_run:
2019 printf_filtered (_("No trace has been run on the target.\n"));
2020 break;
2021 case tstop_command:
f196051f
SS
2022 if (ts->stop_desc)
2023 printf_filtered (_("Trace stopped by a tstop command (%s).\n"),
2024 ts->stop_desc);
2025 else
2026 printf_filtered (_("Trace stopped by a tstop command.\n"));
00bf0b85
SS
2027 break;
2028 case trace_buffer_full:
2029 printf_filtered (_("Trace stopped because the buffer was full.\n"));
2030 break;
2031 case trace_disconnected:
2032 printf_filtered (_("Trace stopped because of disconnection.\n"));
2033 break;
2034 case tracepoint_passcount:
00bf0b85
SS
2035 printf_filtered (_("Trace stopped by tracepoint %d.\n"),
2036 ts->stopping_tracepoint);
2037 break;
6c28cbf2
SS
2038 case tracepoint_error:
2039 if (ts->stopping_tracepoint)
3e43a32a
MS
2040 printf_filtered (_("Trace stopped by an "
2041 "error (%s, tracepoint %d).\n"),
f196051f 2042 ts->stop_desc, ts->stopping_tracepoint);
6c28cbf2
SS
2043 else
2044 printf_filtered (_("Trace stopped by an error (%s).\n"),
f196051f 2045 ts->stop_desc);
6c28cbf2 2046 break;
00bf0b85
SS
2047 case trace_stop_reason_unknown:
2048 printf_filtered (_("Trace stopped for an unknown reason.\n"));
2049 break;
2050 default:
2051 printf_filtered (_("Trace stopped for some other reason (%d).\n"),
2052 ts->stop_reason);
2053 break;
2054 }
2055 }
2056
4daf5ac0
SS
2057 if (ts->traceframes_created >= 0
2058 && ts->traceframe_count != ts->traceframes_created)
2059 {
3e43a32a
MS
2060 printf_filtered (_("Buffer contains %d trace "
2061 "frames (of %d created total).\n"),
4daf5ac0
SS
2062 ts->traceframe_count, ts->traceframes_created);
2063 }
2064 else if (ts->traceframe_count >= 0)
00bf0b85
SS
2065 {
2066 printf_filtered (_("Collected %d trace frames.\n"),
2067 ts->traceframe_count);
2068 }
2069
4daf5ac0 2070 if (ts->buffer_free >= 0)
00bf0b85 2071 {
4daf5ac0
SS
2072 if (ts->buffer_size >= 0)
2073 {
2074 printf_filtered (_("Trace buffer has %d bytes of %d bytes free"),
2075 ts->buffer_free, ts->buffer_size);
2076 if (ts->buffer_size > 0)
2077 printf_filtered (_(" (%d%% full)"),
2078 ((int) ((((long long) (ts->buffer_size
2079 - ts->buffer_free)) * 100)
2080 / ts->buffer_size)));
2081 printf_filtered (_(".\n"));
2082 }
2083 else
2084 printf_filtered (_("Trace buffer has %d bytes free.\n"),
2085 ts->buffer_free);
00bf0b85 2086 }
35b1e5cc 2087
33da3f1c
SS
2088 if (ts->disconnected_tracing)
2089 printf_filtered (_("Trace will continue if GDB disconnects.\n"));
2090 else
2091 printf_filtered (_("Trace will stop if GDB disconnects.\n"));
2092
2093 if (ts->circular_buffer)
2094 printf_filtered (_("Trace buffer is circular.\n"));
2095
f196051f
SS
2096 if (ts->user_name && strlen (ts->user_name) > 0)
2097 printf_filtered (_("Trace user is %s.\n"), ts->user_name);
2098
2099 if (ts->notes && strlen (ts->notes) > 0)
2100 printf_filtered (_("Trace notes: %s.\n"), ts->notes);
2101
00bf0b85 2102 /* Now report on what we're doing with tfind. */
35b1e5cc
SS
2103 if (traceframe_number >= 0)
2104 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
2105 traceframe_number, tracepoint_number);
2106 else
2107 printf_filtered (_("Not looking at any trace frame.\n"));
f196051f
SS
2108
2109 /* Report start/stop times if supplied. */
2110 if (ts->start_time)
2111 {
2112 if (ts->stop_time)
2113 {
2114 LONGEST run_time = ts->stop_time - ts->start_time;
2115
2116 /* Reporting a run time is more readable than two long numbers. */
2117 printf_filtered (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
f30aa5af
DK
2118 (long int) (ts->start_time / 1000000),
2119 (long int) (ts->start_time % 1000000),
2120 (long int) (run_time / 1000000),
2121 (long int) (run_time % 1000000));
f196051f
SS
2122 }
2123 else
2124 printf_filtered (_("Trace started at %ld.%06ld secs.\n"),
f30aa5af
DK
2125 (long int) (ts->start_time / 1000000),
2126 (long int) (ts->start_time % 1000000));
f196051f
SS
2127 }
2128 else if (ts->stop_time)
2129 printf_filtered (_("Trace stopped at %ld.%06ld secs.\n"),
f30aa5af
DK
2130 (long int) (ts->stop_time / 1000000),
2131 (long int) (ts->stop_time % 1000000));
f196051f
SS
2132
2133 /* Now report any per-tracepoint status available. */
2134 tp_vec = all_tracepoints ();
2135
2136 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
2137 target_get_tracepoint_status (t, NULL);
2138
2139 VEC_free (breakpoint_p, tp_vec);
c906108c
SS
2140}
2141
f224b49d
VP
2142/* Report the trace status to uiout, in a way suitable for MI, and not
2143 suitable for CLI. If ON_STOP is true, suppress a few fields that
2144 are not meaningful in the -trace-stop response.
2145
2146 The implementation is essentially parallel to trace_status_command, but
2147 merging them will result in unreadable code. */
2148void
2149trace_status_mi (int on_stop)
2150{
79a45e25 2151 struct ui_out *uiout = current_uiout;
f224b49d
VP
2152 struct trace_status *ts = current_trace_status ();
2153 int status;
f224b49d
VP
2154
2155 status = target_get_trace_status (ts);
2156
f5911ea1 2157 if (status == -1 && ts->filename == NULL)
f224b49d
VP
2158 {
2159 ui_out_field_string (uiout, "supported", "0");
2160 return;
2161 }
2162
f5911ea1 2163 if (ts->filename != NULL)
f224b49d
VP
2164 ui_out_field_string (uiout, "supported", "file");
2165 else if (!on_stop)
2166 ui_out_field_string (uiout, "supported", "1");
2167
f5911ea1
HAQ
2168 if (ts->filename != NULL)
2169 ui_out_field_string (uiout, "trace-file", ts->filename);
2170
f224b49d
VP
2171 gdb_assert (ts->running_known);
2172
2173 if (ts->running)
2174 {
2175 ui_out_field_string (uiout, "running", "1");
2176
2177 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
2178 Given that the frontend gets the status either on -trace-stop, or from
2179 -trace-status after re-connection, it does not seem like this
2180 information is necessary for anything. It is not necessary for either
2181 figuring the vital state of the target nor for navigation of trace
2182 frames. If the frontend wants to show the current state is some
2183 configure dialog, it can request the value when such dialog is
2184 invoked by the user. */
2185 }
2186 else
2187 {
2188 char *stop_reason = NULL;
2189 int stopping_tracepoint = -1;
2190
2191 if (!on_stop)
2192 ui_out_field_string (uiout, "running", "0");
2193
2194 if (ts->stop_reason != trace_stop_reason_unknown)
2195 {
2196 switch (ts->stop_reason)
2197 {
2198 case tstop_command:
2199 stop_reason = "request";
2200 break;
2201 case trace_buffer_full:
2202 stop_reason = "overflow";
2203 break;
2204 case trace_disconnected:
2205 stop_reason = "disconnection";
2206 break;
2207 case tracepoint_passcount:
2208 stop_reason = "passcount";
2209 stopping_tracepoint = ts->stopping_tracepoint;
2210 break;
6c28cbf2
SS
2211 case tracepoint_error:
2212 stop_reason = "error";
2213 stopping_tracepoint = ts->stopping_tracepoint;
2214 break;
f224b49d
VP
2215 }
2216
2217 if (stop_reason)
2218 {
2219 ui_out_field_string (uiout, "stop-reason", stop_reason);
2220 if (stopping_tracepoint != -1)
2221 ui_out_field_int (uiout, "stopping-tracepoint",
2222 stopping_tracepoint);
6c28cbf2
SS
2223 if (ts->stop_reason == tracepoint_error)
2224 ui_out_field_string (uiout, "error-description",
f196051f 2225 ts->stop_desc);
f224b49d
VP
2226 }
2227 }
2228 }
2229
a97153c7 2230 if (ts->traceframe_count != -1)
f224b49d 2231 ui_out_field_int (uiout, "frames", ts->traceframe_count);
87290684
SS
2232 if (ts->traceframes_created != -1)
2233 ui_out_field_int (uiout, "frames-created", ts->traceframes_created);
a97153c7
PA
2234 if (ts->buffer_size != -1)
2235 ui_out_field_int (uiout, "buffer-size", ts->buffer_size);
2236 if (ts->buffer_free != -1)
2237 ui_out_field_int (uiout, "buffer-free", ts->buffer_free);
2238
2239 ui_out_field_int (uiout, "disconnected", ts->disconnected_tracing);
2240 ui_out_field_int (uiout, "circular", ts->circular_buffer);
f196051f
SS
2241
2242 ui_out_field_string (uiout, "user-name", ts->user_name);
2243 ui_out_field_string (uiout, "notes", ts->notes);
2244
2245 {
2246 char buf[100];
2247
2248 xsnprintf (buf, sizeof buf, "%ld.%06ld",
f30aa5af
DK
2249 (long int) (ts->start_time / 1000000),
2250 (long int) (ts->start_time % 1000000));
f196051f
SS
2251 ui_out_field_string (uiout, "start-time", buf);
2252 xsnprintf (buf, sizeof buf, "%ld.%06ld",
f30aa5af
DK
2253 (long int) (ts->stop_time / 1000000),
2254 (long int) (ts->stop_time % 1000000));
f196051f
SS
2255 ui_out_field_string (uiout, "stop-time", buf);
2256 }
f224b49d
VP
2257}
2258
2f9d54cf
PA
2259/* Check if a trace run is ongoing. If so, and FROM_TTY, query the
2260 user if she really wants to detach. */
2261
d5551862 2262void
2f9d54cf 2263query_if_trace_running (int from_tty)
d5551862 2264{
2f9d54cf
PA
2265 if (!from_tty)
2266 return;
2267
00bf0b85
SS
2268 /* It can happen that the target that was tracing went away on its
2269 own, and we didn't notice. Get a status update, and if the
2270 current target doesn't even do tracing, then assume it's not
2271 running anymore. */
26afc0d7 2272 if (target_get_trace_status (current_trace_status ()) < 0)
00bf0b85
SS
2273 current_trace_status ()->running = 0;
2274
33da3f1c
SS
2275 /* If running interactively, give the user the option to cancel and
2276 then decide what to do differently with the run. Scripts are
2277 just going to disconnect and let the target deal with it,
2278 according to how it's been instructed previously via
2279 disconnected-tracing. */
2f9d54cf 2280 if (current_trace_status ()->running)
d5551862 2281 {
bfccc43c
YQ
2282 process_tracepoint_on_disconnect ();
2283
33da3f1c
SS
2284 if (current_trace_status ()->disconnected_tracing)
2285 {
3e43a32a
MS
2286 if (!query (_("Trace is running and will "
2287 "continue after detach; detach anyway? ")))
33da3f1c
SS
2288 error (_("Not confirmed."));
2289 }
2290 else
2291 {
3e43a32a
MS
2292 if (!query (_("Trace is running but will "
2293 "stop on detach; detach anyway? ")))
33da3f1c
SS
2294 error (_("Not confirmed."));
2295 }
d5551862 2296 }
2f9d54cf 2297}
8b9b7ef8 2298
2f9d54cf
PA
2299/* This function handles the details of what to do about an ongoing
2300 tracing run if the user has asked to detach or otherwise disconnect
2301 from the target. */
2302
2303void
2304disconnect_tracing (void)
2305{
8b9b7ef8
SS
2306 /* Also we want to be out of tfind mode, otherwise things can get
2307 confusing upon reconnection. Just use these calls instead of
2308 full tfind_1 behavior because we're in the middle of detaching,
2309 and there's no point to updating current stack frame etc. */
aef525cb 2310 trace_reset_local_state ();
d5551862
SS
2311}
2312
d183932d 2313/* Worker function for the various flavors of the tfind command. */
f197e0f1
VP
2314void
2315tfind_1 (enum trace_find_type type, int num,
cc5925ad 2316 CORE_ADDR addr1, CORE_ADDR addr2,
f197e0f1 2317 int from_tty)
c906108c
SS
2318{
2319 int target_frameno = -1, target_tracept = -1;
2ce6d6bf 2320 struct frame_id old_frame_id = null_frame_id;
d9b3f62e 2321 struct tracepoint *tp;
79a45e25 2322 struct ui_out *uiout = current_uiout;
c906108c 2323
2ce6d6bf
SS
2324 /* Only try to get the current stack frame if we have a chance of
2325 succeeding. In particular, if we're trying to get a first trace
2326 frame while all threads are running, it's not going to succeed,
2327 so leave it with a default value and let the frame comparison
2328 below (correctly) decide to print out the source location of the
2329 trace frame. */
2330 if (!(type == tfind_number && num == -1)
2331 && (has_stack_frames () || traceframe_number >= 0))
2332 old_frame_id = get_frame_id (get_current_frame ());
c906108c 2333
35b1e5cc
SS
2334 target_frameno = target_trace_find (type, num, addr1, addr2,
2335 &target_tracept);
2336
2337 if (type == tfind_number
2338 && num == -1
2339 && target_frameno == -1)
2340 {
2341 /* We told the target to get out of tfind mode, and it did. */
2342 }
2343 else if (target_frameno == -1)
2344 {
2ce6d6bf 2345 /* A request for a non-existent trace frame has failed.
35b1e5cc
SS
2346 Our response will be different, depending on FROM_TTY:
2347
2348 If FROM_TTY is true, meaning that this command was
2349 typed interactively by the user, then give an error
2350 and DO NOT change the state of traceframe_number etc.
2351
2352 However if FROM_TTY is false, meaning that we're either
2353 in a script, a loop, or a user-defined command, then
2354 DON'T give an error, but DO change the state of
2355 traceframe_number etc. to invalid.
2356
2357 The rationalle is that if you typed the command, you
2358 might just have committed a typo or something, and you'd
2359 like to NOT lose your current debugging state. However
2360 if you're in a user-defined command or especially in a
2361 loop, then you need a way to detect that the command
2362 failed WITHOUT aborting. This allows you to write
2363 scripts that search thru the trace buffer until the end,
2364 and then continue on to do something else. */
2365
2366 if (from_tty)
2367 error (_("Target failed to find requested trace frame."));
2368 else
2369 {
2370 if (info_verbose)
2371 printf_filtered ("End of trace buffer.\n");
c378eb4e 2372#if 0 /* dubious now? */
35b1e5cc
SS
2373 /* The following will not recurse, since it's
2374 special-cased. */
2375 trace_find_command ("-1", from_tty);
2376#endif
2377 }
2378 }
2379
d5551862
SS
2380 tp = get_tracepoint_by_number_on_target (target_tracept);
2381
35f196d9 2382 reinit_frame_cache ();
2f4d8875 2383 target_dcache_invalidate ();
8d735b87 2384
201b4506
YQ
2385 set_tracepoint_num (tp ? tp->base.number : target_tracept);
2386
2387 if (target_frameno != get_traceframe_number ())
2388 observer_notify_traceframe_changed (target_frameno, tracepoint_number);
2389
8d735b87
YQ
2390 set_current_traceframe (target_frameno);
2391
c906108c 2392 if (target_frameno == -1)
fb14de7b 2393 set_traceframe_context (NULL);
c906108c 2394 else
fb14de7b 2395 set_traceframe_context (get_current_frame ());
c906108c 2396
f197e0f1
VP
2397 if (traceframe_number >= 0)
2398 {
2399 /* Use different branches for MI and CLI to make CLI messages
2400 i18n-eable. */
2401 if (ui_out_is_mi_like_p (uiout))
2402 {
2403 ui_out_field_string (uiout, "found", "1");
2404 ui_out_field_int (uiout, "tracepoint", tracepoint_number);
2405 ui_out_field_int (uiout, "traceframe", traceframe_number);
2406 }
2407 else
2408 {
2409 printf_unfiltered (_("Found trace frame %d, tracepoint %d\n"),
2410 traceframe_number, tracepoint_number);
2411 }
2412 }
2413 else
2414 {
2415 if (ui_out_is_mi_like_p (uiout))
2416 ui_out_field_string (uiout, "found", "0");
4136fdd2
SS
2417 else if (type == tfind_number && num == -1)
2418 printf_unfiltered (_("No longer looking at any trace frame\n"));
c378eb4e 2419 else /* This case may never occur, check. */
4136fdd2 2420 printf_unfiltered (_("No trace frame found\n"));
f197e0f1
VP
2421 }
2422
00bf0b85
SS
2423 /* If we're in nonstop mode and getting out of looking at trace
2424 frames, there won't be any current frame to go back to and
2425 display. */
2426 if (from_tty
2427 && (has_stack_frames () || traceframe_number >= 0))
c906108c 2428 {
0faf0076 2429 enum print_what print_what;
c906108c 2430
2ce6d6bf 2431 /* NOTE: in imitation of the step command, try to determine
d183932d
MS
2432 whether we have made a transition from one function to
2433 another. If so, we'll print the "stack frame" (ie. the new
2434 function and it's arguments) -- otherwise we'll just show the
fb14de7b
UW
2435 new source line. */
2436
2437 if (frame_id_eq (old_frame_id,
2438 get_frame_id (get_current_frame ())))
0faf0076 2439 print_what = SRC_LINE;
c906108c 2440 else
0faf0076 2441 print_what = SRC_AND_LOC;
c906108c 2442
08d72866 2443 print_stack_frame (get_selected_frame (NULL), 1, print_what, 1);
c906108c
SS
2444 do_displays ();
2445 }
2446}
2447
2448/* trace_find_command takes a trace frame number n,
2449 sends "QTFrame:<n>" to the target,
2450 and accepts a reply that may contain several optional pieces
2451 of information: a frame number, a tracepoint number, and an
2452 indication of whether this is a trap frame or a stepping frame.
2453
2454 The minimal response is just "OK" (which indicates that the
2455 target does not give us a frame number or a tracepoint number).
2456 Instead of that, the target may send us a string containing
2457 any combination of:
c5aa993b
JM
2458 F<hexnum> (gives the selected frame number)
2459 T<hexnum> (gives the selected tracepoint number)
2460 */
c906108c
SS
2461
2462/* tfind command */
2463static void
fba45db2 2464trace_find_command (char *args, int from_tty)
c378eb4e 2465{ /* This should only be called with a numeric argument. */
c906108c 2466 int frameno = -1;
c906108c 2467
f5911ea1
HAQ
2468 if (current_trace_status ()->running
2469 && current_trace_status ()->filename == NULL)
a73c6dcd 2470 error (_("May not look at trace frames while trace is running."));
35b1e5cc
SS
2471
2472 if (args == 0 || *args == 0)
2473 { /* TFIND with no args means find NEXT trace frame. */
2474 if (traceframe_number == -1)
c378eb4e 2475 frameno = 0; /* "next" is first one. */
35b1e5cc
SS
2476 else
2477 frameno = traceframe_number + 1;
2478 }
2479 else if (0 == strcmp (args, "-"))
c906108c 2480 {
35b1e5cc
SS
2481 if (traceframe_number == -1)
2482 error (_("not debugging trace buffer"));
2483 else if (from_tty && traceframe_number == 0)
2484 error (_("already at start of trace buffer"));
2485
2486 frameno = traceframe_number - 1;
2487 }
2488 /* A hack to work around eval's need for fp to have been collected. */
2489 else if (0 == strcmp (args, "-1"))
2490 frameno = -1;
2491 else
2492 frameno = parse_and_eval_long (args);
c906108c 2493
35b1e5cc
SS
2494 if (frameno < -1)
2495 error (_("invalid input (%d is less than zero)"), frameno);
c906108c 2496
f197e0f1 2497 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
c906108c
SS
2498}
2499
2500/* tfind end */
2501static void
fba45db2 2502trace_find_end_command (char *args, int from_tty)
c906108c
SS
2503{
2504 trace_find_command ("-1", from_tty);
2505}
2506
c906108c
SS
2507/* tfind start */
2508static void
fba45db2 2509trace_find_start_command (char *args, int from_tty)
c906108c
SS
2510{
2511 trace_find_command ("0", from_tty);
2512}
2513
2514/* tfind pc command */
2515static void
fba45db2 2516trace_find_pc_command (char *args, int from_tty)
d183932d 2517{
c906108c 2518 CORE_ADDR pc;
c906108c 2519
f5911ea1
HAQ
2520 if (current_trace_status ()->running
2521 && current_trace_status ()->filename == NULL)
a73c6dcd 2522 error (_("May not look at trace frames while trace is running."));
c906108c 2523
35b1e5cc
SS
2524 if (args == 0 || *args == 0)
2525 pc = regcache_read_pc (get_current_regcache ());
c906108c 2526 else
35b1e5cc
SS
2527 pc = parse_and_eval_address (args);
2528
f197e0f1 2529 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
c906108c
SS
2530}
2531
2532/* tfind tracepoint command */
2533static void
fba45db2 2534trace_find_tracepoint_command (char *args, int from_tty)
d183932d 2535{
c906108c 2536 int tdp;
d9b3f62e 2537 struct tracepoint *tp;
c906108c 2538
f5911ea1
HAQ
2539 if (current_trace_status ()->running
2540 && current_trace_status ()->filename == NULL)
a73c6dcd 2541 error (_("May not look at trace frames while trace is running."));
383e5f85 2542
35b1e5cc
SS
2543 if (args == 0 || *args == 0)
2544 {
2545 if (tracepoint_number == -1)
2546 error (_("No current tracepoint -- please supply an argument."));
c906108c 2547 else
c378eb4e 2548 tdp = tracepoint_number; /* Default is current TDP. */
c906108c
SS
2549 }
2550 else
35b1e5cc
SS
2551 tdp = parse_and_eval_long (args);
2552
2553 /* If we have the tracepoint on hand, use the number that the
2554 target knows about (which may be different if we disconnected
2555 and reconnected). */
2556 tp = get_tracepoint (tdp);
2557 if (tp)
2558 tdp = tp->number_on_target;
2559
f197e0f1 2560 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
c906108c
SS
2561}
2562
2563/* TFIND LINE command:
c5aa993b 2564
c906108c 2565 This command will take a sourceline for argument, just like BREAK
d183932d 2566 or TRACE (ie. anything that "decode_line_1" can handle).
c5aa993b 2567
c906108c
SS
2568 With no argument, this command will find the next trace frame
2569 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2570
2571static void
fba45db2 2572trace_find_line_command (char *args, int from_tty)
d183932d 2573{
c906108c
SS
2574 static CORE_ADDR start_pc, end_pc;
2575 struct symtabs_and_lines sals;
2576 struct symtab_and_line sal;
c906108c
SS
2577 struct cleanup *old_chain;
2578
f5911ea1
HAQ
2579 if (current_trace_status ()->running
2580 && current_trace_status ()->filename == NULL)
a73c6dcd 2581 error (_("May not look at trace frames while trace is running."));
5af949e3 2582
35b1e5cc
SS
2583 if (args == 0 || *args == 0)
2584 {
2585 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
2586 sals.nelts = 1;
2587 sals.sals = (struct symtab_and_line *)
2588 xmalloc (sizeof (struct symtab_and_line));
2589 sals.sals[0] = sal;
2590 }
2591 else
42e08e69 2592 {
39cf75f7 2593 sals = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
35b1e5cc
SS
2594 sal = sals.sals[0];
2595 }
2596
2597 old_chain = make_cleanup (xfree, sals.sals);
2598 if (sal.symtab == 0)
42e08e69
SS
2599 error (_("No line number information available."));
2600
2601 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
35b1e5cc
SS
2602 {
2603 if (start_pc == end_pc)
2604 {
2605 printf_filtered ("Line %d of \"%s\"",
05cba821
JK
2606 sal.line,
2607 symtab_to_filename_for_display (sal.symtab));
35b1e5cc
SS
2608 wrap_here (" ");
2609 printf_filtered (" is at address ");
2610 print_address (get_current_arch (), start_pc, gdb_stdout);
2611 wrap_here (" ");
2612 printf_filtered (" but contains no code.\n");
2613 sal = find_pc_line (start_pc, 0);
2614 if (sal.line > 0
2615 && find_line_pc_range (sal, &start_pc, &end_pc)
2616 && start_pc != end_pc)
2617 printf_filtered ("Attempting to find line %d instead.\n",
2618 sal.line);
2619 else
2620 error (_("Cannot find a good line."));
2621 }
2622 }
2623 else
2624 /* Is there any case in which we get here, and have an address
2625 which the user would want to see? If we have debugging
2626 symbols and no line numbers? */
2627 error (_("Line number %d is out of range for \"%s\"."),
05cba821 2628 sal.line, symtab_to_filename_for_display (sal.symtab));
35b1e5cc
SS
2629
2630 /* Find within range of stated line. */
2631 if (args && *args)
f197e0f1 2632 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
c906108c 2633 else
f197e0f1 2634 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
35b1e5cc 2635 do_cleanups (old_chain);
c906108c
SS
2636}
2637
2638/* tfind range command */
2639static void
fba45db2 2640trace_find_range_command (char *args, int from_tty)
104c1213 2641{
c906108c
SS
2642 static CORE_ADDR start, stop;
2643 char *tmp;
2644
f5911ea1
HAQ
2645 if (current_trace_status ()->running
2646 && current_trace_status ()->filename == NULL)
a73c6dcd 2647 error (_("May not look at trace frames while trace is running."));
c906108c 2648
35b1e5cc
SS
2649 if (args == 0 || *args == 0)
2650 { /* XXX FIXME: what should default behavior be? */
2651 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2652 return;
2653 }
c906108c 2654
35b1e5cc
SS
2655 if (0 != (tmp = strchr (args, ',')))
2656 {
c378eb4e 2657 *tmp++ = '\0'; /* Terminate start address. */
529480d0 2658 tmp = skip_spaces (tmp);
35b1e5cc
SS
2659 start = parse_and_eval_address (args);
2660 stop = parse_and_eval_address (tmp);
c906108c
SS
2661 }
2662 else
c378eb4e 2663 { /* No explicit end address? */
35b1e5cc
SS
2664 start = parse_and_eval_address (args);
2665 stop = start + 1; /* ??? */
2666 }
2667
f197e0f1 2668 tfind_1 (tfind_range, 0, start, stop, from_tty);
c906108c
SS
2669}
2670
2671/* tfind outside command */
2672static void
fba45db2 2673trace_find_outside_command (char *args, int from_tty)
104c1213 2674{
c906108c
SS
2675 CORE_ADDR start, stop;
2676 char *tmp;
2677
f5911ea1
HAQ
2678 if (current_trace_status ()->running
2679 && current_trace_status ()->filename == NULL)
a73c6dcd 2680 error (_("May not look at trace frames while trace is running."));
c906108c 2681
35b1e5cc 2682 if (args == 0 || *args == 0)
c378eb4e 2683 { /* XXX FIXME: what should default behavior be? */
35b1e5cc
SS
2684 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2685 return;
2686 }
c906108c 2687
35b1e5cc
SS
2688 if (0 != (tmp = strchr (args, ',')))
2689 {
c378eb4e 2690 *tmp++ = '\0'; /* Terminate start address. */
529480d0 2691 tmp = skip_spaces (tmp);
35b1e5cc
SS
2692 start = parse_and_eval_address (args);
2693 stop = parse_and_eval_address (tmp);
c906108c
SS
2694 }
2695 else
c378eb4e 2696 { /* No explicit end address? */
35b1e5cc
SS
2697 start = parse_and_eval_address (args);
2698 stop = start + 1; /* ??? */
2699 }
2700
f197e0f1 2701 tfind_1 (tfind_outside, 0, start, stop, from_tty);
c906108c
SS
2702}
2703
c906108c
SS
2704/* info scope command: list the locals for a scope. */
2705static void
fba45db2 2706scope_info (char *args, int from_tty)
c906108c 2707{
c906108c
SS
2708 struct symtabs_and_lines sals;
2709 struct symbol *sym;
2710 struct minimal_symbol *msym;
2711 struct block *block;
0d5cff50
DE
2712 const char *symname;
2713 char *save_args = args;
8157b174 2714 struct block_iterator iter;
de4f826b 2715 int j, count = 0;
768a979c
UW
2716 struct gdbarch *gdbarch;
2717 int regno;
c906108c
SS
2718
2719 if (args == 0 || *args == 0)
3e43a32a
MS
2720 error (_("requires an argument (function, "
2721 "line or *addr) to define a scope"));
c906108c 2722
f8eba3c6 2723 sals = decode_line_1 (&args, DECODE_LINE_FUNFIRSTLINE, NULL, 0);
c906108c 2724 if (sals.nelts == 0)
c378eb4e 2725 return; /* Presumably decode_line_1 has already warned. */
c906108c 2726
c378eb4e 2727 /* Resolve line numbers to PC. */
c906108c
SS
2728 resolve_sal_pc (&sals.sals[0]);
2729 block = block_for_pc (sals.sals[0].pc);
2730
2731 while (block != 0)
2732 {
c378eb4e 2733 QUIT; /* Allow user to bail out with ^C. */
de4f826b 2734 ALL_BLOCK_SYMBOLS (block, iter, sym)
c906108c 2735 {
c378eb4e 2736 QUIT; /* Allow user to bail out with ^C. */
c906108c
SS
2737 if (count == 0)
2738 printf_filtered ("Scope for %s:\n", save_args);
2739 count++;
e88c90f2 2740
3567439c 2741 symname = SYMBOL_PRINT_NAME (sym);
c906108c 2742 if (symname == NULL || *symname == '\0')
c378eb4e 2743 continue; /* Probably botched, certainly useless. */
c906108c 2744
768a979c
UW
2745 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
2746
c906108c 2747 printf_filtered ("Symbol %s is ", symname);
24d6c2a0
TT
2748
2749 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
2750 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym,
2751 BLOCK_START (block),
2752 gdb_stdout);
2753 else
c5aa993b 2754 {
24d6c2a0 2755 switch (SYMBOL_CLASS (sym))
c5aa993b 2756 {
24d6c2a0
TT
2757 default:
2758 case LOC_UNDEF: /* Messed up symbol? */
2759 printf_filtered ("a bogus symbol, class %d.\n",
2760 SYMBOL_CLASS (sym));
2761 count--; /* Don't count this one. */
2762 continue;
2763 case LOC_CONST:
2764 printf_filtered ("a constant with value %s (%s)",
2765 plongest (SYMBOL_VALUE (sym)),
2766 hex_string (SYMBOL_VALUE (sym)));
2767 break;
2768 case LOC_CONST_BYTES:
2769 printf_filtered ("constant bytes: ");
2770 if (SYMBOL_TYPE (sym))
2771 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2772 fprintf_filtered (gdb_stdout, " %02x",
2773 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2774 break;
2775 case LOC_STATIC:
2776 printf_filtered ("in static storage at address ");
2777 printf_filtered ("%s", paddress (gdbarch,
2778 SYMBOL_VALUE_ADDRESS (sym)));
2779 break;
2780 case LOC_REGISTER:
2781 /* GDBARCH is the architecture associated with the objfile
2782 the symbol is defined in; the target architecture may be
2783 different, and may provide additional registers. However,
2784 we do not know the target architecture at this point.
2785 We assume the objfile architecture will contain all the
2786 standard registers that occur in debug info in that
2787 objfile. */
2788 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2789 gdbarch);
2790
2791 if (SYMBOL_IS_ARGUMENT (sym))
2792 printf_filtered ("an argument in register $%s",
2793 gdbarch_register_name (gdbarch, regno));
2794 else
2795 printf_filtered ("a local variable in register $%s",
2796 gdbarch_register_name (gdbarch, regno));
2797 break;
2798 case LOC_ARG:
2799 printf_filtered ("an argument at stack/frame offset %s",
2800 plongest (SYMBOL_VALUE (sym)));
2801 break;
2802 case LOC_LOCAL:
2803 printf_filtered ("a local variable at frame offset %s",
2804 plongest (SYMBOL_VALUE (sym)));
2805 break;
2806 case LOC_REF_ARG:
2807 printf_filtered ("a reference argument at offset %s",
2808 plongest (SYMBOL_VALUE (sym)));
2809 break;
2810 case LOC_REGPARM_ADDR:
2811 /* Note comment at LOC_REGISTER. */
2812 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2813 gdbarch);
2814 printf_filtered ("the address of an argument, in register $%s",
2815 gdbarch_register_name (gdbarch, regno));
2816 break;
2817 case LOC_TYPEDEF:
2818 printf_filtered ("a typedef.\n");
2819 continue;
2820 case LOC_LABEL:
2821 printf_filtered ("a label at address ");
2822 printf_filtered ("%s", paddress (gdbarch,
2823 SYMBOL_VALUE_ADDRESS (sym)));
2824 break;
2825 case LOC_BLOCK:
2826 printf_filtered ("a function at address ");
5af949e3 2827 printf_filtered ("%s",
24d6c2a0
TT
2828 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
2829 break;
2830 case LOC_UNRESOLVED:
2831 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
2832 NULL, NULL);
2833 if (msym == NULL)
2834 printf_filtered ("Unresolved Static");
2835 else
2836 {
2837 printf_filtered ("static storage at address ");
2838 printf_filtered ("%s",
2839 paddress (gdbarch,
2840 SYMBOL_VALUE_ADDRESS (msym)));
2841 }
2842 break;
2843 case LOC_OPTIMIZED_OUT:
2844 printf_filtered ("optimized out.\n");
2845 continue;
2846 case LOC_COMPUTED:
2847 gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));
c5aa993b 2848 }
c5aa993b 2849 }
c906108c 2850 if (SYMBOL_TYPE (sym))
c5aa993b 2851 printf_filtered (", length %d.\n",
450bd37b 2852 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
c906108c
SS
2853 }
2854 if (BLOCK_FUNCTION (block))
2855 break;
2856 else
2857 block = BLOCK_SUPERBLOCK (block);
2858 }
2859 if (count <= 0)
2860 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2861 save_args);
2862}
2863
afd02f27
PA
2864/* Helper for trace_dump_command. Dump the action list starting at
2865 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2866 actions of the body of a while-stepping action. STEPPING_FRAME is
2867 set if the current traceframe was determined to be a while-stepping
2868 traceframe. */
2869
c906108c 2870static void
afd02f27
PA
2871trace_dump_actions (struct command_line *action,
2872 int stepping_actions, int stepping_frame,
2873 int from_tty)
c906108c 2874{
6f937416 2875 const char *action_exp, *next_comma;
c906108c 2876
afd02f27 2877 for (; action != NULL; action = action->next)
c906108c
SS
2878 {
2879 struct cmd_list_element *cmd;
2880
c378eb4e 2881 QUIT; /* Allow user to bail out with ^C. */
a7bdde9e 2882 action_exp = action->line;
6f937416 2883 action_exp = skip_spaces_const (action_exp);
c906108c
SS
2884
2885 /* The collection actions to be done while stepping are
c5aa993b 2886 bracketed by the commands "while-stepping" and "end". */
c906108c
SS
2887
2888 if (*action_exp == '#') /* comment line */
2889 continue;
2890
2891 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2892 if (cmd == 0)
8a3fe4f8 2893 error (_("Bad action list item: %s"), action_exp);
c906108c 2894
bbaca940 2895 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
afd02f27
PA
2896 {
2897 int i;
2898
2899 for (i = 0; i < action->body_count; ++i)
2900 trace_dump_actions (action->body_list[i],
2901 1, stepping_frame, from_tty);
2902 }
bbaca940 2903 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
c906108c
SS
2904 {
2905 /* Display the collected data.
d183932d
MS
2906 For the trap frame, display only what was collected at
2907 the trap. Likewise for stepping frames, display only
2908 what was collected while stepping. This means that the
2909 two boolean variables, STEPPING_FRAME and
2910 STEPPING_ACTIONS should be equal. */
c906108c
SS
2911 if (stepping_frame == stepping_actions)
2912 {
6f937416
PA
2913 char *cmd = NULL;
2914 struct cleanup *old_chain
2915 = make_cleanup (free_current_contents, &cmd);
92bc6a20 2916 int trace_string = 0;
6f937416 2917
3065dfb6 2918 if (*action_exp == '/')
92bc6a20 2919 action_exp = decode_agent_options (action_exp, &trace_string);
3065dfb6 2920
c5aa993b 2921 do
c378eb4e
MS
2922 { /* Repeat over a comma-separated list. */
2923 QUIT; /* Allow user to bail out with ^C. */
c5aa993b
JM
2924 if (*action_exp == ',')
2925 action_exp++;
6f937416 2926 action_exp = skip_spaces_const (action_exp);
c5aa993b
JM
2927
2928 next_comma = strchr (action_exp, ',');
2929
2930 if (0 == strncasecmp (action_exp, "$reg", 4))
2931 registers_info (NULL, from_tty);
6710bf39
SS
2932 else if (0 == strncasecmp (action_exp, "$_ret", 5))
2933 ;
c5aa993b
JM
2934 else if (0 == strncasecmp (action_exp, "$loc", 4))
2935 locals_info (NULL, from_tty);
2936 else if (0 == strncasecmp (action_exp, "$arg", 4))
2937 args_info (NULL, from_tty);
2938 else
2939 { /* variable */
6f937416 2940 if (next_comma != NULL)
c5aa993b 2941 {
6f937416
PA
2942 size_t len = next_comma - action_exp;
2943
2944 cmd = xrealloc (cmd, len + 1);
2945 memcpy (cmd, action_exp, len);
2946 cmd[len] = 0;
2947 }
2948 else
2949 {
2950 size_t len = strlen (action_exp);
2951
2952 cmd = xrealloc (cmd, len + 1);
2953 memcpy (cmd, action_exp, len + 1);
c5aa993b 2954 }
6f937416
PA
2955
2956 printf_filtered ("%s = ", cmd);
2957 output_command_const (cmd, from_tty);
c5aa993b
JM
2958 printf_filtered ("\n");
2959 }
c5aa993b
JM
2960 action_exp = next_comma;
2961 }
2962 while (action_exp && *action_exp == ',');
6f937416
PA
2963
2964 do_cleanups (old_chain);
c906108c
SS
2965 }
2966 }
2967 }
afd02f27
PA
2968}
2969
ddacd3c8
YQ
2970/* Return bp_location of the tracepoint associated with the current
2971 traceframe. Set *STEPPING_FRAME_P to 1 if the current traceframe
2972 is a stepping traceframe. */
2973
dc673c81 2974struct bp_location *
ddacd3c8
YQ
2975get_traceframe_location (int *stepping_frame_p)
2976{
2977 struct tracepoint *t;
2978 struct bp_location *tloc;
2979 struct regcache *regcache;
2980
2981 if (tracepoint_number == -1)
2982 error (_("No current trace frame."));
2983
2984 t = get_tracepoint (tracepoint_number);
2985
2986 if (t == NULL)
2987 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2988 tracepoint_number);
2989
2990 /* The current frame is a trap frame if the frame PC is equal to the
2991 tracepoint PC. If not, then the current frame was collected
2992 during single-stepping. */
2993 regcache = get_current_regcache ();
2994
2995 /* If the traceframe's address matches any of the tracepoint's
2996 locations, assume it is a direct hit rather than a while-stepping
2997 frame. (FIXME this is not reliable, should record each frame's
2998 type.) */
2999 for (tloc = t->base.loc; tloc; tloc = tloc->next)
3000 if (tloc->address == regcache_read_pc (regcache))
3001 {
3002 *stepping_frame_p = 0;
3003 return tloc;
3004 }
3005
3006 /* If this is a stepping frame, we don't know which location
3007 triggered. The first is as good (or bad) a guess as any... */
3008 *stepping_frame_p = 1;
3009 return t->base.loc;
3010}
3011
3ca73e0c
YQ
3012/* Return all the actions, including default collect, of a tracepoint
3013 T. It constructs cleanups into the chain, and leaves the caller to
3014 handle them (call do_cleanups). */
3015
3016static struct command_line *
3017all_tracepoint_actions_and_cleanup (struct breakpoint *t)
3018{
3019 struct command_line *actions;
3020
3021 actions = breakpoint_commands (t);
3022
3023 /* If there are default expressions to collect, make up a collect
3024 action and prepend to the action list to encode. Note that since
3025 validation is per-tracepoint (local var "xyz" might be valid for
3026 one tracepoint and not another, etc), we make up the action on
3027 the fly, and don't cache it. */
3028 if (*default_collect)
3029 {
3030 struct command_line *default_collect_action;
3031 char *default_collect_line;
3032
3033 default_collect_line = xstrprintf ("collect %s", default_collect);
3034 make_cleanup (xfree, default_collect_line);
3035
3036 validate_actionline (default_collect_line, t);
3037 default_collect_action = xmalloc (sizeof (struct command_line));
3038 make_cleanup (xfree, default_collect_action);
3039 default_collect_action->next = actions;
3040 default_collect_action->line = default_collect_line;
3041 actions = default_collect_action;
3042 }
3043
3044 return actions;
3045}
3046
afd02f27
PA
3047/* The tdump command. */
3048
3049static void
3050trace_dump_command (char *args, int from_tty)
3051{
afd02f27
PA
3052 int stepping_frame = 0;
3053 struct bp_location *loc;
4fd2d6af 3054 struct cleanup *old_chain;
ddacd3c8 3055 struct command_line *actions;
afd02f27 3056
ddacd3c8
YQ
3057 /* This throws an error is not inspecting a trace frame. */
3058 loc = get_traceframe_location (&stepping_frame);
afd02f27
PA
3059
3060 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
3061 tracepoint_number, traceframe_number);
3062
ddacd3c8 3063 old_chain = make_cleanup (null_cleanup, NULL);
de74e63a
YQ
3064
3065 /* This command only makes sense for the current frame, not the
3066 selected frame. */
3067 make_cleanup_restore_current_thread ();
3068 select_frame (get_current_frame ());
3069
ddacd3c8 3070 actions = all_tracepoint_actions_and_cleanup (loc->owner);
2114d44c
SS
3071
3072 trace_dump_actions (actions, 0, stepping_frame, from_tty);
3073
4fd2d6af 3074 do_cleanups (old_chain);
c906108c
SS
3075}
3076
409873ef
SS
3077/* Encode a piece of a tracepoint's source-level definition in a form
3078 that is suitable for both protocol and saving in files. */
3079/* This version does not do multiple encodes for long strings; it should
3080 return an offset to the next piece to encode. FIXME */
3081
3082extern int
3083encode_source_string (int tpnum, ULONGEST addr,
3084 char *srctype, char *src, char *buf, int buf_size)
3085{
3086 if (80 + strlen (srctype) > buf_size)
3087 error (_("Buffer too small for source encoding"));
3088 sprintf (buf, "%x:%s:%s:%x:%x:",
3e43a32a
MS
3089 tpnum, phex_nz (addr, sizeof (addr)),
3090 srctype, 0, (int) strlen (src));
409873ef
SS
3091 if (strlen (buf) + strlen (src) * 2 >= buf_size)
3092 error (_("Source string too long for buffer"));
bc20a4af 3093 bin2hex ((gdb_byte *) src, buf + strlen (buf), 0);
409873ef
SS
3094 return -1;
3095}
3096
3f43bc09 3097/* Free trace file writer. */
00bf0b85 3098
3f43bc09
YQ
3099static void
3100trace_file_writer_xfree (void *arg)
3101{
3102 struct trace_file_writer *writer = arg;
011aacb0 3103
3f43bc09
YQ
3104 writer->ops->dtor (writer);
3105 xfree (writer);
3106}
3107
3108/* TFILE trace writer. */
3109
3110struct tfile_trace_file_writer
00bf0b85 3111{
3f43bc09
YQ
3112 struct trace_file_writer base;
3113
3114 /* File pointer to tfile trace file. */
00bf0b85 3115 FILE *fp;
3f43bc09
YQ
3116 /* Path name of the tfile trace file. */
3117 char *pathname;
3118};
00bf0b85 3119
3f43bc09
YQ
3120/* This is the implementation of trace_file_write_ops method
3121 target_save. We just call the generic target
3122 target_save_trace_data to do target-side saving. */
00bf0b85 3123
3f43bc09
YQ
3124static int
3125tfile_target_save (struct trace_file_writer *self,
3126 const char *filename)
3127{
3128 int err = target_save_trace_data (filename);
3129
3130 return (err >= 0);
3131}
3132
3133/* This is the implementation of trace_file_write_ops method
3134 dtor. */
3135
3136static void
3137tfile_dtor (struct trace_file_writer *self)
3138{
3139 struct tfile_trace_file_writer *writer
3140 = (struct tfile_trace_file_writer *) self;
3141
3142 xfree (writer->pathname);
00bf0b85 3143
3f43bc09
YQ
3144 if (writer->fp != NULL)
3145 fclose (writer->fp);
3146}
3147
3148/* This is the implementation of trace_file_write_ops method
3149 start. It creates the trace file FILENAME and registers some
3150 cleanups. */
00bf0b85 3151
3f43bc09
YQ
3152static void
3153tfile_start (struct trace_file_writer *self, const char *filename)
3154{
3155 struct tfile_trace_file_writer *writer
3156 = (struct tfile_trace_file_writer *) self;
3157
3158 writer->pathname = tilde_expand (filename);
614c279d 3159 writer->fp = gdb_fopen_cloexec (writer->pathname, "wb");
3f43bc09 3160 if (writer->fp == NULL)
00bf0b85 3161 error (_("Unable to open file '%s' for saving trace data (%s)"),
3f12a589 3162 writer->pathname, safe_strerror (errno));
3f43bc09
YQ
3163}
3164
3165/* This is the implementation of trace_file_write_ops method
3166 write_header. Write the TFILE header. */
3167
3168static void
3169tfile_write_header (struct trace_file_writer *self)
3170{
3171 struct tfile_trace_file_writer *writer
3172 = (struct tfile_trace_file_writer *) self;
3173 int written;
00bf0b85
SS
3174
3175 /* Write a file header, with a high-bit-set char to indicate a
3176 binary file, plus a hint as what this file is, and a version
3177 number in case of future needs. */
3f43bc09 3178 written = fwrite ("\x7fTRACE0\n", 8, 1, writer->fp);
409873ef 3179 if (written < 1)
3f43bc09
YQ
3180 perror_with_name (writer->pathname);
3181}
00bf0b85 3182
3f43bc09
YQ
3183/* This is the implementation of trace_file_write_ops method
3184 write_regblock_type. Write the size of register block. */
00bf0b85 3185
3f43bc09
YQ
3186static void
3187tfile_write_regblock_type (struct trace_file_writer *self, int size)
3188{
3189 struct tfile_trace_file_writer *writer
3190 = (struct tfile_trace_file_writer *) self;
00bf0b85 3191
3f43bc09
YQ
3192 fprintf (writer->fp, "R %x\n", size);
3193}
3194
3195/* This is the implementation of trace_file_write_ops method
3196 write_status. */
3197
3198static void
3199tfile_write_status (struct trace_file_writer *self,
3200 struct trace_status *ts)
3201{
3202 struct tfile_trace_file_writer *writer
3203 = (struct tfile_trace_file_writer *) self;
3204
3205 fprintf (writer->fp, "status %c;%s",
6c28cbf2 3206 (ts->running ? '1' : '0'), stop_reason_names[ts->stop_reason]);
d6682f9e
YQ
3207 if (ts->stop_reason == tracepoint_error
3208 || ts->stop_reason == tstop_command)
6c28cbf2 3209 {
f196051f 3210 char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
5d502164 3211
f196051f 3212 bin2hex ((gdb_byte *) ts->stop_desc, buf, 0);
3f43bc09 3213 fprintf (writer->fp, ":%s", buf);
6c28cbf2 3214 }
3f43bc09 3215 fprintf (writer->fp, ":%x", ts->stopping_tracepoint);
4daf5ac0 3216 if (ts->traceframe_count >= 0)
3f43bc09 3217 fprintf (writer->fp, ";tframes:%x", ts->traceframe_count);
4daf5ac0 3218 if (ts->traceframes_created >= 0)
3f43bc09 3219 fprintf (writer->fp, ";tcreated:%x", ts->traceframes_created);
4daf5ac0 3220 if (ts->buffer_free >= 0)
3f43bc09 3221 fprintf (writer->fp, ";tfree:%x", ts->buffer_free);
4daf5ac0 3222 if (ts->buffer_size >= 0)
3f43bc09 3223 fprintf (writer->fp, ";tsize:%x", ts->buffer_size);
33da3f1c 3224 if (ts->disconnected_tracing)
3f43bc09 3225 fprintf (writer->fp, ";disconn:%x", ts->disconnected_tracing);
33da3f1c 3226 if (ts->circular_buffer)
3f43bc09 3227 fprintf (writer->fp, ";circular:%x", ts->circular_buffer);
cdba14e0
DK
3228 if (ts->start_time)
3229 {
3230 fprintf (writer->fp, ";starttime:%s",
3231 phex_nz (ts->start_time, sizeof (ts->start_time)));
3232 }
3233 if (ts->stop_time)
3234 {
3235 fprintf (writer->fp, ";stoptime:%s",
3236 phex_nz (ts->stop_time, sizeof (ts->stop_time)));
3237 }
a22fa6e4
YQ
3238 if (ts->notes != NULL)
3239 {
3240 char *buf = (char *) alloca (strlen (ts->notes) * 2 + 1);
3241
3242 bin2hex ((gdb_byte *) ts->notes, buf, 0);
3243 fprintf (writer->fp, ";notes:%s", buf);
3244 }
3245 if (ts->user_name != NULL)
3246 {
3247 char *buf = (char *) alloca (strlen (ts->user_name) * 2 + 1);
3248
3249 bin2hex ((gdb_byte *) ts->user_name, buf, 0);
3250 fprintf (writer->fp, ";username:%s", buf);
3251 }
3f43bc09
YQ
3252 fprintf (writer->fp, "\n");
3253}
3254
3255/* This is the implementation of trace_file_write_ops method
3256 write_uploaded_tsv. */
3257
3258static void
3259tfile_write_uploaded_tsv (struct trace_file_writer *self,
3260 struct uploaded_tsv *utsv)
3261{
3262 char *buf = "";
3263 struct tfile_trace_file_writer *writer
3264 = (struct tfile_trace_file_writer *) self;
3265
3266 if (utsv->name)
3267 {
3268 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
3269 bin2hex ((gdb_byte *) (utsv->name), buf, 0);
3270 }
3271
3272 fprintf (writer->fp, "tsv %x:%s:%x:%s\n",
3273 utsv->number, phex_nz (utsv->initial_value, 8),
3274 utsv->builtin, buf);
3275
3276 if (utsv->name)
3277 xfree (buf);
3278}
3279
3280#define MAX_TRACE_UPLOAD 2000
3281
3282/* This is the implementation of trace_file_write_ops method
3283 write_uploaded_tp. */
3284
3285static void
3286tfile_write_uploaded_tp (struct trace_file_writer *self,
3287 struct uploaded_tp *utp)
3288{
3289 struct tfile_trace_file_writer *writer
3290 = (struct tfile_trace_file_writer *) self;
3291 int a;
3292 char *act;
bc20a4af 3293 char buf[MAX_TRACE_UPLOAD];
3f43bc09
YQ
3294
3295 fprintf (writer->fp, "tp T%x:%s:%c:%x:%x",
3296 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
3297 (utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
3298 if (utp->type == bp_fast_tracepoint)
3299 fprintf (writer->fp, ":F%x", utp->orig_size);
3300 if (utp->cond)
3301 fprintf (writer->fp,
3302 ":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
3303 utp->cond);
3304 fprintf (writer->fp, "\n");
3305 for (a = 0; VEC_iterate (char_ptr, utp->actions, a, act); ++a)
3306 fprintf (writer->fp, "tp A%x:%s:%s\n",
3307 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
3308 for (a = 0; VEC_iterate (char_ptr, utp->step_actions, a, act); ++a)
3309 fprintf (writer->fp, "tp S%x:%s:%s\n",
3310 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
3311 if (utp->at_string)
3312 {
3313 encode_source_string (utp->number, utp->addr,
3314 "at", utp->at_string, buf, MAX_TRACE_UPLOAD);
3315 fprintf (writer->fp, "tp Z%s\n", buf);
3316 }
3317 if (utp->cond_string)
3318 {
3319 encode_source_string (utp->number, utp->addr,
3320 "cond", utp->cond_string,
3321 buf, MAX_TRACE_UPLOAD);
3322 fprintf (writer->fp, "tp Z%s\n", buf);
3323 }
3324 for (a = 0; VEC_iterate (char_ptr, utp->cmd_strings, a, act); ++a)
3325 {
3326 encode_source_string (utp->number, utp->addr, "cmd", act,
3327 buf, MAX_TRACE_UPLOAD);
3328 fprintf (writer->fp, "tp Z%s\n", buf);
3329 }
3330 fprintf (writer->fp, "tp V%x:%s:%x:%s\n",
3331 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
3332 utp->hit_count,
3333 phex_nz (utp->traceframe_usage,
3334 sizeof (utp->traceframe_usage)));
3335}
3336
3337/* This is the implementation of trace_file_write_ops method
3338 write_definition_end. */
3339
3340static void
3341tfile_write_definition_end (struct trace_file_writer *self)
3342{
3343 struct tfile_trace_file_writer *writer
3344 = (struct tfile_trace_file_writer *) self;
3345
3346 fprintf (writer->fp, "\n");
3347}
3348
3349/* This is the implementation of trace_file_write_ops method
3350 write_raw_data. */
3351
3352static void
3353tfile_write_raw_data (struct trace_file_writer *self, gdb_byte *buf,
3354 LONGEST len)
3355{
3356 struct tfile_trace_file_writer *writer
3357 = (struct tfile_trace_file_writer *) self;
3358
3359 if (fwrite (buf, len, 1, writer->fp) < 1)
3360 perror_with_name (writer->pathname);
3361}
3362
3363/* This is the implementation of trace_file_write_ops method
3364 end. */
3365
3366static void
3367tfile_end (struct trace_file_writer *self)
3368{
3369 struct tfile_trace_file_writer *writer
3370 = (struct tfile_trace_file_writer *) self;
3371 uint32_t gotten = 0;
3372
3373 /* Mark the end of trace data. */
3374 if (fwrite (&gotten, 4, 1, writer->fp) < 1)
3375 perror_with_name (writer->pathname);
3376}
3377
3378/* Operations to write trace buffers into TFILE format. */
3379
3380static const struct trace_file_write_ops tfile_write_ops =
3381{
3382 tfile_dtor,
3383 tfile_target_save,
3384 tfile_start,
3385 tfile_write_header,
3386 tfile_write_regblock_type,
3387 tfile_write_status,
3388 tfile_write_uploaded_tsv,
3389 tfile_write_uploaded_tp,
3390 tfile_write_definition_end,
3391 tfile_write_raw_data,
3392 NULL,
3393 tfile_end,
3394};
3395
3396/* Helper macros. */
3397
3398#define TRACE_WRITE_R_BLOCK(writer, buf, size) \
3399 writer->ops->frame_ops->write_r_block ((writer), (buf), (size))
3400#define TRACE_WRITE_M_BLOCK_HEADER(writer, addr, size) \
3401 writer->ops->frame_ops->write_m_block_header ((writer), (addr), \
3402 (size))
3403#define TRACE_WRITE_M_BLOCK_MEMORY(writer, buf, size) \
3404 writer->ops->frame_ops->write_m_block_memory ((writer), (buf), \
3405 (size))
3406#define TRACE_WRITE_V_BLOCK(writer, num, val) \
3407 writer->ops->frame_ops->write_v_block ((writer), (num), (val))
3408
3f43bc09
YQ
3409/* Save tracepoint data to file named FILENAME through WRITER. WRITER
3410 determines the trace file format. If TARGET_DOES_SAVE is non-zero,
3411 the save is performed on the target, otherwise GDB obtains all trace
3412 data and saves it locally. */
3413
3414static void
3415trace_save (const char *filename, struct trace_file_writer *writer,
3416 int target_does_save)
3417{
3418 struct trace_status *ts = current_trace_status ();
3419 int status;
3420 struct uploaded_tp *uploaded_tps = NULL, *utp;
3421 struct uploaded_tsv *uploaded_tsvs = NULL, *utsv;
3422
3423 ULONGEST offset = 0;
3424 gdb_byte buf[MAX_TRACE_UPLOAD];
bc20a4af 3425#define MAX_TRACE_UPLOAD 2000
3f43bc09
YQ
3426 int written;
3427 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
3428
3429 /* If the target is to save the data to a file on its own, then just
3430 send the command and be done with it. */
3431 if (target_does_save)
3432 {
3433 if (!writer->ops->target_save (writer, filename))
3434 error (_("Target failed to save trace data to '%s'."),
3435 filename);
3436 return;
3437 }
3438
3439 /* Get the trace status first before opening the file, so if the
3440 target is losing, we can get out without touching files. */
3441 status = target_get_trace_status (ts);
3442
3cf62c1d
KS
3443 writer->ops->start (writer, filename);
3444
3f43bc09
YQ
3445 writer->ops->write_header (writer);
3446
3447 /* Write descriptive info. */
3448
3449 /* Write out the size of a register block. */
3450 writer->ops->write_regblock_type (writer, trace_regblock_size);
3451
3452 /* Write out status of the tracing run (aka "tstatus" info). */
3453 writer->ops->write_status (writer, ts);
00bf0b85
SS
3454
3455 /* Note that we want to upload tracepoints and save those, rather
3456 than simply writing out the local ones, because the user may have
3457 changed tracepoints in GDB in preparation for a future tracing
3458 run, or maybe just mass-deleted all types of breakpoints as part
3459 of cleaning up. So as not to contaminate the session, leave the
3460 data in its uploaded form, don't make into real tracepoints. */
3461
3462 /* Get trace state variables first, they may be checked when parsing
3463 uploaded commands. */
3464
3465 target_upload_trace_state_variables (&uploaded_tsvs);
3466
3467 for (utsv = uploaded_tsvs; utsv; utsv = utsv->next)
3f43bc09 3468 writer->ops->write_uploaded_tsv (writer, utsv);
00bf0b85
SS
3469
3470 free_uploaded_tsvs (&uploaded_tsvs);
3471
3472 target_upload_tracepoints (&uploaded_tps);
3473
f196051f
SS
3474 for (utp = uploaded_tps; utp; utp = utp->next)
3475 target_get_tracepoint_status (NULL, utp);
3476
00bf0b85 3477 for (utp = uploaded_tps; utp; utp = utp->next)
3f43bc09
YQ
3478 writer->ops->write_uploaded_tp (writer, utp);
3479
3480 free_uploaded_tps (&uploaded_tps);
3481
3482 /* Mark the end of the definition section. */
3483 writer->ops->write_definition_end (writer);
3484
3485 /* Get and write the trace data proper. */
3486 while (1)
00bf0b85 3487 {
3f43bc09
YQ
3488 LONGEST gotten = 0;
3489
3490 /* The writer supports writing the contents of trace buffer
3491 directly to trace file. Don't parse the contents of trace
3492 buffer. */
3493 if (writer->ops->write_trace_buffer != NULL)
409873ef 3494 {
3f43bc09
YQ
3495 /* We ask for big blocks, in the hopes of efficiency, but
3496 will take less if the target has packet size limitations
3497 or some such. */
3498 gotten = target_get_raw_trace_data (buf, offset,
3499 MAX_TRACE_UPLOAD);
3500 if (gotten < 0)
3501 error (_("Failure to get requested trace buffer data"));
3502 /* No more data is forthcoming, we're done. */
3503 if (gotten == 0)
3504 break;
3505
3506 writer->ops->write_trace_buffer (writer, buf, gotten);
3507
3508 offset += gotten;
409873ef 3509 }
3f43bc09 3510 else
409873ef 3511 {
3f43bc09
YQ
3512 uint16_t tp_num;
3513 uint32_t tf_size;
3514 /* Parse the trace buffers according to how data are stored
3515 in trace buffer in GDBserver. */
3516
3517 gotten = target_get_raw_trace_data (buf, offset, 6);
3518
3519 if (gotten == 0)
3520 break;
3521
3522 /* Read the first six bytes in, which is the tracepoint
3523 number and trace frame size. */
3524 tp_num = (uint16_t)
3525 extract_unsigned_integer (&buf[0], 2, byte_order);
3526
3527 tf_size = (uint32_t)
3528 extract_unsigned_integer (&buf[2], 4, byte_order);
3529
3530 writer->ops->frame_ops->start (writer, tp_num);
3531 gotten = 6;
3532
3533 if (tf_size > 0)
3534 {
3535 unsigned int block;
3536
3537 offset += 6;
3538
3539 for (block = 0; block < tf_size; )
3540 {
3541 gdb_byte block_type;
3542
3543 /* We'll fetch one block each time, in order to
3544 handle the extremely large 'M' block. We first
3545 fetch one byte to get the type of the block. */
3546 gotten = target_get_raw_trace_data (buf, offset, 1);
3547 if (gotten < 1)
3548 error (_("Failure to get requested trace buffer data"));
3549
3550 gotten = 1;
3551 block += 1;
3552 offset += 1;
3553
3554 block_type = buf[0];
3555 switch (block_type)
3556 {
3557 case 'R':
3558 gotten
3559 = target_get_raw_trace_data (buf, offset,
3560 trace_regblock_size);
3561 if (gotten < trace_regblock_size)
3562 error (_("Failure to get requested trace"
3563 " buffer data"));
3564
3565 TRACE_WRITE_R_BLOCK (writer, buf,
3566 trace_regblock_size);
3567 break;
3568 case 'M':
3569 {
3570 unsigned short mlen;
3571 ULONGEST addr;
3572 LONGEST t;
3573 int j;
3574
3575 t = target_get_raw_trace_data (buf,offset, 10);
3576 if (t < 10)
3577 error (_("Failure to get requested trace"
3578 " buffer data"));
3579
3580 offset += 10;
3581 block += 10;
3582
3583 gotten = 0;
3584 addr = (ULONGEST)
3585 extract_unsigned_integer (buf, 8,
3586 byte_order);
3587 mlen = (unsigned short)
3588 extract_unsigned_integer (&buf[8], 2,
3589 byte_order);
3590
3591 TRACE_WRITE_M_BLOCK_HEADER (writer, addr,
3592 mlen);
3593
3594 /* The memory contents in 'M' block may be
3595 very large. Fetch the data from the target
3596 and write them into file one by one. */
3597 for (j = 0; j < mlen; )
3598 {
3599 unsigned int read_length;
3600
3601 if (mlen - j > MAX_TRACE_UPLOAD)
3602 read_length = MAX_TRACE_UPLOAD;
3603 else
3604 read_length = mlen - j;
3605
3606 t = target_get_raw_trace_data (buf,
3607 offset + j,
3608 read_length);
3609 if (t < read_length)
3610 error (_("Failure to get requested"
3611 " trace buffer data"));
3612
3613 TRACE_WRITE_M_BLOCK_MEMORY (writer, buf,
3614 read_length);
3615
3616 j += read_length;
3617 gotten += read_length;
3618 }
3619
3620 break;
3621 }
3622 case 'V':
3623 {
3624 int vnum;
3625 LONGEST val;
3626
3627 gotten
3628 = target_get_raw_trace_data (buf, offset,
3629 12);
3630 if (gotten < 12)
3631 error (_("Failure to get requested"
3632 " trace buffer data"));
3633
3634 vnum = (int) extract_signed_integer (buf,
3635 4,
3636 byte_order);
3637 val
3638 = extract_signed_integer (&buf[4], 8,
3639 byte_order);
3640
3641 TRACE_WRITE_V_BLOCK (writer, vnum, val);
3642 }
3643 break;
3644 default:
3645 error (_("Unknown block type '%c' (0x%x) in"
3646 " trace frame"),
3647 block_type, block_type);
3648 }
3649
3650 block += gotten;
3651 offset += gotten;
3652 }
3653 }
3654 else
3655 offset += gotten;
3656
3657 writer->ops->frame_ops->end (writer);
409873ef 3658 }
00bf0b85
SS
3659 }
3660
3f43bc09
YQ
3661 writer->ops->end (writer);
3662}
00bf0b85 3663
3f43bc09 3664/* Return a trace writer for TFILE format. */
00bf0b85 3665
3f43bc09
YQ
3666static struct trace_file_writer *
3667tfile_trace_file_writer_new (void)
3668{
3669 struct tfile_trace_file_writer *writer
3670 = xmalloc (sizeof (struct tfile_trace_file_writer));
00bf0b85 3671
3f43bc09
YQ
3672 writer->base.ops = &tfile_write_ops;
3673 writer->fp = NULL;
3674 writer->pathname = NULL;
00bf0b85 3675
3f43bc09 3676 return (struct trace_file_writer *) writer;
011aacb0
VP
3677}
3678
3679static void
3680trace_save_command (char *args, int from_tty)
3681{
3682 int target_does_save = 0;
3683 char **argv;
3684 char *filename = NULL;
3685 struct cleanup *back_to;
d0353e76 3686 int generate_ctf = 0;
3f43bc09 3687 struct trace_file_writer *writer = NULL;
011aacb0
VP
3688
3689 if (args == NULL)
3690 error_no_arg (_("file in which to save trace data"));
3691
3692 argv = gdb_buildargv (args);
3693 back_to = make_cleanup_freeargv (argv);
3694
3695 for (; *argv; ++argv)
3696 {
3697 if (strcmp (*argv, "-r") == 0)
3698 target_does_save = 1;
d0353e76
YQ
3699 if (strcmp (*argv, "-ctf") == 0)
3700 generate_ctf = 1;
011aacb0
VP
3701 else if (**argv == '-')
3702 error (_("unknown option `%s'"), *argv);
3703 else
3704 filename = *argv;
3705 }
3706
3707 if (!filename)
3708 error_no_arg (_("file in which to save trace data"));
3709
d0353e76
YQ
3710 if (generate_ctf)
3711 writer = ctf_trace_file_writer_new ();
3712 else
3713 writer = tfile_trace_file_writer_new ();
3f43bc09
YQ
3714
3715 make_cleanup (trace_file_writer_xfree, writer);
3716
3717 trace_save (filename, writer, target_does_save);
011aacb0 3718
00bf0b85 3719 if (from_tty)
d0353e76
YQ
3720 printf_filtered (_("Trace data saved to %s '%s'.\n"),
3721 generate_ctf ? "directory" : "file", filename);
011aacb0
VP
3722
3723 do_cleanups (back_to);
00bf0b85
SS
3724}
3725
3f43bc09
YQ
3726/* Save the trace data to file FILENAME of tfile format. */
3727
3728void
3729trace_save_tfile (const char *filename, int target_does_save)
3730{
3731 struct trace_file_writer *writer;
3732 struct cleanup *back_to;
3733
3734 writer = tfile_trace_file_writer_new ();
3735 back_to = make_cleanup (trace_file_writer_xfree, writer);
3736 trace_save (filename, writer, target_does_save);
3737 do_cleanups (back_to);
3738}
3739
d0353e76
YQ
3740/* Save the trace data to dir DIRNAME of ctf format. */
3741
3742void
3743trace_save_ctf (const char *dirname, int target_does_save)
3744{
3745 struct trace_file_writer *writer;
3746 struct cleanup *back_to;
3747
3748 writer = ctf_trace_file_writer_new ();
3749 back_to = make_cleanup (trace_file_writer_xfree, writer);
3750
3751 trace_save (dirname, writer, target_does_save);
3752 do_cleanups (back_to);
3753}
3754
d5551862
SS
3755/* Tell the target what to do with an ongoing tracing run if GDB
3756 disconnects for some reason. */
3757
d5551862
SS
3758static void
3759set_disconnected_tracing (char *args, int from_tty,
3760 struct cmd_list_element *c)
3761{
f196051f 3762 target_set_disconnected_tracing (disconnected_tracing);
d5551862
SS
3763}
3764
4daf5ac0
SS
3765static void
3766set_circular_trace_buffer (char *args, int from_tty,
3767 struct cmd_list_element *c)
3768{
3769 target_set_circular_trace_buffer (circular_trace_buffer);
3770}
3771
f6f899bf
HAQ
3772static void
3773set_trace_buffer_size (char *args, int from_tty,
3774 struct cmd_list_element *c)
3775{
3776 target_set_trace_buffer_size (trace_buffer_size);
3777}
3778
f196051f
SS
3779static void
3780set_trace_user (char *args, int from_tty,
3781 struct cmd_list_element *c)
3782{
3783 int ret;
3784
3785 ret = target_set_trace_notes (trace_user, NULL, NULL);
3786
3787 if (!ret)
43011e52 3788 warning (_("Target does not support trace notes, user ignored"));
f196051f
SS
3789}
3790
3791static void
3792set_trace_notes (char *args, int from_tty,
3793 struct cmd_list_element *c)
3794{
3795 int ret;
3796
3797 ret = target_set_trace_notes (NULL, trace_notes, NULL);
3798
3799 if (!ret)
43011e52 3800 warning (_("Target does not support trace notes, note ignored"));
f196051f
SS
3801}
3802
3803static void
3804set_trace_stop_notes (char *args, int from_tty,
3805 struct cmd_list_element *c)
3806{
3807 int ret;
3808
3809 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes);
3810
3811 if (!ret)
43011e52 3812 warning (_("Target does not support trace notes, stop note ignored"));
f196051f
SS
3813}
3814
c906108c
SS
3815/* Convert the memory pointed to by mem into hex, placing result in buf.
3816 * Return a pointer to the last char put in buf (null)
3817 * "stolen" from sparc-stub.c
3818 */
3819
c5aa993b 3820static const char hexchars[] = "0123456789abcdef";
c906108c 3821
47b667de
AC
3822static char *
3823mem2hex (gdb_byte *mem, char *buf, int count)
c906108c 3824{
47b667de 3825 gdb_byte ch;
c906108c
SS
3826
3827 while (count-- > 0)
3828 {
3829 ch = *mem++;
3830
3831 *buf++ = hexchars[ch >> 4];
3832 *buf++ = hexchars[ch & 0xf];
3833 }
3834
3835 *buf = 0;
3836
3837 return buf;
3838}
3839
c5aa993b 3840int
fba45db2 3841get_traceframe_number (void)
c906108c 3842{
c5aa993b 3843 return traceframe_number;
c906108c
SS
3844}
3845
393fd4c3
YQ
3846int
3847get_tracepoint_number (void)
3848{
3849 return tracepoint_number;
3850}
3851
06cd862c
PA
3852/* Make the traceframe NUM be the current trace frame. Does nothing
3853 if NUM is already current. */
3854
3855void
e6e4e701 3856set_current_traceframe (int num)
06cd862c
PA
3857{
3858 int newnum;
3859
3860 if (traceframe_number == num)
3861 {
3862 /* Nothing to do. */
3863 return;
3864 }
3865
3866 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
3867
3868 if (newnum != num)
3869 warning (_("could not change traceframe"));
3870
8d735b87 3871 set_traceframe_num (newnum);
06cd862c
PA
3872
3873 /* Changing the traceframe changes our view of registers and of the
3874 frame chain. */
3875 registers_changed ();
b3b9301e
PA
3876
3877 clear_traceframe_info ();
06cd862c
PA
3878}
3879
e6e4e701
PA
3880/* Make the traceframe NUM be the current trace frame, and do nothing
3881 more. */
3882
3883void
3884set_traceframe_number (int num)
3885{
3886 traceframe_number = num;
3887}
3888
06cd862c
PA
3889/* A cleanup used when switching away and back from tfind mode. */
3890
3891struct current_traceframe_cleanup
3892{
3893 /* The traceframe we were inspecting. */
3894 int traceframe_number;
3895};
3896
3897static void
3898do_restore_current_traceframe_cleanup (void *arg)
3899{
3900 struct current_traceframe_cleanup *old = arg;
3901
e6e4e701 3902 set_current_traceframe (old->traceframe_number);
06cd862c
PA
3903}
3904
3905static void
3906restore_current_traceframe_cleanup_dtor (void *arg)
3907{
3908 struct current_traceframe_cleanup *old = arg;
3909
3910 xfree (old);
3911}
3912
3913struct cleanup *
3914make_cleanup_restore_current_traceframe (void)
3915{
3916 struct current_traceframe_cleanup *old;
3917
3918 old = xmalloc (sizeof (struct current_traceframe_cleanup));
3919 old->traceframe_number = traceframe_number;
3920
3921 return make_cleanup_dtor (do_restore_current_traceframe_cleanup, old,
3922 restore_current_traceframe_cleanup_dtor);
3923}
00bf0b85 3924
e6e4e701
PA
3925struct cleanup *
3926make_cleanup_restore_traceframe_number (void)
3927{
3928 return make_cleanup_restore_integer (&traceframe_number);
3929}
3930
00bf0b85
SS
3931/* Given a number and address, return an uploaded tracepoint with that
3932 number, creating if necessary. */
3933
3934struct uploaded_tp *
3935get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
3936{
3937 struct uploaded_tp *utp;
3938
3939 for (utp = *utpp; utp; utp = utp->next)
3940 if (utp->number == num && utp->addr == addr)
3941 return utp;
3942 utp = (struct uploaded_tp *) xmalloc (sizeof (struct uploaded_tp));
3943 memset (utp, 0, sizeof (struct uploaded_tp));
3944 utp->number = num;
3945 utp->addr = addr;
3149d8c1
SS
3946 utp->actions = NULL;
3947 utp->step_actions = NULL;
3948 utp->cmd_strings = NULL;
00bf0b85
SS
3949 utp->next = *utpp;
3950 *utpp = utp;
3951 return utp;
3952}
3953
3954static void
3955free_uploaded_tps (struct uploaded_tp **utpp)
3956{
3957 struct uploaded_tp *next_one;
3958
3959 while (*utpp)
3960 {
3961 next_one = (*utpp)->next;
3962 xfree (*utpp);
3963 *utpp = next_one;
3964 }
3965}
3966
3967/* Given a number and address, return an uploaded tracepoint with that
3968 number, creating if necessary. */
3969
393fd4c3 3970struct uploaded_tsv *
00bf0b85
SS
3971get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
3972{
3973 struct uploaded_tsv *utsv;
3974
3975 for (utsv = *utsvp; utsv; utsv = utsv->next)
3976 if (utsv->number == num)
3977 return utsv;
3978 utsv = (struct uploaded_tsv *) xmalloc (sizeof (struct uploaded_tsv));
3979 memset (utsv, 0, sizeof (struct uploaded_tsv));
3980 utsv->number = num;
3981 utsv->next = *utsvp;
3982 *utsvp = utsv;
3983 return utsv;
3984}
3985
3986static void
3987free_uploaded_tsvs (struct uploaded_tsv **utsvp)
3988{
3989 struct uploaded_tsv *next_one;
3990
3991 while (*utsvp)
3992 {
3993 next_one = (*utsvp)->next;
3994 xfree (*utsvp);
3995 *utsvp = next_one;
3996 }
3997}
3998
4e5c165d
HZ
3999/* FIXME this function is heuristic and will miss the cases where the
4000 conditional is semantically identical but differs in whitespace,
4001 such as "x == 0" vs "x==0". */
4002
4003static int
4004cond_string_is_same (char *str1, char *str2)
4005{
4006 if (str1 == NULL || str2 == NULL)
4007 return (str1 == str2);
4008
4009 return (strcmp (str1, str2) == 0);
4010}
4011
00bf0b85
SS
4012/* Look for an existing tracepoint that seems similar enough to the
4013 uploaded one. Enablement isn't compared, because the user can
4014 toggle that freely, and may have done so in anticipation of the
1e4d1764 4015 next trace run. Return the location of matched tracepoint. */
00bf0b85 4016
70221824 4017static struct bp_location *
1e4d1764 4018find_matching_tracepoint_location (struct uploaded_tp *utp)
00bf0b85
SS
4019{
4020 VEC(breakpoint_p) *tp_vec = all_tracepoints ();
4021 int ix;
d9b3f62e 4022 struct breakpoint *b;
00bf0b85
SS
4023 struct bp_location *loc;
4024
d9b3f62e 4025 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
00bf0b85 4026 {
d9b3f62e
PA
4027 struct tracepoint *t = (struct tracepoint *) b;
4028
4029 if (b->type == utp->type
00bf0b85
SS
4030 && t->step_count == utp->step
4031 && t->pass_count == utp->pass
4e5c165d
HZ
4032 && cond_string_is_same (t->base.cond_string, utp->cond_string)
4033 /* FIXME also test actions. */
00bf0b85
SS
4034 )
4035 {
4036 /* Scan the locations for an address match. */
d9b3f62e 4037 for (loc = b->loc; loc; loc = loc->next)
00bf0b85
SS
4038 {
4039 if (loc->address == utp->addr)
1e4d1764 4040 return loc;
00bf0b85
SS
4041 }
4042 }
4043 }
4044 return NULL;
4045}
4046
4047/* Given a list of tracepoints uploaded from a target, attempt to
4048 match them up with existing tracepoints, and create new ones if not
4049 found. */
4050
4051void
4052merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
4053{
4054 struct uploaded_tp *utp;
f2a8bc8a
YQ
4055 /* A set of tracepoints which are modified. */
4056 VEC(breakpoint_p) *modified_tp = NULL;
4057 int ix;
4058 struct breakpoint *b;
00bf0b85
SS
4059
4060 /* Look for GDB tracepoints that match up with our uploaded versions. */
4061 for (utp = *uploaded_tps; utp; utp = utp->next)
4062 {
1e4d1764
YQ
4063 struct bp_location *loc;
4064 struct tracepoint *t;
4065
4066 loc = find_matching_tracepoint_location (utp);
4067 if (loc)
4068 {
f2a8bc8a
YQ
4069 int found = 0;
4070
1e4d1764
YQ
4071 /* Mark this location as already inserted. */
4072 loc->inserted = 1;
4073 t = (struct tracepoint *) loc->owner;
4074 printf_filtered (_("Assuming tracepoint %d is same "
4075 "as target's tracepoint %d at %s.\n"),
4076 loc->owner->number, utp->number,
4077 paddress (loc->gdbarch, utp->addr));
f2a8bc8a
YQ
4078
4079 /* The tracepoint LOC->owner was modified (the location LOC
4080 was marked as inserted in the target). Save it in
4081 MODIFIED_TP if not there yet. The 'breakpoint-modified'
4082 observers will be notified later once for each tracepoint
4083 saved in MODIFIED_TP. */
4084 for (ix = 0;
4085 VEC_iterate (breakpoint_p, modified_tp, ix, b);
4086 ix++)
4087 if (b == loc->owner)
4088 {
4089 found = 1;
4090 break;
4091 }
4092 if (!found)
4093 VEC_safe_push (breakpoint_p, modified_tp, loc->owner);
1e4d1764 4094 }
00bf0b85
SS
4095 else
4096 {
4097 t = create_tracepoint_from_upload (utp);
4098 if (t)
3e43a32a
MS
4099 printf_filtered (_("Created tracepoint %d for "
4100 "target's tracepoint %d at %s.\n"),
d9b3f62e 4101 t->base.number, utp->number,
3e43a32a 4102 paddress (get_current_arch (), utp->addr));
00bf0b85 4103 else
3e43a32a
MS
4104 printf_filtered (_("Failed to create tracepoint for target's "
4105 "tracepoint %d at %s, skipping it.\n"),
4106 utp->number,
4107 paddress (get_current_arch (), utp->addr));
00bf0b85
SS
4108 }
4109 /* Whether found or created, record the number used by the
4110 target, to help with mapping target tracepoints back to their
4111 counterparts here. */
4112 if (t)
4113 t->number_on_target = utp->number;
4114 }
4115
f2a8bc8a
YQ
4116 /* Notify 'breakpoint-modified' observer that at least one of B's
4117 locations was changed. */
4118 for (ix = 0; VEC_iterate (breakpoint_p, modified_tp, ix, b); ix++)
4119 observer_notify_breakpoint_modified (b);
4120
4121 VEC_free (breakpoint_p, modified_tp);
00bf0b85
SS
4122 free_uploaded_tps (uploaded_tps);
4123}
4124
4125/* Trace state variables don't have much to identify them beyond their
4126 name, so just use that to detect matches. */
4127
70221824 4128static struct trace_state_variable *
00bf0b85
SS
4129find_matching_tsv (struct uploaded_tsv *utsv)
4130{
4131 if (!utsv->name)
4132 return NULL;
4133
4134 return find_trace_state_variable (utsv->name);
4135}
4136
70221824 4137static struct trace_state_variable *
00bf0b85
SS
4138create_tsv_from_upload (struct uploaded_tsv *utsv)
4139{
4140 const char *namebase;
df5a4bd3 4141 char *buf;
00bf0b85
SS
4142 int try_num = 0;
4143 struct trace_state_variable *tsv;
df5a4bd3 4144 struct cleanup *old_chain;
00bf0b85
SS
4145
4146 if (utsv->name)
4147 {
4148 namebase = utsv->name;
df5a4bd3 4149 buf = xstrprintf ("%s", namebase);
00bf0b85
SS
4150 }
4151 else
4152 {
4153 namebase = "__tsv";
df5a4bd3 4154 buf = xstrprintf ("%s_%d", namebase, try_num++);
00bf0b85
SS
4155 }
4156
4157 /* Fish for a name that is not in use. */
c378eb4e 4158 /* (should check against all internal vars?) */
00bf0b85 4159 while (find_trace_state_variable (buf))
df5a4bd3
HZ
4160 {
4161 xfree (buf);
4162 buf = xstrprintf ("%s_%d", namebase, try_num++);
4163 }
4164
4165 old_chain = make_cleanup (xfree, buf);
00bf0b85
SS
4166
4167 /* We have an available name, create the variable. */
a0aa2878 4168 tsv = create_trace_state_variable (buf);
00bf0b85
SS
4169 tsv->initial_value = utsv->initial_value;
4170 tsv->builtin = utsv->builtin;
4171
134a2066 4172 observer_notify_tsv_created (tsv);
bb25a15c 4173
df5a4bd3
HZ
4174 do_cleanups (old_chain);
4175
00bf0b85
SS
4176 return tsv;
4177}
4178
4179/* Given a list of uploaded trace state variables, try to match them
4180 up with existing variables, or create additional ones. */
4181
4182void
4183merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
4184{
4185 int ix;
4186 struct uploaded_tsv *utsv;
4187 struct trace_state_variable *tsv;
4188 int highest;
4189
4190 /* Most likely some numbers will have to be reassigned as part of
4191 the merge, so clear them all in anticipation. */
4192 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
4193 tsv->number = 0;
4194
4195 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
4196 {
4197 tsv = find_matching_tsv (utsv);
4198 if (tsv)
417b5110
DJ
4199 {
4200 if (info_verbose)
3e43a32a
MS
4201 printf_filtered (_("Assuming trace state variable $%s "
4202 "is same as target's variable %d.\n"),
417b5110
DJ
4203 tsv->name, utsv->number);
4204 }
00bf0b85
SS
4205 else
4206 {
4207 tsv = create_tsv_from_upload (utsv);
417b5110 4208 if (info_verbose)
3e43a32a
MS
4209 printf_filtered (_("Created trace state variable "
4210 "$%s for target's variable %d.\n"),
417b5110 4211 tsv->name, utsv->number);
00bf0b85
SS
4212 }
4213 /* Give precedence to numberings that come from the target. */
4214 if (tsv)
4215 tsv->number = utsv->number;
4216 }
4217
4218 /* Renumber everything that didn't get a target-assigned number. */
4219 highest = 0;
4220 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
4221 if (tsv->number > highest)
4222 highest = tsv->number;
4223
4224 ++highest;
4225 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
4226 if (tsv->number == 0)
4227 tsv->number = highest++;
4228
4229 free_uploaded_tsvs (uploaded_tsvs);
4230}
4231
4232/* target tfile command */
4233
1467929f 4234static struct target_ops tfile_ops;
00bf0b85
SS
4235
4236/* Fill in tfile_ops with its defined operations and properties. */
4237
4238#define TRACE_HEADER_SIZE 8
4239
1467929f
YQ
4240static char *trace_filename;
4241static int trace_fd = -1;
4242static off_t trace_frames_offset;
4243static off_t cur_offset;
4244static int cur_data_size;
00bf0b85
SS
4245int trace_regblock_size;
4246
4247static void tfile_interp_line (char *line,
4248 struct uploaded_tp **utpp,
4249 struct uploaded_tsv **utsvp);
4250
9f41c731
PA
4251/* Read SIZE bytes into READBUF from the trace frame, starting at
4252 TRACE_FD's current position. Note that this call `read'
4253 underneath, hence it advances the file's seek position. Throws an
4254 error if the `read' syscall fails, or less than SIZE bytes are
4255 read. */
4256
4257static void
4258tfile_read (gdb_byte *readbuf, int size)
4259{
4260 int gotten;
4261
4262 gotten = read (trace_fd, readbuf, size);
4263 if (gotten < 0)
4264 perror_with_name (trace_filename);
4265 else if (gotten < size)
4266 error (_("Premature end of file while reading trace file"));
4267}
4268
00bf0b85
SS
4269static void
4270tfile_open (char *filename, int from_tty)
4271{
e93a69ed 4272 volatile struct gdb_exception ex;
00bf0b85
SS
4273 char *temp;
4274 struct cleanup *old_chain;
4275 int flags;
4276 int scratch_chan;
4277 char header[TRACE_HEADER_SIZE];
c378eb4e 4278 char linebuf[1000]; /* Should be max remote packet size or so. */
bc20a4af 4279 gdb_byte byte;
9f41c731 4280 int bytes, i;
00bf0b85
SS
4281 struct trace_status *ts;
4282 struct uploaded_tp *uploaded_tps = NULL;
4283 struct uploaded_tsv *uploaded_tsvs = NULL;
4284
4285 target_preopen (from_tty);
4286 if (!filename)
4287 error (_("No trace file specified."));
4288
4289 filename = tilde_expand (filename);
4290 if (!IS_ABSOLUTE_PATH(filename))
4291 {
c4f7c687 4292 temp = concat (current_directory, "/", filename, (char *) NULL);
00bf0b85
SS
4293 xfree (filename);
4294 filename = temp;
4295 }
4296
4297 old_chain = make_cleanup (xfree, filename);
4298
4299 flags = O_BINARY | O_LARGEFILE;
4300 flags |= O_RDONLY;
614c279d 4301 scratch_chan = gdb_open_cloexec (filename, flags, 0);
00bf0b85
SS
4302 if (scratch_chan < 0)
4303 perror_with_name (filename);
4304
4305 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
4306
c378eb4e 4307 discard_cleanups (old_chain); /* Don't free filename any more. */
00bf0b85
SS
4308 unpush_target (&tfile_ops);
4309
98e03262 4310 trace_filename = xstrdup (filename);
00bf0b85
SS
4311 trace_fd = scratch_chan;
4312
4313 bytes = 0;
4314 /* Read the file header and test for validity. */
9f41c731 4315 tfile_read ((gdb_byte *) &header, TRACE_HEADER_SIZE);
98e03262 4316
00bf0b85
SS
4317 bytes += TRACE_HEADER_SIZE;
4318 if (!(header[0] == 0x7f
4319 && (strncmp (header + 1, "TRACE0\n", 7) == 0)))
4320 error (_("File is not a valid trace file."));
4321
e93a69ed
PA
4322 push_target (&tfile_ops);
4323
00bf0b85
SS
4324 trace_regblock_size = 0;
4325 ts = current_trace_status ();
f5911ea1
HAQ
4326 /* We know we're working with a file. Record its name. */
4327 ts->filename = trace_filename;
00bf0b85
SS
4328 /* Set defaults in case there is no status line. */
4329 ts->running_known = 0;
4330 ts->stop_reason = trace_stop_reason_unknown;
4331 ts->traceframe_count = -1;
4332 ts->buffer_free = 0;
33da3f1c
SS
4333 ts->disconnected_tracing = 0;
4334 ts->circular_buffer = 0;
00bf0b85 4335
e93a69ed 4336 TRY_CATCH (ex, RETURN_MASK_ALL)
00bf0b85 4337 {
e93a69ed
PA
4338 /* Read through a section of newline-terminated lines that
4339 define things like tracepoints. */
4340 i = 0;
4341 while (1)
00bf0b85 4342 {
e93a69ed
PA
4343 tfile_read (&byte, 1);
4344
4345 ++bytes;
4346 if (byte == '\n')
4347 {
4348 /* Empty line marks end of the definition section. */
4349 if (i == 0)
4350 break;
4351 linebuf[i] = '\0';
4352 i = 0;
4353 tfile_interp_line (linebuf, &uploaded_tps, &uploaded_tsvs);
4354 }
4355 else
4356 linebuf[i++] = byte;
4357 if (i >= 1000)
4358 error (_("Excessively long lines in trace file"));
00bf0b85 4359 }
e93a69ed
PA
4360
4361 /* Record the starting offset of the binary trace data. */
4362 trace_frames_offset = bytes;
4363
4364 /* If we don't have a blocksize, we can't interpret the
4365 traceframes. */
4366 if (trace_regblock_size == 0)
4367 error (_("No register block size recorded in trace file"));
4368 }
4369 if (ex.reason < 0)
4370 {
7fdc1521
TT
4371 /* Remove the partially set up target. */
4372 unpush_target (&tfile_ops);
e93a69ed 4373 throw_exception (ex);
00bf0b85
SS
4374 }
4375
e93a69ed
PA
4376 if (ts->traceframe_count <= 0)
4377 warning (_("No traceframes present in this file."));
4378
00bf0b85
SS
4379 /* Add the file's tracepoints and variables into the current mix. */
4380
10ef8d6a
PA
4381 /* Get trace state variables first, they may be checked when parsing
4382 uploaded commands. */
00bf0b85
SS
4383 merge_uploaded_trace_state_variables (&uploaded_tsvs);
4384
10ef8d6a 4385 merge_uploaded_tracepoints (&uploaded_tps);
00bf0b85
SS
4386}
4387
4388/* Interpret the given line from the definitions part of the trace
4389 file. */
4390
4391static void
cf2cb5ec
YQ
4392tfile_interp_line (char *line, struct uploaded_tp **utpp,
4393 struct uploaded_tsv **utsvp)
00bf0b85
SS
4394{
4395 char *p = line;
4396
4397 if (strncmp (p, "R ", strlen ("R ")) == 0)
4398 {
4399 p += strlen ("R ");
4400 trace_regblock_size = strtol (p, &p, 16);
4401 }
4402 else if (strncmp (p, "status ", strlen ("status ")) == 0)
4403 {
4404 p += strlen ("status ");
4405 parse_trace_status (p, current_trace_status ());
4406 }
4407 else if (strncmp (p, "tp ", strlen ("tp ")) == 0)
4408 {
4409 p += strlen ("tp ");
4410 parse_tracepoint_definition (p, utpp);
4411 }
4412 else if (strncmp (p, "tsv ", strlen ("tsv ")) == 0)
4413 {
4414 p += strlen ("tsv ");
4415 parse_tsv_definition (p, utsvp);
4416 }
4417 else
a73c6dcd 4418 warning (_("Ignoring trace file definition \"%s\""), line);
00bf0b85
SS
4419}
4420
4421/* Parse the part of trace status syntax that is shared between
4422 the remote protocol and the trace file reader. */
4423
00bf0b85
SS
4424void
4425parse_trace_status (char *line, struct trace_status *ts)
4426{
f196051f
SS
4427 char *p = line, *p1, *p2, *p3, *p_temp;
4428 int end;
00bf0b85
SS
4429 ULONGEST val;
4430
4431 ts->running_known = 1;
4432 ts->running = (*p++ == '1');
4433 ts->stop_reason = trace_stop_reason_unknown;
f196051f
SS
4434 xfree (ts->stop_desc);
4435 ts->stop_desc = NULL;
4daf5ac0
SS
4436 ts->traceframe_count = -1;
4437 ts->traceframes_created = -1;
4438 ts->buffer_free = -1;
4439 ts->buffer_size = -1;
33da3f1c
SS
4440 ts->disconnected_tracing = 0;
4441 ts->circular_buffer = 0;
f196051f
SS
4442 xfree (ts->user_name);
4443 ts->user_name = NULL;
4444 xfree (ts->notes);
4445 ts->notes = NULL;
4446 ts->start_time = ts->stop_time = 0;
4daf5ac0 4447
00bf0b85
SS
4448 while (*p++)
4449 {
4450 p1 = strchr (p, ':');
4451 if (p1 == NULL)
4452 error (_("Malformed trace status, at %s\n\
4453Status line: '%s'\n"), p, line);
f196051f
SS
4454 p3 = strchr (p, ';');
4455 if (p3 == NULL)
4456 p3 = p + strlen (p);
00bf0b85
SS
4457 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
4458 {
4459 p = unpack_varlen_hex (++p1, &val);
4460 ts->stop_reason = trace_buffer_full;
4461 }
4462 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
4463 {
4464 p = unpack_varlen_hex (++p1, &val);
4465 ts->stop_reason = trace_never_run;
4466 }
3e43a32a
MS
4467 else if (strncmp (p, stop_reason_names[tracepoint_passcount],
4468 p1 - p) == 0)
00bf0b85
SS
4469 {
4470 p = unpack_varlen_hex (++p1, &val);
4471 ts->stop_reason = tracepoint_passcount;
4472 ts->stopping_tracepoint = val;
4473 }
4474 else if (strncmp (p, stop_reason_names[tstop_command], p1 - p) == 0)
4475 {
f196051f
SS
4476 p2 = strchr (++p1, ':');
4477 if (!p2 || p2 > p3)
4478 {
4479 /*older style*/
4480 p2 = p1;
4481 }
4482 else if (p2 != p1)
4483 {
4484 ts->stop_desc = xmalloc (strlen (line));
bc20a4af 4485 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
f196051f
SS
4486 ts->stop_desc[end] = '\0';
4487 }
4488 else
4489 ts->stop_desc = xstrdup ("");
4490
4491 p = unpack_varlen_hex (++p2, &val);
00bf0b85
SS
4492 ts->stop_reason = tstop_command;
4493 }
33da3f1c
SS
4494 else if (strncmp (p, stop_reason_names[trace_disconnected], p1 - p) == 0)
4495 {
4496 p = unpack_varlen_hex (++p1, &val);
4497 ts->stop_reason = trace_disconnected;
4498 }
6c28cbf2
SS
4499 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
4500 {
4501 p2 = strchr (++p1, ':');
4502 if (p2 != p1)
4503 {
f196051f 4504 ts->stop_desc = xmalloc ((p2 - p1) / 2 + 1);
bc20a4af 4505 end = hex2bin (p1, (gdb_byte *) ts->stop_desc, (p2 - p1) / 2);
f196051f 4506 ts->stop_desc[end] = '\0';
6c28cbf2 4507 }
a609a0c8 4508 else
f196051f 4509 ts->stop_desc = xstrdup ("");
a609a0c8 4510
6c28cbf2
SS
4511 p = unpack_varlen_hex (++p2, &val);
4512 ts->stopping_tracepoint = val;
4513 ts->stop_reason = tracepoint_error;
4514 }
4daf5ac0 4515 else if (strncmp (p, "tframes", p1 - p) == 0)
00bf0b85
SS
4516 {
4517 p = unpack_varlen_hex (++p1, &val);
4518 ts->traceframe_count = val;
4519 }
4daf5ac0
SS
4520 else if (strncmp (p, "tcreated", p1 - p) == 0)
4521 {
4522 p = unpack_varlen_hex (++p1, &val);
4523 ts->traceframes_created = val;
4524 }
4525 else if (strncmp (p, "tfree", p1 - p) == 0)
00bf0b85
SS
4526 {
4527 p = unpack_varlen_hex (++p1, &val);
4528 ts->buffer_free = val;
4529 }
4daf5ac0
SS
4530 else if (strncmp (p, "tsize", p1 - p) == 0)
4531 {
4532 p = unpack_varlen_hex (++p1, &val);
4533 ts->buffer_size = val;
4534 }
33da3f1c
SS
4535 else if (strncmp (p, "disconn", p1 - p) == 0)
4536 {
4537 p = unpack_varlen_hex (++p1, &val);
4538 ts->disconnected_tracing = val;
4539 }
4540 else if (strncmp (p, "circular", p1 - p) == 0)
4541 {
4542 p = unpack_varlen_hex (++p1, &val);
4543 ts->circular_buffer = val;
4544 }
f196051f
SS
4545 else if (strncmp (p, "starttime", p1 - p) == 0)
4546 {
4547 p = unpack_varlen_hex (++p1, &val);
4548 ts->start_time = val;
4549 }
4550 else if (strncmp (p, "stoptime", p1 - p) == 0)
4551 {
4552 p = unpack_varlen_hex (++p1, &val);
4553 ts->stop_time = val;
4554 }
4555 else if (strncmp (p, "username", p1 - p) == 0)
4556 {
4557 ++p1;
4558 ts->user_name = xmalloc (strlen (p) / 2);
bc20a4af 4559 end = hex2bin (p1, (gdb_byte *) ts->user_name, (p3 - p1) / 2);
f196051f
SS
4560 ts->user_name[end] = '\0';
4561 p = p3;
4562 }
4563 else if (strncmp (p, "notes", p1 - p) == 0)
4564 {
4565 ++p1;
4566 ts->notes = xmalloc (strlen (p) / 2);
bc20a4af 4567 end = hex2bin (p1, (gdb_byte *) ts->notes, (p3 - p1) / 2);
f196051f
SS
4568 ts->notes[end] = '\0';
4569 p = p3;
4570 }
00bf0b85
SS
4571 else
4572 {
4573 /* Silently skip unknown optional info. */
4574 p_temp = strchr (p1 + 1, ';');
4575 if (p_temp)
4576 p = p_temp;
4577 else
4578 /* Must be at the end. */
4579 break;
4580 }
4581 }
4582}
4583
f196051f
SS
4584void
4585parse_tracepoint_status (char *p, struct breakpoint *bp,
4586 struct uploaded_tp *utp)
4587{
4588 ULONGEST uval;
4589 struct tracepoint *tp = (struct tracepoint *) bp;
4590
4591 p = unpack_varlen_hex (p, &uval);
4592 if (tp)
4593 tp->base.hit_count += uval;
4594 else
4595 utp->hit_count += uval;
4596 p = unpack_varlen_hex (p + 1, &uval);
4597 if (tp)
4598 tp->traceframe_usage += uval;
4599 else
4600 utp->traceframe_usage += uval;
4601 /* Ignore any extra, allowing for future extensions. */
4602}
4603
409873ef
SS
4604/* Given a line of text defining a part of a tracepoint, parse it into
4605 an "uploaded tracepoint". */
00bf0b85
SS
4606
4607void
4608parse_tracepoint_definition (char *line, struct uploaded_tp **utpp)
4609{
4610 char *p;
4611 char piece;
409873ef 4612 ULONGEST num, addr, step, pass, orig_size, xlen, start;
2a2287c7 4613 int enabled, end;
00bf0b85 4614 enum bptype type;
2a2287c7 4615 char *cond, *srctype, *buf;
00bf0b85
SS
4616 struct uploaded_tp *utp = NULL;
4617
4618 p = line;
4619 /* Both tracepoint and action definitions start with the same number
4620 and address sequence. */
4621 piece = *p++;
4622 p = unpack_varlen_hex (p, &num);
4623 p++; /* skip a colon */
4624 p = unpack_varlen_hex (p, &addr);
4625 p++; /* skip a colon */
4626 if (piece == 'T')
4627 {
4628 enabled = (*p++ == 'E');
4629 p++; /* skip a colon */
4630 p = unpack_varlen_hex (p, &step);
4631 p++; /* skip a colon */
4632 p = unpack_varlen_hex (p, &pass);
4633 type = bp_tracepoint;
4634 cond = NULL;
4635 /* Thumb through optional fields. */
4636 while (*p == ':')
4637 {
4638 p++; /* skip a colon */
4639 if (*p == 'F')
4640 {
4641 type = bp_fast_tracepoint;
4642 p++;
4643 p = unpack_varlen_hex (p, &orig_size);
4644 }
0fb4aa4b
PA
4645 else if (*p == 'S')
4646 {
4647 type = bp_static_tracepoint;
4648 p++;
4649 }
00bf0b85
SS
4650 else if (*p == 'X')
4651 {
4652 p++;
4653 p = unpack_varlen_hex (p, &xlen);
4654 p++; /* skip a comma */
4655 cond = (char *) xmalloc (2 * xlen + 1);
4656 strncpy (cond, p, 2 * xlen);
4657 cond[2 * xlen] = '\0';
4658 p += 2 * xlen;
4659 }
4660 else
3e43a32a
MS
4661 warning (_("Unrecognized char '%c' in tracepoint "
4662 "definition, skipping rest"), *p);
00bf0b85
SS
4663 }
4664 utp = get_uploaded_tp (num, addr, utpp);
4665 utp->type = type;
4666 utp->enabled = enabled;
4667 utp->step = step;
4668 utp->pass = pass;
4669 utp->cond = cond;
4670 }
4671 else if (piece == 'A')
4672 {
4673 utp = get_uploaded_tp (num, addr, utpp);
3149d8c1 4674 VEC_safe_push (char_ptr, utp->actions, xstrdup (p));
00bf0b85
SS
4675 }
4676 else if (piece == 'S')
4677 {
4678 utp = get_uploaded_tp (num, addr, utpp);
3149d8c1 4679 VEC_safe_push (char_ptr, utp->step_actions, xstrdup (p));
00bf0b85 4680 }
409873ef
SS
4681 else if (piece == 'Z')
4682 {
4683 /* Parse a chunk of source form definition. */
4684 utp = get_uploaded_tp (num, addr, utpp);
4685 srctype = p;
4686 p = strchr (p, ':');
4687 p++; /* skip a colon */
4688 p = unpack_varlen_hex (p, &start);
4689 p++; /* skip a colon */
4690 p = unpack_varlen_hex (p, &xlen);
4691 p++; /* skip a colon */
4692
4693 buf = alloca (strlen (line));
4694
4695 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
4696 buf[end] = '\0';
4697
4698 if (strncmp (srctype, "at:", strlen ("at:")) == 0)
4699 utp->at_string = xstrdup (buf);
4700 else if (strncmp (srctype, "cond:", strlen ("cond:")) == 0)
4701 utp->cond_string = xstrdup (buf);
4702 else if (strncmp (srctype, "cmd:", strlen ("cmd:")) == 0)
3149d8c1 4703 VEC_safe_push (char_ptr, utp->cmd_strings, xstrdup (buf));
409873ef 4704 }
f196051f
SS
4705 else if (piece == 'V')
4706 {
4707 utp = get_uploaded_tp (num, addr, utpp);
4708
4709 parse_tracepoint_status (p, NULL, utp);
4710 }
00bf0b85
SS
4711 else
4712 {
409873ef
SS
4713 /* Don't error out, the target might be sending us optional
4714 info that we don't care about. */
4715 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
00bf0b85
SS
4716 }
4717}
4718
4719/* Convert a textual description of a trace state variable into an
4720 uploaded object. */
4721
4722void
4723parse_tsv_definition (char *line, struct uploaded_tsv **utsvp)
4724{
4725 char *p, *buf;
4726 ULONGEST num, initval, builtin;
4727 int end;
4728 struct uploaded_tsv *utsv = NULL;
4729
4730 buf = alloca (strlen (line));
4731
4732 p = line;
4733 p = unpack_varlen_hex (p, &num);
4734 p++; /* skip a colon */
4735 p = unpack_varlen_hex (p, &initval);
4736 p++; /* skip a colon */
4737 p = unpack_varlen_hex (p, &builtin);
4738 p++; /* skip a colon */
4739 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
4740 buf[end] = '\0';
4741
4742 utsv = get_uploaded_tsv (num, utsvp);
4743 utsv->initial_value = initval;
4744 utsv->builtin = builtin;
4745 utsv->name = xstrdup (buf);
4746}
4747
4748/* Close the trace file and generally clean up. */
4749
4750static void
460014f5 4751tfile_close (void)
00bf0b85
SS
4752{
4753 int pid;
4754
4755 if (trace_fd < 0)
4756 return;
4757
00bf0b85
SS
4758 close (trace_fd);
4759 trace_fd = -1;
e93a69ed
PA
4760 xfree (trace_filename);
4761 trace_filename = NULL;
aef525cb
YQ
4762
4763 trace_reset_local_state ();
00bf0b85
SS
4764}
4765
4766static void
4767tfile_files_info (struct target_ops *t)
4768{
f5911ea1 4769 printf_filtered ("\t`%s'\n", trace_filename);
00bf0b85
SS
4770}
4771
4772/* The trace status for a file is that tracing can never be run. */
4773
4774static int
4775tfile_get_trace_status (struct trace_status *ts)
4776{
4777 /* Other bits of trace status were collected as part of opening the
4778 trace files, so nothing to do here. */
4779
4780 return -1;
4781}
4782
f196051f
SS
4783static void
4784tfile_get_tracepoint_status (struct breakpoint *tp, struct uploaded_tp *utp)
4785{
4786 /* Other bits of trace status were collected as part of opening the
4787 trace files, so nothing to do here. */
4788}
4789
00bf0b85
SS
4790/* Given the position of a traceframe in the file, figure out what
4791 address the frame was collected at. This would normally be the
4792 value of a collected PC register, but if not available, we
4793 improvise. */
4794
cc5925ad 4795static CORE_ADDR
00bf0b85
SS
4796tfile_get_traceframe_address (off_t tframe_offset)
4797{
cc5925ad 4798 CORE_ADDR addr = 0;
00bf0b85 4799 short tpnum;
d9b3f62e 4800 struct tracepoint *tp;
00bf0b85
SS
4801 off_t saved_offset = cur_offset;
4802
c378eb4e 4803 /* FIXME dig pc out of collected registers. */
00bf0b85
SS
4804
4805 /* Fall back to using tracepoint address. */
4806 lseek (trace_fd, tframe_offset, SEEK_SET);
9f41c731 4807 tfile_read ((gdb_byte *) &tpnum, 2);
8991e9fa
HZ
4808 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
4809 gdbarch_byte_order
f5656ead 4810 (target_gdbarch ()));
98e03262 4811
00bf0b85 4812 tp = get_tracepoint_by_number_on_target (tpnum);
c378eb4e 4813 /* FIXME this is a poor heuristic if multiple locations. */
d9b3f62e
PA
4814 if (tp && tp->base.loc)
4815 addr = tp->base.loc->address;
00bf0b85
SS
4816
4817 /* Restore our seek position. */
4818 cur_offset = saved_offset;
4819 lseek (trace_fd, cur_offset, SEEK_SET);
4820 return addr;
4821}
4822
4823/* Given a type of search and some parameters, scan the collection of
4824 traceframes in the file looking for a match. When found, return
4825 both the traceframe and tracepoint number, otherwise -1 for
4826 each. */
4827
4828static int
4829tfile_trace_find (enum trace_find_type type, int num,
cc5925ad 4830 CORE_ADDR addr1, CORE_ADDR addr2, int *tpp)
00bf0b85
SS
4831{
4832 short tpnum;
9f41c731 4833 int tfnum = 0, found = 0;
8991e9fa 4834 unsigned int data_size;
d9b3f62e 4835 struct tracepoint *tp;
00bf0b85 4836 off_t offset, tframe_offset;
cc5925ad 4837 CORE_ADDR tfaddr;
00bf0b85 4838
a8a64aa8 4839 if (num == -1)
fb80a3c5
HZ
4840 {
4841 if (tpp)
4842 *tpp = -1;
4843 return -1;
4844 }
e6e4e701 4845
00bf0b85
SS
4846 lseek (trace_fd, trace_frames_offset, SEEK_SET);
4847 offset = trace_frames_offset;
4848 while (1)
4849 {
4850 tframe_offset = offset;
9f41c731 4851 tfile_read ((gdb_byte *) &tpnum, 2);
8991e9fa
HZ
4852 tpnum = (short) extract_signed_integer ((gdb_byte *) &tpnum, 2,
4853 gdbarch_byte_order
f5656ead 4854 (target_gdbarch ()));
00bf0b85
SS
4855 offset += 2;
4856 if (tpnum == 0)
4857 break;
9f41c731 4858 tfile_read ((gdb_byte *) &data_size, 4);
8991e9fa
HZ
4859 data_size = (unsigned int) extract_unsigned_integer
4860 ((gdb_byte *) &data_size, 4,
f5656ead 4861 gdbarch_byte_order (target_gdbarch ()));
00bf0b85 4862 offset += 4;
6c8e944d
YQ
4863
4864 if (type == tfind_number)
00bf0b85 4865 {
6c8e944d 4866 /* Looking for a specific trace frame. */
00bf0b85
SS
4867 if (tfnum == num)
4868 found = 1;
00bf0b85 4869 }
6c8e944d
YQ
4870 else
4871 {
4872 /* Start from the _next_ trace frame. */
4873 if (tfnum > traceframe_number)
4874 {
4875 switch (type)
4876 {
4877 case tfind_pc:
4878 tfaddr = tfile_get_traceframe_address (tframe_offset);
4879 if (tfaddr == addr1)
4880 found = 1;
4881 break;
4882 case tfind_tp:
4883 tp = get_tracepoint (num);
4884 if (tp && tpnum == tp->number_on_target)
4885 found = 1;
4886 break;
4887 case tfind_range:
4888 tfaddr = tfile_get_traceframe_address (tframe_offset);
4889 if (addr1 <= tfaddr && tfaddr <= addr2)
4890 found = 1;
4891 break;
4892 case tfind_outside:
4893 tfaddr = tfile_get_traceframe_address (tframe_offset);
4894 if (!(addr1 <= tfaddr && tfaddr <= addr2))
4895 found = 1;
4896 break;
4897 default:
4898 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
4899 }
4900 }
4901 }
4902
00bf0b85
SS
4903 if (found)
4904 {
00bf0b85
SS
4905 if (tpp)
4906 *tpp = tpnum;
4907 cur_offset = offset;
4908 cur_data_size = data_size;
a8a64aa8 4909
00bf0b85
SS
4910 return tfnum;
4911 }
4912 /* Skip past the traceframe's data. */
4913 lseek (trace_fd, data_size, SEEK_CUR);
4914 offset += data_size;
4915 /* Update our own count of traceframes. */
4916 ++tfnum;
4917 }
4918 /* Did not find what we were looking for. */
4919 if (tpp)
4920 *tpp = -1;
4921 return -1;
4922}
4923
9f41c731
PA
4924/* Prototype of the callback passed to tframe_walk_blocks. */
4925typedef int (*walk_blocks_callback_func) (char blocktype, void *data);
4926
4927/* Callback for traceframe_walk_blocks, used to find a given block
4928 type in a traceframe. */
4929
4930static int
4931match_blocktype (char blocktype, void *data)
4932{
4933 char *wantedp = data;
4934
4935 if (*wantedp == blocktype)
4936 return 1;
4937
4938 return 0;
4939}
4940
4941/* Walk over all traceframe block starting at POS offset from
4942 CUR_OFFSET, and call CALLBACK for each block found, passing in DATA
4943 unmodified. If CALLBACK returns true, this returns the position in
4944 the traceframe where the block is found, relative to the start of
4945 the traceframe (cur_offset). Returns -1 if no callback call
4946 returned true, indicating that all blocks have been walked. */
4947
4948static int
4949traceframe_walk_blocks (walk_blocks_callback_func callback,
4950 int pos, void *data)
4951{
4952 /* Iterate through a traceframe's blocks, looking for a block of the
4953 requested type. */
4954
4955 lseek (trace_fd, cur_offset + pos, SEEK_SET);
4956 while (pos < cur_data_size)
4957 {
4958 unsigned short mlen;
4959 char block_type;
4960
bc20a4af 4961 tfile_read ((gdb_byte *) &block_type, 1);
9f41c731
PA
4962
4963 ++pos;
4964
4965 if ((*callback) (block_type, data))
4966 return pos;
4967
4968 switch (block_type)
4969 {
4970 case 'R':
4971 lseek (trace_fd, cur_offset + pos + trace_regblock_size, SEEK_SET);
4972 pos += trace_regblock_size;
4973 break;
4974 case 'M':
4975 lseek (trace_fd, cur_offset + pos + 8, SEEK_SET);
4976 tfile_read ((gdb_byte *) &mlen, 2);
4977 mlen = (unsigned short)
4978 extract_unsigned_integer ((gdb_byte *) &mlen, 2,
4979 gdbarch_byte_order
f5656ead 4980 (target_gdbarch ()));
9f41c731
PA
4981 lseek (trace_fd, mlen, SEEK_CUR);
4982 pos += (8 + 2 + mlen);
4983 break;
4984 case 'V':
4985 lseek (trace_fd, cur_offset + pos + 4 + 8, SEEK_SET);
4986 pos += (4 + 8);
4987 break;
4988 default:
c2f0d045 4989 error (_("Unknown block type '%c' (0x%x) in trace frame"),
9f41c731
PA
4990 block_type, block_type);
4991 break;
4992 }
4993 }
4994
4995 return -1;
4996}
4997
4998/* Convenience wrapper around traceframe_walk_blocks. Looks for the
4999 position offset of a block of type TYPE_WANTED in the current trace
5000 frame, starting at POS. Returns -1 if no such block was found. */
5001
5002static int
5003traceframe_find_block_type (char type_wanted, int pos)
5004{
5005 return traceframe_walk_blocks (match_blocktype, pos, &type_wanted);
5006}
5007
00bf0b85
SS
5008/* Look for a block of saved registers in the traceframe, and get the
5009 requested register from it. */
5010
5011static void
5012tfile_fetch_registers (struct target_ops *ops,
5013 struct regcache *regcache, int regno)
5014{
5015 struct gdbarch *gdbarch = get_regcache_arch (regcache);
22e048c9 5016 int offset, regn, regsize, pc_regno;
948f8e3d 5017 gdb_byte *regs;
00bf0b85
SS
5018
5019 /* An uninitialized reg size says we're not going to be
5020 successful at getting register blocks. */
5021 if (!trace_regblock_size)
5022 return;
5023
5024 regs = alloca (trace_regblock_size);
5025
9f41c731 5026 if (traceframe_find_block_type ('R', 0) >= 0)
00bf0b85 5027 {
9f41c731 5028 tfile_read (regs, trace_regblock_size);
98e03262 5029
9f41c731
PA
5030 /* Assume the block is laid out in GDB register number order,
5031 each register with the size that it has in GDB. */
5032 offset = 0;
5033 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
00bf0b85 5034 {
9f41c731
PA
5035 regsize = register_size (gdbarch, regn);
5036 /* Make sure we stay within block bounds. */
5037 if (offset + regsize >= trace_regblock_size)
5038 break;
5039 if (regcache_register_status (regcache, regn) == REG_UNKNOWN)
00bf0b85 5040 {
9f41c731 5041 if (regno == regn)
00bf0b85 5042 {
9f41c731
PA
5043 regcache_raw_supply (regcache, regno, regs + offset);
5044 break;
5045 }
5046 else if (regno == -1)
5047 {
5048 regcache_raw_supply (regcache, regn, regs + offset);
00bf0b85 5049 }
00bf0b85 5050 }
9f41c731 5051 offset += regsize;
00bf0b85 5052 }
9f41c731 5053 return;
00bf0b85 5054 }
af54718e 5055
9f41c731
PA
5056 /* We get here if no register data has been found. Mark registers
5057 as unavailable. */
af54718e
SS
5058 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
5059 regcache_raw_supply (regcache, regn, NULL);
5060
5061 /* We can often usefully guess that the PC is going to be the same
5062 as the address of the tracepoint. */
5063 pc_regno = gdbarch_pc_regnum (gdbarch);
5064 if (pc_regno >= 0 && (regno == -1 || regno == pc_regno))
5065 {
d9b3f62e 5066 struct tracepoint *tp = get_tracepoint (tracepoint_number);
af54718e 5067
d9b3f62e 5068 if (tp && tp->base.loc)
af54718e
SS
5069 {
5070 /* But don't try to guess if tracepoint is multi-location... */
d9b3f62e 5071 if (tp->base.loc->next)
af54718e 5072 {
a73c6dcd
MS
5073 warning (_("Tracepoint %d has multiple "
5074 "locations, cannot infer $pc"),
d9b3f62e 5075 tp->base.number);
af54718e
SS
5076 return;
5077 }
5078 /* ... or does while-stepping. */
5079 if (tp->step_count > 0)
5080 {
a73c6dcd
MS
5081 warning (_("Tracepoint %d does while-stepping, "
5082 "cannot infer $pc"),
d9b3f62e 5083 tp->base.number);
af54718e
SS
5084 return;
5085 }
5086
5087 store_unsigned_integer (regs, register_size (gdbarch, pc_regno),
5088 gdbarch_byte_order (gdbarch),
d9b3f62e 5089 tp->base.loc->address);
af54718e
SS
5090 regcache_raw_supply (regcache, pc_regno, regs);
5091 }
5092 }
00bf0b85
SS
5093}
5094
5095static LONGEST
5096tfile_xfer_partial (struct target_ops *ops, enum target_object object,
5097 const char *annex, gdb_byte *readbuf,
5098 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
5099{
00bf0b85
SS
5100 /* We're only doing regular memory for now. */
5101 if (object != TARGET_OBJECT_MEMORY)
5102 return -1;
5103
5104 if (readbuf == NULL)
a73c6dcd 5105 error (_("tfile_xfer_partial: trace file is read-only"));
00bf0b85 5106
e6e4e701 5107 if (traceframe_number != -1)
00bf0b85 5108 {
ffd5ec24 5109 int pos = 0;
9f41c731 5110
ffd5ec24
PA
5111 /* Iterate through the traceframe's blocks, looking for
5112 memory. */
5113 while ((pos = traceframe_find_block_type ('M', pos)) >= 0)
00bf0b85 5114 {
ffd5ec24
PA
5115 ULONGEST maddr, amt;
5116 unsigned short mlen;
f5656ead 5117 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
fce3c1f0 5118
ffd5ec24
PA
5119 tfile_read ((gdb_byte *) &maddr, 8);
5120 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
5121 byte_order);
5122 tfile_read ((gdb_byte *) &mlen, 2);
5123 mlen = (unsigned short)
5124 extract_unsigned_integer ((gdb_byte *) &mlen, 2, byte_order);
5125
5126 /* If the block includes the first part of the desired
5127 range, return as much it has; GDB will re-request the
5128 remainder, which might be in a different block of this
5129 trace frame. */
5130 if (maddr <= offset && offset < (maddr + mlen))
5131 {
5132 amt = (maddr + mlen) - offset;
5133 if (amt > len)
5134 amt = len;
5135
7ecbc825
HZ
5136 if (maddr != offset)
5137 lseek (trace_fd, offset - maddr, SEEK_CUR);
ffd5ec24
PA
5138 tfile_read (readbuf, amt);
5139 return amt;
5140 }
9f41c731 5141
ffd5ec24
PA
5142 /* Skip over this block. */
5143 pos += (8 + 2 + mlen);
5144 }
00bf0b85 5145 }
fce3c1f0
SS
5146
5147 /* It's unduly pedantic to refuse to look at the executable for
5148 read-only pieces; so do the equivalent of readonly regions aka
5149 QTro packet. */
c378eb4e 5150 /* FIXME account for relocation at some point. */
fce3c1f0
SS
5151 if (exec_bfd)
5152 {
5153 asection *s;
5154 bfd_size_type size;
2209c807 5155 bfd_vma vma;
fce3c1f0
SS
5156
5157 for (s = exec_bfd->sections; s; s = s->next)
5158 {
9f41c731
PA
5159 if ((s->flags & SEC_LOAD) == 0
5160 || (s->flags & SEC_READONLY) == 0)
fce3c1f0
SS
5161 continue;
5162
2209c807 5163 vma = s->vma;
fce3c1f0 5164 size = bfd_get_section_size (s);
2209c807 5165 if (vma <= offset && offset < (vma + size))
fce3c1f0 5166 {
9f41c731
PA
5167 ULONGEST amt;
5168
2209c807 5169 amt = (vma + size) - offset;
fce3c1f0
SS
5170 if (amt > len)
5171 amt = len;
5172
5173 amt = bfd_get_section_contents (exec_bfd, s,
2209c807 5174 readbuf, offset - vma, amt);
fce3c1f0
SS
5175 return amt;
5176 }
5177 }
5178 }
5179
00bf0b85
SS
5180 /* Indicate failure to find the requested memory block. */
5181 return -1;
5182}
5183
5184/* Iterate through the blocks of a trace frame, looking for a 'V'
5185 block with a matching tsv number. */
5186
5187static int
5188tfile_get_trace_state_variable_value (int tsvnum, LONGEST *val)
5189{
9f41c731 5190 int pos;
8ddb1965 5191 int found = 0;
00bf0b85 5192
8ddb1965
YQ
5193 /* Iterate over blocks in current frame and find the last 'V'
5194 block in which tsv number is TSVNUM. In one trace frame, there
5195 may be multiple 'V' blocks created for a given trace variable,
5196 and the last matched 'V' block contains the updated value. */
00bf0b85 5197 pos = 0;
9f41c731 5198 while ((pos = traceframe_find_block_type ('V', pos)) >= 0)
00bf0b85 5199 {
9f41c731
PA
5200 int vnum;
5201
5202 tfile_read ((gdb_byte *) &vnum, 4);
5203 vnum = (int) extract_signed_integer ((gdb_byte *) &vnum, 4,
8991e9fa 5204 gdbarch_byte_order
f5656ead 5205 (target_gdbarch ()));
9f41c731
PA
5206 if (tsvnum == vnum)
5207 {
5208 tfile_read ((gdb_byte *) val, 8);
5209 *val = extract_signed_integer ((gdb_byte *) val, 8,
5210 gdbarch_byte_order
f5656ead 5211 (target_gdbarch ()));
8ddb1965 5212 found = 1;
00bf0b85 5213 }
9f41c731 5214 pos += (4 + 8);
00bf0b85 5215 }
9f41c731 5216
8ddb1965 5217 return found;
00bf0b85
SS
5218}
5219
fce3c1f0
SS
5220static int
5221tfile_has_all_memory (struct target_ops *ops)
5222{
5223 return 1;
5224}
5225
00bf0b85
SS
5226static int
5227tfile_has_memory (struct target_ops *ops)
5228{
5229 return 1;
5230}
5231
5232static int
5233tfile_has_stack (struct target_ops *ops)
5234{
ffd5ec24 5235 return traceframe_number != -1;
00bf0b85
SS
5236}
5237
5238static int
5239tfile_has_registers (struct target_ops *ops)
5240{
ffd5ec24 5241 return traceframe_number != -1;
00bf0b85
SS
5242}
5243
b3b9301e
PA
5244/* Callback for traceframe_walk_blocks. Builds a traceframe_info
5245 object for the tfile target's current traceframe. */
5246
5247static int
5248build_traceframe_info (char blocktype, void *data)
5249{
5250 struct traceframe_info *info = data;
5251
5252 switch (blocktype)
5253 {
5254 case 'M':
5255 {
5256 struct mem_range *r;
5257 ULONGEST maddr;
5258 unsigned short mlen;
5259
5260 tfile_read ((gdb_byte *) &maddr, 8);
e83b17ba
HZ
5261 maddr = extract_unsigned_integer ((gdb_byte *) &maddr, 8,
5262 gdbarch_byte_order
5263 (target_gdbarch ()));
b3b9301e 5264 tfile_read ((gdb_byte *) &mlen, 2);
e83b17ba
HZ
5265 mlen = (unsigned short)
5266 extract_unsigned_integer ((gdb_byte *) &mlen,
5267 2, gdbarch_byte_order
5268 (target_gdbarch ()));
b3b9301e
PA
5269
5270 r = VEC_safe_push (mem_range_s, info->memory, NULL);
5271
5272 r->start = maddr;
5273 r->length = mlen;
5274 break;
5275 }
5276 case 'V':
28a93511
YQ
5277 {
5278 int vnum;
5279
5280 tfile_read ((gdb_byte *) &vnum, 4);
5281 VEC_safe_push (int, info->tvars, vnum);
5282 }
b3b9301e
PA
5283 case 'R':
5284 case 'S':
5285 {
5286 break;
5287 }
5288 default:
5289 warning (_("Unhandled trace block type (%d) '%c ' "
5290 "while building trace frame info."),
5291 blocktype, blocktype);
5292 break;
5293 }
5294
5295 return 0;
5296}
5297
5298static struct traceframe_info *
5299tfile_traceframe_info (void)
5300{
5301 struct traceframe_info *info = XCNEW (struct traceframe_info);
5302
5303 traceframe_walk_blocks (build_traceframe_info, 0, info);
5304 return info;
5305}
5306
00bf0b85
SS
5307static void
5308init_tfile_ops (void)
5309{
5310 tfile_ops.to_shortname = "tfile";
5311 tfile_ops.to_longname = "Local trace dump file";
3e43a32a
MS
5312 tfile_ops.to_doc
5313 = "Use a trace file as a target. Specify the filename of the trace file.";
00bf0b85
SS
5314 tfile_ops.to_open = tfile_open;
5315 tfile_ops.to_close = tfile_close;
5316 tfile_ops.to_fetch_registers = tfile_fetch_registers;
5317 tfile_ops.to_xfer_partial = tfile_xfer_partial;
5318 tfile_ops.to_files_info = tfile_files_info;
5319 tfile_ops.to_get_trace_status = tfile_get_trace_status;
f196051f 5320 tfile_ops.to_get_tracepoint_status = tfile_get_tracepoint_status;
00bf0b85 5321 tfile_ops.to_trace_find = tfile_trace_find;
3e43a32a
MS
5322 tfile_ops.to_get_trace_state_variable_value
5323 = tfile_get_trace_state_variable_value;
00bf0b85 5324 tfile_ops.to_stratum = process_stratum;
fce3c1f0 5325 tfile_ops.to_has_all_memory = tfile_has_all_memory;
00bf0b85
SS
5326 tfile_ops.to_has_memory = tfile_has_memory;
5327 tfile_ops.to_has_stack = tfile_has_stack;
5328 tfile_ops.to_has_registers = tfile_has_registers;
b3b9301e 5329 tfile_ops.to_traceframe_info = tfile_traceframe_info;
00bf0b85
SS
5330 tfile_ops.to_magic = OPS_MAGIC;
5331}
5332
5808517f
YQ
5333void
5334free_current_marker (void *arg)
5335{
5336 struct static_tracepoint_marker **marker_p = arg;
5337
5338 if (*marker_p != NULL)
5339 {
5340 release_static_tracepoint_marker (*marker_p);
5341 xfree (*marker_p);
5342 }
5343 else
5344 *marker_p = NULL;
5345}
5346
0fb4aa4b
PA
5347/* Given a line of text defining a static tracepoint marker, parse it
5348 into a "static tracepoint marker" object. Throws an error is
5349 parsing fails. If PP is non-null, it points to one past the end of
5350 the parsed marker definition. */
5351
5352void
5353parse_static_tracepoint_marker_definition (char *line, char **pp,
5354 struct static_tracepoint_marker *marker)
5355{
5356 char *p, *endp;
5357 ULONGEST addr;
5358 int end;
5359
5360 p = line;
5361 p = unpack_varlen_hex (p, &addr);
5362 p++; /* skip a colon */
5363
f5656ead 5364 marker->gdbarch = target_gdbarch ();
0fb4aa4b
PA
5365 marker->address = (CORE_ADDR) addr;
5366
5367 endp = strchr (p, ':');
5368 if (endp == NULL)
74232302 5369 error (_("bad marker definition: %s"), line);
0fb4aa4b
PA
5370
5371 marker->str_id = xmalloc (endp - p + 1);
5372 end = hex2bin (p, (gdb_byte *) marker->str_id, (endp - p + 1) / 2);
5373 marker->str_id[end] = '\0';
5374
5375 p += 2 * end;
5376 p++; /* skip a colon */
5377
5378 marker->extra = xmalloc (strlen (p) + 1);
5379 end = hex2bin (p, (gdb_byte *) marker->extra, strlen (p) / 2);
5380 marker->extra[end] = '\0';
5381
5382 if (pp)
5383 *pp = p;
5384}
5385
5386/* Release a static tracepoint marker's contents. Note that the
5387 object itself isn't released here. There objects are usually on
5388 the stack. */
5389
5390void
5391release_static_tracepoint_marker (struct static_tracepoint_marker *marker)
5392{
5393 xfree (marker->str_id);
5394 marker->str_id = NULL;
5395}
5396
5397/* Print MARKER to gdb_stdout. */
5398
5399static void
5400print_one_static_tracepoint_marker (int count,
5401 struct static_tracepoint_marker *marker)
5402{
5403 struct command_line *l;
5404 struct symbol *sym;
5405
5406 char wrap_indent[80];
5407 char extra_field_indent[80];
79a45e25 5408 struct ui_out *uiout = current_uiout;
0fb4aa4b
PA
5409 struct cleanup *bkpt_chain;
5410 VEC(breakpoint_p) *tracepoints;
5411
5412 struct symtab_and_line sal;
5413
5414 init_sal (&sal);
5415
5416 sal.pc = marker->address;
5417
5418 tracepoints = static_tracepoints_here (marker->address);
5419
5420 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "marker");
5421
5422 /* A counter field to help readability. This is not a stable
5423 identifier! */
5424 ui_out_field_int (uiout, "count", count);
5425
5426 ui_out_field_string (uiout, "marker-id", marker->str_id);
5427
5428 ui_out_field_fmt (uiout, "enabled", "%c",
5429 !VEC_empty (breakpoint_p, tracepoints) ? 'y' : 'n');
5430 ui_out_spaces (uiout, 2);
5431
5432 strcpy (wrap_indent, " ");
5433
5434 if (gdbarch_addr_bit (marker->gdbarch) <= 32)
5435 strcat (wrap_indent, " ");
5436 else
5437 strcat (wrap_indent, " ");
5438
5439 strcpy (extra_field_indent, " ");
5440
5441 ui_out_field_core_addr (uiout, "addr", marker->gdbarch, marker->address);
5442
5443 sal = find_pc_line (marker->address, 0);
5444 sym = find_pc_sect_function (marker->address, NULL);
5445 if (sym)
5446 {
5447 ui_out_text (uiout, "in ");
5448 ui_out_field_string (uiout, "func",
5449 SYMBOL_PRINT_NAME (sym));
5450 ui_out_wrap_hint (uiout, wrap_indent);
5451 ui_out_text (uiout, " at ");
5452 }
5453 else
5454 ui_out_field_skip (uiout, "func");
5455
5456 if (sal.symtab != NULL)
5457 {
05cba821
JK
5458 ui_out_field_string (uiout, "file",
5459 symtab_to_filename_for_display (sal.symtab));
0fb4aa4b
PA
5460 ui_out_text (uiout, ":");
5461
5462 if (ui_out_is_mi_like_p (uiout))
5463 {
0b0865da 5464 const char *fullname = symtab_to_fullname (sal.symtab);
0fb4aa4b 5465
f35a17b5 5466 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
5467 }
5468 else
5469 ui_out_field_skip (uiout, "fullname");
5470
5471 ui_out_field_int (uiout, "line", sal.line);
5472 }
5473 else
5474 {
5475 ui_out_field_skip (uiout, "fullname");
5476 ui_out_field_skip (uiout, "line");
5477 }
5478
5479 ui_out_text (uiout, "\n");
5480 ui_out_text (uiout, extra_field_indent);
5481 ui_out_text (uiout, _("Data: \""));
5482 ui_out_field_string (uiout, "extra-data", marker->extra);
5483 ui_out_text (uiout, "\"\n");
5484
5485 if (!VEC_empty (breakpoint_p, tracepoints))
5486 {
5487 struct cleanup *cleanup_chain;
5488 int ix;
5489 struct breakpoint *b;
5490
5491 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout,
5492 "tracepoints-at");
5493
5494 ui_out_text (uiout, extra_field_indent);
5495 ui_out_text (uiout, _("Probed by static tracepoints: "));
5496 for (ix = 0; VEC_iterate(breakpoint_p, tracepoints, ix, b); ix++)
5497 {
5498 if (ix > 0)
5499 ui_out_text (uiout, ", ");
5500 ui_out_text (uiout, "#");
5501 ui_out_field_int (uiout, "tracepoint-id", b->number);
5502 }
5503
5504 do_cleanups (cleanup_chain);
5505
5506 if (ui_out_is_mi_like_p (uiout))
5507 ui_out_field_int (uiout, "number-of-tracepoints",
5508 VEC_length(breakpoint_p, tracepoints));
5509 else
5510 ui_out_text (uiout, "\n");
5511 }
5512 VEC_free (breakpoint_p, tracepoints);
5513
5514 do_cleanups (bkpt_chain);
0fb4aa4b
PA
5515}
5516
5517static void
5518info_static_tracepoint_markers_command (char *arg, int from_tty)
5519{
5520 VEC(static_tracepoint_marker_p) *markers;
5521 struct cleanup *old_chain;
5522 struct static_tracepoint_marker *marker;
79a45e25 5523 struct ui_out *uiout = current_uiout;
0fb4aa4b
PA
5524 int i;
5525
d1feda86
YQ
5526 /* We don't have to check target_can_use_agent and agent's capability on
5527 static tracepoint here, in order to be compatible with older GDBserver.
5528 We don't check USE_AGENT is true or not, because static tracepoints
5529 don't work without in-process agent, so we don't bother users to type
5530 `set agent on' when to use static tracepoint. */
5531
0fb4aa4b
PA
5532 old_chain
5533 = make_cleanup_ui_out_table_begin_end (uiout, 5, -1,
5534 "StaticTracepointMarkersTable");
5535
5536 ui_out_table_header (uiout, 7, ui_left, "counter", "Cnt");
5537
5538 ui_out_table_header (uiout, 40, ui_left, "marker-id", "ID");
5539
5540 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb");
f5656ead 5541 if (gdbarch_addr_bit (target_gdbarch ()) <= 32)
0fb4aa4b
PA
5542 ui_out_table_header (uiout, 10, ui_left, "addr", "Address");
5543 else
5544 ui_out_table_header (uiout, 18, ui_left, "addr", "Address");
5545 ui_out_table_header (uiout, 40, ui_noalign, "what", "What");
5546
5547 ui_out_table_body (uiout);
5548
5549 markers = target_static_tracepoint_markers_by_strid (NULL);
5550 make_cleanup (VEC_cleanup (static_tracepoint_marker_p), &markers);
5551
5552 for (i = 0;
5553 VEC_iterate (static_tracepoint_marker_p,
5554 markers, i, marker);
5555 i++)
5556 {
5557 print_one_static_tracepoint_marker (i + 1, marker);
5558 release_static_tracepoint_marker (marker);
5559 }
5560
5561 do_cleanups (old_chain);
5562}
5563
5564/* The $_sdata convenience variable is a bit special. We don't know
5565 for sure type of the value until we actually have a chance to fetch
5566 the data --- the size of the object depends on what has been
5567 collected. We solve this by making $_sdata be an internalvar that
5568 creates a new value on access. */
5569
5570/* Return a new value with the correct type for the sdata object of
5571 the current trace frame. Return a void value if there's no object
5572 available. */
5573
5574static struct value *
22d2b532
SDJ
5575sdata_make_value (struct gdbarch *gdbarch, struct internalvar *var,
5576 void *ignore)
0fb4aa4b
PA
5577{
5578 LONGEST size;
5579 gdb_byte *buf;
5580
5581 /* We need to read the whole object before we know its size. */
5582 size = target_read_alloc (&current_target,
5583 TARGET_OBJECT_STATIC_TRACE_DATA,
5584 NULL, &buf);
5585 if (size >= 0)
5586 {
5587 struct value *v;
5588 struct type *type;
5589
5590 type = init_vector_type (builtin_type (gdbarch)->builtin_true_char,
5591 size);
5592 v = allocate_value (type);
5593 memcpy (value_contents_raw (v), buf, size);
5594 xfree (buf);
5595 return v;
5596 }
5597 else
5598 return allocate_value (builtin_type (gdbarch)->builtin_void);
5599}
5600
b3b9301e
PA
5601#if !defined(HAVE_LIBEXPAT)
5602
5603struct traceframe_info *
5604parse_traceframe_info (const char *tframe_info)
5605{
5606 static int have_warned;
5607
5608 if (!have_warned)
5609 {
5610 have_warned = 1;
5611 warning (_("Can not parse XML trace frame info; XML support "
5612 "was disabled at compile time"));
5613 }
5614
5615 return NULL;
5616}
5617
5618#else /* HAVE_LIBEXPAT */
5619
5620#include "xml-support.h"
5621
5622/* Handle the start of a <memory> element. */
5623
5624static void
5625traceframe_info_start_memory (struct gdb_xml_parser *parser,
5626 const struct gdb_xml_element *element,
5627 void *user_data, VEC(gdb_xml_value_s) *attributes)
5628{
5629 struct traceframe_info *info = user_data;
5630 struct mem_range *r = VEC_safe_push (mem_range_s, info->memory, NULL);
5631 ULONGEST *start_p, *length_p;
5632
5633 start_p = xml_find_attribute (attributes, "start")->value;
5634 length_p = xml_find_attribute (attributes, "length")->value;
5635
5636 r->start = *start_p;
5637 r->length = *length_p;
5638}
5639
28a93511
YQ
5640/* Handle the start of a <tvar> element. */
5641
5642static void
5643traceframe_info_start_tvar (struct gdb_xml_parser *parser,
5644 const struct gdb_xml_element *element,
5645 void *user_data,
5646 VEC(gdb_xml_value_s) *attributes)
5647{
5648 struct traceframe_info *info = user_data;
5649 const char *id_attrib = xml_find_attribute (attributes, "id")->value;
5650 int id = gdb_xml_parse_ulongest (parser, id_attrib);
5651
5652 VEC_safe_push (int, info->tvars, id);
5653}
5654
b3b9301e
PA
5655/* Discard the constructed trace frame info (if an error occurs). */
5656
5657static void
5658free_result (void *p)
5659{
5660 struct traceframe_info *result = p;
5661
5662 free_traceframe_info (result);
5663}
5664
5665/* The allowed elements and attributes for an XML memory map. */
5666
5667static const struct gdb_xml_attribute memory_attributes[] = {
5668 { "start", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
5669 { "length", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
5670 { NULL, GDB_XML_AF_NONE, NULL, NULL }
5671};
5672
28a93511
YQ
5673static const struct gdb_xml_attribute tvar_attributes[] = {
5674 { "id", GDB_XML_AF_NONE, NULL, NULL },
5675 { NULL, GDB_XML_AF_NONE, NULL, NULL }
5676};
5677
b3b9301e
PA
5678static const struct gdb_xml_element traceframe_info_children[] = {
5679 { "memory", memory_attributes, NULL,
5680 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
5681 traceframe_info_start_memory, NULL },
28a93511
YQ
5682 { "tvar", tvar_attributes, NULL,
5683 GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
5684 traceframe_info_start_tvar, NULL },
b3b9301e
PA
5685 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
5686};
5687
5688static const struct gdb_xml_element traceframe_info_elements[] = {
5689 { "traceframe-info", NULL, traceframe_info_children, GDB_XML_EF_NONE,
5690 NULL, NULL },
5691 { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
5692};
5693
5694/* Parse a traceframe-info XML document. */
5695
5696struct traceframe_info *
5697parse_traceframe_info (const char *tframe_info)
5698{
5699 struct traceframe_info *result;
5700 struct cleanup *back_to;
5701
5702 result = XCNEW (struct traceframe_info);
5703 back_to = make_cleanup (free_result, result);
5704
5705 if (gdb_xml_parse_quick (_("trace frame info"),
5706 "traceframe-info.dtd", traceframe_info_elements,
5707 tframe_info, result) == 0)
5708 {
5709 /* Parsed successfully, keep the result. */
5710 discard_cleanups (back_to);
5711
5712 return result;
5713 }
5714
5715 do_cleanups (back_to);
5716 return NULL;
5717}
5718
5719#endif /* HAVE_LIBEXPAT */
5720
5721/* Returns the traceframe_info object for the current traceframe.
5722 This is where we avoid re-fetching the object from the target if we
5723 already have it cached. */
5724
dc673c81 5725struct traceframe_info *
b3b9301e
PA
5726get_traceframe_info (void)
5727{
5728 if (traceframe_info == NULL)
5729 traceframe_info = target_traceframe_info ();
5730
5731 return traceframe_info;
5732}
5733
c0f61f9c
PA
5734/* If the target supports the query, return in RESULT the set of
5735 collected memory in the current traceframe, found within the LEN
5736 bytes range starting at MEMADDR. Returns true if the target
5737 supports the query, otherwise returns false, and RESULT is left
5738 undefined. */
2a7498d8
PA
5739
5740int
5741traceframe_available_memory (VEC(mem_range_s) **result,
5742 CORE_ADDR memaddr, ULONGEST len)
5743{
5744 struct traceframe_info *info = get_traceframe_info ();
5745
5746 if (info != NULL)
5747 {
5748 struct mem_range *r;
5749 int i;
5750
5751 *result = NULL;
5752
5753 for (i = 0; VEC_iterate (mem_range_s, info->memory, i, r); i++)
5754 if (mem_ranges_overlap (r->start, r->length, memaddr, len))
5755 {
5756 ULONGEST lo1, hi1, lo2, hi2;
5757 struct mem_range *nr;
5758
5759 lo1 = memaddr;
5760 hi1 = memaddr + len;
5761
5762 lo2 = r->start;
5763 hi2 = r->start + r->length;
5764
5765 nr = VEC_safe_push (mem_range_s, *result, NULL);
5766
5767 nr->start = max (lo1, lo2);
5768 nr->length = min (hi1, hi2) - nr->start;
5769 }
5770
5771 normalize_mem_ranges (*result);
5772 return 1;
5773 }
5774
5775 return 0;
5776}
5777
22d2b532
SDJ
5778/* Implementation of `sdata' variable. */
5779
5780static const struct internalvar_funcs sdata_funcs =
5781{
5782 sdata_make_value,
5783 NULL,
5784 NULL
5785};
5786
c906108c
SS
5787/* module initialization */
5788void
fba45db2 5789_initialize_tracepoint (void)
c906108c 5790{
fa58ee11
EZ
5791 struct cmd_list_element *c;
5792
0fb4aa4b
PA
5793 /* Explicitly create without lookup, since that tries to create a
5794 value with a void typed value, and when we get here, gdbarch
5795 isn't initialized yet. At this point, we're quite sure there
5796 isn't another convenience variable of the same name. */
22d2b532 5797 create_internalvar_type_lazy ("_sdata", &sdata_funcs, NULL);
0fb4aa4b 5798
c906108c
SS
5799 traceframe_number = -1;
5800 tracepoint_number = -1;
5801
c5aa993b 5802 add_info ("scope", scope_info,
1bedd215 5803 _("List the variables local to a scope"));
c906108c 5804
e00d1dc8 5805 add_cmd ("tracepoints", class_trace, NULL,
1a966eab 5806 _("Tracing of program execution without stopping the program."),
c906108c
SS
5807 &cmdlist);
5808
c5aa993b 5809 add_com ("tdump", class_trace, trace_dump_command,
1bedd215 5810 _("Print everything collected at the current tracepoint."));
c906108c 5811
00bf0b85
SS
5812 add_com ("tsave", class_trace, trace_save_command, _("\
5813Save the trace data to a file.\n\
d0353e76 5814Use the '-ctf' option to save the data to CTF format.\n\
00bf0b85
SS
5815Use the '-r' option to direct the target to save directly to the file,\n\
5816using its own filesystem."));
5817
f61e138d
SS
5818 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
5819Define a trace state variable.\n\
5820Argument is a $-prefixed name, optionally followed\n\
5821by '=' and an expression that sets the initial value\n\
5822at the start of tracing."));
5823 set_cmd_completer (c, expression_completer);
5824
5825 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
5826Delete one or more trace state variables.\n\
5827Arguments are the names of the variables to delete.\n\
5828If no arguments are supplied, delete all variables."), &deletelist);
c378eb4e 5829 /* FIXME add a trace variable completer. */
f61e138d
SS
5830
5831 add_info ("tvariables", tvariables_info, _("\
5832Status of trace state variables and their values.\n\
0fb4aa4b
PA
5833"));
5834
5835 add_info ("static-tracepoint-markers",
5836 info_static_tracepoint_markers_command, _("\
5837List target static tracepoints markers.\n\
f61e138d
SS
5838"));
5839
1bedd215
AC
5840 add_prefix_cmd ("tfind", class_trace, trace_find_command, _("\
5841Select a trace frame;\n\
5842No argument means forward by one frame; '-' means backward by one frame."),
c906108c
SS
5843 &tfindlist, "tfind ", 1, &cmdlist);
5844
1a966eab 5845 add_cmd ("outside", class_trace, trace_find_outside_command, _("\
081dfbf7 5846Select a trace frame whose PC is outside the given range (exclusive).\n\
1a966eab 5847Usage: tfind outside addr1, addr2"),
c906108c
SS
5848 &tfindlist);
5849
1a966eab 5850 add_cmd ("range", class_trace, trace_find_range_command, _("\
081dfbf7 5851Select a trace frame whose PC is in the given range (inclusive).\n\
1a966eab 5852Usage: tfind range addr1,addr2"),
c906108c
SS
5853 &tfindlist);
5854
1a966eab
AC
5855 add_cmd ("line", class_trace, trace_find_line_command, _("\
5856Select a trace frame by source line.\n\
cce7e648 5857Argument can be a line number (with optional source file),\n\
c906108c 5858a function name, or '*' followed by an address.\n\
1a966eab 5859Default argument is 'the next source line that was traced'."),
c906108c
SS
5860 &tfindlist);
5861
1a966eab
AC
5862 add_cmd ("tracepoint", class_trace, trace_find_tracepoint_command, _("\
5863Select a trace frame by tracepoint number.\n\
5864Default is the tracepoint for the current trace frame."),
c906108c
SS
5865 &tfindlist);
5866
1a966eab
AC
5867 add_cmd ("pc", class_trace, trace_find_pc_command, _("\
5868Select a trace frame by PC.\n\
5869Default is the current PC, or the PC of the current trace frame."),
c906108c
SS
5870 &tfindlist);
5871
1a966eab 5872 add_cmd ("end", class_trace, trace_find_end_command, _("\
1a966eab 5873De-select any trace frame and resume 'live' debugging."),
c906108c
SS
5874 &tfindlist);
5875
8acc4065 5876 add_alias_cmd ("none", "end", class_trace, 0, &tfindlist);
c906108c
SS
5877
5878 add_cmd ("start", class_trace, trace_find_start_command,
1a966eab 5879 _("Select the first trace frame in the trace buffer."),
c906108c
SS
5880 &tfindlist);
5881
c5aa993b 5882 add_com ("tstatus", class_trace, trace_status_command,
1bedd215 5883 _("Display the status of the current trace data collection."));
c906108c 5884
f196051f
SS
5885 add_com ("tstop", class_trace, trace_stop_command, _("\
5886Stop trace data collection.\n\
5887Usage: tstop [ <notes> ... ]\n\
5888Any arguments supplied are recorded with the trace as a stop reason and\n\
5889reported by tstatus (if the target supports trace notes)."));
c906108c 5890
f196051f
SS
5891 add_com ("tstart", class_trace, trace_start_command, _("\
5892Start trace data collection.\n\
5893Usage: tstart [ <notes> ... ]\n\
5894Any arguments supplied are recorded with the trace as a note and\n\
5895reported by tstatus (if the target supports trace notes)."));
c906108c 5896
1bedd215
AC
5897 add_com ("end", class_trace, end_actions_pseudocommand, _("\
5898Ends a list of commands or actions.\n\
c906108c
SS
5899Several GDB commands allow you to enter a list of commands or actions.\n\
5900Entering \"end\" on a line by itself is the normal way to terminate\n\
5901such a list.\n\n\
1bedd215 5902Note: the \"end\" command cannot be used at the gdb prompt."));
c906108c 5903
1bedd215
AC
5904 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
5905Specify single-stepping behavior at a tracepoint.\n\
c906108c
SS
5906Argument is number of instructions to trace in single-step mode\n\
5907following the tracepoint. This command is normally followed by\n\
5908one or more \"collect\" commands, to specify what to collect\n\
5909while single-stepping.\n\n\
1bedd215 5910Note: this command can only be used in a tracepoint \"actions\" list."));
c906108c 5911
c5aa993b
JM
5912 add_com_alias ("ws", "while-stepping", class_alias, 0);
5913 add_com_alias ("stepping", "while-stepping", class_alias, 0);
c906108c 5914
1bedd215
AC
5915 add_com ("collect", class_trace, collect_pseudocommand, _("\
5916Specify one or more data items to be collected at a tracepoint.\n\
c906108c
SS
5917Accepts a comma-separated list of (one or more) expressions. GDB will\n\
5918collect all data (variables, registers) referenced by that expression.\n\
5919Also accepts the following special arguments:\n\
5920 $regs -- all registers.\n\
5921 $args -- all function arguments.\n\
5922 $locals -- all variables local to the block/function scope.\n\
0fb4aa4b 5923 $_sdata -- static tracepoint data (ignored for non-static tracepoints).\n\
1bedd215 5924Note: this command can only be used in a tracepoint \"actions\" list."));
c906108c 5925
6da95a67
SS
5926 add_com ("teval", class_trace, teval_pseudocommand, _("\
5927Specify one or more expressions to be evaluated at a tracepoint.\n\
5928Accepts a comma-separated list of (one or more) expressions.\n\
5929The result of each evaluation will be discarded.\n\
5930Note: this command can only be used in a tracepoint \"actions\" list."));
5931
1bedd215
AC
5932 add_com ("actions", class_trace, trace_actions_command, _("\
5933Specify the actions to be taken at a tracepoint.\n\
cce7e648
PA
5934Tracepoint actions may include collecting of specified data,\n\
5935single-stepping, or enabling/disabling other tracepoints,\n\
1bedd215 5936depending on target's capabilities."));
c906108c 5937
236f1d4d
SS
5938 default_collect = xstrdup ("");
5939 add_setshow_string_cmd ("default-collect", class_trace,
5940 &default_collect, _("\
5941Set the list of expressions to collect by default"), _("\
5942Show the list of expressions to collect by default"), NULL,
5943 NULL, NULL,
5944 &setlist, &showlist);
5945
d5551862
SS
5946 add_setshow_boolean_cmd ("disconnected-tracing", no_class,
5947 &disconnected_tracing, _("\
5948Set whether tracing continues after GDB disconnects."), _("\
5949Show whether tracing continues after GDB disconnects."), _("\
5950Use this to continue a tracing run even if GDB disconnects\n\
5951or detaches from the target. You can reconnect later and look at\n\
5952trace data collected in the meantime."),
5953 set_disconnected_tracing,
5954 NULL,
5955 &setlist,
5956 &showlist);
00bf0b85 5957
4daf5ac0
SS
5958 add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
5959 &circular_trace_buffer, _("\
5960Set target's use of circular trace buffer."), _("\
5961Show target's use of circular trace buffer."), _("\
5962Use this to make the trace buffer into a circular buffer,\n\
5963which will discard traceframes (oldest first) instead of filling\n\
5964up and stopping the trace run."),
5965 set_circular_trace_buffer,
5966 NULL,
5967 &setlist,
5968 &showlist);
5969
f6f899bf 5970 add_setshow_zuinteger_unlimited_cmd ("trace-buffer-size", no_class,
9b67fcec 5971 &trace_buffer_size, _("\
f6f899bf
HAQ
5972Set requested size of trace buffer."), _("\
5973Show requested size of trace buffer."), _("\
5974Use this to choose a size for the trace buffer. Some targets\n\
f81d1120
PA
5975may have fixed or limited buffer sizes. Specifying \"unlimited\" or -1\n\
5976disables any attempt to set the buffer size and lets the target choose."),
9b67fcec
YQ
5977 set_trace_buffer_size, NULL,
5978 &setlist, &showlist);
f6f899bf 5979
f196051f
SS
5980 add_setshow_string_cmd ("trace-user", class_trace,
5981 &trace_user, _("\
5982Set the user name to use for current and future trace runs"), _("\
5983Show the user name to use for current and future trace runs"), NULL,
5984 set_trace_user, NULL,
5985 &setlist, &showlist);
5986
5987 add_setshow_string_cmd ("trace-notes", class_trace,
5988 &trace_notes, _("\
5989Set notes string to use for current and future trace runs"), _("\
5990Show the notes string to use for current and future trace runs"), NULL,
5991 set_trace_notes, NULL,
5992 &setlist, &showlist);
5993
5994 add_setshow_string_cmd ("trace-stop-notes", class_trace,
5995 &trace_stop_notes, _("\
5996Set notes string to use for future tstop commands"), _("\
5997Show the notes string to use for future tstop commands"), NULL,
5998 set_trace_stop_notes, NULL,
5999 &setlist, &showlist);
6000
00bf0b85
SS
6001 init_tfile_ops ();
6002
9852c492 6003 add_target_with_completer (&tfile_ops, filename_completer);
c906108c 6004}
This page took 1.882403 seconds and 4 git commands to generate.