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