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 /* Returns true if both the in-process agent library and the static
220 tracepoints libraries are loaded in the inferior, and agent has
221 capability on static tracepoints. */
224 in_process_agent_supports_ust (void)
228 if (!agent_loaded_p ())
230 warning ("In-process agent not loaded");
234 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
236 /* Agent understands static tracepoint, then check whether UST is in
237 fact loaded in the inferior. */
238 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
240 warning ("Error reading ust_loaded in lib");
251 write_e_ipa_not_loaded (char *buffer
)
254 "E.In-process agent library not loaded in process. "
255 "Fast and static tracepoints unavailable.");
258 /* Write an error to BUFFER indicating that UST isn't loaded in the
262 write_e_ust_not_loaded (char *buffer
)
266 "E.UST library not loaded in process. "
267 "Static tracepoints unavailable.");
269 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
273 /* If the in-process agent library isn't loaded in the inferior, write
274 an error to BUFFER, and return 1. Otherwise, return 0. */
277 maybe_write_ipa_not_loaded (char *buffer
)
279 if (!agent_loaded_p ())
281 write_e_ipa_not_loaded (buffer
);
287 /* If the in-process agent library and the ust (static tracepoints)
288 library aren't loaded in the inferior, write an error to BUFFER,
289 and return 1. Otherwise, return 0. */
292 maybe_write_ipa_ust_not_loaded (char *buffer
)
294 if (!agent_loaded_p ())
296 write_e_ipa_not_loaded (buffer
);
299 else if (!in_process_agent_supports_ust ())
301 write_e_ust_not_loaded (buffer
);
307 /* Cache all future symbols that the tracepoints module might request.
308 We can not request symbols at arbitrary states in the remote
309 protocol, only when the client tells us that new symbols are
310 available. So when we load the in-process library, make sure to
311 check the entire list. */
314 tracepoint_look_up_symbols (void)
318 if (agent_loaded_p ())
321 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
324 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
326 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
329 debug_printf ("symbol `%s' not found\n", symbol_list
[i
].name
);
334 agent_look_up_symbols (NULL
);
339 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
340 of the "stop_tracing" function. When this breakpoint is hit,
341 tracing stopped in the IPA for some reason. E.g., due to
342 tracepoint reaching the pass count, hitting conditional expression
343 evaluation error, etc.
345 The IPA's trace buffer is never in circular tracing mode: instead,
346 GDBserver's is, and whenever the in-process buffer fills, it calls
347 "flush_trace_buffer", which triggers an internal breakpoint.
348 GDBserver reacts to this breakpoint by pulling the meanwhile
349 collected data. Old frames discarding is always handled on the
352 #ifdef IN_PROCESS_AGENT
354 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
356 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
360 /* Call this in the functions where GDBserver places a breakpoint, so
361 that the compiler doesn't try to be clever and skip calling the
362 function at all. This is necessary, even if we tell the compiler
363 to not inline said functions. */
365 #if defined(__GNUC__)
366 # define UNKNOWN_SIDE_EFFECTS() asm ("")
368 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
371 IP_AGENT_EXPORT_FUNC
void
374 /* GDBserver places breakpoint here. */
375 UNKNOWN_SIDE_EFFECTS();
378 IP_AGENT_EXPORT_FUNC
void
379 flush_trace_buffer (void)
381 /* GDBserver places breakpoint here. */
382 UNKNOWN_SIDE_EFFECTS();
387 #ifndef IN_PROCESS_AGENT
389 tracepoint_handler (CORE_ADDR address
)
391 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
396 /* Breakpoint at "stop_tracing" in the inferior lib. */
397 struct breakpoint
*stop_tracing_bkpt
;
398 static int stop_tracing_handler (CORE_ADDR
);
400 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
401 struct breakpoint
*flush_trace_buffer_bkpt
;
402 static int flush_trace_buffer_handler (CORE_ADDR
);
404 static void download_trace_state_variables (void);
405 static void upload_fast_traceframes (void);
407 static int run_inferior_command (char *cmd
, int len
);
410 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
412 return read_inferior_memory (symaddr
, (unsigned char *) val
,
417 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
420 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
422 return read_inferior_memory (symaddr
, (unsigned char *) val
,
427 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
429 void *pval
= (void *) (uintptr_t) val
;
432 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
433 *val
= (uintptr_t) pval
;
438 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
440 void *pval
= (void *) (uintptr_t) val
;
441 return write_inferior_memory (symaddr
,
442 (unsigned char *) &pval
, sizeof (pval
));
446 write_inferior_integer (CORE_ADDR symaddr
, int val
)
448 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
452 write_inferior_int8 (CORE_ADDR symaddr
, int8_t val
)
454 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
458 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
460 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
463 static CORE_ADDR
target_malloc (ULONGEST size
);
465 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
467 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
468 BUF += sizeof ((OBJ)->FIELD); \
473 /* Base action. Concrete actions inherit this. */
475 struct tracepoint_action
480 /* An 'M' (collect memory) action. */
481 struct collect_memory_action
483 struct tracepoint_action base
;
490 /* An 'R' (collect registers) action. */
492 struct collect_registers_action
494 struct tracepoint_action base
;
497 /* An 'X' (evaluate expression) action. */
499 struct eval_expr_action
501 struct tracepoint_action base
;
503 struct agent_expr
*expr
;
506 /* An 'L' (collect static trace data) action. */
507 struct collect_static_trace_data_action
509 struct tracepoint_action base
;
512 #ifndef IN_PROCESS_AGENT
514 m_tracepoint_action_download (const struct tracepoint_action
*action
)
516 CORE_ADDR ipa_action
= target_malloc (sizeof (struct collect_memory_action
));
518 write_inferior_memory (ipa_action
, (unsigned char *) action
,
519 sizeof (struct collect_memory_action
));
524 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
526 struct collect_memory_action
*maction
527 = (struct collect_memory_action
*) action
;
529 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
530 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
531 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
537 r_tracepoint_action_download (const struct tracepoint_action
*action
)
539 CORE_ADDR ipa_action
= target_malloc (sizeof (struct collect_registers_action
));
541 write_inferior_memory (ipa_action
, (unsigned char *) action
,
542 sizeof (struct collect_registers_action
));
548 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
553 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
556 x_tracepoint_action_download (const struct tracepoint_action
*action
)
558 CORE_ADDR ipa_action
= target_malloc (sizeof (struct eval_expr_action
));
561 write_inferior_memory (ipa_action
, (unsigned char *) action
,
562 sizeof (struct eval_expr_action
));
563 expr
= download_agent_expr (((struct eval_expr_action
*) action
)->expr
);
564 write_inferior_data_pointer (ipa_action
565 + offsetof (struct eval_expr_action
, expr
),
571 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
572 copy 0 to P. Return updated header of buffer. */
575 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
577 /* Copy the length of condition first, and then copy its
586 memcpy (p
, &aexpr
->length
, 4);
589 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
596 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
598 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
600 return agent_expr_send (buffer
, eaction
->expr
);
604 l_tracepoint_action_download (const struct tracepoint_action
*action
)
607 = target_malloc (sizeof (struct collect_static_trace_data_action
));
609 write_inferior_memory (ipa_action
, (unsigned char *) action
,
610 sizeof (struct collect_static_trace_data_action
));
616 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
622 tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
624 switch (action
->type
)
627 return m_tracepoint_action_send (buffer
, action
);
629 return r_tracepoint_action_send (buffer
, action
);
631 return x_tracepoint_action_send (buffer
, action
);
633 return l_tracepoint_action_send (buffer
, action
);
635 error ("Unknown trace action '%c'.", action
->type
);
639 tracepoint_action_download (const struct tracepoint_action
*action
)
641 switch (action
->type
)
644 return m_tracepoint_action_download (action
);
646 return r_tracepoint_action_download (action
);
648 return x_tracepoint_action_download (action
);
650 return l_tracepoint_action_download (action
);
652 error ("Unknown trace action '%c'.", action
->type
);
656 /* This structure describes a piece of the source-level definition of
657 the tracepoint. The contents are not interpreted by the target,
658 but preserved verbatim for uploading upon reconnection. */
662 /* The type of string, such as "cond" for a conditional. */
665 /* The source-level string itself. For the sake of target
666 debugging, we store it in plaintext, even though it is always
667 transmitted in hex. */
670 /* Link to the next one in the list. We link them in the order
671 received, in case some make up an ordered list of commands or
673 struct source_string
*next
;
678 /* Trap based tracepoint. */
681 /* A fast tracepoint implemented with a jump instead of a trap. */
684 /* A static tracepoint, implemented by a program call into a tracing
689 struct tracepoint_hit_ctx
;
691 typedef enum eval_result_type (*condfn
) (unsigned char *,
694 /* The definition of a tracepoint. */
696 /* Tracepoints may have multiple locations, each at a different
697 address. This can occur with optimizations, template
698 instantiation, etc. Since the locations may be in different
699 scopes, the conditions and actions may be different for each
700 location. Our target version of tracepoints is more like GDB's
701 notion of "breakpoint locations", but we have almost nothing that
702 is not per-location, so we bother having two kinds of objects. The
703 key consequence is that numbers are not unique, and that it takes
704 both number and address to identify a tracepoint uniquely. */
708 /* The number of the tracepoint, as specified by GDB. Several
709 tracepoint objects here may share a number. */
712 /* Address at which the tracepoint is supposed to trigger. Several
713 tracepoints may share an address. */
716 /* Tracepoint type. */
717 enum tracepoint_type type
;
719 /* True if the tracepoint is currently enabled. */
722 /* The number of single steps that will be performed after each
726 /* The number of times the tracepoint may be hit before it will
727 terminate the entire tracing run. */
730 /* Pointer to the agent expression that is the tracepoint's
731 conditional, or NULL if the tracepoint is unconditional. */
732 struct agent_expr
*cond
;
734 /* The list of actions to take when the tracepoint triggers. */
736 struct tracepoint_action
**actions
;
738 /* Count of the times we've hit this tracepoint during the run.
739 Note that while-stepping steps are not counted as "hits". */
742 /* Cached sum of the sizes of traceframes created by this point. */
743 uint64_t traceframe_usage
;
745 CORE_ADDR compiled_cond
;
747 /* Link to the next tracepoint in the list. */
748 struct tracepoint
*next
;
750 #ifndef IN_PROCESS_AGENT
751 /* The list of actions to take when the tracepoint triggers, in
752 string/packet form. */
755 /* The collection of strings that describe the tracepoint as it was
756 entered into GDB. These are not used by the target, but are
757 reported back to GDB upon reconnection. */
758 struct source_string
*source_strings
;
760 /* The number of bytes displaced by fast tracepoints. It may subsume
761 multiple instructions, for multi-byte fast tracepoints. This
762 field is only valid for fast tracepoints. */
765 /* Only for fast tracepoints. */
766 CORE_ADDR obj_addr_on_target
;
768 /* Address range where the original instruction under a fast
769 tracepoint was relocated to. (_end is actually one byte past
771 CORE_ADDR adjusted_insn_addr
;
772 CORE_ADDR adjusted_insn_addr_end
;
774 /* The address range of the piece of the jump pad buffer that was
775 assigned to this fast tracepoint. (_end is actually one byte
778 CORE_ADDR jump_pad_end
;
780 /* The address range of the piece of the trampoline buffer that was
781 assigned to this fast tracepoint. (_end is actually one byte
783 CORE_ADDR trampoline
;
784 CORE_ADDR trampoline_end
;
786 /* The list of actions to take while in a stepping loop. These
787 fields are only valid for patch-based tracepoints. */
788 int num_step_actions
;
789 struct tracepoint_action
**step_actions
;
790 /* Same, but in string/packet form. */
791 char **step_actions_str
;
793 /* Handle returned by the breakpoint or tracepoint module when we
794 inserted the trap or jump, or hooked into a static tracepoint.
795 NULL if we haven't inserted it yet. */
801 #ifndef IN_PROCESS_AGENT
803 /* Given `while-stepping', a thread may be collecting data for more
804 than one tracepoint simultaneously. On the other hand, the same
805 tracepoint with a while-stepping action may be hit by more than one
806 thread simultaneously (but not quite, each thread could be handling
807 a different step). Each thread holds a list of these objects,
808 representing the current step of each while-stepping action being
813 struct wstep_state
*next
;
815 /* The tracepoint number. */
817 /* The tracepoint's address. */
818 CORE_ADDR tp_address
;
820 /* The number of the current step in this 'while-stepping'
829 /* The linked list of all tracepoints. Marked explicitly as used as
830 the in-process library doesn't use it for the fast tracepoints
832 IP_AGENT_EXPORT_VAR
struct tracepoint
*tracepoints
;
834 /* The first tracepoint to exceed its pass count. */
836 IP_AGENT_EXPORT_VAR
struct tracepoint
*stopping_tracepoint
;
838 /* True if the trace buffer is full or otherwise no longer usable. */
840 IP_AGENT_EXPORT_VAR
int trace_buffer_is_full
;
842 /* The first error that occurred during expression evaluation. */
844 /* Stored as an int to avoid the IPA ABI being dependent on whatever
845 the compiler decides to use for the enum's underlying type. Holds
846 enum eval_result_type values. */
847 IP_AGENT_EXPORT_VAR
int expr_eval_result
= expr_eval_no_error
;
851 #ifndef IN_PROCESS_AGENT
853 /* Pointer to the last tracepoint in the list, new tracepoints are
854 linked in at the end. */
856 static struct tracepoint
*last_tracepoint
;
858 static const char *eval_result_names
[] =
860 "terror:in the attic", /* this should never be reported */
861 "terror:empty expression",
862 "terror:empty stack",
863 "terror:stack overflow",
864 "terror:stack underflow",
865 "terror:unhandled opcode",
866 "terror:unrecognized opcode",
867 "terror:divide by zero"
872 /* The tracepoint in which the error occurred. */
875 IP_AGENT_EXPORT_VAR
struct tracepoint
*error_tracepoint
;
878 struct trace_state_variable
880 /* This is the name of the variable as used in GDB. The target
881 doesn't use the name, but needs to have it for saving and
882 reconnection purposes. */
885 /* This number identifies the variable uniquely. Numbers may be
886 assigned either by the target (in the case of builtin variables),
887 or by GDB, and are presumed unique during the course of a trace
891 /* The variable's initial value, a 64-bit signed integer always. */
892 LONGEST initial_value
;
894 /* The variable's value, a 64-bit signed integer always. */
897 /* Pointer to a getter function, used to supply computed values. */
898 LONGEST (*getter
) (void);
900 /* Link to the next variable. */
901 struct trace_state_variable
*next
;
904 /* Linked list of all trace state variables. */
906 #ifdef IN_PROCESS_AGENT
907 struct trace_state_variable
*alloced_trace_state_variables
;
910 IP_AGENT_EXPORT_VAR
struct trace_state_variable
*trace_state_variables
;
912 /* The results of tracing go into a fixed-size space known as the
913 "trace buffer". Because usage follows a limited number of
914 patterns, we manage it ourselves rather than with malloc. Basic
915 rules are that we create only one trace frame at a time, each is
916 variable in size, they are never moved once created, and we only
917 discard if we are doing a circular buffer, and then only the oldest
918 ones. Each trace frame includes its own size, so we don't need to
919 link them together, and the trace frame number is relative to the
920 first one, so we don't need to record numbers. A trace frame also
921 records the number of the tracepoint that created it. The data
922 itself is a series of blocks, each introduced by a single character
923 and with a defined format. Each type of block has enough
924 type/length info to allow scanners to jump quickly from one block
925 to the next without reading each byte in the block. */
927 /* Trace buffer management would be simple - advance a free pointer
928 from beginning to end, then stop - were it not for the circular
929 buffer option, which is a useful way to prevent a trace run from
930 stopping prematurely because the buffer filled up. In the circular
931 case, the location of the first trace frame (trace_buffer_start)
932 moves as old trace frames are discarded. Also, since we grow trace
933 frames incrementally as actions are performed, we wrap around to
934 the beginning of the trace buffer. This is per-block, so each
935 block within a trace frame remains contiguous. Things get messy
936 when the wrapped-around trace frame is the one being discarded; the
937 free space ends up in two parts at opposite ends of the buffer. */
940 # if defined(__GNUC__)
941 # define ATTR_PACKED __attribute__ ((packed))
943 # define ATTR_PACKED /* nothing */
947 /* The data collected at a tracepoint hit. This object should be as
948 small as possible, since there may be a great many of them. We do
949 not need to keep a frame number, because they are all sequential
950 and there are no deletions; so the Nth frame in the buffer is
951 always frame number N. */
955 /* Number of the tracepoint that collected this traceframe. A value
956 of 0 indicates the current end of the trace buffer. We make this
957 a 16-bit field because it's never going to happen that GDB's
958 numbering of tracepoints reaches 32,000. */
961 /* The size of the data in this trace frame. We limit this to 32
962 bits, even on a 64-bit target, because it's just implausible that
963 one is validly going to collect 4 gigabytes of data at a single
965 unsigned int data_size
: 32;
967 /* The base of the trace data, which is contiguous from this point. */
968 unsigned char data
[0];
972 /* The size of the EOB marker, in bytes. A traceframe with zeroed
973 fields (and no data) marks the end of trace data. */
974 #define TRACEFRAME_EOB_MARKER_SIZE offsetof (struct traceframe, data)
976 /* The traceframe to be used as the source of data to send back to
977 GDB. A value of -1 means to get data from the live program. */
979 int current_traceframe
= -1;
981 /* This flag is true if the trace buffer is circular, meaning that
982 when it fills, the oldest trace frames are discarded in order to
985 #ifndef IN_PROCESS_AGENT
986 static int circular_trace_buffer
;
989 /* Size of the trace buffer. */
991 static LONGEST trace_buffer_size
;
995 /* Pointer to the block of memory that traceframes all go into. */
997 IP_AGENT_EXPORT_VAR
unsigned char *trace_buffer_lo
;
999 /* Pointer to the end of the trace buffer, more precisely to the byte
1000 after the end of the buffer. */
1002 IP_AGENT_EXPORT_VAR
unsigned char *trace_buffer_hi
;
1006 /* Control structure holding the read/write/etc. pointers into the
1007 trace buffer. We need more than one of these to implement a
1008 transaction-like mechanism to garantees that both GDBserver and the
1009 in-process agent can try to change the trace buffer
1012 struct trace_buffer_control
1014 /* Pointer to the first trace frame in the buffer. In the
1015 non-circular case, this is equal to trace_buffer_lo, otherwise it
1016 moves around in the buffer. */
1017 unsigned char *start
;
1019 /* Pointer to the free part of the trace buffer. Note that we clear
1020 several bytes at and after this pointer, so that traceframe
1021 scans/searches terminate properly. */
1022 unsigned char *free
;
1024 /* Pointer to the byte after the end of the free part. Note that
1025 this may be smaller than trace_buffer_free in the circular case,
1026 and means that the free part is in two pieces. Initially it is
1027 equal to trace_buffer_hi, then is generally equivalent to
1028 trace_buffer_start. */
1029 unsigned char *end_free
;
1031 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1032 this is the point at which the trace data breaks, and resumes at
1034 unsigned char *wrap
;
1037 /* Same as above, to be used by GDBserver when updating the in-process
1039 struct ipa_trace_buffer_control
1048 /* We have possibly both GDBserver and an inferior thread accessing
1049 the same IPA trace buffer memory. The IPA is the producer (tries
1050 to put new frames in the buffer), while GDBserver occasionally
1051 consumes them, that is, flushes the IPA's buffer into its own
1052 buffer. Both sides need to update the trace buffer control
1053 pointers (current head, tail, etc.). We can't use a global lock to
1054 synchronize the accesses, as otherwise we could deadlock GDBserver
1055 (if the thread holding the lock stops for a signal, say). So
1056 instead of that, we use a transaction scheme where GDBserver writes
1057 always prevail over the IPAs writes, and, we have the IPA detect
1058 the commit failure/overwrite, and retry the whole attempt. This is
1059 mainly implemented by having a global token object that represents
1060 who wrote last to the buffer control structure. We need to freeze
1061 any inferior writing to the buffer while GDBserver touches memory,
1062 so that the inferior can correctly detect that GDBserver had been
1063 there, otherwise, it could mistakingly think its commit was
1064 successful; that's implemented by simply having GDBserver set a
1065 breakpoint the inferior hits if it is the critical region.
1067 There are three cycling trace buffer control structure copies
1068 (buffer head, tail, etc.), with the token object including an index
1069 indicating which is current live copy. The IPA tentatively builds
1070 an updated copy in a non-current control structure, while GDBserver
1071 always clobbers the current version directly. The IPA then tries
1072 to atomically "commit" its version; if GDBserver clobbered the
1073 structure meanwhile, that will fail, and the IPA restarts the
1076 Listing the step in further detail, we have:
1078 In-process agent (producer):
1080 - passes by `about_to_request_buffer_space' breakpoint/lock
1082 - reads current token, extracts current trace buffer control index,
1083 and starts tentatively updating the rightmost one (0->1, 1->2,
1084 2->0). Note that only one inferior thread is executing this code
1085 at any given time, due to an outer lock in the jump pads.
1087 - updates counters, and tries to commit the token.
1089 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1090 leaving the sync region.
1092 - checks if the update was effective.
1094 - if trace buffer was found full, hits flush_trace_buffer
1095 breakpoint, and restarts later afterwards.
1097 GDBserver (consumer):
1099 - sets `about_to_request_buffer_space' breakpoint/lock.
1101 - updates the token unconditionally, using the current buffer
1102 control index, since it knows that the IP agent always writes to
1103 the rightmost, and due to the breakpoint, at most one IP thread
1104 can try to update the trace buffer concurrently to GDBserver, so
1105 there will be no danger of trace buffer control index wrap making
1106 the IPA write to the same index as GDBserver.
1108 - flushes the IP agent's trace buffer completely, and updates the
1109 current trace buffer control structure. GDBserver *always* wins.
1111 - removes the `about_to_request_buffer_space' breakpoint.
1113 The token is stored in the `trace_buffer_ctrl_curr' variable.
1114 Internally, it's bits are defined as:
1116 |-------------+-----+-------------+--------+-------------+--------------|
1117 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1118 |-------------+-----+-------------+--------+-------------+--------------|
1119 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1120 |-------------+-----+-------------+--------+-------------+--------------|
1122 GSB - GDBserver Stamp Bit
1123 PC - Previous Counter
1124 CC - Current Counter
1125 TBCI - Trace Buffer Control Index
1128 An IPA update of `trace_buffer_ctrl_curr' does:
1130 - read CC from the current token, save as PC.
1132 - atomically tries to write PC+1,CC
1134 A GDBserver update of `trace_buffer_ctrl_curr' does:
1136 - reads PC and CC from the current token.
1141 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1142 for the counters described below. The cleared bits are used to
1143 hold the index of the items of the `trace_buffer_ctrl' array that
1145 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1147 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1148 counter, and the `current' counter. */
1150 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1151 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1153 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1154 always stamps this bit as set. */
1155 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1157 #ifdef IN_PROCESS_AGENT
1158 IP_AGENT_EXPORT_VAR
struct trace_buffer_control trace_buffer_ctrl
[3];
1159 IP_AGENT_EXPORT_VAR
unsigned int trace_buffer_ctrl_curr
;
1161 # define TRACE_BUFFER_CTRL_CURR \
1162 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1166 /* The GDBserver side agent only needs one instance of this object, as
1167 it doesn't need to sync with itself. Define it as array anyway so
1168 that the rest of the code base doesn't need to care for the
1170 struct trace_buffer_control trace_buffer_ctrl
[1];
1171 # define TRACE_BUFFER_CTRL_CURR 0
1174 /* These are convenience macros used to access the current trace
1175 buffer control in effect. */
1176 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1177 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1178 #define trace_buffer_end_free \
1179 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1180 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1183 /* Macro that returns a pointer to the first traceframe in the buffer. */
1185 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1187 /* Macro that returns a pointer to the next traceframe in the buffer.
1188 If the computed location is beyond the wraparound point, subtract
1189 the offset of the wraparound. */
1191 #define NEXT_TRACEFRAME_1(TF) \
1192 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1194 #define NEXT_TRACEFRAME(TF) \
1195 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1196 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1197 ? (trace_buffer_wrap - trace_buffer_lo) \
1200 /* The difference between these counters represents the total number
1201 of complete traceframes present in the trace buffer. The IP agent
1202 writes to the write count, GDBserver writes to read count. */
1204 IP_AGENT_EXPORT_VAR
unsigned int traceframe_write_count
;
1205 IP_AGENT_EXPORT_VAR
unsigned int traceframe_read_count
;
1207 /* Convenience macro. */
1209 #define traceframe_count \
1210 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1212 /* The count of all traceframes created in the current run, including
1213 ones that were discarded to make room. */
1215 IP_AGENT_EXPORT_VAR
int traceframes_created
;
1217 #ifndef IN_PROCESS_AGENT
1219 /* Read-only regions are address ranges whose contents don't change,
1220 and so can be read from target memory even while looking at a trace
1221 frame. Without these, disassembly for instance will likely fail,
1222 because the program code is not usually collected into a trace
1223 frame. This data structure does not need to be very complicated or
1224 particularly efficient, it's only going to be used occasionally,
1225 and only by some commands. */
1227 struct readonly_region
1229 /* The bounds of the region. */
1230 CORE_ADDR start
, end
;
1232 /* Link to the next one. */
1233 struct readonly_region
*next
;
1236 /* Linked list of readonly regions. This list stays in effect from
1237 one tstart to the next. */
1239 static struct readonly_region
*readonly_regions
;
1243 /* The global that controls tracing overall. */
1245 IP_AGENT_EXPORT_VAR
int tracing
;
1247 #ifndef IN_PROCESS_AGENT
1249 /* Controls whether tracing should continue after GDB disconnects. */
1251 int disconnected_tracing
;
1253 /* The reason for the last tracing run to have stopped. We initialize
1254 to a distinct string so that GDB can distinguish between "stopped
1255 after running" and "stopped because never run" cases. */
1257 static const char *tracing_stop_reason
= "tnotrun";
1259 static int tracing_stop_tpnum
;
1261 /* 64-bit timestamps for the trace run's start and finish, expressed
1262 in microseconds from the Unix epoch. */
1264 LONGEST tracing_start_time
;
1265 LONGEST tracing_stop_time
;
1267 /* The (optional) user-supplied name of the user that started the run.
1268 This is an arbitrary string, and may be NULL. */
1270 char *tracing_user_name
;
1272 /* Optional user-supplied text describing the run. This is
1273 an arbitrary string, and may be NULL. */
1275 char *tracing_notes
;
1277 /* Optional user-supplied text explaining a tstop command. This is an
1278 arbitrary string, and may be NULL. */
1280 char *tracing_stop_note
;
1284 /* Functions local to this file. */
1286 /* Base "class" for tracepoint type specific data to be passed down to
1287 collect_data_at_tracepoint. */
1288 struct tracepoint_hit_ctx
1290 enum tracepoint_type type
;
1293 #ifdef IN_PROCESS_AGENT
1295 /* Fast/jump tracepoint specific data to be passed down to
1296 collect_data_at_tracepoint. */
1297 struct fast_tracepoint_ctx
1299 struct tracepoint_hit_ctx base
;
1301 struct regcache regcache
;
1302 int regcache_initted
;
1303 unsigned char *regspace
;
1305 unsigned char *regs
;
1306 struct tracepoint
*tpoint
;
1309 /* Static tracepoint specific data to be passed down to
1310 collect_data_at_tracepoint. */
1311 struct static_tracepoint_ctx
1313 struct tracepoint_hit_ctx base
;
1315 /* The regcache corresponding to the registers state at the time of
1316 the tracepoint hit. Initialized lazily, from REGS. */
1317 struct regcache regcache
;
1318 int regcache_initted
;
1320 /* The buffer space REGCACHE above uses. We use a separate buffer
1321 instead of letting the regcache malloc for both signal safety and
1322 performance reasons; this is allocated on the stack instead. */
1323 unsigned char *regspace
;
1325 /* The register buffer as passed on by lttng/ust. */
1326 struct registers
*regs
;
1328 /* The "printf" formatter and the args the user passed to the marker
1329 call. We use this to be able to collect "static trace data"
1334 /* The GDB tracepoint matching the probed marker that was "hit". */
1335 struct tracepoint
*tpoint
;
1340 /* Static tracepoint specific data to be passed down to
1341 collect_data_at_tracepoint. */
1342 struct trap_tracepoint_ctx
1344 struct tracepoint_hit_ctx base
;
1346 struct regcache
*regcache
;
1351 #ifndef IN_PROCESS_AGENT
1352 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1353 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1356 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1357 struct tracepoint
*tpoint
);
1359 #ifndef IN_PROCESS_AGENT
1360 static void clear_readonly_regions (void);
1361 static void clear_installed_tracepoints (void);
1364 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1366 struct tracepoint
*tpoint
);
1367 #ifndef IN_PROCESS_AGENT
1368 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1370 struct tracepoint
*tpoint
, int current_step
);
1371 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1372 CORE_ADDR
*jump_entry
);
1374 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1376 struct tracepoint
*tpoint
,
1377 struct traceframe
*tframe
,
1378 struct tracepoint_action
*taction
);
1380 #ifndef IN_PROCESS_AGENT
1381 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1383 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1384 static void download_tracepoint (struct tracepoint
*);
1385 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1386 static void clone_fast_tracepoint (struct tracepoint
*to
,
1387 const struct tracepoint
*from
);
1390 static LONGEST
get_timestamp (void);
1392 #if defined(__GNUC__)
1393 # define memory_barrier() asm volatile ("" : : : "memory")
1395 # define memory_barrier() do {} while (0)
1398 /* We only build the IPA if this builtin is supported, and there are
1399 no uses of this in GDBserver itself, so we're safe in defining this
1401 #define cmpxchg(mem, oldval, newval) \
1402 __sync_val_compare_and_swap (mem, oldval, newval)
1404 /* Record that an error occurred during expression evaluation. */
1407 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1408 enum eval_result_type rtype
)
1410 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1411 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1413 #ifdef IN_PROCESS_AGENT
1414 /* Only record the first error we get. */
1415 if (cmpxchg (&expr_eval_result
,
1417 rtype
) != expr_eval_no_error
)
1420 if (expr_eval_result
!= expr_eval_no_error
)
1424 error_tracepoint
= tpoint
;
1427 /* Trace buffer management. */
1430 clear_trace_buffer (void)
1432 trace_buffer_start
= trace_buffer_lo
;
1433 trace_buffer_free
= trace_buffer_lo
;
1434 trace_buffer_end_free
= trace_buffer_hi
;
1435 trace_buffer_wrap
= trace_buffer_hi
;
1436 /* A traceframe with zeroed fields marks the end of trace data. */
1437 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1438 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1439 traceframe_read_count
= traceframe_write_count
= 0;
1440 traceframes_created
= 0;
1443 #ifndef IN_PROCESS_AGENT
1446 clear_inferior_trace_buffer (void)
1448 CORE_ADDR ipa_trace_buffer_lo
;
1449 CORE_ADDR ipa_trace_buffer_hi
;
1450 struct traceframe ipa_traceframe
= { 0 };
1451 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1453 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1454 &ipa_trace_buffer_lo
);
1455 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1456 &ipa_trace_buffer_hi
);
1458 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1459 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1460 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1461 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1463 /* A traceframe with zeroed fields marks the end of trace data. */
1464 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1465 (unsigned char *) &ipa_trace_buffer_ctrl
,
1466 sizeof (ipa_trace_buffer_ctrl
));
1468 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1470 /* A traceframe with zeroed fields marks the end of trace data. */
1471 write_inferior_memory (ipa_trace_buffer_lo
,
1472 (unsigned char *) &ipa_traceframe
,
1473 sizeof (ipa_traceframe
));
1475 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1476 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1477 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1483 init_trace_buffer (LONGEST bufsize
)
1487 trace_buffer_size
= bufsize
;
1489 /* Make sure to internally allocate at least space for the EOB
1491 alloc_size
= (bufsize
< TRACEFRAME_EOB_MARKER_SIZE
1492 ? TRACEFRAME_EOB_MARKER_SIZE
: bufsize
);
1493 trace_buffer_lo
= (unsigned char *) xrealloc (trace_buffer_lo
, alloc_size
);
1495 trace_buffer_hi
= trace_buffer_lo
+ trace_buffer_size
;
1497 clear_trace_buffer ();
1500 #ifdef IN_PROCESS_AGENT
1502 IP_AGENT_EXPORT_FUNC
void
1503 about_to_request_buffer_space (void)
1505 /* GDBserver places breakpoint here while it goes about to flush
1506 data at random times. */
1507 UNKNOWN_SIDE_EFFECTS();
1512 /* Carve out a piece of the trace buffer, returning NULL in case of
1516 trace_buffer_alloc (size_t amt
)
1518 unsigned char *rslt
;
1519 struct trace_buffer_control
*tbctrl
;
1521 #ifdef IN_PROCESS_AGENT
1522 unsigned int prev
, prev_filtered
;
1523 unsigned int commit_count
;
1524 unsigned int commit
;
1525 unsigned int readout
;
1527 struct traceframe
*oldest
;
1528 unsigned char *new_start
;
1531 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1532 (long) amt
, (long) sizeof (struct traceframe
));
1534 /* Account for the EOB marker. */
1535 amt
+= TRACEFRAME_EOB_MARKER_SIZE
;
1537 #ifdef IN_PROCESS_AGENT
1541 /* Read the current token and extract the index to try to write to,
1542 storing it in CURR. */
1543 prev
= trace_buffer_ctrl_curr
;
1544 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1545 curr
= prev_filtered
+ 1;
1549 about_to_request_buffer_space ();
1551 /* Start out with a copy of the current state. GDBserver may be
1552 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1553 be able to commit anyway if that happens. */
1554 trace_buffer_ctrl
[curr
]
1555 = trace_buffer_ctrl
[prev_filtered
];
1556 trace_debug ("trying curr=%u", curr
);
1558 /* The GDBserver's agent doesn't need all that syncing, and always
1559 updates TCB 0 (there's only one, mind you). */
1562 tbctrl
= &trace_buffer_ctrl
[curr
];
1564 /* Offsets are easier to grok for debugging than raw addresses,
1565 especially for the small trace buffer sizes that are useful for
1567 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1569 (int) (tbctrl
->start
- trace_buffer_lo
),
1570 (int) (tbctrl
->free
- trace_buffer_lo
),
1571 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1572 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1573 (int) (trace_buffer_hi
- trace_buffer_lo
));
1575 /* The algorithm here is to keep trying to get a contiguous block of
1576 the requested size, possibly discarding older traceframes to free
1577 up space. Since free space might come in one or two pieces,
1578 depending on whether discarded traceframes wrapped around at the
1579 high end of the buffer, we test both pieces after each
1583 /* First, if we have two free parts, try the upper one first. */
1584 if (tbctrl
->end_free
< tbctrl
->free
)
1586 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1587 /* We have enough in the upper part. */
1591 /* Our high part of free space wasn't enough. Give up
1592 on it for now, set wraparound. We will recover the
1593 space later, if/when the wrapped-around traceframe is
1595 trace_debug ("Upper part too small, setting wraparound");
1596 tbctrl
->wrap
= tbctrl
->free
;
1597 tbctrl
->free
= trace_buffer_lo
;
1601 /* The normal case. */
1602 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1605 #ifdef IN_PROCESS_AGENT
1606 /* The IP Agent's buffer is always circular. It isn't used
1607 currently, but `circular_trace_buffer' could represent
1608 GDBserver's mode. If we didn't find space, ask GDBserver to
1611 flush_trace_buffer ();
1615 trace_debug ("gdbserver flushed buffer, retrying");
1619 /* GDBserver cancelled the tracing. Bail out as well. */
1622 /* If we're here, then neither part is big enough, and
1623 non-circular trace buffers are now full. */
1624 if (!circular_trace_buffer
)
1626 trace_debug ("Not enough space in the trace buffer");
1630 trace_debug ("Need more space in the trace buffer");
1632 /* If we have a circular buffer, we can try discarding the
1633 oldest traceframe and see if that helps. */
1634 oldest
= FIRST_TRACEFRAME ();
1635 if (oldest
->tpnum
== 0)
1637 /* Not good; we have no traceframes to free. Perhaps we're
1638 asking for a block that is larger than the buffer? In
1639 any case, give up. */
1640 trace_debug ("No traceframes to discard");
1644 /* We don't run this code in the in-process agent currently.
1645 E.g., we could leave the in-process agent in autonomous
1646 circular mode if we only have fast tracepoints. If we do
1647 that, then this bit becomes racy with GDBserver, which also
1648 writes to this counter. */
1649 --traceframe_write_count
;
1651 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1652 /* If we freed the traceframe that wrapped around, go back
1653 to the non-wrap case. */
1654 if (new_start
< tbctrl
->start
)
1656 trace_debug ("Discarding past the wraparound");
1657 tbctrl
->wrap
= trace_buffer_hi
;
1659 tbctrl
->start
= new_start
;
1660 tbctrl
->end_free
= tbctrl
->start
;
1662 trace_debug ("Discarded a traceframe\n"
1663 "Trace buffer [%d], start=%d free=%d "
1664 "endfree=%d wrap=%d hi=%d",
1666 (int) (tbctrl
->start
- trace_buffer_lo
),
1667 (int) (tbctrl
->free
- trace_buffer_lo
),
1668 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1669 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1670 (int) (trace_buffer_hi
- trace_buffer_lo
));
1672 /* Now go back around the loop. The discard might have resulted
1673 in either one or two pieces of free space, so we want to try
1674 both before freeing any more traceframes. */
1678 /* If we get here, we know we can provide the asked-for space. */
1680 rslt
= tbctrl
->free
;
1682 /* Adjust the request back down, now that we know we have space for
1683 the marker, but don't commit to AMT yet, we may still need to
1684 restart the operation if GDBserver touches the trace buffer
1685 (obviously only important in the in-process agent's version). */
1686 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1688 /* Or not. If GDBserver changed the trace buffer behind our back,
1689 we get to restart a new allocation attempt. */
1691 #ifdef IN_PROCESS_AGENT
1692 /* Build the tentative token. */
1693 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1694 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1695 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1699 /* Try to commit it. */
1700 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1701 if (readout
!= prev
)
1703 trace_debug ("GDBserver has touched the trace buffer, restarting."
1704 " (prev=%08x, commit=%08x, readout=%08x)",
1705 prev
, commit
, readout
);
1709 /* Hold your horses here. Even if that change was committed,
1710 GDBserver could come in, and clobber it. We need to hold to be
1711 able to tell if GDBserver clobbers before or after we committed
1712 the change. Whenever GDBserver goes about touching the IPA
1713 buffer, it sets a breakpoint in this routine, so we have a sync
1715 about_to_request_buffer_space ();
1717 /* Check if the change has been effective, even if GDBserver stopped
1718 us at the breakpoint. */
1721 unsigned int refetch
;
1725 refetch
= trace_buffer_ctrl_curr
;
1727 if (refetch
== commit
1728 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1731 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1732 "readout=%08x, refetch=%08x)",
1733 prev
, commit
, readout
, refetch
);
1737 trace_debug ("GDBserver has touched the trace buffer, not effective."
1738 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1739 prev
, commit
, readout
, refetch
);
1745 /* We have a new piece of the trace buffer. Hurray! */
1747 /* Add an EOB marker just past this allocation. */
1748 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1749 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1751 /* Adjust the request back down, now that we know we have space for
1753 amt
-= sizeof (struct traceframe
);
1757 trace_debug ("Allocated %d bytes", (int) amt
);
1758 trace_debug ("Trace buffer [%d] start=%d free=%d "
1759 "endfree=%d wrap=%d hi=%d",
1761 (int) (tbctrl
->start
- trace_buffer_lo
),
1762 (int) (tbctrl
->free
- trace_buffer_lo
),
1763 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1764 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1765 (int) (trace_buffer_hi
- trace_buffer_lo
));
1771 #ifndef IN_PROCESS_AGENT
1773 /* Return the total free space. This is not necessarily the largest
1774 block we can allocate, because of the two-part case. */
1779 if (trace_buffer_free
<= trace_buffer_end_free
)
1780 return trace_buffer_end_free
- trace_buffer_free
;
1782 return ((trace_buffer_end_free
- trace_buffer_lo
)
1783 + (trace_buffer_hi
- trace_buffer_free
));
1786 /* An 'S' in continuation packets indicates remainder are for
1789 static int seen_step_action_flag
;
1791 /* Create a tracepoint (location) with given number and address. Add this
1792 new tracepoint to list and sort this list. */
1794 static struct tracepoint
*
1795 add_tracepoint (int num
, CORE_ADDR addr
)
1797 struct tracepoint
*tpoint
, **tp_next
;
1799 tpoint
= XNEW (struct tracepoint
);
1800 tpoint
->number
= num
;
1801 tpoint
->address
= addr
;
1802 tpoint
->numactions
= 0;
1803 tpoint
->actions
= NULL
;
1804 tpoint
->actions_str
= NULL
;
1805 tpoint
->cond
= NULL
;
1806 tpoint
->num_step_actions
= 0;
1807 tpoint
->step_actions
= NULL
;
1808 tpoint
->step_actions_str
= NULL
;
1809 /* Start all off as regular (slow) tracepoints. */
1810 tpoint
->type
= trap_tracepoint
;
1811 tpoint
->orig_size
= -1;
1812 tpoint
->source_strings
= NULL
;
1813 tpoint
->compiled_cond
= 0;
1814 tpoint
->handle
= NULL
;
1815 tpoint
->next
= NULL
;
1817 /* Find a place to insert this tracepoint into list in order to keep
1818 the tracepoint list still in the ascending order. There may be
1819 multiple tracepoints at the same address as TPOINT's, and this
1820 guarantees TPOINT is inserted after all the tracepoints which are
1821 set at the same address. For example, fast tracepoints A, B, C are
1822 set at the same address, and D is to be insert at the same place as
1825 -->| A |--> | B |-->| C |->...
1827 One jump pad was created for tracepoint A, B, and C, and the target
1828 address of A is referenced/used in jump pad. So jump pad will let
1829 inferior jump to A. If D is inserted in front of A, like this,
1831 -->| D |-->| A |--> | B |-->| C |->...
1833 without updating jump pad, D is not reachable during collect, which
1834 is wrong. As we can see, the order of B, C and D doesn't matter, but
1835 A should always be the `first' one. */
1836 for (tp_next
= &tracepoints
;
1837 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1838 tp_next
= &(*tp_next
)->next
)
1840 tpoint
->next
= *tp_next
;
1842 last_tracepoint
= tpoint
;
1844 seen_step_action_flag
= 0;
1849 #ifndef IN_PROCESS_AGENT
1851 /* Return the tracepoint with the given number and address, or NULL. */
1853 static struct tracepoint
*
1854 find_tracepoint (int id
, CORE_ADDR addr
)
1856 struct tracepoint
*tpoint
;
1858 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1859 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1865 /* Remove TPOINT from global list. */
1868 remove_tracepoint (struct tracepoint
*tpoint
)
1870 struct tracepoint
*tp
, *tp_prev
;
1872 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1873 tp_prev
= tp
, tp
= tp
->next
)
1879 tp_prev
->next
= tp
->next
;
1881 tracepoints
= tp
->next
;
1887 /* There may be several tracepoints with the same number (because they
1888 are "locations", in GDB parlance); return the next one after the
1889 given tracepoint, or search from the beginning of the list if the
1890 first argument is NULL. */
1892 static struct tracepoint
*
1893 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1895 struct tracepoint
*tpoint
;
1898 tpoint
= prev_tp
->next
;
1900 tpoint
= tracepoints
;
1901 for (; tpoint
; tpoint
= tpoint
->next
)
1902 if (tpoint
->number
== num
)
1910 /* Append another action to perform when the tracepoint triggers. */
1913 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1919 seen_step_action_flag
= 1;
1927 char *act_start
= act
;
1928 struct tracepoint_action
*action
= NULL
;
1934 struct collect_memory_action
*maction
=
1935 XNEW (struct collect_memory_action
);
1939 maction
->base
.type
= *act
;
1940 action
= &maction
->base
;
1943 is_neg
= (*act
== '-');
1946 act
= unpack_varlen_hex (act
, &basereg
);
1948 act
= unpack_varlen_hex (act
, &maction
->addr
);
1950 act
= unpack_varlen_hex (act
, &maction
->len
);
1951 maction
->basereg
= (is_neg
1954 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1955 pulongest (maction
->len
),
1956 paddress (maction
->addr
), maction
->basereg
);
1961 struct collect_registers_action
*raction
=
1962 XNEW (struct collect_registers_action
);
1964 raction
->base
.type
= *act
;
1965 action
= &raction
->base
;
1967 trace_debug ("Want to collect registers");
1969 /* skip past hex digits of mask for now */
1970 while (isxdigit(*act
))
1976 struct collect_static_trace_data_action
*raction
=
1977 XNEW (struct collect_static_trace_data_action
);
1979 raction
->base
.type
= *act
;
1980 action
= &raction
->base
;
1982 trace_debug ("Want to collect static trace data");
1987 trace_debug ("Unexpected step action, ignoring");
1992 struct eval_expr_action
*xaction
= XNEW (struct eval_expr_action
);
1994 xaction
->base
.type
= *act
;
1995 action
= &xaction
->base
;
1997 trace_debug ("Want to evaluate expression");
1998 xaction
->expr
= gdb_parse_agent_expr (&act
);
2002 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2011 if (seen_step_action_flag
)
2013 tpoint
->num_step_actions
++;
2015 tpoint
->step_actions
2016 = XRESIZEVEC (struct tracepoint_action
*, tpoint
->step_actions
,
2017 tpoint
->num_step_actions
);
2018 tpoint
->step_actions_str
2019 = XRESIZEVEC (char *, tpoint
->step_actions_str
,
2020 tpoint
->num_step_actions
);
2021 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2022 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2023 = savestring (act_start
, act
- act_start
);
2027 tpoint
->numactions
++;
2029 = XRESIZEVEC (struct tracepoint_action
*, tpoint
->actions
,
2030 tpoint
->numactions
);
2032 = XRESIZEVEC (char *, tpoint
->actions_str
, tpoint
->numactions
);
2033 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2034 tpoint
->actions_str
[tpoint
->numactions
- 1]
2035 = savestring (act_start
, act
- act_start
);
2042 /* Find or create a trace state variable with the given number. */
2044 static struct trace_state_variable
*
2045 get_trace_state_variable (int num
)
2047 struct trace_state_variable
*tsv
;
2049 #ifdef IN_PROCESS_AGENT
2050 /* Search for an existing variable. */
2051 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2052 if (tsv
->number
== num
)
2056 /* Search for an existing variable. */
2057 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2058 if (tsv
->number
== num
)
2064 /* Find or create a trace state variable with the given number. */
2066 static struct trace_state_variable
*
2067 create_trace_state_variable (int num
, int gdb
)
2069 struct trace_state_variable
*tsv
;
2071 tsv
= get_trace_state_variable (num
);
2075 /* Create a new variable. */
2076 tsv
= XNEW (struct trace_state_variable
);
2078 tsv
->initial_value
= 0;
2082 #ifdef IN_PROCESS_AGENT
2085 tsv
->next
= alloced_trace_state_variables
;
2086 alloced_trace_state_variables
= tsv
;
2091 tsv
->next
= trace_state_variables
;
2092 trace_state_variables
= tsv
;
2097 IP_AGENT_EXPORT_FUNC LONGEST
2098 get_trace_state_variable_value (int num
)
2100 struct trace_state_variable
*tsv
;
2102 tsv
= get_trace_state_variable (num
);
2106 trace_debug ("No trace state variable %d, skipping value get", num
);
2110 /* Call a getter function if we have one. While it's tempting to
2111 set up something to only call the getter once per tracepoint hit,
2112 it could run afoul of thread races. Better to let the getter
2113 handle it directly, if necessary to worry about it. */
2115 tsv
->value
= (tsv
->getter
) ();
2117 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2118 num
, plongest (tsv
->value
));
2123 IP_AGENT_EXPORT_FUNC
void
2124 set_trace_state_variable_value (int num
, LONGEST val
)
2126 struct trace_state_variable
*tsv
;
2128 tsv
= get_trace_state_variable (num
);
2132 trace_debug ("No trace state variable %d, skipping value set", num
);
2140 agent_get_trace_state_variable_value (int num
)
2142 return get_trace_state_variable_value (num
);
2146 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2148 set_trace_state_variable_value (num
, val
);
2152 set_trace_state_variable_name (int num
, const char *name
)
2154 struct trace_state_variable
*tsv
;
2156 tsv
= get_trace_state_variable (num
);
2160 trace_debug ("No trace state variable %d, skipping name set", num
);
2164 tsv
->name
= (char *) name
;
2168 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2170 struct trace_state_variable
*tsv
;
2172 tsv
= get_trace_state_variable (num
);
2176 trace_debug ("No trace state variable %d, skipping getter set", num
);
2180 tsv
->getter
= getter
;
2183 /* Add a raw traceframe for the given tracepoint. */
2185 static struct traceframe
*
2186 add_traceframe (struct tracepoint
*tpoint
)
2188 struct traceframe
*tframe
;
2191 = (struct traceframe
*) trace_buffer_alloc (sizeof (struct traceframe
));
2196 tframe
->tpnum
= tpoint
->number
;
2197 tframe
->data_size
= 0;
2202 /* Add a block to the traceframe currently being worked on. */
2204 static unsigned char *
2205 add_traceframe_block (struct traceframe
*tframe
,
2206 struct tracepoint
*tpoint
, int amt
)
2208 unsigned char *block
;
2213 block
= (unsigned char *) trace_buffer_alloc (amt
);
2218 gdb_assert (tframe
->tpnum
== tpoint
->number
);
2220 tframe
->data_size
+= amt
;
2221 tpoint
->traceframe_usage
+= amt
;
2226 /* Flag that the current traceframe is finished. */
2229 finish_traceframe (struct traceframe
*tframe
)
2231 ++traceframe_write_count
;
2232 ++traceframes_created
;
2235 #ifndef IN_PROCESS_AGENT
2237 /* Given a traceframe number NUM, find the NUMth traceframe in the
2240 static struct traceframe
*
2241 find_traceframe (int num
)
2243 struct traceframe
*tframe
;
2246 for (tframe
= FIRST_TRACEFRAME ();
2248 tframe
= NEXT_TRACEFRAME (tframe
))
2259 get_traceframe_address (struct traceframe
*tframe
)
2262 struct tracepoint
*tpoint
;
2264 addr
= traceframe_get_pc (tframe
);
2269 /* Fallback strategy, will be incorrect for while-stepping frames
2270 and multi-location tracepoints. */
2271 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2272 return tpoint
->address
;
2275 /* Search for the next traceframe whose address is inside or outside
2278 static struct traceframe
*
2279 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2282 struct traceframe
*tframe
;
2285 *tfnump
= current_traceframe
+ 1;
2286 tframe
= find_traceframe (*tfnump
);
2287 /* The search is not supposed to wrap around. */
2294 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2296 tfaddr
= get_traceframe_address (tframe
);
2298 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2299 : (lo
> tfaddr
|| tfaddr
> hi
))
2308 /* Search for the next traceframe recorded by the given tracepoint.
2309 Note that for multi-location tracepoints, this will find whatever
2310 location appears first. */
2312 static struct traceframe
*
2313 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2315 struct traceframe
*tframe
;
2317 *tfnump
= current_traceframe
+ 1;
2318 tframe
= find_traceframe (*tfnump
);
2319 /* The search is not supposed to wrap around. */
2326 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2328 if (tframe
->tpnum
== num
)
2339 #ifndef IN_PROCESS_AGENT
2341 /* Clear all past trace state. */
2344 cmd_qtinit (char *packet
)
2346 struct trace_state_variable
*tsv
, *prev
, *next
;
2348 /* Can't do this command without a pid attached. */
2349 if (current_thread
== NULL
)
2355 /* Make sure we don't try to read from a trace frame. */
2356 current_traceframe
= -1;
2360 trace_debug ("Initializing the trace");
2362 clear_installed_tracepoints ();
2363 clear_readonly_regions ();
2366 last_tracepoint
= NULL
;
2368 /* Clear out any leftover trace state variables. Ones with target
2369 defined getters should be kept however. */
2371 tsv
= trace_state_variables
;
2374 trace_debug ("Looking at var %d", tsv
->number
);
2375 if (tsv
->getter
== NULL
)
2381 trace_state_variables
= next
;
2382 trace_debug ("Deleting var %d", tsv
->number
);
2393 clear_trace_buffer ();
2394 clear_inferior_trace_buffer ();
2399 /* Unprobe the UST marker at ADDRESS. */
2402 unprobe_marker_at (CORE_ADDR address
)
2404 char cmd
[IPA_CMD_BUF_SIZE
];
2406 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2407 run_inferior_command (cmd
, strlen (cmd
) + 1);
2410 /* Restore the program to its pre-tracing state. This routine may be called
2411 in error situations, so it needs to be careful about only restoring
2412 from known-valid bits. */
2415 clear_installed_tracepoints (void)
2417 struct tracepoint
*tpoint
;
2418 struct tracepoint
*prev_stpoint
;
2422 prev_stpoint
= NULL
;
2424 /* Restore any bytes overwritten by tracepoints. */
2425 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2427 /* Catch the case where we might try to remove a tracepoint that
2428 was never actually installed. */
2429 if (tpoint
->handle
== NULL
)
2431 trace_debug ("Tracepoint %d at 0x%s was "
2432 "never installed, nothing to clear",
2433 tpoint
->number
, paddress (tpoint
->address
));
2437 switch (tpoint
->type
)
2439 case trap_tracepoint
:
2441 struct breakpoint
*bp
2442 = (struct breakpoint
*) tpoint
->handle
;
2444 delete_breakpoint (bp
);
2447 case fast_tracepoint
:
2449 struct fast_tracepoint_jump
*jump
2450 = (struct fast_tracepoint_jump
*) tpoint
->handle
;
2452 delete_fast_tracepoint_jump (jump
);
2455 case static_tracepoint
:
2456 if (prev_stpoint
!= NULL
2457 && prev_stpoint
->address
== tpoint
->address
)
2458 /* Nothing to do. We already unprobed a tracepoint set at
2459 this marker address (and there can only be one probe
2464 unprobe_marker_at (tpoint
->address
);
2465 prev_stpoint
= tpoint
;
2470 tpoint
->handle
= NULL
;
2476 /* Parse a packet that defines a tracepoint. */
2479 cmd_qtdp (char *own_buf
)
2482 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2483 int trail_hyphen
= 0;
2487 struct tracepoint
*tpoint
;
2489 char *packet
= own_buf
;
2491 packet
+= strlen ("QTDP:");
2493 /* A hyphen at the beginning marks a packet specifying actions for a
2494 tracepoint already supplied. */
2501 packet
= unpack_varlen_hex (packet
, &num
);
2502 ++packet
; /* skip a colon */
2503 packet
= unpack_varlen_hex (packet
, &addr
);
2504 ++packet
; /* skip a colon */
2506 /* See if we already have this tracepoint. */
2507 tpoint
= find_tracepoint (num
, addr
);
2511 /* Duplicate tracepoints are never allowed. */
2514 trace_debug ("Tracepoint error: tracepoint %d"
2515 " at 0x%s already exists",
2516 (int) num
, paddress (addr
));
2517 write_enn (own_buf
);
2521 tpoint
= add_tracepoint (num
, addr
);
2523 tpoint
->enabled
= (*packet
== 'E');
2524 ++packet
; /* skip 'E' */
2525 ++packet
; /* skip a colon */
2526 packet
= unpack_varlen_hex (packet
, &count
);
2527 tpoint
->step_count
= count
;
2528 ++packet
; /* skip a colon */
2529 packet
= unpack_varlen_hex (packet
, &count
);
2530 tpoint
->pass_count
= count
;
2531 /* See if we have any of the additional optional fields. */
2532 while (*packet
== ':')
2537 tpoint
->type
= fast_tracepoint
;
2539 packet
= unpack_varlen_hex (packet
, &count
);
2540 tpoint
->orig_size
= count
;
2542 else if (*packet
== 'S')
2544 tpoint
->type
= static_tracepoint
;
2547 else if (*packet
== 'X')
2549 actparm
= (char *) packet
;
2550 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2553 else if (*packet
== '-')
2555 else if (*packet
== '\0')
2558 trace_debug ("Unknown optional tracepoint field");
2563 trace_debug ("Also has actions\n");
2566 trace_debug ("Defined %stracepoint %d at 0x%s, "
2567 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2568 tpoint
->type
== fast_tracepoint
? "fast "
2569 : tpoint
->type
== static_tracepoint
? "static " : "",
2570 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2571 tpoint
->step_count
, tpoint
->pass_count
);
2574 add_tracepoint_action (tpoint
, packet
);
2577 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2578 (int) num
, paddress (addr
));
2579 write_enn (own_buf
);
2583 /* Install tracepoint during tracing only once for each tracepoint location.
2584 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2585 determine the last QTDP packet for one tracepoint location by checking
2586 trailing hyphen in QTDP packet. */
2587 if (tracing
&& !trail_hyphen
)
2589 struct tracepoint
*tp
= NULL
;
2591 /* Pause all threads temporarily while we patch tracepoints. */
2594 /* download_tracepoint will update global `tracepoints'
2595 list, so it is unsafe to leave threads in jump pad. */
2596 stabilize_threads ();
2598 /* Freeze threads. */
2602 if (tpoint
->type
!= trap_tracepoint
)
2604 /* Find another fast or static tracepoint at the same address. */
2605 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2607 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2608 && tp
->number
!= tpoint
->number
)
2612 /* TPOINT is installed at the same address as TP. */
2615 if (tpoint
->type
== fast_tracepoint
)
2616 clone_fast_tracepoint (tpoint
, tp
);
2617 else if (tpoint
->type
== static_tracepoint
)
2618 tpoint
->handle
= (void *) -1;
2622 if (use_agent
&& tpoint
->type
== fast_tracepoint
2623 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2625 /* Download and install fast tracepoint by agent. */
2626 if (tracepoint_send_agent (tpoint
) == 0)
2630 write_enn (own_buf
);
2631 remove_tracepoint (tpoint
);
2636 download_tracepoint (tpoint
);
2638 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2640 install_tracepoint (tpoint
, own_buf
);
2641 if (strcmp (own_buf
, "OK") != 0)
2642 remove_tracepoint (tpoint
);
2656 cmd_qtdpsrc (char *own_buf
)
2658 ULONGEST num
, addr
, start
, slen
;
2659 struct tracepoint
*tpoint
;
2660 char *packet
= own_buf
;
2661 char *saved
, *srctype
, *src
;
2663 struct source_string
*last
, *newlast
;
2665 packet
+= strlen ("QTDPsrc:");
2667 packet
= unpack_varlen_hex (packet
, &num
);
2668 ++packet
; /* skip a colon */
2669 packet
= unpack_varlen_hex (packet
, &addr
);
2670 ++packet
; /* skip a colon */
2672 /* See if we already have this tracepoint. */
2673 tpoint
= find_tracepoint (num
, addr
);
2677 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2678 (int) num
, paddress (addr
));
2679 write_enn (own_buf
);
2684 packet
= strchr (packet
, ':');
2685 srctype
= (char *) xmalloc (packet
- saved
+ 1);
2686 memcpy (srctype
, saved
, packet
- saved
);
2687 srctype
[packet
- saved
] = '\0';
2689 packet
= unpack_varlen_hex (packet
, &start
);
2690 ++packet
; /* skip a colon */
2691 packet
= unpack_varlen_hex (packet
, &slen
);
2692 ++packet
; /* skip a colon */
2693 src
= (char *) xmalloc (slen
+ 1);
2694 nbytes
= hex2bin (packet
, (gdb_byte
*) src
, strlen (packet
) / 2);
2697 newlast
= XNEW (struct source_string
);
2698 newlast
->type
= srctype
;
2700 newlast
->next
= NULL
;
2701 /* Always add a source string to the end of the list;
2702 this keeps sequences of actions/commands in the right
2704 if (tpoint
->source_strings
)
2706 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2708 last
->next
= newlast
;
2711 tpoint
->source_strings
= newlast
;
2717 cmd_qtdv (char *own_buf
)
2719 ULONGEST num
, val
, builtin
;
2722 struct trace_state_variable
*tsv
;
2723 char *packet
= own_buf
;
2725 packet
+= strlen ("QTDV:");
2727 packet
= unpack_varlen_hex (packet
, &num
);
2728 ++packet
; /* skip a colon */
2729 packet
= unpack_varlen_hex (packet
, &val
);
2730 ++packet
; /* skip a colon */
2731 packet
= unpack_varlen_hex (packet
, &builtin
);
2732 ++packet
; /* skip a colon */
2734 nbytes
= strlen (packet
) / 2;
2735 varname
= (char *) xmalloc (nbytes
+ 1);
2736 nbytes
= hex2bin (packet
, (gdb_byte
*) varname
, nbytes
);
2737 varname
[nbytes
] = '\0';
2739 tsv
= create_trace_state_variable (num
, 1);
2740 tsv
->initial_value
= (LONGEST
) val
;
2741 tsv
->name
= varname
;
2743 set_trace_state_variable_value (num
, (LONGEST
) val
);
2749 cmd_qtenable_disable (char *own_buf
, int enable
)
2751 char *packet
= own_buf
;
2753 struct tracepoint
*tp
;
2755 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2756 packet
= unpack_varlen_hex (packet
, &num
);
2757 ++packet
; /* skip a colon */
2758 packet
= unpack_varlen_hex (packet
, &addr
);
2760 tp
= find_tracepoint (num
, addr
);
2764 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2766 trace_debug ("Tracepoint %d at 0x%s is already %s",
2767 (int) num
, paddress (addr
),
2768 enable
? "enabled" : "disabled");
2773 trace_debug ("%s tracepoint %d at 0x%s",
2774 enable
? "Enabling" : "Disabling",
2775 (int) num
, paddress (addr
));
2777 tp
->enabled
= enable
;
2779 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2782 int offset
= offsetof (struct tracepoint
, enabled
);
2783 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2785 ret
= prepare_to_access_memory ();
2788 trace_debug ("Failed to temporarily stop inferior threads");
2789 write_enn (own_buf
);
2793 ret
= write_inferior_int8 (obj_addr
, enable
);
2794 done_accessing_memory ();
2798 trace_debug ("Cannot write enabled flag into "
2799 "inferior process memory");
2800 write_enn (own_buf
);
2809 trace_debug ("Tracepoint %d at 0x%s not found",
2810 (int) num
, paddress (addr
));
2811 write_enn (own_buf
);
2816 cmd_qtv (char *own_buf
)
2821 char *packet
= own_buf
;
2823 packet
+= strlen ("qTV:");
2824 unpack_varlen_hex (packet
, &num
);
2826 if (current_traceframe
>= 0)
2828 err
= traceframe_read_tsv ((int) num
, &val
);
2831 strcpy (own_buf
, "U");
2835 /* Only make tsv's be undefined before the first trace run. After a
2836 trace run is over, the user might want to see the last value of
2837 the tsv, and it might not be available in a traceframe. */
2838 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2840 strcpy (own_buf
, "U");
2844 val
= get_trace_state_variable_value (num
);
2846 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2849 /* Clear out the list of readonly regions. */
2852 clear_readonly_regions (void)
2854 struct readonly_region
*roreg
;
2856 while (readonly_regions
)
2858 roreg
= readonly_regions
;
2859 readonly_regions
= readonly_regions
->next
;
2864 /* Parse the collection of address ranges whose contents GDB believes
2865 to be unchanging and so can be read directly from target memory
2866 even while looking at a traceframe. */
2869 cmd_qtro (char *own_buf
)
2871 ULONGEST start
, end
;
2872 struct readonly_region
*roreg
;
2873 char *packet
= own_buf
;
2875 trace_debug ("Want to mark readonly regions");
2877 clear_readonly_regions ();
2879 packet
+= strlen ("QTro");
2881 while (*packet
== ':')
2883 ++packet
; /* skip a colon */
2884 packet
= unpack_varlen_hex (packet
, &start
);
2885 ++packet
; /* skip a comma */
2886 packet
= unpack_varlen_hex (packet
, &end
);
2888 roreg
= XNEW (struct readonly_region
);
2889 roreg
->start
= start
;
2891 roreg
->next
= readonly_regions
;
2892 readonly_regions
= roreg
;
2893 trace_debug ("Added readonly region from 0x%s to 0x%s",
2894 paddress (roreg
->start
), paddress (roreg
->end
));
2900 /* Test to see if the given range is in our list of readonly ranges.
2901 We only test for being entirely within a range, GDB is not going to
2902 send a single memory packet that spans multiple regions. */
2905 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2907 struct readonly_region
*roreg
;
2909 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2910 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2916 static CORE_ADDR gdb_jump_pad_head
;
2918 /* Return the address of the next free jump space. */
2921 get_jump_space_head (void)
2923 if (gdb_jump_pad_head
== 0)
2925 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2926 &gdb_jump_pad_head
))
2928 internal_error (__FILE__
, __LINE__
,
2929 "error extracting jump_pad_buffer");
2933 return gdb_jump_pad_head
;
2936 /* Reserve USED bytes from the jump space. */
2939 claim_jump_space (ULONGEST used
)
2941 trace_debug ("claim_jump_space reserves %s bytes at %s",
2942 pulongest (used
), paddress (gdb_jump_pad_head
));
2943 gdb_jump_pad_head
+= used
;
2946 static CORE_ADDR trampoline_buffer_head
= 0;
2947 static CORE_ADDR trampoline_buffer_tail
;
2949 /* Reserve USED bytes from the trampoline buffer and return the
2950 address of the start of the reserved space in TRAMPOLINE. Returns
2951 non-zero if the space is successfully claimed. */
2954 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2956 if (!trampoline_buffer_head
)
2958 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2959 &trampoline_buffer_tail
))
2961 internal_error (__FILE__
, __LINE__
,
2962 "error extracting trampoline_buffer");
2965 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2966 &trampoline_buffer_head
))
2968 internal_error (__FILE__
, __LINE__
,
2969 "error extracting trampoline_buffer_end");
2973 /* Start claiming space from the top of the trampoline space. If
2974 the space is located at the bottom of the virtual address space,
2975 this reduces the possibility that corruption will occur if a null
2976 pointer is used to write to memory. */
2977 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2979 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2984 trampoline_buffer_head
-= used
;
2986 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2987 pulongest (used
), paddress (trampoline_buffer_head
));
2989 *trampoline
= trampoline_buffer_head
;
2993 /* Returns non-zero if there is space allocated for use in trampolines
2994 for fast tracepoints. */
2997 have_fast_tracepoint_trampoline_buffer (char *buf
)
2999 CORE_ADDR trampoline_end
, errbuf
;
3001 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
3004 internal_error (__FILE__
, __LINE__
,
3005 "error extracting trampoline_buffer_end");
3011 strcpy (buf
, "was claiming");
3012 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3015 internal_error (__FILE__
, __LINE__
,
3016 "error extracting errbuf");
3019 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3022 return trampoline_end
!= 0;
3025 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3026 the command fails, or 0 otherwise. If the command ran
3027 successfully, but probing the marker failed, ERROUT will be filled
3028 with the error to reply to GDB, and -1 is also returned. This
3029 allows directly passing IPA errors to GDB. */
3032 probe_marker_at (CORE_ADDR address
, char *errout
)
3034 char cmd
[IPA_CMD_BUF_SIZE
];
3037 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3038 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3044 strcpy (errout
, cmd
);
3053 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3055 to
->jump_pad
= from
->jump_pad
;
3056 to
->jump_pad_end
= from
->jump_pad_end
;
3057 to
->trampoline
= from
->trampoline
;
3058 to
->trampoline_end
= from
->trampoline_end
;
3059 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3060 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3061 to
->handle
= from
->handle
;
3063 gdb_assert (from
->handle
);
3064 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3067 #define MAX_JUMP_SIZE 20
3069 /* Install fast tracepoint. Return 0 if successful, otherwise return
3073 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3075 CORE_ADDR jentry
, jump_entry
;
3076 CORE_ADDR trampoline
;
3078 ULONGEST trampoline_size
;
3080 /* The jump to the jump pad of the last fast tracepoint
3082 unsigned char fjump
[MAX_JUMP_SIZE
];
3083 ULONGEST fjump_size
;
3085 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3087 trace_debug ("Requested a fast tracepoint on an instruction "
3088 "that is of less than the minimum length.");
3092 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_collect_ptr
,
3095 error ("error extracting gdb_collect_ptr");
3099 jentry
= jump_entry
= get_jump_space_head ();
3102 trampoline_size
= 0;
3104 /* Install the jump pad. */
3105 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3108 ipa_sym_addrs
.addr_collecting
,
3111 &trampoline
, &trampoline_size
,
3113 &tpoint
->adjusted_insn_addr
,
3114 &tpoint
->adjusted_insn_addr_end
,
3121 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3124 if (tpoint
->handle
!= NULL
)
3126 tpoint
->jump_pad
= jump_entry
;
3127 tpoint
->jump_pad_end
= jentry
;
3128 tpoint
->trampoline
= trampoline
;
3129 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3131 /* Pad to 8-byte alignment. */
3132 jentry
= ((jentry
+ 7) & ~0x7);
3133 claim_jump_space (jentry
- jump_entry
);
3140 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3143 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3145 tpoint
->handle
= NULL
;
3148 if (tpoint
->type
== trap_tracepoint
)
3150 /* Tracepoints are installed as memory breakpoints. Just go
3151 ahead and install the trap. The breakpoints module
3152 handles duplicated breakpoints, and the memory read
3153 routine handles un-patching traps from memory reads. */
3154 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3155 tracepoint_handler
);
3157 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3159 if (!agent_loaded_p ())
3161 trace_debug ("Requested a %s tracepoint, but fast "
3162 "tracepoints aren't supported.",
3163 tpoint
->type
== static_tracepoint
? "static" : "fast");
3164 write_e_ipa_not_loaded (own_buf
);
3167 if (tpoint
->type
== static_tracepoint
3168 && !in_process_agent_supports_ust ())
3170 trace_debug ("Requested a static tracepoint, but static "
3171 "tracepoints are not supported.");
3172 write_e_ust_not_loaded (own_buf
);
3176 if (tpoint
->type
== fast_tracepoint
)
3177 install_fast_tracepoint (tpoint
, own_buf
);
3180 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3181 tpoint
->handle
= (void *) -1;
3186 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3188 if (tpoint
->handle
== NULL
)
3190 if (*own_buf
== '\0')
3191 write_enn (own_buf
);
3197 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3200 cmd_qtstart (char *packet
)
3202 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3203 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3205 trace_debug ("Starting the trace");
3207 /* Pause all threads temporarily while we patch tracepoints. */
3210 /* Get threads out of jump pads. Safe to do here, since this is a
3211 top level command. And, required to do here, since we're
3212 deleting/rewriting jump pads. */
3214 stabilize_threads ();
3216 /* Freeze threads. */
3219 /* Sync the fast tracepoints list in the inferior ftlib. */
3220 if (agent_loaded_p ())
3221 download_trace_state_variables ();
3223 /* No previous fast tpoint yet. */
3224 prev_ftpoint
= NULL
;
3226 /* No previous static tpoint yet. */
3227 prev_stpoint
= NULL
;
3231 if (agent_loaded_p ())
3233 /* Tell IPA about the correct tdesc. */
3234 if (write_inferior_integer (ipa_sym_addrs
.addr_ipa_tdesc_idx
,
3235 target_get_ipa_tdesc_idx ()))
3236 error ("Error setting ipa_tdesc_idx variable in lib");
3239 /* Start out empty. */
3240 if (agent_loaded_p ())
3241 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
, 0);
3243 /* Download and install tracepoints. */
3244 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3246 /* Ensure all the hit counts start at zero. */
3247 tpoint
->hit_count
= 0;
3248 tpoint
->traceframe_usage
= 0;
3250 if (tpoint
->type
== trap_tracepoint
)
3252 /* Tracepoints are installed as memory breakpoints. Just go
3253 ahead and install the trap. The breakpoints module
3254 handles duplicated breakpoints, and the memory read
3255 routine handles un-patching traps from memory reads. */
3256 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3257 tracepoint_handler
);
3259 else if (tpoint
->type
== fast_tracepoint
3260 || tpoint
->type
== static_tracepoint
)
3262 if (maybe_write_ipa_not_loaded (packet
))
3264 trace_debug ("Requested a %s tracepoint, but fast "
3265 "tracepoints aren't supported.",
3266 tpoint
->type
== static_tracepoint
3267 ? "static" : "fast");
3271 if (tpoint
->type
== fast_tracepoint
)
3274 = use_agent
&& agent_capability_check (AGENT_CAPA_FAST_TRACE
);
3276 if (prev_ftpoint
!= NULL
3277 && prev_ftpoint
->address
== tpoint
->address
)
3280 tracepoint_send_agent (tpoint
);
3282 download_tracepoint_1 (tpoint
);
3284 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3288 /* Tracepoint is installed successfully? */
3291 /* Download and install fast tracepoint by agent. */
3293 installed
= !tracepoint_send_agent (tpoint
);
3296 download_tracepoint_1 (tpoint
);
3297 installed
= !install_fast_tracepoint (tpoint
, packet
);
3301 prev_ftpoint
= tpoint
;
3306 if (!in_process_agent_supports_ust ())
3308 trace_debug ("Requested a static tracepoint, but static "
3309 "tracepoints are not supported.");
3313 download_tracepoint_1 (tpoint
);
3314 /* Can only probe a given marker once. */
3315 if (prev_stpoint
!= NULL
3316 && prev_stpoint
->address
== tpoint
->address
)
3317 tpoint
->handle
= (void *) -1;
3320 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3322 tpoint
->handle
= (void *) -1;
3324 /* So that we can handle multiple static tracepoints
3325 at the same address easily. */
3326 prev_stpoint
= tpoint
;
3332 tpptr
= tpoint
->obj_addr_on_target
;
3334 if (tpoint
== tracepoints
)
3335 /* First object in list, set the head pointer in the
3337 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3339 write_inferior_data_pointer (prev_tpptr
3340 + offsetof (struct tracepoint
, next
),
3344 /* Any failure in the inner loop is sufficient cause to give
3346 if (tpoint
->handle
== NULL
)
3350 /* Any error in tracepoint insertion is unacceptable; better to
3351 address the problem now, than end up with a useless or misleading
3355 clear_installed_tracepoints ();
3356 if (*packet
== '\0')
3362 stopping_tracepoint
= NULL
;
3363 trace_buffer_is_full
= 0;
3364 expr_eval_result
= expr_eval_no_error
;
3365 error_tracepoint
= NULL
;
3366 tracing_start_time
= get_timestamp ();
3368 /* Tracing is now active, hits will now start being logged. */
3371 if (agent_loaded_p ())
3373 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3375 internal_error (__FILE__
, __LINE__
,
3376 "Error setting tracing variable in lib");
3379 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3382 internal_error (__FILE__
, __LINE__
,
3383 "Error clearing stopping_tracepoint variable"
3387 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3389 internal_error (__FILE__
, __LINE__
,
3390 "Error clearing trace_buffer_is_full variable"
3394 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3395 stop_tracing_handler
);
3396 if (stop_tracing_bkpt
== NULL
)
3397 error ("Error setting stop_tracing breakpoint");
3399 flush_trace_buffer_bkpt
3400 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3401 flush_trace_buffer_handler
);
3402 if (flush_trace_buffer_bkpt
== NULL
)
3403 error ("Error setting flush_trace_buffer breakpoint");
3411 /* End a tracing run, filling in a stop reason to report back to GDB,
3412 and removing the tracepoints from the code. */
3419 trace_debug ("Tracing is already off, ignoring");
3423 trace_debug ("Stopping the trace");
3425 /* Pause all threads before removing fast jumps from memory,
3426 breakpoints, and touching IPA state variables (inferior memory).
3427 Some thread may hit the internal tracing breakpoints, or be
3428 collecting this moment, but that's ok, we don't release the
3429 tpoint object's memory or the jump pads here (we only do that
3430 when we're sure we can move all threads out of the jump pads).
3431 We can't now, since we may be getting here due to the inferior
3432 agent calling us. */
3435 /* Stop logging. Tracepoints can still be hit, but they will not be
3438 if (agent_loaded_p ())
3440 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3442 internal_error (__FILE__
, __LINE__
,
3443 "Error clearing tracing variable in lib");
3447 tracing_stop_time
= get_timestamp ();
3448 tracing_stop_reason
= "t???";
3449 tracing_stop_tpnum
= 0;
3450 if (stopping_tracepoint
)
3452 trace_debug ("Stopping the trace because "
3453 "tracepoint %d was hit %" PRIu64
" times",
3454 stopping_tracepoint
->number
,
3455 stopping_tracepoint
->pass_count
);
3456 tracing_stop_reason
= "tpasscount";
3457 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3459 else if (trace_buffer_is_full
)
3461 trace_debug ("Stopping the trace because the trace buffer is full");
3462 tracing_stop_reason
= "tfull";
3464 else if (expr_eval_result
!= expr_eval_no_error
)
3466 trace_debug ("Stopping the trace because of an expression eval error");
3467 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3468 tracing_stop_tpnum
= error_tracepoint
->number
;
3470 #ifndef IN_PROCESS_AGENT
3471 else if (!gdb_connected ())
3473 trace_debug ("Stopping the trace because GDB disconnected");
3474 tracing_stop_reason
= "tdisconnected";
3479 trace_debug ("Stopping the trace because of a tstop command");
3480 tracing_stop_reason
= "tstop";
3483 stopping_tracepoint
= NULL
;
3484 error_tracepoint
= NULL
;
3486 /* Clear out the tracepoints. */
3487 clear_installed_tracepoints ();
3489 if (agent_loaded_p ())
3491 /* Pull in fast tracepoint trace frames from the inferior lib
3492 buffer into our buffer, even if our buffer is already full,
3493 because we want to present the full number of created frames
3494 in addition to what fit in the trace buffer. */
3495 upload_fast_traceframes ();
3498 if (stop_tracing_bkpt
!= NULL
)
3500 delete_breakpoint (stop_tracing_bkpt
);
3501 stop_tracing_bkpt
= NULL
;
3504 if (flush_trace_buffer_bkpt
!= NULL
)
3506 delete_breakpoint (flush_trace_buffer_bkpt
);
3507 flush_trace_buffer_bkpt
= NULL
;
3514 stop_tracing_handler (CORE_ADDR addr
)
3516 trace_debug ("lib hit stop_tracing");
3518 /* Don't actually handle it here. When we stop tracing we remove
3519 breakpoints from the inferior, and that is not allowed in a
3520 breakpoint handler (as the caller is walking the breakpoint
3526 flush_trace_buffer_handler (CORE_ADDR addr
)
3528 trace_debug ("lib hit flush_trace_buffer");
3533 cmd_qtstop (char *packet
)
3540 cmd_qtdisconnected (char *own_buf
)
3543 char *packet
= own_buf
;
3545 packet
+= strlen ("QTDisconnected:");
3547 unpack_varlen_hex (packet
, &setting
);
3551 disconnected_tracing
= setting
;
3555 cmd_qtframe (char *own_buf
)
3557 ULONGEST frame
, pc
, lo
, hi
, num
;
3559 struct traceframe
*tframe
;
3560 char *packet
= own_buf
;
3562 packet
+= strlen ("QTFrame:");
3564 if (startswith (packet
, "pc:"))
3566 packet
+= strlen ("pc:");
3567 unpack_varlen_hex (packet
, &pc
);
3568 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3569 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3571 else if (startswith (packet
, "range:"))
3573 packet
+= strlen ("range:");
3574 packet
= unpack_varlen_hex (packet
, &lo
);
3576 unpack_varlen_hex (packet
, &hi
);
3577 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3578 paddress (lo
), paddress (hi
));
3579 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3581 else if (startswith (packet
, "outside:"))
3583 packet
+= strlen ("outside:");
3584 packet
= unpack_varlen_hex (packet
, &lo
);
3586 unpack_varlen_hex (packet
, &hi
);
3587 trace_debug ("Want to find next traceframe "
3588 "outside the range 0x%s to 0x%s",
3589 paddress (lo
), paddress (hi
));
3590 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3592 else if (startswith (packet
, "tdp:"))
3594 packet
+= strlen ("tdp:");
3595 unpack_varlen_hex (packet
, &num
);
3597 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3598 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3602 unpack_varlen_hex (packet
, &frame
);
3603 tfnum
= (int) frame
;
3606 trace_debug ("Want to stop looking at traceframes");
3607 current_traceframe
= -1;
3611 trace_debug ("Want to look at traceframe %d", tfnum
);
3612 tframe
= find_traceframe (tfnum
);
3617 current_traceframe
= tfnum
;
3618 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3621 sprintf (own_buf
, "F-1");
3625 cmd_qtstatus (char *packet
)
3627 char *stop_reason_rsp
= NULL
;
3628 char *buf1
, *buf2
, *buf3
;
3632 /* Translate the plain text of the notes back into hex for
3635 str
= (tracing_user_name
? tracing_user_name
: "");
3636 slen
= strlen (str
);
3637 buf1
= (char *) alloca (slen
* 2 + 1);
3638 bin2hex ((gdb_byte
*) str
, buf1
, slen
);
3640 str
= (tracing_notes
? tracing_notes
: "");
3641 slen
= strlen (str
);
3642 buf2
= (char *) alloca (slen
* 2 + 1);
3643 bin2hex ((gdb_byte
*) str
, buf2
, slen
);
3645 str
= (tracing_stop_note
? tracing_stop_note
: "");
3646 slen
= strlen (str
);
3647 buf3
= (char *) alloca (slen
* 2 + 1);
3648 bin2hex ((gdb_byte
*) str
, buf3
, slen
);
3650 trace_debug ("Returning trace status as %d, stop reason %s",
3651 tracing
, tracing_stop_reason
);
3653 if (agent_loaded_p ())
3657 upload_fast_traceframes ();
3662 stop_reason_rsp
= (char *) tracing_stop_reason
;
3664 /* The user visible error string in terror needs to be hex encoded.
3665 We leave it as plain string in `tracing_stop_reason' to ease
3667 if (startswith (stop_reason_rsp
, "terror:"))
3669 const char *result_name
;
3673 result_name
= stop_reason_rsp
+ strlen ("terror:");
3674 hexstr_len
= strlen (result_name
) * 2;
3676 = (char *) alloca (strlen ("terror:") + hexstr_len
+ 1);
3677 strcpy (p
, "terror:");
3679 bin2hex ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3682 /* If this was a forced stop, include any stop note that was supplied. */
3683 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3685 stop_reason_rsp
= (char *) alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3686 strcpy (stop_reason_rsp
, "tstop:");
3687 strcat (stop_reason_rsp
, buf3
);
3693 "tframes:%x;tcreated:%x;"
3694 "tfree:%x;tsize:%s;"
3697 "starttime:%s;stoptime:%s;"
3698 "username:%s;notes:%s:",
3700 stop_reason_rsp
, tracing_stop_tpnum
,
3701 traceframe_count
, traceframes_created
,
3702 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3703 circular_trace_buffer
,
3704 disconnected_tracing
,
3705 phex_nz (tracing_start_time
, sizeof (tracing_start_time
)),
3706 phex_nz (tracing_stop_time
, sizeof (tracing_stop_time
)),
3711 cmd_qtp (char *own_buf
)
3714 struct tracepoint
*tpoint
;
3715 char *packet
= own_buf
;
3717 packet
+= strlen ("qTP:");
3719 packet
= unpack_varlen_hex (packet
, &num
);
3720 ++packet
; /* skip a colon */
3721 packet
= unpack_varlen_hex (packet
, &addr
);
3723 /* See if we already have this tracepoint. */
3724 tpoint
= find_tracepoint (num
, addr
);
3728 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3729 (int) num
, paddress (addr
));
3730 write_enn (own_buf
);
3734 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3735 tpoint
->traceframe_usage
);
3738 /* State variables to help return all the tracepoint bits. */
3739 static struct tracepoint
*cur_tpoint
;
3740 static unsigned int cur_action
;
3741 static unsigned int cur_step_action
;
3742 static struct source_string
*cur_source_string
;
3743 static struct trace_state_variable
*cur_tsv
;
3745 /* Compose a response that is an imitation of the syntax by which the
3746 tracepoint was originally downloaded. */
3749 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3753 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3754 paddress (tpoint
->address
),
3755 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3756 tpoint
->pass_count
);
3757 if (tpoint
->type
== fast_tracepoint
)
3758 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3759 else if (tpoint
->type
== static_tracepoint
)
3760 sprintf (packet
+ strlen (packet
), ":S");
3764 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3765 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3766 tpoint
->cond
->length
, buf
);
3771 /* Compose a response that is an imitation of the syntax by which the
3772 tracepoint action was originally downloaded (with the difference
3773 that due to the way we store the actions, this will output a packet
3774 per action, while GDB could have combined more than one action
3778 response_action (char *packet
, struct tracepoint
*tpoint
,
3779 char *taction
, int step
)
3781 sprintf (packet
, "%c%x:%s:%s",
3782 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3786 /* Compose a response that is an imitation of the syntax by which the
3787 tracepoint source piece was originally downloaded. */
3790 response_source (char *packet
,
3791 struct tracepoint
*tpoint
, struct source_string
*src
)
3796 len
= strlen (src
->str
);
3797 buf
= (char *) alloca (len
* 2 + 1);
3798 bin2hex ((gdb_byte
*) src
->str
, buf
, len
);
3800 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3801 tpoint
->number
, paddress (tpoint
->address
),
3802 src
->type
, 0, len
, buf
);
3805 /* Return the first piece of tracepoint definition, and initialize the
3806 state machine that will iterate through all the tracepoint
3810 cmd_qtfp (char *packet
)
3812 trace_debug ("Returning first tracepoint definition piece");
3814 cur_tpoint
= tracepoints
;
3815 cur_action
= cur_step_action
= 0;
3816 cur_source_string
= NULL
;
3819 response_tracepoint (packet
, cur_tpoint
);
3821 strcpy (packet
, "l");
3824 /* Return additional pieces of tracepoint definition. Each action and
3825 stepping action must go into its own packet, because of packet size
3826 limits, and so we use state variables to deliver one piece at a
3830 cmd_qtsp (char *packet
)
3832 trace_debug ("Returning subsequent tracepoint definition piece");
3836 /* This case would normally never occur, but be prepared for
3838 strcpy (packet
, "l");
3840 else if (cur_action
< cur_tpoint
->numactions
)
3842 response_action (packet
, cur_tpoint
,
3843 cur_tpoint
->actions_str
[cur_action
], 0);
3846 else if (cur_step_action
< cur_tpoint
->num_step_actions
)
3848 response_action (packet
, cur_tpoint
,
3849 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3852 else if ((cur_source_string
3853 ? cur_source_string
->next
3854 : cur_tpoint
->source_strings
))
3856 if (cur_source_string
)
3857 cur_source_string
= cur_source_string
->next
;
3859 cur_source_string
= cur_tpoint
->source_strings
;
3860 response_source (packet
, cur_tpoint
, cur_source_string
);
3864 cur_tpoint
= cur_tpoint
->next
;
3865 cur_action
= cur_step_action
= 0;
3866 cur_source_string
= NULL
;
3868 response_tracepoint (packet
, cur_tpoint
);
3870 strcpy (packet
, "l");
3874 /* Compose a response that is an imitation of the syntax by which the
3875 trace state variable was originally downloaded. */
3878 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3880 char *buf
= (char *) "";
3885 namelen
= strlen (tsv
->name
);
3886 buf
= (char *) alloca (namelen
* 2 + 1);
3887 bin2hex ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3890 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3891 tsv
->getter
? 1 : 0, buf
);
3894 /* Return the first trace state variable definition, and initialize
3895 the state machine that will iterate through all the tsv bits. */
3898 cmd_qtfv (char *packet
)
3900 trace_debug ("Returning first trace state variable definition");
3902 cur_tsv
= trace_state_variables
;
3905 response_tsv (packet
, cur_tsv
);
3907 strcpy (packet
, "l");
3910 /* Return additional trace state variable definitions. */
3913 cmd_qtsv (char *packet
)
3915 trace_debug ("Returning additional trace state variable definition");
3919 cur_tsv
= cur_tsv
->next
;
3921 response_tsv (packet
, cur_tsv
);
3923 strcpy (packet
, "l");
3926 strcpy (packet
, "l");
3929 /* Return the first static tracepoint marker, and initialize the state
3930 machine that will iterate through all the static tracepoints
3934 cmd_qtfstm (char *packet
)
3936 if (!maybe_write_ipa_ust_not_loaded (packet
))
3937 run_inferior_command (packet
, strlen (packet
) + 1);
3940 /* Return additional static tracepoints markers. */
3943 cmd_qtsstm (char *packet
)
3945 if (!maybe_write_ipa_ust_not_loaded (packet
))
3946 run_inferior_command (packet
, strlen (packet
) + 1);
3949 /* Return the definition of the static tracepoint at a given address.
3950 Result packet is the same as qTsST's. */
3953 cmd_qtstmat (char *packet
)
3955 if (!maybe_write_ipa_ust_not_loaded (packet
))
3956 run_inferior_command (packet
, strlen (packet
) + 1);
3959 /* Helper for gdb_agent_about_to_close.
3960 Return non-zero if thread ENTRY is in the same process in DATA. */
3963 same_process_p (struct inferior_list_entry
*entry
, void *data
)
3965 int *pid
= (int *) data
;
3967 return ptid_get_pid (entry
->id
) == *pid
;
3970 /* Sent the agent a command to close it. */
3973 gdb_agent_about_to_close (int pid
)
3975 char buf
[IPA_CMD_BUF_SIZE
];
3977 if (!maybe_write_ipa_not_loaded (buf
))
3979 struct thread_info
*saved_thread
;
3981 saved_thread
= current_thread
;
3983 /* Find any thread which belongs to process PID. */
3984 current_thread
= (struct thread_info
*)
3985 find_inferior (&all_threads
, same_process_p
, &pid
);
3987 strcpy (buf
, "close");
3989 run_inferior_command (buf
, strlen (buf
) + 1);
3991 current_thread
= saved_thread
;
3995 /* Return the minimum instruction size needed for fast tracepoints as a
3996 hexadecimal number. */
3999 cmd_qtminftpilen (char *packet
)
4001 if (current_thread
== NULL
)
4003 /* Indicate that the minimum length is currently unknown. */
4004 strcpy (packet
, "0");
4008 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
4011 /* Respond to qTBuffer packet with a block of raw data from the trace
4012 buffer. GDB may ask for a lot, but we are allowed to reply with
4013 only as much as will fit within packet limits or whatever. */
4016 cmd_qtbuffer (char *own_buf
)
4018 ULONGEST offset
, num
, tot
;
4020 char *packet
= own_buf
;
4022 packet
+= strlen ("qTBuffer:");
4024 packet
= unpack_varlen_hex (packet
, &offset
);
4025 ++packet
; /* skip a comma */
4026 unpack_varlen_hex (packet
, &num
);
4028 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
4029 (int) num
, phex_nz (offset
, 0));
4031 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
4033 /* If we're right at the end, reply specially that we're done. */
4036 strcpy (own_buf
, "l");
4040 /* Object to any other out-of-bounds request. */
4043 write_enn (own_buf
);
4047 /* Compute the pointer corresponding to the given offset, accounting
4049 tbp
= trace_buffer_start
+ offset
;
4050 if (tbp
>= trace_buffer_wrap
)
4051 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
4053 /* Trim to the remaining bytes if we're close to the end. */
4054 if (num
> tot
- offset
)
4057 /* Trim to available packet size. */
4058 if (num
>= (PBUFSIZ
- 16) / 2 )
4059 num
= (PBUFSIZ
- 16) / 2;
4061 bin2hex (tbp
, own_buf
, num
);
4065 cmd_bigqtbuffer_circular (char *own_buf
)
4068 char *packet
= own_buf
;
4070 packet
+= strlen ("QTBuffer:circular:");
4072 unpack_varlen_hex (packet
, &val
);
4073 circular_trace_buffer
= val
;
4074 trace_debug ("Trace buffer is now %s",
4075 circular_trace_buffer
? "circular" : "linear");
4080 cmd_bigqtbuffer_size (char *own_buf
)
4084 char *packet
= own_buf
;
4086 /* Can't change the size during a tracing run. */
4089 write_enn (own_buf
);
4093 packet
+= strlen ("QTBuffer:size:");
4095 /* -1 is sent as literal "-1". */
4096 if (strcmp (packet
, "-1") == 0)
4097 sval
= DEFAULT_TRACE_BUFFER_SIZE
;
4100 unpack_varlen_hex (packet
, &val
);
4101 sval
= (LONGEST
) val
;
4104 init_trace_buffer (sval
);
4105 trace_debug ("Trace buffer is now %s bytes",
4106 plongest (trace_buffer_size
));
4111 cmd_qtnotes (char *own_buf
)
4114 char *saved
, *user
, *notes
, *stopnote
;
4115 char *packet
= own_buf
;
4117 packet
+= strlen ("QTNotes:");
4121 if (startswith (packet
, "user:"))
4123 packet
+= strlen ("user:");
4125 packet
= strchr (packet
, ';');
4126 nbytes
= (packet
- saved
) / 2;
4127 user
= (char *) xmalloc (nbytes
+ 1);
4128 nbytes
= hex2bin (saved
, (gdb_byte
*) user
, nbytes
);
4129 user
[nbytes
] = '\0';
4130 ++packet
; /* skip the semicolon */
4131 trace_debug ("User is '%s'", user
);
4132 xfree (tracing_user_name
);
4133 tracing_user_name
= user
;
4135 else if (startswith (packet
, "notes:"))
4137 packet
+= strlen ("notes:");
4139 packet
= strchr (packet
, ';');
4140 nbytes
= (packet
- saved
) / 2;
4141 notes
= (char *) xmalloc (nbytes
+ 1);
4142 nbytes
= hex2bin (saved
, (gdb_byte
*) notes
, nbytes
);
4143 notes
[nbytes
] = '\0';
4144 ++packet
; /* skip the semicolon */
4145 trace_debug ("Notes is '%s'", notes
);
4146 xfree (tracing_notes
);
4147 tracing_notes
= notes
;
4149 else if (startswith (packet
, "tstop:"))
4151 packet
+= strlen ("tstop:");
4153 packet
= strchr (packet
, ';');
4154 nbytes
= (packet
- saved
) / 2;
4155 stopnote
= (char *) xmalloc (nbytes
+ 1);
4156 nbytes
= hex2bin (saved
, (gdb_byte
*) stopnote
, nbytes
);
4157 stopnote
[nbytes
] = '\0';
4158 ++packet
; /* skip the semicolon */
4159 trace_debug ("tstop note is '%s'", stopnote
);
4160 xfree (tracing_stop_note
);
4161 tracing_stop_note
= stopnote
;
4171 handle_tracepoint_general_set (char *packet
)
4173 if (strcmp ("QTinit", packet
) == 0)
4175 cmd_qtinit (packet
);
4178 else if (startswith (packet
, "QTDP:"))
4183 else if (startswith (packet
, "QTDPsrc:"))
4185 cmd_qtdpsrc (packet
);
4188 else if (startswith (packet
, "QTEnable:"))
4190 cmd_qtenable_disable (packet
, 1);
4193 else if (startswith (packet
, "QTDisable:"))
4195 cmd_qtenable_disable (packet
, 0);
4198 else if (startswith (packet
, "QTDV:"))
4203 else if (startswith (packet
, "QTro:"))
4208 else if (strcmp ("QTStart", packet
) == 0)
4210 cmd_qtstart (packet
);
4213 else if (strcmp ("QTStop", packet
) == 0)
4215 cmd_qtstop (packet
);
4218 else if (startswith (packet
, "QTDisconnected:"))
4220 cmd_qtdisconnected (packet
);
4223 else if (startswith (packet
, "QTFrame:"))
4225 cmd_qtframe (packet
);
4228 else if (startswith (packet
, "QTBuffer:circular:"))
4230 cmd_bigqtbuffer_circular (packet
);
4233 else if (startswith (packet
, "QTBuffer:size:"))
4235 cmd_bigqtbuffer_size (packet
);
4238 else if (startswith (packet
, "QTNotes:"))
4240 cmd_qtnotes (packet
);
4248 handle_tracepoint_query (char *packet
)
4250 if (strcmp ("qTStatus", packet
) == 0)
4252 cmd_qtstatus (packet
);
4255 else if (startswith (packet
, "qTP:"))
4260 else if (strcmp ("qTfP", packet
) == 0)
4265 else if (strcmp ("qTsP", packet
) == 0)
4270 else if (strcmp ("qTfV", packet
) == 0)
4275 else if (strcmp ("qTsV", packet
) == 0)
4280 else if (startswith (packet
, "qTV:"))
4285 else if (startswith (packet
, "qTBuffer:"))
4287 cmd_qtbuffer (packet
);
4290 else if (strcmp ("qTfSTM", packet
) == 0)
4292 cmd_qtfstm (packet
);
4295 else if (strcmp ("qTsSTM", packet
) == 0)
4297 cmd_qtsstm (packet
);
4300 else if (startswith (packet
, "qTSTMat:"))
4302 cmd_qtstmat (packet
);
4305 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4307 cmd_qtminftpilen (packet
);
4315 #ifndef IN_PROCESS_AGENT
4317 /* Call this when thread TINFO has hit the tracepoint defined by
4318 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4319 action. This adds a while-stepping collecting state item to the
4320 threads' collecting state list, so that we can keep track of
4321 multiple simultaneous while-stepping actions being collected by the
4322 same thread. This can happen in cases like:
4324 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4326 ff0003 INSN3 <-- TP2, collect $regs
4327 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4330 Notice that when instruction INSN5 is reached, the while-stepping
4331 actions of both TP1 and TP3 are still being collected, and that TP2
4332 had been collected meanwhile. The whole range of ff0001-ff0005
4333 should be single-stepped, due to at least TP1's while-stepping
4334 action covering the whole range. */
4337 add_while_stepping_state (struct thread_info
*tinfo
,
4338 int tp_number
, CORE_ADDR tp_address
)
4340 struct wstep_state
*wstep
= XNEW (struct wstep_state
);
4342 wstep
->next
= tinfo
->while_stepping
;
4344 wstep
->tp_number
= tp_number
;
4345 wstep
->tp_address
= tp_address
;
4346 wstep
->current_step
= 0;
4348 tinfo
->while_stepping
= wstep
;
4351 /* Release the while-stepping collecting state WSTEP. */
4354 release_while_stepping_state (struct wstep_state
*wstep
)
4359 /* Release all while-stepping collecting states currently associated
4360 with thread TINFO. */
4363 release_while_stepping_state_list (struct thread_info
*tinfo
)
4365 struct wstep_state
*head
;
4367 while (tinfo
->while_stepping
)
4369 head
= tinfo
->while_stepping
;
4370 tinfo
->while_stepping
= head
->next
;
4371 release_while_stepping_state (head
);
4375 /* If TINFO was handling a 'while-stepping' action, the step has
4376 finished, so collect any step data needed, and check if any more
4377 steps are required. Return true if the thread was indeed
4378 collecting tracepoint data, false otherwise. */
4381 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4383 struct tracepoint
*tpoint
;
4384 struct wstep_state
*wstep
;
4385 struct wstep_state
**wstep_link
;
4386 struct trap_tracepoint_ctx ctx
;
4388 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4390 if (agent_loaded_p ())
4391 upload_fast_traceframes ();
4393 /* Check if we were indeed collecting data for one of more
4394 tracepoints with a 'while-stepping' count. */
4395 if (tinfo
->while_stepping
== NULL
)
4400 /* We're not even tracing anymore. Stop this thread from
4402 release_while_stepping_state_list (tinfo
);
4404 /* The thread had stopped due to a single-step request indeed
4405 explained by a tracepoint. */
4409 wstep
= tinfo
->while_stepping
;
4410 wstep_link
= &tinfo
->while_stepping
;
4412 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4413 target_pid_to_str (tinfo
->entry
.id
),
4414 wstep
->tp_number
, paddress (wstep
->tp_address
));
4416 ctx
.base
.type
= trap_tracepoint
;
4417 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4419 while (wstep
!= NULL
)
4421 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4424 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4425 wstep
->tp_number
, paddress (wstep
->tp_address
),
4426 target_pid_to_str (tinfo
->entry
.id
));
4429 *wstep_link
= wstep
->next
;
4430 release_while_stepping_state (wstep
);
4431 wstep
= *wstep_link
;
4435 /* We've just finished one step. */
4436 ++wstep
->current_step
;
4439 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4440 stop_pc
, tpoint
, wstep
->current_step
);
4442 if (wstep
->current_step
>= tpoint
->step_count
)
4444 /* The requested numbers of steps have occurred. */
4445 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4446 target_pid_to_str (tinfo
->entry
.id
),
4447 wstep
->tp_number
, paddress (wstep
->tp_address
));
4449 /* Unlink the wstep. */
4450 *wstep_link
= wstep
->next
;
4451 release_while_stepping_state (wstep
);
4452 wstep
= *wstep_link
;
4454 /* Only check the hit count now, which ensure that we do all
4455 our stepping before stopping the run. */
4456 if (tpoint
->pass_count
> 0
4457 && tpoint
->hit_count
>= tpoint
->pass_count
4458 && stopping_tracepoint
== NULL
)
4459 stopping_tracepoint
= tpoint
;
4463 /* Keep single-stepping until the requested numbers of steps
4465 wstep_link
= &wstep
->next
;
4466 wstep
= *wstep_link
;
4469 if (stopping_tracepoint
4470 || trace_buffer_is_full
4471 || expr_eval_result
!= expr_eval_no_error
)
4481 /* Handle any internal tracing control breakpoint hits. That means,
4482 pull traceframes from the IPA to our buffer, and syncing both
4483 tracing agents when the IPA's tracing stops for some reason. */
4486 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4488 /* Pull in fast tracepoint trace frames from the inferior in-process
4489 agent's buffer into our buffer. */
4491 if (!agent_loaded_p ())
4494 upload_fast_traceframes ();
4496 /* Check if the in-process agent had decided we should stop
4498 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4500 int ipa_trace_buffer_is_full
;
4501 CORE_ADDR ipa_stopping_tracepoint
;
4502 int ipa_expr_eval_result
;
4503 CORE_ADDR ipa_error_tracepoint
;
4505 trace_debug ("lib stopped at stop_tracing");
4507 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4508 &ipa_trace_buffer_is_full
);
4510 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4511 &ipa_stopping_tracepoint
);
4512 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4514 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4515 &ipa_error_tracepoint
);
4516 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4518 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4519 &ipa_expr_eval_result
);
4520 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4522 trace_debug ("lib: trace_buffer_is_full: %d, "
4523 "stopping_tracepoint: %s, "
4524 "ipa_expr_eval_result: %d, "
4525 "error_tracepoint: %s, ",
4526 ipa_trace_buffer_is_full
,
4527 paddress (ipa_stopping_tracepoint
),
4528 ipa_expr_eval_result
,
4529 paddress (ipa_error_tracepoint
));
4533 if (ipa_trace_buffer_is_full
)
4534 trace_debug ("lib stopped due to full buffer.");
4535 if (ipa_stopping_tracepoint
)
4536 trace_debug ("lib stopped due to tpoint");
4537 if (ipa_error_tracepoint
)
4538 trace_debug ("lib stopped due to error");
4541 if (ipa_stopping_tracepoint
!= 0)
4544 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4546 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4548 expr_eval_result
= ipa_expr_eval_result
;
4550 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4555 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4557 trace_debug ("lib stopped at flush_trace_buffer");
4564 /* Return true if TINFO just hit a tracepoint. Collect data if
4568 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4570 struct tracepoint
*tpoint
;
4572 struct trap_tracepoint_ctx ctx
;
4574 /* Not tracing, don't handle. */
4578 ctx
.base
.type
= trap_tracepoint
;
4579 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4581 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4583 /* Note that we collect fast tracepoints here as well. We'll
4584 step over the fast tracepoint jump later, which avoids the
4585 double collect. However, we don't collect for static
4586 tracepoints here, because UST markers are compiled in program,
4587 and probes will be executed in program. So static tracepoints
4588 are collected there. */
4589 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4590 && tpoint
->type
!= static_tracepoint
)
4592 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4593 target_pid_to_str (tinfo
->entry
.id
),
4594 tpoint
->number
, paddress (tpoint
->address
));
4596 /* Test the condition if present, and collect if true. */
4598 || (condition_true_at_tracepoint
4599 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4600 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4603 if (stopping_tracepoint
4604 || trace_buffer_is_full
4605 || expr_eval_result
!= expr_eval_no_error
)
4609 /* If the tracepoint had a 'while-stepping' action, then set
4610 the thread to collect this tracepoint on the following
4612 else if (tpoint
->step_count
> 0)
4614 add_while_stepping_state (tinfo
,
4615 tpoint
->number
, tpoint
->address
);
4627 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4628 struct ust_marker_data
;
4629 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4630 struct traceframe
*tframe
);
4633 /* Create a trace frame for the hit of the given tracepoint in the
4637 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4638 struct tracepoint
*tpoint
)
4640 struct traceframe
*tframe
;
4643 /* Only count it as a hit when we actually collect data. */
4644 tpoint
->hit_count
++;
4646 /* If we've exceeded a defined pass count, record the event for
4647 later, and finish the collection for this hit. This test is only
4648 for nonstepping tracepoints, stepping tracepoints test at the end
4649 of their while-stepping loop. */
4650 if (tpoint
->pass_count
> 0
4651 && tpoint
->hit_count
>= tpoint
->pass_count
4652 && tpoint
->step_count
== 0
4653 && stopping_tracepoint
== NULL
)
4654 stopping_tracepoint
= tpoint
;
4656 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4657 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4659 tframe
= add_traceframe (tpoint
);
4663 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4665 #ifndef IN_PROCESS_AGENT
4666 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4667 tpoint
->number
, paddress (tpoint
->address
),
4668 tpoint
->actions_str
[acti
]);
4671 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4672 tpoint
->actions
[acti
]);
4675 finish_traceframe (tframe
);
4678 if (tframe
== NULL
&& tracing
)
4679 trace_buffer_is_full
= 1;
4682 #ifndef IN_PROCESS_AGENT
4685 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4687 struct tracepoint
*tpoint
, int current_step
)
4689 struct traceframe
*tframe
;
4692 trace_debug ("Making new step traceframe for "
4693 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4694 tpoint
->number
, paddress (tpoint
->address
),
4695 current_step
, tpoint
->step_count
,
4698 tframe
= add_traceframe (tpoint
);
4702 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4704 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4705 tpoint
->number
, paddress (tpoint
->address
),
4706 tpoint
->step_actions_str
[acti
]);
4708 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4709 tpoint
->step_actions
[acti
]);
4712 finish_traceframe (tframe
);
4715 if (tframe
== NULL
&& tracing
)
4716 trace_buffer_is_full
= 1;
4721 #ifdef IN_PROCESS_AGENT
4722 /* The target description index for IPA. Passed from gdbserver, used
4723 to select ipa_tdesc. */
4725 IP_AGENT_EXPORT_VAR
int ipa_tdesc_idx
;
4729 static struct regcache
*
4730 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4732 struct regcache
*regcache
= NULL
;
4733 #ifdef IN_PROCESS_AGENT
4734 const struct target_desc
*ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
4736 if (ctx
->type
== fast_tracepoint
)
4738 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4739 if (!fctx
->regcache_initted
)
4741 fctx
->regcache_initted
= 1;
4742 init_register_cache (&fctx
->regcache
, ipa_tdesc
, fctx
->regspace
);
4743 supply_regblock (&fctx
->regcache
, NULL
);
4744 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4746 regcache
= &fctx
->regcache
;
4749 if (ctx
->type
== static_tracepoint
)
4751 struct static_tracepoint_ctx
*sctx
4752 = (struct static_tracepoint_ctx
*) ctx
;
4754 if (!sctx
->regcache_initted
)
4756 sctx
->regcache_initted
= 1;
4757 init_register_cache (&sctx
->regcache
, ipa_tdesc
, sctx
->regspace
);
4758 supply_regblock (&sctx
->regcache
, NULL
);
4759 /* Pass down the tracepoint address, because REGS doesn't
4760 include the PC, but we know what it must have been. */
4761 supply_static_tracepoint_registers (&sctx
->regcache
,
4762 (const unsigned char *)
4764 sctx
->tpoint
->address
);
4766 regcache
= &sctx
->regcache
;
4770 if (ctx
->type
== trap_tracepoint
)
4772 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4773 regcache
= tctx
->regcache
;
4777 gdb_assert (regcache
!= NULL
);
4783 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4785 struct tracepoint
*tpoint
,
4786 struct traceframe
*tframe
,
4787 struct tracepoint_action
*taction
)
4789 enum eval_result_type err
;
4791 switch (taction
->type
)
4795 struct collect_memory_action
*maction
;
4796 struct eval_agent_expr_context ax_ctx
;
4798 maction
= (struct collect_memory_action
*) taction
;
4799 ax_ctx
.regcache
= NULL
;
4800 ax_ctx
.tframe
= tframe
;
4801 ax_ctx
.tpoint
= tpoint
;
4803 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4804 pulongest (maction
->len
),
4805 paddress (maction
->addr
), maction
->basereg
);
4806 /* (should use basereg) */
4807 agent_mem_read (&ax_ctx
, NULL
, (CORE_ADDR
) maction
->addr
,
4813 unsigned char *regspace
;
4814 struct regcache tregcache
;
4815 struct regcache
*context_regcache
;
4818 trace_debug ("Want to collect registers");
4820 context_regcache
= get_context_regcache (ctx
);
4821 regcache_size
= register_cache_size (context_regcache
->tdesc
);
4823 /* Collect all registers for now. */
4824 regspace
= add_traceframe_block (tframe
, tpoint
, 1 + regcache_size
);
4825 if (regspace
== NULL
)
4827 trace_debug ("Trace buffer block allocation failed, skipping");
4830 /* Identify a register block. */
4833 /* Wrap the regblock in a register cache (in the stack, we
4834 don't want to malloc here). */
4835 init_register_cache (&tregcache
, context_regcache
->tdesc
,
4838 /* Copy the register data to the regblock. */
4839 regcache_cpy (&tregcache
, context_regcache
);
4841 #ifndef IN_PROCESS_AGENT
4842 /* On some platforms, trap-based tracepoints will have the PC
4843 pointing to the next instruction after the trap, but we
4844 don't want the user or GDB trying to guess whether the
4845 saved PC needs adjusting; so always record the adjusted
4846 stop_pc. Note that we can't use tpoint->address instead,
4847 since it will be wrong for while-stepping actions. This
4848 adjustment is a nop for fast tracepoints collected from the
4849 in-process lib (but not if GDBserver is collecting one
4850 preemptively), since the PC had already been adjusted to
4851 contain the tracepoint's address by the jump pad. */
4852 trace_debug ("Storing stop pc (0x%s) in regblock",
4853 paddress (stop_pc
));
4855 /* This changes the regblock, not the thread's
4857 regcache_write_pc (&tregcache
, stop_pc
);
4863 struct eval_expr_action
*eaction
;
4864 struct eval_agent_expr_context ax_ctx
;
4866 eaction
= (struct eval_expr_action
*) taction
;
4867 ax_ctx
.regcache
= get_context_regcache (ctx
);
4868 ax_ctx
.tframe
= tframe
;
4869 ax_ctx
.tpoint
= tpoint
;
4871 trace_debug ("Want to evaluate expression");
4873 err
= gdb_eval_agent_expr (&ax_ctx
, eaction
->expr
, NULL
);
4875 if (err
!= expr_eval_no_error
)
4877 record_tracepoint_error (tpoint
, "action expression", err
);
4884 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4885 trace_debug ("Want to collect static trace data");
4886 collect_ust_data_at_tracepoint (ctx
, tframe
);
4888 trace_debug ("warning: collecting static trace data, "
4889 "but static tracepoints are not supported");
4894 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4900 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4901 struct tracepoint
*tpoint
)
4904 enum eval_result_type err
;
4906 /* Presently, gdbserver doesn't run compiled conditions, only the
4907 IPA does. If the program stops at a fast tracepoint's address
4908 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4909 gdbserver preemptively collect the fast tracepoint. Later, on
4910 resume, gdbserver steps over the fast tracepoint like it steps
4911 over breakpoints, so that the IPA doesn't see that fast
4912 tracepoint. This avoids double collects of fast tracepoints in
4913 that stopping scenario. Having gdbserver itself handle the fast
4914 tracepoint gives the user a consistent view of when fast or trap
4915 tracepoints are collected, compared to an alternative where only
4916 trap tracepoints are collected on stop, and fast tracepoints on
4917 resume. When a fast tracepoint is being processed by gdbserver,
4918 it is always the non-compiled condition expression that is
4920 #ifdef IN_PROCESS_AGENT
4921 if (tpoint
->compiled_cond
)
4923 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4924 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (fctx
->regs
, &value
);
4929 struct eval_agent_expr_context ax_ctx
;
4931 ax_ctx
.regcache
= get_context_regcache (ctx
);
4932 ax_ctx
.tframe
= NULL
;
4933 ax_ctx
.tpoint
= tpoint
;
4935 err
= gdb_eval_agent_expr (&ax_ctx
, tpoint
->cond
, &value
);
4937 if (err
!= expr_eval_no_error
)
4939 record_tracepoint_error (tpoint
, "condition", err
);
4940 /* The error case must return false. */
4944 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4945 tpoint
->number
, paddress (tpoint
->address
),
4947 return (value
? 1 : 0);
4950 /* Do memory copies for bytecodes. */
4951 /* Do the recording of memory blocks for actions and bytecodes. */
4954 agent_mem_read (struct eval_agent_expr_context
*ctx
,
4955 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4957 unsigned char *mspace
;
4958 ULONGEST remaining
= len
;
4959 unsigned short blocklen
;
4961 /* If a 'to' buffer is specified, use it. */
4964 read_inferior_memory (from
, to
, len
);
4968 /* Otherwise, create a new memory block in the trace buffer. */
4969 while (remaining
> 0)
4973 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4974 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4975 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
4978 /* Identify block as a memory block. */
4981 /* Record address and size. */
4982 memcpy (mspace
, &from
, sizeof (from
));
4983 mspace
+= sizeof (from
);
4984 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4985 mspace
+= sizeof (blocklen
);
4986 /* Record the memory block proper. */
4987 read_inferior_memory (from
, mspace
, blocklen
);
4988 trace_debug ("%d bytes recorded", blocklen
);
4989 remaining
-= blocklen
;
4996 agent_mem_read_string (struct eval_agent_expr_context
*ctx
,
4997 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4999 unsigned char *buf
, *mspace
;
5000 ULONGEST remaining
= len
;
5001 unsigned short blocklen
, i
;
5003 /* To save a bit of space, block lengths are 16-bit, so break large
5004 requests into multiple blocks. Bordering on overkill for strings,
5005 but it could happen that someone specifies a large max length. */
5006 while (remaining
> 0)
5010 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
5011 /* We want working space to accumulate nonzero bytes, since
5012 traceframes must have a predecided size (otherwise it gets
5013 harder to wrap correctly for the circular case, etc). */
5014 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
5015 for (i
= 0; i
< blocklen
; ++i
)
5017 /* Read the string one byte at a time, in case the string is
5018 at the end of a valid memory area - we don't want a
5019 correctly-terminated string to engender segvio
5021 read_inferior_memory (from
+ i
, buf
+ i
, 1);
5026 /* Make sure outer loop stops now too. */
5027 remaining
= blocklen
;
5031 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
5032 mspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
, sp
);
5038 /* Identify block as a memory block. */
5041 /* Record address and size. */
5042 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
5043 mspace
+= sizeof (from
);
5044 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
5045 mspace
+= sizeof (blocklen
);
5046 /* Copy the string contents. */
5047 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
5048 remaining
-= blocklen
;
5055 /* Record the value of a trace state variable. */
5058 agent_tsv_read (struct eval_agent_expr_context
*ctx
, int n
)
5060 unsigned char *vspace
;
5063 vspace
= add_traceframe_block (ctx
->tframe
, ctx
->tpoint
,
5064 1 + sizeof (n
) + sizeof (LONGEST
));
5067 /* Identify block as a variable. */
5069 /* Record variable's number and value. */
5070 memcpy (vspace
+ 1, &n
, sizeof (n
));
5071 val
= get_trace_state_variable_value (n
);
5072 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
5073 trace_debug ("Variable %d recorded", n
);
5077 #ifndef IN_PROCESS_AGENT
5079 /* Callback for traceframe_walk_blocks, used to find a given block
5080 type in a traceframe. */
5083 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
5085 char *wantedp
= (char *) data
;
5087 if (*wantedp
== blocktype
)
5093 /* Walk over all traceframe blocks of the traceframe buffer starting
5094 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5095 block found, passing in DATA unmodified. If CALLBACK returns true,
5096 this returns a pointer to where the block is found. Returns NULL
5097 if no callback call returned true, indicating that all blocks have
5100 static unsigned char *
5101 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
5103 int (*callback
) (char blocktype
,
5104 unsigned char *dataptr
,
5108 unsigned char *dataptr
;
5112 trace_debug ("traceframe %d has no data", tfnum
);
5116 /* Iterate through a traceframe's blocks, looking for a block of the
5118 for (dataptr
= database
;
5119 dataptr
< database
+ datasize
;
5123 unsigned short mlen
;
5125 if (dataptr
== trace_buffer_wrap
)
5127 /* Adjust to reflect wrapping part of the frame around to
5129 datasize
= dataptr
- database
;
5130 dataptr
= database
= trace_buffer_lo
;
5133 blocktype
= *dataptr
++;
5135 if ((*callback
) (blocktype
, dataptr
, data
))
5141 /* Skip over the registers block. */
5142 dataptr
+= current_target_desc ()->registers_size
;
5145 /* Skip over the memory block. */
5146 dataptr
+= sizeof (CORE_ADDR
);
5147 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5148 dataptr
+= (sizeof (mlen
) + mlen
);
5151 /* Skip over the TSV block. */
5152 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5155 /* Skip over the static trace data block. */
5156 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5157 dataptr
+= (sizeof (mlen
) + mlen
);
5160 trace_debug ("traceframe %d has unknown block type 0x%x",
5169 /* Look for the block of type TYPE_WANTED in the trameframe starting
5170 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5173 static unsigned char *
5174 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5175 int tfnum
, char type_wanted
)
5177 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5178 match_blocktype
, &type_wanted
);
5181 static unsigned char *
5182 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5184 unsigned char *regblock
;
5186 regblock
= traceframe_find_block_type (tframe
->data
,
5190 if (regblock
== NULL
)
5191 trace_debug ("traceframe %d has no register data", tfnum
);
5196 /* Get registers from a traceframe. */
5199 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5201 unsigned char *dataptr
;
5202 struct tracepoint
*tpoint
;
5203 struct traceframe
*tframe
;
5205 tframe
= find_traceframe (tfnum
);
5209 trace_debug ("traceframe %d not found", tfnum
);
5213 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5214 if (dataptr
== NULL
)
5216 /* Mark registers unavailable. */
5217 supply_regblock (regcache
, NULL
);
5219 /* We can generally guess at a PC, although this will be
5220 misleading for while-stepping frames and multi-location
5222 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5224 regcache_write_pc (regcache
, tpoint
->address
);
5227 supply_regblock (regcache
, dataptr
);
5233 traceframe_get_pc (struct traceframe
*tframe
)
5235 struct regcache regcache
;
5236 unsigned char *dataptr
;
5237 const struct target_desc
*tdesc
= current_target_desc ();
5239 dataptr
= traceframe_find_regblock (tframe
, -1);
5240 if (dataptr
== NULL
)
5243 init_register_cache (®cache
, tdesc
, dataptr
);
5244 return regcache_read_pc (®cache
);
5247 /* Read a requested block of memory from a trace frame. */
5250 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5251 unsigned char *buf
, ULONGEST length
,
5254 struct traceframe
*tframe
;
5255 unsigned char *database
, *dataptr
;
5256 unsigned int datasize
;
5258 unsigned short mlen
;
5260 trace_debug ("traceframe_read_mem");
5262 tframe
= find_traceframe (tfnum
);
5266 trace_debug ("traceframe %d not found", tfnum
);
5270 datasize
= tframe
->data_size
;
5271 database
= dataptr
= &tframe
->data
[0];
5273 /* Iterate through a traceframe's blocks, looking for memory. */
5274 while ((dataptr
= traceframe_find_block_type (dataptr
,
5276 - (dataptr
- database
),
5277 tfnum
, 'M')) != NULL
)
5279 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5280 dataptr
+= sizeof (maddr
);
5281 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5282 dataptr
+= sizeof (mlen
);
5283 trace_debug ("traceframe %d has %d bytes at %s",
5284 tfnum
, mlen
, paddress (maddr
));
5286 /* If the block includes the first part of the desired range,
5287 return as much it has; GDB will re-request the remainder,
5288 which might be in a different block of this trace frame. */
5289 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5291 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5295 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5300 /* Skip over this block. */
5304 trace_debug ("traceframe %d has no memory data for the desired region",
5312 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5315 struct traceframe
*tframe
;
5316 unsigned char *database
, *dataptr
;
5317 unsigned int datasize
;
5321 trace_debug ("traceframe_read_tsv");
5323 tfnum
= current_traceframe
;
5327 trace_debug ("no current traceframe");
5331 tframe
= find_traceframe (tfnum
);
5335 trace_debug ("traceframe %d not found", tfnum
);
5339 datasize
= tframe
->data_size
;
5340 database
= dataptr
= &tframe
->data
[0];
5342 /* Iterate through a traceframe's blocks, looking for the last
5344 while ((dataptr
= traceframe_find_block_type (dataptr
,
5346 - (dataptr
- database
),
5347 tfnum
, 'V')) != NULL
)
5349 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5350 dataptr
+= sizeof (vnum
);
5352 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5354 /* Check that this is the variable we want. */
5357 memcpy (val
, dataptr
, sizeof (*val
));
5361 /* Skip over this block. */
5362 dataptr
+= sizeof (LONGEST
);
5366 trace_debug ("traceframe %d has no data for variable %d",
5371 /* Read a requested block of static tracepoint data from a trace
5375 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5376 unsigned char *buf
, ULONGEST length
,
5379 struct traceframe
*tframe
;
5380 unsigned char *database
, *dataptr
;
5381 unsigned int datasize
;
5382 unsigned short mlen
;
5384 trace_debug ("traceframe_read_sdata");
5386 tframe
= find_traceframe (tfnum
);
5390 trace_debug ("traceframe %d not found", tfnum
);
5394 datasize
= tframe
->data_size
;
5395 database
= &tframe
->data
[0];
5397 /* Iterate through a traceframe's blocks, looking for static
5399 dataptr
= traceframe_find_block_type (database
, datasize
,
5401 if (dataptr
!= NULL
)
5403 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5404 dataptr
+= sizeof (mlen
);
5407 if (offset
+ length
> mlen
)
5408 length
= mlen
- offset
;
5410 memcpy (buf
, dataptr
, length
);
5418 trace_debug ("traceframe %d has no static trace data", tfnum
);
5424 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5425 object. DATA is pointer to a struct buffer holding the
5426 traceframe-info object being built. */
5429 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5431 struct buffer
*buffer
= (struct buffer
*) data
;
5437 unsigned short mlen
;
5440 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5441 dataptr
+= sizeof (maddr
);
5442 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5443 dataptr
+= sizeof (mlen
);
5444 buffer_xml_printf (buffer
,
5445 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5446 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5453 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5454 buffer_xml_printf (buffer
, "<tvar id=\"%d\"/>\n", vnum
);
5463 warning ("Unhandled trace block type (%d) '%c ' "
5464 "while building trace frame info.",
5465 blocktype
, blocktype
);
5472 /* Build a traceframe-info object for traceframe number TFNUM into
5476 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5478 struct traceframe
*tframe
;
5480 trace_debug ("traceframe_read_info");
5482 tframe
= find_traceframe (tfnum
);
5486 trace_debug ("traceframe %d not found", tfnum
);
5490 buffer_grow_str (buffer
, "<traceframe-info>\n");
5491 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5492 tfnum
, build_traceframe_info_xml
, buffer
);
5493 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5497 /* Return the first fast tracepoint whose jump pad contains PC. */
5499 static struct tracepoint
*
5500 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5502 struct tracepoint
*tpoint
;
5504 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5505 if (tpoint
->type
== fast_tracepoint
)
5506 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5512 /* Return the first fast tracepoint whose trampoline contains PC. */
5514 static struct tracepoint
*
5515 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5517 struct tracepoint
*tpoint
;
5519 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5521 if (tpoint
->type
== fast_tracepoint
5522 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5529 /* Return GDBserver's tracepoint that matches the IP Agent's
5530 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5533 static struct tracepoint
*
5534 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5536 struct tracepoint
*tpoint
;
5538 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5539 if (tpoint
->type
== fast_tracepoint
)
5540 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5548 /* The type of the object that is used to synchronize fast tracepoint
5551 typedef struct collecting_t
5553 /* The fast tracepoint number currently collecting. */
5556 /* A number that GDBserver can use to identify the thread that is
5557 presently holding the collect lock. This need not (and usually
5558 is not) the thread id, as getting the current thread ID usually
5559 requires a system call, which we want to avoid like the plague.
5560 Usually this is thread's TCB, found in the TLS (pseudo-)
5561 register, which is readable with a single insn on several
5563 uintptr_t thread_area
;
5566 #ifndef IN_PROCESS_AGENT
5569 force_unlock_trace_buffer (void)
5571 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5574 /* Check if the thread identified by THREAD_AREA which is stopped at
5575 STOP_PC, is presently locking the fast tracepoint collection, and
5576 if so, gather some status of said collection. Returns 0 if the
5577 thread isn't collecting or in the jump pad at all. 1, if in the
5578 jump pad (or within gdb_collect) and hasn't executed the adjusted
5579 original insn yet (can set a breakpoint there and run to it). 2,
5580 if presently executing the adjusted original insn --- in which
5581 case, if we want to move the thread out of the jump pad, we need to
5582 single-step it until this function returns 0. */
5585 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5587 struct fast_tpoint_collect_status
*status
)
5589 CORE_ADDR ipa_collecting
;
5590 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5591 CORE_ADDR ipa_gdb_trampoline_buffer
;
5592 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5593 struct tracepoint
*tpoint
;
5594 int needs_breakpoint
;
5596 /* The thread THREAD_AREA is either:
5598 0. not collecting at all, not within the jump pad, or within
5599 gdb_collect or one of its callees.
5601 1. in the jump pad and haven't reached gdb_collect
5603 2. within gdb_collect (out of the jump pad) (collect is set)
5605 3. we're in the jump pad, after gdb_collect having returned,
5606 possibly executing the adjusted insns.
5608 For cases 1 and 3, `collecting' may or not be set. The jump pad
5609 doesn't have any complicated jump logic, so we can tell if the
5610 thread is executing the adjust original insn or not by just
5611 matching STOP_PC with known jump pad addresses. If we it isn't
5612 yet executing the original insn, set a breakpoint there, and let
5613 the thread run to it, so to quickly step over a possible (many
5614 insns) gdb_collect call. Otherwise, or when the breakpoint is
5615 hit, only a few (small number of) insns are left to be executed
5616 in the jump pad. Single-step the thread until it leaves the
5621 needs_breakpoint
= 0;
5622 trace_debug ("fast_tracepoint_collecting");
5624 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5625 &ipa_gdb_jump_pad_buffer
))
5627 internal_error (__FILE__
, __LINE__
,
5628 "error extracting `gdb_jump_pad_buffer'");
5630 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5631 &ipa_gdb_jump_pad_buffer_end
))
5633 internal_error (__FILE__
, __LINE__
,
5634 "error extracting `gdb_jump_pad_buffer_end'");
5637 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5638 &ipa_gdb_trampoline_buffer
))
5640 internal_error (__FILE__
, __LINE__
,
5641 "error extracting `gdb_trampoline_buffer'");
5643 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5644 &ipa_gdb_trampoline_buffer_end
))
5646 internal_error (__FILE__
, __LINE__
,
5647 "error extracting `gdb_trampoline_buffer_end'");
5650 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5651 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5653 /* We can tell which tracepoint(s) the thread is collecting by
5654 matching the jump pad address back to the tracepoint. */
5655 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5658 warning ("in jump pad, but no matching tpoint?");
5663 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5665 tpoint
->number
, paddress (tpoint
->address
),
5666 paddress (tpoint
->jump_pad
),
5667 paddress (tpoint
->jump_pad_end
),
5668 paddress (tpoint
->adjusted_insn_addr
),
5669 paddress (tpoint
->adjusted_insn_addr_end
));
5672 /* Definitely in the jump pad. May or may not need
5673 fast-exit-jump-pad breakpoint. */
5674 if (tpoint
->jump_pad
<= stop_pc
5675 && stop_pc
< tpoint
->adjusted_insn_addr
)
5676 needs_breakpoint
= 1;
5678 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5679 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5681 /* We can tell which tracepoint(s) the thread is collecting by
5682 matching the trampoline address back to the tracepoint. */
5683 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5686 warning ("in trampoline, but no matching tpoint?");
5691 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5692 tpoint
->number
, paddress (tpoint
->address
),
5693 paddress (tpoint
->trampoline
),
5694 paddress (tpoint
->trampoline_end
));
5697 /* Have not reached jump pad yet, but treat the trampoline as a
5698 part of the jump pad that is before the adjusted original
5700 needs_breakpoint
= 1;
5704 collecting_t ipa_collecting_obj
;
5706 /* If `collecting' is set/locked, then the THREAD_AREA thread
5707 may or not be the one holding the lock. We have to read the
5708 lock to find out. */
5710 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5713 trace_debug ("fast_tracepoint_collecting:"
5714 " failed reading 'collecting' in the inferior");
5718 if (!ipa_collecting
)
5720 trace_debug ("fast_tracepoint_collecting: not collecting"
5721 " (and nobody is).");
5725 /* Some thread is collecting. Check which. */
5726 if (read_inferior_memory (ipa_collecting
,
5727 (unsigned char *) &ipa_collecting_obj
,
5728 sizeof (ipa_collecting_obj
)) != 0)
5731 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5733 trace_debug ("fast_tracepoint_collecting: not collecting "
5734 "(another thread is)");
5739 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5742 warning ("fast_tracepoint_collecting: collecting, "
5743 "but tpoint %s not found?",
5744 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5748 /* The thread is within `gdb_collect', skip over the rest of
5749 fast tracepoint collection quickly using a breakpoint. */
5750 needs_breakpoint
= 1;
5753 /* The caller wants a bit of status detail. */
5756 status
->tpoint_num
= tpoint
->number
;
5757 status
->tpoint_addr
= tpoint
->address
;
5758 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5759 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5762 if (needs_breakpoint
)
5764 /* Hasn't executed the original instruction yet. Set breakpoint
5765 there, and wait till it's hit, then single-step until exiting
5769 fast_tracepoint_collecting, returning continue-until-break at %s",
5770 paddress (tpoint
->adjusted_insn_addr
));
5772 return 1; /* continue */
5776 /* Just single-step until exiting the jump pad. */
5778 trace_debug ("fast_tracepoint_collecting, returning "
5779 "need-single-step (%s-%s)",
5780 paddress (tpoint
->adjusted_insn_addr
),
5781 paddress (tpoint
->adjusted_insn_addr_end
));
5783 return 2; /* single-step */
5789 #ifdef IN_PROCESS_AGENT
5791 /* The global fast tracepoint collect lock. Points to a collecting_t
5792 object built on the stack by the jump pad, if presently locked;
5793 NULL if it isn't locked. Note that this lock *must* be set while
5794 executing any *function other than the jump pad. See
5795 fast_tracepoint_collecting. */
5797 IP_AGENT_EXPORT_VAR collecting_t
*collecting
;
5800 /* This routine, called from the jump pad (in asm) is designed to be
5801 called from the jump pads of fast tracepoints, thus it is on the
5804 IP_AGENT_EXPORT_FUNC
void
5805 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5807 struct fast_tracepoint_ctx ctx
;
5808 const struct target_desc
*ipa_tdesc
;
5810 /* Don't do anything until the trace run is completely set up. */
5814 ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
5815 ctx
.base
.type
= fast_tracepoint
;
5817 ctx
.regcache_initted
= 0;
5818 /* Wrap the regblock in a register cache (in the stack, we don't
5819 want to malloc here). */
5820 ctx
.regspace
= (unsigned char *) alloca (ipa_tdesc
->registers_size
);
5821 if (ctx
.regspace
== NULL
)
5823 trace_debug ("Trace buffer block allocation failed, skipping");
5827 for (ctx
.tpoint
= tpoint
;
5828 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5829 ctx
.tpoint
= ctx
.tpoint
->next
)
5831 if (!ctx
.tpoint
->enabled
)
5834 /* Multiple tracepoints of different types, such as fast tracepoint and
5835 static tracepoint, can be set at the same address. */
5836 if (ctx
.tpoint
->type
!= tpoint
->type
)
5839 /* Test the condition if present, and collect if true. */
5840 if (ctx
.tpoint
->cond
== NULL
5841 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5844 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5845 ctx
.tpoint
->address
, ctx
.tpoint
);
5847 /* Note that this will cause original insns to be written back
5848 to where we jumped from, but that's OK because we're jumping
5849 back to the next whole instruction. This will go badly if
5850 instruction restoration is not atomic though. */
5851 if (stopping_tracepoint
5852 || trace_buffer_is_full
5853 || expr_eval_result
!= expr_eval_no_error
)
5861 /* If there was a condition and it evaluated to false, the only
5862 way we would stop tracing is if there was an error during
5863 condition expression evaluation. */
5864 if (expr_eval_result
!= expr_eval_no_error
)
5873 /* These global variables points to the corresponding functions. This is
5874 necessary on powerpc64, where asking for function symbol address from gdb
5875 results in returning the actual code pointer, instead of the descriptor
5878 typedef void (*gdb_collect_ptr_type
) (struct tracepoint
*, unsigned char *);
5879 typedef ULONGEST (*get_raw_reg_ptr_type
) (const unsigned char *, int);
5880 typedef LONGEST (*get_trace_state_variable_value_ptr_type
) (int);
5881 typedef void (*set_trace_state_variable_value_ptr_type
) (int, LONGEST
);
5884 IP_AGENT_EXPORT_VAR gdb_collect_ptr_type gdb_collect_ptr
= gdb_collect
;
5885 IP_AGENT_EXPORT_VAR get_raw_reg_ptr_type get_raw_reg_ptr
= get_raw_reg
;
5886 IP_AGENT_EXPORT_VAR get_trace_state_variable_value_ptr_type
5887 get_trace_state_variable_value_ptr
= get_trace_state_variable_value
;
5888 IP_AGENT_EXPORT_VAR set_trace_state_variable_value_ptr_type
5889 set_trace_state_variable_value_ptr
= set_trace_state_variable_value
;
5894 #ifndef IN_PROCESS_AGENT
5897 get_raw_reg_func_addr (void)
5900 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_get_raw_reg_ptr
, &res
))
5902 error ("error extracting get_raw_reg_ptr");
5909 get_get_tsv_func_addr (void)
5912 if (read_inferior_data_pointer (
5913 ipa_sym_addrs
.addr_get_trace_state_variable_value_ptr
, &res
))
5915 error ("error extracting get_trace_state_variable_value_ptr");
5922 get_set_tsv_func_addr (void)
5925 if (read_inferior_data_pointer (
5926 ipa_sym_addrs
.addr_set_trace_state_variable_value_ptr
, &res
))
5928 error ("error extracting set_trace_state_variable_value_ptr");
5935 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5936 CORE_ADDR
*jump_entry
)
5938 CORE_ADDR entry_point
= *jump_entry
;
5939 enum eval_result_type err
;
5941 trace_debug ("Starting condition compilation for tracepoint %d\n",
5944 /* Initialize the global pointer to the code being built. */
5945 current_insn_ptr
= *jump_entry
;
5949 err
= compile_bytecodes (tpoint
->cond
);
5951 if (err
== expr_eval_no_error
)
5955 /* Record the beginning of the compiled code. */
5956 tpoint
->compiled_cond
= entry_point
;
5958 trace_debug ("Condition compilation for tracepoint %d complete\n",
5963 /* Leave the unfinished code in situ, but don't point to it. */
5965 tpoint
->compiled_cond
= 0;
5967 trace_debug ("Condition compilation for tracepoint %d failed, "
5969 tpoint
->number
, err
);
5972 /* Update the code pointer passed in. Note that we do this even if
5973 the compile fails, so that we can look at the partial results
5974 instead of letting them be overwritten. */
5975 *jump_entry
= current_insn_ptr
;
5977 /* Leave a gap, to aid dump decipherment. */
5981 /* The base pointer of the IPA's heap. This is the only memory the
5982 IPA is allowed to use. The IPA should _not_ call the inferior's
5983 `malloc' during operation. That'd be slow, and, most importantly,
5984 it may not be safe. We may be collecting a tracepoint in a signal
5985 handler, for example. */
5986 static CORE_ADDR target_tp_heap
;
5988 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5992 target_malloc (ULONGEST size
)
5996 if (target_tp_heap
== 0)
5998 /* We have the pointer *address*, need what it points to. */
5999 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
6002 internal_error (__FILE__
, __LINE__
,
6003 "couldn't get target heap head pointer");
6007 ptr
= target_tp_heap
;
6008 target_tp_heap
+= size
;
6010 /* Pad to 8-byte alignment. */
6011 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
6017 download_agent_expr (struct agent_expr
*expr
)
6019 CORE_ADDR expr_addr
;
6020 CORE_ADDR expr_bytes
;
6022 expr_addr
= target_malloc (sizeof (*expr
));
6023 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
6025 expr_bytes
= target_malloc (expr
->length
);
6026 write_inferior_data_pointer (expr_addr
+ offsetof (struct agent_expr
, bytes
),
6028 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
6033 /* Align V up to N bits. */
6034 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6036 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6039 download_tracepoint_1 (struct tracepoint
*tpoint
)
6041 struct tracepoint target_tracepoint
;
6042 CORE_ADDR tpptr
= 0;
6044 gdb_assert (tpoint
->type
== fast_tracepoint
6045 || tpoint
->type
== static_tracepoint
);
6047 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6049 CORE_ADDR jentry
, jump_entry
;
6051 jentry
= jump_entry
= get_jump_space_head ();
6053 if (tpoint
->cond
!= NULL
)
6055 /* Pad to 8-byte alignment. (needed?) */
6056 /* Actually this should be left for the target to
6058 jentry
= UALIGN (jentry
, 8);
6060 compile_tracepoint_condition (tpoint
, &jentry
);
6063 /* Pad to 8-byte alignment. */
6064 jentry
= UALIGN (jentry
, 8);
6065 claim_jump_space (jentry
- jump_entry
);
6068 target_tracepoint
= *tpoint
;
6070 tpptr
= target_malloc (sizeof (*tpoint
));
6071 tpoint
->obj_addr_on_target
= tpptr
;
6073 /* Write the whole object. We'll fix up its pointers in a bit.
6074 Assume no next for now. This is fixed up above on the next
6075 iteration, if there's any. */
6076 target_tracepoint
.next
= NULL
;
6077 /* Need to clear this here too, since we're downloading the
6078 tracepoints before clearing our own copy. */
6079 target_tracepoint
.hit_count
= 0;
6081 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6082 sizeof (target_tracepoint
));
6085 write_inferior_data_pointer (tpptr
6086 + offsetof (struct tracepoint
, cond
),
6087 download_agent_expr (tpoint
->cond
));
6089 if (tpoint
->numactions
)
6092 CORE_ADDR actions_array
;
6094 /* The pointers array. */
6096 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6097 write_inferior_data_pointer (tpptr
+ offsetof (struct tracepoint
,
6101 /* Now for each pointer, download the action. */
6102 for (i
= 0; i
< tpoint
->numactions
; i
++)
6104 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6105 CORE_ADDR ipa_action
= tracepoint_action_download (action
);
6107 if (ipa_action
!= 0)
6108 write_inferior_data_pointer (actions_array
6109 + i
* sizeof (*tpoint
->actions
),
6115 #define IPA_PROTO_FAST_TRACE_FLAG 0
6116 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
6117 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
6118 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
6119 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
6121 /* Send a command to agent to download and install tracepoint TPOINT. */
6124 tracepoint_send_agent (struct tracepoint
*tpoint
)
6126 char buf
[IPA_CMD_BUF_SIZE
];
6131 strcpy (p
, "FastTrace:");
6134 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
6135 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
6136 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
6137 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
6138 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
6139 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
6140 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
6141 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
6142 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
6143 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
6144 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
6147 p
= agent_expr_send (p
, tpoint
->cond
);
6149 /* tracepoint_action */
6150 for (i
= 0; i
< tpoint
->numactions
; i
++)
6152 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6154 p
[0] = action
->type
;
6155 p
= tracepoint_action_send (&p
[1], action
);
6158 get_jump_space_head ();
6159 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
6160 agent can use jump pad from it. */
6161 if (tpoint
->type
== fast_tracepoint
)
6163 memcpy (p
, &gdb_jump_pad_head
, 8);
6167 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
6171 if (!startswith (buf
, "OK"))
6174 /* The value of tracepoint's target address is stored in BUF. */
6175 memcpy (&tpoint
->obj_addr_on_target
,
6176 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
6178 if (tpoint
->type
== fast_tracepoint
)
6181 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
6184 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
6185 (unsigned int) tpoint
->obj_addr_on_target
,
6186 (unsigned int) gdb_jump_pad_head
);
6188 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6190 /* This has been done in agent. We should also set up record for it. */
6191 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6194 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6201 download_tracepoint (struct tracepoint
*tpoint
)
6203 struct tracepoint
*tp
, *tp_prev
;
6205 if (tpoint
->type
!= fast_tracepoint
6206 && tpoint
->type
!= static_tracepoint
)
6209 download_tracepoint_1 (tpoint
);
6211 /* Find the previous entry of TPOINT, which is fast tracepoint or
6212 static tracepoint. */
6214 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6216 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6222 CORE_ADDR tp_prev_target_next_addr
;
6224 /* Insert TPOINT after TP_PREV in IPA. */
6225 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6226 + offsetof (struct tracepoint
, next
),
6227 &tp_prev_target_next_addr
))
6229 internal_error (__FILE__
, __LINE__
,
6230 "error reading `tp_prev->next'");
6233 /* tpoint->next = tp_prev->next */
6234 write_inferior_data_pointer (tpoint
->obj_addr_on_target
6235 + offsetof (struct tracepoint
, next
),
6236 tp_prev_target_next_addr
);
6237 /* tp_prev->next = tpoint */
6238 write_inferior_data_pointer (tp_prev
->obj_addr_on_target
6239 + offsetof (struct tracepoint
, next
),
6240 tpoint
->obj_addr_on_target
);
6243 /* First object in list, set the head pointer in the
6245 write_inferior_data_pointer (ipa_sym_addrs
.addr_tracepoints
,
6246 tpoint
->obj_addr_on_target
);
6251 download_trace_state_variables (void)
6253 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6254 struct trace_state_variable
*tsv
;
6256 /* Start out empty. */
6257 write_inferior_data_pointer (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6259 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6261 struct trace_state_variable target_tsv
;
6263 /* TSV's with a getter have been initialized equally in both the
6264 inferior and GDBserver. Skip them. */
6265 if (tsv
->getter
!= NULL
)
6271 ptr
= target_malloc (sizeof (*tsv
));
6273 if (tsv
== trace_state_variables
)
6275 /* First object in list, set the head pointer in the
6278 write_inferior_data_pointer (ipa_sym_addrs
.addr_trace_state_variables
,
6283 write_inferior_data_pointer (prev_ptr
6284 + offsetof (struct trace_state_variable
,
6289 /* Write the whole object. We'll fix up its pointers in a bit.
6290 Assume no next, fixup when needed. */
6291 target_tsv
.next
= NULL
;
6293 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6294 sizeof (target_tsv
));
6296 if (tsv
->name
!= NULL
)
6298 size_t size
= strlen (tsv
->name
) + 1;
6299 CORE_ADDR name_addr
= target_malloc (size
);
6300 write_inferior_memory (name_addr
,
6301 (unsigned char *) tsv
->name
, size
);
6302 write_inferior_data_pointer (ptr
6303 + offsetof (struct trace_state_variable
,
6308 gdb_assert (tsv
->getter
== NULL
);
6313 /* Fixup the next pointer in the last item in the list. */
6314 write_inferior_data_pointer (prev_ptr
6315 + offsetof (struct trace_state_variable
,
6320 /* Upload complete trace frames out of the IP Agent's trace buffer
6321 into GDBserver's trace buffer. This always uploads either all or
6322 no trace frames. This is the counter part of
6323 `trace_alloc_trace_buffer'. See its description of the atomic
6324 synching mechanism. */
6327 upload_fast_traceframes (void)
6329 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6330 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6332 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6333 unsigned int curr_tbctrl_idx
;
6334 unsigned int ipa_trace_buffer_ctrl_curr
;
6335 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6336 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6337 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6338 CORE_ADDR ipa_trace_buffer_lo
;
6339 CORE_ADDR ipa_trace_buffer_hi
;
6341 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6342 &ipa_traceframe_read_count_racy
))
6344 /* This will happen in most targets if the current thread is
6349 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6350 &ipa_traceframe_write_count_racy
))
6353 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6354 ipa_traceframe_write_count_racy
6355 - ipa_traceframe_read_count_racy
,
6356 ipa_traceframe_write_count_racy
,
6357 ipa_traceframe_read_count_racy
);
6359 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6362 about_to_request_buffer_space_bkpt
6363 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6366 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6367 &ipa_trace_buffer_ctrl_curr
))
6370 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6372 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6375 unsigned int prev
, counter
;
6377 /* Update the token, with new counters, and the GDBserver stamp
6378 bit. Alway reuse the current TBC index. */
6379 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6380 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6382 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6388 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6389 ipa_trace_buffer_ctrl_curr
))
6392 trace_debug ("Lib: Committed %08x -> %08x",
6393 ipa_trace_buffer_ctrl_curr_old
,
6394 ipa_trace_buffer_ctrl_curr
);
6396 /* Re-read these, now that we've installed the
6397 `about_to_request_buffer_space' breakpoint/lock. A thread could
6398 have finished a traceframe between the last read of these
6399 counters and setting the breakpoint above. If we start
6400 uploading, we never want to leave this function with
6401 traceframe_read_count != 0, otherwise, GDBserver could end up
6402 incrementing the counter tokens more than once (due to event loop
6403 nesting), which would break the IP agent's "effective" detection
6404 (see trace_alloc_trace_buffer). */
6405 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6406 &ipa_traceframe_read_count
))
6408 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6409 &ipa_traceframe_write_count
))
6414 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6415 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6416 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6418 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6419 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6420 trace_debug ("note that ipa_traceframe_count's parts changed");
6423 /* Get the address of the current TBC object (the IP agent has an
6424 array of 3 such objects). The index is stored in the TBC
6426 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6427 ipa_trace_buffer_ctrl_addr
6428 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6430 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6431 (unsigned char *) &ipa_trace_buffer_ctrl
,
6432 sizeof (struct ipa_trace_buffer_control
)))
6435 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6436 &ipa_trace_buffer_lo
))
6438 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6439 &ipa_trace_buffer_hi
))
6442 /* Offsets are easier to grok for debugging than raw addresses,
6443 especially for the small trace buffer sizes that are useful for
6445 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6446 "endfree=%d wrap=%d hi=%d",
6448 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6449 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6450 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6451 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6452 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6454 /* Note that the IPA's buffer is always circular. */
6456 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6458 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6459 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6461 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6462 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6463 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6464 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6467 tf
= IPA_FIRST_TRACEFRAME ();
6469 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6471 struct tracepoint
*tpoint
;
6472 struct traceframe
*tframe
;
6473 unsigned char *block
;
6474 struct traceframe ipa_tframe
;
6476 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6477 offsetof (struct traceframe
, data
)))
6478 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6480 if (ipa_tframe
.tpnum
== 0)
6482 internal_error (__FILE__
, __LINE__
,
6483 "Uploading: No (more) fast traceframes, but"
6484 " ipa_traceframe_count == %u??\n",
6485 ipa_traceframe_write_count
6486 - ipa_traceframe_read_count
);
6489 /* Note that this will be incorrect for multi-location
6491 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6493 tframe
= add_traceframe (tpoint
);
6496 trace_buffer_is_full
= 1;
6497 trace_debug ("Uploading: trace buffer is full");
6501 /* Copy the whole set of blocks in one go for now. FIXME:
6502 split this in smaller blocks. */
6503 block
= add_traceframe_block (tframe
, tpoint
,
6504 ipa_tframe
.data_size
);
6507 if (read_inferior_memory (tf
6508 + offsetof (struct traceframe
, data
),
6509 block
, ipa_tframe
.data_size
))
6510 error ("Uploading: Couldn't read traceframe data at %s\n",
6511 paddress (tf
+ offsetof (struct traceframe
, data
)));
6514 trace_debug ("Uploading: traceframe didn't fit");
6515 finish_traceframe (tframe
);
6518 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6520 /* If we freed the traceframe that wrapped around, go back
6521 to the non-wrap case. */
6522 if (tf
< ipa_trace_buffer_ctrl
.start
)
6524 trace_debug ("Lib: Discarding past the wraparound");
6525 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6527 ipa_trace_buffer_ctrl
.start
= tf
;
6528 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6529 ++ipa_traceframe_read_count
;
6531 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6532 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6534 trace_debug ("Lib: buffer is fully empty. "
6535 "Trace buffer [%d] start=%d free=%d endfree=%d",
6537 (int) (ipa_trace_buffer_ctrl
.start
6538 - ipa_trace_buffer_lo
),
6539 (int) (ipa_trace_buffer_ctrl
.free
6540 - ipa_trace_buffer_lo
),
6541 (int) (ipa_trace_buffer_ctrl
.end_free
6542 - ipa_trace_buffer_lo
));
6544 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6545 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6546 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6547 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6550 trace_debug ("Uploaded a traceframe\n"
6551 "Lib: Trace buffer [%d] start=%d free=%d "
6552 "endfree=%d wrap=%d hi=%d",
6554 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6555 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6556 (int) (ipa_trace_buffer_ctrl
.end_free
6557 - ipa_trace_buffer_lo
),
6558 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6559 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6562 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6563 (unsigned char *) &ipa_trace_buffer_ctrl
,
6564 sizeof (struct ipa_trace_buffer_control
)))
6567 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6568 ipa_traceframe_read_count
);
6570 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6574 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6575 about_to_request_buffer_space_bkpt
= NULL
;
6579 if (trace_buffer_is_full
)
6584 #ifdef IN_PROCESS_AGENT
6586 IP_AGENT_EXPORT_VAR
int ust_loaded
;
6587 IP_AGENT_EXPORT_VAR
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6591 /* Static tracepoints. */
6593 /* UST puts a "struct tracepoint" in the global namespace, which
6594 conflicts with our tracepoint. Arguably, being a library, it
6595 shouldn't take ownership of such a generic name. We work around it
6597 #define tracepoint ust_tracepoint
6598 #include <ust/ust.h>
6601 extern int serialize_to_text (char *outbuf
, int bufsize
,
6602 const char *fmt
, va_list ap
);
6604 #define GDB_PROBE_NAME "gdb"
6606 /* We dynamically search for the UST symbols instead of linking them
6607 in. This lets the user decide if the application uses static
6608 tracepoints, instead of always pulling libust.so in. This vector
6609 holds pointers to all functions we care about. */
6613 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6614 const char *fmt
, va_list ap
);
6616 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6617 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6619 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6621 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6624 void (*marker_iter_start
) (struct marker_iter
*iter
);
6625 void (*marker_iter_next
) (struct marker_iter
*iter
);
6626 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6627 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6632 /* Cast through typeof to catch incompatible API changes. Since UST
6633 only builds with gcc, we can freely use gcc extensions here
6635 #define GET_UST_SYM(SYM) \
6638 if (ust_ops.SYM == NULL) \
6639 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6640 if (ust_ops.SYM == NULL) \
6644 #define USTF(SYM) ust_ops.SYM
6646 /* Get pointers to all libust.so functions we care about. */
6651 GET_UST_SYM (serialize_to_text
);
6653 GET_UST_SYM (ltt_probe_register
);
6654 GET_UST_SYM (ltt_probe_unregister
);
6655 GET_UST_SYM (ltt_marker_connect
);
6656 GET_UST_SYM (ltt_marker_disconnect
);
6658 GET_UST_SYM (marker_iter_start
);
6659 GET_UST_SYM (marker_iter_next
);
6660 GET_UST_SYM (marker_iter_stop
);
6661 GET_UST_SYM (marker_iter_reset
);
6667 /* Given an UST marker, return the matching gdb static tracepoint.
6668 The match is done by address. */
6670 static struct tracepoint
*
6671 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6673 struct tracepoint
*tpoint
;
6675 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6677 if (tpoint
->type
!= static_tracepoint
)
6680 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6687 /* The probe function we install on lttng/ust markers. Whenever a
6688 probed ust marker is hit, this function is called. This is similar
6689 to gdb_collect, only for static tracepoints, instead of fast
6693 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6694 struct registers
*regs
, void *call_private
,
6695 const char *fmt
, va_list *args
)
6697 struct tracepoint
*tpoint
;
6698 struct static_tracepoint_ctx ctx
;
6699 const struct target_desc
*ipa_tdesc
;
6701 /* Don't do anything until the trace run is completely set up. */
6704 trace_debug ("gdb_probe: not tracing\n");
6708 ipa_tdesc
= get_ipa_tdesc (ipa_tdesc_idx
);
6709 ctx
.base
.type
= static_tracepoint
;
6710 ctx
.regcache_initted
= 0;
6715 /* Wrap the regblock in a register cache (in the stack, we don't
6716 want to malloc here). */
6717 ctx
.regspace
= alloca (ipa_tdesc
->registers_size
);
6718 if (ctx
.regspace
== NULL
)
6720 trace_debug ("Trace buffer block allocation failed, skipping");
6724 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6727 trace_debug ("gdb_probe: marker not known: "
6728 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6729 mdata
->location
, mdata
->channel
,
6730 mdata
->name
, mdata
->format
);
6734 if (!tpoint
->enabled
)
6736 trace_debug ("gdb_probe: tracepoint disabled");
6740 ctx
.tpoint
= tpoint
;
6742 trace_debug ("gdb_probe: collecting marker: "
6743 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6744 mdata
->location
, mdata
->channel
,
6745 mdata
->name
, mdata
->format
);
6747 /* Test the condition if present, and collect if true. */
6748 if (tpoint
->cond
== NULL
6749 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6752 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6753 tpoint
->address
, tpoint
);
6755 if (stopping_tracepoint
6756 || trace_buffer_is_full
6757 || expr_eval_result
!= expr_eval_no_error
)
6762 /* If there was a condition and it evaluated to false, the only
6763 way we would stop tracing is if there was an error during
6764 condition expression evaluation. */
6765 if (expr_eval_result
!= expr_eval_no_error
)
6770 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6771 static tracepoint string data. This is a string passed to the
6772 tracing library by the user, at the time of the tracepoint marker
6773 call. E.g., in the UST marker call:
6775 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6777 the collected data is "str FOOBAZ".
6781 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6782 struct traceframe
*tframe
)
6784 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6785 unsigned char *bufspace
;
6788 unsigned short blocklen
;
6792 trace_debug ("Wanted to collect static trace data, "
6793 "but there's no static trace data");
6797 va_copy (copy
, *umd
->args
);
6798 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6801 trace_debug ("Want to collect ust data");
6803 /* 'S' + size + string */
6804 bufspace
= add_traceframe_block (tframe
, umd
->tpoint
,
6805 1 + sizeof (blocklen
) + size
+ 1);
6806 if (bufspace
== NULL
)
6808 trace_debug ("Trace buffer block allocation failed, skipping");
6812 /* Identify a static trace data block. */
6815 blocklen
= size
+ 1;
6816 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6818 va_copy (copy
, *umd
->args
);
6819 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6820 size
+ 1, umd
->fmt
, copy
);
6823 trace_debug ("Storing static tracepoint data in regblock: %s",
6824 bufspace
+ 1 + sizeof (blocklen
));
6827 /* The probe to register with lttng/ust. */
6828 static struct ltt_available_probe gdb_ust_probe
=
6835 #endif /* HAVE_UST */
6836 #endif /* IN_PROCESS_AGENT */
6838 #ifndef IN_PROCESS_AGENT
6840 /* Ask the in-process agent to run a command. Since we don't want to
6841 have to handle the IPA hitting breakpoints while running the
6842 command, we pause all threads, remove all breakpoints, and then set
6843 the helper thread re-running. We communicate with the helper
6844 thread by means of direct memory xfering, and a socket for
6848 run_inferior_command (char *cmd
, int len
)
6851 int pid
= ptid_get_pid (current_ptid
);
6853 trace_debug ("run_inferior_command: running: %s", cmd
);
6856 uninsert_all_breakpoints ();
6858 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6860 reinsert_all_breakpoints ();
6866 #else /* !IN_PROCESS_AGENT */
6868 #include <sys/socket.h>
6871 #ifndef UNIX_PATH_MAX
6872 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6875 /* Where we put the socked used for synchronization. */
6876 #define SOCK_DIR P_tmpdir
6878 /* Thread ID of the helper thread. GDBserver reads this to know which
6879 is the help thread. This is an LWP id on Linux. */
6881 IP_AGENT_EXPORT_VAR
int helper_thread_id
;
6885 init_named_socket (const char *name
)
6888 struct sockaddr_un addr
;
6890 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6893 warning ("socket creation failed: %s", strerror (errno
));
6897 addr
.sun_family
= AF_UNIX
;
6899 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6900 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6902 result
= access (name
, F_OK
);
6906 result
= unlink (name
);
6909 warning ("unlink failed: %s", strerror (errno
));
6913 warning ("socket %s already exists; overwriting", name
);
6916 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6919 warning ("bind failed: %s", strerror (errno
));
6924 result
= listen (fd
, 1);
6927 warning ("listen: %s", strerror (errno
));
6935 static char agent_socket_name
[UNIX_PATH_MAX
];
6938 gdb_agent_socket_init (void)
6942 result
= xsnprintf (agent_socket_name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6943 SOCK_DIR
, getpid ());
6944 if (result
>= UNIX_PATH_MAX
)
6946 trace_debug ("string overflow allocating socket name");
6950 fd
= init_named_socket (agent_socket_name
);
6952 warning ("Error initializing named socket (%s) for communication with the "
6953 "ust helper thread. Check that directory exists and that it "
6954 "is writable.", agent_socket_name
);
6961 /* The next marker to be returned on a qTsSTM command. */
6962 static const struct marker
*next_st
;
6964 /* Returns the first known marker. */
6969 struct marker_iter iter
;
6971 USTF(marker_iter_reset
) (&iter
);
6972 USTF(marker_iter_start
) (&iter
);
6977 /* Returns the marker following M. */
6979 const struct marker
*
6980 next_marker (const struct marker
*m
)
6982 struct marker_iter iter
;
6984 USTF(marker_iter_reset
) (&iter
);
6985 USTF(marker_iter_start
) (&iter
);
6987 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6989 if (iter
.marker
== m
)
6991 USTF(marker_iter_next
) (&iter
);
6999 /* Return an hexstr version of the STR C string, fit for sending to
7003 cstr_to_hexstr (const char *str
)
7005 int len
= strlen (str
);
7006 char *hexstr
= xmalloc (len
* 2 + 1);
7007 bin2hex ((gdb_byte
*) str
, hexstr
, len
);
7011 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7015 response_ust_marker (char *packet
, const struct marker
*st
)
7017 char *strid
, *format
, *tmp
;
7019 next_st
= next_marker (st
);
7021 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7022 strlen (st
->name
) + 1);
7023 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7025 strid
= cstr_to_hexstr (tmp
);
7028 format
= cstr_to_hexstr (st
->format
);
7030 sprintf (packet
, "m%s:%s:%s",
7031 paddress ((uintptr_t) st
->location
),
7039 /* Return the first static tracepoint, and initialize the state
7040 machine that will iterate through all the static tracepoints. */
7043 cmd_qtfstm (char *packet
)
7045 trace_debug ("Returning first trace state variable definition");
7047 if (first_marker ())
7048 response_ust_marker (packet
, first_marker ());
7050 strcpy (packet
, "l");
7053 /* Return additional trace state variable definitions. */
7056 cmd_qtsstm (char *packet
)
7058 trace_debug ("Returning static tracepoint");
7061 response_ust_marker (packet
, next_st
);
7063 strcpy (packet
, "l");
7066 /* Disconnect the GDB probe from a marker at a given address. */
7069 unprobe_marker_at (char *packet
)
7073 struct marker_iter iter
;
7075 p
+= sizeof ("unprobe_marker_at:") - 1;
7077 p
= unpack_varlen_hex (p
, &address
);
7079 USTF(marker_iter_reset
) (&iter
);
7080 USTF(marker_iter_start
) (&iter
);
7081 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7082 if ((uintptr_t ) iter
.marker
->location
== address
)
7086 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7090 warning ("could not disable marker %s/%s",
7091 iter
.marker
->channel
, iter
.marker
->name
);
7096 /* Connect the GDB probe to a marker at a given address. */
7099 probe_marker_at (char *packet
)
7103 struct marker_iter iter
;
7106 p
+= sizeof ("probe_marker_at:") - 1;
7108 p
= unpack_varlen_hex (p
, &address
);
7110 USTF(marker_iter_reset
) (&iter
);
7112 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7114 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7115 if ((uintptr_t ) m
->location
== address
)
7119 trace_debug ("found marker for address. "
7120 "ltt_marker_connect (marker = %s/%s)",
7121 m
->channel
, m
->name
);
7123 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7125 if (result
&& result
!= -EEXIST
)
7126 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7127 m
->channel
, m
->name
, -result
);
7131 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7132 m
->channel
, m
->name
);
7136 strcpy (packet
, "OK");
7140 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7145 cmd_qtstmat (char *packet
)
7149 struct marker_iter iter
;
7152 p
+= sizeof ("qTSTMat:") - 1;
7154 p
= unpack_varlen_hex (p
, &address
);
7156 USTF(marker_iter_reset
) (&iter
);
7158 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7160 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7161 if ((uintptr_t ) m
->location
== address
)
7163 response_ust_marker (packet
, m
);
7167 strcpy (packet
, "l");
7177 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7180 #endif /* HAVE_UST */
7182 #include <sys/syscall.h>
7185 gdb_agent_remove_socket (void)
7187 unlink (agent_socket_name
);
7190 /* Helper thread of agent. */
7193 gdb_agent_helper_thread (void *arg
)
7197 atexit (gdb_agent_remove_socket
);
7201 listen_fd
= gdb_agent_socket_init ();
7203 if (helper_thread_id
== 0)
7204 helper_thread_id
= syscall (SYS_gettid
);
7206 if (listen_fd
== -1)
7208 warning ("could not create sync socket\n");
7215 struct sockaddr_un sockaddr
;
7221 tmp
= sizeof (sockaddr
);
7225 fd
= accept (listen_fd
, (struct sockaddr
*) &sockaddr
, &tmp
);
7227 /* It seems an ERESTARTSYS can escape out of accept. */
7228 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7232 warning ("Accept returned %d, error: %s\n",
7233 fd
, strerror (errno
));
7239 ret
= read (fd
, buf
, 1);
7240 } while (ret
== -1 && errno
== EINTR
);
7244 warning ("reading socket (fd=%d) failed with %s",
7245 fd
, strerror (errno
));
7252 if (startswith (cmd_buf
, "close"))
7257 else if (strcmp ("qTfSTM", cmd_buf
) == 0)
7259 cmd_qtfstm (cmd_buf
);
7261 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7263 cmd_qtsstm (cmd_buf
);
7265 else if (startswith (cmd_buf
, "unprobe_marker_at:"))
7267 unprobe_marker_at (cmd_buf
);
7269 else if (startswith (cmd_buf
, "probe_marker_at:"))
7271 probe_marker_at (cmd_buf
);
7273 else if (startswith (cmd_buf
, "qTSTMat:"))
7275 cmd_qtstmat (cmd_buf
);
7277 #endif /* HAVE_UST */
7280 /* Fix compiler's warning: ignoring return value of 'write'. */
7281 ret
= write (fd
, buf
, 1);
7287 unlink (agent_socket_name
);
7289 /* Sleep endlessly to wait the whole inferior stops. This
7290 thread can not exit because GDB or GDBserver may still need
7291 'current_thread' (representing this thread) to access
7292 inferior memory. Otherwise, this thread exits earlier than
7293 other threads, and 'current_thread' is set to NULL. */
7304 #include <pthread.h>
7307 IP_AGENT_EXPORT_VAR
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7311 gdb_agent_init (void)
7318 /* We want the helper thread to be as transparent as possible, so
7319 have it inherit an all-signals-blocked mask. */
7321 sigfillset (&new_mask
);
7322 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7324 perror_with_name ("pthread_sigmask (1)");
7326 res
= pthread_create (&thread
,
7328 gdb_agent_helper_thread
,
7331 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7333 perror_with_name ("pthread_sigmask (2)");
7335 while (helper_thread_id
== 0)
7343 #include <sys/mman.h>
7346 IP_AGENT_EXPORT_VAR
char *gdb_tp_heap_buffer
;
7347 IP_AGENT_EXPORT_VAR
char *gdb_jump_pad_buffer
;
7348 IP_AGENT_EXPORT_VAR
char *gdb_jump_pad_buffer_end
;
7349 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer
;
7350 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer_end
;
7351 IP_AGENT_EXPORT_VAR
char *gdb_trampoline_buffer_error
;
7353 /* Record the result of getting buffer space for fast tracepoint
7354 trampolines. Any error message is copied, since caller may not be
7355 using persistent storage. */
7358 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7360 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7361 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7363 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7365 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7368 static void __attribute__ ((constructor
))
7369 initialize_tracepoint_ftlib (void)
7371 initialize_tracepoint ();
7376 #ifndef HAVE_GETAUXVAL
7377 /* Retrieve the value of TYPE from the auxiliary vector. If TYPE is not
7378 found, 0 is returned. This function is provided if glibc is too old. */
7381 getauxval (unsigned long type
)
7383 unsigned long data
[2];
7384 FILE *f
= fopen ("/proc/self/auxv", "r");
7385 unsigned long value
= 0;
7390 while (fread (data
, sizeof (data
), 1, f
) > 0)
7392 if (data
[0] == type
)
7404 #endif /* IN_PROCESS_AGENT */
7406 /* Return a timestamp, expressed as microseconds of the usual Unix
7407 time. (As the result is a 64-bit number, it will not overflow any
7411 get_timestamp (void)
7415 if (gettimeofday (&tv
, 0) != 0)
7418 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7422 initialize_tracepoint (void)
7424 /* Start with the default size. */
7425 init_trace_buffer (DEFAULT_TRACE_BUFFER_SIZE
);
7427 /* Wire trace state variable 1 to be the timestamp. This will be
7428 uploaded to GDB upon connection and become one of its trace state
7429 variables. (In case you're wondering, if GDB already has a trace
7430 variable numbered 1, it will be renumbered.) */
7431 create_trace_state_variable (1, 0);
7432 set_trace_state_variable_name (1, "trace_timestamp");
7433 set_trace_state_variable_getter (1, get_timestamp
);
7435 #ifdef IN_PROCESS_AGENT
7438 size_t jump_pad_size
;
7440 pagesize
= sysconf (_SC_PAGE_SIZE
);
7442 perror_with_name ("sysconf");
7444 #define SCRATCH_BUFFER_NPAGES 20
7446 jump_pad_size
= pagesize
* SCRATCH_BUFFER_NPAGES
;
7448 gdb_tp_heap_buffer
= (char *) xmalloc (5 * 1024 * 1024);
7449 gdb_jump_pad_buffer
= (char *) alloc_jump_pad_buffer (jump_pad_size
);
7450 if (gdb_jump_pad_buffer
== NULL
)
7451 perror_with_name ("mmap");
7452 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ jump_pad_size
;
7455 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7457 /* It's not a fatal error for something to go wrong with trampoline
7458 buffer setup, but it can be mysterious, so create a channel to
7459 report back on what went wrong, using a fixed size since we may
7460 not be able to allocate space later when the problem occurs. */
7461 gdb_trampoline_buffer_error
= (char *) xmalloc (IPA_BUFSIZ
);
7463 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7465 initialize_low_tracepoint ();