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