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_tracepoints (void);
412 static void download_trace_state_variables (void);
413 static void upload_fast_traceframes (void);
415 static int run_inferior_command (char *cmd
);
418 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
420 return read_inferior_memory (symaddr
, (unsigned char *) val
,
425 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
427 return read_inferior_memory (symaddr
, (unsigned char *) val
,
432 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
434 void *pval
= (void *) (uintptr_t) val
;
437 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
438 *val
= (uintptr_t) pval
;
443 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
445 void *pval
= (void *) (uintptr_t) val
;
446 return write_inferior_memory (symaddr
,
447 (unsigned char *) &pval
, sizeof (pval
));
451 write_inferior_integer (CORE_ADDR symaddr
, int val
)
453 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
457 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
459 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
464 /* Base action. Concrete actions inherit this. */
466 struct tracepoint_action
471 /* An 'M' (collect memory) action. */
472 struct collect_memory_action
474 struct tracepoint_action base
;
481 /* An 'R' (collect registers) action. */
483 struct collect_registers_action
485 struct tracepoint_action base
;
488 /* An 'X' (evaluate expression) action. */
490 struct eval_expr_action
492 struct tracepoint_action base
;
494 struct agent_expr
*expr
;
497 /* An 'L' (collect static trace data) action. */
498 struct collect_static_trace_data_action
500 struct tracepoint_action base
;
503 /* This structure describes a piece of the source-level definition of
504 the tracepoint. The contents are not interpreted by the target,
505 but preserved verbatim for uploading upon reconnection. */
509 /* The type of string, such as "cond" for a conditional. */
512 /* The source-level string itself. For the sake of target
513 debugging, we store it in plaintext, even though it is always
514 transmitted in hex. */
517 /* Link to the next one in the list. We link them in the order
518 received, in case some make up an ordered list of commands or
520 struct source_string
*next
;
525 /* Trap based tracepoint. */
528 /* A fast tracepoint implemented with a jump instead of a trap. */
531 /* A static tracepoint, implemented by a program call into a tracing
536 struct tracepoint_hit_ctx
;
538 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
541 /* The definition of a tracepoint. */
543 /* Tracepoints may have multiple locations, each at a different
544 address. This can occur with optimizations, template
545 instantiation, etc. Since the locations may be in different
546 scopes, the conditions and actions may be different for each
547 location. Our target version of tracepoints is more like GDB's
548 notion of "breakpoint locations", but we have almost nothing that
549 is not per-location, so we bother having two kinds of objects. The
550 key consequence is that numbers are not unique, and that it takes
551 both number and address to identify a tracepoint uniquely. */
555 /* The number of the tracepoint, as specified by GDB. Several
556 tracepoint objects here may share a number. */
559 /* Address at which the tracepoint is supposed to trigger. Several
560 tracepoints may share an address. */
563 /* Tracepoint type. */
564 enum tracepoint_type type
;
566 /* True if the tracepoint is currently enabled. */
569 /* The number of single steps that will be performed after each
573 /* The number of times the tracepoint may be hit before it will
574 terminate the entire tracing run. */
577 /* Pointer to the agent expression that is the tracepoint's
578 conditional, or NULL if the tracepoint is unconditional. */
579 struct agent_expr
*cond
;
581 /* The list of actions to take when the tracepoint triggers. */
583 struct tracepoint_action
**actions
;
585 /* Count of the times we've hit this tracepoint during the run.
586 Note that while-stepping steps are not counted as "hits". */
589 /* Cached sum of the sizes of traceframes created by this point. */
590 long traceframe_usage
;
592 CORE_ADDR compiled_cond
;
594 /* Link to the next tracepoint in the list. */
595 struct tracepoint
*next
;
597 #ifndef IN_PROCESS_AGENT
598 /* The list of actions to take when the tracepoint triggers, in
599 string/packet form. */
602 /* The collection of strings that describe the tracepoint as it was
603 entered into GDB. These are not used by the target, but are
604 reported back to GDB upon reconnection. */
605 struct source_string
*source_strings
;
607 /* The number of bytes displaced by fast tracepoints. It may subsume
608 multiple instructions, for multi-byte fast tracepoints. This
609 field is only valid for fast tracepoints. */
612 /* Only for fast tracepoints. */
613 CORE_ADDR obj_addr_on_target
;
615 /* Address range where the original instruction under a fast
616 tracepoint was relocated to. (_end is actually one byte past
618 CORE_ADDR adjusted_insn_addr
;
619 CORE_ADDR adjusted_insn_addr_end
;
621 /* The address range of the piece of the jump pad buffer that was
622 assigned to this fast tracepoint. (_end is actually one byte
625 CORE_ADDR jump_pad_end
;
627 /* The address range of the piece of the trampoline buffer that was
628 assigned to this fast tracepoint. (_end is actually one byte
630 CORE_ADDR trampoline
;
631 CORE_ADDR trampoline_end
;
633 /* The list of actions to take while in a stepping loop. These
634 fields are only valid for patch-based tracepoints. */
635 int num_step_actions
;
636 struct tracepoint_action
**step_actions
;
637 /* Same, but in string/packet form. */
638 char **step_actions_str
;
640 /* Handle returned by the breakpoint or tracepoint module when we
641 inserted the trap or jump, or hooked into a static tracepoint.
642 NULL if we haven't inserted it yet. */
648 #ifndef IN_PROCESS_AGENT
650 /* Given `while-stepping', a thread may be collecting data for more
651 than one tracepoint simultaneously. On the other hand, the same
652 tracepoint with a while-stepping action may be hit by more than one
653 thread simultaneously (but not quite, each thread could be handling
654 a different step). Each thread holds a list of these objects,
655 representing the current step of each while-stepping action being
660 struct wstep_state
*next
;
662 /* The tracepoint number. */
664 /* The tracepoint's address. */
665 CORE_ADDR tp_address
;
667 /* The number of the current step in this 'while-stepping'
674 /* The linked list of all tracepoints. Marked explicitly as used as
675 the in-process library doesn't use it for the fast tracepoints
677 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
679 #ifndef IN_PROCESS_AGENT
681 /* Pointer to the last tracepoint in the list, new tracepoints are
682 linked in at the end. */
684 static struct tracepoint
*last_tracepoint
;
687 /* The first tracepoint to exceed its pass count. */
689 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
691 /* True if the trace buffer is full or otherwise no longer usable. */
693 IP_AGENT_EXPORT
int trace_buffer_is_full
;
695 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
697 #ifndef IN_PROCESS_AGENT
699 static const char *eval_result_names
[] =
701 "terror:in the attic", /* this should never be reported */
702 "terror:empty expression",
703 "terror:empty stack",
704 "terror:stack overflow",
705 "terror:stack underflow",
706 "terror:unhandled opcode",
707 "terror:unrecognized opcode",
708 "terror:divide by zero"
713 /* The tracepoint in which the error occurred. */
715 static struct tracepoint
*error_tracepoint
;
717 struct trace_state_variable
719 /* This is the name of the variable as used in GDB. The target
720 doesn't use the name, but needs to have it for saving and
721 reconnection purposes. */
724 /* This number identifies the variable uniquely. Numbers may be
725 assigned either by the target (in the case of builtin variables),
726 or by GDB, and are presumed unique during the course of a trace
730 /* The variable's initial value, a 64-bit signed integer always. */
731 LONGEST initial_value
;
733 /* The variable's value, a 64-bit signed integer always. */
736 /* Pointer to a getter function, used to supply computed values. */
737 LONGEST (*getter
) (void);
739 /* Link to the next variable. */
740 struct trace_state_variable
*next
;
743 /* Linked list of all trace state variables. */
745 #ifdef IN_PROCESS_AGENT
746 struct trace_state_variable
*alloced_trace_state_variables
;
749 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
751 /* The results of tracing go into a fixed-size space known as the
752 "trace buffer". Because usage follows a limited number of
753 patterns, we manage it ourselves rather than with malloc. Basic
754 rules are that we create only one trace frame at a time, each is
755 variable in size, they are never moved once created, and we only
756 discard if we are doing a circular buffer, and then only the oldest
757 ones. Each trace frame includes its own size, so we don't need to
758 link them together, and the trace frame number is relative to the
759 first one, so we don't need to record numbers. A trace frame also
760 records the number of the tracepoint that created it. The data
761 itself is a series of blocks, each introduced by a single character
762 and with a defined format. Each type of block has enough
763 type/length info to allow scanners to jump quickly from one block
764 to the next without reading each byte in the block. */
766 /* Trace buffer management would be simple - advance a free pointer
767 from beginning to end, then stop - were it not for the circular
768 buffer option, which is a useful way to prevent a trace run from
769 stopping prematurely because the buffer filled up. In the circular
770 case, the location of the first trace frame (trace_buffer_start)
771 moves as old trace frames are discarded. Also, since we grow trace
772 frames incrementally as actions are performed, we wrap around to
773 the beginning of the trace buffer. This is per-block, so each
774 block within a trace frame remains contiguous. Things get messy
775 when the wrapped-around trace frame is the one being discarded; the
776 free space ends up in two parts at opposite ends of the buffer. */
779 # if defined(__GNUC__)
780 # define ATTR_PACKED __attribute__ ((packed))
782 # define ATTR_PACKED /* nothing */
786 /* The data collected at a tracepoint hit. This object should be as
787 small as possible, since there may be a great many of them. We do
788 not need to keep a frame number, because they are all sequential
789 and there are no deletions; so the Nth frame in the buffer is
790 always frame number N. */
794 /* Number of the tracepoint that collected this traceframe. A value
795 of 0 indicates the current end of the trace buffer. We make this
796 a 16-bit field because it's never going to happen that GDB's
797 numbering of tracepoints reaches 32,000. */
800 /* The size of the data in this trace frame. We limit this to 32
801 bits, even on a 64-bit target, because it's just implausible that
802 one is validly going to collect 4 gigabytes of data at a single
804 unsigned int data_size
: 32;
806 /* The base of the trace data, which is contiguous from this point. */
807 unsigned char data
[0];
811 /* The traceframe to be used as the source of data to send back to
812 GDB. A value of -1 means to get data from the live program. */
814 int current_traceframe
= -1;
816 /* This flag is true if the trace buffer is circular, meaning that
817 when it fills, the oldest trace frames are discarded in order to
820 #ifndef IN_PROCESS_AGENT
821 static int circular_trace_buffer
;
824 /* Pointer to the block of memory that traceframes all go into. */
826 static unsigned char *trace_buffer_lo
;
828 /* Pointer to the end of the trace buffer, more precisely to the byte
829 after the end of the buffer. */
831 static unsigned char *trace_buffer_hi
;
833 /* Control structure holding the read/write/etc. pointers into the
834 trace buffer. We need more than one of these to implement a
835 transaction-like mechanism to garantees that both GDBserver and the
836 in-process agent can try to change the trace buffer
839 struct trace_buffer_control
841 /* Pointer to the first trace frame in the buffer. In the
842 non-circular case, this is equal to trace_buffer_lo, otherwise it
843 moves around in the buffer. */
844 unsigned char *start
;
846 /* Pointer to the free part of the trace buffer. Note that we clear
847 several bytes at and after this pointer, so that traceframe
848 scans/searches terminate properly. */
851 /* Pointer to the byte after the end of the free part. Note that
852 this may be smaller than trace_buffer_free in the circular case,
853 and means that the free part is in two pieces. Initially it is
854 equal to trace_buffer_hi, then is generally equivalent to
855 trace_buffer_start. */
856 unsigned char *end_free
;
858 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
859 this is the point at which the trace data breaks, and resumes at
864 /* Same as above, to be used by GDBserver when updating the in-process
866 struct ipa_trace_buffer_control
875 /* We have possibly both GDBserver and an inferior thread accessing
876 the same IPA trace buffer memory. The IPA is the producer (tries
877 to put new frames in the buffer), while GDBserver occasionally
878 consumes them, that is, flushes the IPA's buffer into its own
879 buffer. Both sides need to update the trace buffer control
880 pointers (current head, tail, etc.). We can't use a global lock to
881 synchronize the accesses, as otherwise we could deadlock GDBserver
882 (if the thread holding the lock stops for a signal, say). So
883 instead of that, we use a transaction scheme where GDBserver writes
884 always prevail over the IPAs writes, and, we have the IPA detect
885 the commit failure/overwrite, and retry the whole attempt. This is
886 mainly implemented by having a global token object that represents
887 who wrote last to the buffer control structure. We need to freeze
888 any inferior writing to the buffer while GDBserver touches memory,
889 so that the inferior can correctly detect that GDBserver had been
890 there, otherwise, it could mistakingly think its commit was
891 successful; that's implemented by simply having GDBserver set a
892 breakpoint the inferior hits if it is the critical region.
894 There are three cycling trace buffer control structure copies
895 (buffer head, tail, etc.), with the token object including an index
896 indicating which is current live copy. The IPA tentatively builds
897 an updated copy in a non-current control structure, while GDBserver
898 always clobbers the current version directly. The IPA then tries
899 to atomically "commit" its version; if GDBserver clobbered the
900 structure meanwhile, that will fail, and the IPA restarts the
903 Listing the step in further detail, we have:
905 In-process agent (producer):
907 - passes by `about_to_request_buffer_space' breakpoint/lock
909 - reads current token, extracts current trace buffer control index,
910 and starts tentatively updating the rightmost one (0->1, 1->2,
911 2->0). Note that only one inferior thread is executing this code
912 at any given time, due to an outer lock in the jump pads.
914 - updates counters, and tries to commit the token.
916 - passes by second `about_to_request_buffer_space' breakpoint/lock,
917 leaving the sync region.
919 - checks if the update was effective.
921 - if trace buffer was found full, hits flush_trace_buffer
922 breakpoint, and restarts later afterwards.
924 GDBserver (consumer):
926 - sets `about_to_request_buffer_space' breakpoint/lock.
928 - updates the token unconditionally, using the current buffer
929 control index, since it knows that the IP agent always writes to
930 the rightmost, and due to the breakpoint, at most one IP thread
931 can try to update the trace buffer concurrently to GDBserver, so
932 there will be no danger of trace buffer control index wrap making
933 the IPA write to the same index as GDBserver.
935 - flushes the IP agent's trace buffer completely, and updates the
936 current trace buffer control structure. GDBserver *always* wins.
938 - removes the `about_to_request_buffer_space' breakpoint.
940 The token is stored in the `trace_buffer_ctrl_curr' variable.
941 Internally, it's bits are defined as:
943 |-------------+-----+-------------+--------+-------------+--------------|
944 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
945 |-------------+-----+-------------+--------+-------------+--------------|
946 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
947 |-------------+-----+-------------+--------+-------------+--------------|
949 GSB - GDBserver Stamp Bit
950 PC - Previous Counter
952 TBCI - Trace Buffer Control Index
955 An IPA update of `trace_buffer_ctrl_curr' does:
957 - read CC from the current token, save as PC.
959 - atomically tries to write PC+1,CC
961 A GDBserver update of `trace_buffer_ctrl_curr' does:
963 - reads PC and CC from the current token.
968 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
969 for the counters described below. The cleared bits are used to
970 hold the index of the items of the `trace_buffer_ctrl' array that
972 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
974 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
975 counter, and the `current' counter. */
977 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
978 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
980 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
981 always stamps this bit as set. */
982 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
984 #ifdef IN_PROCESS_AGENT
985 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
986 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
988 # define TRACE_BUFFER_CTRL_CURR \
989 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
993 /* The GDBserver side agent only needs one instance of this object, as
994 it doesn't need to sync with itself. Define it as array anyway so
995 that the rest of the code base doesn't need to care for the
997 struct trace_buffer_control trace_buffer_ctrl
[1];
998 # define TRACE_BUFFER_CTRL_CURR 0
1001 /* These are convenience macros used to access the current trace
1002 buffer control in effect. */
1003 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1004 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1005 #define trace_buffer_end_free \
1006 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1007 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1010 /* Macro that returns a pointer to the first traceframe in the buffer. */
1012 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1014 /* Macro that returns a pointer to the next traceframe in the buffer.
1015 If the computed location is beyond the wraparound point, subtract
1016 the offset of the wraparound. */
1018 #define NEXT_TRACEFRAME_1(TF) \
1019 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1021 #define NEXT_TRACEFRAME(TF) \
1022 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1023 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1024 ? (trace_buffer_wrap - trace_buffer_lo) \
1027 /* The difference between these counters represents the total number
1028 of complete traceframes present in the trace buffer. The IP agent
1029 writes to the write count, GDBserver writes to read count. */
1031 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1032 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1034 /* Convenience macro. */
1036 #define traceframe_count \
1037 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1039 /* The count of all traceframes created in the current run, including
1040 ones that were discarded to make room. */
1042 IP_AGENT_EXPORT
int traceframes_created
;
1044 #ifndef IN_PROCESS_AGENT
1046 /* Read-only regions are address ranges whose contents don't change,
1047 and so can be read from target memory even while looking at a trace
1048 frame. Without these, disassembly for instance will likely fail,
1049 because the program code is not usually collected into a trace
1050 frame. This data structure does not need to be very complicated or
1051 particularly efficient, it's only going to be used occasionally,
1052 and only by some commands. */
1054 struct readonly_region
1056 /* The bounds of the region. */
1057 CORE_ADDR start
, end
;
1059 /* Link to the next one. */
1060 struct readonly_region
*next
;
1063 /* Linked list of readonly regions. This list stays in effect from
1064 one tstart to the next. */
1066 static struct readonly_region
*readonly_regions
;
1070 /* The global that controls tracing overall. */
1072 IP_AGENT_EXPORT
int tracing
;
1074 #ifndef IN_PROCESS_AGENT
1076 /* Controls whether tracing should continue after GDB disconnects. */
1078 int disconnected_tracing
;
1080 /* The reason for the last tracing run to have stopped. We initialize
1081 to a distinct string so that GDB can distinguish between "stopped
1082 after running" and "stopped because never run" cases. */
1084 static const char *tracing_stop_reason
= "tnotrun";
1086 static int tracing_stop_tpnum
;
1088 /* 64-bit timestamps for the trace run's start and finish, expressed
1089 in microseconds from the Unix epoch. */
1091 LONGEST tracing_start_time
;
1092 LONGEST tracing_stop_time
;
1094 /* The (optional) user-supplied name of the user that started the run.
1095 This is an arbitrary string, and may be NULL. */
1097 char *tracing_user_name
;
1099 /* Optional user-supplied text describing the run. This is
1100 an arbitrary string, and may be NULL. */
1102 char *tracing_notes
;
1104 /* Optional user-supplied text explaining a tstop command. This is an
1105 arbitrary string, and may be NULL. */
1107 char *tracing_stop_note
;
1111 /* Functions local to this file. */
1113 /* Base "class" for tracepoint type specific data to be passed down to
1114 collect_data_at_tracepoint. */
1115 struct tracepoint_hit_ctx
1117 enum tracepoint_type type
;
1120 #ifdef IN_PROCESS_AGENT
1122 /* Fast/jump tracepoint specific data to be passed down to
1123 collect_data_at_tracepoint. */
1124 struct fast_tracepoint_ctx
1126 struct tracepoint_hit_ctx base
;
1128 struct regcache regcache
;
1129 int regcache_initted
;
1130 unsigned char *regspace
;
1132 unsigned char *regs
;
1133 struct tracepoint
*tpoint
;
1136 /* Static tracepoint specific data to be passed down to
1137 collect_data_at_tracepoint. */
1138 struct static_tracepoint_ctx
1140 struct tracepoint_hit_ctx base
;
1142 /* The regcache corresponding to the registers state at the time of
1143 the tracepoint hit. Initialized lazily, from REGS. */
1144 struct regcache regcache
;
1145 int regcache_initted
;
1147 /* The buffer space REGCACHE above uses. We use a separate buffer
1148 instead of letting the regcache malloc for both signal safety and
1149 performance reasons; this is allocated on the stack instead. */
1150 unsigned char *regspace
;
1152 /* The register buffer as passed on by lttng/ust. */
1153 struct registers
*regs
;
1155 /* The "printf" formatter and the args the user passed to the marker
1156 call. We use this to be able to collect "static trace data"
1161 /* The GDB tracepoint matching the probed marker that was "hit". */
1162 struct tracepoint
*tpoint
;
1167 /* Static tracepoint specific data to be passed down to
1168 collect_data_at_tracepoint. */
1169 struct trap_tracepoint_ctx
1171 struct tracepoint_hit_ctx base
;
1173 struct regcache
*regcache
;
1178 static enum eval_result_type
1179 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1180 struct traceframe
*tframe
,
1181 struct agent_expr
*aexpr
,
1184 #ifndef IN_PROCESS_AGENT
1185 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1186 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1189 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1190 struct tracepoint
*tpoint
);
1192 #ifndef IN_PROCESS_AGENT
1193 static void clear_readonly_regions (void);
1194 static void clear_installed_tracepoints (void);
1197 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1199 struct tracepoint
*tpoint
);
1200 #ifndef IN_PROCESS_AGENT
1201 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1203 struct tracepoint
*tpoint
, int current_step
);
1204 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1205 CORE_ADDR
*jump_entry
);
1207 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1209 struct tracepoint
*tpoint
,
1210 struct traceframe
*tframe
,
1211 struct tracepoint_action
*taction
);
1213 #ifndef IN_PROCESS_AGENT
1214 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1216 static void install_tracepoint (struct tracepoint
*, char *own_buf
);
1217 static void download_tracepoint (struct tracepoint
*);
1218 static int install_fast_tracepoint (struct tracepoint
*, char *errbuf
);
1221 static LONGEST
get_timestamp (void);
1223 #if defined(__GNUC__)
1224 # define memory_barrier() asm volatile ("" : : : "memory")
1226 # define memory_barrier() do {} while (0)
1229 /* We only build the IPA if this builtin is supported, and there are
1230 no uses of this in GDBserver itself, so we're safe in defining this
1232 #define cmpxchg(mem, oldval, newval) \
1233 __sync_val_compare_and_swap (mem, oldval, newval)
1235 /* Record that an error occurred during expression evaluation. */
1238 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1239 enum eval_result_type rtype
)
1241 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1242 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1244 #ifdef IN_PROCESS_AGENT
1245 /* Only record the first error we get. */
1246 if (cmpxchg (&expr_eval_result
,
1248 rtype
) != expr_eval_no_error
)
1251 if (expr_eval_result
!= expr_eval_no_error
)
1255 error_tracepoint
= tpoint
;
1258 /* Trace buffer management. */
1261 clear_trace_buffer (void)
1263 trace_buffer_start
= trace_buffer_lo
;
1264 trace_buffer_free
= trace_buffer_lo
;
1265 trace_buffer_end_free
= trace_buffer_hi
;
1266 trace_buffer_wrap
= trace_buffer_hi
;
1267 /* A traceframe with zeroed fields marks the end of trace data. */
1268 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1269 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1270 traceframe_read_count
= traceframe_write_count
= 0;
1271 traceframes_created
= 0;
1274 #ifndef IN_PROCESS_AGENT
1277 clear_inferior_trace_buffer (void)
1279 CORE_ADDR ipa_trace_buffer_lo
;
1280 CORE_ADDR ipa_trace_buffer_hi
;
1281 struct traceframe ipa_traceframe
= { 0 };
1282 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1284 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1285 &ipa_trace_buffer_lo
);
1286 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1287 &ipa_trace_buffer_hi
);
1289 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1290 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1291 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1292 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1294 /* A traceframe with zeroed fields marks the end of trace data. */
1295 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1296 (unsigned char *) &ipa_trace_buffer_ctrl
,
1297 sizeof (ipa_trace_buffer_ctrl
));
1299 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1301 /* A traceframe with zeroed fields marks the end of trace data. */
1302 write_inferior_memory (ipa_trace_buffer_lo
,
1303 (unsigned char *) &ipa_traceframe
,
1304 sizeof (ipa_traceframe
));
1306 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1307 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1308 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1314 init_trace_buffer (unsigned char *buf
, int bufsize
)
1316 trace_buffer_lo
= buf
;
1317 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1319 clear_trace_buffer ();
1322 #ifdef IN_PROCESS_AGENT
1324 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1325 about_to_request_buffer_space (void)
1327 /* GDBserver places breakpoint here while it goes about to flush
1328 data at random times. */
1329 UNKNOWN_SIDE_EFFECTS();
1334 /* Carve out a piece of the trace buffer, returning NULL in case of
1338 trace_buffer_alloc (size_t amt
)
1340 unsigned char *rslt
;
1341 struct trace_buffer_control
*tbctrl
;
1343 #ifdef IN_PROCESS_AGENT
1344 unsigned int prev
, prev_filtered
;
1345 unsigned int commit_count
;
1346 unsigned int commit
;
1347 unsigned int readout
;
1349 struct traceframe
*oldest
;
1350 unsigned char *new_start
;
1353 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1354 (long) amt
, (long) sizeof (struct traceframe
));
1356 /* Account for the EOB marker. */
1357 amt
+= sizeof (struct traceframe
);
1359 #ifdef IN_PROCESS_AGENT
1363 /* Read the current token and extract the index to try to write to,
1364 storing it in CURR. */
1365 prev
= trace_buffer_ctrl_curr
;
1366 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1367 curr
= prev_filtered
+ 1;
1371 about_to_request_buffer_space ();
1373 /* Start out with a copy of the current state. GDBserver may be
1374 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1375 be able to commit anyway if that happens. */
1376 trace_buffer_ctrl
[curr
]
1377 = trace_buffer_ctrl
[prev_filtered
];
1378 trace_debug ("trying curr=%u", curr
);
1380 /* The GDBserver's agent doesn't need all that syncing, and always
1381 updates TCB 0 (there's only one, mind you). */
1384 tbctrl
= &trace_buffer_ctrl
[curr
];
1386 /* Offsets are easier to grok for debugging than raw addresses,
1387 especially for the small trace buffer sizes that are useful for
1389 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1391 (int) (tbctrl
->start
- trace_buffer_lo
),
1392 (int) (tbctrl
->free
- trace_buffer_lo
),
1393 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1394 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1395 (int) (trace_buffer_hi
- trace_buffer_lo
));
1397 /* The algorithm here is to keep trying to get a contiguous block of
1398 the requested size, possibly discarding older traceframes to free
1399 up space. Since free space might come in one or two pieces,
1400 depending on whether discarded traceframes wrapped around at the
1401 high end of the buffer, we test both pieces after each
1405 /* First, if we have two free parts, try the upper one first. */
1406 if (tbctrl
->end_free
< tbctrl
->free
)
1408 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1409 /* We have enough in the upper part. */
1413 /* Our high part of free space wasn't enough. Give up
1414 on it for now, set wraparound. We will recover the
1415 space later, if/when the wrapped-around traceframe is
1417 trace_debug ("Upper part too small, setting wraparound");
1418 tbctrl
->wrap
= tbctrl
->free
;
1419 tbctrl
->free
= trace_buffer_lo
;
1423 /* The normal case. */
1424 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1427 #ifdef IN_PROCESS_AGENT
1428 /* The IP Agent's buffer is always circular. It isn't used
1429 currently, but `circular_trace_buffer' could represent
1430 GDBserver's mode. If we didn't find space, ask GDBserver to
1433 flush_trace_buffer ();
1437 trace_debug ("gdbserver flushed buffer, retrying");
1441 /* GDBserver cancelled the tracing. Bail out as well. */
1444 /* If we're here, then neither part is big enough, and
1445 non-circular trace buffers are now full. */
1446 if (!circular_trace_buffer
)
1448 trace_debug ("Not enough space in the trace buffer");
1452 trace_debug ("Need more space in the trace buffer");
1454 /* If we have a circular buffer, we can try discarding the
1455 oldest traceframe and see if that helps. */
1456 oldest
= FIRST_TRACEFRAME ();
1457 if (oldest
->tpnum
== 0)
1459 /* Not good; we have no traceframes to free. Perhaps we're
1460 asking for a block that is larger than the buffer? In
1461 any case, give up. */
1462 trace_debug ("No traceframes to discard");
1466 /* We don't run this code in the in-process agent currently.
1467 E.g., we could leave the in-process agent in autonomous
1468 circular mode if we only have fast tracepoints. If we do
1469 that, then this bit becomes racy with GDBserver, which also
1470 writes to this counter. */
1471 --traceframe_write_count
;
1473 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1474 /* If we freed the traceframe that wrapped around, go back
1475 to the non-wrap case. */
1476 if (new_start
< tbctrl
->start
)
1478 trace_debug ("Discarding past the wraparound");
1479 tbctrl
->wrap
= trace_buffer_hi
;
1481 tbctrl
->start
= new_start
;
1482 tbctrl
->end_free
= tbctrl
->start
;
1484 trace_debug ("Discarded a traceframe\n"
1485 "Trace buffer [%d], start=%d free=%d "
1486 "endfree=%d wrap=%d hi=%d",
1488 (int) (tbctrl
->start
- trace_buffer_lo
),
1489 (int) (tbctrl
->free
- trace_buffer_lo
),
1490 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1491 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1492 (int) (trace_buffer_hi
- trace_buffer_lo
));
1494 /* Now go back around the loop. The discard might have resulted
1495 in either one or two pieces of free space, so we want to try
1496 both before freeing any more traceframes. */
1500 /* If we get here, we know we can provide the asked-for space. */
1502 rslt
= tbctrl
->free
;
1504 /* Adjust the request back down, now that we know we have space for
1505 the marker, but don't commit to AMT yet, we may still need to
1506 restart the operation if GDBserver touches the trace buffer
1507 (obviously only important in the in-process agent's version). */
1508 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1510 /* Or not. If GDBserver changed the trace buffer behind our back,
1511 we get to restart a new allocation attempt. */
1513 #ifdef IN_PROCESS_AGENT
1514 /* Build the tentative token. */
1515 commit_count
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) + 0x100)
1516 & GDBSERVER_FLUSH_COUNT_MASK_CURR
);
1517 commit
= (((prev
& GDBSERVER_FLUSH_COUNT_MASK_CURR
) << 12)
1521 /* Try to commit it. */
1522 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1523 if (readout
!= prev
)
1525 trace_debug ("GDBserver has touched the trace buffer, restarting."
1526 " (prev=%08x, commit=%08x, readout=%08x)",
1527 prev
, commit
, readout
);
1531 /* Hold your horses here. Even if that change was committed,
1532 GDBserver could come in, and clobber it. We need to hold to be
1533 able to tell if GDBserver clobbers before or after we committed
1534 the change. Whenever GDBserver goes about touching the IPA
1535 buffer, it sets a breakpoint in this routine, so we have a sync
1537 about_to_request_buffer_space ();
1539 /* Check if the change has been effective, even if GDBserver stopped
1540 us at the breakpoint. */
1543 unsigned int refetch
;
1547 refetch
= trace_buffer_ctrl_curr
;
1549 if (refetch
== commit
1550 || ((refetch
& GDBSERVER_FLUSH_COUNT_MASK_PREV
) >> 12) == commit_count
)
1553 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1554 "readout=%08x, refetch=%08x)",
1555 prev
, commit
, readout
, refetch
);
1559 trace_debug ("GDBserver has touched the trace buffer, not effective."
1560 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1561 prev
, commit
, readout
, refetch
);
1567 /* We have a new piece of the trace buffer. Hurray! */
1569 /* Add an EOB marker just past this allocation. */
1570 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1571 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1573 /* Adjust the request back down, now that we know we have space for
1575 amt
-= sizeof (struct traceframe
);
1579 trace_debug ("Allocated %d bytes", (int) amt
);
1580 trace_debug ("Trace buffer [%d] start=%d free=%d "
1581 "endfree=%d wrap=%d hi=%d",
1583 (int) (tbctrl
->start
- trace_buffer_lo
),
1584 (int) (tbctrl
->free
- trace_buffer_lo
),
1585 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1586 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1587 (int) (trace_buffer_hi
- trace_buffer_lo
));
1593 #ifndef IN_PROCESS_AGENT
1595 /* Return the total free space. This is not necessarily the largest
1596 block we can allocate, because of the two-part case. */
1601 if (trace_buffer_free
<= trace_buffer_end_free
)
1602 return trace_buffer_end_free
- trace_buffer_free
;
1604 return ((trace_buffer_end_free
- trace_buffer_lo
)
1605 + (trace_buffer_hi
- trace_buffer_free
));
1608 /* An 'S' in continuation packets indicates remainder are for
1611 static int seen_step_action_flag
;
1613 /* Create a tracepoint (location) with given number and address. Add this
1614 new tracepoint to list and sort this list. */
1616 static struct tracepoint
*
1617 add_tracepoint (int num
, CORE_ADDR addr
)
1619 struct tracepoint
*tpoint
, **tp_next
;
1621 tpoint
= xmalloc (sizeof (struct tracepoint
));
1622 tpoint
->number
= num
;
1623 tpoint
->address
= addr
;
1624 tpoint
->numactions
= 0;
1625 tpoint
->actions
= NULL
;
1626 tpoint
->actions_str
= NULL
;
1627 tpoint
->cond
= NULL
;
1628 tpoint
->num_step_actions
= 0;
1629 tpoint
->step_actions
= NULL
;
1630 tpoint
->step_actions_str
= NULL
;
1631 /* Start all off as regular (slow) tracepoints. */
1632 tpoint
->type
= trap_tracepoint
;
1633 tpoint
->orig_size
= -1;
1634 tpoint
->source_strings
= NULL
;
1635 tpoint
->compiled_cond
= 0;
1636 tpoint
->handle
= NULL
;
1637 tpoint
->next
= NULL
;
1639 /* Find a place to insert this tracepoint into list in order to keep
1640 the tracepoint list still in the ascending order. There may be
1641 multiple tracepoints at the same address as TPOINT's, and this
1642 guarantees TPOINT is inserted after all the tracepoints which are
1643 set at the same address. For example, fast tracepoints A, B, C are
1644 set at the same address, and D is to be insert at the same place as
1647 -->| A |--> | B |-->| C |->...
1649 One jump pad was created for tracepoint A, B, and C, and the target
1650 address of A is referenced/used in jump pad. So jump pad will let
1651 inferior jump to A. If D is inserted in front of A, like this,
1653 -->| D |-->| A |--> | B |-->| C |->...
1655 without updating jump pad, D is not reachable during collect, which
1656 is wrong. As we can see, the order of B, C and D doesn't matter, but
1657 A should always be the `first' one. */
1658 for (tp_next
= &tracepoints
;
1659 (*tp_next
) != NULL
&& (*tp_next
)->address
<= tpoint
->address
;
1660 tp_next
= &(*tp_next
)->next
)
1662 tpoint
->next
= *tp_next
;
1664 last_tracepoint
= tpoint
;
1666 seen_step_action_flag
= 0;
1671 #ifndef IN_PROCESS_AGENT
1673 /* Return the tracepoint with the given number and address, or NULL. */
1675 static struct tracepoint
*
1676 find_tracepoint (int id
, CORE_ADDR addr
)
1678 struct tracepoint
*tpoint
;
1680 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1681 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1687 /* Remove TPOINT from global list. */
1690 remove_tracepoint (struct tracepoint
*tpoint
)
1692 struct tracepoint
*tp
, *tp_prev
;
1694 for (tp
= tracepoints
, tp_prev
= NULL
; tp
&& tp
!= tpoint
;
1695 tp_prev
= tp
, tp
= tp
->next
)
1701 tp_prev
->next
= tp
->next
;
1703 tracepoints
= tp
->next
;
1709 /* There may be several tracepoints with the same number (because they
1710 are "locations", in GDB parlance); return the next one after the
1711 given tracepoint, or search from the beginning of the list if the
1712 first argument is NULL. */
1714 static struct tracepoint
*
1715 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1717 struct tracepoint
*tpoint
;
1720 tpoint
= prev_tp
->next
;
1722 tpoint
= tracepoints
;
1723 for (; tpoint
; tpoint
= tpoint
->next
)
1724 if (tpoint
->number
== num
)
1733 save_string (const char *str
, size_t len
)
1737 s
= xmalloc (len
+ 1);
1738 memcpy (s
, str
, len
);
1744 /* Append another action to perform when the tracepoint triggers. */
1747 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1753 seen_step_action_flag
= 1;
1761 char *act_start
= act
;
1762 struct tracepoint_action
*action
= NULL
;
1768 struct collect_memory_action
*maction
;
1772 maction
= xmalloc (sizeof *maction
);
1773 maction
->base
.type
= *act
;
1774 action
= &maction
->base
;
1777 is_neg
= (*act
== '-');
1780 act
= unpack_varlen_hex (act
, &basereg
);
1782 act
= unpack_varlen_hex (act
, &maction
->addr
);
1784 act
= unpack_varlen_hex (act
, &maction
->len
);
1785 maction
->basereg
= (is_neg
1788 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1789 pulongest (maction
->len
),
1790 paddress (maction
->addr
), maction
->basereg
);
1795 struct collect_registers_action
*raction
;
1797 raction
= xmalloc (sizeof *raction
);
1798 raction
->base
.type
= *act
;
1799 action
= &raction
->base
;
1801 trace_debug ("Want to collect registers");
1803 /* skip past hex digits of mask for now */
1804 while (isxdigit(*act
))
1810 struct collect_static_trace_data_action
*raction
;
1812 raction
= xmalloc (sizeof *raction
);
1813 raction
->base
.type
= *act
;
1814 action
= &raction
->base
;
1816 trace_debug ("Want to collect static trace data");
1821 trace_debug ("Unexpected step action, ignoring");
1826 struct eval_expr_action
*xaction
;
1828 xaction
= xmalloc (sizeof (*xaction
));
1829 xaction
->base
.type
= *act
;
1830 action
= &xaction
->base
;
1832 trace_debug ("Want to evaluate expression");
1833 xaction
->expr
= gdb_parse_agent_expr (&act
);
1837 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1846 if (seen_step_action_flag
)
1848 tpoint
->num_step_actions
++;
1850 tpoint
->step_actions
1851 = xrealloc (tpoint
->step_actions
,
1852 (sizeof (*tpoint
->step_actions
)
1853 * tpoint
->num_step_actions
));
1854 tpoint
->step_actions_str
1855 = xrealloc (tpoint
->step_actions_str
,
1856 (sizeof (*tpoint
->step_actions_str
)
1857 * tpoint
->num_step_actions
));
1858 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1859 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1860 = save_string (act_start
, act
- act_start
);
1864 tpoint
->numactions
++;
1866 = xrealloc (tpoint
->actions
,
1867 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1869 = xrealloc (tpoint
->actions_str
,
1870 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1871 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1872 tpoint
->actions_str
[tpoint
->numactions
- 1]
1873 = save_string (act_start
, act
- act_start
);
1880 /* Find or create a trace state variable with the given number. */
1882 static struct trace_state_variable
*
1883 get_trace_state_variable (int num
)
1885 struct trace_state_variable
*tsv
;
1887 #ifdef IN_PROCESS_AGENT
1888 /* Search for an existing variable. */
1889 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1890 if (tsv
->number
== num
)
1894 /* Search for an existing variable. */
1895 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1896 if (tsv
->number
== num
)
1902 /* Find or create a trace state variable with the given number. */
1904 static struct trace_state_variable
*
1905 create_trace_state_variable (int num
, int gdb
)
1907 struct trace_state_variable
*tsv
;
1909 tsv
= get_trace_state_variable (num
);
1913 /* Create a new variable. */
1914 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1916 tsv
->initial_value
= 0;
1920 #ifdef IN_PROCESS_AGENT
1923 tsv
->next
= alloced_trace_state_variables
;
1924 alloced_trace_state_variables
= tsv
;
1929 tsv
->next
= trace_state_variables
;
1930 trace_state_variables
= tsv
;
1935 IP_AGENT_EXPORT LONGEST
1936 get_trace_state_variable_value (int num
)
1938 struct trace_state_variable
*tsv
;
1940 tsv
= get_trace_state_variable (num
);
1944 trace_debug ("No trace state variable %d, skipping value get", num
);
1948 /* Call a getter function if we have one. While it's tempting to
1949 set up something to only call the getter once per tracepoint hit,
1950 it could run afoul of thread races. Better to let the getter
1951 handle it directly, if necessary to worry about it. */
1953 tsv
->value
= (tsv
->getter
) ();
1955 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1956 num
, plongest (tsv
->value
));
1961 IP_AGENT_EXPORT
void
1962 set_trace_state_variable_value (int num
, LONGEST val
)
1964 struct trace_state_variable
*tsv
;
1966 tsv
= get_trace_state_variable (num
);
1970 trace_debug ("No trace state variable %d, skipping value set", num
);
1978 agent_get_trace_state_variable_value (int num
)
1980 return get_trace_state_variable_value (num
);
1984 agent_set_trace_state_variable_value (int num
, LONGEST val
)
1986 set_trace_state_variable_value (num
, val
);
1990 set_trace_state_variable_name (int num
, const char *name
)
1992 struct trace_state_variable
*tsv
;
1994 tsv
= get_trace_state_variable (num
);
1998 trace_debug ("No trace state variable %d, skipping name set", num
);
2002 tsv
->name
= (char *) name
;
2006 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
2008 struct trace_state_variable
*tsv
;
2010 tsv
= get_trace_state_variable (num
);
2014 trace_debug ("No trace state variable %d, skipping getter set", num
);
2018 tsv
->getter
= getter
;
2021 /* Add a raw traceframe for the given tracepoint. */
2023 static struct traceframe
*
2024 add_traceframe (struct tracepoint
*tpoint
)
2026 struct traceframe
*tframe
;
2028 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
2033 tframe
->tpnum
= tpoint
->number
;
2034 tframe
->data_size
= 0;
2039 /* Add a block to the traceframe currently being worked on. */
2041 static unsigned char *
2042 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2044 unsigned char *block
;
2049 block
= trace_buffer_alloc (amt
);
2054 tframe
->data_size
+= amt
;
2059 /* Flag that the current traceframe is finished. */
2062 finish_traceframe (struct traceframe
*tframe
)
2064 ++traceframe_write_count
;
2065 ++traceframes_created
;
2068 #ifndef IN_PROCESS_AGENT
2070 /* Given a traceframe number NUM, find the NUMth traceframe in the
2073 static struct traceframe
*
2074 find_traceframe (int num
)
2076 struct traceframe
*tframe
;
2079 for (tframe
= FIRST_TRACEFRAME ();
2081 tframe
= NEXT_TRACEFRAME (tframe
))
2092 get_traceframe_address (struct traceframe
*tframe
)
2095 struct tracepoint
*tpoint
;
2097 addr
= traceframe_get_pc (tframe
);
2102 /* Fallback strategy, will be incorrect for while-stepping frames
2103 and multi-location tracepoints. */
2104 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2105 return tpoint
->address
;
2108 /* Search for the next traceframe whose address is inside or outside
2111 static struct traceframe
*
2112 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2115 struct traceframe
*tframe
;
2118 *tfnump
= current_traceframe
+ 1;
2119 tframe
= find_traceframe (*tfnump
);
2120 /* The search is not supposed to wrap around. */
2127 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2129 tfaddr
= get_traceframe_address (tframe
);
2131 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2132 : (lo
> tfaddr
|| tfaddr
> hi
))
2141 /* Search for the next traceframe recorded by the given tracepoint.
2142 Note that for multi-location tracepoints, this will find whatever
2143 location appears first. */
2145 static struct traceframe
*
2146 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2148 struct traceframe
*tframe
;
2150 *tfnump
= current_traceframe
+ 1;
2151 tframe
= find_traceframe (*tfnump
);
2152 /* The search is not supposed to wrap around. */
2159 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2161 if (tframe
->tpnum
== num
)
2172 #ifndef IN_PROCESS_AGENT
2174 /* Clear all past trace state. */
2177 cmd_qtinit (char *packet
)
2179 struct trace_state_variable
*tsv
, *prev
, *next
;
2181 /* Make sure we don't try to read from a trace frame. */
2182 current_traceframe
= -1;
2184 trace_debug ("Initializing the trace");
2186 clear_installed_tracepoints ();
2187 clear_readonly_regions ();
2190 last_tracepoint
= NULL
;
2192 /* Clear out any leftover trace state variables. Ones with target
2193 defined getters should be kept however. */
2195 tsv
= trace_state_variables
;
2198 trace_debug ("Looking at var %d", tsv
->number
);
2199 if (tsv
->getter
== NULL
)
2205 trace_state_variables
= next
;
2206 trace_debug ("Deleting var %d", tsv
->number
);
2217 clear_trace_buffer ();
2218 clear_inferior_trace_buffer ();
2223 /* Unprobe the UST marker at ADDRESS. */
2226 unprobe_marker_at (CORE_ADDR address
)
2228 char cmd
[IPA_CMD_BUF_SIZE
];
2230 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2231 run_inferior_command (cmd
);
2234 /* Restore the program to its pre-tracing state. This routine may be called
2235 in error situations, so it needs to be careful about only restoring
2236 from known-valid bits. */
2239 clear_installed_tracepoints (void)
2241 struct tracepoint
*tpoint
;
2242 struct tracepoint
*prev_stpoint
;
2245 cancel_breakpoints ();
2247 prev_stpoint
= NULL
;
2249 /* Restore any bytes overwritten by tracepoints. */
2250 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2252 /* Catch the case where we might try to remove a tracepoint that
2253 was never actually installed. */
2254 if (tpoint
->handle
== NULL
)
2256 trace_debug ("Tracepoint %d at 0x%s was "
2257 "never installed, nothing to clear",
2258 tpoint
->number
, paddress (tpoint
->address
));
2262 switch (tpoint
->type
)
2264 case trap_tracepoint
:
2265 delete_breakpoint (tpoint
->handle
);
2267 case fast_tracepoint
:
2268 delete_fast_tracepoint_jump (tpoint
->handle
);
2270 case static_tracepoint
:
2271 if (prev_stpoint
!= NULL
2272 && prev_stpoint
->address
== tpoint
->address
)
2273 /* Nothing to do. We already unprobed a tracepoint set at
2274 this marker address (and there can only be one probe
2279 unprobe_marker_at (tpoint
->address
);
2280 prev_stpoint
= tpoint
;
2285 tpoint
->handle
= NULL
;
2291 /* Parse a packet that defines a tracepoint. */
2294 cmd_qtdp (char *own_buf
)
2297 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2298 int trail_hyphen
= 0;
2302 struct tracepoint
*tpoint
;
2304 char *packet
= own_buf
;
2306 packet
+= strlen ("QTDP:");
2308 /* A hyphen at the beginning marks a packet specifying actions for a
2309 tracepoint already supplied. */
2316 packet
= unpack_varlen_hex (packet
, &num
);
2317 ++packet
; /* skip a colon */
2318 packet
= unpack_varlen_hex (packet
, &addr
);
2319 ++packet
; /* skip a colon */
2321 /* See if we already have this tracepoint. */
2322 tpoint
= find_tracepoint (num
, addr
);
2326 /* Duplicate tracepoints are never allowed. */
2329 trace_debug ("Tracepoint error: tracepoint %d"
2330 " at 0x%s already exists",
2331 (int) num
, paddress (addr
));
2332 write_enn (own_buf
);
2336 tpoint
= add_tracepoint (num
, addr
);
2338 tpoint
->enabled
= (*packet
== 'E');
2339 ++packet
; /* skip 'E' */
2340 ++packet
; /* skip a colon */
2341 packet
= unpack_varlen_hex (packet
, &count
);
2342 tpoint
->step_count
= count
;
2343 ++packet
; /* skip a colon */
2344 packet
= unpack_varlen_hex (packet
, &count
);
2345 tpoint
->pass_count
= count
;
2346 /* See if we have any of the additional optional fields. */
2347 while (*packet
== ':')
2352 tpoint
->type
= fast_tracepoint
;
2354 packet
= unpack_varlen_hex (packet
, &count
);
2355 tpoint
->orig_size
= count
;
2357 else if (*packet
== 'S')
2359 tpoint
->type
= static_tracepoint
;
2362 else if (*packet
== 'X')
2364 actparm
= (char *) packet
;
2365 tpoint
->cond
= gdb_parse_agent_expr (&actparm
);
2368 else if (*packet
== '-')
2370 else if (*packet
== '\0')
2373 trace_debug ("Unknown optional tracepoint field");
2378 trace_debug ("Also has actions\n");
2381 trace_debug ("Defined %stracepoint %d at 0x%s, "
2382 "enabled %d step %ld pass %ld",
2383 tpoint
->type
== fast_tracepoint
? "fast "
2384 : tpoint
->type
== static_tracepoint
? "static " : "",
2385 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2386 tpoint
->step_count
, tpoint
->pass_count
);
2389 add_tracepoint_action (tpoint
, packet
);
2392 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2393 (int) num
, paddress (addr
));
2394 write_enn (own_buf
);
2398 /* Install tracepoint during tracing only once for each tracepoint location.
2399 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2400 determine the last QTDP packet for one tracepoint location by checking
2401 trailing hyphen in QTDP packet. */
2402 if (tracing
&& !trail_hyphen
)
2404 /* Pause all threads temporarily while we patch tracepoints. */
2407 /* download_tracepoint will update global `tracepoints'
2408 list, so it is unsafe to leave threads in jump pad. */
2409 stabilize_threads ();
2411 /* Freeze threads. */
2414 download_tracepoint (tpoint
);
2415 install_tracepoint (tpoint
, own_buf
);
2416 if (strcmp (own_buf
, "OK") != 0)
2417 remove_tracepoint (tpoint
);
2427 cmd_qtdpsrc (char *own_buf
)
2429 ULONGEST num
, addr
, start
, slen
;
2430 struct tracepoint
*tpoint
;
2431 char *packet
= own_buf
;
2432 char *saved
, *srctype
, *src
;
2434 struct source_string
*last
, *newlast
;
2436 packet
+= strlen ("QTDPsrc:");
2438 packet
= unpack_varlen_hex (packet
, &num
);
2439 ++packet
; /* skip a colon */
2440 packet
= unpack_varlen_hex (packet
, &addr
);
2441 ++packet
; /* skip a colon */
2443 /* See if we already have this tracepoint. */
2444 tpoint
= find_tracepoint (num
, addr
);
2448 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2449 (int) num
, paddress (addr
));
2450 write_enn (own_buf
);
2455 packet
= strchr (packet
, ':');
2456 srctype
= xmalloc (packet
- saved
+ 1);
2457 memcpy (srctype
, saved
, packet
- saved
);
2458 srctype
[packet
- saved
] = '\0';
2460 packet
= unpack_varlen_hex (packet
, &start
);
2461 ++packet
; /* skip a colon */
2462 packet
= unpack_varlen_hex (packet
, &slen
);
2463 ++packet
; /* skip a colon */
2464 src
= xmalloc (slen
+ 1);
2465 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2468 newlast
= xmalloc (sizeof (struct source_string
));
2469 newlast
->type
= srctype
;
2471 newlast
->next
= NULL
;
2472 /* Always add a source string to the end of the list;
2473 this keeps sequences of actions/commands in the right
2475 if (tpoint
->source_strings
)
2477 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2479 last
->next
= newlast
;
2482 tpoint
->source_strings
= newlast
;
2488 cmd_qtdv (char *own_buf
)
2490 ULONGEST num
, val
, builtin
;
2493 struct trace_state_variable
*tsv
;
2494 char *packet
= own_buf
;
2496 packet
+= strlen ("QTDV:");
2498 packet
= unpack_varlen_hex (packet
, &num
);
2499 ++packet
; /* skip a colon */
2500 packet
= unpack_varlen_hex (packet
, &val
);
2501 ++packet
; /* skip a colon */
2502 packet
= unpack_varlen_hex (packet
, &builtin
);
2503 ++packet
; /* skip a colon */
2505 nbytes
= strlen (packet
) / 2;
2506 varname
= xmalloc (nbytes
+ 1);
2507 nbytes
= unhexify (varname
, packet
, nbytes
);
2508 varname
[nbytes
] = '\0';
2510 tsv
= create_trace_state_variable (num
, 1);
2511 tsv
->initial_value
= (LONGEST
) val
;
2512 tsv
->name
= varname
;
2514 set_trace_state_variable_value (num
, (LONGEST
) val
);
2520 cmd_qtenable_disable (char *own_buf
, int enable
)
2522 char *packet
= own_buf
;
2524 struct tracepoint
*tp
;
2526 packet
+= strlen (enable
? "QTEnable:" : "QTDisable:");
2527 packet
= unpack_varlen_hex (packet
, &num
);
2528 ++packet
; /* skip a colon */
2529 packet
= unpack_varlen_hex (packet
, &addr
);
2531 tp
= find_tracepoint (num
, addr
);
2535 if ((enable
&& tp
->enabled
) || (!enable
&& !tp
->enabled
))
2537 trace_debug ("Tracepoint %d at 0x%s is already %s",
2538 (int) num
, paddress (addr
),
2539 enable
? "enabled" : "disabled");
2544 trace_debug ("%s tracepoint %d at 0x%s",
2545 enable
? "Enabling" : "Disabling",
2546 (int) num
, paddress (addr
));
2548 tp
->enabled
= enable
;
2550 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
2553 int offset
= offsetof (struct tracepoint
, enabled
);
2554 CORE_ADDR obj_addr
= tp
->obj_addr_on_target
+ offset
;
2556 ret
= prepare_to_access_memory ();
2559 trace_debug ("Failed to temporarily stop inferior threads");
2560 write_enn (own_buf
);
2564 ret
= write_inferior_integer (obj_addr
, enable
);
2565 done_accessing_memory ();
2569 trace_debug ("Cannot write enabled flag into "
2570 "inferior process memory");
2571 write_enn (own_buf
);
2580 trace_debug ("Tracepoint %d at 0x%s not found",
2581 (int) num
, paddress (addr
));
2582 write_enn (own_buf
);
2587 cmd_qtv (char *own_buf
)
2592 char *packet
= own_buf
;
2594 packet
+= strlen ("qTV:");
2595 unpack_varlen_hex (packet
, &num
);
2597 if (current_traceframe
>= 0)
2599 err
= traceframe_read_tsv ((int) num
, &val
);
2602 strcpy (own_buf
, "U");
2606 /* Only make tsv's be undefined before the first trace run. After a
2607 trace run is over, the user might want to see the last value of
2608 the tsv, and it might not be available in a traceframe. */
2609 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2611 strcpy (own_buf
, "U");
2615 val
= get_trace_state_variable_value (num
);
2617 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2620 /* Clear out the list of readonly regions. */
2623 clear_readonly_regions (void)
2625 struct readonly_region
*roreg
;
2627 while (readonly_regions
)
2629 roreg
= readonly_regions
;
2630 readonly_regions
= readonly_regions
->next
;
2635 /* Parse the collection of address ranges whose contents GDB believes
2636 to be unchanging and so can be read directly from target memory
2637 even while looking at a traceframe. */
2640 cmd_qtro (char *own_buf
)
2642 ULONGEST start
, end
;
2643 struct readonly_region
*roreg
;
2644 char *packet
= own_buf
;
2646 trace_debug ("Want to mark readonly regions");
2648 clear_readonly_regions ();
2650 packet
+= strlen ("QTro");
2652 while (*packet
== ':')
2654 ++packet
; /* skip a colon */
2655 packet
= unpack_varlen_hex (packet
, &start
);
2656 ++packet
; /* skip a comma */
2657 packet
= unpack_varlen_hex (packet
, &end
);
2658 roreg
= xmalloc (sizeof (struct readonly_region
));
2659 roreg
->start
= start
;
2661 roreg
->next
= readonly_regions
;
2662 readonly_regions
= roreg
;
2663 trace_debug ("Added readonly region from 0x%s to 0x%s",
2664 paddress (roreg
->start
), paddress (roreg
->end
));
2670 /* Test to see if the given range is in our list of readonly ranges.
2671 We only test for being entirely within a range, GDB is not going to
2672 send a single memory packet that spans multiple regions. */
2675 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2677 struct readonly_region
*roreg
;
2679 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2680 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2686 /* The maximum size of a jump pad entry. */
2687 static const int max_jump_pad_size
= 0x100;
2689 static CORE_ADDR gdb_jump_pad_head
;
2691 /* Return the address of the next free jump space. */
2694 get_jump_space_head (void)
2696 if (gdb_jump_pad_head
== 0)
2698 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2699 &gdb_jump_pad_head
))
2700 fatal ("error extracting jump_pad_buffer");
2703 return gdb_jump_pad_head
;
2706 /* Reserve USED bytes from the jump space. */
2709 claim_jump_space (ULONGEST used
)
2711 trace_debug ("claim_jump_space reserves %s bytes at %s",
2712 pulongest (used
), paddress (gdb_jump_pad_head
));
2713 gdb_jump_pad_head
+= used
;
2716 static CORE_ADDR trampoline_buffer_head
= 0;
2717 static CORE_ADDR trampoline_buffer_tail
;
2719 /* Reserve USED bytes from the trampoline buffer and return the
2720 address of the start of the reserved space in TRAMPOLINE. Returns
2721 non-zero if the space is successfully claimed. */
2724 claim_trampoline_space (ULONGEST used
, CORE_ADDR
*trampoline
)
2726 if (!trampoline_buffer_head
)
2728 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
2729 &trampoline_buffer_tail
))
2731 fatal ("error extracting trampoline_buffer");
2735 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2736 &trampoline_buffer_head
))
2738 fatal ("error extracting trampoline_buffer_end");
2743 /* Start claiming space from the top of the trampoline space. If
2744 the space is located at the bottom of the virtual address space,
2745 this reduces the possibility that corruption will occur if a null
2746 pointer is used to write to memory. */
2747 if (trampoline_buffer_head
- trampoline_buffer_tail
< used
)
2749 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2754 trampoline_buffer_head
-= used
;
2756 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2757 pulongest (used
), paddress (trampoline_buffer_head
));
2759 *trampoline
= trampoline_buffer_head
;
2763 /* Returns non-zero if there is space allocated for use in trampolines
2764 for fast tracepoints. */
2767 have_fast_tracepoint_trampoline_buffer (char *buf
)
2769 CORE_ADDR trampoline_end
, errbuf
;
2771 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
2774 fatal ("error extracting trampoline_buffer_end");
2781 strcpy (buf
, "was claiming");
2782 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_error
,
2785 fatal ("error extracting errbuf");
2789 read_inferior_memory (errbuf
, (unsigned char *) buf
, 100);
2792 return trampoline_end
!= 0;
2795 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2796 the command fails, or 0 otherwise. If the command ran
2797 successfully, but probing the marker failed, ERROUT will be filled
2798 with the error to reply to GDB, and -1 is also returned. This
2799 allows directly passing IPA errors to GDB. */
2802 probe_marker_at (CORE_ADDR address
, char *errout
)
2804 char cmd
[IPA_CMD_BUF_SIZE
];
2807 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2808 err
= run_inferior_command (cmd
);
2814 strcpy (errout
, cmd
);
2823 clone_fast_tracepoint (struct tracepoint
*to
, const struct tracepoint
*from
)
2825 to
->jump_pad
= from
->jump_pad
;
2826 to
->jump_pad_end
= from
->jump_pad_end
;
2827 to
->trampoline
= from
->trampoline
;
2828 to
->trampoline_end
= from
->trampoline_end
;
2829 to
->adjusted_insn_addr
= from
->adjusted_insn_addr
;
2830 to
->adjusted_insn_addr_end
= from
->adjusted_insn_addr_end
;
2831 to
->handle
= from
->handle
;
2833 gdb_assert (from
->handle
);
2834 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump
*) from
->handle
);
2837 #define MAX_JUMP_SIZE 20
2839 /* Install fast tracepoint. Return 0 if successful, otherwise return
2843 install_fast_tracepoint (struct tracepoint
*tpoint
, char *errbuf
)
2845 CORE_ADDR jentry
, jump_entry
;
2846 CORE_ADDR trampoline
;
2847 ULONGEST trampoline_size
;
2849 /* The jump to the jump pad of the last fast tracepoint
2851 unsigned char fjump
[MAX_JUMP_SIZE
];
2852 ULONGEST fjump_size
;
2854 if (tpoint
->orig_size
< target_get_min_fast_tracepoint_insn_len ())
2856 trace_debug ("Requested a fast tracepoint on an instruction "
2857 "that is of less than the minimum length.");
2861 jentry
= jump_entry
= get_jump_space_head ();
2864 trampoline_size
= 0;
2866 /* Install the jump pad. */
2867 err
= install_fast_tracepoint_jump_pad (tpoint
->obj_addr_on_target
,
2869 ipa_sym_addrs
.addr_gdb_collect
,
2870 ipa_sym_addrs
.addr_collecting
,
2873 &trampoline
, &trampoline_size
,
2875 &tpoint
->adjusted_insn_addr
,
2876 &tpoint
->adjusted_insn_addr_end
,
2883 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
, fjump
,
2886 if (tpoint
->handle
!= NULL
)
2888 tpoint
->jump_pad
= jump_entry
;
2889 tpoint
->jump_pad_end
= jentry
;
2890 tpoint
->trampoline
= trampoline
;
2891 tpoint
->trampoline_end
= trampoline
+ trampoline_size
;
2893 /* Pad to 8-byte alignment. */
2894 jentry
= ((jentry
+ 7) & ~0x7);
2895 claim_jump_space (jentry
- jump_entry
);
2902 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2905 install_tracepoint (struct tracepoint
*tpoint
, char *own_buf
)
2907 tpoint
->handle
= NULL
;
2910 if (tpoint
->type
== trap_tracepoint
)
2912 /* Tracepoints are installed as memory breakpoints. Just go
2913 ahead and install the trap. The breakpoints module
2914 handles duplicated breakpoints, and the memory read
2915 routine handles un-patching traps from memory reads. */
2916 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2917 tracepoint_handler
);
2919 else if (tpoint
->type
== fast_tracepoint
|| tpoint
->type
== static_tracepoint
)
2921 struct tracepoint
*tp
;
2923 if (!agent_loaded_p ())
2925 trace_debug ("Requested a %s tracepoint, but fast "
2926 "tracepoints aren't supported.",
2927 tpoint
->type
== static_tracepoint
? "static" : "fast");
2928 write_e_ipa_not_loaded (own_buf
);
2931 if (tpoint
->type
== static_tracepoint
2932 && !in_process_agent_supports_ust ())
2934 trace_debug ("Requested a static tracepoint, but static "
2935 "tracepoints are not supported.");
2936 write_e_ust_not_loaded (own_buf
);
2940 /* Find another fast or static tracepoint at the same address. */
2941 for (tp
= tracepoints
; tp
; tp
= tp
->next
)
2943 if (tp
->address
== tpoint
->address
&& tp
->type
== tpoint
->type
2944 && tp
->number
!= tpoint
->number
)
2948 if (tpoint
->type
== fast_tracepoint
)
2950 if (tp
) /* TPOINT is installed at the same address as TP. */
2951 clone_fast_tracepoint (tpoint
, tp
);
2953 install_fast_tracepoint (tpoint
, own_buf
);
2958 tpoint
->handle
= (void *) -1;
2961 if (probe_marker_at (tpoint
->address
, own_buf
) == 0)
2962 tpoint
->handle
= (void *) -1;
2968 internal_error (__FILE__
, __LINE__
, "Unknown tracepoint type");
2970 if (tpoint
->handle
== NULL
)
2972 if (*own_buf
== '\0')
2973 write_enn (own_buf
);
2980 cmd_qtstart (char *packet
)
2982 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2984 trace_debug ("Starting the trace");
2986 /* Pause all threads temporarily while we patch tracepoints. */
2989 /* Get threads out of jump pads. Safe to do here, since this is a
2990 top level command. And, required to do here, since we're
2991 deleting/rewriting jump pads. */
2993 stabilize_threads ();
2995 /* Freeze threads. */
2998 /* Sync the fast tracepoints list in the inferior ftlib. */
2999 if (agent_loaded_p ())
3001 download_tracepoints ();
3002 download_trace_state_variables ();
3005 /* No previous fast tpoint yet. */
3006 prev_ftpoint
= NULL
;
3008 /* No previous static tpoint yet. */
3009 prev_stpoint
= NULL
;
3013 /* Install tracepoints. */
3014 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3016 /* Ensure all the hit counts start at zero. */
3017 tpoint
->hit_count
= 0;
3018 tpoint
->traceframe_usage
= 0;
3020 if (tpoint
->type
== trap_tracepoint
)
3022 /* Tracepoints are installed as memory breakpoints. Just go
3023 ahead and install the trap. The breakpoints module
3024 handles duplicated breakpoints, and the memory read
3025 routine handles un-patching traps from memory reads. */
3026 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
3027 tracepoint_handler
);
3029 else if (tpoint
->type
== fast_tracepoint
)
3031 if (maybe_write_ipa_not_loaded (packet
))
3033 trace_debug ("Requested a fast tracepoint, but fast "
3034 "tracepoints aren't supported.");
3038 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
3039 clone_fast_tracepoint (tpoint
, prev_ftpoint
);
3042 if (install_fast_tracepoint (tpoint
, packet
) == 0)
3043 prev_ftpoint
= tpoint
;
3046 else if (tpoint
->type
== static_tracepoint
)
3048 if (maybe_write_ipa_ust_not_loaded (packet
))
3050 trace_debug ("Requested a static tracepoint, but static "
3051 "tracepoints are not supported.");
3055 /* Can only probe a given marker once. */
3056 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
3058 tpoint
->handle
= (void *) -1;
3062 if (probe_marker_at (tpoint
->address
, packet
) == 0)
3064 tpoint
->handle
= (void *) -1;
3066 /* So that we can handle multiple static tracepoints
3067 at the same address easily. */
3068 prev_stpoint
= tpoint
;
3073 /* Any failure in the inner loop is sufficient cause to give
3075 if (tpoint
->handle
== NULL
)
3079 /* Any error in tracepoint insertion is unacceptable; better to
3080 address the problem now, than end up with a useless or misleading
3084 clear_installed_tracepoints ();
3085 if (*packet
== '\0')
3091 stopping_tracepoint
= NULL
;
3092 trace_buffer_is_full
= 0;
3093 expr_eval_result
= expr_eval_no_error
;
3094 error_tracepoint
= NULL
;
3095 tracing_start_time
= get_timestamp ();
3097 /* Tracing is now active, hits will now start being logged. */
3100 if (agent_loaded_p ())
3102 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
3103 fatal ("Error setting tracing variable in lib");
3105 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3107 fatal ("Error clearing stopping_tracepoint variable in lib");
3109 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
3110 fatal ("Error clearing trace_buffer_is_full variable in lib");
3112 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
3113 stop_tracing_handler
);
3114 if (stop_tracing_bkpt
== NULL
)
3115 error ("Error setting stop_tracing breakpoint");
3117 flush_trace_buffer_bkpt
3118 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
3119 flush_trace_buffer_handler
);
3120 if (flush_trace_buffer_bkpt
== NULL
)
3121 error ("Error setting flush_trace_buffer breakpoint");
3129 /* End a tracing run, filling in a stop reason to report back to GDB,
3130 and removing the tracepoints from the code. */
3137 trace_debug ("Tracing is already off, ignoring");
3141 trace_debug ("Stopping the trace");
3143 /* Pause all threads before removing fast jumps from memory,
3144 breakpoints, and touching IPA state variables (inferior memory).
3145 Some thread may hit the internal tracing breakpoints, or be
3146 collecting this moment, but that's ok, we don't release the
3147 tpoint object's memory or the jump pads here (we only do that
3148 when we're sure we can move all threads out of the jump pads).
3149 We can't now, since we may be getting here due to the inferior
3150 agent calling us. */
3152 /* Since we're removing breakpoints, cancel breakpoint hits,
3153 possibly related to the breakpoints we're about to delete. */
3154 cancel_breakpoints ();
3156 /* Stop logging. Tracepoints can still be hit, but they will not be
3159 if (agent_loaded_p ())
3161 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
3162 fatal ("Error clearing tracing variable in lib");
3165 tracing_stop_time
= get_timestamp ();
3166 tracing_stop_reason
= "t???";
3167 tracing_stop_tpnum
= 0;
3168 if (stopping_tracepoint
)
3170 trace_debug ("Stopping the trace because "
3171 "tracepoint %d was hit %ld times",
3172 stopping_tracepoint
->number
,
3173 stopping_tracepoint
->pass_count
);
3174 tracing_stop_reason
= "tpasscount";
3175 tracing_stop_tpnum
= stopping_tracepoint
->number
;
3177 else if (trace_buffer_is_full
)
3179 trace_debug ("Stopping the trace because the trace buffer is full");
3180 tracing_stop_reason
= "tfull";
3182 else if (expr_eval_result
!= expr_eval_no_error
)
3184 trace_debug ("Stopping the trace because of an expression eval error");
3185 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
3186 tracing_stop_tpnum
= error_tracepoint
->number
;
3188 #ifndef IN_PROCESS_AGENT
3189 else if (!gdb_connected ())
3191 trace_debug ("Stopping the trace because GDB disconnected");
3192 tracing_stop_reason
= "tdisconnected";
3197 trace_debug ("Stopping the trace because of a tstop command");
3198 tracing_stop_reason
= "tstop";
3201 stopping_tracepoint
= NULL
;
3202 error_tracepoint
= NULL
;
3204 /* Clear out the tracepoints. */
3205 clear_installed_tracepoints ();
3207 if (agent_loaded_p ())
3209 /* Pull in fast tracepoint trace frames from the inferior lib
3210 buffer into our buffer, even if our buffer is already full,
3211 because we want to present the full number of created frames
3212 in addition to what fit in the trace buffer. */
3213 upload_fast_traceframes ();
3216 if (stop_tracing_bkpt
!= NULL
)
3218 delete_breakpoint (stop_tracing_bkpt
);
3219 stop_tracing_bkpt
= NULL
;
3222 if (flush_trace_buffer_bkpt
!= NULL
)
3224 delete_breakpoint (flush_trace_buffer_bkpt
);
3225 flush_trace_buffer_bkpt
= NULL
;
3232 stop_tracing_handler (CORE_ADDR addr
)
3234 trace_debug ("lib hit stop_tracing");
3236 /* Don't actually handle it here. When we stop tracing we remove
3237 breakpoints from the inferior, and that is not allowed in a
3238 breakpoint handler (as the caller is walking the breakpoint
3244 flush_trace_buffer_handler (CORE_ADDR addr
)
3246 trace_debug ("lib hit flush_trace_buffer");
3251 cmd_qtstop (char *packet
)
3258 cmd_qtdisconnected (char *own_buf
)
3261 char *packet
= own_buf
;
3263 packet
+= strlen ("QTDisconnected:");
3265 unpack_varlen_hex (packet
, &setting
);
3269 disconnected_tracing
= setting
;
3273 cmd_qtframe (char *own_buf
)
3275 ULONGEST frame
, pc
, lo
, hi
, num
;
3277 struct traceframe
*tframe
;
3278 char *packet
= own_buf
;
3280 packet
+= strlen ("QTFrame:");
3282 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3284 packet
+= strlen ("pc:");
3285 unpack_varlen_hex (packet
, &pc
);
3286 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3287 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3289 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3291 packet
+= strlen ("range:");
3292 packet
= unpack_varlen_hex (packet
, &lo
);
3294 unpack_varlen_hex (packet
, &hi
);
3295 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3296 paddress (lo
), paddress (hi
));
3297 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3299 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3301 packet
+= strlen ("outside:");
3302 packet
= unpack_varlen_hex (packet
, &lo
);
3304 unpack_varlen_hex (packet
, &hi
);
3305 trace_debug ("Want to find next traceframe "
3306 "outside the range 0x%s to 0x%s",
3307 paddress (lo
), paddress (hi
));
3308 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3310 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3312 packet
+= strlen ("tdp:");
3313 unpack_varlen_hex (packet
, &num
);
3315 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3316 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3320 unpack_varlen_hex (packet
, &frame
);
3321 tfnum
= (int) frame
;
3324 trace_debug ("Want to stop looking at traceframes");
3325 current_traceframe
= -1;
3329 trace_debug ("Want to look at traceframe %d", tfnum
);
3330 tframe
= find_traceframe (tfnum
);
3335 current_traceframe
= tfnum
;
3336 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3339 sprintf (own_buf
, "F-1");
3343 cmd_qtstatus (char *packet
)
3345 char *stop_reason_rsp
= NULL
;
3346 char *buf1
, *buf2
, *buf3
, *str
;
3349 /* Translate the plain text of the notes back into hex for
3352 str
= (tracing_user_name
? tracing_user_name
: "");
3353 slen
= strlen (str
);
3354 buf1
= (char *) alloca (slen
* 2 + 1);
3355 hexify (buf1
, str
, slen
);
3357 str
= (tracing_notes
? tracing_notes
: "");
3358 slen
= strlen (str
);
3359 buf2
= (char *) alloca (slen
* 2 + 1);
3360 hexify (buf2
, str
, slen
);
3362 str
= (tracing_stop_note
? tracing_stop_note
: "");
3363 slen
= strlen (str
);
3364 buf3
= (char *) alloca (slen
* 2 + 1);
3365 hexify (buf3
, str
, slen
);
3367 trace_debug ("Returning trace status as %d, stop reason %s",
3368 tracing
, tracing_stop_reason
);
3370 if (agent_loaded_p ())
3374 upload_fast_traceframes ();
3379 stop_reason_rsp
= (char *) tracing_stop_reason
;
3381 /* The user visible error string in terror needs to be hex encoded.
3382 We leave it as plain string in `tracing_stop_reason' to ease
3384 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3386 const char *result_name
;
3390 result_name
= stop_reason_rsp
+ strlen ("terror:");
3391 hexstr_len
= strlen (result_name
) * 2;
3392 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3393 strcpy (p
, "terror:");
3395 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3398 /* If this was a forced stop, include any stop note that was supplied. */
3399 if (strcmp (stop_reason_rsp
, "tstop") == 0)
3401 stop_reason_rsp
= alloca (strlen ("tstop:") + strlen (buf3
) + 1);
3402 strcpy (stop_reason_rsp
, "tstop:");
3403 strcat (stop_reason_rsp
, buf3
);
3409 "tframes:%x;tcreated:%x;"
3410 "tfree:%x;tsize:%s;"
3413 "starttime:%s;stoptime:%s;"
3414 "username:%s:;notes:%s:",
3416 stop_reason_rsp
, tracing_stop_tpnum
,
3417 traceframe_count
, traceframes_created
,
3418 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3419 circular_trace_buffer
,
3420 disconnected_tracing
,
3421 plongest (tracing_start_time
), plongest (tracing_stop_time
),
3426 cmd_qtp (char *own_buf
)
3429 struct tracepoint
*tpoint
;
3430 char *packet
= own_buf
;
3432 packet
+= strlen ("qTP:");
3434 packet
= unpack_varlen_hex (packet
, &num
);
3435 ++packet
; /* skip a colon */
3436 packet
= unpack_varlen_hex (packet
, &addr
);
3438 /* See if we already have this tracepoint. */
3439 tpoint
= find_tracepoint (num
, addr
);
3443 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3444 (int) num
, paddress (addr
));
3445 write_enn (own_buf
);
3449 sprintf (own_buf
, "V%lx:%lx", tpoint
->hit_count
, tpoint
->traceframe_usage
);
3452 /* State variables to help return all the tracepoint bits. */
3453 static struct tracepoint
*cur_tpoint
;
3454 static int cur_action
;
3455 static int cur_step_action
;
3456 static struct source_string
*cur_source_string
;
3457 static struct trace_state_variable
*cur_tsv
;
3459 /* Compose a response that is an imitation of the syntax by which the
3460 tracepoint was originally downloaded. */
3463 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3467 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3468 paddress (tpoint
->address
),
3469 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3470 tpoint
->pass_count
);
3471 if (tpoint
->type
== fast_tracepoint
)
3472 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3473 else if (tpoint
->type
== static_tracepoint
)
3474 sprintf (packet
+ strlen (packet
), ":S");
3478 buf
= gdb_unparse_agent_expr (tpoint
->cond
);
3479 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3480 tpoint
->cond
->length
, buf
);
3485 /* Compose a response that is an imitation of the syntax by which the
3486 tracepoint action was originally downloaded (with the difference
3487 that due to the way we store the actions, this will output a packet
3488 per action, while GDB could have combined more than one action
3492 response_action (char *packet
, struct tracepoint
*tpoint
,
3493 char *taction
, int step
)
3495 sprintf (packet
, "%c%x:%s:%s",
3496 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3500 /* Compose a response that is an imitation of the syntax by which the
3501 tracepoint source piece was originally downloaded. */
3504 response_source (char *packet
,
3505 struct tracepoint
*tpoint
, struct source_string
*src
)
3510 len
= strlen (src
->str
);
3511 buf
= alloca (len
* 2 + 1);
3512 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3514 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3515 tpoint
->number
, paddress (tpoint
->address
),
3516 src
->type
, 0, len
, buf
);
3519 /* Return the first piece of tracepoint definition, and initialize the
3520 state machine that will iterate through all the tracepoint
3524 cmd_qtfp (char *packet
)
3526 trace_debug ("Returning first tracepoint definition piece");
3528 cur_tpoint
= tracepoints
;
3529 cur_action
= cur_step_action
= -1;
3530 cur_source_string
= NULL
;
3533 response_tracepoint (packet
, cur_tpoint
);
3535 strcpy (packet
, "l");
3538 /* Return additional pieces of tracepoint definition. Each action and
3539 stepping action must go into its own packet, because of packet size
3540 limits, and so we use state variables to deliver one piece at a
3544 cmd_qtsp (char *packet
)
3546 trace_debug ("Returning subsequent tracepoint definition piece");
3550 /* This case would normally never occur, but be prepared for
3552 strcpy (packet
, "l");
3554 else if (cur_action
< cur_tpoint
->numactions
- 1)
3557 response_action (packet
, cur_tpoint
,
3558 cur_tpoint
->actions_str
[cur_action
], 0);
3560 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3563 response_action (packet
, cur_tpoint
,
3564 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3566 else if ((cur_source_string
3567 ? cur_source_string
->next
3568 : cur_tpoint
->source_strings
))
3570 if (cur_source_string
)
3571 cur_source_string
= cur_source_string
->next
;
3573 cur_source_string
= cur_tpoint
->source_strings
;
3574 response_source (packet
, cur_tpoint
, cur_source_string
);
3578 cur_tpoint
= cur_tpoint
->next
;
3579 cur_action
= cur_step_action
= -1;
3580 cur_source_string
= NULL
;
3582 response_tracepoint (packet
, cur_tpoint
);
3584 strcpy (packet
, "l");
3588 /* Compose a response that is an imitation of the syntax by which the
3589 trace state variable was originally downloaded. */
3592 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3594 char *buf
= (char *) "";
3599 namelen
= strlen (tsv
->name
);
3600 buf
= alloca (namelen
* 2 + 1);
3601 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3604 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3605 tsv
->getter
? 1 : 0, buf
);
3608 /* Return the first trace state variable definition, and initialize
3609 the state machine that will iterate through all the tsv bits. */
3612 cmd_qtfv (char *packet
)
3614 trace_debug ("Returning first trace state variable definition");
3616 cur_tsv
= trace_state_variables
;
3619 response_tsv (packet
, cur_tsv
);
3621 strcpy (packet
, "l");
3624 /* Return additional trace state variable definitions. */
3627 cmd_qtsv (char *packet
)
3629 trace_debug ("Returning first trace state variable definition");
3633 /* This case would normally never occur, but be prepared for
3635 strcpy (packet
, "l");
3639 cur_tsv
= cur_tsv
->next
;
3641 response_tsv (packet
, cur_tsv
);
3643 strcpy (packet
, "l");
3646 strcpy (packet
, "l");
3649 /* Return the first static tracepoint marker, and initialize the state
3650 machine that will iterate through all the static tracepoints
3654 cmd_qtfstm (char *packet
)
3656 if (!maybe_write_ipa_ust_not_loaded (packet
))
3657 run_inferior_command (packet
);
3660 /* Return additional static tracepoints markers. */
3663 cmd_qtsstm (char *packet
)
3665 if (!maybe_write_ipa_ust_not_loaded (packet
))
3666 run_inferior_command (packet
);
3669 /* Return the definition of the static tracepoint at a given address.
3670 Result packet is the same as qTsST's. */
3673 cmd_qtstmat (char *packet
)
3675 if (!maybe_write_ipa_ust_not_loaded (packet
))
3676 run_inferior_command (packet
);
3679 /* Return the minimum instruction size needed for fast tracepoints as a
3680 hexadecimal number. */
3683 cmd_qtminftpilen (char *packet
)
3685 if (current_inferior
== NULL
)
3687 /* Indicate that the minimum length is currently unknown. */
3688 strcpy (packet
, "0");
3692 sprintf (packet
, "%x", target_get_min_fast_tracepoint_insn_len ());
3695 /* Respond to qTBuffer packet with a block of raw data from the trace
3696 buffer. GDB may ask for a lot, but we are allowed to reply with
3697 only as much as will fit within packet limits or whatever. */
3700 cmd_qtbuffer (char *own_buf
)
3702 ULONGEST offset
, num
, tot
;
3704 char *packet
= own_buf
;
3706 packet
+= strlen ("qTBuffer:");
3708 packet
= unpack_varlen_hex (packet
, &offset
);
3709 ++packet
; /* skip a comma */
3710 unpack_varlen_hex (packet
, &num
);
3712 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3713 (int) num
, pulongest (offset
));
3715 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3717 /* If we're right at the end, reply specially that we're done. */
3720 strcpy (own_buf
, "l");
3724 /* Object to any other out-of-bounds request. */
3727 write_enn (own_buf
);
3731 /* Compute the pointer corresponding to the given offset, accounting
3733 tbp
= trace_buffer_start
+ offset
;
3734 if (tbp
>= trace_buffer_wrap
)
3735 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3737 /* Trim to the remaining bytes if we're close to the end. */
3738 if (num
> tot
- offset
)
3741 /* Trim to available packet size. */
3742 if (num
>= (PBUFSIZ
- 16) / 2 )
3743 num
= (PBUFSIZ
- 16) / 2;
3745 convert_int_to_ascii (tbp
, own_buf
, num
);
3746 own_buf
[num
] = '\0';
3750 cmd_bigqtbuffer_circular (char *own_buf
)
3753 char *packet
= own_buf
;
3755 packet
+= strlen ("QTBuffer:circular:");
3757 unpack_varlen_hex (packet
, &val
);
3758 circular_trace_buffer
= val
;
3759 trace_debug ("Trace buffer is now %s",
3760 circular_trace_buffer
? "circular" : "linear");
3765 cmd_qtnotes (char *own_buf
)
3768 char *saved
, *user
, *notes
, *stopnote
;
3769 char *packet
= own_buf
;
3771 packet
+= strlen ("QTNotes:");
3775 if (strncmp ("user:", packet
, strlen ("user:")) == 0)
3777 packet
+= strlen ("user:");
3779 packet
= strchr (packet
, ';');
3780 nbytes
= (packet
- saved
) / 2;
3781 user
= xmalloc (nbytes
+ 1);
3782 nbytes
= unhexify (user
, saved
, nbytes
);
3783 user
[nbytes
] = '\0';
3784 ++packet
; /* skip the semicolon */
3785 trace_debug ("User is '%s'", user
);
3786 tracing_user_name
= user
;
3788 else if (strncmp ("notes:", packet
, strlen ("notes:")) == 0)
3790 packet
+= strlen ("notes:");
3792 packet
= strchr (packet
, ';');
3793 nbytes
= (packet
- saved
) / 2;
3794 notes
= xmalloc (nbytes
+ 1);
3795 nbytes
= unhexify (notes
, saved
, nbytes
);
3796 notes
[nbytes
] = '\0';
3797 ++packet
; /* skip the semicolon */
3798 trace_debug ("Notes is '%s'", notes
);
3799 tracing_notes
= notes
;
3801 else if (strncmp ("tstop:", packet
, strlen ("tstop:")) == 0)
3803 packet
+= strlen ("tstop:");
3805 packet
= strchr (packet
, ';');
3806 nbytes
= (packet
- saved
) / 2;
3807 stopnote
= xmalloc (nbytes
+ 1);
3808 nbytes
= unhexify (stopnote
, saved
, nbytes
);
3809 stopnote
[nbytes
] = '\0';
3810 ++packet
; /* skip the semicolon */
3811 trace_debug ("tstop note is '%s'", stopnote
);
3812 tracing_stop_note
= stopnote
;
3822 handle_tracepoint_general_set (char *packet
)
3824 if (strcmp ("QTinit", packet
) == 0)
3826 cmd_qtinit (packet
);
3829 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3834 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3836 cmd_qtdpsrc (packet
);
3839 else if (strncmp ("QTEnable:", packet
, strlen ("QTEnable:")) == 0)
3841 cmd_qtenable_disable (packet
, 1);
3844 else if (strncmp ("QTDisable:", packet
, strlen ("QTDisable:")) == 0)
3846 cmd_qtenable_disable (packet
, 0);
3849 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3854 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3859 else if (strcmp ("QTStart", packet
) == 0)
3861 cmd_qtstart (packet
);
3864 else if (strcmp ("QTStop", packet
) == 0)
3866 cmd_qtstop (packet
);
3869 else if (strncmp ("QTDisconnected:", packet
,
3870 strlen ("QTDisconnected:")) == 0)
3872 cmd_qtdisconnected (packet
);
3875 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3877 cmd_qtframe (packet
);
3880 else if (strncmp ("QTBuffer:circular:", packet
, strlen ("QTBuffer:circular:")) == 0)
3882 cmd_bigqtbuffer_circular (packet
);
3885 else if (strncmp ("QTNotes:", packet
, strlen ("QTNotes:")) == 0)
3887 cmd_qtnotes (packet
);
3895 handle_tracepoint_query (char *packet
)
3897 if (strcmp ("qTStatus", packet
) == 0)
3899 cmd_qtstatus (packet
);
3902 else if (strncmp ("qTP:", packet
, strlen ("qTP:")) == 0)
3907 else if (strcmp ("qTfP", packet
) == 0)
3912 else if (strcmp ("qTsP", packet
) == 0)
3917 else if (strcmp ("qTfV", packet
) == 0)
3922 else if (strcmp ("qTsV", packet
) == 0)
3927 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3932 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3934 cmd_qtbuffer (packet
);
3937 else if (strcmp ("qTfSTM", packet
) == 0)
3939 cmd_qtfstm (packet
);
3942 else if (strcmp ("qTsSTM", packet
) == 0)
3944 cmd_qtsstm (packet
);
3947 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3949 cmd_qtstmat (packet
);
3952 else if (strcmp ("qTMinFTPILen", packet
) == 0)
3954 cmd_qtminftpilen (packet
);
3962 #ifndef IN_PROCESS_AGENT
3964 /* Call this when thread TINFO has hit the tracepoint defined by
3965 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3966 action. This adds a while-stepping collecting state item to the
3967 threads' collecting state list, so that we can keep track of
3968 multiple simultaneous while-stepping actions being collected by the
3969 same thread. This can happen in cases like:
3971 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3973 ff0003 INSN3 <-- TP2, collect $regs
3974 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3977 Notice that when instruction INSN5 is reached, the while-stepping
3978 actions of both TP1 and TP3 are still being collected, and that TP2
3979 had been collected meanwhile. The whole range of ff0001-ff0005
3980 should be single-stepped, due to at least TP1's while-stepping
3981 action covering the whole range. */
3984 add_while_stepping_state (struct thread_info
*tinfo
,
3985 int tp_number
, CORE_ADDR tp_address
)
3987 struct wstep_state
*wstep
;
3989 wstep
= xmalloc (sizeof (*wstep
));
3990 wstep
->next
= tinfo
->while_stepping
;
3992 wstep
->tp_number
= tp_number
;
3993 wstep
->tp_address
= tp_address
;
3994 wstep
->current_step
= 0;
3996 tinfo
->while_stepping
= wstep
;
3999 /* Release the while-stepping collecting state WSTEP. */
4002 release_while_stepping_state (struct wstep_state
*wstep
)
4007 /* Release all while-stepping collecting states currently associated
4008 with thread TINFO. */
4011 release_while_stepping_state_list (struct thread_info
*tinfo
)
4013 struct wstep_state
*head
;
4015 while (tinfo
->while_stepping
)
4017 head
= tinfo
->while_stepping
;
4018 tinfo
->while_stepping
= head
->next
;
4019 release_while_stepping_state (head
);
4023 /* If TINFO was handling a 'while-stepping' action, the step has
4024 finished, so collect any step data needed, and check if any more
4025 steps are required. Return true if the thread was indeed
4026 collecting tracepoint data, false otherwise. */
4029 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4031 struct tracepoint
*tpoint
;
4032 struct wstep_state
*wstep
;
4033 struct wstep_state
**wstep_link
;
4034 struct trap_tracepoint_ctx ctx
;
4036 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4038 if (agent_loaded_p ())
4039 upload_fast_traceframes ();
4041 /* Check if we were indeed collecting data for one of more
4042 tracepoints with a 'while-stepping' count. */
4043 if (tinfo
->while_stepping
== NULL
)
4048 /* We're not even tracing anymore. Stop this thread from
4050 release_while_stepping_state_list (tinfo
);
4052 /* The thread had stopped due to a single-step request indeed
4053 explained by a tracepoint. */
4057 wstep
= tinfo
->while_stepping
;
4058 wstep_link
= &tinfo
->while_stepping
;
4060 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4061 target_pid_to_str (tinfo
->entry
.id
),
4062 wstep
->tp_number
, paddress (wstep
->tp_address
));
4064 ctx
.base
.type
= trap_tracepoint
;
4065 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4067 while (wstep
!= NULL
)
4069 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
4072 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4073 wstep
->tp_number
, paddress (wstep
->tp_address
),
4074 target_pid_to_str (tinfo
->entry
.id
));
4077 *wstep_link
= wstep
->next
;
4078 release_while_stepping_state (wstep
);
4079 wstep
= *wstep_link
;
4083 /* We've just finished one step. */
4084 ++wstep
->current_step
;
4087 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
4088 stop_pc
, tpoint
, wstep
->current_step
);
4090 if (wstep
->current_step
>= tpoint
->step_count
)
4092 /* The requested numbers of steps have occurred. */
4093 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4094 target_pid_to_str (tinfo
->entry
.id
),
4095 wstep
->tp_number
, paddress (wstep
->tp_address
));
4097 /* Unlink the wstep. */
4098 *wstep_link
= wstep
->next
;
4099 release_while_stepping_state (wstep
);
4100 wstep
= *wstep_link
;
4102 /* Only check the hit count now, which ensure that we do all
4103 our stepping before stopping the run. */
4104 if (tpoint
->pass_count
> 0
4105 && tpoint
->hit_count
>= tpoint
->pass_count
4106 && stopping_tracepoint
== NULL
)
4107 stopping_tracepoint
= tpoint
;
4111 /* Keep single-stepping until the requested numbers of steps
4113 wstep_link
= &wstep
->next
;
4114 wstep
= *wstep_link
;
4117 if (stopping_tracepoint
4118 || trace_buffer_is_full
4119 || expr_eval_result
!= expr_eval_no_error
)
4129 /* Handle any internal tracing control breakpoint hits. That means,
4130 pull traceframes from the IPA to our buffer, and syncing both
4131 tracing agents when the IPA's tracing stops for some reason. */
4134 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4136 /* Pull in fast tracepoint trace frames from the inferior in-process
4137 agent's buffer into our buffer. */
4139 if (!agent_loaded_p ())
4142 upload_fast_traceframes ();
4144 /* Check if the in-process agent had decided we should stop
4146 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
4148 int ipa_trace_buffer_is_full
;
4149 CORE_ADDR ipa_stopping_tracepoint
;
4150 int ipa_expr_eval_result
;
4151 CORE_ADDR ipa_error_tracepoint
;
4153 trace_debug ("lib stopped at stop_tracing");
4155 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
4156 &ipa_trace_buffer_is_full
);
4158 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
4159 &ipa_stopping_tracepoint
);
4160 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
4162 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
4163 &ipa_error_tracepoint
);
4164 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
4166 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
4167 &ipa_expr_eval_result
);
4168 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
4170 trace_debug ("lib: trace_buffer_is_full: %d, "
4171 "stopping_tracepoint: %s, "
4172 "ipa_expr_eval_result: %d, "
4173 "error_tracepoint: %s, ",
4174 ipa_trace_buffer_is_full
,
4175 paddress (ipa_stopping_tracepoint
),
4176 ipa_expr_eval_result
,
4177 paddress (ipa_error_tracepoint
));
4181 if (ipa_trace_buffer_is_full
)
4182 trace_debug ("lib stopped due to full buffer.");
4183 if (ipa_stopping_tracepoint
)
4184 trace_debug ("lib stopped due to tpoint");
4185 if (ipa_stopping_tracepoint
)
4186 trace_debug ("lib stopped due to error");
4189 if (ipa_stopping_tracepoint
!= 0)
4192 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
4194 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
4196 expr_eval_result
= ipa_expr_eval_result
;
4198 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
4203 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
4205 trace_debug ("lib stopped at flush_trace_buffer");
4212 /* Return true if TINFO just hit a tracepoint. Collect data if
4216 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
4218 struct tracepoint
*tpoint
;
4220 struct trap_tracepoint_ctx ctx
;
4222 /* Not tracing, don't handle. */
4226 ctx
.base
.type
= trap_tracepoint
;
4227 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
4229 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
4231 /* Note that we collect fast tracepoints here as well. We'll
4232 step over the fast tracepoint jump later, which avoids the
4233 double collect. However, we don't collect for static
4234 tracepoints here, because UST markers are compiled in program,
4235 and probes will be executed in program. So static tracepoints
4236 are collected there. */
4237 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
4238 && tpoint
->type
!= static_tracepoint
)
4240 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4241 target_pid_to_str (tinfo
->entry
.id
),
4242 tpoint
->number
, paddress (tpoint
->address
));
4244 /* Test the condition if present, and collect if true. */
4246 || (condition_true_at_tracepoint
4247 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
4248 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
4251 if (stopping_tracepoint
4252 || trace_buffer_is_full
4253 || expr_eval_result
!= expr_eval_no_error
)
4257 /* If the tracepoint had a 'while-stepping' action, then set
4258 the thread to collect this tracepoint on the following
4260 else if (tpoint
->step_count
> 0)
4262 add_while_stepping_state (tinfo
,
4263 tpoint
->number
, tpoint
->address
);
4275 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4276 struct ust_marker_data
;
4277 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4278 struct traceframe
*tframe
);
4281 /* Create a trace frame for the hit of the given tracepoint in the
4285 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
4286 struct tracepoint
*tpoint
)
4288 struct traceframe
*tframe
;
4291 /* Only count it as a hit when we actually collect data. */
4292 tpoint
->hit_count
++;
4294 /* If we've exceeded a defined pass count, record the event for
4295 later, and finish the collection for this hit. This test is only
4296 for nonstepping tracepoints, stepping tracepoints test at the end
4297 of their while-stepping loop. */
4298 if (tpoint
->pass_count
> 0
4299 && tpoint
->hit_count
>= tpoint
->pass_count
4300 && tpoint
->step_count
== 0
4301 && stopping_tracepoint
== NULL
)
4302 stopping_tracepoint
= tpoint
;
4304 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4305 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
4307 tframe
= add_traceframe (tpoint
);
4311 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
4313 #ifndef IN_PROCESS_AGENT
4314 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4315 tpoint
->number
, paddress (tpoint
->address
),
4316 tpoint
->actions_str
[acti
]);
4319 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4320 tpoint
->actions
[acti
]);
4323 finish_traceframe (tframe
);
4326 if (tframe
== NULL
&& tracing
)
4327 trace_buffer_is_full
= 1;
4330 #ifndef IN_PROCESS_AGENT
4333 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
4335 struct tracepoint
*tpoint
, int current_step
)
4337 struct traceframe
*tframe
;
4340 trace_debug ("Making new step traceframe for "
4341 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4342 tpoint
->number
, paddress (tpoint
->address
),
4343 current_step
, tpoint
->step_count
,
4346 tframe
= add_traceframe (tpoint
);
4350 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
4352 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4353 tpoint
->number
, paddress (tpoint
->address
),
4354 tpoint
->step_actions_str
[acti
]);
4356 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
4357 tpoint
->step_actions
[acti
]);
4360 finish_traceframe (tframe
);
4363 if (tframe
== NULL
&& tracing
)
4364 trace_buffer_is_full
= 1;
4369 static struct regcache
*
4370 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
4372 struct regcache
*regcache
= NULL
;
4374 #ifdef IN_PROCESS_AGENT
4375 if (ctx
->type
== fast_tracepoint
)
4377 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
4378 if (!fctx
->regcache_initted
)
4380 fctx
->regcache_initted
= 1;
4381 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
4382 supply_regblock (&fctx
->regcache
, NULL
);
4383 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
4385 regcache
= &fctx
->regcache
;
4388 if (ctx
->type
== static_tracepoint
)
4390 struct static_tracepoint_ctx
*sctx
4391 = (struct static_tracepoint_ctx
*) ctx
;
4393 if (!sctx
->regcache_initted
)
4395 sctx
->regcache_initted
= 1;
4396 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
4397 supply_regblock (&sctx
->regcache
, NULL
);
4398 /* Pass down the tracepoint address, because REGS doesn't
4399 include the PC, but we know what it must have been. */
4400 supply_static_tracepoint_registers (&sctx
->regcache
,
4401 (const unsigned char *)
4403 sctx
->tpoint
->address
);
4405 regcache
= &sctx
->regcache
;
4409 if (ctx
->type
== trap_tracepoint
)
4411 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4412 regcache
= tctx
->regcache
;
4416 gdb_assert (regcache
!= NULL
);
4422 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4424 struct tracepoint
*tpoint
,
4425 struct traceframe
*tframe
,
4426 struct tracepoint_action
*taction
)
4428 enum eval_result_type err
;
4430 switch (taction
->type
)
4434 struct collect_memory_action
*maction
;
4436 maction
= (struct collect_memory_action
*) taction
;
4438 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4439 pulongest (maction
->len
),
4440 paddress (maction
->addr
), maction
->basereg
);
4441 /* (should use basereg) */
4442 agent_mem_read (tframe
, NULL
,
4443 (CORE_ADDR
) maction
->addr
, maction
->len
);
4448 unsigned char *regspace
;
4449 struct regcache tregcache
;
4450 struct regcache
*context_regcache
;
4453 trace_debug ("Want to collect registers");
4455 /* Collect all registers for now. */
4456 regspace
= add_traceframe_block (tframe
,
4457 1 + register_cache_size ());
4458 if (regspace
== NULL
)
4460 trace_debug ("Trace buffer block allocation failed, skipping");
4463 /* Identify a register block. */
4466 context_regcache
= get_context_regcache (ctx
);
4468 /* Wrap the regblock in a register cache (in the stack, we
4469 don't want to malloc here). */
4470 init_register_cache (&tregcache
, regspace
+ 1);
4472 /* Copy the register data to the regblock. */
4473 regcache_cpy (&tregcache
, context_regcache
);
4475 #ifndef IN_PROCESS_AGENT
4476 /* On some platforms, trap-based tracepoints will have the PC
4477 pointing to the next instruction after the trap, but we
4478 don't want the user or GDB trying to guess whether the
4479 saved PC needs adjusting; so always record the adjusted
4480 stop_pc. Note that we can't use tpoint->address instead,
4481 since it will be wrong for while-stepping actions. This
4482 adjustment is a nop for fast tracepoints collected from the
4483 in-process lib (but not if GDBserver is collecting one
4484 preemptively), since the PC had already been adjusted to
4485 contain the tracepoint's address by the jump pad. */
4486 trace_debug ("Storing stop pc (0x%s) in regblock",
4487 paddress (stop_pc
));
4489 /* This changes the regblock, not the thread's
4491 regcache_write_pc (&tregcache
, stop_pc
);
4497 struct eval_expr_action
*eaction
;
4499 eaction
= (struct eval_expr_action
*) taction
;
4501 trace_debug ("Want to evaluate expression");
4503 err
= eval_tracepoint_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4505 if (err
!= expr_eval_no_error
)
4507 record_tracepoint_error (tpoint
, "action expression", err
);
4514 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4515 trace_debug ("Want to collect static trace data");
4516 collect_ust_data_at_tracepoint (ctx
, tframe
);
4518 trace_debug ("warning: collecting static trace data, "
4519 "but static tracepoints are not supported");
4524 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4530 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4531 struct tracepoint
*tpoint
)
4534 enum eval_result_type err
;
4536 /* Presently, gdbserver doesn't run compiled conditions, only the
4537 IPA does. If the program stops at a fast tracepoint's address
4538 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4539 gdbserver preemptively collect the fast tracepoint. Later, on
4540 resume, gdbserver steps over the fast tracepoint like it steps
4541 over breakpoints, so that the IPA doesn't see that fast
4542 tracepoint. This avoids double collects of fast tracepoints in
4543 that stopping scenario. Having gdbserver itself handle the fast
4544 tracepoint gives the user a consistent view of when fast or trap
4545 tracepoints are collected, compared to an alternative where only
4546 trap tracepoints are collected on stop, and fast tracepoints on
4547 resume. When a fast tracepoint is being processed by gdbserver,
4548 it is always the non-compiled condition expression that is
4550 #ifdef IN_PROCESS_AGENT
4551 if (tpoint
->compiled_cond
)
4552 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4555 err
= eval_tracepoint_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4557 if (err
!= expr_eval_no_error
)
4559 record_tracepoint_error (tpoint
, "condition", err
);
4560 /* The error case must return false. */
4564 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4565 tpoint
->number
, paddress (tpoint
->address
),
4567 return (value
? 1 : 0);
4570 /* Evaluates a tracepoint agent expression with context CTX,
4571 traceframe TFRAME, agent expression AEXPR and store the
4574 static enum eval_result_type
4575 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4576 struct traceframe
*tframe
,
4577 struct agent_expr
*aexpr
,
4580 struct regcache
*regcache
;
4581 regcache
= get_context_regcache (ctx
);
4583 return gdb_eval_agent_expr (regcache
, tframe
, aexpr
, rslt
);
4586 /* Do memory copies for bytecodes. */
4587 /* Do the recording of memory blocks for actions and bytecodes. */
4590 agent_mem_read (struct traceframe
*tframe
,
4591 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4593 unsigned char *mspace
;
4594 ULONGEST remaining
= len
;
4595 unsigned short blocklen
;
4597 /* If a 'to' buffer is specified, use it. */
4600 read_inferior_memory (from
, to
, len
);
4604 /* Otherwise, create a new memory block in the trace buffer. */
4605 while (remaining
> 0)
4609 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4610 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4611 mspace
= add_traceframe_block (tframe
, sp
);
4614 /* Identify block as a memory block. */
4617 /* Record address and size. */
4618 memcpy (mspace
, &from
, sizeof (from
));
4619 mspace
+= sizeof (from
);
4620 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4621 mspace
+= sizeof (blocklen
);
4622 /* Record the memory block proper. */
4623 read_inferior_memory (from
, mspace
, blocklen
);
4624 trace_debug ("%d bytes recorded", blocklen
);
4625 remaining
-= blocklen
;
4632 agent_mem_read_string (struct traceframe
*tframe
,
4633 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4635 unsigned char *buf
, *mspace
;
4636 ULONGEST remaining
= len
;
4637 unsigned short blocklen
, i
;
4639 /* To save a bit of space, block lengths are 16-bit, so break large
4640 requests into multiple blocks. Bordering on overkill for strings,
4641 but it could happen that someone specifies a large max length. */
4642 while (remaining
> 0)
4646 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4647 /* We want working space to accumulate nonzero bytes, since
4648 traceframes must have a predecided size (otherwise it gets
4649 harder to wrap correctly for the circular case, etc). */
4650 buf
= (unsigned char *) xmalloc (blocklen
+ 1);
4651 for (i
= 0; i
< blocklen
; ++i
)
4653 /* Read the string one byte at a time, in case the string is
4654 at the end of a valid memory area - we don't want a
4655 correctly-terminated string to engender segvio
4657 read_inferior_memory (from
+ i
, buf
+ i
, 1);
4662 /* Make sure outer loop stops now too. */
4663 remaining
= blocklen
;
4667 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4668 mspace
= add_traceframe_block (tframe
, sp
);
4674 /* Identify block as a memory block. */
4677 /* Record address and size. */
4678 memcpy ((void *) mspace
, (void *) &from
, sizeof (from
));
4679 mspace
+= sizeof (from
);
4680 memcpy ((void *) mspace
, (void *) &blocklen
, sizeof (blocklen
));
4681 mspace
+= sizeof (blocklen
);
4682 /* Copy the string contents. */
4683 memcpy ((void *) mspace
, (void *) buf
, blocklen
);
4684 remaining
-= blocklen
;
4691 /* Record the value of a trace state variable. */
4694 agent_tsv_read (struct traceframe
*tframe
, int n
)
4696 unsigned char *vspace
;
4699 vspace
= add_traceframe_block (tframe
,
4700 1 + sizeof (n
) + sizeof (LONGEST
));
4703 /* Identify block as a variable. */
4705 /* Record variable's number and value. */
4706 memcpy (vspace
+ 1, &n
, sizeof (n
));
4707 val
= get_trace_state_variable_value (n
);
4708 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4709 trace_debug ("Variable %d recorded", n
);
4713 #ifndef IN_PROCESS_AGENT
4715 /* Callback for traceframe_walk_blocks, used to find a given block
4716 type in a traceframe. */
4719 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4721 char *wantedp
= data
;
4723 if (*wantedp
== blocktype
)
4729 /* Walk over all traceframe blocks of the traceframe buffer starting
4730 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4731 block found, passing in DATA unmodified. If CALLBACK returns true,
4732 this returns a pointer to where the block is found. Returns NULL
4733 if no callback call returned true, indicating that all blocks have
4736 static unsigned char *
4737 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4739 int (*callback
) (char blocktype
,
4740 unsigned char *dataptr
,
4744 unsigned char *dataptr
;
4748 trace_debug ("traceframe %d has no data", tfnum
);
4752 /* Iterate through a traceframe's blocks, looking for a block of the
4754 for (dataptr
= database
;
4755 dataptr
< database
+ datasize
;
4759 unsigned short mlen
;
4761 if (dataptr
== trace_buffer_wrap
)
4763 /* Adjust to reflect wrapping part of the frame around to
4765 datasize
= dataptr
- database
;
4766 dataptr
= database
= trace_buffer_lo
;
4769 blocktype
= *dataptr
++;
4771 if ((*callback
) (blocktype
, dataptr
, data
))
4777 /* Skip over the registers block. */
4778 dataptr
+= register_cache_size ();
4781 /* Skip over the memory block. */
4782 dataptr
+= sizeof (CORE_ADDR
);
4783 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4784 dataptr
+= (sizeof (mlen
) + mlen
);
4787 /* Skip over the TSV block. */
4788 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4791 /* Skip over the static trace data block. */
4792 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4793 dataptr
+= (sizeof (mlen
) + mlen
);
4796 trace_debug ("traceframe %d has unknown block type 0x%x",
4805 /* Look for the block of type TYPE_WANTED in the trameframe starting
4806 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4809 static unsigned char *
4810 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4811 int tfnum
, char type_wanted
)
4813 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4814 match_blocktype
, &type_wanted
);
4817 static unsigned char *
4818 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4820 unsigned char *regblock
;
4822 regblock
= traceframe_find_block_type (tframe
->data
,
4826 if (regblock
== NULL
)
4827 trace_debug ("traceframe %d has no register data", tfnum
);
4832 /* Get registers from a traceframe. */
4835 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4837 unsigned char *dataptr
;
4838 struct tracepoint
*tpoint
;
4839 struct traceframe
*tframe
;
4841 tframe
= find_traceframe (tfnum
);
4845 trace_debug ("traceframe %d not found", tfnum
);
4849 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4850 if (dataptr
== NULL
)
4852 /* Mark registers unavailable. */
4853 supply_regblock (regcache
, NULL
);
4855 /* We can generally guess at a PC, although this will be
4856 misleading for while-stepping frames and multi-location
4858 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4860 regcache_write_pc (regcache
, tpoint
->address
);
4863 supply_regblock (regcache
, dataptr
);
4869 traceframe_get_pc (struct traceframe
*tframe
)
4871 struct regcache regcache
;
4872 unsigned char *dataptr
;
4874 dataptr
= traceframe_find_regblock (tframe
, -1);
4875 if (dataptr
== NULL
)
4878 init_register_cache (®cache
, dataptr
);
4879 return regcache_read_pc (®cache
);
4882 /* Read a requested block of memory from a trace frame. */
4885 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4886 unsigned char *buf
, ULONGEST length
,
4889 struct traceframe
*tframe
;
4890 unsigned char *database
, *dataptr
;
4891 unsigned int datasize
;
4893 unsigned short mlen
;
4895 trace_debug ("traceframe_read_mem");
4897 tframe
= find_traceframe (tfnum
);
4901 trace_debug ("traceframe %d not found", tfnum
);
4905 datasize
= tframe
->data_size
;
4906 database
= dataptr
= &tframe
->data
[0];
4908 /* Iterate through a traceframe's blocks, looking for memory. */
4909 while ((dataptr
= traceframe_find_block_type (dataptr
,
4911 - (dataptr
- database
),
4912 tfnum
, 'M')) != NULL
)
4914 memcpy (&maddr
, dataptr
, sizeof (maddr
));
4915 dataptr
+= sizeof (maddr
);
4916 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4917 dataptr
+= sizeof (mlen
);
4918 trace_debug ("traceframe %d has %d bytes at %s",
4919 tfnum
, mlen
, paddress (maddr
));
4921 /* If the block includes the first part of the desired range,
4922 return as much it has; GDB will re-request the remainder,
4923 which might be in a different block of this trace frame. */
4924 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
4926 ULONGEST amt
= (maddr
+ mlen
) - addr
;
4930 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
4935 /* Skip over this block. */
4939 trace_debug ("traceframe %d has no memory data for the desired region",
4947 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
4950 struct traceframe
*tframe
;
4951 unsigned char *database
, *dataptr
;
4952 unsigned int datasize
;
4955 trace_debug ("traceframe_read_tsv");
4957 tfnum
= current_traceframe
;
4961 trace_debug ("no current traceframe");
4965 tframe
= find_traceframe (tfnum
);
4969 trace_debug ("traceframe %d not found", tfnum
);
4973 datasize
= tframe
->data_size
;
4974 database
= dataptr
= &tframe
->data
[0];
4976 /* Iterate through a traceframe's blocks, looking for the tsv. */
4977 while ((dataptr
= traceframe_find_block_type (dataptr
,
4979 - (dataptr
- database
),
4980 tfnum
, 'V')) != NULL
)
4982 memcpy (&vnum
, dataptr
, sizeof (vnum
));
4983 dataptr
+= sizeof (vnum
);
4985 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
4987 /* Check that this is the variable we want. */
4990 memcpy (val
, dataptr
, sizeof (*val
));
4994 /* Skip over this block. */
4995 dataptr
+= sizeof (LONGEST
);
4998 trace_debug ("traceframe %d has no data for variable %d",
5003 /* Read a requested block of static tracepoint data from a trace
5007 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
5008 unsigned char *buf
, ULONGEST length
,
5011 struct traceframe
*tframe
;
5012 unsigned char *database
, *dataptr
;
5013 unsigned int datasize
;
5014 unsigned short mlen
;
5016 trace_debug ("traceframe_read_sdata");
5018 tframe
= find_traceframe (tfnum
);
5022 trace_debug ("traceframe %d not found", tfnum
);
5026 datasize
= tframe
->data_size
;
5027 database
= &tframe
->data
[0];
5029 /* Iterate through a traceframe's blocks, looking for static
5031 dataptr
= traceframe_find_block_type (database
, datasize
,
5033 if (dataptr
!= NULL
)
5035 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5036 dataptr
+= sizeof (mlen
);
5039 if (offset
+ length
> mlen
)
5040 length
= mlen
- offset
;
5042 memcpy (buf
, dataptr
, length
);
5050 trace_debug ("traceframe %d has no static trace data", tfnum
);
5056 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5057 object. DATA is pointer to a struct buffer holding the
5058 traceframe-info object being built. */
5061 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5063 struct buffer
*buffer
= data
;
5069 unsigned short mlen
;
5072 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5073 dataptr
+= sizeof (maddr
);
5074 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5075 dataptr
+= sizeof (mlen
);
5076 buffer_xml_printf (buffer
,
5077 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5078 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5088 warning ("Unhandled trace block type (%d) '%c ' "
5089 "while building trace frame info.",
5090 blocktype
, blocktype
);
5097 /* Build a traceframe-info object for traceframe number TFNUM into
5101 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5103 struct traceframe
*tframe
;
5105 trace_debug ("traceframe_read_info");
5107 tframe
= find_traceframe (tfnum
);
5111 trace_debug ("traceframe %d not found", tfnum
);
5115 buffer_grow_str (buffer
, "<traceframe-info>\n");
5116 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5117 tfnum
, build_traceframe_info_xml
, buffer
);
5118 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5122 /* Return the first fast tracepoint whose jump pad contains PC. */
5124 static struct tracepoint
*
5125 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5127 struct tracepoint
*tpoint
;
5129 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5130 if (tpoint
->type
== fast_tracepoint
)
5131 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5137 /* Return the first fast tracepoint whose trampoline contains PC. */
5139 static struct tracepoint
*
5140 fast_tracepoint_from_trampoline_address (CORE_ADDR pc
)
5142 struct tracepoint
*tpoint
;
5144 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5146 if (tpoint
->type
== fast_tracepoint
5147 && tpoint
->trampoline
<= pc
&& pc
< tpoint
->trampoline_end
)
5154 /* Return GDBserver's tracepoint that matches the IP Agent's
5155 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5158 static struct tracepoint
*
5159 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5161 struct tracepoint
*tpoint
;
5163 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5164 if (tpoint
->type
== fast_tracepoint
)
5165 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5173 /* The type of the object that is used to synchronize fast tracepoint
5176 typedef struct collecting_t
5178 /* The fast tracepoint number currently collecting. */
5181 /* A number that GDBserver can use to identify the thread that is
5182 presently holding the collect lock. This need not (and usually
5183 is not) the thread id, as getting the current thread ID usually
5184 requires a system call, which we want to avoid like the plague.
5185 Usually this is thread's TCB, found in the TLS (pseudo-)
5186 register, which is readable with a single insn on several
5188 uintptr_t thread_area
;
5191 #ifndef IN_PROCESS_AGENT
5194 force_unlock_trace_buffer (void)
5196 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5199 /* Check if the thread identified by THREAD_AREA which is stopped at
5200 STOP_PC, is presently locking the fast tracepoint collection, and
5201 if so, gather some status of said collection. Returns 0 if the
5202 thread isn't collecting or in the jump pad at all. 1, if in the
5203 jump pad (or within gdb_collect) and hasn't executed the adjusted
5204 original insn yet (can set a breakpoint there and run to it). 2,
5205 if presently executing the adjusted original insn --- in which
5206 case, if we want to move the thread out of the jump pad, we need to
5207 single-step it until this function returns 0. */
5210 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5212 struct fast_tpoint_collect_status
*status
)
5214 CORE_ADDR ipa_collecting
;
5215 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5216 CORE_ADDR ipa_gdb_trampoline_buffer
;
5217 CORE_ADDR ipa_gdb_trampoline_buffer_end
;
5218 struct tracepoint
*tpoint
;
5219 int needs_breakpoint
;
5221 /* The thread THREAD_AREA is either:
5223 0. not collecting at all, not within the jump pad, or within
5224 gdb_collect or one of its callees.
5226 1. in the jump pad and haven't reached gdb_collect
5228 2. within gdb_collect (out of the jump pad) (collect is set)
5230 3. we're in the jump pad, after gdb_collect having returned,
5231 possibly executing the adjusted insns.
5233 For cases 1 and 3, `collecting' may or not be set. The jump pad
5234 doesn't have any complicated jump logic, so we can tell if the
5235 thread is executing the adjust original insn or not by just
5236 matching STOP_PC with known jump pad addresses. If we it isn't
5237 yet executing the original insn, set a breakpoint there, and let
5238 the thread run to it, so to quickly step over a possible (many
5239 insns) gdb_collect call. Otherwise, or when the breakpoint is
5240 hit, only a few (small number of) insns are left to be executed
5241 in the jump pad. Single-step the thread until it leaves the
5246 needs_breakpoint
= 0;
5247 trace_debug ("fast_tracepoint_collecting");
5249 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5250 &ipa_gdb_jump_pad_buffer
))
5251 fatal ("error extracting `gdb_jump_pad_buffer'");
5252 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5253 &ipa_gdb_jump_pad_buffer_end
))
5254 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5256 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer
,
5257 &ipa_gdb_trampoline_buffer
))
5258 fatal ("error extracting `gdb_trampoline_buffer'");
5259 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_trampoline_buffer_end
,
5260 &ipa_gdb_trampoline_buffer_end
))
5261 fatal ("error extracting `gdb_trampoline_buffer_end'");
5263 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5264 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5266 /* We can tell which tracepoint(s) the thread is collecting by
5267 matching the jump pad address back to the tracepoint. */
5268 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5271 warning ("in jump pad, but no matching tpoint?");
5276 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5278 tpoint
->number
, paddress (tpoint
->address
),
5279 paddress (tpoint
->jump_pad
),
5280 paddress (tpoint
->jump_pad_end
),
5281 paddress (tpoint
->adjusted_insn_addr
),
5282 paddress (tpoint
->adjusted_insn_addr_end
));
5285 /* Definitely in the jump pad. May or may not need
5286 fast-exit-jump-pad breakpoint. */
5287 if (tpoint
->jump_pad
<= stop_pc
5288 && stop_pc
< tpoint
->adjusted_insn_addr
)
5289 needs_breakpoint
= 1;
5291 else if (ipa_gdb_trampoline_buffer
<= stop_pc
5292 && stop_pc
< ipa_gdb_trampoline_buffer_end
)
5294 /* We can tell which tracepoint(s) the thread is collecting by
5295 matching the trampoline address back to the tracepoint. */
5296 tpoint
= fast_tracepoint_from_trampoline_address (stop_pc
);
5299 warning ("in trampoline, but no matching tpoint?");
5304 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5305 tpoint
->number
, paddress (tpoint
->address
),
5306 paddress (tpoint
->trampoline
),
5307 paddress (tpoint
->trampoline_end
));
5310 /* Have not reached jump pad yet, but treat the trampoline as a
5311 part of the jump pad that is before the adjusted original
5313 needs_breakpoint
= 1;
5317 collecting_t ipa_collecting_obj
;
5319 /* If `collecting' is set/locked, then the THREAD_AREA thread
5320 may or not be the one holding the lock. We have to read the
5321 lock to find out. */
5323 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5326 trace_debug ("fast_tracepoint_collecting:"
5327 " failed reading 'collecting' in the inferior");
5331 if (!ipa_collecting
)
5333 trace_debug ("fast_tracepoint_collecting: not collecting"
5334 " (and nobody is).");
5338 /* Some thread is collecting. Check which. */
5339 if (read_inferior_memory (ipa_collecting
,
5340 (unsigned char *) &ipa_collecting_obj
,
5341 sizeof (ipa_collecting_obj
)) != 0)
5344 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5346 trace_debug ("fast_tracepoint_collecting: not collecting "
5347 "(another thread is)");
5352 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5355 warning ("fast_tracepoint_collecting: collecting, "
5356 "but tpoint %s not found?",
5357 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5361 /* The thread is within `gdb_collect', skip over the rest of
5362 fast tracepoint collection quickly using a breakpoint. */
5363 needs_breakpoint
= 1;
5366 /* The caller wants a bit of status detail. */
5369 status
->tpoint_num
= tpoint
->number
;
5370 status
->tpoint_addr
= tpoint
->address
;
5371 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5372 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5375 if (needs_breakpoint
)
5377 /* Hasn't executed the original instruction yet. Set breakpoint
5378 there, and wait till it's hit, then single-step until exiting
5382 fast_tracepoint_collecting, returning continue-until-break at %s",
5383 paddress (tpoint
->adjusted_insn_addr
));
5385 return 1; /* continue */
5389 /* Just single-step until exiting the jump pad. */
5391 trace_debug ("fast_tracepoint_collecting, returning "
5392 "need-single-step (%s-%s)",
5393 paddress (tpoint
->adjusted_insn_addr
),
5394 paddress (tpoint
->adjusted_insn_addr_end
));
5396 return 2; /* single-step */
5402 #ifdef IN_PROCESS_AGENT
5404 /* The global fast tracepoint collect lock. Points to a collecting_t
5405 object built on the stack by the jump pad, if presently locked;
5406 NULL if it isn't locked. Note that this lock *must* be set while
5407 executing any *function other than the jump pad. See
5408 fast_tracepoint_collecting. */
5409 static collecting_t
* ATTR_USED collecting
;
5411 /* This routine, called from the jump pad (in asm) is designed to be
5412 called from the jump pads of fast tracepoints, thus it is on the
5415 IP_AGENT_EXPORT
void ATTR_USED
5416 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5418 struct fast_tracepoint_ctx ctx
;
5420 /* Don't do anything until the trace run is completely set up. */
5424 ctx
.base
.type
= fast_tracepoint
;
5426 ctx
.regcache_initted
= 0;
5427 /* Wrap the regblock in a register cache (in the stack, we don't
5428 want to malloc here). */
5429 ctx
.regspace
= alloca (register_cache_size ());
5430 if (ctx
.regspace
== NULL
)
5432 trace_debug ("Trace buffer block allocation failed, skipping");
5436 for (ctx
.tpoint
= tpoint
;
5437 ctx
.tpoint
!= NULL
&& ctx
.tpoint
->address
== tpoint
->address
;
5438 ctx
.tpoint
= ctx
.tpoint
->next
)
5440 if (!ctx
.tpoint
->enabled
)
5443 /* Multiple tracepoints of different types, such as fast tracepoint and
5444 static tracepoint, can be set at the same address. */
5445 if (ctx
.tpoint
->type
!= tpoint
->type
)
5448 /* Test the condition if present, and collect if true. */
5449 if (ctx
.tpoint
->cond
== NULL
5450 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5453 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5454 ctx
.tpoint
->address
, ctx
.tpoint
);
5456 /* Note that this will cause original insns to be written back
5457 to where we jumped from, but that's OK because we're jumping
5458 back to the next whole instruction. This will go badly if
5459 instruction restoration is not atomic though. */
5460 if (stopping_tracepoint
5461 || trace_buffer_is_full
5462 || expr_eval_result
!= expr_eval_no_error
)
5470 /* If there was a condition and it evaluated to false, the only
5471 way we would stop tracing is if there was an error during
5472 condition expression evaluation. */
5473 if (expr_eval_result
!= expr_eval_no_error
)
5484 #ifndef IN_PROCESS_AGENT
5487 get_raw_reg_func_addr (void)
5489 return ipa_sym_addrs
.addr_get_raw_reg
;
5493 get_get_tsv_func_addr (void)
5495 return ipa_sym_addrs
.addr_get_trace_state_variable_value
;
5499 get_set_tsv_func_addr (void)
5501 return ipa_sym_addrs
.addr_set_trace_state_variable_value
;
5505 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5506 CORE_ADDR
*jump_entry
)
5508 CORE_ADDR entry_point
= *jump_entry
;
5509 enum eval_result_type err
;
5511 trace_debug ("Starting condition compilation for tracepoint %d\n",
5514 /* Initialize the global pointer to the code being built. */
5515 current_insn_ptr
= *jump_entry
;
5519 err
= compile_bytecodes (tpoint
->cond
);
5521 if (err
== expr_eval_no_error
)
5525 /* Record the beginning of the compiled code. */
5526 tpoint
->compiled_cond
= entry_point
;
5528 trace_debug ("Condition compilation for tracepoint %d complete\n",
5533 /* Leave the unfinished code in situ, but don't point to it. */
5535 tpoint
->compiled_cond
= 0;
5537 trace_debug ("Condition compilation for tracepoint %d failed, "
5539 tpoint
->number
, err
);
5542 /* Update the code pointer passed in. Note that we do this even if
5543 the compile fails, so that we can look at the partial results
5544 instead of letting them be overwritten. */
5545 *jump_entry
= current_insn_ptr
;
5547 /* Leave a gap, to aid dump decipherment. */
5551 /* We'll need to adjust these when we consider bi-arch setups, and big
5555 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5557 return write_inferior_memory (where
,
5558 (unsigned char *) &ptr
, sizeof (void *));
5561 /* The base pointer of the IPA's heap. This is the only memory the
5562 IPA is allowed to use. The IPA should _not_ call the inferior's
5563 `malloc' during operation. That'd be slow, and, most importantly,
5564 it may not be safe. We may be collecting a tracepoint in a signal
5565 handler, for example. */
5566 static CORE_ADDR target_tp_heap
;
5568 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5572 target_malloc (ULONGEST size
)
5576 if (target_tp_heap
== 0)
5578 /* We have the pointer *address*, need what it points to. */
5579 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5581 fatal ("could get target heap head pointer");
5584 ptr
= target_tp_heap
;
5585 target_tp_heap
+= size
;
5587 /* Pad to 8-byte alignment. */
5588 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
5594 download_agent_expr (struct agent_expr
*expr
)
5596 CORE_ADDR expr_addr
;
5597 CORE_ADDR expr_bytes
;
5599 expr_addr
= target_malloc (sizeof (*expr
));
5600 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
5602 expr_bytes
= target_malloc (expr
->length
);
5603 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
5605 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
5610 /* Align V up to N bits. */
5611 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5613 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5616 download_tracepoint_1 (struct tracepoint
*tpoint
)
5618 struct tracepoint target_tracepoint
;
5619 CORE_ADDR tpptr
= 0;
5621 gdb_assert (tpoint
->type
== fast_tracepoint
5622 || tpoint
->type
== static_tracepoint
);
5624 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
5626 CORE_ADDR jentry
, jump_entry
;
5628 jentry
= jump_entry
= get_jump_space_head ();
5630 if (tpoint
->cond
!= NULL
)
5632 /* Pad to 8-byte alignment. (needed?) */
5633 /* Actually this should be left for the target to
5635 jentry
= UALIGN (jentry
, 8);
5637 compile_tracepoint_condition (tpoint
, &jentry
);
5640 /* Pad to 8-byte alignment. */
5641 jentry
= UALIGN (jentry
, 8);
5642 claim_jump_space (jentry
- jump_entry
);
5645 target_tracepoint
= *tpoint
;
5647 tpptr
= target_malloc (sizeof (*tpoint
));
5648 tpoint
->obj_addr_on_target
= tpptr
;
5650 /* Write the whole object. We'll fix up its pointers in a bit.
5651 Assume no next for now. This is fixed up above on the next
5652 iteration, if there's any. */
5653 target_tracepoint
.next
= NULL
;
5654 /* Need to clear this here too, since we're downloading the
5655 tracepoints before clearing our own copy. */
5656 target_tracepoint
.hit_count
= 0;
5658 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
5659 sizeof (target_tracepoint
));
5662 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5664 download_agent_expr (tpoint
->cond
));
5666 if (tpoint
->numactions
)
5669 CORE_ADDR actions_array
;
5671 /* The pointers array. */
5673 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
5674 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
5678 /* Now for each pointer, download the action. */
5679 for (i
= 0; i
< tpoint
->numactions
; i
++)
5681 CORE_ADDR ipa_action
= 0;
5682 struct tracepoint_action
*action
= tpoint
->actions
[i
];
5684 switch (action
->type
)
5688 = target_malloc (sizeof (struct collect_memory_action
));
5689 write_inferior_memory (ipa_action
,
5690 (unsigned char *) action
,
5691 sizeof (struct collect_memory_action
));
5695 = target_malloc (sizeof (struct collect_registers_action
));
5696 write_inferior_memory (ipa_action
,
5697 (unsigned char *) action
,
5698 sizeof (struct collect_registers_action
));
5703 struct eval_expr_action
*eaction
5704 = (struct eval_expr_action
*) action
;
5706 ipa_action
= target_malloc (sizeof (*eaction
));
5707 write_inferior_memory (ipa_action
,
5708 (unsigned char *) eaction
,
5711 expr
= download_agent_expr (eaction
->expr
);
5712 write_inferior_data_ptr
5713 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
5718 ipa_action
= target_malloc
5719 (sizeof (struct collect_static_trace_data_action
));
5720 write_inferior_memory
5722 (unsigned char *) action
,
5723 sizeof (struct collect_static_trace_data_action
));
5726 trace_debug ("unknown trace action '%c', ignoring",
5731 if (ipa_action
!= 0)
5732 write_inferior_data_ptr
5733 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
5740 download_tracepoint (struct tracepoint
*tpoint
)
5742 struct tracepoint
*tp
, *tp_prev
;
5744 if (tpoint
->type
!= fast_tracepoint
5745 && tpoint
->type
!= static_tracepoint
)
5748 download_tracepoint_1 (tpoint
);
5750 /* Find the previous entry of TPOINT, which is fast tracepoint or
5751 static tracepoint. */
5753 for (tp
= tracepoints
; tp
!= tpoint
; tp
= tp
->next
)
5755 if (tp
->type
== fast_tracepoint
|| tp
->type
== static_tracepoint
)
5761 CORE_ADDR tp_prev_target_next_addr
;
5763 /* Insert TPOINT after TP_PREV in IPA. */
5764 if (read_inferior_data_pointer (tp_prev
->obj_addr_on_target
5765 + offsetof (struct tracepoint
, next
),
5766 &tp_prev_target_next_addr
))
5767 fatal ("error reading `tp_prev->next'");
5769 /* tpoint->next = tp_prev->next */
5770 write_inferior_data_ptr (tpoint
->obj_addr_on_target
5771 + offsetof (struct tracepoint
, next
),
5772 tp_prev_target_next_addr
);
5773 /* tp_prev->next = tpoint */
5774 write_inferior_data_ptr (tp_prev
->obj_addr_on_target
5775 + offsetof (struct tracepoint
, next
),
5776 tpoint
->obj_addr_on_target
);
5779 /* First object in list, set the head pointer in the
5781 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
,
5782 tpoint
->obj_addr_on_target
);
5787 download_tracepoints (void)
5789 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
5790 struct tracepoint
*tpoint
;
5792 /* Start out empty. */
5793 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
5795 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5797 if (tpoint
->type
!= fast_tracepoint
5798 && tpoint
->type
!= static_tracepoint
)
5803 download_tracepoint_1 (tpoint
);
5805 tpptr
= tpoint
->obj_addr_on_target
;
5807 if (tpoint
== tracepoints
)
5809 /* First object in list, set the head pointer in the
5811 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
5815 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
5823 download_trace_state_variables (void)
5825 CORE_ADDR ptr
= 0, prev_ptr
= 0;
5826 struct trace_state_variable
*tsv
;
5828 /* Start out empty. */
5829 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
5831 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
5833 struct trace_state_variable target_tsv
;
5835 /* TSV's with a getter have been initialized equally in both the
5836 inferior and GDBserver. Skip them. */
5837 if (tsv
->getter
!= NULL
)
5843 ptr
= target_malloc (sizeof (*tsv
));
5845 if (tsv
== trace_state_variables
)
5847 /* First object in list, set the head pointer in the
5850 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
5855 write_inferior_data_ptr (prev_ptr
5856 + offsetof (struct trace_state_variable
,
5861 /* Write the whole object. We'll fix up its pointers in a bit.
5862 Assume no next, fixup when needed. */
5863 target_tsv
.next
= NULL
;
5865 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
5866 sizeof (target_tsv
));
5868 if (tsv
->name
!= NULL
)
5870 size_t size
= strlen (tsv
->name
) + 1;
5871 CORE_ADDR name_addr
= target_malloc (size
);
5872 write_inferior_memory (name_addr
,
5873 (unsigned char *) tsv
->name
, size
);
5874 write_inferior_data_ptr (ptr
5875 + offsetof (struct trace_state_variable
,
5880 if (tsv
->getter
!= NULL
)
5882 fatal ("what to do with these?");
5888 /* Fixup the next pointer in the last item in the list. */
5889 write_inferior_data_ptr (prev_ptr
5890 + offsetof (struct trace_state_variable
,
5895 /* Upload complete trace frames out of the IP Agent's trace buffer
5896 into GDBserver's trace buffer. This always uploads either all or
5897 no trace frames. This is the counter part of
5898 `trace_alloc_trace_buffer'. See its description of the atomic
5899 synching mechanism. */
5902 upload_fast_traceframes (void)
5904 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
5905 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
5907 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
5908 unsigned int curr_tbctrl_idx
;
5909 unsigned int ipa_trace_buffer_ctrl_curr
;
5910 unsigned int ipa_trace_buffer_ctrl_curr_old
;
5911 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
5912 struct breakpoint
*about_to_request_buffer_space_bkpt
;
5913 CORE_ADDR ipa_trace_buffer_lo
;
5914 CORE_ADDR ipa_trace_buffer_hi
;
5916 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5917 &ipa_traceframe_read_count_racy
))
5919 /* This will happen in most targets if the current thread is
5924 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5925 &ipa_traceframe_write_count_racy
))
5928 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5929 ipa_traceframe_write_count_racy
5930 - ipa_traceframe_read_count_racy
,
5931 ipa_traceframe_write_count_racy
,
5932 ipa_traceframe_read_count_racy
);
5934 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
5937 about_to_request_buffer_space_bkpt
5938 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
5941 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5942 &ipa_trace_buffer_ctrl_curr
))
5945 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
5947 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
5950 unsigned int prev
, counter
;
5952 /* Update the token, with new counters, and the GDBserver stamp
5953 bit. Alway reuse the current TBC index. */
5954 prev
= ipa_trace_buffer_ctrl_curr
& GDBSERVER_FLUSH_COUNT_MASK_CURR
;
5955 counter
= (prev
+ 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR
;
5957 ipa_trace_buffer_ctrl_curr
= (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
5963 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
5964 ipa_trace_buffer_ctrl_curr
))
5967 trace_debug ("Lib: Committed %08x -> %08x",
5968 ipa_trace_buffer_ctrl_curr_old
,
5969 ipa_trace_buffer_ctrl_curr
);
5971 /* Re-read these, now that we've installed the
5972 `about_to_request_buffer_space' breakpoint/lock. A thread could
5973 have finished a traceframe between the last read of these
5974 counters and setting the breakpoint above. If we start
5975 uploading, we never want to leave this function with
5976 traceframe_read_count != 0, otherwise, GDBserver could end up
5977 incrementing the counter tokens more than once (due to event loop
5978 nesting), which would break the IP agent's "effective" detection
5979 (see trace_alloc_trace_buffer). */
5980 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
5981 &ipa_traceframe_read_count
))
5983 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
5984 &ipa_traceframe_write_count
))
5989 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5990 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
5991 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
5993 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
5994 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
5995 trace_debug ("note that ipa_traceframe_count's parts changed");
5998 /* Get the address of the current TBC object (the IP agent has an
5999 array of 3 such objects). The index is stored in the TBC
6001 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6002 ipa_trace_buffer_ctrl_addr
6003 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6005 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6006 (unsigned char *) &ipa_trace_buffer_ctrl
,
6007 sizeof (struct ipa_trace_buffer_control
)))
6010 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6011 &ipa_trace_buffer_lo
))
6013 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6014 &ipa_trace_buffer_hi
))
6017 /* Offsets are easier to grok for debugging than raw addresses,
6018 especially for the small trace buffer sizes that are useful for
6020 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6021 "endfree=%d wrap=%d hi=%d",
6023 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6024 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6025 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6026 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6027 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6029 /* Note that the IPA's buffer is always circular. */
6031 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6033 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6034 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6036 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6037 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6038 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6039 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6042 tf
= IPA_FIRST_TRACEFRAME ();
6044 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6046 struct tracepoint
*tpoint
;
6047 struct traceframe
*tframe
;
6048 unsigned char *block
;
6049 struct traceframe ipa_tframe
;
6051 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6052 offsetof (struct traceframe
, data
)))
6053 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6055 if (ipa_tframe
.tpnum
== 0)
6056 fatal ("Uploading: No (more) fast traceframes, but "
6057 "ipa_traceframe_count == %u??\n",
6058 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6060 /* Note that this will be incorrect for multi-location
6062 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6064 tframe
= add_traceframe (tpoint
);
6067 trace_buffer_is_full
= 1;
6068 trace_debug ("Uploading: trace buffer is full");
6072 /* Copy the whole set of blocks in one go for now. FIXME:
6073 split this in smaller blocks. */
6074 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6077 if (read_inferior_memory (tf
6078 + offsetof (struct traceframe
, data
),
6079 block
, ipa_tframe
.data_size
))
6080 error ("Uploading: Couldn't read traceframe data at %s\n",
6081 paddress (tf
+ offsetof (struct traceframe
, data
)));
6084 trace_debug ("Uploading: traceframe didn't fit");
6085 finish_traceframe (tframe
);
6088 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6090 /* If we freed the traceframe that wrapped around, go back
6091 to the non-wrap case. */
6092 if (tf
< ipa_trace_buffer_ctrl
.start
)
6094 trace_debug ("Lib: Discarding past the wraparound");
6095 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6097 ipa_trace_buffer_ctrl
.start
= tf
;
6098 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6099 ++ipa_traceframe_read_count
;
6101 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6102 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6104 trace_debug ("Lib: buffer is fully empty. "
6105 "Trace buffer [%d] start=%d free=%d endfree=%d",
6107 (int) (ipa_trace_buffer_ctrl
.start
6108 - ipa_trace_buffer_lo
),
6109 (int) (ipa_trace_buffer_ctrl
.free
6110 - ipa_trace_buffer_lo
),
6111 (int) (ipa_trace_buffer_ctrl
.end_free
6112 - ipa_trace_buffer_lo
));
6114 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6115 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6116 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6117 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6120 trace_debug ("Uploaded a traceframe\n"
6121 "Lib: Trace buffer [%d] start=%d free=%d "
6122 "endfree=%d wrap=%d hi=%d",
6124 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6125 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6126 (int) (ipa_trace_buffer_ctrl
.end_free
6127 - ipa_trace_buffer_lo
),
6128 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6129 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6132 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6133 (unsigned char *) &ipa_trace_buffer_ctrl
,
6134 sizeof (struct ipa_trace_buffer_control
)))
6137 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6138 ipa_traceframe_read_count
);
6140 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6143 cancel_breakpoints ();
6145 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6146 about_to_request_buffer_space_bkpt
= NULL
;
6150 if (trace_buffer_is_full
)
6155 #ifdef IN_PROCESS_AGENT
6157 IP_AGENT_EXPORT
int ust_loaded
;
6158 IP_AGENT_EXPORT
char cmd_buf
[IPA_CMD_BUF_SIZE
];
6162 /* Static tracepoints. */
6164 /* UST puts a "struct tracepoint" in the global namespace, which
6165 conflicts with our tracepoint. Arguably, being a library, it
6166 shouldn't take ownership of such a generic name. We work around it
6168 #define tracepoint ust_tracepoint
6169 #include <ust/ust.h>
6172 extern int serialize_to_text (char *outbuf
, int bufsize
,
6173 const char *fmt
, va_list ap
);
6175 #define GDB_PROBE_NAME "gdb"
6177 /* We dynamically search for the UST symbols instead of linking them
6178 in. This lets the user decide if the application uses static
6179 tracepoints, instead of always pulling libust.so in. This vector
6180 holds pointers to all functions we care about. */
6184 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6185 const char *fmt
, va_list ap
);
6187 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6188 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6190 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6192 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6195 void (*marker_iter_start
) (struct marker_iter
*iter
);
6196 void (*marker_iter_next
) (struct marker_iter
*iter
);
6197 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6198 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6203 /* Cast through typeof to catch incompatible API changes. Since UST
6204 only builds with gcc, we can freely use gcc extensions here
6206 #define GET_UST_SYM(SYM) \
6209 if (ust_ops.SYM == NULL) \
6210 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6211 if (ust_ops.SYM == NULL) \
6215 #define USTF(SYM) ust_ops.SYM
6217 /* Get pointers to all libust.so functions we care about. */
6222 GET_UST_SYM (serialize_to_text
);
6224 GET_UST_SYM (ltt_probe_register
);
6225 GET_UST_SYM (ltt_probe_unregister
);
6226 GET_UST_SYM (ltt_marker_connect
);
6227 GET_UST_SYM (ltt_marker_disconnect
);
6229 GET_UST_SYM (marker_iter_start
);
6230 GET_UST_SYM (marker_iter_next
);
6231 GET_UST_SYM (marker_iter_stop
);
6232 GET_UST_SYM (marker_iter_reset
);
6238 /* Given an UST marker, return the matching gdb static tracepoint.
6239 The match is done by address. */
6241 static struct tracepoint
*
6242 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6244 struct tracepoint
*tpoint
;
6246 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6248 if (tpoint
->type
!= static_tracepoint
)
6251 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6258 /* The probe function we install on lttng/ust markers. Whenever a
6259 probed ust marker is hit, this function is called. This is similar
6260 to gdb_collect, only for static tracepoints, instead of fast
6264 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6265 struct registers
*regs
, void *call_private
,
6266 const char *fmt
, va_list *args
)
6268 struct tracepoint
*tpoint
;
6269 struct static_tracepoint_ctx ctx
;
6271 /* Don't do anything until the trace run is completely set up. */
6274 trace_debug ("gdb_probe: not tracing\n");
6278 ctx
.base
.type
= static_tracepoint
;
6279 ctx
.regcache_initted
= 0;
6284 /* Wrap the regblock in a register cache (in the stack, we don't
6285 want to malloc here). */
6286 ctx
.regspace
= alloca (register_cache_size ());
6287 if (ctx
.regspace
== NULL
)
6289 trace_debug ("Trace buffer block allocation failed, skipping");
6293 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6296 trace_debug ("gdb_probe: marker not known: "
6297 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6298 mdata
->location
, mdata
->channel
,
6299 mdata
->name
, mdata
->format
);
6303 if (!tpoint
->enabled
)
6305 trace_debug ("gdb_probe: tracepoint disabled");
6309 ctx
.tpoint
= tpoint
;
6311 trace_debug ("gdb_probe: collecting marker: "
6312 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6313 mdata
->location
, mdata
->channel
,
6314 mdata
->name
, mdata
->format
);
6316 /* Test the condition if present, and collect if true. */
6317 if (tpoint
->cond
== NULL
6318 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6321 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6322 tpoint
->address
, tpoint
);
6324 if (stopping_tracepoint
6325 || trace_buffer_is_full
6326 || expr_eval_result
!= expr_eval_no_error
)
6331 /* If there was a condition and it evaluated to false, the only
6332 way we would stop tracing is if there was an error during
6333 condition expression evaluation. */
6334 if (expr_eval_result
!= expr_eval_no_error
)
6339 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6340 static tracepoint string data. This is a string passed to the
6341 tracing library by the user, at the time of the tracepoint marker
6342 call. E.g., in the UST marker call:
6344 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6346 the collected data is "str FOOBAZ".
6350 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6351 struct traceframe
*tframe
)
6353 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6354 unsigned char *bufspace
;
6357 unsigned short blocklen
;
6361 trace_debug ("Wanted to collect static trace data, "
6362 "but there's no static trace data");
6366 va_copy (copy
, *umd
->args
);
6367 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6370 trace_debug ("Want to collect ust data");
6372 /* 'S' + size + string */
6373 bufspace
= add_traceframe_block (tframe
,
6374 1 + sizeof (blocklen
) + size
+ 1);
6375 if (bufspace
== NULL
)
6377 trace_debug ("Trace buffer block allocation failed, skipping");
6381 /* Identify a static trace data block. */
6384 blocklen
= size
+ 1;
6385 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6387 va_copy (copy
, *umd
->args
);
6388 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6389 size
+ 1, umd
->fmt
, copy
);
6392 trace_debug ("Storing static tracepoint data in regblock: %s",
6393 bufspace
+ 1 + sizeof (blocklen
));
6396 /* The probe to register with lttng/ust. */
6397 static struct ltt_available_probe gdb_ust_probe
=
6404 #endif /* HAVE_UST */
6405 #endif /* IN_PROCESS_AGENT */
6407 #ifndef IN_PROCESS_AGENT
6409 /* Ask the in-process agent to run a command. Since we don't want to
6410 have to handle the IPA hitting breakpoints while running the
6411 command, we pause all threads, remove all breakpoints, and then set
6412 the helper thread re-running. We communicate with the helper
6413 thread by means of direct memory xfering, and a socket for
6417 run_inferior_command (char *cmd
)
6420 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6422 trace_debug ("run_inferior_command: running: %s", cmd
);
6425 uninsert_all_breakpoints ();
6427 err
= agent_run_command (pid
, (const char *) cmd
);
6429 reinsert_all_breakpoints ();
6435 #else /* !IN_PROCESS_AGENT */
6437 #include <sys/socket.h>
6440 #ifndef UNIX_PATH_MAX
6441 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6444 /* Where we put the socked used for synchronization. */
6445 #define SOCK_DIR P_tmpdir
6447 /* Thread ID of the helper thread. GDBserver reads this to know which
6448 is the help thread. This is an LWP id on Linux. */
6449 int helper_thread_id
;
6452 init_named_socket (const char *name
)
6455 struct sockaddr_un addr
;
6457 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6460 warning ("socket creation failed: %s", strerror (errno
));
6464 addr
.sun_family
= AF_UNIX
;
6466 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6467 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6469 result
= access (name
, F_OK
);
6473 result
= unlink (name
);
6476 warning ("unlink failed: %s", strerror (errno
));
6480 warning ("socket %s already exists; overwriting", name
);
6483 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6486 warning ("bind failed: %s", strerror (errno
));
6491 result
= listen (fd
, 1);
6494 warning ("listen: %s", strerror (errno
));
6503 gdb_agent_socket_init (void)
6506 char name
[UNIX_PATH_MAX
];
6508 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
6509 SOCK_DIR
, getpid ());
6510 if (result
>= UNIX_PATH_MAX
)
6512 trace_debug ("string overflow allocating socket name");
6516 fd
= init_named_socket (name
);
6518 warning ("Error initializing named socket (%s) for communication with the "
6519 "ust helper thread. Check that directory exists and that it "
6520 "is writable.", name
);
6527 /* The next marker to be returned on a qTsSTM command. */
6528 static const struct marker
*next_st
;
6530 /* Returns the first known marker. */
6535 struct marker_iter iter
;
6537 USTF(marker_iter_reset
) (&iter
);
6538 USTF(marker_iter_start
) (&iter
);
6543 /* Returns the marker following M. */
6545 const struct marker
*
6546 next_marker (const struct marker
*m
)
6548 struct marker_iter iter
;
6550 USTF(marker_iter_reset
) (&iter
);
6551 USTF(marker_iter_start
) (&iter
);
6553 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6555 if (iter
.marker
== m
)
6557 USTF(marker_iter_next
) (&iter
);
6565 /* Return an hexstr version of the STR C string, fit for sending to
6569 cstr_to_hexstr (const char *str
)
6571 int len
= strlen (str
);
6572 char *hexstr
= xmalloc (len
* 2 + 1);
6573 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
6577 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6581 response_ust_marker (char *packet
, const struct marker
*st
)
6583 char *strid
, *format
, *tmp
;
6585 next_st
= next_marker (st
);
6587 tmp
= xmalloc (strlen (st
->channel
) + 1 +
6588 strlen (st
->name
) + 1);
6589 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
6591 strid
= cstr_to_hexstr (tmp
);
6594 format
= cstr_to_hexstr (st
->format
);
6596 sprintf (packet
, "m%s:%s:%s",
6597 paddress ((uintptr_t) st
->location
),
6605 /* Return the first static tracepoint, and initialize the state
6606 machine that will iterate through all the static tracepoints. */
6609 cmd_qtfstm (char *packet
)
6611 trace_debug ("Returning first trace state variable definition");
6613 if (first_marker ())
6614 response_ust_marker (packet
, first_marker ());
6616 strcpy (packet
, "l");
6619 /* Return additional trace state variable definitions. */
6622 cmd_qtsstm (char *packet
)
6624 trace_debug ("Returning static tracepoint");
6627 response_ust_marker (packet
, next_st
);
6629 strcpy (packet
, "l");
6632 /* Disconnect the GDB probe from a marker at a given address. */
6635 unprobe_marker_at (char *packet
)
6639 struct marker_iter iter
;
6641 p
+= sizeof ("unprobe_marker_at:") - 1;
6643 p
= unpack_varlen_hex (p
, &address
);
6645 USTF(marker_iter_reset
) (&iter
);
6646 USTF(marker_iter_start
) (&iter
);
6647 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
6648 if ((uintptr_t ) iter
.marker
->location
== address
)
6652 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
6656 warning ("could not disable marker %s/%s",
6657 iter
.marker
->channel
, iter
.marker
->name
);
6662 /* Connect the GDB probe to a marker at a given address. */
6665 probe_marker_at (char *packet
)
6669 struct marker_iter iter
;
6672 p
+= sizeof ("probe_marker_at:") - 1;
6674 p
= unpack_varlen_hex (p
, &address
);
6676 USTF(marker_iter_reset
) (&iter
);
6678 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6680 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6681 if ((uintptr_t ) m
->location
== address
)
6685 trace_debug ("found marker for address. "
6686 "ltt_marker_connect (marker = %s/%s)",
6687 m
->channel
, m
->name
);
6689 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
6691 if (result
&& result
!= -EEXIST
)
6692 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6693 m
->channel
, m
->name
, -result
);
6697 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
6698 m
->channel
, m
->name
);
6702 strcpy (packet
, "OK");
6706 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
6711 cmd_qtstmat (char *packet
)
6715 struct marker_iter iter
;
6718 p
+= sizeof ("qTSTMat:") - 1;
6720 p
= unpack_varlen_hex (p
, &address
);
6722 USTF(marker_iter_reset
) (&iter
);
6724 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
6726 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
6727 if ((uintptr_t ) m
->location
== address
)
6729 response_ust_marker (packet
, m
);
6733 strcpy (packet
, "l");
6743 USTF(ltt_probe_register
) (&gdb_ust_probe
);
6746 #endif /* HAVE_UST */
6748 #include <sys/syscall.h>
6750 /* Helper thread of agent. */
6753 gdb_agent_helper_thread (void *arg
)
6759 listen_fd
= gdb_agent_socket_init ();
6761 if (helper_thread_id
== 0)
6762 helper_thread_id
= syscall (SYS_gettid
);
6764 if (listen_fd
== -1)
6766 warning ("could not create sync socket\n");
6773 struct sockaddr_un sockaddr
;
6778 tmp
= sizeof (sockaddr
);
6782 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
6784 /* It seems an ERESTARTSYS can escape out of accept. */
6785 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
6789 warning ("Accept returned %d, error: %s\n",
6790 fd
, strerror (errno
));
6796 ret
= read (fd
, buf
, 1);
6797 } while (ret
== -1 && errno
== EINTR
);
6801 warning ("reading socket (fd=%d) failed with %s",
6802 fd
, strerror (errno
));
6810 if (strcmp ("qTfSTM", cmd_buf
) == 0)
6812 cmd_qtfstm (cmd_buf
);
6814 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
6816 cmd_qtsstm (cmd_buf
);
6818 else if (strncmp ("unprobe_marker_at:",
6820 sizeof ("unprobe_marker_at:") - 1) == 0)
6822 unprobe_marker_at (cmd_buf
);
6824 else if (strncmp ("probe_marker_at:",
6826 sizeof ("probe_marker_at:") - 1) == 0)
6828 probe_marker_at (cmd_buf
);
6830 else if (strncmp ("qTSTMat:",
6832 sizeof ("qTSTMat:") - 1) == 0)
6834 cmd_qtstmat (cmd_buf
);
6836 #endif /* HAVE_UST */
6839 /* Fix compiler's warning: ignoring return value of 'write'. */
6840 ret
= write (fd
, buf
, 1);
6849 #include <pthread.h>
6851 IP_AGENT_EXPORT
int gdb_agent_capability
= AGENT_CAPA_STATIC_TRACE
;
6854 gdb_agent_init (void)
6861 /* We want the helper thread to be as transparent as possible, so
6862 have it inherit an all-signals-blocked mask. */
6864 sigfillset (&new_mask
);
6865 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
6867 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
6869 res
= pthread_create (&thread
,
6871 gdb_agent_helper_thread
,
6874 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
6876 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
6878 while (helper_thread_id
== 0)
6886 #include <sys/mman.h>
6889 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
6890 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
6891 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
6892 IP_AGENT_EXPORT
char *gdb_trampoline_buffer
;
6893 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_end
;
6894 IP_AGENT_EXPORT
char *gdb_trampoline_buffer_error
;
6896 /* Record the result of getting buffer space for fast tracepoint
6897 trampolines. Any error message is copied, since caller may not be
6898 using persistent storage. */
6901 set_trampoline_buffer_space (CORE_ADDR begin
, CORE_ADDR end
, char *errmsg
)
6903 gdb_trampoline_buffer
= (char *) (uintptr_t) begin
;
6904 gdb_trampoline_buffer_end
= (char *) (uintptr_t) end
;
6906 strncpy (gdb_trampoline_buffer_error
, errmsg
, 99);
6908 strcpy (gdb_trampoline_buffer_error
, "no buffer passed");
6911 static void __attribute__ ((constructor
))
6912 initialize_tracepoint_ftlib (void)
6914 initialize_tracepoint ();
6919 #endif /* IN_PROCESS_AGENT */
6921 /* Return a timestamp, expressed as microseconds of the usual Unix
6922 time. (As the result is a 64-bit number, it will not overflow any
6926 get_timestamp (void)
6930 if (gettimeofday (&tv
, 0) != 0)
6933 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
6937 initialize_tracepoint (void)
6939 /* There currently no way to change the buffer size. */
6940 const int sizeOfBuffer
= 5 * 1024 * 1024;
6941 unsigned char *buf
= xmalloc (sizeOfBuffer
);
6942 init_trace_buffer (buf
, sizeOfBuffer
);
6944 /* Wire trace state variable 1 to be the timestamp. This will be
6945 uploaded to GDB upon connection and become one of its trace state
6946 variables. (In case you're wondering, if GDB already has a trace
6947 variable numbered 1, it will be renumbered.) */
6948 create_trace_state_variable (1, 0);
6949 set_trace_state_variable_name (1, "trace_timestamp");
6950 set_trace_state_variable_getter (1, get_timestamp
);
6952 #ifdef IN_PROCESS_AGENT
6957 pagesize
= sysconf (_SC_PAGE_SIZE
);
6961 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
6963 #define SCRATCH_BUFFER_NPAGES 20
6965 /* Allocate scratch buffer aligned on a page boundary, at a low
6966 address (close to the main executable's code). */
6967 for (addr
= pagesize
; addr
!= 0; addr
+= pagesize
)
6969 gdb_jump_pad_buffer
= mmap ((void *) addr
, pagesize
* SCRATCH_BUFFER_NPAGES
,
6970 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
6971 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
6973 if (gdb_jump_pad_buffer
!= MAP_FAILED
)
6979 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
6982 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* SCRATCH_BUFFER_NPAGES
;
6985 gdb_trampoline_buffer
= gdb_trampoline_buffer_end
= 0;
6987 /* It's not a fatal error for something to go wrong with trampoline
6988 buffer setup, but it can be mysterious, so create a channel to
6989 report back on what went wrong, using a fixed size since we may
6990 not be able to allocate space later when the problem occurs. */
6991 gdb_trampoline_buffer_error
= xmalloc (IPA_BUFSIZ
);
6993 strcpy (gdb_trampoline_buffer_error
, "No errors reported");
6995 initialize_low_tracepoint ();