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