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