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