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