1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2012 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 static enum eval_result_type
1350 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1351 struct traceframe
*tframe
,
1352 struct agent_expr
*aexpr
,
1355 #ifndef IN_PROCESS_AGENT
1356 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1357 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1360 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1361 struct tracepoint
*tpoint
);
1363 #ifndef IN_PROCESS_AGENT
1364 static void clear_readonly_regions (void);
1365 static void clear_installed_tracepoints (void);
1368 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1370 struct tracepoint
*tpoint
);
1371 #ifndef IN_PROCESS_AGENT
1372 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1374 struct tracepoint
*tpoint
, int current_step
);
1375 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1376 CORE_ADDR
*jump_entry
);
1378 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1380 struct tracepoint
*tpoint
,
1381 struct traceframe
*tframe
,
1382 struct tracepoint_action
*taction
);
1384 #ifndef IN_PROCESS_AGENT
1385 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1387 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1388 static void download_tracepoint (struct tracepoint
*);
1389 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1390 static void clone_fast_tracepoint (struct tracepoint
*to
,
1391 const struct tracepoint
*from
);
1394 static LONGEST
get_timestamp (void);
1396 #if defined(__GNUC__)
1397 # define memory_barrier() asm volatile ("" : : : "memory")
1399 # define memory_barrier() do {} while (0)
1402 /* We only build the IPA if this builtin is supported, and there are
1403 no uses of this in GDBserver itself, so we're safe in defining this
1405 #define cmpxchg(mem, oldval, newval) \
1406 __sync_val_compare_and_swap (mem, oldval, newval)
1408 /* Record that an error occurred during expression evaluation. */
1411 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1412 enum eval_result_type rtype
)
1414 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1415 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1417 #ifdef IN_PROCESS_AGENT
1418 /* Only record the first error we get. */
1419 if (cmpxchg (&expr_eval_result
,
1421 rtype
) != expr_eval_no_error
)
1424 if (expr_eval_result
!= expr_eval_no_error
)
1428 error_tracepoint
= tpoint
;
1431 /* Trace buffer management. */
1434 clear_trace_buffer (void)
1436 trace_buffer_start
= trace_buffer_lo
;
1437 trace_buffer_free
= trace_buffer_lo
;
1438 trace_buffer_end_free
= trace_buffer_hi
;
1439 trace_buffer_wrap
= trace_buffer_hi
;
1440 /* A traceframe with zeroed fields marks the end of trace data. */
1441 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1442 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1443 traceframe_read_count
= traceframe_write_count
= 0;
1444 traceframes_created
= 0;
1447 #ifndef IN_PROCESS_AGENT
1450 clear_inferior_trace_buffer (void)
1452 CORE_ADDR ipa_trace_buffer_lo
;
1453 CORE_ADDR ipa_trace_buffer_hi
;
1454 struct traceframe ipa_traceframe
= { 0 };
1455 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1457 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1458 &ipa_trace_buffer_lo
);
1459 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1460 &ipa_trace_buffer_hi
);
1462 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1463 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1464 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1465 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1467 /* A traceframe with zeroed fields marks the end of trace data. */
1468 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1469 (unsigned char *) &ipa_trace_buffer_ctrl
,
1470 sizeof (ipa_trace_buffer_ctrl
));
1472 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1474 /* A traceframe with zeroed fields marks the end of trace data. */
1475 write_inferior_memory (ipa_trace_buffer_lo
,
1476 (unsigned char *) &ipa_traceframe
,
1477 sizeof (ipa_traceframe
));
1479 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1480 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1481 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1487 init_trace_buffer (unsigned char *buf
, int bufsize
)
1489 trace_buffer_lo
= buf
;
1490 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1492 clear_trace_buffer ();
1495 #ifdef IN_PROCESS_AGENT
1497 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1498 about_to_request_buffer_space (void)
1500 /* GDBserver places breakpoint here while it goes about to flush
1501 data at random times. */
1502 UNKNOWN_SIDE_EFFECTS();
1507 /* Carve out a piece of the trace buffer, returning NULL in case of
1511 trace_buffer_alloc (size_t amt
)
1513 unsigned char *rslt
;
1514 struct trace_buffer_control
*tbctrl
;
1516 #ifdef IN_PROCESS_AGENT
1517 unsigned int prev
, prev_filtered
;
1518 unsigned int commit_count
;
1519 unsigned int commit
;
1520 unsigned int readout
;
1522 struct traceframe
*oldest
;
1523 unsigned char *new_start
;
1526 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1527 (long) amt
, (long) sizeof (struct traceframe
));
1529 /* Account for the EOB marker. */
1530 amt
+= sizeof (struct traceframe
);
1532 #ifdef IN_PROCESS_AGENT
1536 /* Read the current token and extract the index to try to write to,
1537 storing it in CURR. */
1538 prev
= trace_buffer_ctrl_curr
;
1539 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1540 curr
= prev_filtered
+ 1;
1544 about_to_request_buffer_space ();
1546 /* Start out with a copy of the current state. GDBserver may be
1547 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1548 be able to commit anyway if that happens. */
1549 trace_buffer_ctrl
[curr
]
1550 = trace_buffer_ctrl
[prev_filtered
];
1551 trace_debug ("trying curr=%u", curr
);
1553 /* The GDBserver's agent doesn't need all that syncing, and always
1554 updates TCB 0 (there's only one, mind you). */
1557 tbctrl
= &trace_buffer_ctrl
[curr
];
1559 /* Offsets are easier to grok for debugging than raw addresses,
1560 especially for the small trace buffer sizes that are useful for
1562 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1564 (int) (tbctrl
->start
- trace_buffer_lo
),
1565 (int) (tbctrl
->free
- trace_buffer_lo
),
1566 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1567 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1568 (int) (trace_buffer_hi
- trace_buffer_lo
));
1570 /* The algorithm here is to keep trying to get a contiguous block of
1571 the requested size, possibly discarding older traceframes to free
1572 up space. Since free space might come in one or two pieces,
1573 depending on whether discarded traceframes wrapped around at the
1574 high end of the buffer, we test both pieces after each
1578 /* First, if we have two free parts, try the upper one first. */
1579 if (tbctrl
->end_free
< tbctrl
->free
)
1581 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1582 /* We have enough in the upper part. */
1586 /* Our high part of free space wasn't enough. Give up
1587 on it for now, set wraparound. We will recover the
1588 space later, if/when the wrapped-around traceframe is
1590 trace_debug ("Upper part too small, setting wraparound");
1591 tbctrl
->wrap
= tbctrl
->free
;
1592 tbctrl
->free
= trace_buffer_lo
;
1596 /* The normal case. */
1597 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1600 #ifdef IN_PROCESS_AGENT
1601 /* The IP Agent's buffer is always circular. It isn't used
1602 currently, but `circular_trace_buffer' could represent
1603 GDBserver's mode. If we didn't find space, ask GDBserver to
1606 flush_trace_buffer ();
1610 trace_debug ("gdbserver flushed buffer, retrying");
1614 /* GDBserver cancelled the tracing. Bail out as well. */
1617 /* If we're here, then neither part is big enough, and
1618 non-circular trace buffers are now full. */
1619 if (!circular_trace_buffer
)
1621 trace_debug ("Not enough space in the trace buffer");
1625 trace_debug ("Need more space in the trace buffer");
1627 /* If we have a circular buffer, we can try discarding the
1628 oldest traceframe and see if that helps. */
1629 oldest
= FIRST_TRACEFRAME ();
1630 if (oldest
->tpnum
== 0)
1632 /* Not good; we have no traceframes to free. Perhaps we're
1633 asking for a block that is larger than the buffer? In
1634 any case, give up. */
1635 trace_debug ("No traceframes to discard");
1639 /* We don't run this code in the in-process agent currently.
1640 E.g., we could leave the in-process agent in autonomous
1641 circular mode if we only have fast tracepoints. If we do
1642 that, then this bit becomes racy with GDBserver, which also
1643 writes to this counter. */
1644 --traceframe_write_count
;
1646 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1647 /* If we freed the traceframe that wrapped around, go back
1648 to the non-wrap case. */
1649 if (new_start
< tbctrl
->start
)
1651 trace_debug ("Discarding past the wraparound");
1652 tbctrl
->wrap
= trace_buffer_hi
;
1654 tbctrl
->start
= new_start
;
1655 tbctrl
->end_free
= tbctrl
->start
;
1657 trace_debug ("Discarded a traceframe\n"
1658 "Trace buffer [%d], start=%d free=%d "
1659 "endfree=%d wrap=%d hi=%d",
1661 (int) (tbctrl
->start
- trace_buffer_lo
),
1662 (int) (tbctrl
->free
- trace_buffer_lo
),
1663 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1664 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1665 (int) (trace_buffer_hi
- trace_buffer_lo
));
1667 /* Now go back around the loop. The discard might have resulted
1668 in either one or two pieces of free space, so we want to try
1669 both before freeing any more traceframes. */
1673 /* If we get here, we know we can provide the asked-for space. */
1675 rslt
= tbctrl
->free
;
1677 /* Adjust the request back down, now that we know we have space for
1678 the marker, but don't commit to AMT yet, we may still need to
1679 restart the operation if GDBserver touches the trace buffer
1680 (obviously only important in the in-process agent's version). */
1681 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1683 /* Or not. If GDBserver changed the trace buffer behind our back,
1684 we get to restart a new allocation attempt. */
1686 #ifdef IN_PROCESS_AGENT
1687 /* Build the tentative token. */
1688 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1689 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1690 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1694 /* Try to commit it. */
1695 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1696 if (readout
!= prev
)
1698 trace_debug ("GDBserver has touched the trace buffer, restarting."
1699 " (prev=%08x, commit=%08x, readout=%08x)",
1700 prev
, commit
, readout
);
1704 /* Hold your horses here. Even if that change was committed,
1705 GDBserver could come in, and clobber it. We need to hold to be
1706 able to tell if GDBserver clobbers before or after we committed
1707 the change. Whenever GDBserver goes about touching the IPA
1708 buffer, it sets a breakpoint in this routine, so we have a sync
1710 about_to_request_buffer_space ();
1712 /* Check if the change has been effective, even if GDBserver stopped
1713 us at the breakpoint. */
1716 unsigned int refetch
;
1720 refetch
= trace_buffer_ctrl_curr
;
1722 if (refetch
== commit
1723 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1726 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1727 "readout=%08x, refetch=%08x)",
1728 prev
, commit
, readout
, refetch
);
1732 trace_debug ("GDBserver has touched the trace buffer, not effective."
1733 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1734 prev
, commit
, readout
, refetch
);
1740 /* We have a new piece of the trace buffer. Hurray! */
1742 /* Add an EOB marker just past this allocation. */
1743 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1744 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1746 /* Adjust the request back down, now that we know we have space for
1748 amt
-= sizeof (struct traceframe
);
1752 trace_debug ("Allocated %d bytes", (int) amt
);
1753 trace_debug ("Trace buffer [%d] start=%d free=%d "
1754 "endfree=%d wrap=%d hi=%d",
1756 (int) (tbctrl
->start
- trace_buffer_lo
),
1757 (int) (tbctrl
->free
- trace_buffer_lo
),
1758 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1759 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1760 (int) (trace_buffer_hi
- trace_buffer_lo
));
1766 #ifndef IN_PROCESS_AGENT
1768 /* Return the total free space. This is not necessarily the largest
1769 block we can allocate, because of the two-part case. */
1774 if (trace_buffer_free
<= trace_buffer_end_free
)
1775 return trace_buffer_end_free
- trace_buffer_free
;
1777 return ((trace_buffer_end_free
- trace_buffer_lo
)
1778 + (trace_buffer_hi
- trace_buffer_free
));
1781 /* An 'S' in continuation packets indicates remainder are for
1784 static int seen_step_action_flag
;
1786 /* Create a tracepoint (location) with given number and address. Add this
1787 new tracepoint to list and sort this list. */
1789 static struct tracepoint
*
1790 add_tracepoint (int num
, CORE_ADDR addr
)
1792 struct tracepoint
*tpoint
, **tp_next
;
1794 tpoint
= xmalloc (sizeof (struct tracepoint
));
1795 tpoint
->number
= num
;
1796 tpoint
->address
= addr
;
1797 tpoint
->numactions
= 0;
1798 tpoint
->actions
= NULL
;
1799 tpoint
->actions_str
= NULL
;
1800 tpoint
->cond
= NULL
;
1801 tpoint
->num_step_actions
= 0;
1802 tpoint
->step_actions
= NULL
;
1803 tpoint
->step_actions_str
= NULL
;
1804 /* Start all off as regular (slow) tracepoints. */
1805 tpoint
->type
= trap_tracepoint
;
1806 tpoint
->orig_size
= -1;
1807 tpoint
->source_strings
= NULL
;
1808 tpoint
->compiled_cond
= 0;
1809 tpoint
->handle
= NULL
;
1810 tpoint
->next
= NULL
;
1812 /* Find a place to insert this tracepoint into list in order to keep
1813 the tracepoint list still in the ascending order. There may be
1814 multiple tracepoints at the same address as TPOINT's, and this
1815 guarantees TPOINT is inserted after all the tracepoints which are
1816 set at the same address. For example, fast tracepoints A, B, C are
1817 set at the same address, and D is to be insert at the same place as
1820 -->| A |--> | B |-->| C |->...
1822 One jump pad was created for tracepoint A, B, and C, and the target
1823 address of A is referenced/used in jump pad. So jump pad will let
1824 inferior jump to A. If D is inserted in front of A, like this,
1826 -->| D |-->| A |--> | B |-->| C |->...
1828 without updating jump pad, D is not reachable during collect, which
1829 is wrong. As we can see, the order of B, C and D doesn't matter, but
1830 A should always be the `first' one. */
1831 for (tp_next
= &tracepoints
;
1832 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1833 tp_next
= &(*tp_next
)->next
)
1835 tpoint
->next
= *tp_next
;
1837 last_tracepoint
= tpoint
;
1839 seen_step_action_flag
= 0;
1844 #ifndef IN_PROCESS_AGENT
1846 /* Return the tracepoint with the given number and address, or NULL. */
1848 static struct tracepoint
*
1849 find_tracepoint (int id
, CORE_ADDR addr
)
1851 struct tracepoint
*tpoint
;
1853 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1854 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1860 /* Remove TPOINT from global list. */
1863 remove_tracepoint (struct tracepoint
*tpoint
)
1865 struct tracepoint
*tp
, *tp_prev
;
1867 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1868 tp_prev
= tp
, tp
= tp
->next
)
1874 tp_prev
->next
= tp
->next
;
1876 tracepoints
= tp
->next
;
1882 /* There may be several tracepoints with the same number (because they
1883 are "locations", in GDB parlance); return the next one after the
1884 given tracepoint, or search from the beginning of the list if the
1885 first argument is NULL. */
1887 static struct tracepoint
*
1888 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1890 struct tracepoint
*tpoint
;
1893 tpoint
= prev_tp
->next
;
1895 tpoint
= tracepoints
;
1896 for (; tpoint
; tpoint
= tpoint
->next
)
1897 if (tpoint
->number
== num
)
1906 save_string (const char *str
, size_t len
)
1910 s
= xmalloc (len
+ 1);
1911 memcpy (s
, str
, len
);
1917 /* Append another action to perform when the tracepoint triggers. */
1920 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1926 seen_step_action_flag
= 1;
1934 char *act_start
= act
;
1935 struct tracepoint_action
*action
= NULL
;
1941 struct collect_memory_action
*maction
;
1945 maction
= xmalloc (sizeof *maction
);
1946 maction
->base
.type
= *act
;
1947 maction
->base
.ops
= &m_tracepoint_action_ops
;
1948 action
= &maction
->base
;
1951 is_neg
= (*act
== '-');
1954 act
= unpack_varlen_hex (act
, &basereg
);
1956 act
= unpack_varlen_hex (act
, &maction
->addr
);
1958 act
= unpack_varlen_hex (act
, &maction
->len
);
1959 maction
->basereg
= (is_neg
1962 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1963 pulongest (maction
->len
),
1964 paddress (maction
->addr
), maction
->basereg
);
1969 struct collect_registers_action
*raction
;
1971 raction
= xmalloc (sizeof *raction
);
1972 raction
->base
.type
= *act
;
1973 raction
->base
.ops
= &r_tracepoint_action_ops
;
1974 action
= &raction
->base
;
1976 trace_debug ("Want to collect registers");
1978 /* skip past hex digits of mask for now */
1979 while (isxdigit(*act
))
1985 struct collect_static_trace_data_action
*raction
;
1987 raction
= xmalloc (sizeof *raction
);
1988 raction
->base
.type
= *act
;
1989 raction
->base
.ops
= &l_tracepoint_action_ops
;
1990 action
= &raction
->base
;
1992 trace_debug ("Want to collect static trace data");
1997 trace_debug ("Unexpected step action, ignoring");
2002 struct eval_expr_action
*xaction
;
2004 xaction
= xmalloc (sizeof (*xaction
));
2005 xaction
->base
.type
= *act
;
2006 xaction
->base
.ops
= &x_tracepoint_action_ops
;
2007 action
= &xaction
->base
;
2009 trace_debug ("Want to evaluate expression");
2010 xaction
->expr
= gdb_parse_agent_expr (&act
);
2014 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2023 if (seen_step_action_flag
)
2025 tpoint
->num_step_actions
++;
2027 tpoint
->step_actions
2028 = xrealloc (tpoint
->step_actions
,
2029 (sizeof (*tpoint
->step_actions
)
2030 * tpoint
->num_step_actions
));
2031 tpoint
->step_actions_str
2032 = xrealloc (tpoint
->step_actions_str
,
2033 (sizeof (*tpoint
->step_actions_str
)
2034 * tpoint
->num_step_actions
));
2035 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2036 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2037 = save_string (act_start
, act
- act_start
);
2041 tpoint
->numactions
++;
2043 = xrealloc (tpoint
->actions
,
2044 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2046 = xrealloc (tpoint
->actions_str
,
2047 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2048 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2049 tpoint
->actions_str
[tpoint
->numactions
- 1]
2050 = save_string (act_start
, act
- act_start
);
2057 /* Find or create a trace state variable with the given number. */
2059 static struct trace_state_variable
*
2060 get_trace_state_variable (int num
)
2062 struct trace_state_variable
*tsv
;
2064 #ifdef IN_PROCESS_AGENT
2065 /* Search for an existing variable. */
2066 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2067 if (tsv
->number
== num
)
2071 /* Search for an existing variable. */
2072 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2073 if (tsv
->number
== num
)
2079 /* Find or create a trace state variable with the given number. */
2081 static struct trace_state_variable
*
2082 create_trace_state_variable (int num
, int gdb
)
2084 struct trace_state_variable
*tsv
;
2086 tsv
= get_trace_state_variable (num
);
2090 /* Create a new variable. */
2091 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2093 tsv
->initial_value
= 0;
2097 #ifdef IN_PROCESS_AGENT
2100 tsv
->next
= alloced_trace_state_variables
;
2101 alloced_trace_state_variables
= tsv
;
2106 tsv
->next
= trace_state_variables
;
2107 trace_state_variables
= tsv
;
2112 IP_AGENT_EXPORT LONGEST
2113 get_trace_state_variable_value (int num
)
2115 struct trace_state_variable
*tsv
;
2117 tsv
= get_trace_state_variable (num
);
2121 trace_debug ("No trace state variable %d, skipping value get", num
);
2125 /* Call a getter function if we have one. While it's tempting to
2126 set up something to only call the getter once per tracepoint hit,
2127 it could run afoul of thread races. Better to let the getter
2128 handle it directly, if necessary to worry about it. */
2130 tsv
->value
= (tsv
->getter
) ();
2132 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2133 num
, plongest (tsv
->value
));
2138 IP_AGENT_EXPORT
void
2139 set_trace_state_variable_value (int num
, LONGEST val
)
2141 struct trace_state_variable
*tsv
;
2143 tsv
= get_trace_state_variable (num
);
2147 trace_debug ("No trace state variable %d, skipping value set", num
);
2155 agent_get_trace_state_variable_value (int num
)
2157 return get_trace_state_variable_value (num
);
2161 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2163 set_trace_state_variable_value (num
, val
);
2167 set_trace_state_variable_name (int num
, const char *name
)
2169 struct trace_state_variable
*tsv
;
2171 tsv
= get_trace_state_variable (num
);
2175 trace_debug ("No trace state variable %d, skipping name set", num
);
2179 tsv
->name
= (char *) name
;
2183 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2185 struct trace_state_variable
*tsv
;
2187 tsv
= get_trace_state_variable (num
);
2191 trace_debug ("No trace state variable %d, skipping getter set", num
);
2195 tsv
->getter
= getter
;
2198 /* Add a raw traceframe for the given tracepoint. */
2200 static struct traceframe
*
2201 add_traceframe (struct tracepoint
*tpoint
)
2203 struct traceframe
*tframe
;
2205 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2210 tframe
->tpnum
= tpoint
->number
;
2211 tframe
->data_size
= 0;
2216 /* Add a block to the traceframe currently being worked on. */
2218 static unsigned char *
2219 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2221 unsigned char *block
;
2226 block
= trace_buffer_alloc (amt
);
2231 tframe
->data_size
+= amt
;
2236 /* Flag that the current traceframe is finished. */
2239 finish_traceframe (struct traceframe
*tframe
)
2241 ++traceframe_write_count
;
2242 ++traceframes_created
;
2245 #ifndef IN_PROCESS_AGENT
2247 /* Given a traceframe number NUM, find the NUMth traceframe in the
2250 static struct traceframe
*
2251 find_traceframe (int num
)
2253 struct traceframe
*tframe
;
2256 for (tframe
= FIRST_TRACEFRAME ();
2258 tframe
= NEXT_TRACEFRAME (tframe
))
2269 get_traceframe_address (struct traceframe
*tframe
)
2272 struct tracepoint
*tpoint
;
2274 addr
= traceframe_get_pc (tframe
);
2279 /* Fallback strategy, will be incorrect for while-stepping frames
2280 and multi-location tracepoints. */
2281 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2282 return tpoint
->address
;
2285 /* Search for the next traceframe whose address is inside or outside
2288 static struct traceframe
*
2289 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2292 struct traceframe
*tframe
;
2295 *tfnump
= current_traceframe
+ 1;
2296 tframe
= find_traceframe (*tfnump
);
2297 /* The search is not supposed to wrap around. */
2304 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2306 tfaddr
= get_traceframe_address (tframe
);
2308 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2309 : (lo
> tfaddr
|| tfaddr
> hi
))
2318 /* Search for the next traceframe recorded by the given tracepoint.
2319 Note that for multi-location tracepoints, this will find whatever
2320 location appears first. */
2322 static struct traceframe
*
2323 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2325 struct traceframe
*tframe
;
2327 *tfnump
= current_traceframe
+ 1;
2328 tframe
= find_traceframe (*tfnump
);
2329 /* The search is not supposed to wrap around. */
2336 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2338 if (tframe
->tpnum
== num
)
2349 #ifndef IN_PROCESS_AGENT
2351 /* Clear all past trace state. */
2354 cmd_qtinit (char *packet
)
2356 struct trace_state_variable
*tsv
, *prev
, *next
;
2358 /* Make sure we don't try to read from a trace frame. */
2359 current_traceframe
= -1;
2361 trace_debug ("Initializing the trace");
2363 clear_installed_tracepoints ();
2364 clear_readonly_regions ();
2367 last_tracepoint
= NULL
;
2369 /* Clear out any leftover trace state variables. Ones with target
2370 defined getters should be kept however. */
2372 tsv
= trace_state_variables
;
2375 trace_debug ("Looking at var %d", tsv
->number
);
2376 if (tsv
->getter
== NULL
)
2382 trace_state_variables
= next
;
2383 trace_debug ("Deleting var %d", tsv
->number
);
2394 clear_trace_buffer ();
2395 clear_inferior_trace_buffer ();
2400 /* Unprobe the UST marker at ADDRESS. */
2403 unprobe_marker_at (CORE_ADDR address
)
2405 char cmd
[IPA_CMD_BUF_SIZE
];
2407 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2408 run_inferior_command (cmd
, strlen (cmd
) + 1);
2411 /* Restore the program to its pre-tracing state. This routine may be called
2412 in error situations, so it needs to be careful about only restoring
2413 from known-valid bits. */
2416 clear_installed_tracepoints (void)
2418 struct tracepoint
*tpoint
;
2419 struct tracepoint
*prev_stpoint
;
2422 cancel_breakpoints ();
2424 prev_stpoint
= NULL
;
2426 /* Restore any bytes overwritten by tracepoints. */
2427 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2429 /* Catch the case where we might try to remove a tracepoint that
2430 was never actually installed. */
2431 if (tpoint
->handle
== NULL
)
2433 trace_debug ("Tracepoint %d at 0x%s was "
2434 "never installed, nothing to clear",
2435 tpoint
->number
, paddress (tpoint
->address
));
2439 switch (tpoint
->type
)
2441 case trap_tracepoint
:
2442 delete_breakpoint (tpoint
->handle
);
2444 case fast_tracepoint
:
2445 delete_fast_tracepoint_jump (tpoint
->handle
);
2447 case static_tracepoint
:
2448 if (prev_stpoint
!= NULL
2449 && prev_stpoint
->address
== tpoint
->address
)
2450 /* Nothing to do. We already unprobed a tracepoint set at
2451 this marker address (and there can only be one probe
2456 unprobe_marker_at (tpoint
->address
);
2457 prev_stpoint
= tpoint
;
2462 tpoint
->handle
= NULL
;
2468 /* Parse a packet that defines a tracepoint. */
2471 cmd_qtdp (char *own_buf
)
2474 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2475 int trail_hyphen
= 0;
2479 struct tracepoint
*tpoint
;
2481 char *packet
= own_buf
;
2483 packet
+= strlen ("QTDP:");
2485 /* A hyphen at the beginning marks a packet specifying actions for a
2486 tracepoint already supplied. */
2493 packet
= unpack_varlen_hex (packet
, &num
);
2494 ++packet
; /* skip a colon */
2495 packet
= unpack_varlen_hex (packet
, &addr
);
2496 ++packet
; /* skip a colon */
2498 /* See if we already have this tracepoint. */
2499 tpoint
= find_tracepoint (num
, addr
);
2503 /* Duplicate tracepoints are never allowed. */
2506 trace_debug ("Tracepoint error: tracepoint %d"
2507 " at 0x%s already exists",
2508 (int) num
, paddress (addr
));
2509 write_enn (own_buf
);
2513 tpoint
= add_tracepoint (num
, addr
);
2515 tpoint
->enabled
= (*packet
== 'E');
2516 ++packet
; /* skip 'E' */
2517 ++packet
; /* skip a colon */
2518 packet
= unpack_varlen_hex (packet
, &count
);
2519 tpoint
->step_count
= count
;
2520 ++packet
; /* skip a colon */
2521 packet
= unpack_varlen_hex (packet
, &count
);
2522 tpoint
->pass_count
= count
;
2523 /* See if we have any of the additional optional fields. */
2524 while (*packet
== ':')
2529 tpoint
->type
= fast_tracepoint
;
2531 packet
= unpack_varlen_hex (packet
, &count
);
2532 tpoint
->orig_size
= count
;
2534 else if (*packet
== 'S')
2536 tpoint
->type
= static_tracepoint
;
2539 else if (*packet
== 'X')
2541 actparm
= (char *) packet
;
2542 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2545 else if (*packet
== '-')
2547 else if (*packet
== '\0')
2550 trace_debug ("Unknown optional tracepoint field");
2555 trace_debug ("Also has actions\n");
2558 trace_debug ("Defined %stracepoint %d at 0x%s, "
2559 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2560 tpoint
->type
== fast_tracepoint
? "fast "
2561 : tpoint
->type
== static_tracepoint
? "static " : "",
2562 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2563 tpoint
->step_count
, tpoint
->pass_count
);
2566 add_tracepoint_action (tpoint
, packet
);
2569 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2570 (int) num
, paddress (addr
));
2571 write_enn (own_buf
);
2575 /* Install tracepoint during tracing only once for each tracepoint location.
2576 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2577 determine the last QTDP packet for one tracepoint location by checking
2578 trailing hyphen in QTDP packet. */
2579 if (tracing
&& !trail_hyphen
)
2581 struct tracepoint
*tp
= NULL
;
2583 /* Pause all threads temporarily while we patch tracepoints. */
2586 /* download_tracepoint will update global `tracepoints'
2587 list, so it is unsafe to leave threads in jump pad. */
2588 stabilize_threads ();
2590 /* Freeze threads. */
2594 if (tpoint
->type
!= trap_tracepoint
)
2596 /* Find another fast or static tracepoint at the same address. */
2597 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2599 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2600 && tp
->number
!= tpoint
->number
)
2604 /* TPOINT is installed at the same address as TP. */
2607 if (tpoint
->type
== fast_tracepoint
)
2608 clone_fast_tracepoint (tpoint
, tp
);
2609 else if (tpoint
->type
== static_tracepoint
)
2610 tpoint
->handle
= (void *) -1;
2614 if (use_agent
&& tpoint
->type
== fast_tracepoint
2615 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2617 /* Download and install fast tracepoint by agent. */
2618 if (tracepoint_send_agent (tpoint
) == 0)
2622 write_enn (own_buf
);
2623 remove_tracepoint (tpoint
);
2628 download_tracepoint (tpoint
);
2630 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2632 install_tracepoint (tpoint
, own_buf
);
2633 if (strcmp (own_buf
, "OK") != 0)
2634 remove_tracepoint (tpoint
);
2648 cmd_qtdpsrc (char *own_buf
)
2650 ULONGEST num
, addr
, start
, slen
;
2651 struct tracepoint
*tpoint
;
2652 char *packet
= own_buf
;
2653 char *saved
, *srctype
, *src
;
2655 struct source_string
*last
, *newlast
;
2657 packet
+= strlen ("QTDPsrc:");
2659 packet
= unpack_varlen_hex (packet
, &num
);
2660 ++packet
; /* skip a colon */
2661 packet
= unpack_varlen_hex (packet
, &addr
);
2662 ++packet
; /* skip a colon */
2664 /* See if we already have this tracepoint. */
2665 tpoint
= find_tracepoint (num
, addr
);
2669 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2670 (int) num
, paddress (addr
));
2671 write_enn (own_buf
);
2676 packet
= strchr (packet
, ':');
2677 srctype
= xmalloc (packet
- saved
+ 1);
2678 memcpy (srctype
, saved
, packet
- saved
);
2679 srctype
[packet
- saved
] = '\0';
2681 packet
= unpack_varlen_hex (packet
, &start
);
2682 ++packet
; /* skip a colon */
2683 packet
= unpack_varlen_hex (packet
, &slen
);
2684 ++packet
; /* skip a colon */
2685 src
= xmalloc (slen
+ 1);
2686 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2689 newlast
= xmalloc (sizeof (struct source_string
));
2690 newlast
->type
= srctype
;
2692 newlast
->next
= NULL
;
2693 /* Always add a source string to the end of the list;
2694 this keeps sequences of actions/commands in the right
2696 if (tpoint
->source_strings
)
2698 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2700 last
->next
= newlast
;
2703 tpoint
->source_strings
= newlast
;
2709 cmd_qtdv (char *own_buf
)
2711 ULONGEST num
, val
, builtin
;
2714 struct trace_state_variable
*tsv
;
2715 char *packet
= own_buf
;
2717 packet
+= strlen ("QTDV:");
2719 packet
= unpack_varlen_hex (packet
, &num
);
2720 ++packet
; /* skip a colon */
2721 packet
= unpack_varlen_hex (packet
, &val
);
2722 ++packet
; /* skip a colon */
2723 packet
= unpack_varlen_hex (packet
, &builtin
);
2724 ++packet
; /* skip a colon */
2726 nbytes
= strlen (packet
) / 2;
2727 varname
= xmalloc (nbytes
+ 1);
2728 nbytes
= unhexify (varname
, packet
, nbytes
);
2729 varname
[nbytes
] = '\0';
2731 tsv
= create_trace_state_variable (num
, 1);
2732 tsv
->initial_value
= (LONGEST
) val
;
2733 tsv
->name
= varname
;
2735 set_trace_state_variable_value (num
, (LONGEST
) val
);
2741 cmd_qtenable_disable (char *own_buf
, int enable
)
2743 char *packet
= own_buf
;
2745 struct tracepoint
*tp
;
2747 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2748 packet
= unpack_varlen_hex (packet
, &num
);
2749 ++packet
; /* skip a colon */
2750 packet
= unpack_varlen_hex (packet
, &addr
);
2752 tp
= find_tracepoint (num
, addr
);
2756 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2758 trace_debug ("Tracepoint %d at 0x%s is already %s",
2759 (int) num
, paddress (addr
),
2760 enable
? "enabled" : "disabled");
2765 trace_debug ("%s tracepoint %d at 0x%s",
2766 enable
? "Enabling" : "Disabling",
2767 (int) num
, paddress (addr
));
2769 tp
->enabled
= enable
;
2771 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2774 int offset
= offsetof (struct tracepoint
, enabled
);
2775 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2777 ret
= prepare_to_access_memory ();
2780 trace_debug ("Failed to temporarily stop inferior threads");
2781 write_enn (own_buf
);
2785 ret
= write_inferior_integer (obj_addr
, enable
);
2786 done_accessing_memory ();
2790 trace_debug ("Cannot write enabled flag into "
2791 "inferior process memory");
2792 write_enn (own_buf
);
2801 trace_debug ("Tracepoint %d at 0x%s not found",
2802 (int) num
, paddress (addr
));
2803 write_enn (own_buf
);
2808 cmd_qtv (char *own_buf
)
2813 char *packet
= own_buf
;
2815 packet
+= strlen ("qTV:");
2816 unpack_varlen_hex (packet
, &num
);
2818 if (current_traceframe
>= 0)
2820 err
= traceframe_read_tsv ((int) num
, &val
);
2823 strcpy (own_buf
, "U");
2827 /* Only make tsv's be undefined before the first trace run. After a
2828 trace run is over, the user might want to see the last value of
2829 the tsv, and it might not be available in a traceframe. */
2830 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2832 strcpy (own_buf
, "U");
2836 val
= get_trace_state_variable_value (num
);
2838 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2841 /* Clear out the list of readonly regions. */
2844 clear_readonly_regions (void)
2846 struct readonly_region
*roreg
;
2848 while (readonly_regions
)
2850 roreg
= readonly_regions
;
2851 readonly_regions
= readonly_regions
->next
;
2856 /* Parse the collection of address ranges whose contents GDB believes
2857 to be unchanging and so can be read directly from target memory
2858 even while looking at a traceframe. */
2861 cmd_qtro (char *own_buf
)
2863 ULONGEST start
, end
;
2864 struct readonly_region
*roreg
;
2865 char *packet
= own_buf
;
2867 trace_debug ("Want to mark readonly regions");
2869 clear_readonly_regions ();
2871 packet
+= strlen ("QTro");
2873 while (*packet
== ':')
2875 ++packet
; /* skip a colon */
2876 packet
= unpack_varlen_hex (packet
, &start
);
2877 ++packet
; /* skip a comma */
2878 packet
= unpack_varlen_hex (packet
, &end
);
2879 roreg
= xmalloc (sizeof (struct readonly_region
));
2880 roreg
->start
= start
;
2882 roreg
->next
= readonly_regions
;
2883 readonly_regions
= roreg
;
2884 trace_debug ("Added readonly region from 0x%s to 0x%s",
2885 paddress (roreg
->start
), paddress (roreg
->end
));
2891 /* Test to see if the given range is in our list of readonly ranges.
2892 We only test for being entirely within a range, GDB is not going to
2893 send a single memory packet that spans multiple regions. */
2896 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2898 struct readonly_region
*roreg
;
2900 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2901 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2907 /* The maximum size of a jump pad entry. */
2908 static const int max_jump_pad_size
= 0x100;
2910 static CORE_ADDR gdb_jump_pad_head
;
2912 /* Return the address of the next free jump space. */
2915 get_jump_space_head (void)
2917 if (gdb_jump_pad_head
== 0)
2919 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2920 &gdb_jump_pad_head
))
2921 fatal ("error extracting jump_pad_buffer");
2924 return gdb_jump_pad_head
;
2927 /* Reserve USED bytes from the jump space. */
2930 claim_jump_space (ULONGEST used
)
2932 trace_debug ("claim_jump_space reserves %s bytes at %s",
2933 pulongest (used
), paddress (gdb_jump_pad_head
));
2934 gdb_jump_pad_head
+= used
;
2937 static CORE_ADDR trampoline_buffer_head
= 0;
2938 static CORE_ADDR trampoline_buffer_tail
;
2940 /* Reserve USED bytes from the trampoline buffer and return the
2941 address of the start of the reserved space in TRAMPOLINE. Returns
2942 non-zero if the space is successfully claimed. */
2945 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2947 if (!trampoline_buffer_head
)
2949 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2950 &trampoline_buffer_tail
))
2952 fatal ("error extracting trampoline_buffer");
2956 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2957 &trampoline_buffer_head
))
2959 fatal ("error extracting trampoline_buffer_end");
2964 /* Start claiming space from the top of the trampoline space. If
2965 the space is located at the bottom of the virtual address space,
2966 this reduces the possibility that corruption will occur if a null
2967 pointer is used to write to memory. */
2968 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2970 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2975 trampoline_buffer_head
-= used
;
2977 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2978 pulongest (used
), paddress (trampoline_buffer_head
));
2980 *trampoline
= trampoline_buffer_head
;
2984 /* Returns non-zero if there is space allocated for use in trampolines
2985 for fast tracepoints. */
2988 have_fast_tracepoint_trampoline_buffer (char *buf
)
2990 CORE_ADDR trampoline_end
, errbuf
;
2992 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2995 fatal ("error extracting trampoline_buffer_end");
3002 strcpy (buf
, "was claiming");
3003 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3006 fatal ("error extracting errbuf");
3010 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3013 return trampoline_end
!= 0;
3016 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3017 the command fails, or 0 otherwise. If the command ran
3018 successfully, but probing the marker failed, ERROUT will be filled
3019 with the error to reply to GDB, and -1 is also returned. This
3020 allows directly passing IPA errors to GDB. */
3023 probe_marker_at (CORE_ADDR address
, char *errout
)
3025 char cmd
[IPA_CMD_BUF_SIZE
];
3028 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3029 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3035 strcpy (errout
, cmd
);
3044 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3046 to
->jump_pad
= from
->jump_pad
;
3047 to
->jump_pad_end
= from
->jump_pad_end
;
3048 to
->trampoline
= from
->trampoline
;
3049 to
->trampoline_end
= from
->trampoline_end
;
3050 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3051 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3052 to
->handle
= from
->handle
;
3054 gdb_assert (from
->handle
);
3055 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3058 #define MAX_JUMP_SIZE 20
3060 /* Install fast tracepoint. Return 0 if successful, otherwise return
3064 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3066 CORE_ADDR jentry
, jump_entry
;
3067 CORE_ADDR trampoline
;
3068 ULONGEST trampoline_size
;
3070 /* The jump to the jump pad of the last fast tracepoint
3072 unsigned char fjump
[MAX_JUMP_SIZE
];
3073 ULONGEST fjump_size
;
3075 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3077 trace_debug ("Requested a fast tracepoint on an instruction "
3078 "that is of less than the minimum length.");
3082 jentry
= jump_entry
= get_jump_space_head ();
3085 trampoline_size
= 0;
3087 /* Install the jump pad. */
3088 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3090 ipa_sym_addrs
.addr_gdb_collect
,
3091 ipa_sym_addrs
.addr_collecting
,
3094 &trampoline
, &trampoline_size
,
3096 &tpoint
->adjusted_insn_addr
,
3097 &tpoint
->adjusted_insn_addr_end
,
3104 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3107 if (tpoint
->handle
!= NULL
)
3109 tpoint
->jump_pad
= jump_entry
;
3110 tpoint
->jump_pad_end
= jentry
;
3111 tpoint
->trampoline
= trampoline
;
3112 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3114 /* Pad to 8-byte alignment. */
3115 jentry
= ((jentry
+ 7) & ~0x7);
3116 claim_jump_space (jentry
- jump_entry
);
3123 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3126 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3128 tpoint
->handle
= NULL
;
3131 if (tpoint
->type
== trap_tracepoint
)
3133 /* Tracepoints are installed as memory breakpoints. Just go
3134 ahead and install the trap. The breakpoints module
3135 handles duplicated breakpoints, and the memory read
3136 routine handles un-patching traps from memory reads. */
3137 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3138 tracepoint_handler
);
3140 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3142 if (!agent_loaded_p ())
3144 trace_debug ("Requested a %s tracepoint, but fast "
3145 "tracepoints aren't supported.",
3146 tpoint
->type
== static_tracepoint
? "static" : "fast");
3147 write_e_ipa_not_loaded (own_buf
);
3150 if (tpoint
->type
== static_tracepoint
3151 && !in_process_agent_supports_ust ())
3153 trace_debug ("Requested a static tracepoint, but static "
3154 "tracepoints are not supported.");
3155 write_e_ust_not_loaded (own_buf
);
3159 if (tpoint
->type
== fast_tracepoint
)
3160 install_fast_tracepoint (tpoint
, own_buf
);
3163 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3164 tpoint
->handle
= (void *) -1;
3169 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3171 if (tpoint
->handle
== NULL
)
3173 if (*own_buf
== '\0')
3174 write_enn (own_buf
);
3180 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3183 cmd_qtstart (char *packet
)
3185 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3186 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3188 trace_debug ("Starting the trace");
3190 /* Pause all threads temporarily while we patch tracepoints. */
3193 /* Get threads out of jump pads. Safe to do here, since this is a
3194 top level command. And, required to do here, since we're
3195 deleting/rewriting jump pads. */
3197 stabilize_threads ();
3199 /* Freeze threads. */
3202 /* Sync the fast tracepoints list in the inferior ftlib. */
3203 if (agent_loaded_p ())
3204 download_trace_state_variables ();
3206 /* No previous fast tpoint yet. */
3207 prev_ftpoint
= NULL
;
3209 /* No previous static tpoint yet. */
3210 prev_stpoint
= NULL
;
3214 /* Start out empty. */
3215 if (agent_loaded_p ())
3216 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3218 /* Download and install tracepoints. */
3219 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3221 /* Ensure all the hit counts start at zero. */
3222 tpoint
->hit_count
= 0;
3223 tpoint
->traceframe_usage
= 0;
3225 if (tpoint
->type
== trap_tracepoint
)
3227 /* Tracepoints are installed as memory breakpoints. Just go
3228 ahead and install the trap. The breakpoints module
3229 handles duplicated breakpoints, and the memory read
3230 routine handles un-patching traps from memory reads. */
3231 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3232 tracepoint_handler
);
3234 else if (tpoint
->type
== fast_tracepoint
3235 || tpoint
->type
== static_tracepoint
)
3237 if (maybe_write_ipa_not_loaded (packet
))
3239 trace_debug ("Requested a %s tracepoint, but fast "
3240 "tracepoints aren't supported.",
3241 tpoint
->type
== static_tracepoint
3242 ? "static" : "fast");
3246 if (tpoint
->type
== fast_tracepoint
)
3249 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3251 if (prev_ftpoint
!= NULL
3252 && prev_ftpoint
->address
== tpoint
->address
)
3255 tracepoint_send_agent (tpoint
);
3257 download_tracepoint_1 (tpoint
);
3259 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3263 /* Tracepoint is installed successfully? */
3266 /* Download and install fast tracepoint by agent. */
3268 installed
= !tracepoint_send_agent (tpoint
);
3271 download_tracepoint_1 (tpoint
);
3272 installed
= !install_fast_tracepoint (tpoint
, packet
);
3276 prev_ftpoint
= tpoint
;
3281 if (!in_process_agent_supports_ust ())
3283 trace_debug ("Requested a static tracepoint, but static "
3284 "tracepoints are not supported.");
3288 download_tracepoint_1 (tpoint
);
3289 /* Can only probe a given marker once. */
3290 if (prev_stpoint
!= NULL
3291 && prev_stpoint
->address
== tpoint
->address
)
3292 tpoint
->handle
= (void *) -1;
3295 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3297 tpoint
->handle
= (void *) -1;
3299 /* So that we can handle multiple static tracepoints
3300 at the same address easily. */
3301 prev_stpoint
= tpoint
;
3307 tpptr
= tpoint
->obj_addr_on_target
;
3309 if (tpoint
== tracepoints
)
3310 /* First object in list, set the head pointer in the
3312 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3314 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3319 /* Any failure in the inner loop is sufficient cause to give
3321 if (tpoint
->handle
== NULL
)
3325 /* Any error in tracepoint insertion is unacceptable; better to
3326 address the problem now, than end up with a useless or misleading
3330 clear_installed_tracepoints ();
3331 if (*packet
== '\0')
3337 stopping_tracepoint
= NULL
;
3338 trace_buffer_is_full
= 0;
3339 expr_eval_result
= expr_eval_no_error
;
3340 error_tracepoint
= NULL
;
3341 tracing_start_time
= get_timestamp ();
3343 /* Tracing is now active, hits will now start being logged. */
3346 if (agent_loaded_p ())
3348 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3349 fatal ("Error setting tracing variable in lib");
3351 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3353 fatal ("Error clearing stopping_tracepoint variable in lib");
3355 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3356 fatal ("Error clearing trace_buffer_is_full variable in lib");
3358 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3359 stop_tracing_handler
);
3360 if (stop_tracing_bkpt
== NULL
)
3361 error ("Error setting stop_tracing breakpoint");
3363 flush_trace_buffer_bkpt
3364 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3365 flush_trace_buffer_handler
);
3366 if (flush_trace_buffer_bkpt
== NULL
)
3367 error ("Error setting flush_trace_buffer breakpoint");
3375 /* End a tracing run, filling in a stop reason to report back to GDB,
3376 and removing the tracepoints from the code. */
3383 trace_debug ("Tracing is already off, ignoring");
3387 trace_debug ("Stopping the trace");
3389 /* Pause all threads before removing fast jumps from memory,
3390 breakpoints, and touching IPA state variables (inferior memory).
3391 Some thread may hit the internal tracing breakpoints, or be
3392 collecting this moment, but that's ok, we don't release the
3393 tpoint object's memory or the jump pads here (we only do that
3394 when we're sure we can move all threads out of the jump pads).
3395 We can't now, since we may be getting here due to the inferior
3396 agent calling us. */
3398 /* Since we're removing breakpoints, cancel breakpoint hits,
3399 possibly related to the breakpoints we're about to delete. */
3400 cancel_breakpoints ();
3402 /* Stop logging. Tracepoints can still be hit, but they will not be
3405 if (agent_loaded_p ())
3407 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3408 fatal ("Error clearing tracing variable in lib");
3411 tracing_stop_time
= get_timestamp ();
3412 tracing_stop_reason
= "t???";
3413 tracing_stop_tpnum
= 0;
3414 if (stopping_tracepoint
)
3416 trace_debug ("Stopping the trace because "
3417 "tracepoint %d was hit %" PRIu64
" times",
3418 stopping_tracepoint
->number
,
3419 stopping_tracepoint
->pass_count
);
3420 tracing_stop_reason
= "tpasscount";
3421 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3423 else if (trace_buffer_is_full
)
3425 trace_debug ("Stopping the trace because the trace buffer is full");
3426 tracing_stop_reason
= "tfull";
3428 else if (expr_eval_result
!= expr_eval_no_error
)
3430 trace_debug ("Stopping the trace because of an expression eval error");
3431 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3432 tracing_stop_tpnum
= error_tracepoint
->number
;
3434 #ifndef IN_PROCESS_AGENT
3435 else if (!gdb_connected ())
3437 trace_debug ("Stopping the trace because GDB disconnected");
3438 tracing_stop_reason
= "tdisconnected";
3443 trace_debug ("Stopping the trace because of a tstop command");
3444 tracing_stop_reason
= "tstop";
3447 stopping_tracepoint
= NULL
;
3448 error_tracepoint
= NULL
;
3450 /* Clear out the tracepoints. */
3451 clear_installed_tracepoints ();
3453 if (agent_loaded_p ())
3455 /* Pull in fast tracepoint trace frames from the inferior lib
3456 buffer into our buffer, even if our buffer is already full,
3457 because we want to present the full number of created frames
3458 in addition to what fit in the trace buffer. */
3459 upload_fast_traceframes ();
3462 if (stop_tracing_bkpt
!= NULL
)
3464 delete_breakpoint (stop_tracing_bkpt
);
3465 stop_tracing_bkpt
= NULL
;
3468 if (flush_trace_buffer_bkpt
!= NULL
)
3470 delete_breakpoint (flush_trace_buffer_bkpt
);
3471 flush_trace_buffer_bkpt
= NULL
;
3478 stop_tracing_handler (CORE_ADDR addr
)
3480 trace_debug ("lib hit stop_tracing");
3482 /* Don't actually handle it here. When we stop tracing we remove
3483 breakpoints from the inferior, and that is not allowed in a
3484 breakpoint handler (as the caller is walking the breakpoint
3490 flush_trace_buffer_handler (CORE_ADDR addr
)
3492 trace_debug ("lib hit flush_trace_buffer");
3497 cmd_qtstop (char *packet
)
3504 cmd_qtdisconnected (char *own_buf
)
3507 char *packet
= own_buf
;
3509 packet
+= strlen ("QTDisconnected:");
3511 unpack_varlen_hex (packet
, &setting
);
3515 disconnected_tracing
= setting
;
3519 cmd_qtframe (char *own_buf
)
3521 ULONGEST frame
, pc
, lo
, hi
, num
;
3523 struct traceframe
*tframe
;
3524 char *packet
= own_buf
;
3526 packet
+= strlen ("QTFrame:");
3528 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3530 packet
+= strlen ("pc:");
3531 unpack_varlen_hex (packet
, &pc
);
3532 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3533 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3535 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3537 packet
+= strlen ("range:");
3538 packet
= unpack_varlen_hex (packet
, &lo
);
3540 unpack_varlen_hex (packet
, &hi
);
3541 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3542 paddress (lo
), paddress (hi
));
3543 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3545 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3547 packet
+= strlen ("outside:");
3548 packet
= unpack_varlen_hex (packet
, &lo
);
3550 unpack_varlen_hex (packet
, &hi
);
3551 trace_debug ("Want to find next traceframe "
3552 "outside the range 0x%s to 0x%s",
3553 paddress (lo
), paddress (hi
));
3554 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3556 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3558 packet
+= strlen ("tdp:");
3559 unpack_varlen_hex (packet
, &num
);
3561 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3562 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3566 unpack_varlen_hex (packet
, &frame
);
3567 tfnum
= (int) frame
;
3570 trace_debug ("Want to stop looking at traceframes");
3571 current_traceframe
= -1;
3575 trace_debug ("Want to look at traceframe %d", tfnum
);
3576 tframe
= find_traceframe (tfnum
);
3581 current_traceframe
= tfnum
;
3582 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3585 sprintf (own_buf
, "F-1");
3589 cmd_qtstatus (char *packet
)
3591 char *stop_reason_rsp
= NULL
;
3592 char *buf1
, *buf2
, *buf3
, *str
;
3595 /* Translate the plain text of the notes back into hex for
3598 str
= (tracing_user_name
? tracing_user_name
: "");
3599 slen
= strlen (str
);
3600 buf1
= (char *) alloca (slen
* 2 + 1);
3601 hexify (buf1
, str
, slen
);
3603 str
= (tracing_notes
? tracing_notes
: "");
3604 slen
= strlen (str
);
3605 buf2
= (char *) alloca (slen
* 2 + 1);
3606 hexify (buf2
, str
, slen
);
3608 str
= (tracing_stop_note
? tracing_stop_note
: "");
3609 slen
= strlen (str
);
3610 buf3
= (char *) alloca (slen
* 2 + 1);
3611 hexify (buf3
, str
, slen
);
3613 trace_debug ("Returning trace status as %d, stop reason %s",
3614 tracing
, tracing_stop_reason
);
3616 if (agent_loaded_p ())
3620 upload_fast_traceframes ();
3625 stop_reason_rsp
= (char *) tracing_stop_reason
;
3627 /* The user visible error string in terror needs to be hex encoded.
3628 We leave it as plain string in `tracing_stop_reason' to ease
3630 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3632 const char *result_name
;
3636 result_name
= stop_reason_rsp
+ strlen ("terror:");
3637 hexstr_len
= strlen (result_name
) * 2;
3638 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3639 strcpy (p
, "terror:");
3641 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3644 /* If this was a forced stop, include any stop note that was supplied. */
3645 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3647 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3648 strcpy (stop_reason_rsp
, "tstop:");
3649 strcat (stop_reason_rsp
, buf3
);
3655 "tframes:%x;tcreated:%x;"
3656 "tfree:%x;tsize:%s;"
3659 "starttime:%s;stoptime:%s;"
3660 "username:%s:;notes:%s:",
3662 stop_reason_rsp
, tracing_stop_tpnum
,
3663 traceframe_count
, traceframes_created
,
3664 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3665 circular_trace_buffer
,
3666 disconnected_tracing
,
3667 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3672 cmd_qtp (char *own_buf
)
3675 struct tracepoint
*tpoint
;
3676 char *packet
= own_buf
;
3678 packet
+= strlen ("qTP:");
3680 packet
= unpack_varlen_hex (packet
, &num
);
3681 ++packet
; /* skip a colon */
3682 packet
= unpack_varlen_hex (packet
, &addr
);
3684 /* See if we already have this tracepoint. */
3685 tpoint
= find_tracepoint (num
, addr
);
3689 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3690 (int) num
, paddress (addr
));
3691 write_enn (own_buf
);
3695 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3696 tpoint
->traceframe_usage
);
3699 /* State variables to help return all the tracepoint bits. */
3700 static struct tracepoint
*cur_tpoint
;
3701 static int cur_action
;
3702 static int cur_step_action
;
3703 static struct source_string
*cur_source_string
;
3704 static struct trace_state_variable
*cur_tsv
;
3706 /* Compose a response that is an imitation of the syntax by which the
3707 tracepoint was originally downloaded. */
3710 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3714 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3715 paddress (tpoint
->address
),
3716 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3717 tpoint
->pass_count
);
3718 if (tpoint
->type
== fast_tracepoint
)
3719 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3720 else if (tpoint
->type
== static_tracepoint
)
3721 sprintf (packet
+ strlen (packet
), ":S");
3725 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3726 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3727 tpoint
->cond
->length
, buf
);
3732 /* Compose a response that is an imitation of the syntax by which the
3733 tracepoint action was originally downloaded (with the difference
3734 that due to the way we store the actions, this will output a packet
3735 per action, while GDB could have combined more than one action
3739 response_action (char *packet
, struct tracepoint
*tpoint
,
3740 char *taction
, int step
)
3742 sprintf (packet
, "%c%x:%s:%s",
3743 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3747 /* Compose a response that is an imitation of the syntax by which the
3748 tracepoint source piece was originally downloaded. */
3751 response_source (char *packet
,
3752 struct tracepoint
*tpoint
, struct source_string
*src
)
3757 len
= strlen (src
->str
);
3758 buf
= alloca (len
* 2 + 1);
3759 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3761 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3762 tpoint
->number
, paddress (tpoint
->address
),
3763 src
->type
, 0, len
, buf
);
3766 /* Return the first piece of tracepoint definition, and initialize the
3767 state machine that will iterate through all the tracepoint
3771 cmd_qtfp (char *packet
)
3773 trace_debug ("Returning first tracepoint definition piece");
3775 cur_tpoint
= tracepoints
;
3776 cur_action
= cur_step_action
= -1;
3777 cur_source_string
= NULL
;
3780 response_tracepoint (packet
, cur_tpoint
);
3782 strcpy (packet
, "l");
3785 /* Return additional pieces of tracepoint definition. Each action and
3786 stepping action must go into its own packet, because of packet size
3787 limits, and so we use state variables to deliver one piece at a
3791 cmd_qtsp (char *packet
)
3793 trace_debug ("Returning subsequent tracepoint definition piece");
3797 /* This case would normally never occur, but be prepared for
3799 strcpy (packet
, "l");
3801 else if (cur_action
< cur_tpoint
->numactions
- 1)
3804 response_action (packet
, cur_tpoint
,
3805 cur_tpoint
->actions_str
[cur_action
], 0);
3807 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3810 response_action (packet
, cur_tpoint
,
3811 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3813 else if ((cur_source_string
3814 ? cur_source_string
->next
3815 : cur_tpoint
->source_strings
))
3817 if (cur_source_string
)
3818 cur_source_string
= cur_source_string
->next
;
3820 cur_source_string
= cur_tpoint
->source_strings
;
3821 response_source (packet
, cur_tpoint
, cur_source_string
);
3825 cur_tpoint
= cur_tpoint
->next
;
3826 cur_action
= cur_step_action
= -1;
3827 cur_source_string
= NULL
;
3829 response_tracepoint (packet
, cur_tpoint
);
3831 strcpy (packet
, "l");
3835 /* Compose a response that is an imitation of the syntax by which the
3836 trace state variable was originally downloaded. */
3839 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3841 char *buf
= (char *) "";
3846 namelen
= strlen (tsv
->name
);
3847 buf
= alloca (namelen
* 2 + 1);
3848 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3851 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3852 tsv
->getter
? 1 : 0, buf
);
3855 /* Return the first trace state variable definition, and initialize
3856 the state machine that will iterate through all the tsv bits. */
3859 cmd_qtfv (char *packet
)
3861 trace_debug ("Returning first trace state variable definition");
3863 cur_tsv
= trace_state_variables
;
3866 response_tsv (packet
, cur_tsv
);
3868 strcpy (packet
, "l");
3871 /* Return additional trace state variable definitions. */
3874 cmd_qtsv (char *packet
)
3876 trace_debug ("Returning first trace state variable definition");
3880 /* This case would normally never occur, but be prepared for
3882 strcpy (packet
, "l");
3886 cur_tsv
= cur_tsv
->next
;
3888 response_tsv (packet
, cur_tsv
);
3890 strcpy (packet
, "l");
3893 strcpy (packet
, "l");
3896 /* Return the first static tracepoint marker, and initialize the state
3897 machine that will iterate through all the static tracepoints
3901 cmd_qtfstm (char *packet
)
3903 if (!maybe_write_ipa_ust_not_loaded (packet
))
3904 run_inferior_command (packet
, strlen (packet
) + 1);
3907 /* Return additional static tracepoints markers. */
3910 cmd_qtsstm (char *packet
)
3912 if (!maybe_write_ipa_ust_not_loaded (packet
))
3913 run_inferior_command (packet
, strlen (packet
) + 1);
3916 /* Return the definition of the static tracepoint at a given address.
3917 Result packet is the same as qTsST's. */
3920 cmd_qtstmat (char *packet
)
3922 if (!maybe_write_ipa_ust_not_loaded (packet
))
3923 run_inferior_command (packet
, strlen (packet
) + 1);
3926 /* Sent the agent a command to close it. */
3929 gdb_agent_about_to_close (int pid
)
3931 char buf
[IPA_CMD_BUF_SIZE
];
3933 if (!maybe_write_ipa_not_loaded (buf
))
3935 struct thread_info
*save_inferior
;
3936 struct inferior_list_entry
*inf
= all_threads
.head
;
3938 save_inferior
= current_inferior
;
3940 /* Find a certain thread which belongs to process PID. */
3943 if (ptid_get_pid (inf
->id
) == pid
)
3948 current_inferior
= (struct thread_info
*) inf
;
3950 strcpy (buf
, "close");
3952 run_inferior_command (buf
, strlen (buf
) + 1);
3954 current_inferior
= save_inferior
;
3958 /* Return the minimum instruction size needed for fast tracepoints as a
3959 hexadecimal number. */
3962 cmd_qtminftpilen (char *packet
)
3964 if (current_inferior
== NULL
)
3966 /* Indicate that the minimum length is currently unknown. */
3967 strcpy (packet
, "0");
3971 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3974 /* Respond to qTBuffer packet with a block of raw data from the trace
3975 buffer. GDB may ask for a lot, but we are allowed to reply with
3976 only as much as will fit within packet limits or whatever. */
3979 cmd_qtbuffer (char *own_buf
)
3981 ULONGEST offset
, num
, tot
;
3983 char *packet
= own_buf
;
3985 packet
+= strlen ("qTBuffer:");
3987 packet
= unpack_varlen_hex (packet
, &offset
);
3988 ++packet
; /* skip a comma */
3989 unpack_varlen_hex (packet
, &num
);
3991 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3992 (int) num
, pulongest (offset
));
3994 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3996 /* If we're right at the end, reply specially that we're done. */
3999 strcpy (own_buf
, "l");
4003 /* Object to any other out-of-bounds request. */
4006 write_enn (own_buf
);
4010 /* Compute the pointer corresponding to the given offset, accounting
4012 tbp
= trace_buffer_start
+ offset
;
4013 if (tbp
>= trace_buffer_wrap
)
4014 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4016 /* Trim to the remaining bytes if we're close to the end. */
4017 if (num
> tot
- offset
)
4020 /* Trim to available packet size. */
4021 if (num
>= (PBUFSIZ
- 16) / 2 )
4022 num
= (PBUFSIZ
- 16) / 2;
4024 convert_int_to_ascii (tbp
, own_buf
, num
);
4025 own_buf
[num
] = '\0';
4029 cmd_bigqtbuffer_circular (char *own_buf
)
4032 char *packet
= own_buf
;
4034 packet
+= strlen ("QTBuffer:circular:");
4036 unpack_varlen_hex (packet
, &val
);
4037 circular_trace_buffer
= val
;
4038 trace_debug ("Trace buffer is now %s",
4039 circular_trace_buffer
? "circular" : "linear");
4044 cmd_qtnotes (char *own_buf
)
4047 char *saved
, *user
, *notes
, *stopnote
;
4048 char *packet
= own_buf
;
4050 packet
+= strlen ("QTNotes:");
4054 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4056 packet
+= strlen ("user:");
4058 packet
= strchr (packet
, ';');
4059 nbytes
= (packet
- saved
) / 2;
4060 user
= xmalloc (nbytes
+ 1);
4061 nbytes
= unhexify (user
, saved
, nbytes
);
4062 user
[nbytes
] = '\0';
4063 ++packet
; /* skip the semicolon */
4064 trace_debug ("User is '%s'", user
);
4065 tracing_user_name
= user
;
4067 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4069 packet
+= strlen ("notes:");
4071 packet
= strchr (packet
, ';');
4072 nbytes
= (packet
- saved
) / 2;
4073 notes
= xmalloc (nbytes
+ 1);
4074 nbytes
= unhexify (notes
, saved
, nbytes
);
4075 notes
[nbytes
] = '\0';
4076 ++packet
; /* skip the semicolon */
4077 trace_debug ("Notes is '%s'", notes
);
4078 tracing_notes
= notes
;
4080 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4082 packet
+= strlen ("tstop:");
4084 packet
= strchr (packet
, ';');
4085 nbytes
= (packet
- saved
) / 2;
4086 stopnote
= xmalloc (nbytes
+ 1);
4087 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4088 stopnote
[nbytes
] = '\0';
4089 ++packet
; /* skip the semicolon */
4090 trace_debug ("tstop note is '%s'", stopnote
);
4091 tracing_stop_note
= stopnote
;
4101 handle_tracepoint_general_set (char *packet
)
4103 if (strcmp ("QTinit", packet
) == 0)
4105 cmd_qtinit (packet
);
4108 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4113 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4115 cmd_qtdpsrc (packet
);
4118 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4120 cmd_qtenable_disable (packet
, 1);
4123 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4125 cmd_qtenable_disable (packet
, 0);
4128 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4133 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4138 else if (strcmp ("QTStart", packet
) == 0)
4140 cmd_qtstart (packet
);
4143 else if (strcmp ("QTStop", packet
) == 0)
4145 cmd_qtstop (packet
);
4148 else if (strncmp ("QTDisconnected:", packet
,
4149 strlen ("QTDisconnected:")) == 0)
4151 cmd_qtdisconnected (packet
);
4154 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4156 cmd_qtframe (packet
);
4159 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4161 cmd_bigqtbuffer_circular (packet
);
4164 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4166 cmd_qtnotes (packet
);
4174 handle_tracepoint_query (char *packet
)
4176 if (strcmp ("qTStatus", packet
) == 0)
4178 cmd_qtstatus (packet
);
4181 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4186 else if (strcmp ("qTfP", packet
) == 0)
4191 else if (strcmp ("qTsP", packet
) == 0)
4196 else if (strcmp ("qTfV", packet
) == 0)
4201 else if (strcmp ("qTsV", packet
) == 0)
4206 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4211 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4213 cmd_qtbuffer (packet
);
4216 else if (strcmp ("qTfSTM", packet
) == 0)
4218 cmd_qtfstm (packet
);
4221 else if (strcmp ("qTsSTM", packet
) == 0)
4223 cmd_qtsstm (packet
);
4226 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4228 cmd_qtstmat (packet
);
4231 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4233 cmd_qtminftpilen (packet
);
4241 #ifndef IN_PROCESS_AGENT
4243 /* Call this when thread TINFO has hit the tracepoint defined by
4244 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4245 action. This adds a while-stepping collecting state item to the
4246 threads' collecting state list, so that we can keep track of
4247 multiple simultaneous while-stepping actions being collected by the
4248 same thread. This can happen in cases like:
4250 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4252 ff0003 INSN3 <-- TP2, collect $regs
4253 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4256 Notice that when instruction INSN5 is reached, the while-stepping
4257 actions of both TP1 and TP3 are still being collected, and that TP2
4258 had been collected meanwhile. The whole range of ff0001-ff0005
4259 should be single-stepped, due to at least TP1's while-stepping
4260 action covering the whole range. */
4263 add_while_stepping_state (struct thread_info
*tinfo
,
4264 int tp_number
, CORE_ADDR tp_address
)
4266 struct wstep_state
*wstep
;
4268 wstep
= xmalloc (sizeof (*wstep
));
4269 wstep
->next
= tinfo
->while_stepping
;
4271 wstep
->tp_number
= tp_number
;
4272 wstep
->tp_address
= tp_address
;
4273 wstep
->current_step
= 0;
4275 tinfo
->while_stepping
= wstep
;
4278 /* Release the while-stepping collecting state WSTEP. */
4281 release_while_stepping_state (struct wstep_state
*wstep
)
4286 /* Release all while-stepping collecting states currently associated
4287 with thread TINFO. */
4290 release_while_stepping_state_list (struct thread_info
*tinfo
)
4292 struct wstep_state
*head
;
4294 while (tinfo
->while_stepping
)
4296 head
= tinfo
->while_stepping
;
4297 tinfo
->while_stepping
= head
->next
;
4298 release_while_stepping_state (head
);
4302 /* If TINFO was handling a 'while-stepping' action, the step has
4303 finished, so collect any step data needed, and check if any more
4304 steps are required. Return true if the thread was indeed
4305 collecting tracepoint data, false otherwise. */
4308 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4310 struct tracepoint
*tpoint
;
4311 struct wstep_state
*wstep
;
4312 struct wstep_state
**wstep_link
;
4313 struct trap_tracepoint_ctx ctx
;
4315 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4317 if (agent_loaded_p ())
4318 upload_fast_traceframes ();
4320 /* Check if we were indeed collecting data for one of more
4321 tracepoints with a 'while-stepping' count. */
4322 if (tinfo
->while_stepping
== NULL
)
4327 /* We're not even tracing anymore. Stop this thread from
4329 release_while_stepping_state_list (tinfo
);
4331 /* The thread had stopped due to a single-step request indeed
4332 explained by a tracepoint. */
4336 wstep
= tinfo
->while_stepping
;
4337 wstep_link
= &tinfo
->while_stepping
;
4339 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4340 target_pid_to_str (tinfo
->entry
.id
),
4341 wstep
->tp_number
, paddress (wstep
->tp_address
));
4343 ctx
.base
.type
= trap_tracepoint
;
4344 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4346 while (wstep
!= NULL
)
4348 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4351 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4352 wstep
->tp_number
, paddress (wstep
->tp_address
),
4353 target_pid_to_str (tinfo
->entry
.id
));
4356 *wstep_link
= wstep
->next
;
4357 release_while_stepping_state (wstep
);
4358 wstep
= *wstep_link
;
4362 /* We've just finished one step. */
4363 ++wstep
->current_step
;
4366 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4367 stop_pc
, tpoint
, wstep
->current_step
);
4369 if (wstep
->current_step
>= tpoint
->step_count
)
4371 /* The requested numbers of steps have occurred. */
4372 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4373 target_pid_to_str (tinfo
->entry
.id
),
4374 wstep
->tp_number
, paddress (wstep
->tp_address
));
4376 /* Unlink the wstep. */
4377 *wstep_link
= wstep
->next
;
4378 release_while_stepping_state (wstep
);
4379 wstep
= *wstep_link
;
4381 /* Only check the hit count now, which ensure that we do all
4382 our stepping before stopping the run. */
4383 if (tpoint
->pass_count
> 0
4384 && tpoint
->hit_count
>= tpoint
->pass_count
4385 && stopping_tracepoint
== NULL
)
4386 stopping_tracepoint
= tpoint
;
4390 /* Keep single-stepping until the requested numbers of steps
4392 wstep_link
= &wstep
->next
;
4393 wstep
= *wstep_link
;
4396 if (stopping_tracepoint
4397 || trace_buffer_is_full
4398 || expr_eval_result
!= expr_eval_no_error
)
4408 /* Handle any internal tracing control breakpoint hits. That means,
4409 pull traceframes from the IPA to our buffer, and syncing both
4410 tracing agents when the IPA's tracing stops for some reason. */
4413 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4415 /* Pull in fast tracepoint trace frames from the inferior in-process
4416 agent's buffer into our buffer. */
4418 if (!agent_loaded_p ())
4421 upload_fast_traceframes ();
4423 /* Check if the in-process agent had decided we should stop
4425 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4427 int ipa_trace_buffer_is_full
;
4428 CORE_ADDR ipa_stopping_tracepoint
;
4429 int ipa_expr_eval_result
;
4430 CORE_ADDR ipa_error_tracepoint
;
4432 trace_debug ("lib stopped at stop_tracing");
4434 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4435 &ipa_trace_buffer_is_full
);
4437 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4438 &ipa_stopping_tracepoint
);
4439 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4441 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4442 &ipa_error_tracepoint
);
4443 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4445 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4446 &ipa_expr_eval_result
);
4447 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4449 trace_debug ("lib: trace_buffer_is_full: %d, "
4450 "stopping_tracepoint: %s, "
4451 "ipa_expr_eval_result: %d, "
4452 "error_tracepoint: %s, ",
4453 ipa_trace_buffer_is_full
,
4454 paddress (ipa_stopping_tracepoint
),
4455 ipa_expr_eval_result
,
4456 paddress (ipa_error_tracepoint
));
4460 if (ipa_trace_buffer_is_full
)
4461 trace_debug ("lib stopped due to full buffer.");
4462 if (ipa_stopping_tracepoint
)
4463 trace_debug ("lib stopped due to tpoint");
4464 if (ipa_stopping_tracepoint
)
4465 trace_debug ("lib stopped due to error");
4468 if (ipa_stopping_tracepoint
!= 0)
4471 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4473 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4475 expr_eval_result
= ipa_expr_eval_result
;
4477 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4482 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4484 trace_debug ("lib stopped at flush_trace_buffer");
4491 /* Return true if TINFO just hit a tracepoint. Collect data if
4495 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4497 struct tracepoint
*tpoint
;
4499 struct trap_tracepoint_ctx ctx
;
4501 /* Not tracing, don't handle. */
4505 ctx
.base
.type
= trap_tracepoint
;
4506 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4508 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4510 /* Note that we collect fast tracepoints here as well. We'll
4511 step over the fast tracepoint jump later, which avoids the
4512 double collect. However, we don't collect for static
4513 tracepoints here, because UST markers are compiled in program,
4514 and probes will be executed in program. So static tracepoints
4515 are collected there. */
4516 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4517 && tpoint
->type
!= static_tracepoint
)
4519 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4520 target_pid_to_str (tinfo
->entry
.id
),
4521 tpoint
->number
, paddress (tpoint
->address
));
4523 /* Test the condition if present, and collect if true. */
4525 || (condition_true_at_tracepoint
4526 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4527 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4530 if (stopping_tracepoint
4531 || trace_buffer_is_full
4532 || expr_eval_result
!= expr_eval_no_error
)
4536 /* If the tracepoint had a 'while-stepping' action, then set
4537 the thread to collect this tracepoint on the following
4539 else if (tpoint
->step_count
> 0)
4541 add_while_stepping_state (tinfo
,
4542 tpoint
->number
, tpoint
->address
);
4554 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4555 struct ust_marker_data
;
4556 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4557 struct traceframe
*tframe
);
4560 /* Create a trace frame for the hit of the given tracepoint in the
4564 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4565 struct tracepoint
*tpoint
)
4567 struct traceframe
*tframe
;
4570 /* Only count it as a hit when we actually collect data. */
4571 tpoint
->hit_count
++;
4573 /* If we've exceeded a defined pass count, record the event for
4574 later, and finish the collection for this hit. This test is only
4575 for nonstepping tracepoints, stepping tracepoints test at the end
4576 of their while-stepping loop. */
4577 if (tpoint
->pass_count
> 0
4578 && tpoint
->hit_count
>= tpoint
->pass_count
4579 && tpoint
->step_count
== 0
4580 && stopping_tracepoint
== NULL
)
4581 stopping_tracepoint
= tpoint
;
4583 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4584 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4586 tframe
= add_traceframe (tpoint
);
4590 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4592 #ifndef IN_PROCESS_AGENT
4593 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4594 tpoint
->number
, paddress (tpoint
->address
),
4595 tpoint
->actions_str
[acti
]);
4598 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4599 tpoint
->actions
[acti
]);
4602 finish_traceframe (tframe
);
4605 if (tframe
== NULL
&& tracing
)
4606 trace_buffer_is_full
= 1;
4609 #ifndef IN_PROCESS_AGENT
4612 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4614 struct tracepoint
*tpoint
, int current_step
)
4616 struct traceframe
*tframe
;
4619 trace_debug ("Making new step traceframe for "
4620 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4621 tpoint
->number
, paddress (tpoint
->address
),
4622 current_step
, tpoint
->step_count
,
4625 tframe
= add_traceframe (tpoint
);
4629 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4631 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4632 tpoint
->number
, paddress (tpoint
->address
),
4633 tpoint
->step_actions_str
[acti
]);
4635 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4636 tpoint
->step_actions
[acti
]);
4639 finish_traceframe (tframe
);
4642 if (tframe
== NULL
&& tracing
)
4643 trace_buffer_is_full
= 1;
4648 static struct regcache
*
4649 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4651 struct regcache
*regcache
= NULL
;
4653 #ifdef IN_PROCESS_AGENT
4654 if (ctx
->type
== fast_tracepoint
)
4656 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4657 if (!fctx
->regcache_initted
)
4659 fctx
->regcache_initted
= 1;
4660 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4661 supply_regblock (&fctx
->regcache
, NULL
);
4662 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4664 regcache
= &fctx
->regcache
;
4667 if (ctx
->type
== static_tracepoint
)
4669 struct static_tracepoint_ctx
*sctx
4670 = (struct static_tracepoint_ctx
*) ctx
;
4672 if (!sctx
->regcache_initted
)
4674 sctx
->regcache_initted
= 1;
4675 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4676 supply_regblock (&sctx
->regcache
, NULL
);
4677 /* Pass down the tracepoint address, because REGS doesn't
4678 include the PC, but we know what it must have been. */
4679 supply_static_tracepoint_registers (&sctx
->regcache
,
4680 (const unsigned char *)
4682 sctx
->tpoint
->address
);
4684 regcache
= &sctx
->regcache
;
4688 if (ctx
->type
== trap_tracepoint
)
4690 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4691 regcache
= tctx
->regcache
;
4695 gdb_assert (regcache
!= NULL
);
4701 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4703 struct tracepoint
*tpoint
,
4704 struct traceframe
*tframe
,
4705 struct tracepoint_action
*taction
)
4707 enum eval_result_type err
;
4709 switch (taction
->type
)
4713 struct collect_memory_action
*maction
;
4715 maction
= (struct collect_memory_action
*) taction
;
4717 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4718 pulongest (maction
->len
),
4719 paddress (maction
->addr
), maction
->basereg
);
4720 /* (should use basereg) */
4721 agent_mem_read (tframe
, NULL
,
4722 (CORE_ADDR
) maction
->addr
, maction
->len
);
4727 unsigned char *regspace
;
4728 struct regcache tregcache
;
4729 struct regcache
*context_regcache
;
4732 trace_debug ("Want to collect registers");
4734 /* Collect all registers for now. */
4735 regspace
= add_traceframe_block (tframe
,
4736 1 + register_cache_size ());
4737 if (regspace
== NULL
)
4739 trace_debug ("Trace buffer block allocation failed, skipping");
4742 /* Identify a register block. */
4745 context_regcache
= get_context_regcache (ctx
);
4747 /* Wrap the regblock in a register cache (in the stack, we
4748 don't want to malloc here). */
4749 init_register_cache (&tregcache
, regspace
+ 1);
4751 /* Copy the register data to the regblock. */
4752 regcache_cpy (&tregcache
, context_regcache
);
4754 #ifndef IN_PROCESS_AGENT
4755 /* On some platforms, trap-based tracepoints will have the PC
4756 pointing to the next instruction after the trap, but we
4757 don't want the user or GDB trying to guess whether the
4758 saved PC needs adjusting; so always record the adjusted
4759 stop_pc. Note that we can't use tpoint->address instead,
4760 since it will be wrong for while-stepping actions. This
4761 adjustment is a nop for fast tracepoints collected from the
4762 in-process lib (but not if GDBserver is collecting one
4763 preemptively), since the PC had already been adjusted to
4764 contain the tracepoint's address by the jump pad. */
4765 trace_debug ("Storing stop pc (0x%s) in regblock",
4766 paddress (stop_pc
));
4768 /* This changes the regblock, not the thread's
4770 regcache_write_pc (&tregcache
, stop_pc
);
4776 struct eval_expr_action
*eaction
;
4778 eaction
= (struct eval_expr_action
*) taction
;
4780 trace_debug ("Want to evaluate expression");
4782 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4784 if (err
!= expr_eval_no_error
)
4786 record_tracepoint_error (tpoint
, "action expression", err
);
4793 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4794 trace_debug ("Want to collect static trace data");
4795 collect_ust_data_at_tracepoint (ctx
, tframe
);
4797 trace_debug ("warning: collecting static trace data, "
4798 "but static tracepoints are not supported");
4803 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4809 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4810 struct tracepoint
*tpoint
)
4813 enum eval_result_type err
;
4815 /* Presently, gdbserver doesn't run compiled conditions, only the
4816 IPA does. If the program stops at a fast tracepoint's address
4817 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4818 gdbserver preemptively collect the fast tracepoint. Later, on
4819 resume, gdbserver steps over the fast tracepoint like it steps
4820 over breakpoints, so that the IPA doesn't see that fast
4821 tracepoint. This avoids double collects of fast tracepoints in
4822 that stopping scenario. Having gdbserver itself handle the fast
4823 tracepoint gives the user a consistent view of when fast or trap
4824 tracepoints are collected, compared to an alternative where only
4825 trap tracepoints are collected on stop, and fast tracepoints on
4826 resume. When a fast tracepoint is being processed by gdbserver,
4827 it is always the non-compiled condition expression that is
4829 #ifdef IN_PROCESS_AGENT
4830 if (tpoint
->compiled_cond
)
4831 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4834 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4836 if (err
!= expr_eval_no_error
)
4838 record_tracepoint_error (tpoint
, "condition", err
);
4839 /* The error case must return false. */
4843 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4844 tpoint
->number
, paddress (tpoint
->address
),
4846 return (value
? 1 : 0);
4849 /* Evaluates a tracepoint agent expression with context CTX,
4850 traceframe TFRAME, agent expression AEXPR and store the
4853 static enum eval_result_type
4854 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4855 struct traceframe
*tframe
,
4856 struct agent_expr
*aexpr
,
4859 struct regcache
*regcache
;
4860 regcache
= get_context_regcache (ctx
);
4862 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4865 /* Do memory copies for bytecodes. */
4866 /* Do the recording of memory blocks for actions and bytecodes. */
4869 agent_mem_read (struct traceframe
*tframe
,
4870 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4872 unsigned char *mspace
;
4873 ULONGEST remaining
= len
;
4874 unsigned short blocklen
;
4876 /* If a 'to' buffer is specified, use it. */
4879 read_inferior_memory (from
, to
, len
);
4883 /* Otherwise, create a new memory block in the trace buffer. */
4884 while (remaining
> 0)
4888 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4889 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4890 mspace
= add_traceframe_block (tframe
, sp
);
4893 /* Identify block as a memory block. */
4896 /* Record address and size. */
4897 memcpy (mspace
, &from
, sizeof (from
));
4898 mspace
+= sizeof (from
);
4899 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4900 mspace
+= sizeof (blocklen
);
4901 /* Record the memory block proper. */
4902 read_inferior_memory (from
, mspace
, blocklen
);
4903 trace_debug ("%d bytes recorded", blocklen
);
4904 remaining
-= blocklen
;
4911 agent_mem_read_string (struct traceframe
*tframe
,
4912 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4914 unsigned char *buf
, *mspace
;
4915 ULONGEST remaining
= len
;
4916 unsigned short blocklen
, i
;
4918 /* To save a bit of space, block lengths are 16-bit, so break large
4919 requests into multiple blocks. Bordering on overkill for strings,
4920 but it could happen that someone specifies a large max length. */
4921 while (remaining
> 0)
4925 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4926 /* We want working space to accumulate nonzero bytes, since
4927 traceframes must have a predecided size (otherwise it gets
4928 harder to wrap correctly for the circular case, etc). */
4929 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4930 for (i
= 0; i
< blocklen
; ++i
)
4932 /* Read the string one byte at a time, in case the string is
4933 at the end of a valid memory area - we don't want a
4934 correctly-terminated string to engender segvio
4936 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4941 /* Make sure outer loop stops now too. */
4942 remaining
= blocklen
;
4946 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4947 mspace
= add_traceframe_block (tframe
, sp
);
4953 /* Identify block as a memory block. */
4956 /* Record address and size. */
4957 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4958 mspace
+= sizeof (from
);
4959 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4960 mspace
+= sizeof (blocklen
);
4961 /* Copy the string contents. */
4962 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4963 remaining
-= blocklen
;
4970 /* Record the value of a trace state variable. */
4973 agent_tsv_read (struct traceframe
*tframe
, int n
)
4975 unsigned char *vspace
;
4978 vspace
= add_traceframe_block (tframe
,
4979 1 + sizeof (n
) + sizeof (LONGEST
));
4982 /* Identify block as a variable. */
4984 /* Record variable's number and value. */
4985 memcpy (vspace
+ 1, &n
, sizeof (n
));
4986 val
= get_trace_state_variable_value (n
);
4987 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4988 trace_debug ("Variable %d recorded", n
);
4992 #ifndef IN_PROCESS_AGENT
4994 /* Callback for traceframe_walk_blocks, used to find a given block
4995 type in a traceframe. */
4998 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5000 char *wantedp
= data
;
5002 if (*wantedp
== blocktype
)
5008 /* Walk over all traceframe blocks of the traceframe buffer starting
5009 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5010 block found, passing in DATA unmodified. If CALLBACK returns true,
5011 this returns a pointer to where the block is found. Returns NULL
5012 if no callback call returned true, indicating that all blocks have
5015 static unsigned char *
5016 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5018 int (*callback
) (char blocktype
,
5019 unsigned char *dataptr
,
5023 unsigned char *dataptr
;
5027 trace_debug ("traceframe %d has no data", tfnum
);
5031 /* Iterate through a traceframe's blocks, looking for a block of the
5033 for (dataptr
= database
;
5034 dataptr
< database
+ datasize
;
5038 unsigned short mlen
;
5040 if (dataptr
== trace_buffer_wrap
)
5042 /* Adjust to reflect wrapping part of the frame around to
5044 datasize
= dataptr
- database
;
5045 dataptr
= database
= trace_buffer_lo
;
5048 blocktype
= *dataptr
++;
5050 if ((*callback
) (blocktype
, dataptr
, data
))
5056 /* Skip over the registers block. */
5057 dataptr
+= register_cache_size ();
5060 /* Skip over the memory block. */
5061 dataptr
+= sizeof (CORE_ADDR
);
5062 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5063 dataptr
+= (sizeof (mlen
) + mlen
);
5066 /* Skip over the TSV block. */
5067 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5070 /* Skip over the static trace data block. */
5071 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5072 dataptr
+= (sizeof (mlen
) + mlen
);
5075 trace_debug ("traceframe %d has unknown block type 0x%x",
5084 /* Look for the block of type TYPE_WANTED in the trameframe starting
5085 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5088 static unsigned char *
5089 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5090 int tfnum
, char type_wanted
)
5092 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5093 match_blocktype
, &type_wanted
);
5096 static unsigned char *
5097 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5099 unsigned char *regblock
;
5101 regblock
= traceframe_find_block_type (tframe
->data
,
5105 if (regblock
== NULL
)
5106 trace_debug ("traceframe %d has no register data", tfnum
);
5111 /* Get registers from a traceframe. */
5114 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5116 unsigned char *dataptr
;
5117 struct tracepoint
*tpoint
;
5118 struct traceframe
*tframe
;
5120 tframe
= find_traceframe (tfnum
);
5124 trace_debug ("traceframe %d not found", tfnum
);
5128 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5129 if (dataptr
== NULL
)
5131 /* Mark registers unavailable. */
5132 supply_regblock (regcache
, NULL
);
5134 /* We can generally guess at a PC, although this will be
5135 misleading for while-stepping frames and multi-location
5137 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5139 regcache_write_pc (regcache
, tpoint
->address
);
5142 supply_regblock (regcache
, dataptr
);
5148 traceframe_get_pc (struct traceframe
*tframe
)
5150 struct regcache regcache
;
5151 unsigned char *dataptr
;
5153 dataptr
= traceframe_find_regblock (tframe
, -1);
5154 if (dataptr
== NULL
)
5157 init_register_cache (®cache
, dataptr
);
5158 return regcache_read_pc (®cache
);
5161 /* Read a requested block of memory from a trace frame. */
5164 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5165 unsigned char *buf
, ULONGEST length
,
5168 struct traceframe
*tframe
;
5169 unsigned char *database
, *dataptr
;
5170 unsigned int datasize
;
5172 unsigned short mlen
;
5174 trace_debug ("traceframe_read_mem");
5176 tframe
= find_traceframe (tfnum
);
5180 trace_debug ("traceframe %d not found", tfnum
);
5184 datasize
= tframe
->data_size
;
5185 database
= dataptr
= &tframe
->data
[0];
5187 /* Iterate through a traceframe's blocks, looking for memory. */
5188 while ((dataptr
= traceframe_find_block_type (dataptr
,
5190 - (dataptr
- database
),
5191 tfnum
, 'M')) != NULL
)
5193 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5194 dataptr
+= sizeof (maddr
);
5195 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5196 dataptr
+= sizeof (mlen
);
5197 trace_debug ("traceframe %d has %d bytes at %s",
5198 tfnum
, mlen
, paddress (maddr
));
5200 /* If the block includes the first part of the desired range,
5201 return as much it has; GDB will re-request the remainder,
5202 which might be in a different block of this trace frame. */
5203 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5205 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5209 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5214 /* Skip over this block. */
5218 trace_debug ("traceframe %d has no memory data for the desired region",
5226 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5229 struct traceframe
*tframe
;
5230 unsigned char *database
, *dataptr
;
5231 unsigned int datasize
;
5234 trace_debug ("traceframe_read_tsv");
5236 tfnum
= current_traceframe
;
5240 trace_debug ("no current traceframe");
5244 tframe
= find_traceframe (tfnum
);
5248 trace_debug ("traceframe %d not found", tfnum
);
5252 datasize
= tframe
->data_size
;
5253 database
= dataptr
= &tframe
->data
[0];
5255 /* Iterate through a traceframe's blocks, looking for the tsv. */
5256 while ((dataptr
= traceframe_find_block_type (dataptr
,
5258 - (dataptr
- database
),
5259 tfnum
, 'V')) != NULL
)
5261 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5262 dataptr
+= sizeof (vnum
);
5264 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5266 /* Check that this is the variable we want. */
5269 memcpy (val
, dataptr
, sizeof (*val
));
5273 /* Skip over this block. */
5274 dataptr
+= sizeof (LONGEST
);
5277 trace_debug ("traceframe %d has no data for variable %d",
5282 /* Read a requested block of static tracepoint data from a trace
5286 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5287 unsigned char *buf
, ULONGEST length
,
5290 struct traceframe
*tframe
;
5291 unsigned char *database
, *dataptr
;
5292 unsigned int datasize
;
5293 unsigned short mlen
;
5295 trace_debug ("traceframe_read_sdata");
5297 tframe
= find_traceframe (tfnum
);
5301 trace_debug ("traceframe %d not found", tfnum
);
5305 datasize
= tframe
->data_size
;
5306 database
= &tframe
->data
[0];
5308 /* Iterate through a traceframe's blocks, looking for static
5310 dataptr
= traceframe_find_block_type (database
, datasize
,
5312 if (dataptr
!= NULL
)
5314 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5315 dataptr
+= sizeof (mlen
);
5318 if (offset
+ length
> mlen
)
5319 length
= mlen
- offset
;
5321 memcpy (buf
, dataptr
, length
);
5329 trace_debug ("traceframe %d has no static trace data", tfnum
);
5335 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5336 object. DATA is pointer to a struct buffer holding the
5337 traceframe-info object being built. */
5340 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5342 struct buffer
*buffer
= data
;
5348 unsigned short mlen
;
5351 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5352 dataptr
+= sizeof (maddr
);
5353 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5354 dataptr
+= sizeof (mlen
);
5355 buffer_xml_printf (buffer
,
5356 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5357 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5367 warning ("Unhandled trace block type (%d) '%c ' "
5368 "while building trace frame info.",
5369 blocktype
, blocktype
);
5376 /* Build a traceframe-info object for traceframe number TFNUM into
5380 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5382 struct traceframe
*tframe
;
5384 trace_debug ("traceframe_read_info");
5386 tframe
= find_traceframe (tfnum
);
5390 trace_debug ("traceframe %d not found", tfnum
);
5394 buffer_grow_str (buffer
, "<traceframe-info>\n");
5395 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5396 tfnum
, build_traceframe_info_xml
, buffer
);
5397 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5401 /* Return the first fast tracepoint whose jump pad contains PC. */
5403 static struct tracepoint
*
5404 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5406 struct tracepoint
*tpoint
;
5408 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5409 if (tpoint
->type
== fast_tracepoint
)
5410 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5416 /* Return the first fast tracepoint whose trampoline contains PC. */
5418 static struct tracepoint
*
5419 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5421 struct tracepoint
*tpoint
;
5423 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5425 if (tpoint
->type
== fast_tracepoint
5426 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5433 /* Return GDBserver's tracepoint that matches the IP Agent's
5434 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5437 static struct tracepoint
*
5438 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5440 struct tracepoint
*tpoint
;
5442 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5443 if (tpoint
->type
== fast_tracepoint
)
5444 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5452 /* The type of the object that is used to synchronize fast tracepoint
5455 typedef struct collecting_t
5457 /* The fast tracepoint number currently collecting. */
5460 /* A number that GDBserver can use to identify the thread that is
5461 presently holding the collect lock. This need not (and usually
5462 is not) the thread id, as getting the current thread ID usually
5463 requires a system call, which we want to avoid like the plague.
5464 Usually this is thread's TCB, found in the TLS (pseudo-)
5465 register, which is readable with a single insn on several
5467 uintptr_t thread_area
;
5470 #ifndef IN_PROCESS_AGENT
5473 force_unlock_trace_buffer (void)
5475 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5478 /* Check if the thread identified by THREAD_AREA which is stopped at
5479 STOP_PC, is presently locking the fast tracepoint collection, and
5480 if so, gather some status of said collection. Returns 0 if the
5481 thread isn't collecting or in the jump pad at all. 1, if in the
5482 jump pad (or within gdb_collect) and hasn't executed the adjusted
5483 original insn yet (can set a breakpoint there and run to it). 2,
5484 if presently executing the adjusted original insn --- in which
5485 case, if we want to move the thread out of the jump pad, we need to
5486 single-step it until this function returns 0. */
5489 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5491 struct fast_tpoint_collect_status
*status
)
5493 CORE_ADDR ipa_collecting
;
5494 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5495 CORE_ADDR ipa_gdb_trampoline_buffer
;
5496 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5497 struct tracepoint
*tpoint
;
5498 int needs_breakpoint
;
5500 /* The thread THREAD_AREA is either:
5502 0. not collecting at all, not within the jump pad, or within
5503 gdb_collect or one of its callees.
5505 1. in the jump pad and haven't reached gdb_collect
5507 2. within gdb_collect (out of the jump pad) (collect is set)
5509 3. we're in the jump pad, after gdb_collect having returned,
5510 possibly executing the adjusted insns.
5512 For cases 1 and 3, `collecting' may or not be set. The jump pad
5513 doesn't have any complicated jump logic, so we can tell if the
5514 thread is executing the adjust original insn or not by just
5515 matching STOP_PC with known jump pad addresses. If we it isn't
5516 yet executing the original insn, set a breakpoint there, and let
5517 the thread run to it, so to quickly step over a possible (many
5518 insns) gdb_collect call. Otherwise, or when the breakpoint is
5519 hit, only a few (small number of) insns are left to be executed
5520 in the jump pad. Single-step the thread until it leaves the
5525 needs_breakpoint
= 0;
5526 trace_debug ("fast_tracepoint_collecting");
5528 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5529 &ipa_gdb_jump_pad_buffer
))
5530 fatal ("error extracting `gdb_jump_pad_buffer'");
5531 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5532 &ipa_gdb_jump_pad_buffer_end
))
5533 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5535 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5536 &ipa_gdb_trampoline_buffer
))
5537 fatal ("error extracting `gdb_trampoline_buffer'");
5538 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5539 &ipa_gdb_trampoline_buffer_end
))
5540 fatal ("error extracting `gdb_trampoline_buffer_end'");
5542 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5543 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5545 /* We can tell which tracepoint(s) the thread is collecting by
5546 matching the jump pad address back to the tracepoint. */
5547 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5550 warning ("in jump pad, but no matching tpoint?");
5555 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5557 tpoint
->number
, paddress (tpoint
->address
),
5558 paddress (tpoint
->jump_pad
),
5559 paddress (tpoint
->jump_pad_end
),
5560 paddress (tpoint
->adjusted_insn_addr
),
5561 paddress (tpoint
->adjusted_insn_addr_end
));
5564 /* Definitely in the jump pad. May or may not need
5565 fast-exit-jump-pad breakpoint. */
5566 if (tpoint
->jump_pad
<= stop_pc
5567 && stop_pc
< tpoint
->adjusted_insn_addr
)
5568 needs_breakpoint
= 1;
5570 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5571 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5573 /* We can tell which tracepoint(s) the thread is collecting by
5574 matching the trampoline address back to the tracepoint. */
5575 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5578 warning ("in trampoline, but no matching tpoint?");
5583 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5584 tpoint
->number
, paddress (tpoint
->address
),
5585 paddress (tpoint
->trampoline
),
5586 paddress (tpoint
->trampoline_end
));
5589 /* Have not reached jump pad yet, but treat the trampoline as a
5590 part of the jump pad that is before the adjusted original
5592 needs_breakpoint
= 1;
5596 collecting_t ipa_collecting_obj
;
5598 /* If `collecting' is set/locked, then the THREAD_AREA thread
5599 may or not be the one holding the lock. We have to read the
5600 lock to find out. */
5602 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5605 trace_debug ("fast_tracepoint_collecting:"
5606 " failed reading 'collecting' in the inferior");
5610 if (!ipa_collecting
)
5612 trace_debug ("fast_tracepoint_collecting: not collecting"
5613 " (and nobody is).");
5617 /* Some thread is collecting. Check which. */
5618 if (read_inferior_memory (ipa_collecting
,
5619 (unsigned char *) &ipa_collecting_obj
,
5620 sizeof (ipa_collecting_obj
)) != 0)
5623 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5625 trace_debug ("fast_tracepoint_collecting: not collecting "
5626 "(another thread is)");
5631 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5634 warning ("fast_tracepoint_collecting: collecting, "
5635 "but tpoint %s not found?",
5636 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5640 /* The thread is within `gdb_collect', skip over the rest of
5641 fast tracepoint collection quickly using a breakpoint. */
5642 needs_breakpoint
= 1;
5645 /* The caller wants a bit of status detail. */
5648 status
->tpoint_num
= tpoint
->number
;
5649 status
->tpoint_addr
= tpoint
->address
;
5650 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5651 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5654 if (needs_breakpoint
)
5656 /* Hasn't executed the original instruction yet. Set breakpoint
5657 there, and wait till it's hit, then single-step until exiting
5661 fast_tracepoint_collecting, returning continue-until-break at %s",
5662 paddress (tpoint
->adjusted_insn_addr
));
5664 return 1; /* continue */
5668 /* Just single-step until exiting the jump pad. */
5670 trace_debug ("fast_tracepoint_collecting, returning "
5671 "need-single-step (%s-%s)",
5672 paddress (tpoint
->adjusted_insn_addr
),
5673 paddress (tpoint
->adjusted_insn_addr_end
));
5675 return 2; /* single-step */
5681 #ifdef IN_PROCESS_AGENT
5683 /* The global fast tracepoint collect lock. Points to a collecting_t
5684 object built on the stack by the jump pad, if presently locked;
5685 NULL if it isn't locked. Note that this lock *must* be set while
5686 executing any *function other than the jump pad. See
5687 fast_tracepoint_collecting. */
5688 static collecting_t
* ATTR_USED collecting
;
5690 /* This routine, called from the jump pad (in asm) is designed to be
5691 called from the jump pads of fast tracepoints, thus it is on the
5694 IP_AGENT_EXPORT
void ATTR_USED
5695 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5697 struct fast_tracepoint_ctx ctx
;
5699 /* Don't do anything until the trace run is completely set up. */
5703 ctx
.base
.type
= fast_tracepoint
;
5705 ctx
.regcache_initted
= 0;
5706 /* Wrap the regblock in a register cache (in the stack, we don't
5707 want to malloc here). */
5708 ctx
.regspace
= alloca (register_cache_size ());
5709 if (ctx
.regspace
== NULL
)
5711 trace_debug ("Trace buffer block allocation failed, skipping");
5715 for (ctx
.tpoint
= tpoint
;
5716 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5717 ctx
.tpoint
= ctx
.tpoint
->next
)
5719 if (!ctx
.tpoint
->enabled
)
5722 /* Multiple tracepoints of different types, such as fast tracepoint and
5723 static tracepoint, can be set at the same address. */
5724 if (ctx
.tpoint
->type
!= tpoint
->type
)
5727 /* Test the condition if present, and collect if true. */
5728 if (ctx
.tpoint
->cond
== NULL
5729 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5732 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5733 ctx
.tpoint
->address
, ctx
.tpoint
);
5735 /* Note that this will cause original insns to be written back
5736 to where we jumped from, but that's OK because we're jumping
5737 back to the next whole instruction. This will go badly if
5738 instruction restoration is not atomic though. */
5739 if (stopping_tracepoint
5740 || trace_buffer_is_full
5741 || expr_eval_result
!= expr_eval_no_error
)
5749 /* If there was a condition and it evaluated to false, the only
5750 way we would stop tracing is if there was an error during
5751 condition expression evaluation. */
5752 if (expr_eval_result
!= expr_eval_no_error
)
5763 #ifndef IN_PROCESS_AGENT
5766 get_raw_reg_func_addr (void)
5768 return ipa_sym_addrs
.addr_get_raw_reg
;
5772 get_get_tsv_func_addr (void)
5774 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5778 get_set_tsv_func_addr (void)
5780 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5784 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5785 CORE_ADDR
*jump_entry
)
5787 CORE_ADDR entry_point
= *jump_entry
;
5788 enum eval_result_type err
;
5790 trace_debug ("Starting condition compilation for tracepoint %d\n",
5793 /* Initialize the global pointer to the code being built. */
5794 current_insn_ptr
= *jump_entry
;
5798 err
= compile_bytecodes (tpoint
->cond
);
5800 if (err
== expr_eval_no_error
)
5804 /* Record the beginning of the compiled code. */
5805 tpoint
->compiled_cond
= entry_point
;
5807 trace_debug ("Condition compilation for tracepoint %d complete\n",
5812 /* Leave the unfinished code in situ, but don't point to it. */
5814 tpoint
->compiled_cond
= 0;
5816 trace_debug ("Condition compilation for tracepoint %d failed, "
5818 tpoint
->number
, err
);
5821 /* Update the code pointer passed in. Note that we do this even if
5822 the compile fails, so that we can look at the partial results
5823 instead of letting them be overwritten. */
5824 *jump_entry
= current_insn_ptr
;
5826 /* Leave a gap, to aid dump decipherment. */
5830 /* We'll need to adjust these when we consider bi-arch setups, and big
5834 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5836 return write_inferior_memory (where
,
5837 (unsigned char *) &ptr
, sizeof (void *));
5840 /* The base pointer of the IPA's heap. This is the only memory the
5841 IPA is allowed to use. The IPA should _not_ call the inferior's
5842 `malloc' during operation. That'd be slow, and, most importantly,
5843 it may not be safe. We may be collecting a tracepoint in a signal
5844 handler, for example. */
5845 static CORE_ADDR target_tp_heap
;
5847 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5851 target_malloc (ULONGEST size
)
5855 if (target_tp_heap
== 0)
5857 /* We have the pointer *address*, need what it points to. */
5858 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5860 fatal ("could get target heap head pointer");
5863 ptr
= target_tp_heap
;
5864 target_tp_heap
+= size
;
5866 /* Pad to 8-byte alignment. */
5867 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5873 download_agent_expr (struct agent_expr
*expr
)
5875 CORE_ADDR expr_addr
;
5876 CORE_ADDR expr_bytes
;
5878 expr_addr
= target_malloc (sizeof (*expr
));
5879 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5881 expr_bytes
= target_malloc (expr
->length
);
5882 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5884 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5889 /* Align V up to N bits. */
5890 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5892 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5895 download_tracepoint_1 (struct tracepoint
*tpoint
)
5897 struct tracepoint target_tracepoint
;
5898 CORE_ADDR tpptr
= 0;
5900 gdb_assert (tpoint
->type
== fast_tracepoint
5901 || tpoint
->type
== static_tracepoint
);
5903 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5905 CORE_ADDR jentry
, jump_entry
;
5907 jentry
= jump_entry
= get_jump_space_head ();
5909 if (tpoint
->cond
!= NULL
)
5911 /* Pad to 8-byte alignment. (needed?) */
5912 /* Actually this should be left for the target to
5914 jentry
= UALIGN (jentry
, 8);
5916 compile_tracepoint_condition (tpoint
, &jentry
);
5919 /* Pad to 8-byte alignment. */
5920 jentry
= UALIGN (jentry
, 8);
5921 claim_jump_space (jentry
- jump_entry
);
5924 target_tracepoint
= *tpoint
;
5926 tpptr
= target_malloc (sizeof (*tpoint
));
5927 tpoint
->obj_addr_on_target
= tpptr
;
5929 /* Write the whole object. We'll fix up its pointers in a bit.
5930 Assume no next for now. This is fixed up above on the next
5931 iteration, if there's any. */
5932 target_tracepoint
.next
= NULL
;
5933 /* Need to clear this here too, since we're downloading the
5934 tracepoints before clearing our own copy. */
5935 target_tracepoint
.hit_count
= 0;
5937 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5938 sizeof (target_tracepoint
));
5941 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5943 download_agent_expr (tpoint
->cond
));
5945 if (tpoint
->numactions
)
5948 CORE_ADDR actions_array
;
5950 /* The pointers array. */
5952 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5953 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5957 /* Now for each pointer, download the action. */
5958 for (i
= 0; i
< tpoint
->numactions
; i
++)
5960 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5961 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5963 if (ipa_action
!= 0)
5964 write_inferior_data_ptr
5965 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5971 #define IPA_PROTO_FAST_TRACE_FLAG 0
5972 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
5973 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
5974 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
5975 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
5977 /* Send a command to agent to download and install tracepoint TPOINT. */
5980 tracepoint_send_agent (struct tracepoint
*tpoint
)
5982 char buf
[IPA_CMD_BUF_SIZE
];
5987 strcpy (p
, "FastTrace:");
5990 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
5991 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
5992 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
5993 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
5994 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
5995 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
5996 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
5997 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
5998 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
5999 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6000 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6003 p
= agent_expr_send (p
, tpoint
->cond
);
6005 /* tracepoint_action */
6006 for (i
= 0; i
< tpoint
->numactions
; i
++)
6008 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6010 p
[0] = action
->type
;
6011 p
= action
->ops
->send (&p
[1], action
);
6014 get_jump_space_head ();
6015 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6016 agent can use jump pad from it. */
6017 if (tpoint
->type
== fast_tracepoint
)
6019 memcpy (p
, &gdb_jump_pad_head
, 8);
6023 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6027 if (strncmp (buf
, "OK", 2) != 0)
6030 /* The value of tracepoint's target address is stored in BUF. */
6031 memcpy (&tpoint
->obj_addr_on_target
,
6032 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6034 if (tpoint
->type
== fast_tracepoint
)
6037 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6040 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6041 (unsigned int) tpoint
->obj_addr_on_target
,
6042 (unsigned int) gdb_jump_pad_head
);
6044 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6046 /* This has been done in agent. We should also set up record for it. */
6047 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6050 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6057 download_tracepoint (struct tracepoint
*tpoint
)
6059 struct tracepoint
*tp
, *tp_prev
;
6061 if (tpoint
->type
!= fast_tracepoint
6062 && tpoint
->type
!= static_tracepoint
)
6065 download_tracepoint_1 (tpoint
);
6067 /* Find the previous entry of TPOINT, which is fast tracepoint or
6068 static tracepoint. */
6070 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6072 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6078 CORE_ADDR tp_prev_target_next_addr
;
6080 /* Insert TPOINT after TP_PREV in IPA. */
6081 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6082 + offsetof (struct tracepoint
, next
),
6083 &tp_prev_target_next_addr
))
6084 fatal ("error reading `tp_prev->next'");
6086 /* tpoint->next = tp_prev->next */
6087 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6088 + offsetof (struct tracepoint
, next
),
6089 tp_prev_target_next_addr
);
6090 /* tp_prev->next = tpoint */
6091 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6092 + offsetof (struct tracepoint
, next
),
6093 tpoint
->obj_addr_on_target
);
6096 /* First object in list, set the head pointer in the
6098 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6099 tpoint
->obj_addr_on_target
);
6104 download_trace_state_variables (void)
6106 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6107 struct trace_state_variable
*tsv
;
6109 /* Start out empty. */
6110 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6112 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6114 struct trace_state_variable target_tsv
;
6116 /* TSV's with a getter have been initialized equally in both the
6117 inferior and GDBserver. Skip them. */
6118 if (tsv
->getter
!= NULL
)
6124 ptr
= target_malloc (sizeof (*tsv
));
6126 if (tsv
== trace_state_variables
)
6128 /* First object in list, set the head pointer in the
6131 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6136 write_inferior_data_ptr (prev_ptr
6137 + offsetof (struct trace_state_variable
,
6142 /* Write the whole object. We'll fix up its pointers in a bit.
6143 Assume no next, fixup when needed. */
6144 target_tsv
.next
= NULL
;
6146 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6147 sizeof (target_tsv
));
6149 if (tsv
->name
!= NULL
)
6151 size_t size
= strlen (tsv
->name
) + 1;
6152 CORE_ADDR name_addr
= target_malloc (size
);
6153 write_inferior_memory (name_addr
,
6154 (unsigned char *) tsv
->name
, size
);
6155 write_inferior_data_ptr (ptr
6156 + offsetof (struct trace_state_variable
,
6161 if (tsv
->getter
!= NULL
)
6163 fatal ("what to do with these?");
6169 /* Fixup the next pointer in the last item in the list. */
6170 write_inferior_data_ptr (prev_ptr
6171 + offsetof (struct trace_state_variable
,
6176 /* Upload complete trace frames out of the IP Agent's trace buffer
6177 into GDBserver's trace buffer. This always uploads either all or
6178 no trace frames. This is the counter part of
6179 `trace_alloc_trace_buffer'. See its description of the atomic
6180 synching mechanism. */
6183 upload_fast_traceframes (void)
6185 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6186 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6188 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6189 unsigned int curr_tbctrl_idx
;
6190 unsigned int ipa_trace_buffer_ctrl_curr
;
6191 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6192 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6193 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6194 CORE_ADDR ipa_trace_buffer_lo
;
6195 CORE_ADDR ipa_trace_buffer_hi
;
6197 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6198 &ipa_traceframe_read_count_racy
))
6200 /* This will happen in most targets if the current thread is
6205 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6206 &ipa_traceframe_write_count_racy
))
6209 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6210 ipa_traceframe_write_count_racy
6211 - ipa_traceframe_read_count_racy
,
6212 ipa_traceframe_write_count_racy
,
6213 ipa_traceframe_read_count_racy
);
6215 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6218 about_to_request_buffer_space_bkpt
6219 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6222 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6223 &ipa_trace_buffer_ctrl_curr
))
6226 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6228 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6231 unsigned int prev
, counter
;
6233 /* Update the token, with new counters, and the GDBserver stamp
6234 bit. Alway reuse the current TBC index. */
6235 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6236 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6238 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6244 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6245 ipa_trace_buffer_ctrl_curr
))
6248 trace_debug ("Lib: Committed %08x -> %08x",
6249 ipa_trace_buffer_ctrl_curr_old
,
6250 ipa_trace_buffer_ctrl_curr
);
6252 /* Re-read these, now that we've installed the
6253 `about_to_request_buffer_space' breakpoint/lock. A thread could
6254 have finished a traceframe between the last read of these
6255 counters and setting the breakpoint above. If we start
6256 uploading, we never want to leave this function with
6257 traceframe_read_count != 0, otherwise, GDBserver could end up
6258 incrementing the counter tokens more than once (due to event loop
6259 nesting), which would break the IP agent's "effective" detection
6260 (see trace_alloc_trace_buffer). */
6261 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6262 &ipa_traceframe_read_count
))
6264 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6265 &ipa_traceframe_write_count
))
6270 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6271 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6272 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6274 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6275 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6276 trace_debug ("note that ipa_traceframe_count's parts changed");
6279 /* Get the address of the current TBC object (the IP agent has an
6280 array of 3 such objects). The index is stored in the TBC
6282 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6283 ipa_trace_buffer_ctrl_addr
6284 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6286 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6287 (unsigned char *) &ipa_trace_buffer_ctrl
,
6288 sizeof (struct ipa_trace_buffer_control
)))
6291 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6292 &ipa_trace_buffer_lo
))
6294 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6295 &ipa_trace_buffer_hi
))
6298 /* Offsets are easier to grok for debugging than raw addresses,
6299 especially for the small trace buffer sizes that are useful for
6301 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6302 "endfree=%d wrap=%d hi=%d",
6304 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6305 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6306 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6307 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6308 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6310 /* Note that the IPA's buffer is always circular. */
6312 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6314 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6315 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6317 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6318 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6319 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6320 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6323 tf
= IPA_FIRST_TRACEFRAME ();
6325 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6327 struct tracepoint
*tpoint
;
6328 struct traceframe
*tframe
;
6329 unsigned char *block
;
6330 struct traceframe ipa_tframe
;
6332 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6333 offsetof (struct traceframe
, data
)))
6334 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6336 if (ipa_tframe
.tpnum
== 0)
6337 fatal ("Uploading: No (more) fast traceframes, but "
6338 "ipa_traceframe_count == %u??\n",
6339 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6341 /* Note that this will be incorrect for multi-location
6343 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6345 tframe
= add_traceframe (tpoint
);
6348 trace_buffer_is_full
= 1;
6349 trace_debug ("Uploading: trace buffer is full");
6353 /* Copy the whole set of blocks in one go for now. FIXME:
6354 split this in smaller blocks. */
6355 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6358 if (read_inferior_memory (tf
6359 + offsetof (struct traceframe
, data
),
6360 block
, ipa_tframe
.data_size
))
6361 error ("Uploading: Couldn't read traceframe data at %s\n",
6362 paddress (tf
+ offsetof (struct traceframe
, data
)));
6365 trace_debug ("Uploading: traceframe didn't fit");
6366 finish_traceframe (tframe
);
6369 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6371 /* If we freed the traceframe that wrapped around, go back
6372 to the non-wrap case. */
6373 if (tf
< ipa_trace_buffer_ctrl
.start
)
6375 trace_debug ("Lib: Discarding past the wraparound");
6376 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6378 ipa_trace_buffer_ctrl
.start
= tf
;
6379 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6380 ++ipa_traceframe_read_count
;
6382 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6383 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6385 trace_debug ("Lib: buffer is fully empty. "
6386 "Trace buffer [%d] start=%d free=%d endfree=%d",
6388 (int) (ipa_trace_buffer_ctrl
.start
6389 - ipa_trace_buffer_lo
),
6390 (int) (ipa_trace_buffer_ctrl
.free
6391 - ipa_trace_buffer_lo
),
6392 (int) (ipa_trace_buffer_ctrl
.end_free
6393 - ipa_trace_buffer_lo
));
6395 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6396 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6397 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6398 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6401 trace_debug ("Uploaded a traceframe\n"
6402 "Lib: Trace buffer [%d] start=%d free=%d "
6403 "endfree=%d wrap=%d hi=%d",
6405 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6406 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6407 (int) (ipa_trace_buffer_ctrl
.end_free
6408 - ipa_trace_buffer_lo
),
6409 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6410 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6413 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6414 (unsigned char *) &ipa_trace_buffer_ctrl
,
6415 sizeof (struct ipa_trace_buffer_control
)))
6418 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6419 ipa_traceframe_read_count
);
6421 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6424 cancel_breakpoints ();
6426 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6427 about_to_request_buffer_space_bkpt
= NULL
;
6431 if (trace_buffer_is_full
)
6436 #ifdef IN_PROCESS_AGENT
6438 IP_AGENT_EXPORT
int ust_loaded
;
6439 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6443 /* Static tracepoints. */
6445 /* UST puts a "struct tracepoint" in the global namespace, which
6446 conflicts with our tracepoint. Arguably, being a library, it
6447 shouldn't take ownership of such a generic name. We work around it
6449 #define tracepoint ust_tracepoint
6450 #include <ust/ust.h>
6453 extern int serialize_to_text (char *outbuf
, int bufsize
,
6454 const char *fmt
, va_list ap
);
6456 #define GDB_PROBE_NAME "gdb"
6458 /* We dynamically search for the UST symbols instead of linking them
6459 in. This lets the user decide if the application uses static
6460 tracepoints, instead of always pulling libust.so in. This vector
6461 holds pointers to all functions we care about. */
6465 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6466 const char *fmt
, va_list ap
);
6468 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6469 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6471 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6473 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6476 void (*marker_iter_start
) (struct marker_iter
*iter
);
6477 void (*marker_iter_next
) (struct marker_iter
*iter
);
6478 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6479 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6484 /* Cast through typeof to catch incompatible API changes. Since UST
6485 only builds with gcc, we can freely use gcc extensions here
6487 #define GET_UST_SYM(SYM) \
6490 if (ust_ops.SYM == NULL) \
6491 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6492 if (ust_ops.SYM == NULL) \
6496 #define USTF(SYM) ust_ops.SYM
6498 /* Get pointers to all libust.so functions we care about. */
6503 GET_UST_SYM (serialize_to_text
);
6505 GET_UST_SYM (ltt_probe_register
);
6506 GET_UST_SYM (ltt_probe_unregister
);
6507 GET_UST_SYM (ltt_marker_connect
);
6508 GET_UST_SYM (ltt_marker_disconnect
);
6510 GET_UST_SYM (marker_iter_start
);
6511 GET_UST_SYM (marker_iter_next
);
6512 GET_UST_SYM (marker_iter_stop
);
6513 GET_UST_SYM (marker_iter_reset
);
6519 /* Given an UST marker, return the matching gdb static tracepoint.
6520 The match is done by address. */
6522 static struct tracepoint
*
6523 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6525 struct tracepoint
*tpoint
;
6527 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6529 if (tpoint
->type
!= static_tracepoint
)
6532 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6539 /* The probe function we install on lttng/ust markers. Whenever a
6540 probed ust marker is hit, this function is called. This is similar
6541 to gdb_collect, only for static tracepoints, instead of fast
6545 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6546 struct registers
*regs
, void *call_private
,
6547 const char *fmt
, va_list *args
)
6549 struct tracepoint
*tpoint
;
6550 struct static_tracepoint_ctx ctx
;
6552 /* Don't do anything until the trace run is completely set up. */
6555 trace_debug ("gdb_probe: not tracing\n");
6559 ctx
.base
.type
= static_tracepoint
;
6560 ctx
.regcache_initted
= 0;
6565 /* Wrap the regblock in a register cache (in the stack, we don't
6566 want to malloc here). */
6567 ctx
.regspace
= alloca (register_cache_size ());
6568 if (ctx
.regspace
== NULL
)
6570 trace_debug ("Trace buffer block allocation failed, skipping");
6574 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6577 trace_debug ("gdb_probe: marker not known: "
6578 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6579 mdata
->location
, mdata
->channel
,
6580 mdata
->name
, mdata
->format
);
6584 if (!tpoint
->enabled
)
6586 trace_debug ("gdb_probe: tracepoint disabled");
6590 ctx
.tpoint
= tpoint
;
6592 trace_debug ("gdb_probe: collecting marker: "
6593 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6594 mdata
->location
, mdata
->channel
,
6595 mdata
->name
, mdata
->format
);
6597 /* Test the condition if present, and collect if true. */
6598 if (tpoint
->cond
== NULL
6599 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6602 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6603 tpoint
->address
, tpoint
);
6605 if (stopping_tracepoint
6606 || trace_buffer_is_full
6607 || expr_eval_result
!= expr_eval_no_error
)
6612 /* If there was a condition and it evaluated to false, the only
6613 way we would stop tracing is if there was an error during
6614 condition expression evaluation. */
6615 if (expr_eval_result
!= expr_eval_no_error
)
6620 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6621 static tracepoint string data. This is a string passed to the
6622 tracing library by the user, at the time of the tracepoint marker
6623 call. E.g., in the UST marker call:
6625 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6627 the collected data is "str FOOBAZ".
6631 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6632 struct traceframe
*tframe
)
6634 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6635 unsigned char *bufspace
;
6638 unsigned short blocklen
;
6642 trace_debug ("Wanted to collect static trace data, "
6643 "but there's no static trace data");
6647 va_copy (copy
, *umd
->args
);
6648 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6651 trace_debug ("Want to collect ust data");
6653 /* 'S' + size + string */
6654 bufspace
= add_traceframe_block (tframe
,
6655 1 + sizeof (blocklen
) + size
+ 1);
6656 if (bufspace
== NULL
)
6658 trace_debug ("Trace buffer block allocation failed, skipping");
6662 /* Identify a static trace data block. */
6665 blocklen
= size
+ 1;
6666 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6668 va_copy (copy
, *umd
->args
);
6669 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6670 size
+ 1, umd
->fmt
, copy
);
6673 trace_debug ("Storing static tracepoint data in regblock: %s",
6674 bufspace
+ 1 + sizeof (blocklen
));
6677 /* The probe to register with lttng/ust. */
6678 static struct ltt_available_probe gdb_ust_probe
=
6685 #endif /* HAVE_UST */
6686 #endif /* IN_PROCESS_AGENT */
6688 #ifndef IN_PROCESS_AGENT
6690 /* Ask the in-process agent to run a command. Since we don't want to
6691 have to handle the IPA hitting breakpoints while running the
6692 command, we pause all threads, remove all breakpoints, and then set
6693 the helper thread re-running. We communicate with the helper
6694 thread by means of direct memory xfering, and a socket for
6698 run_inferior_command (char *cmd
, int len
)
6701 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6703 trace_debug ("run_inferior_command: running: %s", cmd
);
6706 uninsert_all_breakpoints ();
6708 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6710 reinsert_all_breakpoints ();
6716 #else /* !IN_PROCESS_AGENT */
6718 #include <sys/socket.h>
6721 #ifndef UNIX_PATH_MAX
6722 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6725 /* Where we put the socked used for synchronization. */
6726 #define SOCK_DIR P_tmpdir
6728 /* Thread ID of the helper thread. GDBserver reads this to know which
6729 is the help thread. This is an LWP id on Linux. */
6730 int helper_thread_id
;
6733 init_named_socket (const char *name
)
6736 struct sockaddr_un addr
;
6738 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6741 warning ("socket creation failed: %s", strerror (errno
));
6745 addr
.sun_family
= AF_UNIX
;
6747 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6748 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6750 result
= access (name
, F_OK
);
6754 result
= unlink (name
);
6757 warning ("unlink failed: %s", strerror (errno
));
6761 warning ("socket %s already exists; overwriting", name
);
6764 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6767 warning ("bind failed: %s", strerror (errno
));
6772 result
= listen (fd
, 1);
6775 warning ("listen: %s", strerror (errno
));
6783 static char agent_socket_name
[UNIX_PATH_MAX
];
6786 gdb_agent_socket_init (void)
6790 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6791 SOCK_DIR
, getpid ());
6792 if (result
>= UNIX_PATH_MAX
)
6794 trace_debug ("string overflow allocating socket name");
6798 fd
= init_named_socket (agent_socket_name
);
6800 warning ("Error initializing named socket (%s) for communication with the "
6801 "ust helper thread. Check that directory exists and that it "
6802 "is writable.", agent_socket_name
);
6809 /* The next marker to be returned on a qTsSTM command. */
6810 static const struct marker
*next_st
;
6812 /* Returns the first known marker. */
6817 struct marker_iter iter
;
6819 USTF(marker_iter_reset
) (&iter
);
6820 USTF(marker_iter_start
) (&iter
);
6825 /* Returns the marker following M. */
6827 const struct marker
*
6828 next_marker (const struct marker
*m
)
6830 struct marker_iter iter
;
6832 USTF(marker_iter_reset
) (&iter
);
6833 USTF(marker_iter_start
) (&iter
);
6835 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6837 if (iter
.marker
== m
)
6839 USTF(marker_iter_next
) (&iter
);
6847 /* Return an hexstr version of the STR C string, fit for sending to
6851 cstr_to_hexstr (const char *str
)
6853 int len
= strlen (str
);
6854 char *hexstr
= xmalloc (len
* 2 + 1);
6855 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6859 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6863 response_ust_marker (char *packet
, const struct marker
*st
)
6865 char *strid
, *format
, *tmp
;
6867 next_st
= next_marker (st
);
6869 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6870 strlen (st
->name
) + 1);
6871 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6873 strid
= cstr_to_hexstr (tmp
);
6876 format
= cstr_to_hexstr (st
->format
);
6878 sprintf (packet
, "m%s:%s:%s",
6879 paddress ((uintptr_t) st
->location
),
6887 /* Return the first static tracepoint, and initialize the state
6888 machine that will iterate through all the static tracepoints. */
6891 cmd_qtfstm (char *packet
)
6893 trace_debug ("Returning first trace state variable definition");
6895 if (first_marker ())
6896 response_ust_marker (packet
, first_marker ());
6898 strcpy (packet
, "l");
6901 /* Return additional trace state variable definitions. */
6904 cmd_qtsstm (char *packet
)
6906 trace_debug ("Returning static tracepoint");
6909 response_ust_marker (packet
, next_st
);
6911 strcpy (packet
, "l");
6914 /* Disconnect the GDB probe from a marker at a given address. */
6917 unprobe_marker_at (char *packet
)
6921 struct marker_iter iter
;
6923 p
+= sizeof ("unprobe_marker_at:") - 1;
6925 p
= unpack_varlen_hex (p
, &address
);
6927 USTF(marker_iter_reset
) (&iter
);
6928 USTF(marker_iter_start
) (&iter
);
6929 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6930 if ((uintptr_t ) iter
.marker
->location
== address
)
6934 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6938 warning ("could not disable marker %s/%s",
6939 iter
.marker
->channel
, iter
.marker
->name
);
6944 /* Connect the GDB probe to a marker at a given address. */
6947 probe_marker_at (char *packet
)
6951 struct marker_iter iter
;
6954 p
+= sizeof ("probe_marker_at:") - 1;
6956 p
= unpack_varlen_hex (p
, &address
);
6958 USTF(marker_iter_reset
) (&iter
);
6960 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6962 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6963 if ((uintptr_t ) m
->location
== address
)
6967 trace_debug ("found marker for address. "
6968 "ltt_marker_connect (marker = %s/%s)",
6969 m
->channel
, m
->name
);
6971 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6973 if (result
&& result
!= -EEXIST
)
6974 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6975 m
->channel
, m
->name
, -result
);
6979 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6980 m
->channel
, m
->name
);
6984 strcpy (packet
, "OK");
6988 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6993 cmd_qtstmat (char *packet
)
6997 struct marker_iter iter
;
7000 p
+= sizeof ("qTSTMat:") - 1;
7002 p
= unpack_varlen_hex (p
, &address
);
7004 USTF(marker_iter_reset
) (&iter
);
7006 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7008 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7009 if ((uintptr_t ) m
->location
== address
)
7011 response_ust_marker (packet
, m
);
7015 strcpy (packet
, "l");
7025 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7028 #endif /* HAVE_UST */
7030 #include <sys/syscall.h>
7034 gdb_agent_remove_socket (void)
7036 unlink (agent_socket_name
);
7039 /* Helper thread of agent. */
7042 gdb_agent_helper_thread (void *arg
)
7046 atexit (gdb_agent_remove_socket
);
7050 listen_fd
= gdb_agent_socket_init ();
7052 if (helper_thread_id
== 0)
7053 helper_thread_id
= syscall (SYS_gettid
);
7055 if (listen_fd
== -1)
7057 warning ("could not create sync socket\n");
7064 struct sockaddr_un sockaddr
;
7070 tmp
= sizeof (sockaddr
);
7074 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7076 /* It seems an ERESTARTSYS can escape out of accept. */
7077 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7081 warning ("Accept returned %d, error: %s\n",
7082 fd
, strerror (errno
));
7088 ret
= read (fd
, buf
, 1);
7089 } while (ret
== -1 && errno
== EINTR
);
7093 warning ("reading socket (fd=%d) failed with %s",
7094 fd
, strerror (errno
));
7101 if (strncmp ("close", cmd_buf
, 5) == 0)
7106 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7108 cmd_qtfstm (cmd_buf
);
7110 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7112 cmd_qtsstm (cmd_buf
);
7114 else if (strncmp ("unprobe_marker_at:",
7116 sizeof ("unprobe_marker_at:") - 1) == 0)
7118 unprobe_marker_at (cmd_buf
);
7120 else if (strncmp ("probe_marker_at:",
7122 sizeof ("probe_marker_at:") - 1) == 0)
7124 probe_marker_at (cmd_buf
);
7126 else if (strncmp ("qTSTMat:",
7128 sizeof ("qTSTMat:") - 1) == 0)
7130 cmd_qtstmat (cmd_buf
);
7132 #endif /* HAVE_UST */
7135 /* Fix compiler's warning: ignoring return value of 'write'. */
7136 ret
= write (fd
, buf
, 1);
7142 unlink (agent_socket_name
);
7144 /* Sleep endlessly to wait the whole inferior stops. This
7145 thread can not exit because GDB or GDBserver may still need
7146 'current_inferior' (representing this thread) to access
7147 inferior memory. Otherwise, this thread exits earlier than
7148 other threads, and 'current_inferior' is set to NULL. */
7159 #include <pthread.h>
7161 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7164 gdb_agent_init (void)
7171 /* We want the helper thread to be as transparent as possible, so
7172 have it inherit an all-signals-blocked mask. */
7174 sigfillset (&new_mask
);
7175 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7177 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7179 res
= pthread_create (&thread
,
7181 gdb_agent_helper_thread
,
7184 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7186 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7188 while (helper_thread_id
== 0)
7196 #include <sys/mman.h>
7199 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7200 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7201 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7202 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7203 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7204 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7206 /* Record the result of getting buffer space for fast tracepoint
7207 trampolines. Any error message is copied, since caller may not be
7208 using persistent storage. */
7211 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7213 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7214 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7216 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7218 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7221 static void __attribute__ ((constructor
))
7222 initialize_tracepoint_ftlib (void)
7224 initialize_tracepoint ();
7229 #endif /* IN_PROCESS_AGENT */
7231 /* Return a timestamp, expressed as microseconds of the usual Unix
7232 time. (As the result is a 64-bit number, it will not overflow any
7236 get_timestamp (void)
7240 if (gettimeofday (&tv
, 0) != 0)
7243 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7247 initialize_tracepoint (void)
7249 /* There currently no way to change the buffer size. */
7250 const int sizeOfBuffer
= 5 * 1024 * 1024;
7251 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7252 init_trace_buffer (buf
, sizeOfBuffer
);
7254 /* Wire trace state variable 1 to be the timestamp. This will be
7255 uploaded to GDB upon connection and become one of its trace state
7256 variables. (In case you're wondering, if GDB already has a trace
7257 variable numbered 1, it will be renumbered.) */
7258 create_trace_state_variable (1, 0);
7259 set_trace_state_variable_name (1, "trace_timestamp");
7260 set_trace_state_variable_getter (1, get_timestamp
);
7262 #ifdef IN_PROCESS_AGENT
7267 pagesize
= sysconf (_SC_PAGE_SIZE
);
7271 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7273 #define SCRATCH_BUFFER_NPAGES 20
7275 /* Allocate scratch buffer aligned on a page boundary, at a low
7276 address (close to the main executable's code). */
7277 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7279 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7280 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7281 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7283 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7289 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7292 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7295 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7297 /* It's not a fatal error for something to go wrong with trampoline
7298 buffer setup, but it can be mysterious, so create a channel to
7299 report back on what went wrong, using a fixed size since we may
7300 not be able to allocate space later when the problem occurs. */
7301 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7303 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7305 initialize_low_tracepoint ();