* tracepoint.c (trace_find_command): Error out if trace running.
[deliverable/binutils-gdb.git] / gdb / tracepoint.c
CommitLineData
c906108c 1/* Tracing functionality for remote targets in custom GDB protocol
9f60d481 2
6aba47ca 3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
0fb0cc75 4 2007, 2008, 2009 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
528/* Enter a list of actions for a tracepoint. */
529static void
fba45db2 530trace_actions_command (char *args, int from_tty)
c906108c 531{
1042e4c0 532 struct breakpoint *t;
c906108c
SS
533 char tmpbuf[128];
534 char *end_msg = "End with a line saying just \"end\".";
535
c2d11a7d 536 t = get_tracepoint_by_number (&args, 0, 1);
7a292a7a 537 if (t)
c906108c
SS
538 {
539 sprintf (tmpbuf, "Enter actions for tracepoint %d, one per line.",
540 t->number);
541
542 if (from_tty)
543 {
9a4105ab
AC
544 if (deprecated_readline_begin_hook)
545 (*deprecated_readline_begin_hook) ("%s %s\n", tmpbuf, end_msg);
c906108c
SS
546 else if (input_from_terminal_p ())
547 printf_filtered ("%s\n%s\n", tmpbuf, end_msg);
548 }
549
550 free_actions (t);
551 t->step_count = 0; /* read_actions may set this */
552 read_actions (t);
553
9a4105ab
AC
554 if (deprecated_readline_end_hook)
555 (*deprecated_readline_end_hook) ();
c906108c
SS
556 /* tracepoints_changed () */
557 }
5c44784c 558 /* else just return */
c906108c
SS
559}
560
561/* worker function */
562static void
1042e4c0 563read_actions (struct breakpoint *t)
c906108c
SS
564{
565 char *line;
566 char *prompt1 = "> ", *prompt2 = " > ";
567 char *prompt = prompt1;
568 enum actionline_type linetype;
569 extern FILE *instream;
570 struct action_line *next = NULL, *temp;
571 struct cleanup *old_chain;
572
573 /* Control-C quits instantly if typed while in this loop
574 since it should not wait until the user types a newline. */
575 immediate_quit++;
1a6fae3c
MK
576 /* FIXME: kettenis/20010823: Something is wrong here. In this file
577 STOP_SIGNAL is never defined. So this code has been left out, at
578 least for quite a while now. Replacing STOP_SIGNAL with SIGTSTP
579 leads to compilation failures since the variable job_control
580 isn't declared. Leave this alone for now. */
c906108c
SS
581#ifdef STOP_SIGNAL
582 if (job_control)
362646f5 583 signal (STOP_SIGNAL, handle_stop_sig);
c906108c 584#endif
74b7792f 585 old_chain = make_cleanup_free_actions (t);
c906108c
SS
586 while (1)
587 {
d183932d
MS
588 /* Make sure that all output has been output. Some machines may
589 let you get away with leaving out some of the gdb_flush, but
590 not all. */
c906108c
SS
591 wrap_here ("");
592 gdb_flush (gdb_stdout);
593 gdb_flush (gdb_stderr);
594
9a4105ab
AC
595 if (deprecated_readline_hook && instream == NULL)
596 line = (*deprecated_readline_hook) (prompt);
c906108c
SS
597 else if (instream == stdin && ISATTY (instream))
598 {
b4f5539f 599 line = gdb_readline_wrapper (prompt);
c5aa993b 600 if (line && *line) /* add it to command history */
c906108c
SS
601 add_history (line);
602 }
603 else
604 line = gdb_readline (0);
605
549678da 606 if (!line)
d844e34b
JB
607 {
608 line = xstrdup ("end");
609 printf_filtered ("end\n");
610 }
549678da 611
c906108c
SS
612 linetype = validate_actionline (&line, t);
613 if (linetype == BADLINE)
c5aa993b 614 continue; /* already warned -- collect another line */
c906108c
SS
615
616 temp = xmalloc (sizeof (struct action_line));
617 temp->next = NULL;
618 temp->action = line;
619
620 if (next == NULL) /* first action for this tracepoint? */
621 t->actions = next = temp;
622 else
623 {
624 next->next = temp;
625 next = temp;
626 }
627
628 if (linetype == STEPPING) /* begin "while-stepping" */
7a292a7a
SS
629 {
630 if (prompt == prompt2)
631 {
8a3fe4f8 632 warning (_("Already processing 'while-stepping'"));
7a292a7a
SS
633 continue;
634 }
635 else
636 prompt = prompt2; /* change prompt for stepping actions */
637 }
c906108c 638 else if (linetype == END)
7a292a7a
SS
639 {
640 if (prompt == prompt2)
641 {
642 prompt = prompt1; /* end of single-stepping actions */
643 }
644 else
c5aa993b 645 { /* end of actions */
7a292a7a
SS
646 if (t->actions->next == NULL)
647 {
d183932d
MS
648 /* An "end" all by itself with no other actions
649 means this tracepoint has no actions.
650 Discard empty list. */
7a292a7a
SS
651 free_actions (t);
652 }
653 break;
654 }
655 }
c906108c
SS
656 }
657#ifdef STOP_SIGNAL
658 if (job_control)
659 signal (STOP_SIGNAL, SIG_DFL);
660#endif
8edbea78 661 immediate_quit--;
c906108c
SS
662 discard_cleanups (old_chain);
663}
664
665/* worker function */
666enum actionline_type
1042e4c0 667validate_actionline (char **line, struct breakpoint *t)
c906108c
SS
668{
669 struct cmd_list_element *c;
670 struct expression *exp = NULL;
c906108c
SS
671 struct cleanup *old_chain = NULL;
672 char *p;
673
15255275
MS
674 /* if EOF is typed, *line is NULL */
675 if (*line == NULL)
676 return END;
677
104c1213 678 for (p = *line; isspace ((int) *p);)
c906108c
SS
679 p++;
680
d183932d
MS
681 /* Symbol lookup etc. */
682 if (*p == '\0') /* empty line: just prompt for another line. */
c906108c
SS
683 return BADLINE;
684
c5aa993b 685 if (*p == '#') /* comment line */
c906108c
SS
686 return GENERIC;
687
688 c = lookup_cmd (&p, cmdlist, "", -1, 1);
689 if (c == 0)
690 {
8a3fe4f8 691 warning (_("'%s' is not an action that I know, or is ambiguous."),
d183932d 692 p);
c906108c
SS
693 return BADLINE;
694 }
c5aa993b 695
bbaca940 696 if (cmd_cfunc_eq (c, collect_pseudocommand))
c906108c
SS
697 {
698 struct agent_expr *aexpr;
699 struct agent_reqs areqs;
700
c5aa993b
JM
701 do
702 { /* repeat over a comma-separated list */
703 QUIT; /* allow user to bail out with ^C */
104c1213 704 while (isspace ((int) *p))
c5aa993b 705 p++;
c906108c 706
c5aa993b
JM
707 if (*p == '$') /* look for special pseudo-symbols */
708 {
c5aa993b
JM
709 if ((0 == strncasecmp ("reg", p + 1, 3)) ||
710 (0 == strncasecmp ("arg", p + 1, 3)) ||
711 (0 == strncasecmp ("loc", p + 1, 3)))
712 {
713 p = strchr (p, ',');
714 continue;
715 }
d183932d 716 /* else fall thru, treat p as an expression and parse it! */
c5aa993b 717 }
1042e4c0 718 exp = parse_exp_1 (&p, block_for_pc (t->loc->address), 1);
c13c43fd 719 old_chain = make_cleanup (free_current_contents, &exp);
c906108c 720
c5aa993b
JM
721 if (exp->elts[0].opcode == OP_VAR_VALUE)
722 {
723 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
724 {
8a3fe4f8 725 warning (_("constant %s (value %ld) will not be collected."),
3567439c 726 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
c5aa993b
JM
727 SYMBOL_VALUE (exp->elts[2].symbol));
728 return BADLINE;
729 }
730 else if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_OPTIMIZED_OUT)
731 {
8a3fe4f8 732 warning (_("%s is optimized away and cannot be collected."),
3567439c 733 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
c5aa993b
JM
734 return BADLINE;
735 }
736 }
c906108c 737
d183932d
MS
738 /* We have something to collect, make sure that the expr to
739 bytecode translator can handle it and that it's not too
740 long. */
1042e4c0 741 aexpr = gen_trace_for_expr (t->loc->address, exp);
f23d52e0 742 make_cleanup_free_agent_expr (aexpr);
c906108c 743
c5aa993b 744 if (aexpr->len > MAX_AGENT_EXPR_LEN)
8a3fe4f8 745 error (_("expression too complicated, try simplifying"));
c906108c 746
c5aa993b 747 ax_reqs (aexpr, &areqs);
b8c9b27d 748 (void) make_cleanup (xfree, areqs.reg_mask);
c906108c 749
c5aa993b 750 if (areqs.flaw != agent_flaw_none)
8a3fe4f8 751 error (_("malformed expression"));
c906108c 752
c5aa993b 753 if (areqs.min_height < 0)
8a3fe4f8 754 error (_("gdb: Internal error: expression has min height < 0"));
c906108c 755
c5aa993b 756 if (areqs.max_height > 20)
8a3fe4f8 757 error (_("expression too complicated, try simplifying"));
c906108c 758
c5aa993b
JM
759 do_cleanups (old_chain);
760 }
761 while (p && *p++ == ',');
c906108c
SS
762 return GENERIC;
763 }
bbaca940 764 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
c906108c 765 {
c5aa993b 766 char *steparg; /* in case warning is necessary */
c906108c 767
104c1213 768 while (isspace ((int) *p))
c906108c
SS
769 p++;
770 steparg = p;
771
772 if (*p == '\0' ||
773 (t->step_count = strtol (p, &p, 0)) == 0)
774 {
8a3fe4f8 775 warning (_("'%s': bad step-count; command ignored."), *line);
c906108c
SS
776 return BADLINE;
777 }
778 return STEPPING;
779 }
bbaca940 780 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
c906108c
SS
781 return END;
782 else
783 {
8a3fe4f8 784 warning (_("'%s' is not a supported tracepoint action."), *line);
c906108c
SS
785 return BADLINE;
786 }
787}
788
789/* worker function */
c5aa993b 790void
1042e4c0 791free_actions (struct breakpoint *t)
c906108c
SS
792{
793 struct action_line *line, *next;
794
795 for (line = t->actions; line; line = next)
796 {
797 next = line->next;
c5aa993b 798 if (line->action)
b8c9b27d
KB
799 xfree (line->action);
800 xfree (line);
c906108c
SS
801 }
802 t->actions = NULL;
803}
804
74b7792f
AC
805static void
806do_free_actions_cleanup (void *t)
807{
808 free_actions (t);
809}
810
811static struct cleanup *
1042e4c0 812make_cleanup_free_actions (struct breakpoint *t)
74b7792f
AC
813{
814 return make_cleanup (do_free_actions_cleanup, t);
815}
816
f50e79a4
JB
817enum {
818 memrange_absolute = -1
819};
820
c5aa993b
JM
821struct memrange
822{
f50e79a4
JB
823 int type; /* memrange_absolute for absolute memory range,
824 else basereg number */
c906108c
SS
825 bfd_signed_vma start;
826 bfd_signed_vma end;
827};
828
c5aa993b
JM
829struct collection_list
830 {
549678da 831 unsigned char regs_mask[32]; /* room for up to 256 regs */
c5aa993b
JM
832 long listsize;
833 long next_memrange;
834 struct memrange *list;
835 long aexpr_listsize; /* size of array pointed to by expr_list elt */
836 long next_aexpr_elt;
837 struct agent_expr **aexpr_list;
838
839 }
840tracepoint_list, stepping_list;
c906108c
SS
841
842/* MEMRANGE functions: */
843
a14ed312 844static int memrange_cmp (const void *, const void *);
c906108c
SS
845
846/* compare memranges for qsort */
847static int
fba45db2 848memrange_cmp (const void *va, const void *vb)
c906108c
SS
849{
850 const struct memrange *a = va, *b = vb;
851
852 if (a->type < b->type)
853 return -1;
854 if (a->type > b->type)
c5aa993b 855 return 1;
f50e79a4 856 if (a->type == memrange_absolute)
c906108c 857 {
c5aa993b
JM
858 if ((bfd_vma) a->start < (bfd_vma) b->start)
859 return -1;
860 if ((bfd_vma) a->start > (bfd_vma) b->start)
861 return 1;
c906108c
SS
862 }
863 else
864 {
c5aa993b 865 if (a->start < b->start)
c906108c 866 return -1;
c5aa993b
JM
867 if (a->start > b->start)
868 return 1;
c906108c
SS
869 }
870 return 0;
871}
872
d183932d 873/* Sort the memrange list using qsort, and merge adjacent memranges. */
c906108c 874static void
fba45db2 875memrange_sortmerge (struct collection_list *memranges)
c906108c
SS
876{
877 int a, b;
878
c5aa993b 879 qsort (memranges->list, memranges->next_memrange,
c906108c
SS
880 sizeof (struct memrange), memrange_cmp);
881 if (memranges->next_memrange > 0)
882 {
883 for (a = 0, b = 1; b < memranges->next_memrange; b++)
884 {
885 if (memranges->list[a].type == memranges->list[b].type &&
c5aa993b 886 memranges->list[b].start - memranges->list[a].end <=
0c92afe8 887 MAX_REGISTER_SIZE)
c906108c
SS
888 {
889 /* memrange b starts before memrange a ends; merge them. */
890 if (memranges->list[b].end > memranges->list[a].end)
891 memranges->list[a].end = memranges->list[b].end;
892 continue; /* next b, same a */
893 }
894 a++; /* next a */
895 if (a != b)
c5aa993b 896 memcpy (&memranges->list[a], &memranges->list[b],
c906108c
SS
897 sizeof (struct memrange));
898 }
899 memranges->next_memrange = a + 1;
900 }
901}
902
d183932d 903/* Add a register to a collection list. */
392a587b 904static void
fba45db2 905add_register (struct collection_list *collection, unsigned int regno)
c906108c
SS
906{
907 if (info_verbose)
908 printf_filtered ("collect register %d\n", regno);
27e06d3e 909 if (regno >= (8 * sizeof (collection->regs_mask)))
8a3fe4f8 910 error (_("Internal: register number %d too large for tracepoint"),
c906108c 911 regno);
c5aa993b 912 collection->regs_mask[regno / 8] |= 1 << (regno % 8);
c906108c
SS
913}
914
915/* Add a memrange to a collection list */
916static void
d183932d
MS
917add_memrange (struct collection_list *memranges,
918 int type, bfd_signed_vma base,
fba45db2 919 unsigned long len)
c906108c
SS
920{
921 if (info_verbose)
104c1213
JM
922 {
923 printf_filtered ("(%d,", type);
924 printf_vma (base);
925 printf_filtered (",%ld)\n", len);
926 }
927
f50e79a4 928 /* type: memrange_absolute == memory, other n == basereg */
c5aa993b 929 memranges->list[memranges->next_memrange].type = type;
d183932d 930 /* base: addr if memory, offset if reg relative. */
c906108c
SS
931 memranges->list[memranges->next_memrange].start = base;
932 /* len: we actually save end (base + len) for convenience */
c5aa993b 933 memranges->list[memranges->next_memrange].end = base + len;
c906108c
SS
934 memranges->next_memrange++;
935 if (memranges->next_memrange >= memranges->listsize)
936 {
937 memranges->listsize *= 2;
c5aa993b 938 memranges->list = xrealloc (memranges->list,
c906108c
SS
939 memranges->listsize);
940 }
941
f50e79a4 942 if (type != memrange_absolute) /* Better collect the base register! */
c906108c
SS
943 add_register (memranges, type);
944}
945
d183932d 946/* Add a symbol to a collection list. */
c906108c 947static void
d183932d
MS
948collect_symbol (struct collection_list *collect,
949 struct symbol *sym,
a6d9a66e 950 struct gdbarch *gdbarch,
0936ad1d
SS
951 long frame_regno, long frame_offset,
952 CORE_ADDR scope)
c906108c 953{
c5aa993b 954 unsigned long len;
104c1213 955 unsigned int reg;
c906108c
SS
956 bfd_signed_vma offset;
957
c5aa993b
JM
958 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
959 switch (SYMBOL_CLASS (sym))
960 {
961 default:
962 printf_filtered ("%s: don't know symbol class %d\n",
3567439c 963 SYMBOL_PRINT_NAME (sym),
d183932d 964 SYMBOL_CLASS (sym));
c5aa993b
JM
965 break;
966 case LOC_CONST:
104c1213 967 printf_filtered ("constant %s (value %ld) will not be collected.\n",
3567439c 968 SYMBOL_PRINT_NAME (sym), SYMBOL_VALUE (sym));
c5aa993b
JM
969 break;
970 case LOC_STATIC:
971 offset = SYMBOL_VALUE_ADDRESS (sym);
972 if (info_verbose)
104c1213
JM
973 {
974 char tmp[40];
975
976 sprintf_vma (tmp, offset);
977 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
3567439c 978 SYMBOL_PRINT_NAME (sym), len,
d183932d 979 tmp /* address */);
104c1213 980 }
f50e79a4 981 add_memrange (collect, memrange_absolute, offset, len);
c5aa993b
JM
982 break;
983 case LOC_REGISTER:
a6d9a66e 984 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
c5aa993b 985 if (info_verbose)
d183932d 986 printf_filtered ("LOC_REG[parm] %s: ",
3567439c 987 SYMBOL_PRINT_NAME (sym));
c5aa993b 988 add_register (collect, reg);
d183932d
MS
989 /* Check for doubles stored in two registers. */
990 /* FIXME: how about larger types stored in 3 or more regs? */
c5aa993b 991 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
a6d9a66e 992 len > register_size (gdbarch, reg))
c5aa993b
JM
993 add_register (collect, reg + 1);
994 break;
995 case LOC_REF_ARG:
996 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
997 printf_filtered (" (will not collect %s)\n",
3567439c 998 SYMBOL_PRINT_NAME (sym));
c5aa993b
JM
999 break;
1000 case LOC_ARG:
1001 reg = frame_regno;
1002 offset = frame_offset + SYMBOL_VALUE (sym);
1003 if (info_verbose)
1004 {
104c1213 1005 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
3567439c 1006 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1007 printf_vma (offset);
1008 printf_filtered (" from frame ptr reg %d\n", reg);
c5aa993b
JM
1009 }
1010 add_memrange (collect, reg, offset, len);
1011 break;
1012 case LOC_REGPARM_ADDR:
1013 reg = SYMBOL_VALUE (sym);
1014 offset = 0;
1015 if (info_verbose)
1016 {
104c1213 1017 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset ",
3567439c 1018 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1019 printf_vma (offset);
1020 printf_filtered (" from reg %d\n", reg);
c5aa993b
JM
1021 }
1022 add_memrange (collect, reg, offset, len);
1023 break;
1024 case LOC_LOCAL:
c5aa993b
JM
1025 reg = frame_regno;
1026 offset = frame_offset + SYMBOL_VALUE (sym);
1027 if (info_verbose)
1028 {
104c1213 1029 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
3567439c 1030 SYMBOL_PRINT_NAME (sym), len);
104c1213
JM
1031 printf_vma (offset);
1032 printf_filtered (" from frame ptr reg %d\n", reg);
c5aa993b
JM
1033 }
1034 add_memrange (collect, reg, offset, len);
1035 break;
c5aa993b 1036 case LOC_UNRESOLVED:
d183932d 1037 printf_filtered ("Don't know LOC_UNRESOLVED %s\n",
3567439c 1038 SYMBOL_PRINT_NAME (sym));
c5aa993b
JM
1039 break;
1040 case LOC_OPTIMIZED_OUT:
8e1a459b 1041 printf_filtered ("%s has been optimized out of existence.\n",
3567439c 1042 SYMBOL_PRINT_NAME (sym));
c5aa993b 1043 break;
0936ad1d
SS
1044
1045 case LOC_COMPUTED:
1046 {
1047 struct agent_expr *aexpr;
1048 struct cleanup *old_chain1 = NULL;
1049 struct agent_reqs areqs;
1050
1051 aexpr = gen_trace_for_var (scope, sym);
1052
1053 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1054
1055 ax_reqs (aexpr, &areqs);
1056 if (areqs.flaw != agent_flaw_none)
1057 error (_("malformed expression"));
1058
1059 if (areqs.min_height < 0)
1060 error (_("gdb: Internal error: expression has min height < 0"));
1061 if (areqs.max_height > 20)
1062 error (_("expression too complicated, try simplifying"));
1063
1064 discard_cleanups (old_chain1);
1065 add_aexpr (collect, aexpr);
1066
1067 /* take care of the registers */
1068 if (areqs.reg_mask_len > 0)
1069 {
1070 int ndx1, ndx2;
1071
1072 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
1073 {
1074 QUIT; /* allow user to bail out with ^C */
1075 if (areqs.reg_mask[ndx1] != 0)
1076 {
1077 /* assume chars have 8 bits */
1078 for (ndx2 = 0; ndx2 < 8; ndx2++)
1079 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1080 /* it's used -- record it */
1081 add_register (collect,
1082 ndx1 * 8 + ndx2);
1083 }
1084 }
1085 }
1086 }
1087 break;
c5aa993b 1088 }
c906108c
SS
1089}
1090
1091/* Add all locals (or args) symbols to collection list */
1092static void
a6d9a66e
UW
1093add_local_symbols (struct collection_list *collect,
1094 struct gdbarch *gdbarch, CORE_ADDR pc,
fba45db2 1095 long frame_regno, long frame_offset, int type)
c906108c
SS
1096{
1097 struct symbol *sym;
c5aa993b 1098 struct block *block;
de4f826b
DC
1099 struct dict_iterator iter;
1100 int count = 0;
c906108c
SS
1101
1102 block = block_for_pc (pc);
1103 while (block != 0)
1104 {
c5aa993b 1105 QUIT; /* allow user to bail out with ^C */
de4f826b 1106 ALL_BLOCK_SYMBOLS (block, iter, sym)
c906108c 1107 {
2a2d4dc3
AS
1108 if (SYMBOL_IS_ARGUMENT (sym)
1109 ? type == 'A' /* collecting Arguments */
1110 : type == 'L') /* collecting Locals */
c5aa993b 1111 {
2a2d4dc3 1112 count++;
a6d9a66e 1113 collect_symbol (collect, sym, gdbarch,
0936ad1d 1114 frame_regno, frame_offset, pc);
c5aa993b 1115 }
c906108c
SS
1116 }
1117 if (BLOCK_FUNCTION (block))
1118 break;
1119 else
1120 block = BLOCK_SUPERBLOCK (block);
1121 }
1122 if (count == 0)
8a3fe4f8 1123 warning (_("No %s found in scope."),
d183932d 1124 type == 'L' ? "locals" : "args");
c906108c
SS
1125}
1126
1127/* worker function */
1128static void
fba45db2 1129clear_collection_list (struct collection_list *list)
c906108c
SS
1130{
1131 int ndx;
1132
1133 list->next_memrange = 0;
1134 for (ndx = 0; ndx < list->next_aexpr_elt; ndx++)
1135 {
c5aa993b 1136 free_agent_expr (list->aexpr_list[ndx]);
c906108c
SS
1137 list->aexpr_list[ndx] = NULL;
1138 }
1139 list->next_aexpr_elt = 0;
1140 memset (list->regs_mask, 0, sizeof (list->regs_mask));
1141}
1142
1143/* reduce a collection list to string form (for gdb protocol) */
1144static char **
fba45db2 1145stringify_collection_list (struct collection_list *list, char *string)
c906108c
SS
1146{
1147 char temp_buf[2048];
104c1213 1148 char tmp2[40];
c906108c
SS
1149 int count;
1150 int ndx = 0;
1151 char *(*str_list)[];
1152 char *end;
c5aa993b 1153 long i;
c906108c
SS
1154
1155 count = 1 + list->next_memrange + list->next_aexpr_elt + 1;
c5aa993b 1156 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
c906108c
SS
1157
1158 for (i = sizeof (list->regs_mask) - 1; i > 0; i--)
1159 if (list->regs_mask[i] != 0) /* skip leading zeroes in regs_mask */
1160 break;
1161 if (list->regs_mask[i] != 0) /* prepare to send regs_mask to the stub */
1162 {
1163 if (info_verbose)
1164 printf_filtered ("\nCollecting registers (mask): 0x");
1165 end = temp_buf;
c5aa993b 1166 *end++ = 'R';
c906108c
SS
1167 for (; i >= 0; i--)
1168 {
c5aa993b 1169 QUIT; /* allow user to bail out with ^C */
c906108c
SS
1170 if (info_verbose)
1171 printf_filtered ("%02X", list->regs_mask[i]);
c5aa993b 1172 sprintf (end, "%02X", list->regs_mask[i]);
c906108c
SS
1173 end += 2;
1174 }
1b36a34b 1175 (*str_list)[ndx] = xstrdup (temp_buf);
c906108c
SS
1176 ndx++;
1177 }
1178 if (info_verbose)
1179 printf_filtered ("\n");
1180 if (list->next_memrange > 0 && info_verbose)
1181 printf_filtered ("Collecting memranges: \n");
1182 for (i = 0, count = 0, end = temp_buf; i < list->next_memrange; i++)
1183 {
1184 QUIT; /* allow user to bail out with ^C */
104c1213 1185 sprintf_vma (tmp2, list->list[i].start);
c906108c 1186 if (info_verbose)
104c1213
JM
1187 {
1188 printf_filtered ("(%d, %s, %ld)\n",
1189 list->list[i].type,
1190 tmp2,
1191 (long) (list->list[i].end - list->list[i].start));
1192 }
c906108c
SS
1193 if (count + 27 > MAX_AGENT_EXPR_LEN)
1194 {
c5aa993b 1195 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1196 ndx++;
1197 count = 0;
1198 end = temp_buf;
1199 }
104c1213 1200
d1948716
JB
1201 {
1202 bfd_signed_vma length = list->list[i].end - list->list[i].start;
1203
1204 /* The "%X" conversion specifier expects an unsigned argument,
f50e79a4
JB
1205 so passing -1 (memrange_absolute) to it directly gives you
1206 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1207 Special-case it. */
1208 if (list->list[i].type == memrange_absolute)
d1948716
JB
1209 sprintf (end, "M-1,%s,%lX", tmp2, (long) length);
1210 else
1211 sprintf (end, "M%X,%s,%lX", list->list[i].type, tmp2, (long) length);
1212 }
104c1213 1213
c906108c 1214 count += strlen (end);
3ffbc0a5 1215 end = temp_buf + count;
c906108c
SS
1216 }
1217
1218 for (i = 0; i < list->next_aexpr_elt; i++)
1219 {
1220 QUIT; /* allow user to bail out with ^C */
1221 if ((count + 10 + 2 * list->aexpr_list[i]->len) > MAX_AGENT_EXPR_LEN)
1222 {
c5aa993b 1223 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1224 ndx++;
1225 count = 0;
1226 end = temp_buf;
1227 }
1228 sprintf (end, "X%08X,", list->aexpr_list[i]->len);
1229 end += 10; /* 'X' + 8 hex digits + ',' */
1230 count += 10;
1231
d183932d
MS
1232 end = mem2hex (list->aexpr_list[i]->buf,
1233 end, list->aexpr_list[i]->len);
c906108c
SS
1234 count += 2 * list->aexpr_list[i]->len;
1235 }
1236
1237 if (count != 0)
1238 {
c5aa993b 1239 (*str_list)[ndx] = savestring (temp_buf, count);
c906108c
SS
1240 ndx++;
1241 count = 0;
1242 end = temp_buf;
1243 }
1244 (*str_list)[ndx] = NULL;
1245
1246 if (ndx == 0)
27e06d3e 1247 {
6c761d9c 1248 xfree (str_list);
27e06d3e
MS
1249 return NULL;
1250 }
c906108c
SS
1251 else
1252 return *str_list;
1253}
1254
392a587b 1255static void
fba45db2 1256free_actions_list_cleanup_wrapper (void *al)
392a587b
JM
1257{
1258 free_actions_list (al);
1259}
1260
1261static void
fba45db2 1262free_actions_list (char **actions_list)
c906108c
SS
1263{
1264 int ndx;
1265
1266 if (actions_list == 0)
1267 return;
1268
1269 for (ndx = 0; actions_list[ndx]; ndx++)
b8c9b27d 1270 xfree (actions_list[ndx]);
c906108c 1271
b8c9b27d 1272 xfree (actions_list);
c906108c
SS
1273}
1274
d183932d 1275/* Render all actions into gdb protocol. */
c906108c 1276static void
1042e4c0 1277encode_actions (struct breakpoint *t, char ***tdp_actions,
fba45db2 1278 char ***stepping_actions)
c906108c 1279{
c5aa993b
JM
1280 static char tdp_buff[2048], step_buff[2048];
1281 char *action_exp;
1282 struct expression *exp = NULL;
c906108c 1283 struct action_line *action;
104c1213 1284 int i;
f976f6d4 1285 struct value *tempval;
c5aa993b 1286 struct collection_list *collect;
c906108c
SS
1287 struct cmd_list_element *cmd;
1288 struct agent_expr *aexpr;
39d4ef09
AC
1289 int frame_reg;
1290 LONGEST frame_offset;
236f1d4d
SS
1291 char *default_collect_line = NULL;
1292 struct action_line *default_collect_action = NULL;
c906108c
SS
1293
1294 clear_collection_list (&tracepoint_list);
1295 clear_collection_list (&stepping_list);
1296 collect = &tracepoint_list;
1297
1298 *tdp_actions = NULL;
1299 *stepping_actions = NULL;
1300
a6d9a66e 1301 gdbarch_virtual_frame_pointer (t->gdbarch,
1042e4c0 1302 t->loc->address, &frame_reg, &frame_offset);
c906108c 1303
236f1d4d
SS
1304 action = t->actions;
1305
1306 /* If there are default expressions to collect, make up a collect
1307 action and prepend to the action list to encode. Note that since
1308 validation is per-tracepoint (local var "xyz" might be valid for
1309 one tracepoint and not another, etc), we make up the action on
1310 the fly, and don't cache it. */
1311 if (*default_collect)
1312 {
1313 char *line;
1314 enum actionline_type linetype;
1315
1316 default_collect_line = xmalloc (12 + strlen (default_collect));
1317 sprintf (default_collect_line, "collect %s", default_collect);
1318 line = default_collect_line;
1319 linetype = validate_actionline (&line, t);
1320 if (linetype != BADLINE)
1321 {
1322 default_collect_action = xmalloc (sizeof (struct action_line));
1323 default_collect_action->next = t->actions;
1324 default_collect_action->action = line;
1325 action = default_collect_action;
1326 }
1327 }
1328
1329 for (; action; action = action->next)
c906108c
SS
1330 {
1331 QUIT; /* allow user to bail out with ^C */
1332 action_exp = action->action;
104c1213 1333 while (isspace ((int) *action_exp))
c906108c
SS
1334 action_exp++;
1335
1336 if (*action_exp == '#') /* comment line */
1337 return;
1338
1339 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1340 if (cmd == 0)
8a3fe4f8 1341 error (_("Bad action list item: %s"), action_exp);
c906108c 1342
bbaca940 1343 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
c906108c 1344 {
c5aa993b
JM
1345 do
1346 { /* repeat over a comma-separated list */
1347 QUIT; /* allow user to bail out with ^C */
104c1213 1348 while (isspace ((int) *action_exp))
c5aa993b 1349 action_exp++;
c906108c 1350
c5aa993b
JM
1351 if (0 == strncasecmp ("$reg", action_exp, 4))
1352 {
a6d9a66e 1353 for (i = 0; i < gdbarch_num_regs (t->gdbarch); i++)
c5aa993b
JM
1354 add_register (collect, i);
1355 action_exp = strchr (action_exp, ','); /* more? */
1356 }
1357 else if (0 == strncasecmp ("$arg", action_exp, 4))
1358 {
1359 add_local_symbols (collect,
a6d9a66e 1360 t->gdbarch,
1042e4c0 1361 t->loc->address,
c5aa993b
JM
1362 frame_reg,
1363 frame_offset,
1364 'A');
1365 action_exp = strchr (action_exp, ','); /* more? */
1366 }
1367 else if (0 == strncasecmp ("$loc", action_exp, 4))
1368 {
1369 add_local_symbols (collect,
a6d9a66e 1370 t->gdbarch,
1042e4c0 1371 t->loc->address,
c5aa993b
JM
1372 frame_reg,
1373 frame_offset,
1374 'L');
1375 action_exp = strchr (action_exp, ','); /* more? */
1376 }
1377 else
1378 {
1379 unsigned long addr, len;
1380 struct cleanup *old_chain = NULL;
1381 struct cleanup *old_chain1 = NULL;
1382 struct agent_reqs areqs;
1383
75ac9d7b 1384 exp = parse_exp_1 (&action_exp,
1042e4c0 1385 block_for_pc (t->loc->address), 1);
74b7792f 1386 old_chain = make_cleanup (free_current_contents, &exp);
c906108c 1387
c5aa993b
JM
1388 switch (exp->elts[0].opcode)
1389 {
1390 case OP_REGISTER:
67f3407f
DJ
1391 {
1392 const char *name = &exp->elts[2].string;
1393
a6d9a66e 1394 i = user_reg_map_name_to_regnum (t->gdbarch,
029a67e4 1395 name, strlen (name));
67f3407f
DJ
1396 if (i == -1)
1397 internal_error (__FILE__, __LINE__,
1398 _("Register $%s not available"),
1399 name);
1400 if (info_verbose)
1401 printf_filtered ("OP_REGISTER: ");
1402 add_register (collect, i);
1403 break;
1404 }
c5aa993b
JM
1405
1406 case UNOP_MEMVAL:
1407 /* safe because we know it's a simple expression */
1408 tempval = evaluate_expression (exp);
42ae5230 1409 addr = value_address (tempval);
c5aa993b 1410 len = TYPE_LENGTH (check_typedef (exp->elts[1].type));
f50e79a4 1411 add_memrange (collect, memrange_absolute, addr, len);
c5aa993b
JM
1412 break;
1413
1414 case OP_VAR_VALUE:
1415 collect_symbol (collect,
1416 exp->elts[2].symbol,
a6d9a66e 1417 t->gdbarch,
c5aa993b 1418 frame_reg,
0936ad1d
SS
1419 frame_offset,
1420 t->loc->address);
c5aa993b
JM
1421 break;
1422
1423 default: /* full-fledged expression */
1042e4c0 1424 aexpr = gen_trace_for_expr (t->loc->address, exp);
c5aa993b 1425
f23d52e0 1426 old_chain1 = make_cleanup_free_agent_expr (aexpr);
c5aa993b
JM
1427
1428 ax_reqs (aexpr, &areqs);
1429 if (areqs.flaw != agent_flaw_none)
8a3fe4f8 1430 error (_("malformed expression"));
c5aa993b
JM
1431
1432 if (areqs.min_height < 0)
8a3fe4f8 1433 error (_("gdb: Internal error: expression has min height < 0"));
c5aa993b 1434 if (areqs.max_height > 20)
8a3fe4f8 1435 error (_("expression too complicated, try simplifying"));
c5aa993b
JM
1436
1437 discard_cleanups (old_chain1);
1438 add_aexpr (collect, aexpr);
1439
1440 /* take care of the registers */
1441 if (areqs.reg_mask_len > 0)
c906108c 1442 {
c5aa993b
JM
1443 int ndx1;
1444 int ndx2;
1445
1446 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
c906108c 1447 {
c5aa993b
JM
1448 QUIT; /* allow user to bail out with ^C */
1449 if (areqs.reg_mask[ndx1] != 0)
1450 {
1451 /* assume chars have 8 bits */
1452 for (ndx2 = 0; ndx2 < 8; ndx2++)
1453 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1454 /* it's used -- record it */
d183932d
MS
1455 add_register (collect,
1456 ndx1 * 8 + ndx2);
c5aa993b 1457 }
c906108c
SS
1458 }
1459 }
c5aa993b
JM
1460 break;
1461 } /* switch */
1462 do_cleanups (old_chain);
1463 } /* do */
1464 }
1465 while (action_exp && *action_exp++ == ',');
1466 } /* if */
bbaca940 1467 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
c906108c
SS
1468 {
1469 collect = &stepping_list;
1470 }
bbaca940 1471 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
c906108c
SS
1472 {
1473 if (collect == &stepping_list) /* end stepping actions */
1474 collect = &tracepoint_list;
1475 else
c5aa993b 1476 break; /* end tracepoint actions */
c906108c 1477 }
c5aa993b
JM
1478 } /* for */
1479 memrange_sortmerge (&tracepoint_list);
1480 memrange_sortmerge (&stepping_list);
c906108c 1481
d183932d
MS
1482 *tdp_actions = stringify_collection_list (&tracepoint_list,
1483 tdp_buff);
1484 *stepping_actions = stringify_collection_list (&stepping_list,
1485 step_buff);
236f1d4d
SS
1486
1487 xfree (default_collect_line);
1488 xfree (default_collect_action);
c906108c
SS
1489}
1490
1491static void
fba45db2 1492add_aexpr (struct collection_list *collect, struct agent_expr *aexpr)
c906108c
SS
1493{
1494 if (collect->next_aexpr_elt >= collect->aexpr_listsize)
1495 {
1496 collect->aexpr_list =
1497 xrealloc (collect->aexpr_list,
c5aa993b 1498 2 * collect->aexpr_listsize * sizeof (struct agent_expr *));
c906108c
SS
1499 collect->aexpr_listsize *= 2;
1500 }
1501 collect->aexpr_list[collect->next_aexpr_elt] = aexpr;
1502 collect->next_aexpr_elt++;
1503}
1504
c906108c
SS
1505/* Set "transparent" memory ranges
1506
1507 Allow trace mechanism to treat text-like sections
1508 (and perhaps all read-only sections) transparently,
1509 i.e. don't reject memory requests from these address ranges
1510 just because they haven't been collected. */
1511
1512static void
1513remote_set_transparent_ranges (void)
1514{
c906108c
SS
1515 asection *s;
1516 bfd_size_type size;
1517 bfd_vma lma;
1518 int anysecs = 0;
1519
1520 if (!exec_bfd)
d183932d 1521 return; /* No information to give. */
c906108c
SS
1522
1523 strcpy (target_buf, "QTro");
1524 for (s = exec_bfd->sections; s; s = s->next)
1525 {
104c1213 1526 char tmp1[40], tmp2[40];
c906108c 1527
c5aa993b
JM
1528 if ((s->flags & SEC_LOAD) == 0 ||
1529 /* (s->flags & SEC_CODE) == 0 || */
c906108c
SS
1530 (s->flags & SEC_READONLY) == 0)
1531 continue;
1532
1533 anysecs = 1;
c5aa993b 1534 lma = s->lma;
2c500098 1535 size = bfd_get_section_size (s);
104c1213
JM
1536 sprintf_vma (tmp1, lma);
1537 sprintf_vma (tmp2, lma + size);
1538 sprintf (target_buf + strlen (target_buf),
1539 ":%s,%s", tmp1, tmp2);
c906108c
SS
1540 }
1541 if (anysecs)
1542 {
1543 putpkt (target_buf);
6d820c5c 1544 getpkt (&target_buf, &target_buf_size, 0);
c906108c
SS
1545 }
1546}
1547
1548/* tstart command:
c5aa993b 1549
c906108c
SS
1550 Tell target to clear any previous trace experiment.
1551 Walk the list of tracepoints, and send them (and their actions)
1552 to the target. If no errors,
1553 Tell target to start a new trace experiment. */
1554
1042e4c0
SS
1555void download_tracepoint (struct breakpoint *t);
1556
c906108c 1557static void
fba45db2 1558trace_start_command (char *args, int from_tty)
d183932d 1559{
f61e138d 1560 char buf[2048];
1042e4c0
SS
1561 VEC(breakpoint_p) *tp_vec = NULL;
1562 int ix;
1563 struct breakpoint *t;
f61e138d 1564 struct trace_state_variable *tsv;
c906108c 1565
d183932d 1566 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
c5aa993b 1567
c906108c
SS
1568 if (target_is_remote ())
1569 {
1570 putpkt ("QTinit");
6d820c5c 1571 remote_get_noisy_reply (&target_buf, &target_buf_size);
c906108c 1572 if (strcmp (target_buf, "OK"))
8a3fe4f8 1573 error (_("Target does not support this command."));
c906108c 1574
1042e4c0
SS
1575 tp_vec = all_tracepoints ();
1576 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1577 {
1578 download_tracepoint (t);
1579 }
1580 VEC_free (breakpoint_p, tp_vec);
c5aa993b 1581
f61e138d
SS
1582 /* Init any trace state variables that start with nonzero values. */
1583
1584 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
1585 {
1586 if (tsv->initial_value != 0)
1587 {
1588 sprintf (buf, "QTDV:%x:%s",
1589 tsv->number, phex ((ULONGEST) tsv->initial_value, 8));
1590 putpkt (buf);
1591 remote_get_noisy_reply (&target_buf, &target_buf_size);
1592 }
1593 }
1594
d183932d 1595 /* Tell target to treat text-like sections as transparent. */
c906108c 1596 remote_set_transparent_ranges ();
d183932d 1597 /* Now insert traps and begin collecting data. */
c906108c 1598 putpkt ("QTStart");
6d820c5c 1599 remote_get_noisy_reply (&target_buf, &target_buf_size);
c906108c 1600 if (strcmp (target_buf, "OK"))
8a3fe4f8 1601 error (_("Bogus reply from target: %s"), target_buf);
d183932d 1602 set_traceframe_num (-1); /* All old traceframes invalidated. */
c906108c 1603 set_tracepoint_num (-1);
fb14de7b 1604 set_traceframe_context (NULL);
c906108c 1605 trace_running_p = 1;
9a4105ab
AC
1606 if (deprecated_trace_start_stop_hook)
1607 deprecated_trace_start_stop_hook (1, from_tty);
c5aa993b 1608
c906108c
SS
1609 }
1610 else
8a3fe4f8 1611 error (_("Trace can only be run on remote targets."));
c906108c
SS
1612}
1613
1042e4c0
SS
1614/* Send the definition of a single tracepoint to the target. */
1615
1616void
1617download_tracepoint (struct breakpoint *t)
1618{
1619 char tmp[40];
1620 char buf[2048];
1621 char **tdp_actions;
1622 char **stepping_actions;
1623 int ndx;
1624 struct cleanup *old_chain = NULL;
782b2b07
SS
1625 struct agent_expr *aexpr;
1626 struct cleanup *aexpr_chain = NULL;
1042e4c0
SS
1627
1628 sprintf_vma (tmp, (t->loc ? t->loc->address : 0));
1629 sprintf (buf, "QTDP:%x:%s:%c:%lx:%x", t->number,
1630 tmp, /* address */
1631 (t->enable_state == bp_enabled ? 'E' : 'D'),
1632 t->step_count, t->pass_count);
782b2b07
SS
1633 /* If the tracepoint has a conditional, make it into an agent
1634 expression and append to the definition. */
1635 if (t->loc->cond)
1636 {
1637 /* Only test support at download time, we may not know target
1638 capabilities at definition time. */
1639 if (remote_supports_cond_tracepoints ())
1640 {
1641 aexpr = gen_eval_for_expr (t->loc->address, t->loc->cond);
1642 aexpr_chain = make_cleanup_free_agent_expr (aexpr);
1643 sprintf (buf + strlen (buf), ":X%x,", aexpr->len);
1644 mem2hex (aexpr->buf, buf + strlen (buf), aexpr->len);
1645 do_cleanups (aexpr_chain);
1646 }
1647 else
1648 warning (_("Target does not support conditional tracepoints, ignoring tp %d cond"), t->number);
1649 }
1042e4c0 1650
236f1d4d 1651 if (t->actions || *default_collect)
1042e4c0
SS
1652 strcat (buf, "-");
1653 putpkt (buf);
1654 remote_get_noisy_reply (&target_buf, &target_buf_size);
1655 if (strcmp (target_buf, "OK"))
1656 error (_("Target does not support tracepoints."));
1657
236f1d4d 1658 if (!t->actions && !*default_collect)
1042e4c0
SS
1659 return;
1660
1661 encode_actions (t, &tdp_actions, &stepping_actions);
1662 old_chain = make_cleanup (free_actions_list_cleanup_wrapper,
1663 tdp_actions);
1664 (void) make_cleanup (free_actions_list_cleanup_wrapper, stepping_actions);
1665
1666 /* do_single_steps (t); */
1667 if (tdp_actions)
1668 {
1669 for (ndx = 0; tdp_actions[ndx]; ndx++)
1670 {
1671 QUIT; /* allow user to bail out with ^C */
1672 sprintf (buf, "QTDP:-%x:%s:%s%c",
1673 t->number, tmp, /* address */
1674 tdp_actions[ndx],
1675 ((tdp_actions[ndx + 1] || stepping_actions)
1676 ? '-' : 0));
1677 putpkt (buf);
1678 remote_get_noisy_reply (&target_buf,
1679 &target_buf_size);
1680 if (strcmp (target_buf, "OK"))
1681 error (_("Error on target while setting tracepoints."));
1682 }
1683 }
1684 if (stepping_actions)
1685 {
1686 for (ndx = 0; stepping_actions[ndx]; ndx++)
1687 {
1688 QUIT; /* allow user to bail out with ^C */
1689 sprintf (buf, "QTDP:-%x:%s:%s%s%s",
1690 t->number, tmp, /* address */
1691 ((ndx == 0) ? "S" : ""),
1692 stepping_actions[ndx],
1693 (stepping_actions[ndx + 1] ? "-" : ""));
1694 putpkt (buf);
1695 remote_get_noisy_reply (&target_buf,
1696 &target_buf_size);
1697 if (strcmp (target_buf, "OK"))
1698 error (_("Error on target while setting tracepoints."));
1699 }
1700 }
1701 do_cleanups (old_chain);
1702}
1703
c906108c
SS
1704/* tstop command */
1705static void
fba45db2 1706trace_stop_command (char *args, int from_tty)
d183932d 1707{
c906108c
SS
1708 if (target_is_remote ())
1709 {
1710 putpkt ("QTStop");
6d820c5c 1711 remote_get_noisy_reply (&target_buf, &target_buf_size);
c906108c 1712 if (strcmp (target_buf, "OK"))
8a3fe4f8 1713 error (_("Bogus reply from target: %s"), target_buf);
c906108c 1714 trace_running_p = 0;
9a4105ab
AC
1715 if (deprecated_trace_start_stop_hook)
1716 deprecated_trace_start_stop_hook (0, from_tty);
c906108c
SS
1717 }
1718 else
8a3fe4f8 1719 error (_("Trace can only be run on remote targets."));
c906108c
SS
1720}
1721
1722unsigned long trace_running_p;
1723
1724/* tstatus command */
1725static void
fba45db2 1726trace_status_command (char *args, int from_tty)
d183932d 1727{
c906108c
SS
1728 if (target_is_remote ())
1729 {
1730 putpkt ("qTStatus");
6d820c5c 1731 remote_get_noisy_reply (&target_buf, &target_buf_size);
c906108c
SS
1732
1733 if (target_buf[0] != 'T' ||
1734 (target_buf[1] != '0' && target_buf[1] != '1'))
8a3fe4f8 1735 error (_("Bogus reply from target: %s"), target_buf);
c906108c
SS
1736
1737 /* exported for use by the GUI */
1738 trace_running_p = (target_buf[1] == '1');
d3513012
SS
1739
1740 if (trace_running_p)
1741 printf_filtered (_("Trace is running on the target.\n"));
1742 else
1743 printf_filtered (_("Trace is not running on the target.\n"));
1744
1745 if (traceframe_number >= 0)
1746 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
1747 traceframe_number, tracepoint_number);
1748 else
1749 printf_filtered (_("Not looking at any trace frame.\n"));
1750
c906108c
SS
1751 }
1752 else
8a3fe4f8 1753 error (_("Trace can only be run on remote targets."));
c906108c
SS
1754}
1755
d183932d 1756/* Worker function for the various flavors of the tfind command. */
c906108c 1757static void
6d820c5c
DJ
1758finish_tfind_command (char **msg,
1759 long *sizeof_msg,
c2d11a7d 1760 int from_tty)
c906108c
SS
1761{
1762 int target_frameno = -1, target_tracept = -1;
fb14de7b 1763 struct frame_id old_frame_id;
c906108c
SS
1764 char *reply;
1765
fb14de7b 1766 old_frame_id = get_frame_id (get_current_frame ());
c906108c 1767
6d820c5c 1768 putpkt (*msg);
c2d11a7d 1769 reply = remote_get_noisy_reply (msg, sizeof_msg);
c906108c
SS
1770
1771 while (reply && *reply)
c5aa993b
JM
1772 switch (*reply)
1773 {
1774 case 'F':
1775 if ((target_frameno = (int) strtol (++reply, &reply, 16)) == -1)
1776 {
1777 /* A request for a non-existant trace frame has failed.
1778 Our response will be different, depending on FROM_TTY:
1779
1780 If FROM_TTY is true, meaning that this command was
1781 typed interactively by the user, then give an error
1782 and DO NOT change the state of traceframe_number etc.
1783
1784 However if FROM_TTY is false, meaning that we're either
1785 in a script, a loop, or a user-defined command, then
1786 DON'T give an error, but DO change the state of
1787 traceframe_number etc. to invalid.
1788
1789 The rationalle is that if you typed the command, you
1790 might just have committed a typo or something, and you'd
1791 like to NOT lose your current debugging state. However
1792 if you're in a user-defined command or especially in a
1793 loop, then you need a way to detect that the command
1794 failed WITHOUT aborting. This allows you to write
1795 scripts that search thru the trace buffer until the end,
1796 and then continue on to do something else. */
1797
1798 if (from_tty)
8a3fe4f8 1799 error (_("Target failed to find requested trace frame."));
c5aa993b
JM
1800 else
1801 {
1802 if (info_verbose)
1803 printf_filtered ("End of trace buffer.\n");
d183932d
MS
1804 /* The following will not recurse, since it's
1805 special-cased. */
c5aa993b 1806 trace_find_command ("-1", from_tty);
d183932d
MS
1807 reply = NULL; /* Break out of loop
1808 (avoid recursive nonsense). */
c5aa993b
JM
1809 }
1810 }
1811 break;
1812 case 'T':
1813 if ((target_tracept = (int) strtol (++reply, &reply, 16)) == -1)
8a3fe4f8 1814 error (_("Target failed to find requested trace frame."));
c5aa993b
JM
1815 break;
1816 case 'O': /* "OK"? */
1817 if (reply[1] == 'K' && reply[2] == '\0')
1818 reply += 2;
1819 else
8a3fe4f8 1820 error (_("Bogus reply from target: %s"), reply);
c5aa993b
JM
1821 break;
1822 default:
8a3fe4f8 1823 error (_("Bogus reply from target: %s"), reply);
c5aa993b 1824 }
c906108c 1825
35f196d9 1826 reinit_frame_cache ();
c906108c 1827 registers_changed ();
c906108c
SS
1828 set_traceframe_num (target_frameno);
1829 set_tracepoint_num (target_tracept);
1830 if (target_frameno == -1)
fb14de7b 1831 set_traceframe_context (NULL);
c906108c 1832 else
fb14de7b 1833 set_traceframe_context (get_current_frame ());
c906108c
SS
1834
1835 if (from_tty)
1836 {
0faf0076 1837 enum print_what print_what;
c906108c
SS
1838
1839 /* NOTE: in immitation of the step command, try to determine
d183932d
MS
1840 whether we have made a transition from one function to
1841 another. If so, we'll print the "stack frame" (ie. the new
1842 function and it's arguments) -- otherwise we'll just show the
fb14de7b
UW
1843 new source line. */
1844
1845 if (frame_id_eq (old_frame_id,
1846 get_frame_id (get_current_frame ())))
0faf0076 1847 print_what = SRC_LINE;
c906108c 1848 else
0faf0076 1849 print_what = SRC_AND_LOC;
c906108c 1850
b04f3ab4 1851 print_stack_frame (get_selected_frame (NULL), 1, print_what);
c906108c
SS
1852 do_displays ();
1853 }
1854}
1855
1856/* trace_find_command takes a trace frame number n,
1857 sends "QTFrame:<n>" to the target,
1858 and accepts a reply that may contain several optional pieces
1859 of information: a frame number, a tracepoint number, and an
1860 indication of whether this is a trap frame or a stepping frame.
1861
1862 The minimal response is just "OK" (which indicates that the
1863 target does not give us a frame number or a tracepoint number).
1864 Instead of that, the target may send us a string containing
1865 any combination of:
c5aa993b
JM
1866 F<hexnum> (gives the selected frame number)
1867 T<hexnum> (gives the selected tracepoint number)
1868 */
c906108c
SS
1869
1870/* tfind command */
1871static void
fba45db2 1872trace_find_command (char *args, int from_tty)
d183932d 1873{ /* this should only be called with a numeric argument */
c906108c 1874 int frameno = -1;
c906108c
SS
1875
1876 if (target_is_remote ())
1877 {
383e5f85
SS
1878 if (trace_running_p)
1879 error ("May not look at trace frames while trace is running.");
1880
9a4105ab
AC
1881 if (deprecated_trace_find_hook)
1882 deprecated_trace_find_hook (args, from_tty);
c5aa993b 1883
c906108c 1884 if (args == 0 || *args == 0)
d183932d 1885 { /* TFIND with no args means find NEXT trace frame. */
c906108c
SS
1886 if (traceframe_number == -1)
1887 frameno = 0; /* "next" is first one */
1888 else
1889 frameno = traceframe_number + 1;
1890 }
1891 else if (0 == strcmp (args, "-"))
1892 {
1893 if (traceframe_number == -1)
8a3fe4f8 1894 error (_("not debugging trace buffer"));
c906108c 1895 else if (from_tty && traceframe_number == 0)
8a3fe4f8 1896 error (_("already at start of trace buffer"));
c906108c
SS
1897
1898 frameno = traceframe_number - 1;
1899 }
1900 else
bb518678 1901 frameno = parse_and_eval_long (args);
c906108c
SS
1902
1903 if (frameno < -1)
8a3fe4f8 1904 error (_("invalid input (%d is less than zero)"), frameno);
c906108c
SS
1905
1906 sprintf (target_buf, "QTFrame:%x", frameno);
6d820c5c 1907 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
1908 }
1909 else
8a3fe4f8 1910 error (_("Trace can only be run on remote targets."));
c906108c
SS
1911}
1912
1913/* tfind end */
1914static void
fba45db2 1915trace_find_end_command (char *args, int from_tty)
c906108c
SS
1916{
1917 trace_find_command ("-1", from_tty);
1918}
1919
1920/* tfind none */
1921static void
fba45db2 1922trace_find_none_command (char *args, int from_tty)
c906108c
SS
1923{
1924 trace_find_command ("-1", from_tty);
1925}
1926
1927/* tfind start */
1928static void
fba45db2 1929trace_find_start_command (char *args, int from_tty)
c906108c
SS
1930{
1931 trace_find_command ("0", from_tty);
1932}
1933
1934/* tfind pc command */
1935static void
fba45db2 1936trace_find_pc_command (char *args, int from_tty)
d183932d 1937{
c906108c 1938 CORE_ADDR pc;
104c1213 1939 char tmp[40];
c906108c
SS
1940
1941 if (target_is_remote ())
1942 {
383e5f85
SS
1943 if (trace_running_p)
1944 error ("May not look at trace frames while trace is running.");
1945
c906108c 1946 if (args == 0 || *args == 0)
fb14de7b 1947 pc = regcache_read_pc (get_current_regcache ());
c906108c
SS
1948 else
1949 pc = parse_and_eval_address (args);
1950
104c1213
JM
1951 sprintf_vma (tmp, pc);
1952 sprintf (target_buf, "QTFrame:pc:%s", tmp);
6d820c5c 1953 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
1954 }
1955 else
8a3fe4f8 1956 error (_("Trace can only be run on remote targets."));
c906108c
SS
1957}
1958
1959/* tfind tracepoint command */
1960static void
fba45db2 1961trace_find_tracepoint_command (char *args, int from_tty)
d183932d 1962{
c906108c 1963 int tdp;
c906108c
SS
1964
1965 if (target_is_remote ())
1966 {
383e5f85
SS
1967 if (trace_running_p)
1968 error ("May not look at trace frames while trace is running.");
1969
c906108c 1970 if (args == 0 || *args == 0)
3db26b01
JB
1971 {
1972 if (tracepoint_number == -1)
8a3fe4f8 1973 error (_("No current tracepoint -- please supply an argument."));
3db26b01
JB
1974 else
1975 tdp = tracepoint_number; /* default is current TDP */
1976 }
c906108c 1977 else
0e828ed1 1978 tdp = parse_and_eval_long (args);
c906108c
SS
1979
1980 sprintf (target_buf, "QTFrame:tdp:%x", tdp);
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 LINE command:
c5aa993b 1988
c906108c 1989 This command will take a sourceline for argument, just like BREAK
d183932d 1990 or TRACE (ie. anything that "decode_line_1" can handle).
c5aa993b 1991
c906108c
SS
1992 With no argument, this command will find the next trace frame
1993 corresponding to a source line OTHER THAN THE CURRENT ONE. */
1994
1995static void
fba45db2 1996trace_find_line_command (char *args, int from_tty)
d183932d 1997{
c906108c
SS
1998 static CORE_ADDR start_pc, end_pc;
1999 struct symtabs_and_lines sals;
2000 struct symtab_and_line sal;
c906108c 2001 struct cleanup *old_chain;
104c1213 2002 char startpc_str[40], endpc_str[40];
c906108c
SS
2003
2004 if (target_is_remote ())
2005 {
383e5f85
SS
2006 if (trace_running_p)
2007 error ("May not look at trace frames while trace is running.");
2008
c906108c
SS
2009 if (args == 0 || *args == 0)
2010 {
bdd78e62 2011 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
c906108c
SS
2012 sals.nelts = 1;
2013 sals.sals = (struct symtab_and_line *)
2014 xmalloc (sizeof (struct symtab_and_line));
2015 sals.sals[0] = sal;
2016 }
2017 else
2018 {
2019 sals = decode_line_spec (args, 1);
c5aa993b 2020 sal = sals.sals[0];
c906108c
SS
2021 }
2022
b8c9b27d 2023 old_chain = make_cleanup (xfree, sals.sals);
c906108c
SS
2024 if (sal.symtab == 0)
2025 {
5af949e3
UW
2026 struct gdbarch *gdbarch = get_current_arch ();
2027
c906108c
SS
2028 printf_filtered ("TFIND: No line number information available");
2029 if (sal.pc != 0)
2030 {
d183932d
MS
2031 /* This is useful for "info line *0x7f34". If we can't
2032 tell the user about a source line, at least let them
2033 have the symbolic address. */
c906108c
SS
2034 printf_filtered (" for address ");
2035 wrap_here (" ");
5af949e3 2036 print_address (gdbarch, sal.pc, gdb_stdout);
c906108c
SS
2037 printf_filtered (";\n -- will attempt to find by PC. \n");
2038 }
2039 else
2040 {
2041 printf_filtered (".\n");
d183932d 2042 return; /* No line, no PC; what can we do? */
c906108c
SS
2043 }
2044 }
2045 else if (sal.line > 0
2046 && find_line_pc_range (sal, &start_pc, &end_pc))
2047 {
5af949e3
UW
2048 struct gdbarch *gdbarch = get_objfile_arch (sal.symtab->objfile);
2049
c906108c
SS
2050 if (start_pc == end_pc)
2051 {
2052 printf_filtered ("Line %d of \"%s\"",
2053 sal.line, sal.symtab->filename);
2054 wrap_here (" ");
2055 printf_filtered (" is at address ");
5af949e3 2056 print_address (gdbarch, start_pc, gdb_stdout);
c906108c
SS
2057 wrap_here (" ");
2058 printf_filtered (" but contains no code.\n");
2059 sal = find_pc_line (start_pc, 0);
2060 if (sal.line > 0 &&
2061 find_line_pc_range (sal, &start_pc, &end_pc) &&
2062 start_pc != end_pc)
2063 printf_filtered ("Attempting to find line %d instead.\n",
2064 sal.line);
2065 else
8a3fe4f8 2066 error (_("Cannot find a good line."));
c906108c
SS
2067 }
2068 }
2069 else
2070 /* Is there any case in which we get here, and have an address
d183932d
MS
2071 which the user would want to see? If we have debugging
2072 symbols and no line numbers? */
8a3fe4f8 2073 error (_("Line number %d is out of range for \"%s\"."),
c906108c
SS
2074 sal.line, sal.symtab->filename);
2075
104c1213
JM
2076 sprintf_vma (startpc_str, start_pc);
2077 sprintf_vma (endpc_str, end_pc - 1);
d183932d
MS
2078 /* Find within range of stated line. */
2079 if (args && *args)
2080 sprintf (target_buf, "QTFrame:range:%s:%s",
2081 startpc_str, endpc_str);
2082 /* Find OUTSIDE OF range of CURRENT line. */
2083 else
2084 sprintf (target_buf, "QTFrame:outside:%s:%s",
2085 startpc_str, endpc_str);
6d820c5c 2086 finish_tfind_command (&target_buf, &target_buf_size,
d183932d 2087 from_tty);
c906108c
SS
2088 do_cleanups (old_chain);
2089 }
2090 else
8a3fe4f8 2091 error (_("Trace can only be run on remote targets."));
c906108c
SS
2092}
2093
2094/* tfind range command */
2095static void
fba45db2 2096trace_find_range_command (char *args, int from_tty)
104c1213 2097{
c906108c 2098 static CORE_ADDR start, stop;
104c1213 2099 char start_str[40], stop_str[40];
c906108c
SS
2100 char *tmp;
2101
2102 if (target_is_remote ())
2103 {
383e5f85
SS
2104 if (trace_running_p)
2105 error ("May not look at trace frames while trace is running.");
2106
c906108c 2107 if (args == 0 || *args == 0)
d183932d 2108 { /* XXX FIXME: what should default behavior be? */
c906108c
SS
2109 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2110 return;
2111 }
2112
c5aa993b 2113 if (0 != (tmp = strchr (args, ',')))
c906108c
SS
2114 {
2115 *tmp++ = '\0'; /* terminate start address */
104c1213 2116 while (isspace ((int) *tmp))
c906108c
SS
2117 tmp++;
2118 start = parse_and_eval_address (args);
c5aa993b 2119 stop = parse_and_eval_address (tmp);
c906108c
SS
2120 }
2121 else
c5aa993b 2122 { /* no explicit end address? */
c906108c 2123 start = parse_and_eval_address (args);
c5aa993b 2124 stop = start + 1; /* ??? */
c906108c
SS
2125 }
2126
104c1213
JM
2127 sprintf_vma (start_str, start);
2128 sprintf_vma (stop_str, stop);
2129 sprintf (target_buf, "QTFrame:range:%s:%s", start_str, stop_str);
6d820c5c 2130 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
2131 }
2132 else
8a3fe4f8 2133 error (_("Trace can only be run on remote targets."));
c906108c
SS
2134}
2135
2136/* tfind outside command */
2137static void
fba45db2 2138trace_find_outside_command (char *args, int from_tty)
104c1213 2139{
c906108c 2140 CORE_ADDR start, stop;
104c1213 2141 char start_str[40], stop_str[40];
c906108c
SS
2142 char *tmp;
2143
2144 if (target_is_remote ())
2145 {
383e5f85
SS
2146 if (trace_running_p)
2147 error ("May not look at trace frames while trace is running.");
2148
c906108c 2149 if (args == 0 || *args == 0)
d183932d 2150 { /* XXX FIXME: what should default behavior be? */
c906108c
SS
2151 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2152 return;
2153 }
2154
c5aa993b 2155 if (0 != (tmp = strchr (args, ',')))
c906108c
SS
2156 {
2157 *tmp++ = '\0'; /* terminate start address */
104c1213 2158 while (isspace ((int) *tmp))
c906108c
SS
2159 tmp++;
2160 start = parse_and_eval_address (args);
c5aa993b 2161 stop = parse_and_eval_address (tmp);
c906108c
SS
2162 }
2163 else
c5aa993b 2164 { /* no explicit end address? */
c906108c 2165 start = parse_and_eval_address (args);
c5aa993b 2166 stop = start + 1; /* ??? */
c906108c
SS
2167 }
2168
104c1213
JM
2169 sprintf_vma (start_str, start);
2170 sprintf_vma (stop_str, stop);
2171 sprintf (target_buf, "QTFrame:outside:%s:%s", start_str, stop_str);
6d820c5c 2172 finish_tfind_command (&target_buf, &target_buf_size, from_tty);
c906108c
SS
2173 }
2174 else
8a3fe4f8 2175 error (_("Trace can only be run on remote targets."));
c906108c
SS
2176}
2177
c906108c
SS
2178/* info scope command: list the locals for a scope. */
2179static void
fba45db2 2180scope_info (char *args, int from_tty)
c906108c 2181{
c906108c
SS
2182 struct symtabs_and_lines sals;
2183 struct symbol *sym;
2184 struct minimal_symbol *msym;
2185 struct block *block;
2186 char **canonical, *symname, *save_args = args;
de4f826b
DC
2187 struct dict_iterator iter;
2188 int j, count = 0;
768a979c
UW
2189 struct gdbarch *gdbarch;
2190 int regno;
c906108c
SS
2191
2192 if (args == 0 || *args == 0)
8a3fe4f8 2193 error (_("requires an argument (function, line or *addr) to define a scope"));
c906108c 2194
68219205 2195 sals = decode_line_1 (&args, 1, NULL, 0, &canonical, NULL);
c906108c 2196 if (sals.nelts == 0)
450bd37b 2197 return; /* presumably decode_line_1 has already warned */
c906108c
SS
2198
2199 /* Resolve line numbers to PC */
2200 resolve_sal_pc (&sals.sals[0]);
2201 block = block_for_pc (sals.sals[0].pc);
2202
2203 while (block != 0)
2204 {
c5aa993b 2205 QUIT; /* allow user to bail out with ^C */
de4f826b 2206 ALL_BLOCK_SYMBOLS (block, iter, sym)
c906108c 2207 {
c5aa993b 2208 QUIT; /* allow user to bail out with ^C */
c906108c
SS
2209 if (count == 0)
2210 printf_filtered ("Scope for %s:\n", save_args);
2211 count++;
e88c90f2 2212
3567439c 2213 symname = SYMBOL_PRINT_NAME (sym);
c906108c 2214 if (symname == NULL || *symname == '\0')
c5aa993b 2215 continue; /* probably botched, certainly useless */
c906108c 2216
768a979c
UW
2217 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
2218
c906108c 2219 printf_filtered ("Symbol %s is ", symname);
c5aa993b
JM
2220 switch (SYMBOL_CLASS (sym))
2221 {
2222 default:
2223 case LOC_UNDEF: /* messed up symbol? */
2224 printf_filtered ("a bogus symbol, class %d.\n",
2225 SYMBOL_CLASS (sym));
2226 count--; /* don't count this one */
2227 continue;
2228 case LOC_CONST:
104c1213 2229 printf_filtered ("a constant with value %ld (0x%lx)",
c5aa993b
JM
2230 SYMBOL_VALUE (sym), SYMBOL_VALUE (sym));
2231 break;
2232 case LOC_CONST_BYTES:
2233 printf_filtered ("constant bytes: ");
2234 if (SYMBOL_TYPE (sym))
2235 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2236 fprintf_filtered (gdb_stdout, " %02x",
2237 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2238 break;
2239 case LOC_STATIC:
2240 printf_filtered ("in static storage at address ");
5af949e3
UW
2241 printf_filtered ("%s", paddress (gdbarch,
2242 SYMBOL_VALUE_ADDRESS (sym)));
c5aa993b
JM
2243 break;
2244 case LOC_REGISTER:
768a979c
UW
2245 /* GDBARCH is the architecture associated with the objfile
2246 the symbol is defined in; the target architecture may be
2247 different, and may provide additional registers. However,
2248 we do not know the target architecture at this point.
2249 We assume the objfile architecture will contain all the
2250 standard registers that occur in debug info in that
2251 objfile. */
2252 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
2253
2a2d4dc3
AS
2254 if (SYMBOL_IS_ARGUMENT (sym))
2255 printf_filtered ("an argument in register $%s",
768a979c 2256 gdbarch_register_name (gdbarch, regno));
2a2d4dc3
AS
2257 else
2258 printf_filtered ("a local variable in register $%s",
768a979c 2259 gdbarch_register_name (gdbarch, regno));
c5aa993b
JM
2260 break;
2261 case LOC_ARG:
c5aa993b
JM
2262 printf_filtered ("an argument at stack/frame offset %ld",
2263 SYMBOL_VALUE (sym));
2264 break;
2265 case LOC_LOCAL:
2266 printf_filtered ("a local variable at frame offset %ld",
2267 SYMBOL_VALUE (sym));
2268 break;
2269 case LOC_REF_ARG:
2270 printf_filtered ("a reference argument at offset %ld",
2271 SYMBOL_VALUE (sym));
2272 break;
c5aa993b 2273 case LOC_REGPARM_ADDR:
768a979c
UW
2274 /* Note comment at LOC_REGISTER. */
2275 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
c5aa993b 2276 printf_filtered ("the address of an argument, in register $%s",
768a979c 2277 gdbarch_register_name (gdbarch, regno));
c5aa993b
JM
2278 break;
2279 case LOC_TYPEDEF:
2280 printf_filtered ("a typedef.\n");
2281 continue;
2282 case LOC_LABEL:
2283 printf_filtered ("a label at address ");
5af949e3
UW
2284 printf_filtered ("%s", paddress (gdbarch,
2285 SYMBOL_VALUE_ADDRESS (sym)));
c5aa993b
JM
2286 break;
2287 case LOC_BLOCK:
2288 printf_filtered ("a function at address ");
5af949e3
UW
2289 printf_filtered ("%s",
2290 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
c5aa993b 2291 break;
c5aa993b 2292 case LOC_UNRESOLVED:
3567439c 2293 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
450bd37b 2294 NULL, NULL);
c5aa993b
JM
2295 if (msym == NULL)
2296 printf_filtered ("Unresolved Static");
2297 else
2298 {
2299 printf_filtered ("static storage at address ");
5af949e3
UW
2300 printf_filtered ("%s",
2301 paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msym)));
c5aa993b
JM
2302 }
2303 break;
2304 case LOC_OPTIMIZED_OUT:
2305 printf_filtered ("optimized out.\n");
2306 continue;
450bd37b 2307 case LOC_COMPUTED:
768a979c 2308 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, gdb_stdout);
450bd37b 2309 break;
c5aa993b 2310 }
c906108c 2311 if (SYMBOL_TYPE (sym))
c5aa993b 2312 printf_filtered (", length %d.\n",
450bd37b 2313 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
c906108c
SS
2314 }
2315 if (BLOCK_FUNCTION (block))
2316 break;
2317 else
2318 block = BLOCK_SUPERBLOCK (block);
2319 }
2320 if (count <= 0)
2321 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2322 save_args);
2323}
2324
2325/* worker function (cleanup) */
2326static void
710b33bd 2327replace_comma (void *data)
c906108c 2328{
710b33bd 2329 char *comma = data;
c906108c
SS
2330 *comma = ',';
2331}
2332
2333/* tdump command */
2334static void
fba45db2 2335trace_dump_command (char *args, int from_tty)
c906108c 2336{
515630c5
UW
2337 struct regcache *regcache;
2338 struct gdbarch *gdbarch;
1042e4c0 2339 struct breakpoint *t;
c906108c 2340 struct action_line *action;
c5aa993b
JM
2341 char *action_exp, *next_comma;
2342 struct cleanup *old_cleanups;
2343 int stepping_actions = 0;
2344 int stepping_frame = 0;
c906108c
SS
2345
2346 if (!target_is_remote ())
2347 {
8a3fe4f8 2348 error (_("Trace can only be run on remote targets."));
c906108c
SS
2349 return;
2350 }
2351
2352 if (tracepoint_number == -1)
2353 {
8a3fe4f8 2354 warning (_("No current trace frame."));
c906108c
SS
2355 return;
2356 }
2357
1042e4c0 2358 t = get_tracepoint (tracepoint_number);
c906108c
SS
2359
2360 if (t == NULL)
8a3fe4f8 2361 error (_("No known tracepoint matches 'current' tracepoint #%d."),
c906108c
SS
2362 tracepoint_number);
2363
2364 old_cleanups = make_cleanup (null_cleanup, NULL);
2365
c5aa993b 2366 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
c906108c
SS
2367 tracepoint_number, traceframe_number);
2368
2369 /* The current frame is a trap frame if the frame PC is equal
2370 to the tracepoint PC. If not, then the current frame was
2371 collected during single-stepping. */
2372
515630c5
UW
2373 regcache = get_current_regcache ();
2374 gdbarch = get_regcache_arch (regcache);
2375
1042e4c0 2376 stepping_frame = (t->loc->address != (regcache_read_pc (regcache)
515630c5 2377 - gdbarch_decr_pc_after_break (gdbarch)));
c906108c
SS
2378
2379 for (action = t->actions; action; action = action->next)
2380 {
2381 struct cmd_list_element *cmd;
2382
c5aa993b 2383 QUIT; /* allow user to bail out with ^C */
c906108c 2384 action_exp = action->action;
104c1213 2385 while (isspace ((int) *action_exp))
c906108c
SS
2386 action_exp++;
2387
2388 /* The collection actions to be done while stepping are
c5aa993b 2389 bracketed by the commands "while-stepping" and "end". */
c906108c
SS
2390
2391 if (*action_exp == '#') /* comment line */
2392 continue;
2393
2394 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2395 if (cmd == 0)
8a3fe4f8 2396 error (_("Bad action list item: %s"), action_exp);
c906108c 2397
bbaca940 2398 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
c906108c 2399 stepping_actions = 1;
bbaca940 2400 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
c906108c 2401 stepping_actions = 0;
bbaca940 2402 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
c906108c
SS
2403 {
2404 /* Display the collected data.
d183932d
MS
2405 For the trap frame, display only what was collected at
2406 the trap. Likewise for stepping frames, display only
2407 what was collected while stepping. This means that the
2408 two boolean variables, STEPPING_FRAME and
2409 STEPPING_ACTIONS should be equal. */
c906108c
SS
2410 if (stepping_frame == stepping_actions)
2411 {
c5aa993b
JM
2412 do
2413 { /* repeat over a comma-separated list */
2414 QUIT; /* allow user to bail out with ^C */
2415 if (*action_exp == ',')
2416 action_exp++;
104c1213 2417 while (isspace ((int) *action_exp))
c5aa993b
JM
2418 action_exp++;
2419
2420 next_comma = strchr (action_exp, ',');
2421
2422 if (0 == strncasecmp (action_exp, "$reg", 4))
2423 registers_info (NULL, from_tty);
2424 else if (0 == strncasecmp (action_exp, "$loc", 4))
2425 locals_info (NULL, from_tty);
2426 else if (0 == strncasecmp (action_exp, "$arg", 4))
2427 args_info (NULL, from_tty);
2428 else
2429 { /* variable */
2430 if (next_comma)
2431 {
2432 make_cleanup (replace_comma, next_comma);
2433 *next_comma = '\0';
2434 }
2435 printf_filtered ("%s = ", action_exp);
2436 output_command (action_exp, from_tty);
2437 printf_filtered ("\n");
2438 }
2439 if (next_comma)
2440 *next_comma = ',';
2441 action_exp = next_comma;
2442 }
2443 while (action_exp && *action_exp == ',');
c906108c
SS
2444 }
2445 }
2446 }
2447 discard_cleanups (old_cleanups);
2448}
2449
2450/* Convert the memory pointed to by mem into hex, placing result in buf.
2451 * Return a pointer to the last char put in buf (null)
2452 * "stolen" from sparc-stub.c
2453 */
2454
c5aa993b 2455static const char hexchars[] = "0123456789abcdef";
c906108c 2456
47b667de
AC
2457static char *
2458mem2hex (gdb_byte *mem, char *buf, int count)
c906108c 2459{
47b667de 2460 gdb_byte ch;
c906108c
SS
2461
2462 while (count-- > 0)
2463 {
2464 ch = *mem++;
2465
2466 *buf++ = hexchars[ch >> 4];
2467 *buf++ = hexchars[ch & 0xf];
2468 }
2469
2470 *buf = 0;
2471
2472 return buf;
2473}
2474
c5aa993b 2475int
fba45db2 2476get_traceframe_number (void)
c906108c 2477{
c5aa993b 2478 return traceframe_number;
c906108c
SS
2479}
2480
2481
2482/* module initialization */
2483void
fba45db2 2484_initialize_tracepoint (void)
c906108c 2485{
fa58ee11
EZ
2486 struct cmd_list_element *c;
2487
c906108c
SS
2488 traceframe_number = -1;
2489 tracepoint_number = -1;
2490
c906108c
SS
2491 if (tracepoint_list.list == NULL)
2492 {
2493 tracepoint_list.listsize = 128;
c5aa993b 2494 tracepoint_list.list = xmalloc
c906108c
SS
2495 (tracepoint_list.listsize * sizeof (struct memrange));
2496 }
2497 if (tracepoint_list.aexpr_list == NULL)
2498 {
2499 tracepoint_list.aexpr_listsize = 128;
2500 tracepoint_list.aexpr_list = xmalloc
2501 (tracepoint_list.aexpr_listsize * sizeof (struct agent_expr *));
2502 }
2503
2504 if (stepping_list.list == NULL)
2505 {
2506 stepping_list.listsize = 128;
c5aa993b 2507 stepping_list.list = xmalloc
c906108c
SS
2508 (stepping_list.listsize * sizeof (struct memrange));
2509 }
2510
2511 if (stepping_list.aexpr_list == NULL)
2512 {
2513 stepping_list.aexpr_listsize = 128;
2514 stepping_list.aexpr_list = xmalloc
2515 (stepping_list.aexpr_listsize * sizeof (struct agent_expr *));
2516 }
2517
c5aa993b 2518 add_info ("scope", scope_info,
1bedd215 2519 _("List the variables local to a scope"));
c906108c 2520
e00d1dc8 2521 add_cmd ("tracepoints", class_trace, NULL,
1a966eab 2522 _("Tracing of program execution without stopping the program."),
c906108c
SS
2523 &cmdlist);
2524
c5aa993b 2525 add_com ("tdump", class_trace, trace_dump_command,
1bedd215 2526 _("Print everything collected at the current tracepoint."));
c906108c 2527
f61e138d
SS
2528 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
2529Define a trace state variable.\n\
2530Argument is a $-prefixed name, optionally followed\n\
2531by '=' and an expression that sets the initial value\n\
2532at the start of tracing."));
2533 set_cmd_completer (c, expression_completer);
2534
2535 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
2536Delete one or more trace state variables.\n\
2537Arguments are the names of the variables to delete.\n\
2538If no arguments are supplied, delete all variables."), &deletelist);
2539 /* FIXME add a trace variable completer */
2540
2541 add_info ("tvariables", tvariables_info, _("\
2542Status of trace state variables and their values.\n\
2543"));
2544
1bedd215
AC
2545 add_prefix_cmd ("tfind", class_trace, trace_find_command, _("\
2546Select a trace frame;\n\
2547No argument means forward by one frame; '-' means backward by one frame."),
c906108c
SS
2548 &tfindlist, "tfind ", 1, &cmdlist);
2549
1a966eab
AC
2550 add_cmd ("outside", class_trace, trace_find_outside_command, _("\
2551Select a trace frame whose PC is outside the given range.\n\
2552Usage: tfind outside addr1, addr2"),
c906108c
SS
2553 &tfindlist);
2554
1a966eab
AC
2555 add_cmd ("range", class_trace, trace_find_range_command, _("\
2556Select a trace frame whose PC is in the given range.\n\
2557Usage: tfind range addr1,addr2"),
c906108c
SS
2558 &tfindlist);
2559
1a966eab
AC
2560 add_cmd ("line", class_trace, trace_find_line_command, _("\
2561Select a trace frame by source line.\n\
c906108c
SS
2562Argument can be a line number (with optional source file), \n\
2563a function name, or '*' followed by an address.\n\
1a966eab 2564Default argument is 'the next source line that was traced'."),
c906108c
SS
2565 &tfindlist);
2566
1a966eab
AC
2567 add_cmd ("tracepoint", class_trace, trace_find_tracepoint_command, _("\
2568Select a trace frame by tracepoint number.\n\
2569Default is the tracepoint for the current trace frame."),
c906108c
SS
2570 &tfindlist);
2571
1a966eab
AC
2572 add_cmd ("pc", class_trace, trace_find_pc_command, _("\
2573Select a trace frame by PC.\n\
2574Default is the current PC, or the PC of the current trace frame."),
c906108c
SS
2575 &tfindlist);
2576
1a966eab
AC
2577 add_cmd ("end", class_trace, trace_find_end_command, _("\
2578Synonym for 'none'.\n\
2579De-select any trace frame and resume 'live' debugging."),
c906108c
SS
2580 &tfindlist);
2581
2582 add_cmd ("none", class_trace, trace_find_none_command,
1a966eab 2583 _("De-select any trace frame and resume 'live' debugging."),
c906108c
SS
2584 &tfindlist);
2585
2586 add_cmd ("start", class_trace, trace_find_start_command,
1a966eab 2587 _("Select the first trace frame in the trace buffer."),
c906108c
SS
2588 &tfindlist);
2589
c5aa993b 2590 add_com ("tstatus", class_trace, trace_status_command,
1bedd215 2591 _("Display the status of the current trace data collection."));
c906108c 2592
c5aa993b 2593 add_com ("tstop", class_trace, trace_stop_command,
1bedd215 2594 _("Stop trace data collection."));
c906108c
SS
2595
2596 add_com ("tstart", class_trace, trace_start_command,
1bedd215 2597 _("Start trace data collection."));
c906108c 2598
1bedd215
AC
2599 add_com ("end", class_trace, end_actions_pseudocommand, _("\
2600Ends a list of commands or actions.\n\
c906108c
SS
2601Several GDB commands allow you to enter a list of commands or actions.\n\
2602Entering \"end\" on a line by itself is the normal way to terminate\n\
2603such a list.\n\n\
1bedd215 2604Note: the \"end\" command cannot be used at the gdb prompt."));
c906108c 2605
1bedd215
AC
2606 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
2607Specify single-stepping behavior at a tracepoint.\n\
c906108c
SS
2608Argument is number of instructions to trace in single-step mode\n\
2609following the tracepoint. This command is normally followed by\n\
2610one or more \"collect\" commands, to specify what to collect\n\
2611while single-stepping.\n\n\
1bedd215 2612Note: this command can only be used in a tracepoint \"actions\" list."));
c906108c 2613
c5aa993b
JM
2614 add_com_alias ("ws", "while-stepping", class_alias, 0);
2615 add_com_alias ("stepping", "while-stepping", class_alias, 0);
c906108c 2616
1bedd215
AC
2617 add_com ("collect", class_trace, collect_pseudocommand, _("\
2618Specify one or more data items to be collected at a tracepoint.\n\
c906108c
SS
2619Accepts a comma-separated list of (one or more) expressions. GDB will\n\
2620collect all data (variables, registers) referenced by that expression.\n\
2621Also accepts the following special arguments:\n\
2622 $regs -- all registers.\n\
2623 $args -- all function arguments.\n\
2624 $locals -- all variables local to the block/function scope.\n\
1bedd215 2625Note: this command can only be used in a tracepoint \"actions\" list."));
c906108c 2626
1bedd215
AC
2627 add_com ("actions", class_trace, trace_actions_command, _("\
2628Specify the actions to be taken at a tracepoint.\n\
c906108c
SS
2629Tracepoint actions may include collecting of specified data, \n\
2630single-stepping, or enabling/disabling other tracepoints, \n\
1bedd215 2631depending on target's capabilities."));
c906108c 2632
236f1d4d
SS
2633 default_collect = xstrdup ("");
2634 add_setshow_string_cmd ("default-collect", class_trace,
2635 &default_collect, _("\
2636Set the list of expressions to collect by default"), _("\
2637Show the list of expressions to collect by default"), NULL,
2638 NULL, NULL,
2639 &setlist, &showlist);
2640
6d820c5c
DJ
2641 target_buf_size = 2048;
2642 target_buf = xmalloc (target_buf_size);
c906108c 2643}
This page took 1.151845 seconds and 4 git commands to generate.