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