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