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