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