2010-04-01 Stan Shebs <stan@codesourcery.com>
[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 "linespec.h"
36 #include "regcache.h"
37 #include "completer.h"
38 #include "block.h"
39 #include "dictionary.h"
40 #include "observer.h"
41 #include "user-regs.h"
42 #include "valprint.h"
43 #include "gdbcore.h"
44 #include "objfiles.h"
45 #include "filenames.h"
46 #include "gdbthread.h"
47 #include "stack.h"
48
49 #include "ax.h"
50 #include "ax-gdb.h"
51
52 /* readline include files */
53 #include "readline/readline.h"
54 #include "readline/history.h"
55
56 /* readline defines this. */
57 #undef savestring
58
59 #ifdef HAVE_UNISTD_H
60 #include <unistd.h>
61 #endif
62
63 #ifndef O_LARGEFILE
64 #define O_LARGEFILE 0
65 #endif
66
67 extern int hex2bin (const char *hex, gdb_byte *bin, int count);
68 extern int bin2hex (const gdb_byte *bin, char *hex, int count);
69
70 extern void stop_tracing ();
71
72 /* Maximum length of an agent aexpression.
73 This accounts for the fact that packets are limited to 400 bytes
74 (which includes everything -- including the checksum), and assumes
75 the worst case of maximum length for each of the pieces of a
76 continuation packet.
77
78 NOTE: expressions get mem2hex'ed otherwise this would be twice as
79 large. (400 - 31)/2 == 184 */
80 #define MAX_AGENT_EXPR_LEN 184
81
82 /* A hook used to notify the UI of tracepoint operations. */
83
84 void (*deprecated_trace_find_hook) (char *arg, int from_tty);
85 void (*deprecated_trace_start_stop_hook) (int start, int from_tty);
86
87 extern void (*deprecated_readline_begin_hook) (char *, ...);
88 extern char *(*deprecated_readline_hook) (char *);
89 extern void (*deprecated_readline_end_hook) (void);
90
91 /* GDB commands implemented in other modules:
92 */
93
94 extern void output_command (char *, int);
95
96 /*
97 Tracepoint.c:
98
99 This module defines the following debugger commands:
100 trace : set a tracepoint on a function, line, or address.
101 info trace : list all debugger-defined tracepoints.
102 delete trace : delete one or more tracepoints.
103 enable trace : enable one or more tracepoints.
104 disable trace : disable one or more tracepoints.
105 actions : specify actions to be taken at a tracepoint.
106 passcount : specify a pass count for a tracepoint.
107 tstart : start a trace experiment.
108 tstop : stop a trace experiment.
109 tstatus : query the status of a trace experiment.
110 tfind : find a trace frame in the trace buffer.
111 tdump : print everything collected at the current tracepoint.
112 save-tracepoints : write tracepoint setup into a file.
113
114 This module defines the following user-visible debugger variables:
115 $trace_frame : sequence number of trace frame currently being debugged.
116 $trace_line : source line of trace frame currently being debugged.
117 $trace_file : source file of trace frame currently being debugged.
118 $tracepoint : tracepoint number of trace frame currently being debugged.
119 */
120
121
122 /* ======= Important global variables: ======= */
123
124 /* The list of all trace state variables. We don't retain pointers to
125 any of these for any reason - API is by name or number only - so it
126 works to have a vector of objects. */
127
128 typedef struct trace_state_variable tsv_s;
129 DEF_VEC_O(tsv_s);
130
131 static VEC(tsv_s) *tvariables;
132
133 /* The next integer to assign to a variable. */
134
135 static int next_tsv_number = 1;
136
137 /* Number of last traceframe collected. */
138 static int traceframe_number;
139
140 /* Tracepoint for last traceframe collected. */
141 static int tracepoint_number;
142
143 /* Symbol for function for last traceframe collected */
144 static struct symbol *traceframe_fun;
145
146 /* Symtab and line for last traceframe collected */
147 static struct symtab_and_line traceframe_sal;
148
149 /* Tracing command lists */
150 static struct cmd_list_element *tfindlist;
151
152 /* List of expressions to collect by default at each tracepoint hit. */
153 char *default_collect = "";
154
155 static int disconnected_tracing;
156
157 /* This variable controls whether we ask the target for a linear or
158 circular trace buffer. */
159
160 static int circular_trace_buffer;
161
162 /* ======= Important command functions: ======= */
163 static void trace_actions_command (char *, int);
164 static void trace_start_command (char *, int);
165 static void trace_stop_command (char *, int);
166 static void trace_status_command (char *, int);
167 static void trace_find_command (char *, int);
168 static void trace_find_pc_command (char *, int);
169 static void trace_find_tracepoint_command (char *, int);
170 static void trace_find_line_command (char *, int);
171 static void trace_find_range_command (char *, int);
172 static void trace_find_outside_command (char *, int);
173 static void trace_dump_command (char *, int);
174
175 /* support routines */
176
177 struct collection_list;
178 static void add_aexpr (struct collection_list *, struct agent_expr *);
179 static char *mem2hex (gdb_byte *, char *, int);
180 static void add_register (struct collection_list *collection,
181 unsigned int regno);
182
183 extern void send_disconnected_tracing_value (int value);
184
185 static void free_uploaded_tps (struct uploaded_tp **utpp);
186 static void free_uploaded_tsvs (struct uploaded_tsv **utsvp);
187
188
189 extern void _initialize_tracepoint (void);
190
191 static struct trace_status trace_status;
192
193 char *stop_reason_names[] = {
194 "tunknown",
195 "tnotrun",
196 "tstop",
197 "tfull",
198 "tdisconnected",
199 "tpasscount",
200 "terror"
201 };
202
203 struct trace_status *
204 current_trace_status ()
205 {
206 return &trace_status;
207 }
208
209 /* Set traceframe number to NUM. */
210 static void
211 set_traceframe_num (int num)
212 {
213 traceframe_number = num;
214 set_internalvar_integer (lookup_internalvar ("trace_frame"), num);
215 }
216
217 /* Set tracepoint number to NUM. */
218 static void
219 set_tracepoint_num (int num)
220 {
221 tracepoint_number = num;
222 set_internalvar_integer (lookup_internalvar ("tracepoint"), num);
223 }
224
225 /* Set externally visible debug variables for querying/printing
226 the traceframe context (line, function, file) */
227
228 static void
229 set_traceframe_context (struct frame_info *trace_frame)
230 {
231 CORE_ADDR trace_pc;
232
233 if (trace_frame == NULL) /* Cease debugging any trace buffers. */
234 {
235 traceframe_fun = 0;
236 traceframe_sal.pc = traceframe_sal.line = 0;
237 traceframe_sal.symtab = NULL;
238 clear_internalvar (lookup_internalvar ("trace_func"));
239 clear_internalvar (lookup_internalvar ("trace_file"));
240 set_internalvar_integer (lookup_internalvar ("trace_line"), -1);
241 return;
242 }
243
244 /* Save as globals for internal use. */
245 trace_pc = get_frame_pc (trace_frame);
246 traceframe_sal = find_pc_line (trace_pc, 0);
247 traceframe_fun = find_pc_function (trace_pc);
248
249 /* Save linenumber as "$trace_line", a debugger variable visible to
250 users. */
251 set_internalvar_integer (lookup_internalvar ("trace_line"),
252 traceframe_sal.line);
253
254 /* Save func name as "$trace_func", a debugger variable visible to
255 users. */
256 if (traceframe_fun == NULL
257 || SYMBOL_LINKAGE_NAME (traceframe_fun) == NULL)
258 clear_internalvar (lookup_internalvar ("trace_func"));
259 else
260 set_internalvar_string (lookup_internalvar ("trace_func"),
261 SYMBOL_LINKAGE_NAME (traceframe_fun));
262
263 /* Save file name as "$trace_file", a debugger variable visible to
264 users. */
265 if (traceframe_sal.symtab == NULL
266 || traceframe_sal.symtab->filename == NULL)
267 clear_internalvar (lookup_internalvar ("trace_file"));
268 else
269 set_internalvar_string (lookup_internalvar ("trace_file"),
270 traceframe_sal.symtab->filename);
271 }
272
273 /* Create a new trace state variable with the given name. */
274
275 struct trace_state_variable *
276 create_trace_state_variable (const char *name)
277 {
278 struct trace_state_variable tsv;
279
280 memset (&tsv, 0, sizeof (tsv));
281 tsv.name = xstrdup (name);
282 tsv.number = next_tsv_number++;
283 return VEC_safe_push (tsv_s, tvariables, &tsv);
284 }
285
286 /* Look for a trace state variable of the given name. */
287
288 struct trace_state_variable *
289 find_trace_state_variable (const char *name)
290 {
291 struct trace_state_variable *tsv;
292 int ix;
293
294 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
295 if (strcmp (name, tsv->name) == 0)
296 return tsv;
297
298 return NULL;
299 }
300
301 void
302 delete_trace_state_variable (const char *name)
303 {
304 struct trace_state_variable *tsv;
305 int ix;
306
307 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
308 if (strcmp (name, tsv->name) == 0)
309 {
310 xfree ((void *)tsv->name);
311 VEC_unordered_remove (tsv_s, tvariables, ix);
312 return;
313 }
314
315 warning (_("No trace variable named \"$%s\", not deleting"), name);
316 }
317
318 /* The 'tvariable' command collects a name and optional expression to
319 evaluate into an initial value. */
320
321 void
322 trace_variable_command (char *args, int from_tty)
323 {
324 struct expression *expr;
325 struct cleanup *old_chain;
326 struct internalvar *intvar = NULL;
327 LONGEST initval = 0;
328 struct trace_state_variable *tsv;
329
330 if (!args || !*args)
331 error_no_arg (_("trace state variable name"));
332
333 /* All the possible valid arguments are expressions. */
334 expr = parse_expression (args);
335 old_chain = make_cleanup (free_current_contents, &expr);
336
337 if (expr->nelts == 0)
338 error (_("No expression?"));
339
340 /* Only allow two syntaxes; "$name" and "$name=value". */
341 if (expr->elts[0].opcode == OP_INTERNALVAR)
342 {
343 intvar = expr->elts[1].internalvar;
344 }
345 else if (expr->elts[0].opcode == BINOP_ASSIGN
346 && expr->elts[1].opcode == OP_INTERNALVAR)
347 {
348 intvar = expr->elts[2].internalvar;
349 initval = value_as_long (evaluate_subexpression_type (expr, 4));
350 }
351 else
352 error (_("Syntax must be $NAME [ = EXPR ]"));
353
354 if (!intvar)
355 error (_("No name given"));
356
357 if (strlen (internalvar_name (intvar)) <= 0)
358 error (_("Must supply a non-empty variable name"));
359
360 /* If the variable already exists, just change its initial value. */
361 tsv = find_trace_state_variable (internalvar_name (intvar));
362 if (tsv)
363 {
364 tsv->initial_value = initval;
365 printf_filtered (_("Trace state variable $%s now has initial value %s.\n"),
366 tsv->name, plongest (tsv->initial_value));
367 return;
368 }
369
370 /* Create a new variable. */
371 tsv = create_trace_state_variable (internalvar_name (intvar));
372 tsv->initial_value = initval;
373
374 printf_filtered (_("Trace state variable $%s created, with initial value %s.\n"),
375 tsv->name, plongest (tsv->initial_value));
376
377 do_cleanups (old_chain);
378 }
379
380 void
381 delete_trace_variable_command (char *args, int from_tty)
382 {
383 int i, ix;
384 char **argv;
385 struct cleanup *back_to;
386 struct trace_state_variable *tsv;
387
388 if (args == NULL)
389 {
390 if (query (_("Delete all trace state variables? ")))
391 VEC_free (tsv_s, tvariables);
392 dont_repeat ();
393 return;
394 }
395
396 argv = gdb_buildargv (args);
397 back_to = make_cleanup_freeargv (argv);
398
399 for (i = 0; argv[i] != NULL; i++)
400 {
401 if (*argv[i] == '$')
402 delete_trace_state_variable (argv[i] + 1);
403 else
404 warning (_("Name \"%s\" not prefixed with '$', ignoring"), argv[i]);
405 }
406
407 do_cleanups (back_to);
408
409 dont_repeat ();
410 }
411
412 void
413 tvariables_info_1 (void)
414 {
415 struct trace_state_variable *tsv;
416 int ix;
417 int count = 0;
418 struct cleanup *back_to;
419
420 if (VEC_length (tsv_s, tvariables) == 0 && !ui_out_is_mi_like_p (uiout))
421 {
422 printf_filtered (_("No trace state variables.\n"));
423 return;
424 }
425
426 /* Try to acquire values from the target. */
427 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix, ++count)
428 tsv->value_known = target_get_trace_state_variable_value (tsv->number,
429 &(tsv->value));
430
431 back_to = make_cleanup_ui_out_table_begin_end (uiout, 3,
432 count, "trace-variables");
433 ui_out_table_header (uiout, 15, ui_left, "name", "Name");
434 ui_out_table_header (uiout, 11, ui_left, "initial", "Initial");
435 ui_out_table_header (uiout, 11, ui_left, "current", "Current");
436
437 ui_out_table_body (uiout);
438
439 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
440 {
441 struct cleanup *back_to2;
442 char *c;
443 char *name;
444
445 back_to2 = make_cleanup_ui_out_tuple_begin_end (uiout, "variable");
446
447 name = concat ("$", tsv->name, NULL);
448 make_cleanup (xfree, name);
449 ui_out_field_string (uiout, "name", name);
450 ui_out_field_string (uiout, "initial", plongest (tsv->initial_value));
451
452 if (tsv->value_known)
453 c = plongest (tsv->value);
454 else if (ui_out_is_mi_like_p (uiout))
455 /* For MI, we prefer not to use magic string constants, but rather
456 omit the field completely. The difference between unknown and
457 undefined does not seem important enough to represent. */
458 c = NULL;
459 else if (current_trace_status ()->running || traceframe_number >= 0)
460 /* The value is/was defined, but we don't have it. */
461 c = "<unknown>";
462 else
463 /* It is not meaningful to ask about the value. */
464 c = "<undefined>";
465 if (c)
466 ui_out_field_string (uiout, "current", c);
467 ui_out_text (uiout, "\n");
468
469 do_cleanups (back_to2);
470 }
471
472 do_cleanups (back_to);
473 }
474
475 /* List all the trace state variables. */
476
477 static void
478 tvariables_info (char *args, int from_tty)
479 {
480 tvariables_info_1 ();
481 }
482
483 /* Stash definitions of tsvs into the given file. */
484
485 void
486 save_trace_state_variables (struct ui_file *fp)
487 {
488 struct trace_state_variable *tsv;
489 int ix;
490
491 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
492 {
493 fprintf_unfiltered (fp, "tvariable $%s", tsv->name);
494 if (tsv->initial_value)
495 fprintf_unfiltered (fp, " = %s", plongest (tsv->initial_value));
496 fprintf_unfiltered (fp, "\n");
497 }
498 }
499
500 /* ACTIONS functions: */
501
502 /* The three functions:
503 collect_pseudocommand,
504 while_stepping_pseudocommand, and
505 end_actions_pseudocommand
506 are placeholders for "commands" that are actually ONLY to be used
507 within a tracepoint action list. If the actual function is ever called,
508 it means that somebody issued the "command" at the top level,
509 which is always an error. */
510
511 void
512 end_actions_pseudocommand (char *args, int from_tty)
513 {
514 error (_("This command cannot be used at the top level."));
515 }
516
517 void
518 while_stepping_pseudocommand (char *args, int from_tty)
519 {
520 error (_("This command can only be used in a tracepoint actions list."));
521 }
522
523 static void
524 collect_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 teval_pseudocommand (char *args, int from_tty)
531 {
532 error (_("This command can only be used in a tracepoint actions list."));
533 }
534
535 /* Enter a list of actions for a tracepoint. */
536 static void
537 trace_actions_command (char *args, int from_tty)
538 {
539 struct breakpoint *t;
540 struct command_line *l;
541
542 t = get_tracepoint_by_number (&args, 0, 1);
543 if (t)
544 {
545 char *tmpbuf =
546 xstrprintf ("Enter actions for tracepoint %d, one per line.",
547 t->number);
548 struct cleanup *cleanups = make_cleanup (xfree, tmpbuf);
549
550 l = read_command_lines (tmpbuf, from_tty, 1, check_tracepoint_command, t);
551 do_cleanups (cleanups);
552 breakpoint_set_commands (t, l);
553 }
554 /* else just return */
555 }
556
557 /* Report the results of checking the agent expression, as errors or
558 internal errors. */
559
560 static void
561 report_agent_reqs_errors (struct agent_expr *aexpr, struct agent_reqs *areqs)
562 {
563 /* All of the "flaws" are serious bytecode generation issues that
564 should never occur. */
565 if (areqs->flaw != agent_flaw_none)
566 internal_error (__FILE__, __LINE__, _("expression is malformed"));
567
568 /* If analysis shows a stack underflow, GDB must have done something
569 badly wrong in its bytecode generation. */
570 if (areqs->min_height < 0)
571 internal_error (__FILE__, __LINE__,
572 _("expression has min height < 0"));
573
574 /* Issue this error if the stack is predicted to get too deep. The
575 limit is rather arbitrary; a better scheme might be for the
576 target to report how much stack it will have available. The
577 depth roughly corresponds to parenthesization, so a limit of 20
578 amounts to 20 levels of expression nesting, which is actually
579 a pretty big hairy expression. */
580 if (areqs->max_height > 20)
581 error (_("Expression is too complicated."));
582 }
583
584 /* worker function */
585 void
586 validate_actionline (char **line, struct breakpoint *t)
587 {
588 struct cmd_list_element *c;
589 struct expression *exp = NULL;
590 struct cleanup *old_chain = NULL;
591 char *p, *tmp_p;
592 struct bp_location *loc;
593 struct agent_expr *aexpr;
594 struct agent_reqs areqs;
595
596 /* if EOF is typed, *line is NULL */
597 if (*line == NULL)
598 return;
599
600 for (p = *line; isspace ((int) *p);)
601 p++;
602
603 /* Symbol lookup etc. */
604 if (*p == '\0') /* empty line: just prompt for another line. */
605 return;
606
607 if (*p == '#') /* comment line */
608 return;
609
610 c = lookup_cmd (&p, cmdlist, "", -1, 1);
611 if (c == 0)
612 error (_("`%s' is not a tracepoint action, or is ambiguous."), p);
613
614 if (cmd_cfunc_eq (c, collect_pseudocommand))
615 {
616 do
617 { /* repeat over a comma-separated list */
618 QUIT; /* allow user to bail out with ^C */
619 while (isspace ((int) *p))
620 p++;
621
622 if (*p == '$') /* look for special pseudo-symbols */
623 {
624 if ((0 == strncasecmp ("reg", p + 1, 3)) ||
625 (0 == strncasecmp ("arg", p + 1, 3)) ||
626 (0 == strncasecmp ("loc", p + 1, 3)))
627 {
628 p = strchr (p, ',');
629 continue;
630 }
631 /* else fall thru, treat p as an expression and parse it! */
632 }
633 tmp_p = p;
634 for (loc = t->loc; loc; loc = loc->next)
635 {
636 p = tmp_p;
637 exp = parse_exp_1 (&p, block_for_pc (loc->address), 1);
638 old_chain = make_cleanup (free_current_contents, &exp);
639
640 if (exp->elts[0].opcode == OP_VAR_VALUE)
641 {
642 if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_CONST)
643 {
644 error (_("constant `%s' (value %ld) will not be collected."),
645 SYMBOL_PRINT_NAME (exp->elts[2].symbol),
646 SYMBOL_VALUE (exp->elts[2].symbol));
647 }
648 else if (SYMBOL_CLASS (exp->elts[2].symbol) == LOC_OPTIMIZED_OUT)
649 {
650 error (_("`%s' is optimized away and cannot be collected."),
651 SYMBOL_PRINT_NAME (exp->elts[2].symbol));
652 }
653 }
654
655 /* We have something to collect, make sure that the expr to
656 bytecode translator can handle it and that it's not too
657 long. */
658 aexpr = gen_trace_for_expr (loc->address, exp);
659 make_cleanup_free_agent_expr (aexpr);
660
661 if (aexpr->len > MAX_AGENT_EXPR_LEN)
662 error (_("Expression is too complicated."));
663
664 ax_reqs (aexpr, &areqs);
665 (void) make_cleanup (xfree, areqs.reg_mask);
666
667 report_agent_reqs_errors (aexpr, &areqs);
668
669 do_cleanups (old_chain);
670 }
671 }
672 while (p && *p++ == ',');
673 }
674
675 else if (cmd_cfunc_eq (c, teval_pseudocommand))
676 {
677 do
678 { /* repeat over a comma-separated list */
679 QUIT; /* allow user to bail out with ^C */
680 while (isspace ((int) *p))
681 p++;
682
683 tmp_p = p;
684 for (loc = t->loc; loc; loc = loc->next)
685 {
686 p = tmp_p;
687 /* Only expressions are allowed for this action. */
688 exp = parse_exp_1 (&p, block_for_pc (loc->address), 1);
689 old_chain = make_cleanup (free_current_contents, &exp);
690
691 /* We have something to evaluate, make sure that the expr to
692 bytecode translator can handle it and that it's not too
693 long. */
694 aexpr = gen_eval_for_expr (loc->address, exp);
695 make_cleanup_free_agent_expr (aexpr);
696
697 if (aexpr->len > MAX_AGENT_EXPR_LEN)
698 error (_("Expression is too complicated."));
699
700 ax_reqs (aexpr, &areqs);
701 (void) make_cleanup (xfree, areqs.reg_mask);
702
703 report_agent_reqs_errors (aexpr, &areqs);
704
705 do_cleanups (old_chain);
706 }
707 }
708 while (p && *p++ == ',');
709 }
710
711 else if (cmd_cfunc_eq (c, while_stepping_pseudocommand))
712 {
713 char *steparg; /* in case warning is necessary */
714
715 while (isspace ((int) *p))
716 p++;
717 steparg = p;
718
719 if (*p == '\0' || (t->step_count = strtol (p, &p, 0)) == 0)
720 error (_("while-stepping step count `%s' is malformed."), *line);
721 }
722
723 else if (cmd_cfunc_eq (c, end_actions_pseudocommand))
724 ;
725
726 else
727 error (_("`%s' is not a supported tracepoint action."), *line);
728 }
729
730 enum {
731 memrange_absolute = -1
732 };
733
734 struct memrange
735 {
736 int type; /* memrange_absolute for absolute memory range,
737 else basereg number */
738 bfd_signed_vma start;
739 bfd_signed_vma end;
740 };
741
742 struct collection_list
743 {
744 unsigned char regs_mask[32]; /* room for up to 256 regs */
745 long listsize;
746 long next_memrange;
747 struct memrange *list;
748 long aexpr_listsize; /* size of array pointed to by expr_list elt */
749 long next_aexpr_elt;
750 struct agent_expr **aexpr_list;
751
752 }
753 tracepoint_list, stepping_list;
754
755 /* MEMRANGE functions: */
756
757 static int memrange_cmp (const void *, const void *);
758
759 /* compare memranges for qsort */
760 static int
761 memrange_cmp (const void *va, const void *vb)
762 {
763 const struct memrange *a = va, *b = vb;
764
765 if (a->type < b->type)
766 return -1;
767 if (a->type > b->type)
768 return 1;
769 if (a->type == memrange_absolute)
770 {
771 if ((bfd_vma) a->start < (bfd_vma) b->start)
772 return -1;
773 if ((bfd_vma) a->start > (bfd_vma) b->start)
774 return 1;
775 }
776 else
777 {
778 if (a->start < b->start)
779 return -1;
780 if (a->start > b->start)
781 return 1;
782 }
783 return 0;
784 }
785
786 /* Sort the memrange list using qsort, and merge adjacent memranges. */
787 static void
788 memrange_sortmerge (struct collection_list *memranges)
789 {
790 int a, b;
791
792 qsort (memranges->list, memranges->next_memrange,
793 sizeof (struct memrange), memrange_cmp);
794 if (memranges->next_memrange > 0)
795 {
796 for (a = 0, b = 1; b < memranges->next_memrange; b++)
797 {
798 if (memranges->list[a].type == memranges->list[b].type &&
799 memranges->list[b].start - memranges->list[a].end <=
800 MAX_REGISTER_SIZE)
801 {
802 /* memrange b starts before memrange a ends; merge them. */
803 if (memranges->list[b].end > memranges->list[a].end)
804 memranges->list[a].end = memranges->list[b].end;
805 continue; /* next b, same a */
806 }
807 a++; /* next a */
808 if (a != b)
809 memcpy (&memranges->list[a], &memranges->list[b],
810 sizeof (struct memrange));
811 }
812 memranges->next_memrange = a + 1;
813 }
814 }
815
816 /* Add a register to a collection list. */
817 static void
818 add_register (struct collection_list *collection, unsigned int regno)
819 {
820 if (info_verbose)
821 printf_filtered ("collect register %d\n", regno);
822 if (regno >= (8 * sizeof (collection->regs_mask)))
823 error (_("Internal: register number %d too large for tracepoint"),
824 regno);
825 collection->regs_mask[regno / 8] |= 1 << (regno % 8);
826 }
827
828 /* Add a memrange to a collection list */
829 static void
830 add_memrange (struct collection_list *memranges,
831 int type, bfd_signed_vma base,
832 unsigned long len)
833 {
834 if (info_verbose)
835 {
836 printf_filtered ("(%d,", type);
837 printf_vma (base);
838 printf_filtered (",%ld)\n", len);
839 }
840
841 /* type: memrange_absolute == memory, other n == basereg */
842 memranges->list[memranges->next_memrange].type = type;
843 /* base: addr if memory, offset if reg relative. */
844 memranges->list[memranges->next_memrange].start = base;
845 /* len: we actually save end (base + len) for convenience */
846 memranges->list[memranges->next_memrange].end = base + len;
847 memranges->next_memrange++;
848 if (memranges->next_memrange >= memranges->listsize)
849 {
850 memranges->listsize *= 2;
851 memranges->list = xrealloc (memranges->list,
852 memranges->listsize);
853 }
854
855 if (type != memrange_absolute) /* Better collect the base register! */
856 add_register (memranges, type);
857 }
858
859 /* Add a symbol to a collection list. */
860 static void
861 collect_symbol (struct collection_list *collect,
862 struct symbol *sym,
863 struct gdbarch *gdbarch,
864 long frame_regno, long frame_offset,
865 CORE_ADDR scope)
866 {
867 unsigned long len;
868 unsigned int reg;
869 bfd_signed_vma offset;
870 int treat_as_expr = 0;
871
872 len = TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym)));
873 switch (SYMBOL_CLASS (sym))
874 {
875 default:
876 printf_filtered ("%s: don't know symbol class %d\n",
877 SYMBOL_PRINT_NAME (sym),
878 SYMBOL_CLASS (sym));
879 break;
880 case LOC_CONST:
881 printf_filtered ("constant %s (value %ld) will not be collected.\n",
882 SYMBOL_PRINT_NAME (sym), SYMBOL_VALUE (sym));
883 break;
884 case LOC_STATIC:
885 offset = SYMBOL_VALUE_ADDRESS (sym);
886 if (info_verbose)
887 {
888 char tmp[40];
889
890 sprintf_vma (tmp, offset);
891 printf_filtered ("LOC_STATIC %s: collect %ld bytes at %s.\n",
892 SYMBOL_PRINT_NAME (sym), len,
893 tmp /* address */);
894 }
895 /* A struct may be a C++ class with static fields, go to general
896 expression handling. */
897 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT)
898 treat_as_expr = 1;
899 else
900 add_memrange (collect, memrange_absolute, offset, len);
901 break;
902 case LOC_REGISTER:
903 reg = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
904 if (info_verbose)
905 printf_filtered ("LOC_REG[parm] %s: ",
906 SYMBOL_PRINT_NAME (sym));
907 add_register (collect, reg);
908 /* Check for doubles stored in two registers. */
909 /* FIXME: how about larger types stored in 3 or more regs? */
910 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT &&
911 len > register_size (gdbarch, reg))
912 add_register (collect, reg + 1);
913 break;
914 case LOC_REF_ARG:
915 printf_filtered ("Sorry, don't know how to do LOC_REF_ARG yet.\n");
916 printf_filtered (" (will not collect %s)\n",
917 SYMBOL_PRINT_NAME (sym));
918 break;
919 case LOC_ARG:
920 reg = frame_regno;
921 offset = frame_offset + SYMBOL_VALUE (sym);
922 if (info_verbose)
923 {
924 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
925 SYMBOL_PRINT_NAME (sym), len);
926 printf_vma (offset);
927 printf_filtered (" from frame ptr reg %d\n", reg);
928 }
929 add_memrange (collect, reg, offset, len);
930 break;
931 case LOC_REGPARM_ADDR:
932 reg = SYMBOL_VALUE (sym);
933 offset = 0;
934 if (info_verbose)
935 {
936 printf_filtered ("LOC_REGPARM_ADDR %s: Collect %ld bytes at offset ",
937 SYMBOL_PRINT_NAME (sym), len);
938 printf_vma (offset);
939 printf_filtered (" from reg %d\n", reg);
940 }
941 add_memrange (collect, reg, offset, len);
942 break;
943 case LOC_LOCAL:
944 reg = frame_regno;
945 offset = frame_offset + SYMBOL_VALUE (sym);
946 if (info_verbose)
947 {
948 printf_filtered ("LOC_LOCAL %s: Collect %ld bytes at offset ",
949 SYMBOL_PRINT_NAME (sym), len);
950 printf_vma (offset);
951 printf_filtered (" from frame ptr reg %d\n", reg);
952 }
953 add_memrange (collect, reg, offset, len);
954 break;
955
956 case LOC_UNRESOLVED:
957 treat_as_expr = 1;
958 break;
959
960 case LOC_OPTIMIZED_OUT:
961 printf_filtered ("%s has been optimized out of existence.\n",
962 SYMBOL_PRINT_NAME (sym));
963 break;
964
965 case LOC_COMPUTED:
966 treat_as_expr = 1;
967 break;
968 }
969
970 /* Expressions are the most general case. */
971 if (treat_as_expr)
972 {
973 struct agent_expr *aexpr;
974 struct cleanup *old_chain1 = NULL;
975 struct agent_reqs areqs;
976
977 aexpr = gen_trace_for_var (scope, gdbarch, sym);
978
979 /* It can happen that the symbol is recorded as a computed
980 location, but it's been optimized away and doesn't actually
981 have a location expression. */
982 if (!aexpr)
983 {
984 printf_filtered ("%s has been optimized out of existence.\n",
985 SYMBOL_PRINT_NAME (sym));
986 return;
987 }
988
989 old_chain1 = make_cleanup_free_agent_expr (aexpr);
990
991 ax_reqs (aexpr, &areqs);
992
993 report_agent_reqs_errors (aexpr, &areqs);
994
995 discard_cleanups (old_chain1);
996 add_aexpr (collect, aexpr);
997
998 /* take care of the registers */
999 if (areqs.reg_mask_len > 0)
1000 {
1001 int ndx1, ndx2;
1002
1003 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
1004 {
1005 QUIT; /* allow user to bail out with ^C */
1006 if (areqs.reg_mask[ndx1] != 0)
1007 {
1008 /* assume chars have 8 bits */
1009 for (ndx2 = 0; ndx2 < 8; ndx2++)
1010 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1011 /* it's used -- record it */
1012 add_register (collect, ndx1 * 8 + ndx2);
1013 }
1014 }
1015 }
1016 }
1017 }
1018
1019 /* Data to be passed around in the calls to the locals and args
1020 iterators. */
1021
1022 struct add_local_symbols_data
1023 {
1024 struct collection_list *collect;
1025 struct gdbarch *gdbarch;
1026 CORE_ADDR pc;
1027 long frame_regno;
1028 long frame_offset;
1029 int count;
1030 };
1031
1032 /* The callback for the locals and args iterators */
1033
1034 static void
1035 do_collect_symbol (const char *print_name,
1036 struct symbol *sym,
1037 void *cb_data)
1038 {
1039 struct add_local_symbols_data *p = cb_data;
1040
1041 collect_symbol (p->collect, sym, p->gdbarch, p->frame_regno,
1042 p->frame_offset, p->pc);
1043 p->count++;
1044 }
1045
1046 /* Add all locals (or args) symbols to collection list */
1047 static void
1048 add_local_symbols (struct collection_list *collect,
1049 struct gdbarch *gdbarch, CORE_ADDR pc,
1050 long frame_regno, long frame_offset, int type)
1051 {
1052 struct block *block;
1053 struct add_local_symbols_data cb_data;
1054
1055 cb_data.collect = collect;
1056 cb_data.gdbarch = gdbarch;
1057 cb_data.pc = pc;
1058 cb_data.frame_regno = frame_regno;
1059 cb_data.frame_offset = frame_offset;
1060 cb_data.count = 0;
1061
1062 if (type == 'L')
1063 {
1064 block = block_for_pc (pc);
1065 if (block == NULL)
1066 {
1067 warning (_("Can't collect locals; "
1068 "no symbol table info available.\n"));
1069 return;
1070 }
1071
1072 iterate_over_block_local_vars (block, do_collect_symbol, &cb_data);
1073 if (cb_data.count == 0)
1074 warning (_("No locals found in scope."));
1075 }
1076 else
1077 {
1078 pc = get_pc_function_start (pc);
1079 block = block_for_pc (pc);
1080 if (block == NULL)
1081 {
1082 warning (_("Can't collect args; no symbol table info available.\n"));
1083 return;
1084 }
1085
1086 iterate_over_block_arg_vars (block, do_collect_symbol, &cb_data);
1087 if (cb_data.count == 0)
1088 warning (_("No args found in scope."));
1089 }
1090 }
1091
1092 /* worker function */
1093 static void
1094 clear_collection_list (struct collection_list *list)
1095 {
1096 int ndx;
1097
1098 list->next_memrange = 0;
1099 for (ndx = 0; ndx < list->next_aexpr_elt; ndx++)
1100 {
1101 free_agent_expr (list->aexpr_list[ndx]);
1102 list->aexpr_list[ndx] = NULL;
1103 }
1104 list->next_aexpr_elt = 0;
1105 memset (list->regs_mask, 0, sizeof (list->regs_mask));
1106 }
1107
1108 /* reduce a collection list to string form (for gdb protocol) */
1109 static char **
1110 stringify_collection_list (struct collection_list *list, char *string)
1111 {
1112 char temp_buf[2048];
1113 char tmp2[40];
1114 int count;
1115 int ndx = 0;
1116 char *(*str_list)[];
1117 char *end;
1118 long i;
1119
1120 count = 1 + list->next_memrange + list->next_aexpr_elt + 1;
1121 str_list = (char *(*)[]) xmalloc (count * sizeof (char *));
1122
1123 for (i = sizeof (list->regs_mask) - 1; i > 0; i--)
1124 if (list->regs_mask[i] != 0) /* skip leading zeroes in regs_mask */
1125 break;
1126 if (list->regs_mask[i] != 0) /* prepare to send regs_mask to the stub */
1127 {
1128 if (info_verbose)
1129 printf_filtered ("\nCollecting registers (mask): 0x");
1130 end = temp_buf;
1131 *end++ = 'R';
1132 for (; i >= 0; i--)
1133 {
1134 QUIT; /* allow user to bail out with ^C */
1135 if (info_verbose)
1136 printf_filtered ("%02X", list->regs_mask[i]);
1137 sprintf (end, "%02X", list->regs_mask[i]);
1138 end += 2;
1139 }
1140 (*str_list)[ndx] = xstrdup (temp_buf);
1141 ndx++;
1142 }
1143 if (info_verbose)
1144 printf_filtered ("\n");
1145 if (list->next_memrange > 0 && info_verbose)
1146 printf_filtered ("Collecting memranges: \n");
1147 for (i = 0, count = 0, end = temp_buf; i < list->next_memrange; i++)
1148 {
1149 QUIT; /* allow user to bail out with ^C */
1150 sprintf_vma (tmp2, list->list[i].start);
1151 if (info_verbose)
1152 {
1153 printf_filtered ("(%d, %s, %ld)\n",
1154 list->list[i].type,
1155 tmp2,
1156 (long) (list->list[i].end - list->list[i].start));
1157 }
1158 if (count + 27 > MAX_AGENT_EXPR_LEN)
1159 {
1160 (*str_list)[ndx] = savestring (temp_buf, count);
1161 ndx++;
1162 count = 0;
1163 end = temp_buf;
1164 }
1165
1166 {
1167 bfd_signed_vma length = list->list[i].end - list->list[i].start;
1168
1169 /* The "%X" conversion specifier expects an unsigned argument,
1170 so passing -1 (memrange_absolute) to it directly gives you
1171 "FFFFFFFF" (or more, depending on sizeof (unsigned)).
1172 Special-case it. */
1173 if (list->list[i].type == memrange_absolute)
1174 sprintf (end, "M-1,%s,%lX", tmp2, (long) length);
1175 else
1176 sprintf (end, "M%X,%s,%lX", list->list[i].type, tmp2, (long) length);
1177 }
1178
1179 count += strlen (end);
1180 end = temp_buf + count;
1181 }
1182
1183 for (i = 0; i < list->next_aexpr_elt; i++)
1184 {
1185 QUIT; /* allow user to bail out with ^C */
1186 if ((count + 10 + 2 * list->aexpr_list[i]->len) > MAX_AGENT_EXPR_LEN)
1187 {
1188 (*str_list)[ndx] = savestring (temp_buf, count);
1189 ndx++;
1190 count = 0;
1191 end = temp_buf;
1192 }
1193 sprintf (end, "X%08X,", list->aexpr_list[i]->len);
1194 end += 10; /* 'X' + 8 hex digits + ',' */
1195 count += 10;
1196
1197 end = mem2hex (list->aexpr_list[i]->buf,
1198 end, list->aexpr_list[i]->len);
1199 count += 2 * list->aexpr_list[i]->len;
1200 }
1201
1202 if (count != 0)
1203 {
1204 (*str_list)[ndx] = savestring (temp_buf, count);
1205 ndx++;
1206 count = 0;
1207 end = temp_buf;
1208 }
1209 (*str_list)[ndx] = NULL;
1210
1211 if (ndx == 0)
1212 {
1213 xfree (str_list);
1214 return NULL;
1215 }
1216 else
1217 return *str_list;
1218 }
1219
1220
1221 static void
1222 encode_actions_1 (struct command_line *action,
1223 struct breakpoint *t,
1224 struct bp_location *tloc,
1225 int frame_reg,
1226 LONGEST frame_offset,
1227 struct collection_list *collect,
1228 struct collection_list *stepping_list)
1229 {
1230 char *action_exp;
1231 struct expression *exp = NULL;
1232 struct command_line *actions;
1233 int i;
1234 struct value *tempval;
1235 struct cmd_list_element *cmd;
1236 struct agent_expr *aexpr;
1237
1238 for (; action; action = action->next)
1239 {
1240 QUIT; /* allow user to bail out with ^C */
1241 action_exp = action->line;
1242 while (isspace ((int) *action_exp))
1243 action_exp++;
1244
1245 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
1246 if (cmd == 0)
1247 error (_("Bad action list item: %s"), action_exp);
1248
1249 if (cmd_cfunc_eq (cmd, collect_pseudocommand))
1250 {
1251 do
1252 { /* repeat over a comma-separated list */
1253 QUIT; /* allow user to bail out with ^C */
1254 while (isspace ((int) *action_exp))
1255 action_exp++;
1256
1257 if (0 == strncasecmp ("$reg", action_exp, 4))
1258 {
1259 for (i = 0; i < gdbarch_num_regs (t->gdbarch); i++)
1260 add_register (collect, i);
1261 action_exp = strchr (action_exp, ','); /* more? */
1262 }
1263 else if (0 == strncasecmp ("$arg", action_exp, 4))
1264 {
1265 add_local_symbols (collect,
1266 t->gdbarch,
1267 tloc->address,
1268 frame_reg,
1269 frame_offset,
1270 'A');
1271 action_exp = strchr (action_exp, ','); /* more? */
1272 }
1273 else if (0 == strncasecmp ("$loc", action_exp, 4))
1274 {
1275 add_local_symbols (collect,
1276 t->gdbarch,
1277 tloc->address,
1278 frame_reg,
1279 frame_offset,
1280 'L');
1281 action_exp = strchr (action_exp, ','); /* more? */
1282 }
1283 else
1284 {
1285 unsigned long addr, len;
1286 struct cleanup *old_chain = NULL;
1287 struct cleanup *old_chain1 = NULL;
1288 struct agent_reqs areqs;
1289
1290 exp = parse_exp_1 (&action_exp,
1291 block_for_pc (tloc->address), 1);
1292 old_chain = make_cleanup (free_current_contents, &exp);
1293
1294 switch (exp->elts[0].opcode)
1295 {
1296 case OP_REGISTER:
1297 {
1298 const char *name = &exp->elts[2].string;
1299
1300 i = user_reg_map_name_to_regnum (t->gdbarch,
1301 name, strlen (name));
1302 if (i == -1)
1303 internal_error (__FILE__, __LINE__,
1304 _("Register $%s not available"),
1305 name);
1306 if (info_verbose)
1307 printf_filtered ("OP_REGISTER: ");
1308 add_register (collect, i);
1309 break;
1310 }
1311
1312 case UNOP_MEMVAL:
1313 /* safe because we know it's a simple expression */
1314 tempval = evaluate_expression (exp);
1315 addr = value_address (tempval);
1316 len = TYPE_LENGTH (check_typedef (exp->elts[1].type));
1317 add_memrange (collect, memrange_absolute, addr, len);
1318 break;
1319
1320 case OP_VAR_VALUE:
1321 collect_symbol (collect,
1322 exp->elts[2].symbol,
1323 t->gdbarch,
1324 frame_reg,
1325 frame_offset,
1326 tloc->address);
1327 break;
1328
1329 default: /* full-fledged expression */
1330 aexpr = gen_trace_for_expr (tloc->address, exp);
1331
1332 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1333
1334 ax_reqs (aexpr, &areqs);
1335
1336 report_agent_reqs_errors (aexpr, &areqs);
1337
1338 discard_cleanups (old_chain1);
1339 add_aexpr (collect, aexpr);
1340
1341 /* take care of the registers */
1342 if (areqs.reg_mask_len > 0)
1343 {
1344 int ndx1;
1345 int ndx2;
1346
1347 for (ndx1 = 0; ndx1 < areqs.reg_mask_len; ndx1++)
1348 {
1349 QUIT; /* allow user to bail out with ^C */
1350 if (areqs.reg_mask[ndx1] != 0)
1351 {
1352 /* assume chars have 8 bits */
1353 for (ndx2 = 0; ndx2 < 8; ndx2++)
1354 if (areqs.reg_mask[ndx1] & (1 << ndx2))
1355 /* it's used -- record it */
1356 add_register (collect,
1357 ndx1 * 8 + ndx2);
1358 }
1359 }
1360 }
1361 break;
1362 } /* switch */
1363 do_cleanups (old_chain);
1364 } /* do */
1365 }
1366 while (action_exp && *action_exp++ == ',');
1367 } /* if */
1368 else if (cmd_cfunc_eq (cmd, teval_pseudocommand))
1369 {
1370 do
1371 { /* repeat over a comma-separated list */
1372 QUIT; /* allow user to bail out with ^C */
1373 while (isspace ((int) *action_exp))
1374 action_exp++;
1375
1376 {
1377 unsigned long addr, len;
1378 struct cleanup *old_chain = NULL;
1379 struct cleanup *old_chain1 = NULL;
1380 struct agent_reqs areqs;
1381
1382 exp = parse_exp_1 (&action_exp,
1383 block_for_pc (tloc->address), 1);
1384 old_chain = make_cleanup (free_current_contents, &exp);
1385
1386 aexpr = gen_eval_for_expr (tloc->address, exp);
1387 old_chain1 = make_cleanup_free_agent_expr (aexpr);
1388
1389 ax_reqs (aexpr, &areqs);
1390
1391 report_agent_reqs_errors (aexpr, &areqs);
1392
1393 discard_cleanups (old_chain1);
1394 /* Even though we're not officially collecting, add
1395 to the collect list anyway. */
1396 add_aexpr (collect, aexpr);
1397
1398 do_cleanups (old_chain);
1399 } /* do */
1400 }
1401 while (action_exp && *action_exp++ == ',');
1402 } /* if */
1403 else if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
1404 {
1405 /* We check against nested while-stepping when setting
1406 breakpoint action, so no way to run into nested
1407 here. */
1408 gdb_assert (stepping_list);
1409
1410 encode_actions_1 (action->body_list[0], t, tloc, frame_reg, frame_offset,
1411 stepping_list, NULL);
1412 }
1413 else
1414 error (_("Invalid tracepoint command '%s'"), action->line);
1415 } /* for */
1416 }
1417
1418 /* Render all actions into gdb protocol. */
1419 /*static*/ void
1420 encode_actions (struct breakpoint *t, struct bp_location *tloc,
1421 char ***tdp_actions, char ***stepping_actions)
1422 {
1423 static char tdp_buff[2048], step_buff[2048];
1424 char *default_collect_line = NULL;
1425 struct command_line *actions;
1426 struct command_line *default_collect_action = NULL;
1427 int frame_reg;
1428 LONGEST frame_offset;
1429 struct cleanup *back_to;
1430
1431 back_to = make_cleanup (null_cleanup, NULL);
1432
1433 clear_collection_list (&tracepoint_list);
1434 clear_collection_list (&stepping_list);
1435
1436 *tdp_actions = NULL;
1437 *stepping_actions = NULL;
1438
1439 gdbarch_virtual_frame_pointer (t->gdbarch,
1440 t->loc->address, &frame_reg, &frame_offset);
1441
1442 actions = t->commands->commands;
1443
1444 /* If there are default expressions to collect, make up a collect
1445 action and prepend to the action list to encode. Note that since
1446 validation is per-tracepoint (local var "xyz" might be valid for
1447 one tracepoint and not another, etc), we make up the action on
1448 the fly, and don't cache it. */
1449 if (*default_collect)
1450 {
1451 char *line;
1452
1453 default_collect_line = xstrprintf ("collect %s", default_collect);
1454 make_cleanup (xfree, default_collect_line);
1455
1456 line = default_collect_line;
1457 validate_actionline (&line, t);
1458
1459 default_collect_action = xmalloc (sizeof (struct command_line));
1460 make_cleanup (xfree, default_collect_action);
1461 default_collect_action->next = t->commands->commands;
1462 default_collect_action->line = line;
1463 actions = default_collect_action;
1464 }
1465 encode_actions_1 (actions, t, tloc, frame_reg, frame_offset,
1466 &tracepoint_list, &stepping_list);
1467
1468 memrange_sortmerge (&tracepoint_list);
1469 memrange_sortmerge (&stepping_list);
1470
1471 *tdp_actions = stringify_collection_list (&tracepoint_list,
1472 tdp_buff);
1473 *stepping_actions = stringify_collection_list (&stepping_list,
1474 step_buff);
1475
1476 do_cleanups (back_to);
1477 }
1478
1479 static void
1480 add_aexpr (struct collection_list *collect, struct agent_expr *aexpr)
1481 {
1482 if (collect->next_aexpr_elt >= collect->aexpr_listsize)
1483 {
1484 collect->aexpr_list =
1485 xrealloc (collect->aexpr_list,
1486 2 * collect->aexpr_listsize * sizeof (struct agent_expr *));
1487 collect->aexpr_listsize *= 2;
1488 }
1489 collect->aexpr_list[collect->next_aexpr_elt] = aexpr;
1490 collect->next_aexpr_elt++;
1491 }
1492
1493
1494 void
1495 start_tracing (void)
1496 {
1497 char buf[2048];
1498 VEC(breakpoint_p) *tp_vec = NULL;
1499 int ix;
1500 struct breakpoint *t;
1501 struct trace_state_variable *tsv;
1502 int any_enabled = 0;
1503
1504 tp_vec = all_tracepoints ();
1505
1506 /* No point in tracing without any tracepoints... */
1507 if (VEC_length (breakpoint_p, tp_vec) == 0)
1508 {
1509 VEC_free (breakpoint_p, tp_vec);
1510 error (_("No tracepoints defined, not starting trace"));
1511 }
1512
1513 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1514 {
1515 if (t->enable_state == bp_enabled)
1516 {
1517 any_enabled = 1;
1518 break;
1519 }
1520 }
1521
1522 /* No point in tracing with only disabled tracepoints. */
1523 if (!any_enabled)
1524 {
1525 VEC_free (breakpoint_p, tp_vec);
1526 error (_("No tracepoints enabled, not starting trace"));
1527 }
1528
1529 target_trace_init ();
1530
1531 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
1532 {
1533 t->number_on_target = 0;
1534 target_download_tracepoint (t);
1535 t->number_on_target = t->number;
1536 }
1537 VEC_free (breakpoint_p, tp_vec);
1538
1539 /* Send down all the trace state variables too. */
1540 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
1541 {
1542 target_download_trace_state_variable (tsv);
1543 }
1544
1545 /* Tell target to treat text-like sections as transparent. */
1546 target_trace_set_readonly_regions ();
1547 /* Set some mode flags. */
1548 target_set_disconnected_tracing (disconnected_tracing);
1549 target_set_circular_trace_buffer (circular_trace_buffer);
1550
1551 /* Now insert traps and begin collecting data. */
1552 target_trace_start ();
1553
1554 /* Reset our local state. */
1555 set_traceframe_num (-1);
1556 set_tracepoint_num (-1);
1557 set_traceframe_context (NULL);
1558 current_trace_status()->running = 1;
1559 }
1560
1561 /* tstart command:
1562
1563 Tell target to clear any previous trace experiment.
1564 Walk the list of tracepoints, and send them (and their actions)
1565 to the target. If no errors,
1566 Tell target to start a new trace experiment. */
1567
1568 static void
1569 trace_start_command (char *args, int from_tty)
1570 {
1571 dont_repeat (); /* Like "run", dangerous to repeat accidentally. */
1572
1573 if (current_trace_status ()->running)
1574 {
1575 if (from_tty
1576 && !query (_("A trace is running already. Start a new run? ")))
1577 error (_("New trace run not started."));
1578 }
1579
1580 start_tracing ();
1581 }
1582
1583 /* tstop command */
1584 static void
1585 trace_stop_command (char *args, int from_tty)
1586 {
1587 if (!current_trace_status ()->running)
1588 error (_("Trace is not running."));
1589
1590 stop_tracing ();
1591 }
1592
1593 void
1594 stop_tracing (void)
1595 {
1596 target_trace_stop ();
1597 /* should change in response to reply? */
1598 current_trace_status ()->running = 0;
1599 }
1600
1601 /* tstatus command */
1602 static void
1603 trace_status_command (char *args, int from_tty)
1604 {
1605 struct trace_status *ts = current_trace_status ();
1606 int status;
1607
1608 status = target_get_trace_status (ts);
1609
1610 if (status == -1)
1611 {
1612 if (ts->from_file)
1613 printf_filtered (_("Using a trace file.\n"));
1614 else
1615 {
1616 printf_filtered (_("Trace can not be run on this target.\n"));
1617 return;
1618 }
1619 }
1620
1621 if (!ts->running_known)
1622 {
1623 printf_filtered (_("Run/stop status is unknown.\n"));
1624 }
1625 else if (ts->running)
1626 {
1627 printf_filtered (_("Trace is running on the target.\n"));
1628 if (disconnected_tracing)
1629 printf_filtered (_("Trace will continue if GDB disconnects.\n"));
1630 else
1631 printf_filtered (_("Trace will stop if GDB disconnects.\n"));
1632 }
1633 else
1634 {
1635 switch (ts->stop_reason)
1636 {
1637 case trace_never_run:
1638 printf_filtered (_("No trace has been run on the target.\n"));
1639 break;
1640 case tstop_command:
1641 printf_filtered (_("Trace stopped by a tstop command.\n"));
1642 break;
1643 case trace_buffer_full:
1644 printf_filtered (_("Trace stopped because the buffer was full.\n"));
1645 break;
1646 case trace_disconnected:
1647 printf_filtered (_("Trace stopped because of disconnection.\n"));
1648 break;
1649 case tracepoint_passcount:
1650 printf_filtered (_("Trace stopped by tracepoint %d.\n"),
1651 ts->stopping_tracepoint);
1652 break;
1653 case tracepoint_error:
1654 if (ts->stopping_tracepoint)
1655 printf_filtered (_("Trace stopped by an error (%s, tracepoint %d).\n"),
1656 ts->error_desc, ts->stopping_tracepoint);
1657 else
1658 printf_filtered (_("Trace stopped by an error (%s).\n"),
1659 ts->error_desc);
1660 break;
1661 case trace_stop_reason_unknown:
1662 printf_filtered (_("Trace stopped for an unknown reason.\n"));
1663 break;
1664 default:
1665 printf_filtered (_("Trace stopped for some other reason (%d).\n"),
1666 ts->stop_reason);
1667 break;
1668 }
1669 }
1670
1671 if (ts->traceframes_created >= 0
1672 && ts->traceframe_count != ts->traceframes_created)
1673 {
1674 printf_filtered (_("Buffer contains %d trace frames (of %d created total).\n"),
1675 ts->traceframe_count, ts->traceframes_created);
1676 }
1677 else if (ts->traceframe_count >= 0)
1678 {
1679 printf_filtered (_("Collected %d trace frames.\n"),
1680 ts->traceframe_count);
1681 }
1682
1683 if (ts->buffer_free >= 0)
1684 {
1685 if (ts->buffer_size >= 0)
1686 {
1687 printf_filtered (_("Trace buffer has %d bytes of %d bytes free"),
1688 ts->buffer_free, ts->buffer_size);
1689 if (ts->buffer_size > 0)
1690 printf_filtered (_(" (%d%% full)"),
1691 ((int) ((((long long) (ts->buffer_size
1692 - ts->buffer_free)) * 100)
1693 / ts->buffer_size)));
1694 printf_filtered (_(".\n"));
1695 }
1696 else
1697 printf_filtered (_("Trace buffer has %d bytes free.\n"),
1698 ts->buffer_free);
1699 }
1700
1701 /* Now report on what we're doing with tfind. */
1702 if (traceframe_number >= 0)
1703 printf_filtered (_("Looking at trace frame %d, tracepoint %d.\n"),
1704 traceframe_number, tracepoint_number);
1705 else
1706 printf_filtered (_("Not looking at any trace frame.\n"));
1707 }
1708
1709 /* Report the trace status to uiout, in a way suitable for MI, and not
1710 suitable for CLI. If ON_STOP is true, suppress a few fields that
1711 are not meaningful in the -trace-stop response.
1712
1713 The implementation is essentially parallel to trace_status_command, but
1714 merging them will result in unreadable code. */
1715 void
1716 trace_status_mi (int on_stop)
1717 {
1718 struct trace_status *ts = current_trace_status ();
1719 int status;
1720 char *string_status;
1721
1722 status = target_get_trace_status (ts);
1723
1724 if (status == -1 && !ts->from_file)
1725 {
1726 ui_out_field_string (uiout, "supported", "0");
1727 return;
1728 }
1729
1730 if (ts->from_file)
1731 ui_out_field_string (uiout, "supported", "file");
1732 else if (!on_stop)
1733 ui_out_field_string (uiout, "supported", "1");
1734
1735 gdb_assert (ts->running_known);
1736
1737 if (ts->running)
1738 {
1739 ui_out_field_string (uiout, "running", "1");
1740
1741 /* Unlike CLI, do not show the state of 'disconnected-tracing' variable.
1742 Given that the frontend gets the status either on -trace-stop, or from
1743 -trace-status after re-connection, it does not seem like this
1744 information is necessary for anything. It is not necessary for either
1745 figuring the vital state of the target nor for navigation of trace
1746 frames. If the frontend wants to show the current state is some
1747 configure dialog, it can request the value when such dialog is
1748 invoked by the user. */
1749 }
1750 else
1751 {
1752 char *stop_reason = NULL;
1753 int stopping_tracepoint = -1;
1754
1755 if (!on_stop)
1756 ui_out_field_string (uiout, "running", "0");
1757
1758 if (ts->stop_reason != trace_stop_reason_unknown)
1759 {
1760 switch (ts->stop_reason)
1761 {
1762 case tstop_command:
1763 stop_reason = "request";
1764 break;
1765 case trace_buffer_full:
1766 stop_reason = "overflow";
1767 break;
1768 case trace_disconnected:
1769 stop_reason = "disconnection";
1770 break;
1771 case tracepoint_passcount:
1772 stop_reason = "passcount";
1773 stopping_tracepoint = ts->stopping_tracepoint;
1774 break;
1775 case tracepoint_error:
1776 stop_reason = "error";
1777 stopping_tracepoint = ts->stopping_tracepoint;
1778 break;
1779 }
1780
1781 if (stop_reason)
1782 {
1783 ui_out_field_string (uiout, "stop-reason", stop_reason);
1784 if (stopping_tracepoint != -1)
1785 ui_out_field_int (uiout, "stopping-tracepoint",
1786 stopping_tracepoint);
1787 if (ts->stop_reason == tracepoint_error)
1788 ui_out_field_string (uiout, "error-description",
1789 ts->error_desc);
1790 }
1791 }
1792 }
1793
1794
1795 if ((int) ts->traceframe_count != -1)
1796 ui_out_field_int (uiout, "frames", ts->traceframe_count);
1797 if ((int) ts->buffer_size != -1)
1798 ui_out_field_int (uiout, "buffer-size", (int) ts->buffer_size);
1799 if ((int) ts->buffer_free != -1)
1800 ui_out_field_int (uiout, "buffer-free", (int) ts->buffer_free);
1801 }
1802
1803
1804 void
1805 disconnect_or_stop_tracing (int from_tty)
1806 {
1807 /* It can happen that the target that was tracing went away on its
1808 own, and we didn't notice. Get a status update, and if the
1809 current target doesn't even do tracing, then assume it's not
1810 running anymore. */
1811 if (target_get_trace_status (current_trace_status ()) < 0)
1812 current_trace_status ()->running = 0;
1813
1814 if (current_trace_status ()->running && from_tty)
1815 {
1816 int cont = query (_("Trace is running. Continue tracing after detach? "));
1817 /* Note that we send the query result without affecting the
1818 user's setting of disconnected_tracing, so that the answer is
1819 a one-time-only. */
1820 send_disconnected_tracing_value (cont);
1821
1822 /* Also ensure that we do the equivalent of a tstop command if
1823 tracing is not to continue after the detach. */
1824 if (!cont)
1825 stop_tracing ();
1826 }
1827
1828 /* Also we want to be out of tfind mode, otherwise things can get
1829 confusing upon reconnection. Just use these calls instead of
1830 full tfind_1 behavior because we're in the middle of detaching,
1831 and there's no point to updating current stack frame etc. */
1832 set_traceframe_number (-1);
1833 set_traceframe_context (NULL);
1834 }
1835
1836 /* Worker function for the various flavors of the tfind command. */
1837 void
1838 tfind_1 (enum trace_find_type type, int num,
1839 ULONGEST addr1, ULONGEST addr2,
1840 int from_tty)
1841 {
1842 int target_frameno = -1, target_tracept = -1;
1843 struct frame_id old_frame_id;
1844 char *reply;
1845 struct breakpoint *tp;
1846
1847 old_frame_id = get_frame_id (get_current_frame ());
1848
1849 target_frameno = target_trace_find (type, num, addr1, addr2,
1850 &target_tracept);
1851
1852 if (type == tfind_number
1853 && num == -1
1854 && target_frameno == -1)
1855 {
1856 /* We told the target to get out of tfind mode, and it did. */
1857 }
1858 else if (target_frameno == -1)
1859 {
1860 /* A request for a non-existant trace frame has failed.
1861 Our response will be different, depending on FROM_TTY:
1862
1863 If FROM_TTY is true, meaning that this command was
1864 typed interactively by the user, then give an error
1865 and DO NOT change the state of traceframe_number etc.
1866
1867 However if FROM_TTY is false, meaning that we're either
1868 in a script, a loop, or a user-defined command, then
1869 DON'T give an error, but DO change the state of
1870 traceframe_number etc. to invalid.
1871
1872 The rationalle is that if you typed the command, you
1873 might just have committed a typo or something, and you'd
1874 like to NOT lose your current debugging state. However
1875 if you're in a user-defined command or especially in a
1876 loop, then you need a way to detect that the command
1877 failed WITHOUT aborting. This allows you to write
1878 scripts that search thru the trace buffer until the end,
1879 and then continue on to do something else. */
1880
1881 if (from_tty)
1882 error (_("Target failed to find requested trace frame."));
1883 else
1884 {
1885 if (info_verbose)
1886 printf_filtered ("End of trace buffer.\n");
1887 #if 0 /* dubious now? */
1888 /* The following will not recurse, since it's
1889 special-cased. */
1890 trace_find_command ("-1", from_tty);
1891 #endif
1892 }
1893 }
1894
1895 tp = get_tracepoint_by_number_on_target (target_tracept);
1896
1897 reinit_frame_cache ();
1898 registers_changed ();
1899 target_dcache_invalidate ();
1900 set_traceframe_num (target_frameno);
1901 set_tracepoint_num (tp ? tp->number : target_tracept);
1902 if (target_frameno == -1)
1903 set_traceframe_context (NULL);
1904 else
1905 set_traceframe_context (get_current_frame ());
1906
1907 if (traceframe_number >= 0)
1908 {
1909 /* Use different branches for MI and CLI to make CLI messages
1910 i18n-eable. */
1911 if (ui_out_is_mi_like_p (uiout))
1912 {
1913 ui_out_field_string (uiout, "found", "1");
1914 ui_out_field_int (uiout, "tracepoint", tracepoint_number);
1915 ui_out_field_int (uiout, "traceframe", traceframe_number);
1916 }
1917 else
1918 {
1919 printf_unfiltered (_("Found trace frame %d, tracepoint %d\n"),
1920 traceframe_number, tracepoint_number);
1921 }
1922 }
1923 else
1924 {
1925 if (ui_out_is_mi_like_p (uiout))
1926 ui_out_field_string (uiout, "found", "0");
1927 else
1928 printf_unfiltered (_("No trace frame found"));
1929 }
1930
1931 /* If we're in nonstop mode and getting out of looking at trace
1932 frames, there won't be any current frame to go back to and
1933 display. */
1934 if (from_tty
1935 && (has_stack_frames () || traceframe_number >= 0))
1936 {
1937 enum print_what print_what;
1938
1939 /* NOTE: in immitation of the step command, try to determine
1940 whether we have made a transition from one function to
1941 another. If so, we'll print the "stack frame" (ie. the new
1942 function and it's arguments) -- otherwise we'll just show the
1943 new source line. */
1944
1945 if (frame_id_eq (old_frame_id,
1946 get_frame_id (get_current_frame ())))
1947 print_what = SRC_LINE;
1948 else
1949 print_what = SRC_AND_LOC;
1950
1951 print_stack_frame (get_selected_frame (NULL), 1, print_what);
1952 do_displays ();
1953 }
1954 }
1955
1956 /* trace_find_command takes a trace frame number n,
1957 sends "QTFrame:<n>" to the target,
1958 and accepts a reply that may contain several optional pieces
1959 of information: a frame number, a tracepoint number, and an
1960 indication of whether this is a trap frame or a stepping frame.
1961
1962 The minimal response is just "OK" (which indicates that the
1963 target does not give us a frame number or a tracepoint number).
1964 Instead of that, the target may send us a string containing
1965 any combination of:
1966 F<hexnum> (gives the selected frame number)
1967 T<hexnum> (gives the selected tracepoint number)
1968 */
1969
1970 /* tfind command */
1971 static void
1972 trace_find_command (char *args, int from_tty)
1973 { /* this should only be called with a numeric argument */
1974 int frameno = -1;
1975
1976 if (current_trace_status ()->running && !current_trace_status ()->from_file)
1977 error ("May not look at trace frames while trace is running.");
1978
1979 if (args == 0 || *args == 0)
1980 { /* TFIND with no args means find NEXT trace frame. */
1981 if (traceframe_number == -1)
1982 frameno = 0; /* "next" is first one */
1983 else
1984 frameno = traceframe_number + 1;
1985 }
1986 else if (0 == strcmp (args, "-"))
1987 {
1988 if (traceframe_number == -1)
1989 error (_("not debugging trace buffer"));
1990 else if (from_tty && traceframe_number == 0)
1991 error (_("already at start of trace buffer"));
1992
1993 frameno = traceframe_number - 1;
1994 }
1995 /* A hack to work around eval's need for fp to have been collected. */
1996 else if (0 == strcmp (args, "-1"))
1997 frameno = -1;
1998 else
1999 frameno = parse_and_eval_long (args);
2000
2001 if (frameno < -1)
2002 error (_("invalid input (%d is less than zero)"), frameno);
2003
2004 tfind_1 (tfind_number, frameno, 0, 0, from_tty);
2005 }
2006
2007 /* tfind end */
2008 static void
2009 trace_find_end_command (char *args, int from_tty)
2010 {
2011 trace_find_command ("-1", from_tty);
2012 }
2013
2014 /* tfind none */
2015 static void
2016 trace_find_none_command (char *args, int from_tty)
2017 {
2018 trace_find_command ("-1", from_tty);
2019 }
2020
2021 /* tfind start */
2022 static void
2023 trace_find_start_command (char *args, int from_tty)
2024 {
2025 trace_find_command ("0", from_tty);
2026 }
2027
2028 /* tfind pc command */
2029 static void
2030 trace_find_pc_command (char *args, int from_tty)
2031 {
2032 CORE_ADDR pc;
2033 char tmp[40];
2034
2035 if (current_trace_status ()->running && !current_trace_status ()->from_file)
2036 error ("May not look at trace frames while trace is running.");
2037
2038 if (args == 0 || *args == 0)
2039 pc = regcache_read_pc (get_current_regcache ());
2040 else
2041 pc = parse_and_eval_address (args);
2042
2043 tfind_1 (tfind_pc, 0, pc, 0, from_tty);
2044 }
2045
2046 /* tfind tracepoint command */
2047 static void
2048 trace_find_tracepoint_command (char *args, int from_tty)
2049 {
2050 int tdp;
2051 struct breakpoint *tp;
2052
2053 if (current_trace_status ()->running && !current_trace_status ()->from_file)
2054 error ("May not look at trace frames while trace is running.");
2055
2056 if (args == 0 || *args == 0)
2057 {
2058 if (tracepoint_number == -1)
2059 error (_("No current tracepoint -- please supply an argument."));
2060 else
2061 tdp = tracepoint_number; /* default is current TDP */
2062 }
2063 else
2064 tdp = parse_and_eval_long (args);
2065
2066 /* If we have the tracepoint on hand, use the number that the
2067 target knows about (which may be different if we disconnected
2068 and reconnected). */
2069 tp = get_tracepoint (tdp);
2070 if (tp)
2071 tdp = tp->number_on_target;
2072
2073 tfind_1 (tfind_tp, tdp, 0, 0, from_tty);
2074 }
2075
2076 /* TFIND LINE command:
2077
2078 This command will take a sourceline for argument, just like BREAK
2079 or TRACE (ie. anything that "decode_line_1" can handle).
2080
2081 With no argument, this command will find the next trace frame
2082 corresponding to a source line OTHER THAN THE CURRENT ONE. */
2083
2084 static void
2085 trace_find_line_command (char *args, int from_tty)
2086 {
2087 static CORE_ADDR start_pc, end_pc;
2088 struct symtabs_and_lines sals;
2089 struct symtab_and_line sal;
2090 struct cleanup *old_chain;
2091 char startpc_str[40], endpc_str[40];
2092
2093 if (current_trace_status ()->running && !current_trace_status ()->from_file)
2094 error ("May not look at trace frames while trace is running.");
2095
2096 if (args == 0 || *args == 0)
2097 {
2098 sal = find_pc_line (get_frame_pc (get_current_frame ()), 0);
2099 sals.nelts = 1;
2100 sals.sals = (struct symtab_and_line *)
2101 xmalloc (sizeof (struct symtab_and_line));
2102 sals.sals[0] = sal;
2103 }
2104 else
2105 {
2106 sals = decode_line_spec (args, 1);
2107 sal = sals.sals[0];
2108 }
2109
2110 old_chain = make_cleanup (xfree, sals.sals);
2111 if (sal.symtab == 0)
2112 error (_("No line number information available."));
2113
2114 if (sal.line > 0 && find_line_pc_range (sal, &start_pc, &end_pc))
2115 {
2116 if (start_pc == end_pc)
2117 {
2118 printf_filtered ("Line %d of \"%s\"",
2119 sal.line, sal.symtab->filename);
2120 wrap_here (" ");
2121 printf_filtered (" is at address ");
2122 print_address (get_current_arch (), start_pc, gdb_stdout);
2123 wrap_here (" ");
2124 printf_filtered (" but contains no code.\n");
2125 sal = find_pc_line (start_pc, 0);
2126 if (sal.line > 0
2127 && find_line_pc_range (sal, &start_pc, &end_pc)
2128 && start_pc != end_pc)
2129 printf_filtered ("Attempting to find line %d instead.\n",
2130 sal.line);
2131 else
2132 error (_("Cannot find a good line."));
2133 }
2134 }
2135 else
2136 /* Is there any case in which we get here, and have an address
2137 which the user would want to see? If we have debugging
2138 symbols and no line numbers? */
2139 error (_("Line number %d is out of range for \"%s\"."),
2140 sal.line, sal.symtab->filename);
2141
2142 /* Find within range of stated line. */
2143 if (args && *args)
2144 tfind_1 (tfind_range, 0, start_pc, end_pc - 1, from_tty);
2145 else
2146 tfind_1 (tfind_outside, 0, start_pc, end_pc - 1, from_tty);
2147 do_cleanups (old_chain);
2148 }
2149
2150 /* tfind range command */
2151 static void
2152 trace_find_range_command (char *args, int from_tty)
2153 {
2154 static CORE_ADDR start, stop;
2155 char start_str[40], stop_str[40];
2156 char *tmp;
2157
2158 if (current_trace_status ()->running && !current_trace_status ()->from_file)
2159 error ("May not look at trace frames while trace is running.");
2160
2161 if (args == 0 || *args == 0)
2162 { /* XXX FIXME: what should default behavior be? */
2163 printf_filtered ("Usage: tfind range <startaddr>,<endaddr>\n");
2164 return;
2165 }
2166
2167 if (0 != (tmp = strchr (args, ',')))
2168 {
2169 *tmp++ = '\0'; /* terminate start address */
2170 while (isspace ((int) *tmp))
2171 tmp++;
2172 start = parse_and_eval_address (args);
2173 stop = parse_and_eval_address (tmp);
2174 }
2175 else
2176 { /* no explicit end address? */
2177 start = parse_and_eval_address (args);
2178 stop = start + 1; /* ??? */
2179 }
2180
2181 tfind_1 (tfind_range, 0, start, stop, from_tty);
2182 }
2183
2184 /* tfind outside command */
2185 static void
2186 trace_find_outside_command (char *args, int from_tty)
2187 {
2188 CORE_ADDR start, stop;
2189 char start_str[40], stop_str[40];
2190 char *tmp;
2191
2192 if (current_trace_status ()->running && !current_trace_status ()->from_file)
2193 error ("May not look at trace frames while trace is running.");
2194
2195 if (args == 0 || *args == 0)
2196 { /* XXX FIXME: what should default behavior be? */
2197 printf_filtered ("Usage: tfind outside <startaddr>,<endaddr>\n");
2198 return;
2199 }
2200
2201 if (0 != (tmp = strchr (args, ',')))
2202 {
2203 *tmp++ = '\0'; /* terminate start address */
2204 while (isspace ((int) *tmp))
2205 tmp++;
2206 start = parse_and_eval_address (args);
2207 stop = parse_and_eval_address (tmp);
2208 }
2209 else
2210 { /* no explicit end address? */
2211 start = parse_and_eval_address (args);
2212 stop = start + 1; /* ??? */
2213 }
2214
2215 tfind_1 (tfind_outside, 0, start, stop, from_tty);
2216 }
2217
2218 /* info scope command: list the locals for a scope. */
2219 static void
2220 scope_info (char *args, int from_tty)
2221 {
2222 struct symtabs_and_lines sals;
2223 struct symbol *sym;
2224 struct minimal_symbol *msym;
2225 struct block *block;
2226 char **canonical, *symname, *save_args = args;
2227 struct dict_iterator iter;
2228 int j, count = 0;
2229 struct gdbarch *gdbarch;
2230 int regno;
2231
2232 if (args == 0 || *args == 0)
2233 error (_("requires an argument (function, line or *addr) to define a scope"));
2234
2235 sals = decode_line_1 (&args, 1, NULL, 0, &canonical, NULL);
2236 if (sals.nelts == 0)
2237 return; /* presumably decode_line_1 has already warned */
2238
2239 /* Resolve line numbers to PC */
2240 resolve_sal_pc (&sals.sals[0]);
2241 block = block_for_pc (sals.sals[0].pc);
2242
2243 while (block != 0)
2244 {
2245 QUIT; /* allow user to bail out with ^C */
2246 ALL_BLOCK_SYMBOLS (block, iter, sym)
2247 {
2248 QUIT; /* allow user to bail out with ^C */
2249 if (count == 0)
2250 printf_filtered ("Scope for %s:\n", save_args);
2251 count++;
2252
2253 symname = SYMBOL_PRINT_NAME (sym);
2254 if (symname == NULL || *symname == '\0')
2255 continue; /* probably botched, certainly useless */
2256
2257 gdbarch = get_objfile_arch (SYMBOL_SYMTAB (sym)->objfile);
2258
2259 printf_filtered ("Symbol %s is ", symname);
2260 switch (SYMBOL_CLASS (sym))
2261 {
2262 default:
2263 case LOC_UNDEF: /* messed up symbol? */
2264 printf_filtered ("a bogus symbol, class %d.\n",
2265 SYMBOL_CLASS (sym));
2266 count--; /* don't count this one */
2267 continue;
2268 case LOC_CONST:
2269 printf_filtered ("a constant with value %ld (0x%lx)",
2270 SYMBOL_VALUE (sym), SYMBOL_VALUE (sym));
2271 break;
2272 case LOC_CONST_BYTES:
2273 printf_filtered ("constant bytes: ");
2274 if (SYMBOL_TYPE (sym))
2275 for (j = 0; j < TYPE_LENGTH (SYMBOL_TYPE (sym)); j++)
2276 fprintf_filtered (gdb_stdout, " %02x",
2277 (unsigned) SYMBOL_VALUE_BYTES (sym)[j]);
2278 break;
2279 case LOC_STATIC:
2280 printf_filtered ("in static storage at address ");
2281 printf_filtered ("%s", paddress (gdbarch,
2282 SYMBOL_VALUE_ADDRESS (sym)));
2283 break;
2284 case LOC_REGISTER:
2285 /* GDBARCH is the architecture associated with the objfile
2286 the symbol is defined in; the target architecture may be
2287 different, and may provide additional registers. However,
2288 we do not know the target architecture at this point.
2289 We assume the objfile architecture will contain all the
2290 standard registers that occur in debug info in that
2291 objfile. */
2292 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
2293
2294 if (SYMBOL_IS_ARGUMENT (sym))
2295 printf_filtered ("an argument in register $%s",
2296 gdbarch_register_name (gdbarch, regno));
2297 else
2298 printf_filtered ("a local variable in register $%s",
2299 gdbarch_register_name (gdbarch, regno));
2300 break;
2301 case LOC_ARG:
2302 printf_filtered ("an argument at stack/frame offset %ld",
2303 SYMBOL_VALUE (sym));
2304 break;
2305 case LOC_LOCAL:
2306 printf_filtered ("a local variable at frame offset %ld",
2307 SYMBOL_VALUE (sym));
2308 break;
2309 case LOC_REF_ARG:
2310 printf_filtered ("a reference argument at offset %ld",
2311 SYMBOL_VALUE (sym));
2312 break;
2313 case LOC_REGPARM_ADDR:
2314 /* Note comment at LOC_REGISTER. */
2315 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
2316 printf_filtered ("the address of an argument, in register $%s",
2317 gdbarch_register_name (gdbarch, regno));
2318 break;
2319 case LOC_TYPEDEF:
2320 printf_filtered ("a typedef.\n");
2321 continue;
2322 case LOC_LABEL:
2323 printf_filtered ("a label at address ");
2324 printf_filtered ("%s", paddress (gdbarch,
2325 SYMBOL_VALUE_ADDRESS (sym)));
2326 break;
2327 case LOC_BLOCK:
2328 printf_filtered ("a function at address ");
2329 printf_filtered ("%s",
2330 paddress (gdbarch, BLOCK_START (SYMBOL_BLOCK_VALUE (sym))));
2331 break;
2332 case LOC_UNRESOLVED:
2333 msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (sym),
2334 NULL, NULL);
2335 if (msym == NULL)
2336 printf_filtered ("Unresolved Static");
2337 else
2338 {
2339 printf_filtered ("static storage at address ");
2340 printf_filtered ("%s",
2341 paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msym)));
2342 }
2343 break;
2344 case LOC_OPTIMIZED_OUT:
2345 printf_filtered ("optimized out.\n");
2346 continue;
2347 case LOC_COMPUTED:
2348 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, gdb_stdout);
2349 break;
2350 }
2351 if (SYMBOL_TYPE (sym))
2352 printf_filtered (", length %d.\n",
2353 TYPE_LENGTH (check_typedef (SYMBOL_TYPE (sym))));
2354 }
2355 if (BLOCK_FUNCTION (block))
2356 break;
2357 else
2358 block = BLOCK_SUPERBLOCK (block);
2359 }
2360 if (count <= 0)
2361 printf_filtered ("Scope for %s contains no locals or arguments.\n",
2362 save_args);
2363 }
2364
2365 /* worker function (cleanup) */
2366 static void
2367 replace_comma (void *data)
2368 {
2369 char *comma = data;
2370 *comma = ',';
2371 }
2372
2373 /* tdump command */
2374 static void
2375 trace_dump_command (char *args, int from_tty)
2376 {
2377 struct regcache *regcache;
2378 struct gdbarch *gdbarch;
2379 struct breakpoint *t;
2380 struct command_line *action;
2381 char *action_exp, *next_comma;
2382 struct cleanup *old_cleanups;
2383 int stepping_actions = 0;
2384 int stepping_frame = 0;
2385 struct bp_location *loc;
2386
2387 if (tracepoint_number == -1)
2388 {
2389 warning (_("No current trace frame."));
2390 return;
2391 }
2392
2393 t = get_tracepoint (tracepoint_number);
2394
2395 if (t == NULL)
2396 error (_("No known tracepoint matches 'current' tracepoint #%d."),
2397 tracepoint_number);
2398
2399 old_cleanups = make_cleanup (null_cleanup, NULL);
2400
2401 printf_filtered ("Data collected at tracepoint %d, trace frame %d:\n",
2402 tracepoint_number, traceframe_number);
2403
2404 /* The current frame is a trap frame if the frame PC is equal
2405 to the tracepoint PC. If not, then the current frame was
2406 collected during single-stepping. */
2407
2408 regcache = get_current_regcache ();
2409 gdbarch = get_regcache_arch (regcache);
2410
2411 /* If the traceframe's address matches any of the tracepoint's
2412 locations, assume it is a direct hit rather than a while-stepping
2413 frame. (FIXME this is not reliable, should record each frame's
2414 type.) */
2415 stepping_frame = 1;
2416 for (loc = t->loc; loc; loc = loc->next)
2417 if (loc->address == regcache_read_pc (regcache))
2418 stepping_frame = 0;
2419
2420 for (action = t->commands->commands; action; action = action->next)
2421 {
2422 struct cmd_list_element *cmd;
2423
2424 QUIT; /* allow user to bail out with ^C */
2425 action_exp = action->line;
2426 while (isspace ((int) *action_exp))
2427 action_exp++;
2428
2429 /* The collection actions to be done while stepping are
2430 bracketed by the commands "while-stepping" and "end". */
2431
2432 if (*action_exp == '#') /* comment line */
2433 continue;
2434
2435 cmd = lookup_cmd (&action_exp, cmdlist, "", -1, 1);
2436 if (cmd == 0)
2437 error (_("Bad action list item: %s"), action_exp);
2438
2439 if (cmd_cfunc_eq (cmd, while_stepping_pseudocommand))
2440 stepping_actions = 1;
2441 else if (cmd_cfunc_eq (cmd, end_actions_pseudocommand))
2442 stepping_actions = 0;
2443 else if (cmd_cfunc_eq (cmd, collect_pseudocommand))
2444 {
2445 /* Display the collected data.
2446 For the trap frame, display only what was collected at
2447 the trap. Likewise for stepping frames, display only
2448 what was collected while stepping. This means that the
2449 two boolean variables, STEPPING_FRAME and
2450 STEPPING_ACTIONS should be equal. */
2451 if (stepping_frame == stepping_actions)
2452 {
2453 do
2454 { /* repeat over a comma-separated list */
2455 QUIT; /* allow user to bail out with ^C */
2456 if (*action_exp == ',')
2457 action_exp++;
2458 while (isspace ((int) *action_exp))
2459 action_exp++;
2460
2461 next_comma = strchr (action_exp, ',');
2462
2463 if (0 == strncasecmp (action_exp, "$reg", 4))
2464 registers_info (NULL, from_tty);
2465 else if (0 == strncasecmp (action_exp, "$loc", 4))
2466 locals_info (NULL, from_tty);
2467 else if (0 == strncasecmp (action_exp, "$arg", 4))
2468 args_info (NULL, from_tty);
2469 else
2470 { /* variable */
2471 if (next_comma)
2472 {
2473 make_cleanup (replace_comma, next_comma);
2474 *next_comma = '\0';
2475 }
2476 printf_filtered ("%s = ", action_exp);
2477 output_command (action_exp, from_tty);
2478 printf_filtered ("\n");
2479 }
2480 if (next_comma)
2481 *next_comma = ',';
2482 action_exp = next_comma;
2483 }
2484 while (action_exp && *action_exp == ',');
2485 }
2486 }
2487 }
2488 discard_cleanups (old_cleanups);
2489 }
2490
2491 /* Encode a piece of a tracepoint's source-level definition in a form
2492 that is suitable for both protocol and saving in files. */
2493 /* This version does not do multiple encodes for long strings; it should
2494 return an offset to the next piece to encode. FIXME */
2495
2496 extern int
2497 encode_source_string (int tpnum, ULONGEST addr,
2498 char *srctype, char *src, char *buf, int buf_size)
2499 {
2500 if (80 + strlen (srctype) > buf_size)
2501 error (_("Buffer too small for source encoding"));
2502 sprintf (buf, "%x:%s:%s:%x:%x:",
2503 tpnum, phex_nz (addr, sizeof (addr)), srctype, 0, (int) strlen (src));
2504 if (strlen (buf) + strlen (src) * 2 >= buf_size)
2505 error (_("Source string too long for buffer"));
2506 bin2hex (src, buf + strlen (buf), 0);
2507 return -1;
2508 }
2509
2510 extern int trace_regblock_size;
2511
2512 /* Save tracepoint data to file named FILENAME. If TARGET_DOES_SAVE is
2513 non-zero, the save is performed on the target, otherwise GDB obtains all
2514 trace data and saves it locally. */
2515
2516 void
2517 trace_save (const char *filename, int target_does_save)
2518 {
2519 struct cleanup *cleanup;
2520 char *pathname;
2521 struct trace_status *ts = current_trace_status ();
2522 int err, status;
2523 FILE *fp;
2524 struct uploaded_tp *uploaded_tps = NULL, *utp;
2525 struct uploaded_tsv *uploaded_tsvs = NULL, *utsv;
2526 int a;
2527 struct uploaded_string *cmd;
2528 LONGEST gotten = 0;
2529 ULONGEST offset = 0;
2530 #define MAX_TRACE_UPLOAD 2000
2531 gdb_byte buf[MAX_TRACE_UPLOAD];
2532 int written;
2533
2534 /* If the target is to save the data to a file on its own, then just
2535 send the command and be done with it. */
2536 if (target_does_save)
2537 {
2538 err = target_save_trace_data (filename);
2539 if (err < 0)
2540 error (_("Target failed to save trace data to '%s'."),
2541 filename);
2542 return;
2543 }
2544
2545 /* Get the trace status first before opening the file, so if the
2546 target is losing, we can get out without touching files. */
2547 status = target_get_trace_status (ts);
2548
2549 pathname = tilde_expand (filename);
2550 cleanup = make_cleanup (xfree, pathname);
2551
2552 fp = fopen (pathname, "w");
2553 if (!fp)
2554 error (_("Unable to open file '%s' for saving trace data (%s)"),
2555 filename, safe_strerror (errno));
2556 make_cleanup_fclose (fp);
2557
2558 /* Write a file header, with a high-bit-set char to indicate a
2559 binary file, plus a hint as what this file is, and a version
2560 number in case of future needs. */
2561 written = fwrite ("\x7fTRACE0\n", 8, 1, fp);
2562 if (written < 1)
2563 perror_with_name (pathname);
2564
2565 /* Write descriptive info. */
2566
2567 /* Write out the size of a register block. */
2568 fprintf (fp, "R %x\n", trace_regblock_size);
2569
2570 /* Write out status of the tracing run (aka "tstatus" info). */
2571 fprintf (fp, "status %c;%s",
2572 (ts->running ? '1' : '0'), stop_reason_names[ts->stop_reason]);
2573 if (ts->stop_reason == tracepoint_error)
2574 {
2575 char *buf = (char *) alloca (strlen (ts->error_desc) * 2 + 1);
2576 bin2hex ((gdb_byte *) ts->error_desc, buf, 0);
2577 fprintf (fp, ":%s", buf);
2578 }
2579 fprintf (fp, ":%x", ts->stopping_tracepoint);
2580 if (ts->traceframe_count >= 0)
2581 fprintf (fp, ";tframes:%x", ts->traceframe_count);
2582 if (ts->traceframes_created >= 0)
2583 fprintf (fp, ";tcreated:%x", ts->traceframes_created);
2584 if (ts->buffer_free >= 0)
2585 fprintf (fp, ";tfree:%x", ts->buffer_free);
2586 if (ts->buffer_size >= 0)
2587 fprintf (fp, ";tsize:%x", ts->buffer_size);
2588 fprintf (fp, "\n");
2589
2590 /* Note that we want to upload tracepoints and save those, rather
2591 than simply writing out the local ones, because the user may have
2592 changed tracepoints in GDB in preparation for a future tracing
2593 run, or maybe just mass-deleted all types of breakpoints as part
2594 of cleaning up. So as not to contaminate the session, leave the
2595 data in its uploaded form, don't make into real tracepoints. */
2596
2597 /* Get trace state variables first, they may be checked when parsing
2598 uploaded commands. */
2599
2600 target_upload_trace_state_variables (&uploaded_tsvs);
2601
2602 for (utsv = uploaded_tsvs; utsv; utsv = utsv->next)
2603 {
2604 char *buf = "";
2605
2606 if (utsv->name)
2607 {
2608 buf = (char *) xmalloc (strlen (utsv->name) * 2 + 1);
2609 bin2hex ((gdb_byte *) (utsv->name), buf, 0);
2610 }
2611
2612 fprintf (fp, "tsv %x:%s:%x:%s\n",
2613 utsv->number, phex_nz (utsv->initial_value, 8),
2614 utsv->builtin, buf);
2615
2616 if (utsv->name)
2617 xfree (buf);
2618 }
2619
2620 free_uploaded_tsvs (&uploaded_tsvs);
2621
2622 target_upload_tracepoints (&uploaded_tps);
2623
2624 for (utp = uploaded_tps; utp; utp = utp->next)
2625 {
2626 fprintf (fp, "tp T%x:%s:%c:%x:%x",
2627 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
2628 (utp->enabled ? 'E' : 'D'), utp->step, utp->pass);
2629 if (utp->type == bp_fast_tracepoint)
2630 fprintf (fp, ":F%x", utp->orig_size);
2631 if (utp->cond)
2632 fprintf (fp, ":X%x,%s", (unsigned int) strlen (utp->cond) / 2,
2633 utp->cond);
2634 fprintf (fp, "\n");
2635 for (a = 0; a < utp->numactions; ++a)
2636 fprintf (fp, "tp A%x:%s:%s\n",
2637 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
2638 utp->actions[a]);
2639 for (a = 0; a < utp->num_step_actions; ++a)
2640 fprintf (fp, "tp S%x:%s:%s\n",
2641 utp->number, phex_nz (utp->addr, sizeof (utp->addr)),
2642 utp->step_actions[a]);
2643 if (utp->at_string)
2644 {
2645 encode_source_string (utp->number, utp->addr,
2646 "at", utp->at_string, buf, MAX_TRACE_UPLOAD);
2647 fprintf (fp, "tp Z%s\n", buf);
2648 }
2649 if (utp->cond_string)
2650 {
2651 encode_source_string (utp->number, utp->addr,
2652 "cond", utp->cond_string, buf, MAX_TRACE_UPLOAD);
2653 fprintf (fp, "tp Z%s\n", buf);
2654 }
2655 for (cmd = utp->cmd_strings; cmd; cmd = cmd->next)
2656 {
2657 encode_source_string (utp->number, utp->addr, "cmd", cmd->str,
2658 buf, MAX_TRACE_UPLOAD);
2659 fprintf (fp, "tp Z%s\n", buf);
2660 }
2661 }
2662
2663 free_uploaded_tps (&uploaded_tps);
2664
2665 /* Mark the end of the definition section. */
2666 fprintf (fp, "\n");
2667
2668 /* Get and write the trace data proper. We ask for big blocks, in
2669 the hopes of efficiency, but will take less if the target has
2670 packet size limitations or some such. */
2671 while (1)
2672 {
2673 gotten = target_get_raw_trace_data (buf, offset, MAX_TRACE_UPLOAD);
2674 if (gotten < 0)
2675 error (_("Failure to get requested trace buffer data"));
2676 /* No more data is forthcoming, we're done. */
2677 if (gotten == 0)
2678 break;
2679 written = fwrite (buf, gotten, 1, fp);
2680 if (written < 1)
2681 perror_with_name (pathname);
2682 offset += gotten;
2683 }
2684
2685 /* Mark the end of trace data. (We know that gotten is 0 at this point.) */
2686 written = fwrite (&gotten, 4, 1, fp);
2687 if (written < 1)
2688 perror_with_name (pathname);
2689
2690 do_cleanups (cleanup);
2691 }
2692
2693 static void
2694 trace_save_command (char *args, int from_tty)
2695 {
2696 int target_does_save = 0;
2697 char **argv;
2698 char *filename = NULL;
2699 struct cleanup *back_to;
2700
2701 if (args == NULL)
2702 error_no_arg (_("file in which to save trace data"));
2703
2704 argv = gdb_buildargv (args);
2705 back_to = make_cleanup_freeargv (argv);
2706
2707 for (; *argv; ++argv)
2708 {
2709 if (strcmp (*argv, "-r") == 0)
2710 target_does_save = 1;
2711 else if (**argv == '-')
2712 error (_("unknown option `%s'"), *argv);
2713 else
2714 filename = *argv;
2715 }
2716
2717 if (!filename)
2718 error_no_arg (_("file in which to save trace data"));
2719
2720 trace_save (filename, target_does_save);
2721
2722 if (from_tty)
2723 printf_filtered (_("Trace data saved to file '%s'.\n"), args);
2724
2725 do_cleanups (back_to);
2726 }
2727
2728 /* Tell the target what to do with an ongoing tracing run if GDB
2729 disconnects for some reason. */
2730
2731 void
2732 send_disconnected_tracing_value (int value)
2733 {
2734 target_set_disconnected_tracing (value);
2735 }
2736
2737 static void
2738 set_disconnected_tracing (char *args, int from_tty,
2739 struct cmd_list_element *c)
2740 {
2741 send_disconnected_tracing_value (disconnected_tracing);
2742 }
2743
2744 static void
2745 set_circular_trace_buffer (char *args, int from_tty,
2746 struct cmd_list_element *c)
2747 {
2748 target_set_circular_trace_buffer (circular_trace_buffer);
2749 }
2750
2751 /* Convert the memory pointed to by mem into hex, placing result in buf.
2752 * Return a pointer to the last char put in buf (null)
2753 * "stolen" from sparc-stub.c
2754 */
2755
2756 static const char hexchars[] = "0123456789abcdef";
2757
2758 static char *
2759 mem2hex (gdb_byte *mem, char *buf, int count)
2760 {
2761 gdb_byte ch;
2762
2763 while (count-- > 0)
2764 {
2765 ch = *mem++;
2766
2767 *buf++ = hexchars[ch >> 4];
2768 *buf++ = hexchars[ch & 0xf];
2769 }
2770
2771 *buf = 0;
2772
2773 return buf;
2774 }
2775
2776 int
2777 get_traceframe_number (void)
2778 {
2779 return traceframe_number;
2780 }
2781
2782 /* Make the traceframe NUM be the current trace frame. Does nothing
2783 if NUM is already current. */
2784
2785 void
2786 set_traceframe_number (int num)
2787 {
2788 int newnum;
2789
2790 if (traceframe_number == num)
2791 {
2792 /* Nothing to do. */
2793 return;
2794 }
2795
2796 newnum = target_trace_find (tfind_number, num, 0, 0, NULL);
2797
2798 if (newnum != num)
2799 warning (_("could not change traceframe"));
2800
2801 traceframe_number = newnum;
2802
2803 /* Changing the traceframe changes our view of registers and of the
2804 frame chain. */
2805 registers_changed ();
2806 }
2807
2808 /* A cleanup used when switching away and back from tfind mode. */
2809
2810 struct current_traceframe_cleanup
2811 {
2812 /* The traceframe we were inspecting. */
2813 int traceframe_number;
2814 };
2815
2816 static void
2817 do_restore_current_traceframe_cleanup (void *arg)
2818 {
2819 struct current_traceframe_cleanup *old = arg;
2820
2821 set_traceframe_number (old->traceframe_number);
2822 }
2823
2824 static void
2825 restore_current_traceframe_cleanup_dtor (void *arg)
2826 {
2827 struct current_traceframe_cleanup *old = arg;
2828
2829 xfree (old);
2830 }
2831
2832 struct cleanup *
2833 make_cleanup_restore_current_traceframe (void)
2834 {
2835 struct current_traceframe_cleanup *old;
2836
2837 old = xmalloc (sizeof (struct current_traceframe_cleanup));
2838 old->traceframe_number = traceframe_number;
2839
2840 return make_cleanup_dtor (do_restore_current_traceframe_cleanup, old,
2841 restore_current_traceframe_cleanup_dtor);
2842 }
2843
2844 /* Given a number and address, return an uploaded tracepoint with that
2845 number, creating if necessary. */
2846
2847 struct uploaded_tp *
2848 get_uploaded_tp (int num, ULONGEST addr, struct uploaded_tp **utpp)
2849 {
2850 struct uploaded_tp *utp;
2851
2852 for (utp = *utpp; utp; utp = utp->next)
2853 if (utp->number == num && utp->addr == addr)
2854 return utp;
2855 utp = (struct uploaded_tp *) xmalloc (sizeof (struct uploaded_tp));
2856 memset (utp, 0, sizeof (struct uploaded_tp));
2857 utp->number = num;
2858 utp->addr = addr;
2859 utp->next = *utpp;
2860 *utpp = utp;
2861 return utp;
2862 }
2863
2864 static void
2865 free_uploaded_tps (struct uploaded_tp **utpp)
2866 {
2867 struct uploaded_tp *next_one;
2868
2869 while (*utpp)
2870 {
2871 next_one = (*utpp)->next;
2872 xfree (*utpp);
2873 *utpp = next_one;
2874 }
2875 }
2876
2877 /* Given a number and address, return an uploaded tracepoint with that
2878 number, creating if necessary. */
2879
2880 struct uploaded_tsv *
2881 get_uploaded_tsv (int num, struct uploaded_tsv **utsvp)
2882 {
2883 struct uploaded_tsv *utsv;
2884
2885 for (utsv = *utsvp; utsv; utsv = utsv->next)
2886 if (utsv->number == num)
2887 return utsv;
2888 utsv = (struct uploaded_tsv *) xmalloc (sizeof (struct uploaded_tsv));
2889 memset (utsv, 0, sizeof (struct uploaded_tsv));
2890 utsv->number = num;
2891 utsv->next = *utsvp;
2892 *utsvp = utsv;
2893 return utsv;
2894 }
2895
2896 static void
2897 free_uploaded_tsvs (struct uploaded_tsv **utsvp)
2898 {
2899 struct uploaded_tsv *next_one;
2900
2901 while (*utsvp)
2902 {
2903 next_one = (*utsvp)->next;
2904 xfree (*utsvp);
2905 *utsvp = next_one;
2906 }
2907 }
2908
2909 /* Look for an existing tracepoint that seems similar enough to the
2910 uploaded one. Enablement isn't compared, because the user can
2911 toggle that freely, and may have done so in anticipation of the
2912 next trace run. */
2913
2914 struct breakpoint *
2915 find_matching_tracepoint (struct uploaded_tp *utp)
2916 {
2917 VEC(breakpoint_p) *tp_vec = all_tracepoints ();
2918 int ix;
2919 struct breakpoint *t;
2920 struct bp_location *loc;
2921
2922 for (ix = 0; VEC_iterate (breakpoint_p, tp_vec, ix, t); ix++)
2923 {
2924 if (t->type == utp->type
2925 && t->step_count == utp->step
2926 && t->pass_count == utp->pass
2927 /* FIXME also test conditionals and actions */
2928 )
2929 {
2930 /* Scan the locations for an address match. */
2931 for (loc = t->loc; loc; loc = loc->next)
2932 {
2933 if (loc->address == utp->addr)
2934 return t;
2935 }
2936 }
2937 }
2938 return NULL;
2939 }
2940
2941 /* Given a list of tracepoints uploaded from a target, attempt to
2942 match them up with existing tracepoints, and create new ones if not
2943 found. */
2944
2945 void
2946 merge_uploaded_tracepoints (struct uploaded_tp **uploaded_tps)
2947 {
2948 struct uploaded_tp *utp;
2949 struct breakpoint *t;
2950
2951 /* Look for GDB tracepoints that match up with our uploaded versions. */
2952 for (utp = *uploaded_tps; utp; utp = utp->next)
2953 {
2954 t = find_matching_tracepoint (utp);
2955 if (t)
2956 printf_filtered (_("Assuming tracepoint %d is same as target's tracepoint %d at %s.\n"),
2957 t->number, utp->number, paddress (get_current_arch (), utp->addr));
2958 else
2959 {
2960 t = create_tracepoint_from_upload (utp);
2961 if (t)
2962 printf_filtered (_("Created tracepoint %d for target's tracepoint %d at %s.\n"),
2963 t->number, utp->number, paddress (get_current_arch (), utp->addr));
2964 else
2965 printf_filtered (_("Failed to create tracepoint for target's tracepoint %d at %s, skipping it.\n"),
2966 utp->number, paddress (get_current_arch (), utp->addr));
2967 }
2968 /* Whether found or created, record the number used by the
2969 target, to help with mapping target tracepoints back to their
2970 counterparts here. */
2971 if (t)
2972 t->number_on_target = utp->number;
2973 }
2974
2975 free_uploaded_tps (uploaded_tps);
2976 }
2977
2978 /* Trace state variables don't have much to identify them beyond their
2979 name, so just use that to detect matches. */
2980
2981 struct trace_state_variable *
2982 find_matching_tsv (struct uploaded_tsv *utsv)
2983 {
2984 if (!utsv->name)
2985 return NULL;
2986
2987 return find_trace_state_variable (utsv->name);
2988 }
2989
2990 struct trace_state_variable *
2991 create_tsv_from_upload (struct uploaded_tsv *utsv)
2992 {
2993 const char *namebase;
2994 char buf[20];
2995 int try_num = 0;
2996 struct trace_state_variable *tsv;
2997
2998 if (utsv->name)
2999 {
3000 namebase = utsv->name;
3001 sprintf (buf, "%s", namebase);
3002 }
3003 else
3004 {
3005 namebase = "__tsv";
3006 sprintf (buf, "%s_%d", namebase, try_num++);
3007 }
3008
3009 /* Fish for a name that is not in use. */
3010 /* (should check against all internal vars?) */
3011 while (find_trace_state_variable (buf))
3012 sprintf (buf, "%s_%d", namebase, try_num++);
3013
3014 /* We have an available name, create the variable. */
3015 tsv = create_trace_state_variable (xstrdup (buf));
3016 tsv->initial_value = utsv->initial_value;
3017 tsv->builtin = utsv->builtin;
3018
3019 return tsv;
3020 }
3021
3022 /* Given a list of uploaded trace state variables, try to match them
3023 up with existing variables, or create additional ones. */
3024
3025 void
3026 merge_uploaded_trace_state_variables (struct uploaded_tsv **uploaded_tsvs)
3027 {
3028 int ix;
3029 struct uploaded_tsv *utsv;
3030 struct trace_state_variable *tsv;
3031 int highest;
3032
3033 /* Most likely some numbers will have to be reassigned as part of
3034 the merge, so clear them all in anticipation. */
3035 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3036 tsv->number = 0;
3037
3038 for (utsv = *uploaded_tsvs; utsv; utsv = utsv->next)
3039 {
3040 tsv = find_matching_tsv (utsv);
3041 if (tsv)
3042 printf_filtered (_("Assuming trace state variable $%s is same as target's variable %d.\n"),
3043 tsv->name, utsv->number);
3044 else
3045 {
3046 tsv = create_tsv_from_upload (utsv);
3047 printf_filtered (_("Created trace state variable $%s for target's variable %d.\n"),
3048 tsv->name, utsv->number);
3049 }
3050 /* Give precedence to numberings that come from the target. */
3051 if (tsv)
3052 tsv->number = utsv->number;
3053 }
3054
3055 /* Renumber everything that didn't get a target-assigned number. */
3056 highest = 0;
3057 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3058 if (tsv->number > highest)
3059 highest = tsv->number;
3060
3061 ++highest;
3062 for (ix = 0; VEC_iterate (tsv_s, tvariables, ix, tsv); ++ix)
3063 if (tsv->number == 0)
3064 tsv->number = highest++;
3065
3066 free_uploaded_tsvs (uploaded_tsvs);
3067 }
3068
3069 /* target tfile command */
3070
3071 struct target_ops tfile_ops;
3072
3073 /* Fill in tfile_ops with its defined operations and properties. */
3074
3075 #define TRACE_HEADER_SIZE 8
3076
3077 char *trace_filename;
3078 int trace_fd = -1;
3079 off_t trace_frames_offset;
3080 off_t cur_offset;
3081 int cur_data_size;
3082 int trace_regblock_size;
3083
3084 static void tfile_interp_line (char *line,
3085 struct uploaded_tp **utpp,
3086 struct uploaded_tsv **utsvp);
3087
3088 static void
3089 tfile_open (char *filename, int from_tty)
3090 {
3091 char *temp;
3092 struct cleanup *old_chain;
3093 int flags;
3094 int scratch_chan;
3095 char header[TRACE_HEADER_SIZE];
3096 char linebuf[1000]; /* should be max remote packet size or so */
3097 char byte;
3098 int bytes, i, gotten;
3099 struct trace_status *ts;
3100 struct uploaded_tp *uploaded_tps = NULL;
3101 struct uploaded_tsv *uploaded_tsvs = NULL;
3102
3103 target_preopen (from_tty);
3104 if (!filename)
3105 error (_("No trace file specified."));
3106
3107 filename = tilde_expand (filename);
3108 if (!IS_ABSOLUTE_PATH(filename))
3109 {
3110 temp = concat (current_directory, "/", filename, (char *)NULL);
3111 xfree (filename);
3112 filename = temp;
3113 }
3114
3115 old_chain = make_cleanup (xfree, filename);
3116
3117 flags = O_BINARY | O_LARGEFILE;
3118 flags |= O_RDONLY;
3119 scratch_chan = open (filename, flags, 0);
3120 if (scratch_chan < 0)
3121 perror_with_name (filename);
3122
3123 /* Looks semi-reasonable. Toss the old trace file and work on the new. */
3124
3125 discard_cleanups (old_chain); /* Don't free filename any more */
3126 unpush_target (&tfile_ops);
3127
3128 push_target (&tfile_ops);
3129
3130 trace_filename = xstrdup (filename);
3131 trace_fd = scratch_chan;
3132
3133 bytes = 0;
3134 /* Read the file header and test for validity. */
3135 gotten = read (trace_fd, &header, TRACE_HEADER_SIZE);
3136 if (gotten < 0)
3137 perror_with_name (trace_filename);
3138 else if (gotten < TRACE_HEADER_SIZE)
3139 error (_("Premature end of file while reading trace file"));
3140
3141 bytes += TRACE_HEADER_SIZE;
3142 if (!(header[0] == 0x7f
3143 && (strncmp (header + 1, "TRACE0\n", 7) == 0)))
3144 error (_("File is not a valid trace file."));
3145
3146 trace_regblock_size = 0;
3147 ts = current_trace_status ();
3148 /* We know we're working with a file. */
3149 ts->from_file = 1;
3150 /* Set defaults in case there is no status line. */
3151 ts->running_known = 0;
3152 ts->stop_reason = trace_stop_reason_unknown;
3153 ts->traceframe_count = -1;
3154 ts->buffer_free = 0;
3155
3156 /* Read through a section of newline-terminated lines that
3157 define things like tracepoints. */
3158 i = 0;
3159 while (1)
3160 {
3161 gotten = read (trace_fd, &byte, 1);
3162 if (gotten < 0)
3163 perror_with_name (trace_filename);
3164 else if (gotten < 1)
3165 error (_("Premature end of file while reading trace file"));
3166
3167 ++bytes;
3168 if (byte == '\n')
3169 {
3170 /* Empty line marks end of the definition section. */
3171 if (i == 0)
3172 break;
3173 linebuf[i] = '\0';
3174 i = 0;
3175 tfile_interp_line (linebuf, &uploaded_tps, &uploaded_tsvs);
3176 }
3177 else
3178 linebuf[i++] = byte;
3179 if (i >= 1000)
3180 error (_("Excessively long lines in trace file"));
3181 }
3182
3183 /* Add the file's tracepoints and variables into the current mix. */
3184
3185 /* Get trace state variables first, they may be checked when parsing
3186 uploaded commands. */
3187 merge_uploaded_trace_state_variables (&uploaded_tsvs);
3188
3189 merge_uploaded_tracepoints (&uploaded_tps);
3190
3191 /* Record the starting offset of the binary trace data. */
3192 trace_frames_offset = bytes;
3193
3194 /* If we don't have a blocksize, we can't interpret the
3195 traceframes. */
3196 if (trace_regblock_size == 0)
3197 error (_("No register block size recorded in trace file"));
3198 if (ts->traceframe_count <= 0)
3199 {
3200 warning ("No traceframes present in this file.");
3201 return;
3202 }
3203
3204 #define TFILE_PID (1)
3205 inferior_appeared (current_inferior (), TFILE_PID);
3206 inferior_ptid = pid_to_ptid (TFILE_PID);
3207 add_thread_silent (inferior_ptid);
3208
3209 post_create_inferior (&tfile_ops, from_tty);
3210
3211 #if 0
3212 /* FIXME this will get defined in MI patch submission */
3213 tfind_1 (tfind_number, 0, 0, 0, 0);
3214 #endif
3215 }
3216
3217 /* Interpret the given line from the definitions part of the trace
3218 file. */
3219
3220 static void
3221 tfile_interp_line (char *line,
3222 struct uploaded_tp **utpp, struct uploaded_tsv **utsvp)
3223 {
3224 char *p = line;
3225
3226 if (strncmp (p, "R ", strlen ("R ")) == 0)
3227 {
3228 p += strlen ("R ");
3229 trace_regblock_size = strtol (p, &p, 16);
3230 }
3231 else if (strncmp (p, "status ", strlen ("status ")) == 0)
3232 {
3233 p += strlen ("status ");
3234 parse_trace_status (p, current_trace_status ());
3235 }
3236 else if (strncmp (p, "tp ", strlen ("tp ")) == 0)
3237 {
3238 p += strlen ("tp ");
3239 parse_tracepoint_definition (p, utpp);
3240 }
3241 else if (strncmp (p, "tsv ", strlen ("tsv ")) == 0)
3242 {
3243 p += strlen ("tsv ");
3244 parse_tsv_definition (p, utsvp);
3245 }
3246 else
3247 warning ("Ignoring trace file definition \"%s\"", line);
3248 }
3249
3250 /* Parse the part of trace status syntax that is shared between
3251 the remote protocol and the trace file reader. */
3252
3253 extern char *unpack_varlen_hex (char *buff, ULONGEST *result);
3254
3255 void
3256 parse_trace_status (char *line, struct trace_status *ts)
3257 {
3258 char *p = line, *p1, *p2, *p_temp;
3259 ULONGEST val;
3260
3261 ts->running_known = 1;
3262 ts->running = (*p++ == '1');
3263 ts->stop_reason = trace_stop_reason_unknown;
3264 xfree (ts->error_desc);
3265 ts->error_desc = NULL;
3266 ts->traceframe_count = -1;
3267 ts->traceframes_created = -1;
3268 ts->buffer_free = -1;
3269 ts->buffer_size = -1;
3270
3271 while (*p++)
3272 {
3273 p1 = strchr (p, ':');
3274 if (p1 == NULL)
3275 error (_("Malformed trace status, at %s\n\
3276 Status line: '%s'\n"), p, line);
3277 if (strncmp (p, stop_reason_names[trace_buffer_full], p1 - p) == 0)
3278 {
3279 p = unpack_varlen_hex (++p1, &val);
3280 ts->stop_reason = trace_buffer_full;
3281 }
3282 else if (strncmp (p, stop_reason_names[trace_never_run], p1 - p) == 0)
3283 {
3284 p = unpack_varlen_hex (++p1, &val);
3285 ts->stop_reason = trace_never_run;
3286 }
3287 else if (strncmp (p, stop_reason_names[tracepoint_passcount], p1 - p) == 0)
3288 {
3289 p = unpack_varlen_hex (++p1, &val);
3290 ts->stop_reason = tracepoint_passcount;
3291 ts->stopping_tracepoint = val;
3292 }
3293 else if (strncmp (p, stop_reason_names[tstop_command], p1 - p) == 0)
3294 {
3295 p = unpack_varlen_hex (++p1, &val);
3296 ts->stop_reason = tstop_command;
3297 }
3298 else if (strncmp (p, stop_reason_names[tracepoint_error], p1 - p) == 0)
3299 {
3300 p2 = strchr (++p1, ':');
3301 if (p2 != p1)
3302 {
3303 int end;
3304
3305 ts->error_desc = xmalloc ((p2 - p1) / 2 + 1);
3306 end = hex2bin (p1, ts->error_desc, (p2 - p1) / 2);
3307 ts->error_desc[end] = '\0';
3308 }
3309 else
3310 ts->error_desc = xstrdup ("");
3311
3312 p = unpack_varlen_hex (++p2, &val);
3313 ts->stopping_tracepoint = val;
3314 ts->stop_reason = tracepoint_error;
3315 }
3316 else if (strncmp (p, "tframes", p1 - p) == 0)
3317 {
3318 p = unpack_varlen_hex (++p1, &val);
3319 ts->traceframe_count = val;
3320 }
3321 else if (strncmp (p, "tcreated", p1 - p) == 0)
3322 {
3323 p = unpack_varlen_hex (++p1, &val);
3324 ts->traceframes_created = val;
3325 }
3326 else if (strncmp (p, "tfree", p1 - p) == 0)
3327 {
3328 p = unpack_varlen_hex (++p1, &val);
3329 ts->buffer_free = val;
3330 }
3331 else if (strncmp (p, "tsize", p1 - p) == 0)
3332 {
3333 p = unpack_varlen_hex (++p1, &val);
3334 ts->buffer_size = val;
3335 }
3336 else
3337 {
3338 /* Silently skip unknown optional info. */
3339 p_temp = strchr (p1 + 1, ';');
3340 if (p_temp)
3341 p = p_temp;
3342 else
3343 /* Must be at the end. */
3344 break;
3345 }
3346 }
3347 }
3348
3349 /* Given a line of text defining a part of a tracepoint, parse it into
3350 an "uploaded tracepoint". */
3351
3352 void
3353 parse_tracepoint_definition (char *line, struct uploaded_tp **utpp)
3354 {
3355 char *p;
3356 char piece;
3357 ULONGEST num, addr, step, pass, orig_size, xlen, start;
3358 int enabled, i, end;
3359 enum bptype type;
3360 char *cond, *srctype, *src, *buf;
3361 struct uploaded_tp *utp = NULL;
3362
3363 p = line;
3364 /* Both tracepoint and action definitions start with the same number
3365 and address sequence. */
3366 piece = *p++;
3367 p = unpack_varlen_hex (p, &num);
3368 p++; /* skip a colon */
3369 p = unpack_varlen_hex (p, &addr);
3370 p++; /* skip a colon */
3371 if (piece == 'T')
3372 {
3373 enabled = (*p++ == 'E');
3374 p++; /* skip a colon */
3375 p = unpack_varlen_hex (p, &step);
3376 p++; /* skip a colon */
3377 p = unpack_varlen_hex (p, &pass);
3378 type = bp_tracepoint;
3379 cond = NULL;
3380 /* Thumb through optional fields. */
3381 while (*p == ':')
3382 {
3383 p++; /* skip a colon */
3384 if (*p == 'F')
3385 {
3386 type = bp_fast_tracepoint;
3387 p++;
3388 p = unpack_varlen_hex (p, &orig_size);
3389 }
3390 else if (*p == 'X')
3391 {
3392 p++;
3393 p = unpack_varlen_hex (p, &xlen);
3394 p++; /* skip a comma */
3395 cond = (char *) xmalloc (2 * xlen + 1);
3396 strncpy (cond, p, 2 * xlen);
3397 cond[2 * xlen] = '\0';
3398 p += 2 * xlen;
3399 }
3400 else
3401 warning (_("Unrecognized char '%c' in tracepoint definition, skipping rest"), *p);
3402 }
3403 utp = get_uploaded_tp (num, addr, utpp);
3404 utp->type = type;
3405 utp->enabled = enabled;
3406 utp->step = step;
3407 utp->pass = pass;
3408 utp->cond = cond;
3409 }
3410 else if (piece == 'A')
3411 {
3412 utp = get_uploaded_tp (num, addr, utpp);
3413 utp->actions[utp->numactions++] = xstrdup (p);
3414 }
3415 else if (piece == 'S')
3416 {
3417 utp = get_uploaded_tp (num, addr, utpp);
3418 utp->step_actions[utp->num_step_actions++] = xstrdup (p);
3419 }
3420 else if (piece == 'Z')
3421 {
3422 /* Parse a chunk of source form definition. */
3423 utp = get_uploaded_tp (num, addr, utpp);
3424 srctype = p;
3425 p = strchr (p, ':');
3426 p++; /* skip a colon */
3427 p = unpack_varlen_hex (p, &start);
3428 p++; /* skip a colon */
3429 p = unpack_varlen_hex (p, &xlen);
3430 p++; /* skip a colon */
3431
3432 buf = alloca (strlen (line));
3433
3434 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3435 buf[end] = '\0';
3436
3437 if (strncmp (srctype, "at:", strlen ("at:")) == 0)
3438 utp->at_string = xstrdup (buf);
3439 else if (strncmp (srctype, "cond:", strlen ("cond:")) == 0)
3440 utp->cond_string = xstrdup (buf);
3441 else if (strncmp (srctype, "cmd:", strlen ("cmd:")) == 0)
3442 {
3443 /* FIXME consider using a vector? */
3444 struct uploaded_string *last, *newlast;
3445 newlast = (struct uploaded_string *) xmalloc (sizeof (struct uploaded_string));
3446 newlast->str = xstrdup (buf);
3447 newlast->next = NULL;
3448 if (utp->cmd_strings)
3449 {
3450 for (last = utp->cmd_strings; last->next; last = last->next)
3451 ;
3452 last->next = newlast;
3453 }
3454 else
3455 utp->cmd_strings = newlast;
3456 }
3457 }
3458 else
3459 {
3460 /* Don't error out, the target might be sending us optional
3461 info that we don't care about. */
3462 warning (_("Unrecognized tracepoint piece '%c', ignoring"), piece);
3463 }
3464 }
3465
3466 /* Convert a textual description of a trace state variable into an
3467 uploaded object. */
3468
3469 void
3470 parse_tsv_definition (char *line, struct uploaded_tsv **utsvp)
3471 {
3472 char *p, *buf;
3473 ULONGEST num, initval, builtin;
3474 int end;
3475 struct uploaded_tsv *utsv = NULL;
3476
3477 buf = alloca (strlen (line));
3478
3479 p = line;
3480 p = unpack_varlen_hex (p, &num);
3481 p++; /* skip a colon */
3482 p = unpack_varlen_hex (p, &initval);
3483 p++; /* skip a colon */
3484 p = unpack_varlen_hex (p, &builtin);
3485 p++; /* skip a colon */
3486 end = hex2bin (p, (gdb_byte *) buf, strlen (p) / 2);
3487 buf[end] = '\0';
3488
3489 utsv = get_uploaded_tsv (num, utsvp);
3490 utsv->initial_value = initval;
3491 utsv->builtin = builtin;
3492 utsv->name = xstrdup (buf);
3493 }
3494
3495 /* Close the trace file and generally clean up. */
3496
3497 static void
3498 tfile_close (int quitting)
3499 {
3500 int pid;
3501
3502 if (trace_fd < 0)
3503 return;
3504
3505 pid = ptid_get_pid (inferior_ptid);
3506 inferior_ptid = null_ptid; /* Avoid confusion from thread stuff */
3507 exit_inferior_silent (pid);
3508
3509 close (trace_fd);
3510 trace_fd = -1;
3511 if (trace_filename)
3512 xfree (trace_filename);
3513 }
3514
3515 static void
3516 tfile_files_info (struct target_ops *t)
3517 {
3518 /* (it would be useful to mention the name of the file) */
3519 printf_filtered ("Looking at a trace file.\n");
3520 }
3521
3522 /* The trace status for a file is that tracing can never be run. */
3523
3524 static int
3525 tfile_get_trace_status (struct trace_status *ts)
3526 {
3527 /* Other bits of trace status were collected as part of opening the
3528 trace files, so nothing to do here. */
3529
3530 return -1;
3531 }
3532
3533 /* Given the position of a traceframe in the file, figure out what
3534 address the frame was collected at. This would normally be the
3535 value of a collected PC register, but if not available, we
3536 improvise. */
3537
3538 static ULONGEST
3539 tfile_get_traceframe_address (off_t tframe_offset)
3540 {
3541 ULONGEST addr = 0;
3542 short tpnum;
3543 struct breakpoint *tp;
3544 off_t saved_offset = cur_offset;
3545 int gotten;
3546
3547 /* FIXME dig pc out of collected registers */
3548
3549 /* Fall back to using tracepoint address. */
3550 lseek (trace_fd, tframe_offset, SEEK_SET);
3551 gotten = read (trace_fd, &tpnum, 2);
3552 if (gotten < 0)
3553 perror_with_name (trace_filename);
3554 else if (gotten < 2)
3555 error (_("Premature end of file while reading trace file"));
3556
3557 tp = get_tracepoint_by_number_on_target (tpnum);
3558 /* FIXME this is a poor heuristic if multiple locations */
3559 if (tp && tp->loc)
3560 addr = tp->loc->address;
3561
3562 /* Restore our seek position. */
3563 cur_offset = saved_offset;
3564 lseek (trace_fd, cur_offset, SEEK_SET);
3565 return addr;
3566 }
3567
3568 /* Given a type of search and some parameters, scan the collection of
3569 traceframes in the file looking for a match. When found, return
3570 both the traceframe and tracepoint number, otherwise -1 for
3571 each. */
3572
3573 static int
3574 tfile_trace_find (enum trace_find_type type, int num,
3575 ULONGEST addr1, ULONGEST addr2, int *tpp)
3576 {
3577 short tpnum;
3578 int tfnum = 0, found = 0, gotten;
3579 int data_size;
3580 struct breakpoint *tp;
3581 off_t offset, tframe_offset;
3582 ULONGEST tfaddr;
3583
3584 lseek (trace_fd, trace_frames_offset, SEEK_SET);
3585 offset = trace_frames_offset;
3586 while (1)
3587 {
3588 tframe_offset = offset;
3589 gotten = read (trace_fd, &tpnum, 2);
3590 if (gotten < 0)
3591 perror_with_name (trace_filename);
3592 else if (gotten < 2)
3593 error (_("Premature end of file while reading trace file"));
3594 offset += 2;
3595 if (tpnum == 0)
3596 break;
3597 gotten = read (trace_fd, &data_size, 4);
3598 if (gotten < 0)
3599 perror_with_name (trace_filename);
3600 else if (gotten < 4)
3601 error (_("Premature end of file while reading trace file"));
3602 offset += 4;
3603 switch (type)
3604 {
3605 case tfind_number:
3606 if (tfnum == num)
3607 found = 1;
3608 break;
3609 case tfind_pc:
3610 tfaddr = tfile_get_traceframe_address (tframe_offset);
3611 if (tfaddr == addr1)
3612 found = 1;
3613 break;
3614 case tfind_tp:
3615 tp = get_tracepoint (num);
3616 if (tp && tpnum == tp->number_on_target)
3617 found = 1;
3618 break;
3619 case tfind_range:
3620 tfaddr = tfile_get_traceframe_address (tframe_offset);
3621 if (addr1 <= tfaddr && tfaddr <= addr2)
3622 found = 1;
3623 break;
3624 case tfind_outside:
3625 tfaddr = tfile_get_traceframe_address (tframe_offset);
3626 if (!(addr1 <= tfaddr && tfaddr <= addr2))
3627 found = 1;
3628 break;
3629 default:
3630 internal_error (__FILE__, __LINE__, _("unknown tfind type"));
3631 }
3632 if (found)
3633 {
3634 if (tpp)
3635 *tpp = tpnum;
3636 cur_offset = offset;
3637 cur_data_size = data_size;
3638 return tfnum;
3639 }
3640 /* Skip past the traceframe's data. */
3641 lseek (trace_fd, data_size, SEEK_CUR);
3642 offset += data_size;
3643 /* Update our own count of traceframes. */
3644 ++tfnum;
3645 }
3646 /* Did not find what we were looking for. */
3647 if (tpp)
3648 *tpp = -1;
3649 return -1;
3650 }
3651
3652 /* Look for a block of saved registers in the traceframe, and get the
3653 requested register from it. */
3654
3655 static void
3656 tfile_fetch_registers (struct target_ops *ops,
3657 struct regcache *regcache, int regno)
3658 {
3659 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3660 char block_type;
3661 int i, pos, offset, regn, regsize, gotten;
3662 unsigned short mlen;
3663 char *regs;
3664
3665 /* An uninitialized reg size says we're not going to be
3666 successful at getting register blocks. */
3667 if (!trace_regblock_size)
3668 return;
3669
3670 regs = alloca (trace_regblock_size);
3671
3672 lseek (trace_fd, cur_offset, SEEK_SET);
3673 pos = 0;
3674 while (pos < cur_data_size)
3675 {
3676 gotten = read (trace_fd, &block_type, 1);
3677 if (gotten < 0)
3678 perror_with_name (trace_filename);
3679 else if (gotten < 1)
3680 error (_("Premature end of file while reading trace file"));
3681
3682 ++pos;
3683 switch (block_type)
3684 {
3685 case 'R':
3686 gotten = read (trace_fd, regs, trace_regblock_size);
3687 if (gotten < 0)
3688 perror_with_name (trace_filename);
3689 else if (gotten < trace_regblock_size)
3690 error (_("Premature end of file while reading trace file"));
3691
3692 /* Assume the block is laid out in GDB register number order,
3693 each register with the size that it has in GDB. */
3694 offset = 0;
3695 for (regn = 0; regn < gdbarch_num_regs (gdbarch); regn++)
3696 {
3697 regsize = register_size (gdbarch, regn);
3698 /* Make sure we stay within block bounds. */
3699 if (offset + regsize >= trace_regblock_size)
3700 break;
3701 if (!regcache_valid_p (regcache, regn))
3702 {
3703 if (regno == regn)
3704 {
3705 regcache_raw_supply (regcache, regno, regs + offset);
3706 break;
3707 }
3708 else if (regno == -1)
3709 {
3710 regcache_raw_supply (regcache, regn, regs + offset);
3711 }
3712 }
3713 offset += regsize;
3714 }
3715 return;
3716 case 'M':
3717 lseek (trace_fd, 8, SEEK_CUR);
3718 gotten = read (trace_fd, &mlen, 2);
3719 if (gotten < 0)
3720 perror_with_name (trace_filename);
3721 else if (gotten < 2)
3722 error (_("Premature end of file while reading trace file"));
3723 lseek (trace_fd, mlen, SEEK_CUR);
3724 pos += (8 + 2 + mlen);
3725 break;
3726 case 'V':
3727 lseek (trace_fd, 4 + 8, SEEK_CUR);
3728 pos += (4 + 8);
3729 break;
3730 default:
3731 error ("Unknown block type '%c' (0x%x) in trace frame",
3732 block_type, block_type);
3733 break;
3734 }
3735 }
3736 }
3737
3738 static LONGEST
3739 tfile_xfer_partial (struct target_ops *ops, enum target_object object,
3740 const char *annex, gdb_byte *readbuf,
3741 const gdb_byte *writebuf, ULONGEST offset, LONGEST len)
3742 {
3743 char block_type;
3744 int pos, gotten;
3745 ULONGEST maddr;
3746 unsigned short mlen;
3747
3748 /* We're only doing regular memory for now. */
3749 if (object != TARGET_OBJECT_MEMORY)
3750 return -1;
3751
3752 if (readbuf == NULL)
3753 error ("tfile_xfer_partial: trace file is read-only");
3754
3755 lseek (trace_fd, cur_offset, SEEK_SET);
3756 pos = 0;
3757 while (pos < cur_data_size)
3758 {
3759 gotten = read (trace_fd, &block_type, 1);
3760 if (gotten < 0)
3761 perror_with_name (trace_filename);
3762 else if (gotten < 1)
3763 error (_("Premature end of file while reading trace file"));
3764 ++pos;
3765 switch (block_type)
3766 {
3767 case 'R':
3768 lseek (trace_fd, trace_regblock_size, SEEK_CUR);
3769 pos += trace_regblock_size;
3770 break;
3771 case 'M':
3772 gotten = read (trace_fd, &maddr, 8);
3773 if (gotten < 0)
3774 perror_with_name (trace_filename);
3775 else if (gotten < 8)
3776 error (_("Premature end of file while reading trace file"));
3777
3778 gotten = read (trace_fd, &mlen, 2);
3779 if (gotten < 0)
3780 perror_with_name (trace_filename);
3781 else if (gotten < 2)
3782 error (_("Premature end of file while reading trace file"));
3783 if (maddr <= offset && (offset + len) <= (maddr + mlen))
3784 {
3785 gotten = read (trace_fd, readbuf, mlen);
3786 if (gotten < 0)
3787 perror_with_name (trace_filename);
3788 else if (gotten < mlen)
3789 error (_("Premature end of file qwhile reading trace file"));
3790
3791 return mlen;
3792 }
3793 lseek (trace_fd, mlen, SEEK_CUR);
3794 pos += (8 + 2 + mlen);
3795 break;
3796 case 'V':
3797 lseek (trace_fd, 4 + 8, SEEK_CUR);
3798 pos += (4 + 8);
3799 break;
3800 default:
3801 error ("Unknown block type '%c' (0x%x) in traceframe",
3802 block_type, block_type);
3803 break;
3804 }
3805 }
3806 /* Indicate failure to find the requested memory block. */
3807 return -1;
3808 }
3809
3810 /* Iterate through the blocks of a trace frame, looking for a 'V'
3811 block with a matching tsv number. */
3812
3813 static int
3814 tfile_get_trace_state_variable_value (int tsvnum, LONGEST *val)
3815 {
3816 char block_type;
3817 int pos, vnum, gotten;
3818 unsigned short mlen;
3819
3820 lseek (trace_fd, cur_offset, SEEK_SET);
3821 pos = 0;
3822 while (pos < cur_data_size)
3823 {
3824 gotten = read (trace_fd, &block_type, 1);
3825 if (gotten < 0)
3826 perror_with_name (trace_filename);
3827 else if (gotten < 1)
3828 error (_("Premature end of file while reading trace file"));
3829 ++pos;
3830 switch (block_type)
3831 {
3832 case 'R':
3833 lseek (trace_fd, trace_regblock_size, SEEK_CUR);
3834 pos += trace_regblock_size;
3835 break;
3836 case 'M':
3837 lseek (trace_fd, 8, SEEK_CUR);
3838 gotten = read (trace_fd, &mlen, 2);
3839 if (gotten < 0)
3840 perror_with_name (trace_filename);
3841 else if (gotten < 2)
3842 error (_("Premature end of file while reading trace file"));
3843 lseek (trace_fd, mlen, SEEK_CUR);
3844 pos += (8 + 2 + mlen);
3845 break;
3846 case 'V':
3847 gotten = read (trace_fd, &vnum, 4);
3848 if (gotten < 0)
3849 perror_with_name (trace_filename);
3850 else if (gotten < 4)
3851 error (_("Premature end of file while reading trace file"));
3852 if (tsvnum == vnum)
3853 {
3854 gotten = read (trace_fd, val, 8);
3855 if (gotten < 0)
3856 perror_with_name (trace_filename);
3857 else if (gotten < 8)
3858 error (_("Premature end of file while reading trace file"));
3859 return 1;
3860 }
3861 lseek (trace_fd, 8, SEEK_CUR);
3862 pos += (4 + 8);
3863 break;
3864 default:
3865 error ("Unknown block type '%c' (0x%x) in traceframe",
3866 block_type, block_type);
3867 break;
3868 }
3869 }
3870 /* Didn't find anything. */
3871 return 0;
3872 }
3873
3874 static int
3875 tfile_has_memory (struct target_ops *ops)
3876 {
3877 return 1;
3878 }
3879
3880 static int
3881 tfile_has_stack (struct target_ops *ops)
3882 {
3883 return 1;
3884 }
3885
3886 static int
3887 tfile_has_registers (struct target_ops *ops)
3888 {
3889 return 1;
3890 }
3891
3892 static void
3893 init_tfile_ops (void)
3894 {
3895 tfile_ops.to_shortname = "tfile";
3896 tfile_ops.to_longname = "Local trace dump file";
3897 tfile_ops.to_doc =
3898 "Use a trace file as a target. Specify the filename of the trace file.";
3899 tfile_ops.to_open = tfile_open;
3900 tfile_ops.to_close = tfile_close;
3901 tfile_ops.to_fetch_registers = tfile_fetch_registers;
3902 tfile_ops.to_xfer_partial = tfile_xfer_partial;
3903 tfile_ops.to_files_info = tfile_files_info;
3904 tfile_ops.to_get_trace_status = tfile_get_trace_status;
3905 tfile_ops.to_trace_find = tfile_trace_find;
3906 tfile_ops.to_get_trace_state_variable_value = tfile_get_trace_state_variable_value;
3907 /* core_stratum might seem more logical, but GDB doesn't like having
3908 more than one core_stratum vector. */
3909 tfile_ops.to_stratum = process_stratum;
3910 tfile_ops.to_has_memory = tfile_has_memory;
3911 tfile_ops.to_has_stack = tfile_has_stack;
3912 tfile_ops.to_has_registers = tfile_has_registers;
3913 tfile_ops.to_magic = OPS_MAGIC;
3914 }
3915
3916 /* module initialization */
3917 void
3918 _initialize_tracepoint (void)
3919 {
3920 struct cmd_list_element *c;
3921
3922 traceframe_number = -1;
3923 tracepoint_number = -1;
3924
3925 if (tracepoint_list.list == NULL)
3926 {
3927 tracepoint_list.listsize = 128;
3928 tracepoint_list.list = xmalloc
3929 (tracepoint_list.listsize * sizeof (struct memrange));
3930 }
3931 if (tracepoint_list.aexpr_list == NULL)
3932 {
3933 tracepoint_list.aexpr_listsize = 128;
3934 tracepoint_list.aexpr_list = xmalloc
3935 (tracepoint_list.aexpr_listsize * sizeof (struct agent_expr *));
3936 }
3937
3938 if (stepping_list.list == NULL)
3939 {
3940 stepping_list.listsize = 128;
3941 stepping_list.list = xmalloc
3942 (stepping_list.listsize * sizeof (struct memrange));
3943 }
3944
3945 if (stepping_list.aexpr_list == NULL)
3946 {
3947 stepping_list.aexpr_listsize = 128;
3948 stepping_list.aexpr_list = xmalloc
3949 (stepping_list.aexpr_listsize * sizeof (struct agent_expr *));
3950 }
3951
3952 add_info ("scope", scope_info,
3953 _("List the variables local to a scope"));
3954
3955 add_cmd ("tracepoints", class_trace, NULL,
3956 _("Tracing of program execution without stopping the program."),
3957 &cmdlist);
3958
3959 add_com ("tdump", class_trace, trace_dump_command,
3960 _("Print everything collected at the current tracepoint."));
3961
3962 add_com ("tsave", class_trace, trace_save_command, _("\
3963 Save the trace data to a file.\n\
3964 Use the '-r' option to direct the target to save directly to the file,\n\
3965 using its own filesystem."));
3966
3967 c = add_com ("tvariable", class_trace, trace_variable_command,_("\
3968 Define a trace state variable.\n\
3969 Argument is a $-prefixed name, optionally followed\n\
3970 by '=' and an expression that sets the initial value\n\
3971 at the start of tracing."));
3972 set_cmd_completer (c, expression_completer);
3973
3974 add_cmd ("tvariable", class_trace, delete_trace_variable_command, _("\
3975 Delete one or more trace state variables.\n\
3976 Arguments are the names of the variables to delete.\n\
3977 If no arguments are supplied, delete all variables."), &deletelist);
3978 /* FIXME add a trace variable completer */
3979
3980 add_info ("tvariables", tvariables_info, _("\
3981 Status of trace state variables and their values.\n\
3982 "));
3983
3984 add_prefix_cmd ("tfind", class_trace, trace_find_command, _("\
3985 Select a trace frame;\n\
3986 No argument means forward by one frame; '-' means backward by one frame."),
3987 &tfindlist, "tfind ", 1, &cmdlist);
3988
3989 add_cmd ("outside", class_trace, trace_find_outside_command, _("\
3990 Select a trace frame whose PC is outside the given range (exclusive).\n\
3991 Usage: tfind outside addr1, addr2"),
3992 &tfindlist);
3993
3994 add_cmd ("range", class_trace, trace_find_range_command, _("\
3995 Select a trace frame whose PC is in the given range (inclusive).\n\
3996 Usage: tfind range addr1,addr2"),
3997 &tfindlist);
3998
3999 add_cmd ("line", class_trace, trace_find_line_command, _("\
4000 Select a trace frame by source line.\n\
4001 Argument can be a line number (with optional source file), \n\
4002 a function name, or '*' followed by an address.\n\
4003 Default argument is 'the next source line that was traced'."),
4004 &tfindlist);
4005
4006 add_cmd ("tracepoint", class_trace, trace_find_tracepoint_command, _("\
4007 Select a trace frame by tracepoint number.\n\
4008 Default is the tracepoint for the current trace frame."),
4009 &tfindlist);
4010
4011 add_cmd ("pc", class_trace, trace_find_pc_command, _("\
4012 Select a trace frame by PC.\n\
4013 Default is the current PC, or the PC of the current trace frame."),
4014 &tfindlist);
4015
4016 add_cmd ("end", class_trace, trace_find_end_command, _("\
4017 Synonym for 'none'.\n\
4018 De-select any trace frame and resume 'live' debugging."),
4019 &tfindlist);
4020
4021 add_cmd ("none", class_trace, trace_find_none_command,
4022 _("De-select any trace frame and resume 'live' debugging."),
4023 &tfindlist);
4024
4025 add_cmd ("start", class_trace, trace_find_start_command,
4026 _("Select the first trace frame in the trace buffer."),
4027 &tfindlist);
4028
4029 add_com ("tstatus", class_trace, trace_status_command,
4030 _("Display the status of the current trace data collection."));
4031
4032 add_com ("tstop", class_trace, trace_stop_command,
4033 _("Stop trace data collection."));
4034
4035 add_com ("tstart", class_trace, trace_start_command,
4036 _("Start trace data collection."));
4037
4038 add_com ("end", class_trace, end_actions_pseudocommand, _("\
4039 Ends a list of commands or actions.\n\
4040 Several GDB commands allow you to enter a list of commands or actions.\n\
4041 Entering \"end\" on a line by itself is the normal way to terminate\n\
4042 such a list.\n\n\
4043 Note: the \"end\" command cannot be used at the gdb prompt."));
4044
4045 add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
4046 Specify single-stepping behavior at a tracepoint.\n\
4047 Argument is number of instructions to trace in single-step mode\n\
4048 following the tracepoint. This command is normally followed by\n\
4049 one or more \"collect\" commands, to specify what to collect\n\
4050 while single-stepping.\n\n\
4051 Note: this command can only be used in a tracepoint \"actions\" list."));
4052
4053 add_com_alias ("ws", "while-stepping", class_alias, 0);
4054 add_com_alias ("stepping", "while-stepping", class_alias, 0);
4055
4056 add_com ("collect", class_trace, collect_pseudocommand, _("\
4057 Specify one or more data items to be collected at a tracepoint.\n\
4058 Accepts a comma-separated list of (one or more) expressions. GDB will\n\
4059 collect all data (variables, registers) referenced by that expression.\n\
4060 Also accepts the following special arguments:\n\
4061 $regs -- all registers.\n\
4062 $args -- all function arguments.\n\
4063 $locals -- all variables local to the block/function scope.\n\
4064 Note: this command can only be used in a tracepoint \"actions\" list."));
4065
4066 add_com ("teval", class_trace, teval_pseudocommand, _("\
4067 Specify one or more expressions to be evaluated at a tracepoint.\n\
4068 Accepts a comma-separated list of (one or more) expressions.\n\
4069 The result of each evaluation will be discarded.\n\
4070 Note: this command can only be used in a tracepoint \"actions\" list."));
4071
4072 add_com ("actions", class_trace, trace_actions_command, _("\
4073 Specify the actions to be taken at a tracepoint.\n\
4074 Tracepoint actions may include collecting of specified data, \n\
4075 single-stepping, or enabling/disabling other tracepoints, \n\
4076 depending on target's capabilities."));
4077
4078 default_collect = xstrdup ("");
4079 add_setshow_string_cmd ("default-collect", class_trace,
4080 &default_collect, _("\
4081 Set the list of expressions to collect by default"), _("\
4082 Show the list of expressions to collect by default"), NULL,
4083 NULL, NULL,
4084 &setlist, &showlist);
4085
4086 add_setshow_boolean_cmd ("disconnected-tracing", no_class,
4087 &disconnected_tracing, _("\
4088 Set whether tracing continues after GDB disconnects."), _("\
4089 Show whether tracing continues after GDB disconnects."), _("\
4090 Use this to continue a tracing run even if GDB disconnects\n\
4091 or detaches from the target. You can reconnect later and look at\n\
4092 trace data collected in the meantime."),
4093 set_disconnected_tracing,
4094 NULL,
4095 &setlist,
4096 &showlist);
4097
4098 add_setshow_boolean_cmd ("circular-trace-buffer", no_class,
4099 &circular_trace_buffer, _("\
4100 Set target's use of circular trace buffer."), _("\
4101 Show target's use of circular trace buffer."), _("\
4102 Use this to make the trace buffer into a circular buffer,\n\
4103 which will discard traceframes (oldest first) instead of filling\n\
4104 up and stopping the trace run."),
4105 set_circular_trace_buffer,
4106 NULL,
4107 &setlist,
4108 &showlist);
4109
4110 init_tfile_ops ();
4111
4112 add_target (&tfile_ops);
4113 }