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