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