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