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