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
);
417 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
419 return read_inferior_memory (symaddr
, (unsigned char *) val
,
424 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
426 return read_inferior_memory (symaddr
, (unsigned char *) val
,
431 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
433 void *pval
= (void *) (uintptr_t) val
;
436 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
437 *val
= (uintptr_t) pval
;
442 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
444 void *pval
= (void *) (uintptr_t) val
;
445 return write_inferior_memory (symaddr
,
446 (unsigned char *) &pval
, sizeof (pval
));
450 write_inferior_integer (CORE_ADDR symaddr
, int val
)
452 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
456 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
458 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
461 static CORE_ADDR
target_malloc (ULONGEST size
);
462 static int write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
);
465 /* Operations on various types of tracepoint actions. */
467 struct tracepoint_action
;
469 struct tracepoint_action_ops
471 /* Download tracepoint action ACTION to IPA. Return the address of action
473 CORE_ADDR (*download
) (const struct tracepoint_action
*action
);
476 /* Base action. Concrete actions inherit this. */
478 struct tracepoint_action
480 #ifndef IN_PROCESS_AGENT
481 const struct tracepoint_action_ops
*ops
;
486 /* An 'M' (collect memory) action. */
487 struct collect_memory_action
489 struct tracepoint_action base
;
496 /* An 'R' (collect registers) action. */
498 struct collect_registers_action
500 struct tracepoint_action base
;
503 /* An 'X' (evaluate expression) action. */
505 struct eval_expr_action
507 struct tracepoint_action base
;
509 struct agent_expr
*expr
;
512 /* An 'L' (collect static trace data) action. */
513 struct collect_static_trace_data_action
515 struct tracepoint_action base
;
518 #ifndef IN_PROCESS_AGENT
520 m_tracepoint_action_download (const struct tracepoint_action
*action
)
522 int size_in_ipa
= (sizeof (struct collect_memory_action
)
523 - offsetof (struct tracepoint_action
, type
));
524 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
526 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
532 static const struct tracepoint_action_ops m_tracepoint_action_ops
=
534 m_tracepoint_action_download
,
538 r_tracepoint_action_download (const struct tracepoint_action
*action
)
540 int size_in_ipa
= (sizeof (struct collect_registers_action
)
541 - offsetof (struct tracepoint_action
, type
));
542 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
544 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
550 static const struct tracepoint_action_ops r_tracepoint_action_ops
=
552 r_tracepoint_action_download
,
555 static CORE_ADDR
download_agent_expr (struct agent_expr
*expr
);
558 x_tracepoint_action_download (const struct tracepoint_action
*action
)
560 int size_in_ipa
= (sizeof (struct eval_expr_action
)
561 - offsetof (struct tracepoint_action
, type
));
562 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
565 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
567 expr
= download_agent_expr (((struct eval_expr_action
*)action
)->expr
);
568 write_inferior_data_ptr (ipa_action
+ offsetof (struct eval_expr_action
, expr
)
569 - offsetof (struct tracepoint_action
, type
),
575 static const struct tracepoint_action_ops x_tracepoint_action_ops
=
577 x_tracepoint_action_download
,
581 l_tracepoint_action_download (const struct tracepoint_action
*action
)
583 int size_in_ipa
= (sizeof (struct collect_static_trace_data_action
)
584 - offsetof (struct tracepoint_action
, type
));
585 CORE_ADDR ipa_action
= target_malloc (size_in_ipa
);
587 write_inferior_memory (ipa_action
, (unsigned char *) &action
->type
,
593 static const struct tracepoint_action_ops l_tracepoint_action_ops
=
595 l_tracepoint_action_download
,
599 /* This structure describes a piece of the source-level definition of
600 the tracepoint. The contents are not interpreted by the target,
601 but preserved verbatim for uploading upon reconnection. */
605 /* The type of string, such as "cond" for a conditional. */
608 /* The source-level string itself. For the sake of target
609 debugging, we store it in plaintext, even though it is always
610 transmitted in hex. */
613 /* Link to the next one in the list. We link them in the order
614 received, in case some make up an ordered list of commands or
616 struct source_string
*next
;
621 /* Trap based tracepoint. */
624 /* A fast tracepoint implemented with a jump instead of a trap. */
627 /* A static tracepoint, implemented by a program call into a tracing
632 struct tracepoint_hit_ctx
;
634 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
637 /* The definition of a tracepoint. */
639 /* Tracepoints may have multiple locations, each at a different
640 address. This can occur with optimizations, template
641 instantiation, etc. Since the locations may be in different
642 scopes, the conditions and actions may be different for each
643 location. Our target version of tracepoints is more like GDB's
644 notion of "breakpoint locations", but we have almost nothing that
645 is not per-location, so we bother having two kinds of objects. The
646 key consequence is that numbers are not unique, and that it takes
647 both number and address to identify a tracepoint uniquely. */
651 /* The number of the tracepoint, as specified by GDB. Several
652 tracepoint objects here may share a number. */
655 /* Address at which the tracepoint is supposed to trigger. Several
656 tracepoints may share an address. */
659 /* Tracepoint type. */
660 enum tracepoint_type type
;
662 /* True if the tracepoint is currently enabled. */
665 /* The number of single steps that will be performed after each
669 /* The number of times the tracepoint may be hit before it will
670 terminate the entire tracing run. */
673 /* Pointer to the agent expression that is the tracepoint's
674 conditional, or NULL if the tracepoint is unconditional. */
675 struct agent_expr
*cond
;
677 /* The list of actions to take when the tracepoint triggers. */
679 struct tracepoint_action
**actions
;
681 /* Count of the times we've hit this tracepoint during the run.
682 Note that while-stepping steps are not counted as "hits". */
685 /* Cached sum of the sizes of traceframes created by this point. */
686 uint64_t traceframe_usage
;
688 CORE_ADDR compiled_cond
;
690 /* Link to the next tracepoint in the list. */
691 struct tracepoint
*next
;
693 #ifndef IN_PROCESS_AGENT
694 /* The list of actions to take when the tracepoint triggers, in
695 string/packet form. */
698 /* The collection of strings that describe the tracepoint as it was
699 entered into GDB. These are not used by the target, but are
700 reported back to GDB upon reconnection. */
701 struct source_string
*source_strings
;
703 /* The number of bytes displaced by fast tracepoints. It may subsume
704 multiple instructions, for multi-byte fast tracepoints. This
705 field is only valid for fast tracepoints. */
708 /* Only for fast tracepoints. */
709 CORE_ADDR obj_addr_on_target
;
711 /* Address range where the original instruction under a fast
712 tracepoint was relocated to. (_end is actually one byte past
714 CORE_ADDR adjusted_insn_addr
;
715 CORE_ADDR adjusted_insn_addr_end
;
717 /* The address range of the piece of the jump pad buffer that was
718 assigned to this fast tracepoint. (_end is actually one byte
721 CORE_ADDR jump_pad_end
;
723 /* The address range of the piece of the trampoline buffer that was
724 assigned to this fast tracepoint. (_end is actually one byte
726 CORE_ADDR trampoline
;
727 CORE_ADDR trampoline_end
;
729 /* The list of actions to take while in a stepping loop. These
730 fields are only valid for patch-based tracepoints. */
731 int num_step_actions
;
732 struct tracepoint_action
**step_actions
;
733 /* Same, but in string/packet form. */
734 char **step_actions_str
;
736 /* Handle returned by the breakpoint or tracepoint module when we
737 inserted the trap or jump, or hooked into a static tracepoint.
738 NULL if we haven't inserted it yet. */
744 #ifndef IN_PROCESS_AGENT
746 /* Given `while-stepping', a thread may be collecting data for more
747 than one tracepoint simultaneously. On the other hand, the same
748 tracepoint with a while-stepping action may be hit by more than one
749 thread simultaneously (but not quite, each thread could be handling
750 a different step). Each thread holds a list of these objects,
751 representing the current step of each while-stepping action being
756 struct wstep_state
*next
;
758 /* The tracepoint number. */
760 /* The tracepoint's address. */
761 CORE_ADDR tp_address
;
763 /* The number of the current step in this 'while-stepping'
770 /* The linked list of all tracepoints. Marked explicitly as used as
771 the in-process library doesn't use it for the fast tracepoints
773 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
775 #ifndef IN_PROCESS_AGENT
777 /* Pointer to the last tracepoint in the list, new tracepoints are
778 linked in at the end. */
780 static struct tracepoint
*last_tracepoint
;
783 /* The first tracepoint to exceed its pass count. */
785 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
787 /* True if the trace buffer is full or otherwise no longer usable. */
789 IP_AGENT_EXPORT
int trace_buffer_is_full
;
791 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
793 #ifndef IN_PROCESS_AGENT
795 static const char *eval_result_names
[] =
797 "terror:in the attic", /* this should never be reported */
798 "terror:empty expression",
799 "terror:empty stack",
800 "terror:stack overflow",
801 "terror:stack underflow",
802 "terror:unhandled opcode",
803 "terror:unrecognized opcode",
804 "terror:divide by zero"
809 /* The tracepoint in which the error occurred. */
811 static struct tracepoint
*error_tracepoint
;
813 struct trace_state_variable
815 /* This is the name of the variable as used in GDB. The target
816 doesn't use the name, but needs to have it for saving and
817 reconnection purposes. */
820 /* This number identifies the variable uniquely. Numbers may be
821 assigned either by the target (in the case of builtin variables),
822 or by GDB, and are presumed unique during the course of a trace
826 /* The variable's initial value, a 64-bit signed integer always. */
827 LONGEST initial_value
;
829 /* The variable's value, a 64-bit signed integer always. */
832 /* Pointer to a getter function, used to supply computed values. */
833 LONGEST (*getter
) (void);
835 /* Link to the next variable. */
836 struct trace_state_variable
*next
;
839 /* Linked list of all trace state variables. */
841 #ifdef IN_PROCESS_AGENT
842 struct trace_state_variable
*alloced_trace_state_variables
;
845 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
847 /* The results of tracing go into a fixed-size space known as the
848 "trace buffer". Because usage follows a limited number of
849 patterns, we manage it ourselves rather than with malloc. Basic
850 rules are that we create only one trace frame at a time, each is
851 variable in size, they are never moved once created, and we only
852 discard if we are doing a circular buffer, and then only the oldest
853 ones. Each trace frame includes its own size, so we don't need to
854 link them together, and the trace frame number is relative to the
855 first one, so we don't need to record numbers. A trace frame also
856 records the number of the tracepoint that created it. The data
857 itself is a series of blocks, each introduced by a single character
858 and with a defined format. Each type of block has enough
859 type/length info to allow scanners to jump quickly from one block
860 to the next without reading each byte in the block. */
862 /* Trace buffer management would be simple - advance a free pointer
863 from beginning to end, then stop - were it not for the circular
864 buffer option, which is a useful way to prevent a trace run from
865 stopping prematurely because the buffer filled up. In the circular
866 case, the location of the first trace frame (trace_buffer_start)
867 moves as old trace frames are discarded. Also, since we grow trace
868 frames incrementally as actions are performed, we wrap around to
869 the beginning of the trace buffer. This is per-block, so each
870 block within a trace frame remains contiguous. Things get messy
871 when the wrapped-around trace frame is the one being discarded; the
872 free space ends up in two parts at opposite ends of the buffer. */
875 # if defined(__GNUC__)
876 # define ATTR_PACKED __attribute__ ((packed))
878 # define ATTR_PACKED /* nothing */
882 /* The data collected at a tracepoint hit. This object should be as
883 small as possible, since there may be a great many of them. We do
884 not need to keep a frame number, because they are all sequential
885 and there are no deletions; so the Nth frame in the buffer is
886 always frame number N. */
890 /* Number of the tracepoint that collected this traceframe. A value
891 of 0 indicates the current end of the trace buffer. We make this
892 a 16-bit field because it's never going to happen that GDB's
893 numbering of tracepoints reaches 32,000. */
896 /* The size of the data in this trace frame. We limit this to 32
897 bits, even on a 64-bit target, because it's just implausible that
898 one is validly going to collect 4 gigabytes of data at a single
900 unsigned int data_size
: 32;
902 /* The base of the trace data, which is contiguous from this point. */
903 unsigned char data
[0];
907 /* The traceframe to be used as the source of data to send back to
908 GDB. A value of -1 means to get data from the live program. */
910 int current_traceframe
= -1;
912 /* This flag is true if the trace buffer is circular, meaning that
913 when it fills, the oldest trace frames are discarded in order to
916 #ifndef IN_PROCESS_AGENT
917 static int circular_trace_buffer
;
920 /* Pointer to the block of memory that traceframes all go into. */
922 static unsigned char *trace_buffer_lo
;
924 /* Pointer to the end of the trace buffer, more precisely to the byte
925 after the end of the buffer. */
927 static unsigned char *trace_buffer_hi
;
929 /* Control structure holding the read/write/etc. pointers into the
930 trace buffer. We need more than one of these to implement a
931 transaction-like mechanism to garantees that both GDBserver and the
932 in-process agent can try to change the trace buffer
935 struct trace_buffer_control
937 /* Pointer to the first trace frame in the buffer. In the
938 non-circular case, this is equal to trace_buffer_lo, otherwise it
939 moves around in the buffer. */
940 unsigned char *start
;
942 /* Pointer to the free part of the trace buffer. Note that we clear
943 several bytes at and after this pointer, so that traceframe
944 scans/searches terminate properly. */
947 /* Pointer to the byte after the end of the free part. Note that
948 this may be smaller than trace_buffer_free in the circular case,
949 and means that the free part is in two pieces. Initially it is
950 equal to trace_buffer_hi, then is generally equivalent to
951 trace_buffer_start. */
952 unsigned char *end_free
;
954 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
955 this is the point at which the trace data breaks, and resumes at
960 /* Same as above, to be used by GDBserver when updating the in-process
962 struct ipa_trace_buffer_control
971 /* We have possibly both GDBserver and an inferior thread accessing
972 the same IPA trace buffer memory. The IPA is the producer (tries
973 to put new frames in the buffer), while GDBserver occasionally
974 consumes them, that is, flushes the IPA's buffer into its own
975 buffer. Both sides need to update the trace buffer control
976 pointers (current head, tail, etc.). We can't use a global lock to
977 synchronize the accesses, as otherwise we could deadlock GDBserver
978 (if the thread holding the lock stops for a signal, say). So
979 instead of that, we use a transaction scheme where GDBserver writes
980 always prevail over the IPAs writes, and, we have the IPA detect
981 the commit failure/overwrite, and retry the whole attempt. This is
982 mainly implemented by having a global token object that represents
983 who wrote last to the buffer control structure. We need to freeze
984 any inferior writing to the buffer while GDBserver touches memory,
985 so that the inferior can correctly detect that GDBserver had been
986 there, otherwise, it could mistakingly think its commit was
987 successful; that's implemented by simply having GDBserver set a
988 breakpoint the inferior hits if it is the critical region.
990 There are three cycling trace buffer control structure copies
991 (buffer head, tail, etc.), with the token object including an index
992 indicating which is current live copy. The IPA tentatively builds
993 an updated copy in a non-current control structure, while GDBserver
994 always clobbers the current version directly. The IPA then tries
995 to atomically "commit" its version; if GDBserver clobbered the
996 structure meanwhile, that will fail, and the IPA restarts the
999 Listing the step in further detail, we have:
1001 In-process agent (producer):
1003 - passes by `about_to_request_buffer_space' breakpoint/lock
1005 - reads current token, extracts current trace buffer control index,
1006 and starts tentatively updating the rightmost one (0->1, 1->2,
1007 2->0). Note that only one inferior thread is executing this code
1008 at any given time, due to an outer lock in the jump pads.
1010 - updates counters, and tries to commit the token.
1012 - passes by second `about_to_request_buffer_space' breakpoint/lock,
1013 leaving the sync region.
1015 - checks if the update was effective.
1017 - if trace buffer was found full, hits flush_trace_buffer
1018 breakpoint, and restarts later afterwards.
1020 GDBserver (consumer):
1022 - sets `about_to_request_buffer_space' breakpoint/lock.
1024 - updates the token unconditionally, using the current buffer
1025 control index, since it knows that the IP agent always writes to
1026 the rightmost, and due to the breakpoint, at most one IP thread
1027 can try to update the trace buffer concurrently to GDBserver, so
1028 there will be no danger of trace buffer control index wrap making
1029 the IPA write to the same index as GDBserver.
1031 - flushes the IP agent's trace buffer completely, and updates the
1032 current trace buffer control structure. GDBserver *always* wins.
1034 - removes the `about_to_request_buffer_space' breakpoint.
1036 The token is stored in the `trace_buffer_ctrl_curr' variable.
1037 Internally, it's bits are defined as:
1039 |-------------+-----+-------------+--------+-------------+--------------|
1040 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1041 |-------------+-----+-------------+--------+-------------+--------------|
1042 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1043 |-------------+-----+-------------+--------+-------------+--------------|
1045 GSB - GDBserver Stamp Bit
1046 PC - Previous Counter
1047 CC - Current Counter
1048 TBCI - Trace Buffer Control Index
1051 An IPA update of `trace_buffer_ctrl_curr' does:
1053 - read CC from the current token, save as PC.
1055 - atomically tries to write PC+1,CC
1057 A GDBserver update of `trace_buffer_ctrl_curr' does:
1059 - reads PC and CC from the current token.
1064 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1065 for the counters described below. The cleared bits are used to
1066 hold the index of the items of the `trace_buffer_ctrl' array that
1068 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1070 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1071 counter, and the `current' counter. */
1073 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1074 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1076 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1077 always stamps this bit as set. */
1078 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1080 #ifdef IN_PROCESS_AGENT
1081 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1082 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1084 # define TRACE_BUFFER_CTRL_CURR \
1085 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1089 /* The GDBserver side agent only needs one instance of this object, as
1090 it doesn't need to sync with itself. Define it as array anyway so
1091 that the rest of the code base doesn't need to care for the
1093 struct trace_buffer_control trace_buffer_ctrl
[1];
1094 # define TRACE_BUFFER_CTRL_CURR 0
1097 /* These are convenience macros used to access the current trace
1098 buffer control in effect. */
1099 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1100 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1101 #define trace_buffer_end_free \
1102 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1103 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1106 /* Macro that returns a pointer to the first traceframe in the buffer. */
1108 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1110 /* Macro that returns a pointer to the next traceframe in the buffer.
1111 If the computed location is beyond the wraparound point, subtract
1112 the offset of the wraparound. */
1114 #define NEXT_TRACEFRAME_1(TF) \
1115 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1117 #define NEXT_TRACEFRAME(TF) \
1118 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1119 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1120 ? (trace_buffer_wrap - trace_buffer_lo) \
1123 /* The difference between these counters represents the total number
1124 of complete traceframes present in the trace buffer. The IP agent
1125 writes to the write count, GDBserver writes to read count. */
1127 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1128 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1130 /* Convenience macro. */
1132 #define traceframe_count \
1133 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1135 /* The count of all traceframes created in the current run, including
1136 ones that were discarded to make room. */
1138 IP_AGENT_EXPORT
int traceframes_created
;
1140 #ifndef IN_PROCESS_AGENT
1142 /* Read-only regions are address ranges whose contents don't change,
1143 and so can be read from target memory even while looking at a trace
1144 frame. Without these, disassembly for instance will likely fail,
1145 because the program code is not usually collected into a trace
1146 frame. This data structure does not need to be very complicated or
1147 particularly efficient, it's only going to be used occasionally,
1148 and only by some commands. */
1150 struct readonly_region
1152 /* The bounds of the region. */
1153 CORE_ADDR start
, end
;
1155 /* Link to the next one. */
1156 struct readonly_region
*next
;
1159 /* Linked list of readonly regions. This list stays in effect from
1160 one tstart to the next. */
1162 static struct readonly_region
*readonly_regions
;
1166 /* The global that controls tracing overall. */
1168 IP_AGENT_EXPORT
int tracing
;
1170 #ifndef IN_PROCESS_AGENT
1172 /* Controls whether tracing should continue after GDB disconnects. */
1174 int disconnected_tracing
;
1176 /* The reason for the last tracing run to have stopped. We initialize
1177 to a distinct string so that GDB can distinguish between "stopped
1178 after running" and "stopped because never run" cases. */
1180 static const char *tracing_stop_reason
= "tnotrun";
1182 static int tracing_stop_tpnum
;
1184 /* 64-bit timestamps for the trace run's start and finish, expressed
1185 in microseconds from the Unix epoch. */
1187 LONGEST tracing_start_time
;
1188 LONGEST tracing_stop_time
;
1190 /* The (optional) user-supplied name of the user that started the run.
1191 This is an arbitrary string, and may be NULL. */
1193 char *tracing_user_name
;
1195 /* Optional user-supplied text describing the run. This is
1196 an arbitrary string, and may be NULL. */
1198 char *tracing_notes
;
1200 /* Optional user-supplied text explaining a tstop command. This is an
1201 arbitrary string, and may be NULL. */
1203 char *tracing_stop_note
;
1207 /* Functions local to this file. */
1209 /* Base "class" for tracepoint type specific data to be passed down to
1210 collect_data_at_tracepoint. */
1211 struct tracepoint_hit_ctx
1213 enum tracepoint_type type
;
1216 #ifdef IN_PROCESS_AGENT
1218 /* Fast/jump tracepoint specific data to be passed down to
1219 collect_data_at_tracepoint. */
1220 struct fast_tracepoint_ctx
1222 struct tracepoint_hit_ctx base
;
1224 struct regcache regcache
;
1225 int regcache_initted
;
1226 unsigned char *regspace
;
1228 unsigned char *regs
;
1229 struct tracepoint
*tpoint
;
1232 /* Static tracepoint specific data to be passed down to
1233 collect_data_at_tracepoint. */
1234 struct static_tracepoint_ctx
1236 struct tracepoint_hit_ctx base
;
1238 /* The regcache corresponding to the registers state at the time of
1239 the tracepoint hit. Initialized lazily, from REGS. */
1240 struct regcache regcache
;
1241 int regcache_initted
;
1243 /* The buffer space REGCACHE above uses. We use a separate buffer
1244 instead of letting the regcache malloc for both signal safety and
1245 performance reasons; this is allocated on the stack instead. */
1246 unsigned char *regspace
;
1248 /* The register buffer as passed on by lttng/ust. */
1249 struct registers
*regs
;
1251 /* The "printf" formatter and the args the user passed to the marker
1252 call. We use this to be able to collect "static trace data"
1257 /* The GDB tracepoint matching the probed marker that was "hit". */
1258 struct tracepoint
*tpoint
;
1263 /* Static tracepoint specific data to be passed down to
1264 collect_data_at_tracepoint. */
1265 struct trap_tracepoint_ctx
1267 struct tracepoint_hit_ctx base
;
1269 struct regcache
*regcache
;
1274 static enum eval_result_type
1275 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1276 struct traceframe
*tframe
,
1277 struct agent_expr
*aexpr
,
1280 #ifndef IN_PROCESS_AGENT
1281 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1282 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1285 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1286 struct tracepoint
*tpoint
);
1288 #ifndef IN_PROCESS_AGENT
1289 static void clear_readonly_regions (void);
1290 static void clear_installed_tracepoints (void);
1293 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1295 struct tracepoint
*tpoint
);
1296 #ifndef IN_PROCESS_AGENT
1297 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1299 struct tracepoint
*tpoint
, int current_step
);
1300 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1301 CORE_ADDR
*jump_entry
);
1303 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1305 struct tracepoint
*tpoint
,
1306 struct traceframe
*tframe
,
1307 struct tracepoint_action
*taction
);
1309 #ifndef IN_PROCESS_AGENT
1310 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1312 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1313 static void download_tracepoint (struct tracepoint
*);
1314 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1315 static void clone_fast_tracepoint (struct tracepoint
*to
,
1316 const struct tracepoint
*from
);
1319 static LONGEST
get_timestamp (void);
1321 #if defined(__GNUC__)
1322 # define memory_barrier() asm volatile ("" : : : "memory")
1324 # define memory_barrier() do {} while (0)
1327 /* We only build the IPA if this builtin is supported, and there are
1328 no uses of this in GDBserver itself, so we're safe in defining this
1330 #define cmpxchg(mem, oldval, newval) \
1331 __sync_val_compare_and_swap (mem, oldval, newval)
1333 /* Record that an error occurred during expression evaluation. */
1336 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1337 enum eval_result_type rtype
)
1339 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1340 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1342 #ifdef IN_PROCESS_AGENT
1343 /* Only record the first error we get. */
1344 if (cmpxchg (&expr_eval_result
,
1346 rtype
) != expr_eval_no_error
)
1349 if (expr_eval_result
!= expr_eval_no_error
)
1353 error_tracepoint
= tpoint
;
1356 /* Trace buffer management. */
1359 clear_trace_buffer (void)
1361 trace_buffer_start
= trace_buffer_lo
;
1362 trace_buffer_free
= trace_buffer_lo
;
1363 trace_buffer_end_free
= trace_buffer_hi
;
1364 trace_buffer_wrap
= trace_buffer_hi
;
1365 /* A traceframe with zeroed fields marks the end of trace data. */
1366 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1367 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1368 traceframe_read_count
= traceframe_write_count
= 0;
1369 traceframes_created
= 0;
1372 #ifndef IN_PROCESS_AGENT
1375 clear_inferior_trace_buffer (void)
1377 CORE_ADDR ipa_trace_buffer_lo
;
1378 CORE_ADDR ipa_trace_buffer_hi
;
1379 struct traceframe ipa_traceframe
= { 0 };
1380 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1382 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1383 &ipa_trace_buffer_lo
);
1384 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1385 &ipa_trace_buffer_hi
);
1387 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1388 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1389 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1390 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1392 /* A traceframe with zeroed fields marks the end of trace data. */
1393 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1394 (unsigned char *) &ipa_trace_buffer_ctrl
,
1395 sizeof (ipa_trace_buffer_ctrl
));
1397 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1399 /* A traceframe with zeroed fields marks the end of trace data. */
1400 write_inferior_memory (ipa_trace_buffer_lo
,
1401 (unsigned char *) &ipa_traceframe
,
1402 sizeof (ipa_traceframe
));
1404 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1405 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1406 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1412 init_trace_buffer (unsigned char *buf
, int bufsize
)
1414 trace_buffer_lo
= buf
;
1415 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1417 clear_trace_buffer ();
1420 #ifdef IN_PROCESS_AGENT
1422 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1423 about_to_request_buffer_space (void)
1425 /* GDBserver places breakpoint here while it goes about to flush
1426 data at random times. */
1427 UNKNOWN_SIDE_EFFECTS();
1432 /* Carve out a piece of the trace buffer, returning NULL in case of
1436 trace_buffer_alloc (size_t amt
)
1438 unsigned char *rslt
;
1439 struct trace_buffer_control
*tbctrl
;
1441 #ifdef IN_PROCESS_AGENT
1442 unsigned int prev
, prev_filtered
;
1443 unsigned int commit_count
;
1444 unsigned int commit
;
1445 unsigned int readout
;
1447 struct traceframe
*oldest
;
1448 unsigned char *new_start
;
1451 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1452 (long) amt
, (long) sizeof (struct traceframe
));
1454 /* Account for the EOB marker. */
1455 amt
+= sizeof (struct traceframe
);
1457 #ifdef IN_PROCESS_AGENT
1461 /* Read the current token and extract the index to try to write to,
1462 storing it in CURR. */
1463 prev
= trace_buffer_ctrl_curr
;
1464 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1465 curr
= prev_filtered
+ 1;
1469 about_to_request_buffer_space ();
1471 /* Start out with a copy of the current state. GDBserver may be
1472 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1473 be able to commit anyway if that happens. */
1474 trace_buffer_ctrl
[curr
]
1475 = trace_buffer_ctrl
[prev_filtered
];
1476 trace_debug ("trying curr=%u", curr
);
1478 /* The GDBserver's agent doesn't need all that syncing, and always
1479 updates TCB 0 (there's only one, mind you). */
1482 tbctrl
= &trace_buffer_ctrl
[curr
];
1484 /* Offsets are easier to grok for debugging than raw addresses,
1485 especially for the small trace buffer sizes that are useful for
1487 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1489 (int) (tbctrl
->start
- trace_buffer_lo
),
1490 (int) (tbctrl
->free
- trace_buffer_lo
),
1491 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1492 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1493 (int) (trace_buffer_hi
- trace_buffer_lo
));
1495 /* The algorithm here is to keep trying to get a contiguous block of
1496 the requested size, possibly discarding older traceframes to free
1497 up space. Since free space might come in one or two pieces,
1498 depending on whether discarded traceframes wrapped around at the
1499 high end of the buffer, we test both pieces after each
1503 /* First, if we have two free parts, try the upper one first. */
1504 if (tbctrl
->end_free
< tbctrl
->free
)
1506 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1507 /* We have enough in the upper part. */
1511 /* Our high part of free space wasn't enough. Give up
1512 on it for now, set wraparound. We will recover the
1513 space later, if/when the wrapped-around traceframe is
1515 trace_debug ("Upper part too small, setting wraparound");
1516 tbctrl
->wrap
= tbctrl
->free
;
1517 tbctrl
->free
= trace_buffer_lo
;
1521 /* The normal case. */
1522 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1525 #ifdef IN_PROCESS_AGENT
1526 /* The IP Agent's buffer is always circular. It isn't used
1527 currently, but `circular_trace_buffer' could represent
1528 GDBserver's mode. If we didn't find space, ask GDBserver to
1531 flush_trace_buffer ();
1535 trace_debug ("gdbserver flushed buffer, retrying");
1539 /* GDBserver cancelled the tracing. Bail out as well. */
1542 /* If we're here, then neither part is big enough, and
1543 non-circular trace buffers are now full. */
1544 if (!circular_trace_buffer
)
1546 trace_debug ("Not enough space in the trace buffer");
1550 trace_debug ("Need more space in the trace buffer");
1552 /* If we have a circular buffer, we can try discarding the
1553 oldest traceframe and see if that helps. */
1554 oldest
= FIRST_TRACEFRAME ();
1555 if (oldest
->tpnum
== 0)
1557 /* Not good; we have no traceframes to free. Perhaps we're
1558 asking for a block that is larger than the buffer? In
1559 any case, give up. */
1560 trace_debug ("No traceframes to discard");
1564 /* We don't run this code in the in-process agent currently.
1565 E.g., we could leave the in-process agent in autonomous
1566 circular mode if we only have fast tracepoints. If we do
1567 that, then this bit becomes racy with GDBserver, which also
1568 writes to this counter. */
1569 --traceframe_write_count
;
1571 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1572 /* If we freed the traceframe that wrapped around, go back
1573 to the non-wrap case. */
1574 if (new_start
< tbctrl
->start
)
1576 trace_debug ("Discarding past the wraparound");
1577 tbctrl
->wrap
= trace_buffer_hi
;
1579 tbctrl
->start
= new_start
;
1580 tbctrl
->end_free
= tbctrl
->start
;
1582 trace_debug ("Discarded a traceframe\n"
1583 "Trace buffer [%d], start=%d free=%d "
1584 "endfree=%d wrap=%d hi=%d",
1586 (int) (tbctrl
->start
- trace_buffer_lo
),
1587 (int) (tbctrl
->free
- trace_buffer_lo
),
1588 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1589 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1590 (int) (trace_buffer_hi
- trace_buffer_lo
));
1592 /* Now go back around the loop. The discard might have resulted
1593 in either one or two pieces of free space, so we want to try
1594 both before freeing any more traceframes. */
1598 /* If we get here, we know we can provide the asked-for space. */
1600 rslt
= tbctrl
->free
;
1602 /* Adjust the request back down, now that we know we have space for
1603 the marker, but don't commit to AMT yet, we may still need to
1604 restart the operation if GDBserver touches the trace buffer
1605 (obviously only important in the in-process agent's version). */
1606 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1608 /* Or not. If GDBserver changed the trace buffer behind our back,
1609 we get to restart a new allocation attempt. */
1611 #ifdef IN_PROCESS_AGENT
1612 /* Build the tentative token. */
1613 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1614 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1615 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1619 /* Try to commit it. */
1620 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1621 if (readout
!= prev
)
1623 trace_debug ("GDBserver has touched the trace buffer, restarting."
1624 " (prev=%08x, commit=%08x, readout=%08x)",
1625 prev
, commit
, readout
);
1629 /* Hold your horses here. Even if that change was committed,
1630 GDBserver could come in, and clobber it. We need to hold to be
1631 able to tell if GDBserver clobbers before or after we committed
1632 the change. Whenever GDBserver goes about touching the IPA
1633 buffer, it sets a breakpoint in this routine, so we have a sync
1635 about_to_request_buffer_space ();
1637 /* Check if the change has been effective, even if GDBserver stopped
1638 us at the breakpoint. */
1641 unsigned int refetch
;
1645 refetch
= trace_buffer_ctrl_curr
;
1647 if (refetch
== commit
1648 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1651 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1652 "readout=%08x, refetch=%08x)",
1653 prev
, commit
, readout
, refetch
);
1657 trace_debug ("GDBserver has touched the trace buffer, not effective."
1658 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1659 prev
, commit
, readout
, refetch
);
1665 /* We have a new piece of the trace buffer. Hurray! */
1667 /* Add an EOB marker just past this allocation. */
1668 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1669 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1671 /* Adjust the request back down, now that we know we have space for
1673 amt
-= sizeof (struct traceframe
);
1677 trace_debug ("Allocated %d bytes", (int) amt
);
1678 trace_debug ("Trace buffer [%d] start=%d free=%d "
1679 "endfree=%d wrap=%d hi=%d",
1681 (int) (tbctrl
->start
- trace_buffer_lo
),
1682 (int) (tbctrl
->free
- trace_buffer_lo
),
1683 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1684 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1685 (int) (trace_buffer_hi
- trace_buffer_lo
));
1691 #ifndef IN_PROCESS_AGENT
1693 /* Return the total free space. This is not necessarily the largest
1694 block we can allocate, because of the two-part case. */
1699 if (trace_buffer_free
<= trace_buffer_end_free
)
1700 return trace_buffer_end_free
- trace_buffer_free
;
1702 return ((trace_buffer_end_free
- trace_buffer_lo
)
1703 + (trace_buffer_hi
- trace_buffer_free
));
1706 /* An 'S' in continuation packets indicates remainder are for
1709 static int seen_step_action_flag
;
1711 /* Create a tracepoint (location) with given number and address. Add this
1712 new tracepoint to list and sort this list. */
1714 static struct tracepoint
*
1715 add_tracepoint (int num
, CORE_ADDR addr
)
1717 struct tracepoint
*tpoint
, **tp_next
;
1719 tpoint
= xmalloc (sizeof (struct tracepoint
));
1720 tpoint
->number
= num
;
1721 tpoint
->address
= addr
;
1722 tpoint
->numactions
= 0;
1723 tpoint
->actions
= NULL
;
1724 tpoint
->actions_str
= NULL
;
1725 tpoint
->cond
= NULL
;
1726 tpoint
->num_step_actions
= 0;
1727 tpoint
->step_actions
= NULL
;
1728 tpoint
->step_actions_str
= NULL
;
1729 /* Start all off as regular (slow) tracepoints. */
1730 tpoint
->type
= trap_tracepoint
;
1731 tpoint
->orig_size
= -1;
1732 tpoint
->source_strings
= NULL
;
1733 tpoint
->compiled_cond
= 0;
1734 tpoint
->handle
= NULL
;
1735 tpoint
->next
= NULL
;
1737 /* Find a place to insert this tracepoint into list in order to keep
1738 the tracepoint list still in the ascending order. There may be
1739 multiple tracepoints at the same address as TPOINT's, and this
1740 guarantees TPOINT is inserted after all the tracepoints which are
1741 set at the same address. For example, fast tracepoints A, B, C are
1742 set at the same address, and D is to be insert at the same place as
1745 -->| A |--> | B |-->| C |->...
1747 One jump pad was created for tracepoint A, B, and C, and the target
1748 address of A is referenced/used in jump pad. So jump pad will let
1749 inferior jump to A. If D is inserted in front of A, like this,
1751 -->| D |-->| A |--> | B |-->| C |->...
1753 without updating jump pad, D is not reachable during collect, which
1754 is wrong. As we can see, the order of B, C and D doesn't matter, but
1755 A should always be the `first' one. */
1756 for (tp_next
= &tracepoints
;
1757 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1758 tp_next
= &(*tp_next
)->next
)
1760 tpoint
->next
= *tp_next
;
1762 last_tracepoint
= tpoint
;
1764 seen_step_action_flag
= 0;
1769 #ifndef IN_PROCESS_AGENT
1771 /* Return the tracepoint with the given number and address, or NULL. */
1773 static struct tracepoint
*
1774 find_tracepoint (int id
, CORE_ADDR addr
)
1776 struct tracepoint
*tpoint
;
1778 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1779 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1785 /* Remove TPOINT from global list. */
1788 remove_tracepoint (struct tracepoint
*tpoint
)
1790 struct tracepoint
*tp
, *tp_prev
;
1792 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1793 tp_prev
= tp
, tp
= tp
->next
)
1799 tp_prev
->next
= tp
->next
;
1801 tracepoints
= tp
->next
;
1807 /* There may be several tracepoints with the same number (because they
1808 are "locations", in GDB parlance); return the next one after the
1809 given tracepoint, or search from the beginning of the list if the
1810 first argument is NULL. */
1812 static struct tracepoint
*
1813 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1815 struct tracepoint
*tpoint
;
1818 tpoint
= prev_tp
->next
;
1820 tpoint
= tracepoints
;
1821 for (; tpoint
; tpoint
= tpoint
->next
)
1822 if (tpoint
->number
== num
)
1831 save_string (const char *str
, size_t len
)
1835 s
= xmalloc (len
+ 1);
1836 memcpy (s
, str
, len
);
1842 /* Append another action to perform when the tracepoint triggers. */
1845 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1851 seen_step_action_flag
= 1;
1859 char *act_start
= act
;
1860 struct tracepoint_action
*action
= NULL
;
1866 struct collect_memory_action
*maction
;
1870 maction
= xmalloc (sizeof *maction
);
1871 maction
->base
.type
= *act
;
1872 maction
->base
.ops
= &m_tracepoint_action_ops
;
1873 action
= &maction
->base
;
1876 is_neg
= (*act
== '-');
1879 act
= unpack_varlen_hex (act
, &basereg
);
1881 act
= unpack_varlen_hex (act
, &maction
->addr
);
1883 act
= unpack_varlen_hex (act
, &maction
->len
);
1884 maction
->basereg
= (is_neg
1887 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1888 pulongest (maction
->len
),
1889 paddress (maction
->addr
), maction
->basereg
);
1894 struct collect_registers_action
*raction
;
1896 raction
= xmalloc (sizeof *raction
);
1897 raction
->base
.type
= *act
;
1898 raction
->base
.ops
= &r_tracepoint_action_ops
;
1899 action
= &raction
->base
;
1901 trace_debug ("Want to collect registers");
1903 /* skip past hex digits of mask for now */
1904 while (isxdigit(*act
))
1910 struct collect_static_trace_data_action
*raction
;
1912 raction
= xmalloc (sizeof *raction
);
1913 raction
->base
.type
= *act
;
1914 raction
->base
.ops
= &l_tracepoint_action_ops
;
1915 action
= &raction
->base
;
1917 trace_debug ("Want to collect static trace data");
1922 trace_debug ("Unexpected step action, ignoring");
1927 struct eval_expr_action
*xaction
;
1929 xaction
= xmalloc (sizeof (*xaction
));
1930 xaction
->base
.type
= *act
;
1931 xaction
->base
.ops
= &x_tracepoint_action_ops
;
1932 action
= &xaction
->base
;
1934 trace_debug ("Want to evaluate expression");
1935 xaction
->expr
= gdb_parse_agent_expr (&act
);
1939 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1948 if (seen_step_action_flag
)
1950 tpoint
->num_step_actions
++;
1952 tpoint
->step_actions
1953 = xrealloc (tpoint
->step_actions
,
1954 (sizeof (*tpoint
->step_actions
)
1955 * tpoint
->num_step_actions
));
1956 tpoint
->step_actions_str
1957 = xrealloc (tpoint
->step_actions_str
,
1958 (sizeof (*tpoint
->step_actions_str
)
1959 * tpoint
->num_step_actions
));
1960 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1961 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1962 = save_string (act_start
, act
- act_start
);
1966 tpoint
->numactions
++;
1968 = xrealloc (tpoint
->actions
,
1969 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1971 = xrealloc (tpoint
->actions_str
,
1972 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1973 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1974 tpoint
->actions_str
[tpoint
->numactions
- 1]
1975 = save_string (act_start
, act
- act_start
);
1982 /* Find or create a trace state variable with the given number. */
1984 static struct trace_state_variable
*
1985 get_trace_state_variable (int num
)
1987 struct trace_state_variable
*tsv
;
1989 #ifdef IN_PROCESS_AGENT
1990 /* Search for an existing variable. */
1991 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1992 if (tsv
->number
== num
)
1996 /* Search for an existing variable. */
1997 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1998 if (tsv
->number
== num
)
2004 /* Find or create a trace state variable with the given number. */
2006 static struct trace_state_variable
*
2007 create_trace_state_variable (int num
, int gdb
)
2009 struct trace_state_variable
*tsv
;
2011 tsv
= get_trace_state_variable (num
);
2015 /* Create a new variable. */
2016 tsv
= xmalloc (sizeof (struct trace_state_variable
));
2018 tsv
->initial_value
= 0;
2022 #ifdef IN_PROCESS_AGENT
2025 tsv
->next
= alloced_trace_state_variables
;
2026 alloced_trace_state_variables
= tsv
;
2031 tsv
->next
= trace_state_variables
;
2032 trace_state_variables
= tsv
;
2037 IP_AGENT_EXPORT LONGEST
2038 get_trace_state_variable_value (int num
)
2040 struct trace_state_variable
*tsv
;
2042 tsv
= get_trace_state_variable (num
);
2046 trace_debug ("No trace state variable %d, skipping value get", num
);
2050 /* Call a getter function if we have one. While it's tempting to
2051 set up something to only call the getter once per tracepoint hit,
2052 it could run afoul of thread races. Better to let the getter
2053 handle it directly, if necessary to worry about it. */
2055 tsv
->value
= (tsv
->getter
) ();
2057 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2058 num
, plongest (tsv
->value
));
2063 IP_AGENT_EXPORT
void
2064 set_trace_state_variable_value (int num
, LONGEST val
)
2066 struct trace_state_variable
*tsv
;
2068 tsv
= get_trace_state_variable (num
);
2072 trace_debug ("No trace state variable %d, skipping value set", num
);
2080 agent_get_trace_state_variable_value (int num
)
2082 return get_trace_state_variable_value (num
);
2086 agent_set_trace_state_variable_value (int num
, LONGEST val
)
2088 set_trace_state_variable_value (num
, val
);
2092 set_trace_state_variable_name (int num
, const char *name
)
2094 struct trace_state_variable
*tsv
;
2096 tsv
= get_trace_state_variable (num
);
2100 trace_debug ("No trace state variable %d, skipping name set", num
);
2104 tsv
->name
= (char *) name
;
2108 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2110 struct trace_state_variable
*tsv
;
2112 tsv
= get_trace_state_variable (num
);
2116 trace_debug ("No trace state variable %d, skipping getter set", num
);
2120 tsv
->getter
= getter
;
2123 /* Add a raw traceframe for the given tracepoint. */
2125 static struct traceframe
*
2126 add_traceframe (struct tracepoint
*tpoint
)
2128 struct traceframe
*tframe
;
2130 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2135 tframe
->tpnum
= tpoint
->number
;
2136 tframe
->data_size
= 0;
2141 /* Add a block to the traceframe currently being worked on. */
2143 static unsigned char *
2144 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2146 unsigned char *block
;
2151 block
= trace_buffer_alloc (amt
);
2156 tframe
->data_size
+= amt
;
2161 /* Flag that the current traceframe is finished. */
2164 finish_traceframe (struct traceframe
*tframe
)
2166 ++traceframe_write_count
;
2167 ++traceframes_created
;
2170 #ifndef IN_PROCESS_AGENT
2172 /* Given a traceframe number NUM, find the NUMth traceframe in the
2175 static struct traceframe
*
2176 find_traceframe (int num
)
2178 struct traceframe
*tframe
;
2181 for (tframe
= FIRST_TRACEFRAME ();
2183 tframe
= NEXT_TRACEFRAME (tframe
))
2194 get_traceframe_address (struct traceframe
*tframe
)
2197 struct tracepoint
*tpoint
;
2199 addr
= traceframe_get_pc (tframe
);
2204 /* Fallback strategy, will be incorrect for while-stepping frames
2205 and multi-location tracepoints. */
2206 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2207 return tpoint
->address
;
2210 /* Search for the next traceframe whose address is inside or outside
2213 static struct traceframe
*
2214 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2217 struct traceframe
*tframe
;
2220 *tfnump
= current_traceframe
+ 1;
2221 tframe
= find_traceframe (*tfnump
);
2222 /* The search is not supposed to wrap around. */
2229 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2231 tfaddr
= get_traceframe_address (tframe
);
2233 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2234 : (lo
> tfaddr
|| tfaddr
> hi
))
2243 /* Search for the next traceframe recorded by the given tracepoint.
2244 Note that for multi-location tracepoints, this will find whatever
2245 location appears first. */
2247 static struct traceframe
*
2248 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2250 struct traceframe
*tframe
;
2252 *tfnump
= current_traceframe
+ 1;
2253 tframe
= find_traceframe (*tfnump
);
2254 /* The search is not supposed to wrap around. */
2261 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2263 if (tframe
->tpnum
== num
)
2274 #ifndef IN_PROCESS_AGENT
2276 /* Clear all past trace state. */
2279 cmd_qtinit (char *packet
)
2281 struct trace_state_variable
*tsv
, *prev
, *next
;
2283 /* Make sure we don't try to read from a trace frame. */
2284 current_traceframe
= -1;
2286 trace_debug ("Initializing the trace");
2288 clear_installed_tracepoints ();
2289 clear_readonly_regions ();
2292 last_tracepoint
= NULL
;
2294 /* Clear out any leftover trace state variables. Ones with target
2295 defined getters should be kept however. */
2297 tsv
= trace_state_variables
;
2300 trace_debug ("Looking at var %d", tsv
->number
);
2301 if (tsv
->getter
== NULL
)
2307 trace_state_variables
= next
;
2308 trace_debug ("Deleting var %d", tsv
->number
);
2319 clear_trace_buffer ();
2320 clear_inferior_trace_buffer ();
2325 /* Unprobe the UST marker at ADDRESS. */
2328 unprobe_marker_at (CORE_ADDR address
)
2330 char cmd
[IPA_CMD_BUF_SIZE
];
2332 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2333 run_inferior_command (cmd
);
2336 /* Restore the program to its pre-tracing state. This routine may be called
2337 in error situations, so it needs to be careful about only restoring
2338 from known-valid bits. */
2341 clear_installed_tracepoints (void)
2343 struct tracepoint
*tpoint
;
2344 struct tracepoint
*prev_stpoint
;
2347 cancel_breakpoints ();
2349 prev_stpoint
= NULL
;
2351 /* Restore any bytes overwritten by tracepoints. */
2352 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2354 /* Catch the case where we might try to remove a tracepoint that
2355 was never actually installed. */
2356 if (tpoint
->handle
== NULL
)
2358 trace_debug ("Tracepoint %d at 0x%s was "
2359 "never installed, nothing to clear",
2360 tpoint
->number
, paddress (tpoint
->address
));
2364 switch (tpoint
->type
)
2366 case trap_tracepoint
:
2367 delete_breakpoint (tpoint
->handle
);
2369 case fast_tracepoint
:
2370 delete_fast_tracepoint_jump (tpoint
->handle
);
2372 case static_tracepoint
:
2373 if (prev_stpoint
!= NULL
2374 && prev_stpoint
->address
== tpoint
->address
)
2375 /* Nothing to do. We already unprobed a tracepoint set at
2376 this marker address (and there can only be one probe
2381 unprobe_marker_at (tpoint
->address
);
2382 prev_stpoint
= tpoint
;
2387 tpoint
->handle
= NULL
;
2393 /* Parse a packet that defines a tracepoint. */
2396 cmd_qtdp (char *own_buf
)
2399 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2400 int trail_hyphen
= 0;
2404 struct tracepoint
*tpoint
;
2406 char *packet
= own_buf
;
2408 packet
+= strlen ("QTDP:");
2410 /* A hyphen at the beginning marks a packet specifying actions for a
2411 tracepoint already supplied. */
2418 packet
= unpack_varlen_hex (packet
, &num
);
2419 ++packet
; /* skip a colon */
2420 packet
= unpack_varlen_hex (packet
, &addr
);
2421 ++packet
; /* skip a colon */
2423 /* See if we already have this tracepoint. */
2424 tpoint
= find_tracepoint (num
, addr
);
2428 /* Duplicate tracepoints are never allowed. */
2431 trace_debug ("Tracepoint error: tracepoint %d"
2432 " at 0x%s already exists",
2433 (int) num
, paddress (addr
));
2434 write_enn (own_buf
);
2438 tpoint
= add_tracepoint (num
, addr
);
2440 tpoint
->enabled
= (*packet
== 'E');
2441 ++packet
; /* skip 'E' */
2442 ++packet
; /* skip a colon */
2443 packet
= unpack_varlen_hex (packet
, &count
);
2444 tpoint
->step_count
= count
;
2445 ++packet
; /* skip a colon */
2446 packet
= unpack_varlen_hex (packet
, &count
);
2447 tpoint
->pass_count
= count
;
2448 /* See if we have any of the additional optional fields. */
2449 while (*packet
== ':')
2454 tpoint
->type
= fast_tracepoint
;
2456 packet
= unpack_varlen_hex (packet
, &count
);
2457 tpoint
->orig_size
= count
;
2459 else if (*packet
== 'S')
2461 tpoint
->type
= static_tracepoint
;
2464 else if (*packet
== 'X')
2466 actparm
= (char *) packet
;
2467 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2470 else if (*packet
== '-')
2472 else if (*packet
== '\0')
2475 trace_debug ("Unknown optional tracepoint field");
2480 trace_debug ("Also has actions\n");
2483 trace_debug ("Defined %stracepoint %d at 0x%s, "
2484 "enabled %d step %" PRIu64
" pass %" PRIu64
,
2485 tpoint
->type
== fast_tracepoint
? "fast "
2486 : tpoint
->type
== static_tracepoint
? "static " : "",
2487 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2488 tpoint
->step_count
, tpoint
->pass_count
);
2491 add_tracepoint_action (tpoint
, packet
);
2494 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2495 (int) num
, paddress (addr
));
2496 write_enn (own_buf
);
2500 /* Install tracepoint during tracing only once for each tracepoint location.
2501 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2502 determine the last QTDP packet for one tracepoint location by checking
2503 trailing hyphen in QTDP packet. */
2504 if (tracing
&& !trail_hyphen
)
2506 struct tracepoint
*tp
= NULL
;
2508 /* Pause all threads temporarily while we patch tracepoints. */
2511 /* download_tracepoint will update global `tracepoints'
2512 list, so it is unsafe to leave threads in jump pad. */
2513 stabilize_threads ();
2515 /* Freeze threads. */
2519 if (tpoint
->type
!= trap_tracepoint
)
2521 /* Find another fast or static tracepoint at the same address. */
2522 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2524 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2525 && tp
->number
!= tpoint
->number
)
2529 /* TPOINT is installed at the same address as TP. */
2532 if (tpoint
->type
== fast_tracepoint
)
2533 clone_fast_tracepoint (tpoint
, tp
);
2534 else if (tpoint
->type
== static_tracepoint
)
2535 tpoint
->handle
= (void *) -1;
2539 download_tracepoint (tpoint
);
2541 if (tpoint
->type
== trap_tracepoint
|| tp
== NULL
)
2543 install_tracepoint (tpoint
, own_buf
);
2544 if (strcmp (own_buf
, "OK") != 0)
2545 remove_tracepoint (tpoint
);
2558 cmd_qtdpsrc (char *own_buf
)
2560 ULONGEST num
, addr
, start
, slen
;
2561 struct tracepoint
*tpoint
;
2562 char *packet
= own_buf
;
2563 char *saved
, *srctype
, *src
;
2565 struct source_string
*last
, *newlast
;
2567 packet
+= strlen ("QTDPsrc:");
2569 packet
= unpack_varlen_hex (packet
, &num
);
2570 ++packet
; /* skip a colon */
2571 packet
= unpack_varlen_hex (packet
, &addr
);
2572 ++packet
; /* skip a colon */
2574 /* See if we already have this tracepoint. */
2575 tpoint
= find_tracepoint (num
, addr
);
2579 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2580 (int) num
, paddress (addr
));
2581 write_enn (own_buf
);
2586 packet
= strchr (packet
, ':');
2587 srctype
= xmalloc (packet
- saved
+ 1);
2588 memcpy (srctype
, saved
, packet
- saved
);
2589 srctype
[packet
- saved
] = '\0';
2591 packet
= unpack_varlen_hex (packet
, &start
);
2592 ++packet
; /* skip a colon */
2593 packet
= unpack_varlen_hex (packet
, &slen
);
2594 ++packet
; /* skip a colon */
2595 src
= xmalloc (slen
+ 1);
2596 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2599 newlast
= xmalloc (sizeof (struct source_string
));
2600 newlast
->type
= srctype
;
2602 newlast
->next
= NULL
;
2603 /* Always add a source string to the end of the list;
2604 this keeps sequences of actions/commands in the right
2606 if (tpoint
->source_strings
)
2608 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2610 last
->next
= newlast
;
2613 tpoint
->source_strings
= newlast
;
2619 cmd_qtdv (char *own_buf
)
2621 ULONGEST num
, val
, builtin
;
2624 struct trace_state_variable
*tsv
;
2625 char *packet
= own_buf
;
2627 packet
+= strlen ("QTDV:");
2629 packet
= unpack_varlen_hex (packet
, &num
);
2630 ++packet
; /* skip a colon */
2631 packet
= unpack_varlen_hex (packet
, &val
);
2632 ++packet
; /* skip a colon */
2633 packet
= unpack_varlen_hex (packet
, &builtin
);
2634 ++packet
; /* skip a colon */
2636 nbytes
= strlen (packet
) / 2;
2637 varname
= xmalloc (nbytes
+ 1);
2638 nbytes
= unhexify (varname
, packet
, nbytes
);
2639 varname
[nbytes
] = '\0';
2641 tsv
= create_trace_state_variable (num
, 1);
2642 tsv
->initial_value
= (LONGEST
) val
;
2643 tsv
->name
= varname
;
2645 set_trace_state_variable_value (num
, (LONGEST
) val
);
2651 cmd_qtenable_disable (char *own_buf
, int enable
)
2653 char *packet
= own_buf
;
2655 struct tracepoint
*tp
;
2657 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2658 packet
= unpack_varlen_hex (packet
, &num
);
2659 ++packet
; /* skip a colon */
2660 packet
= unpack_varlen_hex (packet
, &addr
);
2662 tp
= find_tracepoint (num
, addr
);
2666 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2668 trace_debug ("Tracepoint %d at 0x%s is already %s",
2669 (int) num
, paddress (addr
),
2670 enable
? "enabled" : "disabled");
2675 trace_debug ("%s tracepoint %d at 0x%s",
2676 enable
? "Enabling" : "Disabling",
2677 (int) num
, paddress (addr
));
2679 tp
->enabled
= enable
;
2681 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2684 int offset
= offsetof (struct tracepoint
, enabled
);
2685 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2687 ret
= prepare_to_access_memory ();
2690 trace_debug ("Failed to temporarily stop inferior threads");
2691 write_enn (own_buf
);
2695 ret
= write_inferior_integer (obj_addr
, enable
);
2696 done_accessing_memory ();
2700 trace_debug ("Cannot write enabled flag into "
2701 "inferior process memory");
2702 write_enn (own_buf
);
2711 trace_debug ("Tracepoint %d at 0x%s not found",
2712 (int) num
, paddress (addr
));
2713 write_enn (own_buf
);
2718 cmd_qtv (char *own_buf
)
2723 char *packet
= own_buf
;
2725 packet
+= strlen ("qTV:");
2726 unpack_varlen_hex (packet
, &num
);
2728 if (current_traceframe
>= 0)
2730 err
= traceframe_read_tsv ((int) num
, &val
);
2733 strcpy (own_buf
, "U");
2737 /* Only make tsv's be undefined before the first trace run. After a
2738 trace run is over, the user might want to see the last value of
2739 the tsv, and it might not be available in a traceframe. */
2740 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2742 strcpy (own_buf
, "U");
2746 val
= get_trace_state_variable_value (num
);
2748 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2751 /* Clear out the list of readonly regions. */
2754 clear_readonly_regions (void)
2756 struct readonly_region
*roreg
;
2758 while (readonly_regions
)
2760 roreg
= readonly_regions
;
2761 readonly_regions
= readonly_regions
->next
;
2766 /* Parse the collection of address ranges whose contents GDB believes
2767 to be unchanging and so can be read directly from target memory
2768 even while looking at a traceframe. */
2771 cmd_qtro (char *own_buf
)
2773 ULONGEST start
, end
;
2774 struct readonly_region
*roreg
;
2775 char *packet
= own_buf
;
2777 trace_debug ("Want to mark readonly regions");
2779 clear_readonly_regions ();
2781 packet
+= strlen ("QTro");
2783 while (*packet
== ':')
2785 ++packet
; /* skip a colon */
2786 packet
= unpack_varlen_hex (packet
, &start
);
2787 ++packet
; /* skip a comma */
2788 packet
= unpack_varlen_hex (packet
, &end
);
2789 roreg
= xmalloc (sizeof (struct readonly_region
));
2790 roreg
->start
= start
;
2792 roreg
->next
= readonly_regions
;
2793 readonly_regions
= roreg
;
2794 trace_debug ("Added readonly region from 0x%s to 0x%s",
2795 paddress (roreg
->start
), paddress (roreg
->end
));
2801 /* Test to see if the given range is in our list of readonly ranges.
2802 We only test for being entirely within a range, GDB is not going to
2803 send a single memory packet that spans multiple regions. */
2806 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2808 struct readonly_region
*roreg
;
2810 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2811 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2817 /* The maximum size of a jump pad entry. */
2818 static const int max_jump_pad_size
= 0x100;
2820 static CORE_ADDR gdb_jump_pad_head
;
2822 /* Return the address of the next free jump space. */
2825 get_jump_space_head (void)
2827 if (gdb_jump_pad_head
== 0)
2829 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2830 &gdb_jump_pad_head
))
2831 fatal ("error extracting jump_pad_buffer");
2834 return gdb_jump_pad_head
;
2837 /* Reserve USED bytes from the jump space. */
2840 claim_jump_space (ULONGEST used
)
2842 trace_debug ("claim_jump_space reserves %s bytes at %s",
2843 pulongest (used
), paddress (gdb_jump_pad_head
));
2844 gdb_jump_pad_head
+= used
;
2847 static CORE_ADDR trampoline_buffer_head
= 0;
2848 static CORE_ADDR trampoline_buffer_tail
;
2850 /* Reserve USED bytes from the trampoline buffer and return the
2851 address of the start of the reserved space in TRAMPOLINE. Returns
2852 non-zero if the space is successfully claimed. */
2855 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2857 if (!trampoline_buffer_head
)
2859 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2860 &trampoline_buffer_tail
))
2862 fatal ("error extracting trampoline_buffer");
2866 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2867 &trampoline_buffer_head
))
2869 fatal ("error extracting trampoline_buffer_end");
2874 /* Start claiming space from the top of the trampoline space. If
2875 the space is located at the bottom of the virtual address space,
2876 this reduces the possibility that corruption will occur if a null
2877 pointer is used to write to memory. */
2878 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2880 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2885 trampoline_buffer_head
-= used
;
2887 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2888 pulongest (used
), paddress (trampoline_buffer_head
));
2890 *trampoline
= trampoline_buffer_head
;
2894 /* Returns non-zero if there is space allocated for use in trampolines
2895 for fast tracepoints. */
2898 have_fast_tracepoint_trampoline_buffer (char *buf
)
2900 CORE_ADDR trampoline_end
, errbuf
;
2902 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2905 fatal ("error extracting trampoline_buffer_end");
2912 strcpy (buf
, "was claiming");
2913 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2916 fatal ("error extracting errbuf");
2920 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2923 return trampoline_end
!= 0;
2926 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2927 the command fails, or 0 otherwise. If the command ran
2928 successfully, but probing the marker failed, ERROUT will be filled
2929 with the error to reply to GDB, and -1 is also returned. This
2930 allows directly passing IPA errors to GDB. */
2933 probe_marker_at (CORE_ADDR address
, char *errout
)
2935 char cmd
[IPA_CMD_BUF_SIZE
];
2938 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2939 err
= run_inferior_command (cmd
);
2945 strcpy (errout
, cmd
);
2954 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2956 to
->jump_pad
= from
->jump_pad
;
2957 to
->jump_pad_end
= from
->jump_pad_end
;
2958 to
->trampoline
= from
->trampoline
;
2959 to
->trampoline_end
= from
->trampoline_end
;
2960 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2961 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2962 to
->handle
= from
->handle
;
2964 gdb_assert (from
->handle
);
2965 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2968 #define MAX_JUMP_SIZE 20
2970 /* Install fast tracepoint. Return 0 if successful, otherwise return
2974 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
2976 CORE_ADDR jentry
, jump_entry
;
2977 CORE_ADDR trampoline
;
2978 ULONGEST trampoline_size
;
2980 /* The jump to the jump pad of the last fast tracepoint
2982 unsigned char fjump
[MAX_JUMP_SIZE
];
2983 ULONGEST fjump_size
;
2985 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
2987 trace_debug ("Requested a fast tracepoint on an instruction "
2988 "that is of less than the minimum length.");
2992 jentry
= jump_entry
= get_jump_space_head ();
2995 trampoline_size
= 0;
2997 /* Install the jump pad. */
2998 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
3000 ipa_sym_addrs
.addr_gdb_collect
,
3001 ipa_sym_addrs
.addr_collecting
,
3004 &trampoline
, &trampoline_size
,
3006 &tpoint
->adjusted_insn_addr
,
3007 &tpoint
->adjusted_insn_addr_end
,
3014 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
3017 if (tpoint
->handle
!= NULL
)
3019 tpoint
->jump_pad
= jump_entry
;
3020 tpoint
->jump_pad_end
= jentry
;
3021 tpoint
->trampoline
= trampoline
;
3022 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
3024 /* Pad to 8-byte alignment. */
3025 jentry
= ((jentry
+ 7) & ~0x7);
3026 claim_jump_space (jentry
- jump_entry
);
3033 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
3036 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
3038 tpoint
->handle
= NULL
;
3041 if (tpoint
->type
== trap_tracepoint
)
3043 /* Tracepoints are installed as memory breakpoints. Just go
3044 ahead and install the trap. The breakpoints module
3045 handles duplicated breakpoints, and the memory read
3046 routine handles un-patching traps from memory reads. */
3047 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3048 tracepoint_handler
);
3050 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
3052 if (!agent_loaded_p ())
3054 trace_debug ("Requested a %s tracepoint, but fast "
3055 "tracepoints aren't supported.",
3056 tpoint
->type
== static_tracepoint
? "static" : "fast");
3057 write_e_ipa_not_loaded (own_buf
);
3060 if (tpoint
->type
== static_tracepoint
3061 && !in_process_agent_supports_ust ())
3063 trace_debug ("Requested a static tracepoint, but static "
3064 "tracepoints are not supported.");
3065 write_e_ust_not_loaded (own_buf
);
3069 if (tpoint
->type
== fast_tracepoint
)
3070 install_fast_tracepoint (tpoint
, own_buf
);
3073 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
3074 tpoint
->handle
= (void *) -1;
3079 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
3081 if (tpoint
->handle
== NULL
)
3083 if (*own_buf
== '\0')
3084 write_enn (own_buf
);
3090 static void download_tracepoint_1 (struct tracepoint
*tpoint
);
3093 cmd_qtstart (char *packet
)
3095 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
3096 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
3098 trace_debug ("Starting the trace");
3100 /* Pause all threads temporarily while we patch tracepoints. */
3103 /* Get threads out of jump pads. Safe to do here, since this is a
3104 top level command. And, required to do here, since we're
3105 deleting/rewriting jump pads. */
3107 stabilize_threads ();
3109 /* Freeze threads. */
3112 /* Sync the fast tracepoints list in the inferior ftlib. */
3113 if (agent_loaded_p ())
3114 download_trace_state_variables ();
3116 /* No previous fast tpoint yet. */
3117 prev_ftpoint
= NULL
;
3119 /* No previous static tpoint yet. */
3120 prev_stpoint
= NULL
;
3124 /* Start out empty. */
3125 if (agent_loaded_p ())
3126 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
3128 /* Download and install tracepoints. */
3129 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3131 /* Ensure all the hit counts start at zero. */
3132 tpoint
->hit_count
= 0;
3133 tpoint
->traceframe_usage
= 0;
3135 if (tpoint
->type
== trap_tracepoint
)
3137 /* Tracepoints are installed as memory breakpoints. Just go
3138 ahead and install the trap. The breakpoints module
3139 handles duplicated breakpoints, and the memory read
3140 routine handles un-patching traps from memory reads. */
3141 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3142 tracepoint_handler
);
3144 else if (tpoint
->type
== fast_tracepoint
3145 || tpoint
->type
== static_tracepoint
)
3147 if (maybe_write_ipa_not_loaded (packet
))
3149 trace_debug ("Requested a %s tracepoint, but fast "
3150 "tracepoints aren't supported.",
3151 tpoint
->type
== static_tracepoint
3152 ? "static" : "fast");
3156 if (tpoint
->type
== fast_tracepoint
)
3158 download_tracepoint_1 (tpoint
);
3160 if (prev_ftpoint
!= NULL
3161 && prev_ftpoint
->address
== tpoint
->address
)
3162 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3165 if (install_fast_tracepoint (tpoint
, packet
) == 0)
3166 prev_ftpoint
= tpoint
;
3171 if (!in_process_agent_supports_ust ())
3173 trace_debug ("Requested a static tracepoint, but static "
3174 "tracepoints are not supported.");
3178 download_tracepoint_1 (tpoint
);
3179 /* Can only probe a given marker once. */
3180 if (prev_stpoint
!= NULL
3181 && prev_stpoint
->address
== tpoint
->address
)
3182 tpoint
->handle
= (void *) -1;
3185 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3187 tpoint
->handle
= (void *) -1;
3189 /* So that we can handle multiple static tracepoints
3190 at the same address easily. */
3191 prev_stpoint
= tpoint
;
3197 tpptr
= tpoint
->obj_addr_on_target
;
3199 if (tpoint
== tracepoints
)
3200 /* First object in list, set the head pointer in the
3202 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
3204 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
3209 /* Any failure in the inner loop is sufficient cause to give
3211 if (tpoint
->handle
== NULL
)
3215 /* Any error in tracepoint insertion is unacceptable; better to
3216 address the problem now, than end up with a useless or misleading
3220 clear_installed_tracepoints ();
3221 if (*packet
== '\0')
3227 stopping_tracepoint
= NULL
;
3228 trace_buffer_is_full
= 0;
3229 expr_eval_result
= expr_eval_no_error
;
3230 error_tracepoint
= NULL
;
3231 tracing_start_time
= get_timestamp ();
3233 /* Tracing is now active, hits will now start being logged. */
3236 if (agent_loaded_p ())
3238 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3239 fatal ("Error setting tracing variable in lib");
3241 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3243 fatal ("Error clearing stopping_tracepoint variable in lib");
3245 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3246 fatal ("Error clearing trace_buffer_is_full variable in lib");
3248 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3249 stop_tracing_handler
);
3250 if (stop_tracing_bkpt
== NULL
)
3251 error ("Error setting stop_tracing breakpoint");
3253 flush_trace_buffer_bkpt
3254 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3255 flush_trace_buffer_handler
);
3256 if (flush_trace_buffer_bkpt
== NULL
)
3257 error ("Error setting flush_trace_buffer breakpoint");
3265 /* End a tracing run, filling in a stop reason to report back to GDB,
3266 and removing the tracepoints from the code. */
3273 trace_debug ("Tracing is already off, ignoring");
3277 trace_debug ("Stopping the trace");
3279 /* Pause all threads before removing fast jumps from memory,
3280 breakpoints, and touching IPA state variables (inferior memory).
3281 Some thread may hit the internal tracing breakpoints, or be
3282 collecting this moment, but that's ok, we don't release the
3283 tpoint object's memory or the jump pads here (we only do that
3284 when we're sure we can move all threads out of the jump pads).
3285 We can't now, since we may be getting here due to the inferior
3286 agent calling us. */
3288 /* Since we're removing breakpoints, cancel breakpoint hits,
3289 possibly related to the breakpoints we're about to delete. */
3290 cancel_breakpoints ();
3292 /* Stop logging. Tracepoints can still be hit, but they will not be
3295 if (agent_loaded_p ())
3297 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3298 fatal ("Error clearing tracing variable in lib");
3301 tracing_stop_time
= get_timestamp ();
3302 tracing_stop_reason
= "t???";
3303 tracing_stop_tpnum
= 0;
3304 if (stopping_tracepoint
)
3306 trace_debug ("Stopping the trace because "
3307 "tracepoint %d was hit %" PRIu64
" times",
3308 stopping_tracepoint
->number
,
3309 stopping_tracepoint
->pass_count
);
3310 tracing_stop_reason
= "tpasscount";
3311 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3313 else if (trace_buffer_is_full
)
3315 trace_debug ("Stopping the trace because the trace buffer is full");
3316 tracing_stop_reason
= "tfull";
3318 else if (expr_eval_result
!= expr_eval_no_error
)
3320 trace_debug ("Stopping the trace because of an expression eval error");
3321 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3322 tracing_stop_tpnum
= error_tracepoint
->number
;
3324 #ifndef IN_PROCESS_AGENT
3325 else if (!gdb_connected ())
3327 trace_debug ("Stopping the trace because GDB disconnected");
3328 tracing_stop_reason
= "tdisconnected";
3333 trace_debug ("Stopping the trace because of a tstop command");
3334 tracing_stop_reason
= "tstop";
3337 stopping_tracepoint
= NULL
;
3338 error_tracepoint
= NULL
;
3340 /* Clear out the tracepoints. */
3341 clear_installed_tracepoints ();
3343 if (agent_loaded_p ())
3345 /* Pull in fast tracepoint trace frames from the inferior lib
3346 buffer into our buffer, even if our buffer is already full,
3347 because we want to present the full number of created frames
3348 in addition to what fit in the trace buffer. */
3349 upload_fast_traceframes ();
3352 if (stop_tracing_bkpt
!= NULL
)
3354 delete_breakpoint (stop_tracing_bkpt
);
3355 stop_tracing_bkpt
= NULL
;
3358 if (flush_trace_buffer_bkpt
!= NULL
)
3360 delete_breakpoint (flush_trace_buffer_bkpt
);
3361 flush_trace_buffer_bkpt
= NULL
;
3368 stop_tracing_handler (CORE_ADDR addr
)
3370 trace_debug ("lib hit stop_tracing");
3372 /* Don't actually handle it here. When we stop tracing we remove
3373 breakpoints from the inferior, and that is not allowed in a
3374 breakpoint handler (as the caller is walking the breakpoint
3380 flush_trace_buffer_handler (CORE_ADDR addr
)
3382 trace_debug ("lib hit flush_trace_buffer");
3387 cmd_qtstop (char *packet
)
3394 cmd_qtdisconnected (char *own_buf
)
3397 char *packet
= own_buf
;
3399 packet
+= strlen ("QTDisconnected:");
3401 unpack_varlen_hex (packet
, &setting
);
3405 disconnected_tracing
= setting
;
3409 cmd_qtframe (char *own_buf
)
3411 ULONGEST frame
, pc
, lo
, hi
, num
;
3413 struct traceframe
*tframe
;
3414 char *packet
= own_buf
;
3416 packet
+= strlen ("QTFrame:");
3418 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3420 packet
+= strlen ("pc:");
3421 unpack_varlen_hex (packet
, &pc
);
3422 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3423 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3425 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3427 packet
+= strlen ("range:");
3428 packet
= unpack_varlen_hex (packet
, &lo
);
3430 unpack_varlen_hex (packet
, &hi
);
3431 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3432 paddress (lo
), paddress (hi
));
3433 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3435 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3437 packet
+= strlen ("outside:");
3438 packet
= unpack_varlen_hex (packet
, &lo
);
3440 unpack_varlen_hex (packet
, &hi
);
3441 trace_debug ("Want to find next traceframe "
3442 "outside the range 0x%s to 0x%s",
3443 paddress (lo
), paddress (hi
));
3444 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3446 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3448 packet
+= strlen ("tdp:");
3449 unpack_varlen_hex (packet
, &num
);
3451 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3452 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3456 unpack_varlen_hex (packet
, &frame
);
3457 tfnum
= (int) frame
;
3460 trace_debug ("Want to stop looking at traceframes");
3461 current_traceframe
= -1;
3465 trace_debug ("Want to look at traceframe %d", tfnum
);
3466 tframe
= find_traceframe (tfnum
);
3471 current_traceframe
= tfnum
;
3472 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3475 sprintf (own_buf
, "F-1");
3479 cmd_qtstatus (char *packet
)
3481 char *stop_reason_rsp
= NULL
;
3482 char *buf1
, *buf2
, *buf3
, *str
;
3485 /* Translate the plain text of the notes back into hex for
3488 str
= (tracing_user_name
? tracing_user_name
: "");
3489 slen
= strlen (str
);
3490 buf1
= (char *) alloca (slen
* 2 + 1);
3491 hexify (buf1
, str
, slen
);
3493 str
= (tracing_notes
? tracing_notes
: "");
3494 slen
= strlen (str
);
3495 buf2
= (char *) alloca (slen
* 2 + 1);
3496 hexify (buf2
, str
, slen
);
3498 str
= (tracing_stop_note
? tracing_stop_note
: "");
3499 slen
= strlen (str
);
3500 buf3
= (char *) alloca (slen
* 2 + 1);
3501 hexify (buf3
, str
, slen
);
3503 trace_debug ("Returning trace status as %d, stop reason %s",
3504 tracing
, tracing_stop_reason
);
3506 if (agent_loaded_p ())
3510 upload_fast_traceframes ();
3515 stop_reason_rsp
= (char *) tracing_stop_reason
;
3517 /* The user visible error string in terror needs to be hex encoded.
3518 We leave it as plain string in `tracing_stop_reason' to ease
3520 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3522 const char *result_name
;
3526 result_name
= stop_reason_rsp
+ strlen ("terror:");
3527 hexstr_len
= strlen (result_name
) * 2;
3528 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3529 strcpy (p
, "terror:");
3531 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3534 /* If this was a forced stop, include any stop note that was supplied. */
3535 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3537 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3538 strcpy (stop_reason_rsp
, "tstop:");
3539 strcat (stop_reason_rsp
, buf3
);
3545 "tframes:%x;tcreated:%x;"
3546 "tfree:%x;tsize:%s;"
3549 "starttime:%s;stoptime:%s;"
3550 "username:%s:;notes:%s:",
3552 stop_reason_rsp
, tracing_stop_tpnum
,
3553 traceframe_count
, traceframes_created
,
3554 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3555 circular_trace_buffer
,
3556 disconnected_tracing
,
3557 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3562 cmd_qtp (char *own_buf
)
3565 struct tracepoint
*tpoint
;
3566 char *packet
= own_buf
;
3568 packet
+= strlen ("qTP:");
3570 packet
= unpack_varlen_hex (packet
, &num
);
3571 ++packet
; /* skip a colon */
3572 packet
= unpack_varlen_hex (packet
, &addr
);
3574 /* See if we already have this tracepoint. */
3575 tpoint
= find_tracepoint (num
, addr
);
3579 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3580 (int) num
, paddress (addr
));
3581 write_enn (own_buf
);
3585 sprintf (own_buf
, "V%" PRIu64
":%" PRIu64
"", tpoint
->hit_count
,
3586 tpoint
->traceframe_usage
);
3589 /* State variables to help return all the tracepoint bits. */
3590 static struct tracepoint
*cur_tpoint
;
3591 static int cur_action
;
3592 static int cur_step_action
;
3593 static struct source_string
*cur_source_string
;
3594 static struct trace_state_variable
*cur_tsv
;
3596 /* Compose a response that is an imitation of the syntax by which the
3597 tracepoint was originally downloaded. */
3600 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3604 sprintf (packet
, "T%x:%s:%c:%" PRIx64
":%" PRIx64
, tpoint
->number
,
3605 paddress (tpoint
->address
),
3606 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3607 tpoint
->pass_count
);
3608 if (tpoint
->type
== fast_tracepoint
)
3609 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3610 else if (tpoint
->type
== static_tracepoint
)
3611 sprintf (packet
+ strlen (packet
), ":S");
3615 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3616 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3617 tpoint
->cond
->length
, buf
);
3622 /* Compose a response that is an imitation of the syntax by which the
3623 tracepoint action was originally downloaded (with the difference
3624 that due to the way we store the actions, this will output a packet
3625 per action, while GDB could have combined more than one action
3629 response_action (char *packet
, struct tracepoint
*tpoint
,
3630 char *taction
, int step
)
3632 sprintf (packet
, "%c%x:%s:%s",
3633 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3637 /* Compose a response that is an imitation of the syntax by which the
3638 tracepoint source piece was originally downloaded. */
3641 response_source (char *packet
,
3642 struct tracepoint
*tpoint
, struct source_string
*src
)
3647 len
= strlen (src
->str
);
3648 buf
= alloca (len
* 2 + 1);
3649 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3651 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3652 tpoint
->number
, paddress (tpoint
->address
),
3653 src
->type
, 0, len
, buf
);
3656 /* Return the first piece of tracepoint definition, and initialize the
3657 state machine that will iterate through all the tracepoint
3661 cmd_qtfp (char *packet
)
3663 trace_debug ("Returning first tracepoint definition piece");
3665 cur_tpoint
= tracepoints
;
3666 cur_action
= cur_step_action
= -1;
3667 cur_source_string
= NULL
;
3670 response_tracepoint (packet
, cur_tpoint
);
3672 strcpy (packet
, "l");
3675 /* Return additional pieces of tracepoint definition. Each action and
3676 stepping action must go into its own packet, because of packet size
3677 limits, and so we use state variables to deliver one piece at a
3681 cmd_qtsp (char *packet
)
3683 trace_debug ("Returning subsequent tracepoint definition piece");
3687 /* This case would normally never occur, but be prepared for
3689 strcpy (packet
, "l");
3691 else if (cur_action
< cur_tpoint
->numactions
- 1)
3694 response_action (packet
, cur_tpoint
,
3695 cur_tpoint
->actions_str
[cur_action
], 0);
3697 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3700 response_action (packet
, cur_tpoint
,
3701 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3703 else if ((cur_source_string
3704 ? cur_source_string
->next
3705 : cur_tpoint
->source_strings
))
3707 if (cur_source_string
)
3708 cur_source_string
= cur_source_string
->next
;
3710 cur_source_string
= cur_tpoint
->source_strings
;
3711 response_source (packet
, cur_tpoint
, cur_source_string
);
3715 cur_tpoint
= cur_tpoint
->next
;
3716 cur_action
= cur_step_action
= -1;
3717 cur_source_string
= NULL
;
3719 response_tracepoint (packet
, cur_tpoint
);
3721 strcpy (packet
, "l");
3725 /* Compose a response that is an imitation of the syntax by which the
3726 trace state variable was originally downloaded. */
3729 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3731 char *buf
= (char *) "";
3736 namelen
= strlen (tsv
->name
);
3737 buf
= alloca (namelen
* 2 + 1);
3738 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3741 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3742 tsv
->getter
? 1 : 0, buf
);
3745 /* Return the first trace state variable definition, and initialize
3746 the state machine that will iterate through all the tsv bits. */
3749 cmd_qtfv (char *packet
)
3751 trace_debug ("Returning first trace state variable definition");
3753 cur_tsv
= trace_state_variables
;
3756 response_tsv (packet
, cur_tsv
);
3758 strcpy (packet
, "l");
3761 /* Return additional trace state variable definitions. */
3764 cmd_qtsv (char *packet
)
3766 trace_debug ("Returning first trace state variable definition");
3770 /* This case would normally never occur, but be prepared for
3772 strcpy (packet
, "l");
3776 cur_tsv
= cur_tsv
->next
;
3778 response_tsv (packet
, cur_tsv
);
3780 strcpy (packet
, "l");
3783 strcpy (packet
, "l");
3786 /* Return the first static tracepoint marker, and initialize the state
3787 machine that will iterate through all the static tracepoints
3791 cmd_qtfstm (char *packet
)
3793 if (!maybe_write_ipa_ust_not_loaded (packet
))
3794 run_inferior_command (packet
);
3797 /* Return additional static tracepoints markers. */
3800 cmd_qtsstm (char *packet
)
3802 if (!maybe_write_ipa_ust_not_loaded (packet
))
3803 run_inferior_command (packet
);
3806 /* Return the definition of the static tracepoint at a given address.
3807 Result packet is the same as qTsST's. */
3810 cmd_qtstmat (char *packet
)
3812 if (!maybe_write_ipa_ust_not_loaded (packet
))
3813 run_inferior_command (packet
);
3816 /* Return the minimum instruction size needed for fast tracepoints as a
3817 hexadecimal number. */
3820 cmd_qtminftpilen (char *packet
)
3822 if (current_inferior
== NULL
)
3824 /* Indicate that the minimum length is currently unknown. */
3825 strcpy (packet
, "0");
3829 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3832 /* Respond to qTBuffer packet with a block of raw data from the trace
3833 buffer. GDB may ask for a lot, but we are allowed to reply with
3834 only as much as will fit within packet limits or whatever. */
3837 cmd_qtbuffer (char *own_buf
)
3839 ULONGEST offset
, num
, tot
;
3841 char *packet
= own_buf
;
3843 packet
+= strlen ("qTBuffer:");
3845 packet
= unpack_varlen_hex (packet
, &offset
);
3846 ++packet
; /* skip a comma */
3847 unpack_varlen_hex (packet
, &num
);
3849 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3850 (int) num
, pulongest (offset
));
3852 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3854 /* If we're right at the end, reply specially that we're done. */
3857 strcpy (own_buf
, "l");
3861 /* Object to any other out-of-bounds request. */
3864 write_enn (own_buf
);
3868 /* Compute the pointer corresponding to the given offset, accounting
3870 tbp
= trace_buffer_start
+ offset
;
3871 if (tbp
>= trace_buffer_wrap
)
3872 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3874 /* Trim to the remaining bytes if we're close to the end. */
3875 if (num
> tot
- offset
)
3878 /* Trim to available packet size. */
3879 if (num
>= (PBUFSIZ
- 16) / 2 )
3880 num
= (PBUFSIZ
- 16) / 2;
3882 convert_int_to_ascii (tbp
, own_buf
, num
);
3883 own_buf
[num
] = '\0';
3887 cmd_bigqtbuffer_circular (char *own_buf
)
3890 char *packet
= own_buf
;
3892 packet
+= strlen ("QTBuffer:circular:");
3894 unpack_varlen_hex (packet
, &val
);
3895 circular_trace_buffer
= val
;
3896 trace_debug ("Trace buffer is now %s",
3897 circular_trace_buffer
? "circular" : "linear");
3902 cmd_qtnotes (char *own_buf
)
3905 char *saved
, *user
, *notes
, *stopnote
;
3906 char *packet
= own_buf
;
3908 packet
+= strlen ("QTNotes:");
3912 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
3914 packet
+= strlen ("user:");
3916 packet
= strchr (packet
, ';');
3917 nbytes
= (packet
- saved
) / 2;
3918 user
= xmalloc (nbytes
+ 1);
3919 nbytes
= unhexify (user
, saved
, nbytes
);
3920 user
[nbytes
] = '\0';
3921 ++packet
; /* skip the semicolon */
3922 trace_debug ("User is '%s'", user
);
3923 tracing_user_name
= user
;
3925 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
3927 packet
+= strlen ("notes:");
3929 packet
= strchr (packet
, ';');
3930 nbytes
= (packet
- saved
) / 2;
3931 notes
= xmalloc (nbytes
+ 1);
3932 nbytes
= unhexify (notes
, saved
, nbytes
);
3933 notes
[nbytes
] = '\0';
3934 ++packet
; /* skip the semicolon */
3935 trace_debug ("Notes is '%s'", notes
);
3936 tracing_notes
= notes
;
3938 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
3940 packet
+= strlen ("tstop:");
3942 packet
= strchr (packet
, ';');
3943 nbytes
= (packet
- saved
) / 2;
3944 stopnote
= xmalloc (nbytes
+ 1);
3945 nbytes
= unhexify (stopnote
, saved
, nbytes
);
3946 stopnote
[nbytes
] = '\0';
3947 ++packet
; /* skip the semicolon */
3948 trace_debug ("tstop note is '%s'", stopnote
);
3949 tracing_stop_note
= stopnote
;
3959 handle_tracepoint_general_set (char *packet
)
3961 if (strcmp ("QTinit", packet
) == 0)
3963 cmd_qtinit (packet
);
3966 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3971 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3973 cmd_qtdpsrc (packet
);
3976 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3978 cmd_qtenable_disable (packet
, 1);
3981 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3983 cmd_qtenable_disable (packet
, 0);
3986 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3991 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3996 else if (strcmp ("QTStart", packet
) == 0)
3998 cmd_qtstart (packet
);
4001 else if (strcmp ("QTStop", packet
) == 0)
4003 cmd_qtstop (packet
);
4006 else if (strncmp ("QTDisconnected:", packet
,
4007 strlen ("QTDisconnected:")) == 0)
4009 cmd_qtdisconnected (packet
);
4012 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
4014 cmd_qtframe (packet
);
4017 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
4019 cmd_bigqtbuffer_circular (packet
);
4022 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
4024 cmd_qtnotes (packet
);
4032 handle_tracepoint_query (char *packet
)
4034 if (strcmp ("qTStatus", packet
) == 0)
4036 cmd_qtstatus (packet
);
4039 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
4044 else if (strcmp ("qTfP", packet
) == 0)
4049 else if (strcmp ("qTsP", packet
) == 0)
4054 else if (strcmp ("qTfV", packet
) == 0)
4059 else if (strcmp ("qTsV", packet
) == 0)
4064 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
4069 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
4071 cmd_qtbuffer (packet
);
4074 else if (strcmp ("qTfSTM", packet
) == 0)
4076 cmd_qtfstm (packet
);
4079 else if (strcmp ("qTsSTM", packet
) == 0)
4081 cmd_qtsstm (packet
);
4084 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
4086 cmd_qtstmat (packet
);
4089 else if (strcmp ("qTMinFTPILen", packet
) == 0)
4091 cmd_qtminftpilen (packet
);
4099 #ifndef IN_PROCESS_AGENT
4101 /* Call this when thread TINFO has hit the tracepoint defined by
4102 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4103 action. This adds a while-stepping collecting state item to the
4104 threads' collecting state list, so that we can keep track of
4105 multiple simultaneous while-stepping actions being collected by the
4106 same thread. This can happen in cases like:
4108 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4110 ff0003 INSN3 <-- TP2, collect $regs
4111 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4114 Notice that when instruction INSN5 is reached, the while-stepping
4115 actions of both TP1 and TP3 are still being collected, and that TP2
4116 had been collected meanwhile. The whole range of ff0001-ff0005
4117 should be single-stepped, due to at least TP1's while-stepping
4118 action covering the whole range. */
4121 add_while_stepping_state (struct thread_info
*tinfo
,
4122 int tp_number
, CORE_ADDR tp_address
)
4124 struct wstep_state
*wstep
;
4126 wstep
= xmalloc (sizeof (*wstep
));
4127 wstep
->next
= tinfo
->while_stepping
;
4129 wstep
->tp_number
= tp_number
;
4130 wstep
->tp_address
= tp_address
;
4131 wstep
->current_step
= 0;
4133 tinfo
->while_stepping
= wstep
;
4136 /* Release the while-stepping collecting state WSTEP. */
4139 release_while_stepping_state (struct wstep_state
*wstep
)
4144 /* Release all while-stepping collecting states currently associated
4145 with thread TINFO. */
4148 release_while_stepping_state_list (struct thread_info
*tinfo
)
4150 struct wstep_state
*head
;
4152 while (tinfo
->while_stepping
)
4154 head
= tinfo
->while_stepping
;
4155 tinfo
->while_stepping
= head
->next
;
4156 release_while_stepping_state (head
);
4160 /* If TINFO was handling a 'while-stepping' action, the step has
4161 finished, so collect any step data needed, and check if any more
4162 steps are required. Return true if the thread was indeed
4163 collecting tracepoint data, false otherwise. */
4166 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4168 struct tracepoint
*tpoint
;
4169 struct wstep_state
*wstep
;
4170 struct wstep_state
**wstep_link
;
4171 struct trap_tracepoint_ctx ctx
;
4173 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4175 if (agent_loaded_p ())
4176 upload_fast_traceframes ();
4178 /* Check if we were indeed collecting data for one of more
4179 tracepoints with a 'while-stepping' count. */
4180 if (tinfo
->while_stepping
== NULL
)
4185 /* We're not even tracing anymore. Stop this thread from
4187 release_while_stepping_state_list (tinfo
);
4189 /* The thread had stopped due to a single-step request indeed
4190 explained by a tracepoint. */
4194 wstep
= tinfo
->while_stepping
;
4195 wstep_link
= &tinfo
->while_stepping
;
4197 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4198 target_pid_to_str (tinfo
->entry
.id
),
4199 wstep
->tp_number
, paddress (wstep
->tp_address
));
4201 ctx
.base
.type
= trap_tracepoint
;
4202 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4204 while (wstep
!= NULL
)
4206 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4209 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4210 wstep
->tp_number
, paddress (wstep
->tp_address
),
4211 target_pid_to_str (tinfo
->entry
.id
));
4214 *wstep_link
= wstep
->next
;
4215 release_while_stepping_state (wstep
);
4216 wstep
= *wstep_link
;
4220 /* We've just finished one step. */
4221 ++wstep
->current_step
;
4224 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4225 stop_pc
, tpoint
, wstep
->current_step
);
4227 if (wstep
->current_step
>= tpoint
->step_count
)
4229 /* The requested numbers of steps have occurred. */
4230 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4231 target_pid_to_str (tinfo
->entry
.id
),
4232 wstep
->tp_number
, paddress (wstep
->tp_address
));
4234 /* Unlink the wstep. */
4235 *wstep_link
= wstep
->next
;
4236 release_while_stepping_state (wstep
);
4237 wstep
= *wstep_link
;
4239 /* Only check the hit count now, which ensure that we do all
4240 our stepping before stopping the run. */
4241 if (tpoint
->pass_count
> 0
4242 && tpoint
->hit_count
>= tpoint
->pass_count
4243 && stopping_tracepoint
== NULL
)
4244 stopping_tracepoint
= tpoint
;
4248 /* Keep single-stepping until the requested numbers of steps
4250 wstep_link
= &wstep
->next
;
4251 wstep
= *wstep_link
;
4254 if (stopping_tracepoint
4255 || trace_buffer_is_full
4256 || expr_eval_result
!= expr_eval_no_error
)
4266 /* Handle any internal tracing control breakpoint hits. That means,
4267 pull traceframes from the IPA to our buffer, and syncing both
4268 tracing agents when the IPA's tracing stops for some reason. */
4271 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4273 /* Pull in fast tracepoint trace frames from the inferior in-process
4274 agent's buffer into our buffer. */
4276 if (!agent_loaded_p ())
4279 upload_fast_traceframes ();
4281 /* Check if the in-process agent had decided we should stop
4283 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4285 int ipa_trace_buffer_is_full
;
4286 CORE_ADDR ipa_stopping_tracepoint
;
4287 int ipa_expr_eval_result
;
4288 CORE_ADDR ipa_error_tracepoint
;
4290 trace_debug ("lib stopped at stop_tracing");
4292 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4293 &ipa_trace_buffer_is_full
);
4295 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4296 &ipa_stopping_tracepoint
);
4297 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4299 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4300 &ipa_error_tracepoint
);
4301 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4303 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4304 &ipa_expr_eval_result
);
4305 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4307 trace_debug ("lib: trace_buffer_is_full: %d, "
4308 "stopping_tracepoint: %s, "
4309 "ipa_expr_eval_result: %d, "
4310 "error_tracepoint: %s, ",
4311 ipa_trace_buffer_is_full
,
4312 paddress (ipa_stopping_tracepoint
),
4313 ipa_expr_eval_result
,
4314 paddress (ipa_error_tracepoint
));
4318 if (ipa_trace_buffer_is_full
)
4319 trace_debug ("lib stopped due to full buffer.");
4320 if (ipa_stopping_tracepoint
)
4321 trace_debug ("lib stopped due to tpoint");
4322 if (ipa_stopping_tracepoint
)
4323 trace_debug ("lib stopped due to error");
4326 if (ipa_stopping_tracepoint
!= 0)
4329 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4331 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4333 expr_eval_result
= ipa_expr_eval_result
;
4335 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4340 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4342 trace_debug ("lib stopped at flush_trace_buffer");
4349 /* Return true if TINFO just hit a tracepoint. Collect data if
4353 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4355 struct tracepoint
*tpoint
;
4357 struct trap_tracepoint_ctx ctx
;
4359 /* Not tracing, don't handle. */
4363 ctx
.base
.type
= trap_tracepoint
;
4364 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4366 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4368 /* Note that we collect fast tracepoints here as well. We'll
4369 step over the fast tracepoint jump later, which avoids the
4370 double collect. However, we don't collect for static
4371 tracepoints here, because UST markers are compiled in program,
4372 and probes will be executed in program. So static tracepoints
4373 are collected there. */
4374 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4375 && tpoint
->type
!= static_tracepoint
)
4377 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4378 target_pid_to_str (tinfo
->entry
.id
),
4379 tpoint
->number
, paddress (tpoint
->address
));
4381 /* Test the condition if present, and collect if true. */
4383 || (condition_true_at_tracepoint
4384 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4385 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4388 if (stopping_tracepoint
4389 || trace_buffer_is_full
4390 || expr_eval_result
!= expr_eval_no_error
)
4394 /* If the tracepoint had a 'while-stepping' action, then set
4395 the thread to collect this tracepoint on the following
4397 else if (tpoint
->step_count
> 0)
4399 add_while_stepping_state (tinfo
,
4400 tpoint
->number
, tpoint
->address
);
4412 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4413 struct ust_marker_data
;
4414 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4415 struct traceframe
*tframe
);
4418 /* Create a trace frame for the hit of the given tracepoint in the
4422 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4423 struct tracepoint
*tpoint
)
4425 struct traceframe
*tframe
;
4428 /* Only count it as a hit when we actually collect data. */
4429 tpoint
->hit_count
++;
4431 /* If we've exceeded a defined pass count, record the event for
4432 later, and finish the collection for this hit. This test is only
4433 for nonstepping tracepoints, stepping tracepoints test at the end
4434 of their while-stepping loop. */
4435 if (tpoint
->pass_count
> 0
4436 && tpoint
->hit_count
>= tpoint
->pass_count
4437 && tpoint
->step_count
== 0
4438 && stopping_tracepoint
== NULL
)
4439 stopping_tracepoint
= tpoint
;
4441 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %" PRIu64
,
4442 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4444 tframe
= add_traceframe (tpoint
);
4448 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4450 #ifndef IN_PROCESS_AGENT
4451 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4452 tpoint
->number
, paddress (tpoint
->address
),
4453 tpoint
->actions_str
[acti
]);
4456 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4457 tpoint
->actions
[acti
]);
4460 finish_traceframe (tframe
);
4463 if (tframe
== NULL
&& tracing
)
4464 trace_buffer_is_full
= 1;
4467 #ifndef IN_PROCESS_AGENT
4470 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4472 struct tracepoint
*tpoint
, int current_step
)
4474 struct traceframe
*tframe
;
4477 trace_debug ("Making new step traceframe for "
4478 "tracepoint %d at 0x%s, step %d of %" PRIu64
", hit %" PRIu64
,
4479 tpoint
->number
, paddress (tpoint
->address
),
4480 current_step
, tpoint
->step_count
,
4483 tframe
= add_traceframe (tpoint
);
4487 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4489 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4490 tpoint
->number
, paddress (tpoint
->address
),
4491 tpoint
->step_actions_str
[acti
]);
4493 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4494 tpoint
->step_actions
[acti
]);
4497 finish_traceframe (tframe
);
4500 if (tframe
== NULL
&& tracing
)
4501 trace_buffer_is_full
= 1;
4506 static struct regcache
*
4507 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4509 struct regcache
*regcache
= NULL
;
4511 #ifdef IN_PROCESS_AGENT
4512 if (ctx
->type
== fast_tracepoint
)
4514 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4515 if (!fctx
->regcache_initted
)
4517 fctx
->regcache_initted
= 1;
4518 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4519 supply_regblock (&fctx
->regcache
, NULL
);
4520 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4522 regcache
= &fctx
->regcache
;
4525 if (ctx
->type
== static_tracepoint
)
4527 struct static_tracepoint_ctx
*sctx
4528 = (struct static_tracepoint_ctx
*) ctx
;
4530 if (!sctx
->regcache_initted
)
4532 sctx
->regcache_initted
= 1;
4533 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4534 supply_regblock (&sctx
->regcache
, NULL
);
4535 /* Pass down the tracepoint address, because REGS doesn't
4536 include the PC, but we know what it must have been. */
4537 supply_static_tracepoint_registers (&sctx
->regcache
,
4538 (const unsigned char *)
4540 sctx
->tpoint
->address
);
4542 regcache
= &sctx
->regcache
;
4546 if (ctx
->type
== trap_tracepoint
)
4548 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4549 regcache
= tctx
->regcache
;
4553 gdb_assert (regcache
!= NULL
);
4559 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4561 struct tracepoint
*tpoint
,
4562 struct traceframe
*tframe
,
4563 struct tracepoint_action
*taction
)
4565 enum eval_result_type err
;
4567 switch (taction
->type
)
4571 struct collect_memory_action
*maction
;
4573 maction
= (struct collect_memory_action
*) taction
;
4575 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4576 pulongest (maction
->len
),
4577 paddress (maction
->addr
), maction
->basereg
);
4578 /* (should use basereg) */
4579 agent_mem_read (tframe
, NULL
,
4580 (CORE_ADDR
) maction
->addr
, maction
->len
);
4585 unsigned char *regspace
;
4586 struct regcache tregcache
;
4587 struct regcache
*context_regcache
;
4590 trace_debug ("Want to collect registers");
4592 /* Collect all registers for now. */
4593 regspace
= add_traceframe_block (tframe
,
4594 1 + register_cache_size ());
4595 if (regspace
== NULL
)
4597 trace_debug ("Trace buffer block allocation failed, skipping");
4600 /* Identify a register block. */
4603 context_regcache
= get_context_regcache (ctx
);
4605 /* Wrap the regblock in a register cache (in the stack, we
4606 don't want to malloc here). */
4607 init_register_cache (&tregcache
, regspace
+ 1);
4609 /* Copy the register data to the regblock. */
4610 regcache_cpy (&tregcache
, context_regcache
);
4612 #ifndef IN_PROCESS_AGENT
4613 /* On some platforms, trap-based tracepoints will have the PC
4614 pointing to the next instruction after the trap, but we
4615 don't want the user or GDB trying to guess whether the
4616 saved PC needs adjusting; so always record the adjusted
4617 stop_pc. Note that we can't use tpoint->address instead,
4618 since it will be wrong for while-stepping actions. This
4619 adjustment is a nop for fast tracepoints collected from the
4620 in-process lib (but not if GDBserver is collecting one
4621 preemptively), since the PC had already been adjusted to
4622 contain the tracepoint's address by the jump pad. */
4623 trace_debug ("Storing stop pc (0x%s) in regblock",
4624 paddress (stop_pc
));
4626 /* This changes the regblock, not the thread's
4628 regcache_write_pc (&tregcache
, stop_pc
);
4634 struct eval_expr_action
*eaction
;
4636 eaction
= (struct eval_expr_action
*) taction
;
4638 trace_debug ("Want to evaluate expression");
4640 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4642 if (err
!= expr_eval_no_error
)
4644 record_tracepoint_error (tpoint
, "action expression", err
);
4651 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4652 trace_debug ("Want to collect static trace data");
4653 collect_ust_data_at_tracepoint (ctx
, tframe
);
4655 trace_debug ("warning: collecting static trace data, "
4656 "but static tracepoints are not supported");
4661 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4667 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4668 struct tracepoint
*tpoint
)
4671 enum eval_result_type err
;
4673 /* Presently, gdbserver doesn't run compiled conditions, only the
4674 IPA does. If the program stops at a fast tracepoint's address
4675 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4676 gdbserver preemptively collect the fast tracepoint. Later, on
4677 resume, gdbserver steps over the fast tracepoint like it steps
4678 over breakpoints, so that the IPA doesn't see that fast
4679 tracepoint. This avoids double collects of fast tracepoints in
4680 that stopping scenario. Having gdbserver itself handle the fast
4681 tracepoint gives the user a consistent view of when fast or trap
4682 tracepoints are collected, compared to an alternative where only
4683 trap tracepoints are collected on stop, and fast tracepoints on
4684 resume. When a fast tracepoint is being processed by gdbserver,
4685 it is always the non-compiled condition expression that is
4687 #ifdef IN_PROCESS_AGENT
4688 if (tpoint
->compiled_cond
)
4689 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4692 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4694 if (err
!= expr_eval_no_error
)
4696 record_tracepoint_error (tpoint
, "condition", err
);
4697 /* The error case must return false. */
4701 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4702 tpoint
->number
, paddress (tpoint
->address
),
4704 return (value
? 1 : 0);
4707 /* Evaluates a tracepoint agent expression with context CTX,
4708 traceframe TFRAME, agent expression AEXPR and store the
4711 static enum eval_result_type
4712 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4713 struct traceframe
*tframe
,
4714 struct agent_expr
*aexpr
,
4717 struct regcache
*regcache
;
4718 regcache
= get_context_regcache (ctx
);
4720 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4723 /* Do memory copies for bytecodes. */
4724 /* Do the recording of memory blocks for actions and bytecodes. */
4727 agent_mem_read (struct traceframe
*tframe
,
4728 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4730 unsigned char *mspace
;
4731 ULONGEST remaining
= len
;
4732 unsigned short blocklen
;
4734 /* If a 'to' buffer is specified, use it. */
4737 read_inferior_memory (from
, to
, len
);
4741 /* Otherwise, create a new memory block in the trace buffer. */
4742 while (remaining
> 0)
4746 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4747 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4748 mspace
= add_traceframe_block (tframe
, sp
);
4751 /* Identify block as a memory block. */
4754 /* Record address and size. */
4755 memcpy (mspace
, &from
, sizeof (from
));
4756 mspace
+= sizeof (from
);
4757 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4758 mspace
+= sizeof (blocklen
);
4759 /* Record the memory block proper. */
4760 read_inferior_memory (from
, mspace
, blocklen
);
4761 trace_debug ("%d bytes recorded", blocklen
);
4762 remaining
-= blocklen
;
4769 agent_mem_read_string (struct traceframe
*tframe
,
4770 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4772 unsigned char *buf
, *mspace
;
4773 ULONGEST remaining
= len
;
4774 unsigned short blocklen
, i
;
4776 /* To save a bit of space, block lengths are 16-bit, so break large
4777 requests into multiple blocks. Bordering on overkill for strings,
4778 but it could happen that someone specifies a large max length. */
4779 while (remaining
> 0)
4783 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4784 /* We want working space to accumulate nonzero bytes, since
4785 traceframes must have a predecided size (otherwise it gets
4786 harder to wrap correctly for the circular case, etc). */
4787 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4788 for (i
= 0; i
< blocklen
; ++i
)
4790 /* Read the string one byte at a time, in case the string is
4791 at the end of a valid memory area - we don't want a
4792 correctly-terminated string to engender segvio
4794 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4799 /* Make sure outer loop stops now too. */
4800 remaining
= blocklen
;
4804 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4805 mspace
= add_traceframe_block (tframe
, sp
);
4811 /* Identify block as a memory block. */
4814 /* Record address and size. */
4815 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4816 mspace
+= sizeof (from
);
4817 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4818 mspace
+= sizeof (blocklen
);
4819 /* Copy the string contents. */
4820 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4821 remaining
-= blocklen
;
4828 /* Record the value of a trace state variable. */
4831 agent_tsv_read (struct traceframe
*tframe
, int n
)
4833 unsigned char *vspace
;
4836 vspace
= add_traceframe_block (tframe
,
4837 1 + sizeof (n
) + sizeof (LONGEST
));
4840 /* Identify block as a variable. */
4842 /* Record variable's number and value. */
4843 memcpy (vspace
+ 1, &n
, sizeof (n
));
4844 val
= get_trace_state_variable_value (n
);
4845 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4846 trace_debug ("Variable %d recorded", n
);
4850 #ifndef IN_PROCESS_AGENT
4852 /* Callback for traceframe_walk_blocks, used to find a given block
4853 type in a traceframe. */
4856 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4858 char *wantedp
= data
;
4860 if (*wantedp
== blocktype
)
4866 /* Walk over all traceframe blocks of the traceframe buffer starting
4867 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4868 block found, passing in DATA unmodified. If CALLBACK returns true,
4869 this returns a pointer to where the block is found. Returns NULL
4870 if no callback call returned true, indicating that all blocks have
4873 static unsigned char *
4874 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4876 int (*callback
) (char blocktype
,
4877 unsigned char *dataptr
,
4881 unsigned char *dataptr
;
4885 trace_debug ("traceframe %d has no data", tfnum
);
4889 /* Iterate through a traceframe's blocks, looking for a block of the
4891 for (dataptr
= database
;
4892 dataptr
< database
+ datasize
;
4896 unsigned short mlen
;
4898 if (dataptr
== trace_buffer_wrap
)
4900 /* Adjust to reflect wrapping part of the frame around to
4902 datasize
= dataptr
- database
;
4903 dataptr
= database
= trace_buffer_lo
;
4906 blocktype
= *dataptr
++;
4908 if ((*callback
) (blocktype
, dataptr
, data
))
4914 /* Skip over the registers block. */
4915 dataptr
+= register_cache_size ();
4918 /* Skip over the memory block. */
4919 dataptr
+= sizeof (CORE_ADDR
);
4920 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4921 dataptr
+= (sizeof (mlen
) + mlen
);
4924 /* Skip over the TSV block. */
4925 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4928 /* Skip over the static trace data block. */
4929 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4930 dataptr
+= (sizeof (mlen
) + mlen
);
4933 trace_debug ("traceframe %d has unknown block type 0x%x",
4942 /* Look for the block of type TYPE_WANTED in the trameframe starting
4943 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4946 static unsigned char *
4947 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4948 int tfnum
, char type_wanted
)
4950 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4951 match_blocktype
, &type_wanted
);
4954 static unsigned char *
4955 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4957 unsigned char *regblock
;
4959 regblock
= traceframe_find_block_type (tframe
->data
,
4963 if (regblock
== NULL
)
4964 trace_debug ("traceframe %d has no register data", tfnum
);
4969 /* Get registers from a traceframe. */
4972 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4974 unsigned char *dataptr
;
4975 struct tracepoint
*tpoint
;
4976 struct traceframe
*tframe
;
4978 tframe
= find_traceframe (tfnum
);
4982 trace_debug ("traceframe %d not found", tfnum
);
4986 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4987 if (dataptr
== NULL
)
4989 /* Mark registers unavailable. */
4990 supply_regblock (regcache
, NULL
);
4992 /* We can generally guess at a PC, although this will be
4993 misleading for while-stepping frames and multi-location
4995 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4997 regcache_write_pc (regcache
, tpoint
->address
);
5000 supply_regblock (regcache
, dataptr
);
5006 traceframe_get_pc (struct traceframe
*tframe
)
5008 struct regcache regcache
;
5009 unsigned char *dataptr
;
5011 dataptr
= traceframe_find_regblock (tframe
, -1);
5012 if (dataptr
== NULL
)
5015 init_register_cache (®cache
, dataptr
);
5016 return regcache_read_pc (®cache
);
5019 /* Read a requested block of memory from a trace frame. */
5022 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
5023 unsigned char *buf
, ULONGEST length
,
5026 struct traceframe
*tframe
;
5027 unsigned char *database
, *dataptr
;
5028 unsigned int datasize
;
5030 unsigned short mlen
;
5032 trace_debug ("traceframe_read_mem");
5034 tframe
= find_traceframe (tfnum
);
5038 trace_debug ("traceframe %d not found", tfnum
);
5042 datasize
= tframe
->data_size
;
5043 database
= dataptr
= &tframe
->data
[0];
5045 /* Iterate through a traceframe's blocks, looking for memory. */
5046 while ((dataptr
= traceframe_find_block_type (dataptr
,
5048 - (dataptr
- database
),
5049 tfnum
, 'M')) != NULL
)
5051 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5052 dataptr
+= sizeof (maddr
);
5053 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5054 dataptr
+= sizeof (mlen
);
5055 trace_debug ("traceframe %d has %d bytes at %s",
5056 tfnum
, mlen
, paddress (maddr
));
5058 /* If the block includes the first part of the desired range,
5059 return as much it has; GDB will re-request the remainder,
5060 which might be in a different block of this trace frame. */
5061 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
5063 ULONGEST amt
= (maddr
+ mlen
) - addr
;
5067 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
5072 /* Skip over this block. */
5076 trace_debug ("traceframe %d has no memory data for the desired region",
5084 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
5087 struct traceframe
*tframe
;
5088 unsigned char *database
, *dataptr
;
5089 unsigned int datasize
;
5092 trace_debug ("traceframe_read_tsv");
5094 tfnum
= current_traceframe
;
5098 trace_debug ("no current traceframe");
5102 tframe
= find_traceframe (tfnum
);
5106 trace_debug ("traceframe %d not found", tfnum
);
5110 datasize
= tframe
->data_size
;
5111 database
= dataptr
= &tframe
->data
[0];
5113 /* Iterate through a traceframe's blocks, looking for the tsv. */
5114 while ((dataptr
= traceframe_find_block_type (dataptr
,
5116 - (dataptr
- database
),
5117 tfnum
, 'V')) != NULL
)
5119 memcpy (&vnum
, dataptr
, sizeof (vnum
));
5120 dataptr
+= sizeof (vnum
);
5122 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
5124 /* Check that this is the variable we want. */
5127 memcpy (val
, dataptr
, sizeof (*val
));
5131 /* Skip over this block. */
5132 dataptr
+= sizeof (LONGEST
);
5135 trace_debug ("traceframe %d has no data for variable %d",
5140 /* Read a requested block of static tracepoint data from a trace
5144 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5145 unsigned char *buf
, ULONGEST length
,
5148 struct traceframe
*tframe
;
5149 unsigned char *database
, *dataptr
;
5150 unsigned int datasize
;
5151 unsigned short mlen
;
5153 trace_debug ("traceframe_read_sdata");
5155 tframe
= find_traceframe (tfnum
);
5159 trace_debug ("traceframe %d not found", tfnum
);
5163 datasize
= tframe
->data_size
;
5164 database
= &tframe
->data
[0];
5166 /* Iterate through a traceframe's blocks, looking for static
5168 dataptr
= traceframe_find_block_type (database
, datasize
,
5170 if (dataptr
!= NULL
)
5172 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5173 dataptr
+= sizeof (mlen
);
5176 if (offset
+ length
> mlen
)
5177 length
= mlen
- offset
;
5179 memcpy (buf
, dataptr
, length
);
5187 trace_debug ("traceframe %d has no static trace data", tfnum
);
5193 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5194 object. DATA is pointer to a struct buffer holding the
5195 traceframe-info object being built. */
5198 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5200 struct buffer
*buffer
= data
;
5206 unsigned short mlen
;
5209 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5210 dataptr
+= sizeof (maddr
);
5211 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5212 dataptr
+= sizeof (mlen
);
5213 buffer_xml_printf (buffer
,
5214 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5215 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5225 warning ("Unhandled trace block type (%d) '%c ' "
5226 "while building trace frame info.",
5227 blocktype
, blocktype
);
5234 /* Build a traceframe-info object for traceframe number TFNUM into
5238 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5240 struct traceframe
*tframe
;
5242 trace_debug ("traceframe_read_info");
5244 tframe
= find_traceframe (tfnum
);
5248 trace_debug ("traceframe %d not found", tfnum
);
5252 buffer_grow_str (buffer
, "<traceframe-info>\n");
5253 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5254 tfnum
, build_traceframe_info_xml
, buffer
);
5255 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5259 /* Return the first fast tracepoint whose jump pad contains PC. */
5261 static struct tracepoint
*
5262 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5264 struct tracepoint
*tpoint
;
5266 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5267 if (tpoint
->type
== fast_tracepoint
)
5268 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5274 /* Return the first fast tracepoint whose trampoline contains PC. */
5276 static struct tracepoint
*
5277 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5279 struct tracepoint
*tpoint
;
5281 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5283 if (tpoint
->type
== fast_tracepoint
5284 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5291 /* Return GDBserver's tracepoint that matches the IP Agent's
5292 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5295 static struct tracepoint
*
5296 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5298 struct tracepoint
*tpoint
;
5300 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5301 if (tpoint
->type
== fast_tracepoint
)
5302 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5310 /* The type of the object that is used to synchronize fast tracepoint
5313 typedef struct collecting_t
5315 /* The fast tracepoint number currently collecting. */
5318 /* A number that GDBserver can use to identify the thread that is
5319 presently holding the collect lock. This need not (and usually
5320 is not) the thread id, as getting the current thread ID usually
5321 requires a system call, which we want to avoid like the plague.
5322 Usually this is thread's TCB, found in the TLS (pseudo-)
5323 register, which is readable with a single insn on several
5325 uintptr_t thread_area
;
5328 #ifndef IN_PROCESS_AGENT
5331 force_unlock_trace_buffer (void)
5333 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5336 /* Check if the thread identified by THREAD_AREA which is stopped at
5337 STOP_PC, is presently locking the fast tracepoint collection, and
5338 if so, gather some status of said collection. Returns 0 if the
5339 thread isn't collecting or in the jump pad at all. 1, if in the
5340 jump pad (or within gdb_collect) and hasn't executed the adjusted
5341 original insn yet (can set a breakpoint there and run to it). 2,
5342 if presently executing the adjusted original insn --- in which
5343 case, if we want to move the thread out of the jump pad, we need to
5344 single-step it until this function returns 0. */
5347 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5349 struct fast_tpoint_collect_status
*status
)
5351 CORE_ADDR ipa_collecting
;
5352 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5353 CORE_ADDR ipa_gdb_trampoline_buffer
;
5354 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5355 struct tracepoint
*tpoint
;
5356 int needs_breakpoint
;
5358 /* The thread THREAD_AREA is either:
5360 0. not collecting at all, not within the jump pad, or within
5361 gdb_collect or one of its callees.
5363 1. in the jump pad and haven't reached gdb_collect
5365 2. within gdb_collect (out of the jump pad) (collect is set)
5367 3. we're in the jump pad, after gdb_collect having returned,
5368 possibly executing the adjusted insns.
5370 For cases 1 and 3, `collecting' may or not be set. The jump pad
5371 doesn't have any complicated jump logic, so we can tell if the
5372 thread is executing the adjust original insn or not by just
5373 matching STOP_PC with known jump pad addresses. If we it isn't
5374 yet executing the original insn, set a breakpoint there, and let
5375 the thread run to it, so to quickly step over a possible (many
5376 insns) gdb_collect call. Otherwise, or when the breakpoint is
5377 hit, only a few (small number of) insns are left to be executed
5378 in the jump pad. Single-step the thread until it leaves the
5383 needs_breakpoint
= 0;
5384 trace_debug ("fast_tracepoint_collecting");
5386 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5387 &ipa_gdb_jump_pad_buffer
))
5388 fatal ("error extracting `gdb_jump_pad_buffer'");
5389 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5390 &ipa_gdb_jump_pad_buffer_end
))
5391 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5393 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5394 &ipa_gdb_trampoline_buffer
))
5395 fatal ("error extracting `gdb_trampoline_buffer'");
5396 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5397 &ipa_gdb_trampoline_buffer_end
))
5398 fatal ("error extracting `gdb_trampoline_buffer_end'");
5400 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5401 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5403 /* We can tell which tracepoint(s) the thread is collecting by
5404 matching the jump pad address back to the tracepoint. */
5405 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5408 warning ("in jump pad, but no matching tpoint?");
5413 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5415 tpoint
->number
, paddress (tpoint
->address
),
5416 paddress (tpoint
->jump_pad
),
5417 paddress (tpoint
->jump_pad_end
),
5418 paddress (tpoint
->adjusted_insn_addr
),
5419 paddress (tpoint
->adjusted_insn_addr_end
));
5422 /* Definitely in the jump pad. May or may not need
5423 fast-exit-jump-pad breakpoint. */
5424 if (tpoint
->jump_pad
<= stop_pc
5425 && stop_pc
< tpoint
->adjusted_insn_addr
)
5426 needs_breakpoint
= 1;
5428 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5429 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5431 /* We can tell which tracepoint(s) the thread is collecting by
5432 matching the trampoline address back to the tracepoint. */
5433 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5436 warning ("in trampoline, but no matching tpoint?");
5441 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5442 tpoint
->number
, paddress (tpoint
->address
),
5443 paddress (tpoint
->trampoline
),
5444 paddress (tpoint
->trampoline_end
));
5447 /* Have not reached jump pad yet, but treat the trampoline as a
5448 part of the jump pad that is before the adjusted original
5450 needs_breakpoint
= 1;
5454 collecting_t ipa_collecting_obj
;
5456 /* If `collecting' is set/locked, then the THREAD_AREA thread
5457 may or not be the one holding the lock. We have to read the
5458 lock to find out. */
5460 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5463 trace_debug ("fast_tracepoint_collecting:"
5464 " failed reading 'collecting' in the inferior");
5468 if (!ipa_collecting
)
5470 trace_debug ("fast_tracepoint_collecting: not collecting"
5471 " (and nobody is).");
5475 /* Some thread is collecting. Check which. */
5476 if (read_inferior_memory (ipa_collecting
,
5477 (unsigned char *) &ipa_collecting_obj
,
5478 sizeof (ipa_collecting_obj
)) != 0)
5481 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5483 trace_debug ("fast_tracepoint_collecting: not collecting "
5484 "(another thread is)");
5489 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5492 warning ("fast_tracepoint_collecting: collecting, "
5493 "but tpoint %s not found?",
5494 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5498 /* The thread is within `gdb_collect', skip over the rest of
5499 fast tracepoint collection quickly using a breakpoint. */
5500 needs_breakpoint
= 1;
5503 /* The caller wants a bit of status detail. */
5506 status
->tpoint_num
= tpoint
->number
;
5507 status
->tpoint_addr
= tpoint
->address
;
5508 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5509 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5512 if (needs_breakpoint
)
5514 /* Hasn't executed the original instruction yet. Set breakpoint
5515 there, and wait till it's hit, then single-step until exiting
5519 fast_tracepoint_collecting, returning continue-until-break at %s",
5520 paddress (tpoint
->adjusted_insn_addr
));
5522 return 1; /* continue */
5526 /* Just single-step until exiting the jump pad. */
5528 trace_debug ("fast_tracepoint_collecting, returning "
5529 "need-single-step (%s-%s)",
5530 paddress (tpoint
->adjusted_insn_addr
),
5531 paddress (tpoint
->adjusted_insn_addr_end
));
5533 return 2; /* single-step */
5539 #ifdef IN_PROCESS_AGENT
5541 /* The global fast tracepoint collect lock. Points to a collecting_t
5542 object built on the stack by the jump pad, if presently locked;
5543 NULL if it isn't locked. Note that this lock *must* be set while
5544 executing any *function other than the jump pad. See
5545 fast_tracepoint_collecting. */
5546 static collecting_t
* ATTR_USED collecting
;
5548 /* This routine, called from the jump pad (in asm) is designed to be
5549 called from the jump pads of fast tracepoints, thus it is on the
5552 IP_AGENT_EXPORT
void ATTR_USED
5553 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5555 struct fast_tracepoint_ctx ctx
;
5557 /* Don't do anything until the trace run is completely set up. */
5561 ctx
.base
.type
= fast_tracepoint
;
5563 ctx
.regcache_initted
= 0;
5564 /* Wrap the regblock in a register cache (in the stack, we don't
5565 want to malloc here). */
5566 ctx
.regspace
= alloca (register_cache_size ());
5567 if (ctx
.regspace
== NULL
)
5569 trace_debug ("Trace buffer block allocation failed, skipping");
5573 for (ctx
.tpoint
= tpoint
;
5574 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5575 ctx
.tpoint
= ctx
.tpoint
->next
)
5577 if (!ctx
.tpoint
->enabled
)
5580 /* Multiple tracepoints of different types, such as fast tracepoint and
5581 static tracepoint, can be set at the same address. */
5582 if (ctx
.tpoint
->type
!= tpoint
->type
)
5585 /* Test the condition if present, and collect if true. */
5586 if (ctx
.tpoint
->cond
== NULL
5587 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5590 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5591 ctx
.tpoint
->address
, ctx
.tpoint
);
5593 /* Note that this will cause original insns to be written back
5594 to where we jumped from, but that's OK because we're jumping
5595 back to the next whole instruction. This will go badly if
5596 instruction restoration is not atomic though. */
5597 if (stopping_tracepoint
5598 || trace_buffer_is_full
5599 || expr_eval_result
!= expr_eval_no_error
)
5607 /* If there was a condition and it evaluated to false, the only
5608 way we would stop tracing is if there was an error during
5609 condition expression evaluation. */
5610 if (expr_eval_result
!= expr_eval_no_error
)
5621 #ifndef IN_PROCESS_AGENT
5624 get_raw_reg_func_addr (void)
5626 return ipa_sym_addrs
.addr_get_raw_reg
;
5630 get_get_tsv_func_addr (void)
5632 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5636 get_set_tsv_func_addr (void)
5638 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5642 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5643 CORE_ADDR
*jump_entry
)
5645 CORE_ADDR entry_point
= *jump_entry
;
5646 enum eval_result_type err
;
5648 trace_debug ("Starting condition compilation for tracepoint %d\n",
5651 /* Initialize the global pointer to the code being built. */
5652 current_insn_ptr
= *jump_entry
;
5656 err
= compile_bytecodes (tpoint
->cond
);
5658 if (err
== expr_eval_no_error
)
5662 /* Record the beginning of the compiled code. */
5663 tpoint
->compiled_cond
= entry_point
;
5665 trace_debug ("Condition compilation for tracepoint %d complete\n",
5670 /* Leave the unfinished code in situ, but don't point to it. */
5672 tpoint
->compiled_cond
= 0;
5674 trace_debug ("Condition compilation for tracepoint %d failed, "
5676 tpoint
->number
, err
);
5679 /* Update the code pointer passed in. Note that we do this even if
5680 the compile fails, so that we can look at the partial results
5681 instead of letting them be overwritten. */
5682 *jump_entry
= current_insn_ptr
;
5684 /* Leave a gap, to aid dump decipherment. */
5688 /* We'll need to adjust these when we consider bi-arch setups, and big
5692 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5694 return write_inferior_memory (where
,
5695 (unsigned char *) &ptr
, sizeof (void *));
5698 /* The base pointer of the IPA's heap. This is the only memory the
5699 IPA is allowed to use. The IPA should _not_ call the inferior's
5700 `malloc' during operation. That'd be slow, and, most importantly,
5701 it may not be safe. We may be collecting a tracepoint in a signal
5702 handler, for example. */
5703 static CORE_ADDR target_tp_heap
;
5705 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5709 target_malloc (ULONGEST size
)
5713 if (target_tp_heap
== 0)
5715 /* We have the pointer *address*, need what it points to. */
5716 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5718 fatal ("could get target heap head pointer");
5721 ptr
= target_tp_heap
;
5722 target_tp_heap
+= size
;
5724 /* Pad to 8-byte alignment. */
5725 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5731 download_agent_expr (struct agent_expr
*expr
)
5733 CORE_ADDR expr_addr
;
5734 CORE_ADDR expr_bytes
;
5736 expr_addr
= target_malloc (sizeof (*expr
));
5737 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5739 expr_bytes
= target_malloc (expr
->length
);
5740 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5742 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5747 /* Align V up to N bits. */
5748 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5750 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5753 download_tracepoint_1 (struct tracepoint
*tpoint
)
5755 struct tracepoint target_tracepoint
;
5756 CORE_ADDR tpptr
= 0;
5758 gdb_assert (tpoint
->type
== fast_tracepoint
5759 || tpoint
->type
== static_tracepoint
);
5761 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5763 CORE_ADDR jentry
, jump_entry
;
5765 jentry
= jump_entry
= get_jump_space_head ();
5767 if (tpoint
->cond
!= NULL
)
5769 /* Pad to 8-byte alignment. (needed?) */
5770 /* Actually this should be left for the target to
5772 jentry
= UALIGN (jentry
, 8);
5774 compile_tracepoint_condition (tpoint
, &jentry
);
5777 /* Pad to 8-byte alignment. */
5778 jentry
= UALIGN (jentry
, 8);
5779 claim_jump_space (jentry
- jump_entry
);
5782 target_tracepoint
= *tpoint
;
5784 tpptr
= target_malloc (sizeof (*tpoint
));
5785 tpoint
->obj_addr_on_target
= tpptr
;
5787 /* Write the whole object. We'll fix up its pointers in a bit.
5788 Assume no next for now. This is fixed up above on the next
5789 iteration, if there's any. */
5790 target_tracepoint
.next
= NULL
;
5791 /* Need to clear this here too, since we're downloading the
5792 tracepoints before clearing our own copy. */
5793 target_tracepoint
.hit_count
= 0;
5795 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5796 sizeof (target_tracepoint
));
5799 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5801 download_agent_expr (tpoint
->cond
));
5803 if (tpoint
->numactions
)
5806 CORE_ADDR actions_array
;
5808 /* The pointers array. */
5810 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5811 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5815 /* Now for each pointer, download the action. */
5816 for (i
= 0; i
< tpoint
->numactions
; i
++)
5818 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5819 CORE_ADDR ipa_action
= action
->ops
->download (action
);
5821 if (ipa_action
!= 0)
5822 write_inferior_data_ptr
5823 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5830 download_tracepoint (struct tracepoint
*tpoint
)
5832 struct tracepoint
*tp
, *tp_prev
;
5834 if (tpoint
->type
!= fast_tracepoint
5835 && tpoint
->type
!= static_tracepoint
)
5838 download_tracepoint_1 (tpoint
);
5840 /* Find the previous entry of TPOINT, which is fast tracepoint or
5841 static tracepoint. */
5843 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
5845 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
5851 CORE_ADDR tp_prev_target_next_addr
;
5853 /* Insert TPOINT after TP_PREV in IPA. */
5854 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
5855 + offsetof (struct tracepoint
, next
),
5856 &tp_prev_target_next_addr
))
5857 fatal ("error reading `tp_prev->next'");
5859 /* tpoint->next = tp_prev->next */
5860 write_inferior_data_ptr (tpoint
->obj_addr_on_target
5861 + offsetof (struct tracepoint
, next
),
5862 tp_prev_target_next_addr
);
5863 /* tp_prev->next = tpoint */
5864 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
5865 + offsetof (struct tracepoint
, next
),
5866 tpoint
->obj_addr_on_target
);
5869 /* First object in list, set the head pointer in the
5871 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
5872 tpoint
->obj_addr_on_target
);
5877 download_trace_state_variables (void)
5879 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5880 struct trace_state_variable
*tsv
;
5882 /* Start out empty. */
5883 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5885 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5887 struct trace_state_variable target_tsv
;
5889 /* TSV's with a getter have been initialized equally in both the
5890 inferior and GDBserver. Skip them. */
5891 if (tsv
->getter
!= NULL
)
5897 ptr
= target_malloc (sizeof (*tsv
));
5899 if (tsv
== trace_state_variables
)
5901 /* First object in list, set the head pointer in the
5904 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5909 write_inferior_data_ptr (prev_ptr
5910 + offsetof (struct trace_state_variable
,
5915 /* Write the whole object. We'll fix up its pointers in a bit.
5916 Assume no next, fixup when needed. */
5917 target_tsv
.next
= NULL
;
5919 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5920 sizeof (target_tsv
));
5922 if (tsv
->name
!= NULL
)
5924 size_t size
= strlen (tsv
->name
) + 1;
5925 CORE_ADDR name_addr
= target_malloc (size
);
5926 write_inferior_memory (name_addr
,
5927 (unsigned char *) tsv
->name
, size
);
5928 write_inferior_data_ptr (ptr
5929 + offsetof (struct trace_state_variable
,
5934 if (tsv
->getter
!= NULL
)
5936 fatal ("what to do with these?");
5942 /* Fixup the next pointer in the last item in the list. */
5943 write_inferior_data_ptr (prev_ptr
5944 + offsetof (struct trace_state_variable
,
5949 /* Upload complete trace frames out of the IP Agent's trace buffer
5950 into GDBserver's trace buffer. This always uploads either all or
5951 no trace frames. This is the counter part of
5952 `trace_alloc_trace_buffer'. See its description of the atomic
5953 synching mechanism. */
5956 upload_fast_traceframes (void)
5958 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5959 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5961 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5962 unsigned int curr_tbctrl_idx
;
5963 unsigned int ipa_trace_buffer_ctrl_curr
;
5964 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5965 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5966 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5967 CORE_ADDR ipa_trace_buffer_lo
;
5968 CORE_ADDR ipa_trace_buffer_hi
;
5970 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5971 &ipa_traceframe_read_count_racy
))
5973 /* This will happen in most targets if the current thread is
5978 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5979 &ipa_traceframe_write_count_racy
))
5982 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5983 ipa_traceframe_write_count_racy
5984 - ipa_traceframe_read_count_racy
,
5985 ipa_traceframe_write_count_racy
,
5986 ipa_traceframe_read_count_racy
);
5988 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
5991 about_to_request_buffer_space_bkpt
5992 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
5995 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5996 &ipa_trace_buffer_ctrl_curr
))
5999 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6001 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6004 unsigned int prev
, counter
;
6006 /* Update the token, with new counters, and the GDBserver stamp
6007 bit. Alway reuse the current TBC index. */
6008 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6009 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
6011 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
6017 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6018 ipa_trace_buffer_ctrl_curr
))
6021 trace_debug ("Lib: Committed %08x -> %08x",
6022 ipa_trace_buffer_ctrl_curr_old
,
6023 ipa_trace_buffer_ctrl_curr
);
6025 /* Re-read these, now that we've installed the
6026 `about_to_request_buffer_space' breakpoint/lock. A thread could
6027 have finished a traceframe between the last read of these
6028 counters and setting the breakpoint above. If we start
6029 uploading, we never want to leave this function with
6030 traceframe_read_count != 0, otherwise, GDBserver could end up
6031 incrementing the counter tokens more than once (due to event loop
6032 nesting), which would break the IP agent's "effective" detection
6033 (see trace_alloc_trace_buffer). */
6034 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6035 &ipa_traceframe_read_count
))
6037 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6038 &ipa_traceframe_write_count
))
6043 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6044 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6045 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6047 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6048 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6049 trace_debug ("note that ipa_traceframe_count's parts changed");
6052 /* Get the address of the current TBC object (the IP agent has an
6053 array of 3 such objects). The index is stored in the TBC
6055 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6056 ipa_trace_buffer_ctrl_addr
6057 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6059 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6060 (unsigned char *) &ipa_trace_buffer_ctrl
,
6061 sizeof (struct ipa_trace_buffer_control
)))
6064 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6065 &ipa_trace_buffer_lo
))
6067 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6068 &ipa_trace_buffer_hi
))
6071 /* Offsets are easier to grok for debugging than raw addresses,
6072 especially for the small trace buffer sizes that are useful for
6074 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6075 "endfree=%d wrap=%d hi=%d",
6077 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6078 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6079 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6080 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6081 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6083 /* Note that the IPA's buffer is always circular. */
6085 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6087 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6088 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6090 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6091 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6092 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6093 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6096 tf
= IPA_FIRST_TRACEFRAME ();
6098 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6100 struct tracepoint
*tpoint
;
6101 struct traceframe
*tframe
;
6102 unsigned char *block
;
6103 struct traceframe ipa_tframe
;
6105 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6106 offsetof (struct traceframe
, data
)))
6107 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6109 if (ipa_tframe
.tpnum
== 0)
6110 fatal ("Uploading: No (more) fast traceframes, but "
6111 "ipa_traceframe_count == %u??\n",
6112 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6114 /* Note that this will be incorrect for multi-location
6116 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6118 tframe
= add_traceframe (tpoint
);
6121 trace_buffer_is_full
= 1;
6122 trace_debug ("Uploading: trace buffer is full");
6126 /* Copy the whole set of blocks in one go for now. FIXME:
6127 split this in smaller blocks. */
6128 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6131 if (read_inferior_memory (tf
6132 + offsetof (struct traceframe
, data
),
6133 block
, ipa_tframe
.data_size
))
6134 error ("Uploading: Couldn't read traceframe data at %s\n",
6135 paddress (tf
+ offsetof (struct traceframe
, data
)));
6138 trace_debug ("Uploading: traceframe didn't fit");
6139 finish_traceframe (tframe
);
6142 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6144 /* If we freed the traceframe that wrapped around, go back
6145 to the non-wrap case. */
6146 if (tf
< ipa_trace_buffer_ctrl
.start
)
6148 trace_debug ("Lib: Discarding past the wraparound");
6149 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6151 ipa_trace_buffer_ctrl
.start
= tf
;
6152 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6153 ++ipa_traceframe_read_count
;
6155 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6156 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6158 trace_debug ("Lib: buffer is fully empty. "
6159 "Trace buffer [%d] start=%d free=%d endfree=%d",
6161 (int) (ipa_trace_buffer_ctrl
.start
6162 - ipa_trace_buffer_lo
),
6163 (int) (ipa_trace_buffer_ctrl
.free
6164 - ipa_trace_buffer_lo
),
6165 (int) (ipa_trace_buffer_ctrl
.end_free
6166 - ipa_trace_buffer_lo
));
6168 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6169 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6170 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6171 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6174 trace_debug ("Uploaded a traceframe\n"
6175 "Lib: Trace buffer [%d] start=%d free=%d "
6176 "endfree=%d wrap=%d hi=%d",
6178 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6179 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6180 (int) (ipa_trace_buffer_ctrl
.end_free
6181 - ipa_trace_buffer_lo
),
6182 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6183 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6186 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6187 (unsigned char *) &ipa_trace_buffer_ctrl
,
6188 sizeof (struct ipa_trace_buffer_control
)))
6191 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6192 ipa_traceframe_read_count
);
6194 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6197 cancel_breakpoints ();
6199 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6200 about_to_request_buffer_space_bkpt
= NULL
;
6204 if (trace_buffer_is_full
)
6209 #ifdef IN_PROCESS_AGENT
6211 IP_AGENT_EXPORT
int ust_loaded
;
6212 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6216 /* Static tracepoints. */
6218 /* UST puts a "struct tracepoint" in the global namespace, which
6219 conflicts with our tracepoint. Arguably, being a library, it
6220 shouldn't take ownership of such a generic name. We work around it
6222 #define tracepoint ust_tracepoint
6223 #include <ust/ust.h>
6226 extern int serialize_to_text (char *outbuf
, int bufsize
,
6227 const char *fmt
, va_list ap
);
6229 #define GDB_PROBE_NAME "gdb"
6231 /* We dynamically search for the UST symbols instead of linking them
6232 in. This lets the user decide if the application uses static
6233 tracepoints, instead of always pulling libust.so in. This vector
6234 holds pointers to all functions we care about. */
6238 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6239 const char *fmt
, va_list ap
);
6241 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6242 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6244 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6246 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6249 void (*marker_iter_start
) (struct marker_iter
*iter
);
6250 void (*marker_iter_next
) (struct marker_iter
*iter
);
6251 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6252 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6257 /* Cast through typeof to catch incompatible API changes. Since UST
6258 only builds with gcc, we can freely use gcc extensions here
6260 #define GET_UST_SYM(SYM) \
6263 if (ust_ops.SYM == NULL) \
6264 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6265 if (ust_ops.SYM == NULL) \
6269 #define USTF(SYM) ust_ops.SYM
6271 /* Get pointers to all libust.so functions we care about. */
6276 GET_UST_SYM (serialize_to_text
);
6278 GET_UST_SYM (ltt_probe_register
);
6279 GET_UST_SYM (ltt_probe_unregister
);
6280 GET_UST_SYM (ltt_marker_connect
);
6281 GET_UST_SYM (ltt_marker_disconnect
);
6283 GET_UST_SYM (marker_iter_start
);
6284 GET_UST_SYM (marker_iter_next
);
6285 GET_UST_SYM (marker_iter_stop
);
6286 GET_UST_SYM (marker_iter_reset
);
6292 /* Given an UST marker, return the matching gdb static tracepoint.
6293 The match is done by address. */
6295 static struct tracepoint
*
6296 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6298 struct tracepoint
*tpoint
;
6300 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6302 if (tpoint
->type
!= static_tracepoint
)
6305 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6312 /* The probe function we install on lttng/ust markers. Whenever a
6313 probed ust marker is hit, this function is called. This is similar
6314 to gdb_collect, only for static tracepoints, instead of fast
6318 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6319 struct registers
*regs
, void *call_private
,
6320 const char *fmt
, va_list *args
)
6322 struct tracepoint
*tpoint
;
6323 struct static_tracepoint_ctx ctx
;
6325 /* Don't do anything until the trace run is completely set up. */
6328 trace_debug ("gdb_probe: not tracing\n");
6332 ctx
.base
.type
= static_tracepoint
;
6333 ctx
.regcache_initted
= 0;
6338 /* Wrap the regblock in a register cache (in the stack, we don't
6339 want to malloc here). */
6340 ctx
.regspace
= alloca (register_cache_size ());
6341 if (ctx
.regspace
== NULL
)
6343 trace_debug ("Trace buffer block allocation failed, skipping");
6347 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6350 trace_debug ("gdb_probe: marker not known: "
6351 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6352 mdata
->location
, mdata
->channel
,
6353 mdata
->name
, mdata
->format
);
6357 if (!tpoint
->enabled
)
6359 trace_debug ("gdb_probe: tracepoint disabled");
6363 ctx
.tpoint
= tpoint
;
6365 trace_debug ("gdb_probe: collecting marker: "
6366 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6367 mdata
->location
, mdata
->channel
,
6368 mdata
->name
, mdata
->format
);
6370 /* Test the condition if present, and collect if true. */
6371 if (tpoint
->cond
== NULL
6372 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6375 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6376 tpoint
->address
, tpoint
);
6378 if (stopping_tracepoint
6379 || trace_buffer_is_full
6380 || expr_eval_result
!= expr_eval_no_error
)
6385 /* If there was a condition and it evaluated to false, the only
6386 way we would stop tracing is if there was an error during
6387 condition expression evaluation. */
6388 if (expr_eval_result
!= expr_eval_no_error
)
6393 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6394 static tracepoint string data. This is a string passed to the
6395 tracing library by the user, at the time of the tracepoint marker
6396 call. E.g., in the UST marker call:
6398 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6400 the collected data is "str FOOBAZ".
6404 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6405 struct traceframe
*tframe
)
6407 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6408 unsigned char *bufspace
;
6411 unsigned short blocklen
;
6415 trace_debug ("Wanted to collect static trace data, "
6416 "but there's no static trace data");
6420 va_copy (copy
, *umd
->args
);
6421 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6424 trace_debug ("Want to collect ust data");
6426 /* 'S' + size + string */
6427 bufspace
= add_traceframe_block (tframe
,
6428 1 + sizeof (blocklen
) + size
+ 1);
6429 if (bufspace
== NULL
)
6431 trace_debug ("Trace buffer block allocation failed, skipping");
6435 /* Identify a static trace data block. */
6438 blocklen
= size
+ 1;
6439 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6441 va_copy (copy
, *umd
->args
);
6442 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6443 size
+ 1, umd
->fmt
, copy
);
6446 trace_debug ("Storing static tracepoint data in regblock: %s",
6447 bufspace
+ 1 + sizeof (blocklen
));
6450 /* The probe to register with lttng/ust. */
6451 static struct ltt_available_probe gdb_ust_probe
=
6458 #endif /* HAVE_UST */
6459 #endif /* IN_PROCESS_AGENT */
6461 #ifndef IN_PROCESS_AGENT
6463 /* Ask the in-process agent to run a command. Since we don't want to
6464 have to handle the IPA hitting breakpoints while running the
6465 command, we pause all threads, remove all breakpoints, and then set
6466 the helper thread re-running. We communicate with the helper
6467 thread by means of direct memory xfering, and a socket for
6471 run_inferior_command (char *cmd
)
6474 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6476 trace_debug ("run_inferior_command: running: %s", cmd
);
6479 uninsert_all_breakpoints ();
6481 err
= agent_run_command (pid
, (const char *) cmd
);
6483 reinsert_all_breakpoints ();
6489 #else /* !IN_PROCESS_AGENT */
6491 #include <sys/socket.h>
6494 #ifndef UNIX_PATH_MAX
6495 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6498 /* Where we put the socked used for synchronization. */
6499 #define SOCK_DIR P_tmpdir
6501 /* Thread ID of the helper thread. GDBserver reads this to know which
6502 is the help thread. This is an LWP id on Linux. */
6503 int helper_thread_id
;
6506 init_named_socket (const char *name
)
6509 struct sockaddr_un addr
;
6511 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6514 warning ("socket creation failed: %s", strerror (errno
));
6518 addr
.sun_family
= AF_UNIX
;
6520 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6521 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6523 result
= access (name
, F_OK
);
6527 result
= unlink (name
);
6530 warning ("unlink failed: %s", strerror (errno
));
6534 warning ("socket %s already exists; overwriting", name
);
6537 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6540 warning ("bind failed: %s", strerror (errno
));
6545 result
= listen (fd
, 1);
6548 warning ("listen: %s", strerror (errno
));
6557 gdb_agent_socket_init (void)
6560 char name
[UNIX_PATH_MAX
];
6562 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6563 SOCK_DIR
, getpid ());
6564 if (result
>= UNIX_PATH_MAX
)
6566 trace_debug ("string overflow allocating socket name");
6570 fd
= init_named_socket (name
);
6572 warning ("Error initializing named socket (%s) for communication with the "
6573 "ust helper thread. Check that directory exists and that it "
6574 "is writable.", name
);
6581 /* The next marker to be returned on a qTsSTM command. */
6582 static const struct marker
*next_st
;
6584 /* Returns the first known marker. */
6589 struct marker_iter iter
;
6591 USTF(marker_iter_reset
) (&iter
);
6592 USTF(marker_iter_start
) (&iter
);
6597 /* Returns the marker following M. */
6599 const struct marker
*
6600 next_marker (const struct marker
*m
)
6602 struct marker_iter iter
;
6604 USTF(marker_iter_reset
) (&iter
);
6605 USTF(marker_iter_start
) (&iter
);
6607 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6609 if (iter
.marker
== m
)
6611 USTF(marker_iter_next
) (&iter
);
6619 /* Return an hexstr version of the STR C string, fit for sending to
6623 cstr_to_hexstr (const char *str
)
6625 int len
= strlen (str
);
6626 char *hexstr
= xmalloc (len
* 2 + 1);
6627 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6631 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6635 response_ust_marker (char *packet
, const struct marker
*st
)
6637 char *strid
, *format
, *tmp
;
6639 next_st
= next_marker (st
);
6641 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6642 strlen (st
->name
) + 1);
6643 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6645 strid
= cstr_to_hexstr (tmp
);
6648 format
= cstr_to_hexstr (st
->format
);
6650 sprintf (packet
, "m%s:%s:%s",
6651 paddress ((uintptr_t) st
->location
),
6659 /* Return the first static tracepoint, and initialize the state
6660 machine that will iterate through all the static tracepoints. */
6663 cmd_qtfstm (char *packet
)
6665 trace_debug ("Returning first trace state variable definition");
6667 if (first_marker ())
6668 response_ust_marker (packet
, first_marker ());
6670 strcpy (packet
, "l");
6673 /* Return additional trace state variable definitions. */
6676 cmd_qtsstm (char *packet
)
6678 trace_debug ("Returning static tracepoint");
6681 response_ust_marker (packet
, next_st
);
6683 strcpy (packet
, "l");
6686 /* Disconnect the GDB probe from a marker at a given address. */
6689 unprobe_marker_at (char *packet
)
6693 struct marker_iter iter
;
6695 p
+= sizeof ("unprobe_marker_at:") - 1;
6697 p
= unpack_varlen_hex (p
, &address
);
6699 USTF(marker_iter_reset
) (&iter
);
6700 USTF(marker_iter_start
) (&iter
);
6701 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6702 if ((uintptr_t ) iter
.marker
->location
== address
)
6706 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6710 warning ("could not disable marker %s/%s",
6711 iter
.marker
->channel
, iter
.marker
->name
);
6716 /* Connect the GDB probe to a marker at a given address. */
6719 probe_marker_at (char *packet
)
6723 struct marker_iter iter
;
6726 p
+= sizeof ("probe_marker_at:") - 1;
6728 p
= unpack_varlen_hex (p
, &address
);
6730 USTF(marker_iter_reset
) (&iter
);
6732 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6734 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6735 if ((uintptr_t ) m
->location
== address
)
6739 trace_debug ("found marker for address. "
6740 "ltt_marker_connect (marker = %s/%s)",
6741 m
->channel
, m
->name
);
6743 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6745 if (result
&& result
!= -EEXIST
)
6746 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6747 m
->channel
, m
->name
, -result
);
6751 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6752 m
->channel
, m
->name
);
6756 strcpy (packet
, "OK");
6760 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6765 cmd_qtstmat (char *packet
)
6769 struct marker_iter iter
;
6772 p
+= sizeof ("qTSTMat:") - 1;
6774 p
= unpack_varlen_hex (p
, &address
);
6776 USTF(marker_iter_reset
) (&iter
);
6778 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6780 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6781 if ((uintptr_t ) m
->location
== address
)
6783 response_ust_marker (packet
, m
);
6787 strcpy (packet
, "l");
6797 USTF(ltt_probe_register
) (&gdb_ust_probe
);
6800 #endif /* HAVE_UST */
6802 #include <sys/syscall.h>
6804 /* Helper thread of agent. */
6807 gdb_agent_helper_thread (void *arg
)
6813 listen_fd
= gdb_agent_socket_init ();
6815 if (helper_thread_id
== 0)
6816 helper_thread_id
= syscall (SYS_gettid
);
6818 if (listen_fd
== -1)
6820 warning ("could not create sync socket\n");
6827 struct sockaddr_un sockaddr
;
6832 tmp
= sizeof (sockaddr
);
6836 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
6838 /* It seems an ERESTARTSYS can escape out of accept. */
6839 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
6843 warning ("Accept returned %d, error: %s\n",
6844 fd
, strerror (errno
));
6850 ret
= read (fd
, buf
, 1);
6851 } while (ret
== -1 && errno
== EINTR
);
6855 warning ("reading socket (fd=%d) failed with %s",
6856 fd
, strerror (errno
));
6864 if (strcmp ("qTfSTM", cmd_buf
) == 0)
6866 cmd_qtfstm (cmd_buf
);
6868 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
6870 cmd_qtsstm (cmd_buf
);
6872 else if (strncmp ("unprobe_marker_at:",
6874 sizeof ("unprobe_marker_at:") - 1) == 0)
6876 unprobe_marker_at (cmd_buf
);
6878 else if (strncmp ("probe_marker_at:",
6880 sizeof ("probe_marker_at:") - 1) == 0)
6882 probe_marker_at (cmd_buf
);
6884 else if (strncmp ("qTSTMat:",
6886 sizeof ("qTSTMat:") - 1) == 0)
6888 cmd_qtstmat (cmd_buf
);
6890 #endif /* HAVE_UST */
6893 /* Fix compiler's warning: ignoring return value of 'write'. */
6894 ret
= write (fd
, buf
, 1);
6903 #include <pthread.h>
6905 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
6908 gdb_agent_init (void)
6915 /* We want the helper thread to be as transparent as possible, so
6916 have it inherit an all-signals-blocked mask. */
6918 sigfillset (&new_mask
);
6919 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
6921 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
6923 res
= pthread_create (&thread
,
6925 gdb_agent_helper_thread
,
6928 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
6930 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
6932 while (helper_thread_id
== 0)
6940 #include <sys/mman.h>
6943 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
6944 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
6945 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
6946 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
6947 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
6948 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
6950 /* Record the result of getting buffer space for fast tracepoint
6951 trampolines. Any error message is copied, since caller may not be
6952 using persistent storage. */
6955 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
6957 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
6958 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
6960 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
6962 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
6965 static void __attribute__ ((constructor
))
6966 initialize_tracepoint_ftlib (void)
6968 initialize_tracepoint ();
6973 #endif /* IN_PROCESS_AGENT */
6975 /* Return a timestamp, expressed as microseconds of the usual Unix
6976 time. (As the result is a 64-bit number, it will not overflow any
6980 get_timestamp (void)
6984 if (gettimeofday (&tv
, 0) != 0)
6987 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
6991 initialize_tracepoint (void)
6993 /* There currently no way to change the buffer size. */
6994 const int sizeOfBuffer
= 5 * 1024 * 1024;
6995 unsigned char *buf
= xmalloc (sizeOfBuffer
);
6996 init_trace_buffer (buf
, sizeOfBuffer
);
6998 /* Wire trace state variable 1 to be the timestamp. This will be
6999 uploaded to GDB upon connection and become one of its trace state
7000 variables. (In case you're wondering, if GDB already has a trace
7001 variable numbered 1, it will be renumbered.) */
7002 create_trace_state_variable (1, 0);
7003 set_trace_state_variable_name (1, "trace_timestamp");
7004 set_trace_state_variable_getter (1, get_timestamp
);
7006 #ifdef IN_PROCESS_AGENT
7011 pagesize
= sysconf (_SC_PAGE_SIZE
);
7015 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7017 #define SCRATCH_BUFFER_NPAGES 20
7019 /* Allocate scratch buffer aligned on a page boundary, at a low
7020 address (close to the main executable's code). */
7021 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
7023 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
7024 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
7025 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
7027 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
7033 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
7036 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
7039 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
7041 /* It's not a fatal error for something to go wrong with trampoline
7042 buffer setup, but it can be mysterious, so create a channel to
7043 report back on what went wrong, using a fixed size since we may
7044 not be able to allocate space later when the problem occurs. */
7045 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
7047 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
7049 initialize_low_tracepoint ();