Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / tracepoint.c
CommitLineData
5796c8dc
SS
1/* Tracing functionality for remote targets in custom GDB protocol
2
ef5ccd6c 3 Copyright (C) 1997-2013 Free Software Foundation, Inc.
5796c8dc
SS
4
5 This file is part of GDB.
6
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "arch-utils.h"
22#include "symtab.h"
23#include "frame.h"
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"
31#include "inferior.h"
32#include "breakpoint.h"
33#include "tracepoint.h"
5796c8dc
SS
34#include "linespec.h"
35#include "regcache.h"
36#include "completer.h"
37#include "block.h"
38#include "dictionary.h"
39#include "observer.h"
40#include "user-regs.h"
41#include "valprint.h"
42#include "gdbcore.h"
43#include "objfiles.h"
cf7f2e2d
JM
44#include "filenames.h"
45#include "gdbthread.h"
46#include "stack.h"
47#include "gdbcore.h"
48#include "remote.h"
49#include "source.h"
5796c8dc
SS
50#include "ax.h"
51#include "ax-gdb.h"
c50c785c
JM
52#include "memrange.h"
53#include "exceptions.h"
a45ae5f8 54#include "cli/cli-utils.h"
ef5ccd6c 55#include "probe.h"
5796c8dc
SS
56
57/* readline include files */
58#include "readline/readline.h"
59#include "readline/history.h"
60
61/* readline defines this. */
62#undef savestring
63
64#ifdef HAVE_UNISTD_H
65#include <unistd.h>
66#endif
67
cf7f2e2d
JM
68#ifndef O_LARGEFILE
69#define O_LARGEFILE 0
70#endif
71
5796c8dc
SS
72/* Maximum length of an agent aexpression.
73 This accounts for the fact that packets are limited to 400 bytes
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.
77
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
c50c785c
JM
82#define TFILE_PID (1)
83
cf7f2e2d
JM
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);
5796c8dc
SS
88
89extern void (*deprecated_readline_begin_hook) (char *, ...);
90extern char *(*deprecated_readline_hook) (char *);
91extern void (*deprecated_readline_end_hook) (void);
92
5796c8dc
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.
116 */
117
118
119/* ======= Important global variables: ======= */
120
cf7f2e2d
JM
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
c50c785c
JM
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
cf7f2e2d
JM
136static VEC(tsv_s) *tvariables;
137
138/* The next integer to assign to a variable. */
139
140static int next_tsv_number = 1;
141
5796c8dc
SS
142/* Number of last traceframe collected. */
143static int traceframe_number;
144
145/* Tracepoint for last traceframe collected. */
146static int tracepoint_number;
147
c50c785c 148/* Symbol for function for last traceframe collected. */
5796c8dc
SS
149static struct symbol *traceframe_fun;
150
c50c785c 151/* Symtab and line for last traceframe collected. */
5796c8dc
SS
152static struct symtab_and_line traceframe_sal;
153
c50c785c
JM
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
160/* Tracing command lists. */
5796c8dc
SS
161static struct cmd_list_element *tfindlist;
162
cf7f2e2d
JM
163/* List of expressions to collect by default at each tracepoint hit. */
164char *default_collect = "";
165
166static int disconnected_tracing;
167
168/* This variable controls whether we ask the target for a linear or
169 circular trace buffer. */
170
171static int circular_trace_buffer;
172
ef5ccd6c
JM
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
a45ae5f8
JM
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
5796c8dc
SS
190/* ======= Important command functions: ======= */
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);
5796c8dc
SS
201static void trace_dump_command (char *, int);
202
203/* support routines */
204
205struct collection_list;
206static void add_aexpr (struct collection_list *, struct agent_expr *);
207static char *mem2hex (gdb_byte *, char *, int);
208static void add_register (struct collection_list *collection,
209 unsigned int regno);
cf7f2e2d 210
cf7f2e2d
JM
211static void free_uploaded_tps (struct uploaded_tp **utpp);
212static void free_uploaded_tsvs (struct uploaded_tsv **utsvp);
213
5796c8dc
SS
214
215extern void _initialize_tracepoint (void);
216
cf7f2e2d 217static struct trace_status trace_status;
5796c8dc 218
cf7f2e2d
JM
219char *stop_reason_names[] = {
220 "tunknown",
221 "tnotrun",
222 "tstop",
223 "tfull",
224 "tdisconnected",
225 "tpasscount",
226 "terror"
227};
5796c8dc 228
cf7f2e2d 229struct trace_status *
c50c785c 230current_trace_status (void)
5796c8dc 231{
cf7f2e2d 232 return &trace_status;
5796c8dc
SS
233}
234
c50c785c
JM
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
a45ae5f8 248/* Free and clear the traceframe info cache of the current
c50c785c
JM
249 traceframe. */
250
251static void
252clear_traceframe_info (void)
253{
254 free_traceframe_info (traceframe_info);
255 traceframe_info = NULL;
256}
257
5796c8dc
SS
258/* Set traceframe number to NUM. */
259static void
260set_traceframe_num (int num)
261{
262 traceframe_number = num;
263 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
264}
265
266/* Set tracepoint number to NUM. */
267static void
268set_tracepoint_num (int num)
269{
270 tracepoint_number = num;
271 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
272}
273
274/* Set externally visible debug variables for querying/printing
c50c785c 275 the traceframe context (line, function, file). */
5796c8dc
SS
276
277static void
278set_traceframe_context (struct frame_info *trace_frame)
279{
280 CORE_ADDR trace_pc;
281
c50c785c
JM
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
5796c8dc 295 {
c50c785c
JM
296 init_sal (&traceframe_sal);
297 traceframe_fun = NULL;
5796c8dc 298 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
5796c8dc
SS
299 }
300
5796c8dc
SS
301 /* Save func name as "$trace_func", a debugger variable visible to
302 users. */
303 if (traceframe_fun == NULL
304 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
305 clear_internalvar (lookup_internalvar ("trace_func"));
306 else
307 set_internalvar_string (lookup_internalvar ("trace_func"),
308 SYMBOL_LINKAGE_NAME (traceframe_fun));
309
310 /* Save file name as "$trace_file", a debugger variable visible to
311 users. */
ef5ccd6c 312 if (traceframe_sal.symtab == NULL)
5796c8dc
SS
313 clear_internalvar (lookup_internalvar ("trace_file"));
314 else
315 set_internalvar_string (lookup_internalvar ("trace_file"),
ef5ccd6c 316 symtab_to_filename_for_display (traceframe_sal.symtab));
5796c8dc
SS
317}
318
cf7f2e2d
JM
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));
327 tsv.name = xstrdup (name);
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
ef5ccd6c 347static void
cf7f2e2d
JM
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 {
ef5ccd6c
JM
356 observer_notify_tsv_deleted (tsv);
357
cf7f2e2d
JM
358 xfree ((void *)tsv->name);
359 VEC_unordered_remove (tsv_s, tvariables, ix);
ef5ccd6c 360
cf7f2e2d
JM
361 return;
362 }
363
364 warning (_("No trace variable named \"$%s\", not deleting"), name);
365}
366
ef5ccd6c
JM
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
cf7f2e2d
JM
391/* The 'tvariable' command collects a name and optional expression to
392 evaluate into an initial value. */
393
ef5ccd6c 394static void
cf7f2e2d
JM
395trace_variable_command (char *args, int from_tty)
396{
cf7f2e2d 397 struct cleanup *old_chain;
cf7f2e2d
JM
398 LONGEST initval = 0;
399 struct trace_state_variable *tsv;
ef5ccd6c 400 char *name, *p;
cf7f2e2d
JM
401
402 if (!args || !*args)
ef5ccd6c 403 error_no_arg (_("Syntax is $NAME [ = EXPR ]"));
cf7f2e2d 404
ef5ccd6c
JM
405 /* Only allow two syntaxes; "$name" and "$name=value". */
406 p = skip_spaces (args);
cf7f2e2d 407
ef5ccd6c
JM
408 if (*p++ != '$')
409 error (_("Name of trace variable should start with '$'"));
cf7f2e2d 410
ef5ccd6c
JM
411 name = p;
412 while (isalnum (*p) || *p == '_')
413 p++;
414 name = savestring (name, p - name);
415 old_chain = make_cleanup (xfree, name);
416
417 p = skip_spaces (p);
418 if (*p != '=' && *p != '\0')
cf7f2e2d
JM
419 error (_("Syntax must be $NAME [ = EXPR ]"));
420
ef5ccd6c 421 validate_trace_state_variable_name (name);
cf7f2e2d 422
ef5ccd6c
JM
423 if (*p == '=')
424 initval = value_as_long (parse_and_eval (++p));
cf7f2e2d
JM
425
426 /* If the variable already exists, just change its initial value. */
ef5ccd6c 427 tsv = find_trace_state_variable (name);
cf7f2e2d
JM
428 if (tsv)
429 {
ef5ccd6c
JM
430 if (tsv->initial_value != initval)
431 {
432 tsv->initial_value = initval;
433 observer_notify_tsv_modified (tsv);
434 }
c50c785c
JM
435 printf_filtered (_("Trace state variable $%s "
436 "now has initial value %s.\n"),
cf7f2e2d
JM
437 tsv->name, plongest (tsv->initial_value));
438 do_cleanups (old_chain);
439 return;
440 }
441
442 /* Create a new variable. */
ef5ccd6c 443 tsv = create_trace_state_variable (name);
cf7f2e2d
JM
444 tsv->initial_value = initval;
445
ef5ccd6c
JM
446 observer_notify_tsv_created (tsv);
447
c50c785c
JM
448 printf_filtered (_("Trace state variable $%s "
449 "created, with initial value %s.\n"),
cf7f2e2d
JM
450 tsv->name, plongest (tsv->initial_value));
451
452 do_cleanups (old_chain);
453}
454
ef5ccd6c 455static void
cf7f2e2d
JM
456delete_trace_variable_command (char *args, int from_tty)
457{
458 int ix;
459 char **argv;
460 struct cleanup *back_to;
461
462 if (args == NULL)
463 {
464 if (query (_("Delete all trace state variables? ")))
465 VEC_free (tsv_s, tvariables);
466 dont_repeat ();
ef5ccd6c 467 observer_notify_tsv_deleted (NULL);
cf7f2e2d
JM
468 return;
469 }
470
471 argv = gdb_buildargv (args);
472 back_to = make_cleanup_freeargv (argv);
473
474 for (ix = 0; argv[ix] != NULL; ix++)
475 {
476 if (*argv[ix] == '$')
477 delete_trace_state_variable (argv[ix] + 1);
478 else
479 warning (_("Name \"%s\" not prefixed with '$', ignoring"), argv[ix]);
480 }
481
482 do_cleanups (back_to);
483
484 dont_repeat ();
485}
486
487void
488tvariables_info_1 (void)
489{
490 struct trace_state_variable *tsv;
491 int ix;
492 int count = 0;
493 struct cleanup *back_to;
a45ae5f8 494 struct ui_out *uiout = current_uiout;
cf7f2e2d
JM
495
496 if (VEC_length (tsv_s, tvariables) == 0 && !ui_out_is_mi_like_p (uiout))
497 {
498 printf_filtered (_("No trace state variables.\n"));
499 return;
500 }
501
502 /* Try to acquire values from the target. */
503 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix, ++count)
504 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
505 &(tsv->value));
506
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);
514
515 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
516 {
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
523 name = concat ("$", tsv->name, (char *) NULL);
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
528 if (tsv->value_known)
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;
535 else if (current_trace_status ()->running || traceframe_number >= 0)
536 /* The value is/was defined, but we don't have it. */
537 c = "<unknown>";
538 else
539 /* It is not meaningful to ask about the value. */
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);
546 }
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 ();
557}
558
559/* Stash definitions of tsvs into the given file. */
5796c8dc 560
cf7f2e2d
JM
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
576/* ACTIONS functions: */
5796c8dc
SS
577
578/* The three functions:
579 collect_pseudocommand,
580 while_stepping_pseudocommand, and
581 end_actions_pseudocommand
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
ef5ccd6c 587static void
5796c8dc
SS
588end_actions_pseudocommand (char *args, int from_tty)
589{
590 error (_("This command cannot be used at the top level."));
591}
592
ef5ccd6c 593static void
5796c8dc
SS
594while_stepping_pseudocommand (char *args, int from_tty)
595{
596 error (_("This command can only be used in a tracepoint actions list."));
597}
598
599static void
600collect_pseudocommand (char *args, int from_tty)
601{
602 error (_("This command can only be used in a tracepoint actions list."));
603}
604
cf7f2e2d
JM
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
a45ae5f8
JM
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
5796c8dc
SS
651/* Enter a list of actions for a tracepoint. */
652static void
653trace_actions_command (char *args, int from_tty)
654{
a45ae5f8 655 struct tracepoint *t;
cf7f2e2d 656 struct command_line *l;
5796c8dc 657
c50c785c 658 t = get_tracepoint_by_number (&args, NULL, 1);
5796c8dc
SS
659 if (t)
660 {
cf7f2e2d
JM
661 char *tmpbuf =
662 xstrprintf ("Enter actions for tracepoint %d, one per line.",
a45ae5f8 663 t->base.number);
cf7f2e2d
JM
664 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
665
c50c785c
JM
666 l = read_command_lines (tmpbuf, from_tty, 1,
667 check_tracepoint_command, t);
cf7f2e2d 668 do_cleanups (cleanups);
a45ae5f8 669 breakpoint_set_commands (&t->base, l);
5796c8dc
SS
670 }
671 /* else just return */
672}
673
cf7f2e2d
JM
674/* Report the results of checking the agent expression, as errors or
675 internal errors. */
5796c8dc 676
cf7f2e2d
JM
677static void
678report_agent_reqs_errors (struct agent_expr *aexpr)
679{
680 /* All of the "flaws" are serious bytecode generation issues that
681 should never occur. */
682 if (aexpr->flaw != agent_flaw_none)
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. */
687 if (aexpr->min_height < 0)
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. */
697 if (aexpr->max_height > 20)
698 error (_("Expression is too complicated."));
5796c8dc
SS
699}
700
701/* worker function */
cf7f2e2d 702void
a45ae5f8 703validate_actionline (char **line, struct breakpoint *b)
5796c8dc
SS
704{
705 struct cmd_list_element *c;
706 struct expression *exp = NULL;
707 struct cleanup *old_chain = NULL;
cf7f2e2d
JM
708 char *p, *tmp_p;
709 struct bp_location *loc;
710 struct agent_expr *aexpr;
a45ae5f8 711 struct tracepoint *t = (struct tracepoint *) b;
5796c8dc 712
c50c785c 713 /* If EOF is typed, *line is NULL. */
5796c8dc 714 if (*line == NULL)
cf7f2e2d 715 return;
5796c8dc 716
ef5ccd6c 717 p = skip_spaces (*line);
5796c8dc
SS
718
719 /* Symbol lookup etc. */
720 if (*p == '\0') /* empty line: just prompt for another line. */
cf7f2e2d 721 return;
5796c8dc
SS
722
723 if (*p == '#') /* comment line */
cf7f2e2d 724 return;
5796c8dc
SS
725
726 c = lookup_cmd (&p, cmdlist, "", -1, 1);
727 if (c == 0)
cf7f2e2d 728 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
5796c8dc
SS
729
730 if (cmd_cfunc_eq (c, collect_pseudocommand))
731 {
a45ae5f8
JM
732 trace_string_kludge = 0;
733 if (*p == '/')
734 p = decode_agent_options (p);
735
5796c8dc 736 do
c50c785c
JM
737 { /* Repeat over a comma-separated list. */
738 QUIT; /* Allow user to bail out with ^C. */
ef5ccd6c 739 p = skip_spaces (p);
5796c8dc 740
c50c785c 741 if (*p == '$') /* Look for special pseudo-symbols. */
5796c8dc 742 {
cf7f2e2d
JM
743 if (0 == strncasecmp ("reg", p + 1, 3)
744 || 0 == strncasecmp ("arg", p + 1, 3)
745 || 0 == strncasecmp ("loc", p + 1, 3)
a45ae5f8 746 || 0 == strncasecmp ("_ret", p + 1, 4)
cf7f2e2d 747 || 0 == strncasecmp ("_sdata", p + 1, 6))
5796c8dc
SS
748 {
749 p = strchr (p, ',');
750 continue;
751 }
752 /* else fall thru, treat p as an expression and parse it! */
753 }
cf7f2e2d 754 tmp_p = p;
a45ae5f8 755 for (loc = t->base.loc; loc; loc = loc->next)
5796c8dc 756 {
ef5ccd6c
JM
757 const char *q;
758
759 q = tmp_p;
760 exp = parse_exp_1 (&q, loc->address,
761 block_for_pc (loc->address), 1);
762 p = (char *) q;
cf7f2e2d
JM
763 old_chain = make_cleanup (free_current_contents, &exp);
764
765 if (exp->elts[0].opcode == OP_VAR_VALUE)
5796c8dc 766 {
cf7f2e2d
JM
767 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
768 {
ef5ccd6c 769 error (_("constant `%s' (value %s) "
c50c785c 770 "will not be collected."),
cf7f2e2d 771 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
ef5ccd6c 772 plongest (SYMBOL_VALUE (exp->elts[2].symbol)));
cf7f2e2d 773 }
c50c785c
JM
774 else if (SYMBOL_CLASS (exp->elts[2].symbol)
775 == LOC_OPTIMIZED_OUT)
cf7f2e2d 776 {
c50c785c
JM
777 error (_("`%s' is optimized away "
778 "and cannot be collected."),
cf7f2e2d
JM
779 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
780 }
5796c8dc 781 }
5796c8dc 782
cf7f2e2d
JM
783 /* We have something to collect, make sure that the expr to
784 bytecode translator can handle it and that it's not too
785 long. */
786 aexpr = gen_trace_for_expr (loc->address, exp);
787 make_cleanup_free_agent_expr (aexpr);
788
789 if (aexpr->len > MAX_AGENT_EXPR_LEN)
790 error (_("Expression is too complicated."));
791
792 ax_reqs (aexpr);
793
794 report_agent_reqs_errors (aexpr);
795
796 do_cleanups (old_chain);
797 }
798 }
799 while (p && *p++ == ',');
800 }
5796c8dc 801
cf7f2e2d
JM
802 else if (cmd_cfunc_eq (c, teval_pseudocommand))
803 {
804 do
c50c785c
JM
805 { /* Repeat over a comma-separated list. */
806 QUIT; /* Allow user to bail out with ^C. */
ef5ccd6c 807 p = skip_spaces (p);
5796c8dc 808
cf7f2e2d 809 tmp_p = p;
a45ae5f8 810 for (loc = t->base.loc; loc; loc = loc->next)
cf7f2e2d 811 {
ef5ccd6c
JM
812 const char *q;
813
814 q = tmp_p;
cf7f2e2d 815 /* Only expressions are allowed for this action. */
ef5ccd6c
JM
816 exp = parse_exp_1 (&q, loc->address,
817 block_for_pc (loc->address), 1);
818 p = (char *) q;
cf7f2e2d 819 old_chain = make_cleanup (free_current_contents, &exp);
5796c8dc 820
cf7f2e2d
JM
821 /* We have something to evaluate, make sure that the expr to
822 bytecode translator can handle it and that it's not too
823 long. */
824 aexpr = gen_eval_for_expr (loc->address, exp);
825 make_cleanup_free_agent_expr (aexpr);
5796c8dc 826
cf7f2e2d
JM
827 if (aexpr->len > MAX_AGENT_EXPR_LEN)
828 error (_("Expression is too complicated."));
5796c8dc 829
cf7f2e2d
JM
830 ax_reqs (aexpr);
831 report_agent_reqs_errors (aexpr);
5796c8dc 832
cf7f2e2d
JM
833 do_cleanups (old_chain);
834 }
5796c8dc
SS
835 }
836 while (p && *p++ == ',');
5796c8dc 837 }
cf7f2e2d 838
5796c8dc
SS
839 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
840 {
c50c785c 841 char *steparg; /* In case warning is necessary. */
5796c8dc 842
ef5ccd6c 843 p = skip_spaces (p);
5796c8dc
SS
844 steparg = p;
845
cf7f2e2d
JM
846 if (*p == '\0' || (t->step_count = strtol (p, &p, 0)) == 0)
847 error (_("while-stepping step count `%s' is malformed."), *line);
5796c8dc 848 }
5796c8dc 849
cf7f2e2d
JM
850 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
851 ;
5796c8dc 852
cf7f2e2d
JM
853 else
854 error (_("`%s' is not a supported tracepoint action."), *line);
5796c8dc
SS
855}
856
857enum {
858 memrange_absolute = -1
859};
860
861struct memrange
862{
863 int type; /* memrange_absolute for absolute memory range,
c50c785c 864 else basereg number. */
5796c8dc
SS
865 bfd_signed_vma start;
866 bfd_signed_vma end;
867};
868
869struct collection_list
870 {
871 unsigned char regs_mask[32]; /* room for up to 256 regs */
872 long listsize;
873 long next_memrange;
874 struct memrange *list;
875 long aexpr_listsize; /* size of array pointed to by expr_list elt */
876 long next_aexpr_elt;
877 struct agent_expr **aexpr_list;
878
cf7f2e2d
JM
879 /* True is the user requested a collection of "$_sdata", "static
880 tracepoint data". */
881 int strace_data;
5796c8dc
SS
882 }
883tracepoint_list, stepping_list;
884
885/* MEMRANGE functions: */
886
887static int memrange_cmp (const void *, const void *);
888
c50c785c 889/* Compare memranges for qsort. */
5796c8dc
SS
890static int
891memrange_cmp (const void *va, const void *vb)
892{
893 const struct memrange *a = va, *b = vb;
894
895 if (a->type < b->type)
896 return -1;
897 if (a->type > b->type)
898 return 1;
899 if (a->type == memrange_absolute)
900 {
901 if ((bfd_vma) a->start < (bfd_vma) b->start)
902 return -1;
903 if ((bfd_vma) a->start > (bfd_vma) b->start)
904 return 1;
905 }
906 else
907 {
908 if (a->start < b->start)
909 return -1;
910 if (a->start > b->start)
911 return 1;
912 }
913 return 0;
914}
915
916/* Sort the memrange list using qsort, and merge adjacent memranges. */
917static void
918memrange_sortmerge (struct collection_list *memranges)
919{
920 int a, b;
921
922 qsort (memranges->list, memranges->next_memrange,
923 sizeof (struct memrange), memrange_cmp);
924 if (memranges->next_memrange > 0)
925 {
926 for (a = 0, b = 1; b < memranges->next_memrange; b++)
927 {
c50c785c
JM
928 /* If memrange b overlaps or is adjacent to memrange a,
929 merge them. */
930 if (memranges->list[a].type == memranges->list[b].type
931 && memranges->list[b].start <= memranges->list[a].end)
5796c8dc 932 {
5796c8dc
SS
933 if (memranges->list[b].end > memranges->list[a].end)
934 memranges->list[a].end = memranges->list[b].end;
935 continue; /* next b, same a */
936 }
937 a++; /* next a */
938 if (a != b)
939 memcpy (&memranges->list[a], &memranges->list[b],
940 sizeof (struct memrange));
941 }
942 memranges->next_memrange = a + 1;
943 }
944}
945
946/* Add a register to a collection list. */
947static void
948add_register (struct collection_list *collection, unsigned int regno)
949{
950 if (info_verbose)
951 printf_filtered ("collect register %d\n", regno);
952 if (regno >= (8 * sizeof (collection->regs_mask)))
953 error (_("Internal: register number %d too large for tracepoint"),
954 regno);
955 collection->regs_mask[regno / 8] |= 1 << (regno % 8);
956}
957
c50c785c 958/* Add a memrange to a collection list. */
5796c8dc
SS
959static void
960add_memrange (struct collection_list *memranges,
961 int type, bfd_signed_vma base,
962 unsigned long len)
963{
964 if (info_verbose)
965 {
966 printf_filtered ("(%d,", type);
967 printf_vma (base);
968 printf_filtered (",%ld)\n", len);
969 }
970
971 /* type: memrange_absolute == memory, other n == basereg */
972 memranges->list[memranges->next_memrange].type = type;
973 /* base: addr if memory, offset if reg relative. */
974 memranges->list[memranges->next_memrange].start = base;
975 /* len: we actually save end (base + len) for convenience */
976 memranges->list[memranges->next_memrange].end = base + len;
977 memranges->next_memrange++;
978 if (memranges->next_memrange >= memranges->listsize)
979 {
980 memranges->listsize *= 2;
981 memranges->list = xrealloc (memranges->list,
982 memranges->listsize);
983 }
984
c50c785c 985 if (type != memrange_absolute) /* Better collect the base register! */
5796c8dc
SS
986 add_register (memranges, type);
987}
988
989/* Add a symbol to a collection list. */
990static void
991collect_symbol (struct collection_list *collect,
992 struct symbol *sym,
993 struct gdbarch *gdbarch,
cf7f2e2d
JM
994 long frame_regno, long frame_offset,
995 CORE_ADDR scope)
5796c8dc
SS
996{
997 unsigned long len;
998 unsigned int reg;
999 bfd_signed_vma offset;
cf7f2e2d 1000 int treat_as_expr = 0;
5796c8dc
SS
1001
1002 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
1003 switch (SYMBOL_CLASS (sym))
1004 {
1005 default:
1006 printf_filtered ("%s: don't know symbol class %d\n",
1007 SYMBOL_PRINT_NAME (sym),
1008 SYMBOL_CLASS (sym));
1009 break;
1010 case LOC_CONST:
ef5ccd6c
JM
1011 printf_filtered ("constant %s (value %s) will not be collected.\n",
1012 SYMBOL_PRINT_NAME (sym), plongest (SYMBOL_VALUE (sym)));
5796c8dc
SS
1013 break;
1014 case LOC_STATIC:
1015 offset = SYMBOL_VALUE_ADDRESS (sym);
1016 if (info_verbose)
1017 {
1018 char tmp[40];
1019
1020 sprintf_vma (tmp, offset);
1021 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
1022 SYMBOL_PRINT_NAME (sym), len,
1023 tmp /* address */);
1024 }
cf7f2e2d
JM
1025 /* A struct may be a C++ class with static fields, go to general
1026 expression handling. */
1027 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT)
1028 treat_as_expr = 1;
1029 else
1030 add_memrange (collect, memrange_absolute, offset, len);
5796c8dc
SS
1031 break;
1032 case LOC_REGISTER:
1033 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1034 if (info_verbose)
1035 printf_filtered ("LOC_REG[parm] %s: ",
1036 SYMBOL_PRINT_NAME (sym));
1037 add_register (collect, reg);
1038 /* Check for doubles stored in two registers. */
1039 /* FIXME: how about larger types stored in 3 or more regs? */
1040 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
1041 len > register_size (gdbarch, reg))
1042 add_register (collect, reg + 1);
1043 break;
1044 case LOC_REF_ARG:
1045 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
1046 printf_filtered (" (will not collect %s)\n",
1047 SYMBOL_PRINT_NAME (sym));
1048 break;
1049 case LOC_ARG:
1050 reg = frame_regno;
1051 offset = frame_offset + SYMBOL_VALUE (sym);
1052 if (info_verbose)
1053 {
1054 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
1055 SYMBOL_PRINT_NAME (sym), len);
1056 printf_vma (offset);
1057 printf_filtered (" from frame ptr reg %d\n", reg);
1058 }
1059 add_memrange (collect, reg, offset, len);
1060 break;
1061 case LOC_REGPARM_ADDR:
1062 reg = SYMBOL_VALUE (sym);
1063 offset = 0;
1064 if (info_verbose)
1065 {
1066 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset ",
1067 SYMBOL_PRINT_NAME (sym), len);
1068 printf_vma (offset);
1069 printf_filtered (" from reg %d\n", reg);
1070 }
1071 add_memrange (collect, reg, offset, len);
1072 break;
1073 case LOC_LOCAL:
1074 reg = frame_regno;
1075 offset = frame_offset + SYMBOL_VALUE (sym);
1076 if (info_verbose)
1077 {
1078 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
1079 SYMBOL_PRINT_NAME (sym), len);
1080 printf_vma (offset);
1081 printf_filtered (" from frame ptr reg %d\n", reg);
1082 }
1083 add_memrange (collect, reg, offset, len);
1084 break;
cf7f2e2d 1085
5796c8dc 1086 case LOC_UNRESOLVED:
cf7f2e2d 1087 treat_as_expr = 1;
5796c8dc 1088 break;
cf7f2e2d 1089
5796c8dc
SS
1090 case LOC_OPTIMIZED_OUT:
1091 printf_filtered ("%s has been optimized out of existence.\n",
1092 SYMBOL_PRINT_NAME (sym));
1093 break;
cf7f2e2d
JM
1094
1095 case LOC_COMPUTED:
1096 treat_as_expr = 1;
1097 break;
5796c8dc 1098 }
5796c8dc 1099
cf7f2e2d
JM
1100 /* Expressions are the most general case. */
1101 if (treat_as_expr)
5796c8dc 1102 {
cf7f2e2d
JM
1103 struct agent_expr *aexpr;
1104 struct cleanup *old_chain1 = NULL;
1105
1106 aexpr = gen_trace_for_var (scope, gdbarch, sym);
1107
1108 /* It can happen that the symbol is recorded as a computed
1109 location, but it's been optimized away and doesn't actually
1110 have a location expression. */
1111 if (!aexpr)
1112 {
1113 printf_filtered ("%s has been optimized out of existence.\n",
1114 SYMBOL_PRINT_NAME (sym));
1115 return;
1116 }
1117
1118 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1119
1120 ax_reqs (aexpr);
1121
1122 report_agent_reqs_errors (aexpr);
1123
1124 discard_cleanups (old_chain1);
1125 add_aexpr (collect, aexpr);
1126
c50c785c 1127 /* Take care of the registers. */
cf7f2e2d 1128 if (aexpr->reg_mask_len > 0)
5796c8dc 1129 {
cf7f2e2d
JM
1130 int ndx1, ndx2;
1131
1132 for (ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
5796c8dc 1133 {
c50c785c 1134 QUIT; /* Allow user to bail out with ^C. */
cf7f2e2d
JM
1135 if (aexpr->reg_mask[ndx1] != 0)
1136 {
c50c785c 1137 /* Assume chars have 8 bits. */
cf7f2e2d
JM
1138 for (ndx2 = 0; ndx2 < 8; ndx2++)
1139 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
c50c785c 1140 /* It's used -- record it. */
cf7f2e2d
JM
1141 add_register (collect, ndx1 * 8 + ndx2);
1142 }
5796c8dc
SS
1143 }
1144 }
5796c8dc 1145 }
cf7f2e2d
JM
1146}
1147
1148/* Data to be passed around in the calls to the locals and args
1149 iterators. */
1150
1151struct add_local_symbols_data
1152{
1153 struct collection_list *collect;
1154 struct gdbarch *gdbarch;
1155 CORE_ADDR pc;
1156 long frame_regno;
1157 long frame_offset;
1158 int count;
1159};
1160
c50c785c 1161/* The callback for the locals and args iterators. */
cf7f2e2d
JM
1162
1163static void
1164do_collect_symbol (const char *print_name,
1165 struct symbol *sym,
1166 void *cb_data)
1167{
1168 struct add_local_symbols_data *p = cb_data;
1169
1170 collect_symbol (p->collect, sym, p->gdbarch, p->frame_regno,
1171 p->frame_offset, p->pc);
1172 p->count++;
1173}
1174
c50c785c 1175/* Add all locals (or args) symbols to collection list. */
cf7f2e2d
JM
1176static void
1177add_local_symbols (struct collection_list *collect,
1178 struct gdbarch *gdbarch, CORE_ADDR pc,
1179 long frame_regno, long frame_offset, int type)
1180{
1181 struct block *block;
1182 struct add_local_symbols_data cb_data;
1183
1184 cb_data.collect = collect;
1185 cb_data.gdbarch = gdbarch;
1186 cb_data.pc = pc;
1187 cb_data.frame_regno = frame_regno;
1188 cb_data.frame_offset = frame_offset;
1189 cb_data.count = 0;
1190
1191 if (type == 'L')
1192 {
1193 block = block_for_pc (pc);
1194 if (block == NULL)
1195 {
1196 warning (_("Can't collect locals; "
1197 "no symbol table info available.\n"));
1198 return;
1199 }
1200
1201 iterate_over_block_local_vars (block, do_collect_symbol, &cb_data);
1202 if (cb_data.count == 0)
1203 warning (_("No locals found in scope."));
1204 }
1205 else
1206 {
1207 pc = get_pc_function_start (pc);
1208 block = block_for_pc (pc);
1209 if (block == NULL)
1210 {
c50c785c 1211 warning (_("Can't collect args; no symbol table info available."));
cf7f2e2d
JM
1212 return;
1213 }
1214
1215 iterate_over_block_arg_vars (block, do_collect_symbol, &cb_data);
1216 if (cb_data.count == 0)
1217 warning (_("No args found in scope."));
1218 }
1219}
1220
1221static void
1222add_static_trace_data (struct collection_list *collection)
1223{
1224 if (info_verbose)
1225 printf_filtered ("collect static trace data\n");
1226 collection->strace_data = 1;
5796c8dc
SS
1227}
1228
1229/* worker function */
1230static void
1231clear_collection_list (struct collection_list *list)
1232{
1233 int ndx;
1234
1235 list->next_memrange = 0;
1236 for (ndx = 0; ndx < list->next_aexpr_elt; ndx++)
1237 {
1238 free_agent_expr (list->aexpr_list[ndx]);
1239 list->aexpr_list[ndx] = NULL;
1240 }
1241 list->next_aexpr_elt = 0;
1242 memset (list->regs_mask, 0, sizeof (list->regs_mask));
cf7f2e2d 1243 list->strace_data = 0;
5796c8dc
SS
1244}
1245
c50c785c 1246/* Reduce a collection list to string form (for gdb protocol). */
5796c8dc
SS
1247static char **
1248stringify_collection_list (struct collection_list *list, char *string)
1249{
1250 char temp_buf[2048];
1251 char tmp2[40];
1252 int count;
1253 int ndx = 0;
1254 char *(*str_list)[];
1255 char *end;
1256 long i;
1257
cf7f2e2d 1258 count = 1 + 1 + list->next_memrange + list->next_aexpr_elt + 1;
5796c8dc
SS
1259 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
1260
cf7f2e2d
JM
1261 if (list->strace_data)
1262 {
1263 if (info_verbose)
1264 printf_filtered ("\nCollecting static trace data\n");
1265 end = temp_buf;
1266 *end++ = 'L';
1267 (*str_list)[ndx] = savestring (temp_buf, end - temp_buf);
1268 ndx++;
1269 }
1270
5796c8dc 1271 for (i = sizeof (list->regs_mask) - 1; i > 0; i--)
c50c785c 1272 if (list->regs_mask[i] != 0) /* Skip leading zeroes in regs_mask. */
5796c8dc 1273 break;
c50c785c 1274 if (list->regs_mask[i] != 0) /* Prepare to send regs_mask to the stub. */
5796c8dc
SS
1275 {
1276 if (info_verbose)
1277 printf_filtered ("\nCollecting registers (mask): 0x");
1278 end = temp_buf;
1279 *end++ = 'R';
1280 for (; i >= 0; i--)
1281 {
c50c785c 1282 QUIT; /* Allow user to bail out with ^C. */
5796c8dc
SS
1283 if (info_verbose)
1284 printf_filtered ("%02X", list->regs_mask[i]);
1285 sprintf (end, "%02X", list->regs_mask[i]);
1286 end += 2;
1287 }
1288 (*str_list)[ndx] = xstrdup (temp_buf);
1289 ndx++;
1290 }
1291 if (info_verbose)
1292 printf_filtered ("\n");
1293 if (list->next_memrange > 0 && info_verbose)
1294 printf_filtered ("Collecting memranges: \n");
1295 for (i = 0, count = 0, end = temp_buf; i < list->next_memrange; i++)
1296 {
c50c785c 1297 QUIT; /* Allow user to bail out with ^C. */
5796c8dc
SS
1298 sprintf_vma (tmp2, list->list[i].start);
1299 if (info_verbose)
1300 {
1301 printf_filtered ("(%d, %s, %ld)\n",
1302 list->list[i].type,
1303 tmp2,
1304 (long) (list->list[i].end - list->list[i].start));
1305 }
1306 if (count + 27 > MAX_AGENT_EXPR_LEN)
1307 {
1308 (*str_list)[ndx] = savestring (temp_buf, count);
1309 ndx++;
1310 count = 0;
1311 end = temp_buf;
1312 }
1313
1314 {
1315 bfd_signed_vma length = list->list[i].end - list->list[i].start;
1316
1317 /* The "%X" conversion specifier expects an unsigned argument,
1318 so passing -1 (memrange_absolute) to it directly gives you
1319 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1320 Special-case it. */
1321 if (list->list[i].type == memrange_absolute)
1322 sprintf (end, "M-1,%s,%lX", tmp2, (long) length);
1323 else
1324 sprintf (end, "M%X,%s,%lX", list->list[i].type, tmp2, (long) length);
1325 }
1326
1327 count += strlen (end);
1328 end = temp_buf + count;
1329 }
1330
1331 for (i = 0; i < list->next_aexpr_elt; i++)
1332 {
c50c785c 1333 QUIT; /* Allow user to bail out with ^C. */
5796c8dc
SS
1334 if ((count + 10 + 2 * list->aexpr_list[i]->len) > MAX_AGENT_EXPR_LEN)
1335 {
1336 (*str_list)[ndx] = savestring (temp_buf, count);
1337 ndx++;
1338 count = 0;
1339 end = temp_buf;
1340 }
1341 sprintf (end, "X%08X,", list->aexpr_list[i]->len);
1342 end += 10; /* 'X' + 8 hex digits + ',' */
1343 count += 10;
1344
1345 end = mem2hex (list->aexpr_list[i]->buf,
1346 end, list->aexpr_list[i]->len);
1347 count += 2 * list->aexpr_list[i]->len;
1348 }
1349
1350 if (count != 0)
1351 {
1352 (*str_list)[ndx] = savestring (temp_buf, count);
1353 ndx++;
1354 count = 0;
1355 end = temp_buf;
1356 }
1357 (*str_list)[ndx] = NULL;
1358
1359 if (ndx == 0)
1360 {
1361 xfree (str_list);
1362 return NULL;
1363 }
1364 else
1365 return *str_list;
1366}
1367
5796c8dc 1368
5796c8dc 1369static void
cf7f2e2d
JM
1370encode_actions_1 (struct command_line *action,
1371 struct breakpoint *t,
1372 struct bp_location *tloc,
1373 int frame_reg,
1374 LONGEST frame_offset,
1375 struct collection_list *collect,
1376 struct collection_list *stepping_list)
5796c8dc 1377{
5796c8dc
SS
1378 char *action_exp;
1379 struct expression *exp = NULL;
5796c8dc
SS
1380 int i;
1381 struct value *tempval;
5796c8dc
SS
1382 struct cmd_list_element *cmd;
1383 struct agent_expr *aexpr;
5796c8dc 1384
cf7f2e2d 1385 for (; action; action = action->next)
5796c8dc 1386 {
c50c785c 1387 QUIT; /* Allow user to bail out with ^C. */
cf7f2e2d 1388 action_exp = action->line;
ef5ccd6c 1389 action_exp = skip_spaces (action_exp);
5796c8dc 1390
5796c8dc
SS
1391 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1392 if (cmd == 0)
1393 error (_("Bad action list item: %s"), action_exp);
1394
1395 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
1396 {
a45ae5f8
JM
1397 trace_string_kludge = 0;
1398 if (*action_exp == '/')
1399 action_exp = decode_agent_options (action_exp);
1400
5796c8dc 1401 do
c50c785c
JM
1402 { /* Repeat over a comma-separated list. */
1403 QUIT; /* Allow user to bail out with ^C. */
ef5ccd6c 1404 action_exp = skip_spaces (action_exp);
5796c8dc
SS
1405
1406 if (0 == strncasecmp ("$reg", action_exp, 4))
1407 {
a45ae5f8 1408 for (i = 0; i < gdbarch_num_regs (tloc->gdbarch); i++)
5796c8dc
SS
1409 add_register (collect, i);
1410 action_exp = strchr (action_exp, ','); /* more? */
1411 }
1412 else if (0 == strncasecmp ("$arg", action_exp, 4))
1413 {
1414 add_local_symbols (collect,
a45ae5f8 1415 tloc->gdbarch,
cf7f2e2d 1416 tloc->address,
5796c8dc
SS
1417 frame_reg,
1418 frame_offset,
1419 'A');
1420 action_exp = strchr (action_exp, ','); /* more? */
1421 }
1422 else if (0 == strncasecmp ("$loc", action_exp, 4))
1423 {
1424 add_local_symbols (collect,
a45ae5f8 1425 tloc->gdbarch,
cf7f2e2d 1426 tloc->address,
5796c8dc
SS
1427 frame_reg,
1428 frame_offset,
1429 'L');
1430 action_exp = strchr (action_exp, ','); /* more? */
1431 }
a45ae5f8
JM
1432 else if (0 == strncasecmp ("$_ret", action_exp, 5))
1433 {
1434 struct cleanup *old_chain1 = NULL;
1435
1436 aexpr = gen_trace_for_return_address (tloc->address,
1437 tloc->gdbarch);
1438
1439 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1440
1441 ax_reqs (aexpr);
1442 report_agent_reqs_errors (aexpr);
1443
1444 discard_cleanups (old_chain1);
1445 add_aexpr (collect, aexpr);
1446
1447 /* take care of the registers */
1448 if (aexpr->reg_mask_len > 0)
1449 {
1450 int ndx1, ndx2;
1451
1452 for (ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
1453 {
1454 QUIT; /* allow user to bail out with ^C */
1455 if (aexpr->reg_mask[ndx1] != 0)
1456 {
1457 /* assume chars have 8 bits */
1458 for (ndx2 = 0; ndx2 < 8; ndx2++)
1459 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
1460 /* it's used -- record it */
1461 add_register (collect,
1462 ndx1 * 8 + ndx2);
1463 }
1464 }
1465 }
1466
1467 action_exp = strchr (action_exp, ','); /* more? */
1468 }
cf7f2e2d
JM
1469 else if (0 == strncasecmp ("$_sdata", action_exp, 7))
1470 {
1471 add_static_trace_data (collect);
1472 action_exp = strchr (action_exp, ','); /* more? */
1473 }
5796c8dc
SS
1474 else
1475 {
ef5ccd6c 1476 unsigned long addr;
5796c8dc
SS
1477 struct cleanup *old_chain = NULL;
1478 struct cleanup *old_chain1 = NULL;
ef5ccd6c 1479 const char *q;
5796c8dc 1480
ef5ccd6c
JM
1481 q = action_exp;
1482 exp = parse_exp_1 (&q, tloc->address,
cf7f2e2d 1483 block_for_pc (tloc->address), 1);
ef5ccd6c 1484 action_exp = (char *) q;
5796c8dc
SS
1485 old_chain = make_cleanup (free_current_contents, &exp);
1486
1487 switch (exp->elts[0].opcode)
1488 {
1489 case OP_REGISTER:
1490 {
1491 const char *name = &exp->elts[2].string;
1492
a45ae5f8 1493 i = user_reg_map_name_to_regnum (tloc->gdbarch,
5796c8dc
SS
1494 name, strlen (name));
1495 if (i == -1)
1496 internal_error (__FILE__, __LINE__,
1497 _("Register $%s not available"),
1498 name);
1499 if (info_verbose)
1500 printf_filtered ("OP_REGISTER: ");
1501 add_register (collect, i);
1502 break;
1503 }
1504
1505 case UNOP_MEMVAL:
c50c785c 1506 /* Safe because we know it's a simple expression. */
5796c8dc
SS
1507 tempval = evaluate_expression (exp);
1508 addr = value_address (tempval);
ef5ccd6c
JM
1509 /* Initialize the TYPE_LENGTH if it is a typedef. */
1510 check_typedef (exp->elts[1].type);
1511 add_memrange (collect, memrange_absolute, addr,
1512 TYPE_LENGTH (exp->elts[1].type));
5796c8dc
SS
1513 break;
1514
1515 case OP_VAR_VALUE:
1516 collect_symbol (collect,
1517 exp->elts[2].symbol,
a45ae5f8 1518 tloc->gdbarch,
5796c8dc 1519 frame_reg,
cf7f2e2d
JM
1520 frame_offset,
1521 tloc->address);
5796c8dc
SS
1522 break;
1523
c50c785c 1524 default: /* Full-fledged expression. */
cf7f2e2d 1525 aexpr = gen_trace_for_expr (tloc->address, exp);
5796c8dc
SS
1526
1527 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1528
cf7f2e2d 1529 ax_reqs (aexpr);
5796c8dc 1530
cf7f2e2d 1531 report_agent_reqs_errors (aexpr);
5796c8dc
SS
1532
1533 discard_cleanups (old_chain1);
1534 add_aexpr (collect, aexpr);
1535
c50c785c 1536 /* Take care of the registers. */
cf7f2e2d 1537 if (aexpr->reg_mask_len > 0)
5796c8dc
SS
1538 {
1539 int ndx1;
1540 int ndx2;
1541
cf7f2e2d 1542 for (ndx1 = 0; ndx1 < aexpr->reg_mask_len; ndx1++)
5796c8dc 1543 {
c50c785c 1544 QUIT; /* Allow user to bail out with ^C. */
cf7f2e2d 1545 if (aexpr->reg_mask[ndx1] != 0)
5796c8dc 1546 {
c50c785c 1547 /* Assume chars have 8 bits. */
5796c8dc 1548 for (ndx2 = 0; ndx2 < 8; ndx2++)
cf7f2e2d 1549 if (aexpr->reg_mask[ndx1] & (1 << ndx2))
c50c785c 1550 /* It's used -- record it. */
5796c8dc
SS
1551 add_register (collect,
1552 ndx1 * 8 + ndx2);
1553 }
1554 }
1555 }
1556 break;
1557 } /* switch */
1558 do_cleanups (old_chain);
1559 } /* do */
1560 }
1561 while (action_exp && *action_exp++ == ',');
1562 } /* if */
cf7f2e2d 1563 else if (cmd_cfunc_eq (cmd, teval_pseudocommand))
5796c8dc 1564 {
cf7f2e2d 1565 do
c50c785c
JM
1566 { /* Repeat over a comma-separated list. */
1567 QUIT; /* Allow user to bail out with ^C. */
ef5ccd6c 1568 action_exp = skip_spaces (action_exp);
cf7f2e2d
JM
1569
1570 {
1571 struct cleanup *old_chain = NULL;
1572 struct cleanup *old_chain1 = NULL;
ef5ccd6c 1573 const char *q;
cf7f2e2d 1574
ef5ccd6c
JM
1575 q = action_exp;
1576 exp = parse_exp_1 (&q, tloc->address,
cf7f2e2d 1577 block_for_pc (tloc->address), 1);
ef5ccd6c 1578 action_exp = (char *) q;
cf7f2e2d
JM
1579 old_chain = make_cleanup (free_current_contents, &exp);
1580
1581 aexpr = gen_eval_for_expr (tloc->address, exp);
1582 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1583
1584 ax_reqs (aexpr);
1585 report_agent_reqs_errors (aexpr);
1586
1587 discard_cleanups (old_chain1);
1588 /* Even though we're not officially collecting, add
1589 to the collect list anyway. */
1590 add_aexpr (collect, aexpr);
1591
1592 do_cleanups (old_chain);
1593 } /* do */
1594 }
1595 while (action_exp && *action_exp++ == ',');
1596 } /* if */
1597 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
5796c8dc 1598 {
cf7f2e2d
JM
1599 /* We check against nested while-stepping when setting
1600 breakpoint action, so no way to run into nested
1601 here. */
1602 gdb_assert (stepping_list);
1603
1604 encode_actions_1 (action->body_list[0], t, tloc, frame_reg,
1605 frame_offset, stepping_list, NULL);
5796c8dc 1606 }
cf7f2e2d
JM
1607 else
1608 error (_("Invalid tracepoint command '%s'"), action->line);
5796c8dc 1609 } /* for */
cf7f2e2d
JM
1610}
1611
1612/* Render all actions into gdb protocol. */
ef5ccd6c
JM
1613
1614void
cf7f2e2d
JM
1615encode_actions (struct breakpoint *t, struct bp_location *tloc,
1616 char ***tdp_actions, char ***stepping_actions)
1617{
1618 static char tdp_buff[2048], step_buff[2048];
1619 char *default_collect_line = NULL;
1620 struct command_line *actions;
1621 struct command_line *default_collect_action = NULL;
1622 int frame_reg;
1623 LONGEST frame_offset;
1624 struct cleanup *back_to;
1625
1626 back_to = make_cleanup (null_cleanup, NULL);
1627
1628 clear_collection_list (&tracepoint_list);
1629 clear_collection_list (&stepping_list);
1630
1631 *tdp_actions = NULL;
1632 *stepping_actions = NULL;
1633
a45ae5f8
JM
1634 gdbarch_virtual_frame_pointer (tloc->gdbarch,
1635 tloc->address, &frame_reg, &frame_offset);
cf7f2e2d
JM
1636
1637 actions = breakpoint_commands (t);
1638
1639 /* If there are default expressions to collect, make up a collect
1640 action and prepend to the action list to encode. Note that since
1641 validation is per-tracepoint (local var "xyz" might be valid for
1642 one tracepoint and not another, etc), we make up the action on
1643 the fly, and don't cache it. */
1644 if (*default_collect)
1645 {
1646 char *line;
1647
1648 default_collect_line = xstrprintf ("collect %s", default_collect);
1649 make_cleanup (xfree, default_collect_line);
1650
1651 line = default_collect_line;
1652 validate_actionline (&line, t);
1653
1654 default_collect_action = xmalloc (sizeof (struct command_line));
1655 make_cleanup (xfree, default_collect_action);
1656 default_collect_action->next = actions;
1657 default_collect_action->line = line;
1658 actions = default_collect_action;
1659 }
1660 encode_actions_1 (actions, t, tloc, frame_reg, frame_offset,
1661 &tracepoint_list, &stepping_list);
1662
5796c8dc
SS
1663 memrange_sortmerge (&tracepoint_list);
1664 memrange_sortmerge (&stepping_list);
1665
cf7f2e2d 1666 *tdp_actions = stringify_collection_list (&tracepoint_list,
5796c8dc 1667 tdp_buff);
cf7f2e2d 1668 *stepping_actions = stringify_collection_list (&stepping_list,
5796c8dc 1669 step_buff);
cf7f2e2d
JM
1670
1671 do_cleanups (back_to);
5796c8dc
SS
1672}
1673
1674static void
1675add_aexpr (struct collection_list *collect, struct agent_expr *aexpr)
1676{
1677 if (collect->next_aexpr_elt >= collect->aexpr_listsize)
1678 {
1679 collect->aexpr_list =
1680 xrealloc (collect->aexpr_list,
cf7f2e2d 1681 2 * collect->aexpr_listsize * sizeof (struct agent_expr *));
5796c8dc
SS
1682 collect->aexpr_listsize *= 2;
1683 }
1684 collect->aexpr_list[collect->next_aexpr_elt] = aexpr;
1685 collect->next_aexpr_elt++;
1686}
1687
a45ae5f8
JM
1688static void
1689process_tracepoint_on_disconnect (void)
1690{
1691 VEC(breakpoint_p) *tp_vec = NULL;
1692 int ix;
1693 struct breakpoint *b;
1694 int has_pending_p = 0;
1695
1696 /* Check whether we still have pending tracepoint. If we have, warn the
1697 user that pending tracepoint will no longer work. */
1698 tp_vec = all_tracepoints ();
1699 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
1700 {
1701 if (b->loc == NULL)
1702 {
1703 has_pending_p = 1;
1704 break;
1705 }
1706 else
1707 {
1708 struct bp_location *loc1;
1709
1710 for (loc1 = b->loc; loc1; loc1 = loc1->next)
1711 {
1712 if (loc1->shlib_disabled)
1713 {
1714 has_pending_p = 1;
1715 break;
1716 }
1717 }
1718
1719 if (has_pending_p)
1720 break;
1721 }
1722 }
1723 VEC_free (breakpoint_p, tp_vec);
1724
1725 if (has_pending_p)
1726 warning (_("Pending tracepoints will not be resolved while"
1727 " GDB is disconnected\n"));
1728}
1729
5796c8dc 1730
cf7f2e2d 1731void
a45ae5f8 1732start_tracing (char *notes)
cf7f2e2d
JM
1733{
1734 VEC(breakpoint_p) *tp_vec = NULL;
1735 int ix;
a45ae5f8 1736 struct breakpoint *b;
cf7f2e2d
JM
1737 struct trace_state_variable *tsv;
1738 int any_enabled = 0, num_to_download = 0;
a45ae5f8
JM
1739 int ret;
1740
cf7f2e2d
JM
1741 tp_vec = all_tracepoints ();
1742
c50c785c 1743 /* No point in tracing without any tracepoints... */
cf7f2e2d
JM
1744 if (VEC_length (breakpoint_p, tp_vec) == 0)
1745 {
1746 VEC_free (breakpoint_p, tp_vec);
1747 error (_("No tracepoints defined, not starting trace"));
1748 }
5796c8dc 1749
a45ae5f8 1750 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
cf7f2e2d 1751 {
a45ae5f8 1752 struct tracepoint *t = (struct tracepoint *) b;
ef5ccd6c 1753 struct bp_location *loc;
a45ae5f8
JM
1754
1755 if (b->enable_state == bp_enabled)
cf7f2e2d 1756 any_enabled = 1;
5796c8dc 1757
a45ae5f8 1758 if ((b->type == bp_fast_tracepoint
cf7f2e2d
JM
1759 ? may_insert_fast_tracepoints
1760 : may_insert_tracepoints))
1761 ++num_to_download;
1762 else
1763 warning (_("May not insert %stracepoints, skipping tracepoint %d"),
a45ae5f8 1764 (b->type == bp_fast_tracepoint ? "fast " : ""), b->number);
cf7f2e2d 1765 }
5796c8dc 1766
cf7f2e2d
JM
1767 if (!any_enabled)
1768 {
a45ae5f8
JM
1769 if (target_supports_enable_disable_tracepoint ())
1770 warning (_("No tracepoints enabled"));
1771 else
1772 {
1773 /* No point in tracing with only disabled tracepoints that
1774 cannot be re-enabled. */
1775 VEC_free (breakpoint_p, tp_vec);
1776 error (_("No tracepoints enabled, not starting trace"));
1777 }
cf7f2e2d 1778 }
5796c8dc 1779
cf7f2e2d 1780 if (num_to_download <= 0)
5796c8dc 1781 {
cf7f2e2d
JM
1782 VEC_free (breakpoint_p, tp_vec);
1783 error (_("No tracepoints that may be downloaded, not starting trace"));
1784 }
1785
1786 target_trace_init ();
5796c8dc 1787
a45ae5f8 1788 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, b); ix++)
cf7f2e2d 1789 {
a45ae5f8
JM
1790 struct tracepoint *t = (struct tracepoint *) b;
1791 struct bp_location *loc;
ef5ccd6c 1792 int bp_location_downloaded = 0;
a45ae5f8
JM
1793
1794 /* Clear `inserted' flag. */
1795 for (loc = b->loc; loc; loc = loc->next)
1796 loc->inserted = 0;
1797
1798 if ((b->type == bp_fast_tracepoint
cf7f2e2d
JM
1799 ? !may_insert_fast_tracepoints
1800 : !may_insert_tracepoints))
5796c8dc
SS
1801 continue;
1802
cf7f2e2d 1803 t->number_on_target = 0;
a45ae5f8
JM
1804
1805 for (loc = b->loc; loc; loc = loc->next)
1806 {
1807 /* Since tracepoint locations are never duplicated, `inserted'
1808 flag should be zero. */
1809 gdb_assert (!loc->inserted);
1810
1811 target_download_tracepoint (loc);
1812
1813 loc->inserted = 1;
ef5ccd6c 1814 bp_location_downloaded = 1;
a45ae5f8
JM
1815 }
1816
1817 t->number_on_target = b->number;
ef5ccd6c
JM
1818
1819 for (loc = b->loc; loc; loc = loc->next)
1820 if (loc->probe != NULL)
1821 loc->probe->pops->set_semaphore (loc->probe, loc->gdbarch);
1822
1823 if (bp_location_downloaded)
1824 observer_notify_breakpoint_modified (b);
5796c8dc 1825 }
cf7f2e2d
JM
1826 VEC_free (breakpoint_p, tp_vec);
1827
1828 /* Send down all the trace state variables too. */
1829 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
5796c8dc 1830 {
cf7f2e2d 1831 target_download_trace_state_variable (tsv);
5796c8dc 1832 }
cf7f2e2d
JM
1833
1834 /* Tell target to treat text-like sections as transparent. */
1835 target_trace_set_readonly_regions ();
1836 /* Set some mode flags. */
1837 target_set_disconnected_tracing (disconnected_tracing);
1838 target_set_circular_trace_buffer (circular_trace_buffer);
ef5ccd6c 1839 target_set_trace_buffer_size (trace_buffer_size);
cf7f2e2d 1840
a45ae5f8
JM
1841 if (!notes)
1842 notes = trace_notes;
1843 ret = target_set_trace_notes (trace_user, notes, NULL);
1844
1845 if (!ret && (trace_user || notes))
ef5ccd6c 1846 warning (_("Target does not support trace user/notes, info ignored"));
a45ae5f8 1847
cf7f2e2d
JM
1848 /* Now insert traps and begin collecting data. */
1849 target_trace_start ();
1850
1851 /* Reset our local state. */
1852 set_traceframe_num (-1);
1853 set_tracepoint_num (-1);
1854 set_traceframe_context (NULL);
1855 current_trace_status()->running = 1;
c50c785c 1856 clear_traceframe_info ();
5796c8dc
SS
1857}
1858
a45ae5f8
JM
1859/* The tstart command requests the target to start a new trace run.
1860 The command passes any arguments it has to the target verbatim, as
1861 an optional "trace note". This is useful as for instance a warning
1862 to other users if the trace runs disconnected, and you don't want
1863 anybody else messing with the target. */
5796c8dc 1864
5796c8dc
SS
1865static void
1866trace_start_command (char *args, int from_tty)
1867{
5796c8dc
SS
1868 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1869
cf7f2e2d 1870 if (current_trace_status ()->running)
5796c8dc 1871 {
cf7f2e2d
JM
1872 if (from_tty
1873 && !query (_("A trace is running already. Start a new run? ")))
1874 error (_("New trace run not started."));
1875 }
5796c8dc 1876
a45ae5f8 1877 start_tracing (args);
cf7f2e2d 1878}
5796c8dc 1879
a45ae5f8
JM
1880/* The tstop command stops the tracing run. The command passes any
1881 supplied arguments to the target verbatim as a "stop note"; if the
1882 target supports trace notes, then it will be reported back as part
1883 of the trace run's status. */
1884
cf7f2e2d
JM
1885static void
1886trace_stop_command (char *args, int from_tty)
1887{
1888 if (!current_trace_status ()->running)
1889 error (_("Trace is not running."));
5796c8dc 1890
a45ae5f8 1891 stop_tracing (args);
5796c8dc
SS
1892}
1893
5796c8dc 1894void
a45ae5f8 1895stop_tracing (char *note)
5796c8dc 1896{
a45ae5f8 1897 int ret;
ef5ccd6c
JM
1898 VEC(breakpoint_p) *tp_vec = NULL;
1899 int ix;
1900 struct breakpoint *t;
a45ae5f8 1901
cf7f2e2d 1902 target_trace_stop ();
a45ae5f8 1903
ef5ccd6c
JM
1904 tp_vec = all_tracepoints ();
1905 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1906 {
1907 struct bp_location *loc;
1908
1909 if ((t->type == bp_fast_tracepoint
1910 ? !may_insert_fast_tracepoints
1911 : !may_insert_tracepoints))
1912 continue;
1913
1914 for (loc = t->loc; loc; loc = loc->next)
1915 {
1916 /* GDB can be totally absent in some disconnected trace scenarios,
1917 but we don't really care if this semaphore goes out of sync.
1918 That's why we are decrementing it here, but not taking care
1919 in other places. */
1920 if (loc->probe != NULL)
1921 loc->probe->pops->clear_semaphore (loc->probe, loc->gdbarch);
1922 }
1923 }
1924
1925 VEC_free (breakpoint_p, tp_vec);
1926
a45ae5f8
JM
1927 if (!note)
1928 note = trace_stop_notes;
1929 ret = target_set_trace_notes (NULL, NULL, note);
1930
1931 if (!ret && note)
ef5ccd6c 1932 warning (_("Target does not support trace notes, note ignored"));
a45ae5f8 1933
c50c785c 1934 /* Should change in response to reply? */
cf7f2e2d
JM
1935 current_trace_status ()->running = 0;
1936}
5796c8dc 1937
cf7f2e2d
JM
1938/* tstatus command */
1939static void
1940trace_status_command (char *args, int from_tty)
1941{
1942 struct trace_status *ts = current_trace_status ();
a45ae5f8
JM
1943 int status, ix;
1944 VEC(breakpoint_p) *tp_vec = NULL;
1945 struct breakpoint *t;
cf7f2e2d
JM
1946
1947 status = target_get_trace_status (ts);
5796c8dc 1948
cf7f2e2d 1949 if (status == -1)
5796c8dc 1950 {
ef5ccd6c 1951 if (ts->filename != NULL)
cf7f2e2d
JM
1952 printf_filtered (_("Using a trace file.\n"));
1953 else
5796c8dc 1954 {
cf7f2e2d
JM
1955 printf_filtered (_("Trace can not be run on this target.\n"));
1956 return;
5796c8dc
SS
1957 }
1958 }
cf7f2e2d
JM
1959
1960 if (!ts->running_known)
1961 {
1962 printf_filtered (_("Run/stop status is unknown.\n"));
1963 }
1964 else if (ts->running)
1965 {
1966 printf_filtered (_("Trace is running on the target.\n"));
1967 }
1968 else
5796c8dc 1969 {
cf7f2e2d 1970 switch (ts->stop_reason)
5796c8dc 1971 {
cf7f2e2d
JM
1972 case trace_never_run:
1973 printf_filtered (_("No trace has been run on the target.\n"));
1974 break;
1975 case tstop_command:
a45ae5f8
JM
1976 if (ts->stop_desc)
1977 printf_filtered (_("Trace stopped by a tstop command (%s).\n"),
1978 ts->stop_desc);
1979 else
1980 printf_filtered (_("Trace stopped by a tstop command.\n"));
cf7f2e2d
JM
1981 break;
1982 case trace_buffer_full:
1983 printf_filtered (_("Trace stopped because the buffer was full.\n"));
1984 break;
1985 case trace_disconnected:
1986 printf_filtered (_("Trace stopped because of disconnection.\n"));
1987 break;
1988 case tracepoint_passcount:
1989 printf_filtered (_("Trace stopped by tracepoint %d.\n"),
1990 ts->stopping_tracepoint);
1991 break;
1992 case tracepoint_error:
1993 if (ts->stopping_tracepoint)
c50c785c
JM
1994 printf_filtered (_("Trace stopped by an "
1995 "error (%s, tracepoint %d).\n"),
a45ae5f8 1996 ts->stop_desc, ts->stopping_tracepoint);
cf7f2e2d
JM
1997 else
1998 printf_filtered (_("Trace stopped by an error (%s).\n"),
a45ae5f8 1999 ts->stop_desc);
cf7f2e2d
JM
2000 break;
2001 case trace_stop_reason_unknown:
2002 printf_filtered (_("Trace stopped for an unknown reason.\n"));
2003 break;
2004 default:
2005 printf_filtered (_("Trace stopped for some other reason (%d).\n"),
2006 ts->stop_reason);
2007 break;
5796c8dc
SS
2008 }
2009 }
5796c8dc 2010
cf7f2e2d
JM
2011 if (ts->traceframes_created >= 0
2012 && ts->traceframe_count != ts->traceframes_created)
5796c8dc 2013 {
c50c785c
JM
2014 printf_filtered (_("Buffer contains %d trace "
2015 "frames (of %d created total).\n"),
cf7f2e2d
JM
2016 ts->traceframe_count, ts->traceframes_created);
2017 }
2018 else if (ts->traceframe_count >= 0)
2019 {
2020 printf_filtered (_("Collected %d trace frames.\n"),
2021 ts->traceframe_count);
5796c8dc 2022 }
5796c8dc 2023
cf7f2e2d 2024 if (ts->buffer_free >= 0)
5796c8dc 2025 {
cf7f2e2d
JM
2026 if (ts->buffer_size >= 0)
2027 {
2028 printf_filtered (_("Trace buffer has %d bytes of %d bytes free"),
2029 ts->buffer_free, ts->buffer_size);
2030 if (ts->buffer_size > 0)
2031 printf_filtered (_(" (%d%% full)"),
2032 ((int) ((((long long) (ts->buffer_size
2033 - ts->buffer_free)) * 100)
2034 / ts->buffer_size)));
2035 printf_filtered (_(".\n"));
2036 }
2037 else
2038 printf_filtered (_("Trace buffer has %d bytes free.\n"),
2039 ts->buffer_free);
2040 }
5796c8dc 2041
cf7f2e2d
JM
2042 if (ts->disconnected_tracing)
2043 printf_filtered (_("Trace will continue if GDB disconnects.\n"));
2044 else
2045 printf_filtered (_("Trace will stop if GDB disconnects.\n"));
5796c8dc 2046
cf7f2e2d
JM
2047 if (ts->circular_buffer)
2048 printf_filtered (_("Trace buffer is circular.\n"));
2049
a45ae5f8
JM
2050 if (ts->user_name && strlen (ts->user_name) > 0)
2051 printf_filtered (_("Trace user is %s.\n"), ts->user_name);
2052
2053 if (ts->notes && strlen (ts->notes) > 0)
2054 printf_filtered (_("Trace notes: %s.\n"), ts->notes);
2055
cf7f2e2d
JM
2056 /* Now report on what we're doing with tfind. */
2057 if (traceframe_number >= 0)
2058 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
2059 traceframe_number, tracepoint_number);
5796c8dc 2060 else
cf7f2e2d 2061 printf_filtered (_("Not looking at any trace frame.\n"));
a45ae5f8
JM
2062
2063 /* Report start/stop times if supplied. */
2064 if (ts->start_time)
2065 {
2066 if (ts->stop_time)
2067 {
2068 LONGEST run_time = ts->stop_time - ts->start_time;
2069
2070 /* Reporting a run time is more readable than two long numbers. */
2071 printf_filtered (_("Trace started at %ld.%06ld secs, stopped %ld.%06ld secs later.\n"),
2072 (long int) ts->start_time / 1000000,
2073 (long int) ts->start_time % 1000000,
2074 (long int) run_time / 1000000,
2075 (long int) run_time % 1000000);
2076 }
2077 else
2078 printf_filtered (_("Trace started at %ld.%06ld secs.\n"),
2079 (long int) ts->start_time / 1000000,
2080 (long int) ts->start_time % 1000000);
2081 }
2082 else if (ts->stop_time)
2083 printf_filtered (_("Trace stopped at %ld.%06ld secs.\n"),
2084 (long int) ts->stop_time / 1000000,
2085 (long int) ts->stop_time % 1000000);
2086
2087 /* Now report any per-tracepoint status available. */
2088 tp_vec = all_tracepoints ();
2089
2090 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
2091 target_get_tracepoint_status (t, NULL);
2092
2093 VEC_free (breakpoint_p, tp_vec);
5796c8dc
SS
2094}
2095
cf7f2e2d
JM
2096/* Report the trace status to uiout, in a way suitable for MI, and not
2097 suitable for CLI. If ON_STOP is true, suppress a few fields that
2098 are not meaningful in the -trace-stop response.
2099
2100 The implementation is essentially parallel to trace_status_command, but
2101 merging them will result in unreadable code. */
2102void
2103trace_status_mi (int on_stop)
5796c8dc 2104{
a45ae5f8 2105 struct ui_out *uiout = current_uiout;
cf7f2e2d
JM
2106 struct trace_status *ts = current_trace_status ();
2107 int status;
5796c8dc 2108
cf7f2e2d 2109 status = target_get_trace_status (ts);
5796c8dc 2110
ef5ccd6c 2111 if (status == -1 && ts->filename == NULL)
cf7f2e2d
JM
2112 {
2113 ui_out_field_string (uiout, "supported", "0");
2114 return;
2115 }
5796c8dc 2116
ef5ccd6c 2117 if (ts->filename != NULL)
cf7f2e2d
JM
2118 ui_out_field_string (uiout, "supported", "file");
2119 else if (!on_stop)
2120 ui_out_field_string (uiout, "supported", "1");
2121
ef5ccd6c
JM
2122 if (ts->filename != NULL)
2123 ui_out_field_string (uiout, "trace-file", ts->filename);
2124
cf7f2e2d
JM
2125 gdb_assert (ts->running_known);
2126
2127 if (ts->running)
2128 {
2129 ui_out_field_string (uiout, "running", "1");
2130
2131 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
2132 Given that the frontend gets the status either on -trace-stop, or from
2133 -trace-status after re-connection, it does not seem like this
2134 information is necessary for anything. It is not necessary for either
2135 figuring the vital state of the target nor for navigation of trace
2136 frames. If the frontend wants to show the current state is some
2137 configure dialog, it can request the value when such dialog is
2138 invoked by the user. */
2139 }
2140 else
2141 {
2142 char *stop_reason = NULL;
2143 int stopping_tracepoint = -1;
2144
2145 if (!on_stop)
2146 ui_out_field_string (uiout, "running", "0");
2147
2148 if (ts->stop_reason != trace_stop_reason_unknown)
2149 {
2150 switch (ts->stop_reason)
2151 {
2152 case tstop_command:
2153 stop_reason = "request";
2154 break;
2155 case trace_buffer_full:
2156 stop_reason = "overflow";
2157 break;
2158 case trace_disconnected:
2159 stop_reason = "disconnection";
2160 break;
2161 case tracepoint_passcount:
2162 stop_reason = "passcount";
2163 stopping_tracepoint = ts->stopping_tracepoint;
2164 break;
2165 case tracepoint_error:
2166 stop_reason = "error";
2167 stopping_tracepoint = ts->stopping_tracepoint;
2168 break;
2169 }
2170
2171 if (stop_reason)
2172 {
2173 ui_out_field_string (uiout, "stop-reason", stop_reason);
2174 if (stopping_tracepoint != -1)
2175 ui_out_field_int (uiout, "stopping-tracepoint",
2176 stopping_tracepoint);
2177 if (ts->stop_reason == tracepoint_error)
2178 ui_out_field_string (uiout, "error-description",
a45ae5f8 2179 ts->stop_desc);
cf7f2e2d
JM
2180 }
2181 }
2182 }
2183
2184 if (ts->traceframe_count != -1)
2185 ui_out_field_int (uiout, "frames", ts->traceframe_count);
2186 if (ts->traceframes_created != -1)
2187 ui_out_field_int (uiout, "frames-created", ts->traceframes_created);
2188 if (ts->buffer_size != -1)
2189 ui_out_field_int (uiout, "buffer-size", ts->buffer_size);
2190 if (ts->buffer_free != -1)
2191 ui_out_field_int (uiout, "buffer-free", ts->buffer_free);
2192
2193 ui_out_field_int (uiout, "disconnected", ts->disconnected_tracing);
2194 ui_out_field_int (uiout, "circular", ts->circular_buffer);
a45ae5f8
JM
2195
2196 ui_out_field_string (uiout, "user-name", ts->user_name);
2197 ui_out_field_string (uiout, "notes", ts->notes);
2198
2199 {
2200 char buf[100];
2201
2202 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2203 (long int) ts->start_time / 1000000,
2204 (long int) ts->start_time % 1000000);
2205 ui_out_field_string (uiout, "start-time", buf);
2206 xsnprintf (buf, sizeof buf, "%ld.%06ld",
2207 (long int) ts->stop_time / 1000000,
2208 (long int) ts->stop_time % 1000000);
2209 ui_out_field_string (uiout, "stop-time", buf);
2210 }
cf7f2e2d
JM
2211}
2212
2213/* This function handles the details of what to do about an ongoing
2214 tracing run if the user has asked to detach or otherwise disconnect
2215 from the target. */
2216void
2217disconnect_tracing (int from_tty)
2218{
2219 /* It can happen that the target that was tracing went away on its
2220 own, and we didn't notice. Get a status update, and if the
2221 current target doesn't even do tracing, then assume it's not
2222 running anymore. */
2223 if (target_get_trace_status (current_trace_status ()) < 0)
2224 current_trace_status ()->running = 0;
2225
2226 /* If running interactively, give the user the option to cancel and
2227 then decide what to do differently with the run. Scripts are
2228 just going to disconnect and let the target deal with it,
2229 according to how it's been instructed previously via
2230 disconnected-tracing. */
2231 if (current_trace_status ()->running && from_tty)
2232 {
a45ae5f8
JM
2233 process_tracepoint_on_disconnect ();
2234
cf7f2e2d
JM
2235 if (current_trace_status ()->disconnected_tracing)
2236 {
c50c785c
JM
2237 if (!query (_("Trace is running and will "
2238 "continue after detach; detach anyway? ")))
cf7f2e2d
JM
2239 error (_("Not confirmed."));
2240 }
2241 else
2242 {
c50c785c
JM
2243 if (!query (_("Trace is running but will "
2244 "stop on detach; detach anyway? ")))
cf7f2e2d
JM
2245 error (_("Not confirmed."));
2246 }
2247 }
2248
2249 /* Also we want to be out of tfind mode, otherwise things can get
2250 confusing upon reconnection. Just use these calls instead of
2251 full tfind_1 behavior because we're in the middle of detaching,
2252 and there's no point to updating current stack frame etc. */
c50c785c 2253 set_current_traceframe (-1);
ef5ccd6c 2254 set_tracepoint_num (-1);
cf7f2e2d
JM
2255 set_traceframe_context (NULL);
2256}
2257
2258/* Worker function for the various flavors of the tfind command. */
2259void
2260tfind_1 (enum trace_find_type type, int num,
2261 ULONGEST addr1, ULONGEST addr2,
2262 int from_tty)
2263{
2264 int target_frameno = -1, target_tracept = -1;
2265 struct frame_id old_frame_id = null_frame_id;
a45ae5f8
JM
2266 struct tracepoint *tp;
2267 struct ui_out *uiout = current_uiout;
cf7f2e2d
JM
2268
2269 /* Only try to get the current stack frame if we have a chance of
2270 succeeding. In particular, if we're trying to get a first trace
2271 frame while all threads are running, it's not going to succeed,
2272 so leave it with a default value and let the frame comparison
2273 below (correctly) decide to print out the source location of the
2274 trace frame. */
2275 if (!(type == tfind_number && num == -1)
2276 && (has_stack_frames () || traceframe_number >= 0))
2277 old_frame_id = get_frame_id (get_current_frame ());
2278
2279 target_frameno = target_trace_find (type, num, addr1, addr2,
2280 &target_tracept);
2281
2282 if (type == tfind_number
2283 && num == -1
2284 && target_frameno == -1)
2285 {
2286 /* We told the target to get out of tfind mode, and it did. */
2287 }
2288 else if (target_frameno == -1)
2289 {
2290 /* A request for a non-existent trace frame has failed.
2291 Our response will be different, depending on FROM_TTY:
2292
2293 If FROM_TTY is true, meaning that this command was
2294 typed interactively by the user, then give an error
2295 and DO NOT change the state of traceframe_number etc.
2296
2297 However if FROM_TTY is false, meaning that we're either
2298 in a script, a loop, or a user-defined command, then
2299 DON'T give an error, but DO change the state of
2300 traceframe_number etc. to invalid.
2301
2302 The rationalle is that if you typed the command, you
2303 might just have committed a typo or something, and you'd
2304 like to NOT lose your current debugging state. However
2305 if you're in a user-defined command or especially in a
2306 loop, then you need a way to detect that the command
2307 failed WITHOUT aborting. This allows you to write
2308 scripts that search thru the trace buffer until the end,
2309 and then continue on to do something else. */
2310
2311 if (from_tty)
2312 error (_("Target failed to find requested trace frame."));
2313 else
2314 {
2315 if (info_verbose)
2316 printf_filtered ("End of trace buffer.\n");
c50c785c 2317#if 0 /* dubious now? */
cf7f2e2d
JM
2318 /* The following will not recurse, since it's
2319 special-cased. */
2320 trace_find_command ("-1", from_tty);
2321#endif
2322 }
2323 }
2324
2325 tp = get_tracepoint_by_number_on_target (target_tracept);
5796c8dc
SS
2326
2327 reinit_frame_cache ();
cf7f2e2d 2328 target_dcache_invalidate ();
ef5ccd6c 2329
a45ae5f8 2330 set_tracepoint_num (tp ? tp->base.number : target_tracept);
ef5ccd6c
JM
2331
2332 if (target_frameno != get_traceframe_number ())
2333 observer_notify_traceframe_changed (target_frameno, tracepoint_number);
2334
2335 set_current_traceframe (target_frameno);
2336
5796c8dc
SS
2337 if (target_frameno == -1)
2338 set_traceframe_context (NULL);
2339 else
2340 set_traceframe_context (get_current_frame ());
2341
cf7f2e2d
JM
2342 if (traceframe_number >= 0)
2343 {
2344 /* Use different branches for MI and CLI to make CLI messages
2345 i18n-eable. */
2346 if (ui_out_is_mi_like_p (uiout))
2347 {
2348 ui_out_field_string (uiout, "found", "1");
2349 ui_out_field_int (uiout, "tracepoint", tracepoint_number);
2350 ui_out_field_int (uiout, "traceframe", traceframe_number);
2351 }
2352 else
2353 {
2354 printf_unfiltered (_("Found trace frame %d, tracepoint %d\n"),
2355 traceframe_number, tracepoint_number);
2356 }
2357 }
2358 else
2359 {
2360 if (ui_out_is_mi_like_p (uiout))
2361 ui_out_field_string (uiout, "found", "0");
2362 else if (type == tfind_number && num == -1)
2363 printf_unfiltered (_("No longer looking at any trace frame\n"));
c50c785c 2364 else /* This case may never occur, check. */
cf7f2e2d
JM
2365 printf_unfiltered (_("No trace frame found\n"));
2366 }
2367
2368 /* If we're in nonstop mode and getting out of looking at trace
2369 frames, there won't be any current frame to go back to and
2370 display. */
2371 if (from_tty
2372 && (has_stack_frames () || traceframe_number >= 0))
5796c8dc
SS
2373 {
2374 enum print_what print_what;
2375
cf7f2e2d 2376 /* NOTE: in imitation of the step command, try to determine
5796c8dc
SS
2377 whether we have made a transition from one function to
2378 another. If so, we'll print the "stack frame" (ie. the new
2379 function and it's arguments) -- otherwise we'll just show the
2380 new source line. */
2381
2382 if (frame_id_eq (old_frame_id,
2383 get_frame_id (get_current_frame ())))
2384 print_what = SRC_LINE;
2385 else
2386 print_what = SRC_AND_LOC;
2387
2388 print_stack_frame (get_selected_frame (NULL), 1, print_what);
2389 do_displays ();
2390 }
2391}
2392
2393/* trace_find_command takes a trace frame number n,
2394 sends "QTFrame:<n>" to the target,
2395 and accepts a reply that may contain several optional pieces
2396 of information: a frame number, a tracepoint number, and an
2397 indication of whether this is a trap frame or a stepping frame.
2398
2399 The minimal response is just "OK" (which indicates that the
2400 target does not give us a frame number or a tracepoint number).
2401 Instead of that, the target may send us a string containing
2402 any combination of:
2403 F<hexnum> (gives the selected frame number)
2404 T<hexnum> (gives the selected tracepoint number)
2405 */
2406
2407/* tfind command */
2408static void
2409trace_find_command (char *args, int from_tty)
c50c785c 2410{ /* This should only be called with a numeric argument. */
5796c8dc
SS
2411 int frameno = -1;
2412
ef5ccd6c
JM
2413 if (current_trace_status ()->running
2414 && current_trace_status ()->filename == NULL)
c50c785c 2415 error (_("May not look at trace frames while trace is running."));
cf7f2e2d
JM
2416
2417 if (args == 0 || *args == 0)
2418 { /* TFIND with no args means find NEXT trace frame. */
2419 if (traceframe_number == -1)
c50c785c 2420 frameno = 0; /* "next" is first one. */
cf7f2e2d
JM
2421 else
2422 frameno = traceframe_number + 1;
2423 }
2424 else if (0 == strcmp (args, "-"))
5796c8dc 2425 {
cf7f2e2d
JM
2426 if (traceframe_number == -1)
2427 error (_("not debugging trace buffer"));
2428 else if (from_tty && traceframe_number == 0)
2429 error (_("already at start of trace buffer"));
2430
2431 frameno = traceframe_number - 1;
2432 }
2433 /* A hack to work around eval's need for fp to have been collected. */
2434 else if (0 == strcmp (args, "-1"))
2435 frameno = -1;
2436 else
2437 frameno = parse_and_eval_long (args);
5796c8dc 2438
cf7f2e2d
JM
2439 if (frameno < -1)
2440 error (_("invalid input (%d is less than zero)"), frameno);
5796c8dc 2441
cf7f2e2d 2442 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
5796c8dc
SS
2443}
2444
2445/* tfind end */
2446static void
2447trace_find_end_command (char *args, int from_tty)
2448{
2449 trace_find_command ("-1", from_tty);
2450}
2451
5796c8dc
SS
2452/* tfind start */
2453static void
2454trace_find_start_command (char *args, int from_tty)
2455{
2456 trace_find_command ("0", from_tty);
2457}
2458
2459/* tfind pc command */
2460static void
2461trace_find_pc_command (char *args, int from_tty)
2462{
2463 CORE_ADDR pc;
5796c8dc 2464
ef5ccd6c
JM
2465 if (current_trace_status ()->running
2466 && current_trace_status ()->filename == NULL)
c50c785c 2467 error (_("May not look at trace frames while trace is running."));
5796c8dc 2468
cf7f2e2d
JM
2469 if (args == 0 || *args == 0)
2470 pc = regcache_read_pc (get_current_regcache ());
5796c8dc 2471 else
cf7f2e2d
JM
2472 pc = parse_and_eval_address (args);
2473
2474 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
5796c8dc
SS
2475}
2476
2477/* tfind tracepoint command */
2478static void
2479trace_find_tracepoint_command (char *args, int from_tty)
2480{
2481 int tdp;
a45ae5f8 2482 struct tracepoint *tp;
cf7f2e2d 2483
ef5ccd6c
JM
2484 if (current_trace_status ()->running
2485 && current_trace_status ()->filename == NULL)
c50c785c 2486 error (_("May not look at trace frames while trace is running."));
5796c8dc 2487
cf7f2e2d 2488 if (args == 0 || *args == 0)
5796c8dc 2489 {
cf7f2e2d
JM
2490 if (tracepoint_number == -1)
2491 error (_("No current tracepoint -- please supply an argument."));
5796c8dc 2492 else
c50c785c 2493 tdp = tracepoint_number; /* Default is current TDP. */
5796c8dc
SS
2494 }
2495 else
cf7f2e2d
JM
2496 tdp = parse_and_eval_long (args);
2497
2498 /* If we have the tracepoint on hand, use the number that the
2499 target knows about (which may be different if we disconnected
2500 and reconnected). */
2501 tp = get_tracepoint (tdp);
2502 if (tp)
2503 tdp = tp->number_on_target;
2504
2505 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
5796c8dc
SS
2506}
2507
2508/* TFIND LINE command:
2509
2510 This command will take a sourceline for argument, just like BREAK
2511 or TRACE (ie. anything that "decode_line_1" can handle).
2512
2513 With no argument, this command will find the next trace frame
2514 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2515
2516static void
2517trace_find_line_command (char *args, int from_tty)
2518{
2519 static CORE_ADDR start_pc, end_pc;
2520 struct symtabs_and_lines sals;
2521 struct symtab_and_line sal;
2522 struct cleanup *old_chain;
5796c8dc 2523
ef5ccd6c
JM
2524 if (current_trace_status ()->running
2525 && current_trace_status ()->filename == NULL)
c50c785c 2526 error (_("May not look at trace frames while trace is running."));
5796c8dc 2527
cf7f2e2d
JM
2528 if (args == 0 || *args == 0)
2529 {
2530 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
2531 sals.nelts = 1;
2532 sals.sals = (struct symtab_and_line *)
2533 xmalloc (sizeof (struct symtab_and_line));
2534 sals.sals[0] = sal;
5796c8dc
SS
2535 }
2536 else
cf7f2e2d 2537 {
ef5ccd6c 2538 sals = decode_line_with_current_source (args, DECODE_LINE_FUNFIRSTLINE);
cf7f2e2d
JM
2539 sal = sals.sals[0];
2540 }
2541
2542 old_chain = make_cleanup (xfree, sals.sals);
2543 if (sal.symtab == 0)
2544 error (_("No line number information available."));
2545
2546 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2547 {
2548 if (start_pc == end_pc)
2549 {
2550 printf_filtered ("Line %d of \"%s\"",
ef5ccd6c
JM
2551 sal.line,
2552 symtab_to_filename_for_display (sal.symtab));
cf7f2e2d
JM
2553 wrap_here (" ");
2554 printf_filtered (" is at address ");
2555 print_address (get_current_arch (), start_pc, gdb_stdout);
2556 wrap_here (" ");
2557 printf_filtered (" but contains no code.\n");
2558 sal = find_pc_line (start_pc, 0);
2559 if (sal.line > 0
2560 && find_line_pc_range (sal, &start_pc, &end_pc)
2561 && start_pc != end_pc)
2562 printf_filtered ("Attempting to find line %d instead.\n",
2563 sal.line);
2564 else
2565 error (_("Cannot find a good line."));
2566 }
2567 }
2568 else
2569 /* Is there any case in which we get here, and have an address
2570 which the user would want to see? If we have debugging
2571 symbols and no line numbers? */
2572 error (_("Line number %d is out of range for \"%s\"."),
ef5ccd6c 2573 sal.line, symtab_to_filename_for_display (sal.symtab));
cf7f2e2d
JM
2574
2575 /* Find within range of stated line. */
2576 if (args && *args)
2577 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
2578 else
2579 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
2580 do_cleanups (old_chain);
5796c8dc
SS
2581}
2582
2583/* tfind range command */
2584static void
2585trace_find_range_command (char *args, int from_tty)
2586{
2587 static CORE_ADDR start, stop;
5796c8dc
SS
2588 char *tmp;
2589
ef5ccd6c
JM
2590 if (current_trace_status ()->running
2591 && current_trace_status ()->filename == NULL)
c50c785c 2592 error (_("May not look at trace frames while trace is running."));
5796c8dc 2593
cf7f2e2d
JM
2594 if (args == 0 || *args == 0)
2595 { /* XXX FIXME: what should default behavior be? */
2596 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2597 return;
2598 }
5796c8dc 2599
cf7f2e2d
JM
2600 if (0 != (tmp = strchr (args, ',')))
2601 {
c50c785c 2602 *tmp++ = '\0'; /* Terminate start address. */
ef5ccd6c 2603 tmp = skip_spaces (tmp);
cf7f2e2d
JM
2604 start = parse_and_eval_address (args);
2605 stop = parse_and_eval_address (tmp);
5796c8dc
SS
2606 }
2607 else
c50c785c 2608 { /* No explicit end address? */
cf7f2e2d
JM
2609 start = parse_and_eval_address (args);
2610 stop = start + 1; /* ??? */
2611 }
2612
2613 tfind_1 (tfind_range, 0, start, stop, from_tty);
5796c8dc
SS
2614}
2615
2616/* tfind outside command */
2617static void
2618trace_find_outside_command (char *args, int from_tty)
2619{
2620 CORE_ADDR start, stop;
5796c8dc
SS
2621 char *tmp;
2622
ef5ccd6c
JM
2623 if (current_trace_status ()->running
2624 && current_trace_status ()->filename == NULL)
c50c785c 2625 error (_("May not look at trace frames while trace is running."));
5796c8dc 2626
cf7f2e2d 2627 if (args == 0 || *args == 0)
c50c785c 2628 { /* XXX FIXME: what should default behavior be? */
cf7f2e2d
JM
2629 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2630 return;
2631 }
5796c8dc 2632
cf7f2e2d
JM
2633 if (0 != (tmp = strchr (args, ',')))
2634 {
c50c785c 2635 *tmp++ = '\0'; /* Terminate start address. */
ef5ccd6c 2636 tmp = skip_spaces (tmp);
cf7f2e2d
JM
2637 start = parse_and_eval_address (args);
2638 stop = parse_and_eval_address (tmp);
5796c8dc
SS
2639 }
2640 else
c50c785c 2641 { /* No explicit end address? */
cf7f2e2d
JM
2642 start = parse_and_eval_address (args);
2643 stop = start + 1; /* ??? */
2644 }
2645
2646 tfind_1 (tfind_outside, 0, start, stop, from_tty);
5796c8dc
SS
2647}
2648
2649/* info scope command: list the locals for a scope. */
2650static void
2651scope_info (char *args, int from_tty)
2652{
2653 struct symtabs_and_lines sals;
2654 struct symbol *sym;
2655 struct minimal_symbol *msym;
2656 struct block *block;
ef5ccd6c
JM
2657 const char *symname;
2658 char *save_args = args;
2659 struct block_iterator iter;
5796c8dc
SS
2660 int j, count = 0;
2661 struct gdbarch *gdbarch;
2662 int regno;
2663
2664 if (args == 0 || *args == 0)
c50c785c
JM
2665 error (_("requires an argument (function, "
2666 "line or *addr) to define a scope"));
5796c8dc 2667
a45ae5f8 2668 sals = decode_line_1 (&args, DECODE_LINE_FUNFIRSTLINE, NULL, 0);
5796c8dc 2669 if (sals.nelts == 0)
c50c785c 2670 return; /* Presumably decode_line_1 has already warned. */
5796c8dc 2671
c50c785c 2672 /* Resolve line numbers to PC. */
5796c8dc
SS
2673 resolve_sal_pc (&sals.sals[0]);
2674 block = block_for_pc (sals.sals[0].pc);
2675
2676 while (block != 0)
2677 {
c50c785c 2678 QUIT; /* Allow user to bail out with ^C. */
5796c8dc
SS
2679 ALL_BLOCK_SYMBOLS (block, iter, sym)
2680 {
c50c785c 2681 QUIT; /* Allow user to bail out with ^C. */
5796c8dc
SS
2682 if (count == 0)
2683 printf_filtered ("Scope for %s:\n", save_args);
2684 count++;
2685
2686 symname = SYMBOL_PRINT_NAME (sym);
2687 if (symname == NULL || *symname == '\0')
c50c785c 2688 continue; /* Probably botched, certainly useless. */
5796c8dc
SS
2689
2690 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
2691
2692 printf_filtered ("Symbol %s is ", symname);
2693 switch (SYMBOL_CLASS (sym))
2694 {
2695 default:
c50c785c 2696 case LOC_UNDEF: /* Messed up symbol? */
5796c8dc
SS
2697 printf_filtered ("a bogus symbol, class %d.\n",
2698 SYMBOL_CLASS (sym));
c50c785c 2699 count--; /* Don't count this one. */
5796c8dc
SS
2700 continue;
2701 case LOC_CONST:
ef5ccd6c
JM
2702 printf_filtered ("a constant with value %s (%s)",
2703 plongest (SYMBOL_VALUE (sym)),
2704 hex_string (SYMBOL_VALUE (sym)));
5796c8dc
SS
2705 break;
2706 case LOC_CONST_BYTES:
2707 printf_filtered ("constant bytes: ");
2708 if (SYMBOL_TYPE (sym))
2709 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2710 fprintf_filtered (gdb_stdout, " %02x",
2711 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2712 break;
2713 case LOC_STATIC:
2714 printf_filtered ("in static storage at address ");
2715 printf_filtered ("%s", paddress (gdbarch,
2716 SYMBOL_VALUE_ADDRESS (sym)));
2717 break;
2718 case LOC_REGISTER:
2719 /* GDBARCH is the architecture associated with the objfile
2720 the symbol is defined in; the target architecture may be
2721 different, and may provide additional registers. However,
2722 we do not know the target architecture at this point.
2723 We assume the objfile architecture will contain all the
2724 standard registers that occur in debug info in that
2725 objfile. */
c50c785c
JM
2726 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2727 gdbarch);
5796c8dc
SS
2728
2729 if (SYMBOL_IS_ARGUMENT (sym))
2730 printf_filtered ("an argument in register $%s",
2731 gdbarch_register_name (gdbarch, regno));
2732 else
2733 printf_filtered ("a local variable in register $%s",
2734 gdbarch_register_name (gdbarch, regno));
2735 break;
2736 case LOC_ARG:
ef5ccd6c
JM
2737 printf_filtered ("an argument at stack/frame offset %s",
2738 plongest (SYMBOL_VALUE (sym)));
5796c8dc
SS
2739 break;
2740 case LOC_LOCAL:
ef5ccd6c
JM
2741 printf_filtered ("a local variable at frame offset %s",
2742 plongest (SYMBOL_VALUE (sym)));
5796c8dc
SS
2743 break;
2744 case LOC_REF_ARG:
ef5ccd6c
JM
2745 printf_filtered ("a reference argument at offset %s",
2746 plongest (SYMBOL_VALUE (sym)));
5796c8dc
SS
2747 break;
2748 case LOC_REGPARM_ADDR:
2749 /* Note comment at LOC_REGISTER. */
c50c785c
JM
2750 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym,
2751 gdbarch);
5796c8dc
SS
2752 printf_filtered ("the address of an argument, in register $%s",
2753 gdbarch_register_name (gdbarch, regno));
2754 break;
2755 case LOC_TYPEDEF:
2756 printf_filtered ("a typedef.\n");
2757 continue;
2758 case LOC_LABEL:
2759 printf_filtered ("a label at address ");
2760 printf_filtered ("%s", paddress (gdbarch,
2761 SYMBOL_VALUE_ADDRESS (sym)));
2762 break;
2763 case LOC_BLOCK:
2764 printf_filtered ("a function at address ");
2765 printf_filtered ("%s",
2766 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
2767 break;
2768 case LOC_UNRESOLVED:
2769 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
2770 NULL, NULL);
2771 if (msym == NULL)
2772 printf_filtered ("Unresolved Static");
2773 else
2774 {
2775 printf_filtered ("static storage at address ");
2776 printf_filtered ("%s",
2777 paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msym)));
2778 }
2779 break;
2780 case LOC_OPTIMIZED_OUT:
2781 printf_filtered ("optimized out.\n");
2782 continue;
2783 case LOC_COMPUTED:
cf7f2e2d
JM
2784 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym,
2785 BLOCK_START (block),
2786 gdb_stdout);
5796c8dc
SS
2787 break;
2788 }
2789 if (SYMBOL_TYPE (sym))
2790 printf_filtered (", length %d.\n",
2791 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
2792 }
2793 if (BLOCK_FUNCTION (block))
2794 break;
2795 else
2796 block = BLOCK_SUPERBLOCK (block);
2797 }
2798 if (count <= 0)
2799 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2800 save_args);
2801}
2802
2803/* worker function (cleanup) */
2804static void
2805replace_comma (void *data)
2806{
2807 char *comma = data;
2808 *comma = ',';
2809}
2810
cf7f2e2d
JM
2811
2812/* Helper for trace_dump_command. Dump the action list starting at
2813 ACTION. STEPPING_ACTIONS is true if we're iterating over the
2814 actions of the body of a while-stepping action. STEPPING_FRAME is
2815 set if the current traceframe was determined to be a while-stepping
2816 traceframe. */
2817
5796c8dc 2818static void
cf7f2e2d
JM
2819trace_dump_actions (struct command_line *action,
2820 int stepping_actions, int stepping_frame,
2821 int from_tty)
5796c8dc 2822{
5796c8dc 2823 char *action_exp, *next_comma;
5796c8dc 2824
cf7f2e2d 2825 for (; action != NULL; action = action->next)
5796c8dc
SS
2826 {
2827 struct cmd_list_element *cmd;
2828
c50c785c 2829 QUIT; /* Allow user to bail out with ^C. */
cf7f2e2d 2830 action_exp = action->line;
ef5ccd6c 2831 action_exp = skip_spaces (action_exp);
5796c8dc
SS
2832
2833 /* The collection actions to be done while stepping are
2834 bracketed by the commands "while-stepping" and "end". */
2835
2836 if (*action_exp == '#') /* comment line */
2837 continue;
2838
2839 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2840 if (cmd == 0)
2841 error (_("Bad action list item: %s"), action_exp);
2842
2843 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
cf7f2e2d
JM
2844 {
2845 int i;
2846
2847 for (i = 0; i < action->body_count; ++i)
2848 trace_dump_actions (action->body_list[i],
2849 1, stepping_frame, from_tty);
2850 }
5796c8dc
SS
2851 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
2852 {
2853 /* Display the collected data.
2854 For the trap frame, display only what was collected at
2855 the trap. Likewise for stepping frames, display only
2856 what was collected while stepping. This means that the
2857 two boolean variables, STEPPING_FRAME and
2858 STEPPING_ACTIONS should be equal. */
2859 if (stepping_frame == stepping_actions)
2860 {
a45ae5f8
JM
2861 if (*action_exp == '/')
2862 action_exp = decode_agent_options (action_exp);
2863
5796c8dc 2864 do
c50c785c
JM
2865 { /* Repeat over a comma-separated list. */
2866 QUIT; /* Allow user to bail out with ^C. */
5796c8dc
SS
2867 if (*action_exp == ',')
2868 action_exp++;
ef5ccd6c 2869 action_exp = skip_spaces (action_exp);
5796c8dc
SS
2870
2871 next_comma = strchr (action_exp, ',');
2872
2873 if (0 == strncasecmp (action_exp, "$reg", 4))
2874 registers_info (NULL, from_tty);
a45ae5f8
JM
2875 else if (0 == strncasecmp (action_exp, "$_ret", 5))
2876 ;
5796c8dc
SS
2877 else if (0 == strncasecmp (action_exp, "$loc", 4))
2878 locals_info (NULL, from_tty);
2879 else if (0 == strncasecmp (action_exp, "$arg", 4))
2880 args_info (NULL, from_tty);
2881 else
2882 { /* variable */
2883 if (next_comma)
2884 {
2885 make_cleanup (replace_comma, next_comma);
2886 *next_comma = '\0';
2887 }
2888 printf_filtered ("%s = ", action_exp);
2889 output_command (action_exp, from_tty);
2890 printf_filtered ("\n");
2891 }
2892 if (next_comma)
2893 *next_comma = ',';
2894 action_exp = next_comma;
2895 }
2896 while (action_exp && *action_exp == ',');
2897 }
2898 }
2899 }
5796c8dc
SS
2900}
2901
cf7f2e2d 2902/* The tdump command. */
5796c8dc 2903
cf7f2e2d
JM
2904static void
2905trace_dump_command (char *args, int from_tty)
5796c8dc 2906{
cf7f2e2d 2907 struct regcache *regcache;
a45ae5f8 2908 struct tracepoint *t;
cf7f2e2d
JM
2909 int stepping_frame = 0;
2910 struct bp_location *loc;
2911 char *line, *default_collect_line = NULL;
2912 struct command_line *actions, *default_collect_action = NULL;
2913 struct cleanup *old_chain = NULL;
5796c8dc 2914
cf7f2e2d 2915 if (tracepoint_number == -1)
5796c8dc 2916 {
cf7f2e2d
JM
2917 warning (_("No current trace frame."));
2918 return;
2919 }
5796c8dc 2920
cf7f2e2d
JM
2921 t = get_tracepoint (tracepoint_number);
2922
2923 if (t == NULL)
2924 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2925 tracepoint_number);
2926
2927 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2928 tracepoint_number, traceframe_number);
2929
2930 /* The current frame is a trap frame if the frame PC is equal
2931 to the tracepoint PC. If not, then the current frame was
2932 collected during single-stepping. */
2933
2934 regcache = get_current_regcache ();
2935
2936 /* If the traceframe's address matches any of the tracepoint's
2937 locations, assume it is a direct hit rather than a while-stepping
2938 frame. (FIXME this is not reliable, should record each frame's
2939 type.) */
2940 stepping_frame = 1;
a45ae5f8 2941 for (loc = t->base.loc; loc; loc = loc->next)
cf7f2e2d
JM
2942 if (loc->address == regcache_read_pc (regcache))
2943 stepping_frame = 0;
2944
a45ae5f8 2945 actions = breakpoint_commands (&t->base);
cf7f2e2d
JM
2946
2947 /* If there is a default-collect list, make up a collect command,
2948 prepend to the tracepoint's commands, and pass the whole mess to
2949 the trace dump scanner. We need to validate because
2950 default-collect might have been junked since the trace run. */
2951 if (*default_collect)
2952 {
2953 default_collect_line = xstrprintf ("collect %s", default_collect);
2954 old_chain = make_cleanup (xfree, default_collect_line);
2955 line = default_collect_line;
a45ae5f8 2956 validate_actionline (&line, &t->base);
cf7f2e2d
JM
2957 default_collect_action = xmalloc (sizeof (struct command_line));
2958 make_cleanup (xfree, default_collect_action);
2959 default_collect_action->next = actions;
2960 default_collect_action->line = line;
2961 actions = default_collect_action;
5796c8dc
SS
2962 }
2963
cf7f2e2d 2964 trace_dump_actions (actions, 0, stepping_frame, from_tty);
5796c8dc 2965
cf7f2e2d
JM
2966 if (*default_collect)
2967 do_cleanups (old_chain);
5796c8dc
SS
2968}
2969
cf7f2e2d
JM
2970/* Encode a piece of a tracepoint's source-level definition in a form
2971 that is suitable for both protocol and saving in files. */
2972/* This version does not do multiple encodes for long strings; it should
2973 return an offset to the next piece to encode. FIXME */
2974
2975extern int
2976encode_source_string (int tpnum, ULONGEST addr,
2977 char *srctype, char *src, char *buf, int buf_size)
5796c8dc 2978{
cf7f2e2d
JM
2979 if (80 + strlen (srctype) > buf_size)
2980 error (_("Buffer too small for source encoding"));
2981 sprintf (buf, "%x:%s:%s:%x:%x:",
c50c785c
JM
2982 tpnum, phex_nz (addr, sizeof (addr)),
2983 srctype, 0, (int) strlen (src));
cf7f2e2d
JM
2984 if (strlen (buf) + strlen (src) * 2 >= buf_size)
2985 error (_("Source string too long for buffer"));
2986 bin2hex (src, buf + strlen (buf), 0);
2987 return -1;
5796c8dc
SS
2988}
2989
cf7f2e2d
JM
2990extern int trace_regblock_size;
2991
2992/* Save tracepoint data to file named FILENAME. If TARGET_DOES_SAVE is
2993 non-zero, the save is performed on the target, otherwise GDB obtains all
2994 trace data and saves it locally. */
5796c8dc 2995
5796c8dc 2996void
cf7f2e2d 2997trace_save (const char *filename, int target_does_save)
5796c8dc 2998{
cf7f2e2d
JM
2999 struct cleanup *cleanup;
3000 char *pathname;
3001 struct trace_status *ts = current_trace_status ();
3002 int err, status;
3003 FILE *fp;
3004 struct uploaded_tp *uploaded_tps = NULL, *utp;
3005 struct uploaded_tsv *uploaded_tsvs = NULL, *utsv;
3006 int a;
3007 char *act;
3008 LONGEST gotten = 0;
3009 ULONGEST offset = 0;
3010#define MAX_TRACE_UPLOAD 2000
3011 gdb_byte buf[MAX_TRACE_UPLOAD];
3012 int written;
3013
3014 /* If the target is to save the data to a file on its own, then just
3015 send the command and be done with it. */
3016 if (target_does_save)
3017 {
3018 err = target_save_trace_data (filename);
3019 if (err < 0)
3020 error (_("Target failed to save trace data to '%s'."),
3021 filename);
3022 return;
3023 }
5796c8dc 3024
cf7f2e2d
JM
3025 /* Get the trace status first before opening the file, so if the
3026 target is losing, we can get out without touching files. */
3027 status = target_get_trace_status (ts);
5796c8dc 3028
cf7f2e2d
JM
3029 pathname = tilde_expand (filename);
3030 cleanup = make_cleanup (xfree, pathname);
3031
3032 fp = fopen (pathname, "wb");
3033 if (!fp)
3034 error (_("Unable to open file '%s' for saving trace data (%s)"),
3035 filename, safe_strerror (errno));
3036 make_cleanup_fclose (fp);
3037
3038 /* Write a file header, with a high-bit-set char to indicate a
3039 binary file, plus a hint as what this file is, and a version
3040 number in case of future needs. */
3041 written = fwrite ("\x7fTRACE0\n", 8, 1, fp);
3042 if (written < 1)
3043 perror_with_name (pathname);
3044
3045 /* Write descriptive info. */
3046
3047 /* Write out the size of a register block. */
3048 fprintf (fp, "R %x\n", trace_regblock_size);
3049
3050 /* Write out status of the tracing run (aka "tstatus" info). */
3051 fprintf (fp, "status %c;%s",
3052 (ts->running ? '1' : '0'), stop_reason_names[ts->stop_reason]);
3053 if (ts->stop_reason == tracepoint_error)
5796c8dc 3054 {
a45ae5f8 3055 char *buf = (char *) alloca (strlen (ts->stop_desc) * 2 + 1);
cf7f2e2d 3056
a45ae5f8 3057 bin2hex ((gdb_byte *) ts->stop_desc, buf, 0);
cf7f2e2d 3058 fprintf (fp, ":%s", buf);
5796c8dc 3059 }
cf7f2e2d
JM
3060 fprintf (fp, ":%x", ts->stopping_tracepoint);
3061 if (ts->traceframe_count >= 0)
3062 fprintf (fp, ";tframes:%x", ts->traceframe_count);
3063 if (ts->traceframes_created >= 0)
3064 fprintf (fp, ";tcreated:%x", ts->traceframes_created);
3065 if (ts->buffer_free >= 0)
3066 fprintf (fp, ";tfree:%x", ts->buffer_free);
3067 if (ts->buffer_size >= 0)
3068 fprintf (fp, ";tsize:%x", ts->buffer_size);
3069 if (ts->disconnected_tracing)
3070 fprintf (fp, ";disconn:%x", ts->disconnected_tracing);
3071 if (ts->circular_buffer)
3072 fprintf (fp, ";circular:%x", ts->circular_buffer);
3073 fprintf (fp, "\n");
3074
3075 /* Note that we want to upload tracepoints and save those, rather
3076 than simply writing out the local ones, because the user may have
3077 changed tracepoints in GDB in preparation for a future tracing
3078 run, or maybe just mass-deleted all types of breakpoints as part
3079 of cleaning up. So as not to contaminate the session, leave the
3080 data in its uploaded form, don't make into real tracepoints. */
3081
3082 /* Get trace state variables first, they may be checked when parsing
3083 uploaded commands. */
3084
3085 target_upload_trace_state_variables (&uploaded_tsvs);
3086
3087 for (utsv = uploaded_tsvs; utsv; utsv = utsv->next)
5796c8dc 3088 {
cf7f2e2d 3089 char *buf = "";
5796c8dc 3090
cf7f2e2d
JM
3091 if (utsv->name)
3092 {
3093 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
3094 bin2hex ((gdb_byte *) (utsv->name), buf, 0);
3095 }
3096
3097 fprintf (fp, "tsv %x:%s:%x:%s\n",
3098 utsv->number, phex_nz (utsv->initial_value, 8),
3099 utsv->builtin, buf);
3100
3101 if (utsv->name)
3102 xfree (buf);
3103 }
3104
3105 free_uploaded_tsvs (&uploaded_tsvs);
3106
3107 target_upload_tracepoints (&uploaded_tps);
3108
a45ae5f8
JM
3109 for (utp = uploaded_tps; utp; utp = utp->next)
3110 target_get_tracepoint_status (NULL, utp);
3111
cf7f2e2d
JM
3112 for (utp = uploaded_tps; utp; utp = utp->next)
3113 {
3114 fprintf (fp, "tp T%x:%s:%c:%x:%x",
3115 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
3116 (utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
3117 if (utp->type == bp_fast_tracepoint)
3118 fprintf (fp, ":F%x", utp->orig_size);
3119 if (utp->cond)
3120 fprintf (fp, ":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
3121 utp->cond);
3122 fprintf (fp, "\n");
3123 for (a = 0; VEC_iterate (char_ptr, utp->actions, a, act); ++a)
3124 fprintf (fp, "tp A%x:%s:%s\n",
3125 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
c50c785c 3126 for (a = 0; VEC_iterate (char_ptr, utp->step_actions, a, act); ++a)
cf7f2e2d
JM
3127 fprintf (fp, "tp S%x:%s:%s\n",
3128 utp->number, phex_nz (utp->addr, sizeof (utp->addr)), act);
3129 if (utp->at_string)
3130 {
3131 encode_source_string (utp->number, utp->addr,
3132 "at", utp->at_string, buf, MAX_TRACE_UPLOAD);
3133 fprintf (fp, "tp Z%s\n", buf);
3134 }
3135 if (utp->cond_string)
3136 {
3137 encode_source_string (utp->number, utp->addr,
c50c785c
JM
3138 "cond", utp->cond_string,
3139 buf, MAX_TRACE_UPLOAD);
cf7f2e2d
JM
3140 fprintf (fp, "tp Z%s\n", buf);
3141 }
3142 for (a = 0; VEC_iterate (char_ptr, utp->cmd_strings, a, act); ++a)
3143 {
3144 encode_source_string (utp->number, utp->addr, "cmd", act,
3145 buf, MAX_TRACE_UPLOAD);
3146 fprintf (fp, "tp Z%s\n", buf);
3147 }
a45ae5f8
JM
3148 fprintf (fp, "tp V%x:%s:%x:%s\n",
3149 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
3150 utp->hit_count,
3151 phex_nz (utp->traceframe_usage,
3152 sizeof (utp->traceframe_usage)));
cf7f2e2d
JM
3153 }
3154
3155 free_uploaded_tps (&uploaded_tps);
3156
3157 /* Mark the end of the definition section. */
3158 fprintf (fp, "\n");
3159
3160 /* Get and write the trace data proper. We ask for big blocks, in
3161 the hopes of efficiency, but will take less if the target has
3162 packet size limitations or some such. */
3163 while (1)
3164 {
3165 gotten = target_get_raw_trace_data (buf, offset, MAX_TRACE_UPLOAD);
3166 if (gotten < 0)
3167 error (_("Failure to get requested trace buffer data"));
3168 /* No more data is forthcoming, we're done. */
3169 if (gotten == 0)
3170 break;
3171 written = fwrite (buf, gotten, 1, fp);
3172 if (written < 1)
3173 perror_with_name (pathname);
3174 offset += gotten;
3175 }
3176
3177 /* Mark the end of trace data. (We know that gotten is 0 at this point.) */
3178 written = fwrite (&gotten, 4, 1, fp);
3179 if (written < 1)
3180 perror_with_name (pathname);
3181
3182 do_cleanups (cleanup);
3183}
3184
3185static void
3186trace_save_command (char *args, int from_tty)
3187{
3188 int target_does_save = 0;
3189 char **argv;
3190 char *filename = NULL;
3191 struct cleanup *back_to;
3192
3193 if (args == NULL)
3194 error_no_arg (_("file in which to save trace data"));
3195
3196 argv = gdb_buildargv (args);
3197 back_to = make_cleanup_freeargv (argv);
3198
3199 for (; *argv; ++argv)
3200 {
3201 if (strcmp (*argv, "-r") == 0)
3202 target_does_save = 1;
3203 else if (**argv == '-')
3204 error (_("unknown option `%s'"), *argv);
3205 else
3206 filename = *argv;
3207 }
3208
3209 if (!filename)
3210 error_no_arg (_("file in which to save trace data"));
3211
3212 trace_save (filename, target_does_save);
3213
3214 if (from_tty)
a45ae5f8 3215 printf_filtered (_("Trace data saved to file '%s'.\n"), filename);
cf7f2e2d
JM
3216
3217 do_cleanups (back_to);
3218}
3219
3220/* Tell the target what to do with an ongoing tracing run if GDB
3221 disconnects for some reason. */
3222
cf7f2e2d
JM
3223static void
3224set_disconnected_tracing (char *args, int from_tty,
3225 struct cmd_list_element *c)
3226{
a45ae5f8 3227 target_set_disconnected_tracing (disconnected_tracing);
cf7f2e2d
JM
3228}
3229
3230static void
3231set_circular_trace_buffer (char *args, int from_tty,
3232 struct cmd_list_element *c)
3233{
3234 target_set_circular_trace_buffer (circular_trace_buffer);
3235}
3236
ef5ccd6c
JM
3237static void
3238set_trace_buffer_size (char *args, int from_tty,
3239 struct cmd_list_element *c)
3240{
3241 target_set_trace_buffer_size (trace_buffer_size);
3242}
3243
a45ae5f8
JM
3244static void
3245set_trace_user (char *args, int from_tty,
3246 struct cmd_list_element *c)
3247{
3248 int ret;
3249
3250 ret = target_set_trace_notes (trace_user, NULL, NULL);
3251
3252 if (!ret)
ef5ccd6c 3253 warning (_("Target does not support trace notes, user ignored"));
a45ae5f8
JM
3254}
3255
3256static void
3257set_trace_notes (char *args, int from_tty,
3258 struct cmd_list_element *c)
3259{
3260 int ret;
3261
3262 ret = target_set_trace_notes (NULL, trace_notes, NULL);
3263
3264 if (!ret)
ef5ccd6c 3265 warning (_("Target does not support trace notes, note ignored"));
a45ae5f8
JM
3266}
3267
3268static void
3269set_trace_stop_notes (char *args, int from_tty,
3270 struct cmd_list_element *c)
3271{
3272 int ret;
3273
3274 ret = target_set_trace_notes (NULL, NULL, trace_stop_notes);
3275
3276 if (!ret)
ef5ccd6c 3277 warning (_("Target does not support trace notes, stop note ignored"));
a45ae5f8
JM
3278}
3279
cf7f2e2d
JM
3280/* Convert the memory pointed to by mem into hex, placing result in buf.
3281 * Return a pointer to the last char put in buf (null)
3282 * "stolen" from sparc-stub.c
3283 */
3284
3285static const char hexchars[] = "0123456789abcdef";
3286
3287static char *
3288mem2hex (gdb_byte *mem, char *buf, int count)
3289{
3290 gdb_byte ch;
3291
3292 while (count-- > 0)
3293 {
3294 ch = *mem++;
3295
3296 *buf++ = hexchars[ch >> 4];
3297 *buf++ = hexchars[ch & 0xf];
3298 }
3299
3300 *buf = 0;
3301
3302 return buf;
3303}
3304
3305int
3306get_traceframe_number (void)
3307{
3308 return traceframe_number;
3309}
3310
3311/* Make the traceframe NUM be the current trace frame. Does nothing
3312 if NUM is already current. */
3313
3314void
c50c785c 3315set_current_traceframe (int num)
cf7f2e2d
JM
3316{
3317 int newnum;
3318
3319 if (traceframe_number == num)
3320 {
3321 /* Nothing to do. */
3322 return;
3323 }
3324
3325 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
3326
3327 if (newnum != num)
3328 warning (_("could not change traceframe"));
3329
ef5ccd6c 3330 set_traceframe_num (newnum);
cf7f2e2d
JM
3331
3332 /* Changing the traceframe changes our view of registers and of the
3333 frame chain. */
3334 registers_changed ();
c50c785c
JM
3335
3336 clear_traceframe_info ();
3337}
3338
3339/* Make the traceframe NUM be the current trace frame, and do nothing
3340 more. */
3341
3342void
3343set_traceframe_number (int num)
3344{
3345 traceframe_number = num;
cf7f2e2d
JM
3346}
3347
3348/* A cleanup used when switching away and back from tfind mode. */
3349
3350struct current_traceframe_cleanup
3351{
3352 /* The traceframe we were inspecting. */
3353 int traceframe_number;
3354};
3355
3356static void
3357do_restore_current_traceframe_cleanup (void *arg)
3358{
3359 struct current_traceframe_cleanup *old = arg;
3360
c50c785c 3361 set_current_traceframe (old->traceframe_number);
cf7f2e2d
JM
3362}
3363
3364static void
3365restore_current_traceframe_cleanup_dtor (void *arg)
3366{
3367 struct current_traceframe_cleanup *old = arg;
3368
3369 xfree (old);
3370}
3371
3372struct cleanup *
3373make_cleanup_restore_current_traceframe (void)
3374{
3375 struct current_traceframe_cleanup *old;
3376
3377 old = xmalloc (sizeof (struct current_traceframe_cleanup));
3378 old->traceframe_number = traceframe_number;
3379
3380 return make_cleanup_dtor (do_restore_current_traceframe_cleanup, old,
3381 restore_current_traceframe_cleanup_dtor);
3382}
3383
c50c785c
JM
3384struct cleanup *
3385make_cleanup_restore_traceframe_number (void)
3386{
3387 return make_cleanup_restore_integer (&traceframe_number);
3388}
3389
cf7f2e2d
JM
3390/* Given a number and address, return an uploaded tracepoint with that
3391 number, creating if necessary. */
3392
3393struct uploaded_tp *
3394get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
3395{
3396 struct uploaded_tp *utp;
3397
3398 for (utp = *utpp; utp; utp = utp->next)
3399 if (utp->number == num && utp->addr == addr)
3400 return utp;
3401 utp = (struct uploaded_tp *) xmalloc (sizeof (struct uploaded_tp));
3402 memset (utp, 0, sizeof (struct uploaded_tp));
3403 utp->number = num;
3404 utp->addr = addr;
3405 utp->actions = NULL;
3406 utp->step_actions = NULL;
3407 utp->cmd_strings = NULL;
3408 utp->next = *utpp;
3409 *utpp = utp;
3410 return utp;
3411}
3412
3413static void
3414free_uploaded_tps (struct uploaded_tp **utpp)
3415{
3416 struct uploaded_tp *next_one;
3417
3418 while (*utpp)
3419 {
3420 next_one = (*utpp)->next;
3421 xfree (*utpp);
3422 *utpp = next_one;
3423 }
3424}
3425
3426/* Given a number and address, return an uploaded tracepoint with that
3427 number, creating if necessary. */
3428
ef5ccd6c 3429static struct uploaded_tsv *
cf7f2e2d
JM
3430get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
3431{
3432 struct uploaded_tsv *utsv;
3433
3434 for (utsv = *utsvp; utsv; utsv = utsv->next)
3435 if (utsv->number == num)
3436 return utsv;
3437 utsv = (struct uploaded_tsv *) xmalloc (sizeof (struct uploaded_tsv));
3438 memset (utsv, 0, sizeof (struct uploaded_tsv));
3439 utsv->number = num;
3440 utsv->next = *utsvp;
3441 *utsvp = utsv;
3442 return utsv;
3443}
3444
3445static void
3446free_uploaded_tsvs (struct uploaded_tsv **utsvp)
3447{