1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2014 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 "tracepoint.h"
21 #include "gdbthread.h"
36 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
38 /* This file is built for both GDBserver, and the in-process
39 agent (IPA), a shared library that includes a tracing agent that is
40 loaded by the inferior to support fast tracepoints. Fast
41 tracepoints (or more accurately, jump based tracepoints) are
42 implemented by patching the tracepoint location with a jump into a
43 small trampoline function whose job is to save the register state,
44 call the in-process tracing agent, and then execute the original
45 instruction that was under the tracepoint jump (possibly adjusted,
46 if PC-relative, or some such).
48 The current synchronization design is pull based. That means,
49 GDBserver does most of the work, by peeking/poking at the inferior
50 agent's memory directly for downloading tracepoint and associated
51 objects, and for uploading trace frames. Whenever the IPA needs
52 something from GDBserver (trace buffer is full, tracing stopped for
53 some reason, etc.) the IPA calls a corresponding hook function
54 where GDBserver has placed a breakpoint.
56 Each of the agents has its own trace buffer. When browsing the
57 trace frames built from slow and fast tracepoints from GDB (tfind
58 mode), there's no guarantee the user is seeing the trace frames in
59 strict chronological creation order, although, GDBserver tries to
60 keep the order relatively reasonable, by syncing the trace buffers
65 #ifdef IN_PROCESS_AGENT
67 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
70 trace_vdebug (const char *fmt
, ...)
76 vsprintf (buf
, fmt
, ap
);
77 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
81 #define trace_debug_1(level, fmt, args...) \
83 if (level <= debug_threads) \
84 trace_vdebug ((fmt), ##args); \
89 #define trace_debug_1(level, fmt, args...) \
91 if (level <= debug_threads) \
93 debug_printf ((fmt), ##args); \
94 debug_printf ("\n"); \
100 #define trace_debug(FMT, args...) \
101 trace_debug_1 (1, FMT, ##args)
103 #if defined(__GNUC__)
104 # define ATTR_USED __attribute__((used))
105 # define ATTR_NOINLINE __attribute__((noinline))
106 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
109 # define ATTR_NOINLINE
110 # define ATTR_CONSTRUCTOR
113 /* Make sure the functions the IPA needs to export (symbols GDBserver
114 needs to query GDB about) are exported. */
116 #ifdef IN_PROCESS_AGENT
117 # if defined _WIN32 || defined __CYGWIN__
118 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
121 # define IP_AGENT_EXPORT \
122 __attribute__ ((visibility("default"))) ATTR_USED
124 # define IP_AGENT_EXPORT ATTR_USED
128 # define IP_AGENT_EXPORT
131 /* Prefix exported symbols, for good citizenship. All the symbols
132 that need exporting are defined in this module. */
133 #ifdef IN_PROCESS_AGENT
134 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
135 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
136 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
137 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
138 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
139 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
140 # define collecting gdb_agent_collecting
141 # define gdb_collect gdb_agent_gdb_collect
142 # define stop_tracing gdb_agent_stop_tracing
143 # define flush_trace_buffer gdb_agent_flush_trace_buffer
144 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
145 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
146 # define stopping_tracepoint gdb_agent_stopping_tracepoint
147 # define expr_eval_result gdb_agent_expr_eval_result
148 # define error_tracepoint gdb_agent_error_tracepoint
149 # define tracepoints gdb_agent_tracepoints
150 # define tracing gdb_agent_tracing
151 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
152 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
153 # define trace_buffer_lo gdb_agent_trace_buffer_lo
154 # define trace_buffer_hi gdb_agent_trace_buffer_hi
155 # define traceframe_read_count gdb_agent_traceframe_read_count
156 # define traceframe_write_count gdb_agent_traceframe_write_count
157 # define traceframes_created gdb_agent_traceframes_created
158 # define trace_state_variables gdb_agent_trace_state_variables
159 # define get_raw_reg gdb_agent_get_raw_reg
160 # define get_trace_state_variable_value \
161 gdb_agent_get_trace_state_variable_value
162 # define set_trace_state_variable_value \
163 gdb_agent_set_trace_state_variable_value
164 # define ust_loaded gdb_agent_ust_loaded
165 # define helper_thread_id gdb_agent_helper_thread_id
166 # define cmd_buf gdb_agent_cmd_buf
169 #ifndef IN_PROCESS_AGENT
171 /* Addresses of in-process agent's symbols GDBserver cares about. */
173 struct ipa_sym_addresses
175 CORE_ADDR addr_gdb_tp_heap_buffer
;
176 CORE_ADDR addr_gdb_jump_pad_buffer
;
177 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
178 CORE_ADDR addr_gdb_trampoline_buffer
;
179 CORE_ADDR addr_gdb_trampoline_buffer_end
;
180 CORE_ADDR addr_gdb_trampoline_buffer_error
;
181 CORE_ADDR addr_collecting
;
182 CORE_ADDR addr_gdb_collect
;
183 CORE_ADDR addr_stop_tracing
;
184 CORE_ADDR addr_flush_trace_buffer
;
185 CORE_ADDR addr_about_to_request_buffer_space
;
186 CORE_ADDR addr_trace_buffer_is_full
;
187 CORE_ADDR addr_stopping_tracepoint
;
188 CORE_ADDR addr_expr_eval_result
;
189 CORE_ADDR addr_error_tracepoint
;
190 CORE_ADDR addr_tracepoints
;
191 CORE_ADDR addr_tracing
;
192 CORE_ADDR addr_trace_buffer_ctrl
;
193 CORE_ADDR addr_trace_buffer_ctrl_curr
;
194 CORE_ADDR addr_trace_buffer_lo
;
195 CORE_ADDR addr_trace_buffer_hi
;
196 CORE_ADDR addr_traceframe_read_count
;
197 CORE_ADDR addr_traceframe_write_count
;
198 CORE_ADDR addr_traceframes_created
;
199 CORE_ADDR addr_trace_state_variables
;
200 CORE_ADDR addr_get_raw_reg
;
201 CORE_ADDR addr_get_trace_state_variable_value
;
202 CORE_ADDR addr_set_trace_state_variable_value
;
203 CORE_ADDR addr_ust_loaded
;
212 IPA_SYM(gdb_tp_heap_buffer
),
213 IPA_SYM(gdb_jump_pad_buffer
),
214 IPA_SYM(gdb_jump_pad_buffer_end
),
215 IPA_SYM(gdb_trampoline_buffer
),
216 IPA_SYM(gdb_trampoline_buffer_end
),
217 IPA_SYM(gdb_trampoline_buffer_error
),
219 IPA_SYM(gdb_collect
),
220 IPA_SYM(stop_tracing
),
221 IPA_SYM(flush_trace_buffer
),
222 IPA_SYM(about_to_request_buffer_space
),
223 IPA_SYM(trace_buffer_is_full
),
224 IPA_SYM(stopping_tracepoint
),
225 IPA_SYM(expr_eval_result
),
226 IPA_SYM(error_tracepoint
),
227 IPA_SYM(tracepoints
),
229 IPA_SYM(trace_buffer_ctrl
),
230 IPA_SYM(trace_buffer_ctrl_curr
),
231 IPA_SYM(trace_buffer_lo
),
232 IPA_SYM(trace_buffer_hi
),
233 IPA_SYM(traceframe_read_count
),
234 IPA_SYM(traceframe_write_count
),
235 IPA_SYM(traceframes_created
),
236 IPA_SYM(trace_state_variables
),
237 IPA_SYM(get_raw_reg
),
238 IPA_SYM(get_trace_state_variable_value
),
239 IPA_SYM(set_trace_state_variable_value
),
243 static struct ipa_sym_addresses ipa_sym_addrs
;
245 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
247 /* Returns true if both the in-process agent library and the static
248 tracepoints libraries are loaded in the inferior, and agent has
249 capability on static tracepoints. */
252 in_process_agent_supports_ust (void)
256 if (!agent_loaded_p ())
258 warning ("In-process agent not loaded");
262 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
264 /* Agent understands static tracepoint, then check whether UST is in
265 fact loaded in the inferior. */
266 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
268 warning ("Error reading ust_loaded in lib");
279 write_e_ipa_not_loaded (char *buffer
)
282 "E.In-process agent library not loaded in process. "
283 "Fast and static tracepoints unavailable.");
286 /* Write an error to BUFFER indicating that UST isn't loaded in the
290 write_e_ust_not_loaded (char *buffer
)
294 "E.UST library not loaded in process. "
295 "Static tracepoints unavailable.");
297 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
301 /* If the in-process agent library isn't loaded in the inferior, write
302 an error to BUFFER, and return 1. Otherwise, return 0. */
305 maybe_write_ipa_not_loaded (char *buffer
)
307 if (!agent_loaded_p ())
309 write_e_ipa_not_loaded (buffer
);
315 /* If the in-process agent library and the ust (static tracepoints)
316 library aren't loaded in the inferior, write an error to BUFFER,
317 and return 1. Otherwise, return 0. */
320 maybe_write_ipa_ust_not_loaded (char *buffer
)
322 if (!agent_loaded_p ())
324 write_e_ipa_not_loaded (buffer
);
327 else if (!in_process_agent_supports_ust ())
329 write_e_ust_not_loaded (buffer
);
335 /* Cache all future symbols that the tracepoints module might request.
336 We can not request symbols at arbitrary states in the remote
337 protocol, only when the client tells us that new symbols are
338 available. So when we load the in-process library, make sure to
339 check the entire list. */
342 tracepoint_look_up_symbols (void)
346 if (agent_loaded_p ())
349 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
352 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
354 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
357 debug_printf ("symbol `%s' not found\n", symbol_list
[i
].name
);
362 agent_look_up_symbols (NULL
);
367 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
368 of the "stop_tracing" function. When this breakpoint is hit,
369 tracing stopped in the IPA for some reason. E.g., due to
370 tracepoint reaching the pass count, hitting conditional expression
371 evaluation error, etc.
373 The IPA's trace buffer is never in circular tracing mode: instead,
374 GDBserver's is, and whenever the in-process buffer fills, it calls
375 "flush_trace_buffer", which triggers an internal breakpoint.
376 GDBserver reacts to this breakpoint by pulling the meanwhile
377 collected data. Old frames discarding is always handled on the
380 #ifdef IN_PROCESS_AGENT
382 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
384 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
388 /* Call this in the functions where GDBserver places a breakpoint, so
389 that the compiler doesn't try to be clever and skip calling the
390 function at all. This is necessary, even if we tell the compiler
391 to not inline said functions. */
393 #if defined(__GNUC__)
394 # define UNKNOWN_SIDE_EFFECTS() asm ("")
396 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
399 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
402 /* GDBserver places breakpoint here. */
403 UNKNOWN_SIDE_EFFECTS();
406 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
407 flush_trace_buffer (void)
409 /* GDBserver places breakpoint here. */
410 UNKNOWN_SIDE_EFFECTS();
415 #ifndef IN_PROCESS_AGENT
417 tracepoint_handler (CORE_ADDR address
)
419 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
424 /* Breakpoint at "stop_tracing" in the inferior lib. */
425 struct breakpoint
*stop_tracing_bkpt
;
426 static int stop_tracing_handler (CORE_ADDR
);
428 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
429 struct breakpoint
*flush_trace_buffer_bkpt
;
430 static int flush_trace_buffer_handler (CORE_ADDR
);
432 static void download_trace_state_variables (void);
433 static void upload_fast_traceframes (void);
435 static int run_inferior_command (char *cmd
, int len
);
438 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
440 return read_inferior_memory (symaddr
, (unsigned char *) val
,
445 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
448 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
450 return read_inferior_memory (symaddr
, (unsigned char *) val
,
455 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
457 void *pval
= (void *) (uintptr_t) val
;
460 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
461 *val
= (uintptr_t) pval
;
466 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
468 void *pval
= (void *) (uintptr_t) val
;
469 return write_inferior_memory (symaddr
,
470 (unsigned char *) &pval
, sizeof (pval
));
474 write_inferior_integer (CORE_ADDR symaddr
, int val
)
476 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
480 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
482 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
485 static CORE_ADDR
target_malloc (ULONGEST size
);
486 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
488 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
490 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
491 BUF += sizeof ((OBJ)->FIELD); \
496 /* Operations on various types of tracepoint actions. */
498 struct tracepoint_action
;
500 struct tracepoint_action_ops
502 /* Download tracepoint action ACTION to IPA. Return the address of action
504 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
506 /* Send ACTION to agent via command buffer started from BUFFER. Return
507 updated head of command buffer. */
508 char* (*send
) (char *buffer
, const struct tracepoint_action
*action
);
511 /* Base action. Concrete actions inherit this. */
513 struct tracepoint_action
515 #ifndef IN_PROCESS_AGENT
516 const struct tracepoint_action_ops
*ops
;
521 /* An 'M' (collect memory) action. */
522 struct collect_memory_action
524 struct tracepoint_action base
;
531 /* An 'R' (collect registers) action. */
533 struct collect_registers_action
535 struct tracepoint_action base
;
538 /* An 'X' (evaluate expression) action. */
540 struct eval_expr_action
542 struct tracepoint_action base
;
544 struct agent_expr
*expr
;
547 /* An 'L' (collect static trace data) action. */
548 struct collect_static_trace_data_action
550 struct tracepoint_action base
;
553 #ifndef IN_PROCESS_AGENT
555 m_tracepoint_action_download (const struct tracepoint_action
*action
)
557 int size_in_ipa
= (sizeof (struct collect_memory_action
)
558 - offsetof (struct tracepoint_action
, type
));
559 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
561 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
567 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
569 struct collect_memory_action
*maction
570 = (struct collect_memory_action
*) action
;
572 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
573 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
574 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
579 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
581 m_tracepoint_action_download
,
582 m_tracepoint_action_send
,
586 r_tracepoint_action_download (const struct tracepoint_action
*action
)
588 int size_in_ipa
= (sizeof (struct collect_registers_action
)
589 - offsetof (struct tracepoint_action
, type
));
590 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
592 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
599 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
604 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
606 r_tracepoint_action_download
,
607 r_tracepoint_action_send
,
610 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
613 x_tracepoint_action_download (const struct tracepoint_action
*action
)
615 int size_in_ipa
= (sizeof (struct eval_expr_action
)
616 - offsetof (struct tracepoint_action
, type
));
617 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
620 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
622 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
623 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
624 - offsetof (struct tracepoint_action
, type
),
630 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
631 copy 0 to P. Return updated header of buffer. */
634 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
636 /* Copy the length of condition first, and then copy its
645 memcpy (p
, &aexpr
->length
, 4);
648 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
655 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
657 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
659 return agent_expr_send (buffer
, eaction
->expr
);
662 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
664 x_tracepoint_action_download
,
665 x_tracepoint_action_send
,
669 l_tracepoint_action_download (const struct tracepoint_action
*action
)
671 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
672 - offsetof (struct tracepoint_action
, type
));
673 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
675 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
682 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
687 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
689 l_tracepoint_action_download
,
690 l_tracepoint_action_send
,
694 /* This structure describes a piece of the source-level definition of
695 the tracepoint. The contents are not interpreted by the target,
696 but preserved verbatim for uploading upon reconnection. */
700 /* The type of string, such as "cond" for a conditional. */
703 /* The source-level string itself. For the sake of target
704 debugging, we store it in plaintext, even though it is always
705 transmitted in hex. */
708 /* Link to the next one in the list. We link them in the order
709 received, in case some make up an ordered list of commands or
711 struct source_string
*next
;
716 /* Trap based tracepoint. */
719 /* A fast tracepoint implemented with a jump instead of a trap. */
722 /* A static tracepoint, implemented by a program call into a tracing
727 struct tracepoint_hit_ctx
;
729 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
732 /* The definition of a tracepoint. */
734 /* Tracepoints may have multiple locations, each at a different
735 address. This can occur with optimizations, template
736 instantiation, etc. Since the locations may be in different
737 scopes, the conditions and actions may be different for each
738 location. Our target version of tracepoints is more like GDB's
739 notion of "breakpoint locations", but we have almost nothing that
740 is not per-location, so we bother having two kinds of objects. The
741 key consequence is that numbers are not unique, and that it takes
742 both number and address to identify a tracepoint uniquely. */
746 /* The number of the tracepoint, as specified by GDB. Several
747 tracepoint objects here may share a number. */
750 /* Address at which the tracepoint is supposed to trigger. Several
751 tracepoints may share an address. */
754 /* Tracepoint type. */
755 enum tracepoint_type type
;
757 /* True if the tracepoint is currently enabled. */
760 /* The number of single steps that will be performed after each
764 /* The number of times the tracepoint may be hit before it will
765 terminate the entire tracing run. */
768 /* Pointer to the agent expression that is the tracepoint's
769 conditional, or NULL if the tracepoint is unconditional. */
770 struct agent_expr
*cond
;
772 /* The list of actions to take when the tracepoint triggers. */
774 struct tracepoint_action
**actions
;
776 /* Count of the times we've hit this tracepoint during the run.
777 Note that while-stepping steps are not counted as "hits". */
780 /* Cached sum of the sizes of traceframes created by this point. */
781 uint64_t traceframe_usage
;
783 CORE_ADDR compiled_cond
;
785 /* Link to the next tracepoint in the list. */
786 struct tracepoint
*next
;
788 #ifndef IN_PROCESS_AGENT
789 /* The list of actions to take when the tracepoint triggers, in
790 string/packet form. */
793 /* The collection of strings that describe the tracepoint as it was
794 entered into GDB. These are not used by the target, but are
795 reported back to GDB upon reconnection. */
796 struct source_string
*source_strings
;
798 /* The number of bytes displaced by fast tracepoints. It may subsume
799 multiple instructions, for multi-byte fast tracepoints. This
800 field is only valid for fast tracepoints. */
803 /* Only for fast tracepoints. */
804 CORE_ADDR obj_addr_on_target
;
806 /* Address range where the original instruction under a fast
807 tracepoint was relocated to. (_end is actually one byte past
809 CORE_ADDR adjusted_insn_addr
;
810 CORE_ADDR adjusted_insn_addr_end
;
812 /* The address range of the piece of the jump pad buffer that was
813 assigned to this fast tracepoint. (_end is actually one byte
816 CORE_ADDR jump_pad_end
;
818 /* The address range of the piece of the trampoline buffer that was
819 assigned to this fast tracepoint. (_end is actually one byte
821 CORE_ADDR trampoline
;
822 CORE_ADDR trampoline_end
;
824 /* The list of actions to take while in a stepping loop. These
825 fields are only valid for patch-based tracepoints. */
826 int num_step_actions
;
827 struct tracepoint_action
**step_actions
;
828 /* Same, but in string/packet form. */
829 char **step_actions_str
;
831 /* Handle returned by the breakpoint or tracepoint module when we
832 inserted the trap or jump, or hooked into a static tracepoint.
833 NULL if we haven't inserted it yet. */
839 #ifndef IN_PROCESS_AGENT
841 /* Given `while-stepping', a thread may be collecting data for more
842 than one tracepoint simultaneously. On the other hand, the same
843 tracepoint with a while-stepping action may be hit by more than one
844 thread simultaneously (but not quite, each thread could be handling
845 a different step). Each thread holds a list of these objects,
846 representing the current step of each while-stepping action being
851 struct wstep_state
*next
;
853 /* The tracepoint number. */
855 /* The tracepoint's address. */
856 CORE_ADDR tp_address
;
858 /* The number of the current step in this 'while-stepping'
865 /* The linked list of all tracepoints. Marked explicitly as used as
866 the in-process library doesn't use it for the fast tracepoints
868 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
870 #ifndef IN_PROCESS_AGENT
872 /* Pointer to the last tracepoint in the list, new tracepoints are
873 linked in at the end. */
875 static struct tracepoint
*last_tracepoint
;
878 /* The first tracepoint to exceed its pass count. */
880 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
882 /* True if the trace buffer is full or otherwise no longer usable. */
884 IP_AGENT_EXPORT
int trace_buffer_is_full
;
886 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
888 #ifndef IN_PROCESS_AGENT
890 static const char *eval_result_names
[] =
892 "terror:in the attic", /* this should never be reported */
893 "terror:empty expression",
894 "terror:empty stack",
895 "terror:stack overflow",
896 "terror:stack underflow",
897 "terror:unhandled opcode",
898 "terror:unrecognized opcode",
899 "terror:divide by zero"
904 /* The tracepoint in which the error occurred. */
906 static struct tracepoint
*error_tracepoint
;
908 struct trace_state_variable
910 /* This is the name of the variable as used in GDB. The target
911 doesn't use the name, but needs to have it for saving and
912 reconnection purposes. */
915 /* This number identifies the variable uniquely. Numbers may be
916 assigned either by the target (in the case of builtin variables),
917 or by GDB, and are presumed unique during the course of a trace
921 /* The variable's initial value, a 64-bit signed integer always. */
922 LONGEST initial_value
;
924 /* The variable's value, a 64-bit signed integer always. */
927 /* Pointer to a getter function, used to supply computed values. */
928 LONGEST (*getter
) (void);
930 /* Link to the next variable. */
931 struct trace_state_variable
*next
;
934 /* Linked list of all trace state variables. */
936 #ifdef IN_PROCESS_AGENT
937 struct trace_state_variable
*alloced_trace_state_variables
;
940 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
942 /* The results of tracing go into a fixed-size space known as the
943 "trace buffer". Because usage follows a limited number of
944 patterns, we manage it ourselves rather than with malloc. Basic
945 rules are that we create only one trace frame at a time, each is
946 variable in size, they are never moved once created, and we only
947 discard if we are doing a circular buffer, and then only the oldest
948 ones. Each trace frame includes its own size, so we don't need to
949 link them together, and the trace frame number is relative to the
950 first one, so we don't need to record numbers. A trace frame also
951 records the number of the tracepoint that created it. The data
952 itself is a series of blocks, each introduced by a single character
953 and with a defined format. Each type of block has enough
954 type/length info to allow scanners to jump quickly from one block
955 to the next without reading each byte in the block. */
957 /* Trace buffer management would be simple - advance a free pointer
958 from beginning to end, then stop - were it not for the circular
959 buffer option, which is a useful way to prevent a trace run from
960 stopping prematurely because the buffer filled up. In the circular
961 case, the location of the first trace frame (trace_buffer_start)
962 moves as old trace frames are discarded. Also, since we grow trace
963 frames incrementally as actions are performed, we wrap around to
964 the beginning of the trace buffer. This is per-block, so each
965 block within a trace frame remains contiguous. Things get messy
966 when the wrapped-around trace frame is the one being discarded; the
967 free space ends up in two parts at opposite ends of the buffer. */
970 # if defined(__GNUC__)
971 # define ATTR_PACKED __attribute__ ((packed))
973 # define ATTR_PACKED /* nothing */
977 /* The data collected at a tracepoint hit. This object should be as
978 small as possible, since there may be a great many of them. We do
979 not need to keep a frame number, because they are all sequential
980 and there are no deletions; so the Nth frame in the buffer is
981 always frame number N. */
985 /* Number of the tracepoint that collected this traceframe. A value
986 of 0 indicates the current end of the trace buffer. We make this
987 a 16-bit field because it's never going to happen that GDB's
988 numbering of tracepoints reaches 32,000. */
991 /* The size of the data in this trace frame. We limit this to 32
992 bits, even on a 64-bit target, because it's just implausible that
993 one is validly going to collect 4 gigabytes of data at a single
995 unsigned int data_size
: 32;
997 /* The base of the trace data, which is contiguous from this point. */
998 unsigned char data
[0];
1002 /* The size of the EOB marker, in bytes. A traceframe with zeroed
1003 fields (and no data) marks the end of trace data. */
1004 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
1006 /* The traceframe to be used as the source of data to send back to
1007 GDB. A value of -1 means to get data from the live program. */
1009 int current_traceframe
= -1;
1011 /* This flag is true if the trace buffer is circular, meaning that
1012 when it fills, the oldest trace frames are discarded in order to
1015 #ifndef IN_PROCESS_AGENT
1016 static int circular_trace_buffer
;
1019 /* Size of the trace buffer. */
1021 static LONGEST trace_buffer_size
;
1023 /* Pointer to the block of memory that traceframes all go into. */
1025 static unsigned char *trace_buffer_lo
;
1027 /* Pointer to the end of the trace buffer, more precisely to the byte
1028 after the end of the buffer. */
1030 static unsigned char *trace_buffer_hi
;
1032 /* Control structure holding the read/write/etc. pointers into the
1033 trace buffer. We need more than one of these to implement a
1034 transaction-like mechanism to garantees that both GDBserver and the
1035 in-process agent can try to change the trace buffer
1038 struct trace_buffer_control
1040 /* Pointer to the first trace frame in the buffer. In the
1041 non-circular case, this is equal to trace_buffer_lo, otherwise it
1042 moves around in the buffer. */
1043 unsigned char *start
;
1045 /* Pointer to the free part of the trace buffer. Note that we clear
1046 several bytes at and after this pointer, so that traceframe
1047 scans/searches terminate properly. */
1048 unsigned char *free
;
1050 /* Pointer to the byte after the end of the free part. Note that
1051 this may be smaller than trace_buffer_free in the circular case,
1052 and means that the free part is in two pieces. Initially it is
1053 equal to trace_buffer_hi, then is generally equivalent to
1054 trace_buffer_start. */
1055 unsigned char *end_free
;
1057 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1058 this is the point at which the trace data breaks, and resumes at
1060 unsigned char *wrap
;
1063 /* Same as above, to be used by GDBserver when updating the in-process
1065 struct ipa_trace_buffer_control
1074 /* We have possibly both GDBserver and an inferior thread accessing
1075 the same IPA trace buffer memory. The IPA is the producer (tries
1076 to put new frames in the buffer), while GDBserver occasionally
1077 consumes them, that is, flushes the IPA's buffer into its own
1078 buffer. Both sides need to update the trace buffer control
1079 pointers (current head, tail, etc.). We can't use a global lock to
1080 synchronize the accesses, as otherwise we could deadlock GDBserver
1081 (if the thread holding the lock stops for a signal, say). So
1082 instead of that, we use a transaction scheme where GDBserver writes
1083 always prevail over the IPAs writes, and, we have the IPA detect
1084 the commit failure/overwrite, and retry the whole attempt. This is
1085 mainly implemented by having a global token object that represents
1086 who wrote last to the buffer control structure. We need to freeze
1087 any inferior writing to the buffer while GDBserver touches memory,
1088 so that the inferior can correctly detect that GDBserver had been
1089 there, otherwise, it could mistakingly think its commit was
1090 successful; that's implemented by simply having GDBserver set a
1091 breakpoint the inferior hits if it is the critical region.
1093 There are three cycling trace buffer control structure copies
1094 (buffer head, tail, etc.), with the token object including an index
1095 indicating which is current live copy. The IPA tentatively builds
1096 an updated copy in a non-current control structure, while GDBserver
1097 always clobbers the current version directly. The IPA then tries
1098 to atomically "commit" its version; if GDBserver clobbered the
1099 structure meanwhile, that will fail, and the IPA restarts the
1102 Listing the step in further detail, we have:
1104 In-process agent (producer):
1106 - passes by `about_to_request_buffer_space' breakpoint/lock
1108 - reads current token, extracts current trace buffer control index,
1109 and starts tentatively updating the rightmost one (0->1, 1->2,
1110 2->0). Note that only one inferior thread is executing this code
1111 at any given time, due to an outer lock in the jump pads.
1113 - updates counters, and tries to commit the token.
1115 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1116 leaving the sync region.
1118 - checks if the update was effective.
1120 - if trace buffer was found full, hits flush_trace_buffer
1121 breakpoint, and restarts later afterwards.
1123 GDBserver (consumer):
1125 - sets `about_to_request_buffer_space' breakpoint/lock.
1127 - updates the token unconditionally, using the current buffer
1128 control index, since it knows that the IP agent always writes to
1129 the rightmost, and due to the breakpoint, at most one IP thread
1130 can try to update the trace buffer concurrently to GDBserver, so
1131 there will be no danger of trace buffer control index wrap making
1132 the IPA write to the same index as GDBserver.
1134 - flushes the IP agent's trace buffer completely, and updates the
1135 current trace buffer control structure. GDBserver *always* wins.
1137 - removes the `about_to_request_buffer_space' breakpoint.
1139 The token is stored in the `trace_buffer_ctrl_curr' variable.
1140 Internally, it's bits are defined as:
1142 |-------------+-----+-------------+--------+-------------+--------------|
1143 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1144 |-------------+-----+-------------+--------+-------------+--------------|
1145 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1146 |-------------+-----+-------------+--------+-------------+--------------|
1148 GSB - GDBserver Stamp Bit
1149 PC - Previous Counter
1150 CC - Current Counter
1151 TBCI - Trace Buffer Control Index
1154 An IPA update of `trace_buffer_ctrl_curr' does:
1156 - read CC from the current token, save as PC.
1158 - atomically tries to write PC+1,CC
1160 A GDBserver update of `trace_buffer_ctrl_curr' does:
1162 - reads PC and CC from the current token.
1167 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1168 for the counters described below. The cleared bits are used to
1169 hold the index of the items of the `trace_buffer_ctrl' array that
1171 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1173 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1174 counter, and the `current' counter. */
1176 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1177 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1179 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1180 always stamps this bit as set. */
1181 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1183 #ifdef IN_PROCESS_AGENT
1184 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1185 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1187 # define TRACE_BUFFER_CTRL_CURR \
1188 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1192 /* The GDBserver side agent only needs one instance of this object, as
1193 it doesn't need to sync with itself. Define it as array anyway so
1194 that the rest of the code base doesn't need to care for the
1196 struct trace_buffer_control trace_buffer_ctrl
[1];
1197 # define TRACE_BUFFER_CTRL_CURR 0
1200 /* These are convenience macros used to access the current trace
1201 buffer control in effect. */
1202 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1203 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1204 #define trace_buffer_end_free \
1205 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1206 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1209 /* Macro that returns a pointer to the first traceframe in the buffer. */
1211 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1213 /* Macro that returns a pointer to the next traceframe in the buffer.
1214 If the computed location is beyond the wraparound point, subtract
1215 the offset of the wraparound. */
1217 #define NEXT_TRACEFRAME_1(TF) \
1218 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1220 #define NEXT_TRACEFRAME(TF) \
1221 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1222 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1223 ? (trace_buffer_wrap - trace_buffer_lo) \
1226 /* The difference between these counters represents the total number
1227 of complete traceframes present in the trace buffer. The IP agent
1228 writes to the write count, GDBserver writes to read count. */
1230 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1231 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1233 /* Convenience macro. */
1235 #define traceframe_count \
1236 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1238 /* The count of all traceframes created in the current run, including
1239 ones that were discarded to make room. */
1241 IP_AGENT_EXPORT
int traceframes_created
;
1243 #ifndef IN_PROCESS_AGENT
1245 /* Read-only regions are address ranges whose contents don't change,
1246 and so can be read from target memory even while looking at a trace
1247 frame. Without these, disassembly for instance will likely fail,
1248 because the program code is not usually collected into a trace
1249 frame. This data structure does not need to be very complicated or
1250 particularly efficient, it's only going to be used occasionally,
1251 and only by some commands. */
1253 struct readonly_region
1255 /* The bounds of the region. */
1256 CORE_ADDR start
, end
;
1258 /* Link to the next one. */
1259 struct readonly_region
*next
;
1262 /* Linked list of readonly regions. This list stays in effect from
1263 one tstart to the next. */
1265 static struct readonly_region
*readonly_regions
;
1269 /* The global that controls tracing overall. */
1271 IP_AGENT_EXPORT
int tracing
;
1273 #ifndef IN_PROCESS_AGENT
1275 /* Controls whether tracing should continue after GDB disconnects. */
1277 int disconnected_tracing
;
1279 /* The reason for the last tracing run to have stopped. We initialize
1280 to a distinct string so that GDB can distinguish between "stopped
1281 after running" and "stopped because never run" cases. */
1283 static const char *tracing_stop_reason
= "tnotrun";
1285 static int tracing_stop_tpnum
;
1287 /* 64-bit timestamps for the trace run's start and finish, expressed
1288 in microseconds from the Unix epoch. */
1290 LONGEST tracing_start_time
;
1291 LONGEST tracing_stop_time
;
1293 /* The (optional) user-supplied name of the user that started the run.
1294 This is an arbitrary string, and may be NULL. */
1296 char *tracing_user_name
;
1298 /* Optional user-supplied text describing the run. This is
1299 an arbitrary string, and may be NULL. */
1301 char *tracing_notes
;
1303 /* Optional user-supplied text explaining a tstop command. This is an
1304 arbitrary string, and may be NULL. */
1306 char *tracing_stop_note
;
1310 /* Functions local to this file. */
1312 /* Base "class" for tracepoint type specific data to be passed down to
1313 collect_data_at_tracepoint. */
1314 struct tracepoint_hit_ctx
1316 enum tracepoint_type type
;
1319 #ifdef IN_PROCESS_AGENT
1321 /* Fast/jump tracepoint specific data to be passed down to
1322 collect_data_at_tracepoint. */
1323 struct fast_tracepoint_ctx
1325 struct tracepoint_hit_ctx base
;
1327 struct regcache regcache
;
1328 int regcache_initted
;
1329 unsigned char *regspace
;
1331 unsigned char *regs
;
1332 struct tracepoint
*tpoint
;
1335 /* Static tracepoint specific data to be passed down to
1336 collect_data_at_tracepoint. */
1337 struct static_tracepoint_ctx
1339 struct tracepoint_hit_ctx base
;
1341 /* The regcache corresponding to the registers state at the time of
1342 the tracepoint hit. Initialized lazily, from REGS. */
1343 struct regcache regcache
;
1344 int regcache_initted
;
1346 /* The buffer space REGCACHE above uses. We use a separate buffer
1347 instead of letting the regcache malloc for both signal safety and
1348 performance reasons; this is allocated on the stack instead. */
1349 unsigned char *regspace
;
1351 /* The register buffer as passed on by lttng/ust. */
1352 struct registers
*regs
;
1354 /* The "printf" formatter and the args the user passed to the marker
1355 call. We use this to be able to collect "static trace data"
1360 /* The GDB tracepoint matching the probed marker that was "hit". */
1361 struct tracepoint
*tpoint
;
1366 /* Static tracepoint specific data to be passed down to
1367 collect_data_at_tracepoint. */
1368 struct trap_tracepoint_ctx
1370 struct tracepoint_hit_ctx base
;
1372 struct regcache
*regcache
;
1377 #ifndef IN_PROCESS_AGENT
1378 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1379 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1382 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1383 struct tracepoint
*tpoint
);
1385 #ifndef IN_PROCESS_AGENT
1386 static void clear_readonly_regions (void);
1387 static void clear_installed_tracepoints (void);
1390 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1392 struct tracepoint
*tpoint
);
1393 #ifndef IN_PROCESS_AGENT
1394 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1396 struct tracepoint
*tpoint
, int current_step
);
1397 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1398 CORE_ADDR
*jump_entry
);
1400 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1402 struct tracepoint
*tpoint
,
1403 struct traceframe
*tframe
,
1404 struct tracepoint_action
*taction
);
1406 #ifndef IN_PROCESS_AGENT
1407 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1409 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1410 static void download_tracepoint (struct tracepoint
*);
1411 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1412 static void clone_fast_tracepoint (struct tracepoint
*to
,
1413 const struct tracepoint
*from
);
1416 static LONGEST
get_timestamp (void);
1418 #if defined(__GNUC__)
1419 # define memory_barrier() asm volatile ("" : : : "memory")
1421 # define memory_barrier() do {} while (0)
1424 /* We only build the IPA if this builtin is supported, and there are
1425 no uses of this in GDBserver itself, so we're safe in defining this
1427 #define cmpxchg(mem, oldval, newval) \
1428 __sync_val_compare_and_swap (mem, oldval, newval)
1430 /* Record that an error occurred during expression evaluation. */
1433 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1434 enum eval_result_type rtype
)
1436 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1437 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1439 #ifdef IN_PROCESS_AGENT
1440 /* Only record the first error we get. */
1441 if (cmpxchg (&expr_eval_result
,
1443 rtype
) != expr_eval_no_error
)
1446 if (expr_eval_result
!= expr_eval_no_error
)
1450 error_tracepoint
= tpoint
;
1453 /* Trace buffer management. */
1456 clear_trace_buffer (void)
1458 trace_buffer_start
= trace_buffer_lo
;
1459 trace_buffer_free
= trace_buffer_lo
;
1460 trace_buffer_end_free
= trace_buffer_hi
;
1461 trace_buffer_wrap
= trace_buffer_hi
;
1462 /* A traceframe with zeroed fields marks the end of trace data. */
1463 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1464 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1465 traceframe_read_count
= traceframe_write_count
= 0;
1466 traceframes_created
= 0;
1469 #ifndef IN_PROCESS_AGENT
1472 clear_inferior_trace_buffer (void)
1474 CORE_ADDR ipa_trace_buffer_lo
;
1475 CORE_ADDR ipa_trace_buffer_hi
;
1476 struct traceframe ipa_traceframe
= { 0 };
1477 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1479 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1480 &ipa_trace_buffer_lo
);
1481 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1482 &ipa_trace_buffer_hi
);
1484 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1485 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1486 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1487 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1489 /* A traceframe with zeroed fields marks the end of trace data. */
1490 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1491 (unsigned char *) &ipa_trace_buffer_ctrl
,
1492 sizeof (ipa_trace_buffer_ctrl
));
1494 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1496 /* A traceframe with zeroed fields marks the end of trace data. */
1497 write_inferior_memory (ipa_trace_buffer_lo
,
1498 (unsigned char *) &ipa_traceframe
,
1499 sizeof (ipa_traceframe
));
1501 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1502 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1503 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1509 init_trace_buffer (LONGEST bufsize
)
1513 trace_buffer_size
= bufsize
;
1515 /* Make sure to internally allocate at least space for the EOB
1517 alloc_size
= (bufsize
< TRACEFRAME_EOB_MARKER_SIZE
1518 ? TRACEFRAME_EOB_MARKER_SIZE
: bufsize
);
1519 trace_buffer_lo
= xrealloc (trace_buffer_lo
, alloc_size
);
1521 trace_buffer_hi
= trace_buffer_lo
+ trace_buffer_size
;
1523 clear_trace_buffer ();
1526 #ifdef IN_PROCESS_AGENT
1528 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1529 about_to_request_buffer_space (void)
1531 /* GDBserver places breakpoint here while it goes about to flush
1532 data at random times. */
1533 UNKNOWN_SIDE_EFFECTS();
1538 /* Carve out a piece of the trace buffer, returning NULL in case of
1542 trace_buffer_alloc (size_t amt
)
1544 unsigned char *rslt
;
1545 struct trace_buffer_control
*tbctrl
;
1547 #ifdef IN_PROCESS_AGENT
1548 unsigned int prev
, prev_filtered
;
1549 unsigned int commit_count
;
1550 unsigned int commit
;
1551 unsigned int readout
;
1553 struct traceframe
*oldest
;
1554 unsigned char *new_start
;
1557 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1558 (long) amt
, (long) sizeof (struct traceframe
));
1560 /* Account for the EOB marker. */
1561 amt
+= TRACEFRAME_EOB_MARKER_SIZE
;
1563 #ifdef IN_PROCESS_AGENT
1567 /* Read the current token and extract the index to try to write to,
1568 storing it in CURR. */
1569 prev
= trace_buffer_ctrl_curr
;
1570 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1571 curr
= prev_filtered
+ 1;
1575 about_to_request_buffer_space ();
1577 /* Start out with a copy of the current state. GDBserver may be
1578 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1579 be able to commit anyway if that happens. */
1580 trace_buffer_ctrl
[curr
]
1581 = trace_buffer_ctrl
[prev_filtered
];
1582 trace_debug ("trying curr=%u", curr
);
1584 /* The GDBserver's agent doesn't need all that syncing, and always
1585 updates TCB 0 (there's only one, mind you). */
1588 tbctrl
= &trace_buffer_ctrl
[curr
];
1590 /* Offsets are easier to grok for debugging than raw addresses,
1591 especially for the small trace buffer sizes that are useful for
1593 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1595 (int) (tbctrl
->start
- trace_buffer_lo
),
1596 (int) (tbctrl
->free
- trace_buffer_lo
),
1597 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1598 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1599 (int) (trace_buffer_hi
- trace_buffer_lo
));
1601 /* The algorithm here is to keep trying to get a contiguous block of
1602 the requested size, possibly discarding older traceframes to free
1603 up space. Since free space might come in one or two pieces,
1604 depending on whether discarded traceframes wrapped around at the
1605 high end of the buffer, we test both pieces after each
1609 /* First, if we have two free parts, try the upper one first. */
1610 if (tbctrl
->end_free
< tbctrl
->free
)
1612 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1613 /* We have enough in the upper part. */
1617 /* Our high part of free space wasn't enough. Give up
1618 on it for now, set wraparound. We will recover the
1619 space later, if/when the wrapped-around traceframe is
1621 trace_debug ("Upper part too small, setting wraparound");
1622 tbctrl
->wrap
= tbctrl
->free
;
1623 tbctrl
->free
= trace_buffer_lo
;
1627 /* The normal case. */
1628 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1631 #ifdef IN_PROCESS_AGENT
1632 /* The IP Agent's buffer is always circular. It isn't used
1633 currently, but `circular_trace_buffer' could represent
1634 GDBserver's mode. If we didn't find space, ask GDBserver to
1637 flush_trace_buffer ();
1641 trace_debug ("gdbserver flushed buffer, retrying");
1645 /* GDBserver cancelled the tracing. Bail out as well. */
1648 /* If we're here, then neither part is big enough, and
1649 non-circular trace buffers are now full. */
1650 if (!circular_trace_buffer
)
1652 trace_debug ("Not enough space in the trace buffer");
1656 trace_debug ("Need more space in the trace buffer");
1658 /* If we have a circular buffer, we can try discarding the
1659 oldest traceframe and see if that helps. */
1660 oldest
= FIRST_TRACEFRAME ();
1661 if (oldest
->tpnum
== 0)
1663 /* Not good; we have no traceframes to free. Perhaps we're
1664 asking for a block that is larger than the buffer? In
1665 any case, give up. */
1666 trace_debug ("No traceframes to discard");
1670 /* We don't run this code in the in-process agent currently.
1671 E.g., we could leave the in-process agent in autonomous
1672 circular mode if we only have fast tracepoints. If we do
1673 that, then this bit becomes racy with GDBserver, which also
1674 writes to this counter. */
1675 --traceframe_write_count
;
1677 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1678 /* If we freed the traceframe that wrapped around, go back
1679 to the non-wrap case. */
1680 if (new_start
< tbctrl
->start
)
1682 trace_debug ("Discarding past the wraparound");
1683 tbctrl
->wrap
= trace_buffer_hi
;
1685 tbctrl
->start
= new_start
;
1686 tbctrl
->end_free
= tbctrl
->start
;
1688 trace_debug ("Discarded a traceframe\n"
1689 "Trace buffer [%d], start=%d free=%d "
1690 "endfree=%d wrap=%d hi=%d",
1692 (int) (tbctrl
->start
- trace_buffer_lo
),
1693 (int) (tbctrl
->free
- trace_buffer_lo
),
1694 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1695 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1696 (int) (trace_buffer_hi
- trace_buffer_lo
));
1698 /* Now go back around the loop. The discard might have resulted
1699 in either one or two pieces of free space, so we want to try
1700 both before freeing any more traceframes. */
1704 /* If we get here, we know we can provide the asked-for space. */
1706 rslt
= tbctrl
->free
;
1708 /* Adjust the request back down, now that we know we have space for
1709 the marker, but don't commit to AMT yet, we may still need to
1710 restart the operation if GDBserver touches the trace buffer
1711 (obviously only important in the in-process agent's version). */
1712 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1714 /* Or not. If GDBserver changed the trace buffer behind our back,
1715 we get to restart a new allocation attempt. */
1717 #ifdef IN_PROCESS_AGENT
1718 /* Build the tentative token. */
1719 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1720 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1721 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1725 /* Try to commit it. */
1726 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1727 if (readout
!= prev
)
1729 trace_debug ("GDBserver has touched the trace buffer, restarting."
1730 " (prev=%08x, commit=%08x, readout=%08x)",
1731 prev
, commit
, readout
);
1735 /* Hold your horses here. Even if that change was committed,
1736 GDBserver could come in, and clobber it. We need to hold to be
1737 able to tell if GDBserver clobbers before or after we committed
1738 the change. Whenever GDBserver goes about touching the IPA
1739 buffer, it sets a breakpoint in this routine, so we have a sync
1741 about_to_request_buffer_space ();
1743 /* Check if the change has been effective, even if GDBserver stopped
1744 us at the breakpoint. */
1747 unsigned int refetch
;
1751 refetch
= trace_buffer_ctrl_curr
;
1753 if (refetch
== commit
1754 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1757 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1758 "readout=%08x, refetch=%08x)",
1759 prev
, commit
, readout
, refetch
);
1763 trace_debug ("GDBserver has touched the trace buffer, not effective."
1764 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1765 prev
, commit
, readout
, refetch
);
1771 /* We have a new piece of the trace buffer. Hurray! */
1773 /* Add an EOB marker just past this allocation. */
1774 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1775 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1777 /* Adjust the request back down, now that we know we have space for
1779 amt
-= sizeof (struct traceframe
);
1783 trace_debug ("Allocated %d bytes", (int) amt
);
1784 trace_debug ("Trace buffer [%d] start=%d free=%d "
1785 "endfree=%d wrap=%d hi=%d",
1787 (int) (tbctrl
->start
- trace_buffer_lo
),
1788 (int) (tbctrl
->free
- trace_buffer_lo
),
1789 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1790 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1791 (int) (trace_buffer_hi
- trace_buffer_lo
));
1797 #ifndef IN_PROCESS_AGENT
1799 /* Return the total free space. This is not necessarily the largest
1800 block we can allocate, because of the two-part case. */
1805 if (trace_buffer_free
<= trace_buffer_end_free
)
1806 return trace_buffer_end_free
- trace_buffer_free
;
1808 return ((trace_buffer_end_free
- trace_buffer_lo
)
1809 + (trace_buffer_hi
- trace_buffer_free
));
1812 /* An 'S' in continuation packets indicates remainder are for
1815 static int seen_step_action_flag
;
1817 /* Create a tracepoint (location) with given number and address. Add this
1818 new tracepoint to list and sort this list. */
1820 static struct tracepoint
*
1821 add_tracepoint (int num
, CORE_ADDR addr
)
1823 struct tracepoint
*tpoint
, **tp_next
;
1825 tpoint
= xmalloc (sizeof (struct tracepoint
));
1826 tpoint
->number
= num
;
1827 tpoint
->address
= addr
;
1828 tpoint
->numactions
= 0;
1829 tpoint
->actions
= NULL
;
1830 tpoint
->actions_str
= NULL
;
1831 tpoint
->cond
= NULL
;
1832 tpoint
->num_step_actions
= 0;
1833 tpoint
->step_actions
= NULL
;
1834 tpoint
->step_actions_str
= NULL
;
1835 /* Start all off as regular (slow) tracepoints. */
1836 tpoint
->type
= trap_tracepoint
;
1837 tpoint
->orig_size
= -1;
1838 tpoint
->source_strings
= NULL
;
1839 tpoint
->compiled_cond
= 0;
1840 tpoint
->handle
= NULL
;
1841 tpoint
->next
= NULL
;
1843 /* Find a place to insert this tracepoint into list in order to keep
1844 the tracepoint list still in the ascending order. There may be
1845 multiple tracepoints at the same address as TPOINT's, and this
1846 guarantees TPOINT is inserted after all the tracepoints which are
1847 set at the same address. For example, fast tracepoints A, B, C are
1848 set at the same address, and D is to be insert at the same place as
1851 -->| A |--> | B |-->| C |->...
1853 One jump pad was created for tracepoint A, B, and C, and the target
1854 address of A is referenced/used in jump pad. So jump pad will let
1855 inferior jump to A. If D is inserted in front of A, like this,
1857 -->| D |-->| A |--> | B |-->| C |->...
1859 without updating jump pad, D is not reachable during collect, which
1860 is wrong. As we can see, the order of B, C and D doesn't matter, but
1861 A should always be the `first' one. */
1862 for (tp_next
= &tracepoints
;
1863 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1864 tp_next
= &(*tp_next
)->next
)
1866 tpoint
->next
= *tp_next
;
1868 last_tracepoint
= tpoint
;
1870 seen_step_action_flag
= 0;
1875 #ifndef IN_PROCESS_AGENT
1877 /* Return the tracepoint with the given number and address, or NULL. */
1879 static struct tracepoint
*
1880 find_tracepoint (int id
, CORE_ADDR addr
)
1882 struct tracepoint
*tpoint
;
1884 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1885 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1891 /* Remove TPOINT from global list. */
1894 remove_tracepoint (struct tracepoint
*tpoint
)
1896 struct tracepoint
*tp
, *tp_prev
;
1898 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1899 tp_prev
= tp
, tp
= tp
->next
)
1905 tp_prev
->next
= tp
->next
;
1907 tracepoints
= tp
->next
;
1913 /* There may be several tracepoints with the same number (because they
1914 are "locations", in GDB parlance); return the next one after the
1915 given tracepoint, or search from the beginning of the list if the
1916 first argument is NULL. */
1918 static struct tracepoint
*
1919 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1921 struct tracepoint
*tpoint
;
1924 tpoint
= prev_tp
->next
;
1926 tpoint
= tracepoints
;
1927 for (; tpoint
; tpoint
= tpoint
->next
)
1928 if (tpoint
->number
== num
)
1936 /* Append another action to perform when the tracepoint triggers. */
1939 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1945 seen_step_action_flag
= 1;
1953 char *act_start
= act
;
1954 struct tracepoint_action
*action
= NULL
;
1960 struct collect_memory_action
*maction
;
1964 maction
= xmalloc (sizeof *maction
);
1965 maction
->base
.type
= *act
;
1966 maction
->base
.ops
= &m_tracepoint_action_ops
;
1967 action
= &maction
->base
;
1970 is_neg
= (*act
== '-');
1973 act
= unpack_varlen_hex (act
, &basereg
);
1975 act
= unpack_varlen_hex (act
, &maction
->addr
);
1977 act
= unpack_varlen_hex (act
, &maction
->len
);
1978 maction
->basereg
= (is_neg
1981 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1982 pulongest (maction
->len
),
1983 paddress (maction
->addr
), maction
->basereg
);
1988 struct collect_registers_action
*raction
;
1990 raction
= xmalloc (sizeof *raction
);
1991 raction
->base
.type
= *act
;
1992 raction
->base
.ops
= &r_tracepoint_action_ops
;
1993 action
= &raction
->base
;
1995 trace_debug ("Want to collect registers");
1997 /* skip past hex digits of mask for now */
1998 while (isxdigit(*act
))
2004 struct collect_static_trace_data_action
*raction
;
2006 raction
= xmalloc (sizeof *raction
);
2007 raction
->base
.type
= *act
;
2008 raction
->base
.ops
= &l_tracepoint_action_ops
;
2009 action
= &raction
->base
;
2011 trace_debug ("Want to collect static trace data");
2016 trace_debug ("Unexpected step action, ignoring");
2021 struct eval_expr_action
*xaction
;
2023 xaction
= xmalloc (sizeof (*xaction
));
2024 xaction
->base
.type
= *act
;
2025 xaction
->base
.ops
= &x_tracepoint_action_ops
;
2026 action
= &xaction
->base
;
2028 trace_debug ("Want to evaluate expression");
2029 xaction
->expr
= gdb_parse_agent_expr (&act
);
2033 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2042 if (seen_step_action_flag
)
2044 tpoint
->num_step_actions
++;
2046 tpoint
->step_actions
2047 = xrealloc (tpoint
->step_actions
,
2048 (sizeof (*tpoint
->step_actions
)
2049 * tpoint
->num_step_actions
));
2050 tpoint
->step_actions_str
2051 = xrealloc (tpoint
->step_actions_str
,
2052 (sizeof (*tpoint
->step_actions_str
)
2053 * tpoint
->num_step_actions
));
2054 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2055 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2056 = savestring (act_start
, act
- act_start
);
2060 tpoint
->numactions
++;
2062 = xrealloc (tpoint
->actions
,
2063 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2065 = xrealloc (tpoint
->actions_str
,
2066 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2067 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2068 tpoint
->actions_str
[tpoint
->numactions
- 1]
2069 = savestring (act_start
, act
- act_start
);
2076 /* Find or create a trace state variable with the given number. */
2078 static struct trace_state_variable
*
2079 get_trace_state_variable (int num
)
2081 struct trace_state_variable
*tsv
;
2083 #ifdef IN_PROCESS_AGENT
2084 /* Search for an existing variable. */
2085 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2086 if (tsv
->number
== num
)
2090 /* Search for an existing variable. */
2091 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2092 if (tsv
->number
== num
)
2098 /* Find or create a trace state variable with the given number. */
2100 static struct trace_state_variable
*
2101 create_trace_state_variable (int num
, int gdb
)
2103 struct trace_state_variable
*tsv
;
2105 tsv
= get_trace_state_variable (num
);
2109 /* Create a new variable. */
2110 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2112 tsv
->initial_value
= 0;
2116 #ifdef IN_PROCESS_AGENT
2119 tsv
->next
= alloced_trace_state_variables
;
2120 alloced_trace_state_variables
= tsv
;
2125 tsv
->next
= trace_state_variables
;
2126 trace_state_variables
= tsv
;
2131 IP_AGENT_EXPORT LONGEST
2132 get_trace_state_variable_value (int num
)
2134 struct trace_state_variable
*tsv
;
2136 tsv
= get_trace_state_variable (num
);
2140 trace_debug ("No trace state variable %d, skipping value get", num
);
2144 /* Call a getter function if we have one. While it's tempting to
2145 set up something to only call the getter once per tracepoint hit,
2146 it could run afoul of thread races. Better to let the getter
2147 handle it directly, if necessary to worry about it. */
2149 tsv
->value
= (tsv
->getter
) ();
2151 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2152 num
, plongest (tsv
->value
));
2157 IP_AGENT_EXPORT
void
2158 set_trace_state_variable_value (int num
, LONGEST val
)
2160 struct trace_state_variable
*tsv
;
2162 tsv
= get_trace_state_variable (num
);
2166 trace_debug ("No trace state variable %d, skipping value set", num
);
2174 agent_get_trace_state_variable_value (int num
)
2176 return get_trace_state_variable_value (num
);
2180 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2182 set_trace_state_variable_value (num
, val
);
2186 set_trace_state_variable_name (int num
, const char *name
)
2188 struct trace_state_variable
*tsv
;
2190 tsv
= get_trace_state_variable (num
);
2194 trace_debug ("No trace state variable %d, skipping name set", num
);
2198 tsv
->name
= (char *) name
;
2202 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2204 struct trace_state_variable
*tsv
;
2206 tsv
= get_trace_state_variable (num
);
2210 trace_debug ("No trace state variable %d, skipping getter set", num
);
2214 tsv
->getter
= getter
;
2217 /* Add a raw traceframe for the given tracepoint. */
2219 static struct traceframe
*
2220 add_traceframe (struct tracepoint
*tpoint
)
2222 struct traceframe
*tframe
;
2224 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2229 tframe
->tpnum
= tpoint
->number
;
2230 tframe
->data_size
= 0;
2235 /* Add a block to the traceframe currently being worked on. */
2237 static unsigned char *
2238 add_traceframe_block (struct traceframe
*tframe
,
2239 struct tracepoint
*tpoint
, int amt
)
2241 unsigned char *block
;
2246 block
= trace_buffer_alloc (amt
);
2251 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2253 tframe
->data_size
+= amt
;
2254 tpoint
->traceframe_usage
+= amt
;
2259 /* Flag that the current traceframe is finished. */
2262 finish_traceframe (struct traceframe
*tframe
)
2264 ++traceframe_write_count
;
2265 ++traceframes_created
;
2268 #ifndef IN_PROCESS_AGENT
2270 /* Given a traceframe number NUM, find the NUMth traceframe in the
2273 static struct traceframe
*
2274 find_traceframe (int num
)
2276 struct traceframe
*tframe
;
2279 for (tframe
= FIRST_TRACEFRAME ();
2281 tframe
= NEXT_TRACEFRAME (tframe
))
2292 get_traceframe_address (struct traceframe
*tframe
)
2295 struct tracepoint
*tpoint
;
2297 addr
= traceframe_get_pc (tframe
);
2302 /* Fallback strategy, will be incorrect for while-stepping frames
2303 and multi-location tracepoints. */
2304 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2305 return tpoint
->address
;
2308 /* Search for the next traceframe whose address is inside or outside
2311 static struct traceframe
*
2312 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2315 struct traceframe
*tframe
;
2318 *tfnump
= current_traceframe
+ 1;
2319 tframe
= find_traceframe (*tfnump
);
2320 /* The search is not supposed to wrap around. */
2327 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2329 tfaddr
= get_traceframe_address (tframe
);
2331 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2332 : (lo
> tfaddr
|| tfaddr
> hi
))
2341 /* Search for the next traceframe recorded by the given tracepoint.
2342 Note that for multi-location tracepoints, this will find whatever
2343 location appears first. */
2345 static struct traceframe
*
2346 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2348 struct traceframe
*tframe
;
2350 *tfnump
= current_traceframe
+ 1;
2351 tframe
= find_traceframe (*tfnump
);
2352 /* The search is not supposed to wrap around. */
2359 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2361 if (tframe
->tpnum
== num
)
2372 #ifndef IN_PROCESS_AGENT
2374 /* Clear all past trace state. */
2377 cmd_qtinit (char *packet
)
2379 struct trace_state_variable
*tsv
, *prev
, *next
;
2381 /* Make sure we don't try to read from a trace frame. */
2382 current_traceframe
= -1;
2386 trace_debug ("Initializing the trace");
2388 clear_installed_tracepoints ();
2389 clear_readonly_regions ();
2392 last_tracepoint
= NULL
;
2394 /* Clear out any leftover trace state variables. Ones with target
2395 defined getters should be kept however. */
2397 tsv
= trace_state_variables
;
2400 trace_debug ("Looking at var %d", tsv
->number
);
2401 if (tsv
->getter
== NULL
)
2407 trace_state_variables
= next
;
2408 trace_debug ("Deleting var %d", tsv
->number
);
2419 clear_trace_buffer ();
2420 clear_inferior_trace_buffer ();
2425 /* Unprobe the UST marker at ADDRESS. */
2428 unprobe_marker_at (CORE_ADDR address
)
2430 char cmd
[IPA_CMD_BUF_SIZE
];
2432 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2433 run_inferior_command (cmd
, strlen (cmd
) + 1);
2436 /* Restore the program to its pre-tracing state. This routine may be called
2437 in error situations, so it needs to be careful about only restoring
2438 from known-valid bits. */
2441 clear_installed_tracepoints (void)
2443 struct tracepoint
*tpoint
;
2444 struct tracepoint
*prev_stpoint
;
2447 cancel_breakpoints ();
2449 prev_stpoint
= NULL
;
2451 /* Restore any bytes overwritten by tracepoints. */
2452 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2454 /* Catch the case where we might try to remove a tracepoint that
2455 was never actually installed. */
2456 if (tpoint
->handle
== NULL
)
2458 trace_debug ("Tracepoint %d at 0x%s was "
2459 "never installed, nothing to clear",
2460 tpoint
->number
, paddress (tpoint
->address
));
2464 switch (tpoint
->type
)
2466 case trap_tracepoint
:
2467 delete_breakpoint (tpoint
->handle
);
2469 case fast_tracepoint
:
2470 delete_fast_tracepoint_jump (tpoint
->handle
);
2472 case static_tracepoint
:
2473 if (prev_stpoint
!= NULL
2474 && prev_stpoint
->address
== tpoint
->address
)
2475 /* Nothing to do. We already unprobed a tracepoint set at
2476 this marker address (and there can only be one probe
2481 unprobe_marker_at (tpoint
->address
);
2482 prev_stpoint
= tpoint
;
2487 tpoint
->handle
= NULL
;
2493 /* Parse a packet that defines a tracepoint. */
2496 cmd_qtdp (char *own_buf
)
2499 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2500 int trail_hyphen
= 0;
2504 struct tracepoint
*tpoint
;
2506 char *packet
= own_buf
;
2508 packet
+= strlen ("QTDP:");
2510 /* A hyphen at the beginning marks a packet specifying actions for a
2511 tracepoint already supplied. */
2518 packet
= unpack_varlen_hex (packet
, &num
);
2519 ++packet
; /* skip a colon */
2520 packet
= unpack_varlen_hex (packet
, &addr
);
2521 ++packet
; /* skip a colon */
2523 /* See if we already have this tracepoint. */
2524 tpoint
= find_tracepoint (num
, addr
);
2528 /* Duplicate tracepoints are never allowed. */
2531 trace_debug ("Tracepoint error: tracepoint %d"
2532 " at 0x%s already exists",
2533 (int) num
, paddress (addr
));
2534 write_enn (own_buf
);
2538 tpoint
= add_tracepoint (num
, addr
);
2540 tpoint
->enabled
= (*packet
== 'E');
2541 ++packet
; /* skip 'E' */
2542 ++packet
; /* skip a colon */
2543 packet
= unpack_varlen_hex (packet
, &count
);
2544 tpoint
->step_count
= count
;
2545 ++packet
; /* skip a colon */
2546 packet
= unpack_varlen_hex (packet
, &count
);
2547 tpoint
->pass_count
= count
;
2548 /* See if we have any of the additional optional fields. */
2549 while (*packet
== ':')
2554 tpoint
->type
= fast_tracepoint
;
2556 packet
= unpack_varlen_hex (packet
, &count
);
2557 tpoint
->orig_size
= count
;
2559 else if (*packet
== 'S')
2561 tpoint
->type
= static_tracepoint
;
2564 else if (*packet
== 'X')
2566 actparm
= (char *) packet
;
2567 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2570 else if (*packet
== '-')
2572 else if (*packet
== '\0')
2575 trace_debug ("Unknown optional tracepoint field");
2580 trace_debug ("Also has actions\n");
2583 trace_debug ("Defined %stracepoint %d at 0x%s, "
2584 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2585 tpoint
->type
== fast_tracepoint
? "fast "
2586 : tpoint
->type
== static_tracepoint
? "static " : "",
2587 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2588 tpoint
->step_count
, tpoint
->pass_count
);
2591 add_tracepoint_action (tpoint
, packet
);
2594 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2595 (int) num
, paddress (addr
));
2596 write_enn (own_buf
);
2600 /* Install tracepoint during tracing only once for each tracepoint location.
2601 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2602 determine the last QTDP packet for one tracepoint location by checking
2603 trailing hyphen in QTDP packet. */
2604 if (tracing
&& !trail_hyphen
)
2606 struct tracepoint
*tp
= NULL
;
2608 /* Pause all threads temporarily while we patch tracepoints. */
2611 /* download_tracepoint will update global `tracepoints'
2612 list, so it is unsafe to leave threads in jump pad. */
2613 stabilize_threads ();
2615 /* Freeze threads. */
2619 if (tpoint
->type
!= trap_tracepoint
)
2621 /* Find another fast or static tracepoint at the same address. */
2622 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2624 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2625 && tp
->number
!= tpoint
->number
)
2629 /* TPOINT is installed at the same address as TP. */
2632 if (tpoint
->type
== fast_tracepoint
)
2633 clone_fast_tracepoint (tpoint
, tp
);
2634 else if (tpoint
->type
== static_tracepoint
)
2635 tpoint
->handle
= (void *) -1;
2639 if (use_agent
&& tpoint
->type
== fast_tracepoint
2640 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2642 /* Download and install fast tracepoint by agent. */
2643 if (tracepoint_send_agent (tpoint
) == 0)
2647 write_enn (own_buf
);
2648 remove_tracepoint (tpoint
);
2653 download_tracepoint (tpoint
);
2655 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2657 install_tracepoint (tpoint
, own_buf
);
2658 if (strcmp (own_buf
, "OK") != 0)
2659 remove_tracepoint (tpoint
);
2673 cmd_qtdpsrc (char *own_buf
)
2675 ULONGEST num
, addr
, start
, slen
;
2676 struct tracepoint
*tpoint
;
2677 char *packet
= own_buf
;
2678 char *saved
, *srctype
, *src
;
2680 struct source_string
*last
, *newlast
;
2682 packet
+= strlen ("QTDPsrc:");
2684 packet
= unpack_varlen_hex (packet
, &num
);
2685 ++packet
; /* skip a colon */
2686 packet
= unpack_varlen_hex (packet
, &addr
);
2687 ++packet
; /* skip a colon */
2689 /* See if we already have this tracepoint. */
2690 tpoint
= find_tracepoint (num
, addr
);
2694 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2695 (int) num
, paddress (addr
));
2696 write_enn (own_buf
);
2701 packet
= strchr (packet
, ':');
2702 srctype
= xmalloc (packet
- saved
+ 1);
2703 memcpy (srctype
, saved
, packet
- saved
);
2704 srctype
[packet
- saved
] = '\0';
2706 packet
= unpack_varlen_hex (packet
, &start
);
2707 ++packet
; /* skip a colon */
2708 packet
= unpack_varlen_hex (packet
, &slen
);
2709 ++packet
; /* skip a colon */
2710 src
= xmalloc (slen
+ 1);
2711 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2714 newlast
= xmalloc (sizeof (struct source_string
));
2715 newlast
->type
= srctype
;
2717 newlast
->next
= NULL
;
2718 /* Always add a source string to the end of the list;
2719 this keeps sequences of actions/commands in the right
2721 if (tpoint
->source_strings
)
2723 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2725 last
->next
= newlast
;
2728 tpoint
->source_strings
= newlast
;
2734 cmd_qtdv (char *own_buf
)
2736 ULONGEST num
, val
, builtin
;
2739 struct trace_state_variable
*tsv
;
2740 char *packet
= own_buf
;
2742 packet
+= strlen ("QTDV:");
2744 packet
= unpack_varlen_hex (packet
, &num
);
2745 ++packet
; /* skip a colon */
2746 packet
= unpack_varlen_hex (packet
, &val
);
2747 ++packet
; /* skip a colon */
2748 packet
= unpack_varlen_hex (packet
, &builtin
);
2749 ++packet
; /* skip a colon */
2751 nbytes
= strlen (packet
) / 2;
2752 varname
= xmalloc (nbytes
+ 1);
2753 nbytes
= unhexify (varname
, packet
, nbytes
);
2754 varname
[nbytes
] = '\0';
2756 tsv
= create_trace_state_variable (num
, 1);
2757 tsv
->initial_value
= (LONGEST
) val
;
2758 tsv
->name
= varname
;
2760 set_trace_state_variable_value (num
, (LONGEST
) val
);
2766 cmd_qtenable_disable (char *own_buf
, int enable
)
2768 char *packet
= own_buf
;
2770 struct tracepoint
*tp
;
2772 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2773 packet
= unpack_varlen_hex (packet
, &num
);
2774 ++packet
; /* skip a colon */
2775 packet
= unpack_varlen_hex (packet
, &addr
);
2777 tp
= find_tracepoint (num
, addr
);
2781 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2783 trace_debug ("Tracepoint %d at 0x%s is already %s",
2784 (int) num
, paddress (addr
),
2785 enable
? "enabled" : "disabled");
2790 trace_debug ("%s tracepoint %d at 0x%s",
2791 enable
? "Enabling" : "Disabling",
2792 (int) num
, paddress (addr
));
2794 tp
->enabled
= enable
;
2796 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2799 int offset
= offsetof (struct tracepoint
, enabled
);
2800 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2802 ret
= prepare_to_access_memory ();
2805 trace_debug ("Failed to temporarily stop inferior threads");
2806 write_enn (own_buf
);
2810 ret
= write_inferior_integer (obj_addr
, enable
);
2811 done_accessing_memory ();
2815 trace_debug ("Cannot write enabled flag into "
2816 "inferior process memory");
2817 write_enn (own_buf
);
2826 trace_debug ("Tracepoint %d at 0x%s not found",
2827 (int) num
, paddress (addr
));
2828 write_enn (own_buf
);
2833 cmd_qtv (char *own_buf
)
2838 char *packet
= own_buf
;
2840 packet
+= strlen ("qTV:");
2841 unpack_varlen_hex (packet
, &num
);
2843 if (current_traceframe
>= 0)
2845 err
= traceframe_read_tsv ((int) num
, &val
);
2848 strcpy (own_buf
, "U");
2852 /* Only make tsv's be undefined before the first trace run. After a
2853 trace run is over, the user might want to see the last value of
2854 the tsv, and it might not be available in a traceframe. */
2855 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2857 strcpy (own_buf
, "U");
2861 val
= get_trace_state_variable_value (num
);
2863 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2866 /* Clear out the list of readonly regions. */
2869 clear_readonly_regions (void)
2871 struct readonly_region
*roreg
;
2873 while (readonly_regions
)
2875 roreg
= readonly_regions
;
2876 readonly_regions
= readonly_regions
->next
;
2881 /* Parse the collection of address ranges whose contents GDB believes
2882 to be unchanging and so can be read directly from target memory
2883 even while looking at a traceframe. */
2886 cmd_qtro (char *own_buf
)
2888 ULONGEST start
, end
;
2889 struct readonly_region
*roreg
;
2890 char *packet
= own_buf
;
2892 trace_debug ("Want to mark readonly regions");
2894 clear_readonly_regions ();
2896 packet
+= strlen ("QTro");
2898 while (*packet
== ':')
2900 ++packet
; /* skip a colon */
2901 packet
= unpack_varlen_hex (packet
, &start
);
2902 ++packet
; /* skip a comma */
2903 packet
= unpack_varlen_hex (packet
, &end
);
2904 roreg
= xmalloc (sizeof (struct readonly_region
));
2905 roreg
->start
= start
;
2907 roreg
->next
= readonly_regions
;
2908 readonly_regions
= roreg
;
2909 trace_debug ("Added readonly region from 0x%s to 0x%s",
2910 paddress (roreg
->start
), paddress (roreg
->end
));
2916 /* Test to see if the given range is in our list of readonly ranges.
2917 We only test for being entirely within a range, GDB is not going to
2918 send a single memory packet that spans multiple regions. */
2921 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2923 struct readonly_region
*roreg
;
2925 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2926 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2932 /* The maximum size of a jump pad entry. */
2933 static const int max_jump_pad_size
= 0x100;
2935 static CORE_ADDR gdb_jump_pad_head
;
2937 /* Return the address of the next free jump space. */
2940 get_jump_space_head (void)
2942 if (gdb_jump_pad_head
== 0)
2944 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2945 &gdb_jump_pad_head
))
2946 fatal ("error extracting jump_pad_buffer");
2949 return gdb_jump_pad_head
;
2952 /* Reserve USED bytes from the jump space. */
2955 claim_jump_space (ULONGEST used
)
2957 trace_debug ("claim_jump_space reserves %s bytes at %s",
2958 pulongest (used
), paddress (gdb_jump_pad_head
));
2959 gdb_jump_pad_head
+= used
;
2962 static CORE_ADDR trampoline_buffer_head
= 0;
2963 static CORE_ADDR trampoline_buffer_tail
;
2965 /* Reserve USED bytes from the trampoline buffer and return the
2966 address of the start of the reserved space in TRAMPOLINE. Returns
2967 non-zero if the space is successfully claimed. */
2970 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2972 if (!trampoline_buffer_head
)
2974 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2975 &trampoline_buffer_tail
))
2977 fatal ("error extracting trampoline_buffer");
2981 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2982 &trampoline_buffer_head
))
2984 fatal ("error extracting trampoline_buffer_end");
2989 /* Start claiming space from the top of the trampoline space. If
2990 the space is located at the bottom of the virtual address space,
2991 this reduces the possibility that corruption will occur if a null
2992 pointer is used to write to memory. */
2993 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2995 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
3000 trampoline_buffer_head
-= used
;
3002 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
3003 pulongest (used
), paddress (trampoline_buffer_head
));
3005 *trampoline
= trampoline_buffer_head
;
3009 /* Returns non-zero if there is space allocated for use in trampolines
3010 for fast tracepoints. */
3013 have_fast_tracepoint_trampoline_buffer (char *buf
)
3015 CORE_ADDR trampoline_end
, errbuf
;
3017 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
3020 fatal ("error extracting trampoline_buffer_end");
3027 strcpy (buf
, "was claiming");
3028 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3031 fatal ("error extracting errbuf");
3035 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3038 return trampoline_end
!= 0;
3041 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3042 the command fails, or 0 otherwise. If the command ran
3043 successfully, but probing the marker failed, ERROUT will be filled
3044 with the error to reply to GDB, and -1 is also returned. This
3045 allows directly passing IPA errors to GDB. */
3048 probe_marker_at (CORE_ADDR address
, char *errout
)
3050 char cmd
[IPA_CMD_BUF_SIZE
];
3053 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3054 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3060 strcpy (errout
, cmd
);
3069 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3071 to
->jump_pad
= from
->jump_pad
;
3072 to
->jump_pad_end
= from
->jump_pad_end
;
3073 to
->trampoline
= from
->trampoline
;
3074 to
->trampoline_end
= from
->trampoline_end
;
3075 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3076 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3077 to
->handle
= from
->handle
;
3079 gdb_assert (from
->handle
);
3080 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3083 #define MAX_JUMP_SIZE 20
3085 /* Install fast tracepoint. Return 0 if successful, otherwise return
3089 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3091 CORE_ADDR jentry
, jump_entry
;
3092 CORE_ADDR trampoline
;
3093 ULONGEST trampoline_size
;
3095 /* The jump to the jump pad of the last fast tracepoint
3097 unsigned char fjump
[MAX_JUMP_SIZE
];
3098 ULONGEST fjump_size
;
3100 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3102 trace_debug ("Requested a fast tracepoint on an instruction "
3103 "that is of less than the minimum length.");
3107 jentry
= jump_entry
= get_jump_space_head ();
3110 trampoline_size
= 0;
3112 /* Install the jump pad. */
3113 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3115 ipa_sym_addrs
.addr_gdb_collect
,
3116 ipa_sym_addrs
.addr_collecting
,
3119 &trampoline
, &trampoline_size
,
3121 &tpoint
->adjusted_insn_addr
,
3122 &tpoint
->adjusted_insn_addr_end
,
3129 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3132 if (tpoint
->handle
!= NULL
)
3134 tpoint
->jump_pad
= jump_entry
;
3135 tpoint
->jump_pad_end
= jentry
;
3136 tpoint
->trampoline
= trampoline
;
3137 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3139 /* Pad to 8-byte alignment. */
3140 jentry
= ((jentry
+ 7) & ~0x7);
3141 claim_jump_space (jentry
- jump_entry
);
3148 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3151 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3153 tpoint
->handle
= NULL
;
3156 if (tpoint
->type
== trap_tracepoint
)
3158 /* Tracepoints are installed as memory breakpoints. Just go
3159 ahead and install the trap. The breakpoints module
3160 handles duplicated breakpoints, and the memory read
3161 routine handles un-patching traps from memory reads. */
3162 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3163 tracepoint_handler
);
3165 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3167 if (!agent_loaded_p ())
3169 trace_debug ("Requested a %s tracepoint, but fast "
3170 "tracepoints aren't supported.",
3171 tpoint
->type
== static_tracepoint
? "static" : "fast");
3172 write_e_ipa_not_loaded (own_buf
);
3175 if (tpoint
->type
== static_tracepoint
3176 && !in_process_agent_supports_ust ())
3178 trace_debug ("Requested a static tracepoint, but static "
3179 "tracepoints are not supported.");
3180 write_e_ust_not_loaded (own_buf
);
3184 if (tpoint
->type
== fast_tracepoint
)
3185 install_fast_tracepoint (tpoint
, own_buf
);
3188 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3189 tpoint
->handle
= (void *) -1;
3194 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3196 if (tpoint
->handle
== NULL
)
3198 if (*own_buf
== '\0')
3199 write_enn (own_buf
);
3205 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3208 cmd_qtstart (char *packet
)
3210 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3211 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3213 trace_debug ("Starting the trace");
3215 /* Pause all threads temporarily while we patch tracepoints. */
3218 /* Get threads out of jump pads. Safe to do here, since this is a
3219 top level command. And, required to do here, since we're
3220 deleting/rewriting jump pads. */
3222 stabilize_threads ();
3224 /* Freeze threads. */
3227 /* Sync the fast tracepoints list in the inferior ftlib. */
3228 if (agent_loaded_p ())
3229 download_trace_state_variables ();
3231 /* No previous fast tpoint yet. */
3232 prev_ftpoint
= NULL
;
3234 /* No previous static tpoint yet. */
3235 prev_stpoint
= NULL
;
3239 /* Start out empty. */
3240 if (agent_loaded_p ())
3241 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3243 /* Download and install tracepoints. */
3244 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3246 /* Ensure all the hit counts start at zero. */
3247 tpoint
->hit_count
= 0;
3248 tpoint
->traceframe_usage
= 0;
3250 if (tpoint
->type
== trap_tracepoint
)
3252 /* Tracepoints are installed as memory breakpoints. Just go
3253 ahead and install the trap. The breakpoints module
3254 handles duplicated breakpoints, and the memory read
3255 routine handles un-patching traps from memory reads. */
3256 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3257 tracepoint_handler
);
3259 else if (tpoint
->type
== fast_tracepoint
3260 || tpoint
->type
== static_tracepoint
)
3262 if (maybe_write_ipa_not_loaded (packet
))
3264 trace_debug ("Requested a %s tracepoint, but fast "
3265 "tracepoints aren't supported.",
3266 tpoint
->type
== static_tracepoint
3267 ? "static" : "fast");
3271 if (tpoint
->type
== fast_tracepoint
)
3274 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3276 if (prev_ftpoint
!= NULL
3277 && prev_ftpoint
->address
== tpoint
->address
)
3280 tracepoint_send_agent (tpoint
);
3282 download_tracepoint_1 (tpoint
);
3284 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3288 /* Tracepoint is installed successfully? */
3291 /* Download and install fast tracepoint by agent. */
3293 installed
= !tracepoint_send_agent (tpoint
);
3296 download_tracepoint_1 (tpoint
);
3297 installed
= !install_fast_tracepoint (tpoint
, packet
);
3301 prev_ftpoint
= tpoint
;
3306 if (!in_process_agent_supports_ust ())
3308 trace_debug ("Requested a static tracepoint, but static "
3309 "tracepoints are not supported.");
3313 download_tracepoint_1 (tpoint
);
3314 /* Can only probe a given marker once. */
3315 if (prev_stpoint
!= NULL
3316 && prev_stpoint
->address
== tpoint
->address
)
3317 tpoint
->handle
= (void *) -1;
3320 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3322 tpoint
->handle
= (void *) -1;
3324 /* So that we can handle multiple static tracepoints
3325 at the same address easily. */
3326 prev_stpoint
= tpoint
;
3332 tpptr
= tpoint
->obj_addr_on_target
;
3334 if (tpoint
== tracepoints
)
3335 /* First object in list, set the head pointer in the
3337 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3339 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3344 /* Any failure in the inner loop is sufficient cause to give
3346 if (tpoint
->handle
== NULL
)
3350 /* Any error in tracepoint insertion is unacceptable; better to
3351 address the problem now, than end up with a useless or misleading
3355 clear_installed_tracepoints ();
3356 if (*packet
== '\0')
3362 stopping_tracepoint
= NULL
;
3363 trace_buffer_is_full
= 0;
3364 expr_eval_result
= expr_eval_no_error
;
3365 error_tracepoint
= NULL
;
3366 tracing_start_time
= get_timestamp ();
3368 /* Tracing is now active, hits will now start being logged. */
3371 if (agent_loaded_p ())
3373 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3374 fatal ("Error setting tracing variable in lib");
3376 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3378 fatal ("Error clearing stopping_tracepoint variable in lib");
3380 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3381 fatal ("Error clearing trace_buffer_is_full variable in lib");
3383 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3384 stop_tracing_handler
);
3385 if (stop_tracing_bkpt
== NULL
)
3386 error ("Error setting stop_tracing breakpoint");
3388 flush_trace_buffer_bkpt
3389 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3390 flush_trace_buffer_handler
);
3391 if (flush_trace_buffer_bkpt
== NULL
)
3392 error ("Error setting flush_trace_buffer breakpoint");
3400 /* End a tracing run, filling in a stop reason to report back to GDB,
3401 and removing the tracepoints from the code. */
3408 trace_debug ("Tracing is already off, ignoring");
3412 trace_debug ("Stopping the trace");
3414 /* Pause all threads before removing fast jumps from memory,
3415 breakpoints, and touching IPA state variables (inferior memory).
3416 Some thread may hit the internal tracing breakpoints, or be
3417 collecting this moment, but that's ok, we don't release the
3418 tpoint object's memory or the jump pads here (we only do that
3419 when we're sure we can move all threads out of the jump pads).
3420 We can't now, since we may be getting here due to the inferior
3421 agent calling us. */
3423 /* Since we're removing breakpoints, cancel breakpoint hits,
3424 possibly related to the breakpoints we're about to delete. */
3425 cancel_breakpoints ();
3427 /* Stop logging. Tracepoints can still be hit, but they will not be
3430 if (agent_loaded_p ())
3432 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3433 fatal ("Error clearing tracing variable in lib");
3436 tracing_stop_time
= get_timestamp ();
3437 tracing_stop_reason
= "t???";
3438 tracing_stop_tpnum
= 0;
3439 if (stopping_tracepoint
)
3441 trace_debug ("Stopping the trace because "
3442 "tracepoint %d was hit %" PRIu64
" times",
3443 stopping_tracepoint
->number
,
3444 stopping_tracepoint
->pass_count
);
3445 tracing_stop_reason
= "tpasscount";
3446 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3448 else if (trace_buffer_is_full
)
3450 trace_debug ("Stopping the trace because the trace buffer is full");
3451 tracing_stop_reason
= "tfull";
3453 else if (expr_eval_result
!= expr_eval_no_error
)
3455 trace_debug ("Stopping the trace because of an expression eval error");
3456 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3457 tracing_stop_tpnum
= error_tracepoint
->number
;
3459 #ifndef IN_PROCESS_AGENT
3460 else if (!gdb_connected ())
3462 trace_debug ("Stopping the trace because GDB disconnected");
3463 tracing_stop_reason
= "tdisconnected";
3468 trace_debug ("Stopping the trace because of a tstop command");
3469 tracing_stop_reason
= "tstop";
3472 stopping_tracepoint
= NULL
;
3473 error_tracepoint
= NULL
;
3475 /* Clear out the tracepoints. */
3476 clear_installed_tracepoints ();
3478 if (agent_loaded_p ())
3480 /* Pull in fast tracepoint trace frames from the inferior lib
3481 buffer into our buffer, even if our buffer is already full,
3482 because we want to present the full number of created frames
3483 in addition to what fit in the trace buffer. */
3484 upload_fast_traceframes ();
3487 if (stop_tracing_bkpt
!= NULL
)
3489 delete_breakpoint (stop_tracing_bkpt
);
3490 stop_tracing_bkpt
= NULL
;
3493 if (flush_trace_buffer_bkpt
!= NULL
)
3495 delete_breakpoint (flush_trace_buffer_bkpt
);
3496 flush_trace_buffer_bkpt
= NULL
;
3503 stop_tracing_handler (CORE_ADDR addr
)
3505 trace_debug ("lib hit stop_tracing");
3507 /* Don't actually handle it here. When we stop tracing we remove
3508 breakpoints from the inferior, and that is not allowed in a
3509 breakpoint handler (as the caller is walking the breakpoint
3515 flush_trace_buffer_handler (CORE_ADDR addr
)
3517 trace_debug ("lib hit flush_trace_buffer");
3522 cmd_qtstop (char *packet
)
3529 cmd_qtdisconnected (char *own_buf
)
3532 char *packet
= own_buf
;
3534 packet
+= strlen ("QTDisconnected:");
3536 unpack_varlen_hex (packet
, &setting
);
3540 disconnected_tracing
= setting
;
3544 cmd_qtframe (char *own_buf
)
3546 ULONGEST frame
, pc
, lo
, hi
, num
;
3548 struct traceframe
*tframe
;
3549 char *packet
= own_buf
;
3551 packet
+= strlen ("QTFrame:");
3553 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3555 packet
+= strlen ("pc:");
3556 unpack_varlen_hex (packet
, &pc
);
3557 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3558 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3560 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3562 packet
+= strlen ("range:");
3563 packet
= unpack_varlen_hex (packet
, &lo
);
3565 unpack_varlen_hex (packet
, &hi
);
3566 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3567 paddress (lo
), paddress (hi
));
3568 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3570 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3572 packet
+= strlen ("outside:");
3573 packet
= unpack_varlen_hex (packet
, &lo
);
3575 unpack_varlen_hex (packet
, &hi
);
3576 trace_debug ("Want to find next traceframe "
3577 "outside the range 0x%s to 0x%s",
3578 paddress (lo
), paddress (hi
));
3579 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3581 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3583 packet
+= strlen ("tdp:");
3584 unpack_varlen_hex (packet
, &num
);
3586 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3587 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3591 unpack_varlen_hex (packet
, &frame
);
3592 tfnum
= (int) frame
;
3595 trace_debug ("Want to stop looking at traceframes");
3596 current_traceframe
= -1;
3600 trace_debug ("Want to look at traceframe %d", tfnum
);
3601 tframe
= find_traceframe (tfnum
);
3606 current_traceframe
= tfnum
;
3607 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3610 sprintf (own_buf
, "F-1");
3614 cmd_qtstatus (char *packet
)
3616 char *stop_reason_rsp
= NULL
;
3617 char *buf1
, *buf2
, *buf3
, *str
;
3620 /* Translate the plain text of the notes back into hex for
3623 str
= (tracing_user_name
? tracing_user_name
: "");
3624 slen
= strlen (str
);
3625 buf1
= (char *) alloca (slen
* 2 + 1);
3626 bin2hex ((gdb_byte
*) str
, buf1
, slen
);
3628 str
= (tracing_notes
? tracing_notes
: "");
3629 slen
= strlen (str
);
3630 buf2
= (char *) alloca (slen
* 2 + 1);
3631 bin2hex ((gdb_byte
*) str
, buf2
, slen
);
3633 str
= (tracing_stop_note
? tracing_stop_note
: "");
3634 slen
= strlen (str
);
3635 buf3
= (char *) alloca (slen
* 2 + 1);
3636 bin2hex ((gdb_byte
*) str
, buf3
, slen
);
3638 trace_debug ("Returning trace status as %d, stop reason %s",
3639 tracing
, tracing_stop_reason
);
3641 if (agent_loaded_p ())
3645 upload_fast_traceframes ();
3650 stop_reason_rsp
= (char *) tracing_stop_reason
;
3652 /* The user visible error string in terror needs to be hex encoded.
3653 We leave it as plain string in `tracing_stop_reason' to ease
3655 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3657 const char *result_name
;
3661 result_name
= stop_reason_rsp
+ strlen ("terror:");
3662 hexstr_len
= strlen (result_name
) * 2;
3663 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3664 strcpy (p
, "terror:");
3666 bin2hex ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3669 /* If this was a forced stop, include any stop note that was supplied. */
3670 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3672 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3673 strcpy (stop_reason_rsp
, "tstop:");
3674 strcat (stop_reason_rsp
, buf3
);
3680 "tframes:%x;tcreated:%x;"
3681 "tfree:%x;tsize:%s;"
3684 "starttime:%s;stoptime:%s;"
3685 "username:%s;notes:%s:",
3687 stop_reason_rsp
, tracing_stop_tpnum
,
3688 traceframe_count
, traceframes_created
,
3689 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3690 circular_trace_buffer
,
3691 disconnected_tracing
,
3692 phex_nz (tracing_start_time
, sizeof (tracing_start_time
)),
3693 phex_nz (tracing_stop_time
, sizeof (tracing_stop_time
)),
3698 cmd_qtp (char *own_buf
)
3701 struct tracepoint
*tpoint
;
3702 char *packet
= own_buf
;
3704 packet
+= strlen ("qTP:");
3706 packet
= unpack_varlen_hex (packet
, &num
);
3707 ++packet
; /* skip a colon */
3708 packet
= unpack_varlen_hex (packet
, &addr
);
3710 /* See if we already have this tracepoint. */
3711 tpoint
= find_tracepoint (num
, addr
);
3715 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3716 (int) num
, paddress (addr
));
3717 write_enn (own_buf
);
3721 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3722 tpoint
->traceframe_usage
);
3725 /* State variables to help return all the tracepoint bits. */
3726 static struct tracepoint
*cur_tpoint
;
3727 static unsigned int cur_action
;
3728 static unsigned int cur_step_action
;
3729 static struct source_string
*cur_source_string
;
3730 static struct trace_state_variable
*cur_tsv
;
3732 /* Compose a response that is an imitation of the syntax by which the
3733 tracepoint was originally downloaded. */
3736 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3740 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3741 paddress (tpoint
->address
),
3742 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3743 tpoint
->pass_count
);
3744 if (tpoint
->type
== fast_tracepoint
)
3745 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3746 else if (tpoint
->type
== static_tracepoint
)
3747 sprintf (packet
+ strlen (packet
), ":S");
3751 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3752 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3753 tpoint
->cond
->length
, buf
);
3758 /* Compose a response that is an imitation of the syntax by which the
3759 tracepoint action was originally downloaded (with the difference
3760 that due to the way we store the actions, this will output a packet
3761 per action, while GDB could have combined more than one action
3765 response_action (char *packet
, struct tracepoint
*tpoint
,
3766 char *taction
, int step
)
3768 sprintf (packet
, "%c%x:%s:%s",
3769 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3773 /* Compose a response that is an imitation of the syntax by which the
3774 tracepoint source piece was originally downloaded. */
3777 response_source (char *packet
,
3778 struct tracepoint
*tpoint
, struct source_string
*src
)
3783 len
= strlen (src
->str
);
3784 buf
= alloca (len
* 2 + 1);
3785 bin2hex ((gdb_byte
*) src
->str
, buf
, len
);
3787 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3788 tpoint
->number
, paddress (tpoint
->address
),
3789 src
->type
, 0, len
, buf
);
3792 /* Return the first piece of tracepoint definition, and initialize the
3793 state machine that will iterate through all the tracepoint
3797 cmd_qtfp (char *packet
)
3799 trace_debug ("Returning first tracepoint definition piece");
3801 cur_tpoint
= tracepoints
;
3802 cur_action
= cur_step_action
= 0;
3803 cur_source_string
= NULL
;
3806 response_tracepoint (packet
, cur_tpoint
);
3808 strcpy (packet
, "l");
3811 /* Return additional pieces of tracepoint definition. Each action and
3812 stepping action must go into its own packet, because of packet size
3813 limits, and so we use state variables to deliver one piece at a
3817 cmd_qtsp (char *packet
)
3819 trace_debug ("Returning subsequent tracepoint definition piece");
3823 /* This case would normally never occur, but be prepared for
3825 strcpy (packet
, "l");
3827 else if (cur_action
< cur_tpoint
->numactions
)
3829 response_action (packet
, cur_tpoint
,
3830 cur_tpoint
->actions_str
[cur_action
], 0);
3833 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3835 response_action (packet
, cur_tpoint
,
3836 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3839 else if ((cur_source_string
3840 ? cur_source_string
->next
3841 : cur_tpoint
->source_strings
))
3843 if (cur_source_string
)
3844 cur_source_string
= cur_source_string
->next
;
3846 cur_source_string
= cur_tpoint
->source_strings
;
3847 response_source (packet
, cur_tpoint
, cur_source_string
);
3851 cur_tpoint
= cur_tpoint
->next
;
3852 cur_action
= cur_step_action
= 0;
3853 cur_source_string
= NULL
;
3855 response_tracepoint (packet
, cur_tpoint
);
3857 strcpy (packet
, "l");
3861 /* Compose a response that is an imitation of the syntax by which the
3862 trace state variable was originally downloaded. */
3865 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3867 char *buf
= (char *) "";
3872 namelen
= strlen (tsv
->name
);
3873 buf
= alloca (namelen
* 2 + 1);
3874 bin2hex ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3877 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3878 tsv
->getter
? 1 : 0, buf
);
3881 /* Return the first trace state variable definition, and initialize
3882 the state machine that will iterate through all the tsv bits. */
3885 cmd_qtfv (char *packet
)
3887 trace_debug ("Returning first trace state variable definition");
3889 cur_tsv
= trace_state_variables
;
3892 response_tsv (packet
, cur_tsv
);
3894 strcpy (packet
, "l");
3897 /* Return additional trace state variable definitions. */
3900 cmd_qtsv (char *packet
)
3902 trace_debug ("Returning additional trace state variable definition");
3906 cur_tsv
= cur_tsv
->next
;
3908 response_tsv (packet
, cur_tsv
);
3910 strcpy (packet
, "l");
3913 strcpy (packet
, "l");
3916 /* Return the first static tracepoint marker, and initialize the state
3917 machine that will iterate through all the static tracepoints
3921 cmd_qtfstm (char *packet
)
3923 if (!maybe_write_ipa_ust_not_loaded (packet
))
3924 run_inferior_command (packet
, strlen (packet
) + 1);
3927 /* Return additional static tracepoints markers. */
3930 cmd_qtsstm (char *packet
)
3932 if (!maybe_write_ipa_ust_not_loaded (packet
))
3933 run_inferior_command (packet
, strlen (packet
) + 1);
3936 /* Return the definition of the static tracepoint at a given address.
3937 Result packet is the same as qTsST's. */
3940 cmd_qtstmat (char *packet
)
3942 if (!maybe_write_ipa_ust_not_loaded (packet
))
3943 run_inferior_command (packet
, strlen (packet
) + 1);
3946 /* Sent the agent a command to close it. */
3949 gdb_agent_about_to_close (int pid
)
3951 char buf
[IPA_CMD_BUF_SIZE
];
3953 if (!maybe_write_ipa_not_loaded (buf
))
3955 struct thread_info
*save_inferior
;
3956 struct inferior_list_entry
*inf
= all_threads
.head
;
3958 save_inferior
= current_inferior
;
3960 /* Find a certain thread which belongs to process PID. */
3963 if (ptid_get_pid (inf
->id
) == pid
)
3968 current_inferior
= (struct thread_info
*) inf
;
3970 strcpy (buf
, "close");
3972 run_inferior_command (buf
, strlen (buf
) + 1);
3974 current_inferior
= save_inferior
;
3978 /* Return the minimum instruction size needed for fast tracepoints as a
3979 hexadecimal number. */
3982 cmd_qtminftpilen (char *packet
)
3984 if (current_inferior
== NULL
)
3986 /* Indicate that the minimum length is currently unknown. */
3987 strcpy (packet
, "0");
3991 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3994 /* Respond to qTBuffer packet with a block of raw data from the trace
3995 buffer. GDB may ask for a lot, but we are allowed to reply with
3996 only as much as will fit within packet limits or whatever. */
3999 cmd_qtbuffer (char *own_buf
)
4001 ULONGEST offset
, num
, tot
;
4003 char *packet
= own_buf
;
4005 packet
+= strlen ("qTBuffer:");
4007 packet
= unpack_varlen_hex (packet
, &offset
);
4008 ++packet
; /* skip a comma */
4009 unpack_varlen_hex (packet
, &num
);
4011 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
4012 (int) num
, phex_nz (offset
, 0));
4014 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
4016 /* If we're right at the end, reply specially that we're done. */
4019 strcpy (own_buf
, "l");
4023 /* Object to any other out-of-bounds request. */
4026 write_enn (own_buf
);
4030 /* Compute the pointer corresponding to the given offset, accounting
4032 tbp
= trace_buffer_start
+ offset
;
4033 if (tbp
>= trace_buffer_wrap
)
4034 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4036 /* Trim to the remaining bytes if we're close to the end. */
4037 if (num
> tot
- offset
)
4040 /* Trim to available packet size. */
4041 if (num
>= (PBUFSIZ
- 16) / 2 )
4042 num
= (PBUFSIZ
- 16) / 2;
4044 bin2hex (tbp
, own_buf
, num
);
4048 cmd_bigqtbuffer_circular (char *own_buf
)
4051 char *packet
= own_buf
;
4053 packet
+= strlen ("QTBuffer:circular:");
4055 unpack_varlen_hex (packet
, &val
);
4056 circular_trace_buffer
= val
;
4057 trace_debug ("Trace buffer is now %s",
4058 circular_trace_buffer
? "circular" : "linear");
4063 cmd_bigqtbuffer_size (char *own_buf
)
4067 char *packet
= own_buf
;
4069 /* Can't change the size during a tracing run. */
4072 write_enn (own_buf
);
4076 packet
+= strlen ("QTBuffer:size:");
4078 /* -1 is sent as literal "-1". */
4079 if (strcmp (packet
, "-1") == 0)
4080 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4083 unpack_varlen_hex (packet
, &val
);
4084 sval
= (LONGEST
) val
;
4087 init_trace_buffer (sval
);
4088 trace_debug ("Trace buffer is now %s bytes",
4089 plongest (trace_buffer_size
));
4094 cmd_qtnotes (char *own_buf
)
4097 char *saved
, *user
, *notes
, *stopnote
;
4098 char *packet
= own_buf
;
4100 packet
+= strlen ("QTNotes:");
4104 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4106 packet
+= strlen ("user:");
4108 packet
= strchr (packet
, ';');
4109 nbytes
= (packet
- saved
) / 2;
4110 user
= xmalloc (nbytes
+ 1);
4111 nbytes
= unhexify (user
, saved
, nbytes
);
4112 user
[nbytes
] = '\0';
4113 ++packet
; /* skip the semicolon */
4114 trace_debug ("User is '%s'", user
);
4115 xfree (tracing_user_name
);
4116 tracing_user_name
= user
;
4118 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4120 packet
+= strlen ("notes:");
4122 packet
= strchr (packet
, ';');
4123 nbytes
= (packet
- saved
) / 2;
4124 notes
= xmalloc (nbytes
+ 1);
4125 nbytes
= unhexify (notes
, saved
, nbytes
);
4126 notes
[nbytes
] = '\0';
4127 ++packet
; /* skip the semicolon */
4128 trace_debug ("Notes is '%s'", notes
);
4129 xfree (tracing_notes
);
4130 tracing_notes
= notes
;
4132 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4134 packet
+= strlen ("tstop:");
4136 packet
= strchr (packet
, ';');
4137 nbytes
= (packet
- saved
) / 2;
4138 stopnote
= xmalloc (nbytes
+ 1);
4139 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4140 stopnote
[nbytes
] = '\0';
4141 ++packet
; /* skip the semicolon */
4142 trace_debug ("tstop note is '%s'", stopnote
);
4143 xfree (tracing_stop_note
);
4144 tracing_stop_note
= stopnote
;
4154 handle_tracepoint_general_set (char *packet
)
4156 if (strcmp ("QTinit", packet
) == 0)
4158 cmd_qtinit (packet
);
4161 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4166 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4168 cmd_qtdpsrc (packet
);
4171 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4173 cmd_qtenable_disable (packet
, 1);
4176 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4178 cmd_qtenable_disable (packet
, 0);
4181 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4186 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4191 else if (strcmp ("QTStart", packet
) == 0)
4193 cmd_qtstart (packet
);
4196 else if (strcmp ("QTStop", packet
) == 0)
4198 cmd_qtstop (packet
);
4201 else if (strncmp ("QTDisconnected:", packet
,
4202 strlen ("QTDisconnected:")) == 0)
4204 cmd_qtdisconnected (packet
);
4207 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4209 cmd_qtframe (packet
);
4212 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4214 cmd_bigqtbuffer_circular (packet
);
4217 else if (strncmp ("QTBuffer:size:", packet
, strlen ("QTBuffer:size:")) == 0)
4219 cmd_bigqtbuffer_size (packet
);
4222 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4224 cmd_qtnotes (packet
);
4232 handle_tracepoint_query (char *packet
)
4234 if (strcmp ("qTStatus", packet
) == 0)
4236 cmd_qtstatus (packet
);
4239 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4244 else if (strcmp ("qTfP", packet
) == 0)
4249 else if (strcmp ("qTsP", packet
) == 0)
4254 else if (strcmp ("qTfV", packet
) == 0)
4259 else if (strcmp ("qTsV", packet
) == 0)
4264 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4269 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4271 cmd_qtbuffer (packet
);
4274 else if (strcmp ("qTfSTM", packet
) == 0)
4276 cmd_qtfstm (packet
);
4279 else if (strcmp ("qTsSTM", packet
) == 0)
4281 cmd_qtsstm (packet
);
4284 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4286 cmd_qtstmat (packet
);
4289 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4291 cmd_qtminftpilen (packet
);
4299 #ifndef IN_PROCESS_AGENT
4301 /* Call this when thread TINFO has hit the tracepoint defined by
4302 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4303 action. This adds a while-stepping collecting state item to the
4304 threads' collecting state list, so that we can keep track of
4305 multiple simultaneous while-stepping actions being collected by the
4306 same thread. This can happen in cases like:
4308 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4310 ff0003 INSN3 <-- TP2, collect $regs
4311 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4314 Notice that when instruction INSN5 is reached, the while-stepping
4315 actions of both TP1 and TP3 are still being collected, and that TP2
4316 had been collected meanwhile. The whole range of ff0001-ff0005
4317 should be single-stepped, due to at least TP1's while-stepping
4318 action covering the whole range. */
4321 add_while_stepping_state (struct thread_info
*tinfo
,
4322 int tp_number
, CORE_ADDR tp_address
)
4324 struct wstep_state
*wstep
;
4326 wstep
= xmalloc (sizeof (*wstep
));
4327 wstep
->next
= tinfo
->while_stepping
;
4329 wstep
->tp_number
= tp_number
;
4330 wstep
->tp_address
= tp_address
;
4331 wstep
->current_step
= 0;
4333 tinfo
->while_stepping
= wstep
;
4336 /* Release the while-stepping collecting state WSTEP. */
4339 release_while_stepping_state (struct wstep_state
*wstep
)
4344 /* Release all while-stepping collecting states currently associated
4345 with thread TINFO. */
4348 release_while_stepping_state_list (struct thread_info
*tinfo
)
4350 struct wstep_state
*head
;
4352 while (tinfo
->while_stepping
)
4354 head
= tinfo
->while_stepping
;
4355 tinfo
->while_stepping
= head
->next
;
4356 release_while_stepping_state (head
);
4360 /* If TINFO was handling a 'while-stepping' action, the step has
4361 finished, so collect any step data needed, and check if any more
4362 steps are required. Return true if the thread was indeed
4363 collecting tracepoint data, false otherwise. */
4366 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4368 struct tracepoint
*tpoint
;
4369 struct wstep_state
*wstep
;
4370 struct wstep_state
**wstep_link
;
4371 struct trap_tracepoint_ctx ctx
;
4373 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4375 if (agent_loaded_p ())
4376 upload_fast_traceframes ();
4378 /* Check if we were indeed collecting data for one of more
4379 tracepoints with a 'while-stepping' count. */
4380 if (tinfo
->while_stepping
== NULL
)
4385 /* We're not even tracing anymore. Stop this thread from
4387 release_while_stepping_state_list (tinfo
);
4389 /* The thread had stopped due to a single-step request indeed
4390 explained by a tracepoint. */
4394 wstep
= tinfo
->while_stepping
;
4395 wstep_link
= &tinfo
->while_stepping
;
4397 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4398 target_pid_to_str (tinfo
->entry
.id
),
4399 wstep
->tp_number
, paddress (wstep
->tp_address
));
4401 ctx
.base
.type
= trap_tracepoint
;
4402 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4404 while (wstep
!= NULL
)
4406 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4409 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4410 wstep
->tp_number
, paddress (wstep
->tp_address
),
4411 target_pid_to_str (tinfo
->entry
.id
));
4414 *wstep_link
= wstep
->next
;
4415 release_while_stepping_state (wstep
);
4416 wstep
= *wstep_link
;
4420 /* We've just finished one step. */
4421 ++wstep
->current_step
;
4424 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4425 stop_pc
, tpoint
, wstep
->current_step
);
4427 if (wstep
->current_step
>= tpoint
->step_count
)
4429 /* The requested numbers of steps have occurred. */
4430 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4431 target_pid_to_str (tinfo
->entry
.id
),
4432 wstep
->tp_number
, paddress (wstep
->tp_address
));
4434 /* Unlink the wstep. */
4435 *wstep_link
= wstep
->next
;
4436 release_while_stepping_state (wstep
);
4437 wstep
= *wstep_link
;
4439 /* Only check the hit count now, which ensure that we do all
4440 our stepping before stopping the run. */
4441 if (tpoint
->pass_count
> 0
4442 && tpoint
->hit_count
>= tpoint
->pass_count
4443 && stopping_tracepoint
== NULL
)
4444 stopping_tracepoint
= tpoint
;
4448 /* Keep single-stepping until the requested numbers of steps
4450 wstep_link
= &wstep
->next
;
4451 wstep
= *wstep_link
;
4454 if (stopping_tracepoint
4455 || trace_buffer_is_full
4456 || expr_eval_result
!= expr_eval_no_error
)
4466 /* Handle any internal tracing control breakpoint hits. That means,
4467 pull traceframes from the IPA to our buffer, and syncing both
4468 tracing agents when the IPA's tracing stops for some reason. */
4471 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4473 /* Pull in fast tracepoint trace frames from the inferior in-process
4474 agent's buffer into our buffer. */
4476 if (!agent_loaded_p ())
4479 upload_fast_traceframes ();
4481 /* Check if the in-process agent had decided we should stop
4483 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4485 int ipa_trace_buffer_is_full
;
4486 CORE_ADDR ipa_stopping_tracepoint
;
4487 int ipa_expr_eval_result
;
4488 CORE_ADDR ipa_error_tracepoint
;
4490 trace_debug ("lib stopped at stop_tracing");
4492 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4493 &ipa_trace_buffer_is_full
);
4495 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4496 &ipa_stopping_tracepoint
);
4497 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4499 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4500 &ipa_error_tracepoint
);
4501 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4503 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4504 &ipa_expr_eval_result
);
4505 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4507 trace_debug ("lib: trace_buffer_is_full: %d, "
4508 "stopping_tracepoint: %s, "
4509 "ipa_expr_eval_result: %d, "
4510 "error_tracepoint: %s, ",
4511 ipa_trace_buffer_is_full
,
4512 paddress (ipa_stopping_tracepoint
),
4513 ipa_expr_eval_result
,
4514 paddress (ipa_error_tracepoint
));
4518 if (ipa_trace_buffer_is_full
)
4519 trace_debug ("lib stopped due to full buffer.");
4520 if (ipa_stopping_tracepoint
)
4521 trace_debug ("lib stopped due to tpoint");
4522 if (ipa_stopping_tracepoint
)
4523 trace_debug ("lib stopped due to error");
4526 if (ipa_stopping_tracepoint
!= 0)
4529 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4531 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4533 expr_eval_result
= ipa_expr_eval_result
;
4535 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4540 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4542 trace_debug ("lib stopped at flush_trace_buffer");
4549 /* Return true if TINFO just hit a tracepoint. Collect data if
4553 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4555 struct tracepoint
*tpoint
;
4557 struct trap_tracepoint_ctx ctx
;
4559 /* Not tracing, don't handle. */
4563 ctx
.base
.type
= trap_tracepoint
;
4564 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4566 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4568 /* Note that we collect fast tracepoints here as well. We'll
4569 step over the fast tracepoint jump later, which avoids the
4570 double collect. However, we don't collect for static
4571 tracepoints here, because UST markers are compiled in program,
4572 and probes will be executed in program. So static tracepoints
4573 are collected there. */
4574 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4575 && tpoint
->type
!= static_tracepoint
)
4577 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4578 target_pid_to_str (tinfo
->entry
.id
),
4579 tpoint
->number
, paddress (tpoint
->address
));
4581 /* Test the condition if present, and collect if true. */
4583 || (condition_true_at_tracepoint
4584 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4585 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4588 if (stopping_tracepoint
4589 || trace_buffer_is_full
4590 || expr_eval_result
!= expr_eval_no_error
)
4594 /* If the tracepoint had a 'while-stepping' action, then set
4595 the thread to collect this tracepoint on the following
4597 else if (tpoint
->step_count
> 0)
4599 add_while_stepping_state (tinfo
,
4600 tpoint
->number
, tpoint
->address
);
4612 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4613 struct ust_marker_data
;
4614 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4615 struct traceframe
*tframe
);
4618 /* Create a trace frame for the hit of the given tracepoint in the
4622 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4623 struct tracepoint
*tpoint
)
4625 struct traceframe
*tframe
;
4628 /* Only count it as a hit when we actually collect data. */
4629 tpoint
->hit_count
++;
4631 /* If we've exceeded a defined pass count, record the event for
4632 later, and finish the collection for this hit. This test is only
4633 for nonstepping tracepoints, stepping tracepoints test at the end
4634 of their while-stepping loop. */
4635 if (tpoint
->pass_count
> 0
4636 && tpoint
->hit_count
>= tpoint
->pass_count
4637 && tpoint
->step_count
== 0
4638 && stopping_tracepoint
== NULL
)
4639 stopping_tracepoint
= tpoint
;
4641 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4642 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4644 tframe
= add_traceframe (tpoint
);
4648 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4650 #ifndef IN_PROCESS_AGENT
4651 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4652 tpoint
->number
, paddress (tpoint
->address
),
4653 tpoint
->actions_str
[acti
]);
4656 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4657 tpoint
->actions
[acti
]);
4660 finish_traceframe (tframe
);
4663 if (tframe
== NULL
&& tracing
)
4664 trace_buffer_is_full
= 1;
4667 #ifndef IN_PROCESS_AGENT
4670 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4672 struct tracepoint
*tpoint
, int current_step
)
4674 struct traceframe
*tframe
;
4677 trace_debug ("Making new step traceframe for "
4678 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4679 tpoint
->number
, paddress (tpoint
->address
),
4680 current_step
, tpoint
->step_count
,
4683 tframe
= add_traceframe (tpoint
);
4687 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4689 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4690 tpoint
->number
, paddress (tpoint
->address
),
4691 tpoint
->step_actions_str
[acti
]);
4693 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4694 tpoint
->step_actions
[acti
]);
4697 finish_traceframe (tframe
);
4700 if (tframe
== NULL
&& tracing
)
4701 trace_buffer_is_full
= 1;
4706 #ifdef IN_PROCESS_AGENT
4707 /* The target description used by the IPA. Given that the IPA library
4708 is built for a specific architecture that is loaded into the
4709 inferior, there only needs to be one such description per
4711 const struct target_desc
*ipa_tdesc
;
4714 static struct regcache
*
4715 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4717 struct regcache
*regcache
= NULL
;
4719 #ifdef IN_PROCESS_AGENT
4720 if (ctx
->type
== fast_tracepoint
)
4722 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4723 if (!fctx
->regcache_initted
)
4725 fctx
->regcache_initted
= 1;
4726 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4727 supply_regblock (&fctx
->regcache
, NULL
);
4728 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4730 regcache
= &fctx
->regcache
;
4733 if (ctx
->type
== static_tracepoint
)
4735 struct static_tracepoint_ctx
*sctx
4736 = (struct static_tracepoint_ctx
*) ctx
;
4738 if (!sctx
->regcache_initted
)
4740 sctx
->regcache_initted
= 1;
4741 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4742 supply_regblock (&sctx
->regcache
, NULL
);
4743 /* Pass down the tracepoint address, because REGS doesn't
4744 include the PC, but we know what it must have been. */
4745 supply_static_tracepoint_registers (&sctx
->regcache
,
4746 (const unsigned char *)
4748 sctx
->tpoint
->address
);
4750 regcache
= &sctx
->regcache
;
4754 if (ctx
->type
== trap_tracepoint
)
4756 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4757 regcache
= tctx
->regcache
;
4761 gdb_assert (regcache
!= NULL
);
4767 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4769 struct tracepoint
*tpoint
,
4770 struct traceframe
*tframe
,
4771 struct tracepoint_action
*taction
)
4773 enum eval_result_type err
;
4775 switch (taction
->type
)
4779 struct collect_memory_action
*maction
;
4780 struct eval_agent_expr_context ax_ctx
;
4782 maction
= (struct collect_memory_action
*) taction
;
4783 ax_ctx
.regcache
= NULL
;
4784 ax_ctx
.tframe
= tframe
;
4785 ax_ctx
.tpoint
= tpoint
;
4787 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4788 pulongest (maction
->len
),
4789 paddress (maction
->addr
), maction
->basereg
);
4790 /* (should use basereg) */
4791 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4797 unsigned char *regspace
;
4798 struct regcache tregcache
;
4799 struct regcache
*context_regcache
;
4802 trace_debug ("Want to collect registers");
4804 context_regcache
= get_context_regcache (ctx
);
4805 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4807 /* Collect all registers for now. */
4808 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4809 if (regspace
== NULL
)
4811 trace_debug ("Trace buffer block allocation failed, skipping");
4814 /* Identify a register block. */
4817 /* Wrap the regblock in a register cache (in the stack, we
4818 don't want to malloc here). */
4819 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4822 /* Copy the register data to the regblock. */
4823 regcache_cpy (&tregcache
, context_regcache
);
4825 #ifndef IN_PROCESS_AGENT
4826 /* On some platforms, trap-based tracepoints will have the PC
4827 pointing to the next instruction after the trap, but we
4828 don't want the user or GDB trying to guess whether the
4829 saved PC needs adjusting; so always record the adjusted
4830 stop_pc. Note that we can't use tpoint->address instead,
4831 since it will be wrong for while-stepping actions. This
4832 adjustment is a nop for fast tracepoints collected from the
4833 in-process lib (but not if GDBserver is collecting one
4834 preemptively), since the PC had already been adjusted to
4835 contain the tracepoint's address by the jump pad. */
4836 trace_debug ("Storing stop pc (0x%s) in regblock",
4837 paddress (stop_pc
));
4839 /* This changes the regblock, not the thread's
4841 regcache_write_pc (&tregcache
, stop_pc
);
4847 struct eval_expr_action
*eaction
;
4848 struct eval_agent_expr_context ax_ctx
;
4850 eaction
= (struct eval_expr_action
*) taction
;
4851 ax_ctx
.regcache
= get_context_regcache (ctx
);
4852 ax_ctx
.tframe
= tframe
;
4853 ax_ctx
.tpoint
= tpoint
;
4855 trace_debug ("Want to evaluate expression");
4857 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4859 if (err
!= expr_eval_no_error
)
4861 record_tracepoint_error (tpoint
, "action expression", err
);
4868 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4869 trace_debug ("Want to collect static trace data");
4870 collect_ust_data_at_tracepoint (ctx
, tframe
);
4872 trace_debug ("warning: collecting static trace data, "
4873 "but static tracepoints are not supported");
4878 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4884 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4885 struct tracepoint
*tpoint
)
4888 enum eval_result_type err
;
4890 /* Presently, gdbserver doesn't run compiled conditions, only the
4891 IPA does. If the program stops at a fast tracepoint's address
4892 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4893 gdbserver preemptively collect the fast tracepoint. Later, on
4894 resume, gdbserver steps over the fast tracepoint like it steps
4895 over breakpoints, so that the IPA doesn't see that fast
4896 tracepoint. This avoids double collects of fast tracepoints in
4897 that stopping scenario. Having gdbserver itself handle the fast
4898 tracepoint gives the user a consistent view of when fast or trap
4899 tracepoints are collected, compared to an alternative where only
4900 trap tracepoints are collected on stop, and fast tracepoints on
4901 resume. When a fast tracepoint is being processed by gdbserver,
4902 it is always the non-compiled condition expression that is
4904 #ifdef IN_PROCESS_AGENT
4905 if (tpoint
->compiled_cond
)
4906 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4910 struct eval_agent_expr_context ax_ctx
;
4912 ax_ctx
.regcache
= get_context_regcache (ctx
);
4913 ax_ctx
.tframe
= NULL
;
4914 ax_ctx
.tpoint
= tpoint
;
4916 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4918 if (err
!= expr_eval_no_error
)
4920 record_tracepoint_error (tpoint
, "condition", err
);
4921 /* The error case must return false. */
4925 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4926 tpoint
->number
, paddress (tpoint
->address
),
4928 return (value
? 1 : 0);
4931 /* Do memory copies for bytecodes. */
4932 /* Do the recording of memory blocks for actions and bytecodes. */
4935 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4936 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4938 unsigned char *mspace
;
4939 ULONGEST remaining
= len
;
4940 unsigned short blocklen
;
4942 /* If a 'to' buffer is specified, use it. */
4945 read_inferior_memory (from
, to
, len
);
4949 /* Otherwise, create a new memory block in the trace buffer. */
4950 while (remaining
> 0)
4954 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4955 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4956 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4959 /* Identify block as a memory block. */
4962 /* Record address and size. */
4963 memcpy (mspace
, &from
, sizeof (from
));
4964 mspace
+= sizeof (from
);
4965 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4966 mspace
+= sizeof (blocklen
);
4967 /* Record the memory block proper. */
4968 read_inferior_memory (from
, mspace
, blocklen
);
4969 trace_debug ("%d bytes recorded", blocklen
);
4970 remaining
-= blocklen
;
4977 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4978 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4980 unsigned char *buf
, *mspace
;
4981 ULONGEST remaining
= len
;
4982 unsigned short blocklen
, i
;
4984 /* To save a bit of space, block lengths are 16-bit, so break large
4985 requests into multiple blocks. Bordering on overkill for strings,
4986 but it could happen that someone specifies a large max length. */
4987 while (remaining
> 0)
4991 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4992 /* We want working space to accumulate nonzero bytes, since
4993 traceframes must have a predecided size (otherwise it gets
4994 harder to wrap correctly for the circular case, etc). */
4995 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4996 for (i
= 0; i
< blocklen
; ++i
)
4998 /* Read the string one byte at a time, in case the string is
4999 at the end of a valid memory area - we don't want a
5000 correctly-terminated string to engender segvio
5002 read_inferior_memory (from
+ i
, buf
+ i
, 1);
5007 /* Make sure outer loop stops now too. */
5008 remaining
= blocklen
;
5012 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
5013 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5019 /* Identify block as a memory block. */
5022 /* Record address and size. */
5023 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5024 mspace
+= sizeof (from
);
5025 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5026 mspace
+= sizeof (blocklen
);
5027 /* Copy the string contents. */
5028 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5029 remaining
-= blocklen
;
5036 /* Record the value of a trace state variable. */
5039 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5041 unsigned char *vspace
;
5044 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5045 1 + sizeof (n
) + sizeof (LONGEST
));
5048 /* Identify block as a variable. */
5050 /* Record variable's number and value. */
5051 memcpy (vspace
+ 1, &n
, sizeof (n
));
5052 val
= get_trace_state_variable_value (n
);
5053 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5054 trace_debug ("Variable %d recorded", n
);
5058 #ifndef IN_PROCESS_AGENT
5060 /* Callback for traceframe_walk_blocks, used to find a given block
5061 type in a traceframe. */
5064 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5066 char *wantedp
= data
;
5068 if (*wantedp
== blocktype
)
5074 /* Walk over all traceframe blocks of the traceframe buffer starting
5075 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5076 block found, passing in DATA unmodified. If CALLBACK returns true,
5077 this returns a pointer to where the block is found. Returns NULL
5078 if no callback call returned true, indicating that all blocks have
5081 static unsigned char *
5082 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5084 int (*callback
) (char blocktype
,
5085 unsigned char *dataptr
,
5089 unsigned char *dataptr
;
5093 trace_debug ("traceframe %d has no data", tfnum
);
5097 /* Iterate through a traceframe's blocks, looking for a block of the
5099 for (dataptr
= database
;
5100 dataptr
< database
+ datasize
;
5104 unsigned short mlen
;
5106 if (dataptr
== trace_buffer_wrap
)
5108 /* Adjust to reflect wrapping part of the frame around to
5110 datasize
= dataptr
- database
;
5111 dataptr
= database
= trace_buffer_lo
;
5114 blocktype
= *dataptr
++;
5116 if ((*callback
) (blocktype
, dataptr
, data
))
5122 /* Skip over the registers block. */
5123 dataptr
+= current_target_desc ()->registers_size
;
5126 /* Skip over the memory block. */
5127 dataptr
+= sizeof (CORE_ADDR
);
5128 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5129 dataptr
+= (sizeof (mlen
) + mlen
);
5132 /* Skip over the TSV block. */
5133 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5136 /* Skip over the static trace data block. */
5137 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5138 dataptr
+= (sizeof (mlen
) + mlen
);
5141 trace_debug ("traceframe %d has unknown block type 0x%x",
5150 /* Look for the block of type TYPE_WANTED in the trameframe starting
5151 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5154 static unsigned char *
5155 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5156 int tfnum
, char type_wanted
)
5158 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5159 match_blocktype
, &type_wanted
);
5162 static unsigned char *
5163 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5165 unsigned char *regblock
;
5167 regblock
= traceframe_find_block_type (tframe
->data
,
5171 if (regblock
== NULL
)
5172 trace_debug ("traceframe %d has no register data", tfnum
);
5177 /* Get registers from a traceframe. */
5180 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5182 unsigned char *dataptr
;
5183 struct tracepoint
*tpoint
;
5184 struct traceframe
*tframe
;
5186 tframe
= find_traceframe (tfnum
);
5190 trace_debug ("traceframe %d not found", tfnum
);
5194 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5195 if (dataptr
== NULL
)
5197 /* Mark registers unavailable. */
5198 supply_regblock (regcache
, NULL
);
5200 /* We can generally guess at a PC, although this will be
5201 misleading for while-stepping frames and multi-location
5203 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5205 regcache_write_pc (regcache
, tpoint
->address
);
5208 supply_regblock (regcache
, dataptr
);
5214 traceframe_get_pc (struct traceframe
*tframe
)
5216 struct regcache regcache
;
5217 unsigned char *dataptr
;
5218 const struct target_desc
*tdesc
= current_target_desc ();
5220 dataptr
= traceframe_find_regblock (tframe
, -1);
5221 if (dataptr
== NULL
)
5224 init_register_cache (®cache
, tdesc
, dataptr
);
5225 return regcache_read_pc (®cache
);
5228 /* Read a requested block of memory from a trace frame. */
5231 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5232 unsigned char *buf
, ULONGEST length
,
5235 struct traceframe
*tframe
;
5236 unsigned char *database
, *dataptr
;
5237 unsigned int datasize
;
5239 unsigned short mlen
;
5241 trace_debug ("traceframe_read_mem");
5243 tframe
= find_traceframe (tfnum
);
5247 trace_debug ("traceframe %d not found", tfnum
);
5251 datasize
= tframe
->data_size
;
5252 database
= dataptr
= &tframe
->data
[0];
5254 /* Iterate through a traceframe's blocks, looking for memory. */
5255 while ((dataptr
= traceframe_find_block_type (dataptr
,
5257 - (dataptr
- database
),
5258 tfnum
, 'M')) != NULL
)
5260 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5261 dataptr
+= sizeof (maddr
);
5262 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5263 dataptr
+= sizeof (mlen
);
5264 trace_debug ("traceframe %d has %d bytes at %s",
5265 tfnum
, mlen
, paddress (maddr
));
5267 /* If the block includes the first part of the desired range,
5268 return as much it has; GDB will re-request the remainder,
5269 which might be in a different block of this trace frame. */
5270 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5272 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5276 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5281 /* Skip over this block. */
5285 trace_debug ("traceframe %d has no memory data for the desired region",
5293 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5296 struct traceframe
*tframe
;
5297 unsigned char *database
, *dataptr
;
5298 unsigned int datasize
;
5302 trace_debug ("traceframe_read_tsv");
5304 tfnum
= current_traceframe
;
5308 trace_debug ("no current traceframe");
5312 tframe
= find_traceframe (tfnum
);
5316 trace_debug ("traceframe %d not found", tfnum
);
5320 datasize
= tframe
->data_size
;
5321 database
= dataptr
= &tframe
->data
[0];
5323 /* Iterate through a traceframe's blocks, looking for the last
5325 while ((dataptr
= traceframe_find_block_type (dataptr
,
5327 - (dataptr
- database
),
5328 tfnum
, 'V')) != NULL
)
5330 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5331 dataptr
+= sizeof (vnum
);
5333 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5335 /* Check that this is the variable we want. */
5338 memcpy (val
, dataptr
, sizeof (*val
));
5342 /* Skip over this block. */
5343 dataptr
+= sizeof (LONGEST
);
5347 trace_debug ("traceframe %d has no data for variable %d",
5352 /* Read a requested block of static tracepoint data from a trace
5356 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5357 unsigned char *buf
, ULONGEST length
,
5360 struct traceframe
*tframe
;
5361 unsigned char *database
, *dataptr
;
5362 unsigned int datasize
;
5363 unsigned short mlen
;
5365 trace_debug ("traceframe_read_sdata");
5367 tframe
= find_traceframe (tfnum
);
5371 trace_debug ("traceframe %d not found", tfnum
);
5375 datasize
= tframe
->data_size
;
5376 database
= &tframe
->data
[0];
5378 /* Iterate through a traceframe's blocks, looking for static
5380 dataptr
= traceframe_find_block_type (database
, datasize
,
5382 if (dataptr
!= NULL
)
5384 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5385 dataptr
+= sizeof (mlen
);
5388 if (offset
+ length
> mlen
)
5389 length
= mlen
- offset
;
5391 memcpy (buf
, dataptr
, length
);
5399 trace_debug ("traceframe %d has no static trace data", tfnum
);
5405 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5406 object. DATA is pointer to a struct buffer holding the
5407 traceframe-info object being built. */
5410 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5412 struct buffer
*buffer
= data
;
5418 unsigned short mlen
;
5421 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5422 dataptr
+= sizeof (maddr
);
5423 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5424 dataptr
+= sizeof (mlen
);
5425 buffer_xml_printf (buffer
,
5426 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5427 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5434 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5435 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5444 warning ("Unhandled trace block type (%d) '%c ' "
5445 "while building trace frame info.",
5446 blocktype
, blocktype
);
5453 /* Build a traceframe-info object for traceframe number TFNUM into
5457 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5459 struct traceframe
*tframe
;
5461 trace_debug ("traceframe_read_info");
5463 tframe
= find_traceframe (tfnum
);
5467 trace_debug ("traceframe %d not found", tfnum
);
5471 buffer_grow_str (buffer
, "<traceframe-info>\n");
5472 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5473 tfnum
, build_traceframe_info_xml
, buffer
);
5474 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5478 /* Return the first fast tracepoint whose jump pad contains PC. */
5480 static struct tracepoint
*
5481 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5483 struct tracepoint
*tpoint
;
5485 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5486 if (tpoint
->type
== fast_tracepoint
)
5487 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5493 /* Return the first fast tracepoint whose trampoline contains PC. */
5495 static struct tracepoint
*
5496 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5498 struct tracepoint
*tpoint
;
5500 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5502 if (tpoint
->type
== fast_tracepoint
5503 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5510 /* Return GDBserver's tracepoint that matches the IP Agent's
5511 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5514 static struct tracepoint
*
5515 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5517 struct tracepoint
*tpoint
;
5519 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5520 if (tpoint
->type
== fast_tracepoint
)
5521 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5529 /* The type of the object that is used to synchronize fast tracepoint
5532 typedef struct collecting_t
5534 /* The fast tracepoint number currently collecting. */
5537 /* A number that GDBserver can use to identify the thread that is
5538 presently holding the collect lock. This need not (and usually
5539 is not) the thread id, as getting the current thread ID usually
5540 requires a system call, which we want to avoid like the plague.
5541 Usually this is thread's TCB, found in the TLS (pseudo-)
5542 register, which is readable with a single insn on several
5544 uintptr_t thread_area
;
5547 #ifndef IN_PROCESS_AGENT
5550 force_unlock_trace_buffer (void)
5552 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5555 /* Check if the thread identified by THREAD_AREA which is stopped at
5556 STOP_PC, is presently locking the fast tracepoint collection, and
5557 if so, gather some status of said collection. Returns 0 if the
5558 thread isn't collecting or in the jump pad at all. 1, if in the
5559 jump pad (or within gdb_collect) and hasn't executed the adjusted
5560 original insn yet (can set a breakpoint there and run to it). 2,
5561 if presently executing the adjusted original insn --- in which
5562 case, if we want to move the thread out of the jump pad, we need to
5563 single-step it until this function returns 0. */
5566 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5568 struct fast_tpoint_collect_status
*status
)
5570 CORE_ADDR ipa_collecting
;
5571 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5572 CORE_ADDR ipa_gdb_trampoline_buffer
;
5573 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5574 struct tracepoint
*tpoint
;
5575 int needs_breakpoint
;
5577 /* The thread THREAD_AREA is either:
5579 0. not collecting at all, not within the jump pad, or within
5580 gdb_collect or one of its callees.
5582 1. in the jump pad and haven't reached gdb_collect
5584 2. within gdb_collect (out of the jump pad) (collect is set)
5586 3. we're in the jump pad, after gdb_collect having returned,
5587 possibly executing the adjusted insns.
5589 For cases 1 and 3, `collecting' may or not be set. The jump pad
5590 doesn't have any complicated jump logic, so we can tell if the
5591 thread is executing the adjust original insn or not by just
5592 matching STOP_PC with known jump pad addresses. If we it isn't
5593 yet executing the original insn, set a breakpoint there, and let
5594 the thread run to it, so to quickly step over a possible (many
5595 insns) gdb_collect call. Otherwise, or when the breakpoint is
5596 hit, only a few (small number of) insns are left to be executed
5597 in the jump pad. Single-step the thread until it leaves the
5602 needs_breakpoint
= 0;
5603 trace_debug ("fast_tracepoint_collecting");
5605 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5606 &ipa_gdb_jump_pad_buffer
))
5607 fatal ("error extracting `gdb_jump_pad_buffer'");
5608 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5609 &ipa_gdb_jump_pad_buffer_end
))
5610 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5612 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5613 &ipa_gdb_trampoline_buffer
))
5614 fatal ("error extracting `gdb_trampoline_buffer'");
5615 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5616 &ipa_gdb_trampoline_buffer_end
))
5617 fatal ("error extracting `gdb_trampoline_buffer_end'");
5619 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5620 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5622 /* We can tell which tracepoint(s) the thread is collecting by
5623 matching the jump pad address back to the tracepoint. */
5624 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5627 warning ("in jump pad, but no matching tpoint?");
5632 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5634 tpoint
->number
, paddress (tpoint
->address
),
5635 paddress (tpoint
->jump_pad
),
5636 paddress (tpoint
->jump_pad_end
),
5637 paddress (tpoint
->adjusted_insn_addr
),
5638 paddress (tpoint
->adjusted_insn_addr_end
));
5641 /* Definitely in the jump pad. May or may not need
5642 fast-exit-jump-pad breakpoint. */
5643 if (tpoint
->jump_pad
<= stop_pc
5644 && stop_pc
< tpoint
->adjusted_insn_addr
)
5645 needs_breakpoint
= 1;
5647 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5648 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5650 /* We can tell which tracepoint(s) the thread is collecting by
5651 matching the trampoline address back to the tracepoint. */
5652 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5655 warning ("in trampoline, but no matching tpoint?");
5660 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5661 tpoint
->number
, paddress (tpoint
->address
),
5662 paddress (tpoint
->trampoline
),
5663 paddress (tpoint
->trampoline_end
));
5666 /* Have not reached jump pad yet, but treat the trampoline as a
5667 part of the jump pad that is before the adjusted original
5669 needs_breakpoint
= 1;
5673 collecting_t ipa_collecting_obj
;
5675 /* If `collecting' is set/locked, then the THREAD_AREA thread
5676 may or not be the one holding the lock. We have to read the
5677 lock to find out. */
5679 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5682 trace_debug ("fast_tracepoint_collecting:"
5683 " failed reading 'collecting' in the inferior");
5687 if (!ipa_collecting
)
5689 trace_debug ("fast_tracepoint_collecting: not collecting"
5690 " (and nobody is).");
5694 /* Some thread is collecting. Check which. */
5695 if (read_inferior_memory (ipa_collecting
,
5696 (unsigned char *) &ipa_collecting_obj
,
5697 sizeof (ipa_collecting_obj
)) != 0)
5700 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5702 trace_debug ("fast_tracepoint_collecting: not collecting "
5703 "(another thread is)");
5708 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5711 warning ("fast_tracepoint_collecting: collecting, "
5712 "but tpoint %s not found?",
5713 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5717 /* The thread is within `gdb_collect', skip over the rest of
5718 fast tracepoint collection quickly using a breakpoint. */
5719 needs_breakpoint
= 1;
5722 /* The caller wants a bit of status detail. */
5725 status
->tpoint_num
= tpoint
->number
;
5726 status
->tpoint_addr
= tpoint
->address
;
5727 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5728 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5731 if (needs_breakpoint
)
5733 /* Hasn't executed the original instruction yet. Set breakpoint
5734 there, and wait till it's hit, then single-step until exiting
5738 fast_tracepoint_collecting, returning continue-until-break at %s",
5739 paddress (tpoint
->adjusted_insn_addr
));
5741 return 1; /* continue */
5745 /* Just single-step until exiting the jump pad. */
5747 trace_debug ("fast_tracepoint_collecting, returning "
5748 "need-single-step (%s-%s)",
5749 paddress (tpoint
->adjusted_insn_addr
),
5750 paddress (tpoint
->adjusted_insn_addr_end
));
5752 return 2; /* single-step */
5758 #ifdef IN_PROCESS_AGENT
5760 /* The global fast tracepoint collect lock. Points to a collecting_t
5761 object built on the stack by the jump pad, if presently locked;
5762 NULL if it isn't locked. Note that this lock *must* be set while
5763 executing any *function other than the jump pad. See
5764 fast_tracepoint_collecting. */
5765 static collecting_t
* ATTR_USED collecting
;
5767 /* This routine, called from the jump pad (in asm) is designed to be
5768 called from the jump pads of fast tracepoints, thus it is on the
5771 IP_AGENT_EXPORT
void ATTR_USED
5772 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5774 struct fast_tracepoint_ctx ctx
;
5776 /* Don't do anything until the trace run is completely set up. */
5780 ctx
.base
.type
= fast_tracepoint
;
5782 ctx
.regcache_initted
= 0;
5783 /* Wrap the regblock in a register cache (in the stack, we don't
5784 want to malloc here). */
5785 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
5786 if (ctx
.regspace
== NULL
)
5788 trace_debug ("Trace buffer block allocation failed, skipping");
5792 for (ctx
.tpoint
= tpoint
;
5793 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5794 ctx
.tpoint
= ctx
.tpoint
->next
)
5796 if (!ctx
.tpoint
->enabled
)
5799 /* Multiple tracepoints of different types, such as fast tracepoint and
5800 static tracepoint, can be set at the same address. */
5801 if (ctx
.tpoint
->type
!= tpoint
->type
)
5804 /* Test the condition if present, and collect if true. */
5805 if (ctx
.tpoint
->cond
== NULL
5806 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5809 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5810 ctx
.tpoint
->address
, ctx
.tpoint
);
5812 /* Note that this will cause original insns to be written back
5813 to where we jumped from, but that's OK because we're jumping
5814 back to the next whole instruction. This will go badly if
5815 instruction restoration is not atomic though. */
5816 if (stopping_tracepoint
5817 || trace_buffer_is_full
5818 || expr_eval_result
!= expr_eval_no_error
)
5826 /* If there was a condition and it evaluated to false, the only
5827 way we would stop tracing is if there was an error during
5828 condition expression evaluation. */
5829 if (expr_eval_result
!= expr_eval_no_error
)
5840 #ifndef IN_PROCESS_AGENT
5843 get_raw_reg_func_addr (void)
5845 return ipa_sym_addrs
.addr_get_raw_reg
;
5849 get_get_tsv_func_addr (void)
5851 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5855 get_set_tsv_func_addr (void)
5857 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5861 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5862 CORE_ADDR
*jump_entry
)
5864 CORE_ADDR entry_point
= *jump_entry
;
5865 enum eval_result_type err
;
5867 trace_debug ("Starting condition compilation for tracepoint %d\n",
5870 /* Initialize the global pointer to the code being built. */
5871 current_insn_ptr
= *jump_entry
;
5875 err
= compile_bytecodes (tpoint
->cond
);
5877 if (err
== expr_eval_no_error
)
5881 /* Record the beginning of the compiled code. */
5882 tpoint
->compiled_cond
= entry_point
;
5884 trace_debug ("Condition compilation for tracepoint %d complete\n",
5889 /* Leave the unfinished code in situ, but don't point to it. */
5891 tpoint
->compiled_cond
= 0;
5893 trace_debug ("Condition compilation for tracepoint %d failed, "
5895 tpoint
->number
, err
);
5898 /* Update the code pointer passed in. Note that we do this even if
5899 the compile fails, so that we can look at the partial results
5900 instead of letting them be overwritten. */
5901 *jump_entry
= current_insn_ptr
;
5903 /* Leave a gap, to aid dump decipherment. */
5907 /* We'll need to adjust these when we consider bi-arch setups, and big
5911 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5913 return write_inferior_memory (where
,
5914 (unsigned char *) &ptr
, sizeof (void *));
5917 /* The base pointer of the IPA's heap. This is the only memory the
5918 IPA is allowed to use. The IPA should _not_ call the inferior's
5919 `malloc' during operation. That'd be slow, and, most importantly,
5920 it may not be safe. We may be collecting a tracepoint in a signal
5921 handler, for example. */
5922 static CORE_ADDR target_tp_heap
;
5924 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5928 target_malloc (ULONGEST size
)
5932 if (target_tp_heap
== 0)
5934 /* We have the pointer *address*, need what it points to. */
5935 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5937 fatal ("could get target heap head pointer");
5940 ptr
= target_tp_heap
;
5941 target_tp_heap
+= size
;
5943 /* Pad to 8-byte alignment. */
5944 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5950 download_agent_expr (struct agent_expr
*expr
)
5952 CORE_ADDR expr_addr
;
5953 CORE_ADDR expr_bytes
;
5955 expr_addr
= target_malloc (sizeof (*expr
));
5956 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5958 expr_bytes
= target_malloc (expr
->length
);
5959 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5961 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5966 /* Align V up to N bits. */
5967 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5969 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5972 download_tracepoint_1 (struct tracepoint
*tpoint
)
5974 struct tracepoint target_tracepoint
;
5975 CORE_ADDR tpptr
= 0;
5977 gdb_assert (tpoint
->type
== fast_tracepoint
5978 || tpoint
->type
== static_tracepoint
);
5980 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5982 CORE_ADDR jentry
, jump_entry
;
5984 jentry
= jump_entry
= get_jump_space_head ();
5986 if (tpoint
->cond
!= NULL
)
5988 /* Pad to 8-byte alignment. (needed?) */
5989 /* Actually this should be left for the target to
5991 jentry
= UALIGN (jentry
, 8);
5993 compile_tracepoint_condition (tpoint
, &jentry
);
5996 /* Pad to 8-byte alignment. */
5997 jentry
= UALIGN (jentry
, 8);
5998 claim_jump_space (jentry
- jump_entry
);
6001 target_tracepoint
= *tpoint
;
6003 tpptr
= target_malloc (sizeof (*tpoint
));
6004 tpoint
->obj_addr_on_target
= tpptr
;
6006 /* Write the whole object. We'll fix up its pointers in a bit.
6007 Assume no next for now. This is fixed up above on the next
6008 iteration, if there's any. */
6009 target_tracepoint
.next
= NULL
;
6010 /* Need to clear this here too, since we're downloading the
6011 tracepoints before clearing our own copy. */
6012 target_tracepoint
.hit_count
= 0;
6014 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6015 sizeof (target_tracepoint
));
6018 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6020 download_agent_expr (tpoint
->cond
));
6022 if (tpoint
->numactions
)
6025 CORE_ADDR actions_array
;
6027 /* The pointers array. */
6029 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6030 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6034 /* Now for each pointer, download the action. */
6035 for (i
= 0; i
< tpoint
->numactions
; i
++)
6037 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6038 CORE_ADDR ipa_action
= action
->ops
->download (action
);
6040 if (ipa_action
!= 0)
6041 write_inferior_data_ptr
6042 (actions_array
+ i
* sizeof (*tpoint
->actions
),
6048 #define IPA_PROTO_FAST_TRACE_FLAG 0
6049 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6050 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6051 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6052 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6054 /* Send a command to agent to download and install tracepoint TPOINT. */
6057 tracepoint_send_agent (struct tracepoint
*tpoint
)
6059 char buf
[IPA_CMD_BUF_SIZE
];
6064 strcpy (p
, "FastTrace:");
6067 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6068 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6069 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6070 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6071 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6072 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6073 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6074 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6075 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6076 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6077 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6080 p
= agent_expr_send (p
, tpoint
->cond
);
6082 /* tracepoint_action */
6083 for (i
= 0; i
< tpoint
->numactions
; i
++)
6085 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6087 p
[0] = action
->type
;
6088 p
= action
->ops
->send (&p
[1], action
);
6091 get_jump_space_head ();
6092 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6093 agent can use jump pad from it. */
6094 if (tpoint
->type
== fast_tracepoint
)
6096 memcpy (p
, &gdb_jump_pad_head
, 8);
6100 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6104 if (strncmp (buf
, "OK", 2) != 0)
6107 /* The value of tracepoint's target address is stored in BUF. */
6108 memcpy (&tpoint
->obj_addr_on_target
,
6109 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6111 if (tpoint
->type
== fast_tracepoint
)
6114 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6117 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6118 (unsigned int) tpoint
->obj_addr_on_target
,
6119 (unsigned int) gdb_jump_pad_head
);
6121 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6123 /* This has been done in agent. We should also set up record for it. */
6124 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6127 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6134 download_tracepoint (struct tracepoint
*tpoint
)
6136 struct tracepoint
*tp
, *tp_prev
;
6138 if (tpoint
->type
!= fast_tracepoint
6139 && tpoint
->type
!= static_tracepoint
)
6142 download_tracepoint_1 (tpoint
);
6144 /* Find the previous entry of TPOINT, which is fast tracepoint or
6145 static tracepoint. */
6147 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6149 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6155 CORE_ADDR tp_prev_target_next_addr
;
6157 /* Insert TPOINT after TP_PREV in IPA. */
6158 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6159 + offsetof (struct tracepoint
, next
),
6160 &tp_prev_target_next_addr
))
6161 fatal ("error reading `tp_prev->next'");
6163 /* tpoint->next = tp_prev->next */
6164 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6165 + offsetof (struct tracepoint
, next
),
6166 tp_prev_target_next_addr
);
6167 /* tp_prev->next = tpoint */
6168 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6169 + offsetof (struct tracepoint
, next
),
6170 tpoint
->obj_addr_on_target
);
6173 /* First object in list, set the head pointer in the
6175 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6176 tpoint
->obj_addr_on_target
);
6181 download_trace_state_variables (void)
6183 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6184 struct trace_state_variable
*tsv
;
6186 /* Start out empty. */
6187 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6189 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6191 struct trace_state_variable target_tsv
;
6193 /* TSV's with a getter have been initialized equally in both the
6194 inferior and GDBserver. Skip them. */
6195 if (tsv
->getter
!= NULL
)
6201 ptr
= target_malloc (sizeof (*tsv
));
6203 if (tsv
== trace_state_variables
)
6205 /* First object in list, set the head pointer in the
6208 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6213 write_inferior_data_ptr (prev_ptr
6214 + offsetof (struct trace_state_variable
,
6219 /* Write the whole object. We'll fix up its pointers in a bit.
6220 Assume no next, fixup when needed. */
6221 target_tsv
.next
= NULL
;
6223 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6224 sizeof (target_tsv
));
6226 if (tsv
->name
!= NULL
)
6228 size_t size
= strlen (tsv
->name
) + 1;
6229 CORE_ADDR name_addr
= target_malloc (size
);
6230 write_inferior_memory (name_addr
,
6231 (unsigned char *) tsv
->name
, size
);
6232 write_inferior_data_ptr (ptr
6233 + offsetof (struct trace_state_variable
,
6238 if (tsv
->getter
!= NULL
)
6240 fatal ("what to do with these?");
6246 /* Fixup the next pointer in the last item in the list. */
6247 write_inferior_data_ptr (prev_ptr
6248 + offsetof (struct trace_state_variable
,
6253 /* Upload complete trace frames out of the IP Agent's trace buffer
6254 into GDBserver's trace buffer. This always uploads either all or
6255 no trace frames. This is the counter part of
6256 `trace_alloc_trace_buffer'. See its description of the atomic
6257 synching mechanism. */
6260 upload_fast_traceframes (void)
6262 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6263 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6265 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6266 unsigned int curr_tbctrl_idx
;
6267 unsigned int ipa_trace_buffer_ctrl_curr
;
6268 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6269 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6270 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6271 CORE_ADDR ipa_trace_buffer_lo
;
6272 CORE_ADDR ipa_trace_buffer_hi
;
6274 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6275 &ipa_traceframe_read_count_racy
))
6277 /* This will happen in most targets if the current thread is
6282 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6283 &ipa_traceframe_write_count_racy
))
6286 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6287 ipa_traceframe_write_count_racy
6288 - ipa_traceframe_read_count_racy
,
6289 ipa_traceframe_write_count_racy
,
6290 ipa_traceframe_read_count_racy
);
6292 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6295 about_to_request_buffer_space_bkpt
6296 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6299 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6300 &ipa_trace_buffer_ctrl_curr
))
6303 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6305 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6308 unsigned int prev
, counter
;
6310 /* Update the token, with new counters, and the GDBserver stamp
6311 bit. Alway reuse the current TBC index. */
6312 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6313 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6315 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6321 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6322 ipa_trace_buffer_ctrl_curr
))
6325 trace_debug ("Lib: Committed %08x -> %08x",
6326 ipa_trace_buffer_ctrl_curr_old
,
6327 ipa_trace_buffer_ctrl_curr
);
6329 /* Re-read these, now that we've installed the
6330 `about_to_request_buffer_space' breakpoint/lock. A thread could
6331 have finished a traceframe between the last read of these
6332 counters and setting the breakpoint above. If we start
6333 uploading, we never want to leave this function with
6334 traceframe_read_count != 0, otherwise, GDBserver could end up
6335 incrementing the counter tokens more than once (due to event loop
6336 nesting), which would break the IP agent's "effective" detection
6337 (see trace_alloc_trace_buffer). */
6338 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6339 &ipa_traceframe_read_count
))
6341 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6342 &ipa_traceframe_write_count
))
6347 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6348 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6349 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6351 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6352 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6353 trace_debug ("note that ipa_traceframe_count's parts changed");
6356 /* Get the address of the current TBC object (the IP agent has an
6357 array of 3 such objects). The index is stored in the TBC
6359 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6360 ipa_trace_buffer_ctrl_addr
6361 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6363 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6364 (unsigned char *) &ipa_trace_buffer_ctrl
,
6365 sizeof (struct ipa_trace_buffer_control
)))
6368 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6369 &ipa_trace_buffer_lo
))
6371 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6372 &ipa_trace_buffer_hi
))
6375 /* Offsets are easier to grok for debugging than raw addresses,
6376 especially for the small trace buffer sizes that are useful for
6378 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6379 "endfree=%d wrap=%d hi=%d",
6381 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6382 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6383 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6384 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6385 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6387 /* Note that the IPA's buffer is always circular. */
6389 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6391 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6392 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6394 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6395 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6396 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6397 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6400 tf
= IPA_FIRST_TRACEFRAME ();
6402 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6404 struct tracepoint
*tpoint
;
6405 struct traceframe
*tframe
;
6406 unsigned char *block
;
6407 struct traceframe ipa_tframe
;
6409 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6410 offsetof (struct traceframe
, data
)))
6411 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6413 if (ipa_tframe
.tpnum
== 0)
6414 fatal ("Uploading: No (more) fast traceframes, but "
6415 "ipa_traceframe_count == %u??\n",
6416 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6418 /* Note that this will be incorrect for multi-location
6420 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6422 tframe
= add_traceframe (tpoint
);
6425 trace_buffer_is_full
= 1;
6426 trace_debug ("Uploading: trace buffer is full");
6430 /* Copy the whole set of blocks in one go for now. FIXME:
6431 split this in smaller blocks. */
6432 block
= add_traceframe_block (tframe
, tpoint
,
6433 ipa_tframe
.data_size
);
6436 if (read_inferior_memory (tf
6437 + offsetof (struct traceframe
, data
),
6438 block
, ipa_tframe
.data_size
))
6439 error ("Uploading: Couldn't read traceframe data at %s\n",
6440 paddress (tf
+ offsetof (struct traceframe
, data
)));
6443 trace_debug ("Uploading: traceframe didn't fit");
6444 finish_traceframe (tframe
);
6447 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6449 /* If we freed the traceframe that wrapped around, go back
6450 to the non-wrap case. */
6451 if (tf
< ipa_trace_buffer_ctrl
.start
)
6453 trace_debug ("Lib: Discarding past the wraparound");
6454 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6456 ipa_trace_buffer_ctrl
.start
= tf
;
6457 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6458 ++ipa_traceframe_read_count
;
6460 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6461 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6463 trace_debug ("Lib: buffer is fully empty. "
6464 "Trace buffer [%d] start=%d free=%d endfree=%d",
6466 (int) (ipa_trace_buffer_ctrl
.start
6467 - ipa_trace_buffer_lo
),
6468 (int) (ipa_trace_buffer_ctrl
.free
6469 - ipa_trace_buffer_lo
),
6470 (int) (ipa_trace_buffer_ctrl
.end_free
6471 - ipa_trace_buffer_lo
));
6473 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6474 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6475 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6476 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6479 trace_debug ("Uploaded a traceframe\n"
6480 "Lib: Trace buffer [%d] start=%d free=%d "
6481 "endfree=%d wrap=%d hi=%d",
6483 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6484 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6485 (int) (ipa_trace_buffer_ctrl
.end_free
6486 - ipa_trace_buffer_lo
),
6487 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6488 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6491 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6492 (unsigned char *) &ipa_trace_buffer_ctrl
,
6493 sizeof (struct ipa_trace_buffer_control
)))
6496 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6497 ipa_traceframe_read_count
);
6499 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6502 cancel_breakpoints ();
6504 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6505 about_to_request_buffer_space_bkpt
= NULL
;
6509 if (trace_buffer_is_full
)
6514 #ifdef IN_PROCESS_AGENT
6516 IP_AGENT_EXPORT
int ust_loaded
;
6517 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6521 /* Static tracepoints. */
6523 /* UST puts a "struct tracepoint" in the global namespace, which
6524 conflicts with our tracepoint. Arguably, being a library, it
6525 shouldn't take ownership of such a generic name. We work around it
6527 #define tracepoint ust_tracepoint
6528 #include <ust/ust.h>
6531 extern int serialize_to_text (char *outbuf
, int bufsize
,
6532 const char *fmt
, va_list ap
);
6534 #define GDB_PROBE_NAME "gdb"
6536 /* We dynamically search for the UST symbols instead of linking them
6537 in. This lets the user decide if the application uses static
6538 tracepoints, instead of always pulling libust.so in. This vector
6539 holds pointers to all functions we care about. */
6543 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6544 const char *fmt
, va_list ap
);
6546 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6547 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6549 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6551 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6554 void (*marker_iter_start
) (struct marker_iter
*iter
);
6555 void (*marker_iter_next
) (struct marker_iter
*iter
);
6556 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6557 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6562 /* Cast through typeof to catch incompatible API changes. Since UST
6563 only builds with gcc, we can freely use gcc extensions here
6565 #define GET_UST_SYM(SYM) \
6568 if (ust_ops.SYM == NULL) \
6569 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6570 if (ust_ops.SYM == NULL) \
6574 #define USTF(SYM) ust_ops.SYM
6576 /* Get pointers to all libust.so functions we care about. */
6581 GET_UST_SYM (serialize_to_text
);
6583 GET_UST_SYM (ltt_probe_register
);
6584 GET_UST_SYM (ltt_probe_unregister
);
6585 GET_UST_SYM (ltt_marker_connect
);
6586 GET_UST_SYM (ltt_marker_disconnect
);
6588 GET_UST_SYM (marker_iter_start
);
6589 GET_UST_SYM (marker_iter_next
);
6590 GET_UST_SYM (marker_iter_stop
);
6591 GET_UST_SYM (marker_iter_reset
);
6597 /* Given an UST marker, return the matching gdb static tracepoint.
6598 The match is done by address. */
6600 static struct tracepoint
*
6601 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6603 struct tracepoint
*tpoint
;
6605 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6607 if (tpoint
->type
!= static_tracepoint
)
6610 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6617 /* The probe function we install on lttng/ust markers. Whenever a
6618 probed ust marker is hit, this function is called. This is similar
6619 to gdb_collect, only for static tracepoints, instead of fast
6623 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6624 struct registers
*regs
, void *call_private
,
6625 const char *fmt
, va_list *args
)
6627 struct tracepoint
*tpoint
;
6628 struct static_tracepoint_ctx ctx
;
6630 /* Don't do anything until the trace run is completely set up. */
6633 trace_debug ("gdb_probe: not tracing\n");
6637 ctx
.base
.type
= static_tracepoint
;
6638 ctx
.regcache_initted
= 0;
6643 /* Wrap the regblock in a register cache (in the stack, we don't
6644 want to malloc here). */
6645 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6646 if (ctx
.regspace
== NULL
)
6648 trace_debug ("Trace buffer block allocation failed, skipping");
6652 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6655 trace_debug ("gdb_probe: marker not known: "
6656 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6657 mdata
->location
, mdata
->channel
,
6658 mdata
->name
, mdata
->format
);
6662 if (!tpoint
->enabled
)
6664 trace_debug ("gdb_probe: tracepoint disabled");
6668 ctx
.tpoint
= tpoint
;
6670 trace_debug ("gdb_probe: collecting marker: "
6671 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6672 mdata
->location
, mdata
->channel
,
6673 mdata
->name
, mdata
->format
);
6675 /* Test the condition if present, and collect if true. */
6676 if (tpoint
->cond
== NULL
6677 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6680 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6681 tpoint
->address
, tpoint
);
6683 if (stopping_tracepoint
6684 || trace_buffer_is_full
6685 || expr_eval_result
!= expr_eval_no_error
)
6690 /* If there was a condition and it evaluated to false, the only
6691 way we would stop tracing is if there was an error during
6692 condition expression evaluation. */
6693 if (expr_eval_result
!= expr_eval_no_error
)
6698 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6699 static tracepoint string data. This is a string passed to the
6700 tracing library by the user, at the time of the tracepoint marker
6701 call. E.g., in the UST marker call:
6703 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6705 the collected data is "str FOOBAZ".
6709 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6710 struct traceframe
*tframe
)
6712 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6713 unsigned char *bufspace
;
6716 unsigned short blocklen
;
6720 trace_debug ("Wanted to collect static trace data, "
6721 "but there's no static trace data");
6725 va_copy (copy
, *umd
->args
);
6726 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6729 trace_debug ("Want to collect ust data");
6731 /* 'S' + size + string */
6732 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6733 1 + sizeof (blocklen
) + size
+ 1);
6734 if (bufspace
== NULL
)
6736 trace_debug ("Trace buffer block allocation failed, skipping");
6740 /* Identify a static trace data block. */
6743 blocklen
= size
+ 1;
6744 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6746 va_copy (copy
, *umd
->args
);
6747 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6748 size
+ 1, umd
->fmt
, copy
);
6751 trace_debug ("Storing static tracepoint data in regblock: %s",
6752 bufspace
+ 1 + sizeof (blocklen
));
6755 /* The probe to register with lttng/ust. */
6756 static struct ltt_available_probe gdb_ust_probe
=
6763 #endif /* HAVE_UST */
6764 #endif /* IN_PROCESS_AGENT */
6766 #ifndef IN_PROCESS_AGENT
6768 /* Ask the in-process agent to run a command. Since we don't want to
6769 have to handle the IPA hitting breakpoints while running the
6770 command, we pause all threads, remove all breakpoints, and then set
6771 the helper thread re-running. We communicate with the helper
6772 thread by means of direct memory xfering, and a socket for
6776 run_inferior_command (char *cmd
, int len
)
6779 int pid
= ptid_get_pid (current_ptid
);
6781 trace_debug ("run_inferior_command: running: %s", cmd
);
6784 uninsert_all_breakpoints ();
6786 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6788 reinsert_all_breakpoints ();
6794 #else /* !IN_PROCESS_AGENT */
6796 #include <sys/socket.h>
6799 #ifndef UNIX_PATH_MAX
6800 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6803 /* Where we put the socked used for synchronization. */
6804 #define SOCK_DIR P_tmpdir
6806 /* Thread ID of the helper thread. GDBserver reads this to know which
6807 is the help thread. This is an LWP id on Linux. */
6808 int helper_thread_id
;
6811 init_named_socket (const char *name
)
6814 struct sockaddr_un addr
;
6816 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6819 warning ("socket creation failed: %s", strerror (errno
));
6823 addr
.sun_family
= AF_UNIX
;
6825 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6826 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6828 result
= access (name
, F_OK
);
6832 result
= unlink (name
);
6835 warning ("unlink failed: %s", strerror (errno
));
6839 warning ("socket %s already exists; overwriting", name
);
6842 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6845 warning ("bind failed: %s", strerror (errno
));
6850 result
= listen (fd
, 1);
6853 warning ("listen: %s", strerror (errno
));
6861 static char agent_socket_name
[UNIX_PATH_MAX
];
6864 gdb_agent_socket_init (void)
6868 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6869 SOCK_DIR
, getpid ());
6870 if (result
>= UNIX_PATH_MAX
)
6872 trace_debug ("string overflow allocating socket name");
6876 fd
= init_named_socket (agent_socket_name
);
6878 warning ("Error initializing named socket (%s) for communication with the "
6879 "ust helper thread. Check that directory exists and that it "
6880 "is writable.", agent_socket_name
);
6887 /* The next marker to be returned on a qTsSTM command. */
6888 static const struct marker
*next_st
;
6890 /* Returns the first known marker. */
6895 struct marker_iter iter
;
6897 USTF(marker_iter_reset
) (&iter
);
6898 USTF(marker_iter_start
) (&iter
);
6903 /* Returns the marker following M. */
6905 const struct marker
*
6906 next_marker (const struct marker
*m
)
6908 struct marker_iter iter
;
6910 USTF(marker_iter_reset
) (&iter
);
6911 USTF(marker_iter_start
) (&iter
);
6913 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6915 if (iter
.marker
== m
)
6917 USTF(marker_iter_next
) (&iter
);
6925 /* Return an hexstr version of the STR C string, fit for sending to
6929 cstr_to_hexstr (const char *str
)
6931 int len
= strlen (str
);
6932 char *hexstr
= xmalloc (len
* 2 + 1);
6933 bin2hex ((gdb_byte
*) str
, hexstr
, len
);
6937 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6941 response_ust_marker (char *packet
, const struct marker
*st
)
6943 char *strid
, *format
, *tmp
;
6945 next_st
= next_marker (st
);
6947 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6948 strlen (st
->name
) + 1);
6949 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6951 strid
= cstr_to_hexstr (tmp
);
6954 format
= cstr_to_hexstr (st
->format
);
6956 sprintf (packet
, "m%s:%s:%s",
6957 paddress ((uintptr_t) st
->location
),
6965 /* Return the first static tracepoint, and initialize the state
6966 machine that will iterate through all the static tracepoints. */
6969 cmd_qtfstm (char *packet
)
6971 trace_debug ("Returning first trace state variable definition");
6973 if (first_marker ())
6974 response_ust_marker (packet
, first_marker ());
6976 strcpy (packet
, "l");
6979 /* Return additional trace state variable definitions. */
6982 cmd_qtsstm (char *packet
)
6984 trace_debug ("Returning static tracepoint");
6987 response_ust_marker (packet
, next_st
);
6989 strcpy (packet
, "l");
6992 /* Disconnect the GDB probe from a marker at a given address. */
6995 unprobe_marker_at (char *packet
)
6999 struct marker_iter iter
;
7001 p
+= sizeof ("unprobe_marker_at:") - 1;
7003 p
= unpack_varlen_hex (p
, &address
);
7005 USTF(marker_iter_reset
) (&iter
);
7006 USTF(marker_iter_start
) (&iter
);
7007 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7008 if ((uintptr_t ) iter
.marker
->location
== address
)
7012 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7016 warning ("could not disable marker %s/%s",
7017 iter
.marker
->channel
, iter
.marker
->name
);
7022 /* Connect the GDB probe to a marker at a given address. */
7025 probe_marker_at (char *packet
)
7029 struct marker_iter iter
;
7032 p
+= sizeof ("probe_marker_at:") - 1;
7034 p
= unpack_varlen_hex (p
, &address
);
7036 USTF(marker_iter_reset
) (&iter
);
7038 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7040 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7041 if ((uintptr_t ) m
->location
== address
)
7045 trace_debug ("found marker for address. "
7046 "ltt_marker_connect (marker = %s/%s)",
7047 m
->channel
, m
->name
);
7049 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7051 if (result
&& result
!= -EEXIST
)
7052 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7053 m
->channel
, m
->name
, -result
);
7057 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7058 m
->channel
, m
->name
);
7062 strcpy (packet
, "OK");
7066 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7071 cmd_qtstmat (char *packet
)
7075 struct marker_iter iter
;
7078 p
+= sizeof ("qTSTMat:") - 1;
7080 p
= unpack_varlen_hex (p
, &address
);
7082 USTF(marker_iter_reset
) (&iter
);
7084 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7086 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7087 if ((uintptr_t ) m
->location
== address
)
7089 response_ust_marker (packet
, m
);
7093 strcpy (packet
, "l");
7103 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7106 #endif /* HAVE_UST */
7108 #include <sys/syscall.h>
7112 gdb_agent_remove_socket (void)
7114 unlink (agent_socket_name
);
7117 /* Helper thread of agent. */
7120 gdb_agent_helper_thread (void *arg
)
7124 atexit (gdb_agent_remove_socket
);
7128 listen_fd
= gdb_agent_socket_init ();
7130 if (helper_thread_id
== 0)
7131 helper_thread_id
= syscall (SYS_gettid
);
7133 if (listen_fd
== -1)
7135 warning ("could not create sync socket\n");
7142 struct sockaddr_un sockaddr
;
7148 tmp
= sizeof (sockaddr
);
7152 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7154 /* It seems an ERESTARTSYS can escape out of accept. */
7155 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7159 warning ("Accept returned %d, error: %s\n",
7160 fd
, strerror (errno
));
7166 ret
= read (fd
, buf
, 1);
7167 } while (ret
== -1 && errno
== EINTR
);
7171 warning ("reading socket (fd=%d) failed with %s",
7172 fd
, strerror (errno
));
7179 if (strncmp ("close", cmd_buf
, 5) == 0)
7184 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7186 cmd_qtfstm (cmd_buf
);
7188 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7190 cmd_qtsstm (cmd_buf
);
7192 else if (strncmp ("unprobe_marker_at:",
7194 sizeof ("unprobe_marker_at:") - 1) == 0)
7196 unprobe_marker_at (cmd_buf
);
7198 else if (strncmp ("probe_marker_at:",
7200 sizeof ("probe_marker_at:") - 1) == 0)
7202 probe_marker_at (cmd_buf
);
7204 else if (strncmp ("qTSTMat:",
7206 sizeof ("qTSTMat:") - 1) == 0)
7208 cmd_qtstmat (cmd_buf
);
7210 #endif /* HAVE_UST */
7213 /* Fix compiler's warning: ignoring return value of 'write'. */
7214 ret
= write (fd
, buf
, 1);
7220 unlink (agent_socket_name
);
7222 /* Sleep endlessly to wait the whole inferior stops. This
7223 thread can not exit because GDB or GDBserver may still need
7224 'current_inferior' (representing this thread) to access
7225 inferior memory. Otherwise, this thread exits earlier than
7226 other threads, and 'current_inferior' is set to NULL. */
7237 #include <pthread.h>
7239 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7242 gdb_agent_init (void)
7249 /* We want the helper thread to be as transparent as possible, so
7250 have it inherit an all-signals-blocked mask. */
7252 sigfillset (&new_mask
);
7253 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7255 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7257 res
= pthread_create (&thread
,
7259 gdb_agent_helper_thread
,
7262 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7264 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7266 while (helper_thread_id
== 0)
7274 #include <sys/mman.h>
7277 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7278 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7279 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7280 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7281 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7282 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7284 /* Record the result of getting buffer space for fast tracepoint
7285 trampolines. Any error message is copied, since caller may not be
7286 using persistent storage. */
7289 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7291 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7292 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7294 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7296 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7299 static void __attribute__ ((constructor
))
7300 initialize_tracepoint_ftlib (void)
7302 initialize_tracepoint ();
7307 #endif /* IN_PROCESS_AGENT */
7309 /* Return a timestamp, expressed as microseconds of the usual Unix
7310 time. (As the result is a 64-bit number, it will not overflow any
7314 get_timestamp (void)
7318 if (gettimeofday (&tv
, 0) != 0)
7321 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7325 initialize_tracepoint (void)
7327 /* Start with the default size. */
7328 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7330 /* Wire trace state variable 1 to be the timestamp. This will be
7331 uploaded to GDB upon connection and become one of its trace state
7332 variables. (In case you're wondering, if GDB already has a trace
7333 variable numbered 1, it will be renumbered.) */
7334 create_trace_state_variable (1, 0);
7335 set_trace_state_variable_name (1, "trace_timestamp");
7336 set_trace_state_variable_getter (1, get_timestamp
);
7338 #ifdef IN_PROCESS_AGENT
7343 pagesize
= sysconf (_SC_PAGE_SIZE
);
7347 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7349 #define SCRATCH_BUFFER_NPAGES 20
7351 /* Allocate scratch buffer aligned on a page boundary, at a low
7352 address (close to the main executable's code). */
7353 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7355 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7356 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7357 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7359 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7365 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7368 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7371 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7373 /* It's not a fatal error for something to go wrong with trampoline
7374 buffer setup, but it can be mysterious, so create a channel to
7375 report back on what went wrong, using a fixed size since we may
7376 not be able to allocate space later when the problem occurs. */
7377 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7379 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7381 initialize_low_tracepoint ();