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