1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
20 #include "gdbthread.h"
34 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
36 /* This file is built for both GDBserver, and the in-process
37 agent (IPA), a shared library that includes a tracing agent that is
38 loaded by the inferior to support fast tracepoints. Fast
39 tracepoints (or more accurately, jump based tracepoints) are
40 implemented by patching the tracepoint location with a jump into a
41 small trampoline function whose job is to save the register state,
42 call the in-process tracing agent, and then execute the original
43 instruction that was under the tracepoint jump (possibly adjusted,
44 if PC-relative, or some such).
46 The current synchronization design is pull based. That means,
47 GDBserver does most of the work, by peeking/poking at the inferior
48 agent's memory directly for downloading tracepoint and associated
49 objects, and for uploading trace frames. Whenever the IPA needs
50 something from GDBserver (trace buffer is full, tracing stopped for
51 some reason, etc.) the IPA calls a corresponding hook function
52 where GDBserver has placed a breakpoint.
54 Each of the agents has its own trace buffer. When browsing the
55 trace frames built from slow and fast tracepoints from GDB (tfind
56 mode), there's no guarantee the user is seeing the trace frames in
57 strict chronological creation order, although, GDBserver tries to
58 keep the order relatively reasonable, by syncing the trace buffers
63 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
66 trace_vdebug (const char *fmt
, ...)
72 vsprintf (buf
, fmt
, ap
);
73 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
77 #define trace_debug_1(level, fmt, args...) \
79 if (level <= debug_threads) \
80 trace_vdebug ((fmt), ##args); \
83 #define trace_debug(FMT, args...) \
84 trace_debug_1 (1, FMT, ##args)
87 # define ATTR_USED __attribute__((used))
88 # define ATTR_NOINLINE __attribute__((noinline))
89 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
92 # define ATTR_NOINLINE
93 # define ATTR_CONSTRUCTOR
96 /* Make sure the functions the IPA needs to export (symbols GDBserver
97 needs to query GDB about) are exported. */
99 #ifdef IN_PROCESS_AGENT
100 # if defined _WIN32 || defined __CYGWIN__
101 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
104 # define IP_AGENT_EXPORT \
105 __attribute__ ((visibility("default"))) ATTR_USED
107 # define IP_AGENT_EXPORT ATTR_USED
111 # define IP_AGENT_EXPORT
114 /* Prefix exported symbols, for good citizenship. All the symbols
115 that need exporting are defined in this module. */
116 #ifdef IN_PROCESS_AGENT
117 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
118 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
119 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
120 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
121 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
122 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
123 # define collecting gdb_agent_collecting
124 # define gdb_collect gdb_agent_gdb_collect
125 # define stop_tracing gdb_agent_stop_tracing
126 # define flush_trace_buffer gdb_agent_flush_trace_buffer
127 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
128 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
129 # define stopping_tracepoint gdb_agent_stopping_tracepoint
130 # define expr_eval_result gdb_agent_expr_eval_result
131 # define error_tracepoint gdb_agent_error_tracepoint
132 # define tracepoints gdb_agent_tracepoints
133 # define tracing gdb_agent_tracing
134 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
135 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
136 # define trace_buffer_lo gdb_agent_trace_buffer_lo
137 # define trace_buffer_hi gdb_agent_trace_buffer_hi
138 # define traceframe_read_count gdb_agent_traceframe_read_count
139 # define traceframe_write_count gdb_agent_traceframe_write_count
140 # define traceframes_created gdb_agent_traceframes_created
141 # define trace_state_variables gdb_agent_trace_state_variables
142 # define get_raw_reg gdb_agent_get_raw_reg
143 # define get_trace_state_variable_value \
144 gdb_agent_get_trace_state_variable_value
145 # define set_trace_state_variable_value \
146 gdb_agent_set_trace_state_variable_value
147 # define ust_loaded gdb_agent_ust_loaded
148 # define helper_thread_id gdb_agent_helper_thread_id
149 # define cmd_buf gdb_agent_cmd_buf
152 #ifndef IN_PROCESS_AGENT
154 /* Addresses of in-process agent's symbols GDBserver cares about. */
156 struct ipa_sym_addresses
158 CORE_ADDR addr_gdb_tp_heap_buffer
;
159 CORE_ADDR addr_gdb_jump_pad_buffer
;
160 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
161 CORE_ADDR addr_gdb_trampoline_buffer
;
162 CORE_ADDR addr_gdb_trampoline_buffer_end
;
163 CORE_ADDR addr_gdb_trampoline_buffer_error
;
164 CORE_ADDR addr_collecting
;
165 CORE_ADDR addr_gdb_collect
;
166 CORE_ADDR addr_stop_tracing
;
167 CORE_ADDR addr_flush_trace_buffer
;
168 CORE_ADDR addr_about_to_request_buffer_space
;
169 CORE_ADDR addr_trace_buffer_is_full
;
170 CORE_ADDR addr_stopping_tracepoint
;
171 CORE_ADDR addr_expr_eval_result
;
172 CORE_ADDR addr_error_tracepoint
;
173 CORE_ADDR addr_tracepoints
;
174 CORE_ADDR addr_tracing
;
175 CORE_ADDR addr_trace_buffer_ctrl
;
176 CORE_ADDR addr_trace_buffer_ctrl_curr
;
177 CORE_ADDR addr_trace_buffer_lo
;
178 CORE_ADDR addr_trace_buffer_hi
;
179 CORE_ADDR addr_traceframe_read_count
;
180 CORE_ADDR addr_traceframe_write_count
;
181 CORE_ADDR addr_traceframes_created
;
182 CORE_ADDR addr_trace_state_variables
;
183 CORE_ADDR addr_get_raw_reg
;
184 CORE_ADDR addr_get_trace_state_variable_value
;
185 CORE_ADDR addr_set_trace_state_variable_value
;
186 CORE_ADDR addr_ust_loaded
;
195 IPA_SYM(gdb_tp_heap_buffer
),
196 IPA_SYM(gdb_jump_pad_buffer
),
197 IPA_SYM(gdb_jump_pad_buffer_end
),
198 IPA_SYM(gdb_trampoline_buffer
),
199 IPA_SYM(gdb_trampoline_buffer_end
),
200 IPA_SYM(gdb_trampoline_buffer_error
),
202 IPA_SYM(gdb_collect
),
203 IPA_SYM(stop_tracing
),
204 IPA_SYM(flush_trace_buffer
),
205 IPA_SYM(about_to_request_buffer_space
),
206 IPA_SYM(trace_buffer_is_full
),
207 IPA_SYM(stopping_tracepoint
),
208 IPA_SYM(expr_eval_result
),
209 IPA_SYM(error_tracepoint
),
210 IPA_SYM(tracepoints
),
212 IPA_SYM(trace_buffer_ctrl
),
213 IPA_SYM(trace_buffer_ctrl_curr
),
214 IPA_SYM(trace_buffer_lo
),
215 IPA_SYM(trace_buffer_hi
),
216 IPA_SYM(traceframe_read_count
),
217 IPA_SYM(traceframe_write_count
),
218 IPA_SYM(traceframes_created
),
219 IPA_SYM(trace_state_variables
),
220 IPA_SYM(get_raw_reg
),
221 IPA_SYM(get_trace_state_variable_value
),
222 IPA_SYM(set_trace_state_variable_value
),
226 static struct ipa_sym_addresses ipa_sym_addrs
;
228 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
230 /* Returns true if both the in-process agent library and the static
231 tracepoints libraries are loaded in the inferior, and agent has
232 capability on static tracepoints. */
235 in_process_agent_supports_ust (void)
239 if (!agent_loaded_p ())
241 warning ("In-process agent not loaded");
245 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
247 /* Agent understands static tracepoint, then check whether UST is in
248 fact loaded in the inferior. */
249 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
251 warning ("Error reading ust_loaded in lib");
262 write_e_ipa_not_loaded (char *buffer
)
265 "E.In-process agent library not loaded in process. "
266 "Fast and static tracepoints unavailable.");
269 /* Write an error to BUFFER indicating that UST isn't loaded in the
273 write_e_ust_not_loaded (char *buffer
)
277 "E.UST library not loaded in process. "
278 "Static tracepoints unavailable.");
280 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
284 /* If the in-process agent library isn't loaded in the inferior, write
285 an error to BUFFER, and return 1. Otherwise, return 0. */
288 maybe_write_ipa_not_loaded (char *buffer
)
290 if (!agent_loaded_p ())
292 write_e_ipa_not_loaded (buffer
);
298 /* If the in-process agent library and the ust (static tracepoints)
299 library aren't loaded in the inferior, write an error to BUFFER,
300 and return 1. Otherwise, return 0. */
303 maybe_write_ipa_ust_not_loaded (char *buffer
)
305 if (!agent_loaded_p ())
307 write_e_ipa_not_loaded (buffer
);
310 else if (!in_process_agent_supports_ust ())
312 write_e_ust_not_loaded (buffer
);
318 /* Cache all future symbols that the tracepoints module might request.
319 We can not request symbols at arbitrary states in the remote
320 protocol, only when the client tells us that new symbols are
321 available. So when we load the in-process library, make sure to
322 check the entire list. */
325 tracepoint_look_up_symbols (void)
329 if (agent_loaded_p ())
332 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
335 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
337 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
340 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
345 agent_look_up_symbols (NULL
);
350 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
351 of the "stop_tracing" function. When this breakpoint is hit,
352 tracing stopped in the IPA for some reason. E.g., due to
353 tracepoint reaching the pass count, hitting conditional expression
354 evaluation error, etc.
356 The IPA's trace buffer is never in circular tracing mode: instead,
357 GDBserver's is, and whenever the in-process buffer fills, it calls
358 "flush_trace_buffer", which triggers an internal breakpoint.
359 GDBserver reacts to this breakpoint by pulling the meanwhile
360 collected data. Old frames discarding is always handled on the
363 #ifdef IN_PROCESS_AGENT
365 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
367 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
371 /* Call this in the functions where GDBserver places a breakpoint, so
372 that the compiler doesn't try to be clever and skip calling the
373 function at all. This is necessary, even if we tell the compiler
374 to not inline said functions. */
376 #if defined(__GNUC__)
377 # define UNKNOWN_SIDE_EFFECTS() asm ("")
379 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
382 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
385 /* GDBserver places breakpoint here. */
386 UNKNOWN_SIDE_EFFECTS();
389 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
390 flush_trace_buffer (void)
392 /* GDBserver places breakpoint here. */
393 UNKNOWN_SIDE_EFFECTS();
398 #ifndef IN_PROCESS_AGENT
400 tracepoint_handler (CORE_ADDR address
)
402 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
407 /* Breakpoint at "stop_tracing" in the inferior lib. */
408 struct breakpoint
*stop_tracing_bkpt
;
409 static int stop_tracing_handler (CORE_ADDR
);
411 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
412 struct breakpoint
*flush_trace_buffer_bkpt
;
413 static int flush_trace_buffer_handler (CORE_ADDR
);
415 static void download_trace_state_variables (void);
416 static void upload_fast_traceframes (void);
418 static int run_inferior_command (char *cmd
, int len
);
421 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
423 return read_inferior_memory (symaddr
, (unsigned char *) val
,
428 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
431 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
433 return read_inferior_memory (symaddr
, (unsigned char *) val
,
438 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
440 void *pval
= (void *) (uintptr_t) val
;
443 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
444 *val
= (uintptr_t) pval
;
449 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
451 void *pval
= (void *) (uintptr_t) val
;
452 return write_inferior_memory (symaddr
,
453 (unsigned char *) &pval
, sizeof (pval
));
457 write_inferior_integer (CORE_ADDR symaddr
, int val
)
459 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
463 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
465 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
468 static CORE_ADDR
target_malloc (ULONGEST size
);
469 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
471 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
473 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
474 BUF += sizeof ((OBJ)->FIELD); \
479 /* Operations on various types of tracepoint actions. */
481 struct tracepoint_action
;
483 struct tracepoint_action_ops
485 /* Download tracepoint action ACTION to IPA. Return the address of action
487 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
489 /* Send ACTION to agent via command buffer started from BUFFER. Return
490 updated head of command buffer. */
491 char* (*send
) (char *buffer
, const struct tracepoint_action
*action
);
494 /* Base action. Concrete actions inherit this. */
496 struct tracepoint_action
498 #ifndef IN_PROCESS_AGENT
499 const struct tracepoint_action_ops
*ops
;
504 /* An 'M' (collect memory) action. */
505 struct collect_memory_action
507 struct tracepoint_action base
;
514 /* An 'R' (collect registers) action. */
516 struct collect_registers_action
518 struct tracepoint_action base
;
521 /* An 'X' (evaluate expression) action. */
523 struct eval_expr_action
525 struct tracepoint_action base
;
527 struct agent_expr
*expr
;
530 /* An 'L' (collect static trace data) action. */
531 struct collect_static_trace_data_action
533 struct tracepoint_action base
;
536 #ifndef IN_PROCESS_AGENT
538 m_tracepoint_action_download (const struct tracepoint_action
*action
)
540 int size_in_ipa
= (sizeof (struct collect_memory_action
)
541 - offsetof (struct tracepoint_action
, type
));
542 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
544 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
550 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
552 struct collect_memory_action
*maction
553 = (struct collect_memory_action
*) action
;
555 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
556 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
557 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
562 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
564 m_tracepoint_action_download
,
565 m_tracepoint_action_send
,
569 r_tracepoint_action_download (const struct tracepoint_action
*action
)
571 int size_in_ipa
= (sizeof (struct collect_registers_action
)
572 - offsetof (struct tracepoint_action
, type
));
573 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
575 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
582 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
587 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
589 r_tracepoint_action_download
,
590 r_tracepoint_action_send
,
593 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
596 x_tracepoint_action_download (const struct tracepoint_action
*action
)
598 int size_in_ipa
= (sizeof (struct eval_expr_action
)
599 - offsetof (struct tracepoint_action
, type
));
600 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
603 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
605 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
606 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
607 - offsetof (struct tracepoint_action
, type
),
613 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
614 copy 0 to P. Return updated header of buffer. */
617 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
619 /* Copy the length of condition first, and then copy its
628 memcpy (p
, &aexpr
->length
, 4);
631 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
638 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
640 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
642 return agent_expr_send (buffer
, eaction
->expr
);
645 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
647 x_tracepoint_action_download
,
648 x_tracepoint_action_send
,
652 l_tracepoint_action_download (const struct tracepoint_action
*action
)
654 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
655 - offsetof (struct tracepoint_action
, type
));
656 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
658 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
665 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
670 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
672 l_tracepoint_action_download
,
673 l_tracepoint_action_send
,
677 /* This structure describes a piece of the source-level definition of
678 the tracepoint. The contents are not interpreted by the target,
679 but preserved verbatim for uploading upon reconnection. */
683 /* The type of string, such as "cond" for a conditional. */
686 /* The source-level string itself. For the sake of target
687 debugging, we store it in plaintext, even though it is always
688 transmitted in hex. */
691 /* Link to the next one in the list. We link them in the order
692 received, in case some make up an ordered list of commands or
694 struct source_string
*next
;
699 /* Trap based tracepoint. */
702 /* A fast tracepoint implemented with a jump instead of a trap. */
705 /* A static tracepoint, implemented by a program call into a tracing
710 struct tracepoint_hit_ctx
;
712 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
715 /* The definition of a tracepoint. */
717 /* Tracepoints may have multiple locations, each at a different
718 address. This can occur with optimizations, template
719 instantiation, etc. Since the locations may be in different
720 scopes, the conditions and actions may be different for each
721 location. Our target version of tracepoints is more like GDB's
722 notion of "breakpoint locations", but we have almost nothing that
723 is not per-location, so we bother having two kinds of objects. The
724 key consequence is that numbers are not unique, and that it takes
725 both number and address to identify a tracepoint uniquely. */
729 /* The number of the tracepoint, as specified by GDB. Several
730 tracepoint objects here may share a number. */
733 /* Address at which the tracepoint is supposed to trigger. Several
734 tracepoints may share an address. */
737 /* Tracepoint type. */
738 enum tracepoint_type type
;
740 /* True if the tracepoint is currently enabled. */
743 /* The number of single steps that will be performed after each
747 /* The number of times the tracepoint may be hit before it will
748 terminate the entire tracing run. */
751 /* Pointer to the agent expression that is the tracepoint's
752 conditional, or NULL if the tracepoint is unconditional. */
753 struct agent_expr
*cond
;
755 /* The list of actions to take when the tracepoint triggers. */
757 struct tracepoint_action
**actions
;
759 /* Count of the times we've hit this tracepoint during the run.
760 Note that while-stepping steps are not counted as "hits". */
763 /* Cached sum of the sizes of traceframes created by this point. */
764 uint64_t traceframe_usage
;
766 CORE_ADDR compiled_cond
;
768 /* Link to the next tracepoint in the list. */
769 struct tracepoint
*next
;
771 #ifndef IN_PROCESS_AGENT
772 /* The list of actions to take when the tracepoint triggers, in
773 string/packet form. */
776 /* The collection of strings that describe the tracepoint as it was
777 entered into GDB. These are not used by the target, but are
778 reported back to GDB upon reconnection. */
779 struct source_string
*source_strings
;
781 /* The number of bytes displaced by fast tracepoints. It may subsume
782 multiple instructions, for multi-byte fast tracepoints. This
783 field is only valid for fast tracepoints. */
786 /* Only for fast tracepoints. */
787 CORE_ADDR obj_addr_on_target
;
789 /* Address range where the original instruction under a fast
790 tracepoint was relocated to. (_end is actually one byte past
792 CORE_ADDR adjusted_insn_addr
;
793 CORE_ADDR adjusted_insn_addr_end
;
795 /* The address range of the piece of the jump pad buffer that was
796 assigned to this fast tracepoint. (_end is actually one byte
799 CORE_ADDR jump_pad_end
;
801 /* The address range of the piece of the trampoline buffer that was
802 assigned to this fast tracepoint. (_end is actually one byte
804 CORE_ADDR trampoline
;
805 CORE_ADDR trampoline_end
;
807 /* The list of actions to take while in a stepping loop. These
808 fields are only valid for patch-based tracepoints. */
809 int num_step_actions
;
810 struct tracepoint_action
**step_actions
;
811 /* Same, but in string/packet form. */
812 char **step_actions_str
;
814 /* Handle returned by the breakpoint or tracepoint module when we
815 inserted the trap or jump, or hooked into a static tracepoint.
816 NULL if we haven't inserted it yet. */
822 #ifndef IN_PROCESS_AGENT
824 /* Given `while-stepping', a thread may be collecting data for more
825 than one tracepoint simultaneously. On the other hand, the same
826 tracepoint with a while-stepping action may be hit by more than one
827 thread simultaneously (but not quite, each thread could be handling
828 a different step). Each thread holds a list of these objects,
829 representing the current step of each while-stepping action being
834 struct wstep_state
*next
;
836 /* The tracepoint number. */
838 /* The tracepoint's address. */
839 CORE_ADDR tp_address
;
841 /* The number of the current step in this 'while-stepping'
848 /* The linked list of all tracepoints. Marked explicitly as used as
849 the in-process library doesn't use it for the fast tracepoints
851 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
853 #ifndef IN_PROCESS_AGENT
855 /* Pointer to the last tracepoint in the list, new tracepoints are
856 linked in at the end. */
858 static struct tracepoint
*last_tracepoint
;
861 /* The first tracepoint to exceed its pass count. */
863 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
865 /* True if the trace buffer is full or otherwise no longer usable. */
867 IP_AGENT_EXPORT
int trace_buffer_is_full
;
869 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
871 #ifndef IN_PROCESS_AGENT
873 static const char *eval_result_names
[] =
875 "terror:in the attic", /* this should never be reported */
876 "terror:empty expression",
877 "terror:empty stack",
878 "terror:stack overflow",
879 "terror:stack underflow",
880 "terror:unhandled opcode",
881 "terror:unrecognized opcode",
882 "terror:divide by zero"
887 /* The tracepoint in which the error occurred. */
889 static struct tracepoint
*error_tracepoint
;
891 struct trace_state_variable
893 /* This is the name of the variable as used in GDB. The target
894 doesn't use the name, but needs to have it for saving and
895 reconnection purposes. */
898 /* This number identifies the variable uniquely. Numbers may be
899 assigned either by the target (in the case of builtin variables),
900 or by GDB, and are presumed unique during the course of a trace
904 /* The variable's initial value, a 64-bit signed integer always. */
905 LONGEST initial_value
;
907 /* The variable's value, a 64-bit signed integer always. */
910 /* Pointer to a getter function, used to supply computed values. */
911 LONGEST (*getter
) (void);
913 /* Link to the next variable. */
914 struct trace_state_variable
*next
;
917 /* Linked list of all trace state variables. */
919 #ifdef IN_PROCESS_AGENT
920 struct trace_state_variable
*alloced_trace_state_variables
;
923 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
925 /* The results of tracing go into a fixed-size space known as the
926 "trace buffer". Because usage follows a limited number of
927 patterns, we manage it ourselves rather than with malloc. Basic
928 rules are that we create only one trace frame at a time, each is
929 variable in size, they are never moved once created, and we only
930 discard if we are doing a circular buffer, and then only the oldest
931 ones. Each trace frame includes its own size, so we don't need to
932 link them together, and the trace frame number is relative to the
933 first one, so we don't need to record numbers. A trace frame also
934 records the number of the tracepoint that created it. The data
935 itself is a series of blocks, each introduced by a single character
936 and with a defined format. Each type of block has enough
937 type/length info to allow scanners to jump quickly from one block
938 to the next without reading each byte in the block. */
940 /* Trace buffer management would be simple - advance a free pointer
941 from beginning to end, then stop - were it not for the circular
942 buffer option, which is a useful way to prevent a trace run from
943 stopping prematurely because the buffer filled up. In the circular
944 case, the location of the first trace frame (trace_buffer_start)
945 moves as old trace frames are discarded. Also, since we grow trace
946 frames incrementally as actions are performed, we wrap around to
947 the beginning of the trace buffer. This is per-block, so each
948 block within a trace frame remains contiguous. Things get messy
949 when the wrapped-around trace frame is the one being discarded; the
950 free space ends up in two parts at opposite ends of the buffer. */
953 # if defined(__GNUC__)
954 # define ATTR_PACKED __attribute__ ((packed))
956 # define ATTR_PACKED /* nothing */
960 /* The data collected at a tracepoint hit. This object should be as
961 small as possible, since there may be a great many of them. We do
962 not need to keep a frame number, because they are all sequential
963 and there are no deletions; so the Nth frame in the buffer is
964 always frame number N. */
968 /* Number of the tracepoint that collected this traceframe. A value
969 of 0 indicates the current end of the trace buffer. We make this
970 a 16-bit field because it's never going to happen that GDB's
971 numbering of tracepoints reaches 32,000. */
974 /* The size of the data in this trace frame. We limit this to 32
975 bits, even on a 64-bit target, because it's just implausible that
976 one is validly going to collect 4 gigabytes of data at a single
978 unsigned int data_size
: 32;
980 /* The base of the trace data, which is contiguous from this point. */
981 unsigned char data
[0];
985 /* The size of the EOB marker, in bytes. A traceframe with zeroed
986 fields (and no data) marks the end of trace data. */
987 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
989 /* The traceframe to be used as the source of data to send back to
990 GDB. A value of -1 means to get data from the live program. */
992 int current_traceframe
= -1;
994 /* This flag is true if the trace buffer is circular, meaning that
995 when it fills, the oldest trace frames are discarded in order to
998 #ifndef IN_PROCESS_AGENT
999 static int circular_trace_buffer
;
1002 /* Size of the trace buffer. */
1004 static LONGEST trace_buffer_size
;
1006 /* Pointer to the block of memory that traceframes all go into. */
1008 static unsigned char *trace_buffer_lo
;
1010 /* Pointer to the end of the trace buffer, more precisely to the byte
1011 after the end of the buffer. */
1013 static unsigned char *trace_buffer_hi
;
1015 /* Control structure holding the read/write/etc. pointers into the
1016 trace buffer. We need more than one of these to implement a
1017 transaction-like mechanism to garantees that both GDBserver and the
1018 in-process agent can try to change the trace buffer
1021 struct trace_buffer_control
1023 /* Pointer to the first trace frame in the buffer. In the
1024 non-circular case, this is equal to trace_buffer_lo, otherwise it
1025 moves around in the buffer. */
1026 unsigned char *start
;
1028 /* Pointer to the free part of the trace buffer. Note that we clear
1029 several bytes at and after this pointer, so that traceframe
1030 scans/searches terminate properly. */
1031 unsigned char *free
;
1033 /* Pointer to the byte after the end of the free part. Note that
1034 this may be smaller than trace_buffer_free in the circular case,
1035 and means that the free part is in two pieces. Initially it is
1036 equal to trace_buffer_hi, then is generally equivalent to
1037 trace_buffer_start. */
1038 unsigned char *end_free
;
1040 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1041 this is the point at which the trace data breaks, and resumes at
1043 unsigned char *wrap
;
1046 /* Same as above, to be used by GDBserver when updating the in-process
1048 struct ipa_trace_buffer_control
1057 /* We have possibly both GDBserver and an inferior thread accessing
1058 the same IPA trace buffer memory. The IPA is the producer (tries
1059 to put new frames in the buffer), while GDBserver occasionally
1060 consumes them, that is, flushes the IPA's buffer into its own
1061 buffer. Both sides need to update the trace buffer control
1062 pointers (current head, tail, etc.). We can't use a global lock to
1063 synchronize the accesses, as otherwise we could deadlock GDBserver
1064 (if the thread holding the lock stops for a signal, say). So
1065 instead of that, we use a transaction scheme where GDBserver writes
1066 always prevail over the IPAs writes, and, we have the IPA detect
1067 the commit failure/overwrite, and retry the whole attempt. This is
1068 mainly implemented by having a global token object that represents
1069 who wrote last to the buffer control structure. We need to freeze
1070 any inferior writing to the buffer while GDBserver touches memory,
1071 so that the inferior can correctly detect that GDBserver had been
1072 there, otherwise, it could mistakingly think its commit was
1073 successful; that's implemented by simply having GDBserver set a
1074 breakpoint the inferior hits if it is the critical region.
1076 There are three cycling trace buffer control structure copies
1077 (buffer head, tail, etc.), with the token object including an index
1078 indicating which is current live copy. The IPA tentatively builds
1079 an updated copy in a non-current control structure, while GDBserver
1080 always clobbers the current version directly. The IPA then tries
1081 to atomically "commit" its version; if GDBserver clobbered the
1082 structure meanwhile, that will fail, and the IPA restarts the
1085 Listing the step in further detail, we have:
1087 In-process agent (producer):
1089 - passes by `about_to_request_buffer_space' breakpoint/lock
1091 - reads current token, extracts current trace buffer control index,
1092 and starts tentatively updating the rightmost one (0->1, 1->2,
1093 2->0). Note that only one inferior thread is executing this code
1094 at any given time, due to an outer lock in the jump pads.
1096 - updates counters, and tries to commit the token.
1098 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1099 leaving the sync region.
1101 - checks if the update was effective.
1103 - if trace buffer was found full, hits flush_trace_buffer
1104 breakpoint, and restarts later afterwards.
1106 GDBserver (consumer):
1108 - sets `about_to_request_buffer_space' breakpoint/lock.
1110 - updates the token unconditionally, using the current buffer
1111 control index, since it knows that the IP agent always writes to
1112 the rightmost, and due to the breakpoint, at most one IP thread
1113 can try to update the trace buffer concurrently to GDBserver, so
1114 there will be no danger of trace buffer control index wrap making
1115 the IPA write to the same index as GDBserver.
1117 - flushes the IP agent's trace buffer completely, and updates the
1118 current trace buffer control structure. GDBserver *always* wins.
1120 - removes the `about_to_request_buffer_space' breakpoint.
1122 The token is stored in the `trace_buffer_ctrl_curr' variable.
1123 Internally, it's bits are defined as:
1125 |-------------+-----+-------------+--------+-------------+--------------|
1126 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1127 |-------------+-----+-------------+--------+-------------+--------------|
1128 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1129 |-------------+-----+-------------+--------+-------------+--------------|
1131 GSB - GDBserver Stamp Bit
1132 PC - Previous Counter
1133 CC - Current Counter
1134 TBCI - Trace Buffer Control Index
1137 An IPA update of `trace_buffer_ctrl_curr' does:
1139 - read CC from the current token, save as PC.
1141 - atomically tries to write PC+1,CC
1143 A GDBserver update of `trace_buffer_ctrl_curr' does:
1145 - reads PC and CC from the current token.
1150 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1151 for the counters described below. The cleared bits are used to
1152 hold the index of the items of the `trace_buffer_ctrl' array that
1154 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1156 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1157 counter, and the `current' counter. */
1159 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1160 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1162 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1163 always stamps this bit as set. */
1164 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1166 #ifdef IN_PROCESS_AGENT
1167 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1168 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1170 # define TRACE_BUFFER_CTRL_CURR \
1171 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1175 /* The GDBserver side agent only needs one instance of this object, as
1176 it doesn't need to sync with itself. Define it as array anyway so
1177 that the rest of the code base doesn't need to care for the
1179 struct trace_buffer_control trace_buffer_ctrl
[1];
1180 # define TRACE_BUFFER_CTRL_CURR 0
1183 /* These are convenience macros used to access the current trace
1184 buffer control in effect. */
1185 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1186 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1187 #define trace_buffer_end_free \
1188 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1189 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1192 /* Macro that returns a pointer to the first traceframe in the buffer. */
1194 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1196 /* Macro that returns a pointer to the next traceframe in the buffer.
1197 If the computed location is beyond the wraparound point, subtract
1198 the offset of the wraparound. */
1200 #define NEXT_TRACEFRAME_1(TF) \
1201 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1203 #define NEXT_TRACEFRAME(TF) \
1204 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1205 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1206 ? (trace_buffer_wrap - trace_buffer_lo) \
1209 /* The difference between these counters represents the total number
1210 of complete traceframes present in the trace buffer. The IP agent
1211 writes to the write count, GDBserver writes to read count. */
1213 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1214 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1216 /* Convenience macro. */
1218 #define traceframe_count \
1219 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1221 /* The count of all traceframes created in the current run, including
1222 ones that were discarded to make room. */
1224 IP_AGENT_EXPORT
int traceframes_created
;
1226 #ifndef IN_PROCESS_AGENT
1228 /* Read-only regions are address ranges whose contents don't change,
1229 and so can be read from target memory even while looking at a trace
1230 frame. Without these, disassembly for instance will likely fail,
1231 because the program code is not usually collected into a trace
1232 frame. This data structure does not need to be very complicated or
1233 particularly efficient, it's only going to be used occasionally,
1234 and only by some commands. */
1236 struct readonly_region
1238 /* The bounds of the region. */
1239 CORE_ADDR start
, end
;
1241 /* Link to the next one. */
1242 struct readonly_region
*next
;
1245 /* Linked list of readonly regions. This list stays in effect from
1246 one tstart to the next. */
1248 static struct readonly_region
*readonly_regions
;
1252 /* The global that controls tracing overall. */
1254 IP_AGENT_EXPORT
int tracing
;
1256 #ifndef IN_PROCESS_AGENT
1258 /* Controls whether tracing should continue after GDB disconnects. */
1260 int disconnected_tracing
;
1262 /* The reason for the last tracing run to have stopped. We initialize
1263 to a distinct string so that GDB can distinguish between "stopped
1264 after running" and "stopped because never run" cases. */
1266 static const char *tracing_stop_reason
= "tnotrun";
1268 static int tracing_stop_tpnum
;
1270 /* 64-bit timestamps for the trace run's start and finish, expressed
1271 in microseconds from the Unix epoch. */
1273 LONGEST tracing_start_time
;
1274 LONGEST tracing_stop_time
;
1276 /* The (optional) user-supplied name of the user that started the run.
1277 This is an arbitrary string, and may be NULL. */
1279 char *tracing_user_name
;
1281 /* Optional user-supplied text describing the run. This is
1282 an arbitrary string, and may be NULL. */
1284 char *tracing_notes
;
1286 /* Optional user-supplied text explaining a tstop command. This is an
1287 arbitrary string, and may be NULL. */
1289 char *tracing_stop_note
;
1293 /* Functions local to this file. */
1295 /* Base "class" for tracepoint type specific data to be passed down to
1296 collect_data_at_tracepoint. */
1297 struct tracepoint_hit_ctx
1299 enum tracepoint_type type
;
1302 #ifdef IN_PROCESS_AGENT
1304 /* Fast/jump tracepoint specific data to be passed down to
1305 collect_data_at_tracepoint. */
1306 struct fast_tracepoint_ctx
1308 struct tracepoint_hit_ctx base
;
1310 struct regcache regcache
;
1311 int regcache_initted
;
1312 unsigned char *regspace
;
1314 unsigned char *regs
;
1315 struct tracepoint
*tpoint
;
1318 /* Static tracepoint specific data to be passed down to
1319 collect_data_at_tracepoint. */
1320 struct static_tracepoint_ctx
1322 struct tracepoint_hit_ctx base
;
1324 /* The regcache corresponding to the registers state at the time of
1325 the tracepoint hit. Initialized lazily, from REGS. */
1326 struct regcache regcache
;
1327 int regcache_initted
;
1329 /* The buffer space REGCACHE above uses. We use a separate buffer
1330 instead of letting the regcache malloc for both signal safety and
1331 performance reasons; this is allocated on the stack instead. */
1332 unsigned char *regspace
;
1334 /* The register buffer as passed on by lttng/ust. */
1335 struct registers
*regs
;
1337 /* The "printf" formatter and the args the user passed to the marker
1338 call. We use this to be able to collect "static trace data"
1343 /* The GDB tracepoint matching the probed marker that was "hit". */
1344 struct tracepoint
*tpoint
;
1349 /* Static tracepoint specific data to be passed down to
1350 collect_data_at_tracepoint. */
1351 struct trap_tracepoint_ctx
1353 struct tracepoint_hit_ctx base
;
1355 struct regcache
*regcache
;
1360 #ifndef IN_PROCESS_AGENT
1361 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1362 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1365 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1366 struct tracepoint
*tpoint
);
1368 #ifndef IN_PROCESS_AGENT
1369 static void clear_readonly_regions (void);
1370 static void clear_installed_tracepoints (void);
1373 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1375 struct tracepoint
*tpoint
);
1376 #ifndef IN_PROCESS_AGENT
1377 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1379 struct tracepoint
*tpoint
, int current_step
);
1380 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1381 CORE_ADDR
*jump_entry
);
1383 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1385 struct tracepoint
*tpoint
,
1386 struct traceframe
*tframe
,
1387 struct tracepoint_action
*taction
);
1389 #ifndef IN_PROCESS_AGENT
1390 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1392 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1393 static void download_tracepoint (struct tracepoint
*);
1394 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1395 static void clone_fast_tracepoint (struct tracepoint
*to
,
1396 const struct tracepoint
*from
);
1399 static LONGEST
get_timestamp (void);
1401 #if defined(__GNUC__)
1402 # define memory_barrier() asm volatile ("" : : : "memory")
1404 # define memory_barrier() do {} while (0)
1407 /* We only build the IPA if this builtin is supported, and there are
1408 no uses of this in GDBserver itself, so we're safe in defining this
1410 #define cmpxchg(mem, oldval, newval) \
1411 __sync_val_compare_and_swap (mem, oldval, newval)
1413 /* Record that an error occurred during expression evaluation. */
1416 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1417 enum eval_result_type rtype
)
1419 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1420 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1422 #ifdef IN_PROCESS_AGENT
1423 /* Only record the first error we get. */
1424 if (cmpxchg (&expr_eval_result
,
1426 rtype
) != expr_eval_no_error
)
1429 if (expr_eval_result
!= expr_eval_no_error
)
1433 error_tracepoint
= tpoint
;
1436 /* Trace buffer management. */
1439 clear_trace_buffer (void)
1441 trace_buffer_start
= trace_buffer_lo
;
1442 trace_buffer_free
= trace_buffer_lo
;
1443 trace_buffer_end_free
= trace_buffer_hi
;
1444 trace_buffer_wrap
= trace_buffer_hi
;
1445 /* A traceframe with zeroed fields marks the end of trace data. */
1446 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1447 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1448 traceframe_read_count
= traceframe_write_count
= 0;
1449 traceframes_created
= 0;
1452 #ifndef IN_PROCESS_AGENT
1455 clear_inferior_trace_buffer (void)
1457 CORE_ADDR ipa_trace_buffer_lo
;
1458 CORE_ADDR ipa_trace_buffer_hi
;
1459 struct traceframe ipa_traceframe
= { 0 };
1460 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1462 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1463 &ipa_trace_buffer_lo
);
1464 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1465 &ipa_trace_buffer_hi
);
1467 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1468 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1469 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1470 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1472 /* A traceframe with zeroed fields marks the end of trace data. */
1473 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1474 (unsigned char *) &ipa_trace_buffer_ctrl
,
1475 sizeof (ipa_trace_buffer_ctrl
));
1477 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1479 /* A traceframe with zeroed fields marks the end of trace data. */
1480 write_inferior_memory (ipa_trace_buffer_lo
,
1481 (unsigned char *) &ipa_traceframe
,
1482 sizeof (ipa_traceframe
));
1484 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1485 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1486 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1492 init_trace_buffer (LONGEST bufsize
)
1496 trace_buffer_size
= bufsize
;
1498 /* Make sure to internally allocate at least space for the EOB
1500 alloc_size
= (bufsize
< TRACEFRAME_EOB_MARKER_SIZE
1501 ? TRACEFRAME_EOB_MARKER_SIZE
: bufsize
);
1502 trace_buffer_lo
= xrealloc (trace_buffer_lo
, alloc_size
);
1504 trace_buffer_hi
= trace_buffer_lo
+ trace_buffer_size
;
1506 clear_trace_buffer ();
1509 #ifdef IN_PROCESS_AGENT
1511 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1512 about_to_request_buffer_space (void)
1514 /* GDBserver places breakpoint here while it goes about to flush
1515 data at random times. */
1516 UNKNOWN_SIDE_EFFECTS();
1521 /* Carve out a piece of the trace buffer, returning NULL in case of
1525 trace_buffer_alloc (size_t amt
)
1527 unsigned char *rslt
;
1528 struct trace_buffer_control
*tbctrl
;
1530 #ifdef IN_PROCESS_AGENT
1531 unsigned int prev
, prev_filtered
;
1532 unsigned int commit_count
;
1533 unsigned int commit
;
1534 unsigned int readout
;
1536 struct traceframe
*oldest
;
1537 unsigned char *new_start
;
1540 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1541 (long) amt
, (long) sizeof (struct traceframe
));
1543 /* Account for the EOB marker. */
1544 amt
+= TRACEFRAME_EOB_MARKER_SIZE
;
1546 #ifdef IN_PROCESS_AGENT
1550 /* Read the current token and extract the index to try to write to,
1551 storing it in CURR. */
1552 prev
= trace_buffer_ctrl_curr
;
1553 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1554 curr
= prev_filtered
+ 1;
1558 about_to_request_buffer_space ();
1560 /* Start out with a copy of the current state. GDBserver may be
1561 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1562 be able to commit anyway if that happens. */
1563 trace_buffer_ctrl
[curr
]
1564 = trace_buffer_ctrl
[prev_filtered
];
1565 trace_debug ("trying curr=%u", curr
);
1567 /* The GDBserver's agent doesn't need all that syncing, and always
1568 updates TCB 0 (there's only one, mind you). */
1571 tbctrl
= &trace_buffer_ctrl
[curr
];
1573 /* Offsets are easier to grok for debugging than raw addresses,
1574 especially for the small trace buffer sizes that are useful for
1576 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1578 (int) (tbctrl
->start
- trace_buffer_lo
),
1579 (int) (tbctrl
->free
- trace_buffer_lo
),
1580 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1581 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1582 (int) (trace_buffer_hi
- trace_buffer_lo
));
1584 /* The algorithm here is to keep trying to get a contiguous block of
1585 the requested size, possibly discarding older traceframes to free
1586 up space. Since free space might come in one or two pieces,
1587 depending on whether discarded traceframes wrapped around at the
1588 high end of the buffer, we test both pieces after each
1592 /* First, if we have two free parts, try the upper one first. */
1593 if (tbctrl
->end_free
< tbctrl
->free
)
1595 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1596 /* We have enough in the upper part. */
1600 /* Our high part of free space wasn't enough. Give up
1601 on it for now, set wraparound. We will recover the
1602 space later, if/when the wrapped-around traceframe is
1604 trace_debug ("Upper part too small, setting wraparound");
1605 tbctrl
->wrap
= tbctrl
->free
;
1606 tbctrl
->free
= trace_buffer_lo
;
1610 /* The normal case. */
1611 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1614 #ifdef IN_PROCESS_AGENT
1615 /* The IP Agent's buffer is always circular. It isn't used
1616 currently, but `circular_trace_buffer' could represent
1617 GDBserver's mode. If we didn't find space, ask GDBserver to
1620 flush_trace_buffer ();
1624 trace_debug ("gdbserver flushed buffer, retrying");
1628 /* GDBserver cancelled the tracing. Bail out as well. */
1631 /* If we're here, then neither part is big enough, and
1632 non-circular trace buffers are now full. */
1633 if (!circular_trace_buffer
)
1635 trace_debug ("Not enough space in the trace buffer");
1639 trace_debug ("Need more space in the trace buffer");
1641 /* If we have a circular buffer, we can try discarding the
1642 oldest traceframe and see if that helps. */
1643 oldest
= FIRST_TRACEFRAME ();
1644 if (oldest
->tpnum
== 0)
1646 /* Not good; we have no traceframes to free. Perhaps we're
1647 asking for a block that is larger than the buffer? In
1648 any case, give up. */
1649 trace_debug ("No traceframes to discard");
1653 /* We don't run this code in the in-process agent currently.
1654 E.g., we could leave the in-process agent in autonomous
1655 circular mode if we only have fast tracepoints. If we do
1656 that, then this bit becomes racy with GDBserver, which also
1657 writes to this counter. */
1658 --traceframe_write_count
;
1660 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1661 /* If we freed the traceframe that wrapped around, go back
1662 to the non-wrap case. */
1663 if (new_start
< tbctrl
->start
)
1665 trace_debug ("Discarding past the wraparound");
1666 tbctrl
->wrap
= trace_buffer_hi
;
1668 tbctrl
->start
= new_start
;
1669 tbctrl
->end_free
= tbctrl
->start
;
1671 trace_debug ("Discarded a traceframe\n"
1672 "Trace buffer [%d], start=%d free=%d "
1673 "endfree=%d wrap=%d hi=%d",
1675 (int) (tbctrl
->start
- trace_buffer_lo
),
1676 (int) (tbctrl
->free
- trace_buffer_lo
),
1677 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1678 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1679 (int) (trace_buffer_hi
- trace_buffer_lo
));
1681 /* Now go back around the loop. The discard might have resulted
1682 in either one or two pieces of free space, so we want to try
1683 both before freeing any more traceframes. */
1687 /* If we get here, we know we can provide the asked-for space. */
1689 rslt
= tbctrl
->free
;
1691 /* Adjust the request back down, now that we know we have space for
1692 the marker, but don't commit to AMT yet, we may still need to
1693 restart the operation if GDBserver touches the trace buffer
1694 (obviously only important in the in-process agent's version). */
1695 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1697 /* Or not. If GDBserver changed the trace buffer behind our back,
1698 we get to restart a new allocation attempt. */
1700 #ifdef IN_PROCESS_AGENT
1701 /* Build the tentative token. */
1702 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1703 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1704 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1708 /* Try to commit it. */
1709 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1710 if (readout
!= prev
)
1712 trace_debug ("GDBserver has touched the trace buffer, restarting."
1713 " (prev=%08x, commit=%08x, readout=%08x)",
1714 prev
, commit
, readout
);
1718 /* Hold your horses here. Even if that change was committed,
1719 GDBserver could come in, and clobber it. We need to hold to be
1720 able to tell if GDBserver clobbers before or after we committed
1721 the change. Whenever GDBserver goes about touching the IPA
1722 buffer, it sets a breakpoint in this routine, so we have a sync
1724 about_to_request_buffer_space ();
1726 /* Check if the change has been effective, even if GDBserver stopped
1727 us at the breakpoint. */
1730 unsigned int refetch
;
1734 refetch
= trace_buffer_ctrl_curr
;
1736 if (refetch
== commit
1737 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1740 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1741 "readout=%08x, refetch=%08x)",
1742 prev
, commit
, readout
, refetch
);
1746 trace_debug ("GDBserver has touched the trace buffer, not effective."
1747 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1748 prev
, commit
, readout
, refetch
);
1754 /* We have a new piece of the trace buffer. Hurray! */
1756 /* Add an EOB marker just past this allocation. */
1757 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1758 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1760 /* Adjust the request back down, now that we know we have space for
1762 amt
-= sizeof (struct traceframe
);
1766 trace_debug ("Allocated %d bytes", (int) amt
);
1767 trace_debug ("Trace buffer [%d] start=%d free=%d "
1768 "endfree=%d wrap=%d hi=%d",
1770 (int) (tbctrl
->start
- trace_buffer_lo
),
1771 (int) (tbctrl
->free
- trace_buffer_lo
),
1772 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1773 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1774 (int) (trace_buffer_hi
- trace_buffer_lo
));
1780 #ifndef IN_PROCESS_AGENT
1782 /* Return the total free space. This is not necessarily the largest
1783 block we can allocate, because of the two-part case. */
1788 if (trace_buffer_free
<= trace_buffer_end_free
)
1789 return trace_buffer_end_free
- trace_buffer_free
;
1791 return ((trace_buffer_end_free
- trace_buffer_lo
)
1792 + (trace_buffer_hi
- trace_buffer_free
));
1795 /* An 'S' in continuation packets indicates remainder are for
1798 static int seen_step_action_flag
;
1800 /* Create a tracepoint (location) with given number and address. Add this
1801 new tracepoint to list and sort this list. */
1803 static struct tracepoint
*
1804 add_tracepoint (int num
, CORE_ADDR addr
)
1806 struct tracepoint
*tpoint
, **tp_next
;
1808 tpoint
= xmalloc (sizeof (struct tracepoint
));
1809 tpoint
->number
= num
;
1810 tpoint
->address
= addr
;
1811 tpoint
->numactions
= 0;
1812 tpoint
->actions
= NULL
;
1813 tpoint
->actions_str
= NULL
;
1814 tpoint
->cond
= NULL
;
1815 tpoint
->num_step_actions
= 0;
1816 tpoint
->step_actions
= NULL
;
1817 tpoint
->step_actions_str
= NULL
;
1818 /* Start all off as regular (slow) tracepoints. */
1819 tpoint
->type
= trap_tracepoint
;
1820 tpoint
->orig_size
= -1;
1821 tpoint
->source_strings
= NULL
;
1822 tpoint
->compiled_cond
= 0;
1823 tpoint
->handle
= NULL
;
1824 tpoint
->next
= NULL
;
1826 /* Find a place to insert this tracepoint into list in order to keep
1827 the tracepoint list still in the ascending order. There may be
1828 multiple tracepoints at the same address as TPOINT's, and this
1829 guarantees TPOINT is inserted after all the tracepoints which are
1830 set at the same address. For example, fast tracepoints A, B, C are
1831 set at the same address, and D is to be insert at the same place as
1834 -->| A |--> | B |-->| C |->...
1836 One jump pad was created for tracepoint A, B, and C, and the target
1837 address of A is referenced/used in jump pad. So jump pad will let
1838 inferior jump to A. If D is inserted in front of A, like this,
1840 -->| D |-->| A |--> | B |-->| C |->...
1842 without updating jump pad, D is not reachable during collect, which
1843 is wrong. As we can see, the order of B, C and D doesn't matter, but
1844 A should always be the `first' one. */
1845 for (tp_next
= &tracepoints
;
1846 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1847 tp_next
= &(*tp_next
)->next
)
1849 tpoint
->next
= *tp_next
;
1851 last_tracepoint
= tpoint
;
1853 seen_step_action_flag
= 0;
1858 #ifndef IN_PROCESS_AGENT
1860 /* Return the tracepoint with the given number and address, or NULL. */
1862 static struct tracepoint
*
1863 find_tracepoint (int id
, CORE_ADDR addr
)
1865 struct tracepoint
*tpoint
;
1867 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1868 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1874 /* Remove TPOINT from global list. */
1877 remove_tracepoint (struct tracepoint
*tpoint
)
1879 struct tracepoint
*tp
, *tp_prev
;
1881 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1882 tp_prev
= tp
, tp
= tp
->next
)
1888 tp_prev
->next
= tp
->next
;
1890 tracepoints
= tp
->next
;
1896 /* There may be several tracepoints with the same number (because they
1897 are "locations", in GDB parlance); return the next one after the
1898 given tracepoint, or search from the beginning of the list if the
1899 first argument is NULL. */
1901 static struct tracepoint
*
1902 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1904 struct tracepoint
*tpoint
;
1907 tpoint
= prev_tp
->next
;
1909 tpoint
= tracepoints
;
1910 for (; tpoint
; tpoint
= tpoint
->next
)
1911 if (tpoint
->number
== num
)
1919 /* Append another action to perform when the tracepoint triggers. */
1922 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1928 seen_step_action_flag
= 1;
1936 char *act_start
= act
;
1937 struct tracepoint_action
*action
= NULL
;
1943 struct collect_memory_action
*maction
;
1947 maction
= xmalloc (sizeof *maction
);
1948 maction
->base
.type
= *act
;
1949 maction
->base
.ops
= &m_tracepoint_action_ops
;
1950 action
= &maction
->base
;
1953 is_neg
= (*act
== '-');
1956 act
= unpack_varlen_hex (act
, &basereg
);
1958 act
= unpack_varlen_hex (act
, &maction
->addr
);
1960 act
= unpack_varlen_hex (act
, &maction
->len
);
1961 maction
->basereg
= (is_neg
1964 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1965 pulongest (maction
->len
),
1966 paddress (maction
->addr
), maction
->basereg
);
1971 struct collect_registers_action
*raction
;
1973 raction
= xmalloc (sizeof *raction
);
1974 raction
->base
.type
= *act
;
1975 raction
->base
.ops
= &r_tracepoint_action_ops
;
1976 action
= &raction
->base
;
1978 trace_debug ("Want to collect registers");
1980 /* skip past hex digits of mask for now */
1981 while (isxdigit(*act
))
1987 struct collect_static_trace_data_action
*raction
;
1989 raction
= xmalloc (sizeof *raction
);
1990 raction
->base
.type
= *act
;
1991 raction
->base
.ops
= &l_tracepoint_action_ops
;
1992 action
= &raction
->base
;
1994 trace_debug ("Want to collect static trace data");
1999 trace_debug ("Unexpected step action, ignoring");
2004 struct eval_expr_action
*xaction
;
2006 xaction
= xmalloc (sizeof (*xaction
));
2007 xaction
->base
.type
= *act
;
2008 xaction
->base
.ops
= &x_tracepoint_action_ops
;
2009 action
= &xaction
->base
;
2011 trace_debug ("Want to evaluate expression");
2012 xaction
->expr
= gdb_parse_agent_expr (&act
);
2016 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2025 if (seen_step_action_flag
)
2027 tpoint
->num_step_actions
++;
2029 tpoint
->step_actions
2030 = xrealloc (tpoint
->step_actions
,
2031 (sizeof (*tpoint
->step_actions
)
2032 * tpoint
->num_step_actions
));
2033 tpoint
->step_actions_str
2034 = xrealloc (tpoint
->step_actions_str
,
2035 (sizeof (*tpoint
->step_actions_str
)
2036 * tpoint
->num_step_actions
));
2037 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2038 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2039 = savestring (act_start
, act
- act_start
);
2043 tpoint
->numactions
++;
2045 = xrealloc (tpoint
->actions
,
2046 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2048 = xrealloc (tpoint
->actions_str
,
2049 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2050 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2051 tpoint
->actions_str
[tpoint
->numactions
- 1]
2052 = savestring (act_start
, act
- act_start
);
2059 /* Find or create a trace state variable with the given number. */
2061 static struct trace_state_variable
*
2062 get_trace_state_variable (int num
)
2064 struct trace_state_variable
*tsv
;
2066 #ifdef IN_PROCESS_AGENT
2067 /* Search for an existing variable. */
2068 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2069 if (tsv
->number
== num
)
2073 /* Search for an existing variable. */
2074 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2075 if (tsv
->number
== num
)
2081 /* Find or create a trace state variable with the given number. */
2083 static struct trace_state_variable
*
2084 create_trace_state_variable (int num
, int gdb
)
2086 struct trace_state_variable
*tsv
;
2088 tsv
= get_trace_state_variable (num
);
2092 /* Create a new variable. */
2093 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2095 tsv
->initial_value
= 0;
2099 #ifdef IN_PROCESS_AGENT
2102 tsv
->next
= alloced_trace_state_variables
;
2103 alloced_trace_state_variables
= tsv
;
2108 tsv
->next
= trace_state_variables
;
2109 trace_state_variables
= tsv
;
2114 IP_AGENT_EXPORT LONGEST
2115 get_trace_state_variable_value (int num
)
2117 struct trace_state_variable
*tsv
;
2119 tsv
= get_trace_state_variable (num
);
2123 trace_debug ("No trace state variable %d, skipping value get", num
);
2127 /* Call a getter function if we have one. While it's tempting to
2128 set up something to only call the getter once per tracepoint hit,
2129 it could run afoul of thread races. Better to let the getter
2130 handle it directly, if necessary to worry about it. */
2132 tsv
->value
= (tsv
->getter
) ();
2134 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2135 num
, plongest (tsv
->value
));
2140 IP_AGENT_EXPORT
void
2141 set_trace_state_variable_value (int num
, LONGEST val
)
2143 struct trace_state_variable
*tsv
;
2145 tsv
= get_trace_state_variable (num
);
2149 trace_debug ("No trace state variable %d, skipping value set", num
);
2157 agent_get_trace_state_variable_value (int num
)
2159 return get_trace_state_variable_value (num
);
2163 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2165 set_trace_state_variable_value (num
, val
);
2169 set_trace_state_variable_name (int num
, const char *name
)
2171 struct trace_state_variable
*tsv
;
2173 tsv
= get_trace_state_variable (num
);
2177 trace_debug ("No trace state variable %d, skipping name set", num
);
2181 tsv
->name
= (char *) name
;
2185 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2187 struct trace_state_variable
*tsv
;
2189 tsv
= get_trace_state_variable (num
);
2193 trace_debug ("No trace state variable %d, skipping getter set", num
);
2197 tsv
->getter
= getter
;
2200 /* Add a raw traceframe for the given tracepoint. */
2202 static struct traceframe
*
2203 add_traceframe (struct tracepoint
*tpoint
)
2205 struct traceframe
*tframe
;
2207 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2212 tframe
->tpnum
= tpoint
->number
;
2213 tframe
->data_size
= 0;
2218 /* Add a block to the traceframe currently being worked on. */
2220 static unsigned char *
2221 add_traceframe_block (struct traceframe
*tframe
,
2222 struct tracepoint
*tpoint
, int amt
)
2224 unsigned char *block
;
2229 block
= trace_buffer_alloc (amt
);
2234 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2236 tframe
->data_size
+= amt
;
2237 tpoint
->traceframe_usage
+= amt
;
2242 /* Flag that the current traceframe is finished. */
2245 finish_traceframe (struct traceframe
*tframe
)
2247 ++traceframe_write_count
;
2248 ++traceframes_created
;
2251 #ifndef IN_PROCESS_AGENT
2253 /* Given a traceframe number NUM, find the NUMth traceframe in the
2256 static struct traceframe
*
2257 find_traceframe (int num
)
2259 struct traceframe
*tframe
;
2262 for (tframe
= FIRST_TRACEFRAME ();
2264 tframe
= NEXT_TRACEFRAME (tframe
))
2275 get_traceframe_address (struct traceframe
*tframe
)
2278 struct tracepoint
*tpoint
;
2280 addr
= traceframe_get_pc (tframe
);
2285 /* Fallback strategy, will be incorrect for while-stepping frames
2286 and multi-location tracepoints. */
2287 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2288 return tpoint
->address
;
2291 /* Search for the next traceframe whose address is inside or outside
2294 static struct traceframe
*
2295 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2298 struct traceframe
*tframe
;
2301 *tfnump
= current_traceframe
+ 1;
2302 tframe
= find_traceframe (*tfnump
);
2303 /* The search is not supposed to wrap around. */
2310 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2312 tfaddr
= get_traceframe_address (tframe
);
2314 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2315 : (lo
> tfaddr
|| tfaddr
> hi
))
2324 /* Search for the next traceframe recorded by the given tracepoint.
2325 Note that for multi-location tracepoints, this will find whatever
2326 location appears first. */
2328 static struct traceframe
*
2329 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2331 struct traceframe
*tframe
;
2333 *tfnump
= current_traceframe
+ 1;
2334 tframe
= find_traceframe (*tfnump
);
2335 /* The search is not supposed to wrap around. */
2342 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2344 if (tframe
->tpnum
== num
)
2355 #ifndef IN_PROCESS_AGENT
2357 /* Clear all past trace state. */
2360 cmd_qtinit (char *packet
)
2362 struct trace_state_variable
*tsv
, *prev
, *next
;
2364 /* Make sure we don't try to read from a trace frame. */
2365 current_traceframe
= -1;
2369 trace_debug ("Initializing the trace");
2371 clear_installed_tracepoints ();
2372 clear_readonly_regions ();
2375 last_tracepoint
= NULL
;
2377 /* Clear out any leftover trace state variables. Ones with target
2378 defined getters should be kept however. */
2380 tsv
= trace_state_variables
;
2383 trace_debug ("Looking at var %d", tsv
->number
);
2384 if (tsv
->getter
== NULL
)
2390 trace_state_variables
= next
;
2391 trace_debug ("Deleting var %d", tsv
->number
);
2402 clear_trace_buffer ();
2403 clear_inferior_trace_buffer ();
2408 /* Unprobe the UST marker at ADDRESS. */
2411 unprobe_marker_at (CORE_ADDR address
)
2413 char cmd
[IPA_CMD_BUF_SIZE
];
2415 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2416 run_inferior_command (cmd
, strlen (cmd
) + 1);
2419 /* Restore the program to its pre-tracing state. This routine may be called
2420 in error situations, so it needs to be careful about only restoring
2421 from known-valid bits. */
2424 clear_installed_tracepoints (void)
2426 struct tracepoint
*tpoint
;
2427 struct tracepoint
*prev_stpoint
;
2430 cancel_breakpoints ();
2432 prev_stpoint
= NULL
;
2434 /* Restore any bytes overwritten by tracepoints. */
2435 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2437 /* Catch the case where we might try to remove a tracepoint that
2438 was never actually installed. */
2439 if (tpoint
->handle
== NULL
)
2441 trace_debug ("Tracepoint %d at 0x%s was "
2442 "never installed, nothing to clear",
2443 tpoint
->number
, paddress (tpoint
->address
));
2447 switch (tpoint
->type
)
2449 case trap_tracepoint
:
2450 delete_breakpoint (tpoint
->handle
);
2452 case fast_tracepoint
:
2453 delete_fast_tracepoint_jump (tpoint
->handle
);
2455 case static_tracepoint
:
2456 if (prev_stpoint
!= NULL
2457 && prev_stpoint
->address
== tpoint
->address
)
2458 /* Nothing to do. We already unprobed a tracepoint set at
2459 this marker address (and there can only be one probe
2464 unprobe_marker_at (tpoint
->address
);
2465 prev_stpoint
= tpoint
;
2470 tpoint
->handle
= NULL
;
2476 /* Parse a packet that defines a tracepoint. */
2479 cmd_qtdp (char *own_buf
)
2482 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2483 int trail_hyphen
= 0;
2487 struct tracepoint
*tpoint
;
2489 char *packet
= own_buf
;
2491 packet
+= strlen ("QTDP:");
2493 /* A hyphen at the beginning marks a packet specifying actions for a
2494 tracepoint already supplied. */
2501 packet
= unpack_varlen_hex (packet
, &num
);
2502 ++packet
; /* skip a colon */
2503 packet
= unpack_varlen_hex (packet
, &addr
);
2504 ++packet
; /* skip a colon */
2506 /* See if we already have this tracepoint. */
2507 tpoint
= find_tracepoint (num
, addr
);
2511 /* Duplicate tracepoints are never allowed. */
2514 trace_debug ("Tracepoint error: tracepoint %d"
2515 " at 0x%s already exists",
2516 (int) num
, paddress (addr
));
2517 write_enn (own_buf
);
2521 tpoint
= add_tracepoint (num
, addr
);
2523 tpoint
->enabled
= (*packet
== 'E');
2524 ++packet
; /* skip 'E' */
2525 ++packet
; /* skip a colon */
2526 packet
= unpack_varlen_hex (packet
, &count
);
2527 tpoint
->step_count
= count
;
2528 ++packet
; /* skip a colon */
2529 packet
= unpack_varlen_hex (packet
, &count
);
2530 tpoint
->pass_count
= count
;
2531 /* See if we have any of the additional optional fields. */
2532 while (*packet
== ':')
2537 tpoint
->type
= fast_tracepoint
;
2539 packet
= unpack_varlen_hex (packet
, &count
);
2540 tpoint
->orig_size
= count
;
2542 else if (*packet
== 'S')
2544 tpoint
->type
= static_tracepoint
;
2547 else if (*packet
== 'X')
2549 actparm
= (char *) packet
;
2550 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2553 else if (*packet
== '-')
2555 else if (*packet
== '\0')
2558 trace_debug ("Unknown optional tracepoint field");
2563 trace_debug ("Also has actions\n");
2566 trace_debug ("Defined %stracepoint %d at 0x%s, "
2567 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2568 tpoint
->type
== fast_tracepoint
? "fast "
2569 : tpoint
->type
== static_tracepoint
? "static " : "",
2570 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2571 tpoint
->step_count
, tpoint
->pass_count
);
2574 add_tracepoint_action (tpoint
, packet
);
2577 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2578 (int) num
, paddress (addr
));
2579 write_enn (own_buf
);
2583 /* Install tracepoint during tracing only once for each tracepoint location.
2584 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2585 determine the last QTDP packet for one tracepoint location by checking
2586 trailing hyphen in QTDP packet. */
2587 if (tracing
&& !trail_hyphen
)
2589 struct tracepoint
*tp
= NULL
;
2591 /* Pause all threads temporarily while we patch tracepoints. */
2594 /* download_tracepoint will update global `tracepoints'
2595 list, so it is unsafe to leave threads in jump pad. */
2596 stabilize_threads ();
2598 /* Freeze threads. */
2602 if (tpoint
->type
!= trap_tracepoint
)
2604 /* Find another fast or static tracepoint at the same address. */
2605 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2607 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2608 && tp
->number
!= tpoint
->number
)
2612 /* TPOINT is installed at the same address as TP. */
2615 if (tpoint
->type
== fast_tracepoint
)
2616 clone_fast_tracepoint (tpoint
, tp
);
2617 else if (tpoint
->type
== static_tracepoint
)
2618 tpoint
->handle
= (void *) -1;
2622 if (use_agent
&& tpoint
->type
== fast_tracepoint
2623 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2625 /* Download and install fast tracepoint by agent. */
2626 if (tracepoint_send_agent (tpoint
) == 0)
2630 write_enn (own_buf
);
2631 remove_tracepoint (tpoint
);
2636 download_tracepoint (tpoint
);
2638 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2640 install_tracepoint (tpoint
, own_buf
);
2641 if (strcmp (own_buf
, "OK") != 0)
2642 remove_tracepoint (tpoint
);
2656 cmd_qtdpsrc (char *own_buf
)
2658 ULONGEST num
, addr
, start
, slen
;
2659 struct tracepoint
*tpoint
;
2660 char *packet
= own_buf
;
2661 char *saved
, *srctype
, *src
;
2663 struct source_string
*last
, *newlast
;
2665 packet
+= strlen ("QTDPsrc:");
2667 packet
= unpack_varlen_hex (packet
, &num
);
2668 ++packet
; /* skip a colon */
2669 packet
= unpack_varlen_hex (packet
, &addr
);
2670 ++packet
; /* skip a colon */
2672 /* See if we already have this tracepoint. */
2673 tpoint
= find_tracepoint (num
, addr
);
2677 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2678 (int) num
, paddress (addr
));
2679 write_enn (own_buf
);
2684 packet
= strchr (packet
, ':');
2685 srctype
= xmalloc (packet
- saved
+ 1);
2686 memcpy (srctype
, saved
, packet
- saved
);
2687 srctype
[packet
- saved
] = '\0';
2689 packet
= unpack_varlen_hex (packet
, &start
);
2690 ++packet
; /* skip a colon */
2691 packet
= unpack_varlen_hex (packet
, &slen
);
2692 ++packet
; /* skip a colon */
2693 src
= xmalloc (slen
+ 1);
2694 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2697 newlast
= xmalloc (sizeof (struct source_string
));
2698 newlast
->type
= srctype
;
2700 newlast
->next
= NULL
;
2701 /* Always add a source string to the end of the list;
2702 this keeps sequences of actions/commands in the right
2704 if (tpoint
->source_strings
)
2706 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2708 last
->next
= newlast
;
2711 tpoint
->source_strings
= newlast
;
2717 cmd_qtdv (char *own_buf
)
2719 ULONGEST num
, val
, builtin
;
2722 struct trace_state_variable
*tsv
;
2723 char *packet
= own_buf
;
2725 packet
+= strlen ("QTDV:");
2727 packet
= unpack_varlen_hex (packet
, &num
);
2728 ++packet
; /* skip a colon */
2729 packet
= unpack_varlen_hex (packet
, &val
);
2730 ++packet
; /* skip a colon */
2731 packet
= unpack_varlen_hex (packet
, &builtin
);
2732 ++packet
; /* skip a colon */
2734 nbytes
= strlen (packet
) / 2;
2735 varname
= xmalloc (nbytes
+ 1);
2736 nbytes
= unhexify (varname
, packet
, nbytes
);
2737 varname
[nbytes
] = '\0';
2739 tsv
= create_trace_state_variable (num
, 1);
2740 tsv
->initial_value
= (LONGEST
) val
;
2741 tsv
->name
= varname
;
2743 set_trace_state_variable_value (num
, (LONGEST
) val
);
2749 cmd_qtenable_disable (char *own_buf
, int enable
)
2751 char *packet
= own_buf
;
2753 struct tracepoint
*tp
;
2755 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2756 packet
= unpack_varlen_hex (packet
, &num
);
2757 ++packet
; /* skip a colon */
2758 packet
= unpack_varlen_hex (packet
, &addr
);
2760 tp
= find_tracepoint (num
, addr
);
2764 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2766 trace_debug ("Tracepoint %d at 0x%s is already %s",
2767 (int) num
, paddress (addr
),
2768 enable
? "enabled" : "disabled");
2773 trace_debug ("%s tracepoint %d at 0x%s",
2774 enable
? "Enabling" : "Disabling",
2775 (int) num
, paddress (addr
));
2777 tp
->enabled
= enable
;
2779 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2782 int offset
= offsetof (struct tracepoint
, enabled
);
2783 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2785 ret
= prepare_to_access_memory ();
2788 trace_debug ("Failed to temporarily stop inferior threads");
2789 write_enn (own_buf
);
2793 ret
= write_inferior_integer (obj_addr
, enable
);
2794 done_accessing_memory ();
2798 trace_debug ("Cannot write enabled flag into "
2799 "inferior process memory");
2800 write_enn (own_buf
);
2809 trace_debug ("Tracepoint %d at 0x%s not found",
2810 (int) num
, paddress (addr
));
2811 write_enn (own_buf
);
2816 cmd_qtv (char *own_buf
)
2821 char *packet
= own_buf
;
2823 packet
+= strlen ("qTV:");
2824 unpack_varlen_hex (packet
, &num
);
2826 if (current_traceframe
>= 0)
2828 err
= traceframe_read_tsv ((int) num
, &val
);
2831 strcpy (own_buf
, "U");
2835 /* Only make tsv's be undefined before the first trace run. After a
2836 trace run is over, the user might want to see the last value of
2837 the tsv, and it might not be available in a traceframe. */
2838 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2840 strcpy (own_buf
, "U");
2844 val
= get_trace_state_variable_value (num
);
2846 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2849 /* Clear out the list of readonly regions. */
2852 clear_readonly_regions (void)
2854 struct readonly_region
*roreg
;
2856 while (readonly_regions
)
2858 roreg
= readonly_regions
;
2859 readonly_regions
= readonly_regions
->next
;
2864 /* Parse the collection of address ranges whose contents GDB believes
2865 to be unchanging and so can be read directly from target memory
2866 even while looking at a traceframe. */
2869 cmd_qtro (char *own_buf
)
2871 ULONGEST start
, end
;
2872 struct readonly_region
*roreg
;
2873 char *packet
= own_buf
;
2875 trace_debug ("Want to mark readonly regions");
2877 clear_readonly_regions ();
2879 packet
+= strlen ("QTro");
2881 while (*packet
== ':')
2883 ++packet
; /* skip a colon */
2884 packet
= unpack_varlen_hex (packet
, &start
);
2885 ++packet
; /* skip a comma */
2886 packet
= unpack_varlen_hex (packet
, &end
);
2887 roreg
= xmalloc (sizeof (struct readonly_region
));
2888 roreg
->start
= start
;
2890 roreg
->next
= readonly_regions
;
2891 readonly_regions
= roreg
;
2892 trace_debug ("Added readonly region from 0x%s to 0x%s",
2893 paddress (roreg
->start
), paddress (roreg
->end
));
2899 /* Test to see if the given range is in our list of readonly ranges.
2900 We only test for being entirely within a range, GDB is not going to
2901 send a single memory packet that spans multiple regions. */
2904 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2906 struct readonly_region
*roreg
;
2908 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2909 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2915 /* The maximum size of a jump pad entry. */
2916 static const int max_jump_pad_size
= 0x100;
2918 static CORE_ADDR gdb_jump_pad_head
;
2920 /* Return the address of the next free jump space. */
2923 get_jump_space_head (void)
2925 if (gdb_jump_pad_head
== 0)
2927 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2928 &gdb_jump_pad_head
))
2929 fatal ("error extracting jump_pad_buffer");
2932 return gdb_jump_pad_head
;
2935 /* Reserve USED bytes from the jump space. */
2938 claim_jump_space (ULONGEST used
)
2940 trace_debug ("claim_jump_space reserves %s bytes at %s",
2941 pulongest (used
), paddress (gdb_jump_pad_head
));
2942 gdb_jump_pad_head
+= used
;
2945 static CORE_ADDR trampoline_buffer_head
= 0;
2946 static CORE_ADDR trampoline_buffer_tail
;
2948 /* Reserve USED bytes from the trampoline buffer and return the
2949 address of the start of the reserved space in TRAMPOLINE. Returns
2950 non-zero if the space is successfully claimed. */
2953 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2955 if (!trampoline_buffer_head
)
2957 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2958 &trampoline_buffer_tail
))
2960 fatal ("error extracting trampoline_buffer");
2964 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2965 &trampoline_buffer_head
))
2967 fatal ("error extracting trampoline_buffer_end");
2972 /* Start claiming space from the top of the trampoline space. If
2973 the space is located at the bottom of the virtual address space,
2974 this reduces the possibility that corruption will occur if a null
2975 pointer is used to write to memory. */
2976 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2978 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2983 trampoline_buffer_head
-= used
;
2985 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2986 pulongest (used
), paddress (trampoline_buffer_head
));
2988 *trampoline
= trampoline_buffer_head
;
2992 /* Returns non-zero if there is space allocated for use in trampolines
2993 for fast tracepoints. */
2996 have_fast_tracepoint_trampoline_buffer (char *buf
)
2998 CORE_ADDR trampoline_end
, errbuf
;
3000 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
3003 fatal ("error extracting trampoline_buffer_end");
3010 strcpy (buf
, "was claiming");
3011 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3014 fatal ("error extracting errbuf");
3018 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3021 return trampoline_end
!= 0;
3024 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3025 the command fails, or 0 otherwise. If the command ran
3026 successfully, but probing the marker failed, ERROUT will be filled
3027 with the error to reply to GDB, and -1 is also returned. This
3028 allows directly passing IPA errors to GDB. */
3031 probe_marker_at (CORE_ADDR address
, char *errout
)
3033 char cmd
[IPA_CMD_BUF_SIZE
];
3036 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3037 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3043 strcpy (errout
, cmd
);
3052 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3054 to
->jump_pad
= from
->jump_pad
;
3055 to
->jump_pad_end
= from
->jump_pad_end
;
3056 to
->trampoline
= from
->trampoline
;
3057 to
->trampoline_end
= from
->trampoline_end
;
3058 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3059 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3060 to
->handle
= from
->handle
;
3062 gdb_assert (from
->handle
);
3063 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3066 #define MAX_JUMP_SIZE 20
3068 /* Install fast tracepoint. Return 0 if successful, otherwise return
3072 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3074 CORE_ADDR jentry
, jump_entry
;
3075 CORE_ADDR trampoline
;
3076 ULONGEST trampoline_size
;
3078 /* The jump to the jump pad of the last fast tracepoint
3080 unsigned char fjump
[MAX_JUMP_SIZE
];
3081 ULONGEST fjump_size
;
3083 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3085 trace_debug ("Requested a fast tracepoint on an instruction "
3086 "that is of less than the minimum length.");
3090 jentry
= jump_entry
= get_jump_space_head ();
3093 trampoline_size
= 0;
3095 /* Install the jump pad. */
3096 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3098 ipa_sym_addrs
.addr_gdb_collect
,
3099 ipa_sym_addrs
.addr_collecting
,
3102 &trampoline
, &trampoline_size
,
3104 &tpoint
->adjusted_insn_addr
,
3105 &tpoint
->adjusted_insn_addr_end
,
3112 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3115 if (tpoint
->handle
!= NULL
)
3117 tpoint
->jump_pad
= jump_entry
;
3118 tpoint
->jump_pad_end
= jentry
;
3119 tpoint
->trampoline
= trampoline
;
3120 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3122 /* Pad to 8-byte alignment. */
3123 jentry
= ((jentry
+ 7) & ~0x7);
3124 claim_jump_space (jentry
- jump_entry
);
3131 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3134 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3136 tpoint
->handle
= NULL
;
3139 if (tpoint
->type
== trap_tracepoint
)
3141 /* Tracepoints are installed as memory breakpoints. Just go
3142 ahead and install the trap. The breakpoints module
3143 handles duplicated breakpoints, and the memory read
3144 routine handles un-patching traps from memory reads. */
3145 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3146 tracepoint_handler
);
3148 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3150 if (!agent_loaded_p ())
3152 trace_debug ("Requested a %s tracepoint, but fast "
3153 "tracepoints aren't supported.",
3154 tpoint
->type
== static_tracepoint
? "static" : "fast");
3155 write_e_ipa_not_loaded (own_buf
);
3158 if (tpoint
->type
== static_tracepoint
3159 && !in_process_agent_supports_ust ())
3161 trace_debug ("Requested a static tracepoint, but static "
3162 "tracepoints are not supported.");
3163 write_e_ust_not_loaded (own_buf
);
3167 if (tpoint
->type
== fast_tracepoint
)
3168 install_fast_tracepoint (tpoint
, own_buf
);
3171 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3172 tpoint
->handle
= (void *) -1;
3177 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3179 if (tpoint
->handle
== NULL
)
3181 if (*own_buf
== '\0')
3182 write_enn (own_buf
);
3188 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3191 cmd_qtstart (char *packet
)
3193 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3194 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3196 trace_debug ("Starting the trace");
3198 /* Pause all threads temporarily while we patch tracepoints. */
3201 /* Get threads out of jump pads. Safe to do here, since this is a
3202 top level command. And, required to do here, since we're
3203 deleting/rewriting jump pads. */
3205 stabilize_threads ();
3207 /* Freeze threads. */
3210 /* Sync the fast tracepoints list in the inferior ftlib. */
3211 if (agent_loaded_p ())
3212 download_trace_state_variables ();
3214 /* No previous fast tpoint yet. */
3215 prev_ftpoint
= NULL
;
3217 /* No previous static tpoint yet. */
3218 prev_stpoint
= NULL
;
3222 /* Start out empty. */
3223 if (agent_loaded_p ())
3224 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3226 /* Download and install tracepoints. */
3227 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3229 /* Ensure all the hit counts start at zero. */
3230 tpoint
->hit_count
= 0;
3231 tpoint
->traceframe_usage
= 0;
3233 if (tpoint
->type
== trap_tracepoint
)
3235 /* Tracepoints are installed as memory breakpoints. Just go
3236 ahead and install the trap. The breakpoints module
3237 handles duplicated breakpoints, and the memory read
3238 routine handles un-patching traps from memory reads. */
3239 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3240 tracepoint_handler
);
3242 else if (tpoint
->type
== fast_tracepoint
3243 || tpoint
->type
== static_tracepoint
)
3245 if (maybe_write_ipa_not_loaded (packet
))
3247 trace_debug ("Requested a %s tracepoint, but fast "
3248 "tracepoints aren't supported.",
3249 tpoint
->type
== static_tracepoint
3250 ? "static" : "fast");
3254 if (tpoint
->type
== fast_tracepoint
)
3257 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3259 if (prev_ftpoint
!= NULL
3260 && prev_ftpoint
->address
== tpoint
->address
)
3263 tracepoint_send_agent (tpoint
);
3265 download_tracepoint_1 (tpoint
);
3267 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3271 /* Tracepoint is installed successfully? */
3274 /* Download and install fast tracepoint by agent. */
3276 installed
= !tracepoint_send_agent (tpoint
);
3279 download_tracepoint_1 (tpoint
);
3280 installed
= !install_fast_tracepoint (tpoint
, packet
);
3284 prev_ftpoint
= tpoint
;
3289 if (!in_process_agent_supports_ust ())
3291 trace_debug ("Requested a static tracepoint, but static "
3292 "tracepoints are not supported.");
3296 download_tracepoint_1 (tpoint
);
3297 /* Can only probe a given marker once. */
3298 if (prev_stpoint
!= NULL
3299 && prev_stpoint
->address
== tpoint
->address
)
3300 tpoint
->handle
= (void *) -1;
3303 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3305 tpoint
->handle
= (void *) -1;
3307 /* So that we can handle multiple static tracepoints
3308 at the same address easily. */
3309 prev_stpoint
= tpoint
;
3315 tpptr
= tpoint
->obj_addr_on_target
;
3317 if (tpoint
== tracepoints
)
3318 /* First object in list, set the head pointer in the
3320 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3322 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3327 /* Any failure in the inner loop is sufficient cause to give
3329 if (tpoint
->handle
== NULL
)
3333 /* Any error in tracepoint insertion is unacceptable; better to
3334 address the problem now, than end up with a useless or misleading
3338 clear_installed_tracepoints ();
3339 if (*packet
== '\0')
3345 stopping_tracepoint
= NULL
;
3346 trace_buffer_is_full
= 0;
3347 expr_eval_result
= expr_eval_no_error
;
3348 error_tracepoint
= NULL
;
3349 tracing_start_time
= get_timestamp ();
3351 /* Tracing is now active, hits will now start being logged. */
3354 if (agent_loaded_p ())
3356 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3357 fatal ("Error setting tracing variable in lib");
3359 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3361 fatal ("Error clearing stopping_tracepoint variable in lib");
3363 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3364 fatal ("Error clearing trace_buffer_is_full variable in lib");
3366 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3367 stop_tracing_handler
);
3368 if (stop_tracing_bkpt
== NULL
)
3369 error ("Error setting stop_tracing breakpoint");
3371 flush_trace_buffer_bkpt
3372 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3373 flush_trace_buffer_handler
);
3374 if (flush_trace_buffer_bkpt
== NULL
)
3375 error ("Error setting flush_trace_buffer breakpoint");
3383 /* End a tracing run, filling in a stop reason to report back to GDB,
3384 and removing the tracepoints from the code. */
3391 trace_debug ("Tracing is already off, ignoring");
3395 trace_debug ("Stopping the trace");
3397 /* Pause all threads before removing fast jumps from memory,
3398 breakpoints, and touching IPA state variables (inferior memory).
3399 Some thread may hit the internal tracing breakpoints, or be
3400 collecting this moment, but that's ok, we don't release the
3401 tpoint object's memory or the jump pads here (we only do that
3402 when we're sure we can move all threads out of the jump pads).
3403 We can't now, since we may be getting here due to the inferior
3404 agent calling us. */
3406 /* Since we're removing breakpoints, cancel breakpoint hits,
3407 possibly related to the breakpoints we're about to delete. */
3408 cancel_breakpoints ();
3410 /* Stop logging. Tracepoints can still be hit, but they will not be
3413 if (agent_loaded_p ())
3415 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3416 fatal ("Error clearing tracing variable in lib");
3419 tracing_stop_time
= get_timestamp ();
3420 tracing_stop_reason
= "t???";
3421 tracing_stop_tpnum
= 0;
3422 if (stopping_tracepoint
)
3424 trace_debug ("Stopping the trace because "
3425 "tracepoint %d was hit %" PRIu64
" times",
3426 stopping_tracepoint
->number
,
3427 stopping_tracepoint
->pass_count
);
3428 tracing_stop_reason
= "tpasscount";
3429 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3431 else if (trace_buffer_is_full
)
3433 trace_debug ("Stopping the trace because the trace buffer is full");
3434 tracing_stop_reason
= "tfull";
3436 else if (expr_eval_result
!= expr_eval_no_error
)
3438 trace_debug ("Stopping the trace because of an expression eval error");
3439 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3440 tracing_stop_tpnum
= error_tracepoint
->number
;
3442 #ifndef IN_PROCESS_AGENT
3443 else if (!gdb_connected ())
3445 trace_debug ("Stopping the trace because GDB disconnected");
3446 tracing_stop_reason
= "tdisconnected";
3451 trace_debug ("Stopping the trace because of a tstop command");
3452 tracing_stop_reason
= "tstop";
3455 stopping_tracepoint
= NULL
;
3456 error_tracepoint
= NULL
;
3458 /* Clear out the tracepoints. */
3459 clear_installed_tracepoints ();
3461 if (agent_loaded_p ())
3463 /* Pull in fast tracepoint trace frames from the inferior lib
3464 buffer into our buffer, even if our buffer is already full,
3465 because we want to present the full number of created frames
3466 in addition to what fit in the trace buffer. */
3467 upload_fast_traceframes ();
3470 if (stop_tracing_bkpt
!= NULL
)
3472 delete_breakpoint (stop_tracing_bkpt
);
3473 stop_tracing_bkpt
= NULL
;
3476 if (flush_trace_buffer_bkpt
!= NULL
)
3478 delete_breakpoint (flush_trace_buffer_bkpt
);
3479 flush_trace_buffer_bkpt
= NULL
;
3486 stop_tracing_handler (CORE_ADDR addr
)
3488 trace_debug ("lib hit stop_tracing");
3490 /* Don't actually handle it here. When we stop tracing we remove
3491 breakpoints from the inferior, and that is not allowed in a
3492 breakpoint handler (as the caller is walking the breakpoint
3498 flush_trace_buffer_handler (CORE_ADDR addr
)
3500 trace_debug ("lib hit flush_trace_buffer");
3505 cmd_qtstop (char *packet
)
3512 cmd_qtdisconnected (char *own_buf
)
3515 char *packet
= own_buf
;
3517 packet
+= strlen ("QTDisconnected:");
3519 unpack_varlen_hex (packet
, &setting
);
3523 disconnected_tracing
= setting
;
3527 cmd_qtframe (char *own_buf
)
3529 ULONGEST frame
, pc
, lo
, hi
, num
;
3531 struct traceframe
*tframe
;
3532 char *packet
= own_buf
;
3534 packet
+= strlen ("QTFrame:");
3536 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3538 packet
+= strlen ("pc:");
3539 unpack_varlen_hex (packet
, &pc
);
3540 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3541 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3543 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3545 packet
+= strlen ("range:");
3546 packet
= unpack_varlen_hex (packet
, &lo
);
3548 unpack_varlen_hex (packet
, &hi
);
3549 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3550 paddress (lo
), paddress (hi
));
3551 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3553 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3555 packet
+= strlen ("outside:");
3556 packet
= unpack_varlen_hex (packet
, &lo
);
3558 unpack_varlen_hex (packet
, &hi
);
3559 trace_debug ("Want to find next traceframe "
3560 "outside the range 0x%s to 0x%s",
3561 paddress (lo
), paddress (hi
));
3562 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3564 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3566 packet
+= strlen ("tdp:");
3567 unpack_varlen_hex (packet
, &num
);
3569 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3570 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3574 unpack_varlen_hex (packet
, &frame
);
3575 tfnum
= (int) frame
;
3578 trace_debug ("Want to stop looking at traceframes");
3579 current_traceframe
= -1;
3583 trace_debug ("Want to look at traceframe %d", tfnum
);
3584 tframe
= find_traceframe (tfnum
);
3589 current_traceframe
= tfnum
;
3590 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3593 sprintf (own_buf
, "F-1");
3597 cmd_qtstatus (char *packet
)
3599 char *stop_reason_rsp
= NULL
;
3600 char *buf1
, *buf2
, *buf3
, *str
;
3603 /* Translate the plain text of the notes back into hex for
3606 str
= (tracing_user_name
? tracing_user_name
: "");
3607 slen
= strlen (str
);
3608 buf1
= (char *) alloca (slen
* 2 + 1);
3609 hexify (buf1
, str
, slen
);
3611 str
= (tracing_notes
? tracing_notes
: "");
3612 slen
= strlen (str
);
3613 buf2
= (char *) alloca (slen
* 2 + 1);
3614 hexify (buf2
, str
, slen
);
3616 str
= (tracing_stop_note
? tracing_stop_note
: "");
3617 slen
= strlen (str
);
3618 buf3
= (char *) alloca (slen
* 2 + 1);
3619 hexify (buf3
, str
, slen
);
3621 trace_debug ("Returning trace status as %d, stop reason %s",
3622 tracing
, tracing_stop_reason
);
3624 if (agent_loaded_p ())
3628 upload_fast_traceframes ();
3633 stop_reason_rsp
= (char *) tracing_stop_reason
;
3635 /* The user visible error string in terror needs to be hex encoded.
3636 We leave it as plain string in `tracing_stop_reason' to ease
3638 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3640 const char *result_name
;
3644 result_name
= stop_reason_rsp
+ strlen ("terror:");
3645 hexstr_len
= strlen (result_name
) * 2;
3646 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3647 strcpy (p
, "terror:");
3649 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3652 /* If this was a forced stop, include any stop note that was supplied. */
3653 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3655 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3656 strcpy (stop_reason_rsp
, "tstop:");
3657 strcat (stop_reason_rsp
, buf3
);
3663 "tframes:%x;tcreated:%x;"
3664 "tfree:%x;tsize:%s;"
3667 "starttime:%s;stoptime:%s;"
3668 "username:%s;notes:%s:",
3670 stop_reason_rsp
, tracing_stop_tpnum
,
3671 traceframe_count
, traceframes_created
,
3672 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3673 circular_trace_buffer
,
3674 disconnected_tracing
,
3675 phex_nz (tracing_start_time
, sizeof (tracing_start_time
)),
3676 phex_nz (tracing_stop_time
, sizeof (tracing_stop_time
)),
3681 cmd_qtp (char *own_buf
)
3684 struct tracepoint
*tpoint
;
3685 char *packet
= own_buf
;
3687 packet
+= strlen ("qTP:");
3689 packet
= unpack_varlen_hex (packet
, &num
);
3690 ++packet
; /* skip a colon */
3691 packet
= unpack_varlen_hex (packet
, &addr
);
3693 /* See if we already have this tracepoint. */
3694 tpoint
= find_tracepoint (num
, addr
);
3698 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3699 (int) num
, paddress (addr
));
3700 write_enn (own_buf
);
3704 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3705 tpoint
->traceframe_usage
);
3708 /* State variables to help return all the tracepoint bits. */
3709 static struct tracepoint
*cur_tpoint
;
3710 static unsigned int cur_action
;
3711 static unsigned int cur_step_action
;
3712 static struct source_string
*cur_source_string
;
3713 static struct trace_state_variable
*cur_tsv
;
3715 /* Compose a response that is an imitation of the syntax by which the
3716 tracepoint was originally downloaded. */
3719 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3723 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3724 paddress (tpoint
->address
),
3725 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3726 tpoint
->pass_count
);
3727 if (tpoint
->type
== fast_tracepoint
)
3728 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3729 else if (tpoint
->type
== static_tracepoint
)
3730 sprintf (packet
+ strlen (packet
), ":S");
3734 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3735 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3736 tpoint
->cond
->length
, buf
);
3741 /* Compose a response that is an imitation of the syntax by which the
3742 tracepoint action was originally downloaded (with the difference
3743 that due to the way we store the actions, this will output a packet
3744 per action, while GDB could have combined more than one action
3748 response_action (char *packet
, struct tracepoint
*tpoint
,
3749 char *taction
, int step
)
3751 sprintf (packet
, "%c%x:%s:%s",
3752 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3756 /* Compose a response that is an imitation of the syntax by which the
3757 tracepoint source piece was originally downloaded. */
3760 response_source (char *packet
,
3761 struct tracepoint
*tpoint
, struct source_string
*src
)
3766 len
= strlen (src
->str
);
3767 buf
= alloca (len
* 2 + 1);
3768 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3770 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3771 tpoint
->number
, paddress (tpoint
->address
),
3772 src
->type
, 0, len
, buf
);
3775 /* Return the first piece of tracepoint definition, and initialize the
3776 state machine that will iterate through all the tracepoint
3780 cmd_qtfp (char *packet
)
3782 trace_debug ("Returning first tracepoint definition piece");
3784 cur_tpoint
= tracepoints
;
3785 cur_action
= cur_step_action
= 0;
3786 cur_source_string
= NULL
;
3789 response_tracepoint (packet
, cur_tpoint
);
3791 strcpy (packet
, "l");
3794 /* Return additional pieces of tracepoint definition. Each action and
3795 stepping action must go into its own packet, because of packet size
3796 limits, and so we use state variables to deliver one piece at a
3800 cmd_qtsp (char *packet
)
3802 trace_debug ("Returning subsequent tracepoint definition piece");
3806 /* This case would normally never occur, but be prepared for
3808 strcpy (packet
, "l");
3810 else if (cur_action
< cur_tpoint
->numactions
)
3812 response_action (packet
, cur_tpoint
,
3813 cur_tpoint
->actions_str
[cur_action
], 0);
3816 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3818 response_action (packet
, cur_tpoint
,
3819 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3822 else if ((cur_source_string
3823 ? cur_source_string
->next
3824 : cur_tpoint
->source_strings
))
3826 if (cur_source_string
)
3827 cur_source_string
= cur_source_string
->next
;
3829 cur_source_string
= cur_tpoint
->source_strings
;
3830 response_source (packet
, cur_tpoint
, cur_source_string
);
3834 cur_tpoint
= cur_tpoint
->next
;
3835 cur_action
= cur_step_action
= 0;
3836 cur_source_string
= NULL
;
3838 response_tracepoint (packet
, cur_tpoint
);
3840 strcpy (packet
, "l");
3844 /* Compose a response that is an imitation of the syntax by which the
3845 trace state variable was originally downloaded. */
3848 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3850 char *buf
= (char *) "";
3855 namelen
= strlen (tsv
->name
);
3856 buf
= alloca (namelen
* 2 + 1);
3857 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3860 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3861 tsv
->getter
? 1 : 0, buf
);
3864 /* Return the first trace state variable definition, and initialize
3865 the state machine that will iterate through all the tsv bits. */
3868 cmd_qtfv (char *packet
)
3870 trace_debug ("Returning first trace state variable definition");
3872 cur_tsv
= trace_state_variables
;
3875 response_tsv (packet
, cur_tsv
);
3877 strcpy (packet
, "l");
3880 /* Return additional trace state variable definitions. */
3883 cmd_qtsv (char *packet
)
3885 trace_debug ("Returning additional trace state variable definition");
3889 cur_tsv
= cur_tsv
->next
;
3891 response_tsv (packet
, cur_tsv
);
3893 strcpy (packet
, "l");
3896 strcpy (packet
, "l");
3899 /* Return the first static tracepoint marker, and initialize the state
3900 machine that will iterate through all the static tracepoints
3904 cmd_qtfstm (char *packet
)
3906 if (!maybe_write_ipa_ust_not_loaded (packet
))
3907 run_inferior_command (packet
, strlen (packet
) + 1);
3910 /* Return additional static tracepoints markers. */
3913 cmd_qtsstm (char *packet
)
3915 if (!maybe_write_ipa_ust_not_loaded (packet
))
3916 run_inferior_command (packet
, strlen (packet
) + 1);
3919 /* Return the definition of the static tracepoint at a given address.
3920 Result packet is the same as qTsST's. */
3923 cmd_qtstmat (char *packet
)
3925 if (!maybe_write_ipa_ust_not_loaded (packet
))
3926 run_inferior_command (packet
, strlen (packet
) + 1);
3929 /* Sent the agent a command to close it. */
3932 gdb_agent_about_to_close (int pid
)
3934 char buf
[IPA_CMD_BUF_SIZE
];
3936 if (!maybe_write_ipa_not_loaded (buf
))
3938 struct thread_info
*save_inferior
;
3939 struct inferior_list_entry
*inf
= all_threads
.head
;
3941 save_inferior
= current_inferior
;
3943 /* Find a certain thread which belongs to process PID. */
3946 if (ptid_get_pid (inf
->id
) == pid
)
3951 current_inferior
= (struct thread_info
*) inf
;
3953 strcpy (buf
, "close");
3955 run_inferior_command (buf
, strlen (buf
) + 1);
3957 current_inferior
= save_inferior
;
3961 /* Return the minimum instruction size needed for fast tracepoints as a
3962 hexadecimal number. */
3965 cmd_qtminftpilen (char *packet
)
3967 if (current_inferior
== NULL
)
3969 /* Indicate that the minimum length is currently unknown. */
3970 strcpy (packet
, "0");
3974 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3977 /* Respond to qTBuffer packet with a block of raw data from the trace
3978 buffer. GDB may ask for a lot, but we are allowed to reply with
3979 only as much as will fit within packet limits or whatever. */
3982 cmd_qtbuffer (char *own_buf
)
3984 ULONGEST offset
, num
, tot
;
3986 char *packet
= own_buf
;
3988 packet
+= strlen ("qTBuffer:");
3990 packet
= unpack_varlen_hex (packet
, &offset
);
3991 ++packet
; /* skip a comma */
3992 unpack_varlen_hex (packet
, &num
);
3994 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3995 (int) num
, phex_nz (offset
, 0));
3997 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3999 /* If we're right at the end, reply specially that we're done. */
4002 strcpy (own_buf
, "l");
4006 /* Object to any other out-of-bounds request. */
4009 write_enn (own_buf
);
4013 /* Compute the pointer corresponding to the given offset, accounting
4015 tbp
= trace_buffer_start
+ offset
;
4016 if (tbp
>= trace_buffer_wrap
)
4017 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4019 /* Trim to the remaining bytes if we're close to the end. */
4020 if (num
> tot
- offset
)
4023 /* Trim to available packet size. */
4024 if (num
>= (PBUFSIZ
- 16) / 2 )
4025 num
= (PBUFSIZ
- 16) / 2;
4027 convert_int_to_ascii (tbp
, own_buf
, num
);
4031 cmd_bigqtbuffer_circular (char *own_buf
)
4034 char *packet
= own_buf
;
4036 packet
+= strlen ("QTBuffer:circular:");
4038 unpack_varlen_hex (packet
, &val
);
4039 circular_trace_buffer
= val
;
4040 trace_debug ("Trace buffer is now %s",
4041 circular_trace_buffer
? "circular" : "linear");
4046 cmd_bigqtbuffer_size (char *own_buf
)
4050 char *packet
= own_buf
;
4052 /* Can't change the size during a tracing run. */
4055 write_enn (own_buf
);
4059 packet
+= strlen ("QTBuffer:size:");
4061 /* -1 is sent as literal "-1". */
4062 if (strcmp (packet
, "-1") == 0)
4063 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4066 unpack_varlen_hex (packet
, &val
);
4067 sval
= (LONGEST
) val
;
4070 init_trace_buffer (sval
);
4071 trace_debug ("Trace buffer is now %s bytes",
4072 plongest (trace_buffer_size
));
4077 cmd_qtnotes (char *own_buf
)
4080 char *saved
, *user
, *notes
, *stopnote
;
4081 char *packet
= own_buf
;
4083 packet
+= strlen ("QTNotes:");
4087 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4089 packet
+= strlen ("user:");
4091 packet
= strchr (packet
, ';');
4092 nbytes
= (packet
- saved
) / 2;
4093 user
= xmalloc (nbytes
+ 1);
4094 nbytes
= unhexify (user
, saved
, nbytes
);
4095 user
[nbytes
] = '\0';
4096 ++packet
; /* skip the semicolon */
4097 trace_debug ("User is '%s'", user
);
4098 xfree (tracing_user_name
);
4099 tracing_user_name
= user
;
4101 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4103 packet
+= strlen ("notes:");
4105 packet
= strchr (packet
, ';');
4106 nbytes
= (packet
- saved
) / 2;
4107 notes
= xmalloc (nbytes
+ 1);
4108 nbytes
= unhexify (notes
, saved
, nbytes
);
4109 notes
[nbytes
] = '\0';
4110 ++packet
; /* skip the semicolon */
4111 trace_debug ("Notes is '%s'", notes
);
4112 xfree (tracing_notes
);
4113 tracing_notes
= notes
;
4115 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4117 packet
+= strlen ("tstop:");
4119 packet
= strchr (packet
, ';');
4120 nbytes
= (packet
- saved
) / 2;
4121 stopnote
= xmalloc (nbytes
+ 1);
4122 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4123 stopnote
[nbytes
] = '\0';
4124 ++packet
; /* skip the semicolon */
4125 trace_debug ("tstop note is '%s'", stopnote
);
4126 xfree (tracing_stop_note
);
4127 tracing_stop_note
= stopnote
;
4137 handle_tracepoint_general_set (char *packet
)
4139 if (strcmp ("QTinit", packet
) == 0)
4141 cmd_qtinit (packet
);
4144 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4149 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4151 cmd_qtdpsrc (packet
);
4154 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4156 cmd_qtenable_disable (packet
, 1);
4159 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4161 cmd_qtenable_disable (packet
, 0);
4164 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4169 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4174 else if (strcmp ("QTStart", packet
) == 0)
4176 cmd_qtstart (packet
);
4179 else if (strcmp ("QTStop", packet
) == 0)
4181 cmd_qtstop (packet
);
4184 else if (strncmp ("QTDisconnected:", packet
,
4185 strlen ("QTDisconnected:")) == 0)
4187 cmd_qtdisconnected (packet
);
4190 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4192 cmd_qtframe (packet
);
4195 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4197 cmd_bigqtbuffer_circular (packet
);
4200 else if (strncmp ("QTBuffer:size:", packet
, strlen ("QTBuffer:size:")) == 0)
4202 cmd_bigqtbuffer_size (packet
);
4205 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4207 cmd_qtnotes (packet
);
4215 handle_tracepoint_query (char *packet
)
4217 if (strcmp ("qTStatus", packet
) == 0)
4219 cmd_qtstatus (packet
);
4222 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4227 else if (strcmp ("qTfP", packet
) == 0)
4232 else if (strcmp ("qTsP", packet
) == 0)
4237 else if (strcmp ("qTfV", packet
) == 0)
4242 else if (strcmp ("qTsV", packet
) == 0)
4247 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4252 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4254 cmd_qtbuffer (packet
);
4257 else if (strcmp ("qTfSTM", packet
) == 0)
4259 cmd_qtfstm (packet
);
4262 else if (strcmp ("qTsSTM", packet
) == 0)
4264 cmd_qtsstm (packet
);
4267 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4269 cmd_qtstmat (packet
);
4272 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4274 cmd_qtminftpilen (packet
);
4282 #ifndef IN_PROCESS_AGENT
4284 /* Call this when thread TINFO has hit the tracepoint defined by
4285 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4286 action. This adds a while-stepping collecting state item to the
4287 threads' collecting state list, so that we can keep track of
4288 multiple simultaneous while-stepping actions being collected by the
4289 same thread. This can happen in cases like:
4291 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4293 ff0003 INSN3 <-- TP2, collect $regs
4294 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4297 Notice that when instruction INSN5 is reached, the while-stepping
4298 actions of both TP1 and TP3 are still being collected, and that TP2
4299 had been collected meanwhile. The whole range of ff0001-ff0005
4300 should be single-stepped, due to at least TP1's while-stepping
4301 action covering the whole range. */
4304 add_while_stepping_state (struct thread_info
*tinfo
,
4305 int tp_number
, CORE_ADDR tp_address
)
4307 struct wstep_state
*wstep
;
4309 wstep
= xmalloc (sizeof (*wstep
));
4310 wstep
->next
= tinfo
->while_stepping
;
4312 wstep
->tp_number
= tp_number
;
4313 wstep
->tp_address
= tp_address
;
4314 wstep
->current_step
= 0;
4316 tinfo
->while_stepping
= wstep
;
4319 /* Release the while-stepping collecting state WSTEP. */
4322 release_while_stepping_state (struct wstep_state
*wstep
)
4327 /* Release all while-stepping collecting states currently associated
4328 with thread TINFO. */
4331 release_while_stepping_state_list (struct thread_info
*tinfo
)
4333 struct wstep_state
*head
;
4335 while (tinfo
->while_stepping
)
4337 head
= tinfo
->while_stepping
;
4338 tinfo
->while_stepping
= head
->next
;
4339 release_while_stepping_state (head
);
4343 /* If TINFO was handling a 'while-stepping' action, the step has
4344 finished, so collect any step data needed, and check if any more
4345 steps are required. Return true if the thread was indeed
4346 collecting tracepoint data, false otherwise. */
4349 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4351 struct tracepoint
*tpoint
;
4352 struct wstep_state
*wstep
;
4353 struct wstep_state
**wstep_link
;
4354 struct trap_tracepoint_ctx ctx
;
4356 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4358 if (agent_loaded_p ())
4359 upload_fast_traceframes ();
4361 /* Check if we were indeed collecting data for one of more
4362 tracepoints with a 'while-stepping' count. */
4363 if (tinfo
->while_stepping
== NULL
)
4368 /* We're not even tracing anymore. Stop this thread from
4370 release_while_stepping_state_list (tinfo
);
4372 /* The thread had stopped due to a single-step request indeed
4373 explained by a tracepoint. */
4377 wstep
= tinfo
->while_stepping
;
4378 wstep_link
= &tinfo
->while_stepping
;
4380 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4381 target_pid_to_str (tinfo
->entry
.id
),
4382 wstep
->tp_number
, paddress (wstep
->tp_address
));
4384 ctx
.base
.type
= trap_tracepoint
;
4385 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4387 while (wstep
!= NULL
)
4389 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4392 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4393 wstep
->tp_number
, paddress (wstep
->tp_address
),
4394 target_pid_to_str (tinfo
->entry
.id
));
4397 *wstep_link
= wstep
->next
;
4398 release_while_stepping_state (wstep
);
4399 wstep
= *wstep_link
;
4403 /* We've just finished one step. */
4404 ++wstep
->current_step
;
4407 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4408 stop_pc
, tpoint
, wstep
->current_step
);
4410 if (wstep
->current_step
>= tpoint
->step_count
)
4412 /* The requested numbers of steps have occurred. */
4413 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4414 target_pid_to_str (tinfo
->entry
.id
),
4415 wstep
->tp_number
, paddress (wstep
->tp_address
));
4417 /* Unlink the wstep. */
4418 *wstep_link
= wstep
->next
;
4419 release_while_stepping_state (wstep
);
4420 wstep
= *wstep_link
;
4422 /* Only check the hit count now, which ensure that we do all
4423 our stepping before stopping the run. */
4424 if (tpoint
->pass_count
> 0
4425 && tpoint
->hit_count
>= tpoint
->pass_count
4426 && stopping_tracepoint
== NULL
)
4427 stopping_tracepoint
= tpoint
;
4431 /* Keep single-stepping until the requested numbers of steps
4433 wstep_link
= &wstep
->next
;
4434 wstep
= *wstep_link
;
4437 if (stopping_tracepoint
4438 || trace_buffer_is_full
4439 || expr_eval_result
!= expr_eval_no_error
)
4449 /* Handle any internal tracing control breakpoint hits. That means,
4450 pull traceframes from the IPA to our buffer, and syncing both
4451 tracing agents when the IPA's tracing stops for some reason. */
4454 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4456 /* Pull in fast tracepoint trace frames from the inferior in-process
4457 agent's buffer into our buffer. */
4459 if (!agent_loaded_p ())
4462 upload_fast_traceframes ();
4464 /* Check if the in-process agent had decided we should stop
4466 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4468 int ipa_trace_buffer_is_full
;
4469 CORE_ADDR ipa_stopping_tracepoint
;
4470 int ipa_expr_eval_result
;
4471 CORE_ADDR ipa_error_tracepoint
;
4473 trace_debug ("lib stopped at stop_tracing");
4475 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4476 &ipa_trace_buffer_is_full
);
4478 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4479 &ipa_stopping_tracepoint
);
4480 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4482 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4483 &ipa_error_tracepoint
);
4484 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4486 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4487 &ipa_expr_eval_result
);
4488 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4490 trace_debug ("lib: trace_buffer_is_full: %d, "
4491 "stopping_tracepoint: %s, "
4492 "ipa_expr_eval_result: %d, "
4493 "error_tracepoint: %s, ",
4494 ipa_trace_buffer_is_full
,
4495 paddress (ipa_stopping_tracepoint
),
4496 ipa_expr_eval_result
,
4497 paddress (ipa_error_tracepoint
));
4501 if (ipa_trace_buffer_is_full
)
4502 trace_debug ("lib stopped due to full buffer.");
4503 if (ipa_stopping_tracepoint
)
4504 trace_debug ("lib stopped due to tpoint");
4505 if (ipa_stopping_tracepoint
)
4506 trace_debug ("lib stopped due to error");
4509 if (ipa_stopping_tracepoint
!= 0)
4512 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4514 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4516 expr_eval_result
= ipa_expr_eval_result
;
4518 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4523 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4525 trace_debug ("lib stopped at flush_trace_buffer");
4532 /* Return true if TINFO just hit a tracepoint. Collect data if
4536 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4538 struct tracepoint
*tpoint
;
4540 struct trap_tracepoint_ctx ctx
;
4542 /* Not tracing, don't handle. */
4546 ctx
.base
.type
= trap_tracepoint
;
4547 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4549 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4551 /* Note that we collect fast tracepoints here as well. We'll
4552 step over the fast tracepoint jump later, which avoids the
4553 double collect. However, we don't collect for static
4554 tracepoints here, because UST markers are compiled in program,
4555 and probes will be executed in program. So static tracepoints
4556 are collected there. */
4557 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4558 && tpoint
->type
!= static_tracepoint
)
4560 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4561 target_pid_to_str (tinfo
->entry
.id
),
4562 tpoint
->number
, paddress (tpoint
->address
));
4564 /* Test the condition if present, and collect if true. */
4566 || (condition_true_at_tracepoint
4567 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4568 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4571 if (stopping_tracepoint
4572 || trace_buffer_is_full
4573 || expr_eval_result
!= expr_eval_no_error
)
4577 /* If the tracepoint had a 'while-stepping' action, then set
4578 the thread to collect this tracepoint on the following
4580 else if (tpoint
->step_count
> 0)
4582 add_while_stepping_state (tinfo
,
4583 tpoint
->number
, tpoint
->address
);
4595 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4596 struct ust_marker_data
;
4597 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4598 struct traceframe
*tframe
);
4601 /* Create a trace frame for the hit of the given tracepoint in the
4605 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4606 struct tracepoint
*tpoint
)
4608 struct traceframe
*tframe
;
4611 /* Only count it as a hit when we actually collect data. */
4612 tpoint
->hit_count
++;
4614 /* If we've exceeded a defined pass count, record the event for
4615 later, and finish the collection for this hit. This test is only
4616 for nonstepping tracepoints, stepping tracepoints test at the end
4617 of their while-stepping loop. */
4618 if (tpoint
->pass_count
> 0
4619 && tpoint
->hit_count
>= tpoint
->pass_count
4620 && tpoint
->step_count
== 0
4621 && stopping_tracepoint
== NULL
)
4622 stopping_tracepoint
= tpoint
;
4624 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4625 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4627 tframe
= add_traceframe (tpoint
);
4631 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4633 #ifndef IN_PROCESS_AGENT
4634 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4635 tpoint
->number
, paddress (tpoint
->address
),
4636 tpoint
->actions_str
[acti
]);
4639 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4640 tpoint
->actions
[acti
]);
4643 finish_traceframe (tframe
);
4646 if (tframe
== NULL
&& tracing
)
4647 trace_buffer_is_full
= 1;
4650 #ifndef IN_PROCESS_AGENT
4653 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4655 struct tracepoint
*tpoint
, int current_step
)
4657 struct traceframe
*tframe
;
4660 trace_debug ("Making new step traceframe for "
4661 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4662 tpoint
->number
, paddress (tpoint
->address
),
4663 current_step
, tpoint
->step_count
,
4666 tframe
= add_traceframe (tpoint
);
4670 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4672 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4673 tpoint
->number
, paddress (tpoint
->address
),
4674 tpoint
->step_actions_str
[acti
]);
4676 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4677 tpoint
->step_actions
[acti
]);
4680 finish_traceframe (tframe
);
4683 if (tframe
== NULL
&& tracing
)
4684 trace_buffer_is_full
= 1;
4689 #ifdef IN_PROCESS_AGENT
4690 /* The target description used by the IPA. Given that the IPA library
4691 is built for a specific architecture that is loaded into the
4692 inferior, there only needs to be one such description per
4694 const struct target_desc
*ipa_tdesc
;
4697 static struct regcache
*
4698 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4700 struct regcache
*regcache
= NULL
;
4702 #ifdef IN_PROCESS_AGENT
4703 if (ctx
->type
== fast_tracepoint
)
4705 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4706 if (!fctx
->regcache_initted
)
4708 fctx
->regcache_initted
= 1;
4709 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4710 supply_regblock (&fctx
->regcache
, NULL
);
4711 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4713 regcache
= &fctx
->regcache
;
4716 if (ctx
->type
== static_tracepoint
)
4718 struct static_tracepoint_ctx
*sctx
4719 = (struct static_tracepoint_ctx
*) ctx
;
4721 if (!sctx
->regcache_initted
)
4723 sctx
->regcache_initted
= 1;
4724 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4725 supply_regblock (&sctx
->regcache
, NULL
);
4726 /* Pass down the tracepoint address, because REGS doesn't
4727 include the PC, but we know what it must have been. */
4728 supply_static_tracepoint_registers (&sctx
->regcache
,
4729 (const unsigned char *)
4731 sctx
->tpoint
->address
);
4733 regcache
= &sctx
->regcache
;
4737 if (ctx
->type
== trap_tracepoint
)
4739 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4740 regcache
= tctx
->regcache
;
4744 gdb_assert (regcache
!= NULL
);
4750 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4752 struct tracepoint
*tpoint
,
4753 struct traceframe
*tframe
,
4754 struct tracepoint_action
*taction
)
4756 enum eval_result_type err
;
4758 switch (taction
->type
)
4762 struct collect_memory_action
*maction
;
4763 struct eval_agent_expr_context ax_ctx
;
4765 maction
= (struct collect_memory_action
*) taction
;
4766 ax_ctx
.regcache
= NULL
;
4767 ax_ctx
.tframe
= tframe
;
4768 ax_ctx
.tpoint
= tpoint
;
4770 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4771 pulongest (maction
->len
),
4772 paddress (maction
->addr
), maction
->basereg
);
4773 /* (should use basereg) */
4774 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4780 unsigned char *regspace
;
4781 struct regcache tregcache
;
4782 struct regcache
*context_regcache
;
4785 trace_debug ("Want to collect registers");
4787 context_regcache
= get_context_regcache (ctx
);
4788 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4790 /* Collect all registers for now. */
4791 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4792 if (regspace
== NULL
)
4794 trace_debug ("Trace buffer block allocation failed, skipping");
4797 /* Identify a register block. */
4800 /* Wrap the regblock in a register cache (in the stack, we
4801 don't want to malloc here). */
4802 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4805 /* Copy the register data to the regblock. */
4806 regcache_cpy (&tregcache
, context_regcache
);
4808 #ifndef IN_PROCESS_AGENT
4809 /* On some platforms, trap-based tracepoints will have the PC
4810 pointing to the next instruction after the trap, but we
4811 don't want the user or GDB trying to guess whether the
4812 saved PC needs adjusting; so always record the adjusted
4813 stop_pc. Note that we can't use tpoint->address instead,
4814 since it will be wrong for while-stepping actions. This
4815 adjustment is a nop for fast tracepoints collected from the
4816 in-process lib (but not if GDBserver is collecting one
4817 preemptively), since the PC had already been adjusted to
4818 contain the tracepoint's address by the jump pad. */
4819 trace_debug ("Storing stop pc (0x%s) in regblock",
4820 paddress (stop_pc
));
4822 /* This changes the regblock, not the thread's
4824 regcache_write_pc (&tregcache
, stop_pc
);
4830 struct eval_expr_action
*eaction
;
4831 struct eval_agent_expr_context ax_ctx
;
4833 eaction
= (struct eval_expr_action
*) taction
;
4834 ax_ctx
.regcache
= get_context_regcache (ctx
);
4835 ax_ctx
.tframe
= tframe
;
4836 ax_ctx
.tpoint
= tpoint
;
4838 trace_debug ("Want to evaluate expression");
4840 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4842 if (err
!= expr_eval_no_error
)
4844 record_tracepoint_error (tpoint
, "action expression", err
);
4851 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4852 trace_debug ("Want to collect static trace data");
4853 collect_ust_data_at_tracepoint (ctx
, tframe
);
4855 trace_debug ("warning: collecting static trace data, "
4856 "but static tracepoints are not supported");
4861 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4867 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4868 struct tracepoint
*tpoint
)
4871 enum eval_result_type err
;
4873 /* Presently, gdbserver doesn't run compiled conditions, only the
4874 IPA does. If the program stops at a fast tracepoint's address
4875 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4876 gdbserver preemptively collect the fast tracepoint. Later, on
4877 resume, gdbserver steps over the fast tracepoint like it steps
4878 over breakpoints, so that the IPA doesn't see that fast
4879 tracepoint. This avoids double collects of fast tracepoints in
4880 that stopping scenario. Having gdbserver itself handle the fast
4881 tracepoint gives the user a consistent view of when fast or trap
4882 tracepoints are collected, compared to an alternative where only
4883 trap tracepoints are collected on stop, and fast tracepoints on
4884 resume. When a fast tracepoint is being processed by gdbserver,
4885 it is always the non-compiled condition expression that is
4887 #ifdef IN_PROCESS_AGENT
4888 if (tpoint
->compiled_cond
)
4889 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4893 struct eval_agent_expr_context ax_ctx
;
4895 ax_ctx
.regcache
= get_context_regcache (ctx
);
4896 ax_ctx
.tframe
= NULL
;
4897 ax_ctx
.tpoint
= tpoint
;
4899 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4901 if (err
!= expr_eval_no_error
)
4903 record_tracepoint_error (tpoint
, "condition", err
);
4904 /* The error case must return false. */
4908 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4909 tpoint
->number
, paddress (tpoint
->address
),
4911 return (value
? 1 : 0);
4914 /* Do memory copies for bytecodes. */
4915 /* Do the recording of memory blocks for actions and bytecodes. */
4918 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4919 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4921 unsigned char *mspace
;
4922 ULONGEST remaining
= len
;
4923 unsigned short blocklen
;
4925 /* If a 'to' buffer is specified, use it. */
4928 read_inferior_memory (from
, to
, len
);
4932 /* Otherwise, create a new memory block in the trace buffer. */
4933 while (remaining
> 0)
4937 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4938 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4939 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4942 /* Identify block as a memory block. */
4945 /* Record address and size. */
4946 memcpy (mspace
, &from
, sizeof (from
));
4947 mspace
+= sizeof (from
);
4948 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4949 mspace
+= sizeof (blocklen
);
4950 /* Record the memory block proper. */
4951 read_inferior_memory (from
, mspace
, blocklen
);
4952 trace_debug ("%d bytes recorded", blocklen
);
4953 remaining
-= blocklen
;
4960 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4961 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4963 unsigned char *buf
, *mspace
;
4964 ULONGEST remaining
= len
;
4965 unsigned short blocklen
, i
;
4967 /* To save a bit of space, block lengths are 16-bit, so break large
4968 requests into multiple blocks. Bordering on overkill for strings,
4969 but it could happen that someone specifies a large max length. */
4970 while (remaining
> 0)
4974 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4975 /* We want working space to accumulate nonzero bytes, since
4976 traceframes must have a predecided size (otherwise it gets
4977 harder to wrap correctly for the circular case, etc). */
4978 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4979 for (i
= 0; i
< blocklen
; ++i
)
4981 /* Read the string one byte at a time, in case the string is
4982 at the end of a valid memory area - we don't want a
4983 correctly-terminated string to engender segvio
4985 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4990 /* Make sure outer loop stops now too. */
4991 remaining
= blocklen
;
4995 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4996 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5002 /* Identify block as a memory block. */
5005 /* Record address and size. */
5006 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5007 mspace
+= sizeof (from
);
5008 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5009 mspace
+= sizeof (blocklen
);
5010 /* Copy the string contents. */
5011 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5012 remaining
-= blocklen
;
5019 /* Record the value of a trace state variable. */
5022 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5024 unsigned char *vspace
;
5027 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5028 1 + sizeof (n
) + sizeof (LONGEST
));
5031 /* Identify block as a variable. */
5033 /* Record variable's number and value. */
5034 memcpy (vspace
+ 1, &n
, sizeof (n
));
5035 val
= get_trace_state_variable_value (n
);
5036 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5037 trace_debug ("Variable %d recorded", n
);
5041 #ifndef IN_PROCESS_AGENT
5043 /* Callback for traceframe_walk_blocks, used to find a given block
5044 type in a traceframe. */
5047 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5049 char *wantedp
= data
;
5051 if (*wantedp
== blocktype
)
5057 /* Walk over all traceframe blocks of the traceframe buffer starting
5058 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5059 block found, passing in DATA unmodified. If CALLBACK returns true,
5060 this returns a pointer to where the block is found. Returns NULL
5061 if no callback call returned true, indicating that all blocks have
5064 static unsigned char *
5065 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5067 int (*callback
) (char blocktype
,
5068 unsigned char *dataptr
,
5072 unsigned char *dataptr
;
5076 trace_debug ("traceframe %d has no data", tfnum
);
5080 /* Iterate through a traceframe's blocks, looking for a block of the
5082 for (dataptr
= database
;
5083 dataptr
< database
+ datasize
;
5087 unsigned short mlen
;
5089 if (dataptr
== trace_buffer_wrap
)
5091 /* Adjust to reflect wrapping part of the frame around to
5093 datasize
= dataptr
- database
;
5094 dataptr
= database
= trace_buffer_lo
;
5097 blocktype
= *dataptr
++;
5099 if ((*callback
) (blocktype
, dataptr
, data
))
5105 /* Skip over the registers block. */
5106 dataptr
+= current_target_desc ()->registers_size
;
5109 /* Skip over the memory block. */
5110 dataptr
+= sizeof (CORE_ADDR
);
5111 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5112 dataptr
+= (sizeof (mlen
) + mlen
);
5115 /* Skip over the TSV block. */
5116 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5119 /* Skip over the static trace data block. */
5120 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5121 dataptr
+= (sizeof (mlen
) + mlen
);
5124 trace_debug ("traceframe %d has unknown block type 0x%x",
5133 /* Look for the block of type TYPE_WANTED in the trameframe starting
5134 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5137 static unsigned char *
5138 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5139 int tfnum
, char type_wanted
)
5141 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5142 match_blocktype
, &type_wanted
);
5145 static unsigned char *
5146 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5148 unsigned char *regblock
;
5150 regblock
= traceframe_find_block_type (tframe
->data
,
5154 if (regblock
== NULL
)
5155 trace_debug ("traceframe %d has no register data", tfnum
);
5160 /* Get registers from a traceframe. */
5163 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5165 unsigned char *dataptr
;
5166 struct tracepoint
*tpoint
;
5167 struct traceframe
*tframe
;
5169 tframe
= find_traceframe (tfnum
);
5173 trace_debug ("traceframe %d not found", tfnum
);
5177 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5178 if (dataptr
== NULL
)
5180 /* Mark registers unavailable. */
5181 supply_regblock (regcache
, NULL
);
5183 /* We can generally guess at a PC, although this will be
5184 misleading for while-stepping frames and multi-location
5186 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5188 regcache_write_pc (regcache
, tpoint
->address
);
5191 supply_regblock (regcache
, dataptr
);
5197 traceframe_get_pc (struct traceframe
*tframe
)
5199 struct regcache regcache
;
5200 unsigned char *dataptr
;
5201 const struct target_desc
*tdesc
= current_target_desc ();
5203 dataptr
= traceframe_find_regblock (tframe
, -1);
5204 if (dataptr
== NULL
)
5207 init_register_cache (®cache
, tdesc
, dataptr
);
5208 return regcache_read_pc (®cache
);
5211 /* Read a requested block of memory from a trace frame. */
5214 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5215 unsigned char *buf
, ULONGEST length
,
5218 struct traceframe
*tframe
;
5219 unsigned char *database
, *dataptr
;
5220 unsigned int datasize
;
5222 unsigned short mlen
;
5224 trace_debug ("traceframe_read_mem");
5226 tframe
= find_traceframe (tfnum
);
5230 trace_debug ("traceframe %d not found", tfnum
);
5234 datasize
= tframe
->data_size
;
5235 database
= dataptr
= &tframe
->data
[0];
5237 /* Iterate through a traceframe's blocks, looking for memory. */
5238 while ((dataptr
= traceframe_find_block_type (dataptr
,
5240 - (dataptr
- database
),
5241 tfnum
, 'M')) != NULL
)
5243 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5244 dataptr
+= sizeof (maddr
);
5245 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5246 dataptr
+= sizeof (mlen
);
5247 trace_debug ("traceframe %d has %d bytes at %s",
5248 tfnum
, mlen
, paddress (maddr
));
5250 /* If the block includes the first part of the desired range,
5251 return as much it has; GDB will re-request the remainder,
5252 which might be in a different block of this trace frame. */
5253 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5255 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5259 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5264 /* Skip over this block. */
5268 trace_debug ("traceframe %d has no memory data for the desired region",
5276 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5279 struct traceframe
*tframe
;
5280 unsigned char *database
, *dataptr
;
5281 unsigned int datasize
;
5285 trace_debug ("traceframe_read_tsv");
5287 tfnum
= current_traceframe
;
5291 trace_debug ("no current traceframe");
5295 tframe
= find_traceframe (tfnum
);
5299 trace_debug ("traceframe %d not found", tfnum
);
5303 datasize
= tframe
->data_size
;
5304 database
= dataptr
= &tframe
->data
[0];
5306 /* Iterate through a traceframe's blocks, looking for the last
5308 while ((dataptr
= traceframe_find_block_type (dataptr
,
5310 - (dataptr
- database
),
5311 tfnum
, 'V')) != NULL
)
5313 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5314 dataptr
+= sizeof (vnum
);
5316 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5318 /* Check that this is the variable we want. */
5321 memcpy (val
, dataptr
, sizeof (*val
));
5325 /* Skip over this block. */
5326 dataptr
+= sizeof (LONGEST
);
5330 trace_debug ("traceframe %d has no data for variable %d",
5335 /* Read a requested block of static tracepoint data from a trace
5339 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5340 unsigned char *buf
, ULONGEST length
,
5343 struct traceframe
*tframe
;
5344 unsigned char *database
, *dataptr
;
5345 unsigned int datasize
;
5346 unsigned short mlen
;
5348 trace_debug ("traceframe_read_sdata");
5350 tframe
= find_traceframe (tfnum
);
5354 trace_debug ("traceframe %d not found", tfnum
);
5358 datasize
= tframe
->data_size
;
5359 database
= &tframe
->data
[0];
5361 /* Iterate through a traceframe's blocks, looking for static
5363 dataptr
= traceframe_find_block_type (database
, datasize
,
5365 if (dataptr
!= NULL
)
5367 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5368 dataptr
+= sizeof (mlen
);
5371 if (offset
+ length
> mlen
)
5372 length
= mlen
- offset
;
5374 memcpy (buf
, dataptr
, length
);
5382 trace_debug ("traceframe %d has no static trace data", tfnum
);
5388 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5389 object. DATA is pointer to a struct buffer holding the
5390 traceframe-info object being built. */
5393 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5395 struct buffer
*buffer
= data
;
5401 unsigned short mlen
;
5404 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5405 dataptr
+= sizeof (maddr
);
5406 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5407 dataptr
+= sizeof (mlen
);
5408 buffer_xml_printf (buffer
,
5409 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5410 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5417 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5418 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5427 warning ("Unhandled trace block type (%d) '%c ' "
5428 "while building trace frame info.",
5429 blocktype
, blocktype
);
5436 /* Build a traceframe-info object for traceframe number TFNUM into
5440 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5442 struct traceframe
*tframe
;
5444 trace_debug ("traceframe_read_info");
5446 tframe
= find_traceframe (tfnum
);
5450 trace_debug ("traceframe %d not found", tfnum
);
5454 buffer_grow_str (buffer
, "<traceframe-info>\n");
5455 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5456 tfnum
, build_traceframe_info_xml
, buffer
);
5457 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5461 /* Return the first fast tracepoint whose jump pad contains PC. */
5463 static struct tracepoint
*
5464 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5466 struct tracepoint
*tpoint
;
5468 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5469 if (tpoint
->type
== fast_tracepoint
)
5470 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5476 /* Return the first fast tracepoint whose trampoline contains PC. */
5478 static struct tracepoint
*
5479 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5481 struct tracepoint
*tpoint
;
5483 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5485 if (tpoint
->type
== fast_tracepoint
5486 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5493 /* Return GDBserver's tracepoint that matches the IP Agent's
5494 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5497 static struct tracepoint
*
5498 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5500 struct tracepoint
*tpoint
;
5502 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5503 if (tpoint
->type
== fast_tracepoint
)
5504 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5512 /* The type of the object that is used to synchronize fast tracepoint
5515 typedef struct collecting_t
5517 /* The fast tracepoint number currently collecting. */
5520 /* A number that GDBserver can use to identify the thread that is
5521 presently holding the collect lock. This need not (and usually
5522 is not) the thread id, as getting the current thread ID usually
5523 requires a system call, which we want to avoid like the plague.
5524 Usually this is thread's TCB, found in the TLS (pseudo-)
5525 register, which is readable with a single insn on several
5527 uintptr_t thread_area
;
5530 #ifndef IN_PROCESS_AGENT
5533 force_unlock_trace_buffer (void)
5535 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5538 /* Check if the thread identified by THREAD_AREA which is stopped at
5539 STOP_PC, is presently locking the fast tracepoint collection, and
5540 if so, gather some status of said collection. Returns 0 if the
5541 thread isn't collecting or in the jump pad at all. 1, if in the
5542 jump pad (or within gdb_collect) and hasn't executed the adjusted
5543 original insn yet (can set a breakpoint there and run to it). 2,
5544 if presently executing the adjusted original insn --- in which
5545 case, if we want to move the thread out of the jump pad, we need to
5546 single-step it until this function returns 0. */
5549 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5551 struct fast_tpoint_collect_status
*status
)
5553 CORE_ADDR ipa_collecting
;
5554 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5555 CORE_ADDR ipa_gdb_trampoline_buffer
;
5556 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5557 struct tracepoint
*tpoint
;
5558 int needs_breakpoint
;
5560 /* The thread THREAD_AREA is either:
5562 0. not collecting at all, not within the jump pad, or within
5563 gdb_collect or one of its callees.
5565 1. in the jump pad and haven't reached gdb_collect
5567 2. within gdb_collect (out of the jump pad) (collect is set)
5569 3. we're in the jump pad, after gdb_collect having returned,
5570 possibly executing the adjusted insns.
5572 For cases 1 and 3, `collecting' may or not be set. The jump pad
5573 doesn't have any complicated jump logic, so we can tell if the
5574 thread is executing the adjust original insn or not by just
5575 matching STOP_PC with known jump pad addresses. If we it isn't
5576 yet executing the original insn, set a breakpoint there, and let
5577 the thread run to it, so to quickly step over a possible (many
5578 insns) gdb_collect call. Otherwise, or when the breakpoint is
5579 hit, only a few (small number of) insns are left to be executed
5580 in the jump pad. Single-step the thread until it leaves the
5585 needs_breakpoint
= 0;
5586 trace_debug ("fast_tracepoint_collecting");
5588 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5589 &ipa_gdb_jump_pad_buffer
))
5590 fatal ("error extracting `gdb_jump_pad_buffer'");
5591 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5592 &ipa_gdb_jump_pad_buffer_end
))
5593 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5595 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5596 &ipa_gdb_trampoline_buffer
))
5597 fatal ("error extracting `gdb_trampoline_buffer'");
5598 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5599 &ipa_gdb_trampoline_buffer_end
))
5600 fatal ("error extracting `gdb_trampoline_buffer_end'");
5602 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5603 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5605 /* We can tell which tracepoint(s) the thread is collecting by
5606 matching the jump pad address back to the tracepoint. */
5607 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5610 warning ("in jump pad, but no matching tpoint?");
5615 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5617 tpoint
->number
, paddress (tpoint
->address
),
5618 paddress (tpoint
->jump_pad
),
5619 paddress (tpoint
->jump_pad_end
),
5620 paddress (tpoint
->adjusted_insn_addr
),
5621 paddress (tpoint
->adjusted_insn_addr_end
));
5624 /* Definitely in the jump pad. May or may not need
5625 fast-exit-jump-pad breakpoint. */
5626 if (tpoint
->jump_pad
<= stop_pc
5627 && stop_pc
< tpoint
->adjusted_insn_addr
)
5628 needs_breakpoint
= 1;
5630 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5631 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5633 /* We can tell which tracepoint(s) the thread is collecting by
5634 matching the trampoline address back to the tracepoint. */
5635 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5638 warning ("in trampoline, but no matching tpoint?");
5643 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5644 tpoint
->number
, paddress (tpoint
->address
),
5645 paddress (tpoint
->trampoline
),
5646 paddress (tpoint
->trampoline_end
));
5649 /* Have not reached jump pad yet, but treat the trampoline as a
5650 part of the jump pad that is before the adjusted original
5652 needs_breakpoint
= 1;
5656 collecting_t ipa_collecting_obj
;
5658 /* If `collecting' is set/locked, then the THREAD_AREA thread
5659 may or not be the one holding the lock. We have to read the
5660 lock to find out. */
5662 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5665 trace_debug ("fast_tracepoint_collecting:"
5666 " failed reading 'collecting' in the inferior");
5670 if (!ipa_collecting
)
5672 trace_debug ("fast_tracepoint_collecting: not collecting"
5673 " (and nobody is).");
5677 /* Some thread is collecting. Check which. */
5678 if (read_inferior_memory (ipa_collecting
,
5679 (unsigned char *) &ipa_collecting_obj
,
5680 sizeof (ipa_collecting_obj
)) != 0)
5683 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5685 trace_debug ("fast_tracepoint_collecting: not collecting "
5686 "(another thread is)");
5691 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5694 warning ("fast_tracepoint_collecting: collecting, "
5695 "but tpoint %s not found?",
5696 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5700 /* The thread is within `gdb_collect', skip over the rest of
5701 fast tracepoint collection quickly using a breakpoint. */
5702 needs_breakpoint
= 1;
5705 /* The caller wants a bit of status detail. */
5708 status
->tpoint_num
= tpoint
->number
;
5709 status
->tpoint_addr
= tpoint
->address
;
5710 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5711 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5714 if (needs_breakpoint
)
5716 /* Hasn't executed the original instruction yet. Set breakpoint
5717 there, and wait till it's hit, then single-step until exiting
5721 fast_tracepoint_collecting, returning continue-until-break at %s",
5722 paddress (tpoint
->adjusted_insn_addr
));
5724 return 1; /* continue */
5728 /* Just single-step until exiting the jump pad. */
5730 trace_debug ("fast_tracepoint_collecting, returning "
5731 "need-single-step (%s-%s)",
5732 paddress (tpoint
->adjusted_insn_addr
),
5733 paddress (tpoint
->adjusted_insn_addr_end
));
5735 return 2; /* single-step */
5741 #ifdef IN_PROCESS_AGENT
5743 /* The global fast tracepoint collect lock. Points to a collecting_t
5744 object built on the stack by the jump pad, if presently locked;
5745 NULL if it isn't locked. Note that this lock *must* be set while
5746 executing any *function other than the jump pad. See
5747 fast_tracepoint_collecting. */
5748 static collecting_t
* ATTR_USED collecting
;
5750 /* This routine, called from the jump pad (in asm) is designed to be
5751 called from the jump pads of fast tracepoints, thus it is on the
5754 IP_AGENT_EXPORT
void ATTR_USED
5755 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5757 struct fast_tracepoint_ctx ctx
;
5759 /* Don't do anything until the trace run is completely set up. */
5763 ctx
.base
.type
= fast_tracepoint
;
5765 ctx
.regcache_initted
= 0;
5766 /* Wrap the regblock in a register cache (in the stack, we don't
5767 want to malloc here). */
5768 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
5769 if (ctx
.regspace
== NULL
)
5771 trace_debug ("Trace buffer block allocation failed, skipping");
5775 for (ctx
.tpoint
= tpoint
;
5776 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5777 ctx
.tpoint
= ctx
.tpoint
->next
)
5779 if (!ctx
.tpoint
->enabled
)
5782 /* Multiple tracepoints of different types, such as fast tracepoint and
5783 static tracepoint, can be set at the same address. */
5784 if (ctx
.tpoint
->type
!= tpoint
->type
)
5787 /* Test the condition if present, and collect if true. */
5788 if (ctx
.tpoint
->cond
== NULL
5789 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5792 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5793 ctx
.tpoint
->address
, ctx
.tpoint
);
5795 /* Note that this will cause original insns to be written back
5796 to where we jumped from, but that's OK because we're jumping
5797 back to the next whole instruction. This will go badly if
5798 instruction restoration is not atomic though. */
5799 if (stopping_tracepoint
5800 || trace_buffer_is_full
5801 || expr_eval_result
!= expr_eval_no_error
)
5809 /* If there was a condition and it evaluated to false, the only
5810 way we would stop tracing is if there was an error during
5811 condition expression evaluation. */
5812 if (expr_eval_result
!= expr_eval_no_error
)
5823 #ifndef IN_PROCESS_AGENT
5826 get_raw_reg_func_addr (void)
5828 return ipa_sym_addrs
.addr_get_raw_reg
;
5832 get_get_tsv_func_addr (void)
5834 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5838 get_set_tsv_func_addr (void)
5840 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5844 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5845 CORE_ADDR
*jump_entry
)
5847 CORE_ADDR entry_point
= *jump_entry
;
5848 enum eval_result_type err
;
5850 trace_debug ("Starting condition compilation for tracepoint %d\n",
5853 /* Initialize the global pointer to the code being built. */
5854 current_insn_ptr
= *jump_entry
;
5858 err
= compile_bytecodes (tpoint
->cond
);
5860 if (err
== expr_eval_no_error
)
5864 /* Record the beginning of the compiled code. */
5865 tpoint
->compiled_cond
= entry_point
;
5867 trace_debug ("Condition compilation for tracepoint %d complete\n",
5872 /* Leave the unfinished code in situ, but don't point to it. */
5874 tpoint
->compiled_cond
= 0;
5876 trace_debug ("Condition compilation for tracepoint %d failed, "
5878 tpoint
->number
, err
);
5881 /* Update the code pointer passed in. Note that we do this even if
5882 the compile fails, so that we can look at the partial results
5883 instead of letting them be overwritten. */
5884 *jump_entry
= current_insn_ptr
;
5886 /* Leave a gap, to aid dump decipherment. */
5890 /* We'll need to adjust these when we consider bi-arch setups, and big
5894 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5896 return write_inferior_memory (where
,
5897 (unsigned char *) &ptr
, sizeof (void *));
5900 /* The base pointer of the IPA's heap. This is the only memory the
5901 IPA is allowed to use. The IPA should _not_ call the inferior's
5902 `malloc' during operation. That'd be slow, and, most importantly,
5903 it may not be safe. We may be collecting a tracepoint in a signal
5904 handler, for example. */
5905 static CORE_ADDR target_tp_heap
;
5907 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5911 target_malloc (ULONGEST size
)
5915 if (target_tp_heap
== 0)
5917 /* We have the pointer *address*, need what it points to. */
5918 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5920 fatal ("could get target heap head pointer");
5923 ptr
= target_tp_heap
;
5924 target_tp_heap
+= size
;
5926 /* Pad to 8-byte alignment. */
5927 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5933 download_agent_expr (struct agent_expr
*expr
)
5935 CORE_ADDR expr_addr
;
5936 CORE_ADDR expr_bytes
;
5938 expr_addr
= target_malloc (sizeof (*expr
));
5939 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5941 expr_bytes
= target_malloc (expr
->length
);
5942 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5944 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5949 /* Align V up to N bits. */
5950 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5952 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5955 download_tracepoint_1 (struct tracepoint
*tpoint
)
5957 struct tracepoint target_tracepoint
;
5958 CORE_ADDR tpptr
= 0;
5960 gdb_assert (tpoint
->type
== fast_tracepoint
5961 || tpoint
->type
== static_tracepoint
);
5963 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5965 CORE_ADDR jentry
, jump_entry
;
5967 jentry
= jump_entry
= get_jump_space_head ();
5969 if (tpoint
->cond
!= NULL
)
5971 /* Pad to 8-byte alignment. (needed?) */
5972 /* Actually this should be left for the target to
5974 jentry
= UALIGN (jentry
, 8);
5976 compile_tracepoint_condition (tpoint
, &jentry
);
5979 /* Pad to 8-byte alignment. */
5980 jentry
= UALIGN (jentry
, 8);
5981 claim_jump_space (jentry
- jump_entry
);
5984 target_tracepoint
= *tpoint
;
5986 tpptr
= target_malloc (sizeof (*tpoint
));
5987 tpoint
->obj_addr_on_target
= tpptr
;
5989 /* Write the whole object. We'll fix up its pointers in a bit.
5990 Assume no next for now. This is fixed up above on the next
5991 iteration, if there's any. */
5992 target_tracepoint
.next
= NULL
;
5993 /* Need to clear this here too, since we're downloading the
5994 tracepoints before clearing our own copy. */
5995 target_tracepoint
.hit_count
= 0;
5997 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5998 sizeof (target_tracepoint
));
6001 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6003 download_agent_expr (tpoint
->cond
));
6005 if (tpoint
->numactions
)
6008 CORE_ADDR actions_array
;
6010 /* The pointers array. */
6012 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6013 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6017 /* Now for each pointer, download the action. */
6018 for (i
= 0; i
< tpoint
->numactions
; i
++)
6020 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6021 CORE_ADDR ipa_action
= action
->ops
->download (action
);
6023 if (ipa_action
!= 0)
6024 write_inferior_data_ptr
6025 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6031 #define IPA_PROTO_FAST_TRACE_FLAG 0
6032 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6033 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6034 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6035 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6037 /* Send a command to agent to download and install tracepoint TPOINT. */
6040 tracepoint_send_agent (struct tracepoint
*tpoint
)
6042 char buf
[IPA_CMD_BUF_SIZE
];
6047 strcpy (p
, "FastTrace:");
6050 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6051 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6052 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6053 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6054 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6055 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6056 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6057 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6058 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6059 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6060 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6063 p
= agent_expr_send (p
, tpoint
->cond
);
6065 /* tracepoint_action */
6066 for (i
= 0; i
< tpoint
->numactions
; i
++)
6068 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6070 p
[0] = action
->type
;
6071 p
= action
->ops
->send (&p
[1], action
);
6074 get_jump_space_head ();
6075 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6076 agent can use jump pad from it. */
6077 if (tpoint
->type
== fast_tracepoint
)
6079 memcpy (p
, &gdb_jump_pad_head
, 8);
6083 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6087 if (strncmp (buf
, "OK", 2) != 0)
6090 /* The value of tracepoint's target address is stored in BUF. */
6091 memcpy (&tpoint
->obj_addr_on_target
,
6092 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6094 if (tpoint
->type
== fast_tracepoint
)
6097 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6100 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6101 (unsigned int) tpoint
->obj_addr_on_target
,
6102 (unsigned int) gdb_jump_pad_head
);
6104 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6106 /* This has been done in agent. We should also set up record for it. */
6107 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6110 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6117 download_tracepoint (struct tracepoint
*tpoint
)
6119 struct tracepoint
*tp
, *tp_prev
;
6121 if (tpoint
->type
!= fast_tracepoint
6122 && tpoint
->type
!= static_tracepoint
)
6125 download_tracepoint_1 (tpoint
);
6127 /* Find the previous entry of TPOINT, which is fast tracepoint or
6128 static tracepoint. */
6130 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6132 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6138 CORE_ADDR tp_prev_target_next_addr
;
6140 /* Insert TPOINT after TP_PREV in IPA. */
6141 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6142 + offsetof (struct tracepoint
, next
),
6143 &tp_prev_target_next_addr
))
6144 fatal ("error reading `tp_prev->next'");
6146 /* tpoint->next = tp_prev->next */
6147 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6148 + offsetof (struct tracepoint
, next
),
6149 tp_prev_target_next_addr
);
6150 /* tp_prev->next = tpoint */
6151 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6152 + offsetof (struct tracepoint
, next
),
6153 tpoint
->obj_addr_on_target
);
6156 /* First object in list, set the head pointer in the
6158 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6159 tpoint
->obj_addr_on_target
);
6164 download_trace_state_variables (void)
6166 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6167 struct trace_state_variable
*tsv
;
6169 /* Start out empty. */
6170 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6172 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6174 struct trace_state_variable target_tsv
;
6176 /* TSV's with a getter have been initialized equally in both the
6177 inferior and GDBserver. Skip them. */
6178 if (tsv
->getter
!= NULL
)
6184 ptr
= target_malloc (sizeof (*tsv
));
6186 if (tsv
== trace_state_variables
)
6188 /* First object in list, set the head pointer in the
6191 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6196 write_inferior_data_ptr (prev_ptr
6197 + offsetof (struct trace_state_variable
,
6202 /* Write the whole object. We'll fix up its pointers in a bit.
6203 Assume no next, fixup when needed. */
6204 target_tsv
.next
= NULL
;
6206 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6207 sizeof (target_tsv
));
6209 if (tsv
->name
!= NULL
)
6211 size_t size
= strlen (tsv
->name
) + 1;
6212 CORE_ADDR name_addr
= target_malloc (size
);
6213 write_inferior_memory (name_addr
,
6214 (unsigned char *) tsv
->name
, size
);
6215 write_inferior_data_ptr (ptr
6216 + offsetof (struct trace_state_variable
,
6221 if (tsv
->getter
!= NULL
)
6223 fatal ("what to do with these?");
6229 /* Fixup the next pointer in the last item in the list. */
6230 write_inferior_data_ptr (prev_ptr
6231 + offsetof (struct trace_state_variable
,
6236 /* Upload complete trace frames out of the IP Agent's trace buffer
6237 into GDBserver's trace buffer. This always uploads either all or
6238 no trace frames. This is the counter part of
6239 `trace_alloc_trace_buffer'. See its description of the atomic
6240 synching mechanism. */
6243 upload_fast_traceframes (void)
6245 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6246 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6248 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6249 unsigned int curr_tbctrl_idx
;
6250 unsigned int ipa_trace_buffer_ctrl_curr
;
6251 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6252 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6253 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6254 CORE_ADDR ipa_trace_buffer_lo
;
6255 CORE_ADDR ipa_trace_buffer_hi
;
6257 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6258 &ipa_traceframe_read_count_racy
))
6260 /* This will happen in most targets if the current thread is
6265 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6266 &ipa_traceframe_write_count_racy
))
6269 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6270 ipa_traceframe_write_count_racy
6271 - ipa_traceframe_read_count_racy
,
6272 ipa_traceframe_write_count_racy
,
6273 ipa_traceframe_read_count_racy
);
6275 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6278 about_to_request_buffer_space_bkpt
6279 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6282 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6283 &ipa_trace_buffer_ctrl_curr
))
6286 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6288 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6291 unsigned int prev
, counter
;
6293 /* Update the token, with new counters, and the GDBserver stamp
6294 bit. Alway reuse the current TBC index. */
6295 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6296 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6298 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6304 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6305 ipa_trace_buffer_ctrl_curr
))
6308 trace_debug ("Lib: Committed %08x -> %08x",
6309 ipa_trace_buffer_ctrl_curr_old
,
6310 ipa_trace_buffer_ctrl_curr
);
6312 /* Re-read these, now that we've installed the
6313 `about_to_request_buffer_space' breakpoint/lock. A thread could
6314 have finished a traceframe between the last read of these
6315 counters and setting the breakpoint above. If we start
6316 uploading, we never want to leave this function with
6317 traceframe_read_count != 0, otherwise, GDBserver could end up
6318 incrementing the counter tokens more than once (due to event loop
6319 nesting), which would break the IP agent's "effective" detection
6320 (see trace_alloc_trace_buffer). */
6321 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6322 &ipa_traceframe_read_count
))
6324 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6325 &ipa_traceframe_write_count
))
6330 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6331 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6332 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6334 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6335 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6336 trace_debug ("note that ipa_traceframe_count's parts changed");
6339 /* Get the address of the current TBC object (the IP agent has an
6340 array of 3 such objects). The index is stored in the TBC
6342 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6343 ipa_trace_buffer_ctrl_addr
6344 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6346 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6347 (unsigned char *) &ipa_trace_buffer_ctrl
,
6348 sizeof (struct ipa_trace_buffer_control
)))
6351 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6352 &ipa_trace_buffer_lo
))
6354 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6355 &ipa_trace_buffer_hi
))
6358 /* Offsets are easier to grok for debugging than raw addresses,
6359 especially for the small trace buffer sizes that are useful for
6361 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6362 "endfree=%d wrap=%d hi=%d",
6364 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6365 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6366 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6367 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6368 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6370 /* Note that the IPA's buffer is always circular. */
6372 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6374 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6375 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6377 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6378 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6379 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6380 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6383 tf
= IPA_FIRST_TRACEFRAME ();
6385 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6387 struct tracepoint
*tpoint
;
6388 struct traceframe
*tframe
;
6389 unsigned char *block
;
6390 struct traceframe ipa_tframe
;
6392 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6393 offsetof (struct traceframe
, data
)))
6394 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6396 if (ipa_tframe
.tpnum
== 0)
6397 fatal ("Uploading: No (more) fast traceframes, but "
6398 "ipa_traceframe_count == %u??\n",
6399 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6401 /* Note that this will be incorrect for multi-location
6403 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6405 tframe
= add_traceframe (tpoint
);
6408 trace_buffer_is_full
= 1;
6409 trace_debug ("Uploading: trace buffer is full");
6413 /* Copy the whole set of blocks in one go for now. FIXME:
6414 split this in smaller blocks. */
6415 block
= add_traceframe_block (tframe
, tpoint
,
6416 ipa_tframe
.data_size
);
6419 if (read_inferior_memory (tf
6420 + offsetof (struct traceframe
, data
),
6421 block
, ipa_tframe
.data_size
))
6422 error ("Uploading: Couldn't read traceframe data at %s\n",
6423 paddress (tf
+ offsetof (struct traceframe
, data
)));
6426 trace_debug ("Uploading: traceframe didn't fit");
6427 finish_traceframe (tframe
);
6430 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6432 /* If we freed the traceframe that wrapped around, go back
6433 to the non-wrap case. */
6434 if (tf
< ipa_trace_buffer_ctrl
.start
)
6436 trace_debug ("Lib: Discarding past the wraparound");
6437 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6439 ipa_trace_buffer_ctrl
.start
= tf
;
6440 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6441 ++ipa_traceframe_read_count
;
6443 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6444 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6446 trace_debug ("Lib: buffer is fully empty. "
6447 "Trace buffer [%d] start=%d free=%d endfree=%d",
6449 (int) (ipa_trace_buffer_ctrl
.start
6450 - ipa_trace_buffer_lo
),
6451 (int) (ipa_trace_buffer_ctrl
.free
6452 - ipa_trace_buffer_lo
),
6453 (int) (ipa_trace_buffer_ctrl
.end_free
6454 - ipa_trace_buffer_lo
));
6456 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6457 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6458 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6459 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6462 trace_debug ("Uploaded a traceframe\n"
6463 "Lib: Trace buffer [%d] start=%d free=%d "
6464 "endfree=%d wrap=%d hi=%d",
6466 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6467 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6468 (int) (ipa_trace_buffer_ctrl
.end_free
6469 - ipa_trace_buffer_lo
),
6470 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6471 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6474 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6475 (unsigned char *) &ipa_trace_buffer_ctrl
,
6476 sizeof (struct ipa_trace_buffer_control
)))
6479 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6480 ipa_traceframe_read_count
);
6482 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6485 cancel_breakpoints ();
6487 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6488 about_to_request_buffer_space_bkpt
= NULL
;
6492 if (trace_buffer_is_full
)
6497 #ifdef IN_PROCESS_AGENT
6499 IP_AGENT_EXPORT
int ust_loaded
;
6500 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6504 /* Static tracepoints. */
6506 /* UST puts a "struct tracepoint" in the global namespace, which
6507 conflicts with our tracepoint. Arguably, being a library, it
6508 shouldn't take ownership of such a generic name. We work around it
6510 #define tracepoint ust_tracepoint
6511 #include <ust/ust.h>
6514 extern int serialize_to_text (char *outbuf
, int bufsize
,
6515 const char *fmt
, va_list ap
);
6517 #define GDB_PROBE_NAME "gdb"
6519 /* We dynamically search for the UST symbols instead of linking them
6520 in. This lets the user decide if the application uses static
6521 tracepoints, instead of always pulling libust.so in. This vector
6522 holds pointers to all functions we care about. */
6526 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6527 const char *fmt
, va_list ap
);
6529 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6530 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6532 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6534 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6537 void (*marker_iter_start
) (struct marker_iter
*iter
);
6538 void (*marker_iter_next
) (struct marker_iter
*iter
);
6539 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6540 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6545 /* Cast through typeof to catch incompatible API changes. Since UST
6546 only builds with gcc, we can freely use gcc extensions here
6548 #define GET_UST_SYM(SYM) \
6551 if (ust_ops.SYM == NULL) \
6552 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6553 if (ust_ops.SYM == NULL) \
6557 #define USTF(SYM) ust_ops.SYM
6559 /* Get pointers to all libust.so functions we care about. */
6564 GET_UST_SYM (serialize_to_text
);
6566 GET_UST_SYM (ltt_probe_register
);
6567 GET_UST_SYM (ltt_probe_unregister
);
6568 GET_UST_SYM (ltt_marker_connect
);
6569 GET_UST_SYM (ltt_marker_disconnect
);
6571 GET_UST_SYM (marker_iter_start
);
6572 GET_UST_SYM (marker_iter_next
);
6573 GET_UST_SYM (marker_iter_stop
);
6574 GET_UST_SYM (marker_iter_reset
);
6580 /* Given an UST marker, return the matching gdb static tracepoint.
6581 The match is done by address. */
6583 static struct tracepoint
*
6584 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6586 struct tracepoint
*tpoint
;
6588 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6590 if (tpoint
->type
!= static_tracepoint
)
6593 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6600 /* The probe function we install on lttng/ust markers. Whenever a
6601 probed ust marker is hit, this function is called. This is similar
6602 to gdb_collect, only for static tracepoints, instead of fast
6606 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6607 struct registers
*regs
, void *call_private
,
6608 const char *fmt
, va_list *args
)
6610 struct tracepoint
*tpoint
;
6611 struct static_tracepoint_ctx ctx
;
6613 /* Don't do anything until the trace run is completely set up. */
6616 trace_debug ("gdb_probe: not tracing\n");
6620 ctx
.base
.type
= static_tracepoint
;
6621 ctx
.regcache_initted
= 0;
6626 /* Wrap the regblock in a register cache (in the stack, we don't
6627 want to malloc here). */
6628 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6629 if (ctx
.regspace
== NULL
)
6631 trace_debug ("Trace buffer block allocation failed, skipping");
6635 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6638 trace_debug ("gdb_probe: marker not known: "
6639 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6640 mdata
->location
, mdata
->channel
,
6641 mdata
->name
, mdata
->format
);
6645 if (!tpoint
->enabled
)
6647 trace_debug ("gdb_probe: tracepoint disabled");
6651 ctx
.tpoint
= tpoint
;
6653 trace_debug ("gdb_probe: collecting marker: "
6654 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6655 mdata
->location
, mdata
->channel
,
6656 mdata
->name
, mdata
->format
);
6658 /* Test the condition if present, and collect if true. */
6659 if (tpoint
->cond
== NULL
6660 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6663 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6664 tpoint
->address
, tpoint
);
6666 if (stopping_tracepoint
6667 || trace_buffer_is_full
6668 || expr_eval_result
!= expr_eval_no_error
)
6673 /* If there was a condition and it evaluated to false, the only
6674 way we would stop tracing is if there was an error during
6675 condition expression evaluation. */
6676 if (expr_eval_result
!= expr_eval_no_error
)
6681 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6682 static tracepoint string data. This is a string passed to the
6683 tracing library by the user, at the time of the tracepoint marker
6684 call. E.g., in the UST marker call:
6686 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6688 the collected data is "str FOOBAZ".
6692 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6693 struct traceframe
*tframe
)
6695 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6696 unsigned char *bufspace
;
6699 unsigned short blocklen
;
6703 trace_debug ("Wanted to collect static trace data, "
6704 "but there's no static trace data");
6708 va_copy (copy
, *umd
->args
);
6709 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6712 trace_debug ("Want to collect ust data");
6714 /* 'S' + size + string */
6715 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6716 1 + sizeof (blocklen
) + size
+ 1);
6717 if (bufspace
== NULL
)
6719 trace_debug ("Trace buffer block allocation failed, skipping");
6723 /* Identify a static trace data block. */
6726 blocklen
= size
+ 1;
6727 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6729 va_copy (copy
, *umd
->args
);
6730 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6731 size
+ 1, umd
->fmt
, copy
);
6734 trace_debug ("Storing static tracepoint data in regblock: %s",
6735 bufspace
+ 1 + sizeof (blocklen
));
6738 /* The probe to register with lttng/ust. */
6739 static struct ltt_available_probe gdb_ust_probe
=
6746 #endif /* HAVE_UST */
6747 #endif /* IN_PROCESS_AGENT */
6749 #ifndef IN_PROCESS_AGENT
6751 /* Ask the in-process agent to run a command. Since we don't want to
6752 have to handle the IPA hitting breakpoints while running the
6753 command, we pause all threads, remove all breakpoints, and then set
6754 the helper thread re-running. We communicate with the helper
6755 thread by means of direct memory xfering, and a socket for
6759 run_inferior_command (char *cmd
, int len
)
6762 int pid
= ptid_get_pid (current_ptid
);
6764 trace_debug ("run_inferior_command: running: %s", cmd
);
6767 uninsert_all_breakpoints ();
6769 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6771 reinsert_all_breakpoints ();
6777 #else /* !IN_PROCESS_AGENT */
6779 #include <sys/socket.h>
6782 #ifndef UNIX_PATH_MAX
6783 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6786 /* Where we put the socked used for synchronization. */
6787 #define SOCK_DIR P_tmpdir
6789 /* Thread ID of the helper thread. GDBserver reads this to know which
6790 is the help thread. This is an LWP id on Linux. */
6791 int helper_thread_id
;
6794 init_named_socket (const char *name
)
6797 struct sockaddr_un addr
;
6799 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6802 warning ("socket creation failed: %s", strerror (errno
));
6806 addr
.sun_family
= AF_UNIX
;
6808 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6809 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6811 result
= access (name
, F_OK
);
6815 result
= unlink (name
);
6818 warning ("unlink failed: %s", strerror (errno
));
6822 warning ("socket %s already exists; overwriting", name
);
6825 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6828 warning ("bind failed: %s", strerror (errno
));
6833 result
= listen (fd
, 1);
6836 warning ("listen: %s", strerror (errno
));
6844 static char agent_socket_name
[UNIX_PATH_MAX
];
6847 gdb_agent_socket_init (void)
6851 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6852 SOCK_DIR
, getpid ());
6853 if (result
>= UNIX_PATH_MAX
)
6855 trace_debug ("string overflow allocating socket name");
6859 fd
= init_named_socket (agent_socket_name
);
6861 warning ("Error initializing named socket (%s) for communication with the "
6862 "ust helper thread. Check that directory exists and that it "
6863 "is writable.", agent_socket_name
);
6870 /* The next marker to be returned on a qTsSTM command. */
6871 static const struct marker
*next_st
;
6873 /* Returns the first known marker. */
6878 struct marker_iter iter
;
6880 USTF(marker_iter_reset
) (&iter
);
6881 USTF(marker_iter_start
) (&iter
);
6886 /* Returns the marker following M. */
6888 const struct marker
*
6889 next_marker (const struct marker
*m
)
6891 struct marker_iter iter
;
6893 USTF(marker_iter_reset
) (&iter
);
6894 USTF(marker_iter_start
) (&iter
);
6896 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6898 if (iter
.marker
== m
)
6900 USTF(marker_iter_next
) (&iter
);
6908 /* Return an hexstr version of the STR C string, fit for sending to
6912 cstr_to_hexstr (const char *str
)
6914 int len
= strlen (str
);
6915 char *hexstr
= xmalloc (len
* 2 + 1);
6916 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6920 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6924 response_ust_marker (char *packet
, const struct marker
*st
)
6926 char *strid
, *format
, *tmp
;
6928 next_st
= next_marker (st
);
6930 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6931 strlen (st
->name
) + 1);
6932 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6934 strid
= cstr_to_hexstr (tmp
);
6937 format
= cstr_to_hexstr (st
->format
);
6939 sprintf (packet
, "m%s:%s:%s",
6940 paddress ((uintptr_t) st
->location
),
6948 /* Return the first static tracepoint, and initialize the state
6949 machine that will iterate through all the static tracepoints. */
6952 cmd_qtfstm (char *packet
)
6954 trace_debug ("Returning first trace state variable definition");
6956 if (first_marker ())
6957 response_ust_marker (packet
, first_marker ());
6959 strcpy (packet
, "l");
6962 /* Return additional trace state variable definitions. */
6965 cmd_qtsstm (char *packet
)
6967 trace_debug ("Returning static tracepoint");
6970 response_ust_marker (packet
, next_st
);
6972 strcpy (packet
, "l");
6975 /* Disconnect the GDB probe from a marker at a given address. */
6978 unprobe_marker_at (char *packet
)
6982 struct marker_iter iter
;
6984 p
+= sizeof ("unprobe_marker_at:") - 1;
6986 p
= unpack_varlen_hex (p
, &address
);
6988 USTF(marker_iter_reset
) (&iter
);
6989 USTF(marker_iter_start
) (&iter
);
6990 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6991 if ((uintptr_t ) iter
.marker
->location
== address
)
6995 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6999 warning ("could not disable marker %s/%s",
7000 iter
.marker
->channel
, iter
.marker
->name
);
7005 /* Connect the GDB probe to a marker at a given address. */
7008 probe_marker_at (char *packet
)
7012 struct marker_iter iter
;
7015 p
+= sizeof ("probe_marker_at:") - 1;
7017 p
= unpack_varlen_hex (p
, &address
);
7019 USTF(marker_iter_reset
) (&iter
);
7021 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7023 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7024 if ((uintptr_t ) m
->location
== address
)
7028 trace_debug ("found marker for address. "
7029 "ltt_marker_connect (marker = %s/%s)",
7030 m
->channel
, m
->name
);
7032 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7034 if (result
&& result
!= -EEXIST
)
7035 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7036 m
->channel
, m
->name
, -result
);
7040 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7041 m
->channel
, m
->name
);
7045 strcpy (packet
, "OK");
7049 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7054 cmd_qtstmat (char *packet
)
7058 struct marker_iter iter
;
7061 p
+= sizeof ("qTSTMat:") - 1;
7063 p
= unpack_varlen_hex (p
, &address
);
7065 USTF(marker_iter_reset
) (&iter
);
7067 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7069 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7070 if ((uintptr_t ) m
->location
== address
)
7072 response_ust_marker (packet
, m
);
7076 strcpy (packet
, "l");
7086 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7089 #endif /* HAVE_UST */
7091 #include <sys/syscall.h>
7095 gdb_agent_remove_socket (void)
7097 unlink (agent_socket_name
);
7100 /* Helper thread of agent. */
7103 gdb_agent_helper_thread (void *arg
)
7107 atexit (gdb_agent_remove_socket
);
7111 listen_fd
= gdb_agent_socket_init ();
7113 if (helper_thread_id
== 0)
7114 helper_thread_id
= syscall (SYS_gettid
);
7116 if (listen_fd
== -1)
7118 warning ("could not create sync socket\n");
7125 struct sockaddr_un sockaddr
;
7131 tmp
= sizeof (sockaddr
);
7135 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7137 /* It seems an ERESTARTSYS can escape out of accept. */
7138 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7142 warning ("Accept returned %d, error: %s\n",
7143 fd
, strerror (errno
));
7149 ret
= read (fd
, buf
, 1);
7150 } while (ret
== -1 && errno
== EINTR
);
7154 warning ("reading socket (fd=%d) failed with %s",
7155 fd
, strerror (errno
));
7162 if (strncmp ("close", cmd_buf
, 5) == 0)
7167 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7169 cmd_qtfstm (cmd_buf
);
7171 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7173 cmd_qtsstm (cmd_buf
);
7175 else if (strncmp ("unprobe_marker_at:",
7177 sizeof ("unprobe_marker_at:") - 1) == 0)
7179 unprobe_marker_at (cmd_buf
);
7181 else if (strncmp ("probe_marker_at:",
7183 sizeof ("probe_marker_at:") - 1) == 0)
7185 probe_marker_at (cmd_buf
);
7187 else if (strncmp ("qTSTMat:",
7189 sizeof ("qTSTMat:") - 1) == 0)
7191 cmd_qtstmat (cmd_buf
);
7193 #endif /* HAVE_UST */
7196 /* Fix compiler's warning: ignoring return value of 'write'. */
7197 ret
= write (fd
, buf
, 1);
7203 unlink (agent_socket_name
);
7205 /* Sleep endlessly to wait the whole inferior stops. This
7206 thread can not exit because GDB or GDBserver may still need
7207 'current_inferior' (representing this thread) to access
7208 inferior memory. Otherwise, this thread exits earlier than
7209 other threads, and 'current_inferior' is set to NULL. */
7220 #include <pthread.h>
7222 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7225 gdb_agent_init (void)
7232 /* We want the helper thread to be as transparent as possible, so
7233 have it inherit an all-signals-blocked mask. */
7235 sigfillset (&new_mask
);
7236 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7238 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7240 res
= pthread_create (&thread
,
7242 gdb_agent_helper_thread
,
7245 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7247 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7249 while (helper_thread_id
== 0)
7257 #include <sys/mman.h>
7260 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7261 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7262 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7263 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7264 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7265 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7267 /* Record the result of getting buffer space for fast tracepoint
7268 trampolines. Any error message is copied, since caller may not be
7269 using persistent storage. */
7272 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7274 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7275 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7277 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7279 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7282 static void __attribute__ ((constructor
))
7283 initialize_tracepoint_ftlib (void)
7285 initialize_tracepoint ();
7290 #endif /* IN_PROCESS_AGENT */
7292 /* Return a timestamp, expressed as microseconds of the usual Unix
7293 time. (As the result is a 64-bit number, it will not overflow any
7297 get_timestamp (void)
7301 if (gettimeofday (&tv
, 0) != 0)
7304 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7308 initialize_tracepoint (void)
7310 /* Start with the default size. */
7311 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7313 /* Wire trace state variable 1 to be the timestamp. This will be
7314 uploaded to GDB upon connection and become one of its trace state
7315 variables. (In case you're wondering, if GDB already has a trace
7316 variable numbered 1, it will be renumbered.) */
7317 create_trace_state_variable (1, 0);
7318 set_trace_state_variable_name (1, "trace_timestamp");
7319 set_trace_state_variable_getter (1, get_timestamp
);
7321 #ifdef IN_PROCESS_AGENT
7326 pagesize
= sysconf (_SC_PAGE_SIZE
);
7330 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7332 #define SCRATCH_BUFFER_NPAGES 20
7334 /* Allocate scratch buffer aligned on a page boundary, at a low
7335 address (close to the main executable's code). */
7336 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7338 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7339 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7340 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7342 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7348 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7351 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7354 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7356 /* It's not a fatal error for something to go wrong with trampoline
7357 buffer setup, but it can be mysterious, so create a channel to
7358 report back on what went wrong, using a fixed size since we may
7359 not be able to allocate space later when the problem occurs. */
7360 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7362 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7364 initialize_low_tracepoint ();