1 /* Data structures associated with tracepoints in GDB.
2    Copyright (C) 1997-2021 Free Software Foundation, Inc.
3 
4    This file is part of GDB.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18 
19 #if !defined (TRACEPOINT_H)
20 #define TRACEPOINT_H 1
21 
22 #include "breakpoint.h"
23 #include "memrange.h"
24 #include "gdbsupport/gdb_vecs.h"
25 
26 #include <vector>
27 #include <string>
28 
29 /* An object describing the contents of a traceframe.  */
30 
31 struct traceframe_info
32 {
33   /* Collected memory.  */
34   std::vector<mem_range> memory;
35 
36   /* Collected trace state variables.  */
37   std::vector<int> tvars;
38 };
39 
40 typedef std::unique_ptr<traceframe_info> traceframe_info_up;
41 
42 /* A trace state variable is a value managed by a target being
43    traced.  A trace state variable (or tsv for short) can be accessed
44    and assigned to by tracepoint actions and conditionals, but is not
45    part of the program being traced, and it doesn't have to be
46    collected.  Effectively the variables are scratch space for
47    tracepoints.  */
48 
49 struct trace_state_variable
50 {
trace_state_variabletrace_state_variable51   trace_state_variable (std::string &&name_, int number_)
52   : name (name_), number (number_)
53   {}
54 
55   /* The variable's name.  The user has to prefix with a dollar sign,
56      but we don't store that internally.  */
57   std::string name;
58 
59   /* An id number assigned by GDB, and transmitted to targets.  */
60   int number = 0;
61 
62   /* The initial value of a variable is a 64-bit signed integer.  */
63   LONGEST initial_value = 0;
64 
65   /* 1 if the value is known, else 0.  The value is known during a
66      trace run, or in tfind mode if the variable was collected into
67      the current trace frame.  */
68   int value_known = 0;
69 
70   /* The value of a variable is a 64-bit signed integer.  */
71   LONGEST value = 0;
72 
73   /* This is true for variables that are predefined and built into
74      the target.  */
75   int builtin = 0;
76  };
77 
78 /* The trace status encompasses various info about the general state
79    of the tracing run.  */
80 
81 enum trace_stop_reason
82   {
83     trace_stop_reason_unknown,
84     trace_never_run,
85     trace_stop_command,
86     trace_buffer_full,
87     trace_disconnected,
88     tracepoint_passcount,
89     tracepoint_error
90   };
91 
92 struct trace_status
93 {
94   /* If the status is coming from a file rather than a live target,
95      this points at the file's filename.  Otherwise, this is NULL.  */
96   const char *filename;
97 
98   /* This is true if the value of the running field is known.  */
99   int running_known;
100 
101   /* This is true when the trace experiment is actually running.  */
102   int running;
103 
104   enum trace_stop_reason stop_reason;
105 
106   /* If stop_reason is tracepoint_passcount or tracepoint_error, this
107      is the (on-target) number of the tracepoint which caused the
108      stop.  */
109   int stopping_tracepoint;
110 
111   /* If stop_reason is tstop_command or tracepoint_error, this is an
112      arbitrary string that may describe the reason for the stop in
113      more detail.  */
114 
115   char *stop_desc;
116 
117   /* Number of traceframes currently in the buffer.  */
118 
119   int traceframe_count;
120 
121   /* Number of traceframes created since start of run.  */
122 
123   int traceframes_created;
124 
125   /* Total size of the target's trace buffer.  */
126 
127   int buffer_size;
128 
129   /* Unused bytes left in the target's trace buffer.  */
130 
131   int buffer_free;
132 
133   /* 1 if the target will continue tracing after disconnection, else
134      0.  If the target does not report a value, assume 0.  */
135 
136   int disconnected_tracing;
137 
138   /* 1 if the target is using a circular trace buffer, else 0.  If the
139      target does not report a value, assume 0.  */
140 
141   int circular_buffer;
142 
143   /* The "name" of the person running the trace.  This is an
144      arbitrary string.  */
145 
146   char *user_name;
147 
148   /* "Notes" about the trace.  This is an arbitrary string not
149      interpreted by GDBserver in any special way.  */
150 
151   char *notes;
152 
153   /* The calendar times at which the trace run started and stopped,
154      both expressed in microseconds of Unix time.  */
155 
156   LONGEST start_time;
157   LONGEST stop_time;
158 };
159 
160 struct trace_status *current_trace_status (void);
161 
162 extern char *default_collect;
163 
164 extern int trace_regblock_size;
165 
166 extern const char *stop_reason_names[];
167 
168 /* Struct to collect random info about tracepoints on the target.  */
169 
170 struct uploaded_tp
171 {
172   int number = 0;
173   enum bptype type = bp_none;
174   ULONGEST addr = 0;
175   int enabled = 0;
176   int step = 0;
177   int pass = 0;
178   int orig_size = 0;
179 
180   /* String that is the encoded form of the tracepoint's condition.  */
181   gdb::unique_xmalloc_ptr<char[]> cond;
182 
183   /* Vectors of strings that are the encoded forms of a tracepoint's
184      actions.  */
185   std::vector<gdb::unique_xmalloc_ptr<char[]>> actions;
186   std::vector<gdb::unique_xmalloc_ptr<char[]>> step_actions;
187 
188   /* The original string defining the location of the tracepoint.  */
189   gdb::unique_xmalloc_ptr<char[]> at_string;
190 
191   /* The original string defining the tracepoint's condition.  */
192   gdb::unique_xmalloc_ptr<char[]> cond_string;
193 
194   /* List of original strings defining the tracepoint's actions.  */
195   std::vector<gdb::unique_xmalloc_ptr<char[]>> cmd_strings;
196 
197   /* The tracepoint's current hit count.  */
198   int hit_count = 0;
199 
200   /* The tracepoint's current traceframe usage.  */
201   ULONGEST traceframe_usage = 0;
202 
203   struct uploaded_tp *next = nullptr;
204 };
205 
206 /* Struct recording info about trace state variables on the target.  */
207 
208 struct uploaded_tsv
209 {
210   const char *name;
211   int number;
212   LONGEST initial_value;
213   int builtin;
214   struct uploaded_tsv *next;
215 };
216 
217 /* Struct recording info about a target static tracepoint marker.  */
218 
219 struct static_tracepoint_marker
220 {
221   DISABLE_COPY_AND_ASSIGN (static_tracepoint_marker);
222 
223   static_tracepoint_marker () = default;
224   static_tracepoint_marker (static_tracepoint_marker &&) = default;
225   static_tracepoint_marker &operator= (static_tracepoint_marker &&) = default;
226 
227   struct gdbarch *gdbarch = NULL;
228   CORE_ADDR address = 0;
229 
230   /* The string ID of the marker.  */
231   std::string str_id;
232 
233   /* Extra target reported info associated with the marker.  */
234   std::string extra;
235 };
236 
237 struct memrange
238 {
memrangememrange239   memrange (int type_, bfd_signed_vma start_, bfd_signed_vma end_)
240     : type (type_), start (start_), end (end_)
241   {}
242 
memrangememrange243   memrange ()
244   {}
245 
246   /* memrange_absolute for absolute memory range, else basereg
247      number.  */
248   int type;
249   bfd_signed_vma start;
250   bfd_signed_vma end;
251 };
252 
253 class collection_list
254 {
255 public:
256   collection_list ();
257 
258   void add_wholly_collected (const char *print_name);
259 
260   void append_exp (std::string &&exp);
261 
262   /* Add AEXPR to the list, taking ownership.  */
263   void add_aexpr (agent_expr_up aexpr);
264 
265   void add_remote_register (unsigned int regno);
266   void add_ax_registers (struct agent_expr *aexpr);
267   void add_local_register (struct gdbarch *gdbarch,
268 			   unsigned int regno,
269 			   CORE_ADDR scope);
270   void add_memrange (struct gdbarch *gdbarch,
271 		     int type, bfd_signed_vma base,
272 		     unsigned long len, CORE_ADDR scope);
273   void collect_symbol (struct symbol *sym,
274 		       struct gdbarch *gdbarch,
275 		       long frame_regno, long frame_offset,
276 		       CORE_ADDR scope,
277 		       int trace_string);
278 
279   void add_local_symbols (struct gdbarch *gdbarch, CORE_ADDR pc,
280 			  long frame_regno, long frame_offset, int type,
281 			  int trace_string);
282   void add_static_trace_data ();
283 
284   void finish ();
285 
286   std::vector<std::string> stringify ();
287 
wholly_collected()288   const std::vector<std::string> &wholly_collected ()
289   { return m_wholly_collected; }
290 
computed()291   const std::vector<std::string> &computed ()
292   { return m_computed; }
293 
294 private:
295   /* We need the allocator zero-initialize the mask, so we don't use
296      gdb::byte_vector.  */
297   std::vector<unsigned char> m_regs_mask;
298 
299   std::vector<memrange> m_memranges;
300 
301   std::vector<agent_expr_up> m_aexprs;
302 
303   /* True is the user requested a collection of "$_sdata", "static
304      tracepoint data".  */
305   bool m_strace_data;
306 
307   /* A set of names of wholly collected objects.  */
308   std::vector<std::string> m_wholly_collected;
309   /* A set of computed expressions.  */
310   std::vector<std::string> m_computed;
311 };
312 
313 extern void
314   parse_static_tracepoint_marker_definition (const char *line, const char **pp,
315 					     static_tracepoint_marker *marker);
316 
317 /* A hook used to notify the UI of tracepoint operations.  */
318 
319 extern void (*deprecated_trace_find_hook) (char *arg, int from_tty);
320 extern void (*deprecated_trace_start_stop_hook) (int start, int from_tty);
321 
322 /* Returns the current traceframe number.  */
323 extern int get_traceframe_number (void);
324 
325 /* Returns the tracepoint number for current traceframe.  */
326 extern int get_tracepoint_number (void);
327 
328 /* Make the traceframe NUM be the current trace frame, all the way to
329    the target, and flushes all global state (register/frame caches,
330    etc.).  */
331 extern void set_current_traceframe (int num);
332 
333 struct scoped_restore_current_traceframe
334 {
335   scoped_restore_current_traceframe ();
336 
~scoped_restore_current_traceframescoped_restore_current_traceframe337   ~scoped_restore_current_traceframe ()
338   {
339     set_current_traceframe (m_traceframe_number);
340   }
341 
342   DISABLE_COPY_AND_ASSIGN (scoped_restore_current_traceframe);
343 
344 private:
345 
346   /* The traceframe we were inspecting.  */
347   int m_traceframe_number;
348 };
349 
350 void free_actions (struct breakpoint *);
351 
352 extern const char *decode_agent_options (const char *exp, int *trace_string);
353 
354 extern void encode_actions (struct bp_location *tloc,
355 			    struct collection_list *tracepoint_list,
356 			    struct collection_list *stepping_list);
357 
358 extern void encode_actions_rsp (struct bp_location *tloc,
359 				std::vector<std::string> *tdp_actions,
360 				std::vector<std::string> *stepping_actions);
361 
362 extern void validate_actionline (const char *, struct breakpoint *);
363 extern void validate_trace_state_variable_name (const char *name);
364 
365 extern struct trace_state_variable *find_trace_state_variable (const char *name);
366 extern struct trace_state_variable *
367   find_trace_state_variable_by_number (int number);
368 
369 extern struct trace_state_variable *create_trace_state_variable (const char *name);
370 
371 extern int encode_source_string (int num, ULONGEST addr,
372 				 const char *srctype, const char *src,
373 				 char *buf, int buf_size);
374 
375 extern void parse_trace_status (const char *line, struct trace_status *ts);
376 
377 extern void parse_tracepoint_status (const char *p, struct breakpoint *tp,
378 				     struct uploaded_tp *utp);
379 
380 extern void parse_tracepoint_definition (const char *line,
381 					 struct uploaded_tp **utpp);
382 extern void parse_tsv_definition (const char *line, struct uploaded_tsv **utsvp);
383 
384 extern struct uploaded_tp *get_uploaded_tp (int num, ULONGEST addr,
385 					    struct uploaded_tp **utpp);
386 extern void free_uploaded_tps (struct uploaded_tp **utpp);
387 
388 extern struct uploaded_tsv *get_uploaded_tsv (int num,
389 					      struct uploaded_tsv **utsvp);
390 extern void free_uploaded_tsvs (struct uploaded_tsv **utsvp);
391 extern struct tracepoint *create_tracepoint_from_upload (struct uploaded_tp *utp);
392 extern void merge_uploaded_tracepoints (struct uploaded_tp **utpp);
393 extern void merge_uploaded_trace_state_variables (struct uploaded_tsv **utsvp);
394 
395 extern void query_if_trace_running (int from_tty);
396 extern void disconnect_tracing (void);
397 extern void trace_reset_local_state (void);
398 
399 extern void check_trace_running (struct trace_status *);
400 
401 extern void start_tracing (const char *notes);
402 extern void stop_tracing (const char *notes);
403 
404 extern void trace_status_mi (int on_stop);
405 
406 extern void tvariables_info_1 (void);
407 extern void save_trace_state_variables (struct ui_file *fp);
408 
409 /* Enumeration of the kinds of traceframe searches that a target may
410    be able to perform.  */
411 
412 enum trace_find_type
413 {
414   tfind_number,
415   tfind_pc,
416   tfind_tp,
417   tfind_range,
418   tfind_outside,
419 };
420 
421 extern void tfind_1 (enum trace_find_type type, int num,
422 		     CORE_ADDR addr1, CORE_ADDR addr2,
423 		     int from_tty);
424 
425 extern void trace_save_tfile (const char *filename,
426 			      int target_does_save);
427 extern void trace_save_ctf (const char *dirname,
428 			    int target_does_save);
429 
430 extern traceframe_info_up parse_traceframe_info (const char *tframe_info);
431 
432 extern int traceframe_available_memory (std::vector<mem_range> *result,
433 					CORE_ADDR memaddr, ULONGEST len);
434 
435 extern struct traceframe_info *get_traceframe_info (void);
436 
437 extern struct bp_location *get_traceframe_location (int *stepping_frame_p);
438 
439 /* Command element for the 'while-stepping' command.  */
440 extern cmd_list_element *while_stepping_cmd_element;
441 
442 #endif	/* TRACEPOINT_H */
443