1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2016 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"
28 #include "gdb_sys_time.h"
33 #define DEFAULT_TRACE_BUFFER_SIZE 5242880 /* 5*1024*1024 */
35 /* This file is built for both GDBserver, and the in-process
36 agent (IPA), a shared library that includes a tracing agent that is
37 loaded by the inferior to support fast tracepoints. Fast
38 tracepoints (or more accurately, jump based tracepoints) are
39 implemented by patching the tracepoint location with a jump into a
40 small trampoline function whose job is to save the register state,
41 call the in-process tracing agent, and then execute the original
42 instruction that was under the tracepoint jump (possibly adjusted,
43 if PC-relative, or some such).
45 The current synchronization design is pull based. That means,
46 GDBserver does most of the work, by peeking/poking at the inferior
47 agent's memory directly for downloading tracepoint and associated
48 objects, and for uploading trace frames. Whenever the IPA needs
49 something from GDBserver (trace buffer is full, tracing stopped for
50 some reason, etc.) the IPA calls a corresponding hook function
51 where GDBserver has placed a breakpoint.
53 Each of the agents has its own trace buffer. When browsing the
54 trace frames built from slow and fast tracepoints from GDB (tfind
55 mode), there's no guarantee the user is seeing the trace frames in
56 strict chronological creation order, although, GDBserver tries to
57 keep the order relatively reasonable, by syncing the trace buffers
62 #ifdef IN_PROCESS_AGENT
64 static void trace_vdebug (const char *, ...) ATTRIBUTE_PRINTF (1, 2);
67 trace_vdebug (const char *fmt
, ...)
73 vsprintf (buf
, fmt
, ap
);
74 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
78 #define trace_debug_1(level, fmt, args...) \
80 if (level <= debug_threads) \
81 trace_vdebug ((fmt), ##args); \
86 #define trace_debug_1(level, fmt, args...) \
88 if (level <= debug_threads) \
90 debug_printf ((fmt), ##args); \
91 debug_printf ("\n"); \
97 #define trace_debug(FMT, args...) \
98 trace_debug_1 (1, FMT, ##args)
100 /* Prefix exported symbols, for good citizenship. All the symbols
101 that need exporting are defined in this module. Note that all
102 these symbols must be tagged with IP_AGENT_EXPORT_*. */
103 #ifdef IN_PROCESS_AGENT
104 # define gdb_tp_heap_buffer IPA_SYM_EXPORTED_NAME (gdb_tp_heap_buffer)
105 # define gdb_jump_pad_buffer IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer)
106 # define gdb_jump_pad_buffer_end IPA_SYM_EXPORTED_NAME (gdb_jump_pad_buffer_end)
107 # define gdb_trampoline_buffer IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer)
108 # define gdb_trampoline_buffer_end IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_end)
109 # define gdb_trampoline_buffer_error IPA_SYM_EXPORTED_NAME (gdb_trampoline_buffer_error)
110 # define collecting IPA_SYM_EXPORTED_NAME (collecting)
111 # define gdb_collect_ptr IPA_SYM_EXPORTED_NAME (gdb_collect_ptr)
112 # define stop_tracing IPA_SYM_EXPORTED_NAME (stop_tracing)
113 # define flush_trace_buffer IPA_SYM_EXPORTED_NAME (flush_trace_buffer)
114 # define about_to_request_buffer_space IPA_SYM_EXPORTED_NAME (about_to_request_buffer_space)
115 # define trace_buffer_is_full IPA_SYM_EXPORTED_NAME (trace_buffer_is_full)
116 # define stopping_tracepoint IPA_SYM_EXPORTED_NAME (stopping_tracepoint)
117 # define expr_eval_result IPA_SYM_EXPORTED_NAME (expr_eval_result)
118 # define error_tracepoint IPA_SYM_EXPORTED_NAME (error_tracepoint)
119 # define tracepoints IPA_SYM_EXPORTED_NAME (tracepoints)
120 # define tracing IPA_SYM_EXPORTED_NAME (tracing)
121 # define trace_buffer_ctrl IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl)
122 # define trace_buffer_ctrl_curr IPA_SYM_EXPORTED_NAME (trace_buffer_ctrl_curr)
123 # define trace_buffer_lo IPA_SYM_EXPORTED_NAME (trace_buffer_lo)
124 # define trace_buffer_hi IPA_SYM_EXPORTED_NAME (trace_buffer_hi)
125 # define traceframe_read_count IPA_SYM_EXPORTED_NAME (traceframe_read_count)
126 # define traceframe_write_count IPA_SYM_EXPORTED_NAME (traceframe_write_count)
127 # define traceframes_created IPA_SYM_EXPORTED_NAME (traceframes_created)
128 # define trace_state_variables IPA_SYM_EXPORTED_NAME (trace_state_variables)
129 # define get_raw_reg_ptr IPA_SYM_EXPORTED_NAME (get_raw_reg_ptr)
130 # define get_trace_state_variable_value_ptr \
131 IPA_SYM_EXPORTED_NAME (get_trace_state_variable_value_ptr)
132 # define set_trace_state_variable_value_ptr \
133 IPA_SYM_EXPORTED_NAME (set_trace_state_variable_value_ptr)
134 # define ust_loaded IPA_SYM_EXPORTED_NAME (ust_loaded)
135 # define helper_thread_id IPA_SYM_EXPORTED_NAME (helper_thread_id)
136 # define cmd_buf IPA_SYM_EXPORTED_NAME (cmd_buf)
137 # define ipa_tdesc_idx IPA_SYM_EXPORTED_NAME (ipa_tdesc_idx)
140 #ifndef IN_PROCESS_AGENT
142 /* Addresses of in-process agent's symbols GDBserver cares about. */
144 struct ipa_sym_addresses
146 CORE_ADDR addr_gdb_tp_heap_buffer
;
147 CORE_ADDR addr_gdb_jump_pad_buffer
;
148 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
149 CORE_ADDR addr_gdb_trampoline_buffer
;
150 CORE_ADDR addr_gdb_trampoline_buffer_end
;
151 CORE_ADDR addr_gdb_trampoline_buffer_error
;
152 CORE_ADDR addr_collecting
;
153 CORE_ADDR addr_gdb_collect_ptr
;
154 CORE_ADDR addr_stop_tracing
;
155 CORE_ADDR addr_flush_trace_buffer
;
156 CORE_ADDR addr_about_to_request_buffer_space
;
157 CORE_ADDR addr_trace_buffer_is_full
;
158 CORE_ADDR addr_stopping_tracepoint
;
159 CORE_ADDR addr_expr_eval_result
;
160 CORE_ADDR addr_error_tracepoint
;
161 CORE_ADDR addr_tracepoints
;
162 CORE_ADDR addr_tracing
;
163 CORE_ADDR addr_trace_buffer_ctrl
;
164 CORE_ADDR addr_trace_buffer_ctrl_curr
;
165 CORE_ADDR addr_trace_buffer_lo
;
166 CORE_ADDR addr_trace_buffer_hi
;
167 CORE_ADDR addr_traceframe_read_count
;
168 CORE_ADDR addr_traceframe_write_count
;
169 CORE_ADDR addr_traceframes_created
;
170 CORE_ADDR addr_trace_state_variables
;
171 CORE_ADDR addr_get_raw_reg_ptr
;
172 CORE_ADDR addr_get_trace_state_variable_value_ptr
;
173 CORE_ADDR addr_set_trace_state_variable_value_ptr
;
174 CORE_ADDR addr_ust_loaded
;
175 CORE_ADDR addr_ipa_tdesc_idx
;
183 IPA_SYM(gdb_tp_heap_buffer
),
184 IPA_SYM(gdb_jump_pad_buffer
),
185 IPA_SYM(gdb_jump_pad_buffer_end
),
186 IPA_SYM(gdb_trampoline_buffer
),
187 IPA_SYM(gdb_trampoline_buffer_end
),
188 IPA_SYM(gdb_trampoline_buffer_error
),
190 IPA_SYM(gdb_collect_ptr
),
191 IPA_SYM(stop_tracing
),
192 IPA_SYM(flush_trace_buffer
),
193 IPA_SYM(about_to_request_buffer_space
),
194 IPA_SYM(trace_buffer_is_full
),
195 IPA_SYM(stopping_tracepoint
),
196 IPA_SYM(expr_eval_result
),
197 IPA_SYM(error_tracepoint
),
198 IPA_SYM(tracepoints
),
200 IPA_SYM(trace_buffer_ctrl
),
201 IPA_SYM(trace_buffer_ctrl_curr
),
202 IPA_SYM(trace_buffer_lo
),
203 IPA_SYM(trace_buffer_hi
),
204 IPA_SYM(traceframe_read_count
),
205 IPA_SYM(traceframe_write_count
),
206 IPA_SYM(traceframes_created
),
207 IPA_SYM(trace_state_variables
),
208 IPA_SYM(get_raw_reg_ptr
),
209 IPA_SYM(get_trace_state_variable_value_ptr
),
210 IPA_SYM(set_trace_state_variable_value_ptr
),
212 IPA_SYM(ipa_tdesc_idx
),
215 static struct ipa_sym_addresses ipa_sym_addrs
;
217 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
219 #if !defined HAVE_GETAUXVAL && defined IN_PROCESS_AGENT
220 /* Retrieve the value of TYPE from the auxiliary vector. If TYPE is not
221 found, 0 is returned. This function is provided if glibc is too old. */
224 getauxval (unsigned long type
)
226 unsigned long data
[2];
227 FILE *f
= fopen ("/proc/self/auxv", "r");
228 unsigned long value
= 0;
233 while (fread (data
, sizeof (data
), 1, f
) > 0)
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_FUNC
void
402 /* GDBserver places breakpoint here. */
403 UNKNOWN_SIDE_EFFECTS();
406 IP_AGENT_EXPORT_FUNC
void
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
);
487 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
489 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
490 BUF += sizeof ((OBJ)->FIELD); \
495 /* Base action. Concrete actions inherit this. */
497 struct tracepoint_action
502 /* An 'M' (collect memory) action. */
503 struct collect_memory_action
505 struct tracepoint_action base
;
512 /* An 'R' (collect registers) action. */
514 struct collect_registers_action
516 struct tracepoint_action base
;
519 /* An 'X' (evaluate expression) action. */
521 struct eval_expr_action
523 struct tracepoint_action base
;
525 struct agent_expr
*expr
;
528 /* An 'L' (collect static trace data) action. */
529 struct collect_static_trace_data_action
531 struct tracepoint_action base
;
534 #ifndef IN_PROCESS_AGENT
536 m_tracepoint_action_download (const struct tracepoint_action
*action
)
538 CORE_ADDR ipa_action
= target_malloc (sizeof (struct collect_memory_action
));
540 write_inferior_memory (ipa_action
, (unsigned char *) action
,
541 sizeof (struct collect_memory_action
));
546 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
548 struct collect_memory_action
*maction
549 = (struct collect_memory_action
*) action
;
551 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
552 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
553 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
559 r_tracepoint_action_download (const struct tracepoint_action
*action
)
561 CORE_ADDR ipa_action
= target_malloc (sizeof (struct collect_registers_action
));
563 write_inferior_memory (ipa_action
, (unsigned char *) action
,
564 sizeof (struct collect_registers_action
));
570 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
575 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
578 x_tracepoint_action_download (const struct tracepoint_action
*action
)
580 CORE_ADDR ipa_action
= target_malloc (sizeof (struct eval_expr_action
));
583 write_inferior_memory (ipa_action
, (unsigned char *) action
,
584 sizeof (struct eval_expr_action
));
585 expr
= download_agent_expr (((struct eval_expr_action
*) action
)->expr
);
586 write_inferior_data_pointer (ipa_action
587 + offsetof (struct eval_expr_action
, expr
),
593 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
594 copy 0 to P. Return updated header of buffer. */
597 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
599 /* Copy the length of condition first, and then copy its
608 memcpy (p
, &aexpr
->length
, 4);
611 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
618 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
620 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
622 return agent_expr_send (buffer
, eaction
->expr
);
626 l_tracepoint_action_download (const struct tracepoint_action
*action
)
629 = target_malloc (sizeof (struct collect_static_trace_data_action
));
631 write_inferior_memory (ipa_action
, (unsigned char *) action
,
632 sizeof (struct collect_static_trace_data_action
));
638 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
644 tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
646 switch (action
->type
)
649 return m_tracepoint_action_send (buffer
, action
);
651 return r_tracepoint_action_send (buffer
, action
);
653 return x_tracepoint_action_send (buffer
, action
);
655 return l_tracepoint_action_send (buffer
, action
);
657 error ("Unknown trace action '%c'.", action
->type
);
661 tracepoint_action_download (const struct tracepoint_action
*action
)
663 switch (action
->type
)
666 return m_tracepoint_action_download (action
);
668 return r_tracepoint_action_download (action
);
670 return x_tracepoint_action_download (action
);
672 return l_tracepoint_action_download (action
);
674 error ("Unknown trace action '%c'.", action
->type
);
678 /* This structure describes a piece of the source-level definition of
679 the tracepoint. The contents are not interpreted by the target,
680 but preserved verbatim for uploading upon reconnection. */
684 /* The type of string, such as "cond" for a conditional. */
687 /* The source-level string itself. For the sake of target
688 debugging, we store it in plaintext, even though it is always
689 transmitted in hex. */
692 /* Link to the next one in the list. We link them in the order
693 received, in case some make up an ordered list of commands or
695 struct source_string
*next
;
700 /* Trap based tracepoint. */
703 /* A fast tracepoint implemented with a jump instead of a trap. */
706 /* A static tracepoint, implemented by a program call into a tracing
711 struct tracepoint_hit_ctx
;
713 typedef enum eval_result_type (*condfn
) (unsigned char *,
716 /* The definition of a tracepoint. */
718 /* Tracepoints may have multiple locations, each at a different
719 address. This can occur with optimizations, template
720 instantiation, etc. Since the locations may be in different
721 scopes, the conditions and actions may be different for each
722 location. Our target version of tracepoints is more like GDB's
723 notion of "breakpoint locations", but we have almost nothing that
724 is not per-location, so we bother having two kinds of objects. The
725 key consequence is that numbers are not unique, and that it takes
726 both number and address to identify a tracepoint uniquely. */
730 /* The number of the tracepoint, as specified by GDB. Several
731 tracepoint objects here may share a number. */
734 /* Address at which the tracepoint is supposed to trigger. Several
735 tracepoints may share an address. */
738 /* Tracepoint type. */
739 enum tracepoint_type type
;
741 /* True if the tracepoint is currently enabled. */
744 /* The number of single steps that will be performed after each
748 /* The number of times the tracepoint may be hit before it will
749 terminate the entire tracing run. */
752 /* Pointer to the agent expression that is the tracepoint's
753 conditional, or NULL if the tracepoint is unconditional. */
754 struct agent_expr
*cond
;
756 /* The list of actions to take when the tracepoint triggers. */
758 struct tracepoint_action
**actions
;
760 /* Count of the times we've hit this tracepoint during the run.
761 Note that while-stepping steps are not counted as "hits". */
764 /* Cached sum of the sizes of traceframes created by this point. */
765 uint64_t traceframe_usage
;
767 CORE_ADDR compiled_cond
;
769 /* Link to the next tracepoint in the list. */
770 struct tracepoint
*next
;
772 #ifndef IN_PROCESS_AGENT
773 /* The list of actions to take when the tracepoint triggers, in
774 string/packet form. */
777 /* The collection of strings that describe the tracepoint as it was
778 entered into GDB. These are not used by the target, but are
779 reported back to GDB upon reconnection. */
780 struct source_string
*source_strings
;
782 /* The number of bytes displaced by fast tracepoints. It may subsume
783 multiple instructions, for multi-byte fast tracepoints. This
784 field is only valid for fast tracepoints. */
787 /* Only for fast tracepoints. */
788 CORE_ADDR obj_addr_on_target
;
790 /* Address range where the original instruction under a fast
791 tracepoint was relocated to. (_end is actually one byte past
793 CORE_ADDR adjusted_insn_addr
;
794 CORE_ADDR adjusted_insn_addr_end
;
796 /* The address range of the piece of the jump pad buffer that was
797 assigned to this fast tracepoint. (_end is actually one byte
800 CORE_ADDR jump_pad_end
;
802 /* The address range of the piece of the trampoline buffer that was
803 assigned to this fast tracepoint. (_end is actually one byte
805 CORE_ADDR trampoline
;
806 CORE_ADDR trampoline_end
;
808 /* The list of actions to take while in a stepping loop. These
809 fields are only valid for patch-based tracepoints. */
810 int num_step_actions
;
811 struct tracepoint_action
**step_actions
;
812 /* Same, but in string/packet form. */
813 char **step_actions_str
;
815 /* Handle returned by the breakpoint or tracepoint module when we
816 inserted the trap or jump, or hooked into a static tracepoint.
817 NULL if we haven't inserted it yet. */
823 #ifndef IN_PROCESS_AGENT
825 /* Given `while-stepping', a thread may be collecting data for more
826 than one tracepoint simultaneously. On the other hand, the same
827 tracepoint with a while-stepping action may be hit by more than one
828 thread simultaneously (but not quite, each thread could be handling
829 a different step). Each thread holds a list of these objects,
830 representing the current step of each while-stepping action being
835 struct wstep_state
*next
;
837 /* The tracepoint number. */
839 /* The tracepoint's address. */
840 CORE_ADDR tp_address
;
842 /* The number of the current step in this 'while-stepping'
851 /* The linked list of all tracepoints. Marked explicitly as used as
852 the in-process library doesn't use it for the fast tracepoints
854 IP_AGENT_EXPORT_VAR
struct tracepoint
*tracepoints
;
856 /* The first tracepoint to exceed its pass count. */
858 IP_AGENT_EXPORT_VAR
struct tracepoint
*stopping_tracepoint
;
860 /* True if the trace buffer is full or otherwise no longer usable. */
862 IP_AGENT_EXPORT_VAR
int trace_buffer_is_full
;
864 /* The first error that occurred during expression evaluation. */
866 /* Stored as an int to avoid the IPA ABI being dependent on whatever
867 the compiler decides to use for the enum's underlying type. Holds
868 enum eval_result_type values. */
869 IP_AGENT_EXPORT_VAR
int expr_eval_result
= expr_eval_no_error
;
873 #ifndef IN_PROCESS_AGENT
875 /* Pointer to the last tracepoint in the list, new tracepoints are
876 linked in at the end. */
878 static struct tracepoint
*last_tracepoint
;
880 static const char *eval_result_names
[] =
882 "terror:in the attic", /* this should never be reported */
883 "terror:empty expression",
884 "terror:empty stack",
885 "terror:stack overflow",
886 "terror:stack underflow",
887 "terror:unhandled opcode",
888 "terror:unrecognized opcode",
889 "terror:divide by zero"
894 /* The tracepoint in which the error occurred. */
897 IP_AGENT_EXPORT_VAR
struct tracepoint
*error_tracepoint
;
900 struct trace_state_variable
902 /* This is the name of the variable as used in GDB. The target
903 doesn't use the name, but needs to have it for saving and
904 reconnection purposes. */
907 /* This number identifies the variable uniquely. Numbers may be
908 assigned either by the target (in the case of builtin variables),
909 or by GDB, and are presumed unique during the course of a trace
913 /* The variable's initial value, a 64-bit signed integer always. */
914 LONGEST initial_value
;
916 /* The variable's value, a 64-bit signed integer always. */
919 /* Pointer to a getter function, used to supply computed values. */
920 LONGEST (*getter
) (void);
922 /* Link to the next variable. */
923 struct trace_state_variable
*next
;
926 /* Linked list of all trace state variables. */
928 #ifdef IN_PROCESS_AGENT
929 struct trace_state_variable
*alloced_trace_state_variables
;
932 IP_AGENT_EXPORT_VAR
struct trace_state_variable
*trace_state_variables
;
934 /* The results of tracing go into a fixed-size space known as the
935 "trace buffer". Because usage follows a limited number of
936 patterns, we manage it ourselves rather than with malloc. Basic
937 rules are that we create only one trace frame at a time, each is
938 variable in size, they are never moved once created, and we only
939 discard if we are doing a circular buffer, and then only the oldest
940 ones. Each trace frame includes its own size, so we don't need to
941 link them together, and the trace frame number is relative to the
942 first one, so we don't need to record numbers. A trace frame also
943 records the number of the tracepoint that created it. The data
944 itself is a series of blocks, each introduced by a single character
945 and with a defined format. Each type of block has enough
946 type/length info to allow scanners to jump quickly from one block
947 to the next without reading each byte in the block. */
949 /* Trace buffer management would be simple - advance a free pointer
950 from beginning to end, then stop - were it not for the circular
951 buffer option, which is a useful way to prevent a trace run from
952 stopping prematurely because the buffer filled up. In the circular
953 case, the location of the first trace frame (trace_buffer_start)
954 moves as old trace frames are discarded. Also, since we grow trace
955 frames incrementally as actions are performed, we wrap around to
956 the beginning of the trace buffer. This is per-block, so each
957 block within a trace frame remains contiguous. Things get messy
958 when the wrapped-around trace frame is the one being discarded; the
959 free space ends up in two parts at opposite ends of the buffer. */
962 # if defined(__GNUC__)
963 # define ATTR_PACKED __attribute__ ((packed))
965 # define ATTR_PACKED /* nothing */
969 /* The data collected at a tracepoint hit. This object should be as
970 small as possible, since there may be a great many of them. We do
971 not need to keep a frame number, because they are all sequential
972 and there are no deletions; so the Nth frame in the buffer is
973 always frame number N. */
977 /* Number of the tracepoint that collected this traceframe. A value
978 of 0 indicates the current end of the trace buffer. We make this
979 a 16-bit field because it's never going to happen that GDB's
980 numbering of tracepoints reaches 32,000. */
983 /* The size of the data in this trace frame. We limit this to 32
984 bits, even on a 64-bit target, because it's just implausible that
985 one is validly going to collect 4 gigabytes of data at a single
987 unsigned int data_size
: 32;
989 /* The base of the trace data, which is contiguous from this point. */
990 unsigned char data
[0];
994 /* The size of the EOB marker, in bytes. A traceframe with zeroed
995 fields (and no data) marks the end of trace data. */
996 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
998 /* The traceframe to be used as the source of data to send back to
999 GDB. A value of -1 means to get data from the live program. */
1001 int current_traceframe
= -1;
1003 /* This flag is true if the trace buffer is circular, meaning that
1004 when it fills, the oldest trace frames are discarded in order to
1007 #ifndef IN_PROCESS_AGENT
1008 static int circular_trace_buffer
;
1011 /* Size of the trace buffer. */
1013 static LONGEST trace_buffer_size
;
1017 /* Pointer to the block of memory that traceframes all go into. */
1019 IP_AGENT_EXPORT_VAR
unsigned char *trace_buffer_lo
;
1021 /* Pointer to the end of the trace buffer, more precisely to the byte
1022 after the end of the buffer. */
1024 IP_AGENT_EXPORT_VAR
unsigned char *trace_buffer_hi
;
1028 /* Control structure holding the read/write/etc. pointers into the
1029 trace buffer. We need more than one of these to implement a
1030 transaction-like mechanism to garantees that both GDBserver and the
1031 in-process agent can try to change the trace buffer
1034 struct trace_buffer_control
1036 /* Pointer to the first trace frame in the buffer. In the
1037 non-circular case, this is equal to trace_buffer_lo, otherwise it
1038 moves around in the buffer. */
1039 unsigned char *start
;
1041 /* Pointer to the free part of the trace buffer. Note that we clear
1042 several bytes at and after this pointer, so that traceframe
1043 scans/searches terminate properly. */
1044 unsigned char *free
;
1046 /* Pointer to the byte after the end of the free part. Note that
1047 this may be smaller than trace_buffer_free in the circular case,
1048 and means that the free part is in two pieces. Initially it is
1049 equal to trace_buffer_hi, then is generally equivalent to
1050 trace_buffer_start. */
1051 unsigned char *end_free
;
1053 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1054 this is the point at which the trace data breaks, and resumes at
1056 unsigned char *wrap
;
1059 /* Same as above, to be used by GDBserver when updating the in-process
1061 struct ipa_trace_buffer_control
1070 /* We have possibly both GDBserver and an inferior thread accessing
1071 the same IPA trace buffer memory. The IPA is the producer (tries
1072 to put new frames in the buffer), while GDBserver occasionally
1073 consumes them, that is, flushes the IPA's buffer into its own
1074 buffer. Both sides need to update the trace buffer control
1075 pointers (current head, tail, etc.). We can't use a global lock to
1076 synchronize the accesses, as otherwise we could deadlock GDBserver
1077 (if the thread holding the lock stops for a signal, say). So
1078 instead of that, we use a transaction scheme where GDBserver writes
1079 always prevail over the IPAs writes, and, we have the IPA detect
1080 the commit failure/overwrite, and retry the whole attempt. This is
1081 mainly implemented by having a global token object that represents
1082 who wrote last to the buffer control structure. We need to freeze
1083 any inferior writing to the buffer while GDBserver touches memory,
1084 so that the inferior can correctly detect that GDBserver had been
1085 there, otherwise, it could mistakingly think its commit was
1086 successful; that's implemented by simply having GDBserver set a
1087 breakpoint the inferior hits if it is the critical region.
1089 There are three cycling trace buffer control structure copies
1090 (buffer head, tail, etc.), with the token object including an index
1091 indicating which is current live copy. The IPA tentatively builds
1092 an updated copy in a non-current control structure, while GDBserver
1093 always clobbers the current version directly. The IPA then tries
1094 to atomically "commit" its version; if GDBserver clobbered the
1095 structure meanwhile, that will fail, and the IPA restarts the
1098 Listing the step in further detail, we have:
1100 In-process agent (producer):
1102 - passes by `about_to_request_buffer_space' breakpoint/lock
1104 - reads current token, extracts current trace buffer control index,
1105 and starts tentatively updating the rightmost one (0->1, 1->2,
1106 2->0). Note that only one inferior thread is executing this code
1107 at any given time, due to an outer lock in the jump pads.
1109 - updates counters, and tries to commit the token.
1111 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1112 leaving the sync region.
1114 - checks if the update was effective.
1116 - if trace buffer was found full, hits flush_trace_buffer
1117 breakpoint, and restarts later afterwards.
1119 GDBserver (consumer):
1121 - sets `about_to_request_buffer_space' breakpoint/lock.
1123 - updates the token unconditionally, using the current buffer
1124 control index, since it knows that the IP agent always writes to
1125 the rightmost, and due to the breakpoint, at most one IP thread
1126 can try to update the trace buffer concurrently to GDBserver, so
1127 there will be no danger of trace buffer control index wrap making
1128 the IPA write to the same index as GDBserver.
1130 - flushes the IP agent's trace buffer completely, and updates the
1131 current trace buffer control structure. GDBserver *always* wins.
1133 - removes the `about_to_request_buffer_space' breakpoint.
1135 The token is stored in the `trace_buffer_ctrl_curr' variable.
1136 Internally, it's bits are defined as:
1138 |-------------+-----+-------------+--------+-------------+--------------|
1139 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1140 |-------------+-----+-------------+--------+-------------+--------------|
1141 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1142 |-------------+-----+-------------+--------+-------------+--------------|
1144 GSB - GDBserver Stamp Bit
1145 PC - Previous Counter
1146 CC - Current Counter
1147 TBCI - Trace Buffer Control Index
1150 An IPA update of `trace_buffer_ctrl_curr' does:
1152 - read CC from the current token, save as PC.
1154 - atomically tries to write PC+1,CC
1156 A GDBserver update of `trace_buffer_ctrl_curr' does:
1158 - reads PC and CC from the current token.
1163 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1164 for the counters described below. The cleared bits are used to
1165 hold the index of the items of the `trace_buffer_ctrl' array that
1167 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1169 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1170 counter, and the `current' counter. */
1172 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1173 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1175 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1176 always stamps this bit as set. */
1177 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1179 #ifdef IN_PROCESS_AGENT
1180 IP_AGENT_EXPORT_VAR
struct trace_buffer_control trace_buffer_ctrl
[3];
1181 IP_AGENT_EXPORT_VAR
unsigned int trace_buffer_ctrl_curr
;
1183 # define TRACE_BUFFER_CTRL_CURR \
1184 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1188 /* The GDBserver side agent only needs one instance of this object, as
1189 it doesn't need to sync with itself. Define it as array anyway so
1190 that the rest of the code base doesn't need to care for the
1192 struct trace_buffer_control trace_buffer_ctrl
[1];
1193 # define TRACE_BUFFER_CTRL_CURR 0
1196 /* These are convenience macros used to access the current trace
1197 buffer control in effect. */
1198 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1199 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1200 #define trace_buffer_end_free \
1201 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1202 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1205 /* Macro that returns a pointer to the first traceframe in the buffer. */
1207 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1209 /* Macro that returns a pointer to the next traceframe in the buffer.
1210 If the computed location is beyond the wraparound point, subtract
1211 the offset of the wraparound. */
1213 #define NEXT_TRACEFRAME_1(TF) \
1214 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1216 #define NEXT_TRACEFRAME(TF) \
1217 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1218 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1219 ? (trace_buffer_wrap - trace_buffer_lo) \
1222 /* The difference between these counters represents the total number
1223 of complete traceframes present in the trace buffer. The IP agent
1224 writes to the write count, GDBserver writes to read count. */
1226 IP_AGENT_EXPORT_VAR
unsigned int traceframe_write_count
;
1227 IP_AGENT_EXPORT_VAR
unsigned int traceframe_read_count
;
1229 /* Convenience macro. */
1231 #define traceframe_count \
1232 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1234 /* The count of all traceframes created in the current run, including
1235 ones that were discarded to make room. */
1237 IP_AGENT_EXPORT_VAR
int traceframes_created
;
1239 #ifndef IN_PROCESS_AGENT
1241 /* Read-only regions are address ranges whose contents don't change,
1242 and so can be read from target memory even while looking at a trace
1243 frame. Without these, disassembly for instance will likely fail,
1244 because the program code is not usually collected into a trace
1245 frame. This data structure does not need to be very complicated or
1246 particularly efficient, it's only going to be used occasionally,
1247 and only by some commands. */
1249 struct readonly_region
1251 /* The bounds of the region. */
1252 CORE_ADDR start
, end
;
1254 /* Link to the next one. */
1255 struct readonly_region
*next
;
1258 /* Linked list of readonly regions. This list stays in effect from
1259 one tstart to the next. */
1261 static struct readonly_region
*readonly_regions
;
1265 /* The global that controls tracing overall. */
1267 IP_AGENT_EXPORT_VAR
int tracing
;
1269 #ifndef IN_PROCESS_AGENT
1271 /* Controls whether tracing should continue after GDB disconnects. */
1273 int disconnected_tracing
;
1275 /* The reason for the last tracing run to have stopped. We initialize
1276 to a distinct string so that GDB can distinguish between "stopped
1277 after running" and "stopped because never run" cases. */
1279 static const char *tracing_stop_reason
= "tnotrun";
1281 static int tracing_stop_tpnum
;
1283 /* 64-bit timestamps for the trace run's start and finish, expressed
1284 in microseconds from the Unix epoch. */
1286 LONGEST tracing_start_time
;
1287 LONGEST tracing_stop_time
;
1289 /* The (optional) user-supplied name of the user that started the run.
1290 This is an arbitrary string, and may be NULL. */
1292 char *tracing_user_name
;
1294 /* Optional user-supplied text describing the run. This is
1295 an arbitrary string, and may be NULL. */
1297 char *tracing_notes
;
1299 /* Optional user-supplied text explaining a tstop command. This is an
1300 arbitrary string, and may be NULL. */
1302 char *tracing_stop_note
;
1306 /* Functions local to this file. */
1308 /* Base "class" for tracepoint type specific data to be passed down to
1309 collect_data_at_tracepoint. */
1310 struct tracepoint_hit_ctx
1312 enum tracepoint_type type
;
1315 #ifdef IN_PROCESS_AGENT
1317 /* Fast/jump tracepoint specific data to be passed down to
1318 collect_data_at_tracepoint. */
1319 struct fast_tracepoint_ctx
1321 struct tracepoint_hit_ctx base
;
1323 struct regcache regcache
;
1324 int regcache_initted
;
1325 unsigned char *regspace
;
1327 unsigned char *regs
;
1328 struct tracepoint
*tpoint
;
1331 /* Static tracepoint specific data to be passed down to
1332 collect_data_at_tracepoint. */
1333 struct static_tracepoint_ctx
1335 struct tracepoint_hit_ctx base
;
1337 /* The regcache corresponding to the registers state at the time of
1338 the tracepoint hit. Initialized lazily, from REGS. */
1339 struct regcache regcache
;
1340 int regcache_initted
;
1342 /* The buffer space REGCACHE above uses. We use a separate buffer
1343 instead of letting the regcache malloc for both signal safety and
1344 performance reasons; this is allocated on the stack instead. */
1345 unsigned char *regspace
;
1347 /* The register buffer as passed on by lttng/ust. */
1348 struct registers
*regs
;
1350 /* The "printf" formatter and the args the user passed to the marker
1351 call. We use this to be able to collect "static trace data"
1356 /* The GDB tracepoint matching the probed marker that was "hit". */
1357 struct tracepoint
*tpoint
;
1362 /* Static tracepoint specific data to be passed down to
1363 collect_data_at_tracepoint. */
1364 struct trap_tracepoint_ctx
1366 struct tracepoint_hit_ctx base
;
1368 struct regcache
*regcache
;
1373 #ifndef IN_PROCESS_AGENT
1374 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1375 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1378 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1379 struct tracepoint
*tpoint
);
1381 #ifndef IN_PROCESS_AGENT
1382 static void clear_readonly_regions (void);
1383 static void clear_installed_tracepoints (void);
1386 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1388 struct tracepoint
*tpoint
);
1389 #ifndef IN_PROCESS_AGENT
1390 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1392 struct tracepoint
*tpoint
, int current_step
);
1393 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1394 CORE_ADDR
*jump_entry
);
1396 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1398 struct tracepoint
*tpoint
,
1399 struct traceframe
*tframe
,
1400 struct tracepoint_action
*taction
);
1402 #ifndef IN_PROCESS_AGENT
1403 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1405 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1406 static void download_tracepoint (struct tracepoint
*);
1407 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1408 static void clone_fast_tracepoint (struct tracepoint
*to
,
1409 const struct tracepoint
*from
);
1412 static LONGEST
get_timestamp (void);
1414 #if defined(__GNUC__)
1415 # define memory_barrier() asm volatile ("" : : : "memory")
1417 # define memory_barrier() do {} while (0)
1420 /* We only build the IPA if this builtin is supported, and there are
1421 no uses of this in GDBserver itself, so we're safe in defining this
1423 #define cmpxchg(mem, oldval, newval) \
1424 __sync_val_compare_and_swap (mem, oldval, newval)
1426 /* Record that an error occurred during expression evaluation. */
1429 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1430 enum eval_result_type rtype
)
1432 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1433 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1435 #ifdef IN_PROCESS_AGENT
1436 /* Only record the first error we get. */
1437 if (cmpxchg (&expr_eval_result
,
1439 rtype
) != expr_eval_no_error
)
1442 if (expr_eval_result
!= expr_eval_no_error
)
1446 error_tracepoint
= tpoint
;
1449 /* Trace buffer management. */
1452 clear_trace_buffer (void)
1454 trace_buffer_start
= trace_buffer_lo
;
1455 trace_buffer_free
= trace_buffer_lo
;
1456 trace_buffer_end_free
= trace_buffer_hi
;
1457 trace_buffer_wrap
= trace_buffer_hi
;
1458 /* A traceframe with zeroed fields marks the end of trace data. */
1459 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1460 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1461 traceframe_read_count
= traceframe_write_count
= 0;
1462 traceframes_created
= 0;
1465 #ifndef IN_PROCESS_AGENT
1468 clear_inferior_trace_buffer (void)
1470 CORE_ADDR ipa_trace_buffer_lo
;
1471 CORE_ADDR ipa_trace_buffer_hi
;
1472 struct traceframe ipa_traceframe
= { 0 };
1473 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1475 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1476 &ipa_trace_buffer_lo
);
1477 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1478 &ipa_trace_buffer_hi
);
1480 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1481 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1482 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1483 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1485 /* A traceframe with zeroed fields marks the end of trace data. */
1486 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1487 (unsigned char *) &ipa_trace_buffer_ctrl
,
1488 sizeof (ipa_trace_buffer_ctrl
));
1490 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1492 /* A traceframe with zeroed fields marks the end of trace data. */
1493 write_inferior_memory (ipa_trace_buffer_lo
,
1494 (unsigned char *) &ipa_traceframe
,
1495 sizeof (ipa_traceframe
));
1497 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1498 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1499 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1505 init_trace_buffer (LONGEST bufsize
)
1509 trace_buffer_size
= bufsize
;
1511 /* Make sure to internally allocate at least space for the EOB
1513 alloc_size
= (bufsize
< TRACEFRAME_EOB_MARKER_SIZE
1514 ? TRACEFRAME_EOB_MARKER_SIZE
: bufsize
);
1515 trace_buffer_lo
= (unsigned char *) xrealloc (trace_buffer_lo
, alloc_size
);
1517 trace_buffer_hi
= trace_buffer_lo
+ trace_buffer_size
;
1519 clear_trace_buffer ();
1522 #ifdef IN_PROCESS_AGENT
1524 IP_AGENT_EXPORT_FUNC
void
1525 about_to_request_buffer_space (void)
1527 /* GDBserver places breakpoint here while it goes about to flush
1528 data at random times. */
1529 UNKNOWN_SIDE_EFFECTS();
1534 /* Carve out a piece of the trace buffer, returning NULL in case of
1538 trace_buffer_alloc (size_t amt
)
1540 unsigned char *rslt
;
1541 struct trace_buffer_control
*tbctrl
;
1543 #ifdef IN_PROCESS_AGENT
1544 unsigned int prev
, prev_filtered
;
1545 unsigned int commit_count
;
1546 unsigned int commit
;
1547 unsigned int readout
;
1549 struct traceframe
*oldest
;
1550 unsigned char *new_start
;
1553 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1554 (long) amt
, (long) sizeof (struct traceframe
));
1556 /* Account for the EOB marker. */
1557 amt
+= TRACEFRAME_EOB_MARKER_SIZE
;
1559 #ifdef IN_PROCESS_AGENT
1563 /* Read the current token and extract the index to try to write to,
1564 storing it in CURR. */
1565 prev
= trace_buffer_ctrl_curr
;
1566 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1567 curr
= prev_filtered
+ 1;
1571 about_to_request_buffer_space ();
1573 /* Start out with a copy of the current state. GDBserver may be
1574 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1575 be able to commit anyway if that happens. */
1576 trace_buffer_ctrl
[curr
]
1577 = trace_buffer_ctrl
[prev_filtered
];
1578 trace_debug ("trying curr=%u", curr
);
1580 /* The GDBserver's agent doesn't need all that syncing, and always
1581 updates TCB 0 (there's only one, mind you). */
1584 tbctrl
= &trace_buffer_ctrl
[curr
];
1586 /* Offsets are easier to grok for debugging than raw addresses,
1587 especially for the small trace buffer sizes that are useful for
1589 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1591 (int) (tbctrl
->start
- trace_buffer_lo
),
1592 (int) (tbctrl
->free
- trace_buffer_lo
),
1593 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1594 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1595 (int) (trace_buffer_hi
- trace_buffer_lo
));
1597 /* The algorithm here is to keep trying to get a contiguous block of
1598 the requested size, possibly discarding older traceframes to free
1599 up space. Since free space might come in one or two pieces,
1600 depending on whether discarded traceframes wrapped around at the
1601 high end of the buffer, we test both pieces after each
1605 /* First, if we have two free parts, try the upper one first. */
1606 if (tbctrl
->end_free
< tbctrl
->free
)
1608 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1609 /* We have enough in the upper part. */
1613 /* Our high part of free space wasn't enough. Give up
1614 on it for now, set wraparound. We will recover the
1615 space later, if/when the wrapped-around traceframe is
1617 trace_debug ("Upper part too small, setting wraparound");
1618 tbctrl
->wrap
= tbctrl
->free
;
1619 tbctrl
->free
= trace_buffer_lo
;
1623 /* The normal case. */
1624 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1627 #ifdef IN_PROCESS_AGENT
1628 /* The IP Agent's buffer is always circular. It isn't used
1629 currently, but `circular_trace_buffer' could represent
1630 GDBserver's mode. If we didn't find space, ask GDBserver to
1633 flush_trace_buffer ();
1637 trace_debug ("gdbserver flushed buffer, retrying");
1641 /* GDBserver cancelled the tracing. Bail out as well. */
1644 /* If we're here, then neither part is big enough, and
1645 non-circular trace buffers are now full. */
1646 if (!circular_trace_buffer
)
1648 trace_debug ("Not enough space in the trace buffer");
1652 trace_debug ("Need more space in the trace buffer");
1654 /* If we have a circular buffer, we can try discarding the
1655 oldest traceframe and see if that helps. */
1656 oldest
= FIRST_TRACEFRAME ();
1657 if (oldest
->tpnum
== 0)
1659 /* Not good; we have no traceframes to free. Perhaps we're
1660 asking for a block that is larger than the buffer? In
1661 any case, give up. */
1662 trace_debug ("No traceframes to discard");
1666 /* We don't run this code in the in-process agent currently.
1667 E.g., we could leave the in-process agent in autonomous
1668 circular mode if we only have fast tracepoints. If we do
1669 that, then this bit becomes racy with GDBserver, which also
1670 writes to this counter. */
1671 --traceframe_write_count
;
1673 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1674 /* If we freed the traceframe that wrapped around, go back
1675 to the non-wrap case. */
1676 if (new_start
< tbctrl
->start
)
1678 trace_debug ("Discarding past the wraparound");
1679 tbctrl
->wrap
= trace_buffer_hi
;
1681 tbctrl
->start
= new_start
;
1682 tbctrl
->end_free
= tbctrl
->start
;
1684 trace_debug ("Discarded a traceframe\n"
1685 "Trace buffer [%d], start=%d free=%d "
1686 "endfree=%d wrap=%d hi=%d",
1688 (int) (tbctrl
->start
- trace_buffer_lo
),
1689 (int) (tbctrl
->free
- trace_buffer_lo
),
1690 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1691 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1692 (int) (trace_buffer_hi
- trace_buffer_lo
));
1694 /* Now go back around the loop. The discard might have resulted
1695 in either one or two pieces of free space, so we want to try
1696 both before freeing any more traceframes. */
1700 /* If we get here, we know we can provide the asked-for space. */
1702 rslt
= tbctrl
->free
;
1704 /* Adjust the request back down, now that we know we have space for
1705 the marker, but don't commit to AMT yet, we may still need to
1706 restart the operation if GDBserver touches the trace buffer
1707 (obviously only important in the in-process agent's version). */
1708 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1710 /* Or not. If GDBserver changed the trace buffer behind our back,
1711 we get to restart a new allocation attempt. */
1713 #ifdef IN_PROCESS_AGENT
1714 /* Build the tentative token. */
1715 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1716 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1717 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1721 /* Try to commit it. */
1722 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1723 if (readout
!= prev
)
1725 trace_debug ("GDBserver has touched the trace buffer, restarting."
1726 " (prev=%08x, commit=%08x, readout=%08x)",
1727 prev
, commit
, readout
);
1731 /* Hold your horses here. Even if that change was committed,
1732 GDBserver could come in, and clobber it. We need to hold to be
1733 able to tell if GDBserver clobbers before or after we committed
1734 the change. Whenever GDBserver goes about touching the IPA
1735 buffer, it sets a breakpoint in this routine, so we have a sync
1737 about_to_request_buffer_space ();
1739 /* Check if the change has been effective, even if GDBserver stopped
1740 us at the breakpoint. */
1743 unsigned int refetch
;
1747 refetch
= trace_buffer_ctrl_curr
;
1749 if (refetch
== commit
1750 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1753 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1754 "readout=%08x, refetch=%08x)",
1755 prev
, commit
, readout
, refetch
);
1759 trace_debug ("GDBserver has touched the trace buffer, not effective."
1760 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1761 prev
, commit
, readout
, refetch
);
1767 /* We have a new piece of the trace buffer. Hurray! */
1769 /* Add an EOB marker just past this allocation. */
1770 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1771 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1773 /* Adjust the request back down, now that we know we have space for
1775 amt
-= sizeof (struct traceframe
);
1779 trace_debug ("Allocated %d bytes", (int) amt
);
1780 trace_debug ("Trace buffer [%d] start=%d free=%d "
1781 "endfree=%d wrap=%d hi=%d",
1783 (int) (tbctrl
->start
- trace_buffer_lo
),
1784 (int) (tbctrl
->free
- trace_buffer_lo
),
1785 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1786 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1787 (int) (trace_buffer_hi
- trace_buffer_lo
));
1793 #ifndef IN_PROCESS_AGENT
1795 /* Return the total free space. This is not necessarily the largest
1796 block we can allocate, because of the two-part case. */
1801 if (trace_buffer_free
<= trace_buffer_end_free
)
1802 return trace_buffer_end_free
- trace_buffer_free
;
1804 return ((trace_buffer_end_free
- trace_buffer_lo
)
1805 + (trace_buffer_hi
- trace_buffer_free
));
1808 /* An 'S' in continuation packets indicates remainder are for
1811 static int seen_step_action_flag
;
1813 /* Create a tracepoint (location) with given number and address. Add this
1814 new tracepoint to list and sort this list. */
1816 static struct tracepoint
*
1817 add_tracepoint (int num
, CORE_ADDR addr
)
1819 struct tracepoint
*tpoint
, **tp_next
;
1821 tpoint
= XNEW (struct tracepoint
);
1822 tpoint
->number
= num
;
1823 tpoint
->address
= addr
;
1824 tpoint
->numactions
= 0;
1825 tpoint
->actions
= NULL
;
1826 tpoint
->actions_str
= NULL
;
1827 tpoint
->cond
= NULL
;
1828 tpoint
->num_step_actions
= 0;
1829 tpoint
->step_actions
= NULL
;
1830 tpoint
->step_actions_str
= NULL
;
1831 /* Start all off as regular (slow) tracepoints. */
1832 tpoint
->type
= trap_tracepoint
;
1833 tpoint
->orig_size
= -1;
1834 tpoint
->source_strings
= NULL
;
1835 tpoint
->compiled_cond
= 0;
1836 tpoint
->handle
= NULL
;
1837 tpoint
->next
= NULL
;
1839 /* Find a place to insert this tracepoint into list in order to keep
1840 the tracepoint list still in the ascending order. There may be
1841 multiple tracepoints at the same address as TPOINT's, and this
1842 guarantees TPOINT is inserted after all the tracepoints which are
1843 set at the same address. For example, fast tracepoints A, B, C are
1844 set at the same address, and D is to be insert at the same place as
1847 -->| A |--> | B |-->| C |->...
1849 One jump pad was created for tracepoint A, B, and C, and the target
1850 address of A is referenced/used in jump pad. So jump pad will let
1851 inferior jump to A. If D is inserted in front of A, like this,
1853 -->| D |-->| A |--> | B |-->| C |->...
1855 without updating jump pad, D is not reachable during collect, which
1856 is wrong. As we can see, the order of B, C and D doesn't matter, but
1857 A should always be the `first' one. */
1858 for (tp_next
= &tracepoints
;
1859 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1860 tp_next
= &(*tp_next
)->next
)
1862 tpoint
->next
= *tp_next
;
1864 last_tracepoint
= tpoint
;
1866 seen_step_action_flag
= 0;
1871 #ifndef IN_PROCESS_AGENT
1873 /* Return the tracepoint with the given number and address, or NULL. */
1875 static struct tracepoint
*
1876 find_tracepoint (int id
, CORE_ADDR addr
)
1878 struct tracepoint
*tpoint
;
1880 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1881 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1887 /* Remove TPOINT from global list. */
1890 remove_tracepoint (struct tracepoint
*tpoint
)
1892 struct tracepoint
*tp
, *tp_prev
;
1894 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1895 tp_prev
= tp
, tp
= tp
->next
)
1901 tp_prev
->next
= tp
->next
;
1903 tracepoints
= tp
->next
;
1909 /* There may be several tracepoints with the same number (because they
1910 are "locations", in GDB parlance); return the next one after the
1911 given tracepoint, or search from the beginning of the list if the
1912 first argument is NULL. */
1914 static struct tracepoint
*
1915 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1917 struct tracepoint
*tpoint
;
1920 tpoint
= prev_tp
->next
;
1922 tpoint
= tracepoints
;
1923 for (; tpoint
; tpoint
= tpoint
->next
)
1924 if (tpoint
->number
== num
)
1932 /* Append another action to perform when the tracepoint triggers. */
1935 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1941 seen_step_action_flag
= 1;
1949 char *act_start
= act
;
1950 struct tracepoint_action
*action
= NULL
;
1956 struct collect_memory_action
*maction
=
1957 XNEW (struct collect_memory_action
);
1961 maction
->base
.type
= *act
;
1962 action
= &maction
->base
;
1965 is_neg
= (*act
== '-');
1968 act
= unpack_varlen_hex (act
, &basereg
);
1970 act
= unpack_varlen_hex (act
, &maction
->addr
);
1972 act
= unpack_varlen_hex (act
, &maction
->len
);
1973 maction
->basereg
= (is_neg
1976 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1977 pulongest (maction
->len
),
1978 paddress (maction
->addr
), maction
->basereg
);
1983 struct collect_registers_action
*raction
=
1984 XNEW (struct collect_registers_action
);
1986 raction
->base
.type
= *act
;
1987 action
= &raction
->base
;
1989 trace_debug ("Want to collect registers");
1991 /* skip past hex digits of mask for now */
1992 while (isxdigit(*act
))
1998 struct collect_static_trace_data_action
*raction
=
1999 XNEW (struct collect_static_trace_data_action
);
2001 raction
->base
.type
= *act
;
2002 action
= &raction
->base
;
2004 trace_debug ("Want to collect static trace data");
2009 trace_debug ("Unexpected step action, ignoring");
2014 struct eval_expr_action
*xaction
= XNEW (struct eval_expr_action
);
2016 xaction
->base
.type
= *act
;
2017 action
= &xaction
->base
;
2019 trace_debug ("Want to evaluate expression");
2020 xaction
->expr
= gdb_parse_agent_expr (&act
);
2024 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2033 if (seen_step_action_flag
)
2035 tpoint
->num_step_actions
++;
2037 tpoint
->step_actions
2038 = XRESIZEVEC (struct tracepoint_action
*, tpoint
->step_actions
,
2039 tpoint
->num_step_actions
);
2040 tpoint
->step_actions_str
2041 = XRESIZEVEC (char *, tpoint
->step_actions_str
,
2042 tpoint
->num_step_actions
);
2043 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2044 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2045 = savestring (act_start
, act
- act_start
);
2049 tpoint
->numactions
++;
2051 = XRESIZEVEC (struct tracepoint_action
*, tpoint
->actions
,
2052 tpoint
->numactions
);
2054 = XRESIZEVEC (char *, tpoint
->actions_str
, tpoint
->numactions
);
2055 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2056 tpoint
->actions_str
[tpoint
->numactions
- 1]
2057 = savestring (act_start
, act
- act_start
);
2064 /* Find or create a trace state variable with the given number. */
2066 static struct trace_state_variable
*
2067 get_trace_state_variable (int num
)
2069 struct trace_state_variable
*tsv
;
2071 #ifdef IN_PROCESS_AGENT
2072 /* Search for an existing variable. */
2073 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2074 if (tsv
->number
== num
)
2078 /* Search for an existing variable. */
2079 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2080 if (tsv
->number
== num
)
2086 /* Find or create a trace state variable with the given number. */
2088 static struct trace_state_variable
*
2089 create_trace_state_variable (int num
, int gdb
)
2091 struct trace_state_variable
*tsv
;
2093 tsv
= get_trace_state_variable (num
);
2097 /* Create a new variable. */
2098 tsv
= XNEW (struct trace_state_variable
);
2100 tsv
->initial_value
= 0;
2104 #ifdef IN_PROCESS_AGENT
2107 tsv
->next
= alloced_trace_state_variables
;
2108 alloced_trace_state_variables
= tsv
;
2113 tsv
->next
= trace_state_variables
;
2114 trace_state_variables
= tsv
;
2119 IP_AGENT_EXPORT_FUNC LONGEST
2120 get_trace_state_variable_value (int num
)
2122 struct trace_state_variable
*tsv
;
2124 tsv
= get_trace_state_variable (num
);
2128 trace_debug ("No trace state variable %d, skipping value get", num
);
2132 /* Call a getter function if we have one. While it's tempting to
2133 set up something to only call the getter once per tracepoint hit,
2134 it could run afoul of thread races. Better to let the getter
2135 handle it directly, if necessary to worry about it. */
2137 tsv
->value
= (tsv
->getter
) ();
2139 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2140 num
, plongest (tsv
->value
));
2145 IP_AGENT_EXPORT_FUNC
void
2146 set_trace_state_variable_value (int num
, LONGEST val
)
2148 struct trace_state_variable
*tsv
;
2150 tsv
= get_trace_state_variable (num
);
2154 trace_debug ("No trace state variable %d, skipping value set", num
);
2162 agent_get_trace_state_variable_value (int num
)
2164 return get_trace_state_variable_value (num
);
2168 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2170 set_trace_state_variable_value (num
, val
);
2174 set_trace_state_variable_name (int num
, const char *name
)
2176 struct trace_state_variable
*tsv
;
2178 tsv
= get_trace_state_variable (num
);
2182 trace_debug ("No trace state variable %d, skipping name set", num
);
2186 tsv
->name
= (char *) name
;
2190 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2192 struct trace_state_variable
*tsv
;
2194 tsv
= get_trace_state_variable (num
);
2198 trace_debug ("No trace state variable %d, skipping getter set", num
);
2202 tsv
->getter
= getter
;
2205 /* Add a raw traceframe for the given tracepoint. */
2207 static struct traceframe
*
2208 add_traceframe (struct tracepoint
*tpoint
)
2210 struct traceframe
*tframe
;
2213 = (struct traceframe
*) trace_buffer_alloc (sizeof (struct traceframe
));
2218 tframe
->tpnum
= tpoint
->number
;
2219 tframe
->data_size
= 0;
2224 /* Add a block to the traceframe currently being worked on. */
2226 static unsigned char *
2227 add_traceframe_block (struct traceframe
*tframe
,
2228 struct tracepoint
*tpoint
, int amt
)
2230 unsigned char *block
;
2235 block
= (unsigned char *) trace_buffer_alloc (amt
);
2240 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2242 tframe
->data_size
+= amt
;
2243 tpoint
->traceframe_usage
+= amt
;
2248 /* Flag that the current traceframe is finished. */
2251 finish_traceframe (struct traceframe
*tframe
)
2253 ++traceframe_write_count
;
2254 ++traceframes_created
;
2257 #ifndef IN_PROCESS_AGENT
2259 /* Given a traceframe number NUM, find the NUMth traceframe in the
2262 static struct traceframe
*
2263 find_traceframe (int num
)
2265 struct traceframe
*tframe
;
2268 for (tframe
= FIRST_TRACEFRAME ();
2270 tframe
= NEXT_TRACEFRAME (tframe
))
2281 get_traceframe_address (struct traceframe
*tframe
)
2284 struct tracepoint
*tpoint
;
2286 addr
= traceframe_get_pc (tframe
);
2291 /* Fallback strategy, will be incorrect for while-stepping frames
2292 and multi-location tracepoints. */
2293 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2294 return tpoint
->address
;
2297 /* Search for the next traceframe whose address is inside or outside
2300 static struct traceframe
*
2301 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2304 struct traceframe
*tframe
;
2307 *tfnump
= current_traceframe
+ 1;
2308 tframe
= find_traceframe (*tfnump
);
2309 /* The search is not supposed to wrap around. */
2316 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2318 tfaddr
= get_traceframe_address (tframe
);
2320 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2321 : (lo
> tfaddr
|| tfaddr
> hi
))
2330 /* Search for the next traceframe recorded by the given tracepoint.
2331 Note that for multi-location tracepoints, this will find whatever
2332 location appears first. */
2334 static struct traceframe
*
2335 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2337 struct traceframe
*tframe
;
2339 *tfnump
= current_traceframe
+ 1;
2340 tframe
= find_traceframe (*tfnump
);
2341 /* The search is not supposed to wrap around. */
2348 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2350 if (tframe
->tpnum
== num
)
2361 #ifndef IN_PROCESS_AGENT
2363 /* Clear all past trace state. */
2366 cmd_qtinit (char *packet
)
2368 struct trace_state_variable
*tsv
, *prev
, *next
;
2370 /* Can't do this command without a pid attached. */
2371 if (current_thread
== NULL
)
2377 /* Make sure we don't try to read from a trace frame. */
2378 current_traceframe
= -1;
2382 trace_debug ("Initializing the trace");
2384 clear_installed_tracepoints ();
2385 clear_readonly_regions ();
2388 last_tracepoint
= NULL
;
2390 /* Clear out any leftover trace state variables. Ones with target
2391 defined getters should be kept however. */
2393 tsv
= trace_state_variables
;
2396 trace_debug ("Looking at var %d", tsv
->number
);
2397 if (tsv
->getter
== NULL
)
2403 trace_state_variables
= next
;
2404 trace_debug ("Deleting var %d", tsv
->number
);
2415 clear_trace_buffer ();
2416 clear_inferior_trace_buffer ();
2421 /* Unprobe the UST marker at ADDRESS. */
2424 unprobe_marker_at (CORE_ADDR address
)
2426 char cmd
[IPA_CMD_BUF_SIZE
];
2428 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2429 run_inferior_command (cmd
, strlen (cmd
) + 1);
2432 /* Restore the program to its pre-tracing state. This routine may be called
2433 in error situations, so it needs to be careful about only restoring
2434 from known-valid bits. */
2437 clear_installed_tracepoints (void)
2439 struct tracepoint
*tpoint
;
2440 struct tracepoint
*prev_stpoint
;
2444 prev_stpoint
= NULL
;
2446 /* Restore any bytes overwritten by tracepoints. */
2447 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2449 /* Catch the case where we might try to remove a tracepoint that
2450 was never actually installed. */
2451 if (tpoint
->handle
== NULL
)
2453 trace_debug ("Tracepoint %d at 0x%s was "
2454 "never installed, nothing to clear",
2455 tpoint
->number
, paddress (tpoint
->address
));
2459 switch (tpoint
->type
)
2461 case trap_tracepoint
:
2463 struct breakpoint
*bp
2464 = (struct breakpoint
*) tpoint
->handle
;
2466 delete_breakpoint (bp
);
2469 case fast_tracepoint
:
2471 struct fast_tracepoint_jump
*jump
2472 = (struct fast_tracepoint_jump
*) tpoint
->handle
;
2474 delete_fast_tracepoint_jump (jump
);
2477 case static_tracepoint
:
2478 if (prev_stpoint
!= NULL
2479 && prev_stpoint
->address
== tpoint
->address
)
2480 /* Nothing to do. We already unprobed a tracepoint set at
2481 this marker address (and there can only be one probe
2486 unprobe_marker_at (tpoint
->address
);
2487 prev_stpoint
= tpoint
;
2492 tpoint
->handle
= NULL
;
2498 /* Parse a packet that defines a tracepoint. */
2501 cmd_qtdp (char *own_buf
)
2504 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2505 int trail_hyphen
= 0;
2509 struct tracepoint
*tpoint
;
2511 char *packet
= own_buf
;
2513 packet
+= strlen ("QTDP:");
2515 /* A hyphen at the beginning marks a packet specifying actions for a
2516 tracepoint already supplied. */
2523 packet
= unpack_varlen_hex (packet
, &num
);
2524 ++packet
; /* skip a colon */
2525 packet
= unpack_varlen_hex (packet
, &addr
);
2526 ++packet
; /* skip a colon */
2528 /* See if we already have this tracepoint. */
2529 tpoint
= find_tracepoint (num
, addr
);
2533 /* Duplicate tracepoints are never allowed. */
2536 trace_debug ("Tracepoint error: tracepoint %d"
2537 " at 0x%s already exists",
2538 (int) num
, paddress (addr
));
2539 write_enn (own_buf
);
2543 tpoint
= add_tracepoint (num
, addr
);
2545 tpoint
->enabled
= (*packet
== 'E');
2546 ++packet
; /* skip 'E' */
2547 ++packet
; /* skip a colon */
2548 packet
= unpack_varlen_hex (packet
, &count
);
2549 tpoint
->step_count
= count
;
2550 ++packet
; /* skip a colon */
2551 packet
= unpack_varlen_hex (packet
, &count
);
2552 tpoint
->pass_count
= count
;
2553 /* See if we have any of the additional optional fields. */
2554 while (*packet
== ':')
2559 tpoint
->type
= fast_tracepoint
;
2561 packet
= unpack_varlen_hex (packet
, &count
);
2562 tpoint
->orig_size
= count
;
2564 else if (*packet
== 'S')
2566 tpoint
->type
= static_tracepoint
;
2569 else if (*packet
== 'X')
2571 actparm
= (char *) packet
;
2572 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2575 else if (*packet
== '-')
2577 else if (*packet
== '\0')
2580 trace_debug ("Unknown optional tracepoint field");
2585 trace_debug ("Also has actions\n");
2588 trace_debug ("Defined %stracepoint %d at 0x%s, "
2589 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2590 tpoint
->type
== fast_tracepoint
? "fast "
2591 : tpoint
->type
== static_tracepoint
? "static " : "",
2592 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2593 tpoint
->step_count
, tpoint
->pass_count
);
2596 add_tracepoint_action (tpoint
, packet
);
2599 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2600 (int) num
, paddress (addr
));
2601 write_enn (own_buf
);
2605 /* Install tracepoint during tracing only once for each tracepoint location.
2606 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2607 determine the last QTDP packet for one tracepoint location by checking
2608 trailing hyphen in QTDP packet. */
2609 if (tracing
&& !trail_hyphen
)
2611 struct tracepoint
*tp
= NULL
;
2613 /* Pause all threads temporarily while we patch tracepoints. */
2616 /* download_tracepoint will update global `tracepoints'
2617 list, so it is unsafe to leave threads in jump pad. */
2618 stabilize_threads ();
2620 /* Freeze threads. */
2624 if (tpoint
->type
!= trap_tracepoint
)
2626 /* Find another fast or static tracepoint at the same address. */
2627 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2629 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2630 && tp
->number
!= tpoint
->number
)
2634 /* TPOINT is installed at the same address as TP. */
2637 if (tpoint
->type
== fast_tracepoint
)
2638 clone_fast_tracepoint (tpoint
, tp
);
2639 else if (tpoint
->type
== static_tracepoint
)
2640 tpoint
->handle
= (void *) -1;
2644 if (use_agent
&& tpoint
->type
== fast_tracepoint
2645 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2647 /* Download and install fast tracepoint by agent. */
2648 if (tracepoint_send_agent (tpoint
) == 0)
2652 write_enn (own_buf
);
2653 remove_tracepoint (tpoint
);
2658 download_tracepoint (tpoint
);
2660 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2662 install_tracepoint (tpoint
, own_buf
);
2663 if (strcmp (own_buf
, "OK") != 0)
2664 remove_tracepoint (tpoint
);
2678 cmd_qtdpsrc (char *own_buf
)
2680 ULONGEST num
, addr
, start
, slen
;
2681 struct tracepoint
*tpoint
;
2682 char *packet
= own_buf
;
2683 char *saved
, *srctype
, *src
;
2685 struct source_string
*last
, *newlast
;
2687 packet
+= strlen ("QTDPsrc:");
2689 packet
= unpack_varlen_hex (packet
, &num
);
2690 ++packet
; /* skip a colon */
2691 packet
= unpack_varlen_hex (packet
, &addr
);
2692 ++packet
; /* skip a colon */
2694 /* See if we already have this tracepoint. */
2695 tpoint
= find_tracepoint (num
, addr
);
2699 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2700 (int) num
, paddress (addr
));
2701 write_enn (own_buf
);
2706 packet
= strchr (packet
, ':');
2707 srctype
= (char *) xmalloc (packet
- saved
+ 1);
2708 memcpy (srctype
, saved
, packet
- saved
);
2709 srctype
[packet
- saved
] = '\0';
2711 packet
= unpack_varlen_hex (packet
, &start
);
2712 ++packet
; /* skip a colon */
2713 packet
= unpack_varlen_hex (packet
, &slen
);
2714 ++packet
; /* skip a colon */
2715 src
= (char *) xmalloc (slen
+ 1);
2716 nbytes
= hex2bin (packet
, (gdb_byte
*) src
, strlen (packet
) / 2);
2719 newlast
= XNEW (struct source_string
);
2720 newlast
->type
= srctype
;
2722 newlast
->next
= NULL
;
2723 /* Always add a source string to the end of the list;
2724 this keeps sequences of actions/commands in the right
2726 if (tpoint
->source_strings
)
2728 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2730 last
->next
= newlast
;
2733 tpoint
->source_strings
= newlast
;
2739 cmd_qtdv (char *own_buf
)
2741 ULONGEST num
, val
, builtin
;
2744 struct trace_state_variable
*tsv
;
2745 char *packet
= own_buf
;
2747 packet
+= strlen ("QTDV:");
2749 packet
= unpack_varlen_hex (packet
, &num
);
2750 ++packet
; /* skip a colon */
2751 packet
= unpack_varlen_hex (packet
, &val
);
2752 ++packet
; /* skip a colon */
2753 packet
= unpack_varlen_hex (packet
, &builtin
);
2754 ++packet
; /* skip a colon */
2756 nbytes
= strlen (packet
) / 2;
2757 varname
= (char *) xmalloc (nbytes
+ 1);
2758 nbytes
= hex2bin (packet
, (gdb_byte
*) varname
, nbytes
);
2759 varname
[nbytes
] = '\0';
2761 tsv
= create_trace_state_variable (num
, 1);
2762 tsv
->initial_value
= (LONGEST
) val
;
2763 tsv
->name
= varname
;
2765 set_trace_state_variable_value (num
, (LONGEST
) val
);
2771 cmd_qtenable_disable (char *own_buf
, int enable
)
2773 char *packet
= own_buf
;
2775 struct tracepoint
*tp
;
2777 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2778 packet
= unpack_varlen_hex (packet
, &num
);
2779 ++packet
; /* skip a colon */
2780 packet
= unpack_varlen_hex (packet
, &addr
);
2782 tp
= find_tracepoint (num
, addr
);
2786 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2788 trace_debug ("Tracepoint %d at 0x%s is already %s",
2789 (int) num
, paddress (addr
),
2790 enable
? "enabled" : "disabled");
2795 trace_debug ("%s tracepoint %d at 0x%s",
2796 enable
? "Enabling" : "Disabling",
2797 (int) num
, paddress (addr
));
2799 tp
->enabled
= enable
;
2801 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2804 int offset
= offsetof (struct tracepoint
, enabled
);
2805 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2807 ret
= prepare_to_access_memory ();
2810 trace_debug ("Failed to temporarily stop inferior threads");
2811 write_enn (own_buf
);
2815 ret
= write_inferior_integer (obj_addr
, enable
);
2816 done_accessing_memory ();
2820 trace_debug ("Cannot write enabled flag into "
2821 "inferior process memory");
2822 write_enn (own_buf
);
2831 trace_debug ("Tracepoint %d at 0x%s not found",
2832 (int) num
, paddress (addr
));
2833 write_enn (own_buf
);
2838 cmd_qtv (char *own_buf
)
2843 char *packet
= own_buf
;
2845 packet
+= strlen ("qTV:");
2846 unpack_varlen_hex (packet
, &num
);
2848 if (current_traceframe
>= 0)
2850 err
= traceframe_read_tsv ((int) num
, &val
);
2853 strcpy (own_buf
, "U");
2857 /* Only make tsv's be undefined before the first trace run. After a
2858 trace run is over, the user might want to see the last value of
2859 the tsv, and it might not be available in a traceframe. */
2860 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2862 strcpy (own_buf
, "U");
2866 val
= get_trace_state_variable_value (num
);
2868 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2871 /* Clear out the list of readonly regions. */
2874 clear_readonly_regions (void)
2876 struct readonly_region
*roreg
;
2878 while (readonly_regions
)
2880 roreg
= readonly_regions
;
2881 readonly_regions
= readonly_regions
->next
;
2886 /* Parse the collection of address ranges whose contents GDB believes
2887 to be unchanging and so can be read directly from target memory
2888 even while looking at a traceframe. */
2891 cmd_qtro (char *own_buf
)
2893 ULONGEST start
, end
;
2894 struct readonly_region
*roreg
;
2895 char *packet
= own_buf
;
2897 trace_debug ("Want to mark readonly regions");
2899 clear_readonly_regions ();
2901 packet
+= strlen ("QTro");
2903 while (*packet
== ':')
2905 ++packet
; /* skip a colon */
2906 packet
= unpack_varlen_hex (packet
, &start
);
2907 ++packet
; /* skip a comma */
2908 packet
= unpack_varlen_hex (packet
, &end
);
2910 roreg
= XNEW (struct readonly_region
);
2911 roreg
->start
= start
;
2913 roreg
->next
= readonly_regions
;
2914 readonly_regions
= roreg
;
2915 trace_debug ("Added readonly region from 0x%s to 0x%s",
2916 paddress (roreg
->start
), paddress (roreg
->end
));
2922 /* Test to see if the given range is in our list of readonly ranges.
2923 We only test for being entirely within a range, GDB is not going to
2924 send a single memory packet that spans multiple regions. */
2927 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2929 struct readonly_region
*roreg
;
2931 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2932 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2938 static CORE_ADDR gdb_jump_pad_head
;
2940 /* Return the address of the next free jump space. */
2943 get_jump_space_head (void)
2945 if (gdb_jump_pad_head
== 0)
2947 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2948 &gdb_jump_pad_head
))
2950 internal_error (__FILE__
, __LINE__
,
2951 "error extracting jump_pad_buffer");
2955 return gdb_jump_pad_head
;
2958 /* Reserve USED bytes from the jump space. */
2961 claim_jump_space (ULONGEST used
)
2963 trace_debug ("claim_jump_space reserves %s bytes at %s",
2964 pulongest (used
), paddress (gdb_jump_pad_head
));
2965 gdb_jump_pad_head
+= used
;
2968 static CORE_ADDR trampoline_buffer_head
= 0;
2969 static CORE_ADDR trampoline_buffer_tail
;
2971 /* Reserve USED bytes from the trampoline buffer and return the
2972 address of the start of the reserved space in TRAMPOLINE. Returns
2973 non-zero if the space is successfully claimed. */
2976 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2978 if (!trampoline_buffer_head
)
2980 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2981 &trampoline_buffer_tail
))
2983 internal_error (__FILE__
, __LINE__
,
2984 "error extracting trampoline_buffer");
2987 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2988 &trampoline_buffer_head
))
2990 internal_error (__FILE__
, __LINE__
,
2991 "error extracting trampoline_buffer_end");
2995 /* Start claiming space from the top of the trampoline space. If
2996 the space is located at the bottom of the virtual address space,
2997 this reduces the possibility that corruption will occur if a null
2998 pointer is used to write to memory. */
2999 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
3001 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
3006 trampoline_buffer_head
-= used
;
3008 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
3009 pulongest (used
), paddress (trampoline_buffer_head
));
3011 *trampoline
= trampoline_buffer_head
;
3015 /* Returns non-zero if there is space allocated for use in trampolines
3016 for fast tracepoints. */
3019 have_fast_tracepoint_trampoline_buffer (char *buf
)
3021 CORE_ADDR trampoline_end
, errbuf
;
3023 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
3026 internal_error (__FILE__
, __LINE__
,
3027 "error extracting trampoline_buffer_end");
3033 strcpy (buf
, "was claiming");
3034 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3037 internal_error (__FILE__
, __LINE__
,
3038 "error extracting errbuf");
3041 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3044 return trampoline_end
!= 0;
3047 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3048 the command fails, or 0 otherwise. If the command ran
3049 successfully, but probing the marker failed, ERROUT will be filled
3050 with the error to reply to GDB, and -1 is also returned. This
3051 allows directly passing IPA errors to GDB. */
3054 probe_marker_at (CORE_ADDR address
, char *errout
)
3056 char cmd
[IPA_CMD_BUF_SIZE
];
3059 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3060 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3066 strcpy (errout
, cmd
);
3075 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3077 to
->jump_pad
= from
->jump_pad
;
3078 to
->jump_pad_end
= from
->jump_pad_end
;
3079 to
->trampoline
= from
->trampoline
;
3080 to
->trampoline_end
= from
->trampoline_end
;
3081 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3082 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3083 to
->handle
= from
->handle
;
3085 gdb_assert (from
->handle
);
3086 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3089 #define MAX_JUMP_SIZE 20
3091 /* Install fast tracepoint. Return 0 if successful, otherwise return
3095 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3097 CORE_ADDR jentry
, jump_entry
;
3098 CORE_ADDR trampoline
;
3100 ULONGEST trampoline_size
;
3102 /* The jump to the jump pad of the last fast tracepoint
3104 unsigned char fjump
[MAX_JUMP_SIZE
];
3105 ULONGEST fjump_size
;
3107 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3109 trace_debug ("Requested a fast tracepoint on an instruction "
3110 "that is of less than the minimum length.");
3114 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_collect_ptr
,
3117 error ("error extracting gdb_collect_ptr");
3121 jentry
= jump_entry
= get_jump_space_head ();
3124 trampoline_size
= 0;
3126 /* Install the jump pad. */
3127 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3130 ipa_sym_addrs
.addr_collecting
,
3133 &trampoline
, &trampoline_size
,
3135 &tpoint
->adjusted_insn_addr
,
3136 &tpoint
->adjusted_insn_addr_end
,
3143 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3146 if (tpoint
->handle
!= NULL
)
3148 tpoint
->jump_pad
= jump_entry
;
3149 tpoint
->jump_pad_end
= jentry
;
3150 tpoint
->trampoline
= trampoline
;
3151 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3153 /* Pad to 8-byte alignment. */
3154 jentry
= ((jentry
+ 7) & ~0x7);
3155 claim_jump_space (jentry
- jump_entry
);
3162 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3165 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3167 tpoint
->handle
= NULL
;
3170 if (tpoint
->type
== trap_tracepoint
)
3172 /* Tracepoints are installed as memory breakpoints. Just go
3173 ahead and install the trap. The breakpoints module
3174 handles duplicated breakpoints, and the memory read
3175 routine handles un-patching traps from memory reads. */
3176 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3177 tracepoint_handler
);
3179 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3181 if (!agent_loaded_p ())
3183 trace_debug ("Requested a %s tracepoint, but fast "
3184 "tracepoints aren't supported.",
3185 tpoint
->type
== static_tracepoint
? "static" : "fast");
3186 write_e_ipa_not_loaded (own_buf
);
3189 if (tpoint
->type
== static_tracepoint
3190 && !in_process_agent_supports_ust ())
3192 trace_debug ("Requested a static tracepoint, but static "
3193 "tracepoints are not supported.");
3194 write_e_ust_not_loaded (own_buf
);
3198 if (tpoint
->type
== fast_tracepoint
)
3199 install_fast_tracepoint (tpoint
, own_buf
);
3202 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3203 tpoint
->handle
= (void *) -1;
3208 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3210 if (tpoint
->handle
== NULL
)
3212 if (*own_buf
== '\0')
3213 write_enn (own_buf
);
3219 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3222 cmd_qtstart (char *packet
)
3224 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3225 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3227 trace_debug ("Starting the trace");
3229 /* Pause all threads temporarily while we patch tracepoints. */
3232 /* Get threads out of jump pads. Safe to do here, since this is a
3233 top level command. And, required to do here, since we're
3234 deleting/rewriting jump pads. */
3236 stabilize_threads ();
3238 /* Freeze threads. */
3241 /* Sync the fast tracepoints list in the inferior ftlib. */
3242 if (agent_loaded_p ())
3243 download_trace_state_variables ();
3245 /* No previous fast tpoint yet. */
3246 prev_ftpoint
= NULL
;
3248 /* No previous static tpoint yet. */
3249 prev_stpoint
= NULL
;
3253 if (agent_loaded_p ())
3255 /* Tell IPA about the correct tdesc. */
3256 if (write_inferior_integer (ipa_sym_addrs
.addr_ipa_tdesc_idx
,
3257 target_get_ipa_tdesc_idx ()))
3258 error ("Error setting ipa_tdesc_idx variable in lib");
3261 /* Start out empty. */
3262 if (agent_loaded_p ())
3263 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
, 0);
3265 /* Download and install tracepoints. */
3266 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3268 /* Ensure all the hit counts start at zero. */
3269 tpoint
->hit_count
= 0;
3270 tpoint
->traceframe_usage
= 0;
3272 if (tpoint
->type
== trap_tracepoint
)
3274 /* Tracepoints are installed as memory breakpoints. Just go
3275 ahead and install the trap. The breakpoints module
3276 handles duplicated breakpoints, and the memory read
3277 routine handles un-patching traps from memory reads. */
3278 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3279 tracepoint_handler
);
3281 else if (tpoint
->type
== fast_tracepoint
3282 || tpoint
->type
== static_tracepoint
)
3284 if (maybe_write_ipa_not_loaded (packet
))
3286 trace_debug ("Requested a %s tracepoint, but fast "
3287 "tracepoints aren't supported.",
3288 tpoint
->type
== static_tracepoint
3289 ? "static" : "fast");
3293 if (tpoint
->type
== fast_tracepoint
)
3296 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3298 if (prev_ftpoint
!= NULL
3299 && prev_ftpoint
->address
== tpoint
->address
)
3302 tracepoint_send_agent (tpoint
);
3304 download_tracepoint_1 (tpoint
);
3306 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3310 /* Tracepoint is installed successfully? */
3313 /* Download and install fast tracepoint by agent. */
3315 installed
= !tracepoint_send_agent (tpoint
);
3318 download_tracepoint_1 (tpoint
);
3319 installed
= !install_fast_tracepoint (tpoint
, packet
);
3323 prev_ftpoint
= tpoint
;
3328 if (!in_process_agent_supports_ust ())
3330 trace_debug ("Requested a static tracepoint, but static "
3331 "tracepoints are not supported.");
3335 download_tracepoint_1 (tpoint
);
3336 /* Can only probe a given marker once. */
3337 if (prev_stpoint
!= NULL
3338 && prev_stpoint
->address
== tpoint
->address
)
3339 tpoint
->handle
= (void *) -1;
3342 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3344 tpoint
->handle
= (void *) -1;
3346 /* So that we can handle multiple static tracepoints
3347 at the same address easily. */
3348 prev_stpoint
= tpoint
;
3354 tpptr
= tpoint
->obj_addr_on_target
;
3356 if (tpoint
== tracepoints
)
3357 /* First object in list, set the head pointer in the
3359 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3361 write_inferior_data_pointer (prev_tpptr
3362 + offsetof (struct tracepoint
, next
),
3366 /* Any failure in the inner loop is sufficient cause to give
3368 if (tpoint
->handle
== NULL
)
3372 /* Any error in tracepoint insertion is unacceptable; better to
3373 address the problem now, than end up with a useless or misleading
3377 clear_installed_tracepoints ();
3378 if (*packet
== '\0')
3384 stopping_tracepoint
= NULL
;
3385 trace_buffer_is_full
= 0;
3386 expr_eval_result
= expr_eval_no_error
;
3387 error_tracepoint
= NULL
;
3388 tracing_start_time
= get_timestamp ();
3390 /* Tracing is now active, hits will now start being logged. */
3393 if (agent_loaded_p ())
3395 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3397 internal_error (__FILE__
, __LINE__
,
3398 "Error setting tracing variable in lib");
3401 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3404 internal_error (__FILE__
, __LINE__
,
3405 "Error clearing stopping_tracepoint variable"
3409 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3411 internal_error (__FILE__
, __LINE__
,
3412 "Error clearing trace_buffer_is_full variable"
3416 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3417 stop_tracing_handler
);
3418 if (stop_tracing_bkpt
== NULL
)
3419 error ("Error setting stop_tracing breakpoint");
3421 flush_trace_buffer_bkpt
3422 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3423 flush_trace_buffer_handler
);
3424 if (flush_trace_buffer_bkpt
== NULL
)
3425 error ("Error setting flush_trace_buffer breakpoint");
3433 /* End a tracing run, filling in a stop reason to report back to GDB,
3434 and removing the tracepoints from the code. */
3441 trace_debug ("Tracing is already off, ignoring");
3445 trace_debug ("Stopping the trace");
3447 /* Pause all threads before removing fast jumps from memory,
3448 breakpoints, and touching IPA state variables (inferior memory).
3449 Some thread may hit the internal tracing breakpoints, or be
3450 collecting this moment, but that's ok, we don't release the
3451 tpoint object's memory or the jump pads here (we only do that
3452 when we're sure we can move all threads out of the jump pads).
3453 We can't now, since we may be getting here due to the inferior
3454 agent calling us. */
3457 /* Stop logging. Tracepoints can still be hit, but they will not be
3460 if (agent_loaded_p ())
3462 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3464 internal_error (__FILE__
, __LINE__
,
3465 "Error clearing tracing variable in lib");
3469 tracing_stop_time
= get_timestamp ();
3470 tracing_stop_reason
= "t???";
3471 tracing_stop_tpnum
= 0;
3472 if (stopping_tracepoint
)
3474 trace_debug ("Stopping the trace because "
3475 "tracepoint %d was hit %" PRIu64
" times",
3476 stopping_tracepoint
->number
,
3477 stopping_tracepoint
->pass_count
);
3478 tracing_stop_reason
= "tpasscount";
3479 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3481 else if (trace_buffer_is_full
)
3483 trace_debug ("Stopping the trace because the trace buffer is full");
3484 tracing_stop_reason
= "tfull";
3486 else if (expr_eval_result
!= expr_eval_no_error
)
3488 trace_debug ("Stopping the trace because of an expression eval error");
3489 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3490 tracing_stop_tpnum
= error_tracepoint
->number
;
3492 #ifndef IN_PROCESS_AGENT
3493 else if (!gdb_connected ())
3495 trace_debug ("Stopping the trace because GDB disconnected");
3496 tracing_stop_reason
= "tdisconnected";
3501 trace_debug ("Stopping the trace because of a tstop command");
3502 tracing_stop_reason
= "tstop";
3505 stopping_tracepoint
= NULL
;
3506 error_tracepoint
= NULL
;
3508 /* Clear out the tracepoints. */
3509 clear_installed_tracepoints ();
3511 if (agent_loaded_p ())
3513 /* Pull in fast tracepoint trace frames from the inferior lib
3514 buffer into our buffer, even if our buffer is already full,
3515 because we want to present the full number of created frames
3516 in addition to what fit in the trace buffer. */
3517 upload_fast_traceframes ();
3520 if (stop_tracing_bkpt
!= NULL
)
3522 delete_breakpoint (stop_tracing_bkpt
);
3523 stop_tracing_bkpt
= NULL
;
3526 if (flush_trace_buffer_bkpt
!= NULL
)
3528 delete_breakpoint (flush_trace_buffer_bkpt
);
3529 flush_trace_buffer_bkpt
= NULL
;
3536 stop_tracing_handler (CORE_ADDR addr
)
3538 trace_debug ("lib hit stop_tracing");
3540 /* Don't actually handle it here. When we stop tracing we remove
3541 breakpoints from the inferior, and that is not allowed in a
3542 breakpoint handler (as the caller is walking the breakpoint
3548 flush_trace_buffer_handler (CORE_ADDR addr
)
3550 trace_debug ("lib hit flush_trace_buffer");
3555 cmd_qtstop (char *packet
)
3562 cmd_qtdisconnected (char *own_buf
)
3565 char *packet
= own_buf
;
3567 packet
+= strlen ("QTDisconnected:");
3569 unpack_varlen_hex (packet
, &setting
);
3573 disconnected_tracing
= setting
;
3577 cmd_qtframe (char *own_buf
)
3579 ULONGEST frame
, pc
, lo
, hi
, num
;
3581 struct traceframe
*tframe
;
3582 char *packet
= own_buf
;
3584 packet
+= strlen ("QTFrame:");
3586 if (startswith (packet
, "pc:"))
3588 packet
+= strlen ("pc:");
3589 unpack_varlen_hex (packet
, &pc
);
3590 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3591 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3593 else if (startswith (packet
, "range:"))
3595 packet
+= strlen ("range:");
3596 packet
= unpack_varlen_hex (packet
, &lo
);
3598 unpack_varlen_hex (packet
, &hi
);
3599 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3600 paddress (lo
), paddress (hi
));
3601 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3603 else if (startswith (packet
, "outside:"))
3605 packet
+= strlen ("outside:");
3606 packet
= unpack_varlen_hex (packet
, &lo
);
3608 unpack_varlen_hex (packet
, &hi
);
3609 trace_debug ("Want to find next traceframe "
3610 "outside the range 0x%s to 0x%s",
3611 paddress (lo
), paddress (hi
));
3612 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3614 else if (startswith (packet
, "tdp:"))
3616 packet
+= strlen ("tdp:");
3617 unpack_varlen_hex (packet
, &num
);
3619 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3620 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3624 unpack_varlen_hex (packet
, &frame
);
3625 tfnum
= (int) frame
;
3628 trace_debug ("Want to stop looking at traceframes");
3629 current_traceframe
= -1;
3633 trace_debug ("Want to look at traceframe %d", tfnum
);
3634 tframe
= find_traceframe (tfnum
);
3639 current_traceframe
= tfnum
;
3640 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3643 sprintf (own_buf
, "F-1");
3647 cmd_qtstatus (char *packet
)
3649 char *stop_reason_rsp
= NULL
;
3650 char *buf1
, *buf2
, *buf3
;
3654 /* Translate the plain text of the notes back into hex for
3657 str
= (tracing_user_name
? tracing_user_name
: "");
3658 slen
= strlen (str
);
3659 buf1
= (char *) alloca (slen
* 2 + 1);
3660 bin2hex ((gdb_byte
*) str
, buf1
, slen
);
3662 str
= (tracing_notes
? tracing_notes
: "");
3663 slen
= strlen (str
);
3664 buf2
= (char *) alloca (slen
* 2 + 1);
3665 bin2hex ((gdb_byte
*) str
, buf2
, slen
);
3667 str
= (tracing_stop_note
? tracing_stop_note
: "");
3668 slen
= strlen (str
);
3669 buf3
= (char *) alloca (slen
* 2 + 1);
3670 bin2hex ((gdb_byte
*) str
, buf3
, slen
);
3672 trace_debug ("Returning trace status as %d, stop reason %s",
3673 tracing
, tracing_stop_reason
);
3675 if (agent_loaded_p ())
3679 upload_fast_traceframes ();
3684 stop_reason_rsp
= (char *) tracing_stop_reason
;
3686 /* The user visible error string in terror needs to be hex encoded.
3687 We leave it as plain string in `tracing_stop_reason' to ease
3689 if (startswith (stop_reason_rsp
, "terror:"))
3691 const char *result_name
;
3695 result_name
= stop_reason_rsp
+ strlen ("terror:");
3696 hexstr_len
= strlen (result_name
) * 2;
3698 = (char *) alloca (strlen ("terror:") + hexstr_len
+ 1);
3699 strcpy (p
, "terror:");
3701 bin2hex ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3704 /* If this was a forced stop, include any stop note that was supplied. */
3705 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3707 stop_reason_rsp
= (char *) alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3708 strcpy (stop_reason_rsp
, "tstop:");
3709 strcat (stop_reason_rsp
, buf3
);
3715 "tframes:%x;tcreated:%x;"
3716 "tfree:%x;tsize:%s;"
3719 "starttime:%s;stoptime:%s;"
3720 "username:%s;notes:%s:",
3722 stop_reason_rsp
, tracing_stop_tpnum
,
3723 traceframe_count
, traceframes_created
,
3724 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3725 circular_trace_buffer
,
3726 disconnected_tracing
,
3727 phex_nz (tracing_start_time
, sizeof (tracing_start_time
)),
3728 phex_nz (tracing_stop_time
, sizeof (tracing_stop_time
)),
3733 cmd_qtp (char *own_buf
)
3736 struct tracepoint
*tpoint
;
3737 char *packet
= own_buf
;
3739 packet
+= strlen ("qTP:");
3741 packet
= unpack_varlen_hex (packet
, &num
);
3742 ++packet
; /* skip a colon */
3743 packet
= unpack_varlen_hex (packet
, &addr
);
3745 /* See if we already have this tracepoint. */
3746 tpoint
= find_tracepoint (num
, addr
);
3750 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3751 (int) num
, paddress (addr
));
3752 write_enn (own_buf
);
3756 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3757 tpoint
->traceframe_usage
);
3760 /* State variables to help return all the tracepoint bits. */
3761 static struct tracepoint
*cur_tpoint
;
3762 static unsigned int cur_action
;
3763 static unsigned int cur_step_action
;
3764 static struct source_string
*cur_source_string
;
3765 static struct trace_state_variable
*cur_tsv
;
3767 /* Compose a response that is an imitation of the syntax by which the
3768 tracepoint was originally downloaded. */
3771 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3775 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3776 paddress (tpoint
->address
),
3777 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3778 tpoint
->pass_count
);
3779 if (tpoint
->type
== fast_tracepoint
)
3780 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3781 else if (tpoint
->type
== static_tracepoint
)
3782 sprintf (packet
+ strlen (packet
), ":S");
3786 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3787 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3788 tpoint
->cond
->length
, buf
);
3793 /* Compose a response that is an imitation of the syntax by which the
3794 tracepoint action was originally downloaded (with the difference
3795 that due to the way we store the actions, this will output a packet
3796 per action, while GDB could have combined more than one action
3800 response_action (char *packet
, struct tracepoint
*tpoint
,
3801 char *taction
, int step
)
3803 sprintf (packet
, "%c%x:%s:%s",
3804 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3808 /* Compose a response that is an imitation of the syntax by which the
3809 tracepoint source piece was originally downloaded. */
3812 response_source (char *packet
,
3813 struct tracepoint
*tpoint
, struct source_string
*src
)
3818 len
= strlen (src
->str
);
3819 buf
= (char *) alloca (len
* 2 + 1);
3820 bin2hex ((gdb_byte
*) src
->str
, buf
, len
);
3822 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3823 tpoint
->number
, paddress (tpoint
->address
),
3824 src
->type
, 0, len
, buf
);
3827 /* Return the first piece of tracepoint definition, and initialize the
3828 state machine that will iterate through all the tracepoint
3832 cmd_qtfp (char *packet
)
3834 trace_debug ("Returning first tracepoint definition piece");
3836 cur_tpoint
= tracepoints
;
3837 cur_action
= cur_step_action
= 0;
3838 cur_source_string
= NULL
;
3841 response_tracepoint (packet
, cur_tpoint
);
3843 strcpy (packet
, "l");
3846 /* Return additional pieces of tracepoint definition. Each action and
3847 stepping action must go into its own packet, because of packet size
3848 limits, and so we use state variables to deliver one piece at a
3852 cmd_qtsp (char *packet
)
3854 trace_debug ("Returning subsequent tracepoint definition piece");
3858 /* This case would normally never occur, but be prepared for
3860 strcpy (packet
, "l");
3862 else if (cur_action
< cur_tpoint
->numactions
)
3864 response_action (packet
, cur_tpoint
,
3865 cur_tpoint
->actions_str
[cur_action
], 0);
3868 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3870 response_action (packet
, cur_tpoint
,
3871 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3874 else if ((cur_source_string
3875 ? cur_source_string
->next
3876 : cur_tpoint
->source_strings
))
3878 if (cur_source_string
)
3879 cur_source_string
= cur_source_string
->next
;
3881 cur_source_string
= cur_tpoint
->source_strings
;
3882 response_source (packet
, cur_tpoint
, cur_source_string
);
3886 cur_tpoint
= cur_tpoint
->next
;
3887 cur_action
= cur_step_action
= 0;
3888 cur_source_string
= NULL
;
3890 response_tracepoint (packet
, cur_tpoint
);
3892 strcpy (packet
, "l");
3896 /* Compose a response that is an imitation of the syntax by which the
3897 trace state variable was originally downloaded. */
3900 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3902 char *buf
= (char *) "";
3907 namelen
= strlen (tsv
->name
);
3908 buf
= (char *) alloca (namelen
* 2 + 1);
3909 bin2hex ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3912 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3913 tsv
->getter
? 1 : 0, buf
);
3916 /* Return the first trace state variable definition, and initialize
3917 the state machine that will iterate through all the tsv bits. */
3920 cmd_qtfv (char *packet
)
3922 trace_debug ("Returning first trace state variable definition");
3924 cur_tsv
= trace_state_variables
;
3927 response_tsv (packet
, cur_tsv
);
3929 strcpy (packet
, "l");
3932 /* Return additional trace state variable definitions. */
3935 cmd_qtsv (char *packet
)
3937 trace_debug ("Returning additional trace state variable definition");
3941 cur_tsv
= cur_tsv
->next
;
3943 response_tsv (packet
, cur_tsv
);
3945 strcpy (packet
, "l");
3948 strcpy (packet
, "l");
3951 /* Return the first static tracepoint marker, and initialize the state
3952 machine that will iterate through all the static tracepoints
3956 cmd_qtfstm (char *packet
)
3958 if (!maybe_write_ipa_ust_not_loaded (packet
))
3959 run_inferior_command (packet
, strlen (packet
) + 1);
3962 /* Return additional static tracepoints markers. */
3965 cmd_qtsstm (char *packet
)
3967 if (!maybe_write_ipa_ust_not_loaded (packet
))
3968 run_inferior_command (packet
, strlen (packet
) + 1);
3971 /* Return the definition of the static tracepoint at a given address.
3972 Result packet is the same as qTsST's. */
3975 cmd_qtstmat (char *packet
)
3977 if (!maybe_write_ipa_ust_not_loaded (packet
))
3978 run_inferior_command (packet
, strlen (packet
) + 1);
3981 /* Helper for gdb_agent_about_to_close.
3982 Return non-zero if thread ENTRY is in the same process in DATA. */
3985 same_process_p (struct inferior_list_entry
*entry
, void *data
)
3987 int *pid
= (int *) data
;
3989 return ptid_get_pid (entry
->id
) == *pid
;
3992 /* Sent the agent a command to close it. */
3995 gdb_agent_about_to_close (int pid
)
3997 char buf
[IPA_CMD_BUF_SIZE
];
3999 if (!maybe_write_ipa_not_loaded (buf
))
4001 struct thread_info
*saved_thread
;
4003 saved_thread
= current_thread
;
4005 /* Find any thread which belongs to process PID. */
4006 current_thread
= (struct thread_info
*)
4007 find_inferior (&all_threads
, same_process_p
, &pid
);
4009 strcpy (buf
, "close");
4011 run_inferior_command (buf
, strlen (buf
) + 1);
4013 current_thread
= saved_thread
;
4017 /* Return the minimum instruction size needed for fast tracepoints as a
4018 hexadecimal number. */
4021 cmd_qtminftpilen (char *packet
)
4023 if (current_thread
== NULL
)
4025 /* Indicate that the minimum length is currently unknown. */
4026 strcpy (packet
, "0");
4030 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
4033 /* Respond to qTBuffer packet with a block of raw data from the trace
4034 buffer. GDB may ask for a lot, but we are allowed to reply with
4035 only as much as will fit within packet limits or whatever. */
4038 cmd_qtbuffer (char *own_buf
)
4040 ULONGEST offset
, num
, tot
;
4042 char *packet
= own_buf
;
4044 packet
+= strlen ("qTBuffer:");
4046 packet
= unpack_varlen_hex (packet
, &offset
);
4047 ++packet
; /* skip a comma */
4048 unpack_varlen_hex (packet
, &num
);
4050 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
4051 (int) num
, phex_nz (offset
, 0));
4053 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
4055 /* If we're right at the end, reply specially that we're done. */
4058 strcpy (own_buf
, "l");
4062 /* Object to any other out-of-bounds request. */
4065 write_enn (own_buf
);
4069 /* Compute the pointer corresponding to the given offset, accounting
4071 tbp
= trace_buffer_start
+ offset
;
4072 if (tbp
>= trace_buffer_wrap
)
4073 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4075 /* Trim to the remaining bytes if we're close to the end. */
4076 if (num
> tot
- offset
)
4079 /* Trim to available packet size. */
4080 if (num
>= (PBUFSIZ
- 16) / 2 )
4081 num
= (PBUFSIZ
- 16) / 2;
4083 bin2hex (tbp
, own_buf
, num
);
4087 cmd_bigqtbuffer_circular (char *own_buf
)
4090 char *packet
= own_buf
;
4092 packet
+= strlen ("QTBuffer:circular:");
4094 unpack_varlen_hex (packet
, &val
);
4095 circular_trace_buffer
= val
;
4096 trace_debug ("Trace buffer is now %s",
4097 circular_trace_buffer
? "circular" : "linear");
4102 cmd_bigqtbuffer_size (char *own_buf
)
4106 char *packet
= own_buf
;
4108 /* Can't change the size during a tracing run. */
4111 write_enn (own_buf
);
4115 packet
+= strlen ("QTBuffer:size:");
4117 /* -1 is sent as literal "-1". */
4118 if (strcmp (packet
, "-1") == 0)
4119 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4122 unpack_varlen_hex (packet
, &val
);
4123 sval
= (LONGEST
) val
;
4126 init_trace_buffer (sval
);
4127 trace_debug ("Trace buffer is now %s bytes",
4128 plongest (trace_buffer_size
));
4133 cmd_qtnotes (char *own_buf
)
4136 char *saved
, *user
, *notes
, *stopnote
;
4137 char *packet
= own_buf
;
4139 packet
+= strlen ("QTNotes:");
4143 if (startswith (packet
, "user:"))
4145 packet
+= strlen ("user:");
4147 packet
= strchr (packet
, ';');
4148 nbytes
= (packet
- saved
) / 2;
4149 user
= (char *) xmalloc (nbytes
+ 1);
4150 nbytes
= hex2bin (saved
, (gdb_byte
*) user
, nbytes
);
4151 user
[nbytes
] = '\0';
4152 ++packet
; /* skip the semicolon */
4153 trace_debug ("User is '%s'", user
);
4154 xfree (tracing_user_name
);
4155 tracing_user_name
= user
;
4157 else if (startswith (packet
, "notes:"))
4159 packet
+= strlen ("notes:");
4161 packet
= strchr (packet
, ';');
4162 nbytes
= (packet
- saved
) / 2;
4163 notes
= (char *) xmalloc (nbytes
+ 1);
4164 nbytes
= hex2bin (saved
, (gdb_byte
*) notes
, nbytes
);
4165 notes
[nbytes
] = '\0';
4166 ++packet
; /* skip the semicolon */
4167 trace_debug ("Notes is '%s'", notes
);
4168 xfree (tracing_notes
);
4169 tracing_notes
= notes
;
4171 else if (startswith (packet
, "tstop:"))
4173 packet
+= strlen ("tstop:");
4175 packet
= strchr (packet
, ';');
4176 nbytes
= (packet
- saved
) / 2;
4177 stopnote
= (char *) xmalloc (nbytes
+ 1);
4178 nbytes
= hex2bin (saved
, (gdb_byte
*) stopnote
, nbytes
);
4179 stopnote
[nbytes
] = '\0';
4180 ++packet
; /* skip the semicolon */
4181 trace_debug ("tstop note is '%s'", stopnote
);
4182 xfree (tracing_stop_note
);
4183 tracing_stop_note
= stopnote
;
4193 handle_tracepoint_general_set (char *packet
)
4195 if (strcmp ("QTinit", packet
) == 0)
4197 cmd_qtinit (packet
);
4200 else if (startswith (packet
, "QTDP:"))
4205 else if (startswith (packet
, "QTDPsrc:"))
4207 cmd_qtdpsrc (packet
);
4210 else if (startswith (packet
, "QTEnable:"))
4212 cmd_qtenable_disable (packet
, 1);
4215 else if (startswith (packet
, "QTDisable:"))
4217 cmd_qtenable_disable (packet
, 0);
4220 else if (startswith (packet
, "QTDV:"))
4225 else if (startswith (packet
, "QTro:"))
4230 else if (strcmp ("QTStart", packet
) == 0)
4232 cmd_qtstart (packet
);
4235 else if (strcmp ("QTStop", packet
) == 0)
4237 cmd_qtstop (packet
);
4240 else if (startswith (packet
, "QTDisconnected:"))
4242 cmd_qtdisconnected (packet
);
4245 else if (startswith (packet
, "QTFrame:"))
4247 cmd_qtframe (packet
);
4250 else if (startswith (packet
, "QTBuffer:circular:"))
4252 cmd_bigqtbuffer_circular (packet
);
4255 else if (startswith (packet
, "QTBuffer:size:"))
4257 cmd_bigqtbuffer_size (packet
);
4260 else if (startswith (packet
, "QTNotes:"))
4262 cmd_qtnotes (packet
);
4270 handle_tracepoint_query (char *packet
)
4272 if (strcmp ("qTStatus", packet
) == 0)
4274 cmd_qtstatus (packet
);
4277 else if (startswith (packet
, "qTP:"))
4282 else if (strcmp ("qTfP", packet
) == 0)
4287 else if (strcmp ("qTsP", packet
) == 0)
4292 else if (strcmp ("qTfV", packet
) == 0)
4297 else if (strcmp ("qTsV", packet
) == 0)
4302 else if (startswith (packet
, "qTV:"))
4307 else if (startswith (packet
, "qTBuffer:"))
4309 cmd_qtbuffer (packet
);
4312 else if (strcmp ("qTfSTM", packet
) == 0)
4314 cmd_qtfstm (packet
);
4317 else if (strcmp ("qTsSTM", packet
) == 0)
4319 cmd_qtsstm (packet
);
4322 else if (startswith (packet
, "qTSTMat:"))
4324 cmd_qtstmat (packet
);
4327 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4329 cmd_qtminftpilen (packet
);
4337 #ifndef IN_PROCESS_AGENT
4339 /* Call this when thread TINFO has hit the tracepoint defined by
4340 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4341 action. This adds a while-stepping collecting state item to the
4342 threads' collecting state list, so that we can keep track of
4343 multiple simultaneous while-stepping actions being collected by the
4344 same thread. This can happen in cases like:
4346 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4348 ff0003 INSN3 <-- TP2, collect $regs
4349 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4352 Notice that when instruction INSN5 is reached, the while-stepping
4353 actions of both TP1 and TP3 are still being collected, and that TP2
4354 had been collected meanwhile. The whole range of ff0001-ff0005
4355 should be single-stepped, due to at least TP1's while-stepping
4356 action covering the whole range. */
4359 add_while_stepping_state (struct thread_info
*tinfo
,
4360 int tp_number
, CORE_ADDR tp_address
)
4362 struct wstep_state
*wstep
= XNEW (struct wstep_state
);
4364 wstep
->next
= tinfo
->while_stepping
;
4366 wstep
->tp_number
= tp_number
;
4367 wstep
->tp_address
= tp_address
;
4368 wstep
->current_step
= 0;
4370 tinfo
->while_stepping
= wstep
;
4373 /* Release the while-stepping collecting state WSTEP. */
4376 release_while_stepping_state (struct wstep_state
*wstep
)
4381 /* Release all while-stepping collecting states currently associated
4382 with thread TINFO. */
4385 release_while_stepping_state_list (struct thread_info
*tinfo
)
4387 struct wstep_state
*head
;
4389 while (tinfo
->while_stepping
)
4391 head
= tinfo
->while_stepping
;
4392 tinfo
->while_stepping
= head
->next
;
4393 release_while_stepping_state (head
);
4397 /* If TINFO was handling a 'while-stepping' action, the step has
4398 finished, so collect any step data needed, and check if any more
4399 steps are required. Return true if the thread was indeed
4400 collecting tracepoint data, false otherwise. */
4403 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4405 struct tracepoint
*tpoint
;
4406 struct wstep_state
*wstep
;
4407 struct wstep_state
**wstep_link
;
4408 struct trap_tracepoint_ctx ctx
;
4410 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4412 if (agent_loaded_p ())
4413 upload_fast_traceframes ();
4415 /* Check if we were indeed collecting data for one of more
4416 tracepoints with a 'while-stepping' count. */
4417 if (tinfo
->while_stepping
== NULL
)
4422 /* We're not even tracing anymore. Stop this thread from
4424 release_while_stepping_state_list (tinfo
);
4426 /* The thread had stopped due to a single-step request indeed
4427 explained by a tracepoint. */
4431 wstep
= tinfo
->while_stepping
;
4432 wstep_link
= &tinfo
->while_stepping
;
4434 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4435 target_pid_to_str (tinfo
->entry
.id
),
4436 wstep
->tp_number
, paddress (wstep
->tp_address
));
4438 ctx
.base
.type
= trap_tracepoint
;
4439 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4441 while (wstep
!= NULL
)
4443 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4446 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4447 wstep
->tp_number
, paddress (wstep
->tp_address
),
4448 target_pid_to_str (tinfo
->entry
.id
));
4451 *wstep_link
= wstep
->next
;
4452 release_while_stepping_state (wstep
);
4453 wstep
= *wstep_link
;
4457 /* We've just finished one step. */
4458 ++wstep
->current_step
;
4461 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4462 stop_pc
, tpoint
, wstep
->current_step
);
4464 if (wstep
->current_step
>= tpoint
->step_count
)
4466 /* The requested numbers of steps have occurred. */
4467 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4468 target_pid_to_str (tinfo
->entry
.id
),
4469 wstep
->tp_number
, paddress (wstep
->tp_address
));
4471 /* Unlink the wstep. */
4472 *wstep_link
= wstep
->next
;
4473 release_while_stepping_state (wstep
);
4474 wstep
= *wstep_link
;
4476 /* Only check the hit count now, which ensure that we do all
4477 our stepping before stopping the run. */
4478 if (tpoint
->pass_count
> 0
4479 && tpoint
->hit_count
>= tpoint
->pass_count
4480 && stopping_tracepoint
== NULL
)
4481 stopping_tracepoint
= tpoint
;
4485 /* Keep single-stepping until the requested numbers of steps
4487 wstep_link
= &wstep
->next
;
4488 wstep
= *wstep_link
;
4491 if (stopping_tracepoint
4492 || trace_buffer_is_full
4493 || expr_eval_result
!= expr_eval_no_error
)
4503 /* Handle any internal tracing control breakpoint hits. That means,
4504 pull traceframes from the IPA to our buffer, and syncing both
4505 tracing agents when the IPA's tracing stops for some reason. */
4508 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4510 /* Pull in fast tracepoint trace frames from the inferior in-process
4511 agent's buffer into our buffer. */
4513 if (!agent_loaded_p ())
4516 upload_fast_traceframes ();
4518 /* Check if the in-process agent had decided we should stop
4520 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4522 int ipa_trace_buffer_is_full
;
4523 CORE_ADDR ipa_stopping_tracepoint
;
4524 int ipa_expr_eval_result
;
4525 CORE_ADDR ipa_error_tracepoint
;
4527 trace_debug ("lib stopped at stop_tracing");
4529 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4530 &ipa_trace_buffer_is_full
);
4532 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4533 &ipa_stopping_tracepoint
);
4534 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4536 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4537 &ipa_error_tracepoint
);
4538 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4540 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4541 &ipa_expr_eval_result
);
4542 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4544 trace_debug ("lib: trace_buffer_is_full: %d, "
4545 "stopping_tracepoint: %s, "
4546 "ipa_expr_eval_result: %d, "
4547 "error_tracepoint: %s, ",
4548 ipa_trace_buffer_is_full
,
4549 paddress (ipa_stopping_tracepoint
),
4550 ipa_expr_eval_result
,
4551 paddress (ipa_error_tracepoint
));
4555 if (ipa_trace_buffer_is_full
)
4556 trace_debug ("lib stopped due to full buffer.");
4557 if (ipa_stopping_tracepoint
)
4558 trace_debug ("lib stopped due to tpoint");
4559 if (ipa_stopping_tracepoint
)
4560 trace_debug ("lib stopped due to error");
4563 if (ipa_stopping_tracepoint
!= 0)
4566 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4568 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4570 expr_eval_result
= ipa_expr_eval_result
;
4572 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4577 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4579 trace_debug ("lib stopped at flush_trace_buffer");
4586 /* Return true if TINFO just hit a tracepoint. Collect data if
4590 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4592 struct tracepoint
*tpoint
;
4594 struct trap_tracepoint_ctx ctx
;
4596 /* Not tracing, don't handle. */
4600 ctx
.base
.type
= trap_tracepoint
;
4601 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4603 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4605 /* Note that we collect fast tracepoints here as well. We'll
4606 step over the fast tracepoint jump later, which avoids the
4607 double collect. However, we don't collect for static
4608 tracepoints here, because UST markers are compiled in program,
4609 and probes will be executed in program. So static tracepoints
4610 are collected there. */
4611 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4612 && tpoint
->type
!= static_tracepoint
)
4614 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4615 target_pid_to_str (tinfo
->entry
.id
),
4616 tpoint
->number
, paddress (tpoint
->address
));
4618 /* Test the condition if present, and collect if true. */
4620 || (condition_true_at_tracepoint
4621 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4622 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4625 if (stopping_tracepoint
4626 || trace_buffer_is_full
4627 || expr_eval_result
!= expr_eval_no_error
)
4631 /* If the tracepoint had a 'while-stepping' action, then set
4632 the thread to collect this tracepoint on the following
4634 else if (tpoint
->step_count
> 0)
4636 add_while_stepping_state (tinfo
,
4637 tpoint
->number
, tpoint
->address
);
4649 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4650 struct ust_marker_data
;
4651 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4652 struct traceframe
*tframe
);
4655 /* Create a trace frame for the hit of the given tracepoint in the
4659 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4660 struct tracepoint
*tpoint
)
4662 struct traceframe
*tframe
;
4665 /* Only count it as a hit when we actually collect data. */
4666 tpoint
->hit_count
++;
4668 /* If we've exceeded a defined pass count, record the event for
4669 later, and finish the collection for this hit. This test is only
4670 for nonstepping tracepoints, stepping tracepoints test at the end
4671 of their while-stepping loop. */
4672 if (tpoint
->pass_count
> 0
4673 && tpoint
->hit_count
>= tpoint
->pass_count
4674 && tpoint
->step_count
== 0
4675 && stopping_tracepoint
== NULL
)
4676 stopping_tracepoint
= tpoint
;
4678 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4679 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4681 tframe
= add_traceframe (tpoint
);
4685 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4687 #ifndef IN_PROCESS_AGENT
4688 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4689 tpoint
->number
, paddress (tpoint
->address
),
4690 tpoint
->actions_str
[acti
]);
4693 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4694 tpoint
->actions
[acti
]);
4697 finish_traceframe (tframe
);
4700 if (tframe
== NULL
&& tracing
)
4701 trace_buffer_is_full
= 1;
4704 #ifndef IN_PROCESS_AGENT
4707 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4709 struct tracepoint
*tpoint
, int current_step
)
4711 struct traceframe
*tframe
;
4714 trace_debug ("Making new step traceframe for "
4715 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4716 tpoint
->number
, paddress (tpoint
->address
),
4717 current_step
, tpoint
->step_count
,
4720 tframe
= add_traceframe (tpoint
);
4724 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4726 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4727 tpoint
->number
, paddress (tpoint
->address
),
4728 tpoint
->step_actions_str
[acti
]);
4730 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4731 tpoint
->step_actions
[acti
]);
4734 finish_traceframe (tframe
);
4737 if (tframe
== NULL
&& tracing
)
4738 trace_buffer_is_full
= 1;
4743 #ifdef IN_PROCESS_AGENT
4744 /* The target description index for IPA. Passed from gdbserver, used
4745 to select ipa_tdesc. */
4747 IP_AGENT_EXPORT_VAR
int ipa_tdesc_idx
;
4751 static struct regcache
*
4752 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4754 struct regcache
*regcache
= NULL
;
4755 #ifdef IN_PROCESS_AGENT
4756 const struct target_desc
*ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
4758 if (ctx
->type
== fast_tracepoint
)
4760 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4761 if (!fctx
->regcache_initted
)
4763 fctx
->regcache_initted
= 1;
4764 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4765 supply_regblock (&fctx
->regcache
, NULL
);
4766 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4768 regcache
= &fctx
->regcache
;
4771 if (ctx
->type
== static_tracepoint
)
4773 struct static_tracepoint_ctx
*sctx
4774 = (struct static_tracepoint_ctx
*) ctx
;
4776 if (!sctx
->regcache_initted
)
4778 sctx
->regcache_initted
= 1;
4779 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4780 supply_regblock (&sctx
->regcache
, NULL
);
4781 /* Pass down the tracepoint address, because REGS doesn't
4782 include the PC, but we know what it must have been. */
4783 supply_static_tracepoint_registers (&sctx
->regcache
,
4784 (const unsigned char *)
4786 sctx
->tpoint
->address
);
4788 regcache
= &sctx
->regcache
;
4792 if (ctx
->type
== trap_tracepoint
)
4794 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4795 regcache
= tctx
->regcache
;
4799 gdb_assert (regcache
!= NULL
);
4805 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4807 struct tracepoint
*tpoint
,
4808 struct traceframe
*tframe
,
4809 struct tracepoint_action
*taction
)
4811 enum eval_result_type err
;
4813 switch (taction
->type
)
4817 struct collect_memory_action
*maction
;
4818 struct eval_agent_expr_context ax_ctx
;
4820 maction
= (struct collect_memory_action
*) taction
;
4821 ax_ctx
.regcache
= NULL
;
4822 ax_ctx
.tframe
= tframe
;
4823 ax_ctx
.tpoint
= tpoint
;
4825 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4826 pulongest (maction
->len
),
4827 paddress (maction
->addr
), maction
->basereg
);
4828 /* (should use basereg) */
4829 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4835 unsigned char *regspace
;
4836 struct regcache tregcache
;
4837 struct regcache
*context_regcache
;
4840 trace_debug ("Want to collect registers");
4842 context_regcache
= get_context_regcache (ctx
);
4843 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4845 /* Collect all registers for now. */
4846 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4847 if (regspace
== NULL
)
4849 trace_debug ("Trace buffer block allocation failed, skipping");
4852 /* Identify a register block. */
4855 /* Wrap the regblock in a register cache (in the stack, we
4856 don't want to malloc here). */
4857 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4860 /* Copy the register data to the regblock. */
4861 regcache_cpy (&tregcache
, context_regcache
);
4863 #ifndef IN_PROCESS_AGENT
4864 /* On some platforms, trap-based tracepoints will have the PC
4865 pointing to the next instruction after the trap, but we
4866 don't want the user or GDB trying to guess whether the
4867 saved PC needs adjusting; so always record the adjusted
4868 stop_pc. Note that we can't use tpoint->address instead,
4869 since it will be wrong for while-stepping actions. This
4870 adjustment is a nop for fast tracepoints collected from the
4871 in-process lib (but not if GDBserver is collecting one
4872 preemptively), since the PC had already been adjusted to
4873 contain the tracepoint's address by the jump pad. */
4874 trace_debug ("Storing stop pc (0x%s) in regblock",
4875 paddress (stop_pc
));
4877 /* This changes the regblock, not the thread's
4879 regcache_write_pc (&tregcache
, stop_pc
);
4885 struct eval_expr_action
*eaction
;
4886 struct eval_agent_expr_context ax_ctx
;
4888 eaction
= (struct eval_expr_action
*) taction
;
4889 ax_ctx
.regcache
= get_context_regcache (ctx
);
4890 ax_ctx
.tframe
= tframe
;
4891 ax_ctx
.tpoint
= tpoint
;
4893 trace_debug ("Want to evaluate expression");
4895 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4897 if (err
!= expr_eval_no_error
)
4899 record_tracepoint_error (tpoint
, "action expression", err
);
4906 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4907 trace_debug ("Want to collect static trace data");
4908 collect_ust_data_at_tracepoint (ctx
, tframe
);
4910 trace_debug ("warning: collecting static trace data, "
4911 "but static tracepoints are not supported");
4916 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4922 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4923 struct tracepoint
*tpoint
)
4926 enum eval_result_type err
;
4928 /* Presently, gdbserver doesn't run compiled conditions, only the
4929 IPA does. If the program stops at a fast tracepoint's address
4930 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4931 gdbserver preemptively collect the fast tracepoint. Later, on
4932 resume, gdbserver steps over the fast tracepoint like it steps
4933 over breakpoints, so that the IPA doesn't see that fast
4934 tracepoint. This avoids double collects of fast tracepoints in
4935 that stopping scenario. Having gdbserver itself handle the fast
4936 tracepoint gives the user a consistent view of when fast or trap
4937 tracepoints are collected, compared to an alternative where only
4938 trap tracepoints are collected on stop, and fast tracepoints on
4939 resume. When a fast tracepoint is being processed by gdbserver,
4940 it is always the non-compiled condition expression that is
4942 #ifdef IN_PROCESS_AGENT
4943 if (tpoint
->compiled_cond
)
4945 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4946 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (fctx
->regs
, &value
);
4951 struct eval_agent_expr_context ax_ctx
;
4953 ax_ctx
.regcache
= get_context_regcache (ctx
);
4954 ax_ctx
.tframe
= NULL
;
4955 ax_ctx
.tpoint
= tpoint
;
4957 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4959 if (err
!= expr_eval_no_error
)
4961 record_tracepoint_error (tpoint
, "condition", err
);
4962 /* The error case must return false. */
4966 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4967 tpoint
->number
, paddress (tpoint
->address
),
4969 return (value
? 1 : 0);
4972 /* Do memory copies for bytecodes. */
4973 /* Do the recording of memory blocks for actions and bytecodes. */
4976 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4977 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4979 unsigned char *mspace
;
4980 ULONGEST remaining
= len
;
4981 unsigned short blocklen
;
4983 /* If a 'to' buffer is specified, use it. */
4986 read_inferior_memory (from
, to
, len
);
4990 /* Otherwise, create a new memory block in the trace buffer. */
4991 while (remaining
> 0)
4995 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4996 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4997 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5000 /* Identify block as a memory block. */
5003 /* Record address and size. */
5004 memcpy (mspace
, &from
, sizeof (from
));
5005 mspace
+= sizeof (from
);
5006 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
5007 mspace
+= sizeof (blocklen
);
5008 /* Record the memory block proper. */
5009 read_inferior_memory (from
, mspace
, blocklen
);
5010 trace_debug ("%d bytes recorded", blocklen
);
5011 remaining
-= blocklen
;
5018 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
5019 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
5021 unsigned char *buf
, *mspace
;
5022 ULONGEST remaining
= len
;
5023 unsigned short blocklen
, i
;
5025 /* To save a bit of space, block lengths are 16-bit, so break large
5026 requests into multiple blocks. Bordering on overkill for strings,
5027 but it could happen that someone specifies a large max length. */
5028 while (remaining
> 0)
5032 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
5033 /* We want working space to accumulate nonzero bytes, since
5034 traceframes must have a predecided size (otherwise it gets
5035 harder to wrap correctly for the circular case, etc). */
5036 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
5037 for (i
= 0; i
< blocklen
; ++i
)
5039 /* Read the string one byte at a time, in case the string is
5040 at the end of a valid memory area - we don't want a
5041 correctly-terminated string to engender segvio
5043 read_inferior_memory (from
+ i
, buf
+ i
, 1);
5048 /* Make sure outer loop stops now too. */
5049 remaining
= blocklen
;
5053 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
5054 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5060 /* Identify block as a memory block. */
5063 /* Record address and size. */
5064 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5065 mspace
+= sizeof (from
);
5066 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5067 mspace
+= sizeof (blocklen
);
5068 /* Copy the string contents. */
5069 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5070 remaining
-= blocklen
;
5077 /* Record the value of a trace state variable. */
5080 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5082 unsigned char *vspace
;
5085 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5086 1 + sizeof (n
) + sizeof (LONGEST
));
5089 /* Identify block as a variable. */
5091 /* Record variable's number and value. */
5092 memcpy (vspace
+ 1, &n
, sizeof (n
));
5093 val
= get_trace_state_variable_value (n
);
5094 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5095 trace_debug ("Variable %d recorded", n
);
5099 #ifndef IN_PROCESS_AGENT
5101 /* Callback for traceframe_walk_blocks, used to find a given block
5102 type in a traceframe. */
5105 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5107 char *wantedp
= (char *) data
;
5109 if (*wantedp
== blocktype
)
5115 /* Walk over all traceframe blocks of the traceframe buffer starting
5116 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5117 block found, passing in DATA unmodified. If CALLBACK returns true,
5118 this returns a pointer to where the block is found. Returns NULL
5119 if no callback call returned true, indicating that all blocks have
5122 static unsigned char *
5123 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5125 int (*callback
) (char blocktype
,
5126 unsigned char *dataptr
,
5130 unsigned char *dataptr
;
5134 trace_debug ("traceframe %d has no data", tfnum
);
5138 /* Iterate through a traceframe's blocks, looking for a block of the
5140 for (dataptr
= database
;
5141 dataptr
< database
+ datasize
;
5145 unsigned short mlen
;
5147 if (dataptr
== trace_buffer_wrap
)
5149 /* Adjust to reflect wrapping part of the frame around to
5151 datasize
= dataptr
- database
;
5152 dataptr
= database
= trace_buffer_lo
;
5155 blocktype
= *dataptr
++;
5157 if ((*callback
) (blocktype
, dataptr
, data
))
5163 /* Skip over the registers block. */
5164 dataptr
+= current_target_desc ()->registers_size
;
5167 /* Skip over the memory block. */
5168 dataptr
+= sizeof (CORE_ADDR
);
5169 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5170 dataptr
+= (sizeof (mlen
) + mlen
);
5173 /* Skip over the TSV block. */
5174 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5177 /* Skip over the static trace data block. */
5178 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5179 dataptr
+= (sizeof (mlen
) + mlen
);
5182 trace_debug ("traceframe %d has unknown block type 0x%x",
5191 /* Look for the block of type TYPE_WANTED in the trameframe starting
5192 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5195 static unsigned char *
5196 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5197 int tfnum
, char type_wanted
)
5199 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5200 match_blocktype
, &type_wanted
);
5203 static unsigned char *
5204 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5206 unsigned char *regblock
;
5208 regblock
= traceframe_find_block_type (tframe
->data
,
5212 if (regblock
== NULL
)
5213 trace_debug ("traceframe %d has no register data", tfnum
);
5218 /* Get registers from a traceframe. */
5221 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5223 unsigned char *dataptr
;
5224 struct tracepoint
*tpoint
;
5225 struct traceframe
*tframe
;
5227 tframe
= find_traceframe (tfnum
);
5231 trace_debug ("traceframe %d not found", tfnum
);
5235 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5236 if (dataptr
== NULL
)
5238 /* Mark registers unavailable. */
5239 supply_regblock (regcache
, NULL
);
5241 /* We can generally guess at a PC, although this will be
5242 misleading for while-stepping frames and multi-location
5244 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5246 regcache_write_pc (regcache
, tpoint
->address
);
5249 supply_regblock (regcache
, dataptr
);
5255 traceframe_get_pc (struct traceframe
*tframe
)
5257 struct regcache regcache
;
5258 unsigned char *dataptr
;
5259 const struct target_desc
*tdesc
= current_target_desc ();
5261 dataptr
= traceframe_find_regblock (tframe
, -1);
5262 if (dataptr
== NULL
)
5265 init_register_cache (®cache
, tdesc
, dataptr
);
5266 return regcache_read_pc (®cache
);
5269 /* Read a requested block of memory from a trace frame. */
5272 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5273 unsigned char *buf
, ULONGEST length
,
5276 struct traceframe
*tframe
;
5277 unsigned char *database
, *dataptr
;
5278 unsigned int datasize
;
5280 unsigned short mlen
;
5282 trace_debug ("traceframe_read_mem");
5284 tframe
= find_traceframe (tfnum
);
5288 trace_debug ("traceframe %d not found", tfnum
);
5292 datasize
= tframe
->data_size
;
5293 database
= dataptr
= &tframe
->data
[0];
5295 /* Iterate through a traceframe's blocks, looking for memory. */
5296 while ((dataptr
= traceframe_find_block_type (dataptr
,
5298 - (dataptr
- database
),
5299 tfnum
, 'M')) != NULL
)
5301 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5302 dataptr
+= sizeof (maddr
);
5303 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5304 dataptr
+= sizeof (mlen
);
5305 trace_debug ("traceframe %d has %d bytes at %s",
5306 tfnum
, mlen
, paddress (maddr
));
5308 /* If the block includes the first part of the desired range,
5309 return as much it has; GDB will re-request the remainder,
5310 which might be in a different block of this trace frame. */
5311 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5313 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5317 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5322 /* Skip over this block. */
5326 trace_debug ("traceframe %d has no memory data for the desired region",
5334 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5337 struct traceframe
*tframe
;
5338 unsigned char *database
, *dataptr
;
5339 unsigned int datasize
;
5343 trace_debug ("traceframe_read_tsv");
5345 tfnum
= current_traceframe
;
5349 trace_debug ("no current traceframe");
5353 tframe
= find_traceframe (tfnum
);
5357 trace_debug ("traceframe %d not found", tfnum
);
5361 datasize
= tframe
->data_size
;
5362 database
= dataptr
= &tframe
->data
[0];
5364 /* Iterate through a traceframe's blocks, looking for the last
5366 while ((dataptr
= traceframe_find_block_type (dataptr
,
5368 - (dataptr
- database
),
5369 tfnum
, 'V')) != NULL
)
5371 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5372 dataptr
+= sizeof (vnum
);
5374 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5376 /* Check that this is the variable we want. */
5379 memcpy (val
, dataptr
, sizeof (*val
));
5383 /* Skip over this block. */
5384 dataptr
+= sizeof (LONGEST
);
5388 trace_debug ("traceframe %d has no data for variable %d",
5393 /* Read a requested block of static tracepoint data from a trace
5397 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5398 unsigned char *buf
, ULONGEST length
,
5401 struct traceframe
*tframe
;
5402 unsigned char *database
, *dataptr
;
5403 unsigned int datasize
;
5404 unsigned short mlen
;
5406 trace_debug ("traceframe_read_sdata");
5408 tframe
= find_traceframe (tfnum
);
5412 trace_debug ("traceframe %d not found", tfnum
);
5416 datasize
= tframe
->data_size
;
5417 database
= &tframe
->data
[0];
5419 /* Iterate through a traceframe's blocks, looking for static
5421 dataptr
= traceframe_find_block_type (database
, datasize
,
5423 if (dataptr
!= NULL
)
5425 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5426 dataptr
+= sizeof (mlen
);
5429 if (offset
+ length
> mlen
)
5430 length
= mlen
- offset
;
5432 memcpy (buf
, dataptr
, length
);
5440 trace_debug ("traceframe %d has no static trace data", tfnum
);
5446 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5447 object. DATA is pointer to a struct buffer holding the
5448 traceframe-info object being built. */
5451 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5453 struct buffer
*buffer
= (struct buffer
*) data
;
5459 unsigned short mlen
;
5462 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5463 dataptr
+= sizeof (maddr
);
5464 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5465 dataptr
+= sizeof (mlen
);
5466 buffer_xml_printf (buffer
,
5467 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5468 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5475 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5476 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5485 warning ("Unhandled trace block type (%d) '%c ' "
5486 "while building trace frame info.",
5487 blocktype
, blocktype
);
5494 /* Build a traceframe-info object for traceframe number TFNUM into
5498 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5500 struct traceframe
*tframe
;
5502 trace_debug ("traceframe_read_info");
5504 tframe
= find_traceframe (tfnum
);
5508 trace_debug ("traceframe %d not found", tfnum
);
5512 buffer_grow_str (buffer
, "<traceframe-info>\n");
5513 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5514 tfnum
, build_traceframe_info_xml
, buffer
);
5515 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5519 /* Return the first fast tracepoint whose jump pad contains PC. */
5521 static struct tracepoint
*
5522 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5524 struct tracepoint
*tpoint
;
5526 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5527 if (tpoint
->type
== fast_tracepoint
)
5528 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5534 /* Return the first fast tracepoint whose trampoline contains PC. */
5536 static struct tracepoint
*
5537 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5539 struct tracepoint
*tpoint
;
5541 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5543 if (tpoint
->type
== fast_tracepoint
5544 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5551 /* Return GDBserver's tracepoint that matches the IP Agent's
5552 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5555 static struct tracepoint
*
5556 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5558 struct tracepoint
*tpoint
;
5560 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5561 if (tpoint
->type
== fast_tracepoint
)
5562 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5570 /* The type of the object that is used to synchronize fast tracepoint
5573 typedef struct collecting_t
5575 /* The fast tracepoint number currently collecting. */
5578 /* A number that GDBserver can use to identify the thread that is
5579 presently holding the collect lock. This need not (and usually
5580 is not) the thread id, as getting the current thread ID usually
5581 requires a system call, which we want to avoid like the plague.
5582 Usually this is thread's TCB, found in the TLS (pseudo-)
5583 register, which is readable with a single insn on several
5585 uintptr_t thread_area
;
5588 #ifndef IN_PROCESS_AGENT
5591 force_unlock_trace_buffer (void)
5593 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5596 /* Check if the thread identified by THREAD_AREA which is stopped at
5597 STOP_PC, is presently locking the fast tracepoint collection, and
5598 if so, gather some status of said collection. Returns 0 if the
5599 thread isn't collecting or in the jump pad at all. 1, if in the
5600 jump pad (or within gdb_collect) and hasn't executed the adjusted
5601 original insn yet (can set a breakpoint there and run to it). 2,
5602 if presently executing the adjusted original insn --- in which
5603 case, if we want to move the thread out of the jump pad, we need to
5604 single-step it until this function returns 0. */
5607 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5609 struct fast_tpoint_collect_status
*status
)
5611 CORE_ADDR ipa_collecting
;
5612 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5613 CORE_ADDR ipa_gdb_trampoline_buffer
;
5614 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5615 struct tracepoint
*tpoint
;
5616 int needs_breakpoint
;
5618 /* The thread THREAD_AREA is either:
5620 0. not collecting at all, not within the jump pad, or within
5621 gdb_collect or one of its callees.
5623 1. in the jump pad and haven't reached gdb_collect
5625 2. within gdb_collect (out of the jump pad) (collect is set)
5627 3. we're in the jump pad, after gdb_collect having returned,
5628 possibly executing the adjusted insns.
5630 For cases 1 and 3, `collecting' may or not be set. The jump pad
5631 doesn't have any complicated jump logic, so we can tell if the
5632 thread is executing the adjust original insn or not by just
5633 matching STOP_PC with known jump pad addresses. If we it isn't
5634 yet executing the original insn, set a breakpoint there, and let
5635 the thread run to it, so to quickly step over a possible (many
5636 insns) gdb_collect call. Otherwise, or when the breakpoint is
5637 hit, only a few (small number of) insns are left to be executed
5638 in the jump pad. Single-step the thread until it leaves the
5643 needs_breakpoint
= 0;
5644 trace_debug ("fast_tracepoint_collecting");
5646 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5647 &ipa_gdb_jump_pad_buffer
))
5649 internal_error (__FILE__
, __LINE__
,
5650 "error extracting `gdb_jump_pad_buffer'");
5652 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5653 &ipa_gdb_jump_pad_buffer_end
))
5655 internal_error (__FILE__
, __LINE__
,
5656 "error extracting `gdb_jump_pad_buffer_end'");
5659 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5660 &ipa_gdb_trampoline_buffer
))
5662 internal_error (__FILE__
, __LINE__
,
5663 "error extracting `gdb_trampoline_buffer'");
5665 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5666 &ipa_gdb_trampoline_buffer_end
))
5668 internal_error (__FILE__
, __LINE__
,
5669 "error extracting `gdb_trampoline_buffer_end'");
5672 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5673 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5675 /* We can tell which tracepoint(s) the thread is collecting by
5676 matching the jump pad address back to the tracepoint. */
5677 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5680 warning ("in jump pad, but no matching tpoint?");
5685 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5687 tpoint
->number
, paddress (tpoint
->address
),
5688 paddress (tpoint
->jump_pad
),
5689 paddress (tpoint
->jump_pad_end
),
5690 paddress (tpoint
->adjusted_insn_addr
),
5691 paddress (tpoint
->adjusted_insn_addr_end
));
5694 /* Definitely in the jump pad. May or may not need
5695 fast-exit-jump-pad breakpoint. */
5696 if (tpoint
->jump_pad
<= stop_pc
5697 && stop_pc
< tpoint
->adjusted_insn_addr
)
5698 needs_breakpoint
= 1;
5700 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5701 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5703 /* We can tell which tracepoint(s) the thread is collecting by
5704 matching the trampoline address back to the tracepoint. */
5705 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5708 warning ("in trampoline, but no matching tpoint?");
5713 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5714 tpoint
->number
, paddress (tpoint
->address
),
5715 paddress (tpoint
->trampoline
),
5716 paddress (tpoint
->trampoline_end
));
5719 /* Have not reached jump pad yet, but treat the trampoline as a
5720 part of the jump pad that is before the adjusted original
5722 needs_breakpoint
= 1;
5726 collecting_t ipa_collecting_obj
;
5728 /* If `collecting' is set/locked, then the THREAD_AREA thread
5729 may or not be the one holding the lock. We have to read the
5730 lock to find out. */
5732 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5735 trace_debug ("fast_tracepoint_collecting:"
5736 " failed reading 'collecting' in the inferior");
5740 if (!ipa_collecting
)
5742 trace_debug ("fast_tracepoint_collecting: not collecting"
5743 " (and nobody is).");
5747 /* Some thread is collecting. Check which. */
5748 if (read_inferior_memory (ipa_collecting
,
5749 (unsigned char *) &ipa_collecting_obj
,
5750 sizeof (ipa_collecting_obj
)) != 0)
5753 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5755 trace_debug ("fast_tracepoint_collecting: not collecting "
5756 "(another thread is)");
5761 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5764 warning ("fast_tracepoint_collecting: collecting, "
5765 "but tpoint %s not found?",
5766 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5770 /* The thread is within `gdb_collect', skip over the rest of
5771 fast tracepoint collection quickly using a breakpoint. */
5772 needs_breakpoint
= 1;
5775 /* The caller wants a bit of status detail. */
5778 status
->tpoint_num
= tpoint
->number
;
5779 status
->tpoint_addr
= tpoint
->address
;
5780 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5781 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5784 if (needs_breakpoint
)
5786 /* Hasn't executed the original instruction yet. Set breakpoint
5787 there, and wait till it's hit, then single-step until exiting
5791 fast_tracepoint_collecting, returning continue-until-break at %s",
5792 paddress (tpoint
->adjusted_insn_addr
));
5794 return 1; /* continue */
5798 /* Just single-step until exiting the jump pad. */
5800 trace_debug ("fast_tracepoint_collecting, returning "
5801 "need-single-step (%s-%s)",
5802 paddress (tpoint
->adjusted_insn_addr
),
5803 paddress (tpoint
->adjusted_insn_addr_end
));
5805 return 2; /* single-step */
5811 #ifdef IN_PROCESS_AGENT
5813 /* The global fast tracepoint collect lock. Points to a collecting_t
5814 object built on the stack by the jump pad, if presently locked;
5815 NULL if it isn't locked. Note that this lock *must* be set while
5816 executing any *function other than the jump pad. See
5817 fast_tracepoint_collecting. */
5819 IP_AGENT_EXPORT_VAR collecting_t
*collecting
;
5822 /* This routine, called from the jump pad (in asm) is designed to be
5823 called from the jump pads of fast tracepoints, thus it is on the
5826 IP_AGENT_EXPORT_FUNC
void
5827 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5829 struct fast_tracepoint_ctx ctx
;
5830 const struct target_desc
*ipa_tdesc
;
5832 /* Don't do anything until the trace run is completely set up. */
5836 ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
5837 ctx
.base
.type
= fast_tracepoint
;
5839 ctx
.regcache_initted
= 0;
5840 /* Wrap the regblock in a register cache (in the stack, we don't
5841 want to malloc here). */
5842 ctx
.regspace
= (unsigned char *) alloca (ipa_tdesc
->registers_size
);
5843 if (ctx
.regspace
== NULL
)
5845 trace_debug ("Trace buffer block allocation failed, skipping");
5849 for (ctx
.tpoint
= tpoint
;
5850 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5851 ctx
.tpoint
= ctx
.tpoint
->next
)
5853 if (!ctx
.tpoint
->enabled
)
5856 /* Multiple tracepoints of different types, such as fast tracepoint and
5857 static tracepoint, can be set at the same address. */
5858 if (ctx
.tpoint
->type
!= tpoint
->type
)
5861 /* Test the condition if present, and collect if true. */
5862 if (ctx
.tpoint
->cond
== NULL
5863 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5866 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5867 ctx
.tpoint
->address
, ctx
.tpoint
);
5869 /* Note that this will cause original insns to be written back
5870 to where we jumped from, but that's OK because we're jumping
5871 back to the next whole instruction. This will go badly if
5872 instruction restoration is not atomic though. */
5873 if (stopping_tracepoint
5874 || trace_buffer_is_full
5875 || expr_eval_result
!= expr_eval_no_error
)
5883 /* If there was a condition and it evaluated to false, the only
5884 way we would stop tracing is if there was an error during
5885 condition expression evaluation. */
5886 if (expr_eval_result
!= expr_eval_no_error
)
5895 /* These global variables points to the corresponding functions. This is
5896 necessary on powerpc64, where asking for function symbol address from gdb
5897 results in returning the actual code pointer, instead of the descriptor
5900 typedef void (*gdb_collect_ptr_type
) (struct tracepoint
*, unsigned char *);
5901 typedef ULONGEST (*get_raw_reg_ptr_type
) (const unsigned char *, int);
5902 typedef LONGEST (*get_trace_state_variable_value_ptr_type
) (int);
5903 typedef void (*set_trace_state_variable_value_ptr_type
) (int, LONGEST
);
5906 IP_AGENT_EXPORT_VAR
const gdb_collect_ptr_type gdb_collect_ptr
= gdb_collect
;
5907 IP_AGENT_EXPORT_VAR
const get_raw_reg_ptr_type get_raw_reg_ptr
= get_raw_reg
;
5908 IP_AGENT_EXPORT_VAR
const get_trace_state_variable_value_ptr_type
5909 get_trace_state_variable_value_ptr
= get_trace_state_variable_value
;
5910 IP_AGENT_EXPORT_VAR
const set_trace_state_variable_value_ptr_type
5911 set_trace_state_variable_value_ptr
= set_trace_state_variable_value
;
5916 #ifndef IN_PROCESS_AGENT
5919 get_raw_reg_func_addr (void)
5922 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_get_raw_reg_ptr
, &res
))
5924 error ("error extracting get_raw_reg_ptr");
5931 get_get_tsv_func_addr (void)
5934 if (read_inferior_data_pointer (
5935 ipa_sym_addrs
.addr_get_trace_state_variable_value_ptr
, &res
))
5937 error ("error extracting get_trace_state_variable_value_ptr");
5944 get_set_tsv_func_addr (void)
5947 if (read_inferior_data_pointer (
5948 ipa_sym_addrs
.addr_set_trace_state_variable_value_ptr
, &res
))
5950 error ("error extracting set_trace_state_variable_value_ptr");
5957 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5958 CORE_ADDR
*jump_entry
)
5960 CORE_ADDR entry_point
= *jump_entry
;
5961 enum eval_result_type err
;
5963 trace_debug ("Starting condition compilation for tracepoint %d\n",
5966 /* Initialize the global pointer to the code being built. */
5967 current_insn_ptr
= *jump_entry
;
5971 err
= compile_bytecodes (tpoint
->cond
);
5973 if (err
== expr_eval_no_error
)
5977 /* Record the beginning of the compiled code. */
5978 tpoint
->compiled_cond
= entry_point
;
5980 trace_debug ("Condition compilation for tracepoint %d complete\n",
5985 /* Leave the unfinished code in situ, but don't point to it. */
5987 tpoint
->compiled_cond
= 0;
5989 trace_debug ("Condition compilation for tracepoint %d failed, "
5991 tpoint
->number
, err
);
5994 /* Update the code pointer passed in. Note that we do this even if
5995 the compile fails, so that we can look at the partial results
5996 instead of letting them be overwritten. */
5997 *jump_entry
= current_insn_ptr
;
5999 /* Leave a gap, to aid dump decipherment. */
6003 /* The base pointer of the IPA's heap. This is the only memory the
6004 IPA is allowed to use. The IPA should _not_ call the inferior's
6005 `malloc' during operation. That'd be slow, and, most importantly,
6006 it may not be safe. We may be collecting a tracepoint in a signal
6007 handler, for example. */
6008 static CORE_ADDR target_tp_heap
;
6010 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6014 target_malloc (ULONGEST size
)
6018 if (target_tp_heap
== 0)
6020 /* We have the pointer *address*, need what it points to. */
6021 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
6024 internal_error (__FILE__
, __LINE__
,
6025 "couldn't get target heap head pointer");
6029 ptr
= target_tp_heap
;
6030 target_tp_heap
+= size
;
6032 /* Pad to 8-byte alignment. */
6033 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
6039 download_agent_expr (struct agent_expr
*expr
)
6041 CORE_ADDR expr_addr
;
6042 CORE_ADDR expr_bytes
;
6044 expr_addr
= target_malloc (sizeof (*expr
));
6045 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
6047 expr_bytes
= target_malloc (expr
->length
);
6048 write_inferior_data_pointer (expr_addr
+ offsetof (struct agent_expr
, bytes
),
6050 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
6055 /* Align V up to N bits. */
6056 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6058 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6061 download_tracepoint_1 (struct tracepoint
*tpoint
)
6063 struct tracepoint target_tracepoint
;
6064 CORE_ADDR tpptr
= 0;
6066 gdb_assert (tpoint
->type
== fast_tracepoint
6067 || tpoint
->type
== static_tracepoint
);
6069 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6071 CORE_ADDR jentry
, jump_entry
;
6073 jentry
= jump_entry
= get_jump_space_head ();
6075 if (tpoint
->cond
!= NULL
)
6077 /* Pad to 8-byte alignment. (needed?) */
6078 /* Actually this should be left for the target to
6080 jentry
= UALIGN (jentry
, 8);
6082 compile_tracepoint_condition (tpoint
, &jentry
);
6085 /* Pad to 8-byte alignment. */
6086 jentry
= UALIGN (jentry
, 8);
6087 claim_jump_space (jentry
- jump_entry
);
6090 target_tracepoint
= *tpoint
;
6092 tpptr
= target_malloc (sizeof (*tpoint
));
6093 tpoint
->obj_addr_on_target
= tpptr
;
6095 /* Write the whole object. We'll fix up its pointers in a bit.
6096 Assume no next for now. This is fixed up above on the next
6097 iteration, if there's any. */
6098 target_tracepoint
.next
= NULL
;
6099 /* Need to clear this here too, since we're downloading the
6100 tracepoints before clearing our own copy. */
6101 target_tracepoint
.hit_count
= 0;
6103 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6104 sizeof (target_tracepoint
));
6107 write_inferior_data_pointer (tpptr
6108 + offsetof (struct tracepoint
, cond
),
6109 download_agent_expr (tpoint
->cond
));
6111 if (tpoint
->numactions
)
6114 CORE_ADDR actions_array
;
6116 /* The pointers array. */
6118 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6119 write_inferior_data_pointer (tpptr
+ offsetof (struct tracepoint
,
6123 /* Now for each pointer, download the action. */
6124 for (i
= 0; i
< tpoint
->numactions
; i
++)
6126 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6127 CORE_ADDR ipa_action
= tracepoint_action_download (action
);
6129 if (ipa_action
!= 0)
6130 write_inferior_data_pointer (actions_array
6131 + i
* sizeof (*tpoint
->actions
),
6137 #define IPA_PROTO_FAST_TRACE_FLAG 0
6138 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6139 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6140 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6141 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6143 /* Send a command to agent to download and install tracepoint TPOINT. */
6146 tracepoint_send_agent (struct tracepoint
*tpoint
)
6148 char buf
[IPA_CMD_BUF_SIZE
];
6153 strcpy (p
, "FastTrace:");
6156 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6157 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6158 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6159 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6160 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6161 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6162 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6163 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6164 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6165 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6166 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6169 p
= agent_expr_send (p
, tpoint
->cond
);
6171 /* tracepoint_action */
6172 for (i
= 0; i
< tpoint
->numactions
; i
++)
6174 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6176 p
[0] = action
->type
;
6177 p
= tracepoint_action_send (&p
[1], action
);
6180 get_jump_space_head ();
6181 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6182 agent can use jump pad from it. */
6183 if (tpoint
->type
== fast_tracepoint
)
6185 memcpy (p
, &gdb_jump_pad_head
, 8);
6189 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6193 if (!startswith (buf
, "OK"))
6196 /* The value of tracepoint's target address is stored in BUF. */
6197 memcpy (&tpoint
->obj_addr_on_target
,
6198 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6200 if (tpoint
->type
== fast_tracepoint
)
6203 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6206 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6207 (unsigned int) tpoint
->obj_addr_on_target
,
6208 (unsigned int) gdb_jump_pad_head
);
6210 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6212 /* This has been done in agent. We should also set up record for it. */
6213 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6216 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6223 download_tracepoint (struct tracepoint
*tpoint
)
6225 struct tracepoint
*tp
, *tp_prev
;
6227 if (tpoint
->type
!= fast_tracepoint
6228 && tpoint
->type
!= static_tracepoint
)
6231 download_tracepoint_1 (tpoint
);
6233 /* Find the previous entry of TPOINT, which is fast tracepoint or
6234 static tracepoint. */
6236 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6238 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6244 CORE_ADDR tp_prev_target_next_addr
;
6246 /* Insert TPOINT after TP_PREV in IPA. */
6247 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6248 + offsetof (struct tracepoint
, next
),
6249 &tp_prev_target_next_addr
))
6251 internal_error (__FILE__
, __LINE__
,
6252 "error reading `tp_prev->next'");
6255 /* tpoint->next = tp_prev->next */
6256 write_inferior_data_pointer (tpoint
->obj_addr_on_target
6257 + offsetof (struct tracepoint
, next
),
6258 tp_prev_target_next_addr
);
6259 /* tp_prev->next = tpoint */
6260 write_inferior_data_pointer (tp_prev
->obj_addr_on_target
6261 + offsetof (struct tracepoint
, next
),
6262 tpoint
->obj_addr_on_target
);
6265 /* First object in list, set the head pointer in the
6267 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
,
6268 tpoint
->obj_addr_on_target
);
6273 download_trace_state_variables (void)
6275 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6276 struct trace_state_variable
*tsv
;
6278 /* Start out empty. */
6279 write_inferior_data_pointer (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6281 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6283 struct trace_state_variable target_tsv
;
6285 /* TSV's with a getter have been initialized equally in both the
6286 inferior and GDBserver. Skip them. */
6287 if (tsv
->getter
!= NULL
)
6293 ptr
= target_malloc (sizeof (*tsv
));
6295 if (tsv
== trace_state_variables
)
6297 /* First object in list, set the head pointer in the
6300 write_inferior_data_pointer (ipa_sym_addrs
.addr_trace_state_variables
,
6305 write_inferior_data_pointer (prev_ptr
6306 + offsetof (struct trace_state_variable
,
6311 /* Write the whole object. We'll fix up its pointers in a bit.
6312 Assume no next, fixup when needed. */
6313 target_tsv
.next
= NULL
;
6315 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6316 sizeof (target_tsv
));
6318 if (tsv
->name
!= NULL
)
6320 size_t size
= strlen (tsv
->name
) + 1;
6321 CORE_ADDR name_addr
= target_malloc (size
);
6322 write_inferior_memory (name_addr
,
6323 (unsigned char *) tsv
->name
, size
);
6324 write_inferior_data_pointer (ptr
6325 + offsetof (struct trace_state_variable
,
6330 gdb_assert (tsv
->getter
== NULL
);
6335 /* Fixup the next pointer in the last item in the list. */
6336 write_inferior_data_pointer (prev_ptr
6337 + offsetof (struct trace_state_variable
,
6342 /* Upload complete trace frames out of the IP Agent's trace buffer
6343 into GDBserver's trace buffer. This always uploads either all or
6344 no trace frames. This is the counter part of
6345 `trace_alloc_trace_buffer'. See its description of the atomic
6346 synching mechanism. */
6349 upload_fast_traceframes (void)
6351 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6352 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6354 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6355 unsigned int curr_tbctrl_idx
;
6356 unsigned int ipa_trace_buffer_ctrl_curr
;
6357 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6358 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6359 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6360 CORE_ADDR ipa_trace_buffer_lo
;
6361 CORE_ADDR ipa_trace_buffer_hi
;
6363 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6364 &ipa_traceframe_read_count_racy
))
6366 /* This will happen in most targets if the current thread is
6371 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6372 &ipa_traceframe_write_count_racy
))
6375 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6376 ipa_traceframe_write_count_racy
6377 - ipa_traceframe_read_count_racy
,
6378 ipa_traceframe_write_count_racy
,
6379 ipa_traceframe_read_count_racy
);
6381 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6384 about_to_request_buffer_space_bkpt
6385 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6388 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6389 &ipa_trace_buffer_ctrl_curr
))
6392 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6394 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6397 unsigned int prev
, counter
;
6399 /* Update the token, with new counters, and the GDBserver stamp
6400 bit. Alway reuse the current TBC index. */
6401 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6402 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6404 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6410 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6411 ipa_trace_buffer_ctrl_curr
))
6414 trace_debug ("Lib: Committed %08x -> %08x",
6415 ipa_trace_buffer_ctrl_curr_old
,
6416 ipa_trace_buffer_ctrl_curr
);
6418 /* Re-read these, now that we've installed the
6419 `about_to_request_buffer_space' breakpoint/lock. A thread could
6420 have finished a traceframe between the last read of these
6421 counters and setting the breakpoint above. If we start
6422 uploading, we never want to leave this function with
6423 traceframe_read_count != 0, otherwise, GDBserver could end up
6424 incrementing the counter tokens more than once (due to event loop
6425 nesting), which would break the IP agent's "effective" detection
6426 (see trace_alloc_trace_buffer). */
6427 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6428 &ipa_traceframe_read_count
))
6430 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6431 &ipa_traceframe_write_count
))
6436 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6437 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6438 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6440 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6441 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6442 trace_debug ("note that ipa_traceframe_count's parts changed");
6445 /* Get the address of the current TBC object (the IP agent has an
6446 array of 3 such objects). The index is stored in the TBC
6448 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6449 ipa_trace_buffer_ctrl_addr
6450 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6452 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6453 (unsigned char *) &ipa_trace_buffer_ctrl
,
6454 sizeof (struct ipa_trace_buffer_control
)))
6457 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6458 &ipa_trace_buffer_lo
))
6460 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6461 &ipa_trace_buffer_hi
))
6464 /* Offsets are easier to grok for debugging than raw addresses,
6465 especially for the small trace buffer sizes that are useful for
6467 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6468 "endfree=%d wrap=%d hi=%d",
6470 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6471 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6472 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6473 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6474 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6476 /* Note that the IPA's buffer is always circular. */
6478 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6480 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6481 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6483 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6484 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6485 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6486 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6489 tf
= IPA_FIRST_TRACEFRAME ();
6491 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6493 struct tracepoint
*tpoint
;
6494 struct traceframe
*tframe
;
6495 unsigned char *block
;
6496 struct traceframe ipa_tframe
;
6498 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6499 offsetof (struct traceframe
, data
)))
6500 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6502 if (ipa_tframe
.tpnum
== 0)
6504 internal_error (__FILE__
, __LINE__
,
6505 "Uploading: No (more) fast traceframes, but"
6506 " ipa_traceframe_count == %u??\n",
6507 ipa_traceframe_write_count
6508 - ipa_traceframe_read_count
);
6511 /* Note that this will be incorrect for multi-location
6513 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6515 tframe
= add_traceframe (tpoint
);
6518 trace_buffer_is_full
= 1;
6519 trace_debug ("Uploading: trace buffer is full");
6523 /* Copy the whole set of blocks in one go for now. FIXME:
6524 split this in smaller blocks. */
6525 block
= add_traceframe_block (tframe
, tpoint
,
6526 ipa_tframe
.data_size
);
6529 if (read_inferior_memory (tf
6530 + offsetof (struct traceframe
, data
),
6531 block
, ipa_tframe
.data_size
))
6532 error ("Uploading: Couldn't read traceframe data at %s\n",
6533 paddress (tf
+ offsetof (struct traceframe
, data
)));
6536 trace_debug ("Uploading: traceframe didn't fit");
6537 finish_traceframe (tframe
);
6540 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6542 /* If we freed the traceframe that wrapped around, go back
6543 to the non-wrap case. */
6544 if (tf
< ipa_trace_buffer_ctrl
.start
)
6546 trace_debug ("Lib: Discarding past the wraparound");
6547 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6549 ipa_trace_buffer_ctrl
.start
= tf
;
6550 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6551 ++ipa_traceframe_read_count
;
6553 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6554 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6556 trace_debug ("Lib: buffer is fully empty. "
6557 "Trace buffer [%d] start=%d free=%d endfree=%d",
6559 (int) (ipa_trace_buffer_ctrl
.start
6560 - ipa_trace_buffer_lo
),
6561 (int) (ipa_trace_buffer_ctrl
.free
6562 - ipa_trace_buffer_lo
),
6563 (int) (ipa_trace_buffer_ctrl
.end_free
6564 - ipa_trace_buffer_lo
));
6566 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6567 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6568 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6569 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6572 trace_debug ("Uploaded a traceframe\n"
6573 "Lib: Trace buffer [%d] start=%d free=%d "
6574 "endfree=%d wrap=%d hi=%d",
6576 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6577 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6578 (int) (ipa_trace_buffer_ctrl
.end_free
6579 - ipa_trace_buffer_lo
),
6580 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6581 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6584 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6585 (unsigned char *) &ipa_trace_buffer_ctrl
,
6586 sizeof (struct ipa_trace_buffer_control
)))
6589 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6590 ipa_traceframe_read_count
);
6592 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6596 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6597 about_to_request_buffer_space_bkpt
= NULL
;
6601 if (trace_buffer_is_full
)
6606 #ifdef IN_PROCESS_AGENT
6608 IP_AGENT_EXPORT_VAR
int ust_loaded
;
6609 IP_AGENT_EXPORT_VAR
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6613 /* Static tracepoints. */
6615 /* UST puts a "struct tracepoint" in the global namespace, which
6616 conflicts with our tracepoint. Arguably, being a library, it
6617 shouldn't take ownership of such a generic name. We work around it
6619 #define tracepoint ust_tracepoint
6620 #include <ust/ust.h>
6623 extern int serialize_to_text (char *outbuf
, int bufsize
,
6624 const char *fmt
, va_list ap
);
6626 #define GDB_PROBE_NAME "gdb"
6628 /* We dynamically search for the UST symbols instead of linking them
6629 in. This lets the user decide if the application uses static
6630 tracepoints, instead of always pulling libust.so in. This vector
6631 holds pointers to all functions we care about. */
6635 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6636 const char *fmt
, va_list ap
);
6638 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6639 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6641 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6643 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6646 void (*marker_iter_start
) (struct marker_iter
*iter
);
6647 void (*marker_iter_next
) (struct marker_iter
*iter
);
6648 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6649 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6654 /* Cast through typeof to catch incompatible API changes. Since UST
6655 only builds with gcc, we can freely use gcc extensions here
6657 #define GET_UST_SYM(SYM) \
6660 if (ust_ops.SYM == NULL) \
6661 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6662 if (ust_ops.SYM == NULL) \
6666 #define USTF(SYM) ust_ops.SYM
6668 /* Get pointers to all libust.so functions we care about. */
6673 GET_UST_SYM (serialize_to_text
);
6675 GET_UST_SYM (ltt_probe_register
);
6676 GET_UST_SYM (ltt_probe_unregister
);
6677 GET_UST_SYM (ltt_marker_connect
);
6678 GET_UST_SYM (ltt_marker_disconnect
);
6680 GET_UST_SYM (marker_iter_start
);
6681 GET_UST_SYM (marker_iter_next
);
6682 GET_UST_SYM (marker_iter_stop
);
6683 GET_UST_SYM (marker_iter_reset
);
6689 /* Given an UST marker, return the matching gdb static tracepoint.
6690 The match is done by address. */
6692 static struct tracepoint
*
6693 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6695 struct tracepoint
*tpoint
;
6697 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6699 if (tpoint
->type
!= static_tracepoint
)
6702 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6709 /* The probe function we install on lttng/ust markers. Whenever a
6710 probed ust marker is hit, this function is called. This is similar
6711 to gdb_collect, only for static tracepoints, instead of fast
6715 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6716 struct registers
*regs
, void *call_private
,
6717 const char *fmt
, va_list *args
)
6719 struct tracepoint
*tpoint
;
6720 struct static_tracepoint_ctx ctx
;
6721 const struct target_desc
*ipa_tdesc
;
6723 /* Don't do anything until the trace run is completely set up. */
6726 trace_debug ("gdb_probe: not tracing\n");
6730 ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
6731 ctx
.base
.type
= static_tracepoint
;
6732 ctx
.regcache_initted
= 0;
6737 /* Wrap the regblock in a register cache (in the stack, we don't
6738 want to malloc here). */
6739 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6740 if (ctx
.regspace
== NULL
)
6742 trace_debug ("Trace buffer block allocation failed, skipping");
6746 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6749 trace_debug ("gdb_probe: marker not known: "
6750 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6751 mdata
->location
, mdata
->channel
,
6752 mdata
->name
, mdata
->format
);
6756 if (!tpoint
->enabled
)
6758 trace_debug ("gdb_probe: tracepoint disabled");
6762 ctx
.tpoint
= tpoint
;
6764 trace_debug ("gdb_probe: collecting marker: "
6765 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6766 mdata
->location
, mdata
->channel
,
6767 mdata
->name
, mdata
->format
);
6769 /* Test the condition if present, and collect if true. */
6770 if (tpoint
->cond
== NULL
6771 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6774 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6775 tpoint
->address
, tpoint
);
6777 if (stopping_tracepoint
6778 || trace_buffer_is_full
6779 || expr_eval_result
!= expr_eval_no_error
)
6784 /* If there was a condition and it evaluated to false, the only
6785 way we would stop tracing is if there was an error during
6786 condition expression evaluation. */
6787 if (expr_eval_result
!= expr_eval_no_error
)
6792 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6793 static tracepoint string data. This is a string passed to the
6794 tracing library by the user, at the time of the tracepoint marker
6795 call. E.g., in the UST marker call:
6797 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6799 the collected data is "str FOOBAZ".
6803 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6804 struct traceframe
*tframe
)
6806 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6807 unsigned char *bufspace
;
6810 unsigned short blocklen
;
6814 trace_debug ("Wanted to collect static trace data, "
6815 "but there's no static trace data");
6819 va_copy (copy
, *umd
->args
);
6820 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6823 trace_debug ("Want to collect ust data");
6825 /* 'S' + size + string */
6826 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6827 1 + sizeof (blocklen
) + size
+ 1);
6828 if (bufspace
== NULL
)
6830 trace_debug ("Trace buffer block allocation failed, skipping");
6834 /* Identify a static trace data block. */
6837 blocklen
= size
+ 1;
6838 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6840 va_copy (copy
, *umd
->args
);
6841 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6842 size
+ 1, umd
->fmt
, copy
);
6845 trace_debug ("Storing static tracepoint data in regblock: %s",
6846 bufspace
+ 1 + sizeof (blocklen
));
6849 /* The probe to register with lttng/ust. */
6850 static struct ltt_available_probe gdb_ust_probe
=
6857 #endif /* HAVE_UST */
6858 #endif /* IN_PROCESS_AGENT */
6860 #ifndef IN_PROCESS_AGENT
6862 /* Ask the in-process agent to run a command. Since we don't want to
6863 have to handle the IPA hitting breakpoints while running the
6864 command, we pause all threads, remove all breakpoints, and then set
6865 the helper thread re-running. We communicate with the helper
6866 thread by means of direct memory xfering, and a socket for
6870 run_inferior_command (char *cmd
, int len
)
6873 int pid
= ptid_get_pid (current_ptid
);
6875 trace_debug ("run_inferior_command: running: %s", cmd
);
6878 uninsert_all_breakpoints ();
6880 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6882 reinsert_all_breakpoints ();
6888 #else /* !IN_PROCESS_AGENT */
6890 #include <sys/socket.h>
6893 #ifndef UNIX_PATH_MAX
6894 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6897 /* Where we put the socked used for synchronization. */
6898 #define SOCK_DIR P_tmpdir
6900 /* Thread ID of the helper thread. GDBserver reads this to know which
6901 is the help thread. This is an LWP id on Linux. */
6903 IP_AGENT_EXPORT_VAR
int helper_thread_id
;
6907 init_named_socket (const char *name
)
6910 struct sockaddr_un addr
;
6912 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6915 warning ("socket creation failed: %s", strerror (errno
));
6919 addr
.sun_family
= AF_UNIX
;
6921 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6922 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6924 result
= access (name
, F_OK
);
6928 result
= unlink (name
);
6931 warning ("unlink failed: %s", strerror (errno
));
6935 warning ("socket %s already exists; overwriting", name
);
6938 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6941 warning ("bind failed: %s", strerror (errno
));
6946 result
= listen (fd
, 1);
6949 warning ("listen: %s", strerror (errno
));
6957 static char agent_socket_name
[UNIX_PATH_MAX
];
6960 gdb_agent_socket_init (void)
6964 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6965 SOCK_DIR
, getpid ());
6966 if (result
>= UNIX_PATH_MAX
)
6968 trace_debug ("string overflow allocating socket name");
6972 fd
= init_named_socket (agent_socket_name
);
6974 warning ("Error initializing named socket (%s) for communication with the "
6975 "ust helper thread. Check that directory exists and that it "
6976 "is writable.", agent_socket_name
);
6983 /* The next marker to be returned on a qTsSTM command. */
6984 static const struct marker
*next_st
;
6986 /* Returns the first known marker. */
6991 struct marker_iter iter
;
6993 USTF(marker_iter_reset
) (&iter
);
6994 USTF(marker_iter_start
) (&iter
);
6999 /* Returns the marker following M. */
7001 const struct marker
*
7002 next_marker (const struct marker
*m
)
7004 struct marker_iter iter
;
7006 USTF(marker_iter_reset
) (&iter
);
7007 USTF(marker_iter_start
) (&iter
);
7009 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7011 if (iter
.marker
== m
)
7013 USTF(marker_iter_next
) (&iter
);
7021 /* Return an hexstr version of the STR C string, fit for sending to
7025 cstr_to_hexstr (const char *str
)
7027 int len
= strlen (str
);
7028 char *hexstr
= xmalloc (len
* 2 + 1);
7029 bin2hex ((gdb_byte
*) str
, hexstr
, len
);
7033 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7037 response_ust_marker (char *packet
, const struct marker
*st
)
7039 char *strid
, *format
, *tmp
;
7041 next_st
= next_marker (st
);
7043 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7044 strlen (st
->name
) + 1);
7045 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7047 strid
= cstr_to_hexstr (tmp
);
7050 format
= cstr_to_hexstr (st
->format
);
7052 sprintf (packet
, "m%s:%s:%s",
7053 paddress ((uintptr_t) st
->location
),
7061 /* Return the first static tracepoint, and initialize the state
7062 machine that will iterate through all the static tracepoints. */
7065 cmd_qtfstm (char *packet
)
7067 trace_debug ("Returning first trace state variable definition");
7069 if (first_marker ())
7070 response_ust_marker (packet
, first_marker ());
7072 strcpy (packet
, "l");
7075 /* Return additional trace state variable definitions. */
7078 cmd_qtsstm (char *packet
)
7080 trace_debug ("Returning static tracepoint");
7083 response_ust_marker (packet
, next_st
);
7085 strcpy (packet
, "l");
7088 /* Disconnect the GDB probe from a marker at a given address. */
7091 unprobe_marker_at (char *packet
)
7095 struct marker_iter iter
;
7097 p
+= sizeof ("unprobe_marker_at:") - 1;
7099 p
= unpack_varlen_hex (p
, &address
);
7101 USTF(marker_iter_reset
) (&iter
);
7102 USTF(marker_iter_start
) (&iter
);
7103 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7104 if ((uintptr_t ) iter
.marker
->location
== address
)
7108 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7112 warning ("could not disable marker %s/%s",
7113 iter
.marker
->channel
, iter
.marker
->name
);
7118 /* Connect the GDB probe to a marker at a given address. */
7121 probe_marker_at (char *packet
)
7125 struct marker_iter iter
;
7128 p
+= sizeof ("probe_marker_at:") - 1;
7130 p
= unpack_varlen_hex (p
, &address
);
7132 USTF(marker_iter_reset
) (&iter
);
7134 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7136 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7137 if ((uintptr_t ) m
->location
== address
)
7141 trace_debug ("found marker for address. "
7142 "ltt_marker_connect (marker = %s/%s)",
7143 m
->channel
, m
->name
);
7145 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7147 if (result
&& result
!= -EEXIST
)
7148 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7149 m
->channel
, m
->name
, -result
);
7153 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7154 m
->channel
, m
->name
);
7158 strcpy (packet
, "OK");
7162 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7167 cmd_qtstmat (char *packet
)
7171 struct marker_iter iter
;
7174 p
+= sizeof ("qTSTMat:") - 1;
7176 p
= unpack_varlen_hex (p
, &address
);
7178 USTF(marker_iter_reset
) (&iter
);
7180 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7182 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7183 if ((uintptr_t ) m
->location
== address
)
7185 response_ust_marker (packet
, m
);
7189 strcpy (packet
, "l");
7199 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7202 #endif /* HAVE_UST */
7204 #include <sys/syscall.h>
7207 gdb_agent_remove_socket (void)
7209 unlink (agent_socket_name
);
7212 /* Helper thread of agent. */
7215 gdb_agent_helper_thread (void *arg
)
7219 atexit (gdb_agent_remove_socket
);
7223 listen_fd
= gdb_agent_socket_init ();
7225 if (helper_thread_id
== 0)
7226 helper_thread_id
= syscall (SYS_gettid
);
7228 if (listen_fd
== -1)
7230 warning ("could not create sync socket\n");
7237 struct sockaddr_un sockaddr
;
7243 tmp
= sizeof (sockaddr
);
7247 fd
= accept (listen_fd
, (struct sockaddr
*) &sockaddr
, &tmp
);
7249 /* It seems an ERESTARTSYS can escape out of accept. */
7250 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7254 warning ("Accept returned %d, error: %s\n",
7255 fd
, strerror (errno
));
7261 ret
= read (fd
, buf
, 1);
7262 } while (ret
== -1 && errno
== EINTR
);
7266 warning ("reading socket (fd=%d) failed with %s",
7267 fd
, strerror (errno
));
7274 if (startswith (cmd_buf
, "close"))
7279 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7281 cmd_qtfstm (cmd_buf
);
7283 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7285 cmd_qtsstm (cmd_buf
);
7287 else if (startswith (cmd_buf
, "unprobe_marker_at:"))
7289 unprobe_marker_at (cmd_buf
);
7291 else if (startswith (cmd_buf
, "probe_marker_at:"))
7293 probe_marker_at (cmd_buf
);
7295 else if (startswith (cmd_buf
, "qTSTMat:"))
7297 cmd_qtstmat (cmd_buf
);
7299 #endif /* HAVE_UST */
7302 /* Fix compiler's warning: ignoring return value of 'write'. */
7303 ret
= write (fd
, buf
, 1);
7309 unlink (agent_socket_name
);
7311 /* Sleep endlessly to wait the whole inferior stops. This
7312 thread can not exit because GDB or GDBserver may still need
7313 'current_thread' (representing this thread) to access
7314 inferior memory. Otherwise, this thread exits earlier than
7315 other threads, and 'current_thread' is set to NULL. */
7326 #include <pthread.h>
7329 IP_AGENT_EXPORT_VAR
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7333 gdb_agent_init (void)
7340 /* We want the helper thread to be as transparent as possible, so
7341 have it inherit an all-signals-blocked mask. */
7343 sigfillset (&new_mask
);
7344 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7346 perror_with_name ("pthread_sigmask (1)");
7348 res
= pthread_create (&thread
,
7350 gdb_agent_helper_thread
,
7353 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7355 perror_with_name ("pthread_sigmask (2)");
7357 while (helper_thread_id
== 0)
7365 #include <sys/mman.h>
7368 IP_AGENT_EXPORT_VAR
char *gdb_tp_heap_buffer
;
7369 IP_AGENT_EXPORT_VAR
char *gdb_jump_pad_buffer
;
7370 IP_AGENT_EXPORT_VAR
char *gdb_jump_pad_buffer_end
;
7371 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer
;
7372 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer_end
;
7373 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer_error
;
7375 /* Record the result of getting buffer space for fast tracepoint
7376 trampolines. Any error message is copied, since caller may not be
7377 using persistent storage. */
7380 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7382 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7383 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7385 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7387 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7390 static void __attribute__ ((constructor
))
7391 initialize_tracepoint_ftlib (void)
7393 initialize_tracepoint ();
7398 #endif /* IN_PROCESS_AGENT */
7400 /* Return a timestamp, expressed as microseconds of the usual Unix
7401 time. (As the result is a 64-bit number, it will not overflow any
7405 get_timestamp (void)
7409 if (gettimeofday (&tv
, 0) != 0)
7412 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7416 initialize_tracepoint (void)
7418 /* Start with the default size. */
7419 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7421 /* Wire trace state variable 1 to be the timestamp. This will be
7422 uploaded to GDB upon connection and become one of its trace state
7423 variables. (In case you're wondering, if GDB already has a trace
7424 variable numbered 1, it will be renumbered.) */
7425 create_trace_state_variable (1, 0);
7426 set_trace_state_variable_name (1, "trace_timestamp");
7427 set_trace_state_variable_getter (1, get_timestamp
);
7429 #ifdef IN_PROCESS_AGENT
7432 size_t jump_pad_size
;
7434 pagesize
= sysconf (_SC_PAGE_SIZE
);
7436 perror_with_name ("sysconf");
7438 #define SCRATCH_BUFFER_NPAGES 20
7440 jump_pad_size
= pagesize
* SCRATCH_BUFFER_NPAGES
;
7442 gdb_tp_heap_buffer
= (char *) xmalloc (5 * 1024 * 1024);
7443 gdb_jump_pad_buffer
= alloc_jump_pad_buffer (jump_pad_size
);
7444 if (gdb_jump_pad_buffer
== NULL
)
7445 perror_with_name ("mmap");
7446 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ jump_pad_size
;
7449 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7451 /* It's not a fatal error for something to go wrong with trampoline
7452 buffer setup, but it can be mysterious, so create a channel to
7453 report back on what went wrong, using a fixed size since we may
7454 not be able to allocate space later when the problem occurs. */
7455 gdb_trampoline_buffer_error
= (char *) xmalloc (IPA_BUFSIZ
);
7457 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7459 initialize_low_tracepoint ();