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