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