1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /* This file is built for both GDBserver, and the in-process
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
59 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
62 trace_vdebug (const char *fmt
, ...)
68 vsprintf (buf
, fmt
, ap
);
69 fprintf (stderr
, PROG
"/tracepoint: %s\n", buf
);
73 #define trace_debug_1(level, fmt, args...) \
75 if (level <= debug_threads) \
76 trace_vdebug ((fmt), ##args); \
79 #define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
83 # define ATTR_USED __attribute__((used))
84 # define ATTR_NOINLINE __attribute__((noinline))
85 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
88 # define ATTR_NOINLINE
89 # define ATTR_CONSTRUCTOR
92 /* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
95 #ifdef IN_PROCESS_AGENT
96 # if defined _WIN32 || defined __CYGWIN__
97 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
100 # define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
103 # define IP_AGENT_EXPORT ATTR_USED
107 # define IP_AGENT_EXPORT
110 /* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112 #ifdef IN_PROCESS_AGENT
113 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
116 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
117 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
118 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
119 # define collecting gdb_agent_collecting
120 # define gdb_collect gdb_agent_gdb_collect
121 # define stop_tracing gdb_agent_stop_tracing
122 # define flush_trace_buffer gdb_agent_flush_trace_buffer
123 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
124 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
125 # define stopping_tracepoint gdb_agent_stopping_tracepoint
126 # define expr_eval_result gdb_agent_expr_eval_result
127 # define error_tracepoint gdb_agent_error_tracepoint
128 # define tracepoints gdb_agent_tracepoints
129 # define tracing gdb_agent_tracing
130 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
131 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
132 # define trace_buffer_lo gdb_agent_trace_buffer_lo
133 # define trace_buffer_hi gdb_agent_trace_buffer_hi
134 # define traceframe_read_count gdb_agent_traceframe_read_count
135 # define traceframe_write_count gdb_agent_traceframe_write_count
136 # define traceframes_created gdb_agent_traceframes_created
137 # define trace_state_variables gdb_agent_trace_state_variables
138 # define get_raw_reg gdb_agent_get_raw_reg
139 # define get_trace_state_variable_value \
140 gdb_agent_get_trace_state_variable_value
141 # define set_trace_state_variable_value \
142 gdb_agent_set_trace_state_variable_value
143 # define ust_loaded gdb_agent_ust_loaded
144 # define helper_thread_id gdb_agent_helper_thread_id
145 # define cmd_buf gdb_agent_cmd_buf
148 #ifndef IN_PROCESS_AGENT
150 /* Addresses of in-process agent's symbols GDBserver cares about. */
152 struct ipa_sym_addresses
154 CORE_ADDR addr_gdb_tp_heap_buffer
;
155 CORE_ADDR addr_gdb_jump_pad_buffer
;
156 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
157 CORE_ADDR addr_gdb_trampoline_buffer
;
158 CORE_ADDR addr_gdb_trampoline_buffer_end
;
159 CORE_ADDR addr_gdb_trampoline_buffer_error
;
160 CORE_ADDR addr_collecting
;
161 CORE_ADDR addr_gdb_collect
;
162 CORE_ADDR addr_stop_tracing
;
163 CORE_ADDR addr_flush_trace_buffer
;
164 CORE_ADDR addr_about_to_request_buffer_space
;
165 CORE_ADDR addr_trace_buffer_is_full
;
166 CORE_ADDR addr_stopping_tracepoint
;
167 CORE_ADDR addr_expr_eval_result
;
168 CORE_ADDR addr_error_tracepoint
;
169 CORE_ADDR addr_tracepoints
;
170 CORE_ADDR addr_tracing
;
171 CORE_ADDR addr_trace_buffer_ctrl
;
172 CORE_ADDR addr_trace_buffer_ctrl_curr
;
173 CORE_ADDR addr_trace_buffer_lo
;
174 CORE_ADDR addr_trace_buffer_hi
;
175 CORE_ADDR addr_traceframe_read_count
;
176 CORE_ADDR addr_traceframe_write_count
;
177 CORE_ADDR addr_traceframes_created
;
178 CORE_ADDR addr_trace_state_variables
;
179 CORE_ADDR addr_get_raw_reg
;
180 CORE_ADDR addr_get_trace_state_variable_value
;
181 CORE_ADDR addr_set_trace_state_variable_value
;
182 CORE_ADDR addr_ust_loaded
;
191 IPA_SYM(gdb_tp_heap_buffer
),
192 IPA_SYM(gdb_jump_pad_buffer
),
193 IPA_SYM(gdb_jump_pad_buffer_end
),
194 IPA_SYM(gdb_trampoline_buffer
),
195 IPA_SYM(gdb_trampoline_buffer_end
),
196 IPA_SYM(gdb_trampoline_buffer_error
),
198 IPA_SYM(gdb_collect
),
199 IPA_SYM(stop_tracing
),
200 IPA_SYM(flush_trace_buffer
),
201 IPA_SYM(about_to_request_buffer_space
),
202 IPA_SYM(trace_buffer_is_full
),
203 IPA_SYM(stopping_tracepoint
),
204 IPA_SYM(expr_eval_result
),
205 IPA_SYM(error_tracepoint
),
206 IPA_SYM(tracepoints
),
208 IPA_SYM(trace_buffer_ctrl
),
209 IPA_SYM(trace_buffer_ctrl_curr
),
210 IPA_SYM(trace_buffer_lo
),
211 IPA_SYM(trace_buffer_hi
),
212 IPA_SYM(traceframe_read_count
),
213 IPA_SYM(traceframe_write_count
),
214 IPA_SYM(traceframes_created
),
215 IPA_SYM(trace_state_variables
),
216 IPA_SYM(get_raw_reg
),
217 IPA_SYM(get_trace_state_variable_value
),
218 IPA_SYM(set_trace_state_variable_value
),
222 static struct ipa_sym_addresses ipa_sym_addrs
;
224 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
226 /* Returns true if both the in-process agent library and the static
227 tracepoints libraries are loaded in the inferior, and agent has
228 capability on static tracepoints. */
231 in_process_agent_supports_ust (void)
235 if (!agent_loaded_p ())
237 warning ("In-process agent not loaded");
241 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE
))
243 /* Agent understands static tracepoint, then check whether UST is in
244 fact loaded in the inferior. */
245 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
247 warning ("Error reading ust_loaded in lib");
258 write_e_ipa_not_loaded (char *buffer
)
261 "E.In-process agent library not loaded in process. "
262 "Fast and static tracepoints unavailable.");
265 /* Write an error to BUFFER indicating that UST isn't loaded in the
269 write_e_ust_not_loaded (char *buffer
)
273 "E.UST library not loaded in process. "
274 "Static tracepoints unavailable.");
276 sprintf (buffer
, "E.GDBserver was built without static tracepoints support");
280 /* If the in-process agent library isn't loaded in the inferior, write
281 an error to BUFFER, and return 1. Otherwise, return 0. */
284 maybe_write_ipa_not_loaded (char *buffer
)
286 if (!agent_loaded_p ())
288 write_e_ipa_not_loaded (buffer
);
294 /* If the in-process agent library and the ust (static tracepoints)
295 library aren't loaded in the inferior, write an error to BUFFER,
296 and return 1. Otherwise, return 0. */
299 maybe_write_ipa_ust_not_loaded (char *buffer
)
301 if (!agent_loaded_p ())
303 write_e_ipa_not_loaded (buffer
);
306 else if (!in_process_agent_supports_ust ())
308 write_e_ust_not_loaded (buffer
);
314 /* Cache all future symbols that the tracepoints module might request.
315 We can not request symbols at arbitrary states in the remote
316 protocol, only when the client tells us that new symbols are
317 available. So when we load the in-process library, make sure to
318 check the entire list. */
321 tracepoint_look_up_symbols (void)
325 if (agent_loaded_p ())
328 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
331 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
333 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
336 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
341 agent_look_up_symbols (NULL
);
346 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
347 of the "stop_tracing" function. When this breakpoint is hit,
348 tracing stopped in the IPA for some reason. E.g., due to
349 tracepoint reaching the pass count, hitting conditional expression
350 evaluation error, etc.
352 The IPA's trace buffer is never in circular tracing mode: instead,
353 GDBserver's is, and whenever the in-process buffer fills, it calls
354 "flush_trace_buffer", which triggers an internal breakpoint.
355 GDBserver reacts to this breakpoint by pulling the meanwhile
356 collected data. Old frames discarding is always handled on the
359 #ifdef IN_PROCESS_AGENT
361 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
363 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
367 /* Call this in the functions where GDBserver places a breakpoint, so
368 that the compiler doesn't try to be clever and skip calling the
369 function at all. This is necessary, even if we tell the compiler
370 to not inline said functions. */
372 #if defined(__GNUC__)
373 # define UNKNOWN_SIDE_EFFECTS() asm ("")
375 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
378 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
381 /* GDBserver places breakpoint here. */
382 UNKNOWN_SIDE_EFFECTS();
385 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
386 flush_trace_buffer (void)
388 /* GDBserver places breakpoint here. */
389 UNKNOWN_SIDE_EFFECTS();
394 #ifndef IN_PROCESS_AGENT
396 tracepoint_handler (CORE_ADDR address
)
398 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
403 /* Breakpoint at "stop_tracing" in the inferior lib. */
404 struct breakpoint
*stop_tracing_bkpt
;
405 static int stop_tracing_handler (CORE_ADDR
);
407 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
408 struct breakpoint
*flush_trace_buffer_bkpt
;
409 static int flush_trace_buffer_handler (CORE_ADDR
);
411 static void download_trace_state_variables (void);
412 static void upload_fast_traceframes (void);
414 static int run_inferior_command (char *cmd
, int len
);
417 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
419 return read_inferior_memory (symaddr
, (unsigned char *) val
,
424 static int tracepoint_send_agent (struct tracepoint
*tpoint
);
427 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
429 return read_inferior_memory (symaddr
, (unsigned char *) val
,
434 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
436 void *pval
= (void *) (uintptr_t) val
;
439 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
440 *val
= (uintptr_t) pval
;
445 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
447 void *pval
= (void *) (uintptr_t) val
;
448 return write_inferior_memory (symaddr
,
449 (unsigned char *) &pval
, sizeof (pval
));
453 write_inferior_integer (CORE_ADDR symaddr
, int val
)
455 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
459 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
461 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
464 static CORE_ADDR
target_malloc (ULONGEST size
);
465 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
467 #define COPY_FIELD_TO_BUF(BUF, OBJ, FIELD) \
469 memcpy (BUF, &(OBJ)->FIELD, sizeof ((OBJ)->FIELD)); \
470 BUF += sizeof ((OBJ)->FIELD); \
475 /* Operations on various types of tracepoint actions. */
477 struct tracepoint_action
;
479 struct tracepoint_action_ops
481 /* Download tracepoint action ACTION to IPA. Return the address of action
483 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
485 /* Send ACTION to agent via command buffer started from BUFFER. Return
486 updated head of command buffer. */
487 char* (*send
) (char *buffer
, const struct tracepoint_action
*action
);
490 /* Base action. Concrete actions inherit this. */
492 struct tracepoint_action
494 #ifndef IN_PROCESS_AGENT
495 const struct tracepoint_action_ops
*ops
;
500 /* An 'M' (collect memory) action. */
501 struct collect_memory_action
503 struct tracepoint_action base
;
510 /* An 'R' (collect registers) action. */
512 struct collect_registers_action
514 struct tracepoint_action base
;
517 /* An 'X' (evaluate expression) action. */
519 struct eval_expr_action
521 struct tracepoint_action base
;
523 struct agent_expr
*expr
;
526 /* An 'L' (collect static trace data) action. */
527 struct collect_static_trace_data_action
529 struct tracepoint_action base
;
532 #ifndef IN_PROCESS_AGENT
534 m_tracepoint_action_download (const struct tracepoint_action
*action
)
536 int size_in_ipa
= (sizeof (struct collect_memory_action
)
537 - offsetof (struct tracepoint_action
, type
));
538 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
540 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
546 m_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
548 struct collect_memory_action
*maction
549 = (struct collect_memory_action
*) action
;
551 COPY_FIELD_TO_BUF (buffer
, maction
, addr
);
552 COPY_FIELD_TO_BUF (buffer
, maction
, len
);
553 COPY_FIELD_TO_BUF (buffer
, maction
, basereg
);
558 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
560 m_tracepoint_action_download
,
561 m_tracepoint_action_send
,
565 r_tracepoint_action_download (const struct tracepoint_action
*action
)
567 int size_in_ipa
= (sizeof (struct collect_registers_action
)
568 - offsetof (struct tracepoint_action
, type
));
569 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
571 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
578 r_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
583 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
585 r_tracepoint_action_download
,
586 r_tracepoint_action_send
,
589 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
592 x_tracepoint_action_download (const struct tracepoint_action
*action
)
594 int size_in_ipa
= (sizeof (struct eval_expr_action
)
595 - offsetof (struct tracepoint_action
, type
));
596 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
599 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
601 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
602 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
603 - offsetof (struct tracepoint_action
, type
),
609 /* Copy agent expression AEXPR to buffer pointed by P. If AEXPR is NULL,
610 copy 0 to P. Return updated header of buffer. */
613 agent_expr_send (char *p
, const struct agent_expr
*aexpr
)
615 /* Copy the length of condition first, and then copy its
624 memcpy (p
, &aexpr
->length
, 4);
627 memcpy (p
, aexpr
->bytes
, aexpr
->length
);
634 x_tracepoint_action_send ( char *buffer
, const struct tracepoint_action
*action
)
636 struct eval_expr_action
*eaction
= (struct eval_expr_action
*) action
;
638 return agent_expr_send (buffer
, eaction
->expr
);
641 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
643 x_tracepoint_action_download
,
644 x_tracepoint_action_send
,
648 l_tracepoint_action_download (const struct tracepoint_action
*action
)
650 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
651 - offsetof (struct tracepoint_action
, type
));
652 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
654 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
661 l_tracepoint_action_send (char *buffer
, const struct tracepoint_action
*action
)
666 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
668 l_tracepoint_action_download
,
669 l_tracepoint_action_send
,
673 /* This structure describes a piece of the source-level definition of
674 the tracepoint. The contents are not interpreted by the target,
675 but preserved verbatim for uploading upon reconnection. */
679 /* The type of string, such as "cond" for a conditional. */
682 /* The source-level string itself. For the sake of target
683 debugging, we store it in plaintext, even though it is always
684 transmitted in hex. */
687 /* Link to the next one in the list. We link them in the order
688 received, in case some make up an ordered list of commands or
690 struct source_string
*next
;
695 /* Trap based tracepoint. */
698 /* A fast tracepoint implemented with a jump instead of a trap. */
701 /* A static tracepoint, implemented by a program call into a tracing
706 struct tracepoint_hit_ctx
;
708 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
711 /* The definition of a tracepoint. */
713 /* Tracepoints may have multiple locations, each at a different
714 address. This can occur with optimizations, template
715 instantiation, etc. Since the locations may be in different
716 scopes, the conditions and actions may be different for each
717 location. Our target version of tracepoints is more like GDB's
718 notion of "breakpoint locations", but we have almost nothing that
719 is not per-location, so we bother having two kinds of objects. The
720 key consequence is that numbers are not unique, and that it takes
721 both number and address to identify a tracepoint uniquely. */
725 /* The number of the tracepoint, as specified by GDB. Several
726 tracepoint objects here may share a number. */
729 /* Address at which the tracepoint is supposed to trigger. Several
730 tracepoints may share an address. */
733 /* Tracepoint type. */
734 enum tracepoint_type type
;
736 /* True if the tracepoint is currently enabled. */
739 /* The number of single steps that will be performed after each
743 /* The number of times the tracepoint may be hit before it will
744 terminate the entire tracing run. */
747 /* Pointer to the agent expression that is the tracepoint's
748 conditional, or NULL if the tracepoint is unconditional. */
749 struct agent_expr
*cond
;
751 /* The list of actions to take when the tracepoint triggers. */
753 struct tracepoint_action
**actions
;
755 /* Count of the times we've hit this tracepoint during the run.
756 Note that while-stepping steps are not counted as "hits". */
759 /* Cached sum of the sizes of traceframes created by this point. */
760 uint64_t traceframe_usage
;
762 CORE_ADDR compiled_cond
;
764 /* Link to the next tracepoint in the list. */
765 struct tracepoint
*next
;
767 #ifndef IN_PROCESS_AGENT
768 /* The list of actions to take when the tracepoint triggers, in
769 string/packet form. */
772 /* The collection of strings that describe the tracepoint as it was
773 entered into GDB. These are not used by the target, but are
774 reported back to GDB upon reconnection. */
775 struct source_string
*source_strings
;
777 /* The number of bytes displaced by fast tracepoints. It may subsume
778 multiple instructions, for multi-byte fast tracepoints. This
779 field is only valid for fast tracepoints. */
782 /* Only for fast tracepoints. */
783 CORE_ADDR obj_addr_on_target
;
785 /* Address range where the original instruction under a fast
786 tracepoint was relocated to. (_end is actually one byte past
788 CORE_ADDR adjusted_insn_addr
;
789 CORE_ADDR adjusted_insn_addr_end
;
791 /* The address range of the piece of the jump pad buffer that was
792 assigned to this fast tracepoint. (_end is actually one byte
795 CORE_ADDR jump_pad_end
;
797 /* The address range of the piece of the trampoline buffer that was
798 assigned to this fast tracepoint. (_end is actually one byte
800 CORE_ADDR trampoline
;
801 CORE_ADDR trampoline_end
;
803 /* The list of actions to take while in a stepping loop. These
804 fields are only valid for patch-based tracepoints. */
805 int num_step_actions
;
806 struct tracepoint_action
**step_actions
;
807 /* Same, but in string/packet form. */
808 char **step_actions_str
;
810 /* Handle returned by the breakpoint or tracepoint module when we
811 inserted the trap or jump, or hooked into a static tracepoint.
812 NULL if we haven't inserted it yet. */
818 #ifndef IN_PROCESS_AGENT
820 /* Given `while-stepping', a thread may be collecting data for more
821 than one tracepoint simultaneously. On the other hand, the same
822 tracepoint with a while-stepping action may be hit by more than one
823 thread simultaneously (but not quite, each thread could be handling
824 a different step). Each thread holds a list of these objects,
825 representing the current step of each while-stepping action being
830 struct wstep_state
*next
;
832 /* The tracepoint number. */
834 /* The tracepoint's address. */
835 CORE_ADDR tp_address
;
837 /* The number of the current step in this 'while-stepping'
844 /* The linked list of all tracepoints. Marked explicitly as used as
845 the in-process library doesn't use it for the fast tracepoints
847 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
849 #ifndef IN_PROCESS_AGENT
851 /* Pointer to the last tracepoint in the list, new tracepoints are
852 linked in at the end. */
854 static struct tracepoint
*last_tracepoint
;
857 /* The first tracepoint to exceed its pass count. */
859 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
861 /* True if the trace buffer is full or otherwise no longer usable. */
863 IP_AGENT_EXPORT
int trace_buffer_is_full
;
865 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
867 #ifndef IN_PROCESS_AGENT
869 static const char *eval_result_names
[] =
871 "terror:in the attic", /* this should never be reported */
872 "terror:empty expression",
873 "terror:empty stack",
874 "terror:stack overflow",
875 "terror:stack underflow",
876 "terror:unhandled opcode",
877 "terror:unrecognized opcode",
878 "terror:divide by zero"
883 /* The tracepoint in which the error occurred. */
885 static struct tracepoint
*error_tracepoint
;
887 struct trace_state_variable
889 /* This is the name of the variable as used in GDB. The target
890 doesn't use the name, but needs to have it for saving and
891 reconnection purposes. */
894 /* This number identifies the variable uniquely. Numbers may be
895 assigned either by the target (in the case of builtin variables),
896 or by GDB, and are presumed unique during the course of a trace
900 /* The variable's initial value, a 64-bit signed integer always. */
901 LONGEST initial_value
;
903 /* The variable's value, a 64-bit signed integer always. */
906 /* Pointer to a getter function, used to supply computed values. */
907 LONGEST (*getter
) (void);
909 /* Link to the next variable. */
910 struct trace_state_variable
*next
;
913 /* Linked list of all trace state variables. */
915 #ifdef IN_PROCESS_AGENT
916 struct trace_state_variable
*alloced_trace_state_variables
;
919 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
921 /* The results of tracing go into a fixed-size space known as the
922 "trace buffer". Because usage follows a limited number of
923 patterns, we manage it ourselves rather than with malloc. Basic
924 rules are that we create only one trace frame at a time, each is
925 variable in size, they are never moved once created, and we only
926 discard if we are doing a circular buffer, and then only the oldest
927 ones. Each trace frame includes its own size, so we don't need to
928 link them together, and the trace frame number is relative to the
929 first one, so we don't need to record numbers. A trace frame also
930 records the number of the tracepoint that created it. The data
931 itself is a series of blocks, each introduced by a single character
932 and with a defined format. Each type of block has enough
933 type/length info to allow scanners to jump quickly from one block
934 to the next without reading each byte in the block. */
936 /* Trace buffer management would be simple - advance a free pointer
937 from beginning to end, then stop - were it not for the circular
938 buffer option, which is a useful way to prevent a trace run from
939 stopping prematurely because the buffer filled up. In the circular
940 case, the location of the first trace frame (trace_buffer_start)
941 moves as old trace frames are discarded. Also, since we grow trace
942 frames incrementally as actions are performed, we wrap around to
943 the beginning of the trace buffer. This is per-block, so each
944 block within a trace frame remains contiguous. Things get messy
945 when the wrapped-around trace frame is the one being discarded; the
946 free space ends up in two parts at opposite ends of the buffer. */
949 # if defined(__GNUC__)
950 # define ATTR_PACKED __attribute__ ((packed))
952 # define ATTR_PACKED /* nothing */
956 /* The data collected at a tracepoint hit. This object should be as
957 small as possible, since there may be a great many of them. We do
958 not need to keep a frame number, because they are all sequential
959 and there are no deletions; so the Nth frame in the buffer is
960 always frame number N. */
964 /* Number of the tracepoint that collected this traceframe. A value
965 of 0 indicates the current end of the trace buffer. We make this
966 a 16-bit field because it's never going to happen that GDB's
967 numbering of tracepoints reaches 32,000. */
970 /* The size of the data in this trace frame. We limit this to 32
971 bits, even on a 64-bit target, because it's just implausible that
972 one is validly going to collect 4 gigabytes of data at a single
974 unsigned int data_size
: 32;
976 /* The base of the trace data, which is contiguous from this point. */
977 unsigned char data
[0];
981 /* The traceframe to be used as the source of data to send back to
982 GDB. A value of -1 means to get data from the live program. */
984 int current_traceframe
= -1;
986 /* This flag is true if the trace buffer is circular, meaning that
987 when it fills, the oldest trace frames are discarded in order to
990 #ifndef IN_PROCESS_AGENT
991 static int circular_trace_buffer
;
994 /* Pointer to the block of memory that traceframes all go into. */
996 static unsigned char *trace_buffer_lo
;
998 /* Pointer to the end of the trace buffer, more precisely to the byte
999 after the end of the buffer. */
1001 static unsigned char *trace_buffer_hi
;
1003 /* Control structure holding the read/write/etc. pointers into the
1004 trace buffer. We need more than one of these to implement a
1005 transaction-like mechanism to garantees that both GDBserver and the
1006 in-process agent can try to change the trace buffer
1009 struct trace_buffer_control
1011 /* Pointer to the first trace frame in the buffer. In the
1012 non-circular case, this is equal to trace_buffer_lo, otherwise it
1013 moves around in the buffer. */
1014 unsigned char *start
;
1016 /* Pointer to the free part of the trace buffer. Note that we clear
1017 several bytes at and after this pointer, so that traceframe
1018 scans/searches terminate properly. */
1019 unsigned char *free
;
1021 /* Pointer to the byte after the end of the free part. Note that
1022 this may be smaller than trace_buffer_free in the circular case,
1023 and means that the free part is in two pieces. Initially it is
1024 equal to trace_buffer_hi, then is generally equivalent to
1025 trace_buffer_start. */
1026 unsigned char *end_free
;
1028 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
1029 this is the point at which the trace data breaks, and resumes at
1031 unsigned char *wrap
;
1034 /* Same as above, to be used by GDBserver when updating the in-process
1036 struct ipa_trace_buffer_control
1045 /* We have possibly both GDBserver and an inferior thread accessing
1046 the same IPA trace buffer memory. The IPA is the producer (tries
1047 to put new frames in the buffer), while GDBserver occasionally
1048 consumes them, that is, flushes the IPA's buffer into its own
1049 buffer. Both sides need to update the trace buffer control
1050 pointers (current head, tail, etc.). We can't use a global lock to
1051 synchronize the accesses, as otherwise we could deadlock GDBserver
1052 (if the thread holding the lock stops for a signal, say). So
1053 instead of that, we use a transaction scheme where GDBserver writes
1054 always prevail over the IPAs writes, and, we have the IPA detect
1055 the commit failure/overwrite, and retry the whole attempt. This is
1056 mainly implemented by having a global token object that represents
1057 who wrote last to the buffer control structure. We need to freeze
1058 any inferior writing to the buffer while GDBserver touches memory,
1059 so that the inferior can correctly detect that GDBserver had been
1060 there, otherwise, it could mistakingly think its commit was
1061 successful; that's implemented by simply having GDBserver set a
1062 breakpoint the inferior hits if it is the critical region.
1064 There are three cycling trace buffer control structure copies
1065 (buffer head, tail, etc.), with the token object including an index
1066 indicating which is current live copy. The IPA tentatively builds
1067 an updated copy in a non-current control structure, while GDBserver
1068 always clobbers the current version directly. The IPA then tries
1069 to atomically "commit" its version; if GDBserver clobbered the
1070 structure meanwhile, that will fail, and the IPA restarts the
1073 Listing the step in further detail, we have:
1075 In-process agent (producer):
1077 - passes by `about_to_request_buffer_space' breakpoint/lock
1079 - reads current token, extracts current trace buffer control index,
1080 and starts tentatively updating the rightmost one (0->1, 1->2,
1081 2->0). Note that only one inferior thread is executing this code
1082 at any given time, due to an outer lock in the jump pads.
1084 - updates counters, and tries to commit the token.
1086 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1087 leaving the sync region.
1089 - checks if the update was effective.
1091 - if trace buffer was found full, hits flush_trace_buffer
1092 breakpoint, and restarts later afterwards.
1094 GDBserver (consumer):
1096 - sets `about_to_request_buffer_space' breakpoint/lock.
1098 - updates the token unconditionally, using the current buffer
1099 control index, since it knows that the IP agent always writes to
1100 the rightmost, and due to the breakpoint, at most one IP thread
1101 can try to update the trace buffer concurrently to GDBserver, so
1102 there will be no danger of trace buffer control index wrap making
1103 the IPA write to the same index as GDBserver.
1105 - flushes the IP agent's trace buffer completely, and updates the
1106 current trace buffer control structure. GDBserver *always* wins.
1108 - removes the `about_to_request_buffer_space' breakpoint.
1110 The token is stored in the `trace_buffer_ctrl_curr' variable.
1111 Internally, it's bits are defined as:
1113 |-------------+-----+-------------+--------+-------------+--------------|
1114 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1115 |-------------+-----+-------------+--------+-------------+--------------|
1116 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1117 |-------------+-----+-------------+--------+-------------+--------------|
1119 GSB - GDBserver Stamp Bit
1120 PC - Previous Counter
1121 CC - Current Counter
1122 TBCI - Trace Buffer Control Index
1125 An IPA update of `trace_buffer_ctrl_curr' does:
1127 - read CC from the current token, save as PC.
1129 - atomically tries to write PC+1,CC
1131 A GDBserver update of `trace_buffer_ctrl_curr' does:
1133 - reads PC and CC from the current token.
1138 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1139 for the counters described below. The cleared bits are used to
1140 hold the index of the items of the `trace_buffer_ctrl' array that
1142 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1144 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1145 counter, and the `current' counter. */
1147 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1148 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1150 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1151 always stamps this bit as set. */
1152 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1154 #ifdef IN_PROCESS_AGENT
1155 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1156 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1158 # define TRACE_BUFFER_CTRL_CURR \
1159 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1163 /* The GDBserver side agent only needs one instance of this object, as
1164 it doesn't need to sync with itself. Define it as array anyway so
1165 that the rest of the code base doesn't need to care for the
1167 struct trace_buffer_control trace_buffer_ctrl
[1];
1168 # define TRACE_BUFFER_CTRL_CURR 0
1171 /* These are convenience macros used to access the current trace
1172 buffer control in effect. */
1173 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1174 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1175 #define trace_buffer_end_free \
1176 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1177 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1180 /* Macro that returns a pointer to the first traceframe in the buffer. */
1182 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1184 /* Macro that returns a pointer to the next traceframe in the buffer.
1185 If the computed location is beyond the wraparound point, subtract
1186 the offset of the wraparound. */
1188 #define NEXT_TRACEFRAME_1(TF) \
1189 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1191 #define NEXT_TRACEFRAME(TF) \
1192 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1193 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1194 ? (trace_buffer_wrap - trace_buffer_lo) \
1197 /* The difference between these counters represents the total number
1198 of complete traceframes present in the trace buffer. The IP agent
1199 writes to the write count, GDBserver writes to read count. */
1201 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1202 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1204 /* Convenience macro. */
1206 #define traceframe_count \
1207 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1209 /* The count of all traceframes created in the current run, including
1210 ones that were discarded to make room. */
1212 IP_AGENT_EXPORT
int traceframes_created
;
1214 #ifndef IN_PROCESS_AGENT
1216 /* Read-only regions are address ranges whose contents don't change,
1217 and so can be read from target memory even while looking at a trace
1218 frame. Without these, disassembly for instance will likely fail,
1219 because the program code is not usually collected into a trace
1220 frame. This data structure does not need to be very complicated or
1221 particularly efficient, it's only going to be used occasionally,
1222 and only by some commands. */
1224 struct readonly_region
1226 /* The bounds of the region. */
1227 CORE_ADDR start
, end
;
1229 /* Link to the next one. */
1230 struct readonly_region
*next
;
1233 /* Linked list of readonly regions. This list stays in effect from
1234 one tstart to the next. */
1236 static struct readonly_region
*readonly_regions
;
1240 /* The global that controls tracing overall. */
1242 IP_AGENT_EXPORT
int tracing
;
1244 #ifndef IN_PROCESS_AGENT
1246 /* Controls whether tracing should continue after GDB disconnects. */
1248 int disconnected_tracing
;
1250 /* The reason for the last tracing run to have stopped. We initialize
1251 to a distinct string so that GDB can distinguish between "stopped
1252 after running" and "stopped because never run" cases. */
1254 static const char *tracing_stop_reason
= "tnotrun";
1256 static int tracing_stop_tpnum
;
1258 /* 64-bit timestamps for the trace run's start and finish, expressed
1259 in microseconds from the Unix epoch. */
1261 LONGEST tracing_start_time
;
1262 LONGEST tracing_stop_time
;
1264 /* The (optional) user-supplied name of the user that started the run.
1265 This is an arbitrary string, and may be NULL. */
1267 char *tracing_user_name
;
1269 /* Optional user-supplied text describing the run. This is
1270 an arbitrary string, and may be NULL. */
1272 char *tracing_notes
;
1274 /* Optional user-supplied text explaining a tstop command. This is an
1275 arbitrary string, and may be NULL. */
1277 char *tracing_stop_note
;
1281 /* Functions local to this file. */
1283 /* Base "class" for tracepoint type specific data to be passed down to
1284 collect_data_at_tracepoint. */
1285 struct tracepoint_hit_ctx
1287 enum tracepoint_type type
;
1290 #ifdef IN_PROCESS_AGENT
1292 /* Fast/jump tracepoint specific data to be passed down to
1293 collect_data_at_tracepoint. */
1294 struct fast_tracepoint_ctx
1296 struct tracepoint_hit_ctx base
;
1298 struct regcache regcache
;
1299 int regcache_initted
;
1300 unsigned char *regspace
;
1302 unsigned char *regs
;
1303 struct tracepoint
*tpoint
;
1306 /* Static tracepoint specific data to be passed down to
1307 collect_data_at_tracepoint. */
1308 struct static_tracepoint_ctx
1310 struct tracepoint_hit_ctx base
;
1312 /* The regcache corresponding to the registers state at the time of
1313 the tracepoint hit. Initialized lazily, from REGS. */
1314 struct regcache regcache
;
1315 int regcache_initted
;
1317 /* The buffer space REGCACHE above uses. We use a separate buffer
1318 instead of letting the regcache malloc for both signal safety and
1319 performance reasons; this is allocated on the stack instead. */
1320 unsigned char *regspace
;
1322 /* The register buffer as passed on by lttng/ust. */
1323 struct registers
*regs
;
1325 /* The "printf" formatter and the args the user passed to the marker
1326 call. We use this to be able to collect "static trace data"
1331 /* The GDB tracepoint matching the probed marker that was "hit". */
1332 struct tracepoint
*tpoint
;
1337 /* Static tracepoint specific data to be passed down to
1338 collect_data_at_tracepoint. */
1339 struct trap_tracepoint_ctx
1341 struct tracepoint_hit_ctx base
;
1343 struct regcache
*regcache
;
1348 static enum eval_result_type
1349 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1350 struct traceframe
*tframe
,
1351 struct agent_expr
*aexpr
,
1354 #ifndef IN_PROCESS_AGENT
1355 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1356 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1359 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1360 struct tracepoint
*tpoint
);
1362 #ifndef IN_PROCESS_AGENT
1363 static void clear_readonly_regions (void);
1364 static void clear_installed_tracepoints (void);
1367 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1369 struct tracepoint
*tpoint
);
1370 #ifndef IN_PROCESS_AGENT
1371 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1373 struct tracepoint
*tpoint
, int current_step
);
1374 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1375 CORE_ADDR
*jump_entry
);
1377 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1379 struct tracepoint
*tpoint
,
1380 struct traceframe
*tframe
,
1381 struct tracepoint_action
*taction
);
1383 #ifndef IN_PROCESS_AGENT
1384 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1386 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1387 static void download_tracepoint (struct tracepoint
*);
1388 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1389 static void clone_fast_tracepoint (struct tracepoint
*to
,
1390 const struct tracepoint
*from
);
1393 static LONGEST
get_timestamp (void);
1395 #if defined(__GNUC__)
1396 # define memory_barrier() asm volatile ("" : : : "memory")
1398 # define memory_barrier() do {} while (0)
1401 /* We only build the IPA if this builtin is supported, and there are
1402 no uses of this in GDBserver itself, so we're safe in defining this
1404 #define cmpxchg(mem, oldval, newval) \
1405 __sync_val_compare_and_swap (mem, oldval, newval)
1407 /* Record that an error occurred during expression evaluation. */
1410 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1411 enum eval_result_type rtype
)
1413 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1414 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1416 #ifdef IN_PROCESS_AGENT
1417 /* Only record the first error we get. */
1418 if (cmpxchg (&expr_eval_result
,
1420 rtype
) != expr_eval_no_error
)
1423 if (expr_eval_result
!= expr_eval_no_error
)
1427 error_tracepoint
= tpoint
;
1430 /* Trace buffer management. */
1433 clear_trace_buffer (void)
1435 trace_buffer_start
= trace_buffer_lo
;
1436 trace_buffer_free
= trace_buffer_lo
;
1437 trace_buffer_end_free
= trace_buffer_hi
;
1438 trace_buffer_wrap
= trace_buffer_hi
;
1439 /* A traceframe with zeroed fields marks the end of trace data. */
1440 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1441 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1442 traceframe_read_count
= traceframe_write_count
= 0;
1443 traceframes_created
= 0;
1446 #ifndef IN_PROCESS_AGENT
1449 clear_inferior_trace_buffer (void)
1451 CORE_ADDR ipa_trace_buffer_lo
;
1452 CORE_ADDR ipa_trace_buffer_hi
;
1453 struct traceframe ipa_traceframe
= { 0 };
1454 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1456 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1457 &ipa_trace_buffer_lo
);
1458 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1459 &ipa_trace_buffer_hi
);
1461 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1462 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1463 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1464 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1466 /* A traceframe with zeroed fields marks the end of trace data. */
1467 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1468 (unsigned char *) &ipa_trace_buffer_ctrl
,
1469 sizeof (ipa_trace_buffer_ctrl
));
1471 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1473 /* A traceframe with zeroed fields marks the end of trace data. */
1474 write_inferior_memory (ipa_trace_buffer_lo
,
1475 (unsigned char *) &ipa_traceframe
,
1476 sizeof (ipa_traceframe
));
1478 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1479 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1480 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1486 init_trace_buffer (unsigned char *buf
, int bufsize
)
1488 trace_buffer_lo
= buf
;
1489 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1491 clear_trace_buffer ();
1494 #ifdef IN_PROCESS_AGENT
1496 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1497 about_to_request_buffer_space (void)
1499 /* GDBserver places breakpoint here while it goes about to flush
1500 data at random times. */
1501 UNKNOWN_SIDE_EFFECTS();
1506 /* Carve out a piece of the trace buffer, returning NULL in case of
1510 trace_buffer_alloc (size_t amt
)
1512 unsigned char *rslt
;
1513 struct trace_buffer_control
*tbctrl
;
1515 #ifdef IN_PROCESS_AGENT
1516 unsigned int prev
, prev_filtered
;
1517 unsigned int commit_count
;
1518 unsigned int commit
;
1519 unsigned int readout
;
1521 struct traceframe
*oldest
;
1522 unsigned char *new_start
;
1525 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1526 (long) amt
, (long) sizeof (struct traceframe
));
1528 /* Account for the EOB marker. */
1529 amt
+= sizeof (struct traceframe
);
1531 #ifdef IN_PROCESS_AGENT
1535 /* Read the current token and extract the index to try to write to,
1536 storing it in CURR. */
1537 prev
= trace_buffer_ctrl_curr
;
1538 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1539 curr
= prev_filtered
+ 1;
1543 about_to_request_buffer_space ();
1545 /* Start out with a copy of the current state. GDBserver may be
1546 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1547 be able to commit anyway if that happens. */
1548 trace_buffer_ctrl
[curr
]
1549 = trace_buffer_ctrl
[prev_filtered
];
1550 trace_debug ("trying curr=%u", curr
);
1552 /* The GDBserver's agent doesn't need all that syncing, and always
1553 updates TCB 0 (there's only one, mind you). */
1556 tbctrl
= &trace_buffer_ctrl
[curr
];
1558 /* Offsets are easier to grok for debugging than raw addresses,
1559 especially for the small trace buffer sizes that are useful for
1561 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1563 (int) (tbctrl
->start
- trace_buffer_lo
),
1564 (int) (tbctrl
->free
- trace_buffer_lo
),
1565 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1566 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1567 (int) (trace_buffer_hi
- trace_buffer_lo
));
1569 /* The algorithm here is to keep trying to get a contiguous block of
1570 the requested size, possibly discarding older traceframes to free
1571 up space. Since free space might come in one or two pieces,
1572 depending on whether discarded traceframes wrapped around at the
1573 high end of the buffer, we test both pieces after each
1577 /* First, if we have two free parts, try the upper one first. */
1578 if (tbctrl
->end_free
< tbctrl
->free
)
1580 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1581 /* We have enough in the upper part. */
1585 /* Our high part of free space wasn't enough. Give up
1586 on it for now, set wraparound. We will recover the
1587 space later, if/when the wrapped-around traceframe is
1589 trace_debug ("Upper part too small, setting wraparound");
1590 tbctrl
->wrap
= tbctrl
->free
;
1591 tbctrl
->free
= trace_buffer_lo
;
1595 /* The normal case. */
1596 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1599 #ifdef IN_PROCESS_AGENT
1600 /* The IP Agent's buffer is always circular. It isn't used
1601 currently, but `circular_trace_buffer' could represent
1602 GDBserver's mode. If we didn't find space, ask GDBserver to
1605 flush_trace_buffer ();
1609 trace_debug ("gdbserver flushed buffer, retrying");
1613 /* GDBserver cancelled the tracing. Bail out as well. */
1616 /* If we're here, then neither part is big enough, and
1617 non-circular trace buffers are now full. */
1618 if (!circular_trace_buffer
)
1620 trace_debug ("Not enough space in the trace buffer");
1624 trace_debug ("Need more space in the trace buffer");
1626 /* If we have a circular buffer, we can try discarding the
1627 oldest traceframe and see if that helps. */
1628 oldest
= FIRST_TRACEFRAME ();
1629 if (oldest
->tpnum
== 0)
1631 /* Not good; we have no traceframes to free. Perhaps we're
1632 asking for a block that is larger than the buffer? In
1633 any case, give up. */
1634 trace_debug ("No traceframes to discard");
1638 /* We don't run this code in the in-process agent currently.
1639 E.g., we could leave the in-process agent in autonomous
1640 circular mode if we only have fast tracepoints. If we do
1641 that, then this bit becomes racy with GDBserver, which also
1642 writes to this counter. */
1643 --traceframe_write_count
;
1645 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1646 /* If we freed the traceframe that wrapped around, go back
1647 to the non-wrap case. */
1648 if (new_start
< tbctrl
->start
)
1650 trace_debug ("Discarding past the wraparound");
1651 tbctrl
->wrap
= trace_buffer_hi
;
1653 tbctrl
->start
= new_start
;
1654 tbctrl
->end_free
= tbctrl
->start
;
1656 trace_debug ("Discarded a traceframe\n"
1657 "Trace buffer [%d], start=%d free=%d "
1658 "endfree=%d wrap=%d hi=%d",
1660 (int) (tbctrl
->start
- trace_buffer_lo
),
1661 (int) (tbctrl
->free
- trace_buffer_lo
),
1662 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1663 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1664 (int) (trace_buffer_hi
- trace_buffer_lo
));
1666 /* Now go back around the loop. The discard might have resulted
1667 in either one or two pieces of free space, so we want to try
1668 both before freeing any more traceframes. */
1672 /* If we get here, we know we can provide the asked-for space. */
1674 rslt
= tbctrl
->free
;
1676 /* Adjust the request back down, now that we know we have space for
1677 the marker, but don't commit to AMT yet, we may still need to
1678 restart the operation if GDBserver touches the trace buffer
1679 (obviously only important in the in-process agent's version). */
1680 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1682 /* Or not. If GDBserver changed the trace buffer behind our back,
1683 we get to restart a new allocation attempt. */
1685 #ifdef IN_PROCESS_AGENT
1686 /* Build the tentative token. */
1687 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1688 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1689 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1693 /* Try to commit it. */
1694 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1695 if (readout
!= prev
)
1697 trace_debug ("GDBserver has touched the trace buffer, restarting."
1698 " (prev=%08x, commit=%08x, readout=%08x)",
1699 prev
, commit
, readout
);
1703 /* Hold your horses here. Even if that change was committed,
1704 GDBserver could come in, and clobber it. We need to hold to be
1705 able to tell if GDBserver clobbers before or after we committed
1706 the change. Whenever GDBserver goes about touching the IPA
1707 buffer, it sets a breakpoint in this routine, so we have a sync
1709 about_to_request_buffer_space ();
1711 /* Check if the change has been effective, even if GDBserver stopped
1712 us at the breakpoint. */
1715 unsigned int refetch
;
1719 refetch
= trace_buffer_ctrl_curr
;
1721 if (refetch
== commit
1722 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1725 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1726 "readout=%08x, refetch=%08x)",
1727 prev
, commit
, readout
, refetch
);
1731 trace_debug ("GDBserver has touched the trace buffer, not effective."
1732 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1733 prev
, commit
, readout
, refetch
);
1739 /* We have a new piece of the trace buffer. Hurray! */
1741 /* Add an EOB marker just past this allocation. */
1742 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1743 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1745 /* Adjust the request back down, now that we know we have space for
1747 amt
-= sizeof (struct traceframe
);
1751 trace_debug ("Allocated %d bytes", (int) amt
);
1752 trace_debug ("Trace buffer [%d] start=%d free=%d "
1753 "endfree=%d wrap=%d hi=%d",
1755 (int) (tbctrl
->start
- trace_buffer_lo
),
1756 (int) (tbctrl
->free
- trace_buffer_lo
),
1757 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1758 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1759 (int) (trace_buffer_hi
- trace_buffer_lo
));
1765 #ifndef IN_PROCESS_AGENT
1767 /* Return the total free space. This is not necessarily the largest
1768 block we can allocate, because of the two-part case. */
1773 if (trace_buffer_free
<= trace_buffer_end_free
)
1774 return trace_buffer_end_free
- trace_buffer_free
;
1776 return ((trace_buffer_end_free
- trace_buffer_lo
)
1777 + (trace_buffer_hi
- trace_buffer_free
));
1780 /* An 'S' in continuation packets indicates remainder are for
1783 static int seen_step_action_flag
;
1785 /* Create a tracepoint (location) with given number and address. Add this
1786 new tracepoint to list and sort this list. */
1788 static struct tracepoint
*
1789 add_tracepoint (int num
, CORE_ADDR addr
)
1791 struct tracepoint
*tpoint
, **tp_next
;
1793 tpoint
= xmalloc (sizeof (struct tracepoint
));
1794 tpoint
->number
= num
;
1795 tpoint
->address
= addr
;
1796 tpoint
->numactions
= 0;
1797 tpoint
->actions
= NULL
;
1798 tpoint
->actions_str
= NULL
;
1799 tpoint
->cond
= NULL
;
1800 tpoint
->num_step_actions
= 0;
1801 tpoint
->step_actions
= NULL
;
1802 tpoint
->step_actions_str
= NULL
;
1803 /* Start all off as regular (slow) tracepoints. */
1804 tpoint
->type
= trap_tracepoint
;
1805 tpoint
->orig_size
= -1;
1806 tpoint
->source_strings
= NULL
;
1807 tpoint
->compiled_cond
= 0;
1808 tpoint
->handle
= NULL
;
1809 tpoint
->next
= NULL
;
1811 /* Find a place to insert this tracepoint into list in order to keep
1812 the tracepoint list still in the ascending order. There may be
1813 multiple tracepoints at the same address as TPOINT's, and this
1814 guarantees TPOINT is inserted after all the tracepoints which are
1815 set at the same address. For example, fast tracepoints A, B, C are
1816 set at the same address, and D is to be insert at the same place as
1819 -->| A |--> | B |-->| C |->...
1821 One jump pad was created for tracepoint A, B, and C, and the target
1822 address of A is referenced/used in jump pad. So jump pad will let
1823 inferior jump to A. If D is inserted in front of A, like this,
1825 -->| D |-->| A |--> | B |-->| C |->...
1827 without updating jump pad, D is not reachable during collect, which
1828 is wrong. As we can see, the order of B, C and D doesn't matter, but
1829 A should always be the `first' one. */
1830 for (tp_next
= &tracepoints
;
1831 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1832 tp_next
= &(*tp_next
)->next
)
1834 tpoint
->next
= *tp_next
;
1836 last_tracepoint
= tpoint
;
1838 seen_step_action_flag
= 0;
1843 #ifndef IN_PROCESS_AGENT
1845 /* Return the tracepoint with the given number and address, or NULL. */
1847 static struct tracepoint
*
1848 find_tracepoint (int id
, CORE_ADDR addr
)
1850 struct tracepoint
*tpoint
;
1852 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1853 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1859 /* Remove TPOINT from global list. */
1862 remove_tracepoint (struct tracepoint
*tpoint
)
1864 struct tracepoint
*tp
, *tp_prev
;
1866 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1867 tp_prev
= tp
, tp
= tp
->next
)
1873 tp_prev
->next
= tp
->next
;
1875 tracepoints
= tp
->next
;
1881 /* There may be several tracepoints with the same number (because they
1882 are "locations", in GDB parlance); return the next one after the
1883 given tracepoint, or search from the beginning of the list if the
1884 first argument is NULL. */
1886 static struct tracepoint
*
1887 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1889 struct tracepoint
*tpoint
;
1892 tpoint
= prev_tp
->next
;
1894 tpoint
= tracepoints
;
1895 for (; tpoint
; tpoint
= tpoint
->next
)
1896 if (tpoint
->number
== num
)
1905 save_string (const char *str
, size_t len
)
1909 s
= xmalloc (len
+ 1);
1910 memcpy (s
, str
, len
);
1916 /* Append another action to perform when the tracepoint triggers. */
1919 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1925 seen_step_action_flag
= 1;
1933 char *act_start
= act
;
1934 struct tracepoint_action
*action
= NULL
;
1940 struct collect_memory_action
*maction
;
1944 maction
= xmalloc (sizeof *maction
);
1945 maction
->base
.type
= *act
;
1946 maction
->base
.ops
= &m_tracepoint_action_ops
;
1947 action
= &maction
->base
;
1950 is_neg
= (*act
== '-');
1953 act
= unpack_varlen_hex (act
, &basereg
);
1955 act
= unpack_varlen_hex (act
, &maction
->addr
);
1957 act
= unpack_varlen_hex (act
, &maction
->len
);
1958 maction
->basereg
= (is_neg
1961 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1962 pulongest (maction
->len
),
1963 paddress (maction
->addr
), maction
->basereg
);
1968 struct collect_registers_action
*raction
;
1970 raction
= xmalloc (sizeof *raction
);
1971 raction
->base
.type
= *act
;
1972 raction
->base
.ops
= &r_tracepoint_action_ops
;
1973 action
= &raction
->base
;
1975 trace_debug ("Want to collect registers");
1977 /* skip past hex digits of mask for now */
1978 while (isxdigit(*act
))
1984 struct collect_static_trace_data_action
*raction
;
1986 raction
= xmalloc (sizeof *raction
);
1987 raction
->base
.type
= *act
;
1988 raction
->base
.ops
= &l_tracepoint_action_ops
;
1989 action
= &raction
->base
;
1991 trace_debug ("Want to collect static trace data");
1996 trace_debug ("Unexpected step action, ignoring");
2001 struct eval_expr_action
*xaction
;
2003 xaction
= xmalloc (sizeof (*xaction
));
2004 xaction
->base
.type
= *act
;
2005 xaction
->base
.ops
= &x_tracepoint_action_ops
;
2006 action
= &xaction
->base
;
2008 trace_debug ("Want to evaluate expression");
2009 xaction
->expr
= gdb_parse_agent_expr (&act
);
2013 trace_debug ("unknown trace action '%c', ignoring...", *act
);
2022 if (seen_step_action_flag
)
2024 tpoint
->num_step_actions
++;
2026 tpoint
->step_actions
2027 = xrealloc (tpoint
->step_actions
,
2028 (sizeof (*tpoint
->step_actions
)
2029 * tpoint
->num_step_actions
));
2030 tpoint
->step_actions_str
2031 = xrealloc (tpoint
->step_actions_str
,
2032 (sizeof (*tpoint
->step_actions_str
)
2033 * tpoint
->num_step_actions
));
2034 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
2035 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
2036 = save_string (act_start
, act
- act_start
);
2040 tpoint
->numactions
++;
2042 = xrealloc (tpoint
->actions
,
2043 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
2045 = xrealloc (tpoint
->actions_str
,
2046 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
2047 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
2048 tpoint
->actions_str
[tpoint
->numactions
- 1]
2049 = save_string (act_start
, act
- act_start
);
2056 /* Find or create a trace state variable with the given number. */
2058 static struct trace_state_variable
*
2059 get_trace_state_variable (int num
)
2061 struct trace_state_variable
*tsv
;
2063 #ifdef IN_PROCESS_AGENT
2064 /* Search for an existing variable. */
2065 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
2066 if (tsv
->number
== num
)
2070 /* Search for an existing variable. */
2071 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
2072 if (tsv
->number
== num
)
2078 /* Find or create a trace state variable with the given number. */
2080 static struct trace_state_variable
*
2081 create_trace_state_variable (int num
, int gdb
)
2083 struct trace_state_variable
*tsv
;
2085 tsv
= get_trace_state_variable (num
);
2089 /* Create a new variable. */
2090 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2092 tsv
->initial_value
= 0;
2096 #ifdef IN_PROCESS_AGENT
2099 tsv
->next
= alloced_trace_state_variables
;
2100 alloced_trace_state_variables
= tsv
;
2105 tsv
->next
= trace_state_variables
;
2106 trace_state_variables
= tsv
;
2111 IP_AGENT_EXPORT LONGEST
2112 get_trace_state_variable_value (int num
)
2114 struct trace_state_variable
*tsv
;
2116 tsv
= get_trace_state_variable (num
);
2120 trace_debug ("No trace state variable %d, skipping value get", num
);
2124 /* Call a getter function if we have one. While it's tempting to
2125 set up something to only call the getter once per tracepoint hit,
2126 it could run afoul of thread races. Better to let the getter
2127 handle it directly, if necessary to worry about it. */
2129 tsv
->value
= (tsv
->getter
) ();
2131 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2132 num
, plongest (tsv
->value
));
2137 IP_AGENT_EXPORT
void
2138 set_trace_state_variable_value (int num
, LONGEST val
)
2140 struct trace_state_variable
*tsv
;
2142 tsv
= get_trace_state_variable (num
);
2146 trace_debug ("No trace state variable %d, skipping value set", num
);
2154 agent_get_trace_state_variable_value (int num
)
2156 return get_trace_state_variable_value (num
);
2160 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2162 set_trace_state_variable_value (num
, val
);
2166 set_trace_state_variable_name (int num
, const char *name
)
2168 struct trace_state_variable
*tsv
;
2170 tsv
= get_trace_state_variable (num
);
2174 trace_debug ("No trace state variable %d, skipping name set", num
);
2178 tsv
->name
= (char *) name
;
2182 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2184 struct trace_state_variable
*tsv
;
2186 tsv
= get_trace_state_variable (num
);
2190 trace_debug ("No trace state variable %d, skipping getter set", num
);
2194 tsv
->getter
= getter
;
2197 /* Add a raw traceframe for the given tracepoint. */
2199 static struct traceframe
*
2200 add_traceframe (struct tracepoint
*tpoint
)
2202 struct traceframe
*tframe
;
2204 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2209 tframe
->tpnum
= tpoint
->number
;
2210 tframe
->data_size
= 0;
2215 /* Add a block to the traceframe currently being worked on. */
2217 static unsigned char *
2218 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2220 unsigned char *block
;
2225 block
= trace_buffer_alloc (amt
);
2230 tframe
->data_size
+= amt
;
2235 /* Flag that the current traceframe is finished. */
2238 finish_traceframe (struct traceframe
*tframe
)
2240 ++traceframe_write_count
;
2241 ++traceframes_created
;
2244 #ifndef IN_PROCESS_AGENT
2246 /* Given a traceframe number NUM, find the NUMth traceframe in the
2249 static struct traceframe
*
2250 find_traceframe (int num
)
2252 struct traceframe
*tframe
;
2255 for (tframe
= FIRST_TRACEFRAME ();
2257 tframe
= NEXT_TRACEFRAME (tframe
))
2268 get_traceframe_address (struct traceframe
*tframe
)
2271 struct tracepoint
*tpoint
;
2273 addr
= traceframe_get_pc (tframe
);
2278 /* Fallback strategy, will be incorrect for while-stepping frames
2279 and multi-location tracepoints. */
2280 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2281 return tpoint
->address
;
2284 /* Search for the next traceframe whose address is inside or outside
2287 static struct traceframe
*
2288 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2291 struct traceframe
*tframe
;
2294 *tfnump
= current_traceframe
+ 1;
2295 tframe
= find_traceframe (*tfnump
);
2296 /* The search is not supposed to wrap around. */
2303 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2305 tfaddr
= get_traceframe_address (tframe
);
2307 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2308 : (lo
> tfaddr
|| tfaddr
> hi
))
2317 /* Search for the next traceframe recorded by the given tracepoint.
2318 Note that for multi-location tracepoints, this will find whatever
2319 location appears first. */
2321 static struct traceframe
*
2322 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2324 struct traceframe
*tframe
;
2326 *tfnump
= current_traceframe
+ 1;
2327 tframe
= find_traceframe (*tfnump
);
2328 /* The search is not supposed to wrap around. */
2335 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2337 if (tframe
->tpnum
== num
)
2348 #ifndef IN_PROCESS_AGENT
2350 /* Clear all past trace state. */
2353 cmd_qtinit (char *packet
)
2355 struct trace_state_variable
*tsv
, *prev
, *next
;
2357 /* Make sure we don't try to read from a trace frame. */
2358 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
;
2421 cancel_breakpoints ();
2423 prev_stpoint
= NULL
;
2425 /* Restore any bytes overwritten by tracepoints. */
2426 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2428 /* Catch the case where we might try to remove a tracepoint that
2429 was never actually installed. */
2430 if (tpoint
->handle
== NULL
)
2432 trace_debug ("Tracepoint %d at 0x%s was "
2433 "never installed, nothing to clear",
2434 tpoint
->number
, paddress (tpoint
->address
));
2438 switch (tpoint
->type
)
2440 case trap_tracepoint
:
2441 delete_breakpoint (tpoint
->handle
);
2443 case fast_tracepoint
:
2444 delete_fast_tracepoint_jump (tpoint
->handle
);
2446 case static_tracepoint
:
2447 if (prev_stpoint
!= NULL
2448 && prev_stpoint
->address
== tpoint
->address
)
2449 /* Nothing to do. We already unprobed a tracepoint set at
2450 this marker address (and there can only be one probe
2455 unprobe_marker_at (tpoint
->address
);
2456 prev_stpoint
= tpoint
;
2461 tpoint
->handle
= NULL
;
2467 /* Parse a packet that defines a tracepoint. */
2470 cmd_qtdp (char *own_buf
)
2473 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2474 int trail_hyphen
= 0;
2478 struct tracepoint
*tpoint
;
2480 char *packet
= own_buf
;
2482 packet
+= strlen ("QTDP:");
2484 /* A hyphen at the beginning marks a packet specifying actions for a
2485 tracepoint already supplied. */
2492 packet
= unpack_varlen_hex (packet
, &num
);
2493 ++packet
; /* skip a colon */
2494 packet
= unpack_varlen_hex (packet
, &addr
);
2495 ++packet
; /* skip a colon */
2497 /* See if we already have this tracepoint. */
2498 tpoint
= find_tracepoint (num
, addr
);
2502 /* Duplicate tracepoints are never allowed. */
2505 trace_debug ("Tracepoint error: tracepoint %d"
2506 " at 0x%s already exists",
2507 (int) num
, paddress (addr
));
2508 write_enn (own_buf
);
2512 tpoint
= add_tracepoint (num
, addr
);
2514 tpoint
->enabled
= (*packet
== 'E');
2515 ++packet
; /* skip 'E' */
2516 ++packet
; /* skip a colon */
2517 packet
= unpack_varlen_hex (packet
, &count
);
2518 tpoint
->step_count
= count
;
2519 ++packet
; /* skip a colon */
2520 packet
= unpack_varlen_hex (packet
, &count
);
2521 tpoint
->pass_count
= count
;
2522 /* See if we have any of the additional optional fields. */
2523 while (*packet
== ':')
2528 tpoint
->type
= fast_tracepoint
;
2530 packet
= unpack_varlen_hex (packet
, &count
);
2531 tpoint
->orig_size
= count
;
2533 else if (*packet
== 'S')
2535 tpoint
->type
= static_tracepoint
;
2538 else if (*packet
== 'X')
2540 actparm
= (char *) packet
;
2541 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2544 else if (*packet
== '-')
2546 else if (*packet
== '\0')
2549 trace_debug ("Unknown optional tracepoint field");
2554 trace_debug ("Also has actions\n");
2557 trace_debug ("Defined %stracepoint %d at 0x%s, "
2558 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2559 tpoint
->type
== fast_tracepoint
? "fast "
2560 : tpoint
->type
== static_tracepoint
? "static " : "",
2561 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2562 tpoint
->step_count
, tpoint
->pass_count
);
2565 add_tracepoint_action (tpoint
, packet
);
2568 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2569 (int) num
, paddress (addr
));
2570 write_enn (own_buf
);
2574 /* Install tracepoint during tracing only once for each tracepoint location.
2575 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2576 determine the last QTDP packet for one tracepoint location by checking
2577 trailing hyphen in QTDP packet. */
2578 if (tracing
&& !trail_hyphen
)
2580 struct tracepoint
*tp
= NULL
;
2582 /* Pause all threads temporarily while we patch tracepoints. */
2585 /* download_tracepoint will update global `tracepoints'
2586 list, so it is unsafe to leave threads in jump pad. */
2587 stabilize_threads ();
2589 /* Freeze threads. */
2593 if (tpoint
->type
!= trap_tracepoint
)
2595 /* Find another fast or static tracepoint at the same address. */
2596 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2598 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2599 && tp
->number
!= tpoint
->number
)
2603 /* TPOINT is installed at the same address as TP. */
2606 if (tpoint
->type
== fast_tracepoint
)
2607 clone_fast_tracepoint (tpoint
, tp
);
2608 else if (tpoint
->type
== static_tracepoint
)
2609 tpoint
->handle
= (void *) -1;
2613 if (use_agent
&& tpoint
->type
== fast_tracepoint
2614 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
2616 /* Download and install fast tracepoint by agent. */
2617 if (tracepoint_send_agent (tpoint
) == 0)
2621 write_enn (own_buf
);
2622 remove_tracepoint (tpoint
);
2627 download_tracepoint (tpoint
);
2629 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2631 install_tracepoint (tpoint
, own_buf
);
2632 if (strcmp (own_buf
, "OK") != 0)
2633 remove_tracepoint (tpoint
);
2647 cmd_qtdpsrc (char *own_buf
)
2649 ULONGEST num
, addr
, start
, slen
;
2650 struct tracepoint
*tpoint
;
2651 char *packet
= own_buf
;
2652 char *saved
, *srctype
, *src
;
2654 struct source_string
*last
, *newlast
;
2656 packet
+= strlen ("QTDPsrc:");
2658 packet
= unpack_varlen_hex (packet
, &num
);
2659 ++packet
; /* skip a colon */
2660 packet
= unpack_varlen_hex (packet
, &addr
);
2661 ++packet
; /* skip a colon */
2663 /* See if we already have this tracepoint. */
2664 tpoint
= find_tracepoint (num
, addr
);
2668 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2669 (int) num
, paddress (addr
));
2670 write_enn (own_buf
);
2675 packet
= strchr (packet
, ':');
2676 srctype
= xmalloc (packet
- saved
+ 1);
2677 memcpy (srctype
, saved
, packet
- saved
);
2678 srctype
[packet
- saved
] = '\0';
2680 packet
= unpack_varlen_hex (packet
, &start
);
2681 ++packet
; /* skip a colon */
2682 packet
= unpack_varlen_hex (packet
, &slen
);
2683 ++packet
; /* skip a colon */
2684 src
= xmalloc (slen
+ 1);
2685 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2688 newlast
= xmalloc (sizeof (struct source_string
));
2689 newlast
->type
= srctype
;
2691 newlast
->next
= NULL
;
2692 /* Always add a source string to the end of the list;
2693 this keeps sequences of actions/commands in the right
2695 if (tpoint
->source_strings
)
2697 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2699 last
->next
= newlast
;
2702 tpoint
->source_strings
= newlast
;
2708 cmd_qtdv (char *own_buf
)
2710 ULONGEST num
, val
, builtin
;
2713 struct trace_state_variable
*tsv
;
2714 char *packet
= own_buf
;
2716 packet
+= strlen ("QTDV:");
2718 packet
= unpack_varlen_hex (packet
, &num
);
2719 ++packet
; /* skip a colon */
2720 packet
= unpack_varlen_hex (packet
, &val
);
2721 ++packet
; /* skip a colon */
2722 packet
= unpack_varlen_hex (packet
, &builtin
);
2723 ++packet
; /* skip a colon */
2725 nbytes
= strlen (packet
) / 2;
2726 varname
= xmalloc (nbytes
+ 1);
2727 nbytes
= unhexify (varname
, packet
, nbytes
);
2728 varname
[nbytes
] = '\0';
2730 tsv
= create_trace_state_variable (num
, 1);
2731 tsv
->initial_value
= (LONGEST
) val
;
2732 tsv
->name
= varname
;
2734 set_trace_state_variable_value (num
, (LONGEST
) val
);
2740 cmd_qtenable_disable (char *own_buf
, int enable
)
2742 char *packet
= own_buf
;
2744 struct tracepoint
*tp
;
2746 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2747 packet
= unpack_varlen_hex (packet
, &num
);
2748 ++packet
; /* skip a colon */
2749 packet
= unpack_varlen_hex (packet
, &addr
);
2751 tp
= find_tracepoint (num
, addr
);
2755 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2757 trace_debug ("Tracepoint %d at 0x%s is already %s",
2758 (int) num
, paddress (addr
),
2759 enable
? "enabled" : "disabled");
2764 trace_debug ("%s tracepoint %d at 0x%s",
2765 enable
? "Enabling" : "Disabling",
2766 (int) num
, paddress (addr
));
2768 tp
->enabled
= enable
;
2770 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2773 int offset
= offsetof (struct tracepoint
, enabled
);
2774 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2776 ret
= prepare_to_access_memory ();
2779 trace_debug ("Failed to temporarily stop inferior threads");
2780 write_enn (own_buf
);
2784 ret
= write_inferior_integer (obj_addr
, enable
);
2785 done_accessing_memory ();
2789 trace_debug ("Cannot write enabled flag into "
2790 "inferior process memory");
2791 write_enn (own_buf
);
2800 trace_debug ("Tracepoint %d at 0x%s not found",
2801 (int) num
, paddress (addr
));
2802 write_enn (own_buf
);
2807 cmd_qtv (char *own_buf
)
2812 char *packet
= own_buf
;
2814 packet
+= strlen ("qTV:");
2815 unpack_varlen_hex (packet
, &num
);
2817 if (current_traceframe
>= 0)
2819 err
= traceframe_read_tsv ((int) num
, &val
);
2822 strcpy (own_buf
, "U");
2826 /* Only make tsv's be undefined before the first trace run. After a
2827 trace run is over, the user might want to see the last value of
2828 the tsv, and it might not be available in a traceframe. */
2829 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2831 strcpy (own_buf
, "U");
2835 val
= get_trace_state_variable_value (num
);
2837 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2840 /* Clear out the list of readonly regions. */
2843 clear_readonly_regions (void)
2845 struct readonly_region
*roreg
;
2847 while (readonly_regions
)
2849 roreg
= readonly_regions
;
2850 readonly_regions
= readonly_regions
->next
;
2855 /* Parse the collection of address ranges whose contents GDB believes
2856 to be unchanging and so can be read directly from target memory
2857 even while looking at a traceframe. */
2860 cmd_qtro (char *own_buf
)
2862 ULONGEST start
, end
;
2863 struct readonly_region
*roreg
;
2864 char *packet
= own_buf
;
2866 trace_debug ("Want to mark readonly regions");
2868 clear_readonly_regions ();
2870 packet
+= strlen ("QTro");
2872 while (*packet
== ':')
2874 ++packet
; /* skip a colon */
2875 packet
= unpack_varlen_hex (packet
, &start
);
2876 ++packet
; /* skip a comma */
2877 packet
= unpack_varlen_hex (packet
, &end
);
2878 roreg
= xmalloc (sizeof (struct readonly_region
));
2879 roreg
->start
= start
;
2881 roreg
->next
= readonly_regions
;
2882 readonly_regions
= roreg
;
2883 trace_debug ("Added readonly region from 0x%s to 0x%s",
2884 paddress (roreg
->start
), paddress (roreg
->end
));
2890 /* Test to see if the given range is in our list of readonly ranges.
2891 We only test for being entirely within a range, GDB is not going to
2892 send a single memory packet that spans multiple regions. */
2895 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2897 struct readonly_region
*roreg
;
2899 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2900 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2906 /* The maximum size of a jump pad entry. */
2907 static const int max_jump_pad_size
= 0x100;
2909 static CORE_ADDR gdb_jump_pad_head
;
2911 /* Return the address of the next free jump space. */
2914 get_jump_space_head (void)
2916 if (gdb_jump_pad_head
== 0)
2918 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2919 &gdb_jump_pad_head
))
2920 fatal ("error extracting jump_pad_buffer");
2923 return gdb_jump_pad_head
;
2926 /* Reserve USED bytes from the jump space. */
2929 claim_jump_space (ULONGEST used
)
2931 trace_debug ("claim_jump_space reserves %s bytes at %s",
2932 pulongest (used
), paddress (gdb_jump_pad_head
));
2933 gdb_jump_pad_head
+= used
;
2936 static CORE_ADDR trampoline_buffer_head
= 0;
2937 static CORE_ADDR trampoline_buffer_tail
;
2939 /* Reserve USED bytes from the trampoline buffer and return the
2940 address of the start of the reserved space in TRAMPOLINE. Returns
2941 non-zero if the space is successfully claimed. */
2944 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2946 if (!trampoline_buffer_head
)
2948 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2949 &trampoline_buffer_tail
))
2951 fatal ("error extracting trampoline_buffer");
2955 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2956 &trampoline_buffer_head
))
2958 fatal ("error extracting trampoline_buffer_end");
2963 /* Start claiming space from the top of the trampoline space. If
2964 the space is located at the bottom of the virtual address space,
2965 this reduces the possibility that corruption will occur if a null
2966 pointer is used to write to memory. */
2967 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2969 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2974 trampoline_buffer_head
-= used
;
2976 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2977 pulongest (used
), paddress (trampoline_buffer_head
));
2979 *trampoline
= trampoline_buffer_head
;
2983 /* Returns non-zero if there is space allocated for use in trampolines
2984 for fast tracepoints. */
2987 have_fast_tracepoint_trampoline_buffer (char *buf
)
2989 CORE_ADDR trampoline_end
, errbuf
;
2991 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2994 fatal ("error extracting trampoline_buffer_end");
3001 strcpy (buf
, "was claiming");
3002 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
3005 fatal ("error extracting errbuf");
3009 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
3012 return trampoline_end
!= 0;
3015 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
3016 the command fails, or 0 otherwise. If the command ran
3017 successfully, but probing the marker failed, ERROUT will be filled
3018 with the error to reply to GDB, and -1 is also returned. This
3019 allows directly passing IPA errors to GDB. */
3022 probe_marker_at (CORE_ADDR address
, char *errout
)
3024 char cmd
[IPA_CMD_BUF_SIZE
];
3027 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
3028 err
= run_inferior_command (cmd
, strlen (cmd
) + 1);
3034 strcpy (errout
, cmd
);
3043 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
3045 to
->jump_pad
= from
->jump_pad
;
3046 to
->jump_pad_end
= from
->jump_pad_end
;
3047 to
->trampoline
= from
->trampoline
;
3048 to
->trampoline_end
= from
->trampoline_end
;
3049 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
3050 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
3051 to
->handle
= from
->handle
;
3053 gdb_assert (from
->handle
);
3054 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
3057 #define MAX_JUMP_SIZE 20
3059 /* Install fast tracepoint. Return 0 if successful, otherwise return
3063 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
3065 CORE_ADDR jentry
, jump_entry
;
3066 CORE_ADDR trampoline
;
3067 ULONGEST trampoline_size
;
3069 /* The jump to the jump pad of the last fast tracepoint
3071 unsigned char fjump
[MAX_JUMP_SIZE
];
3072 ULONGEST fjump_size
;
3074 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
3076 trace_debug ("Requested a fast tracepoint on an instruction "
3077 "that is of less than the minimum length.");
3081 jentry
= jump_entry
= get_jump_space_head ();
3084 trampoline_size
= 0;
3086 /* Install the jump pad. */
3087 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3089 ipa_sym_addrs
.addr_gdb_collect
,
3090 ipa_sym_addrs
.addr_collecting
,
3093 &trampoline
, &trampoline_size
,
3095 &tpoint
->adjusted_insn_addr
,
3096 &tpoint
->adjusted_insn_addr_end
,
3103 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3106 if (tpoint
->handle
!= NULL
)
3108 tpoint
->jump_pad
= jump_entry
;
3109 tpoint
->jump_pad_end
= jentry
;
3110 tpoint
->trampoline
= trampoline
;
3111 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3113 /* Pad to 8-byte alignment. */
3114 jentry
= ((jentry
+ 7) & ~0x7);
3115 claim_jump_space (jentry
- jump_entry
);
3122 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3125 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3127 tpoint
->handle
= NULL
;
3130 if (tpoint
->type
== trap_tracepoint
)
3132 /* Tracepoints are installed as memory breakpoints. Just go
3133 ahead and install the trap. The breakpoints module
3134 handles duplicated breakpoints, and the memory read
3135 routine handles un-patching traps from memory reads. */
3136 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3137 tracepoint_handler
);
3139 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3141 if (!agent_loaded_p ())
3143 trace_debug ("Requested a %s tracepoint, but fast "
3144 "tracepoints aren't supported.",
3145 tpoint
->type
== static_tracepoint
? "static" : "fast");
3146 write_e_ipa_not_loaded (own_buf
);
3149 if (tpoint
->type
== static_tracepoint
3150 && !in_process_agent_supports_ust ())
3152 trace_debug ("Requested a static tracepoint, but static "
3153 "tracepoints are not supported.");
3154 write_e_ust_not_loaded (own_buf
);
3158 if (tpoint
->type
== fast_tracepoint
)
3159 install_fast_tracepoint (tpoint
, own_buf
);
3162 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3163 tpoint
->handle
= (void *) -1;
3168 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3170 if (tpoint
->handle
== NULL
)
3172 if (*own_buf
== '\0')
3173 write_enn (own_buf
);
3179 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3182 cmd_qtstart (char *packet
)
3184 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3185 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3187 trace_debug ("Starting the trace");
3189 /* Pause all threads temporarily while we patch tracepoints. */
3192 /* Get threads out of jump pads. Safe to do here, since this is a
3193 top level command. And, required to do here, since we're
3194 deleting/rewriting jump pads. */
3196 stabilize_threads ();
3198 /* Freeze threads. */
3201 /* Sync the fast tracepoints list in the inferior ftlib. */
3202 if (agent_loaded_p ())
3203 download_trace_state_variables ();
3205 /* No previous fast tpoint yet. */
3206 prev_ftpoint
= NULL
;
3208 /* No previous static tpoint yet. */
3209 prev_stpoint
= NULL
;
3213 /* Start out empty. */
3214 if (agent_loaded_p ())
3215 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3217 /* Download and install tracepoints. */
3218 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3220 /* Ensure all the hit counts start at zero. */
3221 tpoint
->hit_count
= 0;
3222 tpoint
->traceframe_usage
= 0;
3224 if (tpoint
->type
== trap_tracepoint
)
3226 /* Tracepoints are installed as memory breakpoints. Just go
3227 ahead and install the trap. The breakpoints module
3228 handles duplicated breakpoints, and the memory read
3229 routine handles un-patching traps from memory reads. */
3230 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3231 tracepoint_handler
);
3233 else if (tpoint
->type
== fast_tracepoint
3234 || tpoint
->type
== static_tracepoint
)
3236 if (maybe_write_ipa_not_loaded (packet
))
3238 trace_debug ("Requested a %s tracepoint, but fast "
3239 "tracepoints aren't supported.",
3240 tpoint
->type
== static_tracepoint
3241 ? "static" : "fast");
3245 if (tpoint
->type
== fast_tracepoint
)
3247 if (prev_ftpoint
!= NULL
3248 && prev_ftpoint
->address
== tpoint
->address
)
3249 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3252 /* Tracepoint is installed successfully? */
3255 /* Download and install fast tracepoint by agent. */
3257 && agent_capability_check (AGENT_CAPA_FAST_TRACE
))
3258 installed
= !tracepoint_send_agent (tpoint
);
3261 download_tracepoint_1 (tpoint
);
3262 installed
= !install_fast_tracepoint (tpoint
, packet
);
3266 prev_ftpoint
= tpoint
;
3271 if (!in_process_agent_supports_ust ())
3273 trace_debug ("Requested a static tracepoint, but static "
3274 "tracepoints are not supported.");
3278 download_tracepoint_1 (tpoint
);
3279 /* Can only probe a given marker once. */
3280 if (prev_stpoint
!= NULL
3281 && prev_stpoint
->address
== tpoint
->address
)
3282 tpoint
->handle
= (void *) -1;
3285 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3287 tpoint
->handle
= (void *) -1;
3289 /* So that we can handle multiple static tracepoints
3290 at the same address easily. */
3291 prev_stpoint
= tpoint
;
3297 tpptr
= tpoint
->obj_addr_on_target
;
3299 if (tpoint
== tracepoints
)
3300 /* First object in list, set the head pointer in the
3302 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3304 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3309 /* Any failure in the inner loop is sufficient cause to give
3311 if (tpoint
->handle
== NULL
)
3315 /* Any error in tracepoint insertion is unacceptable; better to
3316 address the problem now, than end up with a useless or misleading
3320 clear_installed_tracepoints ();
3321 if (*packet
== '\0')
3327 stopping_tracepoint
= NULL
;
3328 trace_buffer_is_full
= 0;
3329 expr_eval_result
= expr_eval_no_error
;
3330 error_tracepoint
= NULL
;
3331 tracing_start_time
= get_timestamp ();
3333 /* Tracing is now active, hits will now start being logged. */
3336 if (agent_loaded_p ())
3338 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3339 fatal ("Error setting tracing variable in lib");
3341 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3343 fatal ("Error clearing stopping_tracepoint variable in lib");
3345 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3346 fatal ("Error clearing trace_buffer_is_full variable in lib");
3348 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3349 stop_tracing_handler
);
3350 if (stop_tracing_bkpt
== NULL
)
3351 error ("Error setting stop_tracing breakpoint");
3353 flush_trace_buffer_bkpt
3354 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3355 flush_trace_buffer_handler
);
3356 if (flush_trace_buffer_bkpt
== NULL
)
3357 error ("Error setting flush_trace_buffer breakpoint");
3365 /* End a tracing run, filling in a stop reason to report back to GDB,
3366 and removing the tracepoints from the code. */
3373 trace_debug ("Tracing is already off, ignoring");
3377 trace_debug ("Stopping the trace");
3379 /* Pause all threads before removing fast jumps from memory,
3380 breakpoints, and touching IPA state variables (inferior memory).
3381 Some thread may hit the internal tracing breakpoints, or be
3382 collecting this moment, but that's ok, we don't release the
3383 tpoint object's memory or the jump pads here (we only do that
3384 when we're sure we can move all threads out of the jump pads).
3385 We can't now, since we may be getting here due to the inferior
3386 agent calling us. */
3388 /* Since we're removing breakpoints, cancel breakpoint hits,
3389 possibly related to the breakpoints we're about to delete. */
3390 cancel_breakpoints ();
3392 /* Stop logging. Tracepoints can still be hit, but they will not be
3395 if (agent_loaded_p ())
3397 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3398 fatal ("Error clearing tracing variable in lib");
3401 tracing_stop_time
= get_timestamp ();
3402 tracing_stop_reason
= "t???";
3403 tracing_stop_tpnum
= 0;
3404 if (stopping_tracepoint
)
3406 trace_debug ("Stopping the trace because "
3407 "tracepoint %d was hit %" PRIu64
" times",
3408 stopping_tracepoint
->number
,
3409 stopping_tracepoint
->pass_count
);
3410 tracing_stop_reason
= "tpasscount";
3411 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3413 else if (trace_buffer_is_full
)
3415 trace_debug ("Stopping the trace because the trace buffer is full");
3416 tracing_stop_reason
= "tfull";
3418 else if (expr_eval_result
!= expr_eval_no_error
)
3420 trace_debug ("Stopping the trace because of an expression eval error");
3421 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3422 tracing_stop_tpnum
= error_tracepoint
->number
;
3424 #ifndef IN_PROCESS_AGENT
3425 else if (!gdb_connected ())
3427 trace_debug ("Stopping the trace because GDB disconnected");
3428 tracing_stop_reason
= "tdisconnected";
3433 trace_debug ("Stopping the trace because of a tstop command");
3434 tracing_stop_reason
= "tstop";
3437 stopping_tracepoint
= NULL
;
3438 error_tracepoint
= NULL
;
3440 /* Clear out the tracepoints. */
3441 clear_installed_tracepoints ();
3443 if (agent_loaded_p ())
3445 /* Pull in fast tracepoint trace frames from the inferior lib
3446 buffer into our buffer, even if our buffer is already full,
3447 because we want to present the full number of created frames
3448 in addition to what fit in the trace buffer. */
3449 upload_fast_traceframes ();
3452 if (stop_tracing_bkpt
!= NULL
)
3454 delete_breakpoint (stop_tracing_bkpt
);
3455 stop_tracing_bkpt
= NULL
;
3458 if (flush_trace_buffer_bkpt
!= NULL
)
3460 delete_breakpoint (flush_trace_buffer_bkpt
);
3461 flush_trace_buffer_bkpt
= NULL
;
3468 stop_tracing_handler (CORE_ADDR addr
)
3470 trace_debug ("lib hit stop_tracing");
3472 /* Don't actually handle it here. When we stop tracing we remove
3473 breakpoints from the inferior, and that is not allowed in a
3474 breakpoint handler (as the caller is walking the breakpoint
3480 flush_trace_buffer_handler (CORE_ADDR addr
)
3482 trace_debug ("lib hit flush_trace_buffer");
3487 cmd_qtstop (char *packet
)
3494 cmd_qtdisconnected (char *own_buf
)
3497 char *packet
= own_buf
;
3499 packet
+= strlen ("QTDisconnected:");
3501 unpack_varlen_hex (packet
, &setting
);
3505 disconnected_tracing
= setting
;
3509 cmd_qtframe (char *own_buf
)
3511 ULONGEST frame
, pc
, lo
, hi
, num
;
3513 struct traceframe
*tframe
;
3514 char *packet
= own_buf
;
3516 packet
+= strlen ("QTFrame:");
3518 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3520 packet
+= strlen ("pc:");
3521 unpack_varlen_hex (packet
, &pc
);
3522 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3523 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3525 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3527 packet
+= strlen ("range:");
3528 packet
= unpack_varlen_hex (packet
, &lo
);
3530 unpack_varlen_hex (packet
, &hi
);
3531 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3532 paddress (lo
), paddress (hi
));
3533 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3535 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3537 packet
+= strlen ("outside:");
3538 packet
= unpack_varlen_hex (packet
, &lo
);
3540 unpack_varlen_hex (packet
, &hi
);
3541 trace_debug ("Want to find next traceframe "
3542 "outside the range 0x%s to 0x%s",
3543 paddress (lo
), paddress (hi
));
3544 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3546 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3548 packet
+= strlen ("tdp:");
3549 unpack_varlen_hex (packet
, &num
);
3551 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3552 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3556 unpack_varlen_hex (packet
, &frame
);
3557 tfnum
= (int) frame
;
3560 trace_debug ("Want to stop looking at traceframes");
3561 current_traceframe
= -1;
3565 trace_debug ("Want to look at traceframe %d", tfnum
);
3566 tframe
= find_traceframe (tfnum
);
3571 current_traceframe
= tfnum
;
3572 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3575 sprintf (own_buf
, "F-1");
3579 cmd_qtstatus (char *packet
)
3581 char *stop_reason_rsp
= NULL
;
3582 char *buf1
, *buf2
, *buf3
, *str
;
3585 /* Translate the plain text of the notes back into hex for
3588 str
= (tracing_user_name
? tracing_user_name
: "");
3589 slen
= strlen (str
);
3590 buf1
= (char *) alloca (slen
* 2 + 1);
3591 hexify (buf1
, str
, slen
);
3593 str
= (tracing_notes
? tracing_notes
: "");
3594 slen
= strlen (str
);
3595 buf2
= (char *) alloca (slen
* 2 + 1);
3596 hexify (buf2
, str
, slen
);
3598 str
= (tracing_stop_note
? tracing_stop_note
: "");
3599 slen
= strlen (str
);
3600 buf3
= (char *) alloca (slen
* 2 + 1);
3601 hexify (buf3
, str
, slen
);
3603 trace_debug ("Returning trace status as %d, stop reason %s",
3604 tracing
, tracing_stop_reason
);
3606 if (agent_loaded_p ())
3610 upload_fast_traceframes ();
3615 stop_reason_rsp
= (char *) tracing_stop_reason
;
3617 /* The user visible error string in terror needs to be hex encoded.
3618 We leave it as plain string in `tracing_stop_reason' to ease
3620 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3622 const char *result_name
;
3626 result_name
= stop_reason_rsp
+ strlen ("terror:");
3627 hexstr_len
= strlen (result_name
) * 2;
3628 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3629 strcpy (p
, "terror:");
3631 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3634 /* If this was a forced stop, include any stop note that was supplied. */
3635 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3637 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3638 strcpy (stop_reason_rsp
, "tstop:");
3639 strcat (stop_reason_rsp
, buf3
);
3645 "tframes:%x;tcreated:%x;"
3646 "tfree:%x;tsize:%s;"
3649 "starttime:%s;stoptime:%s;"
3650 "username:%s:;notes:%s:",
3652 stop_reason_rsp
, tracing_stop_tpnum
,
3653 traceframe_count
, traceframes_created
,
3654 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3655 circular_trace_buffer
,
3656 disconnected_tracing
,
3657 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3662 cmd_qtp (char *own_buf
)
3665 struct tracepoint
*tpoint
;
3666 char *packet
= own_buf
;
3668 packet
+= strlen ("qTP:");
3670 packet
= unpack_varlen_hex (packet
, &num
);
3671 ++packet
; /* skip a colon */
3672 packet
= unpack_varlen_hex (packet
, &addr
);
3674 /* See if we already have this tracepoint. */
3675 tpoint
= find_tracepoint (num
, addr
);
3679 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3680 (int) num
, paddress (addr
));
3681 write_enn (own_buf
);
3685 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3686 tpoint
->traceframe_usage
);
3689 /* State variables to help return all the tracepoint bits. */
3690 static struct tracepoint
*cur_tpoint
;
3691 static int cur_action
;
3692 static int cur_step_action
;
3693 static struct source_string
*cur_source_string
;
3694 static struct trace_state_variable
*cur_tsv
;
3696 /* Compose a response that is an imitation of the syntax by which the
3697 tracepoint was originally downloaded. */
3700 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3704 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3705 paddress (tpoint
->address
),
3706 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3707 tpoint
->pass_count
);
3708 if (tpoint
->type
== fast_tracepoint
)
3709 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3710 else if (tpoint
->type
== static_tracepoint
)
3711 sprintf (packet
+ strlen (packet
), ":S");
3715 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3716 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3717 tpoint
->cond
->length
, buf
);
3722 /* Compose a response that is an imitation of the syntax by which the
3723 tracepoint action was originally downloaded (with the difference
3724 that due to the way we store the actions, this will output a packet
3725 per action, while GDB could have combined more than one action
3729 response_action (char *packet
, struct tracepoint
*tpoint
,
3730 char *taction
, int step
)
3732 sprintf (packet
, "%c%x:%s:%s",
3733 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3737 /* Compose a response that is an imitation of the syntax by which the
3738 tracepoint source piece was originally downloaded. */
3741 response_source (char *packet
,
3742 struct tracepoint
*tpoint
, struct source_string
*src
)
3747 len
= strlen (src
->str
);
3748 buf
= alloca (len
* 2 + 1);
3749 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3751 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3752 tpoint
->number
, paddress (tpoint
->address
),
3753 src
->type
, 0, len
, buf
);
3756 /* Return the first piece of tracepoint definition, and initialize the
3757 state machine that will iterate through all the tracepoint
3761 cmd_qtfp (char *packet
)
3763 trace_debug ("Returning first tracepoint definition piece");
3765 cur_tpoint
= tracepoints
;
3766 cur_action
= cur_step_action
= -1;
3767 cur_source_string
= NULL
;
3770 response_tracepoint (packet
, cur_tpoint
);
3772 strcpy (packet
, "l");
3775 /* Return additional pieces of tracepoint definition. Each action and
3776 stepping action must go into its own packet, because of packet size
3777 limits, and so we use state variables to deliver one piece at a
3781 cmd_qtsp (char *packet
)
3783 trace_debug ("Returning subsequent tracepoint definition piece");
3787 /* This case would normally never occur, but be prepared for
3789 strcpy (packet
, "l");
3791 else if (cur_action
< cur_tpoint
->numactions
- 1)
3794 response_action (packet
, cur_tpoint
,
3795 cur_tpoint
->actions_str
[cur_action
], 0);
3797 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3800 response_action (packet
, cur_tpoint
,
3801 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3803 else if ((cur_source_string
3804 ? cur_source_string
->next
3805 : cur_tpoint
->source_strings
))
3807 if (cur_source_string
)
3808 cur_source_string
= cur_source_string
->next
;
3810 cur_source_string
= cur_tpoint
->source_strings
;
3811 response_source (packet
, cur_tpoint
, cur_source_string
);
3815 cur_tpoint
= cur_tpoint
->next
;
3816 cur_action
= cur_step_action
= -1;
3817 cur_source_string
= NULL
;
3819 response_tracepoint (packet
, cur_tpoint
);
3821 strcpy (packet
, "l");
3825 /* Compose a response that is an imitation of the syntax by which the
3826 trace state variable was originally downloaded. */
3829 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3831 char *buf
= (char *) "";
3836 namelen
= strlen (tsv
->name
);
3837 buf
= alloca (namelen
* 2 + 1);
3838 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3841 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3842 tsv
->getter
? 1 : 0, buf
);
3845 /* Return the first trace state variable definition, and initialize
3846 the state machine that will iterate through all the tsv bits. */
3849 cmd_qtfv (char *packet
)
3851 trace_debug ("Returning first trace state variable definition");
3853 cur_tsv
= trace_state_variables
;
3856 response_tsv (packet
, cur_tsv
);
3858 strcpy (packet
, "l");
3861 /* Return additional trace state variable definitions. */
3864 cmd_qtsv (char *packet
)
3866 trace_debug ("Returning first trace state variable definition");
3870 /* This case would normally never occur, but be prepared for
3872 strcpy (packet
, "l");
3876 cur_tsv
= cur_tsv
->next
;
3878 response_tsv (packet
, cur_tsv
);
3880 strcpy (packet
, "l");
3883 strcpy (packet
, "l");
3886 /* Return the first static tracepoint marker, and initialize the state
3887 machine that will iterate through all the static tracepoints
3891 cmd_qtfstm (char *packet
)
3893 if (!maybe_write_ipa_ust_not_loaded (packet
))
3894 run_inferior_command (packet
, strlen (packet
) + 1);
3897 /* Return additional static tracepoints markers. */
3900 cmd_qtsstm (char *packet
)
3902 if (!maybe_write_ipa_ust_not_loaded (packet
))
3903 run_inferior_command (packet
, strlen (packet
) + 1);
3906 /* Return the definition of the static tracepoint at a given address.
3907 Result packet is the same as qTsST's. */
3910 cmd_qtstmat (char *packet
)
3912 if (!maybe_write_ipa_ust_not_loaded (packet
))
3913 run_inferior_command (packet
, strlen (packet
) + 1);
3916 /* Return the minimum instruction size needed for fast tracepoints as a
3917 hexadecimal number. */
3920 cmd_qtminftpilen (char *packet
)
3922 if (current_inferior
== NULL
)
3924 /* Indicate that the minimum length is currently unknown. */
3925 strcpy (packet
, "0");
3929 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3932 /* Respond to qTBuffer packet with a block of raw data from the trace
3933 buffer. GDB may ask for a lot, but we are allowed to reply with
3934 only as much as will fit within packet limits or whatever. */
3937 cmd_qtbuffer (char *own_buf
)
3939 ULONGEST offset
, num
, tot
;
3941 char *packet
= own_buf
;
3943 packet
+= strlen ("qTBuffer:");
3945 packet
= unpack_varlen_hex (packet
, &offset
);
3946 ++packet
; /* skip a comma */
3947 unpack_varlen_hex (packet
, &num
);
3949 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3950 (int) num
, pulongest (offset
));
3952 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3954 /* If we're right at the end, reply specially that we're done. */
3957 strcpy (own_buf
, "l");
3961 /* Object to any other out-of-bounds request. */
3964 write_enn (own_buf
);
3968 /* Compute the pointer corresponding to the given offset, accounting
3970 tbp
= trace_buffer_start
+ offset
;
3971 if (tbp
>= trace_buffer_wrap
)
3972 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3974 /* Trim to the remaining bytes if we're close to the end. */
3975 if (num
> tot
- offset
)
3978 /* Trim to available packet size. */
3979 if (num
>= (PBUFSIZ
- 16) / 2 )
3980 num
= (PBUFSIZ
- 16) / 2;
3982 convert_int_to_ascii (tbp
, own_buf
, num
);
3983 own_buf
[num
] = '\0';
3987 cmd_bigqtbuffer_circular (char *own_buf
)
3990 char *packet
= own_buf
;
3992 packet
+= strlen ("QTBuffer:circular:");
3994 unpack_varlen_hex (packet
, &val
);
3995 circular_trace_buffer
= val
;
3996 trace_debug ("Trace buffer is now %s",
3997 circular_trace_buffer
? "circular" : "linear");
4002 cmd_qtnotes (char *own_buf
)
4005 char *saved
, *user
, *notes
, *stopnote
;
4006 char *packet
= own_buf
;
4008 packet
+= strlen ("QTNotes:");
4012 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
4014 packet
+= strlen ("user:");
4016 packet
= strchr (packet
, ';');
4017 nbytes
= (packet
- saved
) / 2;
4018 user
= xmalloc (nbytes
+ 1);
4019 nbytes
= unhexify (user
, saved
, nbytes
);
4020 user
[nbytes
] = '\0';
4021 ++packet
; /* skip the semicolon */
4022 trace_debug ("User is '%s'", user
);
4023 tracing_user_name
= user
;
4025 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
4027 packet
+= strlen ("notes:");
4029 packet
= strchr (packet
, ';');
4030 nbytes
= (packet
- saved
) / 2;
4031 notes
= xmalloc (nbytes
+ 1);
4032 nbytes
= unhexify (notes
, saved
, nbytes
);
4033 notes
[nbytes
] = '\0';
4034 ++packet
; /* skip the semicolon */
4035 trace_debug ("Notes is '%s'", notes
);
4036 tracing_notes
= notes
;
4038 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
4040 packet
+= strlen ("tstop:");
4042 packet
= strchr (packet
, ';');
4043 nbytes
= (packet
- saved
) / 2;
4044 stopnote
= xmalloc (nbytes
+ 1);
4045 nbytes
= unhexify (stopnote
, saved
, nbytes
);
4046 stopnote
[nbytes
] = '\0';
4047 ++packet
; /* skip the semicolon */
4048 trace_debug ("tstop note is '%s'", stopnote
);
4049 tracing_stop_note
= stopnote
;
4059 handle_tracepoint_general_set (char *packet
)
4061 if (strcmp ("QTinit", packet
) == 0)
4063 cmd_qtinit (packet
);
4066 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
4071 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
4073 cmd_qtdpsrc (packet
);
4076 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
4078 cmd_qtenable_disable (packet
, 1);
4081 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
4083 cmd_qtenable_disable (packet
, 0);
4086 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
4091 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
4096 else if (strcmp ("QTStart", packet
) == 0)
4098 cmd_qtstart (packet
);
4101 else if (strcmp ("QTStop", packet
) == 0)
4103 cmd_qtstop (packet
);
4106 else if (strncmp ("QTDisconnected:", packet
,
4107 strlen ("QTDisconnected:")) == 0)
4109 cmd_qtdisconnected (packet
);
4112 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4114 cmd_qtframe (packet
);
4117 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4119 cmd_bigqtbuffer_circular (packet
);
4122 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4124 cmd_qtnotes (packet
);
4132 handle_tracepoint_query (char *packet
)
4134 if (strcmp ("qTStatus", packet
) == 0)
4136 cmd_qtstatus (packet
);
4139 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4144 else if (strcmp ("qTfP", packet
) == 0)
4149 else if (strcmp ("qTsP", packet
) == 0)
4154 else if (strcmp ("qTfV", packet
) == 0)
4159 else if (strcmp ("qTsV", packet
) == 0)
4164 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4169 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4171 cmd_qtbuffer (packet
);
4174 else if (strcmp ("qTfSTM", packet
) == 0)
4176 cmd_qtfstm (packet
);
4179 else if (strcmp ("qTsSTM", packet
) == 0)
4181 cmd_qtsstm (packet
);
4184 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4186 cmd_qtstmat (packet
);
4189 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4191 cmd_qtminftpilen (packet
);
4199 #ifndef IN_PROCESS_AGENT
4201 /* Call this when thread TINFO has hit the tracepoint defined by
4202 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4203 action. This adds a while-stepping collecting state item to the
4204 threads' collecting state list, so that we can keep track of
4205 multiple simultaneous while-stepping actions being collected by the
4206 same thread. This can happen in cases like:
4208 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4210 ff0003 INSN3 <-- TP2, collect $regs
4211 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4214 Notice that when instruction INSN5 is reached, the while-stepping
4215 actions of both TP1 and TP3 are still being collected, and that TP2
4216 had been collected meanwhile. The whole range of ff0001-ff0005
4217 should be single-stepped, due to at least TP1's while-stepping
4218 action covering the whole range. */
4221 add_while_stepping_state (struct thread_info
*tinfo
,
4222 int tp_number
, CORE_ADDR tp_address
)
4224 struct wstep_state
*wstep
;
4226 wstep
= xmalloc (sizeof (*wstep
));
4227 wstep
->next
= tinfo
->while_stepping
;
4229 wstep
->tp_number
= tp_number
;
4230 wstep
->tp_address
= tp_address
;
4231 wstep
->current_step
= 0;
4233 tinfo
->while_stepping
= wstep
;
4236 /* Release the while-stepping collecting state WSTEP. */
4239 release_while_stepping_state (struct wstep_state
*wstep
)
4244 /* Release all while-stepping collecting states currently associated
4245 with thread TINFO. */
4248 release_while_stepping_state_list (struct thread_info
*tinfo
)
4250 struct wstep_state
*head
;
4252 while (tinfo
->while_stepping
)
4254 head
= tinfo
->while_stepping
;
4255 tinfo
->while_stepping
= head
->next
;
4256 release_while_stepping_state (head
);
4260 /* If TINFO was handling a 'while-stepping' action, the step has
4261 finished, so collect any step data needed, and check if any more
4262 steps are required. Return true if the thread was indeed
4263 collecting tracepoint data, false otherwise. */
4266 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4268 struct tracepoint
*tpoint
;
4269 struct wstep_state
*wstep
;
4270 struct wstep_state
**wstep_link
;
4271 struct trap_tracepoint_ctx ctx
;
4273 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4275 if (agent_loaded_p ())
4276 upload_fast_traceframes ();
4278 /* Check if we were indeed collecting data for one of more
4279 tracepoints with a 'while-stepping' count. */
4280 if (tinfo
->while_stepping
== NULL
)
4285 /* We're not even tracing anymore. Stop this thread from
4287 release_while_stepping_state_list (tinfo
);
4289 /* The thread had stopped due to a single-step request indeed
4290 explained by a tracepoint. */
4294 wstep
= tinfo
->while_stepping
;
4295 wstep_link
= &tinfo
->while_stepping
;
4297 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4298 target_pid_to_str (tinfo
->entry
.id
),
4299 wstep
->tp_number
, paddress (wstep
->tp_address
));
4301 ctx
.base
.type
= trap_tracepoint
;
4302 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4304 while (wstep
!= NULL
)
4306 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4309 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4310 wstep
->tp_number
, paddress (wstep
->tp_address
),
4311 target_pid_to_str (tinfo
->entry
.id
));
4314 *wstep_link
= wstep
->next
;
4315 release_while_stepping_state (wstep
);
4316 wstep
= *wstep_link
;
4320 /* We've just finished one step. */
4321 ++wstep
->current_step
;
4324 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4325 stop_pc
, tpoint
, wstep
->current_step
);
4327 if (wstep
->current_step
>= tpoint
->step_count
)
4329 /* The requested numbers of steps have occurred. */
4330 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4331 target_pid_to_str (tinfo
->entry
.id
),
4332 wstep
->tp_number
, paddress (wstep
->tp_address
));
4334 /* Unlink the wstep. */
4335 *wstep_link
= wstep
->next
;
4336 release_while_stepping_state (wstep
);
4337 wstep
= *wstep_link
;
4339 /* Only check the hit count now, which ensure that we do all
4340 our stepping before stopping the run. */
4341 if (tpoint
->pass_count
> 0
4342 && tpoint
->hit_count
>= tpoint
->pass_count
4343 && stopping_tracepoint
== NULL
)
4344 stopping_tracepoint
= tpoint
;
4348 /* Keep single-stepping until the requested numbers of steps
4350 wstep_link
= &wstep
->next
;
4351 wstep
= *wstep_link
;
4354 if (stopping_tracepoint
4355 || trace_buffer_is_full
4356 || expr_eval_result
!= expr_eval_no_error
)
4366 /* Handle any internal tracing control breakpoint hits. That means,
4367 pull traceframes from the IPA to our buffer, and syncing both
4368 tracing agents when the IPA's tracing stops for some reason. */
4371 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4373 /* Pull in fast tracepoint trace frames from the inferior in-process
4374 agent's buffer into our buffer. */
4376 if (!agent_loaded_p ())
4379 upload_fast_traceframes ();
4381 /* Check if the in-process agent had decided we should stop
4383 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4385 int ipa_trace_buffer_is_full
;
4386 CORE_ADDR ipa_stopping_tracepoint
;
4387 int ipa_expr_eval_result
;
4388 CORE_ADDR ipa_error_tracepoint
;
4390 trace_debug ("lib stopped at stop_tracing");
4392 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4393 &ipa_trace_buffer_is_full
);
4395 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4396 &ipa_stopping_tracepoint
);
4397 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4399 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4400 &ipa_error_tracepoint
);
4401 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4403 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4404 &ipa_expr_eval_result
);
4405 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4407 trace_debug ("lib: trace_buffer_is_full: %d, "
4408 "stopping_tracepoint: %s, "
4409 "ipa_expr_eval_result: %d, "
4410 "error_tracepoint: %s, ",
4411 ipa_trace_buffer_is_full
,
4412 paddress (ipa_stopping_tracepoint
),
4413 ipa_expr_eval_result
,
4414 paddress (ipa_error_tracepoint
));
4418 if (ipa_trace_buffer_is_full
)
4419 trace_debug ("lib stopped due to full buffer.");
4420 if (ipa_stopping_tracepoint
)
4421 trace_debug ("lib stopped due to tpoint");
4422 if (ipa_stopping_tracepoint
)
4423 trace_debug ("lib stopped due to error");
4426 if (ipa_stopping_tracepoint
!= 0)
4429 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4431 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4433 expr_eval_result
= ipa_expr_eval_result
;
4435 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4440 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4442 trace_debug ("lib stopped at flush_trace_buffer");
4449 /* Return true if TINFO just hit a tracepoint. Collect data if
4453 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4455 struct tracepoint
*tpoint
;
4457 struct trap_tracepoint_ctx ctx
;
4459 /* Not tracing, don't handle. */
4463 ctx
.base
.type
= trap_tracepoint
;
4464 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4466 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4468 /* Note that we collect fast tracepoints here as well. We'll
4469 step over the fast tracepoint jump later, which avoids the
4470 double collect. However, we don't collect for static
4471 tracepoints here, because UST markers are compiled in program,
4472 and probes will be executed in program. So static tracepoints
4473 are collected there. */
4474 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4475 && tpoint
->type
!= static_tracepoint
)
4477 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4478 target_pid_to_str (tinfo
->entry
.id
),
4479 tpoint
->number
, paddress (tpoint
->address
));
4481 /* Test the condition if present, and collect if true. */
4483 || (condition_true_at_tracepoint
4484 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4485 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4488 if (stopping_tracepoint
4489 || trace_buffer_is_full
4490 || expr_eval_result
!= expr_eval_no_error
)
4494 /* If the tracepoint had a 'while-stepping' action, then set
4495 the thread to collect this tracepoint on the following
4497 else if (tpoint
->step_count
> 0)
4499 add_while_stepping_state (tinfo
,
4500 tpoint
->number
, tpoint
->address
);
4512 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4513 struct ust_marker_data
;
4514 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4515 struct traceframe
*tframe
);
4518 /* Create a trace frame for the hit of the given tracepoint in the
4522 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4523 struct tracepoint
*tpoint
)
4525 struct traceframe
*tframe
;
4528 /* Only count it as a hit when we actually collect data. */
4529 tpoint
->hit_count
++;
4531 /* If we've exceeded a defined pass count, record the event for
4532 later, and finish the collection for this hit. This test is only
4533 for nonstepping tracepoints, stepping tracepoints test at the end
4534 of their while-stepping loop. */
4535 if (tpoint
->pass_count
> 0
4536 && tpoint
->hit_count
>= tpoint
->pass_count
4537 && tpoint
->step_count
== 0
4538 && stopping_tracepoint
== NULL
)
4539 stopping_tracepoint
= tpoint
;
4541 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4542 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4544 tframe
= add_traceframe (tpoint
);
4548 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4550 #ifndef IN_PROCESS_AGENT
4551 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4552 tpoint
->number
, paddress (tpoint
->address
),
4553 tpoint
->actions_str
[acti
]);
4556 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4557 tpoint
->actions
[acti
]);
4560 finish_traceframe (tframe
);
4563 if (tframe
== NULL
&& tracing
)
4564 trace_buffer_is_full
= 1;
4567 #ifndef IN_PROCESS_AGENT
4570 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4572 struct tracepoint
*tpoint
, int current_step
)
4574 struct traceframe
*tframe
;
4577 trace_debug ("Making new step traceframe for "
4578 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4579 tpoint
->number
, paddress (tpoint
->address
),
4580 current_step
, tpoint
->step_count
,
4583 tframe
= add_traceframe (tpoint
);
4587 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4589 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4590 tpoint
->number
, paddress (tpoint
->address
),
4591 tpoint
->step_actions_str
[acti
]);
4593 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4594 tpoint
->step_actions
[acti
]);
4597 finish_traceframe (tframe
);
4600 if (tframe
== NULL
&& tracing
)
4601 trace_buffer_is_full
= 1;
4606 static struct regcache
*
4607 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4609 struct regcache
*regcache
= NULL
;
4611 #ifdef IN_PROCESS_AGENT
4612 if (ctx
->type
== fast_tracepoint
)
4614 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4615 if (!fctx
->regcache_initted
)
4617 fctx
->regcache_initted
= 1;
4618 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4619 supply_regblock (&fctx
->regcache
, NULL
);
4620 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4622 regcache
= &fctx
->regcache
;
4625 if (ctx
->type
== static_tracepoint
)
4627 struct static_tracepoint_ctx
*sctx
4628 = (struct static_tracepoint_ctx
*) ctx
;
4630 if (!sctx
->regcache_initted
)
4632 sctx
->regcache_initted
= 1;
4633 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4634 supply_regblock (&sctx
->regcache
, NULL
);
4635 /* Pass down the tracepoint address, because REGS doesn't
4636 include the PC, but we know what it must have been. */
4637 supply_static_tracepoint_registers (&sctx
->regcache
,
4638 (const unsigned char *)
4640 sctx
->tpoint
->address
);
4642 regcache
= &sctx
->regcache
;
4646 if (ctx
->type
== trap_tracepoint
)
4648 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4649 regcache
= tctx
->regcache
;
4653 gdb_assert (regcache
!= NULL
);
4659 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4661 struct tracepoint
*tpoint
,
4662 struct traceframe
*tframe
,
4663 struct tracepoint_action
*taction
)
4665 enum eval_result_type err
;
4667 switch (taction
->type
)
4671 struct collect_memory_action
*maction
;
4673 maction
= (struct collect_memory_action
*) taction
;
4675 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4676 pulongest (maction
->len
),
4677 paddress (maction
->addr
), maction
->basereg
);
4678 /* (should use basereg) */
4679 agent_mem_read (tframe
, NULL
,
4680 (CORE_ADDR
) maction
->addr
, maction
->len
);
4685 unsigned char *regspace
;
4686 struct regcache tregcache
;
4687 struct regcache
*context_regcache
;
4690 trace_debug ("Want to collect registers");
4692 /* Collect all registers for now. */
4693 regspace
= add_traceframe_block (tframe
,
4694 1 + register_cache_size ());
4695 if (regspace
== NULL
)
4697 trace_debug ("Trace buffer block allocation failed, skipping");
4700 /* Identify a register block. */
4703 context_regcache
= get_context_regcache (ctx
);
4705 /* Wrap the regblock in a register cache (in the stack, we
4706 don't want to malloc here). */
4707 init_register_cache (&tregcache
, regspace
+ 1);
4709 /* Copy the register data to the regblock. */
4710 regcache_cpy (&tregcache
, context_regcache
);
4712 #ifndef IN_PROCESS_AGENT
4713 /* On some platforms, trap-based tracepoints will have the PC
4714 pointing to the next instruction after the trap, but we
4715 don't want the user or GDB trying to guess whether the
4716 saved PC needs adjusting; so always record the adjusted
4717 stop_pc. Note that we can't use tpoint->address instead,
4718 since it will be wrong for while-stepping actions. This
4719 adjustment is a nop for fast tracepoints collected from the
4720 in-process lib (but not if GDBserver is collecting one
4721 preemptively), since the PC had already been adjusted to
4722 contain the tracepoint's address by the jump pad. */
4723 trace_debug ("Storing stop pc (0x%s) in regblock",
4724 paddress (stop_pc
));
4726 /* This changes the regblock, not the thread's
4728 regcache_write_pc (&tregcache
, stop_pc
);
4734 struct eval_expr_action
*eaction
;
4736 eaction
= (struct eval_expr_action
*) taction
;
4738 trace_debug ("Want to evaluate expression");
4740 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4742 if (err
!= expr_eval_no_error
)
4744 record_tracepoint_error (tpoint
, "action expression", err
);
4751 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4752 trace_debug ("Want to collect static trace data");
4753 collect_ust_data_at_tracepoint (ctx
, tframe
);
4755 trace_debug ("warning: collecting static trace data, "
4756 "but static tracepoints are not supported");
4761 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4767 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4768 struct tracepoint
*tpoint
)
4771 enum eval_result_type err
;
4773 /* Presently, gdbserver doesn't run compiled conditions, only the
4774 IPA does. If the program stops at a fast tracepoint's address
4775 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4776 gdbserver preemptively collect the fast tracepoint. Later, on
4777 resume, gdbserver steps over the fast tracepoint like it steps
4778 over breakpoints, so that the IPA doesn't see that fast
4779 tracepoint. This avoids double collects of fast tracepoints in
4780 that stopping scenario. Having gdbserver itself handle the fast
4781 tracepoint gives the user a consistent view of when fast or trap
4782 tracepoints are collected, compared to an alternative where only
4783 trap tracepoints are collected on stop, and fast tracepoints on
4784 resume. When a fast tracepoint is being processed by gdbserver,
4785 it is always the non-compiled condition expression that is
4787 #ifdef IN_PROCESS_AGENT
4788 if (tpoint
->compiled_cond
)
4789 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4792 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4794 if (err
!= expr_eval_no_error
)
4796 record_tracepoint_error (tpoint
, "condition", err
);
4797 /* The error case must return false. */
4801 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4802 tpoint
->number
, paddress (tpoint
->address
),
4804 return (value
? 1 : 0);
4807 /* Evaluates a tracepoint agent expression with context CTX,
4808 traceframe TFRAME, agent expression AEXPR and store the
4811 static enum eval_result_type
4812 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4813 struct traceframe
*tframe
,
4814 struct agent_expr
*aexpr
,
4817 struct regcache
*regcache
;
4818 regcache
= get_context_regcache (ctx
);
4820 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4823 /* Do memory copies for bytecodes. */
4824 /* Do the recording of memory blocks for actions and bytecodes. */
4827 agent_mem_read (struct traceframe
*tframe
,
4828 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4830 unsigned char *mspace
;
4831 ULONGEST remaining
= len
;
4832 unsigned short blocklen
;
4834 /* If a 'to' buffer is specified, use it. */
4837 read_inferior_memory (from
, to
, len
);
4841 /* Otherwise, create a new memory block in the trace buffer. */
4842 while (remaining
> 0)
4846 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4847 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4848 mspace
= add_traceframe_block (tframe
, sp
);
4851 /* Identify block as a memory block. */
4854 /* Record address and size. */
4855 memcpy (mspace
, &from
, sizeof (from
));
4856 mspace
+= sizeof (from
);
4857 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4858 mspace
+= sizeof (blocklen
);
4859 /* Record the memory block proper. */
4860 read_inferior_memory (from
, mspace
, blocklen
);
4861 trace_debug ("%d bytes recorded", blocklen
);
4862 remaining
-= blocklen
;
4869 agent_mem_read_string (struct traceframe
*tframe
,
4870 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4872 unsigned char *buf
, *mspace
;
4873 ULONGEST remaining
= len
;
4874 unsigned short blocklen
, i
;
4876 /* To save a bit of space, block lengths are 16-bit, so break large
4877 requests into multiple blocks. Bordering on overkill for strings,
4878 but it could happen that someone specifies a large max length. */
4879 while (remaining
> 0)
4883 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4884 /* We want working space to accumulate nonzero bytes, since
4885 traceframes must have a predecided size (otherwise it gets
4886 harder to wrap correctly for the circular case, etc). */
4887 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4888 for (i
= 0; i
< blocklen
; ++i
)
4890 /* Read the string one byte at a time, in case the string is
4891 at the end of a valid memory area - we don't want a
4892 correctly-terminated string to engender segvio
4894 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4899 /* Make sure outer loop stops now too. */
4900 remaining
= blocklen
;
4904 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4905 mspace
= add_traceframe_block (tframe
, sp
);
4911 /* Identify block as a memory block. */
4914 /* Record address and size. */
4915 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4916 mspace
+= sizeof (from
);
4917 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4918 mspace
+= sizeof (blocklen
);
4919 /* Copy the string contents. */
4920 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4921 remaining
-= blocklen
;
4928 /* Record the value of a trace state variable. */
4931 agent_tsv_read (struct traceframe
*tframe
, int n
)
4933 unsigned char *vspace
;
4936 vspace
= add_traceframe_block (tframe
,
4937 1 + sizeof (n
) + sizeof (LONGEST
));
4940 /* Identify block as a variable. */
4942 /* Record variable's number and value. */
4943 memcpy (vspace
+ 1, &n
, sizeof (n
));
4944 val
= get_trace_state_variable_value (n
);
4945 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4946 trace_debug ("Variable %d recorded", n
);
4950 #ifndef IN_PROCESS_AGENT
4952 /* Callback for traceframe_walk_blocks, used to find a given block
4953 type in a traceframe. */
4956 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4958 char *wantedp
= data
;
4960 if (*wantedp
== blocktype
)
4966 /* Walk over all traceframe blocks of the traceframe buffer starting
4967 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4968 block found, passing in DATA unmodified. If CALLBACK returns true,
4969 this returns a pointer to where the block is found. Returns NULL
4970 if no callback call returned true, indicating that all blocks have
4973 static unsigned char *
4974 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4976 int (*callback
) (char blocktype
,
4977 unsigned char *dataptr
,
4981 unsigned char *dataptr
;
4985 trace_debug ("traceframe %d has no data", tfnum
);
4989 /* Iterate through a traceframe's blocks, looking for a block of the
4991 for (dataptr
= database
;
4992 dataptr
< database
+ datasize
;
4996 unsigned short mlen
;
4998 if (dataptr
== trace_buffer_wrap
)
5000 /* Adjust to reflect wrapping part of the frame around to
5002 datasize
= dataptr
- database
;
5003 dataptr
= database
= trace_buffer_lo
;
5006 blocktype
= *dataptr
++;
5008 if ((*callback
) (blocktype
, dataptr
, data
))
5014 /* Skip over the registers block. */
5015 dataptr
+= register_cache_size ();
5018 /* Skip over the memory block. */
5019 dataptr
+= sizeof (CORE_ADDR
);
5020 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5021 dataptr
+= (sizeof (mlen
) + mlen
);
5024 /* Skip over the TSV block. */
5025 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
5028 /* Skip over the static trace data block. */
5029 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5030 dataptr
+= (sizeof (mlen
) + mlen
);
5033 trace_debug ("traceframe %d has unknown block type 0x%x",
5042 /* Look for the block of type TYPE_WANTED in the trameframe starting
5043 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5046 static unsigned char *
5047 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
5048 int tfnum
, char type_wanted
)
5050 return traceframe_walk_blocks (database
, datasize
, tfnum
,
5051 match_blocktype
, &type_wanted
);
5054 static unsigned char *
5055 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
5057 unsigned char *regblock
;
5059 regblock
= traceframe_find_block_type (tframe
->data
,
5063 if (regblock
== NULL
)
5064 trace_debug ("traceframe %d has no register data", tfnum
);
5069 /* Get registers from a traceframe. */
5072 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
5074 unsigned char *dataptr
;
5075 struct tracepoint
*tpoint
;
5076 struct traceframe
*tframe
;
5078 tframe
= find_traceframe (tfnum
);
5082 trace_debug ("traceframe %d not found", tfnum
);
5086 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
5087 if (dataptr
== NULL
)
5089 /* Mark registers unavailable. */
5090 supply_regblock (regcache
, NULL
);
5092 /* We can generally guess at a PC, although this will be
5093 misleading for while-stepping frames and multi-location
5095 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
5097 regcache_write_pc (regcache
, tpoint
->address
);
5100 supply_regblock (regcache
, dataptr
);
5106 traceframe_get_pc (struct traceframe
*tframe
)
5108 struct regcache regcache
;
5109 unsigned char *dataptr
;
5111 dataptr
= traceframe_find_regblock (tframe
, -1);
5112 if (dataptr
== NULL
)
5115 init_register_cache (®cache
, dataptr
);
5116 return regcache_read_pc (®cache
);
5119 /* Read a requested block of memory from a trace frame. */
5122 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5123 unsigned char *buf
, ULONGEST length
,
5126 struct traceframe
*tframe
;
5127 unsigned char *database
, *dataptr
;
5128 unsigned int datasize
;
5130 unsigned short mlen
;
5132 trace_debug ("traceframe_read_mem");
5134 tframe
= find_traceframe (tfnum
);
5138 trace_debug ("traceframe %d not found", tfnum
);
5142 datasize
= tframe
->data_size
;
5143 database
= dataptr
= &tframe
->data
[0];
5145 /* Iterate through a traceframe's blocks, looking for memory. */
5146 while ((dataptr
= traceframe_find_block_type (dataptr
,
5148 - (dataptr
- database
),
5149 tfnum
, 'M')) != NULL
)
5151 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5152 dataptr
+= sizeof (maddr
);
5153 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5154 dataptr
+= sizeof (mlen
);
5155 trace_debug ("traceframe %d has %d bytes at %s",
5156 tfnum
, mlen
, paddress (maddr
));
5158 /* If the block includes the first part of the desired range,
5159 return as much it has; GDB will re-request the remainder,
5160 which might be in a different block of this trace frame. */
5161 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5163 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5167 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5172 /* Skip over this block. */
5176 trace_debug ("traceframe %d has no memory data for the desired region",
5184 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5187 struct traceframe
*tframe
;
5188 unsigned char *database
, *dataptr
;
5189 unsigned int datasize
;
5192 trace_debug ("traceframe_read_tsv");
5194 tfnum
= current_traceframe
;
5198 trace_debug ("no current traceframe");
5202 tframe
= find_traceframe (tfnum
);
5206 trace_debug ("traceframe %d not found", tfnum
);
5210 datasize
= tframe
->data_size
;
5211 database
= dataptr
= &tframe
->data
[0];
5213 /* Iterate through a traceframe's blocks, looking for the tsv. */
5214 while ((dataptr
= traceframe_find_block_type (dataptr
,
5216 - (dataptr
- database
),
5217 tfnum
, 'V')) != NULL
)
5219 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5220 dataptr
+= sizeof (vnum
);
5222 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5224 /* Check that this is the variable we want. */
5227 memcpy (val
, dataptr
, sizeof (*val
));
5231 /* Skip over this block. */
5232 dataptr
+= sizeof (LONGEST
);
5235 trace_debug ("traceframe %d has no data for variable %d",
5240 /* Read a requested block of static tracepoint data from a trace
5244 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5245 unsigned char *buf
, ULONGEST length
,
5248 struct traceframe
*tframe
;
5249 unsigned char *database
, *dataptr
;
5250 unsigned int datasize
;
5251 unsigned short mlen
;
5253 trace_debug ("traceframe_read_sdata");
5255 tframe
= find_traceframe (tfnum
);
5259 trace_debug ("traceframe %d not found", tfnum
);
5263 datasize
= tframe
->data_size
;
5264 database
= &tframe
->data
[0];
5266 /* Iterate through a traceframe's blocks, looking for static
5268 dataptr
= traceframe_find_block_type (database
, datasize
,
5270 if (dataptr
!= NULL
)
5272 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5273 dataptr
+= sizeof (mlen
);
5276 if (offset
+ length
> mlen
)
5277 length
= mlen
- offset
;
5279 memcpy (buf
, dataptr
, length
);
5287 trace_debug ("traceframe %d has no static trace data", tfnum
);
5293 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5294 object. DATA is pointer to a struct buffer holding the
5295 traceframe-info object being built. */
5298 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5300 struct buffer
*buffer
= data
;
5306 unsigned short mlen
;
5309 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5310 dataptr
+= sizeof (maddr
);
5311 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5312 dataptr
+= sizeof (mlen
);
5313 buffer_xml_printf (buffer
,
5314 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5315 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5325 warning ("Unhandled trace block type (%d) '%c ' "
5326 "while building trace frame info.",
5327 blocktype
, blocktype
);
5334 /* Build a traceframe-info object for traceframe number TFNUM into
5338 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5340 struct traceframe
*tframe
;
5342 trace_debug ("traceframe_read_info");
5344 tframe
= find_traceframe (tfnum
);
5348 trace_debug ("traceframe %d not found", tfnum
);
5352 buffer_grow_str (buffer
, "<traceframe-info>\n");
5353 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5354 tfnum
, build_traceframe_info_xml
, buffer
);
5355 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5359 /* Return the first fast tracepoint whose jump pad contains PC. */
5361 static struct tracepoint
*
5362 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5364 struct tracepoint
*tpoint
;
5366 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5367 if (tpoint
->type
== fast_tracepoint
)
5368 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5374 /* Return the first fast tracepoint whose trampoline contains PC. */
5376 static struct tracepoint
*
5377 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5379 struct tracepoint
*tpoint
;
5381 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5383 if (tpoint
->type
== fast_tracepoint
5384 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5391 /* Return GDBserver's tracepoint that matches the IP Agent's
5392 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5395 static struct tracepoint
*
5396 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5398 struct tracepoint
*tpoint
;
5400 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5401 if (tpoint
->type
== fast_tracepoint
)
5402 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5410 /* The type of the object that is used to synchronize fast tracepoint
5413 typedef struct collecting_t
5415 /* The fast tracepoint number currently collecting. */
5418 /* A number that GDBserver can use to identify the thread that is
5419 presently holding the collect lock. This need not (and usually
5420 is not) the thread id, as getting the current thread ID usually
5421 requires a system call, which we want to avoid like the plague.
5422 Usually this is thread's TCB, found in the TLS (pseudo-)
5423 register, which is readable with a single insn on several
5425 uintptr_t thread_area
;
5428 #ifndef IN_PROCESS_AGENT
5431 force_unlock_trace_buffer (void)
5433 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5436 /* Check if the thread identified by THREAD_AREA which is stopped at
5437 STOP_PC, is presently locking the fast tracepoint collection, and
5438 if so, gather some status of said collection. Returns 0 if the
5439 thread isn't collecting or in the jump pad at all. 1, if in the
5440 jump pad (or within gdb_collect) and hasn't executed the adjusted
5441 original insn yet (can set a breakpoint there and run to it). 2,
5442 if presently executing the adjusted original insn --- in which
5443 case, if we want to move the thread out of the jump pad, we need to
5444 single-step it until this function returns 0. */
5447 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5449 struct fast_tpoint_collect_status
*status
)
5451 CORE_ADDR ipa_collecting
;
5452 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5453 CORE_ADDR ipa_gdb_trampoline_buffer
;
5454 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5455 struct tracepoint
*tpoint
;
5456 int needs_breakpoint
;
5458 /* The thread THREAD_AREA is either:
5460 0. not collecting at all, not within the jump pad, or within
5461 gdb_collect or one of its callees.
5463 1. in the jump pad and haven't reached gdb_collect
5465 2. within gdb_collect (out of the jump pad) (collect is set)
5467 3. we're in the jump pad, after gdb_collect having returned,
5468 possibly executing the adjusted insns.
5470 For cases 1 and 3, `collecting' may or not be set. The jump pad
5471 doesn't have any complicated jump logic, so we can tell if the
5472 thread is executing the adjust original insn or not by just
5473 matching STOP_PC with known jump pad addresses. If we it isn't
5474 yet executing the original insn, set a breakpoint there, and let
5475 the thread run to it, so to quickly step over a possible (many
5476 insns) gdb_collect call. Otherwise, or when the breakpoint is
5477 hit, only a few (small number of) insns are left to be executed
5478 in the jump pad. Single-step the thread until it leaves the
5483 needs_breakpoint
= 0;
5484 trace_debug ("fast_tracepoint_collecting");
5486 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5487 &ipa_gdb_jump_pad_buffer
))
5488 fatal ("error extracting `gdb_jump_pad_buffer'");
5489 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5490 &ipa_gdb_jump_pad_buffer_end
))
5491 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5493 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5494 &ipa_gdb_trampoline_buffer
))
5495 fatal ("error extracting `gdb_trampoline_buffer'");
5496 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5497 &ipa_gdb_trampoline_buffer_end
))
5498 fatal ("error extracting `gdb_trampoline_buffer_end'");
5500 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5501 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5503 /* We can tell which tracepoint(s) the thread is collecting by
5504 matching the jump pad address back to the tracepoint. */
5505 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5508 warning ("in jump pad, but no matching tpoint?");
5513 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5515 tpoint
->number
, paddress (tpoint
->address
),
5516 paddress (tpoint
->jump_pad
),
5517 paddress (tpoint
->jump_pad_end
),
5518 paddress (tpoint
->adjusted_insn_addr
),
5519 paddress (tpoint
->adjusted_insn_addr_end
));
5522 /* Definitely in the jump pad. May or may not need
5523 fast-exit-jump-pad breakpoint. */
5524 if (tpoint
->jump_pad
<= stop_pc
5525 && stop_pc
< tpoint
->adjusted_insn_addr
)
5526 needs_breakpoint
= 1;
5528 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5529 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5531 /* We can tell which tracepoint(s) the thread is collecting by
5532 matching the trampoline address back to the tracepoint. */
5533 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5536 warning ("in trampoline, but no matching tpoint?");
5541 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5542 tpoint
->number
, paddress (tpoint
->address
),
5543 paddress (tpoint
->trampoline
),
5544 paddress (tpoint
->trampoline_end
));
5547 /* Have not reached jump pad yet, but treat the trampoline as a
5548 part of the jump pad that is before the adjusted original
5550 needs_breakpoint
= 1;
5554 collecting_t ipa_collecting_obj
;
5556 /* If `collecting' is set/locked, then the THREAD_AREA thread
5557 may or not be the one holding the lock. We have to read the
5558 lock to find out. */
5560 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5563 trace_debug ("fast_tracepoint_collecting:"
5564 " failed reading 'collecting' in the inferior");
5568 if (!ipa_collecting
)
5570 trace_debug ("fast_tracepoint_collecting: not collecting"
5571 " (and nobody is).");
5575 /* Some thread is collecting. Check which. */
5576 if (read_inferior_memory (ipa_collecting
,
5577 (unsigned char *) &ipa_collecting_obj
,
5578 sizeof (ipa_collecting_obj
)) != 0)
5581 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5583 trace_debug ("fast_tracepoint_collecting: not collecting "
5584 "(another thread is)");
5589 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5592 warning ("fast_tracepoint_collecting: collecting, "
5593 "but tpoint %s not found?",
5594 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5598 /* The thread is within `gdb_collect', skip over the rest of
5599 fast tracepoint collection quickly using a breakpoint. */
5600 needs_breakpoint
= 1;
5603 /* The caller wants a bit of status detail. */
5606 status
->tpoint_num
= tpoint
->number
;
5607 status
->tpoint_addr
= tpoint
->address
;
5608 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5609 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5612 if (needs_breakpoint
)
5614 /* Hasn't executed the original instruction yet. Set breakpoint
5615 there, and wait till it's hit, then single-step until exiting
5619 fast_tracepoint_collecting, returning continue-until-break at %s",
5620 paddress (tpoint
->adjusted_insn_addr
));
5622 return 1; /* continue */
5626 /* Just single-step until exiting the jump pad. */
5628 trace_debug ("fast_tracepoint_collecting, returning "
5629 "need-single-step (%s-%s)",
5630 paddress (tpoint
->adjusted_insn_addr
),
5631 paddress (tpoint
->adjusted_insn_addr_end
));
5633 return 2; /* single-step */
5639 #ifdef IN_PROCESS_AGENT
5641 /* The global fast tracepoint collect lock. Points to a collecting_t
5642 object built on the stack by the jump pad, if presently locked;
5643 NULL if it isn't locked. Note that this lock *must* be set while
5644 executing any *function other than the jump pad. See
5645 fast_tracepoint_collecting. */
5646 static collecting_t
* ATTR_USED collecting
;
5648 /* This routine, called from the jump pad (in asm) is designed to be
5649 called from the jump pads of fast tracepoints, thus it is on the
5652 IP_AGENT_EXPORT
void ATTR_USED
5653 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5655 struct fast_tracepoint_ctx ctx
;
5657 /* Don't do anything until the trace run is completely set up. */
5661 ctx
.base
.type
= fast_tracepoint
;
5663 ctx
.regcache_initted
= 0;
5664 /* Wrap the regblock in a register cache (in the stack, we don't
5665 want to malloc here). */
5666 ctx
.regspace
= alloca (register_cache_size ());
5667 if (ctx
.regspace
== NULL
)
5669 trace_debug ("Trace buffer block allocation failed, skipping");
5673 for (ctx
.tpoint
= tpoint
;
5674 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5675 ctx
.tpoint
= ctx
.tpoint
->next
)
5677 if (!ctx
.tpoint
->enabled
)
5680 /* Multiple tracepoints of different types, such as fast tracepoint and
5681 static tracepoint, can be set at the same address. */
5682 if (ctx
.tpoint
->type
!= tpoint
->type
)
5685 /* Test the condition if present, and collect if true. */
5686 if (ctx
.tpoint
->cond
== NULL
5687 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5690 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5691 ctx
.tpoint
->address
, ctx
.tpoint
);
5693 /* Note that this will cause original insns to be written back
5694 to where we jumped from, but that's OK because we're jumping
5695 back to the next whole instruction. This will go badly if
5696 instruction restoration is not atomic though. */
5697 if (stopping_tracepoint
5698 || trace_buffer_is_full
5699 || expr_eval_result
!= expr_eval_no_error
)
5707 /* If there was a condition and it evaluated to false, the only
5708 way we would stop tracing is if there was an error during
5709 condition expression evaluation. */
5710 if (expr_eval_result
!= expr_eval_no_error
)
5721 #ifndef IN_PROCESS_AGENT
5724 get_raw_reg_func_addr (void)
5726 return ipa_sym_addrs
.addr_get_raw_reg
;
5730 get_get_tsv_func_addr (void)
5732 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5736 get_set_tsv_func_addr (void)
5738 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5742 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5743 CORE_ADDR
*jump_entry
)
5745 CORE_ADDR entry_point
= *jump_entry
;
5746 enum eval_result_type err
;
5748 trace_debug ("Starting condition compilation for tracepoint %d\n",
5751 /* Initialize the global pointer to the code being built. */
5752 current_insn_ptr
= *jump_entry
;
5756 err
= compile_bytecodes (tpoint
->cond
);
5758 if (err
== expr_eval_no_error
)
5762 /* Record the beginning of the compiled code. */
5763 tpoint
->compiled_cond
= entry_point
;
5765 trace_debug ("Condition compilation for tracepoint %d complete\n",
5770 /* Leave the unfinished code in situ, but don't point to it. */
5772 tpoint
->compiled_cond
= 0;
5774 trace_debug ("Condition compilation for tracepoint %d failed, "
5776 tpoint
->number
, err
);
5779 /* Update the code pointer passed in. Note that we do this even if
5780 the compile fails, so that we can look at the partial results
5781 instead of letting them be overwritten. */
5782 *jump_entry
= current_insn_ptr
;
5784 /* Leave a gap, to aid dump decipherment. */
5788 /* We'll need to adjust these when we consider bi-arch setups, and big
5792 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5794 return write_inferior_memory (where
,
5795 (unsigned char *) &ptr
, sizeof (void *));
5798 /* The base pointer of the IPA's heap. This is the only memory the
5799 IPA is allowed to use. The IPA should _not_ call the inferior's
5800 `malloc' during operation. That'd be slow, and, most importantly,
5801 it may not be safe. We may be collecting a tracepoint in a signal
5802 handler, for example. */
5803 static CORE_ADDR target_tp_heap
;
5805 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5809 target_malloc (ULONGEST size
)
5813 if (target_tp_heap
== 0)
5815 /* We have the pointer *address*, need what it points to. */
5816 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5818 fatal ("could get target heap head pointer");
5821 ptr
= target_tp_heap
;
5822 target_tp_heap
+= size
;
5824 /* Pad to 8-byte alignment. */
5825 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5831 download_agent_expr (struct agent_expr
*expr
)
5833 CORE_ADDR expr_addr
;
5834 CORE_ADDR expr_bytes
;
5836 expr_addr
= target_malloc (sizeof (*expr
));
5837 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5839 expr_bytes
= target_malloc (expr
->length
);
5840 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5842 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5847 /* Align V up to N bits. */
5848 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5850 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5853 download_tracepoint_1 (struct tracepoint
*tpoint
)
5855 struct tracepoint target_tracepoint
;
5856 CORE_ADDR tpptr
= 0;
5858 gdb_assert (tpoint
->type
== fast_tracepoint
5859 || tpoint
->type
== static_tracepoint
);
5861 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5863 CORE_ADDR jentry
, jump_entry
;
5865 jentry
= jump_entry
= get_jump_space_head ();
5867 if (tpoint
->cond
!= NULL
)
5869 /* Pad to 8-byte alignment. (needed?) */
5870 /* Actually this should be left for the target to
5872 jentry
= UALIGN (jentry
, 8);
5874 compile_tracepoint_condition (tpoint
, &jentry
);
5877 /* Pad to 8-byte alignment. */
5878 jentry
= UALIGN (jentry
, 8);
5879 claim_jump_space (jentry
- jump_entry
);
5882 target_tracepoint
= *tpoint
;
5884 tpptr
= target_malloc (sizeof (*tpoint
));
5885 tpoint
->obj_addr_on_target
= tpptr
;
5887 /* Write the whole object. We'll fix up its pointers in a bit.
5888 Assume no next for now. This is fixed up above on the next
5889 iteration, if there's any. */
5890 target_tracepoint
.next
= NULL
;
5891 /* Need to clear this here too, since we're downloading the
5892 tracepoints before clearing our own copy. */
5893 target_tracepoint
.hit_count
= 0;
5895 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5896 sizeof (target_tracepoint
));
5899 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5901 download_agent_expr (tpoint
->cond
));
5903 if (tpoint
->numactions
)
5906 CORE_ADDR actions_array
;
5908 /* The pointers array. */
5910 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5911 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5915 /* Now for each pointer, download the action. */
5916 for (i
= 0; i
< tpoint
->numactions
; i
++)
5918 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5919 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5921 if (ipa_action
!= 0)
5922 write_inferior_data_ptr
5923 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5929 #define IPA_PROTO_FAST_TRACE_FLAG 0
5930 #define IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET 2
5931 #define IPA_PROTO_FAST_TRACE_JUMP_PAD 10
5932 #define IPA_PROTO_FAST_TRACE_FJUMP_SIZE 18
5933 #define IPA_PROTO_FAST_TRACE_FJUMP_INSN 22
5935 /* Send a command to agent to download and install tracepoint TPOINT. */
5938 tracepoint_send_agent (struct tracepoint
*tpoint
)
5940 char buf
[IPA_CMD_BUF_SIZE
];
5945 strcpy (p
, "FastTrace:");
5948 COPY_FIELD_TO_BUF (p
, tpoint
, number
);
5949 COPY_FIELD_TO_BUF (p
, tpoint
, address
);
5950 COPY_FIELD_TO_BUF (p
, tpoint
, type
);
5951 COPY_FIELD_TO_BUF (p
, tpoint
, enabled
);
5952 COPY_FIELD_TO_BUF (p
, tpoint
, step_count
);
5953 COPY_FIELD_TO_BUF (p
, tpoint
, pass_count
);
5954 COPY_FIELD_TO_BUF (p
, tpoint
, numactions
);
5955 COPY_FIELD_TO_BUF (p
, tpoint
, hit_count
);
5956 COPY_FIELD_TO_BUF (p
, tpoint
, traceframe_usage
);
5957 COPY_FIELD_TO_BUF (p
, tpoint
, compiled_cond
);
5958 COPY_FIELD_TO_BUF (p
, tpoint
, orig_size
);
5961 p
= agent_expr_send (p
, tpoint
->cond
);
5963 /* tracepoint_action */
5964 for (i
= 0; i
< tpoint
->numactions
; i
++)
5966 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5968 p
[0] = action
->type
;
5969 p
= action
->ops
->send (&p
[1], action
);
5972 get_jump_space_head ();
5973 /* Copy the value of GDB_JUMP_PAD_HEAD to command buffer, so that
5974 agent can use jump pad from it. */
5975 if (tpoint
->type
== fast_tracepoint
)
5977 memcpy (p
, &gdb_jump_pad_head
, 8);
5981 ret
= run_inferior_command (buf
, (int) (ptrdiff_t) (p
- buf
));
5985 if (strncmp (buf
, "OK", 2) != 0)
5988 /* The value of tracepoint's target address is stored in BUF. */
5989 memcpy (&tpoint
->obj_addr_on_target
,
5990 &buf
[IPA_PROTO_FAST_TRACE_ADDR_ON_TARGET
], 8);
5992 if (tpoint
->type
== fast_tracepoint
)
5995 = (unsigned char *) &buf
[IPA_PROTO_FAST_TRACE_FJUMP_INSN
];
5998 trace_debug ("agent: read from cmd_buf 0x%x 0x%x\n",
5999 (unsigned int) tpoint
->obj_addr_on_target
,
6000 (unsigned int) gdb_jump_pad_head
);
6002 memcpy (&gdb_jump_pad_head
, &buf
[IPA_PROTO_FAST_TRACE_JUMP_PAD
], 8);
6004 /* This has been done in agent. We should also set up record for it. */
6005 memcpy (&fjump_size
, &buf
[IPA_PROTO_FAST_TRACE_FJUMP_SIZE
], 4);
6008 = set_fast_tracepoint_jump (tpoint
->address
, insn
, fjump_size
);
6015 download_tracepoint (struct tracepoint
*tpoint
)
6017 struct tracepoint
*tp
, *tp_prev
;
6019 if (tpoint
->type
!= fast_tracepoint
6020 && tpoint
->type
!= static_tracepoint
)
6023 download_tracepoint_1 (tpoint
);
6025 /* Find the previous entry of TPOINT, which is fast tracepoint or
6026 static tracepoint. */
6028 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
6030 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
6036 CORE_ADDR tp_prev_target_next_addr
;
6038 /* Insert TPOINT after TP_PREV in IPA. */
6039 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
6040 + offsetof (struct tracepoint
, next
),
6041 &tp_prev_target_next_addr
))
6042 fatal ("error reading `tp_prev->next'");
6044 /* tpoint->next = tp_prev->next */
6045 write_inferior_data_ptr (tpoint
->obj_addr_on_target
6046 + offsetof (struct tracepoint
, next
),
6047 tp_prev_target_next_addr
);
6048 /* tp_prev->next = tpoint */
6049 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
6050 + offsetof (struct tracepoint
, next
),
6051 tpoint
->obj_addr_on_target
);
6054 /* First object in list, set the head pointer in the
6056 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
6057 tpoint
->obj_addr_on_target
);
6062 download_trace_state_variables (void)
6064 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6065 struct trace_state_variable
*tsv
;
6067 /* Start out empty. */
6068 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6070 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6072 struct trace_state_variable target_tsv
;
6074 /* TSV's with a getter have been initialized equally in both the
6075 inferior and GDBserver. Skip them. */
6076 if (tsv
->getter
!= NULL
)
6082 ptr
= target_malloc (sizeof (*tsv
));
6084 if (tsv
== trace_state_variables
)
6086 /* First object in list, set the head pointer in the
6089 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6094 write_inferior_data_ptr (prev_ptr
6095 + offsetof (struct trace_state_variable
,
6100 /* Write the whole object. We'll fix up its pointers in a bit.
6101 Assume no next, fixup when needed. */
6102 target_tsv
.next
= NULL
;
6104 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6105 sizeof (target_tsv
));
6107 if (tsv
->name
!= NULL
)
6109 size_t size
= strlen (tsv
->name
) + 1;
6110 CORE_ADDR name_addr
= target_malloc (size
);
6111 write_inferior_memory (name_addr
,
6112 (unsigned char *) tsv
->name
, size
);
6113 write_inferior_data_ptr (ptr
6114 + offsetof (struct trace_state_variable
,
6119 if (tsv
->getter
!= NULL
)
6121 fatal ("what to do with these?");
6127 /* Fixup the next pointer in the last item in the list. */
6128 write_inferior_data_ptr (prev_ptr
6129 + offsetof (struct trace_state_variable
,
6134 /* Upload complete trace frames out of the IP Agent's trace buffer
6135 into GDBserver's trace buffer. This always uploads either all or
6136 no trace frames. This is the counter part of
6137 `trace_alloc_trace_buffer'. See its description of the atomic
6138 synching mechanism. */
6141 upload_fast_traceframes (void)
6143 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6144 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6146 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6147 unsigned int curr_tbctrl_idx
;
6148 unsigned int ipa_trace_buffer_ctrl_curr
;
6149 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6150 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6151 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6152 CORE_ADDR ipa_trace_buffer_lo
;
6153 CORE_ADDR ipa_trace_buffer_hi
;
6155 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6156 &ipa_traceframe_read_count_racy
))
6158 /* This will happen in most targets if the current thread is
6163 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6164 &ipa_traceframe_write_count_racy
))
6167 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6168 ipa_traceframe_write_count_racy
6169 - ipa_traceframe_read_count_racy
,
6170 ipa_traceframe_write_count_racy
,
6171 ipa_traceframe_read_count_racy
);
6173 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6176 about_to_request_buffer_space_bkpt
6177 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6180 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6181 &ipa_trace_buffer_ctrl_curr
))
6184 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6186 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6189 unsigned int prev
, counter
;
6191 /* Update the token, with new counters, and the GDBserver stamp
6192 bit. Alway reuse the current TBC index. */
6193 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6194 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6196 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6202 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6203 ipa_trace_buffer_ctrl_curr
))
6206 trace_debug ("Lib: Committed %08x -> %08x",
6207 ipa_trace_buffer_ctrl_curr_old
,
6208 ipa_trace_buffer_ctrl_curr
);
6210 /* Re-read these, now that we've installed the
6211 `about_to_request_buffer_space' breakpoint/lock. A thread could
6212 have finished a traceframe between the last read of these
6213 counters and setting the breakpoint above. If we start
6214 uploading, we never want to leave this function with
6215 traceframe_read_count != 0, otherwise, GDBserver could end up
6216 incrementing the counter tokens more than once (due to event loop
6217 nesting), which would break the IP agent's "effective" detection
6218 (see trace_alloc_trace_buffer). */
6219 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6220 &ipa_traceframe_read_count
))
6222 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6223 &ipa_traceframe_write_count
))
6228 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6229 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6230 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6232 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6233 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6234 trace_debug ("note that ipa_traceframe_count's parts changed");
6237 /* Get the address of the current TBC object (the IP agent has an
6238 array of 3 such objects). The index is stored in the TBC
6240 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6241 ipa_trace_buffer_ctrl_addr
6242 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6244 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6245 (unsigned char *) &ipa_trace_buffer_ctrl
,
6246 sizeof (struct ipa_trace_buffer_control
)))
6249 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6250 &ipa_trace_buffer_lo
))
6252 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6253 &ipa_trace_buffer_hi
))
6256 /* Offsets are easier to grok for debugging than raw addresses,
6257 especially for the small trace buffer sizes that are useful for
6259 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6260 "endfree=%d wrap=%d hi=%d",
6262 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6263 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6264 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6265 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6266 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6268 /* Note that the IPA's buffer is always circular. */
6270 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6272 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6273 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6275 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6276 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6277 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6278 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6281 tf
= IPA_FIRST_TRACEFRAME ();
6283 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6285 struct tracepoint
*tpoint
;
6286 struct traceframe
*tframe
;
6287 unsigned char *block
;
6288 struct traceframe ipa_tframe
;
6290 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6291 offsetof (struct traceframe
, data
)))
6292 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6294 if (ipa_tframe
.tpnum
== 0)
6295 fatal ("Uploading: No (more) fast traceframes, but "
6296 "ipa_traceframe_count == %u??\n",
6297 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6299 /* Note that this will be incorrect for multi-location
6301 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6303 tframe
= add_traceframe (tpoint
);
6306 trace_buffer_is_full
= 1;
6307 trace_debug ("Uploading: trace buffer is full");
6311 /* Copy the whole set of blocks in one go for now. FIXME:
6312 split this in smaller blocks. */
6313 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6316 if (read_inferior_memory (tf
6317 + offsetof (struct traceframe
, data
),
6318 block
, ipa_tframe
.data_size
))
6319 error ("Uploading: Couldn't read traceframe data at %s\n",
6320 paddress (tf
+ offsetof (struct traceframe
, data
)));
6323 trace_debug ("Uploading: traceframe didn't fit");
6324 finish_traceframe (tframe
);
6327 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6329 /* If we freed the traceframe that wrapped around, go back
6330 to the non-wrap case. */
6331 if (tf
< ipa_trace_buffer_ctrl
.start
)
6333 trace_debug ("Lib: Discarding past the wraparound");
6334 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6336 ipa_trace_buffer_ctrl
.start
= tf
;
6337 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6338 ++ipa_traceframe_read_count
;
6340 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6341 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6343 trace_debug ("Lib: buffer is fully empty. "
6344 "Trace buffer [%d] start=%d free=%d endfree=%d",
6346 (int) (ipa_trace_buffer_ctrl
.start
6347 - ipa_trace_buffer_lo
),
6348 (int) (ipa_trace_buffer_ctrl
.free
6349 - ipa_trace_buffer_lo
),
6350 (int) (ipa_trace_buffer_ctrl
.end_free
6351 - ipa_trace_buffer_lo
));
6353 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6354 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6355 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6356 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6359 trace_debug ("Uploaded a traceframe\n"
6360 "Lib: Trace buffer [%d] start=%d free=%d "
6361 "endfree=%d wrap=%d hi=%d",
6363 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6364 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6365 (int) (ipa_trace_buffer_ctrl
.end_free
6366 - ipa_trace_buffer_lo
),
6367 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6368 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6371 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6372 (unsigned char *) &ipa_trace_buffer_ctrl
,
6373 sizeof (struct ipa_trace_buffer_control
)))
6376 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6377 ipa_traceframe_read_count
);
6379 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6382 cancel_breakpoints ();
6384 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6385 about_to_request_buffer_space_bkpt
= NULL
;
6389 if (trace_buffer_is_full
)
6394 #ifdef IN_PROCESS_AGENT
6396 IP_AGENT_EXPORT
int ust_loaded
;
6397 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6401 /* Static tracepoints. */
6403 /* UST puts a "struct tracepoint" in the global namespace, which
6404 conflicts with our tracepoint. Arguably, being a library, it
6405 shouldn't take ownership of such a generic name. We work around it
6407 #define tracepoint ust_tracepoint
6408 #include <ust/ust.h>
6411 extern int serialize_to_text (char *outbuf
, int bufsize
,
6412 const char *fmt
, va_list ap
);
6414 #define GDB_PROBE_NAME "gdb"
6416 /* We dynamically search for the UST symbols instead of linking them
6417 in. This lets the user decide if the application uses static
6418 tracepoints, instead of always pulling libust.so in. This vector
6419 holds pointers to all functions we care about. */
6423 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6424 const char *fmt
, va_list ap
);
6426 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6427 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6429 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6431 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6434 void (*marker_iter_start
) (struct marker_iter
*iter
);
6435 void (*marker_iter_next
) (struct marker_iter
*iter
);
6436 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6437 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6442 /* Cast through typeof to catch incompatible API changes. Since UST
6443 only builds with gcc, we can freely use gcc extensions here
6445 #define GET_UST_SYM(SYM) \
6448 if (ust_ops.SYM == NULL) \
6449 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6450 if (ust_ops.SYM == NULL) \
6454 #define USTF(SYM) ust_ops.SYM
6456 /* Get pointers to all libust.so functions we care about. */
6461 GET_UST_SYM (serialize_to_text
);
6463 GET_UST_SYM (ltt_probe_register
);
6464 GET_UST_SYM (ltt_probe_unregister
);
6465 GET_UST_SYM (ltt_marker_connect
);
6466 GET_UST_SYM (ltt_marker_disconnect
);
6468 GET_UST_SYM (marker_iter_start
);
6469 GET_UST_SYM (marker_iter_next
);
6470 GET_UST_SYM (marker_iter_stop
);
6471 GET_UST_SYM (marker_iter_reset
);
6477 /* Given an UST marker, return the matching gdb static tracepoint.
6478 The match is done by address. */
6480 static struct tracepoint
*
6481 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6483 struct tracepoint
*tpoint
;
6485 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6487 if (tpoint
->type
!= static_tracepoint
)
6490 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6497 /* The probe function we install on lttng/ust markers. Whenever a
6498 probed ust marker is hit, this function is called. This is similar
6499 to gdb_collect, only for static tracepoints, instead of fast
6503 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6504 struct registers
*regs
, void *call_private
,
6505 const char *fmt
, va_list *args
)
6507 struct tracepoint
*tpoint
;
6508 struct static_tracepoint_ctx ctx
;
6510 /* Don't do anything until the trace run is completely set up. */
6513 trace_debug ("gdb_probe: not tracing\n");
6517 ctx
.base
.type
= static_tracepoint
;
6518 ctx
.regcache_initted
= 0;
6523 /* Wrap the regblock in a register cache (in the stack, we don't
6524 want to malloc here). */
6525 ctx
.regspace
= alloca (register_cache_size ());
6526 if (ctx
.regspace
== NULL
)
6528 trace_debug ("Trace buffer block allocation failed, skipping");
6532 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6535 trace_debug ("gdb_probe: marker not known: "
6536 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6537 mdata
->location
, mdata
->channel
,
6538 mdata
->name
, mdata
->format
);
6542 if (!tpoint
->enabled
)
6544 trace_debug ("gdb_probe: tracepoint disabled");
6548 ctx
.tpoint
= tpoint
;
6550 trace_debug ("gdb_probe: collecting marker: "
6551 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6552 mdata
->location
, mdata
->channel
,
6553 mdata
->name
, mdata
->format
);
6555 /* Test the condition if present, and collect if true. */
6556 if (tpoint
->cond
== NULL
6557 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6560 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6561 tpoint
->address
, tpoint
);
6563 if (stopping_tracepoint
6564 || trace_buffer_is_full
6565 || expr_eval_result
!= expr_eval_no_error
)
6570 /* If there was a condition and it evaluated to false, the only
6571 way we would stop tracing is if there was an error during
6572 condition expression evaluation. */
6573 if (expr_eval_result
!= expr_eval_no_error
)
6578 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6579 static tracepoint string data. This is a string passed to the
6580 tracing library by the user, at the time of the tracepoint marker
6581 call. E.g., in the UST marker call:
6583 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6585 the collected data is "str FOOBAZ".
6589 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6590 struct traceframe
*tframe
)
6592 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6593 unsigned char *bufspace
;
6596 unsigned short blocklen
;
6600 trace_debug ("Wanted to collect static trace data, "
6601 "but there's no static trace data");
6605 va_copy (copy
, *umd
->args
);
6606 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6609 trace_debug ("Want to collect ust data");
6611 /* 'S' + size + string */
6612 bufspace
= add_traceframe_block (tframe
,
6613 1 + sizeof (blocklen
) + size
+ 1);
6614 if (bufspace
== NULL
)
6616 trace_debug ("Trace buffer block allocation failed, skipping");
6620 /* Identify a static trace data block. */
6623 blocklen
= size
+ 1;
6624 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6626 va_copy (copy
, *umd
->args
);
6627 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6628 size
+ 1, umd
->fmt
, copy
);
6631 trace_debug ("Storing static tracepoint data in regblock: %s",
6632 bufspace
+ 1 + sizeof (blocklen
));
6635 /* The probe to register with lttng/ust. */
6636 static struct ltt_available_probe gdb_ust_probe
=
6643 #endif /* HAVE_UST */
6644 #endif /* IN_PROCESS_AGENT */
6646 #ifndef IN_PROCESS_AGENT
6648 /* Ask the in-process agent to run a command. Since we don't want to
6649 have to handle the IPA hitting breakpoints while running the
6650 command, we pause all threads, remove all breakpoints, and then set
6651 the helper thread re-running. We communicate with the helper
6652 thread by means of direct memory xfering, and a socket for
6656 run_inferior_command (char *cmd
, int len
)
6659 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6661 trace_debug ("run_inferior_command: running: %s", cmd
);
6664 uninsert_all_breakpoints ();
6666 err
= agent_run_command (pid
, (const char *) cmd
, len
);
6668 reinsert_all_breakpoints ();
6674 #else /* !IN_PROCESS_AGENT */
6676 #include <sys/socket.h>
6679 #ifndef UNIX_PATH_MAX
6680 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6683 /* Where we put the socked used for synchronization. */
6684 #define SOCK_DIR P_tmpdir
6686 /* Thread ID of the helper thread. GDBserver reads this to know which
6687 is the help thread. This is an LWP id on Linux. */
6688 int helper_thread_id
;
6691 init_named_socket (const char *name
)
6694 struct sockaddr_un addr
;
6696 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6699 warning ("socket creation failed: %s", strerror (errno
));
6703 addr
.sun_family
= AF_UNIX
;
6705 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6706 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6708 result
= access (name
, F_OK
);
6712 result
= unlink (name
);
6715 warning ("unlink failed: %s", strerror (errno
));
6719 warning ("socket %s already exists; overwriting", name
);
6722 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6725 warning ("bind failed: %s", strerror (errno
));
6730 result
= listen (fd
, 1);
6733 warning ("listen: %s", strerror (errno
));
6742 gdb_agent_socket_init (void)
6745 char name
[UNIX_PATH_MAX
];
6747 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6748 SOCK_DIR
, getpid ());
6749 if (result
>= UNIX_PATH_MAX
)
6751 trace_debug ("string overflow allocating socket name");
6755 fd
= init_named_socket (name
);
6757 warning ("Error initializing named socket (%s) for communication with the "
6758 "ust helper thread. Check that directory exists and that it "
6759 "is writable.", name
);
6766 /* The next marker to be returned on a qTsSTM command. */
6767 static const struct marker
*next_st
;
6769 /* Returns the first known marker. */
6774 struct marker_iter iter
;
6776 USTF(marker_iter_reset
) (&iter
);
6777 USTF(marker_iter_start
) (&iter
);
6782 /* Returns the marker following M. */
6784 const struct marker
*
6785 next_marker (const struct marker
*m
)
6787 struct marker_iter iter
;
6789 USTF(marker_iter_reset
) (&iter
);
6790 USTF(marker_iter_start
) (&iter
);
6792 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6794 if (iter
.marker
== m
)
6796 USTF(marker_iter_next
) (&iter
);
6804 /* Return an hexstr version of the STR C string, fit for sending to
6808 cstr_to_hexstr (const char *str
)
6810 int len
= strlen (str
);
6811 char *hexstr
= xmalloc (len
* 2 + 1);
6812 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6816 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6820 response_ust_marker (char *packet
, const struct marker
*st
)
6822 char *strid
, *format
, *tmp
;
6824 next_st
= next_marker (st
);
6826 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6827 strlen (st
->name
) + 1);
6828 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6830 strid
= cstr_to_hexstr (tmp
);
6833 format
= cstr_to_hexstr (st
->format
);
6835 sprintf (packet
, "m%s:%s:%s",
6836 paddress ((uintptr_t) st
->location
),
6844 /* Return the first static tracepoint, and initialize the state
6845 machine that will iterate through all the static tracepoints. */
6848 cmd_qtfstm (char *packet
)
6850 trace_debug ("Returning first trace state variable definition");
6852 if (first_marker ())
6853 response_ust_marker (packet
, first_marker ());
6855 strcpy (packet
, "l");
6858 /* Return additional trace state variable definitions. */
6861 cmd_qtsstm (char *packet
)
6863 trace_debug ("Returning static tracepoint");
6866 response_ust_marker (packet
, next_st
);
6868 strcpy (packet
, "l");
6871 /* Disconnect the GDB probe from a marker at a given address. */
6874 unprobe_marker_at (char *packet
)
6878 struct marker_iter iter
;
6880 p
+= sizeof ("unprobe_marker_at:") - 1;
6882 p
= unpack_varlen_hex (p
, &address
);
6884 USTF(marker_iter_reset
) (&iter
);
6885 USTF(marker_iter_start
) (&iter
);
6886 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6887 if ((uintptr_t ) iter
.marker
->location
== address
)
6891 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6895 warning ("could not disable marker %s/%s",
6896 iter
.marker
->channel
, iter
.marker
->name
);
6901 /* Connect the GDB probe to a marker at a given address. */
6904 probe_marker_at (char *packet
)
6908 struct marker_iter iter
;
6911 p
+= sizeof ("probe_marker_at:") - 1;
6913 p
= unpack_varlen_hex (p
, &address
);
6915 USTF(marker_iter_reset
) (&iter
);
6917 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6919 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6920 if ((uintptr_t ) m
->location
== address
)
6924 trace_debug ("found marker for address. "
6925 "ltt_marker_connect (marker = %s/%s)",
6926 m
->channel
, m
->name
);
6928 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6930 if (result
&& result
!= -EEXIST
)
6931 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6932 m
->channel
, m
->name
, -result
);
6936 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6937 m
->channel
, m
->name
);
6941 strcpy (packet
, "OK");
6945 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6950 cmd_qtstmat (char *packet
)
6954 struct marker_iter iter
;
6957 p
+= sizeof ("qTSTMat:") - 1;
6959 p
= unpack_varlen_hex (p
, &address
);
6961 USTF(marker_iter_reset
) (&iter
);
6963 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6965 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6966 if ((uintptr_t ) m
->location
== address
)
6968 response_ust_marker (packet
, m
);
6972 strcpy (packet
, "l");
6982 USTF(ltt_probe_register
) (&gdb_ust_probe
);
6985 #endif /* HAVE_UST */
6987 #include <sys/syscall.h>
6989 /* Helper thread of agent. */
6992 gdb_agent_helper_thread (void *arg
)
6998 listen_fd
= gdb_agent_socket_init ();
7000 if (helper_thread_id
== 0)
7001 helper_thread_id
= syscall (SYS_gettid
);
7003 if (listen_fd
== -1)
7005 warning ("could not create sync socket\n");
7012 struct sockaddr_un sockaddr
;
7017 tmp
= sizeof (sockaddr
);
7021 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7023 /* It seems an ERESTARTSYS can escape out of accept. */
7024 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7028 warning ("Accept returned %d, error: %s\n",
7029 fd
, strerror (errno
));
7035 ret
= read (fd
, buf
, 1);
7036 } while (ret
== -1 && errno
== EINTR
);
7040 warning ("reading socket (fd=%d) failed with %s",
7041 fd
, strerror (errno
));
7049 if (strcmp ("qTfSTM", cmd_buf
) == 0)
7051 cmd_qtfstm (cmd_buf
);
7053 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7055 cmd_qtsstm (cmd_buf
);
7057 else if (strncmp ("unprobe_marker_at:",
7059 sizeof ("unprobe_marker_at:") - 1) == 0)
7061 unprobe_marker_at (cmd_buf
);
7063 else if (strncmp ("probe_marker_at:",
7065 sizeof ("probe_marker_at:") - 1) == 0)
7067 probe_marker_at (cmd_buf
);
7069 else if (strncmp ("qTSTMat:",
7071 sizeof ("qTSTMat:") - 1) == 0)
7073 cmd_qtstmat (cmd_buf
);
7075 #endif /* HAVE_UST */
7078 /* Fix compiler's warning: ignoring return value of 'write'. */
7079 ret
= write (fd
, buf
, 1);
7088 #include <pthread.h>
7090 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
7093 gdb_agent_init (void)
7100 /* We want the helper thread to be as transparent as possible, so
7101 have it inherit an all-signals-blocked mask. */
7103 sigfillset (&new_mask
);
7104 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7106 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7108 res
= pthread_create (&thread
,
7110 gdb_agent_helper_thread
,
7113 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7115 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7117 while (helper_thread_id
== 0)
7125 #include <sys/mman.h>
7128 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7129 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7130 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7131 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
7132 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
7133 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
7135 /* Record the result of getting buffer space for fast tracepoint
7136 trampolines. Any error message is copied, since caller may not be
7137 using persistent storage. */
7140 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
7142 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
7143 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
7145 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
7147 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
7150 static void __attribute__ ((constructor
))
7151 initialize_tracepoint_ftlib (void)
7153 initialize_tracepoint ();
7158 #endif /* IN_PROCESS_AGENT */
7160 /* Return a timestamp, expressed as microseconds of the usual Unix
7161 time. (As the result is a 64-bit number, it will not overflow any
7165 get_timestamp (void)
7169 if (gettimeofday (&tv
, 0) != 0)
7172 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7176 initialize_tracepoint (void)
7178 /* There currently no way to change the buffer size. */
7179 const int sizeOfBuffer
= 5 * 1024 * 1024;
7180 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7181 init_trace_buffer (buf
, sizeOfBuffer
);
7183 /* Wire trace state variable 1 to be the timestamp. This will be
7184 uploaded to GDB upon connection and become one of its trace state
7185 variables. (In case you're wondering, if GDB already has a trace
7186 variable numbered 1, it will be renumbered.) */
7187 create_trace_state_variable (1, 0);
7188 set_trace_state_variable_name (1, "trace_timestamp");
7189 set_trace_state_variable_getter (1, get_timestamp
);
7191 #ifdef IN_PROCESS_AGENT
7196 pagesize
= sysconf (_SC_PAGE_SIZE
);
7200 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7202 #define SCRATCH_BUFFER_NPAGES 20
7204 /* Allocate scratch buffer aligned on a page boundary, at a low
7205 address (close to the main executable's code). */
7206 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7208 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7209 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7210 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7212 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7218 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7221 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7224 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7226 /* It's not a fatal error for something to go wrong with trampoline
7227 buffer setup, but it can be mysterious, so create a channel to
7228 report back on what went wrong, using a fixed size since we may
7229 not be able to allocate space later when the problem occurs. */
7230 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7232 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7234 initialize_low_tracepoint ();