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