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"
33 /* This file is built for both GDBserver, and the in-process
34 agent (IPA), a shared library that includes a tracing agent that is
35 loaded by the inferior to support fast tracepoints. Fast
36 tracepoints (or more accurately, jump based tracepoints) are
37 implemented by patching the tracepoint location with a jump into a
38 small trampoline function whose job is to save the register state,
39 call the in-process tracing agent, and then execute the original
40 instruction that was under the tracepoint jump (possibly adjusted,
41 if PC-relative, or some such).
43 The current synchronization design is pull based. That means,
44 GDBserver does most of the work, by peeking/poking at the inferior
45 agent's memory directly for downloading tracepoint and associated
46 objects, and for uploading trace frames. Whenever the IPA needs
47 something from GDBserver (trace buffer is full, tracing stopped for
48 some reason, etc.) the IPA calls a corresponding hook function
49 where GDBserver has placed a breakpoint.
51 Each of the agents has its own trace buffer. When browsing the
52 trace frames built from slow and fast tracepoints from GDB (tfind
53 mode), there's no guarantee the user is seeing the trace frames in
54 strict chronological creation order, although, GDBserver tries to
55 keep the order relatively reasonable, by syncing the trace buffers
60 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
63 trace_vdebug (const char *fmt
, ...)
69 vsprintf (buf
, fmt
, ap
);
70 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
74 #define trace_debug_1(level, fmt, args...) \
76 if (level <= debug_threads) \
77 trace_vdebug ((fmt), ##args); \
80 #define trace_debug(FMT, args...) \
81 trace_debug_1 (1, FMT, ##args)
84 # define ATTR_USED __attribute__((used))
85 # define ATTR_NOINLINE __attribute__((noinline))
86 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
89 # define ATTR_NOINLINE
90 # define ATTR_CONSTRUCTOR
93 /* Make sure the functions the IPA needs to export (symbols GDBserver
94 needs to query GDB about) are exported. */
96 #ifdef IN_PROCESS_AGENT
97 # if defined _WIN32 || defined __CYGWIN__
98 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
101 # define IP_AGENT_EXPORT \
102 __attribute__ ((visibility("default"))) ATTR_USED
104 # define IP_AGENT_EXPORT ATTR_USED
108 # define IP_AGENT_EXPORT
111 /* Prefix exported symbols, for good citizenship. All the symbols
112 that need exporting are defined in this module. */
113 #ifdef IN_PROCESS_AGENT
114 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
115 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
116 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
117 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
118 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
119 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
120 # define collecting gdb_agent_collecting
121 # define gdb_collect gdb_agent_gdb_collect
122 # define stop_tracing gdb_agent_stop_tracing
123 # define flush_trace_buffer gdb_agent_flush_trace_buffer
124 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
125 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
126 # define stopping_tracepoint gdb_agent_stopping_tracepoint
127 # define expr_eval_result gdb_agent_expr_eval_result
128 # define error_tracepoint gdb_agent_error_tracepoint
129 # define tracepoints gdb_agent_tracepoints
130 # define tracing gdb_agent_tracing
131 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
132 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
133 # define trace_buffer_lo gdb_agent_trace_buffer_lo
134 # define trace_buffer_hi gdb_agent_trace_buffer_hi
135 # define traceframe_read_count gdb_agent_traceframe_read_count
136 # define traceframe_write_count gdb_agent_traceframe_write_count
137 # define traceframes_created gdb_agent_traceframes_created
138 # define trace_state_variables gdb_agent_trace_state_variables
139 # define get_raw_reg gdb_agent_get_raw_reg
140 # define get_trace_state_variable_value \
141 gdb_agent_get_trace_state_variable_value
142 # define set_trace_state_variable_value \
143 gdb_agent_set_trace_state_variable_value
144 # define ust_loaded gdb_agent_ust_loaded
145 # define helper_thread_id gdb_agent_helper_thread_id
146 # define cmd_buf gdb_agent_cmd_buf
149 #ifndef IN_PROCESS_AGENT
151 /* Addresses of in-process agent's symbols GDBserver cares about. */
153 struct ipa_sym_addresses
155 CORE_ADDR addr_gdb_tp_heap_buffer
;
156 CORE_ADDR addr_gdb_jump_pad_buffer
;
157 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
158 CORE_ADDR addr_gdb_trampoline_buffer
;
159 CORE_ADDR addr_gdb_trampoline_buffer_end
;
160 CORE_ADDR addr_gdb_trampoline_buffer_error
;
161 CORE_ADDR addr_collecting
;
162 CORE_ADDR addr_gdb_collect
;
163 CORE_ADDR addr_stop_tracing
;
164 CORE_ADDR addr_flush_trace_buffer
;
165 CORE_ADDR addr_about_to_request_buffer_space
;
166 CORE_ADDR addr_trace_buffer_is_full
;
167 CORE_ADDR addr_stopping_tracepoint
;
168 CORE_ADDR addr_expr_eval_result
;
169 CORE_ADDR addr_error_tracepoint
;
170 CORE_ADDR addr_tracepoints
;
171 CORE_ADDR addr_tracing
;
172 CORE_ADDR addr_trace_buffer_ctrl
;
173 CORE_ADDR addr_trace_buffer_ctrl_curr
;
174 CORE_ADDR addr_trace_buffer_lo
;
175 CORE_ADDR addr_trace_buffer_hi
;
176 CORE_ADDR addr_traceframe_read_count
;
177 CORE_ADDR addr_traceframe_write_count
;
178 CORE_ADDR addr_traceframes_created
;
179 CORE_ADDR addr_trace_state_variables
;
180 CORE_ADDR addr_get_raw_reg
;
181 CORE_ADDR addr_get_trace_state_variable_value
;
182 CORE_ADDR addr_set_trace_state_variable_value
;
183 CORE_ADDR addr_ust_loaded
;
192 IPA_SYM(gdb_tp_heap_buffer
),
193 IPA_SYM(gdb_jump_pad_buffer
),
194 IPA_SYM(gdb_jump_pad_buffer_end
),
195 IPA_SYM(gdb_trampoline_buffer
),
196 IPA_SYM(gdb_trampoline_buffer_end
),
197 IPA_SYM(gdb_trampoline_buffer_error
),
199 IPA_SYM(gdb_collect
),
200 IPA_SYM(stop_tracing
),
201 IPA_SYM(flush_trace_buffer
),
202 IPA_SYM(about_to_request_buffer_space
),
203 IPA_SYM(trace_buffer_is_full
),
204 IPA_SYM(stopping_tracepoint
),
205 IPA_SYM(expr_eval_result
),
206 IPA_SYM(error_tracepoint
),
207 IPA_SYM(tracepoints
),
209 IPA_SYM(trace_buffer_ctrl
),
210 IPA_SYM(trace_buffer_ctrl_curr
),
211 IPA_SYM(trace_buffer_lo
),
212 IPA_SYM(trace_buffer_hi
),
213 IPA_SYM(traceframe_read_count
),
214 IPA_SYM(traceframe_write_count
),
215 IPA_SYM(traceframes_created
),
216 IPA_SYM(trace_state_variables
),
217 IPA_SYM(get_raw_reg
),
218 IPA_SYM(get_trace_state_variable_value
),
219 IPA_SYM(set_trace_state_variable_value
),
223 static struct ipa_sym_addresses ipa_sym_addrs
;
225 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
227 /* Returns true if both the in-process agent library and the static
228 tracepoints libraries are loaded in the inferior, and agent has
229 capability on static tracepoints. */
232 in_process_agent_supports_ust (void)
236 if (!agent_loaded_p ())
238 warning ("In-process agent not loaded");
242 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
244 /* Agent understands static tracepoint, then check whether UST is in
245 fact loaded in the inferior. */
246 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
248 warning ("Error reading ust_loaded in lib");
259 write_e_ipa_not_loaded (char *buffer
)
262 "E.In-process agent library not loaded in process. "
263 "Fast and static tracepoints unavailable.");
266 /* Write an error to BUFFER indicating that UST isn't loaded in the
270 write_e_ust_not_loaded (char *buffer
)
274 "E.UST library not loaded in process. "
275 "Static tracepoints unavailable.");
277 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
281 /* If the in-process agent library isn't loaded in the inferior, write
282 an error to BUFFER, and return 1. Otherwise, return 0. */
285 maybe_write_ipa_not_loaded (char *buffer
)
287 if (!agent_loaded_p ())
289 write_e_ipa_not_loaded (buffer
);
295 /* If the in-process agent library and the ust (static tracepoints)
296 library aren't loaded in the inferior, write an error to BUFFER,
297 and return 1. Otherwise, return 0. */
300 maybe_write_ipa_ust_not_loaded (char *buffer
)
302 if (!agent_loaded_p ())
304 write_e_ipa_not_loaded (buffer
);
307 else if (!in_process_agent_supports_ust ())
309 write_e_ust_not_loaded (buffer
);
315 /* Cache all future symbols that the tracepoints module might request.
316 We can not request symbols at arbitrary states in the remote
317 protocol, only when the client tells us that new symbols are
318 available. So when we load the in-process library, make sure to
319 check the entire list. */
322 tracepoint_look_up_symbols (void)
326 if (agent_loaded_p ())
329 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
332 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
334 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
337 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
342 agent_look_up_symbols (NULL
);
347 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
348 of the "stop_tracing" function. When this breakpoint is hit,
349 tracing stopped in the IPA for some reason. E.g., due to
350 tracepoint reaching the pass count, hitting conditional expression
351 evaluation error, etc.
353 The IPA's trace buffer is never in circular tracing mode: instead,
354 GDBserver's is, and whenever the in-process buffer fills, it calls
355 "flush_trace_buffer", which triggers an internal breakpoint.
356 GDBserver reacts to this breakpoint by pulling the meanwhile
357 collected data. Old frames discarding is always handled on the
360 #ifdef IN_PROCESS_AGENT
362 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
364 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
368 /* Call this in the functions where GDBserver places a breakpoint, so
369 that the compiler doesn't try to be clever and skip calling the
370 function at all. This is necessary, even if we tell the compiler
371 to not inline said functions. */
373 #if defined(__GNUC__)
374 # define UNKNOWN_SIDE_EFFECTS() asm ("")
376 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
379 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
382 /* GDBserver places breakpoint here. */
383 UNKNOWN_SIDE_EFFECTS();
386 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
387 flush_trace_buffer (void)
389 /* GDBserver places breakpoint here. */
390 UNKNOWN_SIDE_EFFECTS();
395 #ifndef IN_PROCESS_AGENT
397 tracepoint_handler (CORE_ADDR address
)
399 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
404 /* Breakpoint at "stop_tracing" in the inferior lib. */
405 struct breakpoint
*stop_tracing_bkpt
;
406 static int stop_tracing_handler (CORE_ADDR
);
408 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
409 struct breakpoint
*flush_trace_buffer_bkpt
;
410 static int flush_trace_buffer_handler (CORE_ADDR
);
412 static void download_trace_state_variables (void);
413 static void upload_fast_traceframes (void);
415 static int run_inferior_command (char *cmd
, int len
);
418 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
420 return read_inferior_memory (symaddr
, (unsigned char *) val
,
425 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
428 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
430 return read_inferior_memory (symaddr
, (unsigned char *) val
,
435 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
437 void *pval
= (void *) (uintptr_t) val
;
440 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
441 *val
= (uintptr_t) pval
;
446 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
448 void *pval
= (void *) (uintptr_t) val
;
449 return write_inferior_memory (symaddr
,
450 (unsigned char *) &pval
, sizeof (pval
));
454 write_inferior_integer (CORE_ADDR symaddr
, int val
)
456 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
460 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
462 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
465 static CORE_ADDR
target_malloc (ULONGEST size
);
466 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
468 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
470 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
471 BUF += sizeof ((OBJ)->FIELD); \
476 /* Operations on various types of tracepoint actions. */
478 struct tracepoint_action
;
480 struct tracepoint_action_ops
482 /* Download tracepoint action ACTION to IPA. Return the address of action
484 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
486 /* Send ACTION to agent via command buffer started from BUFFER. Return
487 updated head of command buffer. */
488 char* (*send
) (char *buffer
, const struct tracepoint_action
*action
);
491 /* Base action. Concrete actions inherit this. */
493 struct tracepoint_action
495 #ifndef IN_PROCESS_AGENT
496 const struct tracepoint_action_ops
*ops
;
501 /* An 'M' (collect memory) action. */
502 struct collect_memory_action
504 struct tracepoint_action base
;
511 /* An 'R' (collect registers) action. */
513 struct collect_registers_action
515 struct tracepoint_action base
;
518 /* An 'X' (evaluate expression) action. */
520 struct eval_expr_action
522 struct tracepoint_action base
;
524 struct agent_expr
*expr
;
527 /* An 'L' (collect static trace data) action. */
528 struct collect_static_trace_data_action
530 struct tracepoint_action base
;
533 #ifndef IN_PROCESS_AGENT
535 m_tracepoint_action_download (const struct tracepoint_action
*action
)
537 int size_in_ipa
= (sizeof (struct collect_memory_action
)
538 - offsetof (struct tracepoint_action
, type
));
539 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
541 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
547 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
549 struct collect_memory_action
*maction
550 = (struct collect_memory_action
*) action
;
552 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
553 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
554 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
559 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
561 m_tracepoint_action_download
,
562 m_tracepoint_action_send
,
566 r_tracepoint_action_download (const struct tracepoint_action
*action
)
568 int size_in_ipa
= (sizeof (struct collect_registers_action
)
569 - offsetof (struct tracepoint_action
, type
));
570 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
572 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
579 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
584 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
586 r_tracepoint_action_download
,
587 r_tracepoint_action_send
,
590 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
593 x_tracepoint_action_download (const struct tracepoint_action
*action
)
595 int size_in_ipa
= (sizeof (struct eval_expr_action
)
596 - offsetof (struct tracepoint_action
, type
));
597 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
600 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
602 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
603 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
604 - offsetof (struct tracepoint_action
, type
),
610 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
611 copy 0 to P. Return updated header of buffer. */
614 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
616 /* Copy the length of condition first, and then copy its
625 memcpy (p
, &aexpr
->length
, 4);
628 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
635 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
637 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
639 return agent_expr_send (buffer
, eaction
->expr
);
642 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
644 x_tracepoint_action_download
,
645 x_tracepoint_action_send
,
649 l_tracepoint_action_download (const struct tracepoint_action
*action
)
651 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
652 - offsetof (struct tracepoint_action
, type
));
653 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
655 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
662 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
667 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
669 l_tracepoint_action_download
,
670 l_tracepoint_action_send
,
674 /* This structure describes a piece of the source-level definition of
675 the tracepoint. The contents are not interpreted by the target,
676 but preserved verbatim for uploading upon reconnection. */
680 /* The type of string, such as "cond" for a conditional. */
683 /* The source-level string itself. For the sake of target
684 debugging, we store it in plaintext, even though it is always
685 transmitted in hex. */
688 /* Link to the next one in the list. We link them in the order
689 received, in case some make up an ordered list of commands or
691 struct source_string
*next
;
696 /* Trap based tracepoint. */
699 /* A fast tracepoint implemented with a jump instead of a trap. */
702 /* A static tracepoint, implemented by a program call into a tracing
707 struct tracepoint_hit_ctx
;
709 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
712 /* The definition of a tracepoint. */
714 /* Tracepoints may have multiple locations, each at a different
715 address. This can occur with optimizations, template
716 instantiation, etc. Since the locations may be in different
717 scopes, the conditions and actions may be different for each
718 location. Our target version of tracepoints is more like GDB's
719 notion of "breakpoint locations", but we have almost nothing that
720 is not per-location, so we bother having two kinds of objects. The
721 key consequence is that numbers are not unique, and that it takes
722 both number and address to identify a tracepoint uniquely. */
726 /* The number of the tracepoint, as specified by GDB. Several
727 tracepoint objects here may share a number. */
730 /* Address at which the tracepoint is supposed to trigger. Several
731 tracepoints may share an address. */
734 /* Tracepoint type. */
735 enum tracepoint_type type
;
737 /* True if the tracepoint is currently enabled. */
740 /* The number of single steps that will be performed after each
744 /* The number of times the tracepoint may be hit before it will
745 terminate the entire tracing run. */
748 /* Pointer to the agent expression that is the tracepoint's
749 conditional, or NULL if the tracepoint is unconditional. */
750 struct agent_expr
*cond
;
752 /* The list of actions to take when the tracepoint triggers. */
754 struct tracepoint_action
**actions
;
756 /* Count of the times we've hit this tracepoint during the run.
757 Note that while-stepping steps are not counted as "hits". */
760 /* Cached sum of the sizes of traceframes created by this point. */
761 uint64_t traceframe_usage
;
763 CORE_ADDR compiled_cond
;
765 /* Link to the next tracepoint in the list. */
766 struct tracepoint
*next
;
768 #ifndef IN_PROCESS_AGENT
769 /* The list of actions to take when the tracepoint triggers, in
770 string/packet form. */
773 /* The collection of strings that describe the tracepoint as it was
774 entered into GDB. These are not used by the target, but are
775 reported back to GDB upon reconnection. */
776 struct source_string
*source_strings
;
778 /* The number of bytes displaced by fast tracepoints. It may subsume
779 multiple instructions, for multi-byte fast tracepoints. This
780 field is only valid for fast tracepoints. */
783 /* Only for fast tracepoints. */
784 CORE_ADDR obj_addr_on_target
;
786 /* Address range where the original instruction under a fast
787 tracepoint was relocated to. (_end is actually one byte past
789 CORE_ADDR adjusted_insn_addr
;
790 CORE_ADDR adjusted_insn_addr_end
;
792 /* The address range of the piece of the jump pad buffer that was
793 assigned to this fast tracepoint. (_end is actually one byte
796 CORE_ADDR jump_pad_end
;
798 /* The address range of the piece of the trampoline buffer that was
799 assigned to this fast tracepoint. (_end is actually one byte
801 CORE_ADDR trampoline
;
802 CORE_ADDR trampoline_end
;
804 /* The list of actions to take while in a stepping loop. These
805 fields are only valid for patch-based tracepoints. */
806 int num_step_actions
;
807 struct tracepoint_action
**step_actions
;
808 /* Same, but in string/packet form. */
809 char **step_actions_str
;
811 /* Handle returned by the breakpoint or tracepoint module when we
812 inserted the trap or jump, or hooked into a static tracepoint.
813 NULL if we haven't inserted it yet. */
819 #ifndef IN_PROCESS_AGENT
821 /* Given `while-stepping', a thread may be collecting data for more
822 than one tracepoint simultaneously. On the other hand, the same
823 tracepoint with a while-stepping action may be hit by more than one
824 thread simultaneously (but not quite, each thread could be handling
825 a different step). Each thread holds a list of these objects,
826 representing the current step of each while-stepping action being
831 struct wstep_state
*next
;
833 /* The tracepoint number. */
835 /* The tracepoint's address. */
836 CORE_ADDR tp_address
;
838 /* The number of the current step in this 'while-stepping'
845 /* The linked list of all tracepoints. Marked explicitly as used as
846 the in-process library doesn't use it for the fast tracepoints
848 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
850 #ifndef IN_PROCESS_AGENT
852 /* Pointer to the last tracepoint in the list, new tracepoints are
853 linked in at the end. */
855 static struct tracepoint
*last_tracepoint
;
858 /* The first tracepoint to exceed its pass count. */
860 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
862 /* True if the trace buffer is full or otherwise no longer usable. */
864 IP_AGENT_EXPORT
int trace_buffer_is_full
;
866 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
868 #ifndef IN_PROCESS_AGENT
870 static const char *eval_result_names
[] =
872 "terror:in the attic", /* this should never be reported */
873 "terror:empty expression",
874 "terror:empty stack",
875 "terror:stack overflow",
876 "terror:stack underflow",
877 "terror:unhandled opcode",
878 "terror:unrecognized opcode",
879 "terror:divide by zero"
884 /* The tracepoint in which the error occurred. */
886 static struct tracepoint
*error_tracepoint
;
888 struct trace_state_variable
890 /* This is the name of the variable as used in GDB. The target
891 doesn't use the name, but needs to have it for saving and
892 reconnection purposes. */
895 /* This number identifies the variable uniquely. Numbers may be
896 assigned either by the target (in the case of builtin variables),
897 or by GDB, and are presumed unique during the course of a trace
901 /* The variable's initial value, a 64-bit signed integer always. */
902 LONGEST initial_value
;
904 /* The variable's value, a 64-bit signed integer always. */
907 /* Pointer to a getter function, used to supply computed values. */
908 LONGEST (*getter
) (void);
910 /* Link to the next variable. */
911 struct trace_state_variable
*next
;
914 /* Linked list of all trace state variables. */
916 #ifdef IN_PROCESS_AGENT
917 struct trace_state_variable
*alloced_trace_state_variables
;
920 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
922 /* The results of tracing go into a fixed-size space known as the
923 "trace buffer". Because usage follows a limited number of
924 patterns, we manage it ourselves rather than with malloc. Basic
925 rules are that we create only one trace frame at a time, each is
926 variable in size, they are never moved once created, and we only
927 discard if we are doing a circular buffer, and then only the oldest
928 ones. Each trace frame includes its own size, so we don't need to
929 link them together, and the trace frame number is relative to the
930 first one, so we don't need to record numbers. A trace frame also
931 records the number of the tracepoint that created it. The data
932 itself is a series of blocks, each introduced by a single character
933 and with a defined format. Each type of block has enough
934 type/length info to allow scanners to jump quickly from one block
935 to the next without reading each byte in the block. */
937 /* Trace buffer management would be simple - advance a free pointer
938 from beginning to end, then stop - were it not for the circular
939 buffer option, which is a useful way to prevent a trace run from
940 stopping prematurely because the buffer filled up. In the circular
941 case, the location of the first trace frame (trace_buffer_start)
942 moves as old trace frames are discarded. Also, since we grow trace
943 frames incrementally as actions are performed, we wrap around to
944 the beginning of the trace buffer. This is per-block, so each
945 block within a trace frame remains contiguous. Things get messy
946 when the wrapped-around trace frame is the one being discarded; the
947 free space ends up in two parts at opposite ends of the buffer. */
950 # if defined(__GNUC__)
951 # define ATTR_PACKED __attribute__ ((packed))
953 # define ATTR_PACKED /* nothing */
957 /* The data collected at a tracepoint hit. This object should be as
958 small as possible, since there may be a great many of them. We do
959 not need to keep a frame number, because they are all sequential
960 and there are no deletions; so the Nth frame in the buffer is
961 always frame number N. */
965 /* Number of the tracepoint that collected this traceframe. A value
966 of 0 indicates the current end of the trace buffer. We make this
967 a 16-bit field because it's never going to happen that GDB's
968 numbering of tracepoints reaches 32,000. */
971 /* The size of the data in this trace frame. We limit this to 32
972 bits, even on a 64-bit target, because it's just implausible that
973 one is validly going to collect 4 gigabytes of data at a single
975 unsigned int data_size
: 32;
977 /* The base of the trace data, which is contiguous from this point. */
978 unsigned char data
[0];
982 /* The traceframe to be used as the source of data to send back to
983 GDB. A value of -1 means to get data from the live program. */
985 int current_traceframe
= -1;
987 /* This flag is true if the trace buffer is circular, meaning that
988 when it fills, the oldest trace frames are discarded in order to
991 #ifndef IN_PROCESS_AGENT
992 static int circular_trace_buffer
;
995 /* Pointer to the block of memory that traceframes all go into. */
997 static unsigned char *trace_buffer_lo
;
999 /* Pointer to the end of the trace buffer, more precisely to the byte
1000 after the end of the buffer. */
1002 static unsigned char *trace_buffer_hi
;
1004 /* Control structure holding the read/write/etc. pointers into the
1005 trace buffer. We need more than one of these to implement a
1006 transaction-like mechanism to garantees that both GDBserver and the
1007 in-process agent can try to change the trace buffer
1010 struct trace_buffer_control
1012 /* Pointer to the first trace frame in the buffer. In the
1013 non-circular case, this is equal to trace_buffer_lo, otherwise it
1014 moves around in the buffer. */
1015 unsigned char *start
;
1017 /* Pointer to the free part of the trace buffer. Note that we clear
1018 several bytes at and after this pointer, so that traceframe
1019 scans/searches terminate properly. */
1020 unsigned char *free
;
1022 /* Pointer to the byte after the end of the free part. Note that
1023 this may be smaller than trace_buffer_free in the circular case,
1024 and means that the free part is in two pieces. Initially it is
1025 equal to trace_buffer_hi, then is generally equivalent to
1026 trace_buffer_start. */
1027 unsigned char *end_free
;
1029 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1030 this is the point at which the trace data breaks, and resumes at
1032 unsigned char *wrap
;
1035 /* Same as above, to be used by GDBserver when updating the in-process
1037 struct ipa_trace_buffer_control
1046 /* We have possibly both GDBserver and an inferior thread accessing
1047 the same IPA trace buffer memory. The IPA is the producer (tries
1048 to put new frames in the buffer), while GDBserver occasionally
1049 consumes them, that is, flushes the IPA's buffer into its own
1050 buffer. Both sides need to update the trace buffer control
1051 pointers (current head, tail, etc.). We can't use a global lock to
1052 synchronize the accesses, as otherwise we could deadlock GDBserver
1053 (if the thread holding the lock stops for a signal, say). So
1054 instead of that, we use a transaction scheme where GDBserver writes
1055 always prevail over the IPAs writes, and, we have the IPA detect
1056 the commit failure/overwrite, and retry the whole attempt. This is
1057 mainly implemented by having a global token object that represents
1058 who wrote last to the buffer control structure. We need to freeze
1059 any inferior writing to the buffer while GDBserver touches memory,
1060 so that the inferior can correctly detect that GDBserver had been
1061 there, otherwise, it could mistakingly think its commit was
1062 successful; that's implemented by simply having GDBserver set a
1063 breakpoint the inferior hits if it is the critical region.
1065 There are three cycling trace buffer control structure copies
1066 (buffer head, tail, etc.), with the token object including an index
1067 indicating which is current live copy. The IPA tentatively builds
1068 an updated copy in a non-current control structure, while GDBserver
1069 always clobbers the current version directly. The IPA then tries
1070 to atomically "commit" its version; if GDBserver clobbered the
1071 structure meanwhile, that will fail, and the IPA restarts the
1074 Listing the step in further detail, we have:
1076 In-process agent (producer):
1078 - passes by `about_to_request_buffer_space' breakpoint/lock
1080 - reads current token, extracts current trace buffer control index,
1081 and starts tentatively updating the rightmost one (0->1, 1->2,
1082 2->0). Note that only one inferior thread is executing this code
1083 at any given time, due to an outer lock in the jump pads.
1085 - updates counters, and tries to commit the token.
1087 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1088 leaving the sync region.
1090 - checks if the update was effective.
1092 - if trace buffer was found full, hits flush_trace_buffer
1093 breakpoint, and restarts later afterwards.
1095 GDBserver (consumer):
1097 - sets `about_to_request_buffer_space' breakpoint/lock.
1099 - updates the token unconditionally, using the current buffer
1100 control index, since it knows that the IP agent always writes to
1101 the rightmost, and due to the breakpoint, at most one IP thread
1102 can try to update the trace buffer concurrently to GDBserver, so
1103 there will be no danger of trace buffer control index wrap making
1104 the IPA write to the same index as GDBserver.
1106 - flushes the IP agent's trace buffer completely, and updates the
1107 current trace buffer control structure. GDBserver *always* wins.
1109 - removes the `about_to_request_buffer_space' breakpoint.
1111 The token is stored in the `trace_buffer_ctrl_curr' variable.
1112 Internally, it's bits are defined as:
1114 |-------------+-----+-------------+--------+-------------+--------------|
1115 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1116 |-------------+-----+-------------+--------+-------------+--------------|
1117 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1118 |-------------+-----+-------------+--------+-------------+--------------|
1120 GSB - GDBserver Stamp Bit
1121 PC - Previous Counter
1122 CC - Current Counter
1123 TBCI - Trace Buffer Control Index
1126 An IPA update of `trace_buffer_ctrl_curr' does:
1128 - read CC from the current token, save as PC.
1130 - atomically tries to write PC+1,CC
1132 A GDBserver update of `trace_buffer_ctrl_curr' does:
1134 - reads PC and CC from the current token.
1139 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1140 for the counters described below. The cleared bits are used to
1141 hold the index of the items of the `trace_buffer_ctrl' array that
1143 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1145 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1146 counter, and the `current' counter. */
1148 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1149 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1151 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1152 always stamps this bit as set. */
1153 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1155 #ifdef IN_PROCESS_AGENT
1156 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1157 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1159 # define TRACE_BUFFER_CTRL_CURR \
1160 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1164 /* The GDBserver side agent only needs one instance of this object, as
1165 it doesn't need to sync with itself. Define it as array anyway so
1166 that the rest of the code base doesn't need to care for the
1168 struct trace_buffer_control trace_buffer_ctrl
[1];
1169 # define TRACE_BUFFER_CTRL_CURR 0
1172 /* These are convenience macros used to access the current trace
1173 buffer control in effect. */
1174 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1175 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1176 #define trace_buffer_end_free \
1177 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1178 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1181 /* Macro that returns a pointer to the first traceframe in the buffer. */
1183 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1185 /* Macro that returns a pointer to the next traceframe in the buffer.
1186 If the computed location is beyond the wraparound point, subtract
1187 the offset of the wraparound. */
1189 #define NEXT_TRACEFRAME_1(TF) \
1190 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1192 #define NEXT_TRACEFRAME(TF) \
1193 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1194 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1195 ? (trace_buffer_wrap - trace_buffer_lo) \
1198 /* The difference between these counters represents the total number
1199 of complete traceframes present in the trace buffer. The IP agent
1200 writes to the write count, GDBserver writes to read count. */
1202 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1203 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1205 /* Convenience macro. */
1207 #define traceframe_count \
1208 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1210 /* The count of all traceframes created in the current run, including
1211 ones that were discarded to make room. */
1213 IP_AGENT_EXPORT
int traceframes_created
;
1215 #ifndef IN_PROCESS_AGENT
1217 /* Read-only regions are address ranges whose contents don't change,
1218 and so can be read from target memory even while looking at a trace
1219 frame. Without these, disassembly for instance will likely fail,
1220 because the program code is not usually collected into a trace
1221 frame. This data structure does not need to be very complicated or
1222 particularly efficient, it's only going to be used occasionally,
1223 and only by some commands. */
1225 struct readonly_region
1227 /* The bounds of the region. */
1228 CORE_ADDR start
, end
;
1230 /* Link to the next one. */
1231 struct readonly_region
*next
;
1234 /* Linked list of readonly regions. This list stays in effect from
1235 one tstart to the next. */
1237 static struct readonly_region
*readonly_regions
;
1241 /* The global that controls tracing overall. */
1243 IP_AGENT_EXPORT
int tracing
;
1245 #ifndef IN_PROCESS_AGENT
1247 /* Controls whether tracing should continue after GDB disconnects. */
1249 int disconnected_tracing
;
1251 /* The reason for the last tracing run to have stopped. We initialize
1252 to a distinct string so that GDB can distinguish between "stopped
1253 after running" and "stopped because never run" cases. */
1255 static const char *tracing_stop_reason
= "tnotrun";
1257 static int tracing_stop_tpnum
;
1259 /* 64-bit timestamps for the trace run's start and finish, expressed
1260 in microseconds from the Unix epoch. */
1262 LONGEST tracing_start_time
;
1263 LONGEST tracing_stop_time
;
1265 /* The (optional) user-supplied name of the user that started the run.
1266 This is an arbitrary string, and may be NULL. */
1268 char *tracing_user_name
;
1270 /* Optional user-supplied text describing the run. This is
1271 an arbitrary string, and may be NULL. */
1273 char *tracing_notes
;
1275 /* Optional user-supplied text explaining a tstop command. This is an
1276 arbitrary string, and may be NULL. */
1278 char *tracing_stop_note
;
1282 /* Functions local to this file. */
1284 /* Base "class" for tracepoint type specific data to be passed down to
1285 collect_data_at_tracepoint. */
1286 struct tracepoint_hit_ctx
1288 enum tracepoint_type type
;
1291 #ifdef IN_PROCESS_AGENT
1293 /* Fast/jump tracepoint specific data to be passed down to
1294 collect_data_at_tracepoint. */
1295 struct fast_tracepoint_ctx
1297 struct tracepoint_hit_ctx base
;
1299 struct regcache regcache
;
1300 int regcache_initted
;
1301 unsigned char *regspace
;
1303 unsigned char *regs
;
1304 struct tracepoint
*tpoint
;
1307 /* Static tracepoint specific data to be passed down to
1308 collect_data_at_tracepoint. */
1309 struct static_tracepoint_ctx
1311 struct tracepoint_hit_ctx base
;
1313 /* The regcache corresponding to the registers state at the time of
1314 the tracepoint hit. Initialized lazily, from REGS. */
1315 struct regcache regcache
;
1316 int regcache_initted
;
1318 /* The buffer space REGCACHE above uses. We use a separate buffer
1319 instead of letting the regcache malloc for both signal safety and
1320 performance reasons; this is allocated on the stack instead. */
1321 unsigned char *regspace
;
1323 /* The register buffer as passed on by lttng/ust. */
1324 struct registers
*regs
;
1326 /* The "printf" formatter and the args the user passed to the marker
1327 call. We use this to be able to collect "static trace data"
1332 /* The GDB tracepoint matching the probed marker that was "hit". */
1333 struct tracepoint
*tpoint
;
1338 /* Static tracepoint specific data to be passed down to
1339 collect_data_at_tracepoint. */
1340 struct trap_tracepoint_ctx
1342 struct tracepoint_hit_ctx base
;
1344 struct regcache
*regcache
;
1349 #ifndef IN_PROCESS_AGENT
1350 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1351 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1354 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1355 struct tracepoint
*tpoint
);
1357 #ifndef IN_PROCESS_AGENT
1358 static void clear_readonly_regions (void);
1359 static void clear_installed_tracepoints (void);
1362 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1364 struct tracepoint
*tpoint
);
1365 #ifndef IN_PROCESS_AGENT
1366 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1368 struct tracepoint
*tpoint
, int current_step
);
1369 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1370 CORE_ADDR
*jump_entry
);
1372 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1374 struct tracepoint
*tpoint
,
1375 struct traceframe
*tframe
,
1376 struct tracepoint_action
*taction
);
1378 #ifndef IN_PROCESS_AGENT
1379 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1381 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1382 static void download_tracepoint (struct tracepoint
*);
1383 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1384 static void clone_fast_tracepoint (struct tracepoint
*to
,
1385 const struct tracepoint
*from
);
1388 static LONGEST
get_timestamp (void);
1390 #if defined(__GNUC__)
1391 # define memory_barrier() asm volatile ("" : : : "memory")
1393 # define memory_barrier() do {} while (0)
1396 /* We only build the IPA if this builtin is supported, and there are
1397 no uses of this in GDBserver itself, so we're safe in defining this
1399 #define cmpxchg(mem, oldval, newval) \
1400 __sync_val_compare_and_swap (mem, oldval, newval)
1402 /* Record that an error occurred during expression evaluation. */
1405 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1406 enum eval_result_type rtype
)
1408 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1409 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1411 #ifdef IN_PROCESS_AGENT
1412 /* Only record the first error we get. */
1413 if (cmpxchg (&expr_eval_result
,
1415 rtype
) != expr_eval_no_error
)
1418 if (expr_eval_result
!= expr_eval_no_error
)
1422 error_tracepoint
= tpoint
;
1425 /* Trace buffer management. */
1428 clear_trace_buffer (void)
1430 trace_buffer_start
= trace_buffer_lo
;
1431 trace_buffer_free
= trace_buffer_lo
;
1432 trace_buffer_end_free
= trace_buffer_hi
;
1433 trace_buffer_wrap
= trace_buffer_hi
;
1434 /* A traceframe with zeroed fields marks the end of trace data. */
1435 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1436 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1437 traceframe_read_count
= traceframe_write_count
= 0;
1438 traceframes_created
= 0;
1441 #ifndef IN_PROCESS_AGENT
1444 clear_inferior_trace_buffer (void)
1446 CORE_ADDR ipa_trace_buffer_lo
;
1447 CORE_ADDR ipa_trace_buffer_hi
;
1448 struct traceframe ipa_traceframe
= { 0 };
1449 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1451 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1452 &ipa_trace_buffer_lo
);
1453 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1454 &ipa_trace_buffer_hi
);
1456 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1457 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1458 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1459 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1461 /* A traceframe with zeroed fields marks the end of trace data. */
1462 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1463 (unsigned char *) &ipa_trace_buffer_ctrl
,
1464 sizeof (ipa_trace_buffer_ctrl
));
1466 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1468 /* A traceframe with zeroed fields marks the end of trace data. */
1469 write_inferior_memory (ipa_trace_buffer_lo
,
1470 (unsigned char *) &ipa_traceframe
,
1471 sizeof (ipa_traceframe
));
1473 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1474 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1475 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1481 init_trace_buffer (unsigned char *buf
, int bufsize
)
1483 trace_buffer_lo
= buf
;
1484 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1486 clear_trace_buffer ();
1489 #ifdef IN_PROCESS_AGENT
1491 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1492 about_to_request_buffer_space (void)
1494 /* GDBserver places breakpoint here while it goes about to flush
1495 data at random times. */
1496 UNKNOWN_SIDE_EFFECTS();
1501 /* Carve out a piece of the trace buffer, returning NULL in case of
1505 trace_buffer_alloc (size_t amt
)
1507 unsigned char *rslt
;
1508 struct trace_buffer_control
*tbctrl
;
1510 #ifdef IN_PROCESS_AGENT
1511 unsigned int prev
, prev_filtered
;
1512 unsigned int commit_count
;
1513 unsigned int commit
;
1514 unsigned int readout
;
1516 struct traceframe
*oldest
;
1517 unsigned char *new_start
;
1520 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1521 (long) amt
, (long) sizeof (struct traceframe
));
1523 /* Account for the EOB marker. */
1524 amt
+= sizeof (struct traceframe
);
1526 #ifdef IN_PROCESS_AGENT
1530 /* Read the current token and extract the index to try to write to,
1531 storing it in CURR. */
1532 prev
= trace_buffer_ctrl_curr
;
1533 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1534 curr
= prev_filtered
+ 1;
1538 about_to_request_buffer_space ();
1540 /* Start out with a copy of the current state. GDBserver may be
1541 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1542 be able to commit anyway if that happens. */
1543 trace_buffer_ctrl
[curr
]
1544 = trace_buffer_ctrl
[prev_filtered
];
1545 trace_debug ("trying curr=%u", curr
);
1547 /* The GDBserver's agent doesn't need all that syncing, and always
1548 updates TCB 0 (there's only one, mind you). */
1551 tbctrl
= &trace_buffer_ctrl
[curr
];
1553 /* Offsets are easier to grok for debugging than raw addresses,
1554 especially for the small trace buffer sizes that are useful for
1556 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1558 (int) (tbctrl
->start
- trace_buffer_lo
),
1559 (int) (tbctrl
->free
- trace_buffer_lo
),
1560 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1561 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1562 (int) (trace_buffer_hi
- trace_buffer_lo
));
1564 /* The algorithm here is to keep trying to get a contiguous block of
1565 the requested size, possibly discarding older traceframes to free
1566 up space. Since free space might come in one or two pieces,
1567 depending on whether discarded traceframes wrapped around at the
1568 high end of the buffer, we test both pieces after each
1572 /* First, if we have two free parts, try the upper one first. */
1573 if (tbctrl
->end_free
< tbctrl
->free
)
1575 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1576 /* We have enough in the upper part. */
1580 /* Our high part of free space wasn't enough. Give up
1581 on it for now, set wraparound. We will recover the
1582 space later, if/when the wrapped-around traceframe is
1584 trace_debug ("Upper part too small, setting wraparound");
1585 tbctrl
->wrap
= tbctrl
->free
;
1586 tbctrl
->free
= trace_buffer_lo
;
1590 /* The normal case. */
1591 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1594 #ifdef IN_PROCESS_AGENT
1595 /* The IP Agent's buffer is always circular. It isn't used
1596 currently, but `circular_trace_buffer' could represent
1597 GDBserver's mode. If we didn't find space, ask GDBserver to
1600 flush_trace_buffer ();
1604 trace_debug ("gdbserver flushed buffer, retrying");
1608 /* GDBserver cancelled the tracing. Bail out as well. */
1611 /* If we're here, then neither part is big enough, and
1612 non-circular trace buffers are now full. */
1613 if (!circular_trace_buffer
)
1615 trace_debug ("Not enough space in the trace buffer");
1619 trace_debug ("Need more space in the trace buffer");
1621 /* If we have a circular buffer, we can try discarding the
1622 oldest traceframe and see if that helps. */
1623 oldest
= FIRST_TRACEFRAME ();
1624 if (oldest
->tpnum
== 0)
1626 /* Not good; we have no traceframes to free. Perhaps we're
1627 asking for a block that is larger than the buffer? In
1628 any case, give up. */
1629 trace_debug ("No traceframes to discard");
1633 /* We don't run this code in the in-process agent currently.
1634 E.g., we could leave the in-process agent in autonomous
1635 circular mode if we only have fast tracepoints. If we do
1636 that, then this bit becomes racy with GDBserver, which also
1637 writes to this counter. */
1638 --traceframe_write_count
;
1640 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1641 /* If we freed the traceframe that wrapped around, go back
1642 to the non-wrap case. */
1643 if (new_start
< tbctrl
->start
)
1645 trace_debug ("Discarding past the wraparound");
1646 tbctrl
->wrap
= trace_buffer_hi
;
1648 tbctrl
->start
= new_start
;
1649 tbctrl
->end_free
= tbctrl
->start
;
1651 trace_debug ("Discarded a traceframe\n"
1652 "Trace buffer [%d], start=%d free=%d "
1653 "endfree=%d wrap=%d hi=%d",
1655 (int) (tbctrl
->start
- trace_buffer_lo
),
1656 (int) (tbctrl
->free
- trace_buffer_lo
),
1657 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1658 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1659 (int) (trace_buffer_hi
- trace_buffer_lo
));
1661 /* Now go back around the loop. The discard might have resulted
1662 in either one or two pieces of free space, so we want to try
1663 both before freeing any more traceframes. */
1667 /* If we get here, we know we can provide the asked-for space. */
1669 rslt
= tbctrl
->free
;
1671 /* Adjust the request back down, now that we know we have space for
1672 the marker, but don't commit to AMT yet, we may still need to
1673 restart the operation if GDBserver touches the trace buffer
1674 (obviously only important in the in-process agent's version). */
1675 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1677 /* Or not. If GDBserver changed the trace buffer behind our back,
1678 we get to restart a new allocation attempt. */
1680 #ifdef IN_PROCESS_AGENT
1681 /* Build the tentative token. */
1682 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1683 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1684 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1688 /* Try to commit it. */
1689 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1690 if (readout
!= prev
)
1692 trace_debug ("GDBserver has touched the trace buffer, restarting."
1693 " (prev=%08x, commit=%08x, readout=%08x)",
1694 prev
, commit
, readout
);
1698 /* Hold your horses here. Even if that change was committed,
1699 GDBserver could come in, and clobber it. We need to hold to be
1700 able to tell if GDBserver clobbers before or after we committed
1701 the change. Whenever GDBserver goes about touching the IPA
1702 buffer, it sets a breakpoint in this routine, so we have a sync
1704 about_to_request_buffer_space ();
1706 /* Check if the change has been effective, even if GDBserver stopped
1707 us at the breakpoint. */
1710 unsigned int refetch
;
1714 refetch
= trace_buffer_ctrl_curr
;
1716 if (refetch
== commit
1717 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1720 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1721 "readout=%08x, refetch=%08x)",
1722 prev
, commit
, readout
, refetch
);
1726 trace_debug ("GDBserver has touched the trace buffer, not effective."
1727 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1728 prev
, commit
, readout
, refetch
);
1734 /* We have a new piece of the trace buffer. Hurray! */
1736 /* Add an EOB marker just past this allocation. */
1737 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1738 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1740 /* Adjust the request back down, now that we know we have space for
1742 amt
-= sizeof (struct traceframe
);
1746 trace_debug ("Allocated %d bytes", (int) amt
);
1747 trace_debug ("Trace buffer [%d] start=%d free=%d "
1748 "endfree=%d wrap=%d hi=%d",
1750 (int) (tbctrl
->start
- trace_buffer_lo
),
1751 (int) (tbctrl
->free
- trace_buffer_lo
),
1752 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1753 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1754 (int) (trace_buffer_hi
- trace_buffer_lo
));
1760 #ifndef IN_PROCESS_AGENT
1762 /* Return the total free space. This is not necessarily the largest
1763 block we can allocate, because of the two-part case. */
1768 if (trace_buffer_free
<= trace_buffer_end_free
)
1769 return trace_buffer_end_free
- trace_buffer_free
;
1771 return ((trace_buffer_end_free
- trace_buffer_lo
)
1772 + (trace_buffer_hi
- trace_buffer_free
));
1775 /* An 'S' in continuation packets indicates remainder are for
1778 static int seen_step_action_flag
;
1780 /* Create a tracepoint (location) with given number and address. Add this
1781 new tracepoint to list and sort this list. */
1783 static struct tracepoint
*
1784 add_tracepoint (int num
, CORE_ADDR addr
)
1786 struct tracepoint
*tpoint
, **tp_next
;
1788 tpoint
= xmalloc (sizeof (struct tracepoint
));
1789 tpoint
->number
= num
;
1790 tpoint
->address
= addr
;
1791 tpoint
->numactions
= 0;
1792 tpoint
->actions
= NULL
;
1793 tpoint
->actions_str
= NULL
;
1794 tpoint
->cond
= NULL
;
1795 tpoint
->num_step_actions
= 0;
1796 tpoint
->step_actions
= NULL
;
1797 tpoint
->step_actions_str
= NULL
;
1798 /* Start all off as regular (slow) tracepoints. */
1799 tpoint
->type
= trap_tracepoint
;
1800 tpoint
->orig_size
= -1;
1801 tpoint
->source_strings
= NULL
;
1802 tpoint
->compiled_cond
= 0;
1803 tpoint
->handle
= NULL
;
1804 tpoint
->next
= NULL
;
1806 /* Find a place to insert this tracepoint into list in order to keep
1807 the tracepoint list still in the ascending order. There may be
1808 multiple tracepoints at the same address as TPOINT's, and this
1809 guarantees TPOINT is inserted after all the tracepoints which are
1810 set at the same address. For example, fast tracepoints A, B, C are
1811 set at the same address, and D is to be insert at the same place as
1814 -->| A |--> | B |-->| C |->...
1816 One jump pad was created for tracepoint A, B, and C, and the target
1817 address of A is referenced/used in jump pad. So jump pad will let
1818 inferior jump to A. If D is inserted in front of A, like this,
1820 -->| D |-->| A |--> | B |-->| C |->...
1822 without updating jump pad, D is not reachable during collect, which
1823 is wrong. As we can see, the order of B, C and D doesn't matter, but
1824 A should always be the `first' one. */
1825 for (tp_next
= &tracepoints
;
1826 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1827 tp_next
= &(*tp_next
)->next
)
1829 tpoint
->next
= *tp_next
;
1831 last_tracepoint
= tpoint
;
1833 seen_step_action_flag
= 0;
1838 #ifndef IN_PROCESS_AGENT
1840 /* Return the tracepoint with the given number and address, or NULL. */
1842 static struct tracepoint
*
1843 find_tracepoint (int id
, CORE_ADDR addr
)
1845 struct tracepoint
*tpoint
;
1847 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1848 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1854 /* Remove TPOINT from global list. */
1857 remove_tracepoint (struct tracepoint
*tpoint
)
1859 struct tracepoint
*tp
, *tp_prev
;
1861 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1862 tp_prev
= tp
, tp
= tp
->next
)
1868 tp_prev
->next
= tp
->next
;
1870 tracepoints
= tp
->next
;
1876 /* There may be several tracepoints with the same number (because they
1877 are "locations", in GDB parlance); return the next one after the
1878 given tracepoint, or search from the beginning of the list if the
1879 first argument is NULL. */
1881 static struct tracepoint
*
1882 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1884 struct tracepoint
*tpoint
;
1887 tpoint
= prev_tp
->next
;
1889 tpoint
= tracepoints
;
1890 for (; tpoint
; tpoint
= tpoint
->next
)
1891 if (tpoint
->number
== num
)
1899 /* Append another action to perform when the tracepoint triggers. */
1902 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1908 seen_step_action_flag
= 1;
1916 char *act_start
= act
;
1917 struct tracepoint_action
*action
= NULL
;
1923 struct collect_memory_action
*maction
;
1927 maction
= xmalloc (sizeof *maction
);
1928 maction
->base
.type
= *act
;
1929 maction
->base
.ops
= &m_tracepoint_action_ops
;
1930 action
= &maction
->base
;
1933 is_neg
= (*act
== '-');
1936 act
= unpack_varlen_hex (act
, &basereg
);
1938 act
= unpack_varlen_hex (act
, &maction
->addr
);
1940 act
= unpack_varlen_hex (act
, &maction
->len
);
1941 maction
->basereg
= (is_neg
1944 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1945 pulongest (maction
->len
),
1946 paddress (maction
->addr
), maction
->basereg
);
1951 struct collect_registers_action
*raction
;
1953 raction
= xmalloc (sizeof *raction
);
1954 raction
->base
.type
= *act
;
1955 raction
->base
.ops
= &r_tracepoint_action_ops
;
1956 action
= &raction
->base
;
1958 trace_debug ("Want to collect registers");
1960 /* skip past hex digits of mask for now */
1961 while (isxdigit(*act
))
1967 struct collect_static_trace_data_action
*raction
;
1969 raction
= xmalloc (sizeof *raction
);
1970 raction
->base
.type
= *act
;
1971 raction
->base
.ops
= &l_tracepoint_action_ops
;
1972 action
= &raction
->base
;
1974 trace_debug ("Want to collect static trace data");
1979 trace_debug ("Unexpected step action, ignoring");
1984 struct eval_expr_action
*xaction
;
1986 xaction
= xmalloc (sizeof (*xaction
));
1987 xaction
->base
.type
= *act
;
1988 xaction
->base
.ops
= &x_tracepoint_action_ops
;
1989 action
= &xaction
->base
;
1991 trace_debug ("Want to evaluate expression");
1992 xaction
->expr
= gdb_parse_agent_expr (&act
);
1996 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2005 if (seen_step_action_flag
)
2007 tpoint
->num_step_actions
++;
2009 tpoint
->step_actions
2010 = xrealloc (tpoint
->step_actions
,
2011 (sizeof (*tpoint
->step_actions
)
2012 * tpoint
->num_step_actions
));
2013 tpoint
->step_actions_str
2014 = xrealloc (tpoint
->step_actions_str
,
2015 (sizeof (*tpoint
->step_actions_str
)
2016 * tpoint
->num_step_actions
));
2017 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2018 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2019 = savestring (act_start
, act
- act_start
);
2023 tpoint
->numactions
++;
2025 = xrealloc (tpoint
->actions
,
2026 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2028 = xrealloc (tpoint
->actions_str
,
2029 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2030 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2031 tpoint
->actions_str
[tpoint
->numactions
- 1]
2032 = savestring (act_start
, act
- act_start
);
2039 /* Find or create a trace state variable with the given number. */
2041 static struct trace_state_variable
*
2042 get_trace_state_variable (int num
)
2044 struct trace_state_variable
*tsv
;
2046 #ifdef IN_PROCESS_AGENT
2047 /* Search for an existing variable. */
2048 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2049 if (tsv
->number
== num
)
2053 /* Search for an existing variable. */
2054 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2055 if (tsv
->number
== num
)
2061 /* Find or create a trace state variable with the given number. */
2063 static struct trace_state_variable
*
2064 create_trace_state_variable (int num
, int gdb
)
2066 struct trace_state_variable
*tsv
;
2068 tsv
= get_trace_state_variable (num
);
2072 /* Create a new variable. */
2073 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2075 tsv
->initial_value
= 0;
2079 #ifdef IN_PROCESS_AGENT
2082 tsv
->next
= alloced_trace_state_variables
;
2083 alloced_trace_state_variables
= tsv
;
2088 tsv
->next
= trace_state_variables
;
2089 trace_state_variables
= tsv
;
2094 IP_AGENT_EXPORT LONGEST
2095 get_trace_state_variable_value (int num
)
2097 struct trace_state_variable
*tsv
;
2099 tsv
= get_trace_state_variable (num
);
2103 trace_debug ("No trace state variable %d, skipping value get", num
);
2107 /* Call a getter function if we have one. While it's tempting to
2108 set up something to only call the getter once per tracepoint hit,
2109 it could run afoul of thread races. Better to let the getter
2110 handle it directly, if necessary to worry about it. */
2112 tsv
->value
= (tsv
->getter
) ();
2114 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2115 num
, plongest (tsv
->value
));
2120 IP_AGENT_EXPORT
void
2121 set_trace_state_variable_value (int num
, LONGEST val
)
2123 struct trace_state_variable
*tsv
;
2125 tsv
= get_trace_state_variable (num
);
2129 trace_debug ("No trace state variable %d, skipping value set", num
);
2137 agent_get_trace_state_variable_value (int num
)
2139 return get_trace_state_variable_value (num
);
2143 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2145 set_trace_state_variable_value (num
, val
);
2149 set_trace_state_variable_name (int num
, const char *name
)
2151 struct trace_state_variable
*tsv
;
2153 tsv
= get_trace_state_variable (num
);
2157 trace_debug ("No trace state variable %d, skipping name set", num
);
2161 tsv
->name
= (char *) name
;
2165 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2167 struct trace_state_variable
*tsv
;
2169 tsv
= get_trace_state_variable (num
);
2173 trace_debug ("No trace state variable %d, skipping getter set", num
);
2177 tsv
->getter
= getter
;
2180 /* Add a raw traceframe for the given tracepoint. */
2182 static struct traceframe
*
2183 add_traceframe (struct tracepoint
*tpoint
)
2185 struct traceframe
*tframe
;
2187 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2192 tframe
->tpnum
= tpoint
->number
;
2193 tframe
->data_size
= 0;
2198 /* Add a block to the traceframe currently being worked on. */
2200 static unsigned char *
2201 add_traceframe_block (struct traceframe
*tframe
,
2202 struct tracepoint
*tpoint
, int amt
)
2204 unsigned char *block
;
2209 block
= trace_buffer_alloc (amt
);
2214 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2216 tframe
->data_size
+= amt
;
2217 tpoint
->traceframe_usage
+= amt
;
2222 /* Flag that the current traceframe is finished. */
2225 finish_traceframe (struct traceframe
*tframe
)
2227 ++traceframe_write_count
;
2228 ++traceframes_created
;
2231 #ifndef IN_PROCESS_AGENT
2233 /* Given a traceframe number NUM, find the NUMth traceframe in the
2236 static struct traceframe
*
2237 find_traceframe (int num
)
2239 struct traceframe
*tframe
;
2242 for (tframe
= FIRST_TRACEFRAME ();
2244 tframe
= NEXT_TRACEFRAME (tframe
))
2255 get_traceframe_address (struct traceframe
*tframe
)
2258 struct tracepoint
*tpoint
;
2260 addr
= traceframe_get_pc (tframe
);
2265 /* Fallback strategy, will be incorrect for while-stepping frames
2266 and multi-location tracepoints. */
2267 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2268 return tpoint
->address
;
2271 /* Search for the next traceframe whose address is inside or outside
2274 static struct traceframe
*
2275 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2278 struct traceframe
*tframe
;
2281 *tfnump
= current_traceframe
+ 1;
2282 tframe
= find_traceframe (*tfnump
);
2283 /* The search is not supposed to wrap around. */
2290 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2292 tfaddr
= get_traceframe_address (tframe
);
2294 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2295 : (lo
> tfaddr
|| tfaddr
> hi
))
2304 /* Search for the next traceframe recorded by the given tracepoint.
2305 Note that for multi-location tracepoints, this will find whatever
2306 location appears first. */
2308 static struct traceframe
*
2309 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2311 struct traceframe
*tframe
;
2313 *tfnump
= current_traceframe
+ 1;
2314 tframe
= find_traceframe (*tfnump
);
2315 /* The search is not supposed to wrap around. */
2322 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2324 if (tframe
->tpnum
== num
)
2335 #ifndef IN_PROCESS_AGENT
2337 /* Clear all past trace state. */
2340 cmd_qtinit (char *packet
)
2342 struct trace_state_variable
*tsv
, *prev
, *next
;
2344 /* Make sure we don't try to read from a trace frame. */
2345 current_traceframe
= -1;
2347 trace_debug ("Initializing the trace");
2349 clear_installed_tracepoints ();
2350 clear_readonly_regions ();
2353 last_tracepoint
= NULL
;
2355 /* Clear out any leftover trace state variables. Ones with target
2356 defined getters should be kept however. */
2358 tsv
= trace_state_variables
;
2361 trace_debug ("Looking at var %d", tsv
->number
);
2362 if (tsv
->getter
== NULL
)
2368 trace_state_variables
= next
;
2369 trace_debug ("Deleting var %d", tsv
->number
);
2380 clear_trace_buffer ();
2381 clear_inferior_trace_buffer ();
2386 /* Unprobe the UST marker at ADDRESS. */
2389 unprobe_marker_at (CORE_ADDR address
)
2391 char cmd
[IPA_CMD_BUF_SIZE
];
2393 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2394 run_inferior_command (cmd
, strlen (cmd
) + 1);
2397 /* Restore the program to its pre-tracing state. This routine may be called
2398 in error situations, so it needs to be careful about only restoring
2399 from known-valid bits. */
2402 clear_installed_tracepoints (void)
2404 struct tracepoint
*tpoint
;
2405 struct tracepoint
*prev_stpoint
;
2408 cancel_breakpoints ();
2410 prev_stpoint
= NULL
;
2412 /* Restore any bytes overwritten by tracepoints. */
2413 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2415 /* Catch the case where we might try to remove a tracepoint that
2416 was never actually installed. */
2417 if (tpoint
->handle
== NULL
)
2419 trace_debug ("Tracepoint %d at 0x%s was "
2420 "never installed, nothing to clear",
2421 tpoint
->number
, paddress (tpoint
->address
));
2425 switch (tpoint
->type
)
2427 case trap_tracepoint
:
2428 delete_breakpoint (tpoint
->handle
);
2430 case fast_tracepoint
:
2431 delete_fast_tracepoint_jump (tpoint
->handle
);
2433 case static_tracepoint
:
2434 if (prev_stpoint
!= NULL
2435 && prev_stpoint
->address
== tpoint
->address
)
2436 /* Nothing to do. We already unprobed a tracepoint set at
2437 this marker address (and there can only be one probe
2442 unprobe_marker_at (tpoint
->address
);
2443 prev_stpoint
= tpoint
;
2448 tpoint
->handle
= NULL
;
2454 /* Parse a packet that defines a tracepoint. */
2457 cmd_qtdp (char *own_buf
)
2460 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2461 int trail_hyphen
= 0;
2465 struct tracepoint
*tpoint
;
2467 char *packet
= own_buf
;
2469 packet
+= strlen ("QTDP:");
2471 /* A hyphen at the beginning marks a packet specifying actions for a
2472 tracepoint already supplied. */
2479 packet
= unpack_varlen_hex (packet
, &num
);
2480 ++packet
; /* skip a colon */
2481 packet
= unpack_varlen_hex (packet
, &addr
);
2482 ++packet
; /* skip a colon */
2484 /* See if we already have this tracepoint. */
2485 tpoint
= find_tracepoint (num
, addr
);
2489 /* Duplicate tracepoints are never allowed. */
2492 trace_debug ("Tracepoint error: tracepoint %d"
2493 " at 0x%s already exists",
2494 (int) num
, paddress (addr
));
2495 write_enn (own_buf
);
2499 tpoint
= add_tracepoint (num
, addr
);
2501 tpoint
->enabled
= (*packet
== 'E');
2502 ++packet
; /* skip 'E' */
2503 ++packet
; /* skip a colon */
2504 packet
= unpack_varlen_hex (packet
, &count
);
2505 tpoint
->step_count
= count
;
2506 ++packet
; /* skip a colon */
2507 packet
= unpack_varlen_hex (packet
, &count
);
2508 tpoint
->pass_count
= count
;
2509 /* See if we have any of the additional optional fields. */
2510 while (*packet
== ':')
2515 tpoint
->type
= fast_tracepoint
;
2517 packet
= unpack_varlen_hex (packet
, &count
);
2518 tpoint
->orig_size
= count
;
2520 else if (*packet
== 'S')
2522 tpoint
->type
= static_tracepoint
;
2525 else if (*packet
== 'X')
2527 actparm
= (char *) packet
;
2528 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2531 else if (*packet
== '-')
2533 else if (*packet
== '\0')
2536 trace_debug ("Unknown optional tracepoint field");
2541 trace_debug ("Also has actions\n");
2544 trace_debug ("Defined %stracepoint %d at 0x%s, "
2545 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2546 tpoint
->type
== fast_tracepoint
? "fast "
2547 : tpoint
->type
== static_tracepoint
? "static " : "",
2548 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2549 tpoint
->step_count
, tpoint
->pass_count
);
2552 add_tracepoint_action (tpoint
, packet
);
2555 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2556 (int) num
, paddress (addr
));
2557 write_enn (own_buf
);
2561 /* Install tracepoint during tracing only once for each tracepoint location.
2562 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2563 determine the last QTDP packet for one tracepoint location by checking
2564 trailing hyphen in QTDP packet. */
2565 if (tracing
&& !trail_hyphen
)
2567 struct tracepoint
*tp
= NULL
;
2569 /* Pause all threads temporarily while we patch tracepoints. */
2572 /* download_tracepoint will update global `tracepoints'
2573 list, so it is unsafe to leave threads in jump pad. */
2574 stabilize_threads ();
2576 /* Freeze threads. */
2580 if (tpoint
->type
!= trap_tracepoint
)
2582 /* Find another fast or static tracepoint at the same address. */
2583 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2585 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2586 && tp
->number
!= tpoint
->number
)
2590 /* TPOINT is installed at the same address as TP. */
2593 if (tpoint
->type
== fast_tracepoint
)
2594 clone_fast_tracepoint (tpoint
, tp
);
2595 else if (tpoint
->type
== static_tracepoint
)
2596 tpoint
->handle
= (void *) -1;
2600 if (use_agent
&& tpoint
->type
== fast_tracepoint
2601 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2603 /* Download and install fast tracepoint by agent. */
2604 if (tracepoint_send_agent (tpoint
) == 0)
2608 write_enn (own_buf
);
2609 remove_tracepoint (tpoint
);
2614 download_tracepoint (tpoint
);
2616 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2618 install_tracepoint (tpoint
, own_buf
);
2619 if (strcmp (own_buf
, "OK") != 0)
2620 remove_tracepoint (tpoint
);
2634 cmd_qtdpsrc (char *own_buf
)
2636 ULONGEST num
, addr
, start
, slen
;
2637 struct tracepoint
*tpoint
;
2638 char *packet
= own_buf
;
2639 char *saved
, *srctype
, *src
;
2641 struct source_string
*last
, *newlast
;
2643 packet
+= strlen ("QTDPsrc:");
2645 packet
= unpack_varlen_hex (packet
, &num
);
2646 ++packet
; /* skip a colon */
2647 packet
= unpack_varlen_hex (packet
, &addr
);
2648 ++packet
; /* skip a colon */
2650 /* See if we already have this tracepoint. */
2651 tpoint
= find_tracepoint (num
, addr
);
2655 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2656 (int) num
, paddress (addr
));
2657 write_enn (own_buf
);
2662 packet
= strchr (packet
, ':');
2663 srctype
= xmalloc (packet
- saved
+ 1);
2664 memcpy (srctype
, saved
, packet
- saved
);
2665 srctype
[packet
- saved
] = '\0';
2667 packet
= unpack_varlen_hex (packet
, &start
);
2668 ++packet
; /* skip a colon */
2669 packet
= unpack_varlen_hex (packet
, &slen
);
2670 ++packet
; /* skip a colon */
2671 src
= xmalloc (slen
+ 1);
2672 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2675 newlast
= xmalloc (sizeof (struct source_string
));
2676 newlast
->type
= srctype
;
2678 newlast
->next
= NULL
;
2679 /* Always add a source string to the end of the list;
2680 this keeps sequences of actions/commands in the right
2682 if (tpoint
->source_strings
)
2684 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2686 last
->next
= newlast
;
2689 tpoint
->source_strings
= newlast
;
2695 cmd_qtdv (char *own_buf
)
2697 ULONGEST num
, val
, builtin
;
2700 struct trace_state_variable
*tsv
;
2701 char *packet
= own_buf
;
2703 packet
+= strlen ("QTDV:");
2705 packet
= unpack_varlen_hex (packet
, &num
);
2706 ++packet
; /* skip a colon */
2707 packet
= unpack_varlen_hex (packet
, &val
);
2708 ++packet
; /* skip a colon */
2709 packet
= unpack_varlen_hex (packet
, &builtin
);
2710 ++packet
; /* skip a colon */
2712 nbytes
= strlen (packet
) / 2;
2713 varname
= xmalloc (nbytes
+ 1);
2714 nbytes
= unhexify (varname
, packet
, nbytes
);
2715 varname
[nbytes
] = '\0';
2717 tsv
= create_trace_state_variable (num
, 1);
2718 tsv
->initial_value
= (LONGEST
) val
;
2719 tsv
->name
= varname
;
2721 set_trace_state_variable_value (num
, (LONGEST
) val
);
2727 cmd_qtenable_disable (char *own_buf
, int enable
)
2729 char *packet
= own_buf
;
2731 struct tracepoint
*tp
;
2733 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2734 packet
= unpack_varlen_hex (packet
, &num
);
2735 ++packet
; /* skip a colon */
2736 packet
= unpack_varlen_hex (packet
, &addr
);
2738 tp
= find_tracepoint (num
, addr
);
2742 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2744 trace_debug ("Tracepoint %d at 0x%s is already %s",
2745 (int) num
, paddress (addr
),
2746 enable
? "enabled" : "disabled");
2751 trace_debug ("%s tracepoint %d at 0x%s",
2752 enable
? "Enabling" : "Disabling",
2753 (int) num
, paddress (addr
));
2755 tp
->enabled
= enable
;
2757 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2760 int offset
= offsetof (struct tracepoint
, enabled
);
2761 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2763 ret
= prepare_to_access_memory ();
2766 trace_debug ("Failed to temporarily stop inferior threads");
2767 write_enn (own_buf
);
2771 ret
= write_inferior_integer (obj_addr
, enable
);
2772 done_accessing_memory ();
2776 trace_debug ("Cannot write enabled flag into "
2777 "inferior process memory");
2778 write_enn (own_buf
);
2787 trace_debug ("Tracepoint %d at 0x%s not found",
2788 (int) num
, paddress (addr
));
2789 write_enn (own_buf
);
2794 cmd_qtv (char *own_buf
)
2799 char *packet
= own_buf
;
2801 packet
+= strlen ("qTV:");
2802 unpack_varlen_hex (packet
, &num
);
2804 if (current_traceframe
>= 0)
2806 err
= traceframe_read_tsv ((int) num
, &val
);
2809 strcpy (own_buf
, "U");
2813 /* Only make tsv's be undefined before the first trace run. After a
2814 trace run is over, the user might want to see the last value of
2815 the tsv, and it might not be available in a traceframe. */
2816 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2818 strcpy (own_buf
, "U");
2822 val
= get_trace_state_variable_value (num
);
2824 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2827 /* Clear out the list of readonly regions. */
2830 clear_readonly_regions (void)
2832 struct readonly_region
*roreg
;
2834 while (readonly_regions
)
2836 roreg
= readonly_regions
;
2837 readonly_regions
= readonly_regions
->next
;
2842 /* Parse the collection of address ranges whose contents GDB believes
2843 to be unchanging and so can be read directly from target memory
2844 even while looking at a traceframe. */
2847 cmd_qtro (char *own_buf
)
2849 ULONGEST start
, end
;
2850 struct readonly_region
*roreg
;
2851 char *packet
= own_buf
;
2853 trace_debug ("Want to mark readonly regions");
2855 clear_readonly_regions ();
2857 packet
+= strlen ("QTro");
2859 while (*packet
== ':')
2861 ++packet
; /* skip a colon */
2862 packet
= unpack_varlen_hex (packet
, &start
);
2863 ++packet
; /* skip a comma */
2864 packet
= unpack_varlen_hex (packet
, &end
);
2865 roreg
= xmalloc (sizeof (struct readonly_region
));
2866 roreg
->start
= start
;
2868 roreg
->next
= readonly_regions
;
2869 readonly_regions
= roreg
;
2870 trace_debug ("Added readonly region from 0x%s to 0x%s",
2871 paddress (roreg
->start
), paddress (roreg
->end
));
2877 /* Test to see if the given range is in our list of readonly ranges.
2878 We only test for being entirely within a range, GDB is not going to
2879 send a single memory packet that spans multiple regions. */
2882 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2884 struct readonly_region
*roreg
;
2886 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2887 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2893 /* The maximum size of a jump pad entry. */
2894 static const int max_jump_pad_size
= 0x100;
2896 static CORE_ADDR gdb_jump_pad_head
;
2898 /* Return the address of the next free jump space. */
2901 get_jump_space_head (void)
2903 if (gdb_jump_pad_head
== 0)
2905 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2906 &gdb_jump_pad_head
))
2907 fatal ("error extracting jump_pad_buffer");
2910 return gdb_jump_pad_head
;
2913 /* Reserve USED bytes from the jump space. */
2916 claim_jump_space (ULONGEST used
)
2918 trace_debug ("claim_jump_space reserves %s bytes at %s",
2919 pulongest (used
), paddress (gdb_jump_pad_head
));
2920 gdb_jump_pad_head
+= used
;
2923 static CORE_ADDR trampoline_buffer_head
= 0;
2924 static CORE_ADDR trampoline_buffer_tail
;
2926 /* Reserve USED bytes from the trampoline buffer and return the
2927 address of the start of the reserved space in TRAMPOLINE. Returns
2928 non-zero if the space is successfully claimed. */
2931 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2933 if (!trampoline_buffer_head
)
2935 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2936 &trampoline_buffer_tail
))
2938 fatal ("error extracting trampoline_buffer");
2942 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2943 &trampoline_buffer_head
))
2945 fatal ("error extracting trampoline_buffer_end");
2950 /* Start claiming space from the top of the trampoline space. If
2951 the space is located at the bottom of the virtual address space,
2952 this reduces the possibility that corruption will occur if a null
2953 pointer is used to write to memory. */
2954 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2956 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2961 trampoline_buffer_head
-= used
;
2963 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2964 pulongest (used
), paddress (trampoline_buffer_head
));
2966 *trampoline
= trampoline_buffer_head
;
2970 /* Returns non-zero if there is space allocated for use in trampolines
2971 for fast tracepoints. */
2974 have_fast_tracepoint_trampoline_buffer (char *buf
)
2976 CORE_ADDR trampoline_end
, errbuf
;
2978 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2981 fatal ("error extracting trampoline_buffer_end");
2988 strcpy (buf
, "was claiming");
2989 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2992 fatal ("error extracting errbuf");
2996 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2999 return trampoline_end
!= 0;
3002 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3003 the command fails, or 0 otherwise. If the command ran
3004 successfully, but probing the marker failed, ERROUT will be filled
3005 with the error to reply to GDB, and -1 is also returned. This
3006 allows directly passing IPA errors to GDB. */
3009 probe_marker_at (CORE_ADDR address
, char *errout
)
3011 char cmd
[IPA_CMD_BUF_SIZE
];
3014 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3015 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3021 strcpy (errout
, cmd
);
3030 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3032 to
->jump_pad
= from
->jump_pad
;
3033 to
->jump_pad_end
= from
->jump_pad_end
;
3034 to
->trampoline
= from
->trampoline
;
3035 to
->trampoline_end
= from
->trampoline_end
;
3036 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3037 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3038 to
->handle
= from
->handle
;
3040 gdb_assert (from
->handle
);
3041 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3044 #define MAX_JUMP_SIZE 20
3046 /* Install fast tracepoint. Return 0 if successful, otherwise return
3050 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3052 CORE_ADDR jentry
, jump_entry
;
3053 CORE_ADDR trampoline
;
3054 ULONGEST trampoline_size
;
3056 /* The jump to the jump pad of the last fast tracepoint
3058 unsigned char fjump
[MAX_JUMP_SIZE
];
3059 ULONGEST fjump_size
;
3061 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3063 trace_debug ("Requested a fast tracepoint on an instruction "
3064 "that is of less than the minimum length.");
3068 jentry
= jump_entry
= get_jump_space_head ();
3071 trampoline_size
= 0;
3073 /* Install the jump pad. */
3074 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3076 ipa_sym_addrs
.addr_gdb_collect
,
3077 ipa_sym_addrs
.addr_collecting
,
3080 &trampoline
, &trampoline_size
,
3082 &tpoint
->adjusted_insn_addr
,
3083 &tpoint
->adjusted_insn_addr_end
,
3090 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3093 if (tpoint
->handle
!= NULL
)
3095 tpoint
->jump_pad
= jump_entry
;
3096 tpoint
->jump_pad_end
= jentry
;
3097 tpoint
->trampoline
= trampoline
;
3098 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3100 /* Pad to 8-byte alignment. */
3101 jentry
= ((jentry
+ 7) & ~0x7);
3102 claim_jump_space (jentry
- jump_entry
);
3109 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3112 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3114 tpoint
->handle
= NULL
;
3117 if (tpoint
->type
== trap_tracepoint
)
3119 /* Tracepoints are installed as memory breakpoints. Just go
3120 ahead and install the trap. The breakpoints module
3121 handles duplicated breakpoints, and the memory read
3122 routine handles un-patching traps from memory reads. */
3123 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3124 tracepoint_handler
);
3126 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3128 if (!agent_loaded_p ())
3130 trace_debug ("Requested a %s tracepoint, but fast "
3131 "tracepoints aren't supported.",
3132 tpoint
->type
== static_tracepoint
? "static" : "fast");
3133 write_e_ipa_not_loaded (own_buf
);
3136 if (tpoint
->type
== static_tracepoint
3137 && !in_process_agent_supports_ust ())
3139 trace_debug ("Requested a static tracepoint, but static "
3140 "tracepoints are not supported.");
3141 write_e_ust_not_loaded (own_buf
);
3145 if (tpoint
->type
== fast_tracepoint
)
3146 install_fast_tracepoint (tpoint
, own_buf
);
3149 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3150 tpoint
->handle
= (void *) -1;
3155 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3157 if (tpoint
->handle
== NULL
)
3159 if (*own_buf
== '\0')
3160 write_enn (own_buf
);
3166 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3169 cmd_qtstart (char *packet
)
3171 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3172 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3174 trace_debug ("Starting the trace");
3176 /* Pause all threads temporarily while we patch tracepoints. */
3179 /* Get threads out of jump pads. Safe to do here, since this is a
3180 top level command. And, required to do here, since we're
3181 deleting/rewriting jump pads. */
3183 stabilize_threads ();
3185 /* Freeze threads. */
3188 /* Sync the fast tracepoints list in the inferior ftlib. */
3189 if (agent_loaded_p ())
3190 download_trace_state_variables ();
3192 /* No previous fast tpoint yet. */
3193 prev_ftpoint
= NULL
;
3195 /* No previous static tpoint yet. */
3196 prev_stpoint
= NULL
;
3200 /* Start out empty. */
3201 if (agent_loaded_p ())
3202 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3204 /* Download and install tracepoints. */
3205 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3207 /* Ensure all the hit counts start at zero. */
3208 tpoint
->hit_count
= 0;
3209 tpoint
->traceframe_usage
= 0;
3211 if (tpoint
->type
== trap_tracepoint
)
3213 /* Tracepoints are installed as memory breakpoints. Just go
3214 ahead and install the trap. The breakpoints module
3215 handles duplicated breakpoints, and the memory read
3216 routine handles un-patching traps from memory reads. */
3217 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3218 tracepoint_handler
);
3220 else if (tpoint
->type
== fast_tracepoint
3221 || tpoint
->type
== static_tracepoint
)
3223 if (maybe_write_ipa_not_loaded (packet
))
3225 trace_debug ("Requested a %s tracepoint, but fast "
3226 "tracepoints aren't supported.",
3227 tpoint
->type
== static_tracepoint
3228 ? "static" : "fast");
3232 if (tpoint
->type
== fast_tracepoint
)
3235 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3237 if (prev_ftpoint
!= NULL
3238 && prev_ftpoint
->address
== tpoint
->address
)
3241 tracepoint_send_agent (tpoint
);
3243 download_tracepoint_1 (tpoint
);
3245 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3249 /* Tracepoint is installed successfully? */
3252 /* Download and install fast tracepoint by agent. */
3254 installed
= !tracepoint_send_agent (tpoint
);
3257 download_tracepoint_1 (tpoint
);
3258 installed
= !install_fast_tracepoint (tpoint
, packet
);
3262 prev_ftpoint
= tpoint
;
3267 if (!in_process_agent_supports_ust ())
3269 trace_debug ("Requested a static tracepoint, but static "
3270 "tracepoints are not supported.");
3274 download_tracepoint_1 (tpoint
);
3275 /* Can only probe a given marker once. */
3276 if (prev_stpoint
!= NULL
3277 && prev_stpoint
->address
== tpoint
->address
)
3278 tpoint
->handle
= (void *) -1;
3281 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3283 tpoint
->handle
= (void *) -1;
3285 /* So that we can handle multiple static tracepoints
3286 at the same address easily. */
3287 prev_stpoint
= tpoint
;
3293 tpptr
= tpoint
->obj_addr_on_target
;
3295 if (tpoint
== tracepoints
)
3296 /* First object in list, set the head pointer in the
3298 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3300 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3305 /* Any failure in the inner loop is sufficient cause to give
3307 if (tpoint
->handle
== NULL
)
3311 /* Any error in tracepoint insertion is unacceptable; better to
3312 address the problem now, than end up with a useless or misleading
3316 clear_installed_tracepoints ();
3317 if (*packet
== '\0')
3323 stopping_tracepoint
= NULL
;
3324 trace_buffer_is_full
= 0;
3325 expr_eval_result
= expr_eval_no_error
;
3326 error_tracepoint
= NULL
;
3327 tracing_start_time
= get_timestamp ();
3329 /* Tracing is now active, hits will now start being logged. */
3332 if (agent_loaded_p ())
3334 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3335 fatal ("Error setting tracing variable in lib");
3337 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3339 fatal ("Error clearing stopping_tracepoint variable in lib");
3341 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3342 fatal ("Error clearing trace_buffer_is_full variable in lib");
3344 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3345 stop_tracing_handler
);
3346 if (stop_tracing_bkpt
== NULL
)
3347 error ("Error setting stop_tracing breakpoint");
3349 flush_trace_buffer_bkpt
3350 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3351 flush_trace_buffer_handler
);
3352 if (flush_trace_buffer_bkpt
== NULL
)
3353 error ("Error setting flush_trace_buffer breakpoint");
3361 /* End a tracing run, filling in a stop reason to report back to GDB,
3362 and removing the tracepoints from the code. */
3369 trace_debug ("Tracing is already off, ignoring");
3373 trace_debug ("Stopping the trace");
3375 /* Pause all threads before removing fast jumps from memory,
3376 breakpoints, and touching IPA state variables (inferior memory).
3377 Some thread may hit the internal tracing breakpoints, or be
3378 collecting this moment, but that's ok, we don't release the
3379 tpoint object's memory or the jump pads here (we only do that
3380 when we're sure we can move all threads out of the jump pads).
3381 We can't now, since we may be getting here due to the inferior
3382 agent calling us. */
3384 /* Since we're removing breakpoints, cancel breakpoint hits,
3385 possibly related to the breakpoints we're about to delete. */
3386 cancel_breakpoints ();
3388 /* Stop logging. Tracepoints can still be hit, but they will not be
3391 if (agent_loaded_p ())
3393 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3394 fatal ("Error clearing tracing variable in lib");
3397 tracing_stop_time
= get_timestamp ();
3398 tracing_stop_reason
= "t???";
3399 tracing_stop_tpnum
= 0;
3400 if (stopping_tracepoint
)
3402 trace_debug ("Stopping the trace because "
3403 "tracepoint %d was hit %" PRIu64
" times",
3404 stopping_tracepoint
->number
,
3405 stopping_tracepoint
->pass_count
);
3406 tracing_stop_reason
= "tpasscount";
3407 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3409 else if (trace_buffer_is_full
)
3411 trace_debug ("Stopping the trace because the trace buffer is full");
3412 tracing_stop_reason
= "tfull";
3414 else if (expr_eval_result
!= expr_eval_no_error
)
3416 trace_debug ("Stopping the trace because of an expression eval error");
3417 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3418 tracing_stop_tpnum
= error_tracepoint
->number
;
3420 #ifndef IN_PROCESS_AGENT
3421 else if (!gdb_connected ())
3423 trace_debug ("Stopping the trace because GDB disconnected");
3424 tracing_stop_reason
= "tdisconnected";
3429 trace_debug ("Stopping the trace because of a tstop command");
3430 tracing_stop_reason
= "tstop";
3433 stopping_tracepoint
= NULL
;
3434 error_tracepoint
= NULL
;
3436 /* Clear out the tracepoints. */
3437 clear_installed_tracepoints ();
3439 if (agent_loaded_p ())
3441 /* Pull in fast tracepoint trace frames from the inferior lib
3442 buffer into our buffer, even if our buffer is already full,
3443 because we want to present the full number of created frames
3444 in addition to what fit in the trace buffer. */
3445 upload_fast_traceframes ();
3448 if (stop_tracing_bkpt
!= NULL
)
3450 delete_breakpoint (stop_tracing_bkpt
);
3451 stop_tracing_bkpt
= NULL
;
3454 if (flush_trace_buffer_bkpt
!= NULL
)
3456 delete_breakpoint (flush_trace_buffer_bkpt
);
3457 flush_trace_buffer_bkpt
= NULL
;
3464 stop_tracing_handler (CORE_ADDR addr
)
3466 trace_debug ("lib hit stop_tracing");
3468 /* Don't actually handle it here. When we stop tracing we remove
3469 breakpoints from the inferior, and that is not allowed in a
3470 breakpoint handler (as the caller is walking the breakpoint
3476 flush_trace_buffer_handler (CORE_ADDR addr
)
3478 trace_debug ("lib hit flush_trace_buffer");
3483 cmd_qtstop (char *packet
)
3490 cmd_qtdisconnected (char *own_buf
)
3493 char *packet
= own_buf
;
3495 packet
+= strlen ("QTDisconnected:");
3497 unpack_varlen_hex (packet
, &setting
);
3501 disconnected_tracing
= setting
;
3505 cmd_qtframe (char *own_buf
)
3507 ULONGEST frame
, pc
, lo
, hi
, num
;
3509 struct traceframe
*tframe
;
3510 char *packet
= own_buf
;
3512 packet
+= strlen ("QTFrame:");
3514 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3516 packet
+= strlen ("pc:");
3517 unpack_varlen_hex (packet
, &pc
);
3518 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3519 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3521 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3523 packet
+= strlen ("range:");
3524 packet
= unpack_varlen_hex (packet
, &lo
);
3526 unpack_varlen_hex (packet
, &hi
);
3527 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3528 paddress (lo
), paddress (hi
));
3529 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3531 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3533 packet
+= strlen ("outside:");
3534 packet
= unpack_varlen_hex (packet
, &lo
);
3536 unpack_varlen_hex (packet
, &hi
);
3537 trace_debug ("Want to find next traceframe "
3538 "outside the range 0x%s to 0x%s",
3539 paddress (lo
), paddress (hi
));
3540 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3542 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3544 packet
+= strlen ("tdp:");
3545 unpack_varlen_hex (packet
, &num
);
3547 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3548 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3552 unpack_varlen_hex (packet
, &frame
);
3553 tfnum
= (int) frame
;
3556 trace_debug ("Want to stop looking at traceframes");
3557 current_traceframe
= -1;
3561 trace_debug ("Want to look at traceframe %d", tfnum
);
3562 tframe
= find_traceframe (tfnum
);
3567 current_traceframe
= tfnum
;
3568 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3571 sprintf (own_buf
, "F-1");
3575 cmd_qtstatus (char *packet
)
3577 char *stop_reason_rsp
= NULL
;
3578 char *buf1
, *buf2
, *buf3
, *str
;
3581 /* Translate the plain text of the notes back into hex for
3584 str
= (tracing_user_name
? tracing_user_name
: "");
3585 slen
= strlen (str
);
3586 buf1
= (char *) alloca (slen
* 2 + 1);
3587 hexify (buf1
, str
, slen
);
3589 str
= (tracing_notes
? tracing_notes
: "");
3590 slen
= strlen (str
);
3591 buf2
= (char *) alloca (slen
* 2 + 1);
3592 hexify (buf2
, str
, slen
);
3594 str
= (tracing_stop_note
? tracing_stop_note
: "");
3595 slen
= strlen (str
);
3596 buf3
= (char *) alloca (slen
* 2 + 1);
3597 hexify (buf3
, str
, slen
);
3599 trace_debug ("Returning trace status as %d, stop reason %s",
3600 tracing
, tracing_stop_reason
);
3602 if (agent_loaded_p ())
3606 upload_fast_traceframes ();
3611 stop_reason_rsp
= (char *) tracing_stop_reason
;
3613 /* The user visible error string in terror needs to be hex encoded.
3614 We leave it as plain string in `tracing_stop_reason' to ease
3616 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3618 const char *result_name
;
3622 result_name
= stop_reason_rsp
+ strlen ("terror:");
3623 hexstr_len
= strlen (result_name
) * 2;
3624 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3625 strcpy (p
, "terror:");
3627 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3630 /* If this was a forced stop, include any stop note that was supplied. */
3631 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3633 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3634 strcpy (stop_reason_rsp
, "tstop:");
3635 strcat (stop_reason_rsp
, buf3
);
3641 "tframes:%x;tcreated:%x;"
3642 "tfree:%x;tsize:%s;"
3645 "starttime:%s;stoptime:%s;"
3646 "username:%s:;notes:%s:",
3648 stop_reason_rsp
, tracing_stop_tpnum
,
3649 traceframe_count
, traceframes_created
,
3650 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3651 circular_trace_buffer
,
3652 disconnected_tracing
,
3653 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3658 cmd_qtp (char *own_buf
)
3661 struct tracepoint
*tpoint
;
3662 char *packet
= own_buf
;
3664 packet
+= strlen ("qTP:");
3666 packet
= unpack_varlen_hex (packet
, &num
);
3667 ++packet
; /* skip a colon */
3668 packet
= unpack_varlen_hex (packet
, &addr
);
3670 /* See if we already have this tracepoint. */
3671 tpoint
= find_tracepoint (num
, addr
);
3675 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3676 (int) num
, paddress (addr
));
3677 write_enn (own_buf
);
3681 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3682 tpoint
->traceframe_usage
);
3685 /* State variables to help return all the tracepoint bits. */
3686 static struct tracepoint
*cur_tpoint
;
3687 static int cur_action
;
3688 static int cur_step_action
;
3689 static struct source_string
*cur_source_string
;
3690 static struct trace_state_variable
*cur_tsv
;
3692 /* Compose a response that is an imitation of the syntax by which the
3693 tracepoint was originally downloaded. */
3696 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3700 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3701 paddress (tpoint
->address
),
3702 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3703 tpoint
->pass_count
);
3704 if (tpoint
->type
== fast_tracepoint
)
3705 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3706 else if (tpoint
->type
== static_tracepoint
)
3707 sprintf (packet
+ strlen (packet
), ":S");
3711 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3712 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3713 tpoint
->cond
->length
, buf
);
3718 /* Compose a response that is an imitation of the syntax by which the
3719 tracepoint action was originally downloaded (with the difference
3720 that due to the way we store the actions, this will output a packet
3721 per action, while GDB could have combined more than one action
3725 response_action (char *packet
, struct tracepoint
*tpoint
,
3726 char *taction
, int step
)
3728 sprintf (packet
, "%c%x:%s:%s",
3729 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3733 /* Compose a response that is an imitation of the syntax by which the
3734 tracepoint source piece was originally downloaded. */
3737 response_source (char *packet
,
3738 struct tracepoint
*tpoint
, struct source_string
*src
)
3743 len
= strlen (src
->str
);
3744 buf
= alloca (len
* 2 + 1);
3745 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3747 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3748 tpoint
->number
, paddress (tpoint
->address
),
3749 src
->type
, 0, len
, buf
);
3752 /* Return the first piece of tracepoint definition, and initialize the
3753 state machine that will iterate through all the tracepoint
3757 cmd_qtfp (char *packet
)
3759 trace_debug ("Returning first tracepoint definition piece");
3761 cur_tpoint
= tracepoints
;
3762 cur_action
= cur_step_action
= -1;
3763 cur_source_string
= NULL
;
3766 response_tracepoint (packet
, cur_tpoint
);
3768 strcpy (packet
, "l");
3771 /* Return additional pieces of tracepoint definition. Each action and
3772 stepping action must go into its own packet, because of packet size
3773 limits, and so we use state variables to deliver one piece at a
3777 cmd_qtsp (char *packet
)
3779 trace_debug ("Returning subsequent tracepoint definition piece");
3783 /* This case would normally never occur, but be prepared for
3785 strcpy (packet
, "l");
3787 else if (cur_action
< cur_tpoint
->numactions
- 1)
3790 response_action (packet
, cur_tpoint
,
3791 cur_tpoint
->actions_str
[cur_action
], 0);
3793 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3796 response_action (packet
, cur_tpoint
,
3797 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3799 else if ((cur_source_string
3800 ? cur_source_string
->next
3801 : cur_tpoint
->source_strings
))
3803 if (cur_source_string
)
3804 cur_source_string
= cur_source_string
->next
;
3806 cur_source_string
= cur_tpoint
->source_strings
;
3807 response_source (packet
, cur_tpoint
, cur_source_string
);
3811 cur_tpoint
= cur_tpoint
->next
;
3812 cur_action
= cur_step_action
= -1;
3813 cur_source_string
= NULL
;
3815 response_tracepoint (packet
, cur_tpoint
);
3817 strcpy (packet
, "l");
3821 /* Compose a response that is an imitation of the syntax by which the
3822 trace state variable was originally downloaded. */
3825 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3827 char *buf
= (char *) "";
3832 namelen
= strlen (tsv
->name
);
3833 buf
= alloca (namelen
* 2 + 1);
3834 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3837 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3838 tsv
->getter
? 1 : 0, buf
);
3841 /* Return the first trace state variable definition, and initialize
3842 the state machine that will iterate through all the tsv bits. */
3845 cmd_qtfv (char *packet
)
3847 trace_debug ("Returning first trace state variable definition");
3849 cur_tsv
= trace_state_variables
;
3852 response_tsv (packet
, cur_tsv
);
3854 strcpy (packet
, "l");
3857 /* Return additional trace state variable definitions. */
3860 cmd_qtsv (char *packet
)
3862 trace_debug ("Returning additional trace state variable definition");
3866 cur_tsv
= cur_tsv
->next
;
3868 response_tsv (packet
, cur_tsv
);
3870 strcpy (packet
, "l");
3873 strcpy (packet
, "l");
3876 /* Return the first static tracepoint marker, and initialize the state
3877 machine that will iterate through all the static tracepoints
3881 cmd_qtfstm (char *packet
)
3883 if (!maybe_write_ipa_ust_not_loaded (packet
))
3884 run_inferior_command (packet
, strlen (packet
) + 1);
3887 /* Return additional static tracepoints markers. */
3890 cmd_qtsstm (char *packet
)
3892 if (!maybe_write_ipa_ust_not_loaded (packet
))
3893 run_inferior_command (packet
, strlen (packet
) + 1);
3896 /* Return the definition of the static tracepoint at a given address.
3897 Result packet is the same as qTsST's. */
3900 cmd_qtstmat (char *packet
)
3902 if (!maybe_write_ipa_ust_not_loaded (packet
))
3903 run_inferior_command (packet
, strlen (packet
) + 1);
3906 /* Sent the agent a command to close it. */
3909 gdb_agent_about_to_close (int pid
)
3911 char buf
[IPA_CMD_BUF_SIZE
];
3913 if (!maybe_write_ipa_not_loaded (buf
))
3915 struct thread_info
*save_inferior
;
3916 struct inferior_list_entry
*inf
= all_threads
.head
;
3918 save_inferior
= current_inferior
;
3920 /* Find a certain thread which belongs to process PID. */
3923 if (ptid_get_pid (inf
->id
) == pid
)
3928 current_inferior
= (struct thread_info
*) inf
;
3930 strcpy (buf
, "close");
3932 run_inferior_command (buf
, strlen (buf
) + 1);
3934 current_inferior
= save_inferior
;
3938 /* Return the minimum instruction size needed for fast tracepoints as a
3939 hexadecimal number. */
3942 cmd_qtminftpilen (char *packet
)
3944 if (current_inferior
== NULL
)
3946 /* Indicate that the minimum length is currently unknown. */
3947 strcpy (packet
, "0");
3951 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3954 /* Respond to qTBuffer packet with a block of raw data from the trace
3955 buffer. GDB may ask for a lot, but we are allowed to reply with
3956 only as much as will fit within packet limits or whatever. */
3959 cmd_qtbuffer (char *own_buf
)
3961 ULONGEST offset
, num
, tot
;
3963 char *packet
= own_buf
;
3965 packet
+= strlen ("qTBuffer:");
3967 packet
= unpack_varlen_hex (packet
, &offset
);
3968 ++packet
; /* skip a comma */
3969 unpack_varlen_hex (packet
, &num
);
3971 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3972 (int) num
, pulongest (offset
));
3974 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3976 /* If we're right at the end, reply specially that we're done. */
3979 strcpy (own_buf
, "l");
3983 /* Object to any other out-of-bounds request. */
3986 write_enn (own_buf
);
3990 /* Compute the pointer corresponding to the given offset, accounting
3992 tbp
= trace_buffer_start
+ offset
;
3993 if (tbp
>= trace_buffer_wrap
)
3994 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3996 /* Trim to the remaining bytes if we're close to the end. */
3997 if (num
> tot
- offset
)
4000 /* Trim to available packet size. */
4001 if (num
>= (PBUFSIZ
- 16) / 2 )
4002 num
= (PBUFSIZ
- 16) / 2;
4004 convert_int_to_ascii (tbp
, own_buf
, num
);
4005 own_buf
[num
] = '\0';
4009 cmd_bigqtbuffer_circular (char *own_buf
)
4012 char *packet
= own_buf
;
4014 packet
+= strlen ("QTBuffer:circular:");
4016 unpack_varlen_hex (packet
, &val
);
4017 circular_trace_buffer
= val
;
4018 trace_debug ("Trace buffer is now %s",
4019 circular_trace_buffer
? "circular" : "linear");
4024 cmd_qtnotes (char *own_buf
)
4027 char *saved
, *user
, *notes
, *stopnote
;
4028 char *packet
= own_buf
;
4030 packet
+= strlen ("QTNotes:");
4034 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4036 packet
+= strlen ("user:");
4038 packet
= strchr (packet
, ';');
4039 nbytes
= (packet
- saved
) / 2;
4040 user
= xmalloc (nbytes
+ 1);
4041 nbytes
= unhexify (user
, saved
, nbytes
);
4042 user
[nbytes
] = '\0';
4043 ++packet
; /* skip the semicolon */
4044 trace_debug ("User is '%s'", user
);
4045 tracing_user_name
= user
;
4047 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4049 packet
+= strlen ("notes:");
4051 packet
= strchr (packet
, ';');
4052 nbytes
= (packet
- saved
) / 2;
4053 notes
= xmalloc (nbytes
+ 1);
4054 nbytes
= unhexify (notes
, saved
, nbytes
);
4055 notes
[nbytes
] = '\0';
4056 ++packet
; /* skip the semicolon */
4057 trace_debug ("Notes is '%s'", notes
);
4058 tracing_notes
= notes
;
4060 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4062 packet
+= strlen ("tstop:");
4064 packet
= strchr (packet
, ';');
4065 nbytes
= (packet
- saved
) / 2;
4066 stopnote
= xmalloc (nbytes
+ 1);
4067 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4068 stopnote
[nbytes
] = '\0';
4069 ++packet
; /* skip the semicolon */
4070 trace_debug ("tstop note is '%s'", stopnote
);
4071 tracing_stop_note
= stopnote
;
4081 handle_tracepoint_general_set (char *packet
)
4083 if (strcmp ("QTinit", packet
) == 0)
4085 cmd_qtinit (packet
);
4088 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4093 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4095 cmd_qtdpsrc (packet
);
4098 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4100 cmd_qtenable_disable (packet
, 1);
4103 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4105 cmd_qtenable_disable (packet
, 0);
4108 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4113 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4118 else if (strcmp ("QTStart", packet
) == 0)
4120 cmd_qtstart (packet
);
4123 else if (strcmp ("QTStop", packet
) == 0)
4125 cmd_qtstop (packet
);
4128 else if (strncmp ("QTDisconnected:", packet
,
4129 strlen ("QTDisconnected:")) == 0)
4131 cmd_qtdisconnected (packet
);
4134 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4136 cmd_qtframe (packet
);
4139 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4141 cmd_bigqtbuffer_circular (packet
);
4144 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4146 cmd_qtnotes (packet
);
4154 handle_tracepoint_query (char *packet
)
4156 if (strcmp ("qTStatus", packet
) == 0)
4158 cmd_qtstatus (packet
);
4161 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4166 else if (strcmp ("qTfP", packet
) == 0)
4171 else if (strcmp ("qTsP", packet
) == 0)
4176 else if (strcmp ("qTfV", packet
) == 0)
4181 else if (strcmp ("qTsV", packet
) == 0)
4186 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4191 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4193 cmd_qtbuffer (packet
);
4196 else if (strcmp ("qTfSTM", packet
) == 0)
4198 cmd_qtfstm (packet
);
4201 else if (strcmp ("qTsSTM", packet
) == 0)
4203 cmd_qtsstm (packet
);
4206 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4208 cmd_qtstmat (packet
);
4211 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4213 cmd_qtminftpilen (packet
);
4221 #ifndef IN_PROCESS_AGENT
4223 /* Call this when thread TINFO has hit the tracepoint defined by
4224 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4225 action. This adds a while-stepping collecting state item to the
4226 threads' collecting state list, so that we can keep track of
4227 multiple simultaneous while-stepping actions being collected by the
4228 same thread. This can happen in cases like:
4230 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4232 ff0003 INSN3 <-- TP2, collect $regs
4233 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4236 Notice that when instruction INSN5 is reached, the while-stepping
4237 actions of both TP1 and TP3 are still being collected, and that TP2
4238 had been collected meanwhile. The whole range of ff0001-ff0005
4239 should be single-stepped, due to at least TP1's while-stepping
4240 action covering the whole range. */
4243 add_while_stepping_state (struct thread_info
*tinfo
,
4244 int tp_number
, CORE_ADDR tp_address
)
4246 struct wstep_state
*wstep
;
4248 wstep
= xmalloc (sizeof (*wstep
));
4249 wstep
->next
= tinfo
->while_stepping
;
4251 wstep
->tp_number
= tp_number
;
4252 wstep
->tp_address
= tp_address
;
4253 wstep
->current_step
= 0;
4255 tinfo
->while_stepping
= wstep
;
4258 /* Release the while-stepping collecting state WSTEP. */
4261 release_while_stepping_state (struct wstep_state
*wstep
)
4266 /* Release all while-stepping collecting states currently associated
4267 with thread TINFO. */
4270 release_while_stepping_state_list (struct thread_info
*tinfo
)
4272 struct wstep_state
*head
;
4274 while (tinfo
->while_stepping
)
4276 head
= tinfo
->while_stepping
;
4277 tinfo
->while_stepping
= head
->next
;
4278 release_while_stepping_state (head
);
4282 /* If TINFO was handling a 'while-stepping' action, the step has
4283 finished, so collect any step data needed, and check if any more
4284 steps are required. Return true if the thread was indeed
4285 collecting tracepoint data, false otherwise. */
4288 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4290 struct tracepoint
*tpoint
;
4291 struct wstep_state
*wstep
;
4292 struct wstep_state
**wstep_link
;
4293 struct trap_tracepoint_ctx ctx
;
4295 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4297 if (agent_loaded_p ())
4298 upload_fast_traceframes ();
4300 /* Check if we were indeed collecting data for one of more
4301 tracepoints with a 'while-stepping' count. */
4302 if (tinfo
->while_stepping
== NULL
)
4307 /* We're not even tracing anymore. Stop this thread from
4309 release_while_stepping_state_list (tinfo
);
4311 /* The thread had stopped due to a single-step request indeed
4312 explained by a tracepoint. */
4316 wstep
= tinfo
->while_stepping
;
4317 wstep_link
= &tinfo
->while_stepping
;
4319 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4320 target_pid_to_str (tinfo
->entry
.id
),
4321 wstep
->tp_number
, paddress (wstep
->tp_address
));
4323 ctx
.base
.type
= trap_tracepoint
;
4324 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4326 while (wstep
!= NULL
)
4328 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4331 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4332 wstep
->tp_number
, paddress (wstep
->tp_address
),
4333 target_pid_to_str (tinfo
->entry
.id
));
4336 *wstep_link
= wstep
->next
;
4337 release_while_stepping_state (wstep
);
4338 wstep
= *wstep_link
;
4342 /* We've just finished one step. */
4343 ++wstep
->current_step
;
4346 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4347 stop_pc
, tpoint
, wstep
->current_step
);
4349 if (wstep
->current_step
>= tpoint
->step_count
)
4351 /* The requested numbers of steps have occurred. */
4352 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4353 target_pid_to_str (tinfo
->entry
.id
),
4354 wstep
->tp_number
, paddress (wstep
->tp_address
));
4356 /* Unlink the wstep. */
4357 *wstep_link
= wstep
->next
;
4358 release_while_stepping_state (wstep
);
4359 wstep
= *wstep_link
;
4361 /* Only check the hit count now, which ensure that we do all
4362 our stepping before stopping the run. */
4363 if (tpoint
->pass_count
> 0
4364 && tpoint
->hit_count
>= tpoint
->pass_count
4365 && stopping_tracepoint
== NULL
)
4366 stopping_tracepoint
= tpoint
;
4370 /* Keep single-stepping until the requested numbers of steps
4372 wstep_link
= &wstep
->next
;
4373 wstep
= *wstep_link
;
4376 if (stopping_tracepoint
4377 || trace_buffer_is_full
4378 || expr_eval_result
!= expr_eval_no_error
)
4388 /* Handle any internal tracing control breakpoint hits. That means,
4389 pull traceframes from the IPA to our buffer, and syncing both
4390 tracing agents when the IPA's tracing stops for some reason. */
4393 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4395 /* Pull in fast tracepoint trace frames from the inferior in-process
4396 agent's buffer into our buffer. */
4398 if (!agent_loaded_p ())
4401 upload_fast_traceframes ();
4403 /* Check if the in-process agent had decided we should stop
4405 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4407 int ipa_trace_buffer_is_full
;
4408 CORE_ADDR ipa_stopping_tracepoint
;
4409 int ipa_expr_eval_result
;
4410 CORE_ADDR ipa_error_tracepoint
;
4412 trace_debug ("lib stopped at stop_tracing");
4414 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4415 &ipa_trace_buffer_is_full
);
4417 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4418 &ipa_stopping_tracepoint
);
4419 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4421 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4422 &ipa_error_tracepoint
);
4423 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4425 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4426 &ipa_expr_eval_result
);
4427 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4429 trace_debug ("lib: trace_buffer_is_full: %d, "
4430 "stopping_tracepoint: %s, "
4431 "ipa_expr_eval_result: %d, "
4432 "error_tracepoint: %s, ",
4433 ipa_trace_buffer_is_full
,
4434 paddress (ipa_stopping_tracepoint
),
4435 ipa_expr_eval_result
,
4436 paddress (ipa_error_tracepoint
));
4440 if (ipa_trace_buffer_is_full
)
4441 trace_debug ("lib stopped due to full buffer.");
4442 if (ipa_stopping_tracepoint
)
4443 trace_debug ("lib stopped due to tpoint");
4444 if (ipa_stopping_tracepoint
)
4445 trace_debug ("lib stopped due to error");
4448 if (ipa_stopping_tracepoint
!= 0)
4451 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4453 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4455 expr_eval_result
= ipa_expr_eval_result
;
4457 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4462 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4464 trace_debug ("lib stopped at flush_trace_buffer");
4471 /* Return true if TINFO just hit a tracepoint. Collect data if
4475 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4477 struct tracepoint
*tpoint
;
4479 struct trap_tracepoint_ctx ctx
;
4481 /* Not tracing, don't handle. */
4485 ctx
.base
.type
= trap_tracepoint
;
4486 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4488 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4490 /* Note that we collect fast tracepoints here as well. We'll
4491 step over the fast tracepoint jump later, which avoids the
4492 double collect. However, we don't collect for static
4493 tracepoints here, because UST markers are compiled in program,
4494 and probes will be executed in program. So static tracepoints
4495 are collected there. */
4496 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4497 && tpoint
->type
!= static_tracepoint
)
4499 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4500 target_pid_to_str (tinfo
->entry
.id
),
4501 tpoint
->number
, paddress (tpoint
->address
));
4503 /* Test the condition if present, and collect if true. */
4505 || (condition_true_at_tracepoint
4506 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4507 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4510 if (stopping_tracepoint
4511 || trace_buffer_is_full
4512 || expr_eval_result
!= expr_eval_no_error
)
4516 /* If the tracepoint had a 'while-stepping' action, then set
4517 the thread to collect this tracepoint on the following
4519 else if (tpoint
->step_count
> 0)
4521 add_while_stepping_state (tinfo
,
4522 tpoint
->number
, tpoint
->address
);
4534 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4535 struct ust_marker_data
;
4536 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4537 struct traceframe
*tframe
);
4540 /* Create a trace frame for the hit of the given tracepoint in the
4544 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4545 struct tracepoint
*tpoint
)
4547 struct traceframe
*tframe
;
4550 /* Only count it as a hit when we actually collect data. */
4551 tpoint
->hit_count
++;
4553 /* If we've exceeded a defined pass count, record the event for
4554 later, and finish the collection for this hit. This test is only
4555 for nonstepping tracepoints, stepping tracepoints test at the end
4556 of their while-stepping loop. */
4557 if (tpoint
->pass_count
> 0
4558 && tpoint
->hit_count
>= tpoint
->pass_count
4559 && tpoint
->step_count
== 0
4560 && stopping_tracepoint
== NULL
)
4561 stopping_tracepoint
= tpoint
;
4563 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4564 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4566 tframe
= add_traceframe (tpoint
);
4570 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4572 #ifndef IN_PROCESS_AGENT
4573 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4574 tpoint
->number
, paddress (tpoint
->address
),
4575 tpoint
->actions_str
[acti
]);
4578 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4579 tpoint
->actions
[acti
]);
4582 finish_traceframe (tframe
);
4585 if (tframe
== NULL
&& tracing
)
4586 trace_buffer_is_full
= 1;
4589 #ifndef IN_PROCESS_AGENT
4592 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4594 struct tracepoint
*tpoint
, int current_step
)
4596 struct traceframe
*tframe
;
4599 trace_debug ("Making new step traceframe for "
4600 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4601 tpoint
->number
, paddress (tpoint
->address
),
4602 current_step
, tpoint
->step_count
,
4605 tframe
= add_traceframe (tpoint
);
4609 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4611 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4612 tpoint
->number
, paddress (tpoint
->address
),
4613 tpoint
->step_actions_str
[acti
]);
4615 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4616 tpoint
->step_actions
[acti
]);
4619 finish_traceframe (tframe
);
4622 if (tframe
== NULL
&& tracing
)
4623 trace_buffer_is_full
= 1;
4628 static struct regcache
*
4629 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4631 struct regcache
*regcache
= NULL
;
4633 #ifdef IN_PROCESS_AGENT
4634 if (ctx
->type
== fast_tracepoint
)
4636 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4637 if (!fctx
->regcache_initted
)
4639 fctx
->regcache_initted
= 1;
4640 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4641 supply_regblock (&fctx
->regcache
, NULL
);
4642 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4644 regcache
= &fctx
->regcache
;
4647 if (ctx
->type
== static_tracepoint
)
4649 struct static_tracepoint_ctx
*sctx
4650 = (struct static_tracepoint_ctx
*) ctx
;
4652 if (!sctx
->regcache_initted
)
4654 sctx
->regcache_initted
= 1;
4655 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4656 supply_regblock (&sctx
->regcache
, NULL
);
4657 /* Pass down the tracepoint address, because REGS doesn't
4658 include the PC, but we know what it must have been. */
4659 supply_static_tracepoint_registers (&sctx
->regcache
,
4660 (const unsigned char *)
4662 sctx
->tpoint
->address
);
4664 regcache
= &sctx
->regcache
;
4668 if (ctx
->type
== trap_tracepoint
)
4670 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4671 regcache
= tctx
->regcache
;
4675 gdb_assert (regcache
!= NULL
);
4681 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4683 struct tracepoint
*tpoint
,
4684 struct traceframe
*tframe
,
4685 struct tracepoint_action
*taction
)
4687 enum eval_result_type err
;
4689 switch (taction
->type
)
4693 struct collect_memory_action
*maction
;
4694 struct eval_agent_expr_context ax_ctx
;
4696 maction
= (struct collect_memory_action
*) taction
;
4697 ax_ctx
.regcache
= NULL
;
4698 ax_ctx
.tframe
= tframe
;
4699 ax_ctx
.tpoint
= tpoint
;
4701 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4702 pulongest (maction
->len
),
4703 paddress (maction
->addr
), maction
->basereg
);
4704 /* (should use basereg) */
4705 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4711 unsigned char *regspace
;
4712 struct regcache tregcache
;
4713 struct regcache
*context_regcache
;
4716 trace_debug ("Want to collect registers");
4718 /* Collect all registers for now. */
4719 regspace
= add_traceframe_block (tframe
, tpoint
,
4720 1 + register_cache_size ());
4721 if (regspace
== NULL
)
4723 trace_debug ("Trace buffer block allocation failed, skipping");
4726 /* Identify a register block. */
4729 context_regcache
= get_context_regcache (ctx
);
4731 /* Wrap the regblock in a register cache (in the stack, we
4732 don't want to malloc here). */
4733 init_register_cache (&tregcache
, regspace
+ 1);
4735 /* Copy the register data to the regblock. */
4736 regcache_cpy (&tregcache
, context_regcache
);
4738 #ifndef IN_PROCESS_AGENT
4739 /* On some platforms, trap-based tracepoints will have the PC
4740 pointing to the next instruction after the trap, but we
4741 don't want the user or GDB trying to guess whether the
4742 saved PC needs adjusting; so always record the adjusted
4743 stop_pc. Note that we can't use tpoint->address instead,
4744 since it will be wrong for while-stepping actions. This
4745 adjustment is a nop for fast tracepoints collected from the
4746 in-process lib (but not if GDBserver is collecting one
4747 preemptively), since the PC had already been adjusted to
4748 contain the tracepoint's address by the jump pad. */
4749 trace_debug ("Storing stop pc (0x%s) in regblock",
4750 paddress (stop_pc
));
4752 /* This changes the regblock, not the thread's
4754 regcache_write_pc (&tregcache
, stop_pc
);
4760 struct eval_expr_action
*eaction
;
4761 struct eval_agent_expr_context ax_ctx
;
4763 eaction
= (struct eval_expr_action
*) taction
;
4764 ax_ctx
.regcache
= get_context_regcache (ctx
);
4765 ax_ctx
.tframe
= tframe
;
4766 ax_ctx
.tpoint
= tpoint
;
4768 trace_debug ("Want to evaluate expression");
4770 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4772 if (err
!= expr_eval_no_error
)
4774 record_tracepoint_error (tpoint
, "action expression", err
);
4781 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4782 trace_debug ("Want to collect static trace data");
4783 collect_ust_data_at_tracepoint (ctx
, tframe
);
4785 trace_debug ("warning: collecting static trace data, "
4786 "but static tracepoints are not supported");
4791 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4797 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4798 struct tracepoint
*tpoint
)
4801 enum eval_result_type err
;
4803 /* Presently, gdbserver doesn't run compiled conditions, only the
4804 IPA does. If the program stops at a fast tracepoint's address
4805 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4806 gdbserver preemptively collect the fast tracepoint. Later, on
4807 resume, gdbserver steps over the fast tracepoint like it steps
4808 over breakpoints, so that the IPA doesn't see that fast
4809 tracepoint. This avoids double collects of fast tracepoints in
4810 that stopping scenario. Having gdbserver itself handle the fast
4811 tracepoint gives the user a consistent view of when fast or trap
4812 tracepoints are collected, compared to an alternative where only
4813 trap tracepoints are collected on stop, and fast tracepoints on
4814 resume. When a fast tracepoint is being processed by gdbserver,
4815 it is always the non-compiled condition expression that is
4817 #ifdef IN_PROCESS_AGENT
4818 if (tpoint
->compiled_cond
)
4819 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4823 struct eval_agent_expr_context ax_ctx
;
4825 ax_ctx
.regcache
= get_context_regcache (ctx
);
4826 ax_ctx
.tframe
= NULL
;
4827 ax_ctx
.tpoint
= tpoint
;
4829 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4831 if (err
!= expr_eval_no_error
)
4833 record_tracepoint_error (tpoint
, "condition", err
);
4834 /* The error case must return false. */
4838 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4839 tpoint
->number
, paddress (tpoint
->address
),
4841 return (value
? 1 : 0);
4844 /* Do memory copies for bytecodes. */
4845 /* Do the recording of memory blocks for actions and bytecodes. */
4848 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4849 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4851 unsigned char *mspace
;
4852 ULONGEST remaining
= len
;
4853 unsigned short blocklen
;
4855 /* If a 'to' buffer is specified, use it. */
4858 read_inferior_memory (from
, to
, len
);
4862 /* Otherwise, create a new memory block in the trace buffer. */
4863 while (remaining
> 0)
4867 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4868 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4869 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4872 /* Identify block as a memory block. */
4875 /* Record address and size. */
4876 memcpy (mspace
, &from
, sizeof (from
));
4877 mspace
+= sizeof (from
);
4878 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4879 mspace
+= sizeof (blocklen
);
4880 /* Record the memory block proper. */
4881 read_inferior_memory (from
, mspace
, blocklen
);
4882 trace_debug ("%d bytes recorded", blocklen
);
4883 remaining
-= blocklen
;
4890 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4891 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4893 unsigned char *buf
, *mspace
;
4894 ULONGEST remaining
= len
;
4895 unsigned short blocklen
, i
;
4897 /* To save a bit of space, block lengths are 16-bit, so break large
4898 requests into multiple blocks. Bordering on overkill for strings,
4899 but it could happen that someone specifies a large max length. */
4900 while (remaining
> 0)
4904 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4905 /* We want working space to accumulate nonzero bytes, since
4906 traceframes must have a predecided size (otherwise it gets
4907 harder to wrap correctly for the circular case, etc). */
4908 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4909 for (i
= 0; i
< blocklen
; ++i
)
4911 /* Read the string one byte at a time, in case the string is
4912 at the end of a valid memory area - we don't want a
4913 correctly-terminated string to engender segvio
4915 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4920 /* Make sure outer loop stops now too. */
4921 remaining
= blocklen
;
4925 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4926 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4932 /* Identify block as a memory block. */
4935 /* Record address and size. */
4936 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4937 mspace
+= sizeof (from
);
4938 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4939 mspace
+= sizeof (blocklen
);
4940 /* Copy the string contents. */
4941 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4942 remaining
-= blocklen
;
4949 /* Record the value of a trace state variable. */
4952 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
4954 unsigned char *vspace
;
4957 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
4958 1 + sizeof (n
) + sizeof (LONGEST
));
4961 /* Identify block as a variable. */
4963 /* Record variable's number and value. */
4964 memcpy (vspace
+ 1, &n
, sizeof (n
));
4965 val
= get_trace_state_variable_value (n
);
4966 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4967 trace_debug ("Variable %d recorded", n
);
4971 #ifndef IN_PROCESS_AGENT
4973 /* Callback for traceframe_walk_blocks, used to find a given block
4974 type in a traceframe. */
4977 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4979 char *wantedp
= data
;
4981 if (*wantedp
== blocktype
)
4987 /* Walk over all traceframe blocks of the traceframe buffer starting
4988 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4989 block found, passing in DATA unmodified. If CALLBACK returns true,
4990 this returns a pointer to where the block is found. Returns NULL
4991 if no callback call returned true, indicating that all blocks have
4994 static unsigned char *
4995 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4997 int (*callback
) (char blocktype
,
4998 unsigned char *dataptr
,
5002 unsigned char *dataptr
;
5006 trace_debug ("traceframe %d has no data", tfnum
);
5010 /* Iterate through a traceframe's blocks, looking for a block of the
5012 for (dataptr
= database
;
5013 dataptr
< database
+ datasize
;
5017 unsigned short mlen
;
5019 if (dataptr
== trace_buffer_wrap
)
5021 /* Adjust to reflect wrapping part of the frame around to
5023 datasize
= dataptr
- database
;
5024 dataptr
= database
= trace_buffer_lo
;
5027 blocktype
= *dataptr
++;
5029 if ((*callback
) (blocktype
, dataptr
, data
))
5035 /* Skip over the registers block. */
5036 dataptr
+= register_cache_size ();
5039 /* Skip over the memory block. */
5040 dataptr
+= sizeof (CORE_ADDR
);
5041 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5042 dataptr
+= (sizeof (mlen
) + mlen
);
5045 /* Skip over the TSV block. */
5046 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5049 /* Skip over the static trace data block. */
5050 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5051 dataptr
+= (sizeof (mlen
) + mlen
);
5054 trace_debug ("traceframe %d has unknown block type 0x%x",
5063 /* Look for the block of type TYPE_WANTED in the trameframe starting
5064 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5067 static unsigned char *
5068 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5069 int tfnum
, char type_wanted
)
5071 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5072 match_blocktype
, &type_wanted
);
5075 static unsigned char *
5076 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5078 unsigned char *regblock
;
5080 regblock
= traceframe_find_block_type (tframe
->data
,
5084 if (regblock
== NULL
)
5085 trace_debug ("traceframe %d has no register data", tfnum
);
5090 /* Get registers from a traceframe. */
5093 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5095 unsigned char *dataptr
;
5096 struct tracepoint
*tpoint
;
5097 struct traceframe
*tframe
;
5099 tframe
= find_traceframe (tfnum
);
5103 trace_debug ("traceframe %d not found", tfnum
);
5107 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5108 if (dataptr
== NULL
)
5110 /* Mark registers unavailable. */
5111 supply_regblock (regcache
, NULL
);
5113 /* We can generally guess at a PC, although this will be
5114 misleading for while-stepping frames and multi-location
5116 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5118 regcache_write_pc (regcache
, tpoint
->address
);
5121 supply_regblock (regcache
, dataptr
);
5127 traceframe_get_pc (struct traceframe
*tframe
)
5129 struct regcache regcache
;
5130 unsigned char *dataptr
;
5132 dataptr
= traceframe_find_regblock (tframe
, -1);
5133 if (dataptr
== NULL
)
5136 init_register_cache (®cache
, dataptr
);
5137 return regcache_read_pc (®cache
);
5140 /* Read a requested block of memory from a trace frame. */
5143 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5144 unsigned char *buf
, ULONGEST length
,
5147 struct traceframe
*tframe
;
5148 unsigned char *database
, *dataptr
;
5149 unsigned int datasize
;
5151 unsigned short mlen
;
5153 trace_debug ("traceframe_read_mem");
5155 tframe
= find_traceframe (tfnum
);
5159 trace_debug ("traceframe %d not found", tfnum
);
5163 datasize
= tframe
->data_size
;
5164 database
= dataptr
= &tframe
->data
[0];
5166 /* Iterate through a traceframe's blocks, looking for memory. */
5167 while ((dataptr
= traceframe_find_block_type (dataptr
,
5169 - (dataptr
- database
),
5170 tfnum
, 'M')) != NULL
)
5172 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5173 dataptr
+= sizeof (maddr
);
5174 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5175 dataptr
+= sizeof (mlen
);
5176 trace_debug ("traceframe %d has %d bytes at %s",
5177 tfnum
, mlen
, paddress (maddr
));
5179 /* If the block includes the first part of the desired range,
5180 return as much it has; GDB will re-request the remainder,
5181 which might be in a different block of this trace frame. */
5182 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5184 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5188 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5193 /* Skip over this block. */
5197 trace_debug ("traceframe %d has no memory data for the desired region",
5205 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5208 struct traceframe
*tframe
;
5209 unsigned char *database
, *dataptr
;
5210 unsigned int datasize
;
5213 trace_debug ("traceframe_read_tsv");
5215 tfnum
= current_traceframe
;
5219 trace_debug ("no current traceframe");
5223 tframe
= find_traceframe (tfnum
);
5227 trace_debug ("traceframe %d not found", tfnum
);
5231 datasize
= tframe
->data_size
;
5232 database
= dataptr
= &tframe
->data
[0];
5234 /* Iterate through a traceframe's blocks, looking for the tsv. */
5235 while ((dataptr
= traceframe_find_block_type (dataptr
,
5237 - (dataptr
- database
),
5238 tfnum
, 'V')) != NULL
)
5240 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5241 dataptr
+= sizeof (vnum
);
5243 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5245 /* Check that this is the variable we want. */
5248 memcpy (val
, dataptr
, sizeof (*val
));
5252 /* Skip over this block. */
5253 dataptr
+= sizeof (LONGEST
);
5256 trace_debug ("traceframe %d has no data for variable %d",
5261 /* Read a requested block of static tracepoint data from a trace
5265 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5266 unsigned char *buf
, ULONGEST length
,
5269 struct traceframe
*tframe
;
5270 unsigned char *database
, *dataptr
;
5271 unsigned int datasize
;
5272 unsigned short mlen
;
5274 trace_debug ("traceframe_read_sdata");
5276 tframe
= find_traceframe (tfnum
);
5280 trace_debug ("traceframe %d not found", tfnum
);
5284 datasize
= tframe
->data_size
;
5285 database
= &tframe
->data
[0];
5287 /* Iterate through a traceframe's blocks, looking for static
5289 dataptr
= traceframe_find_block_type (database
, datasize
,
5291 if (dataptr
!= NULL
)
5293 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5294 dataptr
+= sizeof (mlen
);
5297 if (offset
+ length
> mlen
)
5298 length
= mlen
- offset
;
5300 memcpy (buf
, dataptr
, length
);
5308 trace_debug ("traceframe %d has no static trace data", tfnum
);
5314 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5315 object. DATA is pointer to a struct buffer holding the
5316 traceframe-info object being built. */
5319 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5321 struct buffer
*buffer
= data
;
5327 unsigned short mlen
;
5330 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5331 dataptr
+= sizeof (maddr
);
5332 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5333 dataptr
+= sizeof (mlen
);
5334 buffer_xml_printf (buffer
,
5335 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5336 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5346 warning ("Unhandled trace block type (%d) '%c ' "
5347 "while building trace frame info.",
5348 blocktype
, blocktype
);
5355 /* Build a traceframe-info object for traceframe number TFNUM into
5359 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5361 struct traceframe
*tframe
;
5363 trace_debug ("traceframe_read_info");
5365 tframe
= find_traceframe (tfnum
);
5369 trace_debug ("traceframe %d not found", tfnum
);
5373 buffer_grow_str (buffer
, "<traceframe-info>\n");
5374 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5375 tfnum
, build_traceframe_info_xml
, buffer
);
5376 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5380 /* Return the first fast tracepoint whose jump pad contains PC. */
5382 static struct tracepoint
*
5383 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5385 struct tracepoint
*tpoint
;
5387 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5388 if (tpoint
->type
== fast_tracepoint
)
5389 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5395 /* Return the first fast tracepoint whose trampoline contains PC. */
5397 static struct tracepoint
*
5398 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5400 struct tracepoint
*tpoint
;
5402 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5404 if (tpoint
->type
== fast_tracepoint
5405 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5412 /* Return GDBserver's tracepoint that matches the IP Agent's
5413 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5416 static struct tracepoint
*
5417 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5419 struct tracepoint
*tpoint
;
5421 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5422 if (tpoint
->type
== fast_tracepoint
)
5423 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5431 /* The type of the object that is used to synchronize fast tracepoint
5434 typedef struct collecting_t
5436 /* The fast tracepoint number currently collecting. */
5439 /* A number that GDBserver can use to identify the thread that is
5440 presently holding the collect lock. This need not (and usually
5441 is not) the thread id, as getting the current thread ID usually
5442 requires a system call, which we want to avoid like the plague.
5443 Usually this is thread's TCB, found in the TLS (pseudo-)
5444 register, which is readable with a single insn on several
5446 uintptr_t thread_area
;
5449 #ifndef IN_PROCESS_AGENT
5452 force_unlock_trace_buffer (void)
5454 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5457 /* Check if the thread identified by THREAD_AREA which is stopped at
5458 STOP_PC, is presently locking the fast tracepoint collection, and
5459 if so, gather some status of said collection. Returns 0 if the
5460 thread isn't collecting or in the jump pad at all. 1, if in the
5461 jump pad (or within gdb_collect) and hasn't executed the adjusted
5462 original insn yet (can set a breakpoint there and run to it). 2,
5463 if presently executing the adjusted original insn --- in which
5464 case, if we want to move the thread out of the jump pad, we need to
5465 single-step it until this function returns 0. */
5468 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5470 struct fast_tpoint_collect_status
*status
)
5472 CORE_ADDR ipa_collecting
;
5473 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5474 CORE_ADDR ipa_gdb_trampoline_buffer
;
5475 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5476 struct tracepoint
*tpoint
;
5477 int needs_breakpoint
;
5479 /* The thread THREAD_AREA is either:
5481 0. not collecting at all, not within the jump pad, or within
5482 gdb_collect or one of its callees.
5484 1. in the jump pad and haven't reached gdb_collect
5486 2. within gdb_collect (out of the jump pad) (collect is set)
5488 3. we're in the jump pad, after gdb_collect having returned,
5489 possibly executing the adjusted insns.
5491 For cases 1 and 3, `collecting' may or not be set. The jump pad
5492 doesn't have any complicated jump logic, so we can tell if the
5493 thread is executing the adjust original insn or not by just
5494 matching STOP_PC with known jump pad addresses. If we it isn't
5495 yet executing the original insn, set a breakpoint there, and let
5496 the thread run to it, so to quickly step over a possible (many
5497 insns) gdb_collect call. Otherwise, or when the breakpoint is
5498 hit, only a few (small number of) insns are left to be executed
5499 in the jump pad. Single-step the thread until it leaves the
5504 needs_breakpoint
= 0;
5505 trace_debug ("fast_tracepoint_collecting");
5507 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5508 &ipa_gdb_jump_pad_buffer
))
5509 fatal ("error extracting `gdb_jump_pad_buffer'");
5510 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5511 &ipa_gdb_jump_pad_buffer_end
))
5512 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5514 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5515 &ipa_gdb_trampoline_buffer
))
5516 fatal ("error extracting `gdb_trampoline_buffer'");
5517 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5518 &ipa_gdb_trampoline_buffer_end
))
5519 fatal ("error extracting `gdb_trampoline_buffer_end'");
5521 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5522 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5524 /* We can tell which tracepoint(s) the thread is collecting by
5525 matching the jump pad address back to the tracepoint. */
5526 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5529 warning ("in jump pad, but no matching tpoint?");
5534 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5536 tpoint
->number
, paddress (tpoint
->address
),
5537 paddress (tpoint
->jump_pad
),
5538 paddress (tpoint
->jump_pad_end
),
5539 paddress (tpoint
->adjusted_insn_addr
),
5540 paddress (tpoint
->adjusted_insn_addr_end
));
5543 /* Definitely in the jump pad. May or may not need
5544 fast-exit-jump-pad breakpoint. */
5545 if (tpoint
->jump_pad
<= stop_pc
5546 && stop_pc
< tpoint
->adjusted_insn_addr
)
5547 needs_breakpoint
= 1;
5549 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5550 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5552 /* We can tell which tracepoint(s) the thread is collecting by
5553 matching the trampoline address back to the tracepoint. */
5554 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5557 warning ("in trampoline, but no matching tpoint?");
5562 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5563 tpoint
->number
, paddress (tpoint
->address
),
5564 paddress (tpoint
->trampoline
),
5565 paddress (tpoint
->trampoline_end
));
5568 /* Have not reached jump pad yet, but treat the trampoline as a
5569 part of the jump pad that is before the adjusted original
5571 needs_breakpoint
= 1;
5575 collecting_t ipa_collecting_obj
;
5577 /* If `collecting' is set/locked, then the THREAD_AREA thread
5578 may or not be the one holding the lock. We have to read the
5579 lock to find out. */
5581 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5584 trace_debug ("fast_tracepoint_collecting:"
5585 " failed reading 'collecting' in the inferior");
5589 if (!ipa_collecting
)
5591 trace_debug ("fast_tracepoint_collecting: not collecting"
5592 " (and nobody is).");
5596 /* Some thread is collecting. Check which. */
5597 if (read_inferior_memory (ipa_collecting
,
5598 (unsigned char *) &ipa_collecting_obj
,
5599 sizeof (ipa_collecting_obj
)) != 0)
5602 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5604 trace_debug ("fast_tracepoint_collecting: not collecting "
5605 "(another thread is)");
5610 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5613 warning ("fast_tracepoint_collecting: collecting, "
5614 "but tpoint %s not found?",
5615 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5619 /* The thread is within `gdb_collect', skip over the rest of
5620 fast tracepoint collection quickly using a breakpoint. */
5621 needs_breakpoint
= 1;
5624 /* The caller wants a bit of status detail. */
5627 status
->tpoint_num
= tpoint
->number
;
5628 status
->tpoint_addr
= tpoint
->address
;
5629 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5630 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5633 if (needs_breakpoint
)
5635 /* Hasn't executed the original instruction yet. Set breakpoint
5636 there, and wait till it's hit, then single-step until exiting
5640 fast_tracepoint_collecting, returning continue-until-break at %s",
5641 paddress (tpoint
->adjusted_insn_addr
));
5643 return 1; /* continue */
5647 /* Just single-step until exiting the jump pad. */
5649 trace_debug ("fast_tracepoint_collecting, returning "
5650 "need-single-step (%s-%s)",
5651 paddress (tpoint
->adjusted_insn_addr
),
5652 paddress (tpoint
->adjusted_insn_addr_end
));
5654 return 2; /* single-step */
5660 #ifdef IN_PROCESS_AGENT
5662 /* The global fast tracepoint collect lock. Points to a collecting_t
5663 object built on the stack by the jump pad, if presently locked;
5664 NULL if it isn't locked. Note that this lock *must* be set while
5665 executing any *function other than the jump pad. See
5666 fast_tracepoint_collecting. */
5667 static collecting_t
* ATTR_USED collecting
;
5669 /* This routine, called from the jump pad (in asm) is designed to be
5670 called from the jump pads of fast tracepoints, thus it is on the
5673 IP_AGENT_EXPORT
void ATTR_USED
5674 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5676 struct fast_tracepoint_ctx ctx
;
5678 /* Don't do anything until the trace run is completely set up. */
5682 ctx
.base
.type
= fast_tracepoint
;
5684 ctx
.regcache_initted
= 0;
5685 /* Wrap the regblock in a register cache (in the stack, we don't
5686 want to malloc here). */
5687 ctx
.regspace
= alloca (register_cache_size ());
5688 if (ctx
.regspace
== NULL
)
5690 trace_debug ("Trace buffer block allocation failed, skipping");
5694 for (ctx
.tpoint
= tpoint
;
5695 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5696 ctx
.tpoint
= ctx
.tpoint
->next
)
5698 if (!ctx
.tpoint
->enabled
)
5701 /* Multiple tracepoints of different types, such as fast tracepoint and
5702 static tracepoint, can be set at the same address. */
5703 if (ctx
.tpoint
->type
!= tpoint
->type
)
5706 /* Test the condition if present, and collect if true. */
5707 if (ctx
.tpoint
->cond
== NULL
5708 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5711 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5712 ctx
.tpoint
->address
, ctx
.tpoint
);
5714 /* Note that this will cause original insns to be written back
5715 to where we jumped from, but that's OK because we're jumping
5716 back to the next whole instruction. This will go badly if
5717 instruction restoration is not atomic though. */
5718 if (stopping_tracepoint
5719 || trace_buffer_is_full
5720 || expr_eval_result
!= expr_eval_no_error
)
5728 /* If there was a condition and it evaluated to false, the only
5729 way we would stop tracing is if there was an error during
5730 condition expression evaluation. */
5731 if (expr_eval_result
!= expr_eval_no_error
)
5742 #ifndef IN_PROCESS_AGENT
5745 get_raw_reg_func_addr (void)
5747 return ipa_sym_addrs
.addr_get_raw_reg
;
5751 get_get_tsv_func_addr (void)
5753 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5757 get_set_tsv_func_addr (void)
5759 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5763 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5764 CORE_ADDR
*jump_entry
)
5766 CORE_ADDR entry_point
= *jump_entry
;
5767 enum eval_result_type err
;
5769 trace_debug ("Starting condition compilation for tracepoint %d\n",
5772 /* Initialize the global pointer to the code being built. */
5773 current_insn_ptr
= *jump_entry
;
5777 err
= compile_bytecodes (tpoint
->cond
);
5779 if (err
== expr_eval_no_error
)
5783 /* Record the beginning of the compiled code. */
5784 tpoint
->compiled_cond
= entry_point
;
5786 trace_debug ("Condition compilation for tracepoint %d complete\n",
5791 /* Leave the unfinished code in situ, but don't point to it. */
5793 tpoint
->compiled_cond
= 0;
5795 trace_debug ("Condition compilation for tracepoint %d failed, "
5797 tpoint
->number
, err
);
5800 /* Update the code pointer passed in. Note that we do this even if
5801 the compile fails, so that we can look at the partial results
5802 instead of letting them be overwritten. */
5803 *jump_entry
= current_insn_ptr
;
5805 /* Leave a gap, to aid dump decipherment. */
5809 /* We'll need to adjust these when we consider bi-arch setups, and big
5813 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5815 return write_inferior_memory (where
,
5816 (unsigned char *) &ptr
, sizeof (void *));
5819 /* The base pointer of the IPA's heap. This is the only memory the
5820 IPA is allowed to use. The IPA should _not_ call the inferior's
5821 `malloc' during operation. That'd be slow, and, most importantly,
5822 it may not be safe. We may be collecting a tracepoint in a signal
5823 handler, for example. */
5824 static CORE_ADDR target_tp_heap
;
5826 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5830 target_malloc (ULONGEST size
)
5834 if (target_tp_heap
== 0)
5836 /* We have the pointer *address*, need what it points to. */
5837 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5839 fatal ("could get target heap head pointer");
5842 ptr
= target_tp_heap
;
5843 target_tp_heap
+= size
;
5845 /* Pad to 8-byte alignment. */
5846 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5852 download_agent_expr (struct agent_expr
*expr
)
5854 CORE_ADDR expr_addr
;
5855 CORE_ADDR expr_bytes
;
5857 expr_addr
= target_malloc (sizeof (*expr
));
5858 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5860 expr_bytes
= target_malloc (expr
->length
);
5861 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5863 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5868 /* Align V up to N bits. */
5869 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5871 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5874 download_tracepoint_1 (struct tracepoint
*tpoint
)
5876 struct tracepoint target_tracepoint
;
5877 CORE_ADDR tpptr
= 0;
5879 gdb_assert (tpoint
->type
== fast_tracepoint
5880 || tpoint
->type
== static_tracepoint
);
5882 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5884 CORE_ADDR jentry
, jump_entry
;
5886 jentry
= jump_entry
= get_jump_space_head ();
5888 if (tpoint
->cond
!= NULL
)
5890 /* Pad to 8-byte alignment. (needed?) */
5891 /* Actually this should be left for the target to
5893 jentry
= UALIGN (jentry
, 8);
5895 compile_tracepoint_condition (tpoint
, &jentry
);
5898 /* Pad to 8-byte alignment. */
5899 jentry
= UALIGN (jentry
, 8);
5900 claim_jump_space (jentry
- jump_entry
);
5903 target_tracepoint
= *tpoint
;
5905 tpptr
= target_malloc (sizeof (*tpoint
));
5906 tpoint
->obj_addr_on_target
= tpptr
;
5908 /* Write the whole object. We'll fix up its pointers in a bit.
5909 Assume no next for now. This is fixed up above on the next
5910 iteration, if there's any. */
5911 target_tracepoint
.next
= NULL
;
5912 /* Need to clear this here too, since we're downloading the
5913 tracepoints before clearing our own copy. */
5914 target_tracepoint
.hit_count
= 0;
5916 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5917 sizeof (target_tracepoint
));
5920 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5922 download_agent_expr (tpoint
->cond
));
5924 if (tpoint
->numactions
)
5927 CORE_ADDR actions_array
;
5929 /* The pointers array. */
5931 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5932 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5936 /* Now for each pointer, download the action. */
5937 for (i
= 0; i
< tpoint
->numactions
; i
++)
5939 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5940 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5942 if (ipa_action
!= 0)
5943 write_inferior_data_ptr
5944 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5950 #define IPA_PROTO_FAST_TRACE_FLAG 0
5951 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
5952 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
5953 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
5954 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
5956 /* Send a command to agent to download and install tracepoint TPOINT. */
5959 tracepoint_send_agent (struct tracepoint
*tpoint
)
5961 char buf
[IPA_CMD_BUF_SIZE
];
5966 strcpy (p
, "FastTrace:");
5969 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
5970 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
5971 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
5972 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
5973 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
5974 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
5975 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
5976 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
5977 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
5978 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
5979 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
5982 p
= agent_expr_send (p
, tpoint
->cond
);
5984 /* tracepoint_action */
5985 for (i
= 0; i
< tpoint
->numactions
; i
++)
5987 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5989 p
[0] = action
->type
;
5990 p
= action
->ops
->send (&p
[1], action
);
5993 get_jump_space_head ();
5994 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
5995 agent can use jump pad from it. */
5996 if (tpoint
->type
== fast_tracepoint
)
5998 memcpy (p
, &gdb_jump_pad_head
, 8);
6002 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6006 if (strncmp (buf
, "OK", 2) != 0)
6009 /* The value of tracepoint's target address is stored in BUF. */
6010 memcpy (&tpoint
->obj_addr_on_target
,
6011 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6013 if (tpoint
->type
== fast_tracepoint
)
6016 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6019 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6020 (unsigned int) tpoint
->obj_addr_on_target
,
6021 (unsigned int) gdb_jump_pad_head
);
6023 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6025 /* This has been done in agent. We should also set up record for it. */
6026 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6029 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6036 download_tracepoint (struct tracepoint
*tpoint
)
6038 struct tracepoint
*tp
, *tp_prev
;
6040 if (tpoint
->type
!= fast_tracepoint
6041 && tpoint
->type
!= static_tracepoint
)
6044 download_tracepoint_1 (tpoint
);
6046 /* Find the previous entry of TPOINT, which is fast tracepoint or
6047 static tracepoint. */
6049 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6051 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6057 CORE_ADDR tp_prev_target_next_addr
;
6059 /* Insert TPOINT after TP_PREV in IPA. */
6060 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6061 + offsetof (struct tracepoint
, next
),
6062 &tp_prev_target_next_addr
))
6063 fatal ("error reading `tp_prev->next'");
6065 /* tpoint->next = tp_prev->next */
6066 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6067 + offsetof (struct tracepoint
, next
),
6068 tp_prev_target_next_addr
);
6069 /* tp_prev->next = tpoint */
6070 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6071 + offsetof (struct tracepoint
, next
),
6072 tpoint
->obj_addr_on_target
);
6075 /* First object in list, set the head pointer in the
6077 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6078 tpoint
->obj_addr_on_target
);
6083 download_trace_state_variables (void)
6085 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6086 struct trace_state_variable
*tsv
;
6088 /* Start out empty. */
6089 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6091 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6093 struct trace_state_variable target_tsv
;
6095 /* TSV's with a getter have been initialized equally in both the
6096 inferior and GDBserver. Skip them. */
6097 if (tsv
->getter
!= NULL
)
6103 ptr
= target_malloc (sizeof (*tsv
));
6105 if (tsv
== trace_state_variables
)
6107 /* First object in list, set the head pointer in the
6110 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6115 write_inferior_data_ptr (prev_ptr
6116 + offsetof (struct trace_state_variable
,
6121 /* Write the whole object. We'll fix up its pointers in a bit.
6122 Assume no next, fixup when needed. */
6123 target_tsv
.next
= NULL
;
6125 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6126 sizeof (target_tsv
));
6128 if (tsv
->name
!= NULL
)
6130 size_t size
= strlen (tsv
->name
) + 1;
6131 CORE_ADDR name_addr
= target_malloc (size
);
6132 write_inferior_memory (name_addr
,
6133 (unsigned char *) tsv
->name
, size
);
6134 write_inferior_data_ptr (ptr
6135 + offsetof (struct trace_state_variable
,
6140 if (tsv
->getter
!= NULL
)
6142 fatal ("what to do with these?");
6148 /* Fixup the next pointer in the last item in the list. */
6149 write_inferior_data_ptr (prev_ptr
6150 + offsetof (struct trace_state_variable
,
6155 /* Upload complete trace frames out of the IP Agent's trace buffer
6156 into GDBserver's trace buffer. This always uploads either all or
6157 no trace frames. This is the counter part of
6158 `trace_alloc_trace_buffer'. See its description of the atomic
6159 synching mechanism. */
6162 upload_fast_traceframes (void)
6164 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6165 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6167 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6168 unsigned int curr_tbctrl_idx
;
6169 unsigned int ipa_trace_buffer_ctrl_curr
;
6170 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6171 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6172 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6173 CORE_ADDR ipa_trace_buffer_lo
;
6174 CORE_ADDR ipa_trace_buffer_hi
;
6176 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6177 &ipa_traceframe_read_count_racy
))
6179 /* This will happen in most targets if the current thread is
6184 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6185 &ipa_traceframe_write_count_racy
))
6188 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6189 ipa_traceframe_write_count_racy
6190 - ipa_traceframe_read_count_racy
,
6191 ipa_traceframe_write_count_racy
,
6192 ipa_traceframe_read_count_racy
);
6194 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6197 about_to_request_buffer_space_bkpt
6198 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6201 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6202 &ipa_trace_buffer_ctrl_curr
))
6205 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6207 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6210 unsigned int prev
, counter
;
6212 /* Update the token, with new counters, and the GDBserver stamp
6213 bit. Alway reuse the current TBC index. */
6214 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6215 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6217 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6223 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6224 ipa_trace_buffer_ctrl_curr
))
6227 trace_debug ("Lib: Committed %08x -> %08x",
6228 ipa_trace_buffer_ctrl_curr_old
,
6229 ipa_trace_buffer_ctrl_curr
);
6231 /* Re-read these, now that we've installed the
6232 `about_to_request_buffer_space' breakpoint/lock. A thread could
6233 have finished a traceframe between the last read of these
6234 counters and setting the breakpoint above. If we start
6235 uploading, we never want to leave this function with
6236 traceframe_read_count != 0, otherwise, GDBserver could end up
6237 incrementing the counter tokens more than once (due to event loop
6238 nesting), which would break the IP agent's "effective" detection
6239 (see trace_alloc_trace_buffer). */
6240 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6241 &ipa_traceframe_read_count
))
6243 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6244 &ipa_traceframe_write_count
))
6249 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6250 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6251 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6253 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6254 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6255 trace_debug ("note that ipa_traceframe_count's parts changed");
6258 /* Get the address of the current TBC object (the IP agent has an
6259 array of 3 such objects). The index is stored in the TBC
6261 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6262 ipa_trace_buffer_ctrl_addr
6263 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6265 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6266 (unsigned char *) &ipa_trace_buffer_ctrl
,
6267 sizeof (struct ipa_trace_buffer_control
)))
6270 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6271 &ipa_trace_buffer_lo
))
6273 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6274 &ipa_trace_buffer_hi
))
6277 /* Offsets are easier to grok for debugging than raw addresses,
6278 especially for the small trace buffer sizes that are useful for
6280 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6281 "endfree=%d wrap=%d hi=%d",
6283 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6284 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6285 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6286 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6287 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6289 /* Note that the IPA's buffer is always circular. */
6291 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6293 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6294 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6296 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6297 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6298 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6299 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6302 tf
= IPA_FIRST_TRACEFRAME ();
6304 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6306 struct tracepoint
*tpoint
;
6307 struct traceframe
*tframe
;
6308 unsigned char *block
;
6309 struct traceframe ipa_tframe
;
6311 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6312 offsetof (struct traceframe
, data
)))
6313 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6315 if (ipa_tframe
.tpnum
== 0)
6316 fatal ("Uploading: No (more) fast traceframes, but "
6317 "ipa_traceframe_count == %u??\n",
6318 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6320 /* Note that this will be incorrect for multi-location
6322 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6324 tframe
= add_traceframe (tpoint
);
6327 trace_buffer_is_full
= 1;
6328 trace_debug ("Uploading: trace buffer is full");
6332 /* Copy the whole set of blocks in one go for now. FIXME:
6333 split this in smaller blocks. */
6334 block
= add_traceframe_block (tframe
, tpoint
,
6335 ipa_tframe
.data_size
);
6338 if (read_inferior_memory (tf
6339 + offsetof (struct traceframe
, data
),
6340 block
, ipa_tframe
.data_size
))
6341 error ("Uploading: Couldn't read traceframe data at %s\n",
6342 paddress (tf
+ offsetof (struct traceframe
, data
)));
6345 trace_debug ("Uploading: traceframe didn't fit");
6346 finish_traceframe (tframe
);
6349 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6351 /* If we freed the traceframe that wrapped around, go back
6352 to the non-wrap case. */
6353 if (tf
< ipa_trace_buffer_ctrl
.start
)
6355 trace_debug ("Lib: Discarding past the wraparound");
6356 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6358 ipa_trace_buffer_ctrl
.start
= tf
;
6359 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6360 ++ipa_traceframe_read_count
;
6362 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6363 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6365 trace_debug ("Lib: buffer is fully empty. "
6366 "Trace buffer [%d] start=%d free=%d endfree=%d",
6368 (int) (ipa_trace_buffer_ctrl
.start
6369 - ipa_trace_buffer_lo
),
6370 (int) (ipa_trace_buffer_ctrl
.free
6371 - ipa_trace_buffer_lo
),
6372 (int) (ipa_trace_buffer_ctrl
.end_free
6373 - ipa_trace_buffer_lo
));
6375 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6376 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6377 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6378 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6381 trace_debug ("Uploaded a traceframe\n"
6382 "Lib: Trace buffer [%d] start=%d free=%d "
6383 "endfree=%d wrap=%d hi=%d",
6385 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6386 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6387 (int) (ipa_trace_buffer_ctrl
.end_free
6388 - ipa_trace_buffer_lo
),
6389 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6390 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6393 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6394 (unsigned char *) &ipa_trace_buffer_ctrl
,
6395 sizeof (struct ipa_trace_buffer_control
)))
6398 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6399 ipa_traceframe_read_count
);
6401 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6404 cancel_breakpoints ();
6406 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6407 about_to_request_buffer_space_bkpt
= NULL
;
6411 if (trace_buffer_is_full
)
6416 #ifdef IN_PROCESS_AGENT
6418 IP_AGENT_EXPORT
int ust_loaded
;
6419 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6423 /* Static tracepoints. */
6425 /* UST puts a "struct tracepoint" in the global namespace, which
6426 conflicts with our tracepoint. Arguably, being a library, it
6427 shouldn't take ownership of such a generic name. We work around it
6429 #define tracepoint ust_tracepoint
6430 #include <ust/ust.h>
6433 extern int serialize_to_text (char *outbuf
, int bufsize
,
6434 const char *fmt
, va_list ap
);
6436 #define GDB_PROBE_NAME "gdb"
6438 /* We dynamically search for the UST symbols instead of linking them
6439 in. This lets the user decide if the application uses static
6440 tracepoints, instead of always pulling libust.so in. This vector
6441 holds pointers to all functions we care about. */
6445 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6446 const char *fmt
, va_list ap
);
6448 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6449 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6451 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6453 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6456 void (*marker_iter_start
) (struct marker_iter
*iter
);
6457 void (*marker_iter_next
) (struct marker_iter
*iter
);
6458 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6459 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6464 /* Cast through typeof to catch incompatible API changes. Since UST
6465 only builds with gcc, we can freely use gcc extensions here
6467 #define GET_UST_SYM(SYM) \
6470 if (ust_ops.SYM == NULL) \
6471 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6472 if (ust_ops.SYM == NULL) \
6476 #define USTF(SYM) ust_ops.SYM
6478 /* Get pointers to all libust.so functions we care about. */
6483 GET_UST_SYM (serialize_to_text
);
6485 GET_UST_SYM (ltt_probe_register
);
6486 GET_UST_SYM (ltt_probe_unregister
);
6487 GET_UST_SYM (ltt_marker_connect
);
6488 GET_UST_SYM (ltt_marker_disconnect
);
6490 GET_UST_SYM (marker_iter_start
);
6491 GET_UST_SYM (marker_iter_next
);
6492 GET_UST_SYM (marker_iter_stop
);
6493 GET_UST_SYM (marker_iter_reset
);
6499 /* Given an UST marker, return the matching gdb static tracepoint.
6500 The match is done by address. */
6502 static struct tracepoint
*
6503 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6505 struct tracepoint
*tpoint
;
6507 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6509 if (tpoint
->type
!= static_tracepoint
)
6512 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6519 /* The probe function we install on lttng/ust markers. Whenever a
6520 probed ust marker is hit, this function is called. This is similar
6521 to gdb_collect, only for static tracepoints, instead of fast
6525 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6526 struct registers
*regs
, void *call_private
,
6527 const char *fmt
, va_list *args
)
6529 struct tracepoint
*tpoint
;
6530 struct static_tracepoint_ctx ctx
;
6532 /* Don't do anything until the trace run is completely set up. */
6535 trace_debug ("gdb_probe: not tracing\n");
6539 ctx
.base
.type
= static_tracepoint
;
6540 ctx
.regcache_initted
= 0;
6545 /* Wrap the regblock in a register cache (in the stack, we don't
6546 want to malloc here). */
6547 ctx
.regspace
= alloca (register_cache_size ());
6548 if (ctx
.regspace
== NULL
)
6550 trace_debug ("Trace buffer block allocation failed, skipping");
6554 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6557 trace_debug ("gdb_probe: marker not known: "
6558 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6559 mdata
->location
, mdata
->channel
,
6560 mdata
->name
, mdata
->format
);
6564 if (!tpoint
->enabled
)
6566 trace_debug ("gdb_probe: tracepoint disabled");
6570 ctx
.tpoint
= tpoint
;
6572 trace_debug ("gdb_probe: collecting marker: "
6573 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6574 mdata
->location
, mdata
->channel
,
6575 mdata
->name
, mdata
->format
);
6577 /* Test the condition if present, and collect if true. */
6578 if (tpoint
->cond
== NULL
6579 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6582 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6583 tpoint
->address
, tpoint
);
6585 if (stopping_tracepoint
6586 || trace_buffer_is_full
6587 || expr_eval_result
!= expr_eval_no_error
)
6592 /* If there was a condition and it evaluated to false, the only
6593 way we would stop tracing is if there was an error during
6594 condition expression evaluation. */
6595 if (expr_eval_result
!= expr_eval_no_error
)
6600 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6601 static tracepoint string data. This is a string passed to the
6602 tracing library by the user, at the time of the tracepoint marker
6603 call. E.g., in the UST marker call:
6605 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6607 the collected data is "str FOOBAZ".
6611 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6612 struct traceframe
*tframe
)
6614 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6615 unsigned char *bufspace
;
6618 unsigned short blocklen
;
6622 trace_debug ("Wanted to collect static trace data, "
6623 "but there's no static trace data");
6627 va_copy (copy
, *umd
->args
);
6628 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6631 trace_debug ("Want to collect ust data");
6633 /* 'S' + size + string */
6634 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6635 1 + sizeof (blocklen
) + size
+ 1);
6636 if (bufspace
== NULL
)
6638 trace_debug ("Trace buffer block allocation failed, skipping");
6642 /* Identify a static trace data block. */
6645 blocklen
= size
+ 1;
6646 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6648 va_copy (copy
, *umd
->args
);
6649 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6650 size
+ 1, umd
->fmt
, copy
);
6653 trace_debug ("Storing static tracepoint data in regblock: %s",
6654 bufspace
+ 1 + sizeof (blocklen
));
6657 /* The probe to register with lttng/ust. */
6658 static struct ltt_available_probe gdb_ust_probe
=
6665 #endif /* HAVE_UST */
6666 #endif /* IN_PROCESS_AGENT */
6668 #ifndef IN_PROCESS_AGENT
6670 /* Ask the in-process agent to run a command. Since we don't want to
6671 have to handle the IPA hitting breakpoints while running the
6672 command, we pause all threads, remove all breakpoints, and then set
6673 the helper thread re-running. We communicate with the helper
6674 thread by means of direct memory xfering, and a socket for
6678 run_inferior_command (char *cmd
, int len
)
6681 int pid
= ptid_get_pid (current_ptid
);
6683 trace_debug ("run_inferior_command: running: %s", cmd
);
6686 uninsert_all_breakpoints ();
6688 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6690 reinsert_all_breakpoints ();
6696 #else /* !IN_PROCESS_AGENT */
6698 #include <sys/socket.h>
6701 #ifndef UNIX_PATH_MAX
6702 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6705 /* Where we put the socked used for synchronization. */
6706 #define SOCK_DIR P_tmpdir
6708 /* Thread ID of the helper thread. GDBserver reads this to know which
6709 is the help thread. This is an LWP id on Linux. */
6710 int helper_thread_id
;
6713 init_named_socket (const char *name
)
6716 struct sockaddr_un addr
;
6718 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6721 warning ("socket creation failed: %s", strerror (errno
));
6725 addr
.sun_family
= AF_UNIX
;
6727 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6728 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6730 result
= access (name
, F_OK
);
6734 result
= unlink (name
);
6737 warning ("unlink failed: %s", strerror (errno
));
6741 warning ("socket %s already exists; overwriting", name
);
6744 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6747 warning ("bind failed: %s", strerror (errno
));
6752 result
= listen (fd
, 1);
6755 warning ("listen: %s", strerror (errno
));
6763 static char agent_socket_name
[UNIX_PATH_MAX
];
6766 gdb_agent_socket_init (void)
6770 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6771 SOCK_DIR
, getpid ());
6772 if (result
>= UNIX_PATH_MAX
)
6774 trace_debug ("string overflow allocating socket name");
6778 fd
= init_named_socket (agent_socket_name
);
6780 warning ("Error initializing named socket (%s) for communication with the "
6781 "ust helper thread. Check that directory exists and that it "
6782 "is writable.", agent_socket_name
);
6789 /* The next marker to be returned on a qTsSTM command. */
6790 static const struct marker
*next_st
;
6792 /* Returns the first known marker. */
6797 struct marker_iter iter
;
6799 USTF(marker_iter_reset
) (&iter
);
6800 USTF(marker_iter_start
) (&iter
);
6805 /* Returns the marker following M. */
6807 const struct marker
*
6808 next_marker (const struct marker
*m
)
6810 struct marker_iter iter
;
6812 USTF(marker_iter_reset
) (&iter
);
6813 USTF(marker_iter_start
) (&iter
);
6815 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6817 if (iter
.marker
== m
)
6819 USTF(marker_iter_next
) (&iter
);
6827 /* Return an hexstr version of the STR C string, fit for sending to
6831 cstr_to_hexstr (const char *str
)
6833 int len
= strlen (str
);
6834 char *hexstr
= xmalloc (len
* 2 + 1);
6835 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6839 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6843 response_ust_marker (char *packet
, const struct marker
*st
)
6845 char *strid
, *format
, *tmp
;
6847 next_st
= next_marker (st
);
6849 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6850 strlen (st
->name
) + 1);
6851 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6853 strid
= cstr_to_hexstr (tmp
);
6856 format
= cstr_to_hexstr (st
->format
);
6858 sprintf (packet
, "m%s:%s:%s",
6859 paddress ((uintptr_t) st
->location
),
6867 /* Return the first static tracepoint, and initialize the state
6868 machine that will iterate through all the static tracepoints. */
6871 cmd_qtfstm (char *packet
)
6873 trace_debug ("Returning first trace state variable definition");
6875 if (first_marker ())
6876 response_ust_marker (packet
, first_marker ());
6878 strcpy (packet
, "l");
6881 /* Return additional trace state variable definitions. */
6884 cmd_qtsstm (char *packet
)
6886 trace_debug ("Returning static tracepoint");
6889 response_ust_marker (packet
, next_st
);
6891 strcpy (packet
, "l");
6894 /* Disconnect the GDB probe from a marker at a given address. */
6897 unprobe_marker_at (char *packet
)
6901 struct marker_iter iter
;
6903 p
+= sizeof ("unprobe_marker_at:") - 1;
6905 p
= unpack_varlen_hex (p
, &address
);
6907 USTF(marker_iter_reset
) (&iter
);
6908 USTF(marker_iter_start
) (&iter
);
6909 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6910 if ((uintptr_t ) iter
.marker
->location
== address
)
6914 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6918 warning ("could not disable marker %s/%s",
6919 iter
.marker
->channel
, iter
.marker
->name
);
6924 /* Connect the GDB probe to a marker at a given address. */
6927 probe_marker_at (char *packet
)
6931 struct marker_iter iter
;
6934 p
+= sizeof ("probe_marker_at:") - 1;
6936 p
= unpack_varlen_hex (p
, &address
);
6938 USTF(marker_iter_reset
) (&iter
);
6940 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6942 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6943 if ((uintptr_t ) m
->location
== address
)
6947 trace_debug ("found marker for address. "
6948 "ltt_marker_connect (marker = %s/%s)",
6949 m
->channel
, m
->name
);
6951 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6953 if (result
&& result
!= -EEXIST
)
6954 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6955 m
->channel
, m
->name
, -result
);
6959 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6960 m
->channel
, m
->name
);
6964 strcpy (packet
, "OK");
6968 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6973 cmd_qtstmat (char *packet
)
6977 struct marker_iter iter
;
6980 p
+= sizeof ("qTSTMat:") - 1;
6982 p
= unpack_varlen_hex (p
, &address
);
6984 USTF(marker_iter_reset
) (&iter
);
6986 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6988 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6989 if ((uintptr_t ) m
->location
== address
)
6991 response_ust_marker (packet
, m
);
6995 strcpy (packet
, "l");
7005 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7008 #endif /* HAVE_UST */
7010 #include <sys/syscall.h>
7014 gdb_agent_remove_socket (void)
7016 unlink (agent_socket_name
);
7019 /* Helper thread of agent. */
7022 gdb_agent_helper_thread (void *arg
)
7026 atexit (gdb_agent_remove_socket
);
7030 listen_fd
= gdb_agent_socket_init ();
7032 if (helper_thread_id
== 0)
7033 helper_thread_id
= syscall (SYS_gettid
);
7035 if (listen_fd
== -1)
7037 warning ("could not create sync socket\n");
7044 struct sockaddr_un sockaddr
;
7050 tmp
= sizeof (sockaddr
);
7054 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7056 /* It seems an ERESTARTSYS can escape out of accept. */
7057 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7061 warning ("Accept returned %d, error: %s\n",
7062 fd
, strerror (errno
));
7068 ret
= read (fd
, buf
, 1);
7069 } while (ret
== -1 && errno
== EINTR
);
7073 warning ("reading socket (fd=%d) failed with %s",
7074 fd
, strerror (errno
));
7081 if (strncmp ("close", cmd_buf
, 5) == 0)
7086 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7088 cmd_qtfstm (cmd_buf
);
7090 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7092 cmd_qtsstm (cmd_buf
);
7094 else if (strncmp ("unprobe_marker_at:",
7096 sizeof ("unprobe_marker_at:") - 1) == 0)
7098 unprobe_marker_at (cmd_buf
);
7100 else if (strncmp ("probe_marker_at:",
7102 sizeof ("probe_marker_at:") - 1) == 0)
7104 probe_marker_at (cmd_buf
);
7106 else if (strncmp ("qTSTMat:",
7108 sizeof ("qTSTMat:") - 1) == 0)
7110 cmd_qtstmat (cmd_buf
);
7112 #endif /* HAVE_UST */
7115 /* Fix compiler's warning: ignoring return value of 'write'. */
7116 ret
= write (fd
, buf
, 1);
7122 unlink (agent_socket_name
);
7124 /* Sleep endlessly to wait the whole inferior stops. This
7125 thread can not exit because GDB or GDBserver may still need
7126 'current_inferior' (representing this thread) to access
7127 inferior memory. Otherwise, this thread exits earlier than
7128 other threads, and 'current_inferior' is set to NULL. */
7139 #include <pthread.h>
7141 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7144 gdb_agent_init (void)
7151 /* We want the helper thread to be as transparent as possible, so
7152 have it inherit an all-signals-blocked mask. */
7154 sigfillset (&new_mask
);
7155 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7157 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7159 res
= pthread_create (&thread
,
7161 gdb_agent_helper_thread
,
7164 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7166 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7168 while (helper_thread_id
== 0)
7176 #include <sys/mman.h>
7179 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7180 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7181 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7182 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7183 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7184 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7186 /* Record the result of getting buffer space for fast tracepoint
7187 trampolines. Any error message is copied, since caller may not be
7188 using persistent storage. */
7191 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7193 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7194 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7196 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7198 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7201 static void __attribute__ ((constructor
))
7202 initialize_tracepoint_ftlib (void)
7204 initialize_tracepoint ();
7209 #endif /* IN_PROCESS_AGENT */
7211 /* Return a timestamp, expressed as microseconds of the usual Unix
7212 time. (As the result is a 64-bit number, it will not overflow any
7216 get_timestamp (void)
7220 if (gettimeofday (&tv
, 0) != 0)
7223 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7227 initialize_tracepoint (void)
7229 /* There currently no way to change the buffer size. */
7230 const int sizeOfBuffer
= 5 * 1024 * 1024;
7231 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7232 init_trace_buffer (buf
, sizeOfBuffer
);
7234 /* Wire trace state variable 1 to be the timestamp. This will be
7235 uploaded to GDB upon connection and become one of its trace state
7236 variables. (In case you're wondering, if GDB already has a trace
7237 variable numbered 1, it will be renumbered.) */
7238 create_trace_state_variable (1, 0);
7239 set_trace_state_variable_name (1, "trace_timestamp");
7240 set_trace_state_variable_getter (1, get_timestamp
);
7242 #ifdef IN_PROCESS_AGENT
7247 pagesize
= sysconf (_SC_PAGE_SIZE
);
7251 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7253 #define SCRATCH_BUFFER_NPAGES 20
7255 /* Allocate scratch buffer aligned on a page boundary, at a low
7256 address (close to the main executable's code). */
7257 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7259 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7260 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7261 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7263 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7269 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7272 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7275 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7277 /* It's not a fatal error for something to go wrong with trampoline
7278 buffer setup, but it can be mysterious, so create a channel to
7279 report back on what went wrong, using a fixed size since we may
7280 not be able to allocate space later when the problem occurs. */
7281 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7283 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7285 initialize_low_tracepoint ();