gdb/gdbserver/
[deliverable/binutils-gdb.git] / gdb / gdbserver / tracepoint.c
1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2012 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 #include "server.h"
20 #include "agent.h"
21
22 #include <ctype.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/time.h>
26 #include <stddef.h>
27 #include <inttypes.h>
28 #include <stdint.h>
29
30 #include "ax.h"
31
32 /* This file is built for both GDBserver, and the in-process
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
41
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
49
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
55 at appropriate times.
56
57 */
58
59 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf, 1, 2);
60
61 static void
62 trace_vdebug (const char *fmt, ...)
63 {
64 char buf[1024];
65 va_list ap;
66
67 va_start (ap, fmt);
68 vsprintf (buf, fmt, ap);
69 fprintf (stderr, PROG "/tracepoint: %s\n", buf);
70 va_end (ap);
71 }
72
73 #define trace_debug_1(level, fmt, args...) \
74 do { \
75 if (level <= debug_threads) \
76 trace_vdebug ((fmt), ##args); \
77 } while (0)
78
79 #define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
81
82 #if defined(__GNUC__)
83 # define ATTR_USED __attribute__((used))
84 # define ATTR_NOINLINE __attribute__((noinline))
85 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
86 #else
87 # define ATTR_USED
88 # define ATTR_NOINLINE
89 # define ATTR_CONSTRUCTOR
90 #endif
91
92 /* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
94
95 #ifdef IN_PROCESS_AGENT
96 # if defined _WIN32 || defined __CYGWIN__
97 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
98 # else
99 # if __GNUC__ >= 4
100 # define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
102 # else
103 # define IP_AGENT_EXPORT ATTR_USED
104 # endif
105 # endif
106 #else
107 # define IP_AGENT_EXPORT
108 #endif
109
110 /* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112 #ifdef IN_PROCESS_AGENT
113 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
116 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
117 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
118 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
119 # define collecting gdb_agent_collecting
120 # define gdb_collect gdb_agent_gdb_collect
121 # define stop_tracing gdb_agent_stop_tracing
122 # define flush_trace_buffer gdb_agent_flush_trace_buffer
123 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
124 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
125 # define stopping_tracepoint gdb_agent_stopping_tracepoint
126 # define expr_eval_result gdb_agent_expr_eval_result
127 # define error_tracepoint gdb_agent_error_tracepoint
128 # define tracepoints gdb_agent_tracepoints
129 # define tracing gdb_agent_tracing
130 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
131 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
132 # define trace_buffer_lo gdb_agent_trace_buffer_lo
133 # define trace_buffer_hi gdb_agent_trace_buffer_hi
134 # define traceframe_read_count gdb_agent_traceframe_read_count
135 # define traceframe_write_count gdb_agent_traceframe_write_count
136 # define traceframes_created gdb_agent_traceframes_created
137 # define trace_state_variables gdb_agent_trace_state_variables
138 # define get_raw_reg gdb_agent_get_raw_reg
139 # define get_trace_state_variable_value \
140 gdb_agent_get_trace_state_variable_value
141 # define set_trace_state_variable_value \
142 gdb_agent_set_trace_state_variable_value
143 # define ust_loaded gdb_agent_ust_loaded
144 # define helper_thread_id gdb_agent_helper_thread_id
145 # define cmd_buf gdb_agent_cmd_buf
146 #endif
147
148 #ifndef IN_PROCESS_AGENT
149
150 /* Addresses of in-process agent's symbols GDBserver cares about. */
151
152 struct ipa_sym_addresses
153 {
154 CORE_ADDR addr_gdb_tp_heap_buffer;
155 CORE_ADDR addr_gdb_jump_pad_buffer;
156 CORE_ADDR addr_gdb_jump_pad_buffer_end;
157 CORE_ADDR addr_gdb_trampoline_buffer;
158 CORE_ADDR addr_gdb_trampoline_buffer_end;
159 CORE_ADDR addr_gdb_trampoline_buffer_error;
160 CORE_ADDR addr_collecting;
161 CORE_ADDR addr_gdb_collect;
162 CORE_ADDR addr_stop_tracing;
163 CORE_ADDR addr_flush_trace_buffer;
164 CORE_ADDR addr_about_to_request_buffer_space;
165 CORE_ADDR addr_trace_buffer_is_full;
166 CORE_ADDR addr_stopping_tracepoint;
167 CORE_ADDR addr_expr_eval_result;
168 CORE_ADDR addr_error_tracepoint;
169 CORE_ADDR addr_tracepoints;
170 CORE_ADDR addr_tracing;
171 CORE_ADDR addr_trace_buffer_ctrl;
172 CORE_ADDR addr_trace_buffer_ctrl_curr;
173 CORE_ADDR addr_trace_buffer_lo;
174 CORE_ADDR addr_trace_buffer_hi;
175 CORE_ADDR addr_traceframe_read_count;
176 CORE_ADDR addr_traceframe_write_count;
177 CORE_ADDR addr_traceframes_created;
178 CORE_ADDR addr_trace_state_variables;
179 CORE_ADDR addr_get_raw_reg;
180 CORE_ADDR addr_get_trace_state_variable_value;
181 CORE_ADDR addr_set_trace_state_variable_value;
182 CORE_ADDR addr_ust_loaded;
183 };
184
185 static struct
186 {
187 const char *name;
188 int offset;
189 int required;
190 } symbol_list[] = {
191 IPA_SYM(gdb_tp_heap_buffer),
192 IPA_SYM(gdb_jump_pad_buffer),
193 IPA_SYM(gdb_jump_pad_buffer_end),
194 IPA_SYM(gdb_trampoline_buffer),
195 IPA_SYM(gdb_trampoline_buffer_end),
196 IPA_SYM(gdb_trampoline_buffer_error),
197 IPA_SYM(collecting),
198 IPA_SYM(gdb_collect),
199 IPA_SYM(stop_tracing),
200 IPA_SYM(flush_trace_buffer),
201 IPA_SYM(about_to_request_buffer_space),
202 IPA_SYM(trace_buffer_is_full),
203 IPA_SYM(stopping_tracepoint),
204 IPA_SYM(expr_eval_result),
205 IPA_SYM(error_tracepoint),
206 IPA_SYM(tracepoints),
207 IPA_SYM(tracing),
208 IPA_SYM(trace_buffer_ctrl),
209 IPA_SYM(trace_buffer_ctrl_curr),
210 IPA_SYM(trace_buffer_lo),
211 IPA_SYM(trace_buffer_hi),
212 IPA_SYM(traceframe_read_count),
213 IPA_SYM(traceframe_write_count),
214 IPA_SYM(traceframes_created),
215 IPA_SYM(trace_state_variables),
216 IPA_SYM(get_raw_reg),
217 IPA_SYM(get_trace_state_variable_value),
218 IPA_SYM(set_trace_state_variable_value),
219 IPA_SYM(ust_loaded),
220 };
221
222 static struct ipa_sym_addresses ipa_sym_addrs;
223
224 static int read_inferior_integer (CORE_ADDR symaddr, int *val);
225
226 /* Returns true if both the in-process agent library and the static
227 tracepoints libraries are loaded in the inferior, and agent has
228 capability on static tracepoints. */
229
230 static int
231 in_process_agent_supports_ust (void)
232 {
233 int loaded = 0;
234
235 if (!agent_loaded_p ())
236 {
237 warning ("In-process agent not loaded");
238 return 0;
239 }
240
241 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE))
242 {
243 /* Agent understands static tracepoint, then check whether UST is in
244 fact loaded in the inferior. */
245 if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
246 {
247 warning ("Error reading ust_loaded in lib");
248 return 0;
249 }
250
251 return loaded;
252 }
253 else
254 return 0;
255 }
256
257 static void
258 write_e_ipa_not_loaded (char *buffer)
259 {
260 sprintf (buffer,
261 "E.In-process agent library not loaded in process. "
262 "Fast and static tracepoints unavailable.");
263 }
264
265 /* Write an error to BUFFER indicating that UST isn't loaded in the
266 inferior. */
267
268 static void
269 write_e_ust_not_loaded (char *buffer)
270 {
271 #ifdef HAVE_UST
272 sprintf (buffer,
273 "E.UST library not loaded in process. "
274 "Static tracepoints unavailable.");
275 #else
276 sprintf (buffer, "E.GDBserver was built without static tracepoints support");
277 #endif
278 }
279
280 /* If the in-process agent library isn't loaded in the inferior, write
281 an error to BUFFER, and return 1. Otherwise, return 0. */
282
283 static int
284 maybe_write_ipa_not_loaded (char *buffer)
285 {
286 if (!agent_loaded_p ())
287 {
288 write_e_ipa_not_loaded (buffer);
289 return 1;
290 }
291 return 0;
292 }
293
294 /* If the in-process agent library and the ust (static tracepoints)
295 library aren't loaded in the inferior, write an error to BUFFER,
296 and return 1. Otherwise, return 0. */
297
298 static int
299 maybe_write_ipa_ust_not_loaded (char *buffer)
300 {
301 if (!agent_loaded_p ())
302 {
303 write_e_ipa_not_loaded (buffer);
304 return 1;
305 }
306 else if (!in_process_agent_supports_ust ())
307 {
308 write_e_ust_not_loaded (buffer);
309 return 1;
310 }
311 return 0;
312 }
313
314 /* Cache all future symbols that the tracepoints module might request.
315 We can not request symbols at arbitrary states in the remote
316 protocol, only when the client tells us that new symbols are
317 available. So when we load the in-process library, make sure to
318 check the entire list. */
319
320 void
321 tracepoint_look_up_symbols (void)
322 {
323 int i;
324
325 if (agent_loaded_p ())
326 return;
327
328 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
329 {
330 CORE_ADDR *addrp =
331 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
332
333 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
334 {
335 if (debug_threads)
336 fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
337 return;
338 }
339 }
340
341 agent_look_up_symbols (NULL);
342 }
343
344 #endif
345
346 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
347 of the "stop_tracing" function. When this breakpoint is hit,
348 tracing stopped in the IPA for some reason. E.g., due to
349 tracepoint reaching the pass count, hitting conditional expression
350 evaluation error, etc.
351
352 The IPA's trace buffer is never in circular tracing mode: instead,
353 GDBserver's is, and whenever the in-process buffer fills, it calls
354 "flush_trace_buffer", which triggers an internal breakpoint.
355 GDBserver reacts to this breakpoint by pulling the meanwhile
356 collected data. Old frames discarding is always handled on the
357 GDBserver side. */
358
359 #ifdef IN_PROCESS_AGENT
360 int
361 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
362 {
363 memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
364 return 0;
365 }
366
367 /* Call this in the functions where GDBserver places a breakpoint, so
368 that the compiler doesn't try to be clever and skip calling the
369 function at all. This is necessary, even if we tell the compiler
370 to not inline said functions. */
371
372 #if defined(__GNUC__)
373 # define UNKNOWN_SIDE_EFFECTS() asm ("")
374 #else
375 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
376 #endif
377
378 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
379 stop_tracing (void)
380 {
381 /* GDBserver places breakpoint here. */
382 UNKNOWN_SIDE_EFFECTS();
383 }
384
385 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
386 flush_trace_buffer (void)
387 {
388 /* GDBserver places breakpoint here. */
389 UNKNOWN_SIDE_EFFECTS();
390 }
391
392 #endif
393
394 #ifndef IN_PROCESS_AGENT
395 static int
396 tracepoint_handler (CORE_ADDR address)
397 {
398 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
399 paddress (address));
400 return 0;
401 }
402
403 /* Breakpoint at "stop_tracing" in the inferior lib. */
404 struct breakpoint *stop_tracing_bkpt;
405 static int stop_tracing_handler (CORE_ADDR);
406
407 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
408 struct breakpoint *flush_trace_buffer_bkpt;
409 static int flush_trace_buffer_handler (CORE_ADDR);
410
411 static void download_trace_state_variables (void);
412 static void upload_fast_traceframes (void);
413
414 static int run_inferior_command (char *cmd);
415
416 static int
417 read_inferior_integer (CORE_ADDR symaddr, int *val)
418 {
419 return read_inferior_memory (symaddr, (unsigned char *) val,
420 sizeof (*val));
421 }
422
423 static int
424 read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
425 {
426 return read_inferior_memory (symaddr, (unsigned char *) val,
427 sizeof (*val));
428 }
429
430 static int
431 read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
432 {
433 void *pval = (void *) (uintptr_t) val;
434 int ret;
435
436 ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
437 *val = (uintptr_t) pval;
438 return ret;
439 }
440
441 static int
442 write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
443 {
444 void *pval = (void *) (uintptr_t) val;
445 return write_inferior_memory (symaddr,
446 (unsigned char *) &pval, sizeof (pval));
447 }
448
449 static int
450 write_inferior_integer (CORE_ADDR symaddr, int val)
451 {
452 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
453 }
454
455 static int
456 write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
457 {
458 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
459 }
460
461 static CORE_ADDR target_malloc (ULONGEST size);
462 static int write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr);
463 #endif
464
465 /* Operations on various types of tracepoint actions. */
466
467 struct tracepoint_action;
468
469 struct tracepoint_action_ops
470 {
471 /* Download tracepoint action ACTION to IPA. Return the address of action
472 in IPA/inferior. */
473 CORE_ADDR (*download) (const struct tracepoint_action *action);
474 };
475
476 /* Base action. Concrete actions inherit this. */
477
478 struct tracepoint_action
479 {
480 #ifndef IN_PROCESS_AGENT
481 const struct tracepoint_action_ops *ops;
482 #endif
483 char type;
484 };
485
486 /* An 'M' (collect memory) action. */
487 struct collect_memory_action
488 {
489 struct tracepoint_action base;
490
491 ULONGEST addr;
492 ULONGEST len;
493 int32_t basereg;
494 };
495
496 /* An 'R' (collect registers) action. */
497
498 struct collect_registers_action
499 {
500 struct tracepoint_action base;
501 };
502
503 /* An 'X' (evaluate expression) action. */
504
505 struct eval_expr_action
506 {
507 struct tracepoint_action base;
508
509 struct agent_expr *expr;
510 };
511
512 /* An 'L' (collect static trace data) action. */
513 struct collect_static_trace_data_action
514 {
515 struct tracepoint_action base;
516 };
517
518 #ifndef IN_PROCESS_AGENT
519 static CORE_ADDR
520 m_tracepoint_action_download (const struct tracepoint_action *action)
521 {
522 int size_in_ipa = (sizeof (struct collect_memory_action)
523 - offsetof (struct tracepoint_action, type));
524 CORE_ADDR ipa_action = target_malloc (size_in_ipa);
525
526 write_inferior_memory (ipa_action, (unsigned char *) &action->type,
527 size_in_ipa);
528
529 return ipa_action;
530 }
531
532 static const struct tracepoint_action_ops m_tracepoint_action_ops =
533 {
534 m_tracepoint_action_download,
535 };
536
537 static CORE_ADDR
538 r_tracepoint_action_download (const struct tracepoint_action *action)
539 {
540 int size_in_ipa = (sizeof (struct collect_registers_action)
541 - offsetof (struct tracepoint_action, type));
542 CORE_ADDR ipa_action = target_malloc (size_in_ipa);
543
544 write_inferior_memory (ipa_action, (unsigned char *) &action->type,
545 size_in_ipa);
546
547 return ipa_action;
548 }
549
550 static const struct tracepoint_action_ops r_tracepoint_action_ops =
551 {
552 r_tracepoint_action_download,
553 };
554
555 static CORE_ADDR download_agent_expr (struct agent_expr *expr);
556
557 static CORE_ADDR
558 x_tracepoint_action_download (const struct tracepoint_action *action)
559 {
560 int size_in_ipa = (sizeof (struct eval_expr_action)
561 - offsetof (struct tracepoint_action, type));
562 CORE_ADDR ipa_action = target_malloc (size_in_ipa);
563 CORE_ADDR expr;
564
565 write_inferior_memory (ipa_action, (unsigned char *) &action->type,
566 size_in_ipa);
567 expr = download_agent_expr (((struct eval_expr_action *)action)->expr);
568 write_inferior_data_ptr (ipa_action + offsetof (struct eval_expr_action, expr)
569 - offsetof (struct tracepoint_action, type),
570 expr);
571
572 return ipa_action;
573 }
574
575 static const struct tracepoint_action_ops x_tracepoint_action_ops =
576 {
577 x_tracepoint_action_download,
578 };
579
580 static CORE_ADDR
581 l_tracepoint_action_download (const struct tracepoint_action *action)
582 {
583 int size_in_ipa = (sizeof (struct collect_static_trace_data_action)
584 - offsetof (struct tracepoint_action, type));
585 CORE_ADDR ipa_action = target_malloc (size_in_ipa);
586
587 write_inferior_memory (ipa_action, (unsigned char *) &action->type,
588 size_in_ipa);
589
590 return ipa_action;
591 }
592
593 static const struct tracepoint_action_ops l_tracepoint_action_ops =
594 {
595 l_tracepoint_action_download,
596 };
597 #endif
598
599 /* This structure describes a piece of the source-level definition of
600 the tracepoint. The contents are not interpreted by the target,
601 but preserved verbatim for uploading upon reconnection. */
602
603 struct source_string
604 {
605 /* The type of string, such as "cond" for a conditional. */
606 char *type;
607
608 /* The source-level string itself. For the sake of target
609 debugging, we store it in plaintext, even though it is always
610 transmitted in hex. */
611 char *str;
612
613 /* Link to the next one in the list. We link them in the order
614 received, in case some make up an ordered list of commands or
615 some such. */
616 struct source_string *next;
617 };
618
619 enum tracepoint_type
620 {
621 /* Trap based tracepoint. */
622 trap_tracepoint,
623
624 /* A fast tracepoint implemented with a jump instead of a trap. */
625 fast_tracepoint,
626
627 /* A static tracepoint, implemented by a program call into a tracing
628 library. */
629 static_tracepoint
630 };
631
632 struct tracepoint_hit_ctx;
633
634 typedef enum eval_result_type (*condfn) (struct tracepoint_hit_ctx *,
635 ULONGEST *);
636
637 /* The definition of a tracepoint. */
638
639 /* Tracepoints may have multiple locations, each at a different
640 address. This can occur with optimizations, template
641 instantiation, etc. Since the locations may be in different
642 scopes, the conditions and actions may be different for each
643 location. Our target version of tracepoints is more like GDB's
644 notion of "breakpoint locations", but we have almost nothing that
645 is not per-location, so we bother having two kinds of objects. The
646 key consequence is that numbers are not unique, and that it takes
647 both number and address to identify a tracepoint uniquely. */
648
649 struct tracepoint
650 {
651 /* The number of the tracepoint, as specified by GDB. Several
652 tracepoint objects here may share a number. */
653 uint32_t number;
654
655 /* Address at which the tracepoint is supposed to trigger. Several
656 tracepoints may share an address. */
657 CORE_ADDR address;
658
659 /* Tracepoint type. */
660 enum tracepoint_type type;
661
662 /* True if the tracepoint is currently enabled. */
663 int8_t enabled;
664
665 /* The number of single steps that will be performed after each
666 tracepoint hit. */
667 uint64_t step_count;
668
669 /* The number of times the tracepoint may be hit before it will
670 terminate the entire tracing run. */
671 uint64_t pass_count;
672
673 /* Pointer to the agent expression that is the tracepoint's
674 conditional, or NULL if the tracepoint is unconditional. */
675 struct agent_expr *cond;
676
677 /* The list of actions to take when the tracepoint triggers. */
678 uint32_t numactions;
679 struct tracepoint_action **actions;
680
681 /* Count of the times we've hit this tracepoint during the run.
682 Note that while-stepping steps are not counted as "hits". */
683 uint64_t hit_count;
684
685 /* Cached sum of the sizes of traceframes created by this point. */
686 uint64_t traceframe_usage;
687
688 CORE_ADDR compiled_cond;
689
690 /* Link to the next tracepoint in the list. */
691 struct tracepoint *next;
692
693 #ifndef IN_PROCESS_AGENT
694 /* The list of actions to take when the tracepoint triggers, in
695 string/packet form. */
696 char **actions_str;
697
698 /* The collection of strings that describe the tracepoint as it was
699 entered into GDB. These are not used by the target, but are
700 reported back to GDB upon reconnection. */
701 struct source_string *source_strings;
702
703 /* The number of bytes displaced by fast tracepoints. It may subsume
704 multiple instructions, for multi-byte fast tracepoints. This
705 field is only valid for fast tracepoints. */
706 uint32_t orig_size;
707
708 /* Only for fast tracepoints. */
709 CORE_ADDR obj_addr_on_target;
710
711 /* Address range where the original instruction under a fast
712 tracepoint was relocated to. (_end is actually one byte past
713 the end). */
714 CORE_ADDR adjusted_insn_addr;
715 CORE_ADDR adjusted_insn_addr_end;
716
717 /* The address range of the piece of the jump pad buffer that was
718 assigned to this fast tracepoint. (_end is actually one byte
719 past the end).*/
720 CORE_ADDR jump_pad;
721 CORE_ADDR jump_pad_end;
722
723 /* The address range of the piece of the trampoline buffer that was
724 assigned to this fast tracepoint. (_end is actually one byte
725 past the end). */
726 CORE_ADDR trampoline;
727 CORE_ADDR trampoline_end;
728
729 /* The list of actions to take while in a stepping loop. These
730 fields are only valid for patch-based tracepoints. */
731 int num_step_actions;
732 struct tracepoint_action **step_actions;
733 /* Same, but in string/packet form. */
734 char **step_actions_str;
735
736 /* Handle returned by the breakpoint or tracepoint module when we
737 inserted the trap or jump, or hooked into a static tracepoint.
738 NULL if we haven't inserted it yet. */
739 void *handle;
740 #endif
741
742 };
743
744 #ifndef IN_PROCESS_AGENT
745
746 /* Given `while-stepping', a thread may be collecting data for more
747 than one tracepoint simultaneously. On the other hand, the same
748 tracepoint with a while-stepping action may be hit by more than one
749 thread simultaneously (but not quite, each thread could be handling
750 a different step). Each thread holds a list of these objects,
751 representing the current step of each while-stepping action being
752 collected. */
753
754 struct wstep_state
755 {
756 struct wstep_state *next;
757
758 /* The tracepoint number. */
759 int tp_number;
760 /* The tracepoint's address. */
761 CORE_ADDR tp_address;
762
763 /* The number of the current step in this 'while-stepping'
764 action. */
765 long current_step;
766 };
767
768 #endif
769
770 /* The linked list of all tracepoints. Marked explicitly as used as
771 the in-process library doesn't use it for the fast tracepoints
772 support. */
773 IP_AGENT_EXPORT struct tracepoint *tracepoints ATTR_USED;
774
775 #ifndef IN_PROCESS_AGENT
776
777 /* Pointer to the last tracepoint in the list, new tracepoints are
778 linked in at the end. */
779
780 static struct tracepoint *last_tracepoint;
781 #endif
782
783 /* The first tracepoint to exceed its pass count. */
784
785 IP_AGENT_EXPORT struct tracepoint *stopping_tracepoint;
786
787 /* True if the trace buffer is full or otherwise no longer usable. */
788
789 IP_AGENT_EXPORT int trace_buffer_is_full;
790
791 static enum eval_result_type expr_eval_result = expr_eval_no_error;
792
793 #ifndef IN_PROCESS_AGENT
794
795 static const char *eval_result_names[] =
796 {
797 "terror:in the attic", /* this should never be reported */
798 "terror:empty expression",
799 "terror:empty stack",
800 "terror:stack overflow",
801 "terror:stack underflow",
802 "terror:unhandled opcode",
803 "terror:unrecognized opcode",
804 "terror:divide by zero"
805 };
806
807 #endif
808
809 /* The tracepoint in which the error occurred. */
810
811 static struct tracepoint *error_tracepoint;
812
813 struct trace_state_variable
814 {
815 /* This is the name of the variable as used in GDB. The target
816 doesn't use the name, but needs to have it for saving and
817 reconnection purposes. */
818 char *name;
819
820 /* This number identifies the variable uniquely. Numbers may be
821 assigned either by the target (in the case of builtin variables),
822 or by GDB, and are presumed unique during the course of a trace
823 experiment. */
824 int number;
825
826 /* The variable's initial value, a 64-bit signed integer always. */
827 LONGEST initial_value;
828
829 /* The variable's value, a 64-bit signed integer always. */
830 LONGEST value;
831
832 /* Pointer to a getter function, used to supply computed values. */
833 LONGEST (*getter) (void);
834
835 /* Link to the next variable. */
836 struct trace_state_variable *next;
837 };
838
839 /* Linked list of all trace state variables. */
840
841 #ifdef IN_PROCESS_AGENT
842 struct trace_state_variable *alloced_trace_state_variables;
843 #endif
844
845 IP_AGENT_EXPORT struct trace_state_variable *trace_state_variables;
846
847 /* The results of tracing go into a fixed-size space known as the
848 "trace buffer". Because usage follows a limited number of
849 patterns, we manage it ourselves rather than with malloc. Basic
850 rules are that we create only one trace frame at a time, each is
851 variable in size, they are never moved once created, and we only
852 discard if we are doing a circular buffer, and then only the oldest
853 ones. Each trace frame includes its own size, so we don't need to
854 link them together, and the trace frame number is relative to the
855 first one, so we don't need to record numbers. A trace frame also
856 records the number of the tracepoint that created it. The data
857 itself is a series of blocks, each introduced by a single character
858 and with a defined format. Each type of block has enough
859 type/length info to allow scanners to jump quickly from one block
860 to the next without reading each byte in the block. */
861
862 /* Trace buffer management would be simple - advance a free pointer
863 from beginning to end, then stop - were it not for the circular
864 buffer option, which is a useful way to prevent a trace run from
865 stopping prematurely because the buffer filled up. In the circular
866 case, the location of the first trace frame (trace_buffer_start)
867 moves as old trace frames are discarded. Also, since we grow trace
868 frames incrementally as actions are performed, we wrap around to
869 the beginning of the trace buffer. This is per-block, so each
870 block within a trace frame remains contiguous. Things get messy
871 when the wrapped-around trace frame is the one being discarded; the
872 free space ends up in two parts at opposite ends of the buffer. */
873
874 #ifndef ATTR_PACKED
875 # if defined(__GNUC__)
876 # define ATTR_PACKED __attribute__ ((packed))
877 # else
878 # define ATTR_PACKED /* nothing */
879 # endif
880 #endif
881
882 /* The data collected at a tracepoint hit. This object should be as
883 small as possible, since there may be a great many of them. We do
884 not need to keep a frame number, because they are all sequential
885 and there are no deletions; so the Nth frame in the buffer is
886 always frame number N. */
887
888 struct traceframe
889 {
890 /* Number of the tracepoint that collected this traceframe. A value
891 of 0 indicates the current end of the trace buffer. We make this
892 a 16-bit field because it's never going to happen that GDB's
893 numbering of tracepoints reaches 32,000. */
894 int tpnum : 16;
895
896 /* The size of the data in this trace frame. We limit this to 32
897 bits, even on a 64-bit target, because it's just implausible that
898 one is validly going to collect 4 gigabytes of data at a single
899 tracepoint hit. */
900 unsigned int data_size : 32;
901
902 /* The base of the trace data, which is contiguous from this point. */
903 unsigned char data[0];
904
905 } ATTR_PACKED;
906
907 /* The traceframe to be used as the source of data to send back to
908 GDB. A value of -1 means to get data from the live program. */
909
910 int current_traceframe = -1;
911
912 /* This flag is true if the trace buffer is circular, meaning that
913 when it fills, the oldest trace frames are discarded in order to
914 make room. */
915
916 #ifndef IN_PROCESS_AGENT
917 static int circular_trace_buffer;
918 #endif
919
920 /* Pointer to the block of memory that traceframes all go into. */
921
922 static unsigned char *trace_buffer_lo;
923
924 /* Pointer to the end of the trace buffer, more precisely to the byte
925 after the end of the buffer. */
926
927 static unsigned char *trace_buffer_hi;
928
929 /* Control structure holding the read/write/etc. pointers into the
930 trace buffer. We need more than one of these to implement a
931 transaction-like mechanism to garantees that both GDBserver and the
932 in-process agent can try to change the trace buffer
933 simultaneously. */
934
935 struct trace_buffer_control
936 {
937 /* Pointer to the first trace frame in the buffer. In the
938 non-circular case, this is equal to trace_buffer_lo, otherwise it
939 moves around in the buffer. */
940 unsigned char *start;
941
942 /* Pointer to the free part of the trace buffer. Note that we clear
943 several bytes at and after this pointer, so that traceframe
944 scans/searches terminate properly. */
945 unsigned char *free;
946
947 /* Pointer to the byte after the end of the free part. Note that
948 this may be smaller than trace_buffer_free in the circular case,
949 and means that the free part is in two pieces. Initially it is
950 equal to trace_buffer_hi, then is generally equivalent to
951 trace_buffer_start. */
952 unsigned char *end_free;
953
954 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
955 this is the point at which the trace data breaks, and resumes at
956 trace_buffer_lo. */
957 unsigned char *wrap;
958 };
959
960 /* Same as above, to be used by GDBserver when updating the in-process
961 agent. */
962 struct ipa_trace_buffer_control
963 {
964 uintptr_t start;
965 uintptr_t free;
966 uintptr_t end_free;
967 uintptr_t wrap;
968 };
969
970
971 /* We have possibly both GDBserver and an inferior thread accessing
972 the same IPA trace buffer memory. The IPA is the producer (tries
973 to put new frames in the buffer), while GDBserver occasionally
974 consumes them, that is, flushes the IPA's buffer into its own
975 buffer. Both sides need to update the trace buffer control
976 pointers (current head, tail, etc.). We can't use a global lock to
977 synchronize the accesses, as otherwise we could deadlock GDBserver
978 (if the thread holding the lock stops for a signal, say). So
979 instead of that, we use a transaction scheme where GDBserver writes
980 always prevail over the IPAs writes, and, we have the IPA detect
981 the commit failure/overwrite, and retry the whole attempt. This is
982 mainly implemented by having a global token object that represents
983 who wrote last to the buffer control structure. We need to freeze
984 any inferior writing to the buffer while GDBserver touches memory,
985 so that the inferior can correctly detect that GDBserver had been
986 there, otherwise, it could mistakingly think its commit was
987 successful; that's implemented by simply having GDBserver set a
988 breakpoint the inferior hits if it is the critical region.
989
990 There are three cycling trace buffer control structure copies
991 (buffer head, tail, etc.), with the token object including an index
992 indicating which is current live copy. The IPA tentatively builds
993 an updated copy in a non-current control structure, while GDBserver
994 always clobbers the current version directly. The IPA then tries
995 to atomically "commit" its version; if GDBserver clobbered the
996 structure meanwhile, that will fail, and the IPA restarts the
997 allocation process.
998
999 Listing the step in further detail, we have:
1000
1001 In-process agent (producer):
1002
1003 - passes by `about_to_request_buffer_space' breakpoint/lock
1004
1005 - reads current token, extracts current trace buffer control index,
1006 and starts tentatively updating the rightmost one (0->1, 1->2,
1007 2->0). Note that only one inferior thread is executing this code
1008 at any given time, due to an outer lock in the jump pads.
1009
1010 - updates counters, and tries to commit the token.
1011
1012 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1013 leaving the sync region.
1014
1015 - checks if the update was effective.
1016
1017 - if trace buffer was found full, hits flush_trace_buffer
1018 breakpoint, and restarts later afterwards.
1019
1020 GDBserver (consumer):
1021
1022 - sets `about_to_request_buffer_space' breakpoint/lock.
1023
1024 - updates the token unconditionally, using the current buffer
1025 control index, since it knows that the IP agent always writes to
1026 the rightmost, and due to the breakpoint, at most one IP thread
1027 can try to update the trace buffer concurrently to GDBserver, so
1028 there will be no danger of trace buffer control index wrap making
1029 the IPA write to the same index as GDBserver.
1030
1031 - flushes the IP agent's trace buffer completely, and updates the
1032 current trace buffer control structure. GDBserver *always* wins.
1033
1034 - removes the `about_to_request_buffer_space' breakpoint.
1035
1036 The token is stored in the `trace_buffer_ctrl_curr' variable.
1037 Internally, it's bits are defined as:
1038
1039 |-------------+-----+-------------+--------+-------------+--------------|
1040 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1041 |-------------+-----+-------------+--------+-------------+--------------|
1042 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1043 |-------------+-----+-------------+--------+-------------+--------------|
1044
1045 GSB - GDBserver Stamp Bit
1046 PC - Previous Counter
1047 CC - Current Counter
1048 TBCI - Trace Buffer Control Index
1049
1050
1051 An IPA update of `trace_buffer_ctrl_curr' does:
1052
1053 - read CC from the current token, save as PC.
1054 - updates pointers
1055 - atomically tries to write PC+1,CC
1056
1057 A GDBserver update of `trace_buffer_ctrl_curr' does:
1058
1059 - reads PC and CC from the current token.
1060 - updates pointers
1061 - writes GSB,PC,CC
1062 */
1063
1064 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1065 for the counters described below. The cleared bits are used to
1066 hold the index of the items of the `trace_buffer_ctrl' array that
1067 is "current". */
1068 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1069
1070 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1071 counter, and the `current' counter. */
1072
1073 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1074 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1075
1076 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1077 always stamps this bit as set. */
1078 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1079
1080 #ifdef IN_PROCESS_AGENT
1081 IP_AGENT_EXPORT struct trace_buffer_control trace_buffer_ctrl[3];
1082 IP_AGENT_EXPORT unsigned int trace_buffer_ctrl_curr;
1083
1084 # define TRACE_BUFFER_CTRL_CURR \
1085 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1086
1087 #else
1088
1089 /* The GDBserver side agent only needs one instance of this object, as
1090 it doesn't need to sync with itself. Define it as array anyway so
1091 that the rest of the code base doesn't need to care for the
1092 difference. */
1093 struct trace_buffer_control trace_buffer_ctrl[1];
1094 # define TRACE_BUFFER_CTRL_CURR 0
1095 #endif
1096
1097 /* These are convenience macros used to access the current trace
1098 buffer control in effect. */
1099 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1100 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1101 #define trace_buffer_end_free \
1102 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1103 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1104
1105
1106 /* Macro that returns a pointer to the first traceframe in the buffer. */
1107
1108 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1109
1110 /* Macro that returns a pointer to the next traceframe in the buffer.
1111 If the computed location is beyond the wraparound point, subtract
1112 the offset of the wraparound. */
1113
1114 #define NEXT_TRACEFRAME_1(TF) \
1115 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1116
1117 #define NEXT_TRACEFRAME(TF) \
1118 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1119 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1120 ? (trace_buffer_wrap - trace_buffer_lo) \
1121 : 0)))
1122
1123 /* The difference between these counters represents the total number
1124 of complete traceframes present in the trace buffer. The IP agent
1125 writes to the write count, GDBserver writes to read count. */
1126
1127 IP_AGENT_EXPORT unsigned int traceframe_write_count;
1128 IP_AGENT_EXPORT unsigned int traceframe_read_count;
1129
1130 /* Convenience macro. */
1131
1132 #define traceframe_count \
1133 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1134
1135 /* The count of all traceframes created in the current run, including
1136 ones that were discarded to make room. */
1137
1138 IP_AGENT_EXPORT int traceframes_created;
1139
1140 #ifndef IN_PROCESS_AGENT
1141
1142 /* Read-only regions are address ranges whose contents don't change,
1143 and so can be read from target memory even while looking at a trace
1144 frame. Without these, disassembly for instance will likely fail,
1145 because the program code is not usually collected into a trace
1146 frame. This data structure does not need to be very complicated or
1147 particularly efficient, it's only going to be used occasionally,
1148 and only by some commands. */
1149
1150 struct readonly_region
1151 {
1152 /* The bounds of the region. */
1153 CORE_ADDR start, end;
1154
1155 /* Link to the next one. */
1156 struct readonly_region *next;
1157 };
1158
1159 /* Linked list of readonly regions. This list stays in effect from
1160 one tstart to the next. */
1161
1162 static struct readonly_region *readonly_regions;
1163
1164 #endif
1165
1166 /* The global that controls tracing overall. */
1167
1168 IP_AGENT_EXPORT int tracing;
1169
1170 #ifndef IN_PROCESS_AGENT
1171
1172 /* Controls whether tracing should continue after GDB disconnects. */
1173
1174 int disconnected_tracing;
1175
1176 /* The reason for the last tracing run to have stopped. We initialize
1177 to a distinct string so that GDB can distinguish between "stopped
1178 after running" and "stopped because never run" cases. */
1179
1180 static const char *tracing_stop_reason = "tnotrun";
1181
1182 static int tracing_stop_tpnum;
1183
1184 /* 64-bit timestamps for the trace run's start and finish, expressed
1185 in microseconds from the Unix epoch. */
1186
1187 LONGEST tracing_start_time;
1188 LONGEST tracing_stop_time;
1189
1190 /* The (optional) user-supplied name of the user that started the run.
1191 This is an arbitrary string, and may be NULL. */
1192
1193 char *tracing_user_name;
1194
1195 /* Optional user-supplied text describing the run. This is
1196 an arbitrary string, and may be NULL. */
1197
1198 char *tracing_notes;
1199
1200 /* Optional user-supplied text explaining a tstop command. This is an
1201 arbitrary string, and may be NULL. */
1202
1203 char *tracing_stop_note;
1204
1205 #endif
1206
1207 /* Functions local to this file. */
1208
1209 /* Base "class" for tracepoint type specific data to be passed down to
1210 collect_data_at_tracepoint. */
1211 struct tracepoint_hit_ctx
1212 {
1213 enum tracepoint_type type;
1214 };
1215
1216 #ifdef IN_PROCESS_AGENT
1217
1218 /* Fast/jump tracepoint specific data to be passed down to
1219 collect_data_at_tracepoint. */
1220 struct fast_tracepoint_ctx
1221 {
1222 struct tracepoint_hit_ctx base;
1223
1224 struct regcache regcache;
1225 int regcache_initted;
1226 unsigned char *regspace;
1227
1228 unsigned char *regs;
1229 struct tracepoint *tpoint;
1230 };
1231
1232 /* Static tracepoint specific data to be passed down to
1233 collect_data_at_tracepoint. */
1234 struct static_tracepoint_ctx
1235 {
1236 struct tracepoint_hit_ctx base;
1237
1238 /* The regcache corresponding to the registers state at the time of
1239 the tracepoint hit. Initialized lazily, from REGS. */
1240 struct regcache regcache;
1241 int regcache_initted;
1242
1243 /* The buffer space REGCACHE above uses. We use a separate buffer
1244 instead of letting the regcache malloc for both signal safety and
1245 performance reasons; this is allocated on the stack instead. */
1246 unsigned char *regspace;
1247
1248 /* The register buffer as passed on by lttng/ust. */
1249 struct registers *regs;
1250
1251 /* The "printf" formatter and the args the user passed to the marker
1252 call. We use this to be able to collect "static trace data"
1253 ($_sdata). */
1254 const char *fmt;
1255 va_list *args;
1256
1257 /* The GDB tracepoint matching the probed marker that was "hit". */
1258 struct tracepoint *tpoint;
1259 };
1260
1261 #else
1262
1263 /* Static tracepoint specific data to be passed down to
1264 collect_data_at_tracepoint. */
1265 struct trap_tracepoint_ctx
1266 {
1267 struct tracepoint_hit_ctx base;
1268
1269 struct regcache *regcache;
1270 };
1271
1272 #endif
1273
1274 static enum eval_result_type
1275 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx *ctx,
1276 struct traceframe *tframe,
1277 struct agent_expr *aexpr,
1278 ULONGEST *rslt);
1279
1280 #ifndef IN_PROCESS_AGENT
1281 static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1282 static int traceframe_read_tsv (int num, LONGEST *val);
1283 #endif
1284
1285 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1286 struct tracepoint *tpoint);
1287
1288 #ifndef IN_PROCESS_AGENT
1289 static void clear_readonly_regions (void);
1290 static void clear_installed_tracepoints (void);
1291 #endif
1292
1293 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1294 CORE_ADDR stop_pc,
1295 struct tracepoint *tpoint);
1296 #ifndef IN_PROCESS_AGENT
1297 static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1298 CORE_ADDR stop_pc,
1299 struct tracepoint *tpoint, int current_step);
1300 static void compile_tracepoint_condition (struct tracepoint *tpoint,
1301 CORE_ADDR *jump_entry);
1302 #endif
1303 static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1304 CORE_ADDR stop_pc,
1305 struct tracepoint *tpoint,
1306 struct traceframe *tframe,
1307 struct tracepoint_action *taction);
1308
1309 #ifndef IN_PROCESS_AGENT
1310 static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1311
1312 static void install_tracepoint (struct tracepoint *, char *own_buf);
1313 static void download_tracepoint (struct tracepoint *);
1314 static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
1315 static void clone_fast_tracepoint (struct tracepoint *to,
1316 const struct tracepoint *from);
1317 #endif
1318
1319 static LONGEST get_timestamp (void);
1320
1321 #if defined(__GNUC__)
1322 # define memory_barrier() asm volatile ("" : : : "memory")
1323 #else
1324 # define memory_barrier() do {} while (0)
1325 #endif
1326
1327 /* We only build the IPA if this builtin is supported, and there are
1328 no uses of this in GDBserver itself, so we're safe in defining this
1329 unconditionally. */
1330 #define cmpxchg(mem, oldval, newval) \
1331 __sync_val_compare_and_swap (mem, oldval, newval)
1332
1333 /* Record that an error occurred during expression evaluation. */
1334
1335 static void
1336 record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1337 enum eval_result_type rtype)
1338 {
1339 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1340 tpoint->number, paddress (tpoint->address), which, rtype);
1341
1342 #ifdef IN_PROCESS_AGENT
1343 /* Only record the first error we get. */
1344 if (cmpxchg (&expr_eval_result,
1345 expr_eval_no_error,
1346 rtype) != expr_eval_no_error)
1347 return;
1348 #else
1349 if (expr_eval_result != expr_eval_no_error)
1350 return;
1351 #endif
1352
1353 error_tracepoint = tpoint;
1354 }
1355
1356 /* Trace buffer management. */
1357
1358 static void
1359 clear_trace_buffer (void)
1360 {
1361 trace_buffer_start = trace_buffer_lo;
1362 trace_buffer_free = trace_buffer_lo;
1363 trace_buffer_end_free = trace_buffer_hi;
1364 trace_buffer_wrap = trace_buffer_hi;
1365 /* A traceframe with zeroed fields marks the end of trace data. */
1366 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1367 ((struct traceframe *) trace_buffer_free)->data_size = 0;
1368 traceframe_read_count = traceframe_write_count = 0;
1369 traceframes_created = 0;
1370 }
1371
1372 #ifndef IN_PROCESS_AGENT
1373
1374 static void
1375 clear_inferior_trace_buffer (void)
1376 {
1377 CORE_ADDR ipa_trace_buffer_lo;
1378 CORE_ADDR ipa_trace_buffer_hi;
1379 struct traceframe ipa_traceframe = { 0 };
1380 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1381
1382 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1383 &ipa_trace_buffer_lo);
1384 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1385 &ipa_trace_buffer_hi);
1386
1387 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1388 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1389 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1390 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1391
1392 /* A traceframe with zeroed fields marks the end of trace data. */
1393 write_inferior_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1394 (unsigned char *) &ipa_trace_buffer_ctrl,
1395 sizeof (ipa_trace_buffer_ctrl));
1396
1397 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1398
1399 /* A traceframe with zeroed fields marks the end of trace data. */
1400 write_inferior_memory (ipa_trace_buffer_lo,
1401 (unsigned char *) &ipa_traceframe,
1402 sizeof (ipa_traceframe));
1403
1404 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1405 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1406 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1407 }
1408
1409 #endif
1410
1411 static void
1412 init_trace_buffer (unsigned char *buf, int bufsize)
1413 {
1414 trace_buffer_lo = buf;
1415 trace_buffer_hi = trace_buffer_lo + bufsize;
1416
1417 clear_trace_buffer ();
1418 }
1419
1420 #ifdef IN_PROCESS_AGENT
1421
1422 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
1423 about_to_request_buffer_space (void)
1424 {
1425 /* GDBserver places breakpoint here while it goes about to flush
1426 data at random times. */
1427 UNKNOWN_SIDE_EFFECTS();
1428 }
1429
1430 #endif
1431
1432 /* Carve out a piece of the trace buffer, returning NULL in case of
1433 failure. */
1434
1435 static void *
1436 trace_buffer_alloc (size_t amt)
1437 {
1438 unsigned char *rslt;
1439 struct trace_buffer_control *tbctrl;
1440 unsigned int curr;
1441 #ifdef IN_PROCESS_AGENT
1442 unsigned int prev, prev_filtered;
1443 unsigned int commit_count;
1444 unsigned int commit;
1445 unsigned int readout;
1446 #else
1447 struct traceframe *oldest;
1448 unsigned char *new_start;
1449 #endif
1450
1451 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1452 (long) amt, (long) sizeof (struct traceframe));
1453
1454 /* Account for the EOB marker. */
1455 amt += sizeof (struct traceframe);
1456
1457 #ifdef IN_PROCESS_AGENT
1458 again:
1459 memory_barrier ();
1460
1461 /* Read the current token and extract the index to try to write to,
1462 storing it in CURR. */
1463 prev = trace_buffer_ctrl_curr;
1464 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1465 curr = prev_filtered + 1;
1466 if (curr > 2)
1467 curr = 0;
1468
1469 about_to_request_buffer_space ();
1470
1471 /* Start out with a copy of the current state. GDBserver may be
1472 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1473 be able to commit anyway if that happens. */
1474 trace_buffer_ctrl[curr]
1475 = trace_buffer_ctrl[prev_filtered];
1476 trace_debug ("trying curr=%u", curr);
1477 #else
1478 /* The GDBserver's agent doesn't need all that syncing, and always
1479 updates TCB 0 (there's only one, mind you). */
1480 curr = 0;
1481 #endif
1482 tbctrl = &trace_buffer_ctrl[curr];
1483
1484 /* Offsets are easier to grok for debugging than raw addresses,
1485 especially for the small trace buffer sizes that are useful for
1486 testing. */
1487 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1488 curr,
1489 (int) (tbctrl->start - trace_buffer_lo),
1490 (int) (tbctrl->free - trace_buffer_lo),
1491 (int) (tbctrl->end_free - trace_buffer_lo),
1492 (int) (tbctrl->wrap - trace_buffer_lo),
1493 (int) (trace_buffer_hi - trace_buffer_lo));
1494
1495 /* The algorithm here is to keep trying to get a contiguous block of
1496 the requested size, possibly discarding older traceframes to free
1497 up space. Since free space might come in one or two pieces,
1498 depending on whether discarded traceframes wrapped around at the
1499 high end of the buffer, we test both pieces after each
1500 discard. */
1501 while (1)
1502 {
1503 /* First, if we have two free parts, try the upper one first. */
1504 if (tbctrl->end_free < tbctrl->free)
1505 {
1506 if (tbctrl->free + amt <= trace_buffer_hi)
1507 /* We have enough in the upper part. */
1508 break;
1509 else
1510 {
1511 /* Our high part of free space wasn't enough. Give up
1512 on it for now, set wraparound. We will recover the
1513 space later, if/when the wrapped-around traceframe is
1514 discarded. */
1515 trace_debug ("Upper part too small, setting wraparound");
1516 tbctrl->wrap = tbctrl->free;
1517 tbctrl->free = trace_buffer_lo;
1518 }
1519 }
1520
1521 /* The normal case. */
1522 if (tbctrl->free + amt <= tbctrl->end_free)
1523 break;
1524
1525 #ifdef IN_PROCESS_AGENT
1526 /* The IP Agent's buffer is always circular. It isn't used
1527 currently, but `circular_trace_buffer' could represent
1528 GDBserver's mode. If we didn't find space, ask GDBserver to
1529 flush. */
1530
1531 flush_trace_buffer ();
1532 memory_barrier ();
1533 if (tracing)
1534 {
1535 trace_debug ("gdbserver flushed buffer, retrying");
1536 goto again;
1537 }
1538
1539 /* GDBserver cancelled the tracing. Bail out as well. */
1540 return NULL;
1541 #else
1542 /* If we're here, then neither part is big enough, and
1543 non-circular trace buffers are now full. */
1544 if (!circular_trace_buffer)
1545 {
1546 trace_debug ("Not enough space in the trace buffer");
1547 return NULL;
1548 }
1549
1550 trace_debug ("Need more space in the trace buffer");
1551
1552 /* If we have a circular buffer, we can try discarding the
1553 oldest traceframe and see if that helps. */
1554 oldest = FIRST_TRACEFRAME ();
1555 if (oldest->tpnum == 0)
1556 {
1557 /* Not good; we have no traceframes to free. Perhaps we're
1558 asking for a block that is larger than the buffer? In
1559 any case, give up. */
1560 trace_debug ("No traceframes to discard");
1561 return NULL;
1562 }
1563
1564 /* We don't run this code in the in-process agent currently.
1565 E.g., we could leave the in-process agent in autonomous
1566 circular mode if we only have fast tracepoints. If we do
1567 that, then this bit becomes racy with GDBserver, which also
1568 writes to this counter. */
1569 --traceframe_write_count;
1570
1571 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1572 /* If we freed the traceframe that wrapped around, go back
1573 to the non-wrap case. */
1574 if (new_start < tbctrl->start)
1575 {
1576 trace_debug ("Discarding past the wraparound");
1577 tbctrl->wrap = trace_buffer_hi;
1578 }
1579 tbctrl->start = new_start;
1580 tbctrl->end_free = tbctrl->start;
1581
1582 trace_debug ("Discarded a traceframe\n"
1583 "Trace buffer [%d], start=%d free=%d "
1584 "endfree=%d wrap=%d hi=%d",
1585 curr,
1586 (int) (tbctrl->start - trace_buffer_lo),
1587 (int) (tbctrl->free - trace_buffer_lo),
1588 (int) (tbctrl->end_free - trace_buffer_lo),
1589 (int) (tbctrl->wrap - trace_buffer_lo),
1590 (int) (trace_buffer_hi - trace_buffer_lo));
1591
1592 /* Now go back around the loop. The discard might have resulted
1593 in either one or two pieces of free space, so we want to try
1594 both before freeing any more traceframes. */
1595 #endif
1596 }
1597
1598 /* If we get here, we know we can provide the asked-for space. */
1599
1600 rslt = tbctrl->free;
1601
1602 /* Adjust the request back down, now that we know we have space for
1603 the marker, but don't commit to AMT yet, we may still need to
1604 restart the operation if GDBserver touches the trace buffer
1605 (obviously only important in the in-process agent's version). */
1606 tbctrl->free += (amt - sizeof (struct traceframe));
1607
1608 /* Or not. If GDBserver changed the trace buffer behind our back,
1609 we get to restart a new allocation attempt. */
1610
1611 #ifdef IN_PROCESS_AGENT
1612 /* Build the tentative token. */
1613 commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100)
1614 & GDBSERVER_FLUSH_COUNT_MASK_CURR);
1615 commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12)
1616 | commit_count
1617 | curr);
1618
1619 /* Try to commit it. */
1620 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1621 if (readout != prev)
1622 {
1623 trace_debug ("GDBserver has touched the trace buffer, restarting."
1624 " (prev=%08x, commit=%08x, readout=%08x)",
1625 prev, commit, readout);
1626 goto again;
1627 }
1628
1629 /* Hold your horses here. Even if that change was committed,
1630 GDBserver could come in, and clobber it. We need to hold to be
1631 able to tell if GDBserver clobbers before or after we committed
1632 the change. Whenever GDBserver goes about touching the IPA
1633 buffer, it sets a breakpoint in this routine, so we have a sync
1634 point here. */
1635 about_to_request_buffer_space ();
1636
1637 /* Check if the change has been effective, even if GDBserver stopped
1638 us at the breakpoint. */
1639
1640 {
1641 unsigned int refetch;
1642
1643 memory_barrier ();
1644
1645 refetch = trace_buffer_ctrl_curr;
1646
1647 if (refetch == commit
1648 || ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
1649 {
1650 /* effective */
1651 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1652 "readout=%08x, refetch=%08x)",
1653 prev, commit, readout, refetch);
1654 }
1655 else
1656 {
1657 trace_debug ("GDBserver has touched the trace buffer, not effective."
1658 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1659 prev, commit, readout, refetch);
1660 goto again;
1661 }
1662 }
1663 #endif
1664
1665 /* We have a new piece of the trace buffer. Hurray! */
1666
1667 /* Add an EOB marker just past this allocation. */
1668 ((struct traceframe *) tbctrl->free)->tpnum = 0;
1669 ((struct traceframe *) tbctrl->free)->data_size = 0;
1670
1671 /* Adjust the request back down, now that we know we have space for
1672 the marker. */
1673 amt -= sizeof (struct traceframe);
1674
1675 if (debug_threads)
1676 {
1677 trace_debug ("Allocated %d bytes", (int) amt);
1678 trace_debug ("Trace buffer [%d] start=%d free=%d "
1679 "endfree=%d wrap=%d hi=%d",
1680 curr,
1681 (int) (tbctrl->start - trace_buffer_lo),
1682 (int) (tbctrl->free - trace_buffer_lo),
1683 (int) (tbctrl->end_free - trace_buffer_lo),
1684 (int) (tbctrl->wrap - trace_buffer_lo),
1685 (int) (trace_buffer_hi - trace_buffer_lo));
1686 }
1687
1688 return rslt;
1689 }
1690
1691 #ifndef IN_PROCESS_AGENT
1692
1693 /* Return the total free space. This is not necessarily the largest
1694 block we can allocate, because of the two-part case. */
1695
1696 static int
1697 free_space (void)
1698 {
1699 if (trace_buffer_free <= trace_buffer_end_free)
1700 return trace_buffer_end_free - trace_buffer_free;
1701 else
1702 return ((trace_buffer_end_free - trace_buffer_lo)
1703 + (trace_buffer_hi - trace_buffer_free));
1704 }
1705
1706 /* An 'S' in continuation packets indicates remainder are for
1707 while-stepping. */
1708
1709 static int seen_step_action_flag;
1710
1711 /* Create a tracepoint (location) with given number and address. Add this
1712 new tracepoint to list and sort this list. */
1713
1714 static struct tracepoint *
1715 add_tracepoint (int num, CORE_ADDR addr)
1716 {
1717 struct tracepoint *tpoint, **tp_next;
1718
1719 tpoint = xmalloc (sizeof (struct tracepoint));
1720 tpoint->number = num;
1721 tpoint->address = addr;
1722 tpoint->numactions = 0;
1723 tpoint->actions = NULL;
1724 tpoint->actions_str = NULL;
1725 tpoint->cond = NULL;
1726 tpoint->num_step_actions = 0;
1727 tpoint->step_actions = NULL;
1728 tpoint->step_actions_str = NULL;
1729 /* Start all off as regular (slow) tracepoints. */
1730 tpoint->type = trap_tracepoint;
1731 tpoint->orig_size = -1;
1732 tpoint->source_strings = NULL;
1733 tpoint->compiled_cond = 0;
1734 tpoint->handle = NULL;
1735 tpoint->next = NULL;
1736
1737 /* Find a place to insert this tracepoint into list in order to keep
1738 the tracepoint list still in the ascending order. There may be
1739 multiple tracepoints at the same address as TPOINT's, and this
1740 guarantees TPOINT is inserted after all the tracepoints which are
1741 set at the same address. For example, fast tracepoints A, B, C are
1742 set at the same address, and D is to be insert at the same place as
1743 well,
1744
1745 -->| A |--> | B |-->| C |->...
1746
1747 One jump pad was created for tracepoint A, B, and C, and the target
1748 address of A is referenced/used in jump pad. So jump pad will let
1749 inferior jump to A. If D is inserted in front of A, like this,
1750
1751 -->| D |-->| A |--> | B |-->| C |->...
1752
1753 without updating jump pad, D is not reachable during collect, which
1754 is wrong. As we can see, the order of B, C and D doesn't matter, but
1755 A should always be the `first' one. */
1756 for (tp_next = &tracepoints;
1757 (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
1758 tp_next = &(*tp_next)->next)
1759 ;
1760 tpoint->next = *tp_next;
1761 *tp_next = tpoint;
1762 last_tracepoint = tpoint;
1763
1764 seen_step_action_flag = 0;
1765
1766 return tpoint;
1767 }
1768
1769 #ifndef IN_PROCESS_AGENT
1770
1771 /* Return the tracepoint with the given number and address, or NULL. */
1772
1773 static struct tracepoint *
1774 find_tracepoint (int id, CORE_ADDR addr)
1775 {
1776 struct tracepoint *tpoint;
1777
1778 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1779 if (tpoint->number == id && tpoint->address == addr)
1780 return tpoint;
1781
1782 return NULL;
1783 }
1784
1785 /* Remove TPOINT from global list. */
1786
1787 static void
1788 remove_tracepoint (struct tracepoint *tpoint)
1789 {
1790 struct tracepoint *tp, *tp_prev;
1791
1792 for (tp = tracepoints, tp_prev = NULL; tp && tp != tpoint;
1793 tp_prev = tp, tp = tp->next)
1794 ;
1795
1796 if (tp)
1797 {
1798 if (tp_prev)
1799 tp_prev->next = tp->next;
1800 else
1801 tracepoints = tp->next;
1802
1803 xfree (tp);
1804 }
1805 }
1806
1807 /* There may be several tracepoints with the same number (because they
1808 are "locations", in GDB parlance); return the next one after the
1809 given tracepoint, or search from the beginning of the list if the
1810 first argument is NULL. */
1811
1812 static struct tracepoint *
1813 find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1814 {
1815 struct tracepoint *tpoint;
1816
1817 if (prev_tp)
1818 tpoint = prev_tp->next;
1819 else
1820 tpoint = tracepoints;
1821 for (; tpoint; tpoint = tpoint->next)
1822 if (tpoint->number == num)
1823 return tpoint;
1824
1825 return NULL;
1826 }
1827
1828 #endif
1829
1830 static char *
1831 save_string (const char *str, size_t len)
1832 {
1833 char *s;
1834
1835 s = xmalloc (len + 1);
1836 memcpy (s, str, len);
1837 s[len] = '\0';
1838
1839 return s;
1840 }
1841
1842 /* Append another action to perform when the tracepoint triggers. */
1843
1844 static void
1845 add_tracepoint_action (struct tracepoint *tpoint, char *packet)
1846 {
1847 char *act;
1848
1849 if (*packet == 'S')
1850 {
1851 seen_step_action_flag = 1;
1852 ++packet;
1853 }
1854
1855 act = packet;
1856
1857 while (*act)
1858 {
1859 char *act_start = act;
1860 struct tracepoint_action *action = NULL;
1861
1862 switch (*act)
1863 {
1864 case 'M':
1865 {
1866 struct collect_memory_action *maction;
1867 ULONGEST basereg;
1868 int is_neg;
1869
1870 maction = xmalloc (sizeof *maction);
1871 maction->base.type = *act;
1872 maction->base.ops = &m_tracepoint_action_ops;
1873 action = &maction->base;
1874
1875 ++act;
1876 is_neg = (*act == '-');
1877 if (*act == '-')
1878 ++act;
1879 act = unpack_varlen_hex (act, &basereg);
1880 ++act;
1881 act = unpack_varlen_hex (act, &maction->addr);
1882 ++act;
1883 act = unpack_varlen_hex (act, &maction->len);
1884 maction->basereg = (is_neg
1885 ? - (int) basereg
1886 : (int) basereg);
1887 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1888 pulongest (maction->len),
1889 paddress (maction->addr), maction->basereg);
1890 break;
1891 }
1892 case 'R':
1893 {
1894 struct collect_registers_action *raction;
1895
1896 raction = xmalloc (sizeof *raction);
1897 raction->base.type = *act;
1898 raction->base.ops = &r_tracepoint_action_ops;
1899 action = &raction->base;
1900
1901 trace_debug ("Want to collect registers");
1902 ++act;
1903 /* skip past hex digits of mask for now */
1904 while (isxdigit(*act))
1905 ++act;
1906 break;
1907 }
1908 case 'L':
1909 {
1910 struct collect_static_trace_data_action *raction;
1911
1912 raction = xmalloc (sizeof *raction);
1913 raction->base.type = *act;
1914 raction->base.ops = &l_tracepoint_action_ops;
1915 action = &raction->base;
1916
1917 trace_debug ("Want to collect static trace data");
1918 ++act;
1919 break;
1920 }
1921 case 'S':
1922 trace_debug ("Unexpected step action, ignoring");
1923 ++act;
1924 break;
1925 case 'X':
1926 {
1927 struct eval_expr_action *xaction;
1928
1929 xaction = xmalloc (sizeof (*xaction));
1930 xaction->base.type = *act;
1931 xaction->base.ops = &x_tracepoint_action_ops;
1932 action = &xaction->base;
1933
1934 trace_debug ("Want to evaluate expression");
1935 xaction->expr = gdb_parse_agent_expr (&act);
1936 break;
1937 }
1938 default:
1939 trace_debug ("unknown trace action '%c', ignoring...", *act);
1940 break;
1941 case '-':
1942 break;
1943 }
1944
1945 if (action == NULL)
1946 break;
1947
1948 if (seen_step_action_flag)
1949 {
1950 tpoint->num_step_actions++;
1951
1952 tpoint->step_actions
1953 = xrealloc (tpoint->step_actions,
1954 (sizeof (*tpoint->step_actions)
1955 * tpoint->num_step_actions));
1956 tpoint->step_actions_str
1957 = xrealloc (tpoint->step_actions_str,
1958 (sizeof (*tpoint->step_actions_str)
1959 * tpoint->num_step_actions));
1960 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
1961 tpoint->step_actions_str[tpoint->num_step_actions - 1]
1962 = save_string (act_start, act - act_start);
1963 }
1964 else
1965 {
1966 tpoint->numactions++;
1967 tpoint->actions
1968 = xrealloc (tpoint->actions,
1969 sizeof (*tpoint->actions) * tpoint->numactions);
1970 tpoint->actions_str
1971 = xrealloc (tpoint->actions_str,
1972 sizeof (*tpoint->actions_str) * tpoint->numactions);
1973 tpoint->actions[tpoint->numactions - 1] = action;
1974 tpoint->actions_str[tpoint->numactions - 1]
1975 = save_string (act_start, act - act_start);
1976 }
1977 }
1978 }
1979
1980 #endif
1981
1982 /* Find or create a trace state variable with the given number. */
1983
1984 static struct trace_state_variable *
1985 get_trace_state_variable (int num)
1986 {
1987 struct trace_state_variable *tsv;
1988
1989 #ifdef IN_PROCESS_AGENT
1990 /* Search for an existing variable. */
1991 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
1992 if (tsv->number == num)
1993 return tsv;
1994 #endif
1995
1996 /* Search for an existing variable. */
1997 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1998 if (tsv->number == num)
1999 return tsv;
2000
2001 return NULL;
2002 }
2003
2004 /* Find or create a trace state variable with the given number. */
2005
2006 static struct trace_state_variable *
2007 create_trace_state_variable (int num, int gdb)
2008 {
2009 struct trace_state_variable *tsv;
2010
2011 tsv = get_trace_state_variable (num);
2012 if (tsv != NULL)
2013 return tsv;
2014
2015 /* Create a new variable. */
2016 tsv = xmalloc (sizeof (struct trace_state_variable));
2017 tsv->number = num;
2018 tsv->initial_value = 0;
2019 tsv->value = 0;
2020 tsv->getter = NULL;
2021 tsv->name = NULL;
2022 #ifdef IN_PROCESS_AGENT
2023 if (!gdb)
2024 {
2025 tsv->next = alloced_trace_state_variables;
2026 alloced_trace_state_variables = tsv;
2027 }
2028 else
2029 #endif
2030 {
2031 tsv->next = trace_state_variables;
2032 trace_state_variables = tsv;
2033 }
2034 return tsv;
2035 }
2036
2037 IP_AGENT_EXPORT LONGEST
2038 get_trace_state_variable_value (int num)
2039 {
2040 struct trace_state_variable *tsv;
2041
2042 tsv = get_trace_state_variable (num);
2043
2044 if (!tsv)
2045 {
2046 trace_debug ("No trace state variable %d, skipping value get", num);
2047 return 0;
2048 }
2049
2050 /* Call a getter function if we have one. While it's tempting to
2051 set up something to only call the getter once per tracepoint hit,
2052 it could run afoul of thread races. Better to let the getter
2053 handle it directly, if necessary to worry about it. */
2054 if (tsv->getter)
2055 tsv->value = (tsv->getter) ();
2056
2057 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2058 num, plongest (tsv->value));
2059
2060 return tsv->value;
2061 }
2062
2063 IP_AGENT_EXPORT void
2064 set_trace_state_variable_value (int num, LONGEST val)
2065 {
2066 struct trace_state_variable *tsv;
2067
2068 tsv = get_trace_state_variable (num);
2069
2070 if (!tsv)
2071 {
2072 trace_debug ("No trace state variable %d, skipping value set", num);
2073 return;
2074 }
2075
2076 tsv->value = val;
2077 }
2078
2079 LONGEST
2080 agent_get_trace_state_variable_value (int num)
2081 {
2082 return get_trace_state_variable_value (num);
2083 }
2084
2085 void
2086 agent_set_trace_state_variable_value (int num, LONGEST val)
2087 {
2088 set_trace_state_variable_value (num, val);
2089 }
2090
2091 static void
2092 set_trace_state_variable_name (int num, const char *name)
2093 {
2094 struct trace_state_variable *tsv;
2095
2096 tsv = get_trace_state_variable (num);
2097
2098 if (!tsv)
2099 {
2100 trace_debug ("No trace state variable %d, skipping name set", num);
2101 return;
2102 }
2103
2104 tsv->name = (char *) name;
2105 }
2106
2107 static void
2108 set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
2109 {
2110 struct trace_state_variable *tsv;
2111
2112 tsv = get_trace_state_variable (num);
2113
2114 if (!tsv)
2115 {
2116 trace_debug ("No trace state variable %d, skipping getter set", num);
2117 return;
2118 }
2119
2120 tsv->getter = getter;
2121 }
2122
2123 /* Add a raw traceframe for the given tracepoint. */
2124
2125 static struct traceframe *
2126 add_traceframe (struct tracepoint *tpoint)
2127 {
2128 struct traceframe *tframe;
2129
2130 tframe = trace_buffer_alloc (sizeof (struct traceframe));
2131
2132 if (tframe == NULL)
2133 return NULL;
2134
2135 tframe->tpnum = tpoint->number;
2136 tframe->data_size = 0;
2137
2138 return tframe;
2139 }
2140
2141 /* Add a block to the traceframe currently being worked on. */
2142
2143 static unsigned char *
2144 add_traceframe_block (struct traceframe *tframe, int amt)
2145 {
2146 unsigned char *block;
2147
2148 if (!tframe)
2149 return NULL;
2150
2151 block = trace_buffer_alloc (amt);
2152
2153 if (!block)
2154 return NULL;
2155
2156 tframe->data_size += amt;
2157
2158 return block;
2159 }
2160
2161 /* Flag that the current traceframe is finished. */
2162
2163 static void
2164 finish_traceframe (struct traceframe *tframe)
2165 {
2166 ++traceframe_write_count;
2167 ++traceframes_created;
2168 }
2169
2170 #ifndef IN_PROCESS_AGENT
2171
2172 /* Given a traceframe number NUM, find the NUMth traceframe in the
2173 buffer. */
2174
2175 static struct traceframe *
2176 find_traceframe (int num)
2177 {
2178 struct traceframe *tframe;
2179 int tfnum = 0;
2180
2181 for (tframe = FIRST_TRACEFRAME ();
2182 tframe->tpnum != 0;
2183 tframe = NEXT_TRACEFRAME (tframe))
2184 {
2185 if (tfnum == num)
2186 return tframe;
2187 ++tfnum;
2188 }
2189
2190 return NULL;
2191 }
2192
2193 static CORE_ADDR
2194 get_traceframe_address (struct traceframe *tframe)
2195 {
2196 CORE_ADDR addr;
2197 struct tracepoint *tpoint;
2198
2199 addr = traceframe_get_pc (tframe);
2200
2201 if (addr)
2202 return addr;
2203
2204 /* Fallback strategy, will be incorrect for while-stepping frames
2205 and multi-location tracepoints. */
2206 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2207 return tpoint->address;
2208 }
2209
2210 /* Search for the next traceframe whose address is inside or outside
2211 the given range. */
2212
2213 static struct traceframe *
2214 find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2215 int *tfnump)
2216 {
2217 struct traceframe *tframe;
2218 CORE_ADDR tfaddr;
2219
2220 *tfnump = current_traceframe + 1;
2221 tframe = find_traceframe (*tfnump);
2222 /* The search is not supposed to wrap around. */
2223 if (!tframe)
2224 {
2225 *tfnump = -1;
2226 return NULL;
2227 }
2228
2229 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2230 {
2231 tfaddr = get_traceframe_address (tframe);
2232 if (inside_p
2233 ? (lo <= tfaddr && tfaddr <= hi)
2234 : (lo > tfaddr || tfaddr > hi))
2235 return tframe;
2236 ++*tfnump;
2237 }
2238
2239 *tfnump = -1;
2240 return NULL;
2241 }
2242
2243 /* Search for the next traceframe recorded by the given tracepoint.
2244 Note that for multi-location tracepoints, this will find whatever
2245 location appears first. */
2246
2247 static struct traceframe *
2248 find_next_traceframe_by_tracepoint (int num, int *tfnump)
2249 {
2250 struct traceframe *tframe;
2251
2252 *tfnump = current_traceframe + 1;
2253 tframe = find_traceframe (*tfnump);
2254 /* The search is not supposed to wrap around. */
2255 if (!tframe)
2256 {
2257 *tfnump = -1;
2258 return NULL;
2259 }
2260
2261 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2262 {
2263 if (tframe->tpnum == num)
2264 return tframe;
2265 ++*tfnump;
2266 }
2267
2268 *tfnump = -1;
2269 return NULL;
2270 }
2271
2272 #endif
2273
2274 #ifndef IN_PROCESS_AGENT
2275
2276 /* Clear all past trace state. */
2277
2278 static void
2279 cmd_qtinit (char *packet)
2280 {
2281 struct trace_state_variable *tsv, *prev, *next;
2282
2283 /* Make sure we don't try to read from a trace frame. */
2284 current_traceframe = -1;
2285
2286 trace_debug ("Initializing the trace");
2287
2288 clear_installed_tracepoints ();
2289 clear_readonly_regions ();
2290
2291 tracepoints = NULL;
2292 last_tracepoint = NULL;
2293
2294 /* Clear out any leftover trace state variables. Ones with target
2295 defined getters should be kept however. */
2296 prev = NULL;
2297 tsv = trace_state_variables;
2298 while (tsv)
2299 {
2300 trace_debug ("Looking at var %d", tsv->number);
2301 if (tsv->getter == NULL)
2302 {
2303 next = tsv->next;
2304 if (prev)
2305 prev->next = next;
2306 else
2307 trace_state_variables = next;
2308 trace_debug ("Deleting var %d", tsv->number);
2309 free (tsv);
2310 tsv = next;
2311 }
2312 else
2313 {
2314 prev = tsv;
2315 tsv = tsv->next;
2316 }
2317 }
2318
2319 clear_trace_buffer ();
2320 clear_inferior_trace_buffer ();
2321
2322 write_ok (packet);
2323 }
2324
2325 /* Unprobe the UST marker at ADDRESS. */
2326
2327 static void
2328 unprobe_marker_at (CORE_ADDR address)
2329 {
2330 char cmd[IPA_CMD_BUF_SIZE];
2331
2332 sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
2333 run_inferior_command (cmd);
2334 }
2335
2336 /* Restore the program to its pre-tracing state. This routine may be called
2337 in error situations, so it needs to be careful about only restoring
2338 from known-valid bits. */
2339
2340 static void
2341 clear_installed_tracepoints (void)
2342 {
2343 struct tracepoint *tpoint;
2344 struct tracepoint *prev_stpoint;
2345
2346 pause_all (1);
2347 cancel_breakpoints ();
2348
2349 prev_stpoint = NULL;
2350
2351 /* Restore any bytes overwritten by tracepoints. */
2352 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2353 {
2354 /* Catch the case where we might try to remove a tracepoint that
2355 was never actually installed. */
2356 if (tpoint->handle == NULL)
2357 {
2358 trace_debug ("Tracepoint %d at 0x%s was "
2359 "never installed, nothing to clear",
2360 tpoint->number, paddress (tpoint->address));
2361 continue;
2362 }
2363
2364 switch (tpoint->type)
2365 {
2366 case trap_tracepoint:
2367 delete_breakpoint (tpoint->handle);
2368 break;
2369 case fast_tracepoint:
2370 delete_fast_tracepoint_jump (tpoint->handle);
2371 break;
2372 case static_tracepoint:
2373 if (prev_stpoint != NULL
2374 && prev_stpoint->address == tpoint->address)
2375 /* Nothing to do. We already unprobed a tracepoint set at
2376 this marker address (and there can only be one probe
2377 per marker). */
2378 ;
2379 else
2380 {
2381 unprobe_marker_at (tpoint->address);
2382 prev_stpoint = tpoint;
2383 }
2384 break;
2385 }
2386
2387 tpoint->handle = NULL;
2388 }
2389
2390 unpause_all (1);
2391 }
2392
2393 /* Parse a packet that defines a tracepoint. */
2394
2395 static void
2396 cmd_qtdp (char *own_buf)
2397 {
2398 int tppacket;
2399 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2400 int trail_hyphen = 0;
2401 ULONGEST num;
2402 ULONGEST addr;
2403 ULONGEST count;
2404 struct tracepoint *tpoint;
2405 char *actparm;
2406 char *packet = own_buf;
2407
2408 packet += strlen ("QTDP:");
2409
2410 /* A hyphen at the beginning marks a packet specifying actions for a
2411 tracepoint already supplied. */
2412 tppacket = 1;
2413 if (*packet == '-')
2414 {
2415 tppacket = 0;
2416 ++packet;
2417 }
2418 packet = unpack_varlen_hex (packet, &num);
2419 ++packet; /* skip a colon */
2420 packet = unpack_varlen_hex (packet, &addr);
2421 ++packet; /* skip a colon */
2422
2423 /* See if we already have this tracepoint. */
2424 tpoint = find_tracepoint (num, addr);
2425
2426 if (tppacket)
2427 {
2428 /* Duplicate tracepoints are never allowed. */
2429 if (tpoint)
2430 {
2431 trace_debug ("Tracepoint error: tracepoint %d"
2432 " at 0x%s already exists",
2433 (int) num, paddress (addr));
2434 write_enn (own_buf);
2435 return;
2436 }
2437
2438 tpoint = add_tracepoint (num, addr);
2439
2440 tpoint->enabled = (*packet == 'E');
2441 ++packet; /* skip 'E' */
2442 ++packet; /* skip a colon */
2443 packet = unpack_varlen_hex (packet, &count);
2444 tpoint->step_count = count;
2445 ++packet; /* skip a colon */
2446 packet = unpack_varlen_hex (packet, &count);
2447 tpoint->pass_count = count;
2448 /* See if we have any of the additional optional fields. */
2449 while (*packet == ':')
2450 {
2451 ++packet;
2452 if (*packet == 'F')
2453 {
2454 tpoint->type = fast_tracepoint;
2455 ++packet;
2456 packet = unpack_varlen_hex (packet, &count);
2457 tpoint->orig_size = count;
2458 }
2459 else if (*packet == 'S')
2460 {
2461 tpoint->type = static_tracepoint;
2462 ++packet;
2463 }
2464 else if (*packet == 'X')
2465 {
2466 actparm = (char *) packet;
2467 tpoint->cond = gdb_parse_agent_expr (&actparm);
2468 packet = actparm;
2469 }
2470 else if (*packet == '-')
2471 break;
2472 else if (*packet == '\0')
2473 break;
2474 else
2475 trace_debug ("Unknown optional tracepoint field");
2476 }
2477 if (*packet == '-')
2478 {
2479 trail_hyphen = 1;
2480 trace_debug ("Also has actions\n");
2481 }
2482
2483 trace_debug ("Defined %stracepoint %d at 0x%s, "
2484 "enabled %d step %" PRIu64 " pass %" PRIu64,
2485 tpoint->type == fast_tracepoint ? "fast "
2486 : tpoint->type == static_tracepoint ? "static " : "",
2487 tpoint->number, paddress (tpoint->address), tpoint->enabled,
2488 tpoint->step_count, tpoint->pass_count);
2489 }
2490 else if (tpoint)
2491 add_tracepoint_action (tpoint, packet);
2492 else
2493 {
2494 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2495 (int) num, paddress (addr));
2496 write_enn (own_buf);
2497 return;
2498 }
2499
2500 /* Install tracepoint during tracing only once for each tracepoint location.
2501 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2502 determine the last QTDP packet for one tracepoint location by checking
2503 trailing hyphen in QTDP packet. */
2504 if (tracing && !trail_hyphen)
2505 {
2506 struct tracepoint *tp = NULL;
2507
2508 /* Pause all threads temporarily while we patch tracepoints. */
2509 pause_all (0);
2510
2511 /* download_tracepoint will update global `tracepoints'
2512 list, so it is unsafe to leave threads in jump pad. */
2513 stabilize_threads ();
2514
2515 /* Freeze threads. */
2516 pause_all (1);
2517
2518
2519 if (tpoint->type != trap_tracepoint)
2520 {
2521 /* Find another fast or static tracepoint at the same address. */
2522 for (tp = tracepoints; tp; tp = tp->next)
2523 {
2524 if (tp->address == tpoint->address && tp->type == tpoint->type
2525 && tp->number != tpoint->number)
2526 break;
2527 }
2528
2529 /* TPOINT is installed at the same address as TP. */
2530 if (tp)
2531 {
2532 if (tpoint->type == fast_tracepoint)
2533 clone_fast_tracepoint (tpoint, tp);
2534 else if (tpoint->type == static_tracepoint)
2535 tpoint->handle = (void *) -1;
2536 }
2537 }
2538
2539 download_tracepoint (tpoint);
2540
2541 if (tpoint->type == trap_tracepoint || tp == NULL)
2542 {
2543 install_tracepoint (tpoint, own_buf);
2544 if (strcmp (own_buf, "OK") != 0)
2545 remove_tracepoint (tpoint);
2546 }
2547 else
2548 write_ok (own_buf);
2549
2550 unpause_all (1);
2551 return;
2552 }
2553
2554 write_ok (own_buf);
2555 }
2556
2557 static void
2558 cmd_qtdpsrc (char *own_buf)
2559 {
2560 ULONGEST num, addr, start, slen;
2561 struct tracepoint *tpoint;
2562 char *packet = own_buf;
2563 char *saved, *srctype, *src;
2564 size_t nbytes;
2565 struct source_string *last, *newlast;
2566
2567 packet += strlen ("QTDPsrc:");
2568
2569 packet = unpack_varlen_hex (packet, &num);
2570 ++packet; /* skip a colon */
2571 packet = unpack_varlen_hex (packet, &addr);
2572 ++packet; /* skip a colon */
2573
2574 /* See if we already have this tracepoint. */
2575 tpoint = find_tracepoint (num, addr);
2576
2577 if (!tpoint)
2578 {
2579 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2580 (int) num, paddress (addr));
2581 write_enn (own_buf);
2582 return;
2583 }
2584
2585 saved = packet;
2586 packet = strchr (packet, ':');
2587 srctype = xmalloc (packet - saved + 1);
2588 memcpy (srctype, saved, packet - saved);
2589 srctype[packet - saved] = '\0';
2590 ++packet;
2591 packet = unpack_varlen_hex (packet, &start);
2592 ++packet; /* skip a colon */
2593 packet = unpack_varlen_hex (packet, &slen);
2594 ++packet; /* skip a colon */
2595 src = xmalloc (slen + 1);
2596 nbytes = unhexify (src, packet, strlen (packet) / 2);
2597 src[nbytes] = '\0';
2598
2599 newlast = xmalloc (sizeof (struct source_string));
2600 newlast->type = srctype;
2601 newlast->str = src;
2602 newlast->next = NULL;
2603 /* Always add a source string to the end of the list;
2604 this keeps sequences of actions/commands in the right
2605 order. */
2606 if (tpoint->source_strings)
2607 {
2608 for (last = tpoint->source_strings; last->next; last = last->next)
2609 ;
2610 last->next = newlast;
2611 }
2612 else
2613 tpoint->source_strings = newlast;
2614
2615 write_ok (own_buf);
2616 }
2617
2618 static void
2619 cmd_qtdv (char *own_buf)
2620 {
2621 ULONGEST num, val, builtin;
2622 char *varname;
2623 size_t nbytes;
2624 struct trace_state_variable *tsv;
2625 char *packet = own_buf;
2626
2627 packet += strlen ("QTDV:");
2628
2629 packet = unpack_varlen_hex (packet, &num);
2630 ++packet; /* skip a colon */
2631 packet = unpack_varlen_hex (packet, &val);
2632 ++packet; /* skip a colon */
2633 packet = unpack_varlen_hex (packet, &builtin);
2634 ++packet; /* skip a colon */
2635
2636 nbytes = strlen (packet) / 2;
2637 varname = xmalloc (nbytes + 1);
2638 nbytes = unhexify (varname, packet, nbytes);
2639 varname[nbytes] = '\0';
2640
2641 tsv = create_trace_state_variable (num, 1);
2642 tsv->initial_value = (LONGEST) val;
2643 tsv->name = varname;
2644
2645 set_trace_state_variable_value (num, (LONGEST) val);
2646
2647 write_ok (own_buf);
2648 }
2649
2650 static void
2651 cmd_qtenable_disable (char *own_buf, int enable)
2652 {
2653 char *packet = own_buf;
2654 ULONGEST num, addr;
2655 struct tracepoint *tp;
2656
2657 packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2658 packet = unpack_varlen_hex (packet, &num);
2659 ++packet; /* skip a colon */
2660 packet = unpack_varlen_hex (packet, &addr);
2661
2662 tp = find_tracepoint (num, addr);
2663
2664 if (tp)
2665 {
2666 if ((enable && tp->enabled) || (!enable && !tp->enabled))
2667 {
2668 trace_debug ("Tracepoint %d at 0x%s is already %s",
2669 (int) num, paddress (addr),
2670 enable ? "enabled" : "disabled");
2671 write_ok (own_buf);
2672 return;
2673 }
2674
2675 trace_debug ("%s tracepoint %d at 0x%s",
2676 enable ? "Enabling" : "Disabling",
2677 (int) num, paddress (addr));
2678
2679 tp->enabled = enable;
2680
2681 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2682 {
2683 int ret;
2684 int offset = offsetof (struct tracepoint, enabled);
2685 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2686
2687 ret = prepare_to_access_memory ();
2688 if (ret)
2689 {
2690 trace_debug ("Failed to temporarily stop inferior threads");
2691 write_enn (own_buf);
2692 return;
2693 }
2694
2695 ret = write_inferior_integer (obj_addr, enable);
2696 done_accessing_memory ();
2697
2698 if (ret)
2699 {
2700 trace_debug ("Cannot write enabled flag into "
2701 "inferior process memory");
2702 write_enn (own_buf);
2703 return;
2704 }
2705 }
2706
2707 write_ok (own_buf);
2708 }
2709 else
2710 {
2711 trace_debug ("Tracepoint %d at 0x%s not found",
2712 (int) num, paddress (addr));
2713 write_enn (own_buf);
2714 }
2715 }
2716
2717 static void
2718 cmd_qtv (char *own_buf)
2719 {
2720 ULONGEST num;
2721 LONGEST val;
2722 int err;
2723 char *packet = own_buf;
2724
2725 packet += strlen ("qTV:");
2726 unpack_varlen_hex (packet, &num);
2727
2728 if (current_traceframe >= 0)
2729 {
2730 err = traceframe_read_tsv ((int) num, &val);
2731 if (err)
2732 {
2733 strcpy (own_buf, "U");
2734 return;
2735 }
2736 }
2737 /* Only make tsv's be undefined before the first trace run. After a
2738 trace run is over, the user might want to see the last value of
2739 the tsv, and it might not be available in a traceframe. */
2740 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2741 {
2742 strcpy (own_buf, "U");
2743 return;
2744 }
2745 else
2746 val = get_trace_state_variable_value (num);
2747
2748 sprintf (own_buf, "V%s", phex_nz (val, 0));
2749 }
2750
2751 /* Clear out the list of readonly regions. */
2752
2753 static void
2754 clear_readonly_regions (void)
2755 {
2756 struct readonly_region *roreg;
2757
2758 while (readonly_regions)
2759 {
2760 roreg = readonly_regions;
2761 readonly_regions = readonly_regions->next;
2762 free (roreg);
2763 }
2764 }
2765
2766 /* Parse the collection of address ranges whose contents GDB believes
2767 to be unchanging and so can be read directly from target memory
2768 even while looking at a traceframe. */
2769
2770 static void
2771 cmd_qtro (char *own_buf)
2772 {
2773 ULONGEST start, end;
2774 struct readonly_region *roreg;
2775 char *packet = own_buf;
2776
2777 trace_debug ("Want to mark readonly regions");
2778
2779 clear_readonly_regions ();
2780
2781 packet += strlen ("QTro");
2782
2783 while (*packet == ':')
2784 {
2785 ++packet; /* skip a colon */
2786 packet = unpack_varlen_hex (packet, &start);
2787 ++packet; /* skip a comma */
2788 packet = unpack_varlen_hex (packet, &end);
2789 roreg = xmalloc (sizeof (struct readonly_region));
2790 roreg->start = start;
2791 roreg->end = end;
2792 roreg->next = readonly_regions;
2793 readonly_regions = roreg;
2794 trace_debug ("Added readonly region from 0x%s to 0x%s",
2795 paddress (roreg->start), paddress (roreg->end));
2796 }
2797
2798 write_ok (own_buf);
2799 }
2800
2801 /* Test to see if the given range is in our list of readonly ranges.
2802 We only test for being entirely within a range, GDB is not going to
2803 send a single memory packet that spans multiple regions. */
2804
2805 int
2806 in_readonly_region (CORE_ADDR addr, ULONGEST length)
2807 {
2808 struct readonly_region *roreg;
2809
2810 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2811 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2812 return 1;
2813
2814 return 0;
2815 }
2816
2817 /* The maximum size of a jump pad entry. */
2818 static const int max_jump_pad_size = 0x100;
2819
2820 static CORE_ADDR gdb_jump_pad_head;
2821
2822 /* Return the address of the next free jump space. */
2823
2824 static CORE_ADDR
2825 get_jump_space_head (void)
2826 {
2827 if (gdb_jump_pad_head == 0)
2828 {
2829 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2830 &gdb_jump_pad_head))
2831 fatal ("error extracting jump_pad_buffer");
2832 }
2833
2834 return gdb_jump_pad_head;
2835 }
2836
2837 /* Reserve USED bytes from the jump space. */
2838
2839 static void
2840 claim_jump_space (ULONGEST used)
2841 {
2842 trace_debug ("claim_jump_space reserves %s bytes at %s",
2843 pulongest (used), paddress (gdb_jump_pad_head));
2844 gdb_jump_pad_head += used;
2845 }
2846
2847 static CORE_ADDR trampoline_buffer_head = 0;
2848 static CORE_ADDR trampoline_buffer_tail;
2849
2850 /* Reserve USED bytes from the trampoline buffer and return the
2851 address of the start of the reserved space in TRAMPOLINE. Returns
2852 non-zero if the space is successfully claimed. */
2853
2854 int
2855 claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
2856 {
2857 if (!trampoline_buffer_head)
2858 {
2859 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
2860 &trampoline_buffer_tail))
2861 {
2862 fatal ("error extracting trampoline_buffer");
2863 return 0;
2864 }
2865
2866 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2867 &trampoline_buffer_head))
2868 {
2869 fatal ("error extracting trampoline_buffer_end");
2870 return 0;
2871 }
2872 }
2873
2874 /* Start claiming space from the top of the trampoline space. If
2875 the space is located at the bottom of the virtual address space,
2876 this reduces the possibility that corruption will occur if a null
2877 pointer is used to write to memory. */
2878 if (trampoline_buffer_head - trampoline_buffer_tail < used)
2879 {
2880 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2881 pulongest (used));
2882 return 0;
2883 }
2884
2885 trampoline_buffer_head -= used;
2886
2887 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2888 pulongest (used), paddress (trampoline_buffer_head));
2889
2890 *trampoline = trampoline_buffer_head;
2891 return 1;
2892 }
2893
2894 /* Returns non-zero if there is space allocated for use in trampolines
2895 for fast tracepoints. */
2896
2897 int
2898 have_fast_tracepoint_trampoline_buffer (char *buf)
2899 {
2900 CORE_ADDR trampoline_end, errbuf;
2901
2902 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2903 &trampoline_end))
2904 {
2905 fatal ("error extracting trampoline_buffer_end");
2906 return 0;
2907 }
2908
2909 if (buf)
2910 {
2911 buf[0] = '\0';
2912 strcpy (buf, "was claiming");
2913 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
2914 &errbuf))
2915 {
2916 fatal ("error extracting errbuf");
2917 return 0;
2918 }
2919
2920 read_inferior_memory (errbuf, (unsigned char *) buf, 100);
2921 }
2922
2923 return trampoline_end != 0;
2924 }
2925
2926 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2927 the command fails, or 0 otherwise. If the command ran
2928 successfully, but probing the marker failed, ERROUT will be filled
2929 with the error to reply to GDB, and -1 is also returned. This
2930 allows directly passing IPA errors to GDB. */
2931
2932 static int
2933 probe_marker_at (CORE_ADDR address, char *errout)
2934 {
2935 char cmd[IPA_CMD_BUF_SIZE];
2936 int err;
2937
2938 sprintf (cmd, "probe_marker_at:%s", paddress (address));
2939 err = run_inferior_command (cmd);
2940
2941 if (err == 0)
2942 {
2943 if (*cmd == 'E')
2944 {
2945 strcpy (errout, cmd);
2946 return -1;
2947 }
2948 }
2949
2950 return err;
2951 }
2952
2953 static void
2954 clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
2955 {
2956 to->jump_pad = from->jump_pad;
2957 to->jump_pad_end = from->jump_pad_end;
2958 to->trampoline = from->trampoline;
2959 to->trampoline_end = from->trampoline_end;
2960 to->adjusted_insn_addr = from->adjusted_insn_addr;
2961 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
2962 to->handle = from->handle;
2963
2964 gdb_assert (from->handle);
2965 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
2966 }
2967
2968 #define MAX_JUMP_SIZE 20
2969
2970 /* Install fast tracepoint. Return 0 if successful, otherwise return
2971 non-zero. */
2972
2973 static int
2974 install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
2975 {
2976 CORE_ADDR jentry, jump_entry;
2977 CORE_ADDR trampoline;
2978 ULONGEST trampoline_size;
2979 int err = 0;
2980 /* The jump to the jump pad of the last fast tracepoint
2981 installed. */
2982 unsigned char fjump[MAX_JUMP_SIZE];
2983 ULONGEST fjump_size;
2984
2985 if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
2986 {
2987 trace_debug ("Requested a fast tracepoint on an instruction "
2988 "that is of less than the minimum length.");
2989 return 0;
2990 }
2991
2992 jentry = jump_entry = get_jump_space_head ();
2993
2994 trampoline = 0;
2995 trampoline_size = 0;
2996
2997 /* Install the jump pad. */
2998 err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
2999 tpoint->address,
3000 ipa_sym_addrs.addr_gdb_collect,
3001 ipa_sym_addrs.addr_collecting,
3002 tpoint->orig_size,
3003 &jentry,
3004 &trampoline, &trampoline_size,
3005 fjump, &fjump_size,
3006 &tpoint->adjusted_insn_addr,
3007 &tpoint->adjusted_insn_addr_end,
3008 errbuf);
3009
3010 if (err)
3011 return 1;
3012
3013 /* Wire it in. */
3014 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
3015 fjump_size);
3016
3017 if (tpoint->handle != NULL)
3018 {
3019 tpoint->jump_pad = jump_entry;
3020 tpoint->jump_pad_end = jentry;
3021 tpoint->trampoline = trampoline;
3022 tpoint->trampoline_end = trampoline + trampoline_size;
3023
3024 /* Pad to 8-byte alignment. */
3025 jentry = ((jentry + 7) & ~0x7);
3026 claim_jump_space (jentry - jump_entry);
3027 }
3028
3029 return 0;
3030 }
3031
3032
3033 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3034
3035 static void
3036 install_tracepoint (struct tracepoint *tpoint, char *own_buf)
3037 {
3038 tpoint->handle = NULL;
3039 *own_buf = '\0';
3040
3041 if (tpoint->type == trap_tracepoint)
3042 {
3043 /* Tracepoints are installed as memory breakpoints. Just go
3044 ahead and install the trap. The breakpoints module
3045 handles duplicated breakpoints, and the memory read
3046 routine handles un-patching traps from memory reads. */
3047 tpoint->handle = set_breakpoint_at (tpoint->address,
3048 tracepoint_handler);
3049 }
3050 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
3051 {
3052 if (!agent_loaded_p ())
3053 {
3054 trace_debug ("Requested a %s tracepoint, but fast "
3055 "tracepoints aren't supported.",
3056 tpoint->type == static_tracepoint ? "static" : "fast");
3057 write_e_ipa_not_loaded (own_buf);
3058 return;
3059 }
3060 if (tpoint->type == static_tracepoint
3061 && !in_process_agent_supports_ust ())
3062 {
3063 trace_debug ("Requested a static tracepoint, but static "
3064 "tracepoints are not supported.");
3065 write_e_ust_not_loaded (own_buf);
3066 return;
3067 }
3068
3069 if (tpoint->type == fast_tracepoint)
3070 install_fast_tracepoint (tpoint, own_buf);
3071 else
3072 {
3073 if (probe_marker_at (tpoint->address, own_buf) == 0)
3074 tpoint->handle = (void *) -1;
3075 }
3076
3077 }
3078 else
3079 internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
3080
3081 if (tpoint->handle == NULL)
3082 {
3083 if (*own_buf == '\0')
3084 write_enn (own_buf);
3085 }
3086 else
3087 write_ok (own_buf);
3088 }
3089
3090 static void download_tracepoint_1 (struct tracepoint *tpoint);
3091
3092 static void
3093 cmd_qtstart (char *packet)
3094 {
3095 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
3096 CORE_ADDR tpptr = 0, prev_tpptr = 0;
3097
3098 trace_debug ("Starting the trace");
3099
3100 /* Pause all threads temporarily while we patch tracepoints. */
3101 pause_all (0);
3102
3103 /* Get threads out of jump pads. Safe to do here, since this is a
3104 top level command. And, required to do here, since we're
3105 deleting/rewriting jump pads. */
3106
3107 stabilize_threads ();
3108
3109 /* Freeze threads. */
3110 pause_all (1);
3111
3112 /* Sync the fast tracepoints list in the inferior ftlib. */
3113 if (agent_loaded_p ())
3114 download_trace_state_variables ();
3115
3116 /* No previous fast tpoint yet. */
3117 prev_ftpoint = NULL;
3118
3119 /* No previous static tpoint yet. */
3120 prev_stpoint = NULL;
3121
3122 *packet = '\0';
3123
3124 /* Start out empty. */
3125 if (agent_loaded_p ())
3126 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
3127
3128 /* Download and install tracepoints. */
3129 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3130 {
3131 /* Ensure all the hit counts start at zero. */
3132 tpoint->hit_count = 0;
3133 tpoint->traceframe_usage = 0;
3134
3135 if (tpoint->type == trap_tracepoint)
3136 {
3137 /* Tracepoints are installed as memory breakpoints. Just go
3138 ahead and install the trap. The breakpoints module
3139 handles duplicated breakpoints, and the memory read
3140 routine handles un-patching traps from memory reads. */
3141 tpoint->handle = set_breakpoint_at (tpoint->address,
3142 tracepoint_handler);
3143 }
3144 else if (tpoint->type == fast_tracepoint
3145 || tpoint->type == static_tracepoint)
3146 {
3147 if (maybe_write_ipa_not_loaded (packet))
3148 {
3149 trace_debug ("Requested a %s tracepoint, but fast "
3150 "tracepoints aren't supported.",
3151 tpoint->type == static_tracepoint
3152 ? "static" : "fast");
3153 break;
3154 }
3155
3156 if (tpoint->type == fast_tracepoint)
3157 {
3158 download_tracepoint_1 (tpoint);
3159
3160 if (prev_ftpoint != NULL
3161 && prev_ftpoint->address == tpoint->address)
3162 clone_fast_tracepoint (tpoint, prev_ftpoint);
3163 else
3164 {
3165 if (install_fast_tracepoint (tpoint, packet) == 0)
3166 prev_ftpoint = tpoint;
3167 }
3168 }
3169 else
3170 {
3171 if (!in_process_agent_supports_ust ())
3172 {
3173 trace_debug ("Requested a static tracepoint, but static "
3174 "tracepoints are not supported.");
3175 break;
3176 }
3177
3178 download_tracepoint_1 (tpoint);
3179 /* Can only probe a given marker once. */
3180 if (prev_stpoint != NULL
3181 && prev_stpoint->address == tpoint->address)
3182 tpoint->handle = (void *) -1;
3183 else
3184 {
3185 if (probe_marker_at (tpoint->address, packet) == 0)
3186 {
3187 tpoint->handle = (void *) -1;
3188
3189 /* So that we can handle multiple static tracepoints
3190 at the same address easily. */
3191 prev_stpoint = tpoint;
3192 }
3193 }
3194 }
3195
3196 prev_tpptr = tpptr;
3197 tpptr = tpoint->obj_addr_on_target;
3198
3199 if (tpoint == tracepoints)
3200 /* First object in list, set the head pointer in the
3201 inferior. */
3202 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
3203 else
3204 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
3205 next),
3206 tpptr);
3207 }
3208
3209 /* Any failure in the inner loop is sufficient cause to give
3210 up. */
3211 if (tpoint->handle == NULL)
3212 break;
3213 }
3214
3215 /* Any error in tracepoint insertion is unacceptable; better to
3216 address the problem now, than end up with a useless or misleading
3217 trace run. */
3218 if (tpoint != NULL)
3219 {
3220 clear_installed_tracepoints ();
3221 if (*packet == '\0')
3222 write_enn (packet);
3223 unpause_all (1);
3224 return;
3225 }
3226
3227 stopping_tracepoint = NULL;
3228 trace_buffer_is_full = 0;
3229 expr_eval_result = expr_eval_no_error;
3230 error_tracepoint = NULL;
3231 tracing_start_time = get_timestamp ();
3232
3233 /* Tracing is now active, hits will now start being logged. */
3234 tracing = 1;
3235
3236 if (agent_loaded_p ())
3237 {
3238 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
3239 fatal ("Error setting tracing variable in lib");
3240
3241 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3242 0))
3243 fatal ("Error clearing stopping_tracepoint variable in lib");
3244
3245 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
3246 fatal ("Error clearing trace_buffer_is_full variable in lib");
3247
3248 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3249 stop_tracing_handler);
3250 if (stop_tracing_bkpt == NULL)
3251 error ("Error setting stop_tracing breakpoint");
3252
3253 flush_trace_buffer_bkpt
3254 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3255 flush_trace_buffer_handler);
3256 if (flush_trace_buffer_bkpt == NULL)
3257 error ("Error setting flush_trace_buffer breakpoint");
3258 }
3259
3260 unpause_all (1);
3261
3262 write_ok (packet);
3263 }
3264
3265 /* End a tracing run, filling in a stop reason to report back to GDB,
3266 and removing the tracepoints from the code. */
3267
3268 void
3269 stop_tracing (void)
3270 {
3271 if (!tracing)
3272 {
3273 trace_debug ("Tracing is already off, ignoring");
3274 return;
3275 }
3276
3277 trace_debug ("Stopping the trace");
3278
3279 /* Pause all threads before removing fast jumps from memory,
3280 breakpoints, and touching IPA state variables (inferior memory).
3281 Some thread may hit the internal tracing breakpoints, or be
3282 collecting this moment, but that's ok, we don't release the
3283 tpoint object's memory or the jump pads here (we only do that
3284 when we're sure we can move all threads out of the jump pads).
3285 We can't now, since we may be getting here due to the inferior
3286 agent calling us. */
3287 pause_all (1);
3288 /* Since we're removing breakpoints, cancel breakpoint hits,
3289 possibly related to the breakpoints we're about to delete. */
3290 cancel_breakpoints ();
3291
3292 /* Stop logging. Tracepoints can still be hit, but they will not be
3293 recorded. */
3294 tracing = 0;
3295 if (agent_loaded_p ())
3296 {
3297 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
3298 fatal ("Error clearing tracing variable in lib");
3299 }
3300
3301 tracing_stop_time = get_timestamp ();
3302 tracing_stop_reason = "t???";
3303 tracing_stop_tpnum = 0;
3304 if (stopping_tracepoint)
3305 {
3306 trace_debug ("Stopping the trace because "
3307 "tracepoint %d was hit %" PRIu64 " times",
3308 stopping_tracepoint->number,
3309 stopping_tracepoint->pass_count);
3310 tracing_stop_reason = "tpasscount";
3311 tracing_stop_tpnum = stopping_tracepoint->number;
3312 }
3313 else if (trace_buffer_is_full)
3314 {
3315 trace_debug ("Stopping the trace because the trace buffer is full");
3316 tracing_stop_reason = "tfull";
3317 }
3318 else if (expr_eval_result != expr_eval_no_error)
3319 {
3320 trace_debug ("Stopping the trace because of an expression eval error");
3321 tracing_stop_reason = eval_result_names[expr_eval_result];
3322 tracing_stop_tpnum = error_tracepoint->number;
3323 }
3324 #ifndef IN_PROCESS_AGENT
3325 else if (!gdb_connected ())
3326 {
3327 trace_debug ("Stopping the trace because GDB disconnected");
3328 tracing_stop_reason = "tdisconnected";
3329 }
3330 #endif
3331 else
3332 {
3333 trace_debug ("Stopping the trace because of a tstop command");
3334 tracing_stop_reason = "tstop";
3335 }
3336
3337 stopping_tracepoint = NULL;
3338 error_tracepoint = NULL;
3339
3340 /* Clear out the tracepoints. */
3341 clear_installed_tracepoints ();
3342
3343 if (agent_loaded_p ())
3344 {
3345 /* Pull in fast tracepoint trace frames from the inferior lib
3346 buffer into our buffer, even if our buffer is already full,
3347 because we want to present the full number of created frames
3348 in addition to what fit in the trace buffer. */
3349 upload_fast_traceframes ();
3350 }
3351
3352 if (stop_tracing_bkpt != NULL)
3353 {
3354 delete_breakpoint (stop_tracing_bkpt);
3355 stop_tracing_bkpt = NULL;
3356 }
3357
3358 if (flush_trace_buffer_bkpt != NULL)
3359 {
3360 delete_breakpoint (flush_trace_buffer_bkpt);
3361 flush_trace_buffer_bkpt = NULL;
3362 }
3363
3364 unpause_all (1);
3365 }
3366
3367 static int
3368 stop_tracing_handler (CORE_ADDR addr)
3369 {
3370 trace_debug ("lib hit stop_tracing");
3371
3372 /* Don't actually handle it here. When we stop tracing we remove
3373 breakpoints from the inferior, and that is not allowed in a
3374 breakpoint handler (as the caller is walking the breakpoint
3375 list). */
3376 return 0;
3377 }
3378
3379 static int
3380 flush_trace_buffer_handler (CORE_ADDR addr)
3381 {
3382 trace_debug ("lib hit flush_trace_buffer");
3383 return 0;
3384 }
3385
3386 static void
3387 cmd_qtstop (char *packet)
3388 {
3389 stop_tracing ();
3390 write_ok (packet);
3391 }
3392
3393 static void
3394 cmd_qtdisconnected (char *own_buf)
3395 {
3396 ULONGEST setting;
3397 char *packet = own_buf;
3398
3399 packet += strlen ("QTDisconnected:");
3400
3401 unpack_varlen_hex (packet, &setting);
3402
3403 write_ok (own_buf);
3404
3405 disconnected_tracing = setting;
3406 }
3407
3408 static void
3409 cmd_qtframe (char *own_buf)
3410 {
3411 ULONGEST frame, pc, lo, hi, num;
3412 int tfnum, tpnum;
3413 struct traceframe *tframe;
3414 char *packet = own_buf;
3415
3416 packet += strlen ("QTFrame:");
3417
3418 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3419 {
3420 packet += strlen ("pc:");
3421 unpack_varlen_hex (packet, &pc);
3422 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3423 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3424 }
3425 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3426 {
3427 packet += strlen ("range:");
3428 packet = unpack_varlen_hex (packet, &lo);
3429 ++packet;
3430 unpack_varlen_hex (packet, &hi);
3431 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3432 paddress (lo), paddress (hi));
3433 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3434 }
3435 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3436 {
3437 packet += strlen ("outside:");
3438 packet = unpack_varlen_hex (packet, &lo);
3439 ++packet;
3440 unpack_varlen_hex (packet, &hi);
3441 trace_debug ("Want to find next traceframe "
3442 "outside the range 0x%s to 0x%s",
3443 paddress (lo), paddress (hi));
3444 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3445 }
3446 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3447 {
3448 packet += strlen ("tdp:");
3449 unpack_varlen_hex (packet, &num);
3450 tpnum = (int) num;
3451 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3452 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3453 }
3454 else
3455 {
3456 unpack_varlen_hex (packet, &frame);
3457 tfnum = (int) frame;
3458 if (tfnum == -1)
3459 {
3460 trace_debug ("Want to stop looking at traceframes");
3461 current_traceframe = -1;
3462 write_ok (own_buf);
3463 return;
3464 }
3465 trace_debug ("Want to look at traceframe %d", tfnum);
3466 tframe = find_traceframe (tfnum);
3467 }
3468
3469 if (tframe)
3470 {
3471 current_traceframe = tfnum;
3472 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3473 }
3474 else
3475 sprintf (own_buf, "F-1");
3476 }
3477
3478 static void
3479 cmd_qtstatus (char *packet)
3480 {
3481 char *stop_reason_rsp = NULL;
3482 char *buf1, *buf2, *buf3, *str;
3483 int slen;
3484
3485 /* Translate the plain text of the notes back into hex for
3486 transmission. */
3487
3488 str = (tracing_user_name ? tracing_user_name : "");
3489 slen = strlen (str);
3490 buf1 = (char *) alloca (slen * 2 + 1);
3491 hexify (buf1, str, slen);
3492
3493 str = (tracing_notes ? tracing_notes : "");
3494 slen = strlen (str);
3495 buf2 = (char *) alloca (slen * 2 + 1);
3496 hexify (buf2, str, slen);
3497
3498 str = (tracing_stop_note ? tracing_stop_note : "");
3499 slen = strlen (str);
3500 buf3 = (char *) alloca (slen * 2 + 1);
3501 hexify (buf3, str, slen);
3502
3503 trace_debug ("Returning trace status as %d, stop reason %s",
3504 tracing, tracing_stop_reason);
3505
3506 if (agent_loaded_p ())
3507 {
3508 pause_all (1);
3509
3510 upload_fast_traceframes ();
3511
3512 unpause_all (1);
3513 }
3514
3515 stop_reason_rsp = (char *) tracing_stop_reason;
3516
3517 /* The user visible error string in terror needs to be hex encoded.
3518 We leave it as plain string in `tracing_stop_reason' to ease
3519 debugging. */
3520 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3521 {
3522 const char *result_name;
3523 int hexstr_len;
3524 char *p;
3525
3526 result_name = stop_reason_rsp + strlen ("terror:");
3527 hexstr_len = strlen (result_name) * 2;
3528 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3529 strcpy (p, "terror:");
3530 p += strlen (p);
3531 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3532 }
3533
3534 /* If this was a forced stop, include any stop note that was supplied. */
3535 if (strcmp (stop_reason_rsp, "tstop") == 0)
3536 {
3537 stop_reason_rsp = alloca (strlen ("tstop:") + strlen (buf3) + 1);
3538 strcpy (stop_reason_rsp, "tstop:");
3539 strcat (stop_reason_rsp, buf3);
3540 }
3541
3542 sprintf (packet,
3543 "T%d;"
3544 "%s:%x;"
3545 "tframes:%x;tcreated:%x;"
3546 "tfree:%x;tsize:%s;"
3547 "circular:%d;"
3548 "disconn:%d;"
3549 "starttime:%s;stoptime:%s;"
3550 "username:%s:;notes:%s:",
3551 tracing ? 1 : 0,
3552 stop_reason_rsp, tracing_stop_tpnum,
3553 traceframe_count, traceframes_created,
3554 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3555 circular_trace_buffer,
3556 disconnected_tracing,
3557 plongest (tracing_start_time), plongest (tracing_stop_time),
3558 buf1, buf2);
3559 }
3560
3561 static void
3562 cmd_qtp (char *own_buf)
3563 {
3564 ULONGEST num, addr;
3565 struct tracepoint *tpoint;
3566 char *packet = own_buf;
3567
3568 packet += strlen ("qTP:");
3569
3570 packet = unpack_varlen_hex (packet, &num);
3571 ++packet; /* skip a colon */
3572 packet = unpack_varlen_hex (packet, &addr);
3573
3574 /* See if we already have this tracepoint. */
3575 tpoint = find_tracepoint (num, addr);
3576
3577 if (!tpoint)
3578 {
3579 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3580 (int) num, paddress (addr));
3581 write_enn (own_buf);
3582 return;
3583 }
3584
3585 sprintf (own_buf, "V%" PRIu64 ":%" PRIu64 "", tpoint->hit_count,
3586 tpoint->traceframe_usage);
3587 }
3588
3589 /* State variables to help return all the tracepoint bits. */
3590 static struct tracepoint *cur_tpoint;
3591 static int cur_action;
3592 static int cur_step_action;
3593 static struct source_string *cur_source_string;
3594 static struct trace_state_variable *cur_tsv;
3595
3596 /* Compose a response that is an imitation of the syntax by which the
3597 tracepoint was originally downloaded. */
3598
3599 static void
3600 response_tracepoint (char *packet, struct tracepoint *tpoint)
3601 {
3602 char *buf;
3603
3604 sprintf (packet, "T%x:%s:%c:%" PRIx64 ":%" PRIx64, tpoint->number,
3605 paddress (tpoint->address),
3606 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3607 tpoint->pass_count);
3608 if (tpoint->type == fast_tracepoint)
3609 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
3610 else if (tpoint->type == static_tracepoint)
3611 sprintf (packet + strlen (packet), ":S");
3612
3613 if (tpoint->cond)
3614 {
3615 buf = gdb_unparse_agent_expr (tpoint->cond);
3616 sprintf (packet + strlen (packet), ":X%x,%s",
3617 tpoint->cond->length, buf);
3618 free (buf);
3619 }
3620 }
3621
3622 /* Compose a response that is an imitation of the syntax by which the
3623 tracepoint action was originally downloaded (with the difference
3624 that due to the way we store the actions, this will output a packet
3625 per action, while GDB could have combined more than one action
3626 per-packet. */
3627
3628 static void
3629 response_action (char *packet, struct tracepoint *tpoint,
3630 char *taction, int step)
3631 {
3632 sprintf (packet, "%c%x:%s:%s",
3633 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3634 taction);
3635 }
3636
3637 /* Compose a response that is an imitation of the syntax by which the
3638 tracepoint source piece was originally downloaded. */
3639
3640 static void
3641 response_source (char *packet,
3642 struct tracepoint *tpoint, struct source_string *src)
3643 {
3644 char *buf;
3645 int len;
3646
3647 len = strlen (src->str);
3648 buf = alloca (len * 2 + 1);
3649 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3650
3651 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3652 tpoint->number, paddress (tpoint->address),
3653 src->type, 0, len, buf);
3654 }
3655
3656 /* Return the first piece of tracepoint definition, and initialize the
3657 state machine that will iterate through all the tracepoint
3658 bits. */
3659
3660 static void
3661 cmd_qtfp (char *packet)
3662 {
3663 trace_debug ("Returning first tracepoint definition piece");
3664
3665 cur_tpoint = tracepoints;
3666 cur_action = cur_step_action = -1;
3667 cur_source_string = NULL;
3668
3669 if (cur_tpoint)
3670 response_tracepoint (packet, cur_tpoint);
3671 else
3672 strcpy (packet, "l");
3673 }
3674
3675 /* Return additional pieces of tracepoint definition. Each action and
3676 stepping action must go into its own packet, because of packet size
3677 limits, and so we use state variables to deliver one piece at a
3678 time. */
3679
3680 static void
3681 cmd_qtsp (char *packet)
3682 {
3683 trace_debug ("Returning subsequent tracepoint definition piece");
3684
3685 if (!cur_tpoint)
3686 {
3687 /* This case would normally never occur, but be prepared for
3688 GDB misbehavior. */
3689 strcpy (packet, "l");
3690 }
3691 else if (cur_action < cur_tpoint->numactions - 1)
3692 {
3693 ++cur_action;
3694 response_action (packet, cur_tpoint,
3695 cur_tpoint->actions_str[cur_action], 0);
3696 }
3697 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3698 {
3699 ++cur_step_action;
3700 response_action (packet, cur_tpoint,
3701 cur_tpoint->step_actions_str[cur_step_action], 1);
3702 }
3703 else if ((cur_source_string
3704 ? cur_source_string->next
3705 : cur_tpoint->source_strings))
3706 {
3707 if (cur_source_string)
3708 cur_source_string = cur_source_string->next;
3709 else
3710 cur_source_string = cur_tpoint->source_strings;
3711 response_source (packet, cur_tpoint, cur_source_string);
3712 }
3713 else
3714 {
3715 cur_tpoint = cur_tpoint->next;
3716 cur_action = cur_step_action = -1;
3717 cur_source_string = NULL;
3718 if (cur_tpoint)
3719 response_tracepoint (packet, cur_tpoint);
3720 else
3721 strcpy (packet, "l");
3722 }
3723 }
3724
3725 /* Compose a response that is an imitation of the syntax by which the
3726 trace state variable was originally downloaded. */
3727
3728 static void
3729 response_tsv (char *packet, struct trace_state_variable *tsv)
3730 {
3731 char *buf = (char *) "";
3732 int namelen;
3733
3734 if (tsv->name)
3735 {
3736 namelen = strlen (tsv->name);
3737 buf = alloca (namelen * 2 + 1);
3738 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3739 }
3740
3741 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3742 tsv->getter ? 1 : 0, buf);
3743 }
3744
3745 /* Return the first trace state variable definition, and initialize
3746 the state machine that will iterate through all the tsv bits. */
3747
3748 static void
3749 cmd_qtfv (char *packet)
3750 {
3751 trace_debug ("Returning first trace state variable definition");
3752
3753 cur_tsv = trace_state_variables;
3754
3755 if (cur_tsv)
3756 response_tsv (packet, cur_tsv);
3757 else
3758 strcpy (packet, "l");
3759 }
3760
3761 /* Return additional trace state variable definitions. */
3762
3763 static void
3764 cmd_qtsv (char *packet)
3765 {
3766 trace_debug ("Returning first trace state variable definition");
3767
3768 if (!cur_tpoint)
3769 {
3770 /* This case would normally never occur, but be prepared for
3771 GDB misbehavior. */
3772 strcpy (packet, "l");
3773 }
3774 else if (cur_tsv)
3775 {
3776 cur_tsv = cur_tsv->next;
3777 if (cur_tsv)
3778 response_tsv (packet, cur_tsv);
3779 else
3780 strcpy (packet, "l");
3781 }
3782 else
3783 strcpy (packet, "l");
3784 }
3785
3786 /* Return the first static tracepoint marker, and initialize the state
3787 machine that will iterate through all the static tracepoints
3788 markers. */
3789
3790 static void
3791 cmd_qtfstm (char *packet)
3792 {
3793 if (!maybe_write_ipa_ust_not_loaded (packet))
3794 run_inferior_command (packet);
3795 }
3796
3797 /* Return additional static tracepoints markers. */
3798
3799 static void
3800 cmd_qtsstm (char *packet)
3801 {
3802 if (!maybe_write_ipa_ust_not_loaded (packet))
3803 run_inferior_command (packet);
3804 }
3805
3806 /* Return the definition of the static tracepoint at a given address.
3807 Result packet is the same as qTsST's. */
3808
3809 static void
3810 cmd_qtstmat (char *packet)
3811 {
3812 if (!maybe_write_ipa_ust_not_loaded (packet))
3813 run_inferior_command (packet);
3814 }
3815
3816 /* Return the minimum instruction size needed for fast tracepoints as a
3817 hexadecimal number. */
3818
3819 static void
3820 cmd_qtminftpilen (char *packet)
3821 {
3822 if (current_inferior == NULL)
3823 {
3824 /* Indicate that the minimum length is currently unknown. */
3825 strcpy (packet, "0");
3826 return;
3827 }
3828
3829 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
3830 }
3831
3832 /* Respond to qTBuffer packet with a block of raw data from the trace
3833 buffer. GDB may ask for a lot, but we are allowed to reply with
3834 only as much as will fit within packet limits or whatever. */
3835
3836 static void
3837 cmd_qtbuffer (char *own_buf)
3838 {
3839 ULONGEST offset, num, tot;
3840 unsigned char *tbp;
3841 char *packet = own_buf;
3842
3843 packet += strlen ("qTBuffer:");
3844
3845 packet = unpack_varlen_hex (packet, &offset);
3846 ++packet; /* skip a comma */
3847 unpack_varlen_hex (packet, &num);
3848
3849 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3850 (int) num, pulongest (offset));
3851
3852 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3853
3854 /* If we're right at the end, reply specially that we're done. */
3855 if (offset == tot)
3856 {
3857 strcpy (own_buf, "l");
3858 return;
3859 }
3860
3861 /* Object to any other out-of-bounds request. */
3862 if (offset > tot)
3863 {
3864 write_enn (own_buf);
3865 return;
3866 }
3867
3868 /* Compute the pointer corresponding to the given offset, accounting
3869 for wraparound. */
3870 tbp = trace_buffer_start + offset;
3871 if (tbp >= trace_buffer_wrap)
3872 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3873
3874 /* Trim to the remaining bytes if we're close to the end. */
3875 if (num > tot - offset)
3876 num = tot - offset;
3877
3878 /* Trim to available packet size. */
3879 if (num >= (PBUFSIZ - 16) / 2 )
3880 num = (PBUFSIZ - 16) / 2;
3881
3882 convert_int_to_ascii (tbp, own_buf, num);
3883 own_buf[num] = '\0';
3884 }
3885
3886 static void
3887 cmd_bigqtbuffer_circular (char *own_buf)
3888 {
3889 ULONGEST val;
3890 char *packet = own_buf;
3891
3892 packet += strlen ("QTBuffer:circular:");
3893
3894 unpack_varlen_hex (packet, &val);
3895 circular_trace_buffer = val;
3896 trace_debug ("Trace buffer is now %s",
3897 circular_trace_buffer ? "circular" : "linear");
3898 write_ok (own_buf);
3899 }
3900
3901 static void
3902 cmd_qtnotes (char *own_buf)
3903 {
3904 size_t nbytes;
3905 char *saved, *user, *notes, *stopnote;
3906 char *packet = own_buf;
3907
3908 packet += strlen ("QTNotes:");
3909
3910 while (*packet)
3911 {
3912 if (strncmp ("user:", packet, strlen ("user:")) == 0)
3913 {
3914 packet += strlen ("user:");
3915 saved = packet;
3916 packet = strchr (packet, ';');
3917 nbytes = (packet - saved) / 2;
3918 user = xmalloc (nbytes + 1);
3919 nbytes = unhexify (user, saved, nbytes);
3920 user[nbytes] = '\0';
3921 ++packet; /* skip the semicolon */
3922 trace_debug ("User is '%s'", user);
3923 tracing_user_name = user;
3924 }
3925 else if (strncmp ("notes:", packet, strlen ("notes:")) == 0)
3926 {
3927 packet += strlen ("notes:");
3928 saved = packet;
3929 packet = strchr (packet, ';');
3930 nbytes = (packet - saved) / 2;
3931 notes = xmalloc (nbytes + 1);
3932 nbytes = unhexify (notes, saved, nbytes);
3933 notes[nbytes] = '\0';
3934 ++packet; /* skip the semicolon */
3935 trace_debug ("Notes is '%s'", notes);
3936 tracing_notes = notes;
3937 }
3938 else if (strncmp ("tstop:", packet, strlen ("tstop:")) == 0)
3939 {
3940 packet += strlen ("tstop:");
3941 saved = packet;
3942 packet = strchr (packet, ';');
3943 nbytes = (packet - saved) / 2;
3944 stopnote = xmalloc (nbytes + 1);
3945 nbytes = unhexify (stopnote, saved, nbytes);
3946 stopnote[nbytes] = '\0';
3947 ++packet; /* skip the semicolon */
3948 trace_debug ("tstop note is '%s'", stopnote);
3949 tracing_stop_note = stopnote;
3950 }
3951 else
3952 break;
3953 }
3954
3955 write_ok (own_buf);
3956 }
3957
3958 int
3959 handle_tracepoint_general_set (char *packet)
3960 {
3961 if (strcmp ("QTinit", packet) == 0)
3962 {
3963 cmd_qtinit (packet);
3964 return 1;
3965 }
3966 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3967 {
3968 cmd_qtdp (packet);
3969 return 1;
3970 }
3971 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3972 {
3973 cmd_qtdpsrc (packet);
3974 return 1;
3975 }
3976 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
3977 {
3978 cmd_qtenable_disable (packet, 1);
3979 return 1;
3980 }
3981 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
3982 {
3983 cmd_qtenable_disable (packet, 0);
3984 return 1;
3985 }
3986 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3987 {
3988 cmd_qtdv (packet);
3989 return 1;
3990 }
3991 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3992 {
3993 cmd_qtro (packet);
3994 return 1;
3995 }
3996 else if (strcmp ("QTStart", packet) == 0)
3997 {
3998 cmd_qtstart (packet);
3999 return 1;
4000 }
4001 else if (strcmp ("QTStop", packet) == 0)
4002 {
4003 cmd_qtstop (packet);
4004 return 1;
4005 }
4006 else if (strncmp ("QTDisconnected:", packet,
4007 strlen ("QTDisconnected:")) == 0)
4008 {
4009 cmd_qtdisconnected (packet);
4010 return 1;
4011 }
4012 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
4013 {
4014 cmd_qtframe (packet);
4015 return 1;
4016 }
4017 else if (strncmp ("QTBuffer:circular:", packet, strlen ("QTBuffer:circular:")) == 0)
4018 {
4019 cmd_bigqtbuffer_circular (packet);
4020 return 1;
4021 }
4022 else if (strncmp ("QTNotes:", packet, strlen ("QTNotes:")) == 0)
4023 {
4024 cmd_qtnotes (packet);
4025 return 1;
4026 }
4027
4028 return 0;
4029 }
4030
4031 int
4032 handle_tracepoint_query (char *packet)
4033 {
4034 if (strcmp ("qTStatus", packet) == 0)
4035 {
4036 cmd_qtstatus (packet);
4037 return 1;
4038 }
4039 else if (strncmp ("qTP:", packet, strlen ("qTP:")) == 0)
4040 {
4041 cmd_qtp (packet);
4042 return 1;
4043 }
4044 else if (strcmp ("qTfP", packet) == 0)
4045 {
4046 cmd_qtfp (packet);
4047 return 1;
4048 }
4049 else if (strcmp ("qTsP", packet) == 0)
4050 {
4051 cmd_qtsp (packet);
4052 return 1;
4053 }
4054 else if (strcmp ("qTfV", packet) == 0)
4055 {
4056 cmd_qtfv (packet);
4057 return 1;
4058 }
4059 else if (strcmp ("qTsV", packet) == 0)
4060 {
4061 cmd_qtsv (packet);
4062 return 1;
4063 }
4064 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
4065 {
4066 cmd_qtv (packet);
4067 return 1;
4068 }
4069 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
4070 {
4071 cmd_qtbuffer (packet);
4072 return 1;
4073 }
4074 else if (strcmp ("qTfSTM", packet) == 0)
4075 {
4076 cmd_qtfstm (packet);
4077 return 1;
4078 }
4079 else if (strcmp ("qTsSTM", packet) == 0)
4080 {
4081 cmd_qtsstm (packet);
4082 return 1;
4083 }
4084 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
4085 {
4086 cmd_qtstmat (packet);
4087 return 1;
4088 }
4089 else if (strcmp ("qTMinFTPILen", packet) == 0)
4090 {
4091 cmd_qtminftpilen (packet);
4092 return 1;
4093 }
4094
4095 return 0;
4096 }
4097
4098 #endif
4099 #ifndef IN_PROCESS_AGENT
4100
4101 /* Call this when thread TINFO has hit the tracepoint defined by
4102 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4103 action. This adds a while-stepping collecting state item to the
4104 threads' collecting state list, so that we can keep track of
4105 multiple simultaneous while-stepping actions being collected by the
4106 same thread. This can happen in cases like:
4107
4108 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4109 ff0002 INSN2
4110 ff0003 INSN3 <-- TP2, collect $regs
4111 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4112 ff0005 INSN5
4113
4114 Notice that when instruction INSN5 is reached, the while-stepping
4115 actions of both TP1 and TP3 are still being collected, and that TP2
4116 had been collected meanwhile. The whole range of ff0001-ff0005
4117 should be single-stepped, due to at least TP1's while-stepping
4118 action covering the whole range. */
4119
4120 static void
4121 add_while_stepping_state (struct thread_info *tinfo,
4122 int tp_number, CORE_ADDR tp_address)
4123 {
4124 struct wstep_state *wstep;
4125
4126 wstep = xmalloc (sizeof (*wstep));
4127 wstep->next = tinfo->while_stepping;
4128
4129 wstep->tp_number = tp_number;
4130 wstep->tp_address = tp_address;
4131 wstep->current_step = 0;
4132
4133 tinfo->while_stepping = wstep;
4134 }
4135
4136 /* Release the while-stepping collecting state WSTEP. */
4137
4138 static void
4139 release_while_stepping_state (struct wstep_state *wstep)
4140 {
4141 free (wstep);
4142 }
4143
4144 /* Release all while-stepping collecting states currently associated
4145 with thread TINFO. */
4146
4147 void
4148 release_while_stepping_state_list (struct thread_info *tinfo)
4149 {
4150 struct wstep_state *head;
4151
4152 while (tinfo->while_stepping)
4153 {
4154 head = tinfo->while_stepping;
4155 tinfo->while_stepping = head->next;
4156 release_while_stepping_state (head);
4157 }
4158 }
4159
4160 /* If TINFO was handling a 'while-stepping' action, the step has
4161 finished, so collect any step data needed, and check if any more
4162 steps are required. Return true if the thread was indeed
4163 collecting tracepoint data, false otherwise. */
4164
4165 int
4166 tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
4167 {
4168 struct tracepoint *tpoint;
4169 struct wstep_state *wstep;
4170 struct wstep_state **wstep_link;
4171 struct trap_tracepoint_ctx ctx;
4172
4173 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4174 our buffer. */
4175 if (agent_loaded_p ())
4176 upload_fast_traceframes ();
4177
4178 /* Check if we were indeed collecting data for one of more
4179 tracepoints with a 'while-stepping' count. */
4180 if (tinfo->while_stepping == NULL)
4181 return 0;
4182
4183 if (!tracing)
4184 {
4185 /* We're not even tracing anymore. Stop this thread from
4186 collecting. */
4187 release_while_stepping_state_list (tinfo);
4188
4189 /* The thread had stopped due to a single-step request indeed
4190 explained by a tracepoint. */
4191 return 1;
4192 }
4193
4194 wstep = tinfo->while_stepping;
4195 wstep_link = &tinfo->while_stepping;
4196
4197 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4198 target_pid_to_str (tinfo->entry.id),
4199 wstep->tp_number, paddress (wstep->tp_address));
4200
4201 ctx.base.type = trap_tracepoint;
4202 ctx.regcache = get_thread_regcache (tinfo, 1);
4203
4204 while (wstep != NULL)
4205 {
4206 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
4207 if (tpoint == NULL)
4208 {
4209 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4210 wstep->tp_number, paddress (wstep->tp_address),
4211 target_pid_to_str (tinfo->entry.id));
4212
4213 /* Unlink. */
4214 *wstep_link = wstep->next;
4215 release_while_stepping_state (wstep);
4216 wstep = *wstep_link;
4217 continue;
4218 }
4219
4220 /* We've just finished one step. */
4221 ++wstep->current_step;
4222
4223 /* Collect data. */
4224 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
4225 stop_pc, tpoint, wstep->current_step);
4226
4227 if (wstep->current_step >= tpoint->step_count)
4228 {
4229 /* The requested numbers of steps have occurred. */
4230 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4231 target_pid_to_str (tinfo->entry.id),
4232 wstep->tp_number, paddress (wstep->tp_address));
4233
4234 /* Unlink the wstep. */
4235 *wstep_link = wstep->next;
4236 release_while_stepping_state (wstep);
4237 wstep = *wstep_link;
4238
4239 /* Only check the hit count now, which ensure that we do all
4240 our stepping before stopping the run. */
4241 if (tpoint->pass_count > 0
4242 && tpoint->hit_count >= tpoint->pass_count
4243 && stopping_tracepoint == NULL)
4244 stopping_tracepoint = tpoint;
4245 }
4246 else
4247 {
4248 /* Keep single-stepping until the requested numbers of steps
4249 have occurred. */
4250 wstep_link = &wstep->next;
4251 wstep = *wstep_link;
4252 }
4253
4254 if (stopping_tracepoint
4255 || trace_buffer_is_full
4256 || expr_eval_result != expr_eval_no_error)
4257 {
4258 stop_tracing ();
4259 break;
4260 }
4261 }
4262
4263 return 1;
4264 }
4265
4266 /* Handle any internal tracing control breakpoint hits. That means,
4267 pull traceframes from the IPA to our buffer, and syncing both
4268 tracing agents when the IPA's tracing stops for some reason. */
4269
4270 int
4271 handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4272 {
4273 /* Pull in fast tracepoint trace frames from the inferior in-process
4274 agent's buffer into our buffer. */
4275
4276 if (!agent_loaded_p ())
4277 return 0;
4278
4279 upload_fast_traceframes ();
4280
4281 /* Check if the in-process agent had decided we should stop
4282 tracing. */
4283 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4284 {
4285 int ipa_trace_buffer_is_full;
4286 CORE_ADDR ipa_stopping_tracepoint;
4287 int ipa_expr_eval_result;
4288 CORE_ADDR ipa_error_tracepoint;
4289
4290 trace_debug ("lib stopped at stop_tracing");
4291
4292 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4293 &ipa_trace_buffer_is_full);
4294
4295 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4296 &ipa_stopping_tracepoint);
4297 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4298
4299 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4300 &ipa_error_tracepoint);
4301 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4302
4303 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4304 &ipa_expr_eval_result);
4305 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4306
4307 trace_debug ("lib: trace_buffer_is_full: %d, "
4308 "stopping_tracepoint: %s, "
4309 "ipa_expr_eval_result: %d, "
4310 "error_tracepoint: %s, ",
4311 ipa_trace_buffer_is_full,
4312 paddress (ipa_stopping_tracepoint),
4313 ipa_expr_eval_result,
4314 paddress (ipa_error_tracepoint));
4315
4316 if (debug_threads)
4317 {
4318 if (ipa_trace_buffer_is_full)
4319 trace_debug ("lib stopped due to full buffer.");
4320 if (ipa_stopping_tracepoint)
4321 trace_debug ("lib stopped due to tpoint");
4322 if (ipa_stopping_tracepoint)
4323 trace_debug ("lib stopped due to error");
4324 }
4325
4326 if (ipa_stopping_tracepoint != 0)
4327 {
4328 stopping_tracepoint
4329 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4330 }
4331 else if (ipa_expr_eval_result != expr_eval_no_error)
4332 {
4333 expr_eval_result = ipa_expr_eval_result;
4334 error_tracepoint
4335 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4336 }
4337 stop_tracing ();
4338 return 1;
4339 }
4340 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4341 {
4342 trace_debug ("lib stopped at flush_trace_buffer");
4343 return 1;
4344 }
4345
4346 return 0;
4347 }
4348
4349 /* Return true if TINFO just hit a tracepoint. Collect data if
4350 so. */
4351
4352 int
4353 tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4354 {
4355 struct tracepoint *tpoint;
4356 int ret = 0;
4357 struct trap_tracepoint_ctx ctx;
4358
4359 /* Not tracing, don't handle. */
4360 if (!tracing)
4361 return 0;
4362
4363 ctx.base.type = trap_tracepoint;
4364 ctx.regcache = get_thread_regcache (tinfo, 1);
4365
4366 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4367 {
4368 /* Note that we collect fast tracepoints here as well. We'll
4369 step over the fast tracepoint jump later, which avoids the
4370 double collect. However, we don't collect for static
4371 tracepoints here, because UST markers are compiled in program,
4372 and probes will be executed in program. So static tracepoints
4373 are collected there. */
4374 if (tpoint->enabled && stop_pc == tpoint->address
4375 && tpoint->type != static_tracepoint)
4376 {
4377 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4378 target_pid_to_str (tinfo->entry.id),
4379 tpoint->number, paddress (tpoint->address));
4380
4381 /* Test the condition if present, and collect if true. */
4382 if (!tpoint->cond
4383 || (condition_true_at_tracepoint
4384 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4385 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4386 stop_pc, tpoint);
4387
4388 if (stopping_tracepoint
4389 || trace_buffer_is_full
4390 || expr_eval_result != expr_eval_no_error)
4391 {
4392 stop_tracing ();
4393 }
4394 /* If the tracepoint had a 'while-stepping' action, then set
4395 the thread to collect this tracepoint on the following
4396 single-steps. */
4397 else if (tpoint->step_count > 0)
4398 {
4399 add_while_stepping_state (tinfo,
4400 tpoint->number, tpoint->address);
4401 }
4402
4403 ret = 1;
4404 }
4405 }
4406
4407 return ret;
4408 }
4409
4410 #endif
4411
4412 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4413 struct ust_marker_data;
4414 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4415 struct traceframe *tframe);
4416 #endif
4417
4418 /* Create a trace frame for the hit of the given tracepoint in the
4419 given thread. */
4420
4421 static void
4422 collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4423 struct tracepoint *tpoint)
4424 {
4425 struct traceframe *tframe;
4426 int acti;
4427
4428 /* Only count it as a hit when we actually collect data. */
4429 tpoint->hit_count++;
4430
4431 /* If we've exceeded a defined pass count, record the event for
4432 later, and finish the collection for this hit. This test is only
4433 for nonstepping tracepoints, stepping tracepoints test at the end
4434 of their while-stepping loop. */
4435 if (tpoint->pass_count > 0
4436 && tpoint->hit_count >= tpoint->pass_count
4437 && tpoint->step_count == 0
4438 && stopping_tracepoint == NULL)
4439 stopping_tracepoint = tpoint;
4440
4441 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64,
4442 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4443
4444 tframe = add_traceframe (tpoint);
4445
4446 if (tframe)
4447 {
4448 for (acti = 0; acti < tpoint->numactions; ++acti)
4449 {
4450 #ifndef IN_PROCESS_AGENT
4451 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4452 tpoint->number, paddress (tpoint->address),
4453 tpoint->actions_str[acti]);
4454 #endif
4455
4456 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4457 tpoint->actions[acti]);
4458 }
4459
4460 finish_traceframe (tframe);
4461 }
4462
4463 if (tframe == NULL && tracing)
4464 trace_buffer_is_full = 1;
4465 }
4466
4467 #ifndef IN_PROCESS_AGENT
4468
4469 static void
4470 collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4471 CORE_ADDR stop_pc,
4472 struct tracepoint *tpoint, int current_step)
4473 {
4474 struct traceframe *tframe;
4475 int acti;
4476
4477 trace_debug ("Making new step traceframe for "
4478 "tracepoint %d at 0x%s, step %d of %" PRIu64 ", hit %" PRIu64,
4479 tpoint->number, paddress (tpoint->address),
4480 current_step, tpoint->step_count,
4481 tpoint->hit_count);
4482
4483 tframe = add_traceframe (tpoint);
4484
4485 if (tframe)
4486 {
4487 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4488 {
4489 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4490 tpoint->number, paddress (tpoint->address),
4491 tpoint->step_actions_str[acti]);
4492
4493 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4494 tpoint->step_actions[acti]);
4495 }
4496
4497 finish_traceframe (tframe);
4498 }
4499
4500 if (tframe == NULL && tracing)
4501 trace_buffer_is_full = 1;
4502 }
4503
4504 #endif
4505
4506 static struct regcache *
4507 get_context_regcache (struct tracepoint_hit_ctx *ctx)
4508 {
4509 struct regcache *regcache = NULL;
4510
4511 #ifdef IN_PROCESS_AGENT
4512 if (ctx->type == fast_tracepoint)
4513 {
4514 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4515 if (!fctx->regcache_initted)
4516 {
4517 fctx->regcache_initted = 1;
4518 init_register_cache (&fctx->regcache, fctx->regspace);
4519 supply_regblock (&fctx->regcache, NULL);
4520 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4521 }
4522 regcache = &fctx->regcache;
4523 }
4524 #ifdef HAVE_UST
4525 if (ctx->type == static_tracepoint)
4526 {
4527 struct static_tracepoint_ctx *sctx
4528 = (struct static_tracepoint_ctx *) ctx;
4529
4530 if (!sctx->regcache_initted)
4531 {
4532 sctx->regcache_initted = 1;
4533 init_register_cache (&sctx->regcache, sctx->regspace);
4534 supply_regblock (&sctx->regcache, NULL);
4535 /* Pass down the tracepoint address, because REGS doesn't
4536 include the PC, but we know what it must have been. */
4537 supply_static_tracepoint_registers (&sctx->regcache,
4538 (const unsigned char *)
4539 sctx->regs,
4540 sctx->tpoint->address);
4541 }
4542 regcache = &sctx->regcache;
4543 }
4544 #endif
4545 #else
4546 if (ctx->type == trap_tracepoint)
4547 {
4548 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4549 regcache = tctx->regcache;
4550 }
4551 #endif
4552
4553 gdb_assert (regcache != NULL);
4554
4555 return regcache;
4556 }
4557
4558 static void
4559 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4560 CORE_ADDR stop_pc,
4561 struct tracepoint *tpoint,
4562 struct traceframe *tframe,
4563 struct tracepoint_action *taction)
4564 {
4565 enum eval_result_type err;
4566
4567 switch (taction->type)
4568 {
4569 case 'M':
4570 {
4571 struct collect_memory_action *maction;
4572
4573 maction = (struct collect_memory_action *) taction;
4574
4575 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4576 pulongest (maction->len),
4577 paddress (maction->addr), maction->basereg);
4578 /* (should use basereg) */
4579 agent_mem_read (tframe, NULL,
4580 (CORE_ADDR) maction->addr, maction->len);
4581 break;
4582 }
4583 case 'R':
4584 {
4585 unsigned char *regspace;
4586 struct regcache tregcache;
4587 struct regcache *context_regcache;
4588
4589
4590 trace_debug ("Want to collect registers");
4591
4592 /* Collect all registers for now. */
4593 regspace = add_traceframe_block (tframe,
4594 1 + register_cache_size ());
4595 if (regspace == NULL)
4596 {
4597 trace_debug ("Trace buffer block allocation failed, skipping");
4598 break;
4599 }
4600 /* Identify a register block. */
4601 *regspace = 'R';
4602
4603 context_regcache = get_context_regcache (ctx);
4604
4605 /* Wrap the regblock in a register cache (in the stack, we
4606 don't want to malloc here). */
4607 init_register_cache (&tregcache, regspace + 1);
4608
4609 /* Copy the register data to the regblock. */
4610 regcache_cpy (&tregcache, context_regcache);
4611
4612 #ifndef IN_PROCESS_AGENT
4613 /* On some platforms, trap-based tracepoints will have the PC
4614 pointing to the next instruction after the trap, but we
4615 don't want the user or GDB trying to guess whether the
4616 saved PC needs adjusting; so always record the adjusted
4617 stop_pc. Note that we can't use tpoint->address instead,
4618 since it will be wrong for while-stepping actions. This
4619 adjustment is a nop for fast tracepoints collected from the
4620 in-process lib (but not if GDBserver is collecting one
4621 preemptively), since the PC had already been adjusted to
4622 contain the tracepoint's address by the jump pad. */
4623 trace_debug ("Storing stop pc (0x%s) in regblock",
4624 paddress (stop_pc));
4625
4626 /* This changes the regblock, not the thread's
4627 regcache. */
4628 regcache_write_pc (&tregcache, stop_pc);
4629 #endif
4630 }
4631 break;
4632 case 'X':
4633 {
4634 struct eval_expr_action *eaction;
4635
4636 eaction = (struct eval_expr_action *) taction;
4637
4638 trace_debug ("Want to evaluate expression");
4639
4640 err = eval_tracepoint_agent_expr (ctx, tframe, eaction->expr, NULL);
4641
4642 if (err != expr_eval_no_error)
4643 {
4644 record_tracepoint_error (tpoint, "action expression", err);
4645 return;
4646 }
4647 }
4648 break;
4649 case 'L':
4650 {
4651 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4652 trace_debug ("Want to collect static trace data");
4653 collect_ust_data_at_tracepoint (ctx, tframe);
4654 #else
4655 trace_debug ("warning: collecting static trace data, "
4656 "but static tracepoints are not supported");
4657 #endif
4658 }
4659 break;
4660 default:
4661 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4662 break;
4663 }
4664 }
4665
4666 static int
4667 condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4668 struct tracepoint *tpoint)
4669 {
4670 ULONGEST value = 0;
4671 enum eval_result_type err;
4672
4673 /* Presently, gdbserver doesn't run compiled conditions, only the
4674 IPA does. If the program stops at a fast tracepoint's address
4675 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4676 gdbserver preemptively collect the fast tracepoint. Later, on
4677 resume, gdbserver steps over the fast tracepoint like it steps
4678 over breakpoints, so that the IPA doesn't see that fast
4679 tracepoint. This avoids double collects of fast tracepoints in
4680 that stopping scenario. Having gdbserver itself handle the fast
4681 tracepoint gives the user a consistent view of when fast or trap
4682 tracepoints are collected, compared to an alternative where only
4683 trap tracepoints are collected on stop, and fast tracepoints on
4684 resume. When a fast tracepoint is being processed by gdbserver,
4685 it is always the non-compiled condition expression that is
4686 used. */
4687 #ifdef IN_PROCESS_AGENT
4688 if (tpoint->compiled_cond)
4689 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4690 else
4691 #endif
4692 err = eval_tracepoint_agent_expr (ctx, NULL, tpoint->cond, &value);
4693
4694 if (err != expr_eval_no_error)
4695 {
4696 record_tracepoint_error (tpoint, "condition", err);
4697 /* The error case must return false. */
4698 return 0;
4699 }
4700
4701 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4702 tpoint->number, paddress (tpoint->address),
4703 pulongest (value));
4704 return (value ? 1 : 0);
4705 }
4706
4707 /* Evaluates a tracepoint agent expression with context CTX,
4708 traceframe TFRAME, agent expression AEXPR and store the
4709 result in RSLT. */
4710
4711 static enum eval_result_type
4712 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx *ctx,
4713 struct traceframe *tframe,
4714 struct agent_expr *aexpr,
4715 ULONGEST *rslt)
4716 {
4717 struct regcache *regcache;
4718 regcache = get_context_regcache (ctx);
4719
4720 return gdb_eval_agent_expr (regcache, tframe, aexpr, rslt);
4721 }
4722
4723 /* Do memory copies for bytecodes. */
4724 /* Do the recording of memory blocks for actions and bytecodes. */
4725
4726 int
4727 agent_mem_read (struct traceframe *tframe,
4728 unsigned char *to, CORE_ADDR from, ULONGEST len)
4729 {
4730 unsigned char *mspace;
4731 ULONGEST remaining = len;
4732 unsigned short blocklen;
4733
4734 /* If a 'to' buffer is specified, use it. */
4735 if (to != NULL)
4736 {
4737 read_inferior_memory (from, to, len);
4738 return 0;
4739 }
4740
4741 /* Otherwise, create a new memory block in the trace buffer. */
4742 while (remaining > 0)
4743 {
4744 size_t sp;
4745
4746 blocklen = (remaining > 65535 ? 65535 : remaining);
4747 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4748 mspace = add_traceframe_block (tframe, sp);
4749 if (mspace == NULL)
4750 return 1;
4751 /* Identify block as a memory block. */
4752 *mspace = 'M';
4753 ++mspace;
4754 /* Record address and size. */
4755 memcpy (mspace, &from, sizeof (from));
4756 mspace += sizeof (from);
4757 memcpy (mspace, &blocklen, sizeof (blocklen));
4758 mspace += sizeof (blocklen);
4759 /* Record the memory block proper. */
4760 read_inferior_memory (from, mspace, blocklen);
4761 trace_debug ("%d bytes recorded", blocklen);
4762 remaining -= blocklen;
4763 from += blocklen;
4764 }
4765 return 0;
4766 }
4767
4768 int
4769 agent_mem_read_string (struct traceframe *tframe,
4770 unsigned char *to, CORE_ADDR from, ULONGEST len)
4771 {
4772 unsigned char *buf, *mspace;
4773 ULONGEST remaining = len;
4774 unsigned short blocklen, i;
4775
4776 /* To save a bit of space, block lengths are 16-bit, so break large
4777 requests into multiple blocks. Bordering on overkill for strings,
4778 but it could happen that someone specifies a large max length. */
4779 while (remaining > 0)
4780 {
4781 size_t sp;
4782
4783 blocklen = (remaining > 65535 ? 65535 : remaining);
4784 /* We want working space to accumulate nonzero bytes, since
4785 traceframes must have a predecided size (otherwise it gets
4786 harder to wrap correctly for the circular case, etc). */
4787 buf = (unsigned char *) xmalloc (blocklen + 1);
4788 for (i = 0; i < blocklen; ++i)
4789 {
4790 /* Read the string one byte at a time, in case the string is
4791 at the end of a valid memory area - we don't want a
4792 correctly-terminated string to engender segvio
4793 complaints. */
4794 read_inferior_memory (from + i, buf + i, 1);
4795
4796 if (buf[i] == '\0')
4797 {
4798 blocklen = i + 1;
4799 /* Make sure outer loop stops now too. */
4800 remaining = blocklen;
4801 break;
4802 }
4803 }
4804 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4805 mspace = add_traceframe_block (tframe, sp);
4806 if (mspace == NULL)
4807 {
4808 xfree (buf);
4809 return 1;
4810 }
4811 /* Identify block as a memory block. */
4812 *mspace = 'M';
4813 ++mspace;
4814 /* Record address and size. */
4815 memcpy ((void *) mspace, (void *) &from, sizeof (from));
4816 mspace += sizeof (from);
4817 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
4818 mspace += sizeof (blocklen);
4819 /* Copy the string contents. */
4820 memcpy ((void *) mspace, (void *) buf, blocklen);
4821 remaining -= blocklen;
4822 from += blocklen;
4823 xfree (buf);
4824 }
4825 return 0;
4826 }
4827
4828 /* Record the value of a trace state variable. */
4829
4830 int
4831 agent_tsv_read (struct traceframe *tframe, int n)
4832 {
4833 unsigned char *vspace;
4834 LONGEST val;
4835
4836 vspace = add_traceframe_block (tframe,
4837 1 + sizeof (n) + sizeof (LONGEST));
4838 if (vspace == NULL)
4839 return 1;
4840 /* Identify block as a variable. */
4841 *vspace = 'V';
4842 /* Record variable's number and value. */
4843 memcpy (vspace + 1, &n, sizeof (n));
4844 val = get_trace_state_variable_value (n);
4845 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
4846 trace_debug ("Variable %d recorded", n);
4847 return 0;
4848 }
4849
4850 #ifndef IN_PROCESS_AGENT
4851
4852 /* Callback for traceframe_walk_blocks, used to find a given block
4853 type in a traceframe. */
4854
4855 static int
4856 match_blocktype (char blocktype, unsigned char *dataptr, void *data)
4857 {
4858 char *wantedp = data;
4859
4860 if (*wantedp == blocktype)
4861 return 1;
4862
4863 return 0;
4864 }
4865
4866 /* Walk over all traceframe blocks of the traceframe buffer starting
4867 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4868 block found, passing in DATA unmodified. If CALLBACK returns true,
4869 this returns a pointer to where the block is found. Returns NULL
4870 if no callback call returned true, indicating that all blocks have
4871 been walked. */
4872
4873 static unsigned char *
4874 traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
4875 int tfnum,
4876 int (*callback) (char blocktype,
4877 unsigned char *dataptr,
4878 void *data),
4879 void *data)
4880 {
4881 unsigned char *dataptr;
4882
4883 if (datasize == 0)
4884 {
4885 trace_debug ("traceframe %d has no data", tfnum);
4886 return NULL;
4887 }
4888
4889 /* Iterate through a traceframe's blocks, looking for a block of the
4890 requested type. */
4891 for (dataptr = database;
4892 dataptr < database + datasize;
4893 /* nothing */)
4894 {
4895 char blocktype;
4896 unsigned short mlen;
4897
4898 if (dataptr == trace_buffer_wrap)
4899 {
4900 /* Adjust to reflect wrapping part of the frame around to
4901 the beginning. */
4902 datasize = dataptr - database;
4903 dataptr = database = trace_buffer_lo;
4904 }
4905
4906 blocktype = *dataptr++;
4907
4908 if ((*callback) (blocktype, dataptr, data))
4909 return dataptr;
4910
4911 switch (blocktype)
4912 {
4913 case 'R':
4914 /* Skip over the registers block. */
4915 dataptr += register_cache_size ();
4916 break;
4917 case 'M':
4918 /* Skip over the memory block. */
4919 dataptr += sizeof (CORE_ADDR);
4920 memcpy (&mlen, dataptr, sizeof (mlen));
4921 dataptr += (sizeof (mlen) + mlen);
4922 break;
4923 case 'V':
4924 /* Skip over the TSV block. */
4925 dataptr += (sizeof (int) + sizeof (LONGEST));
4926 break;
4927 case 'S':
4928 /* Skip over the static trace data block. */
4929 memcpy (&mlen, dataptr, sizeof (mlen));
4930 dataptr += (sizeof (mlen) + mlen);
4931 break;
4932 default:
4933 trace_debug ("traceframe %d has unknown block type 0x%x",
4934 tfnum, blocktype);
4935 return NULL;
4936 }
4937 }
4938
4939 return NULL;
4940 }
4941
4942 /* Look for the block of type TYPE_WANTED in the trameframe starting
4943 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4944 number. */
4945
4946 static unsigned char *
4947 traceframe_find_block_type (unsigned char *database, unsigned int datasize,
4948 int tfnum, char type_wanted)
4949 {
4950 return traceframe_walk_blocks (database, datasize, tfnum,
4951 match_blocktype, &type_wanted);
4952 }
4953
4954 static unsigned char *
4955 traceframe_find_regblock (struct traceframe *tframe, int tfnum)
4956 {
4957 unsigned char *regblock;
4958
4959 regblock = traceframe_find_block_type (tframe->data,
4960 tframe->data_size,
4961 tfnum, 'R');
4962
4963 if (regblock == NULL)
4964 trace_debug ("traceframe %d has no register data", tfnum);
4965
4966 return regblock;
4967 }
4968
4969 /* Get registers from a traceframe. */
4970
4971 int
4972 fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
4973 {
4974 unsigned char *dataptr;
4975 struct tracepoint *tpoint;
4976 struct traceframe *tframe;
4977
4978 tframe = find_traceframe (tfnum);
4979
4980 if (tframe == NULL)
4981 {
4982 trace_debug ("traceframe %d not found", tfnum);
4983 return 1;
4984 }
4985
4986 dataptr = traceframe_find_regblock (tframe, tfnum);
4987 if (dataptr == NULL)
4988 {
4989 /* Mark registers unavailable. */
4990 supply_regblock (regcache, NULL);
4991
4992 /* We can generally guess at a PC, although this will be
4993 misleading for while-stepping frames and multi-location
4994 tracepoints. */
4995 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
4996 if (tpoint != NULL)
4997 regcache_write_pc (regcache, tpoint->address);
4998 }
4999 else
5000 supply_regblock (regcache, dataptr);
5001
5002 return 0;
5003 }
5004
5005 static CORE_ADDR
5006 traceframe_get_pc (struct traceframe *tframe)
5007 {
5008 struct regcache regcache;
5009 unsigned char *dataptr;
5010
5011 dataptr = traceframe_find_regblock (tframe, -1);
5012 if (dataptr == NULL)
5013 return 0;
5014
5015 init_register_cache (&regcache, dataptr);
5016 return regcache_read_pc (&regcache);
5017 }
5018
5019 /* Read a requested block of memory from a trace frame. */
5020
5021 int
5022 traceframe_read_mem (int tfnum, CORE_ADDR addr,
5023 unsigned char *buf, ULONGEST length,
5024 ULONGEST *nbytes)
5025 {
5026 struct traceframe *tframe;
5027 unsigned char *database, *dataptr;
5028 unsigned int datasize;
5029 CORE_ADDR maddr;
5030 unsigned short mlen;
5031
5032 trace_debug ("traceframe_read_mem");
5033
5034 tframe = find_traceframe (tfnum);
5035
5036 if (!tframe)
5037 {
5038 trace_debug ("traceframe %d not found", tfnum);
5039 return 1;
5040 }
5041
5042 datasize = tframe->data_size;
5043 database = dataptr = &tframe->data[0];
5044
5045 /* Iterate through a traceframe's blocks, looking for memory. */
5046 while ((dataptr = traceframe_find_block_type (dataptr,
5047 datasize
5048 - (dataptr - database),
5049 tfnum, 'M')) != NULL)
5050 {
5051 memcpy (&maddr, dataptr, sizeof (maddr));
5052 dataptr += sizeof (maddr);
5053 memcpy (&mlen, dataptr, sizeof (mlen));
5054 dataptr += sizeof (mlen);
5055 trace_debug ("traceframe %d has %d bytes at %s",
5056 tfnum, mlen, paddress (maddr));
5057
5058 /* If the block includes the first part of the desired range,
5059 return as much it has; GDB will re-request the remainder,
5060 which might be in a different block of this trace frame. */
5061 if (maddr <= addr && addr < (maddr + mlen))
5062 {
5063 ULONGEST amt = (maddr + mlen) - addr;
5064 if (amt > length)
5065 amt = length;
5066
5067 memcpy (buf, dataptr + (addr - maddr), amt);
5068 *nbytes = amt;
5069 return 0;
5070 }
5071
5072 /* Skip over this block. */
5073 dataptr += mlen;
5074 }
5075
5076 trace_debug ("traceframe %d has no memory data for the desired region",
5077 tfnum);
5078
5079 *nbytes = 0;
5080 return 0;
5081 }
5082
5083 static int
5084 traceframe_read_tsv (int tsvnum, LONGEST *val)
5085 {
5086 int tfnum;
5087 struct traceframe *tframe;
5088 unsigned char *database, *dataptr;
5089 unsigned int datasize;
5090 int vnum;
5091
5092 trace_debug ("traceframe_read_tsv");
5093
5094 tfnum = current_traceframe;
5095
5096 if (tfnum < 0)
5097 {
5098 trace_debug ("no current traceframe");
5099 return 1;
5100 }
5101
5102 tframe = find_traceframe (tfnum);
5103
5104 if (tframe == NULL)
5105 {
5106 trace_debug ("traceframe %d not found", tfnum);
5107 return 1;
5108 }
5109
5110 datasize = tframe->data_size;
5111 database = dataptr = &tframe->data[0];
5112
5113 /* Iterate through a traceframe's blocks, looking for the tsv. */
5114 while ((dataptr = traceframe_find_block_type (dataptr,
5115 datasize
5116 - (dataptr - database),
5117 tfnum, 'V')) != NULL)
5118 {
5119 memcpy (&vnum, dataptr, sizeof (vnum));
5120 dataptr += sizeof (vnum);
5121
5122 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
5123
5124 /* Check that this is the variable we want. */
5125 if (tsvnum == vnum)
5126 {
5127 memcpy (val, dataptr, sizeof (*val));
5128 return 0;
5129 }
5130
5131 /* Skip over this block. */
5132 dataptr += sizeof (LONGEST);
5133 }
5134
5135 trace_debug ("traceframe %d has no data for variable %d",
5136 tfnum, tsvnum);
5137 return 1;
5138 }
5139
5140 /* Read a requested block of static tracepoint data from a trace
5141 frame. */
5142
5143 int
5144 traceframe_read_sdata (int tfnum, ULONGEST offset,
5145 unsigned char *buf, ULONGEST length,
5146 ULONGEST *nbytes)
5147 {
5148 struct traceframe *tframe;
5149 unsigned char *database, *dataptr;
5150 unsigned int datasize;
5151 unsigned short mlen;
5152
5153 trace_debug ("traceframe_read_sdata");
5154
5155 tframe = find_traceframe (tfnum);
5156
5157 if (!tframe)
5158 {
5159 trace_debug ("traceframe %d not found", tfnum);
5160 return 1;
5161 }
5162
5163 datasize = tframe->data_size;
5164 database = &tframe->data[0];
5165
5166 /* Iterate through a traceframe's blocks, looking for static
5167 tracepoint data. */
5168 dataptr = traceframe_find_block_type (database, datasize,
5169 tfnum, 'S');
5170 if (dataptr != NULL)
5171 {
5172 memcpy (&mlen, dataptr, sizeof (mlen));
5173 dataptr += sizeof (mlen);
5174 if (offset < mlen)
5175 {
5176 if (offset + length > mlen)
5177 length = mlen - offset;
5178
5179 memcpy (buf, dataptr, length);
5180 *nbytes = length;
5181 }
5182 else
5183 *nbytes = 0;
5184 return 0;
5185 }
5186
5187 trace_debug ("traceframe %d has no static trace data", tfnum);
5188
5189 *nbytes = 0;
5190 return 0;
5191 }
5192
5193 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5194 object. DATA is pointer to a struct buffer holding the
5195 traceframe-info object being built. */
5196
5197 static int
5198 build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5199 {
5200 struct buffer *buffer = data;
5201
5202 switch (blocktype)
5203 {
5204 case 'M':
5205 {
5206 unsigned short mlen;
5207 CORE_ADDR maddr;
5208
5209 memcpy (&maddr, dataptr, sizeof (maddr));
5210 dataptr += sizeof (maddr);
5211 memcpy (&mlen, dataptr, sizeof (mlen));
5212 dataptr += sizeof (mlen);
5213 buffer_xml_printf (buffer,
5214 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5215 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5216 break;
5217 }
5218 case 'V':
5219 case 'R':
5220 case 'S':
5221 {
5222 break;
5223 }
5224 default:
5225 warning ("Unhandled trace block type (%d) '%c ' "
5226 "while building trace frame info.",
5227 blocktype, blocktype);
5228 break;
5229 }
5230
5231 return 0;
5232 }
5233
5234 /* Build a traceframe-info object for traceframe number TFNUM into
5235 BUFFER. */
5236
5237 int
5238 traceframe_read_info (int tfnum, struct buffer *buffer)
5239 {
5240 struct traceframe *tframe;
5241
5242 trace_debug ("traceframe_read_info");
5243
5244 tframe = find_traceframe (tfnum);
5245
5246 if (!tframe)
5247 {
5248 trace_debug ("traceframe %d not found", tfnum);
5249 return 1;
5250 }
5251
5252 buffer_grow_str (buffer, "<traceframe-info>\n");
5253 traceframe_walk_blocks (tframe->data, tframe->data_size,
5254 tfnum, build_traceframe_info_xml, buffer);
5255 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5256 return 0;
5257 }
5258
5259 /* Return the first fast tracepoint whose jump pad contains PC. */
5260
5261 static struct tracepoint *
5262 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5263 {
5264 struct tracepoint *tpoint;
5265
5266 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5267 if (tpoint->type == fast_tracepoint)
5268 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5269 return tpoint;
5270
5271 return NULL;
5272 }
5273
5274 /* Return the first fast tracepoint whose trampoline contains PC. */
5275
5276 static struct tracepoint *
5277 fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5278 {
5279 struct tracepoint *tpoint;
5280
5281 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5282 {
5283 if (tpoint->type == fast_tracepoint
5284 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5285 return tpoint;
5286 }
5287
5288 return NULL;
5289 }
5290
5291 /* Return GDBserver's tracepoint that matches the IP Agent's
5292 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5293 address space. */
5294
5295 static struct tracepoint *
5296 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5297 {
5298 struct tracepoint *tpoint;
5299
5300 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5301 if (tpoint->type == fast_tracepoint)
5302 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5303 return tpoint;
5304
5305 return NULL;
5306 }
5307
5308 #endif
5309
5310 /* The type of the object that is used to synchronize fast tracepoint
5311 collection. */
5312
5313 typedef struct collecting_t
5314 {
5315 /* The fast tracepoint number currently collecting. */
5316 uintptr_t tpoint;
5317
5318 /* A number that GDBserver can use to identify the thread that is
5319 presently holding the collect lock. This need not (and usually
5320 is not) the thread id, as getting the current thread ID usually
5321 requires a system call, which we want to avoid like the plague.
5322 Usually this is thread's TCB, found in the TLS (pseudo-)
5323 register, which is readable with a single insn on several
5324 architectures. */
5325 uintptr_t thread_area;
5326 } collecting_t;
5327
5328 #ifndef IN_PROCESS_AGENT
5329
5330 void
5331 force_unlock_trace_buffer (void)
5332 {
5333 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5334 }
5335
5336 /* Check if the thread identified by THREAD_AREA which is stopped at
5337 STOP_PC, is presently locking the fast tracepoint collection, and
5338 if so, gather some status of said collection. Returns 0 if the
5339 thread isn't collecting or in the jump pad at all. 1, if in the
5340 jump pad (or within gdb_collect) and hasn't executed the adjusted
5341 original insn yet (can set a breakpoint there and run to it). 2,
5342 if presently executing the adjusted original insn --- in which
5343 case, if we want to move the thread out of the jump pad, we need to
5344 single-step it until this function returns 0. */
5345
5346 int
5347 fast_tracepoint_collecting (CORE_ADDR thread_area,
5348 CORE_ADDR stop_pc,
5349 struct fast_tpoint_collect_status *status)
5350 {
5351 CORE_ADDR ipa_collecting;
5352 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
5353 CORE_ADDR ipa_gdb_trampoline_buffer;
5354 CORE_ADDR ipa_gdb_trampoline_buffer_end;
5355 struct tracepoint *tpoint;
5356 int needs_breakpoint;
5357
5358 /* The thread THREAD_AREA is either:
5359
5360 0. not collecting at all, not within the jump pad, or within
5361 gdb_collect or one of its callees.
5362
5363 1. in the jump pad and haven't reached gdb_collect
5364
5365 2. within gdb_collect (out of the jump pad) (collect is set)
5366
5367 3. we're in the jump pad, after gdb_collect having returned,
5368 possibly executing the adjusted insns.
5369
5370 For cases 1 and 3, `collecting' may or not be set. The jump pad
5371 doesn't have any complicated jump logic, so we can tell if the
5372 thread is executing the adjust original insn or not by just
5373 matching STOP_PC with known jump pad addresses. If we it isn't
5374 yet executing the original insn, set a breakpoint there, and let
5375 the thread run to it, so to quickly step over a possible (many
5376 insns) gdb_collect call. Otherwise, or when the breakpoint is
5377 hit, only a few (small number of) insns are left to be executed
5378 in the jump pad. Single-step the thread until it leaves the
5379 jump pad. */
5380
5381 again:
5382 tpoint = NULL;
5383 needs_breakpoint = 0;
5384 trace_debug ("fast_tracepoint_collecting");
5385
5386 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5387 &ipa_gdb_jump_pad_buffer))
5388 fatal ("error extracting `gdb_jump_pad_buffer'");
5389 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5390 &ipa_gdb_jump_pad_buffer_end))
5391 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5392
5393 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5394 &ipa_gdb_trampoline_buffer))
5395 fatal ("error extracting `gdb_trampoline_buffer'");
5396 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5397 &ipa_gdb_trampoline_buffer_end))
5398 fatal ("error extracting `gdb_trampoline_buffer_end'");
5399
5400 if (ipa_gdb_jump_pad_buffer <= stop_pc
5401 && stop_pc < ipa_gdb_jump_pad_buffer_end)
5402 {
5403 /* We can tell which tracepoint(s) the thread is collecting by
5404 matching the jump pad address back to the tracepoint. */
5405 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5406 if (tpoint == NULL)
5407 {
5408 warning ("in jump pad, but no matching tpoint?");
5409 return 0;
5410 }
5411 else
5412 {
5413 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5414 "adj_insn(%s, %s)",
5415 tpoint->number, paddress (tpoint->address),
5416 paddress (tpoint->jump_pad),
5417 paddress (tpoint->jump_pad_end),
5418 paddress (tpoint->adjusted_insn_addr),
5419 paddress (tpoint->adjusted_insn_addr_end));
5420 }
5421
5422 /* Definitely in the jump pad. May or may not need
5423 fast-exit-jump-pad breakpoint. */
5424 if (tpoint->jump_pad <= stop_pc
5425 && stop_pc < tpoint->adjusted_insn_addr)
5426 needs_breakpoint = 1;
5427 }
5428 else if (ipa_gdb_trampoline_buffer <= stop_pc
5429 && stop_pc < ipa_gdb_trampoline_buffer_end)
5430 {
5431 /* We can tell which tracepoint(s) the thread is collecting by
5432 matching the trampoline address back to the tracepoint. */
5433 tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5434 if (tpoint == NULL)
5435 {
5436 warning ("in trampoline, but no matching tpoint?");
5437 return 0;
5438 }
5439 else
5440 {
5441 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5442 tpoint->number, paddress (tpoint->address),
5443 paddress (tpoint->trampoline),
5444 paddress (tpoint->trampoline_end));
5445 }
5446
5447 /* Have not reached jump pad yet, but treat the trampoline as a
5448 part of the jump pad that is before the adjusted original
5449 instruction. */
5450 needs_breakpoint = 1;
5451 }
5452 else
5453 {
5454 collecting_t ipa_collecting_obj;
5455
5456 /* If `collecting' is set/locked, then the THREAD_AREA thread
5457 may or not be the one holding the lock. We have to read the
5458 lock to find out. */
5459
5460 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5461 &ipa_collecting))
5462 {
5463 trace_debug ("fast_tracepoint_collecting:"
5464 " failed reading 'collecting' in the inferior");
5465 return 0;
5466 }
5467
5468 if (!ipa_collecting)
5469 {
5470 trace_debug ("fast_tracepoint_collecting: not collecting"
5471 " (and nobody is).");
5472 return 0;
5473 }
5474
5475 /* Some thread is collecting. Check which. */
5476 if (read_inferior_memory (ipa_collecting,
5477 (unsigned char *) &ipa_collecting_obj,
5478 sizeof (ipa_collecting_obj)) != 0)
5479 goto again;
5480
5481 if (ipa_collecting_obj.thread_area != thread_area)
5482 {
5483 trace_debug ("fast_tracepoint_collecting: not collecting "
5484 "(another thread is)");
5485 return 0;
5486 }
5487
5488 tpoint
5489 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5490 if (tpoint == NULL)
5491 {
5492 warning ("fast_tracepoint_collecting: collecting, "
5493 "but tpoint %s not found?",
5494 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5495 return 0;
5496 }
5497
5498 /* The thread is within `gdb_collect', skip over the rest of
5499 fast tracepoint collection quickly using a breakpoint. */
5500 needs_breakpoint = 1;
5501 }
5502
5503 /* The caller wants a bit of status detail. */
5504 if (status != NULL)
5505 {
5506 status->tpoint_num = tpoint->number;
5507 status->tpoint_addr = tpoint->address;
5508 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5509 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5510 }
5511
5512 if (needs_breakpoint)
5513 {
5514 /* Hasn't executed the original instruction yet. Set breakpoint
5515 there, and wait till it's hit, then single-step until exiting
5516 the jump pad. */
5517
5518 trace_debug ("\
5519 fast_tracepoint_collecting, returning continue-until-break at %s",
5520 paddress (tpoint->adjusted_insn_addr));
5521
5522 return 1; /* continue */
5523 }
5524 else
5525 {
5526 /* Just single-step until exiting the jump pad. */
5527
5528 trace_debug ("fast_tracepoint_collecting, returning "
5529 "need-single-step (%s-%s)",
5530 paddress (tpoint->adjusted_insn_addr),
5531 paddress (tpoint->adjusted_insn_addr_end));
5532
5533 return 2; /* single-step */
5534 }
5535 }
5536
5537 #endif
5538
5539 #ifdef IN_PROCESS_AGENT
5540
5541 /* The global fast tracepoint collect lock. Points to a collecting_t
5542 object built on the stack by the jump pad, if presently locked;
5543 NULL if it isn't locked. Note that this lock *must* be set while
5544 executing any *function other than the jump pad. See
5545 fast_tracepoint_collecting. */
5546 static collecting_t * ATTR_USED collecting;
5547
5548 /* This routine, called from the jump pad (in asm) is designed to be
5549 called from the jump pads of fast tracepoints, thus it is on the
5550 critical path. */
5551
5552 IP_AGENT_EXPORT void ATTR_USED
5553 gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5554 {
5555 struct fast_tracepoint_ctx ctx;
5556
5557 /* Don't do anything until the trace run is completely set up. */
5558 if (!tracing)
5559 return;
5560
5561 ctx.base.type = fast_tracepoint;
5562 ctx.regs = regs;
5563 ctx.regcache_initted = 0;
5564 /* Wrap the regblock in a register cache (in the stack, we don't
5565 want to malloc here). */
5566 ctx.regspace = alloca (register_cache_size ());
5567 if (ctx.regspace == NULL)
5568 {
5569 trace_debug ("Trace buffer block allocation failed, skipping");
5570 return;
5571 }
5572
5573 for (ctx.tpoint = tpoint;
5574 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5575 ctx.tpoint = ctx.tpoint->next)
5576 {
5577 if (!ctx.tpoint->enabled)
5578 continue;
5579
5580 /* Multiple tracepoints of different types, such as fast tracepoint and
5581 static tracepoint, can be set at the same address. */
5582 if (ctx.tpoint->type != tpoint->type)
5583 continue;
5584
5585 /* Test the condition if present, and collect if true. */
5586 if (ctx.tpoint->cond == NULL
5587 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5588 ctx.tpoint))
5589 {
5590 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5591 ctx.tpoint->address, ctx.tpoint);
5592
5593 /* Note that this will cause original insns to be written back
5594 to where we jumped from, but that's OK because we're jumping
5595 back to the next whole instruction. This will go badly if
5596 instruction restoration is not atomic though. */
5597 if (stopping_tracepoint
5598 || trace_buffer_is_full
5599 || expr_eval_result != expr_eval_no_error)
5600 {
5601 stop_tracing ();
5602 break;
5603 }
5604 }
5605 else
5606 {
5607 /* If there was a condition and it evaluated to false, the only
5608 way we would stop tracing is if there was an error during
5609 condition expression evaluation. */
5610 if (expr_eval_result != expr_eval_no_error)
5611 {
5612 stop_tracing ();
5613 break;
5614 }
5615 }
5616 }
5617 }
5618
5619 #endif
5620
5621 #ifndef IN_PROCESS_AGENT
5622
5623 CORE_ADDR
5624 get_raw_reg_func_addr (void)
5625 {
5626 return ipa_sym_addrs.addr_get_raw_reg;
5627 }
5628
5629 CORE_ADDR
5630 get_get_tsv_func_addr (void)
5631 {
5632 return ipa_sym_addrs.addr_get_trace_state_variable_value;
5633 }
5634
5635 CORE_ADDR
5636 get_set_tsv_func_addr (void)
5637 {
5638 return ipa_sym_addrs.addr_set_trace_state_variable_value;
5639 }
5640
5641 static void
5642 compile_tracepoint_condition (struct tracepoint *tpoint,
5643 CORE_ADDR *jump_entry)
5644 {
5645 CORE_ADDR entry_point = *jump_entry;
5646 enum eval_result_type err;
5647
5648 trace_debug ("Starting condition compilation for tracepoint %d\n",
5649 tpoint->number);
5650
5651 /* Initialize the global pointer to the code being built. */
5652 current_insn_ptr = *jump_entry;
5653
5654 emit_prologue ();
5655
5656 err = compile_bytecodes (tpoint->cond);
5657
5658 if (err == expr_eval_no_error)
5659 {
5660 emit_epilogue ();
5661
5662 /* Record the beginning of the compiled code. */
5663 tpoint->compiled_cond = entry_point;
5664
5665 trace_debug ("Condition compilation for tracepoint %d complete\n",
5666 tpoint->number);
5667 }
5668 else
5669 {
5670 /* Leave the unfinished code in situ, but don't point to it. */
5671
5672 tpoint->compiled_cond = 0;
5673
5674 trace_debug ("Condition compilation for tracepoint %d failed, "
5675 "error code %d",
5676 tpoint->number, err);
5677 }
5678
5679 /* Update the code pointer passed in. Note that we do this even if
5680 the compile fails, so that we can look at the partial results
5681 instead of letting them be overwritten. */
5682 *jump_entry = current_insn_ptr;
5683
5684 /* Leave a gap, to aid dump decipherment. */
5685 *jump_entry += 16;
5686 }
5687
5688 /* We'll need to adjust these when we consider bi-arch setups, and big
5689 endian machines. */
5690
5691 static int
5692 write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
5693 {
5694 return write_inferior_memory (where,
5695 (unsigned char *) &ptr, sizeof (void *));
5696 }
5697
5698 /* The base pointer of the IPA's heap. This is the only memory the
5699 IPA is allowed to use. The IPA should _not_ call the inferior's
5700 `malloc' during operation. That'd be slow, and, most importantly,
5701 it may not be safe. We may be collecting a tracepoint in a signal
5702 handler, for example. */
5703 static CORE_ADDR target_tp_heap;
5704
5705 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5706 to 8 bytes. */
5707
5708 static CORE_ADDR
5709 target_malloc (ULONGEST size)
5710 {
5711 CORE_ADDR ptr;
5712
5713 if (target_tp_heap == 0)
5714 {
5715 /* We have the pointer *address*, need what it points to. */
5716 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5717 &target_tp_heap))
5718 fatal ("could get target heap head pointer");
5719 }
5720
5721 ptr = target_tp_heap;
5722 target_tp_heap += size;
5723
5724 /* Pad to 8-byte alignment. */
5725 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5726
5727 return ptr;
5728 }
5729
5730 static CORE_ADDR
5731 download_agent_expr (struct agent_expr *expr)
5732 {
5733 CORE_ADDR expr_addr;
5734 CORE_ADDR expr_bytes;
5735
5736 expr_addr = target_malloc (sizeof (*expr));
5737 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5738
5739 expr_bytes = target_malloc (expr->length);
5740 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
5741 expr_bytes);
5742 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
5743
5744 return expr_addr;
5745 }
5746
5747 /* Align V up to N bits. */
5748 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5749
5750 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5751
5752 static void
5753 download_tracepoint_1 (struct tracepoint *tpoint)
5754 {
5755 struct tracepoint target_tracepoint;
5756 CORE_ADDR tpptr = 0;
5757
5758 gdb_assert (tpoint->type == fast_tracepoint
5759 || tpoint->type == static_tracepoint);
5760
5761 if (tpoint->cond != NULL && target_emit_ops () != NULL)
5762 {
5763 CORE_ADDR jentry, jump_entry;
5764
5765 jentry = jump_entry = get_jump_space_head ();
5766
5767 if (tpoint->cond != NULL)
5768 {
5769 /* Pad to 8-byte alignment. (needed?) */
5770 /* Actually this should be left for the target to
5771 decide. */
5772 jentry = UALIGN (jentry, 8);
5773
5774 compile_tracepoint_condition (tpoint, &jentry);
5775 }
5776
5777 /* Pad to 8-byte alignment. */
5778 jentry = UALIGN (jentry, 8);
5779 claim_jump_space (jentry - jump_entry);
5780 }
5781
5782 target_tracepoint = *tpoint;
5783
5784 tpptr = target_malloc (sizeof (*tpoint));
5785 tpoint->obj_addr_on_target = tpptr;
5786
5787 /* Write the whole object. We'll fix up its pointers in a bit.
5788 Assume no next for now. This is fixed up above on the next
5789 iteration, if there's any. */
5790 target_tracepoint.next = NULL;
5791 /* Need to clear this here too, since we're downloading the
5792 tracepoints before clearing our own copy. */
5793 target_tracepoint.hit_count = 0;
5794
5795 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
5796 sizeof (target_tracepoint));
5797
5798 if (tpoint->cond)
5799 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5800 cond),
5801 download_agent_expr (tpoint->cond));
5802
5803 if (tpoint->numactions)
5804 {
5805 int i;
5806 CORE_ADDR actions_array;
5807
5808 /* The pointers array. */
5809 actions_array
5810 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
5811 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5812 actions),
5813 actions_array);
5814
5815 /* Now for each pointer, download the action. */
5816 for (i = 0; i < tpoint->numactions; i++)
5817 {
5818 struct tracepoint_action *action = tpoint->actions[i];
5819 CORE_ADDR ipa_action = action->ops->download (action);
5820
5821 if (ipa_action != 0)
5822 write_inferior_data_ptr
5823 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
5824 ipa_action);
5825 }
5826 }
5827 }
5828
5829 static void
5830 download_tracepoint (struct tracepoint *tpoint)
5831 {
5832 struct tracepoint *tp, *tp_prev;
5833
5834 if (tpoint->type != fast_tracepoint
5835 && tpoint->type != static_tracepoint)
5836 return;
5837
5838 download_tracepoint_1 (tpoint);
5839
5840 /* Find the previous entry of TPOINT, which is fast tracepoint or
5841 static tracepoint. */
5842 tp_prev = NULL;
5843 for (tp = tracepoints; tp != tpoint; tp = tp->next)
5844 {
5845 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
5846 tp_prev = tp;
5847 }
5848
5849 if (tp_prev)
5850 {
5851 CORE_ADDR tp_prev_target_next_addr;
5852
5853 /* Insert TPOINT after TP_PREV in IPA. */
5854 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
5855 + offsetof (struct tracepoint, next),
5856 &tp_prev_target_next_addr))
5857 fatal ("error reading `tp_prev->next'");
5858
5859 /* tpoint->next = tp_prev->next */
5860 write_inferior_data_ptr (tpoint->obj_addr_on_target
5861 + offsetof (struct tracepoint, next),
5862 tp_prev_target_next_addr);
5863 /* tp_prev->next = tpoint */
5864 write_inferior_data_ptr (tp_prev->obj_addr_on_target
5865 + offsetof (struct tracepoint, next),
5866 tpoint->obj_addr_on_target);
5867 }
5868 else
5869 /* First object in list, set the head pointer in the
5870 inferior. */
5871 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
5872 tpoint->obj_addr_on_target);
5873
5874 }
5875
5876 static void
5877 download_trace_state_variables (void)
5878 {
5879 CORE_ADDR ptr = 0, prev_ptr = 0;
5880 struct trace_state_variable *tsv;
5881
5882 /* Start out empty. */
5883 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
5884
5885 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
5886 {
5887 struct trace_state_variable target_tsv;
5888
5889 /* TSV's with a getter have been initialized equally in both the
5890 inferior and GDBserver. Skip them. */
5891 if (tsv->getter != NULL)
5892 continue;
5893
5894 target_tsv = *tsv;
5895
5896 prev_ptr = ptr;
5897 ptr = target_malloc (sizeof (*tsv));
5898
5899 if (tsv == trace_state_variables)
5900 {
5901 /* First object in list, set the head pointer in the
5902 inferior. */
5903
5904 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
5905 ptr);
5906 }
5907 else
5908 {
5909 write_inferior_data_ptr (prev_ptr
5910 + offsetof (struct trace_state_variable,
5911 next),
5912 ptr);
5913 }
5914
5915 /* Write the whole object. We'll fix up its pointers in a bit.
5916 Assume no next, fixup when needed. */
5917 target_tsv.next = NULL;
5918
5919 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
5920 sizeof (target_tsv));
5921
5922 if (tsv->name != NULL)
5923 {
5924 size_t size = strlen (tsv->name) + 1;
5925 CORE_ADDR name_addr = target_malloc (size);
5926 write_inferior_memory (name_addr,
5927 (unsigned char *) tsv->name, size);
5928 write_inferior_data_ptr (ptr
5929 + offsetof (struct trace_state_variable,
5930 name),
5931 name_addr);
5932 }
5933
5934 if (tsv->getter != NULL)
5935 {
5936 fatal ("what to do with these?");
5937 }
5938 }
5939
5940 if (prev_ptr != 0)
5941 {
5942 /* Fixup the next pointer in the last item in the list. */
5943 write_inferior_data_ptr (prev_ptr
5944 + offsetof (struct trace_state_variable,
5945 next), 0);
5946 }
5947 }
5948
5949 /* Upload complete trace frames out of the IP Agent's trace buffer
5950 into GDBserver's trace buffer. This always uploads either all or
5951 no trace frames. This is the counter part of
5952 `trace_alloc_trace_buffer'. See its description of the atomic
5953 synching mechanism. */
5954
5955 static void
5956 upload_fast_traceframes (void)
5957 {
5958 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
5959 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
5960 CORE_ADDR tf;
5961 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
5962 unsigned int curr_tbctrl_idx;
5963 unsigned int ipa_trace_buffer_ctrl_curr;
5964 unsigned int ipa_trace_buffer_ctrl_curr_old;
5965 CORE_ADDR ipa_trace_buffer_ctrl_addr;
5966 struct breakpoint *about_to_request_buffer_space_bkpt;
5967 CORE_ADDR ipa_trace_buffer_lo;
5968 CORE_ADDR ipa_trace_buffer_hi;
5969
5970 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5971 &ipa_traceframe_read_count_racy))
5972 {
5973 /* This will happen in most targets if the current thread is
5974 running. */
5975 return;
5976 }
5977
5978 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5979 &ipa_traceframe_write_count_racy))
5980 return;
5981
5982 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5983 ipa_traceframe_write_count_racy
5984 - ipa_traceframe_read_count_racy,
5985 ipa_traceframe_write_count_racy,
5986 ipa_traceframe_read_count_racy);
5987
5988 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
5989 return;
5990
5991 about_to_request_buffer_space_bkpt
5992 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
5993 NULL);
5994
5995 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5996 &ipa_trace_buffer_ctrl_curr))
5997 return;
5998
5999 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
6000
6001 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
6002
6003 {
6004 unsigned int prev, counter;
6005
6006 /* Update the token, with new counters, and the GDBserver stamp
6007 bit. Alway reuse the current TBC index. */
6008 prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
6009 counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
6010
6011 ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6012 | (prev << 12)
6013 | counter
6014 | curr_tbctrl_idx);
6015 }
6016
6017 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6018 ipa_trace_buffer_ctrl_curr))
6019 return;
6020
6021 trace_debug ("Lib: Committed %08x -> %08x",
6022 ipa_trace_buffer_ctrl_curr_old,
6023 ipa_trace_buffer_ctrl_curr);
6024
6025 /* Re-read these, now that we've installed the
6026 `about_to_request_buffer_space' breakpoint/lock. A thread could
6027 have finished a traceframe between the last read of these
6028 counters and setting the breakpoint above. If we start
6029 uploading, we never want to leave this function with
6030 traceframe_read_count != 0, otherwise, GDBserver could end up
6031 incrementing the counter tokens more than once (due to event loop
6032 nesting), which would break the IP agent's "effective" detection
6033 (see trace_alloc_trace_buffer). */
6034 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6035 &ipa_traceframe_read_count))
6036 return;
6037 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6038 &ipa_traceframe_write_count))
6039 return;
6040
6041 if (debug_threads)
6042 {
6043 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6044 ipa_traceframe_write_count - ipa_traceframe_read_count,
6045 ipa_traceframe_write_count, ipa_traceframe_read_count);
6046
6047 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
6048 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
6049 trace_debug ("note that ipa_traceframe_count's parts changed");
6050 }
6051
6052 /* Get the address of the current TBC object (the IP agent has an
6053 array of 3 such objects). The index is stored in the TBC
6054 token. */
6055 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
6056 ipa_trace_buffer_ctrl_addr
6057 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
6058
6059 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
6060 (unsigned char *) &ipa_trace_buffer_ctrl,
6061 sizeof (struct ipa_trace_buffer_control)))
6062 return;
6063
6064 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
6065 &ipa_trace_buffer_lo))
6066 return;
6067 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6068 &ipa_trace_buffer_hi))
6069 return;
6070
6071 /* Offsets are easier to grok for debugging than raw addresses,
6072 especially for the small trace buffer sizes that are useful for
6073 testing. */
6074 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6075 "endfree=%d wrap=%d hi=%d",
6076 curr_tbctrl_idx,
6077 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6078 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6079 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6080 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6081 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6082
6083 /* Note that the IPA's buffer is always circular. */
6084
6085 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6086
6087 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6088 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6089
6090 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6091 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6092 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6093 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6094 : 0))
6095
6096 tf = IPA_FIRST_TRACEFRAME ();
6097
6098 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6099 {
6100 struct tracepoint *tpoint;
6101 struct traceframe *tframe;
6102 unsigned char *block;
6103 struct traceframe ipa_tframe;
6104
6105 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6106 offsetof (struct traceframe, data)))
6107 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6108
6109 if (ipa_tframe.tpnum == 0)
6110 fatal ("Uploading: No (more) fast traceframes, but "
6111 "ipa_traceframe_count == %u??\n",
6112 ipa_traceframe_write_count - ipa_traceframe_read_count);
6113
6114 /* Note that this will be incorrect for multi-location
6115 tracepoints... */
6116 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6117
6118 tframe = add_traceframe (tpoint);
6119 if (tframe == NULL)
6120 {
6121 trace_buffer_is_full = 1;
6122 trace_debug ("Uploading: trace buffer is full");
6123 }
6124 else
6125 {
6126 /* Copy the whole set of blocks in one go for now. FIXME:
6127 split this in smaller blocks. */
6128 block = add_traceframe_block (tframe, ipa_tframe.data_size);
6129 if (block != NULL)
6130 {
6131 if (read_inferior_memory (tf
6132 + offsetof (struct traceframe, data),
6133 block, ipa_tframe.data_size))
6134 error ("Uploading: Couldn't read traceframe data at %s\n",
6135 paddress (tf + offsetof (struct traceframe, data)));
6136 }
6137
6138 trace_debug ("Uploading: traceframe didn't fit");
6139 finish_traceframe (tframe);
6140 }
6141
6142 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6143
6144 /* If we freed the traceframe that wrapped around, go back
6145 to the non-wrap case. */
6146 if (tf < ipa_trace_buffer_ctrl.start)
6147 {
6148 trace_debug ("Lib: Discarding past the wraparound");
6149 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6150 }
6151 ipa_trace_buffer_ctrl.start = tf;
6152 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6153 ++ipa_traceframe_read_count;
6154
6155 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6156 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6157 {
6158 trace_debug ("Lib: buffer is fully empty. "
6159 "Trace buffer [%d] start=%d free=%d endfree=%d",
6160 curr_tbctrl_idx,
6161 (int) (ipa_trace_buffer_ctrl.start
6162 - ipa_trace_buffer_lo),
6163 (int) (ipa_trace_buffer_ctrl.free
6164 - ipa_trace_buffer_lo),
6165 (int) (ipa_trace_buffer_ctrl.end_free
6166 - ipa_trace_buffer_lo));
6167
6168 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6169 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6170 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6171 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6172 }
6173
6174 trace_debug ("Uploaded a traceframe\n"
6175 "Lib: Trace buffer [%d] start=%d free=%d "
6176 "endfree=%d wrap=%d hi=%d",
6177 curr_tbctrl_idx,
6178 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6179 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6180 (int) (ipa_trace_buffer_ctrl.end_free
6181 - ipa_trace_buffer_lo),
6182 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6183 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6184 }
6185
6186 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6187 (unsigned char *) &ipa_trace_buffer_ctrl,
6188 sizeof (struct ipa_trace_buffer_control)))
6189 return;
6190
6191 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6192 ipa_traceframe_read_count);
6193
6194 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6195
6196 pause_all (1);
6197 cancel_breakpoints ();
6198
6199 delete_breakpoint (about_to_request_buffer_space_bkpt);
6200 about_to_request_buffer_space_bkpt = NULL;
6201
6202 unpause_all (1);
6203
6204 if (trace_buffer_is_full)
6205 stop_tracing ();
6206 }
6207 #endif
6208
6209 #ifdef IN_PROCESS_AGENT
6210
6211 IP_AGENT_EXPORT int ust_loaded;
6212 IP_AGENT_EXPORT char cmd_buf[IPA_CMD_BUF_SIZE];
6213
6214 #ifdef HAVE_UST
6215
6216 /* Static tracepoints. */
6217
6218 /* UST puts a "struct tracepoint" in the global namespace, which
6219 conflicts with our tracepoint. Arguably, being a library, it
6220 shouldn't take ownership of such a generic name. We work around it
6221 here. */
6222 #define tracepoint ust_tracepoint
6223 #include <ust/ust.h>
6224 #undef tracepoint
6225
6226 extern int serialize_to_text (char *outbuf, int bufsize,
6227 const char *fmt, va_list ap);
6228
6229 #define GDB_PROBE_NAME "gdb"
6230
6231 /* We dynamically search for the UST symbols instead of linking them
6232 in. This lets the user decide if the application uses static
6233 tracepoints, instead of always pulling libust.so in. This vector
6234 holds pointers to all functions we care about. */
6235
6236 static struct
6237 {
6238 int (*serialize_to_text) (char *outbuf, int bufsize,
6239 const char *fmt, va_list ap);
6240
6241 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6242 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6243
6244 int (*ltt_marker_connect) (const char *channel, const char *mname,
6245 const char *pname);
6246 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6247 const char *pname);
6248
6249 void (*marker_iter_start) (struct marker_iter *iter);
6250 void (*marker_iter_next) (struct marker_iter *iter);
6251 void (*marker_iter_stop) (struct marker_iter *iter);
6252 void (*marker_iter_reset) (struct marker_iter *iter);
6253 } ust_ops;
6254
6255 #include <dlfcn.h>
6256
6257 /* Cast through typeof to catch incompatible API changes. Since UST
6258 only builds with gcc, we can freely use gcc extensions here
6259 too. */
6260 #define GET_UST_SYM(SYM) \
6261 do \
6262 { \
6263 if (ust_ops.SYM == NULL) \
6264 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6265 if (ust_ops.SYM == NULL) \
6266 return 0; \
6267 } while (0)
6268
6269 #define USTF(SYM) ust_ops.SYM
6270
6271 /* Get pointers to all libust.so functions we care about. */
6272
6273 static int
6274 dlsym_ust (void)
6275 {
6276 GET_UST_SYM (serialize_to_text);
6277
6278 GET_UST_SYM (ltt_probe_register);
6279 GET_UST_SYM (ltt_probe_unregister);
6280 GET_UST_SYM (ltt_marker_connect);
6281 GET_UST_SYM (ltt_marker_disconnect);
6282
6283 GET_UST_SYM (marker_iter_start);
6284 GET_UST_SYM (marker_iter_next);
6285 GET_UST_SYM (marker_iter_stop);
6286 GET_UST_SYM (marker_iter_reset);
6287
6288 ust_loaded = 1;
6289 return 1;
6290 }
6291
6292 /* Given an UST marker, return the matching gdb static tracepoint.
6293 The match is done by address. */
6294
6295 static struct tracepoint *
6296 ust_marker_to_static_tracepoint (const struct marker *mdata)
6297 {
6298 struct tracepoint *tpoint;
6299
6300 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6301 {
6302 if (tpoint->type != static_tracepoint)
6303 continue;
6304
6305 if (tpoint->address == (uintptr_t) mdata->location)
6306 return tpoint;
6307 }
6308
6309 return NULL;
6310 }
6311
6312 /* The probe function we install on lttng/ust markers. Whenever a
6313 probed ust marker is hit, this function is called. This is similar
6314 to gdb_collect, only for static tracepoints, instead of fast
6315 tracepoints. */
6316
6317 static void
6318 gdb_probe (const struct marker *mdata, void *probe_private,
6319 struct registers *regs, void *call_private,
6320 const char *fmt, va_list *args)
6321 {
6322 struct tracepoint *tpoint;
6323 struct static_tracepoint_ctx ctx;
6324
6325 /* Don't do anything until the trace run is completely set up. */
6326 if (!tracing)
6327 {
6328 trace_debug ("gdb_probe: not tracing\n");
6329 return;
6330 }
6331
6332 ctx.base.type = static_tracepoint;
6333 ctx.regcache_initted = 0;
6334 ctx.regs = regs;
6335 ctx.fmt = fmt;
6336 ctx.args = args;
6337
6338 /* Wrap the regblock in a register cache (in the stack, we don't
6339 want to malloc here). */
6340 ctx.regspace = alloca (register_cache_size ());
6341 if (ctx.regspace == NULL)
6342 {
6343 trace_debug ("Trace buffer block allocation failed, skipping");
6344 return;
6345 }
6346
6347 tpoint = ust_marker_to_static_tracepoint (mdata);
6348 if (tpoint == NULL)
6349 {
6350 trace_debug ("gdb_probe: marker not known: "
6351 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6352 mdata->location, mdata->channel,
6353 mdata->name, mdata->format);
6354 return;
6355 }
6356
6357 if (!tpoint->enabled)
6358 {
6359 trace_debug ("gdb_probe: tracepoint disabled");
6360 return;
6361 }
6362
6363 ctx.tpoint = tpoint;
6364
6365 trace_debug ("gdb_probe: collecting marker: "
6366 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6367 mdata->location, mdata->channel,
6368 mdata->name, mdata->format);
6369
6370 /* Test the condition if present, and collect if true. */
6371 if (tpoint->cond == NULL
6372 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6373 tpoint))
6374 {
6375 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6376 tpoint->address, tpoint);
6377
6378 if (stopping_tracepoint
6379 || trace_buffer_is_full
6380 || expr_eval_result != expr_eval_no_error)
6381 stop_tracing ();
6382 }
6383 else
6384 {
6385 /* If there was a condition and it evaluated to false, the only
6386 way we would stop tracing is if there was an error during
6387 condition expression evaluation. */
6388 if (expr_eval_result != expr_eval_no_error)
6389 stop_tracing ();
6390 }
6391 }
6392
6393 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6394 static tracepoint string data. This is a string passed to the
6395 tracing library by the user, at the time of the tracepoint marker
6396 call. E.g., in the UST marker call:
6397
6398 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6399
6400 the collected data is "str FOOBAZ".
6401 */
6402
6403 static void
6404 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
6405 struct traceframe *tframe)
6406 {
6407 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
6408 unsigned char *bufspace;
6409 int size;
6410 va_list copy;
6411 unsigned short blocklen;
6412
6413 if (umd == NULL)
6414 {
6415 trace_debug ("Wanted to collect static trace data, "
6416 "but there's no static trace data");
6417 return;
6418 }
6419
6420 va_copy (copy, *umd->args);
6421 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
6422 va_end (copy);
6423
6424 trace_debug ("Want to collect ust data");
6425
6426 /* 'S' + size + string */
6427 bufspace = add_traceframe_block (tframe,
6428 1 + sizeof (blocklen) + size + 1);
6429 if (bufspace == NULL)
6430 {
6431 trace_debug ("Trace buffer block allocation failed, skipping");
6432 return;
6433 }
6434
6435 /* Identify a static trace data block. */
6436 *bufspace = 'S';
6437
6438 blocklen = size + 1;
6439 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
6440
6441 va_copy (copy, *umd->args);
6442 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
6443 size + 1, umd->fmt, copy);
6444 va_end (copy);
6445
6446 trace_debug ("Storing static tracepoint data in regblock: %s",
6447 bufspace + 1 + sizeof (blocklen));
6448 }
6449
6450 /* The probe to register with lttng/ust. */
6451 static struct ltt_available_probe gdb_ust_probe =
6452 {
6453 GDB_PROBE_NAME,
6454 NULL,
6455 gdb_probe,
6456 };
6457
6458 #endif /* HAVE_UST */
6459 #endif /* IN_PROCESS_AGENT */
6460
6461 #ifndef IN_PROCESS_AGENT
6462
6463 /* Ask the in-process agent to run a command. Since we don't want to
6464 have to handle the IPA hitting breakpoints while running the
6465 command, we pause all threads, remove all breakpoints, and then set
6466 the helper thread re-running. We communicate with the helper
6467 thread by means of direct memory xfering, and a socket for
6468 synchronization. */
6469
6470 static int
6471 run_inferior_command (char *cmd)
6472 {
6473 int err = -1;
6474 int pid = ptid_get_pid (current_inferior->entry.id);
6475
6476 trace_debug ("run_inferior_command: running: %s", cmd);
6477
6478 pause_all (0);
6479 uninsert_all_breakpoints ();
6480
6481 err = agent_run_command (pid, (const char *) cmd);
6482
6483 reinsert_all_breakpoints ();
6484 unpause_all (0);
6485
6486 return err;
6487 }
6488
6489 #else /* !IN_PROCESS_AGENT */
6490
6491 #include <sys/socket.h>
6492 #include <sys/un.h>
6493
6494 #ifndef UNIX_PATH_MAX
6495 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6496 #endif
6497
6498 /* Where we put the socked used for synchronization. */
6499 #define SOCK_DIR P_tmpdir
6500
6501 /* Thread ID of the helper thread. GDBserver reads this to know which
6502 is the help thread. This is an LWP id on Linux. */
6503 int helper_thread_id;
6504
6505 static int
6506 init_named_socket (const char *name)
6507 {
6508 int result, fd;
6509 struct sockaddr_un addr;
6510
6511 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
6512 if (result == -1)
6513 {
6514 warning ("socket creation failed: %s", strerror (errno));
6515 return -1;
6516 }
6517
6518 addr.sun_family = AF_UNIX;
6519
6520 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
6521 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
6522
6523 result = access (name, F_OK);
6524 if (result == 0)
6525 {
6526 /* File exists. */
6527 result = unlink (name);
6528 if (result == -1)
6529 {
6530 warning ("unlink failed: %s", strerror (errno));
6531 close (fd);
6532 return -1;
6533 }
6534 warning ("socket %s already exists; overwriting", name);
6535 }
6536
6537 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
6538 if (result == -1)
6539 {
6540 warning ("bind failed: %s", strerror (errno));
6541 close (fd);
6542 return -1;
6543 }
6544
6545 result = listen (fd, 1);
6546 if (result == -1)
6547 {
6548 warning ("listen: %s", strerror (errno));
6549 close (fd);
6550 return -1;
6551 }
6552
6553 return fd;
6554 }
6555
6556 static int
6557 gdb_agent_socket_init (void)
6558 {
6559 int result, fd;
6560 char name[UNIX_PATH_MAX];
6561
6562 result = xsnprintf (name, UNIX_PATH_MAX, "%s/gdb_ust%d",
6563 SOCK_DIR, getpid ());
6564 if (result >= UNIX_PATH_MAX)
6565 {
6566 trace_debug ("string overflow allocating socket name");
6567 return -1;
6568 }
6569
6570 fd = init_named_socket (name);
6571 if (fd < 0)
6572 warning ("Error initializing named socket (%s) for communication with the "
6573 "ust helper thread. Check that directory exists and that it "
6574 "is writable.", name);
6575
6576 return fd;
6577 }
6578
6579 #ifdef HAVE_UST
6580
6581 /* The next marker to be returned on a qTsSTM command. */
6582 static const struct marker *next_st;
6583
6584 /* Returns the first known marker. */
6585
6586 struct marker *
6587 first_marker (void)
6588 {
6589 struct marker_iter iter;
6590
6591 USTF(marker_iter_reset) (&iter);
6592 USTF(marker_iter_start) (&iter);
6593
6594 return iter.marker;
6595 }
6596
6597 /* Returns the marker following M. */
6598
6599 const struct marker *
6600 next_marker (const struct marker *m)
6601 {
6602 struct marker_iter iter;
6603
6604 USTF(marker_iter_reset) (&iter);
6605 USTF(marker_iter_start) (&iter);
6606
6607 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6608 {
6609 if (iter.marker == m)
6610 {
6611 USTF(marker_iter_next) (&iter);
6612 return iter.marker;
6613 }
6614 }
6615
6616 return NULL;
6617 }
6618
6619 /* Return an hexstr version of the STR C string, fit for sending to
6620 GDB. */
6621
6622 static char *
6623 cstr_to_hexstr (const char *str)
6624 {
6625 int len = strlen (str);
6626 char *hexstr = xmalloc (len * 2 + 1);
6627 convert_int_to_ascii ((gdb_byte *) str, hexstr, len);
6628 return hexstr;
6629 }
6630
6631 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6632 packets. */
6633
6634 static void
6635 response_ust_marker (char *packet, const struct marker *st)
6636 {
6637 char *strid, *format, *tmp;
6638
6639 next_st = next_marker (st);
6640
6641 tmp = xmalloc (strlen (st->channel) + 1 +
6642 strlen (st->name) + 1);
6643 sprintf (tmp, "%s/%s", st->channel, st->name);
6644
6645 strid = cstr_to_hexstr (tmp);
6646 free (tmp);
6647
6648 format = cstr_to_hexstr (st->format);
6649
6650 sprintf (packet, "m%s:%s:%s",
6651 paddress ((uintptr_t) st->location),
6652 strid,
6653 format);
6654
6655 free (strid);
6656 free (format);
6657 }
6658
6659 /* Return the first static tracepoint, and initialize the state
6660 machine that will iterate through all the static tracepoints. */
6661
6662 static void
6663 cmd_qtfstm (char *packet)
6664 {
6665 trace_debug ("Returning first trace state variable definition");
6666
6667 if (first_marker ())
6668 response_ust_marker (packet, first_marker ());
6669 else
6670 strcpy (packet, "l");
6671 }
6672
6673 /* Return additional trace state variable definitions. */
6674
6675 static void
6676 cmd_qtsstm (char *packet)
6677 {
6678 trace_debug ("Returning static tracepoint");
6679
6680 if (next_st)
6681 response_ust_marker (packet, next_st);
6682 else
6683 strcpy (packet, "l");
6684 }
6685
6686 /* Disconnect the GDB probe from a marker at a given address. */
6687
6688 static void
6689 unprobe_marker_at (char *packet)
6690 {
6691 char *p = packet;
6692 ULONGEST address;
6693 struct marker_iter iter;
6694
6695 p += sizeof ("unprobe_marker_at:") - 1;
6696
6697 p = unpack_varlen_hex (p, &address);
6698
6699 USTF(marker_iter_reset) (&iter);
6700 USTF(marker_iter_start) (&iter);
6701 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6702 if ((uintptr_t ) iter.marker->location == address)
6703 {
6704 int result;
6705
6706 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
6707 iter.marker->name,
6708 GDB_PROBE_NAME);
6709 if (result < 0)
6710 warning ("could not disable marker %s/%s",
6711 iter.marker->channel, iter.marker->name);
6712 break;
6713 }
6714 }
6715
6716 /* Connect the GDB probe to a marker at a given address. */
6717
6718 static int
6719 probe_marker_at (char *packet)
6720 {
6721 char *p = packet;
6722 ULONGEST address;
6723 struct marker_iter iter;
6724 struct marker *m;
6725
6726 p += sizeof ("probe_marker_at:") - 1;
6727
6728 p = unpack_varlen_hex (p, &address);
6729
6730 USTF(marker_iter_reset) (&iter);
6731
6732 for (USTF(marker_iter_start) (&iter), m = iter.marker;
6733 m != NULL;
6734 USTF(marker_iter_next) (&iter), m = iter.marker)
6735 if ((uintptr_t ) m->location == address)
6736 {
6737 int result;
6738
6739 trace_debug ("found marker for address. "
6740 "ltt_marker_connect (marker = %s/%s)",
6741 m->channel, m->name);
6742
6743 result = USTF(ltt_marker_connect) (m->channel, m->name,
6744 GDB_PROBE_NAME);
6745 if (result && result != -EEXIST)
6746 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6747 m->channel, m->name, -result);
6748
6749 if (result < 0)
6750 {
6751 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
6752 m->channel, m->name);
6753 return -1;
6754 }
6755
6756 strcpy (packet, "OK");
6757 return 0;
6758 }
6759
6760 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
6761 return -1;
6762 }
6763
6764 static int
6765 cmd_qtstmat (char *packet)
6766 {
6767 char *p = packet;
6768 ULONGEST address;
6769 struct marker_iter iter;
6770 struct marker *m;
6771
6772 p += sizeof ("qTSTMat:") - 1;
6773
6774 p = unpack_varlen_hex (p, &address);
6775
6776 USTF(marker_iter_reset) (&iter);
6777
6778 for (USTF(marker_iter_start) (&iter), m = iter.marker;
6779 m != NULL;
6780 USTF(marker_iter_next) (&iter), m = iter.marker)
6781 if ((uintptr_t ) m->location == address)
6782 {
6783 response_ust_marker (packet, m);
6784 return 0;
6785 }
6786
6787 strcpy (packet, "l");
6788 return -1;
6789 }
6790
6791 static void
6792 gdb_ust_init (void)
6793 {
6794 if (!dlsym_ust ())
6795 return;
6796
6797 USTF(ltt_probe_register) (&gdb_ust_probe);
6798 }
6799
6800 #endif /* HAVE_UST */
6801
6802 #include <sys/syscall.h>
6803
6804 /* Helper thread of agent. */
6805
6806 static void *
6807 gdb_agent_helper_thread (void *arg)
6808 {
6809 int listen_fd;
6810
6811 while (1)
6812 {
6813 listen_fd = gdb_agent_socket_init ();
6814
6815 if (helper_thread_id == 0)
6816 helper_thread_id = syscall (SYS_gettid);
6817
6818 if (listen_fd == -1)
6819 {
6820 warning ("could not create sync socket\n");
6821 break;
6822 }
6823
6824 while (1)
6825 {
6826 socklen_t tmp;
6827 struct sockaddr_un sockaddr;
6828 int fd;
6829 char buf[1];
6830 int ret;
6831
6832 tmp = sizeof (sockaddr);
6833
6834 do
6835 {
6836 fd = accept (listen_fd, &sockaddr, &tmp);
6837 }
6838 /* It seems an ERESTARTSYS can escape out of accept. */
6839 while (fd == -512 || (fd == -1 && errno == EINTR));
6840
6841 if (fd < 0)
6842 {
6843 warning ("Accept returned %d, error: %s\n",
6844 fd, strerror (errno));
6845 break;
6846 }
6847
6848 do
6849 {
6850 ret = read (fd, buf, 1);
6851 } while (ret == -1 && errno == EINTR);
6852
6853 if (ret == -1)
6854 {
6855 warning ("reading socket (fd=%d) failed with %s",
6856 fd, strerror (errno));
6857 close (fd);
6858 break;
6859 }
6860
6861 if (cmd_buf[0])
6862 {
6863 #ifdef HAVE_UST
6864 if (strcmp ("qTfSTM", cmd_buf) == 0)
6865 {
6866 cmd_qtfstm (cmd_buf);
6867 }
6868 else if (strcmp ("qTsSTM", cmd_buf) == 0)
6869 {
6870 cmd_qtsstm (cmd_buf);
6871 }
6872 else if (strncmp ("unprobe_marker_at:",
6873 cmd_buf,
6874 sizeof ("unprobe_marker_at:") - 1) == 0)
6875 {
6876 unprobe_marker_at (cmd_buf);
6877 }
6878 else if (strncmp ("probe_marker_at:",
6879 cmd_buf,
6880 sizeof ("probe_marker_at:") - 1) == 0)
6881 {
6882 probe_marker_at (cmd_buf);
6883 }
6884 else if (strncmp ("qTSTMat:",
6885 cmd_buf,
6886 sizeof ("qTSTMat:") - 1) == 0)
6887 {
6888 cmd_qtstmat (cmd_buf);
6889 }
6890 #endif /* HAVE_UST */
6891 }
6892
6893 /* Fix compiler's warning: ignoring return value of 'write'. */
6894 ret = write (fd, buf, 1);
6895 close (fd);
6896 }
6897 }
6898
6899 return NULL;
6900 }
6901
6902 #include <signal.h>
6903 #include <pthread.h>
6904
6905 IP_AGENT_EXPORT int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
6906
6907 static void
6908 gdb_agent_init (void)
6909 {
6910 int res;
6911 pthread_t thread;
6912 sigset_t new_mask;
6913 sigset_t orig_mask;
6914
6915 /* We want the helper thread to be as transparent as possible, so
6916 have it inherit an all-signals-blocked mask. */
6917
6918 sigfillset (&new_mask);
6919 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
6920 if (res)
6921 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
6922
6923 res = pthread_create (&thread,
6924 NULL,
6925 gdb_agent_helper_thread,
6926 NULL);
6927
6928 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
6929 if (res)
6930 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
6931
6932 while (helper_thread_id == 0)
6933 usleep (1);
6934
6935 #ifdef HAVE_UST
6936 gdb_ust_init ();
6937 #endif
6938 }
6939
6940 #include <sys/mman.h>
6941 #include <fcntl.h>
6942
6943 IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
6944 IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
6945 IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
6946 IP_AGENT_EXPORT char *gdb_trampoline_buffer;
6947 IP_AGENT_EXPORT char *gdb_trampoline_buffer_end;
6948 IP_AGENT_EXPORT char *gdb_trampoline_buffer_error;
6949
6950 /* Record the result of getting buffer space for fast tracepoint
6951 trampolines. Any error message is copied, since caller may not be
6952 using persistent storage. */
6953
6954 void
6955 set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
6956 {
6957 gdb_trampoline_buffer = (char *) (uintptr_t) begin;
6958 gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
6959 if (errmsg)
6960 strncpy (gdb_trampoline_buffer_error, errmsg, 99);
6961 else
6962 strcpy (gdb_trampoline_buffer_error, "no buffer passed");
6963 }
6964
6965 static void __attribute__ ((constructor))
6966 initialize_tracepoint_ftlib (void)
6967 {
6968 initialize_tracepoint ();
6969
6970 gdb_agent_init ();
6971 }
6972
6973 #endif /* IN_PROCESS_AGENT */
6974
6975 /* Return a timestamp, expressed as microseconds of the usual Unix
6976 time. (As the result is a 64-bit number, it will not overflow any
6977 time soon.) */
6978
6979 static LONGEST
6980 get_timestamp (void)
6981 {
6982 struct timeval tv;
6983
6984 if (gettimeofday (&tv, 0) != 0)
6985 return -1;
6986 else
6987 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
6988 }
6989
6990 void
6991 initialize_tracepoint (void)
6992 {
6993 /* There currently no way to change the buffer size. */
6994 const int sizeOfBuffer = 5 * 1024 * 1024;
6995 unsigned char *buf = xmalloc (sizeOfBuffer);
6996 init_trace_buffer (buf, sizeOfBuffer);
6997
6998 /* Wire trace state variable 1 to be the timestamp. This will be
6999 uploaded to GDB upon connection and become one of its trace state
7000 variables. (In case you're wondering, if GDB already has a trace
7001 variable numbered 1, it will be renumbered.) */
7002 create_trace_state_variable (1, 0);
7003 set_trace_state_variable_name (1, "trace_timestamp");
7004 set_trace_state_variable_getter (1, get_timestamp);
7005
7006 #ifdef IN_PROCESS_AGENT
7007 {
7008 uintptr_t addr;
7009 int pagesize;
7010
7011 pagesize = sysconf (_SC_PAGE_SIZE);
7012 if (pagesize == -1)
7013 fatal ("sysconf");
7014
7015 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
7016
7017 #define SCRATCH_BUFFER_NPAGES 20
7018
7019 /* Allocate scratch buffer aligned on a page boundary, at a low
7020 address (close to the main executable's code). */
7021 for (addr = pagesize; addr != 0; addr += pagesize)
7022 {
7023 gdb_jump_pad_buffer = mmap ((void *) addr, pagesize * SCRATCH_BUFFER_NPAGES,
7024 PROT_READ | PROT_WRITE | PROT_EXEC,
7025 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
7026 -1, 0);
7027 if (gdb_jump_pad_buffer != MAP_FAILED)
7028 break;
7029 }
7030
7031 if (addr == 0)
7032 fatal ("\
7033 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7034 strerror (errno));
7035
7036 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * SCRATCH_BUFFER_NPAGES;
7037 }
7038
7039 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
7040
7041 /* It's not a fatal error for something to go wrong with trampoline
7042 buffer setup, but it can be mysterious, so create a channel to
7043 report back on what went wrong, using a fixed size since we may
7044 not be able to allocate space later when the problem occurs. */
7045 gdb_trampoline_buffer_error = xmalloc (IPA_BUFSIZ);
7046
7047 strcpy (gdb_trampoline_buffer_error, "No errors reported");
7048
7049 initialize_low_tracepoint ();
7050 #endif
7051 }
This page took 0.221331 seconds and 4 git commands to generate.