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