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