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