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