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 traceframe to be used as the source of data to send back to
986 GDB. A value of -1 means to get data from the live program. */
988 int current_traceframe
= -1;
990 /* This flag is true if the trace buffer is circular, meaning that
991 when it fills, the oldest trace frames are discarded in order to
994 #ifndef IN_PROCESS_AGENT
995 static int circular_trace_buffer
;
998 /* Size of the trace buffer. */
1000 static LONGEST trace_buffer_size
;
1002 /* Pointer to the block of memory that traceframes all go into. */
1004 static unsigned char *trace_buffer_lo
;
1006 /* Pointer to the end of the trace buffer, more precisely to the byte
1007 after the end of the buffer. */
1009 static unsigned char *trace_buffer_hi
;
1011 /* Control structure holding the read/write/etc. pointers into the
1012 trace buffer. We need more than one of these to implement a
1013 transaction-like mechanism to garantees that both GDBserver and the
1014 in-process agent can try to change the trace buffer
1017 struct trace_buffer_control
1019 /* Pointer to the first trace frame in the buffer. In the
1020 non-circular case, this is equal to trace_buffer_lo, otherwise it
1021 moves around in the buffer. */
1022 unsigned char *start
;
1024 /* Pointer to the free part of the trace buffer. Note that we clear
1025 several bytes at and after this pointer, so that traceframe
1026 scans/searches terminate properly. */
1027 unsigned char *free
;
1029 /* Pointer to the byte after the end of the free part. Note that
1030 this may be smaller than trace_buffer_free in the circular case,
1031 and means that the free part is in two pieces. Initially it is
1032 equal to trace_buffer_hi, then is generally equivalent to
1033 trace_buffer_start. */
1034 unsigned char *end_free
;
1036 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1037 this is the point at which the trace data breaks, and resumes at
1039 unsigned char *wrap
;
1042 /* Same as above, to be used by GDBserver when updating the in-process
1044 struct ipa_trace_buffer_control
1053 /* We have possibly both GDBserver and an inferior thread accessing
1054 the same IPA trace buffer memory. The IPA is the producer (tries
1055 to put new frames in the buffer), while GDBserver occasionally
1056 consumes them, that is, flushes the IPA's buffer into its own
1057 buffer. Both sides need to update the trace buffer control
1058 pointers (current head, tail, etc.). We can't use a global lock to
1059 synchronize the accesses, as otherwise we could deadlock GDBserver
1060 (if the thread holding the lock stops for a signal, say). So
1061 instead of that, we use a transaction scheme where GDBserver writes
1062 always prevail over the IPAs writes, and, we have the IPA detect
1063 the commit failure/overwrite, and retry the whole attempt. This is
1064 mainly implemented by having a global token object that represents
1065 who wrote last to the buffer control structure. We need to freeze
1066 any inferior writing to the buffer while GDBserver touches memory,
1067 so that the inferior can correctly detect that GDBserver had been
1068 there, otherwise, it could mistakingly think its commit was
1069 successful; that's implemented by simply having GDBserver set a
1070 breakpoint the inferior hits if it is the critical region.
1072 There are three cycling trace buffer control structure copies
1073 (buffer head, tail, etc.), with the token object including an index
1074 indicating which is current live copy. The IPA tentatively builds
1075 an updated copy in a non-current control structure, while GDBserver
1076 always clobbers the current version directly. The IPA then tries
1077 to atomically "commit" its version; if GDBserver clobbered the
1078 structure meanwhile, that will fail, and the IPA restarts the
1081 Listing the step in further detail, we have:
1083 In-process agent (producer):
1085 - passes by `about_to_request_buffer_space' breakpoint/lock
1087 - reads current token, extracts current trace buffer control index,
1088 and starts tentatively updating the rightmost one (0->1, 1->2,
1089 2->0). Note that only one inferior thread is executing this code
1090 at any given time, due to an outer lock in the jump pads.
1092 - updates counters, and tries to commit the token.
1094 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1095 leaving the sync region.
1097 - checks if the update was effective.
1099 - if trace buffer was found full, hits flush_trace_buffer
1100 breakpoint, and restarts later afterwards.
1102 GDBserver (consumer):
1104 - sets `about_to_request_buffer_space' breakpoint/lock.
1106 - updates the token unconditionally, using the current buffer
1107 control index, since it knows that the IP agent always writes to
1108 the rightmost, and due to the breakpoint, at most one IP thread
1109 can try to update the trace buffer concurrently to GDBserver, so
1110 there will be no danger of trace buffer control index wrap making
1111 the IPA write to the same index as GDBserver.
1113 - flushes the IP agent's trace buffer completely, and updates the
1114 current trace buffer control structure. GDBserver *always* wins.
1116 - removes the `about_to_request_buffer_space' breakpoint.
1118 The token is stored in the `trace_buffer_ctrl_curr' variable.
1119 Internally, it's bits are defined as:
1121 |-------------+-----+-------------+--------+-------------+--------------|
1122 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1123 |-------------+-----+-------------+--------+-------------+--------------|
1124 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1125 |-------------+-----+-------------+--------+-------------+--------------|
1127 GSB - GDBserver Stamp Bit
1128 PC - Previous Counter
1129 CC - Current Counter
1130 TBCI - Trace Buffer Control Index
1133 An IPA update of `trace_buffer_ctrl_curr' does:
1135 - read CC from the current token, save as PC.
1137 - atomically tries to write PC+1,CC
1139 A GDBserver update of `trace_buffer_ctrl_curr' does:
1141 - reads PC and CC from the current token.
1146 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1147 for the counters described below. The cleared bits are used to
1148 hold the index of the items of the `trace_buffer_ctrl' array that
1150 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1152 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1153 counter, and the `current' counter. */
1155 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1156 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1158 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1159 always stamps this bit as set. */
1160 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1162 #ifdef IN_PROCESS_AGENT
1163 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1164 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1166 # define TRACE_BUFFER_CTRL_CURR \
1167 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1171 /* The GDBserver side agent only needs one instance of this object, as
1172 it doesn't need to sync with itself. Define it as array anyway so
1173 that the rest of the code base doesn't need to care for the
1175 struct trace_buffer_control trace_buffer_ctrl
[1];
1176 # define TRACE_BUFFER_CTRL_CURR 0
1179 /* These are convenience macros used to access the current trace
1180 buffer control in effect. */
1181 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1182 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1183 #define trace_buffer_end_free \
1184 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1185 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1188 /* Macro that returns a pointer to the first traceframe in the buffer. */
1190 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1192 /* Macro that returns a pointer to the next traceframe in the buffer.
1193 If the computed location is beyond the wraparound point, subtract
1194 the offset of the wraparound. */
1196 #define NEXT_TRACEFRAME_1(TF) \
1197 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1199 #define NEXT_TRACEFRAME(TF) \
1200 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1201 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1202 ? (trace_buffer_wrap - trace_buffer_lo) \
1205 /* The difference between these counters represents the total number
1206 of complete traceframes present in the trace buffer. The IP agent
1207 writes to the write count, GDBserver writes to read count. */
1209 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1210 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1212 /* Convenience macro. */
1214 #define traceframe_count \
1215 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1217 /* The count of all traceframes created in the current run, including
1218 ones that were discarded to make room. */
1220 IP_AGENT_EXPORT
int traceframes_created
;
1222 #ifndef IN_PROCESS_AGENT
1224 /* Read-only regions are address ranges whose contents don't change,
1225 and so can be read from target memory even while looking at a trace
1226 frame. Without these, disassembly for instance will likely fail,
1227 because the program code is not usually collected into a trace
1228 frame. This data structure does not need to be very complicated or
1229 particularly efficient, it's only going to be used occasionally,
1230 and only by some commands. */
1232 struct readonly_region
1234 /* The bounds of the region. */
1235 CORE_ADDR start
, end
;
1237 /* Link to the next one. */
1238 struct readonly_region
*next
;
1241 /* Linked list of readonly regions. This list stays in effect from
1242 one tstart to the next. */
1244 static struct readonly_region
*readonly_regions
;
1248 /* The global that controls tracing overall. */
1250 IP_AGENT_EXPORT
int tracing
;
1252 #ifndef IN_PROCESS_AGENT
1254 /* Controls whether tracing should continue after GDB disconnects. */
1256 int disconnected_tracing
;
1258 /* The reason for the last tracing run to have stopped. We initialize
1259 to a distinct string so that GDB can distinguish between "stopped
1260 after running" and "stopped because never run" cases. */
1262 static const char *tracing_stop_reason
= "tnotrun";
1264 static int tracing_stop_tpnum
;
1266 /* 64-bit timestamps for the trace run's start and finish, expressed
1267 in microseconds from the Unix epoch. */
1269 LONGEST tracing_start_time
;
1270 LONGEST tracing_stop_time
;
1272 /* The (optional) user-supplied name of the user that started the run.
1273 This is an arbitrary string, and may be NULL. */
1275 char *tracing_user_name
;
1277 /* Optional user-supplied text describing the run. This is
1278 an arbitrary string, and may be NULL. */
1280 char *tracing_notes
;
1282 /* Optional user-supplied text explaining a tstop command. This is an
1283 arbitrary string, and may be NULL. */
1285 char *tracing_stop_note
;
1289 /* Functions local to this file. */
1291 /* Base "class" for tracepoint type specific data to be passed down to
1292 collect_data_at_tracepoint. */
1293 struct tracepoint_hit_ctx
1295 enum tracepoint_type type
;
1298 #ifdef IN_PROCESS_AGENT
1300 /* Fast/jump tracepoint specific data to be passed down to
1301 collect_data_at_tracepoint. */
1302 struct fast_tracepoint_ctx
1304 struct tracepoint_hit_ctx base
;
1306 struct regcache regcache
;
1307 int regcache_initted
;
1308 unsigned char *regspace
;
1310 unsigned char *regs
;
1311 struct tracepoint
*tpoint
;
1314 /* Static tracepoint specific data to be passed down to
1315 collect_data_at_tracepoint. */
1316 struct static_tracepoint_ctx
1318 struct tracepoint_hit_ctx base
;
1320 /* The regcache corresponding to the registers state at the time of
1321 the tracepoint hit. Initialized lazily, from REGS. */
1322 struct regcache regcache
;
1323 int regcache_initted
;
1325 /* The buffer space REGCACHE above uses. We use a separate buffer
1326 instead of letting the regcache malloc for both signal safety and
1327 performance reasons; this is allocated on the stack instead. */
1328 unsigned char *regspace
;
1330 /* The register buffer as passed on by lttng/ust. */
1331 struct registers
*regs
;
1333 /* The "printf" formatter and the args the user passed to the marker
1334 call. We use this to be able to collect "static trace data"
1339 /* The GDB tracepoint matching the probed marker that was "hit". */
1340 struct tracepoint
*tpoint
;
1345 /* Static tracepoint specific data to be passed down to
1346 collect_data_at_tracepoint. */
1347 struct trap_tracepoint_ctx
1349 struct tracepoint_hit_ctx base
;
1351 struct regcache
*regcache
;
1356 #ifndef IN_PROCESS_AGENT
1357 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1358 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1361 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1362 struct tracepoint
*tpoint
);
1364 #ifndef IN_PROCESS_AGENT
1365 static void clear_readonly_regions (void);
1366 static void clear_installed_tracepoints (void);
1369 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1371 struct tracepoint
*tpoint
);
1372 #ifndef IN_PROCESS_AGENT
1373 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1375 struct tracepoint
*tpoint
, int current_step
);
1376 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1377 CORE_ADDR
*jump_entry
);
1379 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1381 struct tracepoint
*tpoint
,
1382 struct traceframe
*tframe
,
1383 struct tracepoint_action
*taction
);
1385 #ifndef IN_PROCESS_AGENT
1386 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1388 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1389 static void download_tracepoint (struct tracepoint
*);
1390 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1391 static void clone_fast_tracepoint (struct tracepoint
*to
,
1392 const struct tracepoint
*from
);
1395 static LONGEST
get_timestamp (void);
1397 #if defined(__GNUC__)
1398 # define memory_barrier() asm volatile ("" : : : "memory")
1400 # define memory_barrier() do {} while (0)
1403 /* We only build the IPA if this builtin is supported, and there are
1404 no uses of this in GDBserver itself, so we're safe in defining this
1406 #define cmpxchg(mem, oldval, newval) \
1407 __sync_val_compare_and_swap (mem, oldval, newval)
1409 /* Record that an error occurred during expression evaluation. */
1412 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1413 enum eval_result_type rtype
)
1415 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1416 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1418 #ifdef IN_PROCESS_AGENT
1419 /* Only record the first error we get. */
1420 if (cmpxchg (&expr_eval_result
,
1422 rtype
) != expr_eval_no_error
)
1425 if (expr_eval_result
!= expr_eval_no_error
)
1429 error_tracepoint
= tpoint
;
1432 /* Trace buffer management. */
1435 clear_trace_buffer (void)
1437 trace_buffer_start
= trace_buffer_lo
;
1438 trace_buffer_free
= trace_buffer_lo
;
1439 trace_buffer_end_free
= trace_buffer_hi
;
1440 trace_buffer_wrap
= trace_buffer_hi
;
1441 /* A traceframe with zeroed fields marks the end of trace data. */
1442 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1443 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1444 traceframe_read_count
= traceframe_write_count
= 0;
1445 traceframes_created
= 0;
1448 #ifndef IN_PROCESS_AGENT
1451 clear_inferior_trace_buffer (void)
1453 CORE_ADDR ipa_trace_buffer_lo
;
1454 CORE_ADDR ipa_trace_buffer_hi
;
1455 struct traceframe ipa_traceframe
= { 0 };
1456 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1458 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1459 &ipa_trace_buffer_lo
);
1460 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1461 &ipa_trace_buffer_hi
);
1463 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1464 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1465 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1466 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1468 /* A traceframe with zeroed fields marks the end of trace data. */
1469 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1470 (unsigned char *) &ipa_trace_buffer_ctrl
,
1471 sizeof (ipa_trace_buffer_ctrl
));
1473 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1475 /* A traceframe with zeroed fields marks the end of trace data. */
1476 write_inferior_memory (ipa_trace_buffer_lo
,
1477 (unsigned char *) &ipa_traceframe
,
1478 sizeof (ipa_traceframe
));
1480 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1481 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1482 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1488 init_trace_buffer (LONGEST bufsize
)
1490 trace_buffer_size
= bufsize
;
1492 /* If we already have a trace buffer, try realloc'ing. */
1493 trace_buffer_lo
= xrealloc (trace_buffer_lo
, bufsize
);
1495 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1497 clear_trace_buffer ();
1500 #ifdef IN_PROCESS_AGENT
1502 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1503 about_to_request_buffer_space (void)
1505 /* GDBserver places breakpoint here while it goes about to flush
1506 data at random times. */
1507 UNKNOWN_SIDE_EFFECTS();
1512 /* Carve out a piece of the trace buffer, returning NULL in case of
1516 trace_buffer_alloc (size_t amt
)
1518 unsigned char *rslt
;
1519 struct trace_buffer_control
*tbctrl
;
1521 #ifdef IN_PROCESS_AGENT
1522 unsigned int prev
, prev_filtered
;
1523 unsigned int commit_count
;
1524 unsigned int commit
;
1525 unsigned int readout
;
1527 struct traceframe
*oldest
;
1528 unsigned char *new_start
;
1531 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1532 (long) amt
, (long) sizeof (struct traceframe
));
1534 /* Account for the EOB marker. */
1535 amt
+= sizeof (struct traceframe
);
1537 #ifdef IN_PROCESS_AGENT
1541 /* Read the current token and extract the index to try to write to,
1542 storing it in CURR. */
1543 prev
= trace_buffer_ctrl_curr
;
1544 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1545 curr
= prev_filtered
+ 1;
1549 about_to_request_buffer_space ();
1551 /* Start out with a copy of the current state. GDBserver may be
1552 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1553 be able to commit anyway if that happens. */
1554 trace_buffer_ctrl
[curr
]
1555 = trace_buffer_ctrl
[prev_filtered
];
1556 trace_debug ("trying curr=%u", curr
);
1558 /* The GDBserver's agent doesn't need all that syncing, and always
1559 updates TCB 0 (there's only one, mind you). */
1562 tbctrl
= &trace_buffer_ctrl
[curr
];
1564 /* Offsets are easier to grok for debugging than raw addresses,
1565 especially for the small trace buffer sizes that are useful for
1567 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1569 (int) (tbctrl
->start
- trace_buffer_lo
),
1570 (int) (tbctrl
->free
- trace_buffer_lo
),
1571 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1572 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1573 (int) (trace_buffer_hi
- trace_buffer_lo
));
1575 /* The algorithm here is to keep trying to get a contiguous block of
1576 the requested size, possibly discarding older traceframes to free
1577 up space. Since free space might come in one or two pieces,
1578 depending on whether discarded traceframes wrapped around at the
1579 high end of the buffer, we test both pieces after each
1583 /* First, if we have two free parts, try the upper one first. */
1584 if (tbctrl
->end_free
< tbctrl
->free
)
1586 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1587 /* We have enough in the upper part. */
1591 /* Our high part of free space wasn't enough. Give up
1592 on it for now, set wraparound. We will recover the
1593 space later, if/when the wrapped-around traceframe is
1595 trace_debug ("Upper part too small, setting wraparound");
1596 tbctrl
->wrap
= tbctrl
->free
;
1597 tbctrl
->free
= trace_buffer_lo
;
1601 /* The normal case. */
1602 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1605 #ifdef IN_PROCESS_AGENT
1606 /* The IP Agent's buffer is always circular. It isn't used
1607 currently, but `circular_trace_buffer' could represent
1608 GDBserver's mode. If we didn't find space, ask GDBserver to
1611 flush_trace_buffer ();
1615 trace_debug ("gdbserver flushed buffer, retrying");
1619 /* GDBserver cancelled the tracing. Bail out as well. */
1622 /* If we're here, then neither part is big enough, and
1623 non-circular trace buffers are now full. */
1624 if (!circular_trace_buffer
)
1626 trace_debug ("Not enough space in the trace buffer");
1630 trace_debug ("Need more space in the trace buffer");
1632 /* If we have a circular buffer, we can try discarding the
1633 oldest traceframe and see if that helps. */
1634 oldest
= FIRST_TRACEFRAME ();
1635 if (oldest
->tpnum
== 0)
1637 /* Not good; we have no traceframes to free. Perhaps we're
1638 asking for a block that is larger than the buffer? In
1639 any case, give up. */
1640 trace_debug ("No traceframes to discard");
1644 /* We don't run this code in the in-process agent currently.
1645 E.g., we could leave the in-process agent in autonomous
1646 circular mode if we only have fast tracepoints. If we do
1647 that, then this bit becomes racy with GDBserver, which also
1648 writes to this counter. */
1649 --traceframe_write_count
;
1651 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1652 /* If we freed the traceframe that wrapped around, go back
1653 to the non-wrap case. */
1654 if (new_start
< tbctrl
->start
)
1656 trace_debug ("Discarding past the wraparound");
1657 tbctrl
->wrap
= trace_buffer_hi
;
1659 tbctrl
->start
= new_start
;
1660 tbctrl
->end_free
= tbctrl
->start
;
1662 trace_debug ("Discarded a traceframe\n"
1663 "Trace buffer [%d], start=%d free=%d "
1664 "endfree=%d wrap=%d hi=%d",
1666 (int) (tbctrl
->start
- trace_buffer_lo
),
1667 (int) (tbctrl
->free
- trace_buffer_lo
),
1668 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1669 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1670 (int) (trace_buffer_hi
- trace_buffer_lo
));
1672 /* Now go back around the loop. The discard might have resulted
1673 in either one or two pieces of free space, so we want to try
1674 both before freeing any more traceframes. */
1678 /* If we get here, we know we can provide the asked-for space. */
1680 rslt
= tbctrl
->free
;
1682 /* Adjust the request back down, now that we know we have space for
1683 the marker, but don't commit to AMT yet, we may still need to
1684 restart the operation if GDBserver touches the trace buffer
1685 (obviously only important in the in-process agent's version). */
1686 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1688 /* Or not. If GDBserver changed the trace buffer behind our back,
1689 we get to restart a new allocation attempt. */
1691 #ifdef IN_PROCESS_AGENT
1692 /* Build the tentative token. */
1693 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1694 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1695 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1699 /* Try to commit it. */
1700 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1701 if (readout
!= prev
)
1703 trace_debug ("GDBserver has touched the trace buffer, restarting."
1704 " (prev=%08x, commit=%08x, readout=%08x)",
1705 prev
, commit
, readout
);
1709 /* Hold your horses here. Even if that change was committed,
1710 GDBserver could come in, and clobber it. We need to hold to be
1711 able to tell if GDBserver clobbers before or after we committed
1712 the change. Whenever GDBserver goes about touching the IPA
1713 buffer, it sets a breakpoint in this routine, so we have a sync
1715 about_to_request_buffer_space ();
1717 /* Check if the change has been effective, even if GDBserver stopped
1718 us at the breakpoint. */
1721 unsigned int refetch
;
1725 refetch
= trace_buffer_ctrl_curr
;
1727 if (refetch
== commit
1728 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1731 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1732 "readout=%08x, refetch=%08x)",
1733 prev
, commit
, readout
, refetch
);
1737 trace_debug ("GDBserver has touched the trace buffer, not effective."
1738 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1739 prev
, commit
, readout
, refetch
);
1745 /* We have a new piece of the trace buffer. Hurray! */
1747 /* Add an EOB marker just past this allocation. */
1748 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1749 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1751 /* Adjust the request back down, now that we know we have space for
1753 amt
-= sizeof (struct traceframe
);
1757 trace_debug ("Allocated %d bytes", (int) amt
);
1758 trace_debug ("Trace buffer [%d] start=%d free=%d "
1759 "endfree=%d wrap=%d hi=%d",
1761 (int) (tbctrl
->start
- trace_buffer_lo
),
1762 (int) (tbctrl
->free
- trace_buffer_lo
),
1763 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1764 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1765 (int) (trace_buffer_hi
- trace_buffer_lo
));
1771 #ifndef IN_PROCESS_AGENT
1773 /* Return the total free space. This is not necessarily the largest
1774 block we can allocate, because of the two-part case. */
1779 if (trace_buffer_free
<= trace_buffer_end_free
)
1780 return trace_buffer_end_free
- trace_buffer_free
;
1782 return ((trace_buffer_end_free
- trace_buffer_lo
)
1783 + (trace_buffer_hi
- trace_buffer_free
));
1786 /* An 'S' in continuation packets indicates remainder are for
1789 static int seen_step_action_flag
;
1791 /* Create a tracepoint (location) with given number and address. Add this
1792 new tracepoint to list and sort this list. */
1794 static struct tracepoint
*
1795 add_tracepoint (int num
, CORE_ADDR addr
)
1797 struct tracepoint
*tpoint
, **tp_next
;
1799 tpoint
= xmalloc (sizeof (struct tracepoint
));
1800 tpoint
->number
= num
;
1801 tpoint
->address
= addr
;
1802 tpoint
->numactions
= 0;
1803 tpoint
->actions
= NULL
;
1804 tpoint
->actions_str
= NULL
;
1805 tpoint
->cond
= NULL
;
1806 tpoint
->num_step_actions
= 0;
1807 tpoint
->step_actions
= NULL
;
1808 tpoint
->step_actions_str
= NULL
;
1809 /* Start all off as regular (slow) tracepoints. */
1810 tpoint
->type
= trap_tracepoint
;
1811 tpoint
->orig_size
= -1;
1812 tpoint
->source_strings
= NULL
;
1813 tpoint
->compiled_cond
= 0;
1814 tpoint
->handle
= NULL
;
1815 tpoint
->next
= NULL
;
1817 /* Find a place to insert this tracepoint into list in order to keep
1818 the tracepoint list still in the ascending order. There may be
1819 multiple tracepoints at the same address as TPOINT's, and this
1820 guarantees TPOINT is inserted after all the tracepoints which are
1821 set at the same address. For example, fast tracepoints A, B, C are
1822 set at the same address, and D is to be insert at the same place as
1825 -->| A |--> | B |-->| C |->...
1827 One jump pad was created for tracepoint A, B, and C, and the target
1828 address of A is referenced/used in jump pad. So jump pad will let
1829 inferior jump to A. If D is inserted in front of A, like this,
1831 -->| D |-->| A |--> | B |-->| C |->...
1833 without updating jump pad, D is not reachable during collect, which
1834 is wrong. As we can see, the order of B, C and D doesn't matter, but
1835 A should always be the `first' one. */
1836 for (tp_next
= &tracepoints
;
1837 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1838 tp_next
= &(*tp_next
)->next
)
1840 tpoint
->next
= *tp_next
;
1842 last_tracepoint
= tpoint
;
1844 seen_step_action_flag
= 0;
1849 #ifndef IN_PROCESS_AGENT
1851 /* Return the tracepoint with the given number and address, or NULL. */
1853 static struct tracepoint
*
1854 find_tracepoint (int id
, CORE_ADDR addr
)
1856 struct tracepoint
*tpoint
;
1858 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1859 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1865 /* Remove TPOINT from global list. */
1868 remove_tracepoint (struct tracepoint
*tpoint
)
1870 struct tracepoint
*tp
, *tp_prev
;
1872 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1873 tp_prev
= tp
, tp
= tp
->next
)
1879 tp_prev
->next
= tp
->next
;
1881 tracepoints
= tp
->next
;
1887 /* There may be several tracepoints with the same number (because they
1888 are "locations", in GDB parlance); return the next one after the
1889 given tracepoint, or search from the beginning of the list if the
1890 first argument is NULL. */
1892 static struct tracepoint
*
1893 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1895 struct tracepoint
*tpoint
;
1898 tpoint
= prev_tp
->next
;
1900 tpoint
= tracepoints
;
1901 for (; tpoint
; tpoint
= tpoint
->next
)
1902 if (tpoint
->number
== num
)
1910 /* Append another action to perform when the tracepoint triggers. */
1913 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1919 seen_step_action_flag
= 1;
1927 char *act_start
= act
;
1928 struct tracepoint_action
*action
= NULL
;
1934 struct collect_memory_action
*maction
;
1938 maction
= xmalloc (sizeof *maction
);
1939 maction
->base
.type
= *act
;
1940 maction
->base
.ops
= &m_tracepoint_action_ops
;
1941 action
= &maction
->base
;
1944 is_neg
= (*act
== '-');
1947 act
= unpack_varlen_hex (act
, &basereg
);
1949 act
= unpack_varlen_hex (act
, &maction
->addr
);
1951 act
= unpack_varlen_hex (act
, &maction
->len
);
1952 maction
->basereg
= (is_neg
1955 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1956 pulongest (maction
->len
),
1957 paddress (maction
->addr
), maction
->basereg
);
1962 struct collect_registers_action
*raction
;
1964 raction
= xmalloc (sizeof *raction
);
1965 raction
->base
.type
= *act
;
1966 raction
->base
.ops
= &r_tracepoint_action_ops
;
1967 action
= &raction
->base
;
1969 trace_debug ("Want to collect registers");
1971 /* skip past hex digits of mask for now */
1972 while (isxdigit(*act
))
1978 struct collect_static_trace_data_action
*raction
;
1980 raction
= xmalloc (sizeof *raction
);
1981 raction
->base
.type
= *act
;
1982 raction
->base
.ops
= &l_tracepoint_action_ops
;
1983 action
= &raction
->base
;
1985 trace_debug ("Want to collect static trace data");
1990 trace_debug ("Unexpected step action, ignoring");
1995 struct eval_expr_action
*xaction
;
1997 xaction
= xmalloc (sizeof (*xaction
));
1998 xaction
->base
.type
= *act
;
1999 xaction
->base
.ops
= &x_tracepoint_action_ops
;
2000 action
= &xaction
->base
;
2002 trace_debug ("Want to evaluate expression");
2003 xaction
->expr
= gdb_parse_agent_expr (&act
);
2007 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2016 if (seen_step_action_flag
)
2018 tpoint
->num_step_actions
++;
2020 tpoint
->step_actions
2021 = xrealloc (tpoint
->step_actions
,
2022 (sizeof (*tpoint
->step_actions
)
2023 * tpoint
->num_step_actions
));
2024 tpoint
->step_actions_str
2025 = xrealloc (tpoint
->step_actions_str
,
2026 (sizeof (*tpoint
->step_actions_str
)
2027 * tpoint
->num_step_actions
));
2028 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2029 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2030 = savestring (act_start
, act
- act_start
);
2034 tpoint
->numactions
++;
2036 = xrealloc (tpoint
->actions
,
2037 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2039 = xrealloc (tpoint
->actions_str
,
2040 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2041 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2042 tpoint
->actions_str
[tpoint
->numactions
- 1]
2043 = savestring (act_start
, act
- act_start
);
2050 /* Find or create a trace state variable with the given number. */
2052 static struct trace_state_variable
*
2053 get_trace_state_variable (int num
)
2055 struct trace_state_variable
*tsv
;
2057 #ifdef IN_PROCESS_AGENT
2058 /* Search for an existing variable. */
2059 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2060 if (tsv
->number
== num
)
2064 /* Search for an existing variable. */
2065 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2066 if (tsv
->number
== num
)
2072 /* Find or create a trace state variable with the given number. */
2074 static struct trace_state_variable
*
2075 create_trace_state_variable (int num
, int gdb
)
2077 struct trace_state_variable
*tsv
;
2079 tsv
= get_trace_state_variable (num
);
2083 /* Create a new variable. */
2084 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2086 tsv
->initial_value
= 0;
2090 #ifdef IN_PROCESS_AGENT
2093 tsv
->next
= alloced_trace_state_variables
;
2094 alloced_trace_state_variables
= tsv
;
2099 tsv
->next
= trace_state_variables
;
2100 trace_state_variables
= tsv
;
2105 IP_AGENT_EXPORT LONGEST
2106 get_trace_state_variable_value (int num
)
2108 struct trace_state_variable
*tsv
;
2110 tsv
= get_trace_state_variable (num
);
2114 trace_debug ("No trace state variable %d, skipping value get", num
);
2118 /* Call a getter function if we have one. While it's tempting to
2119 set up something to only call the getter once per tracepoint hit,
2120 it could run afoul of thread races. Better to let the getter
2121 handle it directly, if necessary to worry about it. */
2123 tsv
->value
= (tsv
->getter
) ();
2125 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2126 num
, plongest (tsv
->value
));
2131 IP_AGENT_EXPORT
void
2132 set_trace_state_variable_value (int num
, LONGEST val
)
2134 struct trace_state_variable
*tsv
;
2136 tsv
= get_trace_state_variable (num
);
2140 trace_debug ("No trace state variable %d, skipping value set", num
);
2148 agent_get_trace_state_variable_value (int num
)
2150 return get_trace_state_variable_value (num
);
2154 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2156 set_trace_state_variable_value (num
, val
);
2160 set_trace_state_variable_name (int num
, const char *name
)
2162 struct trace_state_variable
*tsv
;
2164 tsv
= get_trace_state_variable (num
);
2168 trace_debug ("No trace state variable %d, skipping name set", num
);
2172 tsv
->name
= (char *) name
;
2176 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2178 struct trace_state_variable
*tsv
;
2180 tsv
= get_trace_state_variable (num
);
2184 trace_debug ("No trace state variable %d, skipping getter set", num
);
2188 tsv
->getter
= getter
;
2191 /* Add a raw traceframe for the given tracepoint. */
2193 static struct traceframe
*
2194 add_traceframe (struct tracepoint
*tpoint
)
2196 struct traceframe
*tframe
;
2198 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2203 tframe
->tpnum
= tpoint
->number
;
2204 tframe
->data_size
= 0;
2209 /* Add a block to the traceframe currently being worked on. */
2211 static unsigned char *
2212 add_traceframe_block (struct traceframe
*tframe
,
2213 struct tracepoint
*tpoint
, int amt
)
2215 unsigned char *block
;
2220 block
= trace_buffer_alloc (amt
);
2225 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2227 tframe
->data_size
+= amt
;
2228 tpoint
->traceframe_usage
+= amt
;
2233 /* Flag that the current traceframe is finished. */
2236 finish_traceframe (struct traceframe
*tframe
)
2238 ++traceframe_write_count
;
2239 ++traceframes_created
;
2242 #ifndef IN_PROCESS_AGENT
2244 /* Given a traceframe number NUM, find the NUMth traceframe in the
2247 static struct traceframe
*
2248 find_traceframe (int num
)
2250 struct traceframe
*tframe
;
2253 for (tframe
= FIRST_TRACEFRAME ();
2255 tframe
= NEXT_TRACEFRAME (tframe
))
2266 get_traceframe_address (struct traceframe
*tframe
)
2269 struct tracepoint
*tpoint
;
2271 addr
= traceframe_get_pc (tframe
);
2276 /* Fallback strategy, will be incorrect for while-stepping frames
2277 and multi-location tracepoints. */
2278 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2279 return tpoint
->address
;
2282 /* Search for the next traceframe whose address is inside or outside
2285 static struct traceframe
*
2286 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2289 struct traceframe
*tframe
;
2292 *tfnump
= current_traceframe
+ 1;
2293 tframe
= find_traceframe (*tfnump
);
2294 /* The search is not supposed to wrap around. */
2301 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2303 tfaddr
= get_traceframe_address (tframe
);
2305 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2306 : (lo
> tfaddr
|| tfaddr
> hi
))
2315 /* Search for the next traceframe recorded by the given tracepoint.
2316 Note that for multi-location tracepoints, this will find whatever
2317 location appears first. */
2319 static struct traceframe
*
2320 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2322 struct traceframe
*tframe
;
2324 *tfnump
= current_traceframe
+ 1;
2325 tframe
= find_traceframe (*tfnump
);
2326 /* The search is not supposed to wrap around. */
2333 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2335 if (tframe
->tpnum
== num
)
2346 #ifndef IN_PROCESS_AGENT
2348 /* Clear all past trace state. */
2351 cmd_qtinit (char *packet
)
2353 struct trace_state_variable
*tsv
, *prev
, *next
;
2355 /* Make sure we don't try to read from a trace frame. */
2356 current_traceframe
= -1;
2360 trace_debug ("Initializing the trace");
2362 clear_installed_tracepoints ();
2363 clear_readonly_regions ();
2366 last_tracepoint
= NULL
;
2368 /* Clear out any leftover trace state variables. Ones with target
2369 defined getters should be kept however. */
2371 tsv
= trace_state_variables
;
2374 trace_debug ("Looking at var %d", tsv
->number
);
2375 if (tsv
->getter
== NULL
)
2381 trace_state_variables
= next
;
2382 trace_debug ("Deleting var %d", tsv
->number
);
2393 clear_trace_buffer ();
2394 clear_inferior_trace_buffer ();
2399 /* Unprobe the UST marker at ADDRESS. */
2402 unprobe_marker_at (CORE_ADDR address
)
2404 char cmd
[IPA_CMD_BUF_SIZE
];
2406 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2407 run_inferior_command (cmd
, strlen (cmd
) + 1);
2410 /* Restore the program to its pre-tracing state. This routine may be called
2411 in error situations, so it needs to be careful about only restoring
2412 from known-valid bits. */
2415 clear_installed_tracepoints (void)
2417 struct tracepoint
*tpoint
;
2418 struct tracepoint
*prev_stpoint
;
2421 cancel_breakpoints ();
2423 prev_stpoint
= NULL
;
2425 /* Restore any bytes overwritten by tracepoints. */
2426 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2428 /* Catch the case where we might try to remove a tracepoint that
2429 was never actually installed. */
2430 if (tpoint
->handle
== NULL
)
2432 trace_debug ("Tracepoint %d at 0x%s was "
2433 "never installed, nothing to clear",
2434 tpoint
->number
, paddress (tpoint
->address
));
2438 switch (tpoint
->type
)
2440 case trap_tracepoint
:
2441 delete_breakpoint (tpoint
->handle
);
2443 case fast_tracepoint
:
2444 delete_fast_tracepoint_jump (tpoint
->handle
);
2446 case static_tracepoint
:
2447 if (prev_stpoint
!= NULL
2448 && prev_stpoint
->address
== tpoint
->address
)
2449 /* Nothing to do. We already unprobed a tracepoint set at
2450 this marker address (and there can only be one probe
2455 unprobe_marker_at (tpoint
->address
);
2456 prev_stpoint
= tpoint
;
2461 tpoint
->handle
= NULL
;
2467 /* Parse a packet that defines a tracepoint. */
2470 cmd_qtdp (char *own_buf
)
2473 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2474 int trail_hyphen
= 0;
2478 struct tracepoint
*tpoint
;
2480 char *packet
= own_buf
;
2482 packet
+= strlen ("QTDP:");
2484 /* A hyphen at the beginning marks a packet specifying actions for a
2485 tracepoint already supplied. */
2492 packet
= unpack_varlen_hex (packet
, &num
);
2493 ++packet
; /* skip a colon */
2494 packet
= unpack_varlen_hex (packet
, &addr
);
2495 ++packet
; /* skip a colon */
2497 /* See if we already have this tracepoint. */
2498 tpoint
= find_tracepoint (num
, addr
);
2502 /* Duplicate tracepoints are never allowed. */
2505 trace_debug ("Tracepoint error: tracepoint %d"
2506 " at 0x%s already exists",
2507 (int) num
, paddress (addr
));
2508 write_enn (own_buf
);
2512 tpoint
= add_tracepoint (num
, addr
);
2514 tpoint
->enabled
= (*packet
== 'E');
2515 ++packet
; /* skip 'E' */
2516 ++packet
; /* skip a colon */
2517 packet
= unpack_varlen_hex (packet
, &count
);
2518 tpoint
->step_count
= count
;
2519 ++packet
; /* skip a colon */
2520 packet
= unpack_varlen_hex (packet
, &count
);
2521 tpoint
->pass_count
= count
;
2522 /* See if we have any of the additional optional fields. */
2523 while (*packet
== ':')
2528 tpoint
->type
= fast_tracepoint
;
2530 packet
= unpack_varlen_hex (packet
, &count
);
2531 tpoint
->orig_size
= count
;
2533 else if (*packet
== 'S')
2535 tpoint
->type
= static_tracepoint
;
2538 else if (*packet
== 'X')
2540 actparm
= (char *) packet
;
2541 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2544 else if (*packet
== '-')
2546 else if (*packet
== '\0')
2549 trace_debug ("Unknown optional tracepoint field");
2554 trace_debug ("Also has actions\n");
2557 trace_debug ("Defined %stracepoint %d at 0x%s, "
2558 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2559 tpoint
->type
== fast_tracepoint
? "fast "
2560 : tpoint
->type
== static_tracepoint
? "static " : "",
2561 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2562 tpoint
->step_count
, tpoint
->pass_count
);
2565 add_tracepoint_action (tpoint
, packet
);
2568 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2569 (int) num
, paddress (addr
));
2570 write_enn (own_buf
);
2574 /* Install tracepoint during tracing only once for each tracepoint location.
2575 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2576 determine the last QTDP packet for one tracepoint location by checking
2577 trailing hyphen in QTDP packet. */
2578 if (tracing
&& !trail_hyphen
)
2580 struct tracepoint
*tp
= NULL
;
2582 /* Pause all threads temporarily while we patch tracepoints. */
2585 /* download_tracepoint will update global `tracepoints'
2586 list, so it is unsafe to leave threads in jump pad. */
2587 stabilize_threads ();
2589 /* Freeze threads. */
2593 if (tpoint
->type
!= trap_tracepoint
)
2595 /* Find another fast or static tracepoint at the same address. */
2596 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2598 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2599 && tp
->number
!= tpoint
->number
)
2603 /* TPOINT is installed at the same address as TP. */
2606 if (tpoint
->type
== fast_tracepoint
)
2607 clone_fast_tracepoint (tpoint
, tp
);
2608 else if (tpoint
->type
== static_tracepoint
)
2609 tpoint
->handle
= (void *) -1;
2613 if (use_agent
&& tpoint
->type
== fast_tracepoint
2614 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2616 /* Download and install fast tracepoint by agent. */
2617 if (tracepoint_send_agent (tpoint
) == 0)
2621 write_enn (own_buf
);
2622 remove_tracepoint (tpoint
);
2627 download_tracepoint (tpoint
);
2629 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2631 install_tracepoint (tpoint
, own_buf
);
2632 if (strcmp (own_buf
, "OK") != 0)
2633 remove_tracepoint (tpoint
);
2647 cmd_qtdpsrc (char *own_buf
)
2649 ULONGEST num
, addr
, start
, slen
;
2650 struct tracepoint
*tpoint
;
2651 char *packet
= own_buf
;
2652 char *saved
, *srctype
, *src
;
2654 struct source_string
*last
, *newlast
;
2656 packet
+= strlen ("QTDPsrc:");
2658 packet
= unpack_varlen_hex (packet
, &num
);
2659 ++packet
; /* skip a colon */
2660 packet
= unpack_varlen_hex (packet
, &addr
);
2661 ++packet
; /* skip a colon */
2663 /* See if we already have this tracepoint. */
2664 tpoint
= find_tracepoint (num
, addr
);
2668 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2669 (int) num
, paddress (addr
));
2670 write_enn (own_buf
);
2675 packet
= strchr (packet
, ':');
2676 srctype
= xmalloc (packet
- saved
+ 1);
2677 memcpy (srctype
, saved
, packet
- saved
);
2678 srctype
[packet
- saved
] = '\0';
2680 packet
= unpack_varlen_hex (packet
, &start
);
2681 ++packet
; /* skip a colon */
2682 packet
= unpack_varlen_hex (packet
, &slen
);
2683 ++packet
; /* skip a colon */
2684 src
= xmalloc (slen
+ 1);
2685 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2688 newlast
= xmalloc (sizeof (struct source_string
));
2689 newlast
->type
= srctype
;
2691 newlast
->next
= NULL
;
2692 /* Always add a source string to the end of the list;
2693 this keeps sequences of actions/commands in the right
2695 if (tpoint
->source_strings
)
2697 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2699 last
->next
= newlast
;
2702 tpoint
->source_strings
= newlast
;
2708 cmd_qtdv (char *own_buf
)
2710 ULONGEST num
, val
, builtin
;
2713 struct trace_state_variable
*tsv
;
2714 char *packet
= own_buf
;
2716 packet
+= strlen ("QTDV:");
2718 packet
= unpack_varlen_hex (packet
, &num
);
2719 ++packet
; /* skip a colon */
2720 packet
= unpack_varlen_hex (packet
, &val
);
2721 ++packet
; /* skip a colon */
2722 packet
= unpack_varlen_hex (packet
, &builtin
);
2723 ++packet
; /* skip a colon */
2725 nbytes
= strlen (packet
) / 2;
2726 varname
= xmalloc (nbytes
+ 1);
2727 nbytes
= unhexify (varname
, packet
, nbytes
);
2728 varname
[nbytes
] = '\0';
2730 tsv
= create_trace_state_variable (num
, 1);
2731 tsv
->initial_value
= (LONGEST
) val
;
2732 tsv
->name
= varname
;
2734 set_trace_state_variable_value (num
, (LONGEST
) val
);
2740 cmd_qtenable_disable (char *own_buf
, int enable
)
2742 char *packet
= own_buf
;
2744 struct tracepoint
*tp
;
2746 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2747 packet
= unpack_varlen_hex (packet
, &num
);
2748 ++packet
; /* skip a colon */
2749 packet
= unpack_varlen_hex (packet
, &addr
);
2751 tp
= find_tracepoint (num
, addr
);
2755 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2757 trace_debug ("Tracepoint %d at 0x%s is already %s",
2758 (int) num
, paddress (addr
),
2759 enable
? "enabled" : "disabled");
2764 trace_debug ("%s tracepoint %d at 0x%s",
2765 enable
? "Enabling" : "Disabling",
2766 (int) num
, paddress (addr
));
2768 tp
->enabled
= enable
;
2770 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2773 int offset
= offsetof (struct tracepoint
, enabled
);
2774 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2776 ret
= prepare_to_access_memory ();
2779 trace_debug ("Failed to temporarily stop inferior threads");
2780 write_enn (own_buf
);
2784 ret
= write_inferior_integer (obj_addr
, enable
);
2785 done_accessing_memory ();
2789 trace_debug ("Cannot write enabled flag into "
2790 "inferior process memory");
2791 write_enn (own_buf
);
2800 trace_debug ("Tracepoint %d at 0x%s not found",
2801 (int) num
, paddress (addr
));
2802 write_enn (own_buf
);
2807 cmd_qtv (char *own_buf
)
2812 char *packet
= own_buf
;
2814 packet
+= strlen ("qTV:");
2815 unpack_varlen_hex (packet
, &num
);
2817 if (current_traceframe
>= 0)
2819 err
= traceframe_read_tsv ((int) num
, &val
);
2822 strcpy (own_buf
, "U");
2826 /* Only make tsv's be undefined before the first trace run. After a
2827 trace run is over, the user might want to see the last value of
2828 the tsv, and it might not be available in a traceframe. */
2829 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2831 strcpy (own_buf
, "U");
2835 val
= get_trace_state_variable_value (num
);
2837 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2840 /* Clear out the list of readonly regions. */
2843 clear_readonly_regions (void)
2845 struct readonly_region
*roreg
;
2847 while (readonly_regions
)
2849 roreg
= readonly_regions
;
2850 readonly_regions
= readonly_regions
->next
;
2855 /* Parse the collection of address ranges whose contents GDB believes
2856 to be unchanging and so can be read directly from target memory
2857 even while looking at a traceframe. */
2860 cmd_qtro (char *own_buf
)
2862 ULONGEST start
, end
;
2863 struct readonly_region
*roreg
;
2864 char *packet
= own_buf
;
2866 trace_debug ("Want to mark readonly regions");
2868 clear_readonly_regions ();
2870 packet
+= strlen ("QTro");
2872 while (*packet
== ':')
2874 ++packet
; /* skip a colon */
2875 packet
= unpack_varlen_hex (packet
, &start
);
2876 ++packet
; /* skip a comma */
2877 packet
= unpack_varlen_hex (packet
, &end
);
2878 roreg
= xmalloc (sizeof (struct readonly_region
));
2879 roreg
->start
= start
;
2881 roreg
->next
= readonly_regions
;
2882 readonly_regions
= roreg
;
2883 trace_debug ("Added readonly region from 0x%s to 0x%s",
2884 paddress (roreg
->start
), paddress (roreg
->end
));
2890 /* Test to see if the given range is in our list of readonly ranges.
2891 We only test for being entirely within a range, GDB is not going to
2892 send a single memory packet that spans multiple regions. */
2895 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2897 struct readonly_region
*roreg
;
2899 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2900 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2906 /* The maximum size of a jump pad entry. */
2907 static const int max_jump_pad_size
= 0x100;
2909 static CORE_ADDR gdb_jump_pad_head
;
2911 /* Return the address of the next free jump space. */
2914 get_jump_space_head (void)
2916 if (gdb_jump_pad_head
== 0)
2918 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2919 &gdb_jump_pad_head
))
2920 fatal ("error extracting jump_pad_buffer");
2923 return gdb_jump_pad_head
;
2926 /* Reserve USED bytes from the jump space. */
2929 claim_jump_space (ULONGEST used
)
2931 trace_debug ("claim_jump_space reserves %s bytes at %s",
2932 pulongest (used
), paddress (gdb_jump_pad_head
));
2933 gdb_jump_pad_head
+= used
;
2936 static CORE_ADDR trampoline_buffer_head
= 0;
2937 static CORE_ADDR trampoline_buffer_tail
;
2939 /* Reserve USED bytes from the trampoline buffer and return the
2940 address of the start of the reserved space in TRAMPOLINE. Returns
2941 non-zero if the space is successfully claimed. */
2944 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2946 if (!trampoline_buffer_head
)
2948 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2949 &trampoline_buffer_tail
))
2951 fatal ("error extracting trampoline_buffer");
2955 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2956 &trampoline_buffer_head
))
2958 fatal ("error extracting trampoline_buffer_end");
2963 /* Start claiming space from the top of the trampoline space. If
2964 the space is located at the bottom of the virtual address space,
2965 this reduces the possibility that corruption will occur if a null
2966 pointer is used to write to memory. */
2967 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2969 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2974 trampoline_buffer_head
-= used
;
2976 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2977 pulongest (used
), paddress (trampoline_buffer_head
));
2979 *trampoline
= trampoline_buffer_head
;
2983 /* Returns non-zero if there is space allocated for use in trampolines
2984 for fast tracepoints. */
2987 have_fast_tracepoint_trampoline_buffer (char *buf
)
2989 CORE_ADDR trampoline_end
, errbuf
;
2991 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2994 fatal ("error extracting trampoline_buffer_end");
3001 strcpy (buf
, "was claiming");
3002 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3005 fatal ("error extracting errbuf");
3009 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3012 return trampoline_end
!= 0;
3015 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3016 the command fails, or 0 otherwise. If the command ran
3017 successfully, but probing the marker failed, ERROUT will be filled
3018 with the error to reply to GDB, and -1 is also returned. This
3019 allows directly passing IPA errors to GDB. */
3022 probe_marker_at (CORE_ADDR address
, char *errout
)
3024 char cmd
[IPA_CMD_BUF_SIZE
];
3027 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3028 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3034 strcpy (errout
, cmd
);
3043 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3045 to
->jump_pad
= from
->jump_pad
;
3046 to
->jump_pad_end
= from
->jump_pad_end
;
3047 to
->trampoline
= from
->trampoline
;
3048 to
->trampoline_end
= from
->trampoline_end
;
3049 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3050 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3051 to
->handle
= from
->handle
;
3053 gdb_assert (from
->handle
);
3054 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3057 #define MAX_JUMP_SIZE 20
3059 /* Install fast tracepoint. Return 0 if successful, otherwise return
3063 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3065 CORE_ADDR jentry
, jump_entry
;
3066 CORE_ADDR trampoline
;
3067 ULONGEST trampoline_size
;
3069 /* The jump to the jump pad of the last fast tracepoint
3071 unsigned char fjump
[MAX_JUMP_SIZE
];
3072 ULONGEST fjump_size
;
3074 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3076 trace_debug ("Requested a fast tracepoint on an instruction "
3077 "that is of less than the minimum length.");
3081 jentry
= jump_entry
= get_jump_space_head ();
3084 trampoline_size
= 0;
3086 /* Install the jump pad. */
3087 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3089 ipa_sym_addrs
.addr_gdb_collect
,
3090 ipa_sym_addrs
.addr_collecting
,
3093 &trampoline
, &trampoline_size
,
3095 &tpoint
->adjusted_insn_addr
,
3096 &tpoint
->adjusted_insn_addr_end
,
3103 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3106 if (tpoint
->handle
!= NULL
)
3108 tpoint
->jump_pad
= jump_entry
;
3109 tpoint
->jump_pad_end
= jentry
;
3110 tpoint
->trampoline
= trampoline
;
3111 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3113 /* Pad to 8-byte alignment. */
3114 jentry
= ((jentry
+ 7) & ~0x7);
3115 claim_jump_space (jentry
- jump_entry
);
3122 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3125 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3127 tpoint
->handle
= NULL
;
3130 if (tpoint
->type
== trap_tracepoint
)
3132 /* Tracepoints are installed as memory breakpoints. Just go
3133 ahead and install the trap. The breakpoints module
3134 handles duplicated breakpoints, and the memory read
3135 routine handles un-patching traps from memory reads. */
3136 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3137 tracepoint_handler
);
3139 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3141 if (!agent_loaded_p ())
3143 trace_debug ("Requested a %s tracepoint, but fast "
3144 "tracepoints aren't supported.",
3145 tpoint
->type
== static_tracepoint
? "static" : "fast");
3146 write_e_ipa_not_loaded (own_buf
);
3149 if (tpoint
->type
== static_tracepoint
3150 && !in_process_agent_supports_ust ())
3152 trace_debug ("Requested a static tracepoint, but static "
3153 "tracepoints are not supported.");
3154 write_e_ust_not_loaded (own_buf
);
3158 if (tpoint
->type
== fast_tracepoint
)
3159 install_fast_tracepoint (tpoint
, own_buf
);
3162 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3163 tpoint
->handle
= (void *) -1;
3168 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3170 if (tpoint
->handle
== NULL
)
3172 if (*own_buf
== '\0')
3173 write_enn (own_buf
);
3179 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3182 cmd_qtstart (char *packet
)
3184 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3185 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3187 trace_debug ("Starting the trace");
3189 /* Pause all threads temporarily while we patch tracepoints. */
3192 /* Get threads out of jump pads. Safe to do here, since this is a
3193 top level command. And, required to do here, since we're
3194 deleting/rewriting jump pads. */
3196 stabilize_threads ();
3198 /* Freeze threads. */
3201 /* Sync the fast tracepoints list in the inferior ftlib. */
3202 if (agent_loaded_p ())
3203 download_trace_state_variables ();
3205 /* No previous fast tpoint yet. */
3206 prev_ftpoint
= NULL
;
3208 /* No previous static tpoint yet. */
3209 prev_stpoint
= NULL
;
3213 /* Start out empty. */
3214 if (agent_loaded_p ())
3215 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3217 /* Download and install tracepoints. */
3218 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3220 /* Ensure all the hit counts start at zero. */
3221 tpoint
->hit_count
= 0;
3222 tpoint
->traceframe_usage
= 0;
3224 if (tpoint
->type
== trap_tracepoint
)
3226 /* Tracepoints are installed as memory breakpoints. Just go
3227 ahead and install the trap. The breakpoints module
3228 handles duplicated breakpoints, and the memory read
3229 routine handles un-patching traps from memory reads. */
3230 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3231 tracepoint_handler
);
3233 else if (tpoint
->type
== fast_tracepoint
3234 || tpoint
->type
== static_tracepoint
)
3236 if (maybe_write_ipa_not_loaded (packet
))
3238 trace_debug ("Requested a %s tracepoint, but fast "
3239 "tracepoints aren't supported.",
3240 tpoint
->type
== static_tracepoint
3241 ? "static" : "fast");
3245 if (tpoint
->type
== fast_tracepoint
)
3248 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3250 if (prev_ftpoint
!= NULL
3251 && prev_ftpoint
->address
== tpoint
->address
)
3254 tracepoint_send_agent (tpoint
);
3256 download_tracepoint_1 (tpoint
);
3258 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3262 /* Tracepoint is installed successfully? */
3265 /* Download and install fast tracepoint by agent. */
3267 installed
= !tracepoint_send_agent (tpoint
);
3270 download_tracepoint_1 (tpoint
);
3271 installed
= !install_fast_tracepoint (tpoint
, packet
);
3275 prev_ftpoint
= tpoint
;
3280 if (!in_process_agent_supports_ust ())
3282 trace_debug ("Requested a static tracepoint, but static "
3283 "tracepoints are not supported.");
3287 download_tracepoint_1 (tpoint
);
3288 /* Can only probe a given marker once. */
3289 if (prev_stpoint
!= NULL
3290 && prev_stpoint
->address
== tpoint
->address
)
3291 tpoint
->handle
= (void *) -1;
3294 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3296 tpoint
->handle
= (void *) -1;
3298 /* So that we can handle multiple static tracepoints
3299 at the same address easily. */
3300 prev_stpoint
= tpoint
;
3306 tpptr
= tpoint
->obj_addr_on_target
;
3308 if (tpoint
== tracepoints
)
3309 /* First object in list, set the head pointer in the
3311 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3313 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3318 /* Any failure in the inner loop is sufficient cause to give
3320 if (tpoint
->handle
== NULL
)
3324 /* Any error in tracepoint insertion is unacceptable; better to
3325 address the problem now, than end up with a useless or misleading
3329 clear_installed_tracepoints ();
3330 if (*packet
== '\0')
3336 stopping_tracepoint
= NULL
;
3337 trace_buffer_is_full
= 0;
3338 expr_eval_result
= expr_eval_no_error
;
3339 error_tracepoint
= NULL
;
3340 tracing_start_time
= get_timestamp ();
3342 /* Tracing is now active, hits will now start being logged. */
3345 if (agent_loaded_p ())
3347 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3348 fatal ("Error setting tracing variable in lib");
3350 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3352 fatal ("Error clearing stopping_tracepoint variable in lib");
3354 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3355 fatal ("Error clearing trace_buffer_is_full variable in lib");
3357 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3358 stop_tracing_handler
);
3359 if (stop_tracing_bkpt
== NULL
)
3360 error ("Error setting stop_tracing breakpoint");
3362 flush_trace_buffer_bkpt
3363 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3364 flush_trace_buffer_handler
);
3365 if (flush_trace_buffer_bkpt
== NULL
)
3366 error ("Error setting flush_trace_buffer breakpoint");
3374 /* End a tracing run, filling in a stop reason to report back to GDB,
3375 and removing the tracepoints from the code. */
3382 trace_debug ("Tracing is already off, ignoring");
3386 trace_debug ("Stopping the trace");
3388 /* Pause all threads before removing fast jumps from memory,
3389 breakpoints, and touching IPA state variables (inferior memory).
3390 Some thread may hit the internal tracing breakpoints, or be
3391 collecting this moment, but that's ok, we don't release the
3392 tpoint object's memory or the jump pads here (we only do that
3393 when we're sure we can move all threads out of the jump pads).
3394 We can't now, since we may be getting here due to the inferior
3395 agent calling us. */
3397 /* Since we're removing breakpoints, cancel breakpoint hits,
3398 possibly related to the breakpoints we're about to delete. */
3399 cancel_breakpoints ();
3401 /* Stop logging. Tracepoints can still be hit, but they will not be
3404 if (agent_loaded_p ())
3406 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3407 fatal ("Error clearing tracing variable in lib");
3410 tracing_stop_time
= get_timestamp ();
3411 tracing_stop_reason
= "t???";
3412 tracing_stop_tpnum
= 0;
3413 if (stopping_tracepoint
)
3415 trace_debug ("Stopping the trace because "
3416 "tracepoint %d was hit %" PRIu64
" times",
3417 stopping_tracepoint
->number
,
3418 stopping_tracepoint
->pass_count
);
3419 tracing_stop_reason
= "tpasscount";
3420 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3422 else if (trace_buffer_is_full
)
3424 trace_debug ("Stopping the trace because the trace buffer is full");
3425 tracing_stop_reason
= "tfull";
3427 else if (expr_eval_result
!= expr_eval_no_error
)
3429 trace_debug ("Stopping the trace because of an expression eval error");
3430 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3431 tracing_stop_tpnum
= error_tracepoint
->number
;
3433 #ifndef IN_PROCESS_AGENT
3434 else if (!gdb_connected ())
3436 trace_debug ("Stopping the trace because GDB disconnected");
3437 tracing_stop_reason
= "tdisconnected";
3442 trace_debug ("Stopping the trace because of a tstop command");
3443 tracing_stop_reason
= "tstop";
3446 stopping_tracepoint
= NULL
;
3447 error_tracepoint
= NULL
;
3449 /* Clear out the tracepoints. */
3450 clear_installed_tracepoints ();
3452 if (agent_loaded_p ())
3454 /* Pull in fast tracepoint trace frames from the inferior lib
3455 buffer into our buffer, even if our buffer is already full,
3456 because we want to present the full number of created frames
3457 in addition to what fit in the trace buffer. */
3458 upload_fast_traceframes ();
3461 if (stop_tracing_bkpt
!= NULL
)
3463 delete_breakpoint (stop_tracing_bkpt
);
3464 stop_tracing_bkpt
= NULL
;
3467 if (flush_trace_buffer_bkpt
!= NULL
)
3469 delete_breakpoint (flush_trace_buffer_bkpt
);
3470 flush_trace_buffer_bkpt
= NULL
;
3477 stop_tracing_handler (CORE_ADDR addr
)
3479 trace_debug ("lib hit stop_tracing");
3481 /* Don't actually handle it here. When we stop tracing we remove
3482 breakpoints from the inferior, and that is not allowed in a
3483 breakpoint handler (as the caller is walking the breakpoint
3489 flush_trace_buffer_handler (CORE_ADDR addr
)
3491 trace_debug ("lib hit flush_trace_buffer");
3496 cmd_qtstop (char *packet
)
3503 cmd_qtdisconnected (char *own_buf
)
3506 char *packet
= own_buf
;
3508 packet
+= strlen ("QTDisconnected:");
3510 unpack_varlen_hex (packet
, &setting
);
3514 disconnected_tracing
= setting
;
3518 cmd_qtframe (char *own_buf
)
3520 ULONGEST frame
, pc
, lo
, hi
, num
;
3522 struct traceframe
*tframe
;
3523 char *packet
= own_buf
;
3525 packet
+= strlen ("QTFrame:");
3527 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3529 packet
+= strlen ("pc:");
3530 unpack_varlen_hex (packet
, &pc
);
3531 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3532 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3534 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3536 packet
+= strlen ("range:");
3537 packet
= unpack_varlen_hex (packet
, &lo
);
3539 unpack_varlen_hex (packet
, &hi
);
3540 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3541 paddress (lo
), paddress (hi
));
3542 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3544 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3546 packet
+= strlen ("outside:");
3547 packet
= unpack_varlen_hex (packet
, &lo
);
3549 unpack_varlen_hex (packet
, &hi
);
3550 trace_debug ("Want to find next traceframe "
3551 "outside the range 0x%s to 0x%s",
3552 paddress (lo
), paddress (hi
));
3553 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3555 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3557 packet
+= strlen ("tdp:");
3558 unpack_varlen_hex (packet
, &num
);
3560 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3561 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3565 unpack_varlen_hex (packet
, &frame
);
3566 tfnum
= (int) frame
;
3569 trace_debug ("Want to stop looking at traceframes");
3570 current_traceframe
= -1;
3574 trace_debug ("Want to look at traceframe %d", tfnum
);
3575 tframe
= find_traceframe (tfnum
);
3580 current_traceframe
= tfnum
;
3581 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3584 sprintf (own_buf
, "F-1");
3588 cmd_qtstatus (char *packet
)
3590 char *stop_reason_rsp
= NULL
;
3591 char *buf1
, *buf2
, *buf3
, *str
;
3594 /* Translate the plain text of the notes back into hex for
3597 str
= (tracing_user_name
? tracing_user_name
: "");
3598 slen
= strlen (str
);
3599 buf1
= (char *) alloca (slen
* 2 + 1);
3600 hexify (buf1
, str
, slen
);
3602 str
= (tracing_notes
? tracing_notes
: "");
3603 slen
= strlen (str
);
3604 buf2
= (char *) alloca (slen
* 2 + 1);
3605 hexify (buf2
, str
, slen
);
3607 str
= (tracing_stop_note
? tracing_stop_note
: "");
3608 slen
= strlen (str
);
3609 buf3
= (char *) alloca (slen
* 2 + 1);
3610 hexify (buf3
, str
, slen
);
3612 trace_debug ("Returning trace status as %d, stop reason %s",
3613 tracing
, tracing_stop_reason
);
3615 if (agent_loaded_p ())
3619 upload_fast_traceframes ();
3624 stop_reason_rsp
= (char *) tracing_stop_reason
;
3626 /* The user visible error string in terror needs to be hex encoded.
3627 We leave it as plain string in `tracing_stop_reason' to ease
3629 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3631 const char *result_name
;
3635 result_name
= stop_reason_rsp
+ strlen ("terror:");
3636 hexstr_len
= strlen (result_name
) * 2;
3637 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3638 strcpy (p
, "terror:");
3640 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3643 /* If this was a forced stop, include any stop note that was supplied. */
3644 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3646 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3647 strcpy (stop_reason_rsp
, "tstop:");
3648 strcat (stop_reason_rsp
, buf3
);
3654 "tframes:%x;tcreated:%x;"
3655 "tfree:%x;tsize:%s;"
3658 "starttime:%s;stoptime:%s;"
3659 "username:%s:;notes:%s:",
3661 stop_reason_rsp
, tracing_stop_tpnum
,
3662 traceframe_count
, traceframes_created
,
3663 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3664 circular_trace_buffer
,
3665 disconnected_tracing
,
3666 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3671 cmd_qtp (char *own_buf
)
3674 struct tracepoint
*tpoint
;
3675 char *packet
= own_buf
;
3677 packet
+= strlen ("qTP:");
3679 packet
= unpack_varlen_hex (packet
, &num
);
3680 ++packet
; /* skip a colon */
3681 packet
= unpack_varlen_hex (packet
, &addr
);
3683 /* See if we already have this tracepoint. */
3684 tpoint
= find_tracepoint (num
, addr
);
3688 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3689 (int) num
, paddress (addr
));
3690 write_enn (own_buf
);
3694 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3695 tpoint
->traceframe_usage
);
3698 /* State variables to help return all the tracepoint bits. */
3699 static struct tracepoint
*cur_tpoint
;
3700 static unsigned int cur_action
;
3701 static unsigned int cur_step_action
;
3702 static struct source_string
*cur_source_string
;
3703 static struct trace_state_variable
*cur_tsv
;
3705 /* Compose a response that is an imitation of the syntax by which the
3706 tracepoint was originally downloaded. */
3709 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3713 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3714 paddress (tpoint
->address
),
3715 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3716 tpoint
->pass_count
);
3717 if (tpoint
->type
== fast_tracepoint
)
3718 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3719 else if (tpoint
->type
== static_tracepoint
)
3720 sprintf (packet
+ strlen (packet
), ":S");
3724 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3725 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3726 tpoint
->cond
->length
, buf
);
3731 /* Compose a response that is an imitation of the syntax by which the
3732 tracepoint action was originally downloaded (with the difference
3733 that due to the way we store the actions, this will output a packet
3734 per action, while GDB could have combined more than one action
3738 response_action (char *packet
, struct tracepoint
*tpoint
,
3739 char *taction
, int step
)
3741 sprintf (packet
, "%c%x:%s:%s",
3742 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3746 /* Compose a response that is an imitation of the syntax by which the
3747 tracepoint source piece was originally downloaded. */
3750 response_source (char *packet
,
3751 struct tracepoint
*tpoint
, struct source_string
*src
)
3756 len
= strlen (src
->str
);
3757 buf
= alloca (len
* 2 + 1);
3758 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3760 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3761 tpoint
->number
, paddress (tpoint
->address
),
3762 src
->type
, 0, len
, buf
);
3765 /* Return the first piece of tracepoint definition, and initialize the
3766 state machine that will iterate through all the tracepoint
3770 cmd_qtfp (char *packet
)
3772 trace_debug ("Returning first tracepoint definition piece");
3774 cur_tpoint
= tracepoints
;
3775 cur_action
= cur_step_action
= 0;
3776 cur_source_string
= NULL
;
3779 response_tracepoint (packet
, cur_tpoint
);
3781 strcpy (packet
, "l");
3784 /* Return additional pieces of tracepoint definition. Each action and
3785 stepping action must go into its own packet, because of packet size
3786 limits, and so we use state variables to deliver one piece at a
3790 cmd_qtsp (char *packet
)
3792 trace_debug ("Returning subsequent tracepoint definition piece");
3796 /* This case would normally never occur, but be prepared for
3798 strcpy (packet
, "l");
3800 else if (cur_action
< cur_tpoint
->numactions
)
3802 response_action (packet
, cur_tpoint
,
3803 cur_tpoint
->actions_str
[cur_action
], 0);
3806 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3808 response_action (packet
, cur_tpoint
,
3809 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3812 else if ((cur_source_string
3813 ? cur_source_string
->next
3814 : cur_tpoint
->source_strings
))
3816 if (cur_source_string
)
3817 cur_source_string
= cur_source_string
->next
;
3819 cur_source_string
= cur_tpoint
->source_strings
;
3820 response_source (packet
, cur_tpoint
, cur_source_string
);
3824 cur_tpoint
= cur_tpoint
->next
;
3825 cur_action
= cur_step_action
= 0;
3826 cur_source_string
= NULL
;
3828 response_tracepoint (packet
, cur_tpoint
);
3830 strcpy (packet
, "l");
3834 /* Compose a response that is an imitation of the syntax by which the
3835 trace state variable was originally downloaded. */
3838 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3840 char *buf
= (char *) "";
3845 namelen
= strlen (tsv
->name
);
3846 buf
= alloca (namelen
* 2 + 1);
3847 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3850 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3851 tsv
->getter
? 1 : 0, buf
);
3854 /* Return the first trace state variable definition, and initialize
3855 the state machine that will iterate through all the tsv bits. */
3858 cmd_qtfv (char *packet
)
3860 trace_debug ("Returning first trace state variable definition");
3862 cur_tsv
= trace_state_variables
;
3865 response_tsv (packet
, cur_tsv
);
3867 strcpy (packet
, "l");
3870 /* Return additional trace state variable definitions. */
3873 cmd_qtsv (char *packet
)
3875 trace_debug ("Returning additional trace state variable definition");
3879 cur_tsv
= cur_tsv
->next
;
3881 response_tsv (packet
, cur_tsv
);
3883 strcpy (packet
, "l");
3886 strcpy (packet
, "l");
3889 /* Return the first static tracepoint marker, and initialize the state
3890 machine that will iterate through all the static tracepoints
3894 cmd_qtfstm (char *packet
)
3896 if (!maybe_write_ipa_ust_not_loaded (packet
))
3897 run_inferior_command (packet
, strlen (packet
) + 1);
3900 /* Return additional static tracepoints markers. */
3903 cmd_qtsstm (char *packet
)
3905 if (!maybe_write_ipa_ust_not_loaded (packet
))
3906 run_inferior_command (packet
, strlen (packet
) + 1);
3909 /* Return the definition of the static tracepoint at a given address.
3910 Result packet is the same as qTsST's. */
3913 cmd_qtstmat (char *packet
)
3915 if (!maybe_write_ipa_ust_not_loaded (packet
))
3916 run_inferior_command (packet
, strlen (packet
) + 1);
3919 /* Sent the agent a command to close it. */
3922 gdb_agent_about_to_close (int pid
)
3924 char buf
[IPA_CMD_BUF_SIZE
];
3926 if (!maybe_write_ipa_not_loaded (buf
))
3928 struct thread_info
*save_inferior
;
3929 struct inferior_list_entry
*inf
= all_threads
.head
;
3931 save_inferior
= current_inferior
;
3933 /* Find a certain thread which belongs to process PID. */
3936 if (ptid_get_pid (inf
->id
) == pid
)
3941 current_inferior
= (struct thread_info
*) inf
;
3943 strcpy (buf
, "close");
3945 run_inferior_command (buf
, strlen (buf
) + 1);
3947 current_inferior
= save_inferior
;
3951 /* Return the minimum instruction size needed for fast tracepoints as a
3952 hexadecimal number. */
3955 cmd_qtminftpilen (char *packet
)
3957 if (current_inferior
== NULL
)
3959 /* Indicate that the minimum length is currently unknown. */
3960 strcpy (packet
, "0");
3964 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3967 /* Respond to qTBuffer packet with a block of raw data from the trace
3968 buffer. GDB may ask for a lot, but we are allowed to reply with
3969 only as much as will fit within packet limits or whatever. */
3972 cmd_qtbuffer (char *own_buf
)
3974 ULONGEST offset
, num
, tot
;
3976 char *packet
= own_buf
;
3978 packet
+= strlen ("qTBuffer:");
3980 packet
= unpack_varlen_hex (packet
, &offset
);
3981 ++packet
; /* skip a comma */
3982 unpack_varlen_hex (packet
, &num
);
3984 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3985 (int) num
, phex_nz (offset
, 0));
3987 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3989 /* If we're right at the end, reply specially that we're done. */
3992 strcpy (own_buf
, "l");
3996 /* Object to any other out-of-bounds request. */
3999 write_enn (own_buf
);
4003 /* Compute the pointer corresponding to the given offset, accounting
4005 tbp
= trace_buffer_start
+ offset
;
4006 if (tbp
>= trace_buffer_wrap
)
4007 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4009 /* Trim to the remaining bytes if we're close to the end. */
4010 if (num
> tot
- offset
)
4013 /* Trim to available packet size. */
4014 if (num
>= (PBUFSIZ
- 16) / 2 )
4015 num
= (PBUFSIZ
- 16) / 2;
4017 convert_int_to_ascii (tbp
, own_buf
, num
);
4021 cmd_bigqtbuffer_circular (char *own_buf
)
4024 char *packet
= own_buf
;
4026 packet
+= strlen ("QTBuffer:circular:");
4028 unpack_varlen_hex (packet
, &val
);
4029 circular_trace_buffer
= val
;
4030 trace_debug ("Trace buffer is now %s",
4031 circular_trace_buffer
? "circular" : "linear");
4036 cmd_bigqtbuffer_size (char *own_buf
)
4040 char *packet
= own_buf
;
4042 /* Can't change the size during a tracing run. */
4045 write_enn (own_buf
);
4049 packet
+= strlen ("QTBuffer:size:");
4051 /* -1 is sent as literal "-1". */
4052 if (strcmp (packet
, "-1") == 0)
4053 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4056 unpack_varlen_hex (packet
, &val
);
4057 sval
= (LONGEST
) val
;
4060 init_trace_buffer (sval
);
4061 trace_debug ("Trace buffer is now %s bytes",
4062 plongest (trace_buffer_size
));
4067 cmd_qtnotes (char *own_buf
)
4070 char *saved
, *user
, *notes
, *stopnote
;
4071 char *packet
= own_buf
;
4073 packet
+= strlen ("QTNotes:");
4077 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4079 packet
+= strlen ("user:");
4081 packet
= strchr (packet
, ';');
4082 nbytes
= (packet
- saved
) / 2;
4083 user
= xmalloc (nbytes
+ 1);
4084 nbytes
= unhexify (user
, saved
, nbytes
);
4085 user
[nbytes
] = '\0';
4086 ++packet
; /* skip the semicolon */
4087 trace_debug ("User is '%s'", user
);
4088 xfree (tracing_user_name
);
4089 tracing_user_name
= user
;
4091 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4093 packet
+= strlen ("notes:");
4095 packet
= strchr (packet
, ';');
4096 nbytes
= (packet
- saved
) / 2;
4097 notes
= xmalloc (nbytes
+ 1);
4098 nbytes
= unhexify (notes
, saved
, nbytes
);
4099 notes
[nbytes
] = '\0';
4100 ++packet
; /* skip the semicolon */
4101 trace_debug ("Notes is '%s'", notes
);
4102 xfree (tracing_notes
);
4103 tracing_notes
= notes
;
4105 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4107 packet
+= strlen ("tstop:");
4109 packet
= strchr (packet
, ';');
4110 nbytes
= (packet
- saved
) / 2;
4111 stopnote
= xmalloc (nbytes
+ 1);
4112 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4113 stopnote
[nbytes
] = '\0';
4114 ++packet
; /* skip the semicolon */
4115 trace_debug ("tstop note is '%s'", stopnote
);
4116 xfree (tracing_stop_note
);
4117 tracing_stop_note
= stopnote
;
4127 handle_tracepoint_general_set (char *packet
)
4129 if (strcmp ("QTinit", packet
) == 0)
4131 cmd_qtinit (packet
);
4134 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4139 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4141 cmd_qtdpsrc (packet
);
4144 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4146 cmd_qtenable_disable (packet
, 1);
4149 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4151 cmd_qtenable_disable (packet
, 0);
4154 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4159 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4164 else if (strcmp ("QTStart", packet
) == 0)
4166 cmd_qtstart (packet
);
4169 else if (strcmp ("QTStop", packet
) == 0)
4171 cmd_qtstop (packet
);
4174 else if (strncmp ("QTDisconnected:", packet
,
4175 strlen ("QTDisconnected:")) == 0)
4177 cmd_qtdisconnected (packet
);
4180 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4182 cmd_qtframe (packet
);
4185 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4187 cmd_bigqtbuffer_circular (packet
);
4190 else if (strncmp ("QTBuffer:size:", packet
, strlen ("QTBuffer:size:")) == 0)
4192 cmd_bigqtbuffer_size (packet
);
4195 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4197 cmd_qtnotes (packet
);
4205 handle_tracepoint_query (char *packet
)
4207 if (strcmp ("qTStatus", packet
) == 0)
4209 cmd_qtstatus (packet
);
4212 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4217 else if (strcmp ("qTfP", packet
) == 0)
4222 else if (strcmp ("qTsP", packet
) == 0)
4227 else if (strcmp ("qTfV", packet
) == 0)
4232 else if (strcmp ("qTsV", packet
) == 0)
4237 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4242 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4244 cmd_qtbuffer (packet
);
4247 else if (strcmp ("qTfSTM", packet
) == 0)
4249 cmd_qtfstm (packet
);
4252 else if (strcmp ("qTsSTM", packet
) == 0)
4254 cmd_qtsstm (packet
);
4257 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4259 cmd_qtstmat (packet
);
4262 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4264 cmd_qtminftpilen (packet
);
4272 #ifndef IN_PROCESS_AGENT
4274 /* Call this when thread TINFO has hit the tracepoint defined by
4275 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4276 action. This adds a while-stepping collecting state item to the
4277 threads' collecting state list, so that we can keep track of
4278 multiple simultaneous while-stepping actions being collected by the
4279 same thread. This can happen in cases like:
4281 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4283 ff0003 INSN3 <-- TP2, collect $regs
4284 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4287 Notice that when instruction INSN5 is reached, the while-stepping
4288 actions of both TP1 and TP3 are still being collected, and that TP2
4289 had been collected meanwhile. The whole range of ff0001-ff0005
4290 should be single-stepped, due to at least TP1's while-stepping
4291 action covering the whole range. */
4294 add_while_stepping_state (struct thread_info
*tinfo
,
4295 int tp_number
, CORE_ADDR tp_address
)
4297 struct wstep_state
*wstep
;
4299 wstep
= xmalloc (sizeof (*wstep
));
4300 wstep
->next
= tinfo
->while_stepping
;
4302 wstep
->tp_number
= tp_number
;
4303 wstep
->tp_address
= tp_address
;
4304 wstep
->current_step
= 0;
4306 tinfo
->while_stepping
= wstep
;
4309 /* Release the while-stepping collecting state WSTEP. */
4312 release_while_stepping_state (struct wstep_state
*wstep
)
4317 /* Release all while-stepping collecting states currently associated
4318 with thread TINFO. */
4321 release_while_stepping_state_list (struct thread_info
*tinfo
)
4323 struct wstep_state
*head
;
4325 while (tinfo
->while_stepping
)
4327 head
= tinfo
->while_stepping
;
4328 tinfo
->while_stepping
= head
->next
;
4329 release_while_stepping_state (head
);
4333 /* If TINFO was handling a 'while-stepping' action, the step has
4334 finished, so collect any step data needed, and check if any more
4335 steps are required. Return true if the thread was indeed
4336 collecting tracepoint data, false otherwise. */
4339 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4341 struct tracepoint
*tpoint
;
4342 struct wstep_state
*wstep
;
4343 struct wstep_state
**wstep_link
;
4344 struct trap_tracepoint_ctx ctx
;
4346 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4348 if (agent_loaded_p ())
4349 upload_fast_traceframes ();
4351 /* Check if we were indeed collecting data for one of more
4352 tracepoints with a 'while-stepping' count. */
4353 if (tinfo
->while_stepping
== NULL
)
4358 /* We're not even tracing anymore. Stop this thread from
4360 release_while_stepping_state_list (tinfo
);
4362 /* The thread had stopped due to a single-step request indeed
4363 explained by a tracepoint. */
4367 wstep
= tinfo
->while_stepping
;
4368 wstep_link
= &tinfo
->while_stepping
;
4370 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4371 target_pid_to_str (tinfo
->entry
.id
),
4372 wstep
->tp_number
, paddress (wstep
->tp_address
));
4374 ctx
.base
.type
= trap_tracepoint
;
4375 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4377 while (wstep
!= NULL
)
4379 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4382 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4383 wstep
->tp_number
, paddress (wstep
->tp_address
),
4384 target_pid_to_str (tinfo
->entry
.id
));
4387 *wstep_link
= wstep
->next
;
4388 release_while_stepping_state (wstep
);
4389 wstep
= *wstep_link
;
4393 /* We've just finished one step. */
4394 ++wstep
->current_step
;
4397 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4398 stop_pc
, tpoint
, wstep
->current_step
);
4400 if (wstep
->current_step
>= tpoint
->step_count
)
4402 /* The requested numbers of steps have occurred. */
4403 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4404 target_pid_to_str (tinfo
->entry
.id
),
4405 wstep
->tp_number
, paddress (wstep
->tp_address
));
4407 /* Unlink the wstep. */
4408 *wstep_link
= wstep
->next
;
4409 release_while_stepping_state (wstep
);
4410 wstep
= *wstep_link
;
4412 /* Only check the hit count now, which ensure that we do all
4413 our stepping before stopping the run. */
4414 if (tpoint
->pass_count
> 0
4415 && tpoint
->hit_count
>= tpoint
->pass_count
4416 && stopping_tracepoint
== NULL
)
4417 stopping_tracepoint
= tpoint
;
4421 /* Keep single-stepping until the requested numbers of steps
4423 wstep_link
= &wstep
->next
;
4424 wstep
= *wstep_link
;
4427 if (stopping_tracepoint
4428 || trace_buffer_is_full
4429 || expr_eval_result
!= expr_eval_no_error
)
4439 /* Handle any internal tracing control breakpoint hits. That means,
4440 pull traceframes from the IPA to our buffer, and syncing both
4441 tracing agents when the IPA's tracing stops for some reason. */
4444 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4446 /* Pull in fast tracepoint trace frames from the inferior in-process
4447 agent's buffer into our buffer. */
4449 if (!agent_loaded_p ())
4452 upload_fast_traceframes ();
4454 /* Check if the in-process agent had decided we should stop
4456 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4458 int ipa_trace_buffer_is_full
;
4459 CORE_ADDR ipa_stopping_tracepoint
;
4460 int ipa_expr_eval_result
;
4461 CORE_ADDR ipa_error_tracepoint
;
4463 trace_debug ("lib stopped at stop_tracing");
4465 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4466 &ipa_trace_buffer_is_full
);
4468 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4469 &ipa_stopping_tracepoint
);
4470 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4472 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4473 &ipa_error_tracepoint
);
4474 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4476 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4477 &ipa_expr_eval_result
);
4478 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4480 trace_debug ("lib: trace_buffer_is_full: %d, "
4481 "stopping_tracepoint: %s, "
4482 "ipa_expr_eval_result: %d, "
4483 "error_tracepoint: %s, ",
4484 ipa_trace_buffer_is_full
,
4485 paddress (ipa_stopping_tracepoint
),
4486 ipa_expr_eval_result
,
4487 paddress (ipa_error_tracepoint
));
4491 if (ipa_trace_buffer_is_full
)
4492 trace_debug ("lib stopped due to full buffer.");
4493 if (ipa_stopping_tracepoint
)
4494 trace_debug ("lib stopped due to tpoint");
4495 if (ipa_stopping_tracepoint
)
4496 trace_debug ("lib stopped due to error");
4499 if (ipa_stopping_tracepoint
!= 0)
4502 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4504 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4506 expr_eval_result
= ipa_expr_eval_result
;
4508 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4513 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4515 trace_debug ("lib stopped at flush_trace_buffer");
4522 /* Return true if TINFO just hit a tracepoint. Collect data if
4526 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4528 struct tracepoint
*tpoint
;
4530 struct trap_tracepoint_ctx ctx
;
4532 /* Not tracing, don't handle. */
4536 ctx
.base
.type
= trap_tracepoint
;
4537 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4539 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4541 /* Note that we collect fast tracepoints here as well. We'll
4542 step over the fast tracepoint jump later, which avoids the
4543 double collect. However, we don't collect for static
4544 tracepoints here, because UST markers are compiled in program,
4545 and probes will be executed in program. So static tracepoints
4546 are collected there. */
4547 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4548 && tpoint
->type
!= static_tracepoint
)
4550 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4551 target_pid_to_str (tinfo
->entry
.id
),
4552 tpoint
->number
, paddress (tpoint
->address
));
4554 /* Test the condition if present, and collect if true. */
4556 || (condition_true_at_tracepoint
4557 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4558 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4561 if (stopping_tracepoint
4562 || trace_buffer_is_full
4563 || expr_eval_result
!= expr_eval_no_error
)
4567 /* If the tracepoint had a 'while-stepping' action, then set
4568 the thread to collect this tracepoint on the following
4570 else if (tpoint
->step_count
> 0)
4572 add_while_stepping_state (tinfo
,
4573 tpoint
->number
, tpoint
->address
);
4585 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4586 struct ust_marker_data
;
4587 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4588 struct traceframe
*tframe
);
4591 /* Create a trace frame for the hit of the given tracepoint in the
4595 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4596 struct tracepoint
*tpoint
)
4598 struct traceframe
*tframe
;
4601 /* Only count it as a hit when we actually collect data. */
4602 tpoint
->hit_count
++;
4604 /* If we've exceeded a defined pass count, record the event for
4605 later, and finish the collection for this hit. This test is only
4606 for nonstepping tracepoints, stepping tracepoints test at the end
4607 of their while-stepping loop. */
4608 if (tpoint
->pass_count
> 0
4609 && tpoint
->hit_count
>= tpoint
->pass_count
4610 && tpoint
->step_count
== 0
4611 && stopping_tracepoint
== NULL
)
4612 stopping_tracepoint
= tpoint
;
4614 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4615 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4617 tframe
= add_traceframe (tpoint
);
4621 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4623 #ifndef IN_PROCESS_AGENT
4624 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4625 tpoint
->number
, paddress (tpoint
->address
),
4626 tpoint
->actions_str
[acti
]);
4629 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4630 tpoint
->actions
[acti
]);
4633 finish_traceframe (tframe
);
4636 if (tframe
== NULL
&& tracing
)
4637 trace_buffer_is_full
= 1;
4640 #ifndef IN_PROCESS_AGENT
4643 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4645 struct tracepoint
*tpoint
, int current_step
)
4647 struct traceframe
*tframe
;
4650 trace_debug ("Making new step traceframe for "
4651 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4652 tpoint
->number
, paddress (tpoint
->address
),
4653 current_step
, tpoint
->step_count
,
4656 tframe
= add_traceframe (tpoint
);
4660 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4662 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4663 tpoint
->number
, paddress (tpoint
->address
),
4664 tpoint
->step_actions_str
[acti
]);
4666 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4667 tpoint
->step_actions
[acti
]);
4670 finish_traceframe (tframe
);
4673 if (tframe
== NULL
&& tracing
)
4674 trace_buffer_is_full
= 1;
4679 #ifdef IN_PROCESS_AGENT
4680 /* The target description used by the IPA. Given that the IPA library
4681 is built for a specific architecture that is loaded into the
4682 inferior, there only needs to be one such description per
4684 const struct target_desc
*ipa_tdesc
;
4687 static struct regcache
*
4688 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4690 struct regcache
*regcache
= NULL
;
4692 #ifdef IN_PROCESS_AGENT
4693 if (ctx
->type
== fast_tracepoint
)
4695 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4696 if (!fctx
->regcache_initted
)
4698 fctx
->regcache_initted
= 1;
4699 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4700 supply_regblock (&fctx
->regcache
, NULL
);
4701 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4703 regcache
= &fctx
->regcache
;
4706 if (ctx
->type
== static_tracepoint
)
4708 struct static_tracepoint_ctx
*sctx
4709 = (struct static_tracepoint_ctx
*) ctx
;
4711 if (!sctx
->regcache_initted
)
4713 sctx
->regcache_initted
= 1;
4714 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4715 supply_regblock (&sctx
->regcache
, NULL
);
4716 /* Pass down the tracepoint address, because REGS doesn't
4717 include the PC, but we know what it must have been. */
4718 supply_static_tracepoint_registers (&sctx
->regcache
,
4719 (const unsigned char *)
4721 sctx
->tpoint
->address
);
4723 regcache
= &sctx
->regcache
;
4727 if (ctx
->type
== trap_tracepoint
)
4729 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4730 regcache
= tctx
->regcache
;
4734 gdb_assert (regcache
!= NULL
);
4740 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4742 struct tracepoint
*tpoint
,
4743 struct traceframe
*tframe
,
4744 struct tracepoint_action
*taction
)
4746 enum eval_result_type err
;
4748 switch (taction
->type
)
4752 struct collect_memory_action
*maction
;
4753 struct eval_agent_expr_context ax_ctx
;
4755 maction
= (struct collect_memory_action
*) taction
;
4756 ax_ctx
.regcache
= NULL
;
4757 ax_ctx
.tframe
= tframe
;
4758 ax_ctx
.tpoint
= tpoint
;
4760 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4761 pulongest (maction
->len
),
4762 paddress (maction
->addr
), maction
->basereg
);
4763 /* (should use basereg) */
4764 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4770 unsigned char *regspace
;
4771 struct regcache tregcache
;
4772 struct regcache
*context_regcache
;
4775 trace_debug ("Want to collect registers");
4777 context_regcache
= get_context_regcache (ctx
);
4778 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4780 /* Collect all registers for now. */
4781 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4782 if (regspace
== NULL
)
4784 trace_debug ("Trace buffer block allocation failed, skipping");
4787 /* Identify a register block. */
4790 /* Wrap the regblock in a register cache (in the stack, we
4791 don't want to malloc here). */
4792 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4795 /* Copy the register data to the regblock. */
4796 regcache_cpy (&tregcache
, context_regcache
);
4798 #ifndef IN_PROCESS_AGENT
4799 /* On some platforms, trap-based tracepoints will have the PC
4800 pointing to the next instruction after the trap, but we
4801 don't want the user or GDB trying to guess whether the
4802 saved PC needs adjusting; so always record the adjusted
4803 stop_pc. Note that we can't use tpoint->address instead,
4804 since it will be wrong for while-stepping actions. This
4805 adjustment is a nop for fast tracepoints collected from the
4806 in-process lib (but not if GDBserver is collecting one
4807 preemptively), since the PC had already been adjusted to
4808 contain the tracepoint's address by the jump pad. */
4809 trace_debug ("Storing stop pc (0x%s) in regblock",
4810 paddress (stop_pc
));
4812 /* This changes the regblock, not the thread's
4814 regcache_write_pc (&tregcache
, stop_pc
);
4820 struct eval_expr_action
*eaction
;
4821 struct eval_agent_expr_context ax_ctx
;
4823 eaction
= (struct eval_expr_action
*) taction
;
4824 ax_ctx
.regcache
= get_context_regcache (ctx
);
4825 ax_ctx
.tframe
= tframe
;
4826 ax_ctx
.tpoint
= tpoint
;
4828 trace_debug ("Want to evaluate expression");
4830 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4832 if (err
!= expr_eval_no_error
)
4834 record_tracepoint_error (tpoint
, "action expression", err
);
4841 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4842 trace_debug ("Want to collect static trace data");
4843 collect_ust_data_at_tracepoint (ctx
, tframe
);
4845 trace_debug ("warning: collecting static trace data, "
4846 "but static tracepoints are not supported");
4851 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4857 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4858 struct tracepoint
*tpoint
)
4861 enum eval_result_type err
;
4863 /* Presently, gdbserver doesn't run compiled conditions, only the
4864 IPA does. If the program stops at a fast tracepoint's address
4865 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4866 gdbserver preemptively collect the fast tracepoint. Later, on
4867 resume, gdbserver steps over the fast tracepoint like it steps
4868 over breakpoints, so that the IPA doesn't see that fast
4869 tracepoint. This avoids double collects of fast tracepoints in
4870 that stopping scenario. Having gdbserver itself handle the fast
4871 tracepoint gives the user a consistent view of when fast or trap
4872 tracepoints are collected, compared to an alternative where only
4873 trap tracepoints are collected on stop, and fast tracepoints on
4874 resume. When a fast tracepoint is being processed by gdbserver,
4875 it is always the non-compiled condition expression that is
4877 #ifdef IN_PROCESS_AGENT
4878 if (tpoint
->compiled_cond
)
4879 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4883 struct eval_agent_expr_context ax_ctx
;
4885 ax_ctx
.regcache
= get_context_regcache (ctx
);
4886 ax_ctx
.tframe
= NULL
;
4887 ax_ctx
.tpoint
= tpoint
;
4889 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4891 if (err
!= expr_eval_no_error
)
4893 record_tracepoint_error (tpoint
, "condition", err
);
4894 /* The error case must return false. */
4898 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4899 tpoint
->number
, paddress (tpoint
->address
),
4901 return (value
? 1 : 0);
4904 /* Do memory copies for bytecodes. */
4905 /* Do the recording of memory blocks for actions and bytecodes. */
4908 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4909 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4911 unsigned char *mspace
;
4912 ULONGEST remaining
= len
;
4913 unsigned short blocklen
;
4915 /* If a 'to' buffer is specified, use it. */
4918 read_inferior_memory (from
, to
, len
);
4922 /* Otherwise, create a new memory block in the trace buffer. */
4923 while (remaining
> 0)
4927 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4928 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4929 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4932 /* Identify block as a memory block. */
4935 /* Record address and size. */
4936 memcpy (mspace
, &from
, sizeof (from
));
4937 mspace
+= sizeof (from
);
4938 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4939 mspace
+= sizeof (blocklen
);
4940 /* Record the memory block proper. */
4941 read_inferior_memory (from
, mspace
, blocklen
);
4942 trace_debug ("%d bytes recorded", blocklen
);
4943 remaining
-= blocklen
;
4950 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4951 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4953 unsigned char *buf
, *mspace
;
4954 ULONGEST remaining
= len
;
4955 unsigned short blocklen
, i
;
4957 /* To save a bit of space, block lengths are 16-bit, so break large
4958 requests into multiple blocks. Bordering on overkill for strings,
4959 but it could happen that someone specifies a large max length. */
4960 while (remaining
> 0)
4964 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4965 /* We want working space to accumulate nonzero bytes, since
4966 traceframes must have a predecided size (otherwise it gets
4967 harder to wrap correctly for the circular case, etc). */
4968 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4969 for (i
= 0; i
< blocklen
; ++i
)
4971 /* Read the string one byte at a time, in case the string is
4972 at the end of a valid memory area - we don't want a
4973 correctly-terminated string to engender segvio
4975 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4980 /* Make sure outer loop stops now too. */
4981 remaining
= blocklen
;
4985 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4986 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4992 /* Identify block as a memory block. */
4995 /* Record address and size. */
4996 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4997 mspace
+= sizeof (from
);
4998 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4999 mspace
+= sizeof (blocklen
);
5000 /* Copy the string contents. */
5001 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5002 remaining
-= blocklen
;
5009 /* Record the value of a trace state variable. */
5012 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5014 unsigned char *vspace
;
5017 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5018 1 + sizeof (n
) + sizeof (LONGEST
));
5021 /* Identify block as a variable. */
5023 /* Record variable's number and value. */
5024 memcpy (vspace
+ 1, &n
, sizeof (n
));
5025 val
= get_trace_state_variable_value (n
);
5026 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5027 trace_debug ("Variable %d recorded", n
);
5031 #ifndef IN_PROCESS_AGENT
5033 /* Callback for traceframe_walk_blocks, used to find a given block
5034 type in a traceframe. */
5037 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5039 char *wantedp
= data
;
5041 if (*wantedp
== blocktype
)
5047 /* Walk over all traceframe blocks of the traceframe buffer starting
5048 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5049 block found, passing in DATA unmodified. If CALLBACK returns true,
5050 this returns a pointer to where the block is found. Returns NULL
5051 if no callback call returned true, indicating that all blocks have
5054 static unsigned char *
5055 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5057 int (*callback
) (char blocktype
,
5058 unsigned char *dataptr
,
5062 unsigned char *dataptr
;
5066 trace_debug ("traceframe %d has no data", tfnum
);
5070 /* Iterate through a traceframe's blocks, looking for a block of the
5072 for (dataptr
= database
;
5073 dataptr
< database
+ datasize
;
5077 unsigned short mlen
;
5079 if (dataptr
== trace_buffer_wrap
)
5081 /* Adjust to reflect wrapping part of the frame around to
5083 datasize
= dataptr
- database
;
5084 dataptr
= database
= trace_buffer_lo
;
5087 blocktype
= *dataptr
++;
5089 if ((*callback
) (blocktype
, dataptr
, data
))
5095 /* Skip over the registers block. */
5096 dataptr
+= current_target_desc ()->registers_size
;
5099 /* Skip over the memory block. */
5100 dataptr
+= sizeof (CORE_ADDR
);
5101 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5102 dataptr
+= (sizeof (mlen
) + mlen
);
5105 /* Skip over the TSV block. */
5106 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5109 /* Skip over the static trace data block. */
5110 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5111 dataptr
+= (sizeof (mlen
) + mlen
);
5114 trace_debug ("traceframe %d has unknown block type 0x%x",
5123 /* Look for the block of type TYPE_WANTED in the trameframe starting
5124 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5127 static unsigned char *
5128 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5129 int tfnum
, char type_wanted
)
5131 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5132 match_blocktype
, &type_wanted
);
5135 static unsigned char *
5136 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5138 unsigned char *regblock
;
5140 regblock
= traceframe_find_block_type (tframe
->data
,
5144 if (regblock
== NULL
)
5145 trace_debug ("traceframe %d has no register data", tfnum
);
5150 /* Get registers from a traceframe. */
5153 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5155 unsigned char *dataptr
;
5156 struct tracepoint
*tpoint
;
5157 struct traceframe
*tframe
;
5159 tframe
= find_traceframe (tfnum
);
5163 trace_debug ("traceframe %d not found", tfnum
);
5167 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5168 if (dataptr
== NULL
)
5170 /* Mark registers unavailable. */
5171 supply_regblock (regcache
, NULL
);
5173 /* We can generally guess at a PC, although this will be
5174 misleading for while-stepping frames and multi-location
5176 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5178 regcache_write_pc (regcache
, tpoint
->address
);
5181 supply_regblock (regcache
, dataptr
);
5187 traceframe_get_pc (struct traceframe
*tframe
)
5189 struct regcache regcache
;
5190 unsigned char *dataptr
;
5191 const struct target_desc
*tdesc
= current_target_desc ();
5193 dataptr
= traceframe_find_regblock (tframe
, -1);
5194 if (dataptr
== NULL
)
5197 init_register_cache (®cache
, tdesc
, dataptr
);
5198 return regcache_read_pc (®cache
);
5201 /* Read a requested block of memory from a trace frame. */
5204 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5205 unsigned char *buf
, ULONGEST length
,
5208 struct traceframe
*tframe
;
5209 unsigned char *database
, *dataptr
;
5210 unsigned int datasize
;
5212 unsigned short mlen
;
5214 trace_debug ("traceframe_read_mem");
5216 tframe
= find_traceframe (tfnum
);
5220 trace_debug ("traceframe %d not found", tfnum
);
5224 datasize
= tframe
->data_size
;
5225 database
= dataptr
= &tframe
->data
[0];
5227 /* Iterate through a traceframe's blocks, looking for memory. */
5228 while ((dataptr
= traceframe_find_block_type (dataptr
,
5230 - (dataptr
- database
),
5231 tfnum
, 'M')) != NULL
)
5233 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5234 dataptr
+= sizeof (maddr
);
5235 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5236 dataptr
+= sizeof (mlen
);
5237 trace_debug ("traceframe %d has %d bytes at %s",
5238 tfnum
, mlen
, paddress (maddr
));
5240 /* If the block includes the first part of the desired range,
5241 return as much it has; GDB will re-request the remainder,
5242 which might be in a different block of this trace frame. */
5243 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5245 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5249 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5254 /* Skip over this block. */
5258 trace_debug ("traceframe %d has no memory data for the desired region",
5266 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5269 struct traceframe
*tframe
;
5270 unsigned char *database
, *dataptr
;
5271 unsigned int datasize
;
5275 trace_debug ("traceframe_read_tsv");
5277 tfnum
= current_traceframe
;
5281 trace_debug ("no current traceframe");
5285 tframe
= find_traceframe (tfnum
);
5289 trace_debug ("traceframe %d not found", tfnum
);
5293 datasize
= tframe
->data_size
;
5294 database
= dataptr
= &tframe
->data
[0];
5296 /* Iterate through a traceframe's blocks, looking for the last
5298 while ((dataptr
= traceframe_find_block_type (dataptr
,
5300 - (dataptr
- database
),
5301 tfnum
, 'V')) != NULL
)
5303 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5304 dataptr
+= sizeof (vnum
);
5306 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5308 /* Check that this is the variable we want. */
5311 memcpy (val
, dataptr
, sizeof (*val
));
5315 /* Skip over this block. */
5316 dataptr
+= sizeof (LONGEST
);
5320 trace_debug ("traceframe %d has no data for variable %d",
5325 /* Read a requested block of static tracepoint data from a trace
5329 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5330 unsigned char *buf
, ULONGEST length
,
5333 struct traceframe
*tframe
;
5334 unsigned char *database
, *dataptr
;
5335 unsigned int datasize
;
5336 unsigned short mlen
;
5338 trace_debug ("traceframe_read_sdata");
5340 tframe
= find_traceframe (tfnum
);
5344 trace_debug ("traceframe %d not found", tfnum
);
5348 datasize
= tframe
->data_size
;
5349 database
= &tframe
->data
[0];
5351 /* Iterate through a traceframe's blocks, looking for static
5353 dataptr
= traceframe_find_block_type (database
, datasize
,
5355 if (dataptr
!= NULL
)
5357 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5358 dataptr
+= sizeof (mlen
);
5361 if (offset
+ length
> mlen
)
5362 length
= mlen
- offset
;
5364 memcpy (buf
, dataptr
, length
);
5372 trace_debug ("traceframe %d has no static trace data", tfnum
);
5378 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5379 object. DATA is pointer to a struct buffer holding the
5380 traceframe-info object being built. */
5383 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5385 struct buffer
*buffer
= data
;
5391 unsigned short mlen
;
5394 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5395 dataptr
+= sizeof (maddr
);
5396 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5397 dataptr
+= sizeof (mlen
);
5398 buffer_xml_printf (buffer
,
5399 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5400 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5407 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5408 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5417 warning ("Unhandled trace block type (%d) '%c ' "
5418 "while building trace frame info.",
5419 blocktype
, blocktype
);
5426 /* Build a traceframe-info object for traceframe number TFNUM into
5430 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5432 struct traceframe
*tframe
;
5434 trace_debug ("traceframe_read_info");
5436 tframe
= find_traceframe (tfnum
);
5440 trace_debug ("traceframe %d not found", tfnum
);
5444 buffer_grow_str (buffer
, "<traceframe-info>\n");
5445 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5446 tfnum
, build_traceframe_info_xml
, buffer
);
5447 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5451 /* Return the first fast tracepoint whose jump pad contains PC. */
5453 static struct tracepoint
*
5454 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5456 struct tracepoint
*tpoint
;
5458 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5459 if (tpoint
->type
== fast_tracepoint
)
5460 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5466 /* Return the first fast tracepoint whose trampoline contains PC. */
5468 static struct tracepoint
*
5469 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5471 struct tracepoint
*tpoint
;
5473 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5475 if (tpoint
->type
== fast_tracepoint
5476 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5483 /* Return GDBserver's tracepoint that matches the IP Agent's
5484 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5487 static struct tracepoint
*
5488 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5490 struct tracepoint
*tpoint
;
5492 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5493 if (tpoint
->type
== fast_tracepoint
)
5494 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5502 /* The type of the object that is used to synchronize fast tracepoint
5505 typedef struct collecting_t
5507 /* The fast tracepoint number currently collecting. */
5510 /* A number that GDBserver can use to identify the thread that is
5511 presently holding the collect lock. This need not (and usually
5512 is not) the thread id, as getting the current thread ID usually
5513 requires a system call, which we want to avoid like the plague.
5514 Usually this is thread's TCB, found in the TLS (pseudo-)
5515 register, which is readable with a single insn on several
5517 uintptr_t thread_area
;
5520 #ifndef IN_PROCESS_AGENT
5523 force_unlock_trace_buffer (void)
5525 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5528 /* Check if the thread identified by THREAD_AREA which is stopped at
5529 STOP_PC, is presently locking the fast tracepoint collection, and
5530 if so, gather some status of said collection. Returns 0 if the
5531 thread isn't collecting or in the jump pad at all. 1, if in the
5532 jump pad (or within gdb_collect) and hasn't executed the adjusted
5533 original insn yet (can set a breakpoint there and run to it). 2,
5534 if presently executing the adjusted original insn --- in which
5535 case, if we want to move the thread out of the jump pad, we need to
5536 single-step it until this function returns 0. */
5539 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5541 struct fast_tpoint_collect_status
*status
)
5543 CORE_ADDR ipa_collecting
;
5544 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5545 CORE_ADDR ipa_gdb_trampoline_buffer
;
5546 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5547 struct tracepoint
*tpoint
;
5548 int needs_breakpoint
;
5550 /* The thread THREAD_AREA is either:
5552 0. not collecting at all, not within the jump pad, or within
5553 gdb_collect or one of its callees.
5555 1. in the jump pad and haven't reached gdb_collect
5557 2. within gdb_collect (out of the jump pad) (collect is set)
5559 3. we're in the jump pad, after gdb_collect having returned,
5560 possibly executing the adjusted insns.
5562 For cases 1 and 3, `collecting' may or not be set. The jump pad
5563 doesn't have any complicated jump logic, so we can tell if the
5564 thread is executing the adjust original insn or not by just
5565 matching STOP_PC with known jump pad addresses. If we it isn't
5566 yet executing the original insn, set a breakpoint there, and let
5567 the thread run to it, so to quickly step over a possible (many
5568 insns) gdb_collect call. Otherwise, or when the breakpoint is
5569 hit, only a few (small number of) insns are left to be executed
5570 in the jump pad. Single-step the thread until it leaves the
5575 needs_breakpoint
= 0;
5576 trace_debug ("fast_tracepoint_collecting");
5578 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5579 &ipa_gdb_jump_pad_buffer
))
5580 fatal ("error extracting `gdb_jump_pad_buffer'");
5581 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5582 &ipa_gdb_jump_pad_buffer_end
))
5583 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5585 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5586 &ipa_gdb_trampoline_buffer
))
5587 fatal ("error extracting `gdb_trampoline_buffer'");
5588 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5589 &ipa_gdb_trampoline_buffer_end
))
5590 fatal ("error extracting `gdb_trampoline_buffer_end'");
5592 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5593 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5595 /* We can tell which tracepoint(s) the thread is collecting by
5596 matching the jump pad address back to the tracepoint. */
5597 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5600 warning ("in jump pad, but no matching tpoint?");
5605 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5607 tpoint
->number
, paddress (tpoint
->address
),
5608 paddress (tpoint
->jump_pad
),
5609 paddress (tpoint
->jump_pad_end
),
5610 paddress (tpoint
->adjusted_insn_addr
),
5611 paddress (tpoint
->adjusted_insn_addr_end
));
5614 /* Definitely in the jump pad. May or may not need
5615 fast-exit-jump-pad breakpoint. */
5616 if (tpoint
->jump_pad
<= stop_pc
5617 && stop_pc
< tpoint
->adjusted_insn_addr
)
5618 needs_breakpoint
= 1;
5620 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5621 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5623 /* We can tell which tracepoint(s) the thread is collecting by
5624 matching the trampoline address back to the tracepoint. */
5625 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5628 warning ("in trampoline, but no matching tpoint?");
5633 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5634 tpoint
->number
, paddress (tpoint
->address
),
5635 paddress (tpoint
->trampoline
),
5636 paddress (tpoint
->trampoline_end
));
5639 /* Have not reached jump pad yet, but treat the trampoline as a
5640 part of the jump pad that is before the adjusted original
5642 needs_breakpoint
= 1;
5646 collecting_t ipa_collecting_obj
;
5648 /* If `collecting' is set/locked, then the THREAD_AREA thread
5649 may or not be the one holding the lock. We have to read the
5650 lock to find out. */
5652 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5655 trace_debug ("fast_tracepoint_collecting:"
5656 " failed reading 'collecting' in the inferior");
5660 if (!ipa_collecting
)
5662 trace_debug ("fast_tracepoint_collecting: not collecting"
5663 " (and nobody is).");
5667 /* Some thread is collecting. Check which. */
5668 if (read_inferior_memory (ipa_collecting
,
5669 (unsigned char *) &ipa_collecting_obj
,
5670 sizeof (ipa_collecting_obj
)) != 0)
5673 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5675 trace_debug ("fast_tracepoint_collecting: not collecting "
5676 "(another thread is)");
5681 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5684 warning ("fast_tracepoint_collecting: collecting, "
5685 "but tpoint %s not found?",
5686 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5690 /* The thread is within `gdb_collect', skip over the rest of
5691 fast tracepoint collection quickly using a breakpoint. */
5692 needs_breakpoint
= 1;
5695 /* The caller wants a bit of status detail. */
5698 status
->tpoint_num
= tpoint
->number
;
5699 status
->tpoint_addr
= tpoint
->address
;
5700 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5701 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5704 if (needs_breakpoint
)
5706 /* Hasn't executed the original instruction yet. Set breakpoint
5707 there, and wait till it's hit, then single-step until exiting
5711 fast_tracepoint_collecting, returning continue-until-break at %s",
5712 paddress (tpoint
->adjusted_insn_addr
));
5714 return 1; /* continue */
5718 /* Just single-step until exiting the jump pad. */
5720 trace_debug ("fast_tracepoint_collecting, returning "
5721 "need-single-step (%s-%s)",
5722 paddress (tpoint
->adjusted_insn_addr
),
5723 paddress (tpoint
->adjusted_insn_addr_end
));
5725 return 2; /* single-step */
5731 #ifdef IN_PROCESS_AGENT
5733 /* The global fast tracepoint collect lock. Points to a collecting_t
5734 object built on the stack by the jump pad, if presently locked;
5735 NULL if it isn't locked. Note that this lock *must* be set while
5736 executing any *function other than the jump pad. See
5737 fast_tracepoint_collecting. */
5738 static collecting_t
* ATTR_USED collecting
;
5740 /* This routine, called from the jump pad (in asm) is designed to be
5741 called from the jump pads of fast tracepoints, thus it is on the
5744 IP_AGENT_EXPORT
void ATTR_USED
5745 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5747 struct fast_tracepoint_ctx ctx
;
5749 /* Don't do anything until the trace run is completely set up. */
5753 ctx
.base
.type
= fast_tracepoint
;
5755 ctx
.regcache_initted
= 0;
5756 /* Wrap the regblock in a register cache (in the stack, we don't
5757 want to malloc here). */
5758 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
5759 if (ctx
.regspace
== NULL
)
5761 trace_debug ("Trace buffer block allocation failed, skipping");
5765 for (ctx
.tpoint
= tpoint
;
5766 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5767 ctx
.tpoint
= ctx
.tpoint
->next
)
5769 if (!ctx
.tpoint
->enabled
)
5772 /* Multiple tracepoints of different types, such as fast tracepoint and
5773 static tracepoint, can be set at the same address. */
5774 if (ctx
.tpoint
->type
!= tpoint
->type
)
5777 /* Test the condition if present, and collect if true. */
5778 if (ctx
.tpoint
->cond
== NULL
5779 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5782 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5783 ctx
.tpoint
->address
, ctx
.tpoint
);
5785 /* Note that this will cause original insns to be written back
5786 to where we jumped from, but that's OK because we're jumping
5787 back to the next whole instruction. This will go badly if
5788 instruction restoration is not atomic though. */
5789 if (stopping_tracepoint
5790 || trace_buffer_is_full
5791 || expr_eval_result
!= expr_eval_no_error
)
5799 /* If there was a condition and it evaluated to false, the only
5800 way we would stop tracing is if there was an error during
5801 condition expression evaluation. */
5802 if (expr_eval_result
!= expr_eval_no_error
)
5813 #ifndef IN_PROCESS_AGENT
5816 get_raw_reg_func_addr (void)
5818 return ipa_sym_addrs
.addr_get_raw_reg
;
5822 get_get_tsv_func_addr (void)
5824 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5828 get_set_tsv_func_addr (void)
5830 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5834 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5835 CORE_ADDR
*jump_entry
)
5837 CORE_ADDR entry_point
= *jump_entry
;
5838 enum eval_result_type err
;
5840 trace_debug ("Starting condition compilation for tracepoint %d\n",
5843 /* Initialize the global pointer to the code being built. */
5844 current_insn_ptr
= *jump_entry
;
5848 err
= compile_bytecodes (tpoint
->cond
);
5850 if (err
== expr_eval_no_error
)
5854 /* Record the beginning of the compiled code. */
5855 tpoint
->compiled_cond
= entry_point
;
5857 trace_debug ("Condition compilation for tracepoint %d complete\n",
5862 /* Leave the unfinished code in situ, but don't point to it. */
5864 tpoint
->compiled_cond
= 0;
5866 trace_debug ("Condition compilation for tracepoint %d failed, "
5868 tpoint
->number
, err
);
5871 /* Update the code pointer passed in. Note that we do this even if
5872 the compile fails, so that we can look at the partial results
5873 instead of letting them be overwritten. */
5874 *jump_entry
= current_insn_ptr
;
5876 /* Leave a gap, to aid dump decipherment. */
5880 /* We'll need to adjust these when we consider bi-arch setups, and big
5884 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5886 return write_inferior_memory (where
,
5887 (unsigned char *) &ptr
, sizeof (void *));
5890 /* The base pointer of the IPA's heap. This is the only memory the
5891 IPA is allowed to use. The IPA should _not_ call the inferior's
5892 `malloc' during operation. That'd be slow, and, most importantly,
5893 it may not be safe. We may be collecting a tracepoint in a signal
5894 handler, for example. */
5895 static CORE_ADDR target_tp_heap
;
5897 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5901 target_malloc (ULONGEST size
)
5905 if (target_tp_heap
== 0)
5907 /* We have the pointer *address*, need what it points to. */
5908 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5910 fatal ("could get target heap head pointer");
5913 ptr
= target_tp_heap
;
5914 target_tp_heap
+= size
;
5916 /* Pad to 8-byte alignment. */
5917 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5923 download_agent_expr (struct agent_expr
*expr
)
5925 CORE_ADDR expr_addr
;
5926 CORE_ADDR expr_bytes
;
5928 expr_addr
= target_malloc (sizeof (*expr
));
5929 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5931 expr_bytes
= target_malloc (expr
->length
);
5932 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5934 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5939 /* Align V up to N bits. */
5940 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5942 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5945 download_tracepoint_1 (struct tracepoint
*tpoint
)
5947 struct tracepoint target_tracepoint
;
5948 CORE_ADDR tpptr
= 0;
5950 gdb_assert (tpoint
->type
== fast_tracepoint
5951 || tpoint
->type
== static_tracepoint
);
5953 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5955 CORE_ADDR jentry
, jump_entry
;
5957 jentry
= jump_entry
= get_jump_space_head ();
5959 if (tpoint
->cond
!= NULL
)
5961 /* Pad to 8-byte alignment. (needed?) */
5962 /* Actually this should be left for the target to
5964 jentry
= UALIGN (jentry
, 8);
5966 compile_tracepoint_condition (tpoint
, &jentry
);
5969 /* Pad to 8-byte alignment. */
5970 jentry
= UALIGN (jentry
, 8);
5971 claim_jump_space (jentry
- jump_entry
);
5974 target_tracepoint
= *tpoint
;
5976 tpptr
= target_malloc (sizeof (*tpoint
));
5977 tpoint
->obj_addr_on_target
= tpptr
;
5979 /* Write the whole object. We'll fix up its pointers in a bit.
5980 Assume no next for now. This is fixed up above on the next
5981 iteration, if there's any. */
5982 target_tracepoint
.next
= NULL
;
5983 /* Need to clear this here too, since we're downloading the
5984 tracepoints before clearing our own copy. */
5985 target_tracepoint
.hit_count
= 0;
5987 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5988 sizeof (target_tracepoint
));
5991 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5993 download_agent_expr (tpoint
->cond
));
5995 if (tpoint
->numactions
)
5998 CORE_ADDR actions_array
;
6000 /* The pointers array. */
6002 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6003 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6007 /* Now for each pointer, download the action. */
6008 for (i
= 0; i
< tpoint
->numactions
; i
++)
6010 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6011 CORE_ADDR ipa_action
= action
->ops
->download (action
);
6013 if (ipa_action
!= 0)
6014 write_inferior_data_ptr
6015 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6021 #define IPA_PROTO_FAST_TRACE_FLAG 0
6022 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6023 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6024 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6025 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6027 /* Send a command to agent to download and install tracepoint TPOINT. */
6030 tracepoint_send_agent (struct tracepoint
*tpoint
)
6032 char buf
[IPA_CMD_BUF_SIZE
];
6037 strcpy (p
, "FastTrace:");
6040 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6041 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6042 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6043 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6044 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6045 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6046 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6047 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6048 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6049 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6050 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6053 p
= agent_expr_send (p
, tpoint
->cond
);
6055 /* tracepoint_action */
6056 for (i
= 0; i
< tpoint
->numactions
; i
++)
6058 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6060 p
[0] = action
->type
;
6061 p
= action
->ops
->send (&p
[1], action
);
6064 get_jump_space_head ();
6065 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6066 agent can use jump pad from it. */
6067 if (tpoint
->type
== fast_tracepoint
)
6069 memcpy (p
, &gdb_jump_pad_head
, 8);
6073 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6077 if (strncmp (buf
, "OK", 2) != 0)
6080 /* The value of tracepoint's target address is stored in BUF. */
6081 memcpy (&tpoint
->obj_addr_on_target
,
6082 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6084 if (tpoint
->type
== fast_tracepoint
)
6087 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6090 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6091 (unsigned int) tpoint
->obj_addr_on_target
,
6092 (unsigned int) gdb_jump_pad_head
);
6094 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6096 /* This has been done in agent. We should also set up record for it. */
6097 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6100 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6107 download_tracepoint (struct tracepoint
*tpoint
)
6109 struct tracepoint
*tp
, *tp_prev
;
6111 if (tpoint
->type
!= fast_tracepoint
6112 && tpoint
->type
!= static_tracepoint
)
6115 download_tracepoint_1 (tpoint
);
6117 /* Find the previous entry of TPOINT, which is fast tracepoint or
6118 static tracepoint. */
6120 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6122 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6128 CORE_ADDR tp_prev_target_next_addr
;
6130 /* Insert TPOINT after TP_PREV in IPA. */
6131 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6132 + offsetof (struct tracepoint
, next
),
6133 &tp_prev_target_next_addr
))
6134 fatal ("error reading `tp_prev->next'");
6136 /* tpoint->next = tp_prev->next */
6137 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6138 + offsetof (struct tracepoint
, next
),
6139 tp_prev_target_next_addr
);
6140 /* tp_prev->next = tpoint */
6141 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6142 + offsetof (struct tracepoint
, next
),
6143 tpoint
->obj_addr_on_target
);
6146 /* First object in list, set the head pointer in the
6148 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6149 tpoint
->obj_addr_on_target
);
6154 download_trace_state_variables (void)
6156 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6157 struct trace_state_variable
*tsv
;
6159 /* Start out empty. */
6160 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6162 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6164 struct trace_state_variable target_tsv
;
6166 /* TSV's with a getter have been initialized equally in both the
6167 inferior and GDBserver. Skip them. */
6168 if (tsv
->getter
!= NULL
)
6174 ptr
= target_malloc (sizeof (*tsv
));
6176 if (tsv
== trace_state_variables
)
6178 /* First object in list, set the head pointer in the
6181 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6186 write_inferior_data_ptr (prev_ptr
6187 + offsetof (struct trace_state_variable
,
6192 /* Write the whole object. We'll fix up its pointers in a bit.
6193 Assume no next, fixup when needed. */
6194 target_tsv
.next
= NULL
;
6196 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6197 sizeof (target_tsv
));
6199 if (tsv
->name
!= NULL
)
6201 size_t size
= strlen (tsv
->name
) + 1;
6202 CORE_ADDR name_addr
= target_malloc (size
);
6203 write_inferior_memory (name_addr
,
6204 (unsigned char *) tsv
->name
, size
);
6205 write_inferior_data_ptr (ptr
6206 + offsetof (struct trace_state_variable
,
6211 if (tsv
->getter
!= NULL
)
6213 fatal ("what to do with these?");
6219 /* Fixup the next pointer in the last item in the list. */
6220 write_inferior_data_ptr (prev_ptr
6221 + offsetof (struct trace_state_variable
,
6226 /* Upload complete trace frames out of the IP Agent's trace buffer
6227 into GDBserver's trace buffer. This always uploads either all or
6228 no trace frames. This is the counter part of
6229 `trace_alloc_trace_buffer'. See its description of the atomic
6230 synching mechanism. */
6233 upload_fast_traceframes (void)
6235 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6236 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6238 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6239 unsigned int curr_tbctrl_idx
;
6240 unsigned int ipa_trace_buffer_ctrl_curr
;
6241 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6242 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6243 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6244 CORE_ADDR ipa_trace_buffer_lo
;
6245 CORE_ADDR ipa_trace_buffer_hi
;
6247 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6248 &ipa_traceframe_read_count_racy
))
6250 /* This will happen in most targets if the current thread is
6255 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6256 &ipa_traceframe_write_count_racy
))
6259 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6260 ipa_traceframe_write_count_racy
6261 - ipa_traceframe_read_count_racy
,
6262 ipa_traceframe_write_count_racy
,
6263 ipa_traceframe_read_count_racy
);
6265 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6268 about_to_request_buffer_space_bkpt
6269 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6272 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6273 &ipa_trace_buffer_ctrl_curr
))
6276 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6278 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6281 unsigned int prev
, counter
;
6283 /* Update the token, with new counters, and the GDBserver stamp
6284 bit. Alway reuse the current TBC index. */
6285 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6286 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6288 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6294 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6295 ipa_trace_buffer_ctrl_curr
))
6298 trace_debug ("Lib: Committed %08x -> %08x",
6299 ipa_trace_buffer_ctrl_curr_old
,
6300 ipa_trace_buffer_ctrl_curr
);
6302 /* Re-read these, now that we've installed the
6303 `about_to_request_buffer_space' breakpoint/lock. A thread could
6304 have finished a traceframe between the last read of these
6305 counters and setting the breakpoint above. If we start
6306 uploading, we never want to leave this function with
6307 traceframe_read_count != 0, otherwise, GDBserver could end up
6308 incrementing the counter tokens more than once (due to event loop
6309 nesting), which would break the IP agent's "effective" detection
6310 (see trace_alloc_trace_buffer). */
6311 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6312 &ipa_traceframe_read_count
))
6314 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6315 &ipa_traceframe_write_count
))
6320 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6321 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6322 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6324 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6325 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6326 trace_debug ("note that ipa_traceframe_count's parts changed");
6329 /* Get the address of the current TBC object (the IP agent has an
6330 array of 3 such objects). The index is stored in the TBC
6332 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6333 ipa_trace_buffer_ctrl_addr
6334 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6336 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6337 (unsigned char *) &ipa_trace_buffer_ctrl
,
6338 sizeof (struct ipa_trace_buffer_control
)))
6341 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6342 &ipa_trace_buffer_lo
))
6344 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6345 &ipa_trace_buffer_hi
))
6348 /* Offsets are easier to grok for debugging than raw addresses,
6349 especially for the small trace buffer sizes that are useful for
6351 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6352 "endfree=%d wrap=%d hi=%d",
6354 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6355 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6356 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6357 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6358 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6360 /* Note that the IPA's buffer is always circular. */
6362 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6364 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6365 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6367 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6368 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6369 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6370 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6373 tf
= IPA_FIRST_TRACEFRAME ();
6375 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6377 struct tracepoint
*tpoint
;
6378 struct traceframe
*tframe
;
6379 unsigned char *block
;
6380 struct traceframe ipa_tframe
;
6382 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6383 offsetof (struct traceframe
, data
)))
6384 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6386 if (ipa_tframe
.tpnum
== 0)
6387 fatal ("Uploading: No (more) fast traceframes, but "
6388 "ipa_traceframe_count == %u??\n",
6389 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6391 /* Note that this will be incorrect for multi-location
6393 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6395 tframe
= add_traceframe (tpoint
);
6398 trace_buffer_is_full
= 1;
6399 trace_debug ("Uploading: trace buffer is full");
6403 /* Copy the whole set of blocks in one go for now. FIXME:
6404 split this in smaller blocks. */
6405 block
= add_traceframe_block (tframe
, tpoint
,
6406 ipa_tframe
.data_size
);
6409 if (read_inferior_memory (tf
6410 + offsetof (struct traceframe
, data
),
6411 block
, ipa_tframe
.data_size
))
6412 error ("Uploading: Couldn't read traceframe data at %s\n",
6413 paddress (tf
+ offsetof (struct traceframe
, data
)));
6416 trace_debug ("Uploading: traceframe didn't fit");
6417 finish_traceframe (tframe
);
6420 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6422 /* If we freed the traceframe that wrapped around, go back
6423 to the non-wrap case. */
6424 if (tf
< ipa_trace_buffer_ctrl
.start
)
6426 trace_debug ("Lib: Discarding past the wraparound");
6427 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6429 ipa_trace_buffer_ctrl
.start
= tf
;
6430 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6431 ++ipa_traceframe_read_count
;
6433 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6434 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6436 trace_debug ("Lib: buffer is fully empty. "
6437 "Trace buffer [%d] start=%d free=%d endfree=%d",
6439 (int) (ipa_trace_buffer_ctrl
.start
6440 - ipa_trace_buffer_lo
),
6441 (int) (ipa_trace_buffer_ctrl
.free
6442 - ipa_trace_buffer_lo
),
6443 (int) (ipa_trace_buffer_ctrl
.end_free
6444 - ipa_trace_buffer_lo
));
6446 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6447 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6448 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6449 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6452 trace_debug ("Uploaded a traceframe\n"
6453 "Lib: Trace buffer [%d] start=%d free=%d "
6454 "endfree=%d wrap=%d hi=%d",
6456 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6457 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6458 (int) (ipa_trace_buffer_ctrl
.end_free
6459 - ipa_trace_buffer_lo
),
6460 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6461 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6464 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6465 (unsigned char *) &ipa_trace_buffer_ctrl
,
6466 sizeof (struct ipa_trace_buffer_control
)))
6469 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6470 ipa_traceframe_read_count
);
6472 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6475 cancel_breakpoints ();
6477 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6478 about_to_request_buffer_space_bkpt
= NULL
;
6482 if (trace_buffer_is_full
)
6487 #ifdef IN_PROCESS_AGENT
6489 IP_AGENT_EXPORT
int ust_loaded
;
6490 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6494 /* Static tracepoints. */
6496 /* UST puts a "struct tracepoint" in the global namespace, which
6497 conflicts with our tracepoint. Arguably, being a library, it
6498 shouldn't take ownership of such a generic name. We work around it
6500 #define tracepoint ust_tracepoint
6501 #include <ust/ust.h>
6504 extern int serialize_to_text (char *outbuf
, int bufsize
,
6505 const char *fmt
, va_list ap
);
6507 #define GDB_PROBE_NAME "gdb"
6509 /* We dynamically search for the UST symbols instead of linking them
6510 in. This lets the user decide if the application uses static
6511 tracepoints, instead of always pulling libust.so in. This vector
6512 holds pointers to all functions we care about. */
6516 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6517 const char *fmt
, va_list ap
);
6519 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6520 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6522 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6524 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6527 void (*marker_iter_start
) (struct marker_iter
*iter
);
6528 void (*marker_iter_next
) (struct marker_iter
*iter
);
6529 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6530 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6535 /* Cast through typeof to catch incompatible API changes. Since UST
6536 only builds with gcc, we can freely use gcc extensions here
6538 #define GET_UST_SYM(SYM) \
6541 if (ust_ops.SYM == NULL) \
6542 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6543 if (ust_ops.SYM == NULL) \
6547 #define USTF(SYM) ust_ops.SYM
6549 /* Get pointers to all libust.so functions we care about. */
6554 GET_UST_SYM (serialize_to_text
);
6556 GET_UST_SYM (ltt_probe_register
);
6557 GET_UST_SYM (ltt_probe_unregister
);
6558 GET_UST_SYM (ltt_marker_connect
);
6559 GET_UST_SYM (ltt_marker_disconnect
);
6561 GET_UST_SYM (marker_iter_start
);
6562 GET_UST_SYM (marker_iter_next
);
6563 GET_UST_SYM (marker_iter_stop
);
6564 GET_UST_SYM (marker_iter_reset
);
6570 /* Given an UST marker, return the matching gdb static tracepoint.
6571 The match is done by address. */
6573 static struct tracepoint
*
6574 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6576 struct tracepoint
*tpoint
;
6578 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6580 if (tpoint
->type
!= static_tracepoint
)
6583 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6590 /* The probe function we install on lttng/ust markers. Whenever a
6591 probed ust marker is hit, this function is called. This is similar
6592 to gdb_collect, only for static tracepoints, instead of fast
6596 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6597 struct registers
*regs
, void *call_private
,
6598 const char *fmt
, va_list *args
)
6600 struct tracepoint
*tpoint
;
6601 struct static_tracepoint_ctx ctx
;
6603 /* Don't do anything until the trace run is completely set up. */
6606 trace_debug ("gdb_probe: not tracing\n");
6610 ctx
.base
.type
= static_tracepoint
;
6611 ctx
.regcache_initted
= 0;
6616 /* Wrap the regblock in a register cache (in the stack, we don't
6617 want to malloc here). */
6618 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6619 if (ctx
.regspace
== NULL
)
6621 trace_debug ("Trace buffer block allocation failed, skipping");
6625 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6628 trace_debug ("gdb_probe: marker not known: "
6629 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6630 mdata
->location
, mdata
->channel
,
6631 mdata
->name
, mdata
->format
);
6635 if (!tpoint
->enabled
)
6637 trace_debug ("gdb_probe: tracepoint disabled");
6641 ctx
.tpoint
= tpoint
;
6643 trace_debug ("gdb_probe: collecting marker: "
6644 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6645 mdata
->location
, mdata
->channel
,
6646 mdata
->name
, mdata
->format
);
6648 /* Test the condition if present, and collect if true. */
6649 if (tpoint
->cond
== NULL
6650 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6653 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6654 tpoint
->address
, tpoint
);
6656 if (stopping_tracepoint
6657 || trace_buffer_is_full
6658 || expr_eval_result
!= expr_eval_no_error
)
6663 /* If there was a condition and it evaluated to false, the only
6664 way we would stop tracing is if there was an error during
6665 condition expression evaluation. */
6666 if (expr_eval_result
!= expr_eval_no_error
)
6671 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6672 static tracepoint string data. This is a string passed to the
6673 tracing library by the user, at the time of the tracepoint marker
6674 call. E.g., in the UST marker call:
6676 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6678 the collected data is "str FOOBAZ".
6682 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6683 struct traceframe
*tframe
)
6685 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6686 unsigned char *bufspace
;
6689 unsigned short blocklen
;
6693 trace_debug ("Wanted to collect static trace data, "
6694 "but there's no static trace data");
6698 va_copy (copy
, *umd
->args
);
6699 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6702 trace_debug ("Want to collect ust data");
6704 /* 'S' + size + string */
6705 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6706 1 + sizeof (blocklen
) + size
+ 1);
6707 if (bufspace
== NULL
)
6709 trace_debug ("Trace buffer block allocation failed, skipping");
6713 /* Identify a static trace data block. */
6716 blocklen
= size
+ 1;
6717 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6719 va_copy (copy
, *umd
->args
);
6720 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6721 size
+ 1, umd
->fmt
, copy
);
6724 trace_debug ("Storing static tracepoint data in regblock: %s",
6725 bufspace
+ 1 + sizeof (blocklen
));
6728 /* The probe to register with lttng/ust. */
6729 static struct ltt_available_probe gdb_ust_probe
=
6736 #endif /* HAVE_UST */
6737 #endif /* IN_PROCESS_AGENT */
6739 #ifndef IN_PROCESS_AGENT
6741 /* Ask the in-process agent to run a command. Since we don't want to
6742 have to handle the IPA hitting breakpoints while running the
6743 command, we pause all threads, remove all breakpoints, and then set
6744 the helper thread re-running. We communicate with the helper
6745 thread by means of direct memory xfering, and a socket for
6749 run_inferior_command (char *cmd
, int len
)
6752 int pid
= ptid_get_pid (current_ptid
);
6754 trace_debug ("run_inferior_command: running: %s", cmd
);
6757 uninsert_all_breakpoints ();
6759 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6761 reinsert_all_breakpoints ();
6767 #else /* !IN_PROCESS_AGENT */
6769 #include <sys/socket.h>
6772 #ifndef UNIX_PATH_MAX
6773 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6776 /* Where we put the socked used for synchronization. */
6777 #define SOCK_DIR P_tmpdir
6779 /* Thread ID of the helper thread. GDBserver reads this to know which
6780 is the help thread. This is an LWP id on Linux. */
6781 int helper_thread_id
;
6784 init_named_socket (const char *name
)
6787 struct sockaddr_un addr
;
6789 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6792 warning ("socket creation failed: %s", strerror (errno
));
6796 addr
.sun_family
= AF_UNIX
;
6798 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6799 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6801 result
= access (name
, F_OK
);
6805 result
= unlink (name
);
6808 warning ("unlink failed: %s", strerror (errno
));
6812 warning ("socket %s already exists; overwriting", name
);
6815 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6818 warning ("bind failed: %s", strerror (errno
));
6823 result
= listen (fd
, 1);
6826 warning ("listen: %s", strerror (errno
));
6834 static char agent_socket_name
[UNIX_PATH_MAX
];
6837 gdb_agent_socket_init (void)
6841 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6842 SOCK_DIR
, getpid ());
6843 if (result
>= UNIX_PATH_MAX
)
6845 trace_debug ("string overflow allocating socket name");
6849 fd
= init_named_socket (agent_socket_name
);
6851 warning ("Error initializing named socket (%s) for communication with the "
6852 "ust helper thread. Check that directory exists and that it "
6853 "is writable.", agent_socket_name
);
6860 /* The next marker to be returned on a qTsSTM command. */
6861 static const struct marker
*next_st
;
6863 /* Returns the first known marker. */
6868 struct marker_iter iter
;
6870 USTF(marker_iter_reset
) (&iter
);
6871 USTF(marker_iter_start
) (&iter
);
6876 /* Returns the marker following M. */
6878 const struct marker
*
6879 next_marker (const struct marker
*m
)
6881 struct marker_iter iter
;
6883 USTF(marker_iter_reset
) (&iter
);
6884 USTF(marker_iter_start
) (&iter
);
6886 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6888 if (iter
.marker
== m
)
6890 USTF(marker_iter_next
) (&iter
);
6898 /* Return an hexstr version of the STR C string, fit for sending to
6902 cstr_to_hexstr (const char *str
)
6904 int len
= strlen (str
);
6905 char *hexstr
= xmalloc (len
* 2 + 1);
6906 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6910 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6914 response_ust_marker (char *packet
, const struct marker
*st
)
6916 char *strid
, *format
, *tmp
;
6918 next_st
= next_marker (st
);
6920 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6921 strlen (st
->name
) + 1);
6922 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6924 strid
= cstr_to_hexstr (tmp
);
6927 format
= cstr_to_hexstr (st
->format
);
6929 sprintf (packet
, "m%s:%s:%s",
6930 paddress ((uintptr_t) st
->location
),
6938 /* Return the first static tracepoint, and initialize the state
6939 machine that will iterate through all the static tracepoints. */
6942 cmd_qtfstm (char *packet
)
6944 trace_debug ("Returning first trace state variable definition");
6946 if (first_marker ())
6947 response_ust_marker (packet
, first_marker ());
6949 strcpy (packet
, "l");
6952 /* Return additional trace state variable definitions. */
6955 cmd_qtsstm (char *packet
)
6957 trace_debug ("Returning static tracepoint");
6960 response_ust_marker (packet
, next_st
);
6962 strcpy (packet
, "l");
6965 /* Disconnect the GDB probe from a marker at a given address. */
6968 unprobe_marker_at (char *packet
)
6972 struct marker_iter iter
;
6974 p
+= sizeof ("unprobe_marker_at:") - 1;
6976 p
= unpack_varlen_hex (p
, &address
);
6978 USTF(marker_iter_reset
) (&iter
);
6979 USTF(marker_iter_start
) (&iter
);
6980 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6981 if ((uintptr_t ) iter
.marker
->location
== address
)
6985 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6989 warning ("could not disable marker %s/%s",
6990 iter
.marker
->channel
, iter
.marker
->name
);
6995 /* Connect the GDB probe to a marker at a given address. */
6998 probe_marker_at (char *packet
)
7002 struct marker_iter iter
;
7005 p
+= sizeof ("probe_marker_at:") - 1;
7007 p
= unpack_varlen_hex (p
, &address
);
7009 USTF(marker_iter_reset
) (&iter
);
7011 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7013 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7014 if ((uintptr_t ) m
->location
== address
)
7018 trace_debug ("found marker for address. "
7019 "ltt_marker_connect (marker = %s/%s)",
7020 m
->channel
, m
->name
);
7022 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7024 if (result
&& result
!= -EEXIST
)
7025 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7026 m
->channel
, m
->name
, -result
);
7030 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7031 m
->channel
, m
->name
);
7035 strcpy (packet
, "OK");
7039 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7044 cmd_qtstmat (char *packet
)
7048 struct marker_iter iter
;
7051 p
+= sizeof ("qTSTMat:") - 1;
7053 p
= unpack_varlen_hex (p
, &address
);
7055 USTF(marker_iter_reset
) (&iter
);
7057 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7059 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7060 if ((uintptr_t ) m
->location
== address
)
7062 response_ust_marker (packet
, m
);
7066 strcpy (packet
, "l");
7076 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7079 #endif /* HAVE_UST */
7081 #include <sys/syscall.h>
7085 gdb_agent_remove_socket (void)
7087 unlink (agent_socket_name
);
7090 /* Helper thread of agent. */
7093 gdb_agent_helper_thread (void *arg
)
7097 atexit (gdb_agent_remove_socket
);
7101 listen_fd
= gdb_agent_socket_init ();
7103 if (helper_thread_id
== 0)
7104 helper_thread_id
= syscall (SYS_gettid
);
7106 if (listen_fd
== -1)
7108 warning ("could not create sync socket\n");
7115 struct sockaddr_un sockaddr
;
7121 tmp
= sizeof (sockaddr
);
7125 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7127 /* It seems an ERESTARTSYS can escape out of accept. */
7128 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7132 warning ("Accept returned %d, error: %s\n",
7133 fd
, strerror (errno
));
7139 ret
= read (fd
, buf
, 1);
7140 } while (ret
== -1 && errno
== EINTR
);
7144 warning ("reading socket (fd=%d) failed with %s",
7145 fd
, strerror (errno
));
7152 if (strncmp ("close", cmd_buf
, 5) == 0)
7157 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7159 cmd_qtfstm (cmd_buf
);
7161 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7163 cmd_qtsstm (cmd_buf
);
7165 else if (strncmp ("unprobe_marker_at:",
7167 sizeof ("unprobe_marker_at:") - 1) == 0)
7169 unprobe_marker_at (cmd_buf
);
7171 else if (strncmp ("probe_marker_at:",
7173 sizeof ("probe_marker_at:") - 1) == 0)
7175 probe_marker_at (cmd_buf
);
7177 else if (strncmp ("qTSTMat:",
7179 sizeof ("qTSTMat:") - 1) == 0)
7181 cmd_qtstmat (cmd_buf
);
7183 #endif /* HAVE_UST */
7186 /* Fix compiler's warning: ignoring return value of 'write'. */
7187 ret
= write (fd
, buf
, 1);
7193 unlink (agent_socket_name
);
7195 /* Sleep endlessly to wait the whole inferior stops. This
7196 thread can not exit because GDB or GDBserver may still need
7197 'current_inferior' (representing this thread) to access
7198 inferior memory. Otherwise, this thread exits earlier than
7199 other threads, and 'current_inferior' is set to NULL. */
7210 #include <pthread.h>
7212 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7215 gdb_agent_init (void)
7222 /* We want the helper thread to be as transparent as possible, so
7223 have it inherit an all-signals-blocked mask. */
7225 sigfillset (&new_mask
);
7226 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7228 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7230 res
= pthread_create (&thread
,
7232 gdb_agent_helper_thread
,
7235 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7237 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7239 while (helper_thread_id
== 0)
7247 #include <sys/mman.h>
7250 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7251 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7252 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7253 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7254 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7255 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7257 /* Record the result of getting buffer space for fast tracepoint
7258 trampolines. Any error message is copied, since caller may not be
7259 using persistent storage. */
7262 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7264 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7265 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7267 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7269 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7272 static void __attribute__ ((constructor
))
7273 initialize_tracepoint_ftlib (void)
7275 initialize_tracepoint ();
7280 #endif /* IN_PROCESS_AGENT */
7282 /* Return a timestamp, expressed as microseconds of the usual Unix
7283 time. (As the result is a 64-bit number, it will not overflow any
7287 get_timestamp (void)
7291 if (gettimeofday (&tv
, 0) != 0)
7294 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7298 initialize_tracepoint (void)
7300 /* Start with the default size. */
7301 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7303 /* Wire trace state variable 1 to be the timestamp. This will be
7304 uploaded to GDB upon connection and become one of its trace state
7305 variables. (In case you're wondering, if GDB already has a trace
7306 variable numbered 1, it will be renumbered.) */
7307 create_trace_state_variable (1, 0);
7308 set_trace_state_variable_name (1, "trace_timestamp");
7309 set_trace_state_variable_getter (1, get_timestamp
);
7311 #ifdef IN_PROCESS_AGENT
7316 pagesize
= sysconf (_SC_PAGE_SIZE
);
7320 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7322 #define SCRATCH_BUFFER_NPAGES 20
7324 /* Allocate scratch buffer aligned on a page boundary, at a low
7325 address (close to the main executable's code). */
7326 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7328 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7329 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7330 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7332 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7338 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7341 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7344 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7346 /* It's not a fatal error for something to go wrong with trampoline
7347 buffer setup, but it can be mysterious, so create a channel to
7348 report back on what went wrong, using a fixed size since we may
7349 not be able to allocate space later when the problem occurs. */
7350 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7352 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7354 initialize_low_tracepoint ();