]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/tracepoint.c
New python script adding pretty printers for types defined in GDB.
[thirdparty/binutils-gdb.git] / gdb / tracepoint.c
CommitLineData
c906108c 1/* Tracing functionality for remote targets in custom GDB protocol
9f60d481 2
6aba47ca 3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4c38e0a4 4 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
c5aa993b 11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b 18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "defs.h"
5af949e3 22#include "arch-utils.h"
c906108c
SS
23#include "symtab.h"
24#include "frame.h"
c906108c
SS
25#include "gdbtypes.h"
26#include "expression.h"
27#include "gdbcmd.h"
28#include "value.h"
29#include "target.h"
30#include "language.h"
31#include "gdb_string.h"
104c1213 32#include "inferior.h"
1042e4c0 33#include "breakpoint.h"
104c1213 34#include "tracepoint.h"
c2c6d25f 35#include "remote.h"
782b2b07 36extern int remote_supports_cond_tracepoints (void);
f61e138d 37extern char *unpack_varlen_hex (char *buff, ULONGEST *result);
c5f0f3d0 38#include "linespec.h"
4e052eda 39#include "regcache.h"
c94fdfd0 40#include "completer.h"
fe898f56 41#include "block.h"
de4f826b 42#include "dictionary.h"
383f836e 43#include "observer.h"
029a67e4 44#include "user-regs.h"
79a45b7d 45#include "valprint.h"
41575630 46#include "gdbcore.h"
768a979c 47#include "objfiles.h"
c906108c
SS
48
49#include "ax.h"
50#include "ax-gdb.h"
51
52/* readline include files */
dbda9972
AC
53#include "readline/readline.h"
54#include "readline/history.h"
c906108c
SS
55
56/* readline defines this. */
57#undef savestring
58
59#ifdef HAVE_UNISTD_H
60#include <unistd.h>
61#endif
62
d183932d
MS
63/* Maximum length of an agent aexpression.
64 This accounts for the fact that packets are limited to 400 bytes
c906108c
SS
65 (which includes everything -- including the checksum), and assumes
66 the worst case of maximum length for each of the pieces of a
67 continuation packet.
c5aa993b 68
c906108c
SS
69 NOTE: expressions get mem2hex'ed otherwise this would be twice as
70 large. (400 - 31)/2 == 184 */
71#define MAX_AGENT_EXPR_LEN 184
72
98c5b216
TT
73/* A hook used to notify the UI of tracepoint operations. */
74
75void (*deprecated_trace_find_hook) (char *arg, int from_tty);
76void (*deprecated_trace_start_stop_hook) (int start, int from_tty);
c906108c 77
9a4105ab
AC
78extern void (*deprecated_readline_begin_hook) (char *, ...);
79extern char *(*deprecated_readline_hook) (char *);
80extern void (*deprecated_readline_end_hook) (void);
c906108c 81
104c1213
JM
82/* GDB commands implemented in other modules:
83 */
84
a14ed312 85extern void output_command (char *, int);
104c1213 86
c906108c
SS
87/*
88 Tracepoint.c:
89
90 This module defines the following debugger commands:
91 trace : set a tracepoint on a function, line, or address.
92 info trace : list all debugger-defined tracepoints.
93 delete trace : delete one or more tracepoints.
94 enable trace : enable one or more tracepoints.
95 disable trace : disable one or more tracepoints.
96 actions : specify actions to be taken at a tracepoint.
97 passcount : specify a pass count for a tracepoint.
98 tstart : start a trace experiment.
99 tstop : stop a trace experiment.
100 tstatus : query the status of a trace experiment.
101 tfind : find a trace frame in the trace buffer.
102 tdump : print everything collected at the current tracepoint.
103 save-tracepoints : write tracepoint setup into a file.
104
105 This module defines the following user-visible debugger variables:
106 $trace_frame : sequence number of trace frame currently being debugged.
107 $trace_line : source line of trace frame currently being debugged.
108 $trace_file : source file of trace frame currently being debugged.
109 $tracepoint : tracepoint number of trace frame currently being debugged.
c5aa993b 110 */
c906108c
SS
111
112
113/* ======= Important global variables: ======= */
114
f61e138d
SS
115/* The list of all trace state variables. We don't retain pointers to
116 any of these for any reason - API is by name or number only - so it
117 works to have a vector of objects. */
118
119typedef struct trace_state_variable tsv_s;
120DEF_VEC_O(tsv_s);
121
122static VEC(tsv_s) *tvariables;
123
124/* The next integer to assign to a variable. */
125
126static int next_tsv_number = 1;
127
c906108c
SS
128/* Number of last traceframe collected. */
129static int traceframe_number;
130
131/* Tracepoint for last traceframe collected. */
132static int tracepoint_number;
133
134/* Symbol for function for last traceframe collected */
135static struct symbol *traceframe_fun;
136
137/* Symtab and line for last traceframe collected */
138static struct symtab_and_line traceframe_sal;
139
140/* Tracing command lists */
141static struct cmd_list_element *tfindlist;
142
236f1d4d
SS
143/* List of expressions to collect by default at each tracepoint hit. */
144static char *default_collect = "";
145
f61e138d
SS
146static char *target_buf;
147static long target_buf_size;
236f1d4d 148
c906108c 149/* ======= Important command functions: ======= */
a14ed312
KB
150static void trace_actions_command (char *, int);
151static void trace_start_command (char *, int);
152static void trace_stop_command (char *, int);
153static void trace_status_command (char *, int);
154static void trace_find_command (char *, int);
155static void trace_find_pc_command (char *, int);
156static void trace_find_tracepoint_command (char *, int);
157static void trace_find_line_command (char *, int);
158static void trace_find_range_command (char *, int);
159static void trace_find_outside_command (char *, int);
160static void tracepoint_save_command (char *, int);
161static void trace_dump_command (char *, int);
c906108c
SS
162
163/* support routines */
c906108c
SS
164
165struct collection_list;
a14ed312 166static void add_aexpr (struct collection_list *, struct agent_expr *);
47b667de 167static char *mem2hex (gdb_byte *, char *, int);
a14ed312
KB
168static void add_register (struct collection_list *collection,
169 unsigned int regno);
1042e4c0 170static struct cleanup *make_cleanup_free_actions (struct breakpoint *t);
a14ed312
KB
171static void free_actions_list (char **actions_list);
172static void free_actions_list_cleanup_wrapper (void *);
392a587b 173
a14ed312 174extern void _initialize_tracepoint (void);
c906108c
SS
175
176/* Utility: returns true if "target remote" */
177static int
fba45db2 178target_is_remote (void)
c906108c
SS
179{
180 if (current_target.to_shortname &&
549678da
NS
181 (strcmp (current_target.to_shortname, "remote") == 0
182 || strcmp (current_target.to_shortname, "extended-remote") == 0))
c906108c
SS
183 return 1;
184 else
185 return 0;
186}
187
188/* Utility: generate error from an incoming stub packet. */
c5aa993b 189static void
fba45db2 190trace_error (char *buf)
c906108c
SS
191{
192 if (*buf++ != 'E')
193 return; /* not an error msg */
c5aa993b 194 switch (*buf)
c906108c
SS
195 {
196 case '1': /* malformed packet error */
197 if (*++buf == '0') /* general case: */
8a3fe4f8 198 error (_("tracepoint.c: error in outgoing packet."));
c906108c 199 else
8a3fe4f8 200 error (_("tracepoint.c: error in outgoing packet at field #%ld."),
c906108c
SS
201 strtol (buf, NULL, 16));
202 case '2':
8a3fe4f8 203 error (_("trace API error 0x%s."), ++buf);
c906108c 204 default:
8a3fe4f8 205 error (_("Target returns error code '%s'."), buf);
c906108c
SS
206 }
207}
208
d183932d 209/* Utility: wait for reply from stub, while accepting "O" packets. */
c906108c 210static char *
6d820c5c
DJ
211remote_get_noisy_reply (char **buf_p,
212 long *sizeof_buf)
c906108c 213{
d183932d 214 do /* Loop on reply from remote stub. */
c906108c 215 {
6d820c5c 216 char *buf;
c5aa993b 217 QUIT; /* allow user to bail out with ^C */
6d820c5c
DJ
218 getpkt (buf_p, sizeof_buf, 0);
219 buf = *buf_p;
c906108c 220 if (buf[0] == 0)
8a3fe4f8 221 error (_("Target does not support this command."));
c906108c
SS
222 else if (buf[0] == 'E')
223 trace_error (buf);
224 else if (buf[0] == 'O' &&
225 buf[1] != 'K')
226 remote_console_output (buf + 1); /* 'O' message from stub */
227 else
c5aa993b
JM
228 return buf; /* here's the actual reply */
229 }
230 while (1);
c906108c
SS
231}
232
c906108c
SS
233/* Set traceframe number to NUM. */
234static void
fba45db2 235set_traceframe_num (int num)
c906108c
SS
236{
237 traceframe_number = num;
4fa62494 238 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
c906108c
SS
239}
240
241/* Set tracepoint number to NUM. */
242static void
fba45db2 243set_tracepoint_num (int num)
c906108c
SS
244{
245 tracepoint_number = num;
4fa62494 246 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
c906108c
SS
247}
248
249/* Set externally visible debug variables for querying/printing
250 the traceframe context (line, function, file) */
251
252static void
fb14de7b 253set_traceframe_context (struct frame_info *trace_frame)
c906108c 254{
fb14de7b
UW
255 CORE_ADDR trace_pc;
256
fb14de7b 257 if (trace_frame == NULL) /* Cease debugging any trace buffers. */
c906108c
SS
258 {
259 traceframe_fun = 0;
260 traceframe_sal.pc = traceframe_sal.line = 0;
261 traceframe_sal.symtab = NULL;
4fa62494
UW
262 clear_internalvar (lookup_internalvar ("trace_func"));
263 clear_internalvar (lookup_internalvar ("trace_file"));
264 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
c906108c
SS
265 return;
266 }
267
d183932d 268 /* Save as globals for internal use. */
fb14de7b 269 trace_pc = get_frame_pc (trace_frame);
c906108c
SS
270 traceframe_sal = find_pc_line (trace_pc, 0);
271 traceframe_fun = find_pc_function (trace_pc);
272
d183932d
MS
273 /* Save linenumber as "$trace_line", a debugger variable visible to
274 users. */
4fa62494
UW
275 set_internalvar_integer (lookup_internalvar ("trace_line"),
276 traceframe_sal.line);
c906108c 277
d183932d
MS
278 /* Save func name as "$trace_func", a debugger variable visible to
279 users. */
4fa62494
UW
280 if (traceframe_fun == NULL
281 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
282 clear_internalvar (lookup_internalvar ("trace_func"));
c906108c 283 else
78267919
UW
284 set_internalvar_string (lookup_internalvar ("trace_func"),
285 SYMBOL_LINKAGE_NAME (traceframe_fun));
c906108c 286
d183932d
MS
287 /* Save file name as "$trace_file", a debugger variable visible to
288 users. */
4fa62494
UW
289 if (traceframe_sal.symtab == NULL
290 || traceframe_sal.symtab->filename == NULL)
291 clear_internalvar (lookup_internalvar ("trace_file"));
c906108c 292 else
78267919
UW
293 set_internalvar_string (lookup_internalvar ("trace_file"),
294 traceframe_sal.symtab->filename);
c906108c
SS
295}
296
f61e138d
SS
297/* Create a new trace state variable with the given name. */
298
299struct trace_state_variable *
300create_trace_state_variable (const char *name)
301{
302 struct trace_state_variable tsv;
303
304 memset (&tsv, 0, sizeof (tsv));
305 tsv.name = name;
306 tsv.number = next_tsv_number++;
307 return VEC_safe_push (tsv_s, tvariables, &tsv);
308}
309
310/* Look for a trace state variable of the given name. */
311
312struct trace_state_variable *
313find_trace_state_variable (const char *name)
314{
315 struct trace_state_variable *tsv;
316 int ix;
317
318 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
319 if (strcmp (name, tsv->name) == 0)
320 return tsv;
321
322 return NULL;
323}
324
325void
326delete_trace_state_variable (const char *name)
327{
328 struct trace_state_variable *tsv;
329 int ix;
330
331 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
332 if (strcmp (name, tsv->name) == 0)
333 {
334 VEC_unordered_remove (tsv_s, tvariables, ix);
335 return;
336 }
337
338 warning (_("No trace variable named \"$%s\", not deleting"), name);
339}
340
341/* The 'tvariable' command collects a name and optional expression to
342 evaluate into an initial value. */
343
344void
345trace_variable_command (char *args, int from_tty)
346{
347 struct expression *expr;
348 struct cleanup *old_chain;
349 struct internalvar *intvar = NULL;
350 LONGEST initval = 0;
351 struct trace_state_variable *tsv;
352
353 if (!args || !*args)
354 error_no_arg (_("trace state variable name"));
355
356 /* All the possible valid arguments are expressions. */
357 expr = parse_expression (args);
358 old_chain = make_cleanup (free_current_contents, &expr);
359
360 if (expr->nelts == 0)
361 error (_("No expression?"));
362
363 /* Only allow two syntaxes; "$name" and "$name=value". */
364 if (expr->elts[0].opcode == OP_INTERNALVAR)
365 {
366 intvar = expr->elts[1].internalvar;
367 }
368 else if (expr->elts[0].opcode == BINOP_ASSIGN
369 && expr->elts[1].opcode == OP_INTERNALVAR)
370 {
371 intvar = expr->elts[2].internalvar;
372 initval = value_as_long (evaluate_subexpression_type (expr, 4));
373 }
374 else
375 error (_("Syntax must be $NAME [ = EXPR ]"));
376
377 if (!intvar)
378 error (_("No name given"));
379
380 if (strlen (internalvar_name (intvar)) <= 0)
381 error (_("Must supply a non-empty variable name"));
382
383 /* If the variable already exists, just change its initial value. */
384 tsv = find_trace_state_variable (internalvar_name (intvar));
385 if (tsv)
386 {
387 tsv->initial_value = initval;
388 printf_filtered (_("Trace state variable $%s now has initial value %s.\n"),
389 tsv->name, plongest (tsv->initial_value));
390 return;
391 }
392
393 /* Create a new variable. */
394 tsv = create_trace_state_variable (internalvar_name (intvar));
395 tsv->initial_value = initval;
396
397 printf_filtered (_("Trace state variable $%s created, with initial value %s.\n"),
398 tsv->name, plongest (tsv->initial_value));
399
400 do_cleanups (old_chain);
401}
402
403void
404delete_trace_variable_command (char *args, int from_tty)
405{
406 int i, ix;
407 char **argv;
408 struct cleanup *back_to;
409 struct trace_state_variable *tsv;
410
411 if (args == NULL)
412 {
413 if (query (_("Delete all trace state variables? ")))
414 VEC_free (tsv_s, tvariables);
415 dont_repeat ();
416 return;
417 }
418
419 argv = gdb_buildargv (args);
420 back_to = make_cleanup_freeargv (argv);
421
422 for (i = 0; argv[i] != NULL; i++)
423 {
424 if (*argv[i] == '$')
425 delete_trace_state_variable (argv[i] + 1);
426 else
427 warning (_("Name \"%s\" not prefixed with '$', ignoring"), argv[i]);
428 }
429
430 do_cleanups (back_to);
431
432 dont_repeat ();
433}
434
435/* List all the trace state variables. */
436
437static void
438tvariables_info (char *args, int from_tty)
439{
440 struct trace_state_variable *tsv;
441 int ix;
442 char *reply;
443 ULONGEST tval;
444
445 if (target_is_remote ())
446 {
447 char buf[20];
448
449 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
450 {
451 /* We don't know anything about the value until we get a
452 valid packet. */
453 tsv->value_known = 0;
454 sprintf (buf, "qTV:%x", tsv->number);
455 putpkt (buf);
456 reply = remote_get_noisy_reply (&target_buf, &target_buf_size);
457 if (reply && *reply)
458 {
459 if (*reply == 'V')
460 {
461 unpack_varlen_hex (reply + 1, &tval);
462 tsv->value = (LONGEST) tval;
463 tsv->value_known = 1;
464 }
465 /* FIXME say anything about oddball replies? */
466 }
467 }
468 }
469
470 if (VEC_length (tsv_s, tvariables) == 0)
471 {
472 printf_filtered (_("No trace state variables.\n"));
473 return;
474 }
475
476 printf_filtered (_("Name\t\t Initial\tCurrent\n"));
477
478 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
479 {
480 printf_filtered ("$%s", tsv->name);
481 print_spaces_filtered (17 - strlen (tsv->name), gdb_stdout);
482 printf_filtered ("%s ", plongest (tsv->initial_value));
483 print_spaces_filtered (11 - strlen (plongest (tsv->initial_value)), gdb_stdout);
484 if (tsv->value_known)
485 printf_filtered (" %s", plongest (tsv->value));
486 else if (trace_running_p || traceframe_number >= 0)
487 /* The value is/was defined, but we don't have it. */
488 printf_filtered (_(" <unknown>"));
489 else
490 /* It is not meaningful to ask about the value. */
491 printf_filtered (_(" <undefined>"));
492 printf_filtered ("\n");
493 }
494}
495
c906108c
SS
496/* ACTIONS functions: */
497
498/* Prototypes for action-parsing utility commands */
1042e4c0 499static void read_actions (struct breakpoint *);
c906108c
SS
500
501/* The three functions:
c5aa993b
JM
502 collect_pseudocommand,
503 while_stepping_pseudocommand, and
504 end_actions_pseudocommand
c906108c
SS
505 are placeholders for "commands" that are actually ONLY to be used
506 within a tracepoint action list. If the actual function is ever called,
507 it means that somebody issued the "command" at the top level,
508 which is always an error. */
509
1042e4c0 510void
fba45db2 511end_actions_pseudocommand (char *args, int from_tty)
c906108c 512{
8a3fe4f8 513 error (_("This command cannot be used at the top level."));
c906108c
SS
514}
515
1042e4c0 516void
fba45db2 517while_stepping_pseudocommand (char *args, int from_tty)
c906108c 518{
8a3fe4f8 519 error (_("This command can only be used in a tracepoint actions list."));
c906108c
SS
520}
521
522static void
fba45db2 523collect_pseudocommand (char *args, int from_tty)
c906108c 524{
8a3fe4f8 525 error (_("This command can only be used in a tracepoint actions list."));
c906108c
SS
526}
527
6da95a67
SS
528static void
529teval_pseudocommand (char *args, int from_tty)
530{
531 error (_("This command can only be used in a tracepoint actions list."));
532}
533
c906108c
SS
534/* Enter a list of actions for a tracepoint. */
535static void
fba45db2 536trace_actions_command (char *args, int from_tty)
c906108c 537{
1042e4c0 538 struct breakpoint *t;
c906108c
SS
539 char tmpbuf[128];
540 char *end_msg = "End with a line saying just \"end\".";
541
c2d11a7d 542 t = get_tracepoint_by_number (&args, 0, 1);
7a292a7a 543 if (t)
c906108c
SS
544 {
545 sprintf (tmpbuf, "Enter actions for tracepoint %d, one per line.",
546 t->number);
547
548 if (from_tty)
549 {
9a4105ab
AC
550 if (deprecated_readline_begin_hook)
551 (*deprecated_readline_begin_hook) ("%s %s\n", tmpbuf, end_msg);
c906108c
SS
552 else if (input_from_terminal_p ())
553 printf_filtered ("%s\n%s\n", tmpbuf, end_msg);
554 }
555
556 free_actions (t);
557 t->step_count = 0; /* read_actions may set this */
558 read_actions (t);
559
9a4105ab
AC
560 if (deprecated_readline_end_hook)
561 (*deprecated_readline_end_hook) ();
c906108c
SS
562 /* tracepoints_changed () */
563 }
5c44784c 564 /* else just return */
c906108c
SS
565}
566
567/* worker function */
568static void
1042e4c0 569read_actions (struct breakpoint *t)
c906108c
SS
570{
571 char *line;
572 char *prompt1 = "> ", *prompt2 = " > ";
573 char *prompt = prompt1;
574 enum actionline_type linetype;
575 extern FILE *instream;
576 struct action_line *next = NULL, *temp;
577 struct cleanup *old_chain;
578
579 /* Control-C quits instantly if typed while in this loop
580 since it should not wait until the user types a newline. */
581 immediate_quit++;
1a6fae3c
MK
582 /* FIXME: kettenis/20010823: Something is wrong here. In this file
583 STOP_SIGNAL is never defined. So this code has been left out, at
584 least for quite a while now. Replacing STOP_SIGNAL with SIGTSTP
585 leads to compilation failures since the variable job_control
586 isn't declared. Leave this alone for now. */
c906108c
SS
587#ifdef STOP_SIGNAL
588 if (job_control)
362646f5 589 signal (STOP_SIGNAL, handle_stop_sig);
c906108c 590#endif
74b7792f 591 old_chain = make_cleanup_free_actions (t);
c906108c
SS
592 while (1)
593 {
d183932d
MS
594 /* Make sure that all output has been output. Some machines may
595 let you get away with leaving out some of the gdb_flush, but
596 not all. */
c906108c
SS
597 wrap_here ("");
598 gdb_flush (gdb_stdout);
599 gdb_flush (gdb_stderr);
600
9a4105ab
AC
601 if (deprecated_readline_hook && instream == NULL)
602 line = (*deprecated_readline_hook) (prompt);
c906108c
SS
603 else if (instream == stdin && ISATTY (instream))
604 {
b4f5539f 605 line = gdb_readline_wrapper (prompt);
c5aa993b 606 if (line && *line) /* add it to command history */
c906108c
SS
607 add_history (line);
608 }
609 else
610 line = gdb_readline (0);
611
549678da 612 if (!line)
d844e34b
JB
613 {
614 line = xstrdup ("end");
615 printf_filtered ("end\n");
616 }
549678da 617
c906108c
SS
618 linetype = validate_actionline (&line, t);
619 if (linetype == BADLINE)
c5aa993b 620 continue; /* already warned -- collect another line */
c906108c
SS
621
622 temp = xmalloc (sizeof (struct action_line));
623 temp->next = NULL;
624 temp->action = line;
625
626 if (next == NULL) /* first action for this tracepoint? */
627 t->actions = next = temp;
628 else
629 {
630 next->next = temp;
631 next = temp;
632 }
633
634 if (linetype == STEPPING) /* begin "while-stepping" */
7a292a7a
SS
635 {
636 if (prompt == prompt2)
637 {
8a3fe4f8 638 warning (_("Already processing 'while-stepping'"));
7a292a7a
SS
639 continue;
640 }
641 else
642 prompt = prompt2; /* change prompt for stepping actions */
643 }
c906108c 644 else if (linetype == END)
7a292a7a
SS
645 {
646 if (prompt == prompt2)
647 {
648 prompt = prompt1; /* end of single-stepping actions */
649 }
650 else
c5aa993b 651 { /* end of actions */
7a292a7a
SS
652 if (t->actions->next == NULL)
653 {
d183932d
MS
654 /* An "end" all by itself with no other actions
655 means this tracepoint has no actions.
656 Discard empty list. */
7a292a7a
SS
657 free_actions (t);
658 }
659 break;
660 }
661 }
c906108c
SS
662 }
663#ifdef STOP_SIGNAL
664 if (job_control)
665 signal (STOP_SIGNAL, SIG_DFL);
666#endif
8edbea78 667 immediate_quit--;
c906108c
SS
668 discard_cleanups (old_chain);
669}
670
671/* worker function */
672enum actionline_type
1042e4c0 673validate_actionline (char **line, struct breakpoint *t)
c906108c
SS
674{
675 struct cmd_list_element *c;
676 struct expression *exp = NULL;
c906108c
SS
677 struct cleanup *old_chain = NULL;
678 char *p;
679
15255275
MS
680 /* if EOF is typed, *line is NULL */
681 if (*line == NULL)
682 return END;
683
104c1213 684 for (p = *line; isspace ((int) *p);)
c906108c
SS
685 p++;
686
d183932d
MS
687 /* Symbol lookup etc. */
688 if (*p == '\0') /* empty line: just prompt for another line. */
c906108c
SS
689 return BADLINE;
690
c5aa993b 691 if (*p == '#') /* comment line */
c906108c
SS
692 return GENERIC;
693
694 c = lookup_cmd (&p, cmdlist, "", -1, 1);
695 if (c == 0)
696 {
8a3fe4f8 697 warning (_("'%s' is not an action that I know, or is ambiguous."),
d183932d 698 p);
c906108c
SS
699 return BADLINE;
700 }
c5aa993b 701
bbaca940 702 if (cmd_cfunc_eq (c, collect_pseudocommand))
c906108c
SS
703 {
704 struct agent_expr *aexpr;
705 struct agent_reqs areqs;
706
c5aa993b
JM
707 do
708 { /* repeat over a comma-separated list */
709 QUIT; /* allow user to bail out with ^C */
104c1213 710 while (isspace ((int) *p))
c5aa993b 711 p++;
c906108c 712
c5aa993b
JM
713 if (*p == '$') /* look for special pseudo-symbols */
714 {
c5aa993b
JM
715 if ((0 == strncasecmp ("reg", p + 1, 3)) ||
716 (0 == strncasecmp ("arg", p + 1, 3)) ||
717 (0 == strncasecmp ("loc", p + 1, 3)))
718 {
719 p = strchr (p, ',');
720 continue;
721 }
d183932d 722 /* else fall thru, treat p as an expression and parse it! */
c5aa993b 723 }
1042e4c0 724 exp = parse_exp_1 (&p, block_for_pc (t->loc->address), 1);
c13c43fd 725 old_chain = make_cleanup (free_current_contents, &exp);
c906108c 726
c5aa993b
JM
727 if (exp->elts[0].opcode == OP_VAR_VALUE)
728 {
729 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
730 {
8a3fe4f8 731 warning (_("constant %s (value %ld) will not be collected."),
3567439c 732 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
c5aa993b
JM
733 SYMBOL_VALUE (exp->elts[2].symbol));
734 return BADLINE;
735 }
736 else if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_OPTIMIZED_OUT)
737 {
8a3fe4f8 738 warning (_("%s is optimized away and cannot be collected."),
3567439c 739 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
c5aa993b
JM
740 return BADLINE;
741 }
742 }
c906108c 743
d183932d
MS
744 /* We have something to collect, make sure that the expr to
745 bytecode translator can handle it and that it's not too
746 long. */
1042e4c0 747 aexpr = gen_trace_for_expr (t->loc->address, exp);
f23d52e0 748 make_cleanup_free_agent_expr (aexpr);
c906108c 749
c5aa993b 750 if (aexpr->len > MAX_AGENT_EXPR_LEN)
8a3fe4f8 751 error (_("expression too complicated, try simplifying"));
c906108c 752
c5aa993b 753 ax_reqs (aexpr, &areqs);
b8c9b27d 754 (void) make_cleanup (xfree, areqs.reg_mask);
c906108c 755
c5aa993b 756 if (areqs.flaw != agent_flaw_none)
8a3fe4f8 757 error (_("malformed expression"));
c906108c 758
c5aa993b 759 if (areqs.min_height < 0)
8a3fe4f8 760 error (_("gdb: Internal error: expression has min height < 0"));
c906108c 761
c5aa993b 762 if (areqs.max_height > 20)
8a3fe4f8 763 error (_("expression too complicated, try simplifying"));
c906108c 764
c5aa993b
JM
765 do_cleanups (old_chain);
766 }
767 while (p && *p++ == ',');
c906108c
SS
768 return GENERIC;
769 }
6da95a67
SS
770 else if (cmd_cfunc_eq (c, teval_pseudocommand))
771 {
772 struct agent_expr *aexpr;
773
774 do
775 { /* repeat over a comma-separated list */
776 QUIT; /* allow user to bail out with ^C */
777 while (isspace ((int) *p))
778 p++;
779
780 /* Only expressions are allowed for this action. */
781 exp = parse_exp_1 (&p, block_for_pc (t->loc->address), 1);
782 old_chain = make_cleanup (free_current_contents, &exp);
783
784 /* We have something to evaluate, make sure that the expr to
785 bytecode translator can handle it and that it's not too
786 long. */
787 aexpr = gen_eval_for_expr (t->loc->address, exp);
788 make_cleanup_free_agent_expr (aexpr);
789
790 if (aexpr->len > MAX_AGENT_EXPR_LEN)
791 error (_("expression too complicated, try simplifying"));
792
793 do_cleanups (old_chain);
794 }
795 while (p && *p++ == ',');
796 return GENERIC;
797 }
bbaca940 798 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
c906108c 799 {
c5aa993b 800 char *steparg; /* in case warning is necessary */
c906108c 801
104c1213 802 while (isspace ((int) *p))
c906108c
SS
803 p++;
804 steparg = p;
805
806 if (*p == '\0' ||
807 (t->step_count = strtol (p, &p, 0)) == 0)
808 {
8a3fe4f8 809 warning (_("'%s': bad step-count; command ignored."), *line);
c906108c
SS
810 return BADLINE;
811 }
812 return STEPPING;
813 }
bbaca940 814 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
c906108c
SS
815 return END;
816 else
817 {
8a3fe4f8 818 warning (_("'%s' is not a supported tracepoint action."), *line);
c906108c
SS
819 return BADLINE;
820 }
821}
822
823/* worker function */
c5aa993b 824void
1042e4c0 825free_actions (struct breakpoint *t)
c906108c
SS
826{
827 struct action_line *line, *next;
828
829 for (line = t->actions; line; line = next)
830 {
831 next = line->next;
c5aa993b 832 if (line->action)
b8c9b27d
KB
833 xfree (line->action);
834 xfree (line);
c906108c
SS
835 }
836 t->actions = NULL;
837}
838
74b7792f
AC
839static void
840do_free_actions_cleanup (void *t)
841{
842 free_actions (t);
843}
844
845static struct cleanup *
1042e4c0 846make_cleanup_free_actions (struct breakpoint *t)
74b7792f
AC
847{
848 return make_cleanup (do_free_actions_cleanup, t);
849}
850
f50e79a4
JB
851enum {
852 memrange_absolute = -1
853};
854
c5aa993b
JM
855struct memrange
856{
f50e79a4
JB
857 int type; /* memrange_absolute for absolute memory range,
858 else basereg number */
c906108c
SS
859 bfd_signed_vma start;
860 bfd_signed_vma end;
861};
862
c5aa993b
JM
863struct collection_list
864 {
549678da 865 unsigned char regs_mask[32]; /* room for up to 256 regs */
c5aa993b
JM
866 long listsize;
867 long next_memrange;
868 struct memrange *list;
869 long aexpr_listsize; /* size of array pointed to by expr_list elt */
870 long next_aexpr_elt;
871 struct agent_expr **aexpr_list;
872
873 }
874tracepoint_list, stepping_list;
c906108c
SS
875
876/* MEMRANGE functions: */
877
a14ed312 878static int memrange_cmp (const void *, const void *);
c906108c
SS
879
880/* compare memranges for qsort */
881static int
fba45db2 882memrange_cmp (const void *va, const void *vb)
c906108c
SS
883{
884 const struct memrange *a = va, *b = vb;
885
886 if (a->type < b->type)
887 return -1;
888 if (a->type > b->type)
c5aa993b 889 return 1;
f50e79a4 890 if (a->type == memrange_absolute)
c906108c 891 {
c5aa993b
JM
892 if ((bfd_vma) a->start < (bfd_vma) b->start)
893 return -1;
894 if ((bfd_vma) a->start > (bfd_vma) b->start)
895 return 1;
c906108c
SS
896 }
897 else
898 {
c5aa993b 899 if (a->start < b->start)
c906108c 900 return -1;
c5aa993b
JM
901 if (a->start > b->start)
902 return 1;
c906108c
SS
903 }
904 return 0;
905}
906
d183932d 907/* Sort the memrange list using qsort, and merge adjacent memranges. */
c906108c 908static void
fba45db2 909memrange_sortmerge (struct collection_list *memranges)
c906108c
SS
910{
911 int a, b;
912
c5aa993b 913 qsort (memranges->list, memranges->next_memrange,
c906108c
SS
914 sizeof (struct memrange), memrange_cmp);
915 if (memranges->next_memrange > 0)
916 {
917 for (a = 0, b = 1; b < memranges->next_memrange; b++)
918 {
919 if (memranges->list[a].type == memranges->list[b].type &&
c5aa993b 920 memranges->list[b].start - memranges->list[a].end <=
0c92afe8 921 MAX_REGISTER_SIZE)
c906108c
SS
922 {
923 /* memrange b starts before memrange a ends; merge them. */
924 if (memranges->list[b].end > memranges->list[a].end)
925 memranges->list[a].end = memranges->list[b].end;
926 continue; /* next b, same a */
927 }
928 a++; /* next a */
929 if (a != b)
c5aa993b 930 memcpy (&memranges->list[a], &memranges->list[b],
c906108c
SS
931 sizeof (struct memrange));
932 }
933 memranges->next_memrange = a + 1;
934 }
935}
936
d183932d 937/* Add a register to a collection list. */
392a587b 938static void
fba45db2 939add_register (struct collection_list *collection, unsigned int regno)
c906108c
SS
940{
941 if (info_verbose)
942 printf_filtered ("collect register %d\n", regno);
27e06d3e 943 if (regno >= (8 * sizeof (collection->regs_mask)))
8a3fe4f8 944 error (_("Internal: register number %d too large for tracepoint"),
c906108c 945 regno);
c5aa993b 946 collection->regs_mask[regno / 8] |= 1 << (regno % 8);
c906108c
SS
947}
948
949/* Add a memrange to a collection list */
950static void
d183932d
MS
951add_memrange (struct collection_list *memranges,
952 int type, bfd_signed_vma base,
fba45db2 953 unsigned long len)
c906108c
SS
954{
955 if (info_verbose)
104c1213
JM
956 {
957 printf_filtered ("(%d,", type);
958 printf_vma (base);
959 printf_filtered (",%ld)\n", len);
960 }
961
f50e79a4 962 /* type: memrange_absolute == memory, other n == basereg */
c5aa993b 963 memranges->list[memranges->next_memrange].type = type;
d183932d 964 /* base: addr if memory, offset if reg relative. */
c906108c
SS
965 memranges->list[memranges->next_memrange].start = base;
966 /* len: we actually save end (base + len) for convenience */
c5aa993b 967 memranges->list[memranges->next_memrange].end = base + len;
c906108c
SS
968 memranges->next_memrange++;
969 if (memranges->next_memrange >= memranges->listsize)
970 {
971 memranges->listsize *= 2;
c5aa993b 972 memranges->list = xrealloc (memranges->list,
c906108c
SS
973 memranges->listsize);
974 }
975
f50e79a4 976 if (type != memrange_absolute) /* Better collect the base register! */
c906108c
SS
977 add_register (memranges, type);
978}
979
d183932d 980/* Add a symbol to a collection list. */
c906108c 981static void
d183932d
MS
982collect_symbol (struct collection_list *collect,
983 struct symbol *sym,
a6d9a66e 984 struct gdbarch *gdbarch,
0936ad1d
SS
985 long frame_regno, long frame_offset,
986 CORE_ADDR scope)
c906108c 987{
c5aa993b 988 unsigned long len;
104c1213 989 unsigned int reg;
c906108c
SS
990 bfd_signed_vma offset;
991
c5aa993b
JM
992 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
993 switch (SYMBOL_CLASS (sym))
994 {
995 default:
996 printf_filtered ("%s: don't know symbol class %d\n",
3567439c 997 SYMBOL_PRINT_NAME (sym),
d183932d 998 SYMBOL_CLASS (sym));
c5aa993b
JM
999 break;
1000 case LOC_CONST:
104c1213 1001 printf_filtered ("constant %s (value %ld) will not be collected.\n",
3567439c 1002 SYMBOL_PRINT_NAME (sym), SYMBOL_VALUE (sym));
c5aa993b
JM
1003 break;
1004 case LOC_STATIC:
1005 offset = SYMBOL_VALUE_ADDRESS (sym);
1006 if (info_verbose)
104c1213
JM
1007 {
1008 char tmp[40];
1009
1010 sprintf_vma (tmp, offset);
1011 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
3567439c 1012 SYMBOL_PRINT_NAME (sym), len,
d183932d 1013 tmp /* address */);
104c1213 1014 }
f50e79a4 1015 add_memrange (collect, memrange_absolute, offset, len);
c5aa993b
JM
1016 break;
1017 case LOC_REGISTER:
a6d9a66e 1018 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
c5aa993b 1019 if (info_verbose)
d183932d 1020 printf_filtered ("LOC_REG[parm] %s: ",
3567439c 1021 SYMBOL_PRINT_NAME (sym));
c5aa993b 1022 add_register (collect, reg);
d183932d
MS
1023 /* Check for doubles stored in two registers. */
1024 /* FIXME: how about larger types stored in 3 or more regs? */
c5aa993b 1025 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
a6d9a66e 1026 len > register_size (gdbarch, reg))
c5aa993b
JM
1027 add_register (collect, reg + 1);
1028 break;
1029 case LOC_REF_ARG:
1030 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
1031 printf_filtered (" (will not collect %s)\n",
3567439c 1032 SYMBOL_PRINT_NAME (sym));
c5aa993b
JM
1033 break;
1034 case LOC_ARG:
1035 reg = frame_regno;
1036 offset = frame_offset + SYMBOL_VALUE (sym);
1037 if (info_verbose)
1038 {
104c1213 1039 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
3567439c 1040 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1041 printf_vma (offset);
1042 printf_filtered (" from frame ptr reg %d\n", reg);
c5aa993b
JM
1043 }
1044 add_memrange (collect, reg, offset, len);
1045 break;
1046 case LOC_REGPARM_ADDR:
1047 reg = SYMBOL_VALUE (sym);
1048 offset = 0;
1049 if (info_verbose)
1050 {
104c1213 1051 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset ",
3567439c 1052 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1053 printf_vma (offset);
1054 printf_filtered (" from reg %d\n", reg);
c5aa993b
JM
1055 }
1056 add_memrange (collect, reg, offset, len);
1057 break;
1058 case LOC_LOCAL:
c5aa993b
JM
1059 reg = frame_regno;
1060 offset = frame_offset + SYMBOL_VALUE (sym);
1061 if (info_verbose)
1062 {
104c1213 1063 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
3567439c 1064 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1065 printf_vma (offset);
1066 printf_filtered (" from frame ptr reg %d\n", reg);
c5aa993b
JM
1067 }
1068 add_memrange (collect, reg, offset, len);
1069 break;
c5aa993b 1070 case LOC_UNRESOLVED:
d183932d 1071 printf_filtered ("Don't know LOC_UNRESOLVED %s\n",
3567439c 1072 SYMBOL_PRINT_NAME (sym));
c5aa993b
JM
1073 break;
1074 case LOC_OPTIMIZED_OUT:
8e1a459b 1075 printf_filtered ("%s has been optimized out of existence.\n",
3567439c 1076 SYMBOL_PRINT_NAME (sym));
c5aa993b 1077 break;
0936ad1d
SS
1078
1079 case LOC_COMPUTED:
1080 {
1081 struct agent_expr *aexpr;
1082 struct cleanup *old_chain1 = NULL;
1083 struct agent_reqs areqs;
1084
1085 aexpr = gen_trace_for_var (scope, sym);
1086
1087 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1088
1089 ax_reqs (aexpr, &areqs);
1090 if (areqs.flaw != agent_flaw_none)
1091 error (_("malformed expression"));
1092
1093 if (areqs.min_height < 0)
1094 error (_("gdb: Internal error: expression has min height < 0"));
1095 if (areqs.max_height > 20)
1096 error (_("expression too complicated, try simplifying"));
1097
1098 discard_cleanups (old_chain1);
1099 add_aexpr (collect, aexpr);
1100
1101 /* take care of the registers */
1102 if (areqs.reg_mask_len > 0)
1103 {
1104 int ndx1, ndx2;
1105
1106 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
1107 {
1108 QUIT; /* allow user to bail out with ^C */
1109 if (areqs.reg_mask[ndx1] != 0)
1110 {
1111 /* assume chars have 8 bits */
1112 for (ndx2 = 0; ndx2 < 8; ndx2++)
1113 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1114 /* it's used -- record it */
1115 add_register (collect,
1116 ndx1 * 8 + ndx2);
1117 }
1118 }
1119 }
1120 }
1121 break;
c5aa993b 1122 }
c906108c
SS
1123}
1124
1125/* Add all locals (or args) symbols to collection list */
1126static void
a6d9a66e
UW
1127add_local_symbols (struct collection_list *collect,
1128 struct gdbarch *gdbarch, CORE_ADDR pc,
fba45db2 1129 long frame_regno, long frame_offset, int type)
c906108c
SS
1130{
1131 struct symbol *sym;
c5aa993b 1132 struct block *block;
de4f826b
DC
1133 struct dict_iterator iter;
1134 int count = 0;
c906108c
SS
1135
1136 block = block_for_pc (pc);
1137 while (block != 0)
1138 {
c5aa993b 1139 QUIT; /* allow user to bail out with ^C */
de4f826b 1140 ALL_BLOCK_SYMBOLS (block, iter, sym)
c906108c 1141 {
2a2d4dc3
AS
1142 if (SYMBOL_IS_ARGUMENT (sym)
1143 ? type == 'A' /* collecting Arguments */
1144 : type == 'L') /* collecting Locals */
c5aa993b 1145 {
2a2d4dc3 1146 count++;
a6d9a66e 1147 collect_symbol (collect, sym, gdbarch,
0936ad1d 1148 frame_regno, frame_offset, pc);
c5aa993b 1149 }
c906108c
SS
1150 }
1151 if (BLOCK_FUNCTION (block))
1152 break;
1153 else
1154 block = BLOCK_SUPERBLOCK (block);
1155 }
1156 if (count == 0)
8a3fe4f8 1157 warning (_("No %s found in scope."),
d183932d 1158 type == 'L' ? "locals" : "args");
c906108c
SS
1159}
1160
1161/* worker function */
1162static void
fba45db2 1163clear_collection_list (struct collection_list *list)
c906108c
SS
1164{
1165 int ndx;
1166
1167 list->next_memrange = 0;
1168 for (ndx = 0; ndx < list->next_aexpr_elt; ndx++)
1169 {
c5aa993b 1170 free_agent_expr (list->aexpr_list[ndx]);
c906108c
SS
1171 list->aexpr_list[ndx] = NULL;
1172 }
1173 list->next_aexpr_elt = 0;
1174 memset (list->regs_mask, 0, sizeof (list->regs_mask));
1175}
1176
1177/* reduce a collection list to string form (for gdb protocol) */
1178static char **
fba45db2 1179stringify_collection_list (struct collection_list *list, char *string)
c906108c
SS
1180{
1181 char temp_buf[2048];
104c1213 1182 char tmp2[40];
c906108c
SS
1183 int count;
1184 int ndx = 0;
1185 char *(*str_list)[];
1186 char *end;
c5aa993b 1187 long i;
c906108c
SS
1188
1189 count = 1 + list->next_memrange + list->next_aexpr_elt + 1;
c5aa993b 1190 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
c906108c
SS
1191
1192 for (i = sizeof (list->regs_mask) - 1; i > 0; i--)
1193 if (list->regs_mask[i] != 0) /* skip leading zeroes in regs_mask */
1194 break;
1195 if (list->regs_mask[i] != 0) /* prepare to send regs_mask to the stub */
1196 {
1197 if (info_verbose)
1198 printf_filtered ("\nCollecting registers (mask): 0x");
1199 end = temp_buf;
c5aa993b 1200 *end++ = 'R';
c906108c
SS
1201 for (; i >= 0; i--)
1202 {
c5aa993b 1203 QUIT; /* allow user to bail out with ^C */
c906108c
SS
1204 if (info_verbose)
1205 printf_filtered ("%02X", list->regs_mask[i]);
c5aa993b 1206 sprintf (end, "%02X", list->regs_mask[i]);
c906108c
SS
1207 end += 2;
1208 }
1b36a34b 1209 (*str_list)[ndx] = xstrdup (temp_buf);
c906108c
SS
1210 ndx++;
1211 }
1212 if (info_verbose)
1213 printf_filtered ("\n");
1214 if (list->next_memrange > 0 && info_verbose)
1215 printf_filtered ("Collecting memranges: \n");
1216 for (i = 0, count = 0, end = temp_buf; i < list->next_memrange; i++)
1217 {
1218 QUIT; /* allow user to bail out with ^C */
104c1213 1219 sprintf_vma (tmp2, list->list[i].start);
c906108c 1220 if (info_verbose)
104c1213
JM
1221 {
1222 printf_filtered ("(%d, %s, %ld)\n",
1223 list->list[i].type,
1224 tmp2,
1225 (long) (list->list[i].end - list->list[i].start));
1226 }
c906108c
SS
1227 if (count + 27 > MAX_AGENT_EXPR_LEN)
1228 {
c5aa993b 1229 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1230 ndx++;
1231 count = 0;
1232 end = temp_buf;
1233 }
104c1213 1234
d1948716
JB
1235 {
1236 bfd_signed_vma length = list->list[i].end - list->list[i].start;
1237
1238 /* The "%X" conversion specifier expects an unsigned argument,
f50e79a4
JB
1239 so passing -1 (memrange_absolute) to it directly gives you
1240 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1241 Special-case it. */
1242 if (list->list[i].type == memrange_absolute)
d1948716
JB
1243 sprintf (end, "M-1,%s,%lX", tmp2, (long) length);
1244 else
1245 sprintf (end, "M%X,%s,%lX", list->list[i].type, tmp2, (long) length);
1246 }
104c1213 1247
c906108c 1248 count += strlen (end);
3ffbc0a5 1249 end = temp_buf + count;
c906108c
SS
1250 }
1251
1252 for (i = 0; i < list->next_aexpr_elt; i++)
1253 {
1254 QUIT; /* allow user to bail out with ^C */
1255 if ((count + 10 + 2 * list->aexpr_list[i]->len) > MAX_AGENT_EXPR_LEN)
1256 {
c5aa993b 1257 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1258 ndx++;
1259 count = 0;
1260 end = temp_buf;
1261 }
1262 sprintf (end, "X%08X,", list->aexpr_list[i]->len);
1263 end += 10; /* 'X' + 8 hex digits + ',' */
1264 count += 10;
1265
d183932d
MS
1266 end = mem2hex (list->aexpr_list[i]->buf,
1267 end, list->aexpr_list[i]->len);
c906108c
SS
1268 count += 2 * list->aexpr_list[i]->len;
1269 }
1270
1271 if (count != 0)
1272 {
c5aa993b 1273 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1274 ndx++;
1275 count = 0;
1276 end = temp_buf;
1277 }
1278 (*str_list)[ndx] = NULL;
1279
1280 if (ndx == 0)
27e06d3e 1281 {
6c761d9c 1282 xfree (str_list);
27e06d3e
MS
1283 return NULL;
1284 }
c906108c
SS
1285 else
1286 return *str_list;
1287}
1288
392a587b 1289static void
fba45db2 1290free_actions_list_cleanup_wrapper (void *al)
392a587b
JM
1291{
1292 free_actions_list (al);
1293}
1294
1295static void
fba45db2 1296free_actions_list (char **actions_list)
c906108c
SS
1297{
1298 int ndx;
1299
1300 if (actions_list == 0)
1301 return;
1302
1303 for (ndx = 0; actions_list[ndx]; ndx++)
b8c9b27d 1304 xfree (actions_list[ndx]);
c906108c 1305
b8c9b27d 1306 xfree (actions_list);
c906108c
SS
1307}
1308
d183932d 1309/* Render all actions into gdb protocol. */
c906108c 1310static void
1042e4c0 1311encode_actions (struct breakpoint *t, char ***tdp_actions,
fba45db2 1312 char ***stepping_actions)
c906108c 1313{
c5aa993b
JM
1314 static char tdp_buff[2048], step_buff[2048];
1315 char *action_exp;
1316 struct expression *exp = NULL;
c906108c 1317 struct action_line *action;
104c1213 1318 int i;
f976f6d4 1319 struct value *tempval;
c5aa993b 1320 struct collection_list *collect;
c906108c
SS
1321 struct cmd_list_element *cmd;
1322 struct agent_expr *aexpr;
39d4ef09
AC
1323 int frame_reg;
1324 LONGEST frame_offset;
236f1d4d
SS
1325 char *default_collect_line = NULL;
1326 struct action_line *default_collect_action = NULL;
c906108c
SS
1327
1328 clear_collection_list (&tracepoint_list);
1329 clear_collection_list (&stepping_list);
1330 collect = &tracepoint_list;
1331
1332 *tdp_actions = NULL;
1333 *stepping_actions = NULL;
1334
a6d9a66e 1335 gdbarch_virtual_frame_pointer (t->gdbarch,
1042e4c0 1336 t->loc->address, &frame_reg, &frame_offset);
c906108c 1337
236f1d4d
SS
1338 action = t->actions;
1339
1340 /* If there are default expressions to collect, make up a collect
1341 action and prepend to the action list to encode. Note that since
1342 validation is per-tracepoint (local var "xyz" might be valid for
1343 one tracepoint and not another, etc), we make up the action on
1344 the fly, and don't cache it. */
1345 if (*default_collect)
1346 {
1347 char *line;
1348 enum actionline_type linetype;
1349
1350 default_collect_line = xmalloc (12 + strlen (default_collect));
1351 sprintf (default_collect_line, "collect %s", default_collect);
1352 line = default_collect_line;
1353 linetype = validate_actionline (&line, t);
1354 if (linetype != BADLINE)
1355 {
1356 default_collect_action = xmalloc (sizeof (struct action_line));
1357 default_collect_action->next = t->actions;
1358 default_collect_action->action = line;
1359 action = default_collect_action;
1360 }
1361 }
1362
1363 for (; action; action = action->next)
c906108c
SS
1364 {
1365 QUIT; /* allow user to bail out with ^C */
1366 action_exp = action->action;
104c1213 1367 while (isspace ((int) *action_exp))
c906108c
SS
1368 action_exp++;
1369
1370 if (*action_exp == '#') /* comment line */
1371 return;
1372
1373 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1374 if (cmd == 0)
8a3fe4f8 1375 error (_("Bad action list item: %s"), action_exp);
c906108c 1376
bbaca940 1377 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
c906108c 1378 {
c5aa993b
JM
1379 do
1380 { /* repeat over a comma-separated list */
1381 QUIT; /* allow user to bail out with ^C */
104c1213 1382 while (isspace ((int) *action_exp))
c5aa993b 1383 action_exp++;
c906108c 1384
c5aa993b
JM
1385 if (0 == strncasecmp ("$reg", action_exp, 4))
1386 {
a6d9a66e 1387 for (i = 0; i < gdbarch_num_regs (t->gdbarch); i++)
c5aa993b
JM
1388 add_register (collect, i);
1389 action_exp = strchr (action_exp, ','); /* more? */
1390 }
1391 else if (0 == strncasecmp ("$arg", action_exp, 4))
1392 {
1393 add_local_symbols (collect,
a6d9a66e 1394 t->gdbarch,
1042e4c0 1395 t->loc->address,
c5aa993b
JM
1396 frame_reg,
1397 frame_offset,
1398 'A');
1399 action_exp = strchr (action_exp, ','); /* more? */
1400 }
1401 else if (0 == strncasecmp ("$loc", action_exp, 4))
1402 {
1403 add_local_symbols (collect,
a6d9a66e 1404 t->gdbarch,
1042e4c0 1405 t->loc->address,
c5aa993b
JM
1406 frame_reg,
1407 frame_offset,
1408 'L');
1409 action_exp = strchr (action_exp, ','); /* more? */
1410 }
1411 else
1412 {
1413 unsigned long addr, len;
1414 struct cleanup *old_chain = NULL;
1415 struct cleanup *old_chain1 = NULL;
1416 struct agent_reqs areqs;
1417
75ac9d7b 1418 exp = parse_exp_1 (&action_exp,
1042e4c0 1419 block_for_pc (t->loc->address), 1);
74b7792f 1420 old_chain = make_cleanup (free_current_contents, &exp);
c906108c 1421
c5aa993b
JM
1422 switch (exp->elts[0].opcode)
1423 {
1424 case OP_REGISTER:
67f3407f
DJ
1425 {
1426 const char *name = &exp->elts[2].string;
1427
a6d9a66e 1428 i = user_reg_map_name_to_regnum (t->gdbarch,
029a67e4 1429 name, strlen (name));
67f3407f
DJ
1430 if (i == -1)
1431 internal_error (__FILE__, __LINE__,
1432 _("Register $%s not available"),
1433 name);
1434 if (info_verbose)
1435 printf_filtered ("OP_REGISTER: ");
1436 add_register (collect, i);
1437 break;
1438 }
c5aa993b
JM
1439
1440 case UNOP_MEMVAL:
1441 /* safe because we know it's a simple expression */
1442 tempval = evaluate_expression (exp);
42ae5230 1443 addr = value_address (tempval);
c5aa993b 1444 len = TYPE_LENGTH (check_typedef (exp->elts[1].type));
f50e79a4 1445 add_memrange (collect, memrange_absolute, addr, len);
c5aa993b
JM
1446 break;
1447
1448 case OP_VAR_VALUE:
1449 collect_symbol (collect,
1450 exp->elts[2].symbol,
a6d9a66e 1451 t->gdbarch,
c5aa993b 1452 frame_reg,
0936ad1d
SS
1453 frame_offset,
1454 t->loc->address);
c5aa993b
JM
1455 break;
1456
1457 default: /* full-fledged expression */
1042e4c0 1458 aexpr = gen_trace_for_expr (t->loc->address, exp);
c5aa993b 1459
f23d52e0 1460 old_chain1 = make_cleanup_free_agent_expr (aexpr);
c5aa993b
JM
1461
1462 ax_reqs (aexpr, &areqs);
1463 if (areqs.flaw != agent_flaw_none)
8a3fe4f8 1464 error (_("malformed expression"));
c5aa993b
JM
1465
1466 if (areqs.min_height < 0)
8a3fe4f8 1467 error (_("gdb: Internal error: expression has min height < 0"));
c5aa993b 1468 if (areqs.max_height > 20)
8a3fe4f8 1469 error (_("expression too complicated, try simplifying"));
c5aa993b
JM
1470
1471 discard_cleanups (old_chain1);
1472 add_aexpr (collect, aexpr);
1473
1474 /* take care of the registers */
1475 if (areqs.reg_mask_len > 0)
c906108c 1476 {
c5aa993b
JM
1477 int ndx1;
1478 int ndx2;
1479
1480 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
c906108c 1481 {
c5aa993b
JM
1482 QUIT; /* allow user to bail out with ^C */
1483 if (areqs.reg_mask[ndx1] != 0)
1484 {
1485 /* assume chars have 8 bits */
1486 for (ndx2 = 0; ndx2 < 8; ndx2++)
1487 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1488 /* it's used -- record it */
d183932d
MS
1489 add_register (collect,
1490 ndx1 * 8 + ndx2);
c5aa993b 1491 }
c906108c
SS
1492 }
1493 }
c5aa993b
JM
1494 break;
1495 } /* switch */
1496 do_cleanups (old_chain);
1497 } /* do */
1498 }
1499 while (action_exp && *action_exp++ == ',');
1500 } /* if */
6da95a67
SS
1501 else if (cmd_cfunc_eq (cmd, teval_pseudocommand))
1502 {
1503 do
1504 { /* repeat over a comma-separated list */
1505 QUIT; /* allow user to bail out with ^C */
1506 while (isspace ((int) *action_exp))
1507 action_exp++;
1508
1509 {
1510 unsigned long addr, len;
1511 struct cleanup *old_chain = NULL;
1512 struct cleanup *old_chain1 = NULL;
1513 struct agent_reqs areqs;
1514
1515 exp = parse_exp_1 (&action_exp,
1516 block_for_pc (t->loc->address), 1);
1517 old_chain = make_cleanup (free_current_contents, &exp);
1518
1519 aexpr = gen_eval_for_expr (t->loc->address, exp);
1520 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1521
1522 ax_reqs (aexpr, &areqs);
1523 if (areqs.flaw != agent_flaw_none)
1524 error (_("malformed expression"));
1525
1526 if (areqs.min_height < 0)
1527 error (_("gdb: Internal error: expression has min height < 0"));
1528 if (areqs.max_height > 20)
1529 error (_("expression too complicated, try simplifying"));
1530
1531 discard_cleanups (old_chain1);
1532 /* Even though we're not officially collecting, add
1533 to the collect list anyway. */
1534 add_aexpr (collect, aexpr);
1535
1536 do_cleanups (old_chain);
1537 } /* do */
1538 }
1539 while (action_exp && *action_exp++ == ',');
1540 } /* if */
bbaca940 1541 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
c906108c
SS
1542 {
1543 collect = &stepping_list;
1544 }
bbaca940 1545 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
c906108c
SS
1546 {
1547 if (collect == &stepping_list) /* end stepping actions */
1548 collect = &tracepoint_list;
1549 else
c5aa993b 1550 break; /* end tracepoint actions */
c906108c 1551 }
c5aa993b
JM
1552 } /* for */
1553 memrange_sortmerge (&tracepoint_list);
1554 memrange_sortmerge (&stepping_list);
c906108c 1555
d183932d
MS
1556 *tdp_actions = stringify_collection_list (&tracepoint_list,
1557 tdp_buff);
1558 *stepping_actions = stringify_collection_list (&stepping_list,
1559 step_buff);
236f1d4d
SS
1560
1561 xfree (default_collect_line);
1562 xfree (default_collect_action);
c906108c
SS
1563}
1564
1565static void
fba45db2 1566add_aexpr (struct collection_list *collect, struct agent_expr *aexpr)
c906108c
SS
1567{
1568 if (collect->next_aexpr_elt >= collect->aexpr_listsize)
1569 {
1570 collect->aexpr_list =
1571 xrealloc (collect->aexpr_list,
c5aa993b 1572 2 * collect->aexpr_listsize * sizeof (struct agent_expr *));
c906108c
SS
1573 collect->aexpr_listsize *= 2;
1574 }
1575 collect->aexpr_list[collect->next_aexpr_elt] = aexpr;
1576 collect->next_aexpr_elt++;
1577}
1578
c906108c
SS
1579/* Set "transparent" memory ranges
1580
1581 Allow trace mechanism to treat text-like sections
1582 (and perhaps all read-only sections) transparently,
1583 i.e. don't reject memory requests from these address ranges
1584 just because they haven't been collected. */
1585
1586static void
1587remote_set_transparent_ranges (void)
1588{
c906108c
SS
1589 asection *s;
1590 bfd_size_type size;
1591 bfd_vma lma;
1592 int anysecs = 0;
1593
1594 if (!exec_bfd)
d183932d 1595 return; /* No information to give. */
c906108c
SS
1596
1597 strcpy (target_buf, "QTro");
1598 for (s = exec_bfd->sections; s; s = s->next)
1599 {
104c1213 1600 char tmp1[40], tmp2[40];
c906108c 1601
c5aa993b
JM
1602 if ((s->flags & SEC_LOAD) == 0 ||
1603 /* (s->flags & SEC_CODE) == 0 || */
c906108c
SS
1604 (s->flags & SEC_READONLY) == 0)
1605 continue;
1606
1607 anysecs = 1;
c5aa993b 1608 lma = s->lma;
2c500098 1609 size = bfd_get_section_size (s);
104c1213
JM
1610 sprintf_vma (tmp1, lma);
1611 sprintf_vma (tmp2, lma + size);
1612 sprintf (target_buf + strlen (target_buf),
1613 ":%s,%s", tmp1, tmp2);
c906108c
SS
1614 }
1615 if (anysecs)
1616 {
1617 putpkt (target_buf);
6d820c5c 1618 getpkt (&target_buf, &target_buf_size, 0);
c906108c
SS
1619 }
1620}
1621
1622/* tstart command:
c5aa993b 1623
c906108c
SS
1624 Tell target to clear any previous trace experiment.
1625 Walk the list of tracepoints, and send them (and their actions)
1626 to the target. If no errors,
1627 Tell target to start a new trace experiment. */
1628
1042e4c0
SS
1629void download_tracepoint (struct breakpoint *t);
1630
c906108c 1631static void
fba45db2 1632trace_start_command (char *args, int from_tty)
d183932d 1633{
f61e138d 1634 char buf[2048];
1042e4c0
SS
1635 VEC(breakpoint_p) *tp_vec = NULL;
1636 int ix;
1637 struct breakpoint *t;
f61e138d 1638 struct trace_state_variable *tsv;
c906108c 1639
d183932d 1640 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
c5aa993b 1641
c906108c
SS
1642 if (target_is_remote ())
1643 {
1644 putpkt ("QTinit");
6d820c5c 1645 remote_get_noisy_reply (&target_buf, &target_buf_size);
c906108c 1646 if (strcmp (target_buf, "OK"))
8a3fe4f8 1647 error (_("Target does not support this command."));
c906108c 1648
1042e4c0
SS
1649 tp_vec = all_tracepoints ();
1650 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1651 {
1652 download_tracepoint (t);
1653 }
1654 VEC_free (breakpoint_p, tp_vec);
c5aa993b 1655
f61e138d
SS
1656 /* Init any trace state variables that start with nonzero values. */
1657
1658 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
1659 {
1660 if (tsv->initial_value != 0)
1661 {
1662 sprintf (buf, "QTDV:%x:%s",
1663 tsv->number, phex ((ULONGEST) tsv->initial_value, 8));
1664 putpkt (buf);
1665 remote_get_noisy_reply (&target_buf, &target_buf_size);
1666 }
1667 }
1668
d183932d 1669 /* Tell target to treat text-like sections as transparent. */
c906108c 1670 remote_set_transparent_ranges ();
d183932d 1671 /* Now insert traps and begin collecting data. */
c906108c 1672 putpkt ("QTStart");
6d820c5c 1673 remote_get_noisy_reply (&target_buf, &target_buf_size);
c906108c 1674 if (strcmp (target_buf, "OK"))
8a3fe4f8 1675 error (_("Bogus reply from target: %s"), target_buf);
d183932d 1676 set_traceframe_num (-1); /* All old traceframes invalidated. */
c906108c 1677 set_tracepoint_num (-1);
fb14de7b 1678 set_traceframe_context (NULL);
c906108c 1679 trace_running_p = 1;
9a4105ab
AC
1680 if (deprecated_trace_start_stop_hook)
1681 deprecated_trace_start_stop_hook (1, from_tty);
c5aa993b 1682
c906108c
SS
1683 }
1684 else
8a3fe4f8 1685 error (_("Trace can only be run on remote targets."));
c906108c
SS
1686}
1687
1042e4c0
SS
1688/* Send the definition of a single tracepoint to the target. */
1689
1690void
1691download_tracepoint (struct breakpoint *t)
1692{
1693 char tmp[40];
1694 char buf[2048];
1695 char **tdp_actions;
1696 char **stepping_actions;
1697 int ndx;
1698 struct cleanup *old_chain = NULL;
782b2b07
SS
1699 struct agent_expr *aexpr;
1700 struct cleanup *aexpr_chain = NULL;
1042e4c0
SS
1701
1702 sprintf_vma (tmp, (t->loc ? t->loc->address : 0));
1703 sprintf (buf, "QTDP:%x:%s:%c:%lx:%x", t->number,
1704 tmp, /* address */
1705 (t->enable_state == bp_enabled ? 'E' : 'D'),
1706 t->step_count, t->pass_count);
782b2b07
SS
1707 /* If the tracepoint has a conditional, make it into an agent
1708 expression and append to the definition. */
1709 if (t->loc->cond)
1710 {
1711 /* Only test support at download time, we may not know target
1712 capabilities at definition time. */
1713 if (remote_supports_cond_tracepoints ())
1714 {
1715 aexpr = gen_eval_for_expr (t->loc->address, t->loc->cond);
1716 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
1717 sprintf (buf + strlen (buf), ":X%x,", aexpr->len);
1718 mem2hex (aexpr->buf, buf + strlen (buf), aexpr->len);
1719 do_cleanups (aexpr_chain);
1720 }
1721 else
1722 warning (_("Target does not support conditional tracepoints, ignoring tp %d cond"), t->number);
1723 }
1042e4c0 1724
236f1d4d 1725 if (t->actions || *default_collect)
1042e4c0
SS
1726 strcat (buf, "-");
1727 putpkt (buf);
1728 remote_get_noisy_reply (&target_buf, &target_buf_size);
1729 if (strcmp (target_buf, "OK"))
1730 error (_("Target does not support tracepoints."));
1731
236f1d4d 1732 if (!t->actions && !*default_collect)
1042e4c0
SS
1733 return;
1734
1735 encode_actions (t, &tdp_actions, &stepping_actions);
1736 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
1737 tdp_actions);
1738 (void) make_cleanup (free_actions_list_cleanup_wrapper, stepping_actions);
1739
1740 /* do_single_steps (t); */
1741 if (tdp_actions)
1742 {
1743 for (ndx = 0; tdp_actions[ndx]; ndx++)
1744 {
1745 QUIT; /* allow user to bail out with ^C */
1746 sprintf (buf, "QTDP:-%x:%s:%s%c",
1747 t->number, tmp, /* address */
1748 tdp_actions[ndx],
1749 ((tdp_actions[ndx + 1] || stepping_actions)
1750 ? '-' : 0));
1751 putpkt (buf);
1752 remote_get_noisy_reply (&target_buf,
1753 &target_buf_size);
1754 if (strcmp (target_buf, "OK"))
1755 error (_("Error on target while setting tracepoints."));
1756 }
1757 }
1758 if (stepping_actions)
1759 {
1760 for (ndx = 0; stepping_actions[ndx]; ndx++)
1761 {
1762 QUIT; /* allow user to bail out with ^C */
1763 sprintf (buf, "QTDP:-%x:%s:%s%s%s",
1764 t->number, tmp, /* address */
1765 ((ndx == 0) ? "S" : ""),
1766 stepping_actions[ndx],
1767 (stepping_actions[ndx + 1] ? "-" : ""));
1768 putpkt (buf);
1769 remote_get_noisy_reply (&target_buf,
1770 &target_buf_size);
1771 if (strcmp (target_buf, "OK"))
1772 error (_("Error on target while setting tracepoints."));
1773 }
1774 }
1775 do_cleanups (old_chain);
1776}
1777
c906108c
SS
1778/* tstop command */
1779static void
fba45db2 1780trace_stop_command (char *args, int from_tty)
d183932d 1781{
c906108c
SS
1782 if (target_is_remote ())
1783 {
1784 putpkt ("QTStop");
6d820c5c 1785 remote_get_noisy_reply (&target_buf, &target_buf_size);
c906108c 1786 if (strcmp (target_buf, "OK"))
8a3fe4f8 1787 error (_("Bogus reply from target: %s"), target_buf);
c906108c 1788 trace_running_p = 0;
9a4105ab
AC
1789 if (deprecated_trace_start_stop_hook)
1790 deprecated_trace_start_stop_hook (0, from_tty);
c906108c
SS
1791 }
1792 else
8a3fe4f8 1793 error (_("Trace can only be run on remote targets."));
c906108c
SS
1794}
1795
1796unsigned long trace_running_p;
1797
1798/* tstatus command */
1799static void
fba45db2 1800trace_status_command (char *args, int from_tty)
d183932d 1801{
c906108c
SS
1802 if (target_is_remote ())
1803 {
1804 putpkt ("qTStatus");
6d820c5c 1805 remote_get_noisy_reply (&target_buf, &target_buf_size);
c906108c
SS
1806
1807 if (target_buf[0] != 'T' ||
1808 (target_buf[1] != '0' && target_buf[1] != '1'))
8a3fe4f8 1809 error (_("Bogus reply from target: %s"), target_buf);
c906108c
SS
1810
1811 /* exported for use by the GUI */
1812 trace_running_p = (target_buf[1] == '1');
d3513012
SS
1813
1814 if (trace_running_p)
1815 printf_filtered (_("Trace is running on the target.\n"));
1816 else
1817 printf_filtered (_("Trace is not running on the target.\n"));
1818
1819 if (traceframe_number >= 0)
1820 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
1821 traceframe_number, tracepoint_number);
1822 else
1823 printf_filtered (_("Not looking at any trace frame.\n"));
1824
c906108c
SS
1825 }
1826 else
8a3fe4f8 1827 error (_("Trace can only be run on remote targets."));
c906108c
SS
1828}
1829
d183932d 1830/* Worker function for the various flavors of the tfind command. */
c906108c 1831static void
6d820c5c
DJ
1832finish_tfind_command (char **msg,
1833 long *sizeof_msg,
c2d11a7d 1834 int from_tty)
c906108c
SS
1835{
1836 int target_frameno = -1, target_tracept = -1;
fb14de7b 1837 struct frame_id old_frame_id;
c906108c
SS
1838 char *reply;
1839
fb14de7b 1840 old_frame_id = get_frame_id (get_current_frame ());
c906108c 1841
6d820c5c 1842 putpkt (*msg);
c2d11a7d 1843 reply = remote_get_noisy_reply (msg, sizeof_msg);
c906108c
SS
1844
1845 while (reply && *reply)
c5aa993b
JM
1846 switch (*reply)
1847 {
1848 case 'F':
1849 if ((target_frameno = (int) strtol (++reply, &reply, 16)) == -1)
1850 {
1851 /* A request for a non-existant trace frame has failed.
1852 Our response will be different, depending on FROM_TTY:
1853
1854 If FROM_TTY is true, meaning that this command was
1855 typed interactively by the user, then give an error
1856 and DO NOT change the state of traceframe_number etc.
1857
1858 However if FROM_TTY is false, meaning that we're either
1859 in a script, a loop, or a user-defined command, then
1860 DON'T give an error, but DO change the state of
1861 traceframe_number etc. to invalid.
1862
1863 The rationalle is that if you typed the command, you
1864 might just have committed a typo or something, and you'd
1865 like to NOT lose your current debugging state. However
1866 if you're in a user-defined command or especially in a
1867 loop, then you need a way to detect that the command
1868 failed WITHOUT aborting. This allows you to write
1869 scripts that search thru the trace buffer until the end,
1870 and then continue on to do something else. */
1871
1872 if (from_tty)
8a3fe4f8 1873 error (_("Target failed to find requested trace frame."));
c5aa993b
JM
1874 else
1875 {
1876 if (info_verbose)
1877 printf_filtered ("End of trace buffer.\n");
d183932d
MS
1878 /* The following will not recurse, since it's
1879 special-cased. */
c5aa993b 1880 trace_find_command ("-1", from_tty);
d183932d
MS
1881 reply = NULL; /* Break out of loop
1882 (avoid recursive nonsense). */
c5aa993b
JM
1883 }
1884 }
1885 break;
1886 case 'T':
1887 if ((target_tracept = (int) strtol (++reply, &reply, 16)) == -1)
8a3fe4f8 1888 error (_("Target failed to find requested trace frame."));
c5aa993b
JM
1889 break;
1890 case 'O': /* "OK"? */
1891 if (reply[1] == 'K' && reply[2] == '\0')
1892 reply += 2;
1893 else
8a3fe4f8 1894 error (_("Bogus reply from target: %s"), reply);
c5aa993b
JM
1895 break;
1896 default:
8a3fe4f8 1897 error (_("Bogus reply from target: %s"), reply);
c5aa993b 1898 }
c906108c 1899
35f196d9 1900 reinit_frame_cache ();
c906108c 1901 registers_changed ();
c906108c
SS
1902 set_traceframe_num (target_frameno);
1903 set_tracepoint_num (target_tracept);
1904 if (target_frameno == -1)
fb14de7b 1905 set_traceframe_context (NULL);
c906108c 1906 else
fb14de7b 1907 set_traceframe_context (get_current_frame ());
c906108c
SS
1908
1909 if (from_tty)
1910 {
0faf0076 1911 enum print_what print_what;
c906108c
SS
1912
1913 /* NOTE: in immitation of the step command, try to determine
d183932d
MS
1914 whether we have made a transition from one function to
1915 another. If so, we'll print the "stack frame" (ie. the new
1916 function and it's arguments) -- otherwise we'll just show the
fb14de7b
UW
1917 new source line. */
1918
1919 if (frame_id_eq (old_frame_id,
1920 get_frame_id (get_current_frame ())))
0faf0076 1921 print_what = SRC_LINE;
c906108c 1922 else
0faf0076 1923 print_what = SRC_AND_LOC;
c906108c 1924
b04f3ab4 1925 print_stack_frame (get_selected_frame (NULL), 1, print_what);
c906108c
SS
1926 do_displays ();
1927 }
1928}
1929
1930/* trace_find_command takes a trace frame number n,
1931 sends "QTFrame:<n>" to the target,
1932 and accepts a reply that may contain several optional pieces
1933 of information: a frame number, a tracepoint number, and an
1934 indication of whether this is a trap frame or a stepping frame.
1935
1936 The minimal response is just "OK" (which indicates that the
1937 target does not give us a frame number or a tracepoint number).
1938 Instead of that, the target may send us a string containing
1939 any combination of:
c5aa993b
JM
1940 F<hexnum> (gives the selected frame number)
1941 T<hexnum> (gives the selected tracepoint number)
1942 */
c906108c
SS
1943
1944/* tfind command */
1945static void
fba45db2 1946trace_find_command (char *args, int from_tty)
d183932d 1947{ /* this should only be called with a numeric argument */
c906108c 1948 int frameno = -1;
c906108c
SS
1949
1950 if (target_is_remote ())
1951 {
383e5f85
SS
1952 if (trace_running_p)
1953 error ("May not look at trace frames while trace is running.");
1954
9a4105ab
AC
1955 if (deprecated_trace_find_hook)
1956 deprecated_trace_find_hook (args, from_tty);
c5aa993b 1957
c906108c 1958 if (args == 0 || *args == 0)
d183932d 1959 { /* TFIND with no args means find NEXT trace frame. */
c906108c
SS
1960 if (traceframe_number == -1)
1961 frameno = 0; /* "next" is first one */
1962 else
1963 frameno = traceframe_number + 1;
1964 }
1965 else if (0 == strcmp (args, "-"))
1966 {
1967 if (traceframe_number == -1)
8a3fe4f8 1968 error (_("not debugging trace buffer"));
c906108c 1969 else if (from_tty && traceframe_number == 0)
8a3fe4f8 1970 error (_("already at start of trace buffer"));
c906108c
SS
1971
1972 frameno = traceframe_number - 1;
1973 }
1974 else
bb518678 1975 frameno = parse_and_eval_long (args);
c906108c
SS
1976
1977 if (frameno < -1)
8a3fe4f8 1978 error (_("invalid input (%d is less than zero)"), frameno);
c906108c
SS
1979
1980 sprintf (target_buf, "QTFrame:%x", frameno);
6d820c5c 1981 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
1982 }
1983 else
8a3fe4f8 1984 error (_("Trace can only be run on remote targets."));
c906108c
SS
1985}
1986
1987/* tfind end */
1988static void
fba45db2 1989trace_find_end_command (char *args, int from_tty)
c906108c
SS
1990{
1991 trace_find_command ("-1", from_tty);
1992}
1993
1994/* tfind none */
1995static void
fba45db2 1996trace_find_none_command (char *args, int from_tty)
c906108c
SS
1997{
1998 trace_find_command ("-1", from_tty);
1999}
2000
2001/* tfind start */
2002static void
fba45db2 2003trace_find_start_command (char *args, int from_tty)
c906108c
SS
2004{
2005 trace_find_command ("0", from_tty);
2006}
2007
2008/* tfind pc command */
2009static void
fba45db2 2010trace_find_pc_command (char *args, int from_tty)
d183932d 2011{
c906108c 2012 CORE_ADDR pc;
104c1213 2013 char tmp[40];
c906108c
SS
2014
2015 if (target_is_remote ())
2016 {
383e5f85
SS
2017 if (trace_running_p)
2018 error ("May not look at trace frames while trace is running.");
2019
c906108c 2020 if (args == 0 || *args == 0)
fb14de7b 2021 pc = regcache_read_pc (get_current_regcache ());
c906108c
SS
2022 else
2023 pc = parse_and_eval_address (args);
2024
104c1213
JM
2025 sprintf_vma (tmp, pc);
2026 sprintf (target_buf, "QTFrame:pc:%s", tmp);
6d820c5c 2027 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
2028 }
2029 else
8a3fe4f8 2030 error (_("Trace can only be run on remote targets."));
c906108c
SS
2031}
2032
2033/* tfind tracepoint command */
2034static void
fba45db2 2035trace_find_tracepoint_command (char *args, int from_tty)
d183932d 2036{
c906108c 2037 int tdp;
c906108c
SS
2038
2039 if (target_is_remote ())
2040 {
383e5f85
SS
2041 if (trace_running_p)
2042 error ("May not look at trace frames while trace is running.");
2043
c906108c 2044 if (args == 0 || *args == 0)
3db26b01
JB
2045 {
2046 if (tracepoint_number == -1)
8a3fe4f8 2047 error (_("No current tracepoint -- please supply an argument."));
3db26b01
JB
2048 else
2049 tdp = tracepoint_number; /* default is current TDP */
2050 }
c906108c 2051 else
0e828ed1 2052 tdp = parse_and_eval_long (args);
c906108c
SS
2053
2054 sprintf (target_buf, "QTFrame:tdp:%x", tdp);
6d820c5c 2055 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
2056 }
2057 else
8a3fe4f8 2058 error (_("Trace can only be run on remote targets."));
c906108c
SS
2059}
2060
2061/* TFIND LINE command:
c5aa993b 2062
c906108c 2063 This command will take a sourceline for argument, just like BREAK
d183932d 2064 or TRACE (ie. anything that "decode_line_1" can handle).
c5aa993b 2065
c906108c
SS
2066 With no argument, this command will find the next trace frame
2067 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2068
2069static void
fba45db2 2070trace_find_line_command (char *args, int from_tty)
d183932d 2071{
c906108c
SS
2072 static CORE_ADDR start_pc, end_pc;
2073 struct symtabs_and_lines sals;
2074 struct symtab_and_line sal;
c906108c 2075 struct cleanup *old_chain;
104c1213 2076 char startpc_str[40], endpc_str[40];
c906108c
SS
2077
2078 if (target_is_remote ())
2079 {
383e5f85
SS
2080 if (trace_running_p)
2081 error ("May not look at trace frames while trace is running.");
2082
c906108c
SS
2083 if (args == 0 || *args == 0)
2084 {
bdd78e62 2085 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
c906108c
SS
2086 sals.nelts = 1;
2087 sals.sals = (struct symtab_and_line *)
2088 xmalloc (sizeof (struct symtab_and_line));
2089 sals.sals[0] = sal;
2090 }
2091 else
2092 {
2093 sals = decode_line_spec (args, 1);
c5aa993b 2094 sal = sals.sals[0];
c906108c
SS
2095 }
2096
b8c9b27d 2097 old_chain = make_cleanup (xfree, sals.sals);
c906108c
SS
2098 if (sal.symtab == 0)
2099 {
5af949e3
UW
2100 struct gdbarch *gdbarch = get_current_arch ();
2101
c906108c
SS
2102 printf_filtered ("TFIND: No line number information available");
2103 if (sal.pc != 0)
2104 {
d183932d
MS
2105 /* This is useful for "info line *0x7f34". If we can't
2106 tell the user about a source line, at least let them
2107 have the symbolic address. */
c906108c
SS
2108 printf_filtered (" for address ");
2109 wrap_here (" ");
5af949e3 2110 print_address (gdbarch, sal.pc, gdb_stdout);
c906108c
SS
2111 printf_filtered (";\n -- will attempt to find by PC. \n");
2112 }
2113 else
2114 {
2115 printf_filtered (".\n");
d183932d 2116 return; /* No line, no PC; what can we do? */
c906108c
SS
2117 }
2118 }
2119 else if (sal.line > 0
2120 && find_line_pc_range (sal, &start_pc, &end_pc))
2121 {
5af949e3
UW
2122 struct gdbarch *gdbarch = get_objfile_arch (sal.symtab->objfile);
2123
c906108c
SS
2124 if (start_pc == end_pc)
2125 {
2126 printf_filtered ("Line %d of \"%s\"",
2127 sal.line, sal.symtab->filename);
2128 wrap_here (" ");
2129 printf_filtered (" is at address ");
5af949e3 2130 print_address (gdbarch, start_pc, gdb_stdout);
c906108c
SS
2131 wrap_here (" ");
2132 printf_filtered (" but contains no code.\n");
2133 sal = find_pc_line (start_pc, 0);
2134 if (sal.line > 0 &&
2135 find_line_pc_range (sal, &start_pc, &end_pc) &&
2136 start_pc != end_pc)
2137 printf_filtered ("Attempting to find line %d instead.\n",
2138 sal.line);
2139 else
8a3fe4f8 2140 error (_("Cannot find a good line."));
c906108c
SS
2141 }
2142 }
2143 else
2144 /* Is there any case in which we get here, and have an address
d183932d
MS
2145 which the user would want to see? If we have debugging
2146 symbols and no line numbers? */
8a3fe4f8 2147 error (_("Line number %d is out of range for \"%s\"."),
c906108c
SS
2148 sal.line, sal.symtab->filename);
2149
104c1213
JM
2150 sprintf_vma (startpc_str, start_pc);
2151 sprintf_vma (endpc_str, end_pc - 1);
d183932d
MS
2152 /* Find within range of stated line. */
2153 if (args && *args)
2154 sprintf (target_buf, "QTFrame:range:%s:%s",
2155 startpc_str, endpc_str);
2156 /* Find OUTSIDE OF range of CURRENT line. */
2157 else
2158 sprintf (target_buf, "QTFrame:outside:%s:%s",
2159 startpc_str, endpc_str);
6d820c5c 2160 finish_tfind_command (&target_buf, &target_buf_size,
d183932d 2161 from_tty);
c906108c
SS
2162 do_cleanups (old_chain);
2163 }
2164 else
8a3fe4f8 2165 error (_("Trace can only be run on remote targets."));
c906108c
SS
2166}
2167
2168/* tfind range command */
2169static void
fba45db2 2170trace_find_range_command (char *args, int from_tty)
104c1213 2171{
c906108c 2172 static CORE_ADDR start, stop;
104c1213 2173 char start_str[40], stop_str[40];
c906108c
SS
2174 char *tmp;
2175
2176 if (target_is_remote ())
2177 {
383e5f85
SS
2178 if (trace_running_p)
2179 error ("May not look at trace frames while trace is running.");
2180
c906108c 2181 if (args == 0 || *args == 0)
d183932d 2182 { /* XXX FIXME: what should default behavior be? */
c906108c
SS
2183 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2184 return;
2185 }
2186
c5aa993b 2187 if (0 != (tmp = strchr (args, ',')))
c906108c
SS
2188 {
2189 *tmp++ = '\0'; /* terminate start address */
104c1213 2190 while (isspace ((int) *tmp))
c906108c
SS
2191 tmp++;
2192 start = parse_and_eval_address (args);
c5aa993b 2193 stop = parse_and_eval_address (tmp);
c906108c
SS
2194 }
2195 else
c5aa993b 2196 { /* no explicit end address? */
c906108c 2197 start = parse_and_eval_address (args);
c5aa993b 2198 stop = start + 1; /* ??? */
c906108c
SS
2199 }
2200
104c1213
JM
2201 sprintf_vma (start_str, start);
2202 sprintf_vma (stop_str, stop);
2203 sprintf (target_buf, "QTFrame:range:%s:%s", start_str, stop_str);
6d820c5c 2204 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
2205 }
2206 else
8a3fe4f8 2207 error (_("Trace can only be run on remote targets."));
c906108c
SS
2208}
2209
2210/* tfind outside command */
2211static void
fba45db2 2212trace_find_outside_command (char *args, int from_tty)
104c1213 2213{
c906108c 2214 CORE_ADDR start, stop;
104c1213 2215 char start_str[40], stop_str[40];
c906108c
SS
2216 char *tmp;
2217
2218 if (target_is_remote ())
2219 {
383e5f85
SS
2220 if (trace_running_p)
2221 error ("May not look at trace frames while trace is running.");
2222
c906108c 2223 if (args == 0 || *args == 0)
d183932d 2224 { /* XXX FIXME: what should default behavior be? */
c906108c
SS
2225 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2226 return;
2227 }
2228
c5aa993b 2229 if (0 != (tmp = strchr (args, ',')))
c906108c
SS
2230 {
2231 *tmp++ = '\0'; /* terminate start address */
104c1213 2232 while (isspace ((int) *tmp))
c906108c
SS
2233 tmp++;
2234 start = parse_and_eval_address (args);
c5aa993b 2235 stop = parse_and_eval_address (tmp);
c906108c
SS
2236 }
2237 else
c5aa993b 2238 { /* no explicit end address? */
c906108c 2239 start = parse_and_eval_address (args);
c5aa993b 2240 stop = start + 1; /* ??? */
c906108c
SS
2241 }
2242
104c1213
JM
2243 sprintf_vma (start_str, start);
2244 sprintf_vma (stop_str, stop);
2245 sprintf (target_buf, "QTFrame:outside:%s:%s", start_str, stop_str);
6d820c5c 2246 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
2247 }
2248 else
8a3fe4f8 2249 error (_("Trace can only be run on remote targets."));
c906108c
SS
2250}
2251
c906108c
SS
2252/* info scope command: list the locals for a scope. */
2253static void
fba45db2 2254scope_info (char *args, int from_tty)
c906108c 2255{
c906108c
SS
2256 struct symtabs_and_lines sals;
2257 struct symbol *sym;
2258 struct minimal_symbol *msym;
2259 struct block *block;
2260 char **canonical, *symname, *save_args = args;
de4f826b
DC
2261 struct dict_iterator iter;
2262 int j, count = 0;
768a979c
UW
2263 struct gdbarch *gdbarch;
2264 int regno;
c906108c
SS
2265
2266 if (args == 0 || *args == 0)
8a3fe4f8 2267 error (_("requires an argument (function, line or *addr) to define a scope"));
c906108c 2268
68219205 2269 sals = decode_line_1 (&args, 1, NULL, 0, &canonical, NULL);
c906108c 2270 if (sals.nelts == 0)
450bd37b 2271 return; /* presumably decode_line_1 has already warned */
c906108c
SS
2272
2273 /* Resolve line numbers to PC */
2274 resolve_sal_pc (&sals.sals[0]);
2275 block = block_for_pc (sals.sals[0].pc);
2276
2277 while (block != 0)
2278 {
c5aa993b 2279 QUIT; /* allow user to bail out with ^C */
de4f826b 2280 ALL_BLOCK_SYMBOLS (block, iter, sym)
c906108c 2281 {
c5aa993b 2282 QUIT; /* allow user to bail out with ^C */
c906108c
SS
2283 if (count == 0)
2284 printf_filtered ("Scope for %s:\n", save_args);
2285 count++;
e88c90f2 2286
3567439c 2287 symname = SYMBOL_PRINT_NAME (sym);
c906108c 2288 if (symname == NULL || *symname == '\0')
c5aa993b 2289 continue; /* probably botched, certainly useless */
c906108c 2290
768a979c
UW
2291 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
2292
c906108c 2293 printf_filtered ("Symbol %s is ", symname);
c5aa993b
JM
2294 switch (SYMBOL_CLASS (sym))
2295 {
2296 default:
2297 case LOC_UNDEF: /* messed up symbol? */
2298 printf_filtered ("a bogus symbol, class %d.\n",
2299 SYMBOL_CLASS (sym));
2300 count--; /* don't count this one */
2301 continue;
2302 case LOC_CONST:
104c1213 2303 printf_filtered ("a constant with value %ld (0x%lx)",
c5aa993b
JM
2304 SYMBOL_VALUE (sym), SYMBOL_VALUE (sym));
2305 break;
2306 case LOC_CONST_BYTES:
2307 printf_filtered ("constant bytes: ");
2308 if (SYMBOL_TYPE (sym))
2309 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2310 fprintf_filtered (gdb_stdout, " %02x",
2311 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2312 break;
2313 case LOC_STATIC:
2314 printf_filtered ("in static storage at address ");
5af949e3
UW
2315 printf_filtered ("%s", paddress (gdbarch,
2316 SYMBOL_VALUE_ADDRESS (sym)));
c5aa993b
JM
2317 break;
2318 case LOC_REGISTER:
768a979c
UW
2319 /* GDBARCH is the architecture associated with the objfile
2320 the symbol is defined in; the target architecture may be
2321 different, and may provide additional registers. However,
2322 we do not know the target architecture at this point.
2323 We assume the objfile architecture will contain all the
2324 standard registers that occur in debug info in that
2325 objfile. */
2326 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
2327
2a2d4dc3
AS
2328 if (SYMBOL_IS_ARGUMENT (sym))
2329 printf_filtered ("an argument in register $%s",
768a979c 2330 gdbarch_register_name (gdbarch, regno));
2a2d4dc3
AS
2331 else
2332 printf_filtered ("a local variable in register $%s",
768a979c 2333 gdbarch_register_name (gdbarch, regno));
c5aa993b
JM
2334 break;
2335 case LOC_ARG:
c5aa993b
JM
2336 printf_filtered ("an argument at stack/frame offset %ld",
2337 SYMBOL_VALUE (sym));
2338 break;
2339 case LOC_LOCAL:
2340 printf_filtered ("a local variable at frame offset %ld",
2341 SYMBOL_VALUE (sym));
2342 break;
2343 case LOC_REF_ARG:
2344 printf_filtered ("a reference argument at offset %ld",
2345 SYMBOL_VALUE (sym));
2346 break;
c5aa993b 2347 case LOC_REGPARM_ADDR:
768a979c
UW
2348 /* Note comment at LOC_REGISTER. */
2349 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
c5aa993b 2350 printf_filtered ("the address of an argument, in register $%s",
768a979c 2351 gdbarch_register_name (gdbarch, regno));
c5aa993b
JM
2352 break;
2353 case LOC_TYPEDEF:
2354 printf_filtered ("a typedef.\n");
2355 continue;
2356 case LOC_LABEL:
2357 printf_filtered ("a label at address ");
5af949e3
UW
2358 printf_filtered ("%s", paddress (gdbarch,
2359 SYMBOL_VALUE_ADDRESS (sym)));
c5aa993b
JM
2360 break;
2361 case LOC_BLOCK:
2362 printf_filtered ("a function at address ");
5af949e3
UW
2363 printf_filtered ("%s",
2364 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
c5aa993b 2365 break;
c5aa993b 2366 case LOC_UNRESOLVED:
3567439c 2367 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
450bd37b 2368 NULL, NULL);
c5aa993b
JM
2369 if (msym == NULL)
2370 printf_filtered ("Unresolved Static");
2371 else
2372 {
2373 printf_filtered ("static storage at address ");
5af949e3
UW
2374 printf_filtered ("%s",
2375 paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msym)));
c5aa993b
JM
2376 }
2377 break;
2378 case LOC_OPTIMIZED_OUT:
2379 printf_filtered ("optimized out.\n");
2380 continue;
450bd37b 2381 case LOC_COMPUTED:
768a979c 2382 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, gdb_stdout);
450bd37b 2383 break;
c5aa993b 2384 }
c906108c 2385 if (SYMBOL_TYPE (sym))
c5aa993b 2386 printf_filtered (", length %d.\n",
450bd37b 2387 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
c906108c
SS
2388 }
2389 if (BLOCK_FUNCTION (block))
2390 break;
2391 else
2392 block = BLOCK_SUPERBLOCK (block);
2393 }
2394 if (count <= 0)
2395 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2396 save_args);
2397}
2398
2399/* worker function (cleanup) */
2400static void
710b33bd 2401replace_comma (void *data)
c906108c 2402{
710b33bd 2403 char *comma = data;
c906108c
SS
2404 *comma = ',';
2405}
2406
2407/* tdump command */
2408static void
fba45db2 2409trace_dump_command (char *args, int from_tty)
c906108c 2410{
515630c5
UW
2411 struct regcache *regcache;
2412 struct gdbarch *gdbarch;
1042e4c0 2413 struct breakpoint *t;
c906108c 2414 struct action_line *action;
c5aa993b
JM
2415 char *action_exp, *next_comma;
2416 struct cleanup *old_cleanups;
2417 int stepping_actions = 0;
2418 int stepping_frame = 0;
c906108c
SS
2419
2420 if (!target_is_remote ())
2421 {
8a3fe4f8 2422 error (_("Trace can only be run on remote targets."));
c906108c
SS
2423 return;
2424 }
2425
2426 if (tracepoint_number == -1)
2427 {
8a3fe4f8 2428 warning (_("No current trace frame."));
c906108c
SS
2429 return;
2430 }
2431
1042e4c0 2432 t = get_tracepoint (tracepoint_number);
c906108c
SS
2433
2434 if (t == NULL)
8a3fe4f8 2435 error (_("No known tracepoint matches 'current' tracepoint #%d."),
c906108c
SS
2436 tracepoint_number);
2437
2438 old_cleanups = make_cleanup (null_cleanup, NULL);
2439
c5aa993b 2440 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
c906108c
SS
2441 tracepoint_number, traceframe_number);
2442
2443 /* The current frame is a trap frame if the frame PC is equal
2444 to the tracepoint PC. If not, then the current frame was
2445 collected during single-stepping. */
2446
515630c5
UW
2447 regcache = get_current_regcache ();
2448 gdbarch = get_regcache_arch (regcache);
2449
1042e4c0 2450 stepping_frame = (t->loc->address != (regcache_read_pc (regcache)
515630c5 2451 - gdbarch_decr_pc_after_break (gdbarch)));
c906108c
SS
2452
2453 for (action = t->actions; action; action = action->next)
2454 {
2455 struct cmd_list_element *cmd;
2456
c5aa993b 2457 QUIT; /* allow user to bail out with ^C */
c906108c 2458 action_exp = action->action;
104c1213 2459 while (isspace ((int) *action_exp))
c906108c
SS
2460 action_exp++;
2461
2462 /* The collection actions to be done while stepping are
c5aa993b 2463 bracketed by the commands "while-stepping" and "end". */
c906108c
SS
2464
2465 if (*action_exp == '#') /* comment line */
2466 continue;
2467
2468 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2469 if (cmd == 0)
8a3fe4f8 2470 error (_("Bad action list item: %s"), action_exp);
c906108c 2471
bbaca940 2472 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
c906108c 2473 stepping_actions = 1;
bbaca940 2474 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
c906108c 2475 stepping_actions = 0;
bbaca940 2476 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
c906108c
SS
2477 {
2478 /* Display the collected data.
d183932d
MS
2479 For the trap frame, display only what was collected at
2480 the trap. Likewise for stepping frames, display only
2481 what was collected while stepping. This means that the
2482 two boolean variables, STEPPING_FRAME and
2483 STEPPING_ACTIONS should be equal. */
c906108c
SS
2484 if (stepping_frame == stepping_actions)
2485 {
c5aa993b
JM
2486 do
2487 { /* repeat over a comma-separated list */
2488 QUIT; /* allow user to bail out with ^C */
2489 if (*action_exp == ',')
2490 action_exp++;
104c1213 2491 while (isspace ((int) *action_exp))
c5aa993b
JM
2492 action_exp++;
2493
2494 next_comma = strchr (action_exp, ',');
2495
2496 if (0 == strncasecmp (action_exp, "$reg", 4))
2497 registers_info (NULL, from_tty);
2498 else if (0 == strncasecmp (action_exp, "$loc", 4))
2499 locals_info (NULL, from_tty);
2500 else if (0 == strncasecmp (action_exp, "$arg", 4))
2501 args_info (NULL, from_tty);
2502 else
2503 { /* variable */
2504 if (next_comma)
2505 {
2506 make_cleanup (replace_comma, next_comma);
2507 *next_comma = '\0';
2508 }
2509 printf_filtered ("%s = ", action_exp);
2510 output_command (action_exp, from_tty);
2511 printf_filtered ("\n");
2512 }
2513 if (next_comma)
2514 *next_comma = ',';
2515 action_exp = next_comma;
2516 }
2517 while (action_exp && *action_exp == ',');
c906108c
SS
2518 }
2519 }
2520 }
2521 discard_cleanups (old_cleanups);
2522}
2523
2524/* Convert the memory pointed to by mem into hex, placing result in buf.
2525 * Return a pointer to the last char put in buf (null)
2526 * "stolen" from sparc-stub.c
2527 */
2528
c5aa993b 2529static const char hexchars[] = "0123456789abcdef";
c906108c 2530
47b667de
AC
2531static char *
2532mem2hex (gdb_byte *mem, char *buf, int count)
c906108c 2533{
47b667de 2534 gdb_byte ch;
c906108c
SS
2535
2536 while (count-- > 0)
2537 {
2538 ch = *mem++;
2539
2540 *buf++ = hexchars[ch >> 4];
2541 *buf++ = hexchars[ch & 0xf];
2542 }
2543
2544 *buf = 0;
2545
2546 return buf;
2547}
2548
c5aa993b 2549int
fba45db2 2550get_traceframe_number (void)
c906108c 2551{
c5aa993b 2552 return traceframe_number;
c906108c
SS
2553}
2554
2555
2556/* module initialization */
2557void
fba45db2 2558_initialize_tracepoint (void)
c906108c 2559{
fa58ee11
EZ
2560 struct cmd_list_element *c;
2561
c906108c
SS
2562 traceframe_number = -1;
2563 tracepoint_number = -1;
2564
c906108c
SS
2565 if (tracepoint_list.list == NULL)
2566 {
2567 tracepoint_list.listsize = 128;
c5aa993b 2568 tracepoint_list.list = xmalloc
c906108c
SS
2569 (tracepoint_list.listsize * sizeof (struct memrange));
2570 }
2571 if (tracepoint_list.aexpr_list == NULL)
2572 {
2573 tracepoint_list.aexpr_listsize = 128;
2574 tracepoint_list.aexpr_list = xmalloc
2575 (tracepoint_list.aexpr_listsize * sizeof (struct agent_expr *));
2576 }
2577
2578 if (stepping_list.list == NULL)
2579 {
2580 stepping_list.listsize = 128;
c5aa993b 2581 stepping_list.list = xmalloc
c906108c
SS
2582 (stepping_list.listsize * sizeof (struct memrange));
2583 }
2584
2585 if (stepping_list.aexpr_list == NULL)
2586 {
2587 stepping_list.aexpr_listsize = 128;
2588 stepping_list.aexpr_list = xmalloc
2589 (stepping_list.aexpr_listsize * sizeof (struct agent_expr *));
2590 }
2591
c5aa993b 2592 add_info ("scope", scope_info,
1bedd215 2593 _("List the variables local to a scope"));
c906108c 2594
e00d1dc8 2595 add_cmd ("tracepoints", class_trace, NULL,
1a966eab 2596 _("Tracing of program execution without stopping the program."),
c906108c
SS
2597 &cmdlist);
2598
c5aa993b 2599 add_com ("tdump", class_trace, trace_dump_command,
1bedd215 2600 _("Print everything collected at the current tracepoint."));
c906108c 2601
f61e138d
SS
2602 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
2603Define a trace state variable.\n\
2604Argument is a $-prefixed name, optionally followed\n\
2605by '=' and an expression that sets the initial value\n\
2606at the start of tracing."));
2607 set_cmd_completer (c, expression_completer);
2608
2609 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
2610Delete one or more trace state variables.\n\
2611Arguments are the names of the variables to delete.\n\
2612If no arguments are supplied, delete all variables."), &deletelist);
2613 /* FIXME add a trace variable completer */
2614
2615 add_info ("tvariables", tvariables_info, _("\
2616Status of trace state variables and their values.\n\
2617"));
2618
1bedd215
AC
2619 add_prefix_cmd ("tfind", class_trace, trace_find_command, _("\
2620Select a trace frame;\n\
2621No argument means forward by one frame; '-' means backward by one frame."),
c906108c
SS
2622 &tfindlist, "tfind ", 1, &cmdlist);
2623
1a966eab
AC
2624 add_cmd ("outside", class_trace, trace_find_outside_command, _("\
2625Select a trace frame whose PC is outside the given range.\n\
2626Usage: tfind outside addr1, addr2"),
c906108c
SS
2627 &tfindlist);
2628
1a966eab
AC
2629 add_cmd ("range", class_trace, trace_find_range_command, _("\
2630Select a trace frame whose PC is in the given range.\n\
2631Usage: tfind range addr1,addr2"),
c906108c
SS
2632 &tfindlist);
2633
1a966eab
AC
2634 add_cmd ("line", class_trace, trace_find_line_command, _("\
2635Select a trace frame by source line.\n\
c906108c
SS
2636Argument can be a line number (with optional source file), \n\
2637a function name, or '*' followed by an address.\n\
1a966eab 2638Default argument is 'the next source line that was traced'."),
c906108c
SS
2639 &tfindlist);
2640
1a966eab
AC
2641 add_cmd ("tracepoint", class_trace, trace_find_tracepoint_command, _("\
2642Select a trace frame by tracepoint number.\n\
2643Default is the tracepoint for the current trace frame."),
c906108c
SS
2644 &tfindlist);
2645
1a966eab
AC
2646 add_cmd ("pc", class_trace, trace_find_pc_command, _("\
2647Select a trace frame by PC.\n\
2648Default is the current PC, or the PC of the current trace frame."),
c906108c
SS
2649 &tfindlist);
2650
1a966eab
AC
2651 add_cmd ("end", class_trace, trace_find_end_command, _("\
2652Synonym for 'none'.\n\
2653De-select any trace frame and resume 'live' debugging."),
c906108c
SS
2654 &tfindlist);
2655
2656 add_cmd ("none", class_trace, trace_find_none_command,
1a966eab 2657 _("De-select any trace frame and resume 'live' debugging."),
c906108c
SS
2658 &tfindlist);
2659
2660 add_cmd ("start", class_trace, trace_find_start_command,
1a966eab 2661 _("Select the first trace frame in the trace buffer."),
c906108c
SS
2662 &tfindlist);
2663
c5aa993b 2664 add_com ("tstatus", class_trace, trace_status_command,
1bedd215 2665 _("Display the status of the current trace data collection."));
c906108c 2666
c5aa993b 2667 add_com ("tstop", class_trace, trace_stop_command,
1bedd215 2668 _("Stop trace data collection."));
c906108c
SS
2669
2670 add_com ("tstart", class_trace, trace_start_command,
1bedd215 2671 _("Start trace data collection."));
c906108c 2672
1bedd215
AC
2673 add_com ("end", class_trace, end_actions_pseudocommand, _("\
2674Ends a list of commands or actions.\n\
c906108c
SS
2675Several GDB commands allow you to enter a list of commands or actions.\n\
2676Entering \"end\" on a line by itself is the normal way to terminate\n\
2677such a list.\n\n\
1bedd215 2678Note: the \"end\" command cannot be used at the gdb prompt."));
c906108c 2679
1bedd215
AC
2680 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
2681Specify single-stepping behavior at a tracepoint.\n\
c906108c
SS
2682Argument is number of instructions to trace in single-step mode\n\
2683following the tracepoint. This command is normally followed by\n\
2684one or more \"collect\" commands, to specify what to collect\n\
2685while single-stepping.\n\n\
1bedd215 2686Note: this command can only be used in a tracepoint \"actions\" list."));
c906108c 2687
c5aa993b
JM
2688 add_com_alias ("ws", "while-stepping", class_alias, 0);
2689 add_com_alias ("stepping", "while-stepping", class_alias, 0);
c906108c 2690
1bedd215
AC
2691 add_com ("collect", class_trace, collect_pseudocommand, _("\
2692Specify one or more data items to be collected at a tracepoint.\n\
c906108c
SS
2693Accepts a comma-separated list of (one or more) expressions. GDB will\n\
2694collect all data (variables, registers) referenced by that expression.\n\
2695Also accepts the following special arguments:\n\
2696 $regs -- all registers.\n\
2697 $args -- all function arguments.\n\
2698 $locals -- all variables local to the block/function scope.\n\
1bedd215 2699Note: this command can only be used in a tracepoint \"actions\" list."));
c906108c 2700
6da95a67
SS
2701 add_com ("teval", class_trace, teval_pseudocommand, _("\
2702Specify one or more expressions to be evaluated at a tracepoint.\n\
2703Accepts a comma-separated list of (one or more) expressions.\n\
2704The result of each evaluation will be discarded.\n\
2705Note: this command can only be used in a tracepoint \"actions\" list."));
2706
1bedd215
AC
2707 add_com ("actions", class_trace, trace_actions_command, _("\
2708Specify the actions to be taken at a tracepoint.\n\
c906108c
SS
2709Tracepoint actions may include collecting of specified data, \n\
2710single-stepping, or enabling/disabling other tracepoints, \n\
1bedd215 2711depending on target's capabilities."));
c906108c 2712
236f1d4d
SS
2713 default_collect = xstrdup ("");
2714 add_setshow_string_cmd ("default-collect", class_trace,
2715 &default_collect, _("\
2716Set the list of expressions to collect by default"), _("\
2717Show the list of expressions to collect by default"), NULL,
2718 NULL, NULL,
2719 &setlist, &showlist);
2720
6d820c5c
DJ
2721 target_buf_size = 2048;
2722 target_buf = xmalloc (target_buf_size);
c906108c 2723}