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