1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009, 2010, 2011 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/>. */
29 /* This file is built for both GDBserver, and the in-process
30 agent (IPA), a shared library that includes a tracing agent that is
31 loaded by the inferior to support fast tracepoints. Fast
32 tracepoints (or more accurately, jump based tracepoints) are
33 implemented by patching the tracepoint location with a jump into a
34 small trampoline function whose job is to save the register state,
35 call the in-process tracing agent, and then execute the original
36 instruction that was under the tracepoint jump (possibly adjusted,
37 if PC-relative, or some such).
39 The current synchronization design is pull based. That means,
40 GDBserver does most of the work, by peeking/poking at the inferior
41 agent's memory directly for downloading tracepoint and associated
42 objects, and for uploading trace frames. Whenever the IPA needs
43 something from GDBserver (trace buffer is full, tracing stopped for
44 some reason, etc.) the IPA calls a corresponding hook function
45 where GDBserver has placed a breakpoint.
47 Each of the agents has its own trace buffer. When browsing the
48 trace frames built from slow and fast tracepoints from GDB (tfind
49 mode), there's no guarantee the user is seeing the trace frames in
50 strict chronological creation order, although, GDBserver tries to
51 keep the order relatively reasonable, by syncing the trace buffers
56 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf
, 1, 2);
59 trace_vdebug (const char *fmt
, ...)
65 vsprintf (buf
, fmt
, ap
);
66 fprintf (stderr
, "gdbserver/tracepoint: %s\n", buf
);
70 #define trace_debug_1(level, fmt, args...) \
72 if (level <= debug_threads) \
73 trace_vdebug ((fmt), ##args); \
76 #define trace_debug(FMT, args...) \
77 trace_debug_1 (1, FMT, ##args)
80 # define ATTR_USED __attribute__((used))
81 # define ATTR_NOINLINE __attribute__((noinline))
82 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
85 # define ATTR_NOINLINE
86 # define ATTR_CONSTRUCTOR
89 /* Make sure the functions the IPA needs to export (symbols GDBserver
90 needs to query GDB about) are exported. */
92 #ifdef IN_PROCESS_AGENT
93 # if defined _WIN32 || defined __CYGWIN__
94 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
97 # define IP_AGENT_EXPORT \
98 __attribute__ ((visibility("default"))) ATTR_USED
100 # define IP_AGENT_EXPORT ATTR_USED
104 # define IP_AGENT_EXPORT
107 /* Prefix exported symbols, for good citizenship. All the symbols
108 that need exporting are defined in this module. */
109 #ifdef IN_PROCESS_AGENT
110 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
111 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
112 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
113 # define collecting gdb_agent_collecting
114 # define gdb_collect gdb_agent_gdb_collect
115 # define stop_tracing gdb_agent_stop_tracing
116 # define flush_trace_buffer gdb_agent_flush_trace_buffer
117 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
118 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
119 # define stopping_tracepoint gdb_agent_stopping_tracepoint
120 # define expr_eval_result gdb_agent_expr_eval_result
121 # define error_tracepoint gdb_agent_error_tracepoint
122 # define tracepoints gdb_agent_tracepoints
123 # define tracing gdb_agent_tracing
124 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
125 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
126 # define trace_buffer_lo gdb_agent_trace_buffer_lo
127 # define trace_buffer_hi gdb_agent_trace_buffer_hi
128 # define traceframe_read_count gdb_agent_traceframe_read_count
129 # define traceframe_write_count gdb_agent_traceframe_write_count
130 # define traceframes_created gdb_agent_traceframes_created
131 # define trace_state_variables gdb_agent_trace_state_variables
132 # define get_raw_reg gdb_agent_get_raw_reg
133 # define get_trace_state_variable_value \
134 gdb_agent_get_trace_state_variable_value
135 # define set_trace_state_variable_value \
136 gdb_agent_set_trace_state_variable_value
137 # define ust_loaded gdb_agent_ust_loaded
138 # define helper_thread_id gdb_agent_helper_thread_id
139 # define cmd_buf gdb_agent_cmd_buf
142 #ifndef IN_PROCESS_AGENT
144 /* Addresses of in-process agent's symbols GDBserver cares about. */
146 struct ipa_sym_addresses
148 CORE_ADDR addr_gdb_tp_heap_buffer
;
149 CORE_ADDR addr_gdb_jump_pad_buffer
;
150 CORE_ADDR addr_gdb_jump_pad_buffer_end
;
151 CORE_ADDR addr_collecting
;
152 CORE_ADDR addr_gdb_collect
;
153 CORE_ADDR addr_stop_tracing
;
154 CORE_ADDR addr_flush_trace_buffer
;
155 CORE_ADDR addr_about_to_request_buffer_space
;
156 CORE_ADDR addr_trace_buffer_is_full
;
157 CORE_ADDR addr_stopping_tracepoint
;
158 CORE_ADDR addr_expr_eval_result
;
159 CORE_ADDR addr_error_tracepoint
;
160 CORE_ADDR addr_tracepoints
;
161 CORE_ADDR addr_tracing
;
162 CORE_ADDR addr_trace_buffer_ctrl
;
163 CORE_ADDR addr_trace_buffer_ctrl_curr
;
164 CORE_ADDR addr_trace_buffer_lo
;
165 CORE_ADDR addr_trace_buffer_hi
;
166 CORE_ADDR addr_traceframe_read_count
;
167 CORE_ADDR addr_traceframe_write_count
;
168 CORE_ADDR addr_traceframes_created
;
169 CORE_ADDR addr_trace_state_variables
;
170 CORE_ADDR addr_get_raw_reg
;
171 CORE_ADDR addr_get_trace_state_variable_value
;
172 CORE_ADDR addr_set_trace_state_variable_value
;
173 CORE_ADDR addr_ust_loaded
;
174 CORE_ADDR addr_helper_thread_id
;
175 CORE_ADDR addr_cmd_buf
;
178 #define STRINGIZE_1(STR) #STR
179 #define STRINGIZE(STR) STRINGIZE_1(STR)
180 #define IPA_SYM(SYM) \
182 STRINGIZE (gdb_agent_ ## SYM), \
183 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
192 IPA_SYM(gdb_tp_heap_buffer
),
193 IPA_SYM(gdb_jump_pad_buffer
),
194 IPA_SYM(gdb_jump_pad_buffer_end
),
196 IPA_SYM(gdb_collect
),
197 IPA_SYM(stop_tracing
),
198 IPA_SYM(flush_trace_buffer
),
199 IPA_SYM(about_to_request_buffer_space
),
200 IPA_SYM(trace_buffer_is_full
),
201 IPA_SYM(stopping_tracepoint
),
202 IPA_SYM(expr_eval_result
),
203 IPA_SYM(error_tracepoint
),
204 IPA_SYM(tracepoints
),
206 IPA_SYM(trace_buffer_ctrl
),
207 IPA_SYM(trace_buffer_ctrl_curr
),
208 IPA_SYM(trace_buffer_lo
),
209 IPA_SYM(trace_buffer_hi
),
210 IPA_SYM(traceframe_read_count
),
211 IPA_SYM(traceframe_write_count
),
212 IPA_SYM(traceframes_created
),
213 IPA_SYM(trace_state_variables
),
214 IPA_SYM(get_raw_reg
),
215 IPA_SYM(get_trace_state_variable_value
),
216 IPA_SYM(set_trace_state_variable_value
),
218 IPA_SYM(helper_thread_id
),
222 struct ipa_sym_addresses ipa_sym_addrs
;
224 int all_tracepoint_symbols_looked_up
;
227 in_process_agent_loaded (void)
229 return all_tracepoint_symbols_looked_up
;
232 static int read_inferior_integer (CORE_ADDR symaddr
, int *val
);
234 /* Returns true if both the in-process agent library and the static
235 tracepoints libraries are loaded in the inferior. */
238 in_process_agent_loaded_ust (void)
242 if (!in_process_agent_loaded ())
244 warning ("In-process agent not loaded");
248 if (read_inferior_integer (ipa_sym_addrs
.addr_ust_loaded
, &loaded
))
250 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 (!in_process_agent_loaded ())
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 (!in_process_agent_loaded ())
303 write_e_ipa_not_loaded (buffer
);
306 else if (!in_process_agent_loaded_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)
326 if (all_tracepoint_symbols_looked_up
)
330 for (i
= 0; i
< sizeof (symbol_list
) / sizeof (symbol_list
[0]); i
++)
333 (CORE_ADDR
*) ((char *) &ipa_sym_addrs
+ symbol_list
[i
].offset
);
335 if (look_up_one_symbol (symbol_list
[i
].name
, addrp
, 1) == 0)
338 fprintf (stderr
, "symbol `%s' not found\n", symbol_list
[i
].name
);
343 all_tracepoint_symbols_looked_up
= all_ok
;
348 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
349 of the "stop_tracing" function. When this breakpoint is hit,
350 tracing stopped in the IPA for some reason. E.g., due to
351 tracepoint reaching the pass count, hitting conditional expression
352 evaluation error, etc.
354 The IPA's trace buffer is never in circular tracing mode: instead,
355 GDBserver's is, and whenever the in-process buffer fills, it calls
356 "flush_trace_buffer", which triggers an internal breakpoint.
357 GDBserver reacts to this breakpoint by pulling the meanwhile
358 collected data. Old frames discarding is always handled on the
361 #ifdef IN_PROCESS_AGENT
362 int debug_threads
= 0;
365 read_inferior_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
367 memcpy (myaddr
, (void *) (uintptr_t) memaddr
, len
);
371 /* Call this in the functions where GDBserver places a breakpoint, so
372 that the compiler doesn't try to be clever and skip calling the
373 function at all. This is necessary, even if we tell the compiler
374 to not inline said functions. */
376 #if defined(__GNUC__)
377 # define UNKNOWN_SIDE_EFFECTS() asm ("")
379 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
382 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
385 /* GDBserver places breakpoint here. */
386 UNKNOWN_SIDE_EFFECTS();
389 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
390 flush_trace_buffer (void)
392 /* GDBserver places breakpoint here. */
393 UNKNOWN_SIDE_EFFECTS();
398 #ifndef IN_PROCESS_AGENT
400 tracepoint_handler (CORE_ADDR address
)
402 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
407 /* Breakpoint at "stop_tracing" in the inferior lib. */
408 struct breakpoint
*stop_tracing_bkpt
;
409 static int stop_tracing_handler (CORE_ADDR
);
411 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
412 struct breakpoint
*flush_trace_buffer_bkpt
;
413 static int flush_trace_buffer_handler (CORE_ADDR
);
415 static void download_tracepoints (void);
416 static void download_trace_state_variables (void);
417 static void upload_fast_traceframes (void);
419 static int run_inferior_command (char *cmd
);
422 read_inferior_integer (CORE_ADDR symaddr
, int *val
)
424 return read_inferior_memory (symaddr
, (unsigned char *) val
,
429 read_inferior_uinteger (CORE_ADDR symaddr
, unsigned int *val
)
431 return read_inferior_memory (symaddr
, (unsigned char *) val
,
436 read_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR
*val
)
438 void *pval
= (void *) (uintptr_t) val
;
441 ret
= read_inferior_memory (symaddr
, (unsigned char *) &pval
, sizeof (pval
));
442 *val
= (uintptr_t) pval
;
447 write_inferior_data_pointer (CORE_ADDR symaddr
, CORE_ADDR val
)
449 void *pval
= (void *) (uintptr_t) val
;
450 return write_inferior_memory (symaddr
,
451 (unsigned char *) &pval
, sizeof (pval
));
455 write_inferior_integer (CORE_ADDR symaddr
, int val
)
457 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
461 write_inferior_uinteger (CORE_ADDR symaddr
, unsigned int val
)
463 return write_inferior_memory (symaddr
, (unsigned char *) &val
, sizeof (val
));
468 /* This enum must exactly match what is documented in
469 gdb/doc/agentexpr.texi, including all the numerical values. */
473 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
474 gdb_agent_op_ ## NAME = VALUE,
480 static const char *gdb_agent_op_names
[gdb_agent_op_last
] =
483 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
492 unsigned char *bytes
;
495 /* Base action. Concrete actions inherit this. */
497 struct tracepoint_action
502 /* An 'M' (collect memory) action. */
503 struct collect_memory_action
505 struct tracepoint_action base
;
512 /* An 'R' (collect registers) action. */
514 struct collect_registers_action
516 struct tracepoint_action base
;
519 /* An 'X' (evaluate expression) action. */
521 struct eval_expr_action
523 struct tracepoint_action base
;
525 struct agent_expr
*expr
;
528 /* An 'L' (collect static trace data) action. */
529 struct collect_static_trace_data_action
531 struct tracepoint_action base
;
534 /* This structure describes a piece of the source-level definition of
535 the tracepoint. The contents are not interpreted by the target,
536 but preserved verbatim for uploading upon reconnection. */
540 /* The type of string, such as "cond" for a conditional. */
543 /* The source-level string itself. For the sake of target
544 debugging, we store it in plaintext, even though it is always
545 transmitted in hex. */
548 /* Link to the next one in the list. We link them in the order
549 received, in case some make up an ordered list of commands or
551 struct source_string
*next
;
556 /* Trap based tracepoint. */
559 /* A fast tracepoint implemented with a jump instead of a trap. */
562 /* A static tracepoint, implemented by a program call into a tracing
567 struct tracepoint_hit_ctx
;
569 typedef enum eval_result_type (*condfn
) (struct tracepoint_hit_ctx
*,
572 /* The definition of a tracepoint. */
574 /* Tracepoints may have multiple locations, each at a different
575 address. This can occur with optimizations, template
576 instantiation, etc. Since the locations may be in different
577 scopes, the conditions and actions may be different for each
578 location. Our target version of tracepoints is more like GDB's
579 notion of "breakpoint locations", but we have almost nothing that
580 is not per-location, so we bother having two kinds of objects. The
581 key consequence is that numbers are not unique, and that it takes
582 both number and address to identify a tracepoint uniquely. */
586 /* The number of the tracepoint, as specified by GDB. Several
587 tracepoint objects here may share a number. */
590 /* Address at which the tracepoint is supposed to trigger. Several
591 tracepoints may share an address. */
594 /* Tracepoint type. */
595 enum tracepoint_type type
;
597 /* True if the tracepoint is currently enabled. */
600 /* The number of single steps that will be performed after each
604 /* The number of times the tracepoint may be hit before it will
605 terminate the entire tracing run. */
608 /* Pointer to the agent expression that is the tracepoint's
609 conditional, or NULL if the tracepoint is unconditional. */
610 struct agent_expr
*cond
;
612 /* The list of actions to take when the tracepoint triggers. */
614 struct tracepoint_action
**actions
;
616 /* Count of the times we've hit this tracepoint during the run.
617 Note that while-stepping steps are not counted as "hits". */
620 CORE_ADDR compiled_cond
;
622 /* Link to the next tracepoint in the list. */
623 struct tracepoint
*next
;
625 #ifndef IN_PROCESS_AGENT
626 /* The list of actions to take when the tracepoint triggers, in
627 string/packet form. */
630 /* The collection of strings that describe the tracepoint as it was
631 entered into GDB. These are not used by the target, but are
632 reported back to GDB upon reconnection. */
633 struct source_string
*source_strings
;
635 /* The number of bytes displaced by fast tracepoints. It may subsume
636 multiple instructions, for multi-byte fast tracepoints. This
637 field is only valid for fast tracepoints. */
640 /* Only for fast tracepoints. */
641 CORE_ADDR obj_addr_on_target
;
643 /* Address range where the original instruction under a fast
644 tracepoint was relocated to. (_end is actually one byte past
646 CORE_ADDR adjusted_insn_addr
;
647 CORE_ADDR adjusted_insn_addr_end
;
649 /* The address range of the piece of the jump pad buffer that was
650 assigned to this fast tracepoint. (_end is actually one byte
653 CORE_ADDR jump_pad_end
;
655 /* The list of actions to take while in a stepping loop. These
656 fields are only valid for patch-based tracepoints. */
657 int num_step_actions
;
658 struct tracepoint_action
**step_actions
;
659 /* Same, but in string/packet form. */
660 char **step_actions_str
;
662 /* Handle returned by the breakpoint or tracepoint module when we
663 inserted the trap or jump, or hooked into a static tracepoint.
664 NULL if we haven't inserted it yet. */
670 #ifndef IN_PROCESS_AGENT
672 /* Given `while-stepping', a thread may be collecting data for more
673 than one tracepoint simultaneously. On the other hand, the same
674 tracepoint with a while-stepping action may be hit by more than one
675 thread simultaneously (but not quite, each thread could be handling
676 a different step). Each thread holds a list of these objects,
677 representing the current step of each while-stepping action being
682 struct wstep_state
*next
;
684 /* The tracepoint number. */
686 /* The tracepoint's address. */
687 CORE_ADDR tp_address
;
689 /* The number of the current step in this 'while-stepping'
696 /* The linked list of all tracepoints. Marked explicitly as used as
697 the in-process library doesn't use it for the fast tracepoints
699 IP_AGENT_EXPORT
struct tracepoint
*tracepoints ATTR_USED
;
701 #ifndef IN_PROCESS_AGENT
703 /* Pointer to the last tracepoint in the list, new tracepoints are
704 linked in at the end. */
706 static struct tracepoint
*last_tracepoint
;
709 /* The first tracepoint to exceed its pass count. */
711 IP_AGENT_EXPORT
struct tracepoint
*stopping_tracepoint
;
713 /* True if the trace buffer is full or otherwise no longer usable. */
715 IP_AGENT_EXPORT
int trace_buffer_is_full
;
717 /* Enumeration of the different kinds of things that can happen during
718 agent expression evaluation. */
720 enum eval_result_type
723 expr_eval_empty_expression
,
724 expr_eval_empty_stack
,
725 expr_eval_stack_overflow
,
726 expr_eval_stack_underflow
,
727 expr_eval_unhandled_opcode
,
728 expr_eval_unrecognized_opcode
,
729 expr_eval_divide_by_zero
,
730 expr_eval_invalid_goto
733 static enum eval_result_type expr_eval_result
= expr_eval_no_error
;
735 #ifndef IN_PROCESS_AGENT
737 static const char *eval_result_names
[] =
739 "terror:in the attic", /* this should never be reported */
740 "terror:empty expression",
741 "terror:empty stack",
742 "terror:stack overflow",
743 "terror:stack underflow",
744 "terror:unhandled opcode",
745 "terror:unrecognized opcode",
746 "terror:divide by zero"
751 /* The tracepoint in which the error occurred. */
753 static struct tracepoint
*error_tracepoint
;
755 struct trace_state_variable
757 /* This is the name of the variable as used in GDB. The target
758 doesn't use the name, but needs to have it for saving and
759 reconnection purposes. */
762 /* This number identifies the variable uniquely. Numbers may be
763 assigned either by the target (in the case of builtin variables),
764 or by GDB, and are presumed unique during the course of a trace
768 /* The variable's initial value, a 64-bit signed integer always. */
769 LONGEST initial_value
;
771 /* The variable's value, a 64-bit signed integer always. */
774 /* Pointer to a getter function, used to supply computed values. */
775 LONGEST (*getter
) (void);
777 /* Link to the next variable. */
778 struct trace_state_variable
*next
;
781 /* Linked list of all trace state variables. */
783 #ifdef IN_PROCESS_AGENT
784 struct trace_state_variable
*alloced_trace_state_variables
;
787 IP_AGENT_EXPORT
struct trace_state_variable
*trace_state_variables
;
789 /* The results of tracing go into a fixed-size space known as the
790 "trace buffer". Because usage follows a limited number of
791 patterns, we manage it ourselves rather than with malloc. Basic
792 rules are that we create only one trace frame at a time, each is
793 variable in size, they are never moved once created, and we only
794 discard if we are doing a circular buffer, and then only the oldest
795 ones. Each trace frame includes its own size, so we don't need to
796 link them together, and the trace frame number is relative to the
797 first one, so we don't need to record numbers. A trace frame also
798 records the number of the tracepoint that created it. The data
799 itself is a series of blocks, each introduced by a single character
800 and with a defined format. Each type of block has enough
801 type/length info to allow scanners to jump quickly from one block
802 to the next without reading each byte in the block. */
804 /* Trace buffer management would be simple - advance a free pointer
805 from beginning to end, then stop - were it not for the circular
806 buffer option, which is a useful way to prevent a trace run from
807 stopping prematurely because the buffer filled up. In the circular
808 case, the location of the first trace frame (trace_buffer_start)
809 moves as old trace frames are discarded. Also, since we grow trace
810 frames incrementally as actions are performed, we wrap around to
811 the beginning of the trace buffer. This is per-block, so each
812 block within a trace frame remains contiguous. Things get messy
813 when the wrapped-around trace frame is the one being discarded; the
814 free space ends up in two parts at opposite ends of the buffer. */
817 # if defined(__GNUC__)
818 # define ATTR_PACKED __attribute__ ((packed))
820 # define ATTR_PACKED /* nothing */
824 /* The data collected at a tracepoint hit. This object should be as
825 small as possible, since there may be a great many of them. We do
826 not need to keep a frame number, because they are all sequential
827 and there are no deletions; so the Nth frame in the buffer is
828 always frame number N. */
832 /* Number of the tracepoint that collected this traceframe. A value
833 of 0 indicates the current end of the trace buffer. We make this
834 a 16-bit field because it's never going to happen that GDB's
835 numbering of tracepoints reaches 32,000. */
838 /* The size of the data in this trace frame. We limit this to 32
839 bits, even on a 64-bit target, because it's just implausible that
840 one is validly going to collect 4 gigabytes of data at a single
842 unsigned int data_size
: 32;
844 /* The base of the trace data, which is contiguous from this point. */
845 unsigned char data
[0];
849 /* The traceframe to be used as the source of data to send back to
850 GDB. A value of -1 means to get data from the live program. */
852 int current_traceframe
= -1;
854 /* This flag is true if the trace buffer is circular, meaning that
855 when it fills, the oldest trace frames are discarded in order to
858 #ifndef IN_PROCESS_AGENT
859 static int circular_trace_buffer
;
862 /* Pointer to the block of memory that traceframes all go into. */
864 static unsigned char *trace_buffer_lo
;
866 /* Pointer to the end of the trace buffer, more precisely to the byte
867 after the end of the buffer. */
869 static unsigned char *trace_buffer_hi
;
871 /* Control structure holding the read/write/etc. pointers into the
872 trace buffer. We need more than one of these to implement a
873 transaction-like mechanism to garantees that both GDBserver and the
874 in-process agent can try to change the trace buffer
877 struct trace_buffer_control
879 /* Pointer to the first trace frame in the buffer. In the
880 non-circular case, this is equal to trace_buffer_lo, otherwise it
881 moves around in the buffer. */
882 unsigned char *start
;
884 /* Pointer to the free part of the trace buffer. Note that we clear
885 several bytes at and after this pointer, so that traceframe
886 scans/searches terminate properly. */
889 /* Pointer to the byte after the end of the free part. Note that
890 this may be smaller than trace_buffer_free in the circular case,
891 and means that the free part is in two pieces. Initially it is
892 equal to trace_buffer_hi, then is generally equivalent to
893 trace_buffer_start. */
894 unsigned char *end_free
;
896 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
897 this is the point at which the trace data breaks, and resumes at
902 /* Same as above, to be used by GDBserver when updating the in-process
904 struct ipa_trace_buffer_control
913 /* We have possibly both GDBserver and an inferior thread accessing
914 the same IPA trace buffer memory. The IPA is the producer (tries
915 to put new frames in the buffer), while GDBserver occasionally
916 consumes them, that is, flushes the IPA's buffer into its own
917 buffer. Both sides need to update the trace buffer control
918 pointers (current head, tail, etc.). We can't use a global lock to
919 synchronize the accesses, as otherwise we could deadlock GDBserver
920 (if the thread holding the lock stops for a signal, say). So
921 instead of that, we use a transaction scheme where GDBserver writes
922 always prevail over the IPAs writes, and, we have the IPA detect
923 the commit failure/overwrite, and retry the whole attempt. This is
924 mainly implemented by having a global token object that represents
925 who wrote last to the buffer control structure. We need to freeze
926 any inferior writing to the buffer while GDBserver touches memory,
927 so that the inferior can correctly detect that GDBserver had been
928 there, otherwise, it could mistakingly think its commit was
929 successful; that's implemented by simply having GDBserver set a
930 breakpoint the inferior hits if it is the critical region.
932 There are three cycling trace buffer control structure copies
933 (buffer head, tail, etc.), with the token object including an index
934 indicating which is current live copy. The IPA tentatively builds
935 an updated copy in a non-current control structure, while GDBserver
936 always clobbers the current version directly. The IPA then tries
937 to atomically "commit" its version; if GDBserver clobbered the
938 structure meanwhile, that will fail, and the IPA restarts the
941 Listing the step in further detail, we have:
943 In-process agent (producer):
945 - passes by `about_to_request_buffer_space' breakpoint/lock
947 - reads current token, extracts current trace buffer control index,
948 and starts tentatively updating the rightmost one (0->1, 1->2,
949 2->0). Note that only one inferior thread is executing this code
950 at any given time, due to an outer lock in the jump pads.
952 - updates counters, and tries to commit the token.
954 - passes by second `about_to_request_buffer_space' breakpoint/lock,
955 leaving the sync region.
957 - checks if the update was effective.
959 - if trace buffer was found full, hits flush_trace_buffer
960 breakpoint, and restarts later afterwards.
962 GDBserver (consumer):
964 - sets `about_to_request_buffer_space' breakpoint/lock.
966 - updates the token unconditionally, using the current buffer
967 control index, since it knows that the IP agent always writes to
968 the rightmost, and due to the breakpoint, at most one IP thread
969 can try to update the trace buffer concurrently to GDBserver, so
970 there will be no danger of trace buffer control index wrap making
971 the IPA write to the same index as GDBserver.
973 - flushes the IP agent's trace buffer completely, and updates the
974 current trace buffer control structure. GDBserver *always* wins.
976 - removes the `about_to_request_buffer_space' breakpoint.
978 The token is stored in the `trace_buffer_ctrl_curr' variable.
979 Internally, it's bits are defined as:
981 |-------------+-----+-------------+--------+-------------+--------------|
982 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
983 |-------------+-----+-------------+--------+-------------+--------------|
984 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
985 |-------------+-----+-------------+--------+-------------+--------------|
987 GSB - GDBserver Stamp Bit
988 PC - Previous Counter
990 TBCI - Trace Buffer Control Index
993 An IPA update of `trace_buffer_ctrl_curr' does:
995 - read CC from the current token, save as PC.
997 - atomically tries to write PC+1,CC
999 A GDBserver update of `trace_buffer_ctrl_curr' does:
1001 - reads PC and CC from the current token.
1006 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1007 for the counters described below. The cleared bits are used to
1008 hold the index of the items of the `trace_buffer_ctrl' array that
1010 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1012 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1013 counter, and the `current' counter. */
1015 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1016 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1018 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1019 always stamps this bit as set. */
1020 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1022 #ifdef IN_PROCESS_AGENT
1023 IP_AGENT_EXPORT
struct trace_buffer_control trace_buffer_ctrl
[3];
1024 IP_AGENT_EXPORT
unsigned int trace_buffer_ctrl_curr
;
1026 # define TRACE_BUFFER_CTRL_CURR \
1027 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1031 /* The GDBserver side agent only needs one instance of this object, as
1032 it doesn't need to sync with itself. Define it as array anyway so
1033 that the rest of the code base doesn't need to care for the
1035 struct trace_buffer_control trace_buffer_ctrl
[1];
1036 # define TRACE_BUFFER_CTRL_CURR 0
1039 /* These are convenience macros used to access the current trace
1040 buffer control in effect. */
1041 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1042 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1043 #define trace_buffer_end_free \
1044 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1045 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1048 /* Macro that returns a pointer to the first traceframe in the buffer. */
1050 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1052 /* Macro that returns a pointer to the next traceframe in the buffer.
1053 If the computed location is beyond the wraparound point, subtract
1054 the offset of the wraparound. */
1056 #define NEXT_TRACEFRAME_1(TF) \
1057 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1059 #define NEXT_TRACEFRAME(TF) \
1060 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1061 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1062 ? (trace_buffer_wrap - trace_buffer_lo) \
1065 /* The difference between these counters represents the total number
1066 of complete traceframes present in the trace buffer. The IP agent
1067 writes to the write count, GDBserver writes to read count. */
1069 IP_AGENT_EXPORT
unsigned int traceframe_write_count
;
1070 IP_AGENT_EXPORT
unsigned int traceframe_read_count
;
1072 /* Convenience macro. */
1074 #define traceframe_count \
1075 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1077 /* The count of all traceframes created in the current run, including
1078 ones that were discarded to make room. */
1080 IP_AGENT_EXPORT
int traceframes_created
;
1082 #ifndef IN_PROCESS_AGENT
1084 /* Read-only regions are address ranges whose contents don't change,
1085 and so can be read from target memory even while looking at a trace
1086 frame. Without these, disassembly for instance will likely fail,
1087 because the program code is not usually collected into a trace
1088 frame. This data structure does not need to be very complicated or
1089 particularly efficient, it's only going to be used occasionally,
1090 and only by some commands. */
1092 struct readonly_region
1094 /* The bounds of the region. */
1095 CORE_ADDR start
, end
;
1097 /* Link to the next one. */
1098 struct readonly_region
*next
;
1101 /* Linked list of readonly regions. This list stays in effect from
1102 one tstart to the next. */
1104 static struct readonly_region
*readonly_regions
;
1108 /* The global that controls tracing overall. */
1110 IP_AGENT_EXPORT
int tracing
;
1112 #ifndef IN_PROCESS_AGENT
1114 /* Controls whether tracing should continue after GDB disconnects. */
1116 int disconnected_tracing
;
1118 /* The reason for the last tracing run to have stopped. We initialize
1119 to a distinct string so that GDB can distinguish between "stopped
1120 after running" and "stopped because never run" cases. */
1122 static const char *tracing_stop_reason
= "tnotrun";
1124 static int tracing_stop_tpnum
;
1128 /* Functions local to this file. */
1130 /* Base "class" for tracepoint type specific data to be passed down to
1131 collect_data_at_tracepoint. */
1132 struct tracepoint_hit_ctx
1134 enum tracepoint_type type
;
1137 #ifdef IN_PROCESS_AGENT
1139 /* Fast/jump tracepoint specific data to be passed down to
1140 collect_data_at_tracepoint. */
1141 struct fast_tracepoint_ctx
1143 struct tracepoint_hit_ctx base
;
1145 struct regcache regcache
;
1146 int regcache_initted
;
1147 unsigned char *regspace
;
1149 unsigned char *regs
;
1150 struct tracepoint
*tpoint
;
1153 /* Static tracepoint specific data to be passed down to
1154 collect_data_at_tracepoint. */
1155 struct static_tracepoint_ctx
1157 struct tracepoint_hit_ctx base
;
1159 /* The regcache corresponding to the registers state at the time of
1160 the tracepoint hit. Initialized lazily, from REGS. */
1161 struct regcache regcache
;
1162 int regcache_initted
;
1164 /* The buffer space REGCACHE above uses. We use a separate buffer
1165 instead of letting the regcache malloc for both signal safety and
1166 performance reasons; this is allocated on the stack instead. */
1167 unsigned char *regspace
;
1169 /* The register buffer as passed on by lttng/ust. */
1170 struct registers
*regs
;
1172 /* The "printf" formatter and the args the user passed to the marker
1173 call. We use this to be able to collect "static trace data"
1178 /* The GDB tracepoint matching the probed marker that was "hit". */
1179 struct tracepoint
*tpoint
;
1184 /* Static tracepoint specific data to be passed down to
1185 collect_data_at_tracepoint. */
1186 struct trap_tracepoint_ctx
1188 struct tracepoint_hit_ctx base
;
1190 struct regcache
*regcache
;
1195 #ifndef IN_PROCESS_AGENT
1196 static struct agent_expr
*parse_agent_expr (char **actparm
);
1197 static char *unparse_agent_expr (struct agent_expr
*aexpr
);
1199 static enum eval_result_type
eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
1200 struct traceframe
*tframe
,
1201 struct agent_expr
*aexpr
,
1204 static int agent_mem_read (struct traceframe
*tframe
,
1205 unsigned char *to
, CORE_ADDR from
, ULONGEST len
);
1206 static int agent_tsv_read (struct traceframe
*tframe
, int n
);
1208 #ifndef IN_PROCESS_AGENT
1209 static CORE_ADDR
traceframe_get_pc (struct traceframe
*tframe
);
1210 static int traceframe_read_tsv (int num
, LONGEST
*val
);
1213 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1214 struct tracepoint
*tpoint
);
1216 #ifndef IN_PROCESS_AGENT
1217 static void clear_readonly_regions (void);
1218 static void clear_installed_tracepoints (void);
1221 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1223 struct tracepoint
*tpoint
);
1224 #ifndef IN_PROCESS_AGENT
1225 static void collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
1227 struct tracepoint
*tpoint
, int current_step
);
1228 static void compile_tracepoint_condition (struct tracepoint
*tpoint
,
1229 CORE_ADDR
*jump_entry
);
1231 static void do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
1233 struct tracepoint
*tpoint
,
1234 struct traceframe
*tframe
,
1235 struct tracepoint_action
*taction
);
1237 #ifndef IN_PROCESS_AGENT
1238 static struct tracepoint
*fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR
);
1241 #if defined(__GNUC__)
1242 # define memory_barrier() asm volatile ("" : : : "memory")
1244 # define memory_barrier() do {} while (0)
1247 /* We only build the IPA if this builtin is supported, and there are
1248 no uses of this in GDBserver itself, so we're safe in defining this
1250 #define cmpxchg(mem, oldval, newval) \
1251 __sync_val_compare_and_swap (mem, oldval, newval)
1253 /* The size in bytes of the buffer used to talk to the IPA helper
1255 #define CMD_BUF_SIZE 1024
1257 /* Record that an error occurred during expression evaluation. */
1260 record_tracepoint_error (struct tracepoint
*tpoint
, const char *which
,
1261 enum eval_result_type rtype
)
1263 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1264 tpoint
->number
, paddress (tpoint
->address
), which
, rtype
);
1266 #ifdef IN_PROCESS_AGENT
1267 /* Only record the first error we get. */
1268 if (cmpxchg (&expr_eval_result
,
1270 rtype
) != expr_eval_no_error
)
1273 if (expr_eval_result
!= expr_eval_no_error
)
1277 error_tracepoint
= tpoint
;
1280 /* Trace buffer management. */
1283 clear_trace_buffer (void)
1285 trace_buffer_start
= trace_buffer_lo
;
1286 trace_buffer_free
= trace_buffer_lo
;
1287 trace_buffer_end_free
= trace_buffer_hi
;
1288 trace_buffer_wrap
= trace_buffer_hi
;
1289 /* A traceframe with zeroed fields marks the end of trace data. */
1290 ((struct traceframe
*) trace_buffer_free
)->tpnum
= 0;
1291 ((struct traceframe
*) trace_buffer_free
)->data_size
= 0;
1292 traceframe_read_count
= traceframe_write_count
= 0;
1293 traceframes_created
= 0;
1296 #ifndef IN_PROCESS_AGENT
1299 clear_inferior_trace_buffer (void)
1301 CORE_ADDR ipa_trace_buffer_lo
;
1302 CORE_ADDR ipa_trace_buffer_hi
;
1303 struct traceframe ipa_traceframe
= { 0 };
1304 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
1306 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
1307 &ipa_trace_buffer_lo
);
1308 read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
1309 &ipa_trace_buffer_hi
);
1311 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
1312 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
1313 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
1314 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
1316 /* A traceframe with zeroed fields marks the end of trace data. */
1317 write_inferior_memory (ipa_sym_addrs
.addr_trace_buffer_ctrl
,
1318 (unsigned char *) &ipa_trace_buffer_ctrl
,
1319 sizeof (ipa_trace_buffer_ctrl
));
1321 write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
, 0);
1323 /* A traceframe with zeroed fields marks the end of trace data. */
1324 write_inferior_memory (ipa_trace_buffer_lo
,
1325 (unsigned char *) &ipa_traceframe
,
1326 sizeof (ipa_traceframe
));
1328 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
, 0);
1329 write_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
, 0);
1330 write_inferior_integer (ipa_sym_addrs
.addr_traceframes_created
, 0);
1336 init_trace_buffer (unsigned char *buf
, int bufsize
)
1338 trace_buffer_lo
= buf
;
1339 trace_buffer_hi
= trace_buffer_lo
+ bufsize
;
1341 clear_trace_buffer ();
1344 #ifdef IN_PROCESS_AGENT
1346 IP_AGENT_EXPORT
void ATTR_USED ATTR_NOINLINE
1347 about_to_request_buffer_space (void)
1349 /* GDBserver places breakpoint here while it goes about to flush
1350 data at random times. */
1351 UNKNOWN_SIDE_EFFECTS();
1356 /* Carve out a piece of the trace buffer, returning NULL in case of
1360 trace_buffer_alloc (size_t amt
)
1362 unsigned char *rslt
;
1363 struct trace_buffer_control
*tbctrl
;
1365 #ifdef IN_PROCESS_AGENT
1366 unsigned int prev
, prev_filtered
;
1367 unsigned int commit_count
;
1368 unsigned int commit
;
1369 unsigned int readout
;
1371 struct traceframe
*oldest
;
1372 unsigned char *new_start
;
1375 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1376 (long) amt
, (long) sizeof (struct traceframe
));
1378 /* Account for the EOB marker. */
1379 amt
+= sizeof (struct traceframe
);
1381 #ifdef IN_PROCESS_AGENT
1385 /* Read the current token and extract the index to try to write to,
1386 storing it in CURR. */
1387 prev
= trace_buffer_ctrl_curr
;
1388 prev_filtered
= prev
& ~GDBSERVER_FLUSH_COUNT_MASK
;
1389 curr
= prev_filtered
+ 1;
1393 about_to_request_buffer_space ();
1395 /* Start out with a copy of the current state. GDBserver may be
1396 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1397 be able to commit anyway if that happens. */
1398 trace_buffer_ctrl
[curr
]
1399 = trace_buffer_ctrl
[prev_filtered
];
1400 trace_debug ("trying curr=%u", curr
);
1402 /* The GDBserver's agent doesn't need all that syncing, and always
1403 updates TCB 0 (there's only one, mind you). */
1406 tbctrl
= &trace_buffer_ctrl
[curr
];
1408 /* Offsets are easier to grok for debugging than raw addresses,
1409 especially for the small trace buffer sizes that are useful for
1411 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1413 (int) (tbctrl
->start
- trace_buffer_lo
),
1414 (int) (tbctrl
->free
- trace_buffer_lo
),
1415 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1416 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1417 (int) (trace_buffer_hi
- trace_buffer_lo
));
1419 /* The algorithm here is to keep trying to get a contiguous block of
1420 the requested size, possibly discarding older traceframes to free
1421 up space. Since free space might come in one or two pieces,
1422 depending on whether discarded traceframes wrapped around at the
1423 high end of the buffer, we test both pieces after each
1427 /* First, if we have two free parts, try the upper one first. */
1428 if (tbctrl
->end_free
< tbctrl
->free
)
1430 if (tbctrl
->free
+ amt
<= trace_buffer_hi
)
1431 /* We have enough in the upper part. */
1435 /* Our high part of free space wasn't enough. Give up
1436 on it for now, set wraparound. We will recover the
1437 space later, if/when the wrapped-around traceframe is
1439 trace_debug ("Upper part too small, setting wraparound");
1440 tbctrl
->wrap
= tbctrl
->free
;
1441 tbctrl
->free
= trace_buffer_lo
;
1445 /* The normal case. */
1446 if (tbctrl
->free
+ amt
<= tbctrl
->end_free
)
1449 #ifdef IN_PROCESS_AGENT
1450 /* The IP Agent's buffer is always circular. It isn't used
1451 currently, but `circular_trace_buffer' could represent
1452 GDBserver's mode. If we didn't find space, ask GDBserver to
1455 flush_trace_buffer ();
1459 trace_debug ("gdbserver flushed buffer, retrying");
1463 /* GDBserver cancelled the tracing. Bail out as well. */
1466 /* If we're here, then neither part is big enough, and
1467 non-circular trace buffers are now full. */
1468 if (!circular_trace_buffer
)
1470 trace_debug ("Not enough space in the trace buffer");
1474 trace_debug ("Need more space in the trace buffer");
1476 /* If we have a circular buffer, we can try discarding the
1477 oldest traceframe and see if that helps. */
1478 oldest
= FIRST_TRACEFRAME ();
1479 if (oldest
->tpnum
== 0)
1481 /* Not good; we have no traceframes to free. Perhaps we're
1482 asking for a block that is larger than the buffer? In
1483 any case, give up. */
1484 trace_debug ("No traceframes to discard");
1488 /* We don't run this code in the in-process agent currently.
1489 E.g., we could leave the in-process agent in autonomous
1490 circular mode if we only have fast tracepoints. If we do
1491 that, then this bit becomes racy with GDBserver, which also
1492 writes to this counter. */
1493 --traceframe_write_count
;
1495 new_start
= (unsigned char *) NEXT_TRACEFRAME (oldest
);
1496 /* If we freed the traceframe that wrapped around, go back
1497 to the non-wrap case. */
1498 if (new_start
< tbctrl
->start
)
1500 trace_debug ("Discarding past the wraparound");
1501 tbctrl
->wrap
= trace_buffer_hi
;
1503 tbctrl
->start
= new_start
;
1504 tbctrl
->end_free
= tbctrl
->start
;
1506 trace_debug ("Discarded a traceframe\n"
1507 "Trace buffer [%d], start=%d free=%d "
1508 "endfree=%d wrap=%d hi=%d",
1510 (int) (tbctrl
->start
- trace_buffer_lo
),
1511 (int) (tbctrl
->free
- trace_buffer_lo
),
1512 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1513 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1514 (int) (trace_buffer_hi
- trace_buffer_lo
));
1516 /* Now go back around the loop. The discard might have resulted
1517 in either one or two pieces of free space, so we want to try
1518 both before freeing any more traceframes. */
1522 /* If we get here, we know we can provide the asked-for space. */
1524 rslt
= tbctrl
->free
;
1526 /* Adjust the request back down, now that we know we have space for
1527 the marker, but don't commit to AMT yet, we may still need to
1528 restart the operation if GDBserver touches the trace buffer
1529 (obviously only important in the in-process agent's version). */
1530 tbctrl
->free
+= (amt
- sizeof (struct traceframe
));
1532 /* Or not. If GDBserver changed the trace buffer behind our back,
1533 we get to restart a new allocation attempt. */
1535 #ifdef IN_PROCESS_AGENT
1536 /* Build the tentative token. */
1537 commit_count
= (((prev
& 0x0007ff00) + 0x100) & 0x0007ff00);
1538 commit
= (((prev
& 0x0007ff00) << 12)
1542 /* Try to commit it. */
1543 readout
= cmpxchg (&trace_buffer_ctrl_curr
, prev
, commit
);
1544 if (readout
!= prev
)
1546 trace_debug ("GDBserver has touched the trace buffer, restarting."
1547 " (prev=%08x, commit=%08x, readout=%08x)",
1548 prev
, commit
, readout
);
1552 /* Hold your horses here. Even if that change was committed,
1553 GDBserver could come in, and clobber it. We need to hold to be
1554 able to tell if GDBserver clobbers before or after we committed
1555 the change. Whenever GDBserver goes about touching the IPA
1556 buffer, it sets a breakpoint in this routine, so we have a sync
1558 about_to_request_buffer_space ();
1560 /* Check if the change has been effective, even if GDBserver stopped
1561 us at the breakpoint. */
1564 unsigned int refetch
;
1568 refetch
= trace_buffer_ctrl_curr
;
1570 if ((refetch
== commit
1571 || ((refetch
& 0x7ff00000) >> 12) == commit_count
))
1574 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1575 "readout=%08x, refetch=%08x)",
1576 prev
, commit
, readout
, refetch
);
1580 trace_debug ("GDBserver has touched the trace buffer, not effective."
1581 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1582 prev
, commit
, readout
, refetch
);
1588 /* We have a new piece of the trace buffer. Hurray! */
1590 /* Add an EOB marker just past this allocation. */
1591 ((struct traceframe
*) tbctrl
->free
)->tpnum
= 0;
1592 ((struct traceframe
*) tbctrl
->free
)->data_size
= 0;
1594 /* Adjust the request back down, now that we know we have space for
1596 amt
-= sizeof (struct traceframe
);
1600 trace_debug ("Allocated %d bytes", (int) amt
);
1601 trace_debug ("Trace buffer [%d] start=%d free=%d "
1602 "endfree=%d wrap=%d hi=%d",
1604 (int) (tbctrl
->start
- trace_buffer_lo
),
1605 (int) (tbctrl
->free
- trace_buffer_lo
),
1606 (int) (tbctrl
->end_free
- trace_buffer_lo
),
1607 (int) (tbctrl
->wrap
- trace_buffer_lo
),
1608 (int) (trace_buffer_hi
- trace_buffer_lo
));
1614 #ifndef IN_PROCESS_AGENT
1616 /* Return the total free space. This is not necessarily the largest
1617 block we can allocate, because of the two-part case. */
1622 if (trace_buffer_free
<= trace_buffer_end_free
)
1623 return trace_buffer_end_free
- trace_buffer_free
;
1625 return ((trace_buffer_end_free
- trace_buffer_lo
)
1626 + (trace_buffer_hi
- trace_buffer_free
));
1629 /* An 'S' in continuation packets indicates remainder are for
1632 static int seen_step_action_flag
;
1634 /* Create a tracepoint (location) with given number and address. */
1636 static struct tracepoint
*
1637 add_tracepoint (int num
, CORE_ADDR addr
)
1639 struct tracepoint
*tpoint
;
1641 tpoint
= xmalloc (sizeof (struct tracepoint
));
1642 tpoint
->number
= num
;
1643 tpoint
->address
= addr
;
1644 tpoint
->numactions
= 0;
1645 tpoint
->actions
= NULL
;
1646 tpoint
->actions_str
= NULL
;
1647 tpoint
->cond
= NULL
;
1648 tpoint
->num_step_actions
= 0;
1649 tpoint
->step_actions
= NULL
;
1650 tpoint
->step_actions_str
= NULL
;
1651 /* Start all off as regular (slow) tracepoints. */
1652 tpoint
->type
= trap_tracepoint
;
1653 tpoint
->orig_size
= -1;
1654 tpoint
->source_strings
= NULL
;
1655 tpoint
->compiled_cond
= 0;
1656 tpoint
->handle
= NULL
;
1657 tpoint
->next
= NULL
;
1659 if (!last_tracepoint
)
1660 tracepoints
= tpoint
;
1662 last_tracepoint
->next
= tpoint
;
1663 last_tracepoint
= tpoint
;
1665 seen_step_action_flag
= 0;
1670 #ifndef IN_PROCESS_AGENT
1672 /* Return the tracepoint with the given number and address, or NULL. */
1674 static struct tracepoint
*
1675 find_tracepoint (int id
, CORE_ADDR addr
)
1677 struct tracepoint
*tpoint
;
1679 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
1680 if (tpoint
->number
== id
&& tpoint
->address
== addr
)
1686 /* There may be several tracepoints with the same number (because they
1687 are "locations", in GDB parlance); return the next one after the
1688 given tracepoint, or search from the beginning of the list if the
1689 first argument is NULL. */
1691 static struct tracepoint
*
1692 find_next_tracepoint_by_number (struct tracepoint
*prev_tp
, int num
)
1694 struct tracepoint
*tpoint
;
1697 tpoint
= prev_tp
->next
;
1699 tpoint
= tracepoints
;
1700 for (; tpoint
; tpoint
= tpoint
->next
)
1701 if (tpoint
->number
== num
)
1710 save_string (const char *str
, size_t len
)
1714 s
= xmalloc (len
+ 1);
1715 memcpy (s
, str
, len
);
1721 /* Append another action to perform when the tracepoint triggers. */
1724 add_tracepoint_action (struct tracepoint
*tpoint
, char *packet
)
1730 seen_step_action_flag
= 1;
1738 char *act_start
= act
;
1739 struct tracepoint_action
*action
= NULL
;
1745 struct collect_memory_action
*maction
;
1749 maction
= xmalloc (sizeof *maction
);
1750 maction
->base
.type
= *act
;
1751 action
= &maction
->base
;
1754 is_neg
= (*act
== '-');
1757 act
= unpack_varlen_hex (act
, &basereg
);
1759 act
= unpack_varlen_hex (act
, &maction
->addr
);
1761 act
= unpack_varlen_hex (act
, &maction
->len
);
1762 maction
->basereg
= (is_neg
1765 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1766 pulongest (maction
->len
),
1767 paddress (maction
->addr
), maction
->basereg
);
1772 struct collect_registers_action
*raction
;
1774 raction
= xmalloc (sizeof *raction
);
1775 raction
->base
.type
= *act
;
1776 action
= &raction
->base
;
1778 trace_debug ("Want to collect registers");
1780 /* skip past hex digits of mask for now */
1781 while (isxdigit(*act
))
1787 struct collect_static_trace_data_action
*raction
;
1789 raction
= xmalloc (sizeof *raction
);
1790 raction
->base
.type
= *act
;
1791 action
= &raction
->base
;
1793 trace_debug ("Want to collect static trace data");
1798 trace_debug ("Unexpected step action, ignoring");
1803 struct eval_expr_action
*xaction
;
1805 xaction
= xmalloc (sizeof (*xaction
));
1806 xaction
->base
.type
= *act
;
1807 action
= &xaction
->base
;
1809 trace_debug ("Want to evaluate expression");
1810 xaction
->expr
= parse_agent_expr (&act
);
1814 trace_debug ("unknown trace action '%c', ignoring...", *act
);
1823 if (seen_step_action_flag
)
1825 tpoint
->num_step_actions
++;
1827 tpoint
->step_actions
1828 = xrealloc (tpoint
->step_actions
,
1829 (sizeof (*tpoint
->step_actions
)
1830 * tpoint
->num_step_actions
));
1831 tpoint
->step_actions_str
1832 = xrealloc (tpoint
->step_actions_str
,
1833 (sizeof (*tpoint
->step_actions_str
)
1834 * tpoint
->num_step_actions
));
1835 tpoint
->step_actions
[tpoint
->num_step_actions
- 1] = action
;
1836 tpoint
->step_actions_str
[tpoint
->num_step_actions
- 1]
1837 = save_string (act_start
, act
- act_start
);
1841 tpoint
->numactions
++;
1843 = xrealloc (tpoint
->actions
,
1844 sizeof (*tpoint
->actions
) * tpoint
->numactions
);
1846 = xrealloc (tpoint
->actions_str
,
1847 sizeof (*tpoint
->actions_str
) * tpoint
->numactions
);
1848 tpoint
->actions
[tpoint
->numactions
- 1] = action
;
1849 tpoint
->actions_str
[tpoint
->numactions
- 1]
1850 = save_string (act_start
, act
- act_start
);
1857 /* Find or create a trace state variable with the given number. */
1859 static struct trace_state_variable
*
1860 get_trace_state_variable (int num
)
1862 struct trace_state_variable
*tsv
;
1864 #ifdef IN_PROCESS_AGENT
1865 /* Search for an existing variable. */
1866 for (tsv
= alloced_trace_state_variables
; tsv
; tsv
= tsv
->next
)
1867 if (tsv
->number
== num
)
1871 /* Search for an existing variable. */
1872 for (tsv
= trace_state_variables
; tsv
; tsv
= tsv
->next
)
1873 if (tsv
->number
== num
)
1879 /* Find or create a trace state variable with the given number. */
1881 static struct trace_state_variable
*
1882 create_trace_state_variable (int num
, int gdb
)
1884 struct trace_state_variable
*tsv
;
1886 tsv
= get_trace_state_variable (num
);
1890 /* Create a new variable. */
1891 tsv
= xmalloc (sizeof (struct trace_state_variable
));
1893 tsv
->initial_value
= 0;
1897 #ifdef IN_PROCESS_AGENT
1900 tsv
->next
= alloced_trace_state_variables
;
1901 alloced_trace_state_variables
= tsv
;
1906 tsv
->next
= trace_state_variables
;
1907 trace_state_variables
= tsv
;
1912 IP_AGENT_EXPORT LONGEST
1913 get_trace_state_variable_value (int num
)
1915 struct trace_state_variable
*tsv
;
1917 tsv
= get_trace_state_variable (num
);
1921 trace_debug ("No trace state variable %d, skipping value get", num
);
1925 /* Call a getter function if we have one. While it's tempting to
1926 set up something to only call the getter once per tracepoint hit,
1927 it could run afoul of thread races. Better to let the getter
1928 handle it directly, if necessary to worry about it. */
1930 tsv
->value
= (tsv
->getter
) ();
1932 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1933 num
, plongest (tsv
->value
));
1938 IP_AGENT_EXPORT
void
1939 set_trace_state_variable_value (int num
, LONGEST val
)
1941 struct trace_state_variable
*tsv
;
1943 tsv
= get_trace_state_variable (num
);
1947 trace_debug ("No trace state variable %d, skipping value set", num
);
1955 set_trace_state_variable_name (int num
, const char *name
)
1957 struct trace_state_variable
*tsv
;
1959 tsv
= get_trace_state_variable (num
);
1963 trace_debug ("No trace state variable %d, skipping name set", num
);
1967 tsv
->name
= (char *) name
;
1971 set_trace_state_variable_getter (int num
, LONGEST (*getter
) (void))
1973 struct trace_state_variable
*tsv
;
1975 tsv
= get_trace_state_variable (num
);
1979 trace_debug ("No trace state variable %d, skipping getter set", num
);
1983 tsv
->getter
= getter
;
1986 /* Add a raw traceframe for the given tracepoint. */
1988 static struct traceframe
*
1989 add_traceframe (struct tracepoint
*tpoint
)
1991 struct traceframe
*tframe
;
1993 tframe
= trace_buffer_alloc (sizeof (struct traceframe
));
1998 tframe
->tpnum
= tpoint
->number
;
1999 tframe
->data_size
= 0;
2004 /* Add a block to the traceframe currently being worked on. */
2006 static unsigned char *
2007 add_traceframe_block (struct traceframe
*tframe
, int amt
)
2009 unsigned char *block
;
2014 block
= trace_buffer_alloc (amt
);
2019 tframe
->data_size
+= amt
;
2024 /* Flag that the current traceframe is finished. */
2027 finish_traceframe (struct traceframe
*tframe
)
2029 ++traceframe_write_count
;
2030 ++traceframes_created
;
2033 #ifndef IN_PROCESS_AGENT
2035 /* Given a traceframe number NUM, find the NUMth traceframe in the
2038 static struct traceframe
*
2039 find_traceframe (int num
)
2041 struct traceframe
*tframe
;
2044 for (tframe
= FIRST_TRACEFRAME ();
2046 tframe
= NEXT_TRACEFRAME (tframe
))
2057 get_traceframe_address (struct traceframe
*tframe
)
2060 struct tracepoint
*tpoint
;
2062 addr
= traceframe_get_pc (tframe
);
2067 /* Fallback strategy, will be incorrect for while-stepping frames
2068 and multi-location tracepoints. */
2069 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
2070 return tpoint
->address
;
2073 /* Search for the next traceframe whose address is inside or outside
2076 static struct traceframe
*
2077 find_next_traceframe_in_range (CORE_ADDR lo
, CORE_ADDR hi
, int inside_p
,
2080 struct traceframe
*tframe
;
2083 *tfnump
= current_traceframe
+ 1;
2084 tframe
= find_traceframe (*tfnump
);
2085 /* The search is not supposed to wrap around. */
2092 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2094 tfaddr
= get_traceframe_address (tframe
);
2096 ? (lo
<= tfaddr
&& tfaddr
<= hi
)
2097 : (lo
> tfaddr
|| tfaddr
> hi
))
2106 /* Search for the next traceframe recorded by the given tracepoint.
2107 Note that for multi-location tracepoints, this will find whatever
2108 location appears first. */
2110 static struct traceframe
*
2111 find_next_traceframe_by_tracepoint (int num
, int *tfnump
)
2113 struct traceframe
*tframe
;
2115 *tfnump
= current_traceframe
+ 1;
2116 tframe
= find_traceframe (*tfnump
);
2117 /* The search is not supposed to wrap around. */
2124 for (; tframe
->tpnum
!= 0; tframe
= NEXT_TRACEFRAME (tframe
))
2126 if (tframe
->tpnum
== num
)
2137 #ifndef IN_PROCESS_AGENT
2139 /* Clear all past trace state. */
2142 cmd_qtinit (char *packet
)
2144 struct trace_state_variable
*tsv
, *prev
, *next
;
2146 /* Make sure we don't try to read from a trace frame. */
2147 current_traceframe
= -1;
2149 trace_debug ("Initializing the trace");
2151 clear_installed_tracepoints ();
2152 clear_readonly_regions ();
2155 last_tracepoint
= NULL
;
2157 /* Clear out any leftover trace state variables. Ones with target
2158 defined getters should be kept however. */
2160 tsv
= trace_state_variables
;
2163 trace_debug ("Looking at var %d", tsv
->number
);
2164 if (tsv
->getter
== NULL
)
2170 trace_state_variables
= next
;
2171 trace_debug ("Deleting var %d", tsv
->number
);
2182 clear_trace_buffer ();
2183 clear_inferior_trace_buffer ();
2188 /* Unprobe the UST marker at ADDRESS. */
2191 unprobe_marker_at (CORE_ADDR address
)
2193 char cmd
[CMD_BUF_SIZE
];
2195 sprintf (cmd
, "unprobe_marker_at:%s", paddress (address
));
2196 run_inferior_command (cmd
);
2199 /* Restore the program to its pre-tracing state. This routine may be called
2200 in error situations, so it needs to be careful about only restoring
2201 from known-valid bits. */
2204 clear_installed_tracepoints (void)
2206 struct tracepoint
*tpoint
;
2207 struct tracepoint
*prev_stpoint
;
2210 cancel_breakpoints ();
2212 prev_stpoint
= NULL
;
2214 /* Restore any bytes overwritten by tracepoints. */
2215 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2217 if (!tpoint
->enabled
)
2220 /* Catch the case where we might try to remove a tracepoint that
2221 was never actually installed. */
2222 if (tpoint
->handle
== NULL
)
2224 trace_debug ("Tracepoint %d at 0x%s was "
2225 "never installed, nothing to clear",
2226 tpoint
->number
, paddress (tpoint
->address
));
2230 switch (tpoint
->type
)
2232 case trap_tracepoint
:
2233 delete_breakpoint (tpoint
->handle
);
2235 case fast_tracepoint
:
2236 delete_fast_tracepoint_jump (tpoint
->handle
);
2238 case static_tracepoint
:
2239 if (prev_stpoint
!= NULL
2240 && prev_stpoint
->address
== tpoint
->address
)
2241 /* Nothing to do. We already unprobed a tracepoint set at
2242 this marker address (and there can only be one probe
2247 unprobe_marker_at (tpoint
->address
);
2248 prev_stpoint
= tpoint
;
2253 tpoint
->handle
= NULL
;
2259 /* Parse a packet that defines a tracepoint. */
2262 cmd_qtdp (char *own_buf
)
2268 struct tracepoint
*tpoint
;
2270 char *packet
= own_buf
;
2272 packet
+= strlen ("QTDP:");
2274 /* A hyphen at the beginning marks a packet specifying actions for a
2275 tracepoint already supplied. */
2282 packet
= unpack_varlen_hex (packet
, &num
);
2283 ++packet
; /* skip a colon */
2284 packet
= unpack_varlen_hex (packet
, &addr
);
2285 ++packet
; /* skip a colon */
2287 /* See if we already have this tracepoint. */
2288 tpoint
= find_tracepoint (num
, addr
);
2292 /* Duplicate tracepoints are never allowed. */
2295 trace_debug ("Tracepoint error: tracepoint %d"
2296 " at 0x%s already exists",
2297 (int) num
, paddress (addr
));
2298 write_enn (own_buf
);
2302 tpoint
= add_tracepoint (num
, addr
);
2304 tpoint
->enabled
= (*packet
== 'E');
2305 ++packet
; /* skip 'E' */
2306 ++packet
; /* skip a colon */
2307 packet
= unpack_varlen_hex (packet
, &count
);
2308 tpoint
->step_count
= count
;
2309 ++packet
; /* skip a colon */
2310 packet
= unpack_varlen_hex (packet
, &count
);
2311 tpoint
->pass_count
= count
;
2312 /* See if we have any of the additional optional fields. */
2313 while (*packet
== ':')
2318 tpoint
->type
= fast_tracepoint
;
2320 packet
= unpack_varlen_hex (packet
, &count
);
2321 tpoint
->orig_size
= count
;
2323 else if (*packet
== 'S')
2325 tpoint
->type
= static_tracepoint
;
2328 else if (*packet
== 'X')
2330 actparm
= (char *) packet
;
2331 tpoint
->cond
= parse_agent_expr (&actparm
);
2334 else if (*packet
== '-')
2336 else if (*packet
== '\0')
2339 trace_debug ("Unknown optional tracepoint field");
2342 trace_debug ("Also has actions\n");
2344 trace_debug ("Defined %stracepoint %d at 0x%s, "
2345 "enabled %d step %ld pass %ld",
2346 tpoint
->type
== fast_tracepoint
? "fast "
2348 tpoint
->number
, paddress (tpoint
->address
), tpoint
->enabled
,
2349 tpoint
->step_count
, tpoint
->pass_count
);
2352 add_tracepoint_action (tpoint
, packet
);
2355 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2356 (int) num
, paddress (addr
));
2357 write_enn (own_buf
);
2365 cmd_qtdpsrc (char *own_buf
)
2367 ULONGEST num
, addr
, start
, slen
;
2368 struct tracepoint
*tpoint
;
2369 char *packet
= own_buf
;
2370 char *saved
, *srctype
, *src
;
2372 struct source_string
*last
, *newlast
;
2374 packet
+= strlen ("QTDPsrc:");
2376 packet
= unpack_varlen_hex (packet
, &num
);
2377 ++packet
; /* skip a colon */
2378 packet
= unpack_varlen_hex (packet
, &addr
);
2379 ++packet
; /* skip a colon */
2381 /* See if we already have this tracepoint. */
2382 tpoint
= find_tracepoint (num
, addr
);
2386 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2387 (int) num
, paddress (addr
));
2388 write_enn (own_buf
);
2393 packet
= strchr (packet
, ':');
2394 srctype
= xmalloc (packet
- saved
+ 1);
2395 memcpy (srctype
, saved
, packet
- saved
);
2396 srctype
[packet
- saved
] = '\0';
2398 packet
= unpack_varlen_hex (packet
, &start
);
2399 ++packet
; /* skip a colon */
2400 packet
= unpack_varlen_hex (packet
, &slen
);
2401 ++packet
; /* skip a colon */
2402 src
= xmalloc (slen
+ 1);
2403 nbytes
= unhexify (src
, packet
, strlen (packet
) / 2);
2406 newlast
= xmalloc (sizeof (struct source_string
));
2407 newlast
->type
= srctype
;
2409 newlast
->next
= NULL
;
2410 /* Always add a source string to the end of the list;
2411 this keeps sequences of actions/commands in the right
2413 if (tpoint
->source_strings
)
2415 for (last
= tpoint
->source_strings
; last
->next
; last
= last
->next
)
2417 last
->next
= newlast
;
2420 tpoint
->source_strings
= newlast
;
2426 cmd_qtdv (char *own_buf
)
2428 ULONGEST num
, val
, builtin
;
2431 struct trace_state_variable
*tsv
;
2432 char *packet
= own_buf
;
2434 packet
+= strlen ("QTDV:");
2436 packet
= unpack_varlen_hex (packet
, &num
);
2437 ++packet
; /* skip a colon */
2438 packet
= unpack_varlen_hex (packet
, &val
);
2439 ++packet
; /* skip a colon */
2440 packet
= unpack_varlen_hex (packet
, &builtin
);
2441 ++packet
; /* skip a colon */
2443 nbytes
= strlen (packet
) / 2;
2444 varname
= xmalloc (nbytes
+ 1);
2445 nbytes
= unhexify (varname
, packet
, nbytes
);
2446 varname
[nbytes
] = '\0';
2448 tsv
= create_trace_state_variable (num
, 1);
2449 tsv
->initial_value
= (LONGEST
) val
;
2450 tsv
->name
= varname
;
2452 set_trace_state_variable_value (num
, (LONGEST
) val
);
2458 cmd_qtv (char *own_buf
)
2463 char *packet
= own_buf
;
2465 packet
+= strlen ("qTV:");
2466 unpack_varlen_hex (packet
, &num
);
2468 if (current_traceframe
>= 0)
2470 err
= traceframe_read_tsv ((int) num
, &val
);
2473 strcpy (own_buf
, "U");
2477 /* Only make tsv's be undefined before the first trace run. After a
2478 trace run is over, the user might want to see the last value of
2479 the tsv, and it might not be available in a traceframe. */
2480 else if (!tracing
&& strcmp (tracing_stop_reason
, "tnotrun") == 0)
2482 strcpy (own_buf
, "U");
2486 val
= get_trace_state_variable_value (num
);
2488 sprintf (own_buf
, "V%s", phex_nz (val
, 0));
2491 /* Clear out the list of readonly regions. */
2494 clear_readonly_regions (void)
2496 struct readonly_region
*roreg
;
2498 while (readonly_regions
)
2500 roreg
= readonly_regions
;
2501 readonly_regions
= readonly_regions
->next
;
2506 /* Parse the collection of address ranges whose contents GDB believes
2507 to be unchanging and so can be read directly from target memory
2508 even while looking at a traceframe. */
2511 cmd_qtro (char *own_buf
)
2513 ULONGEST start
, end
;
2514 struct readonly_region
*roreg
;
2515 char *packet
= own_buf
;
2517 trace_debug ("Want to mark readonly regions");
2519 clear_readonly_regions ();
2521 packet
+= strlen ("QTro");
2523 while (*packet
== ':')
2525 ++packet
; /* skip a colon */
2526 packet
= unpack_varlen_hex (packet
, &start
);
2527 ++packet
; /* skip a comma */
2528 packet
= unpack_varlen_hex (packet
, &end
);
2529 roreg
= xmalloc (sizeof (struct readonly_region
));
2530 roreg
->start
= start
;
2532 roreg
->next
= readonly_regions
;
2533 readonly_regions
= roreg
;
2534 trace_debug ("Added readonly region from 0x%s to 0x%s",
2535 paddress (roreg
->start
), paddress (roreg
->end
));
2541 /* Test to see if the given range is in our list of readonly ranges.
2542 We only test for being entirely within a range, GDB is not going to
2543 send a single memory packet that spans multiple regions. */
2546 in_readonly_region (CORE_ADDR addr
, ULONGEST length
)
2548 struct readonly_region
*roreg
;
2550 for (roreg
= readonly_regions
; roreg
; roreg
= roreg
->next
)
2551 if (roreg
->start
<= addr
&& (addr
+ length
- 1) <= roreg
->end
)
2557 /* The maximum size of a jump pad entry. */
2558 static const int max_jump_pad_size
= 0x100;
2560 static CORE_ADDR gdb_jump_pad_head
;
2562 /* Return the address of the next free jump space. */
2565 get_jump_space_head (void)
2567 if (gdb_jump_pad_head
== 0)
2569 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
2570 &gdb_jump_pad_head
))
2571 fatal ("error extracting jump_pad_buffer");
2574 return gdb_jump_pad_head
;
2577 /* Reserve USED bytes from the jump space. */
2580 claim_jump_space (ULONGEST used
)
2582 trace_debug ("claim_jump_space reserves %s bytes at %s",
2583 pulongest (used
), paddress (gdb_jump_pad_head
));
2584 gdb_jump_pad_head
+= used
;
2587 /* Sort tracepoints by PC, using a bubble sort. */
2590 sort_tracepoints (void)
2592 struct tracepoint
*lst
, *tmp
, *prev
= NULL
;
2595 if (tracepoints
== NULL
)
2599 for (tmp
= tracepoints
; tmp
->next
; tmp
= tmp
->next
)
2602 for (i
= 0; i
< n
- 1; i
++)
2603 for (j
= 0, lst
= tracepoints
;
2604 lst
&& lst
->next
&& (j
<= n
- 1 - i
);
2607 /* If we're at beginning, the start node is the prev
2612 /* Compare neighbors. */
2613 if (lst
->next
->address
< lst
->address
)
2615 struct tracepoint
*p
;
2618 tmp
= (lst
->next
? lst
->next
->next
: NULL
);
2620 if (j
== 0 && prev
== tracepoints
)
2621 tracepoints
= lst
->next
;
2624 prev
->next
= lst
->next
;
2625 lst
->next
->next
= lst
;
2632 /* Keep track of the previous node. We need it if we need
2640 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2641 the command fails, or 0 otherwise. If the command ran
2642 successfully, but probing the marker failed, ERROUT will be filled
2643 with the error to reply to GDB, and -1 is also returned. This
2644 allows directly passing IPA errors to GDB. */
2647 probe_marker_at (CORE_ADDR address
, char *errout
)
2649 char cmd
[CMD_BUF_SIZE
];
2652 sprintf (cmd
, "probe_marker_at:%s", paddress (address
));
2653 err
= run_inferior_command (cmd
);
2659 strcpy (errout
, cmd
);
2667 #define MAX_JUMP_SIZE 20
2670 cmd_qtstart (char *packet
)
2672 struct tracepoint
*tpoint
, *prev_ftpoint
, *prev_stpoint
;
2673 int slow_tracepoint_count
, fast_count
;
2674 CORE_ADDR jump_entry
;
2676 /* The jump to the jump pad of the last fast tracepoint
2678 unsigned char fjump
[MAX_JUMP_SIZE
];
2679 ULONGEST fjump_size
;
2681 trace_debug ("Starting the trace");
2683 slow_tracepoint_count
= fast_count
= 0;
2685 /* Sort tracepoints by ascending address. This makes installing
2686 fast tracepoints at the same address easier to handle. */
2687 sort_tracepoints ();
2689 /* Pause all threads temporarily while we patch tracepoints. */
2692 /* Get threads out of jump pads. Safe to do here, since this is a
2693 top level command. And, required to do here, since we're
2694 deleting/rewriting jump pads. */
2696 stabilize_threads ();
2698 /* Freeze threads. */
2701 /* Sync the fast tracepoints list in the inferior ftlib. */
2702 if (in_process_agent_loaded ())
2704 download_tracepoints ();
2705 download_trace_state_variables ();
2708 /* No previous fast tpoint yet. */
2709 prev_ftpoint
= NULL
;
2711 /* No previous static tpoint yet. */
2712 prev_stpoint
= NULL
;
2716 /* Install tracepoints. */
2717 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
2719 /* Ensure all the hit counts start at zero. */
2720 tpoint
->hit_count
= 0;
2722 if (!tpoint
->enabled
)
2725 if (tpoint
->type
== trap_tracepoint
)
2727 ++slow_tracepoint_count
;
2729 /* Tracepoints are installed as memory breakpoints. Just go
2730 ahead and install the trap. The breakpoints module
2731 handles duplicated breakpoints, and the memory read
2732 routine handles un-patching traps from memory reads. */
2733 tpoint
->handle
= set_breakpoint_at (tpoint
->address
,
2734 tracepoint_handler
);
2736 else if (tpoint
->type
== fast_tracepoint
)
2740 if (maybe_write_ipa_not_loaded (packet
))
2742 trace_debug ("Requested a fast tracepoint, but fast "
2743 "tracepoints aren't supported.");
2747 if (prev_ftpoint
!= NULL
&& prev_ftpoint
->address
== tpoint
->address
)
2749 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2752 tpoint
->jump_pad
= prev_ftpoint
->jump_pad
;
2753 tpoint
->jump_pad_end
= prev_ftpoint
->jump_pad_end
;
2754 tpoint
->adjusted_insn_addr
= prev_ftpoint
->adjusted_insn_addr
;
2755 tpoint
->adjusted_insn_addr_end
2756 = prev_ftpoint
->adjusted_insn_addr_end
;
2763 prev_ftpoint
= NULL
;
2765 jentry
= jump_entry
= get_jump_space_head ();
2767 /* Install the jump pad. */
2768 err
= install_fast_tracepoint_jump_pad
2769 (tpoint
->obj_addr_on_target
,
2771 ipa_sym_addrs
.addr_gdb_collect
,
2772 ipa_sym_addrs
.addr_collecting
,
2776 &tpoint
->adjusted_insn_addr
,
2777 &tpoint
->adjusted_insn_addr_end
);
2781 tpoint
->handle
= set_fast_tracepoint_jump (tpoint
->address
,
2784 if (tpoint
->handle
!= NULL
)
2786 tpoint
->jump_pad
= jump_entry
;
2787 tpoint
->jump_pad_end
= jentry
;
2789 /* Pad to 8-byte alignment. */
2790 jentry
= ((jentry
+ 7) & ~0x7);
2791 claim_jump_space (jentry
- jump_entry
);
2793 /* So that we can handle multiple fast tracepoints
2794 at the same address easily. */
2795 prev_ftpoint
= tpoint
;
2799 else if (tpoint
->type
== static_tracepoint
)
2801 if (maybe_write_ipa_ust_not_loaded (packet
))
2803 trace_debug ("Requested a static tracepoint, but static "
2804 "tracepoints are not supported.");
2808 /* Can only probe a given marker once. */
2809 if (prev_stpoint
!= NULL
&& prev_stpoint
->address
== tpoint
->address
)
2811 tpoint
->handle
= (void *) -1;
2815 if (probe_marker_at (tpoint
->address
, packet
) == 0)
2817 tpoint
->handle
= (void *) -1;
2819 /* So that we can handle multiple static tracepoints
2820 at the same address easily. */
2821 prev_stpoint
= tpoint
;
2826 /* Any failure in the inner loop is sufficient cause to give
2828 if (tpoint
->handle
== NULL
)
2832 /* Any error in tracepoint insertion is unacceptable; better to
2833 address the problem now, than end up with a useless or misleading
2837 clear_installed_tracepoints ();
2838 if (*packet
== '\0')
2844 stopping_tracepoint
= NULL
;
2845 trace_buffer_is_full
= 0;
2846 expr_eval_result
= expr_eval_no_error
;
2847 error_tracepoint
= NULL
;
2849 /* Tracing is now active, hits will now start being logged. */
2852 if (in_process_agent_loaded ())
2854 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 1))
2855 fatal ("Error setting tracing variable in lib");
2857 if (write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
2859 fatal ("Error clearing stopping_tracepoint variable in lib");
2861 if (write_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
, 0))
2862 fatal ("Error clearing trace_buffer_is_full variable in lib");
2864 stop_tracing_bkpt
= set_breakpoint_at (ipa_sym_addrs
.addr_stop_tracing
,
2865 stop_tracing_handler
);
2866 if (stop_tracing_bkpt
== NULL
)
2867 error ("Error setting stop_tracing breakpoint");
2869 flush_trace_buffer_bkpt
2870 = set_breakpoint_at (ipa_sym_addrs
.addr_flush_trace_buffer
,
2871 flush_trace_buffer_handler
);
2872 if (flush_trace_buffer_bkpt
== NULL
)
2873 error ("Error setting flush_trace_buffer breakpoint");
2881 /* End a tracing run, filling in a stop reason to report back to GDB,
2882 and removing the tracepoints from the code. */
2889 trace_debug ("Tracing is already off, ignoring");
2893 trace_debug ("Stopping the trace");
2895 /* Pause all threads before removing fast jumps from memory,
2896 breakpoints, and touching IPA state variables (inferior memory).
2897 Some thread may hit the internal tracing breakpoints, or be
2898 collecting this moment, but that's ok, we don't release the
2899 tpoint object's memory or the jump pads here (we only do that
2900 when we're sure we can move all threads out of the jump pads).
2901 We can't now, since we may be getting here due to the inferior
2902 agent calling us. */
2904 /* Since we're removing breakpoints, cancel breakpoint hits,
2905 possibly related to the breakpoints we're about to delete. */
2906 cancel_breakpoints ();
2908 /* Stop logging. Tracepoints can still be hit, but they will not be
2911 if (in_process_agent_loaded ())
2913 if (write_inferior_integer (ipa_sym_addrs
.addr_tracing
, 0))
2914 fatal ("Error clearing tracing variable in lib");
2917 tracing_stop_reason
= "t???";
2918 tracing_stop_tpnum
= 0;
2919 if (stopping_tracepoint
)
2921 trace_debug ("Stopping the trace because "
2922 "tracepoint %d was hit %ld times",
2923 stopping_tracepoint
->number
,
2924 stopping_tracepoint
->pass_count
);
2925 tracing_stop_reason
= "tpasscount";
2926 tracing_stop_tpnum
= stopping_tracepoint
->number
;
2928 else if (trace_buffer_is_full
)
2930 trace_debug ("Stopping the trace because the trace buffer is full");
2931 tracing_stop_reason
= "tfull";
2933 else if (expr_eval_result
!= expr_eval_no_error
)
2935 trace_debug ("Stopping the trace because of an expression eval error");
2936 tracing_stop_reason
= eval_result_names
[expr_eval_result
];
2937 tracing_stop_tpnum
= error_tracepoint
->number
;
2939 #ifndef IN_PROCESS_AGENT
2940 else if (!gdb_connected ())
2942 trace_debug ("Stopping the trace because GDB disconnected");
2943 tracing_stop_reason
= "tdisconnected";
2948 trace_debug ("Stopping the trace because of a tstop command");
2949 tracing_stop_reason
= "tstop";
2952 stopping_tracepoint
= NULL
;
2953 error_tracepoint
= NULL
;
2955 /* Clear out the tracepoints. */
2956 clear_installed_tracepoints ();
2958 if (in_process_agent_loaded ())
2960 /* Pull in fast tracepoint trace frames from the inferior lib
2961 buffer into our buffer, even if our buffer is already full,
2962 because we want to present the full number of created frames
2963 in addition to what fit in the trace buffer. */
2964 upload_fast_traceframes ();
2967 if (stop_tracing_bkpt
!= NULL
)
2969 delete_breakpoint (stop_tracing_bkpt
);
2970 stop_tracing_bkpt
= NULL
;
2973 if (flush_trace_buffer_bkpt
!= NULL
)
2975 delete_breakpoint (flush_trace_buffer_bkpt
);
2976 flush_trace_buffer_bkpt
= NULL
;
2983 stop_tracing_handler (CORE_ADDR addr
)
2985 trace_debug ("lib hit stop_tracing");
2987 /* Don't actually handle it here. When we stop tracing we remove
2988 breakpoints from the inferior, and that is not allowed in a
2989 breakpoint handler (as the caller is walking the breakpoint
2995 flush_trace_buffer_handler (CORE_ADDR addr
)
2997 trace_debug ("lib hit flush_trace_buffer");
3002 cmd_qtstop (char *packet
)
3009 cmd_qtdisconnected (char *own_buf
)
3012 char *packet
= own_buf
;
3014 packet
+= strlen ("QTDisconnected:");
3016 unpack_varlen_hex (packet
, &setting
);
3020 disconnected_tracing
= setting
;
3024 cmd_qtframe (char *own_buf
)
3026 ULONGEST frame
, pc
, lo
, hi
, num
;
3028 struct traceframe
*tframe
;
3029 char *packet
= own_buf
;
3031 packet
+= strlen ("QTFrame:");
3033 if (strncmp (packet
, "pc:", strlen ("pc:")) == 0)
3035 packet
+= strlen ("pc:");
3036 unpack_varlen_hex (packet
, &pc
);
3037 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc
));
3038 tframe
= find_next_traceframe_in_range (pc
, pc
, 1, &tfnum
);
3040 else if (strncmp (packet
, "range:", strlen ("range:")) == 0)
3042 packet
+= strlen ("range:");
3043 packet
= unpack_varlen_hex (packet
, &lo
);
3045 unpack_varlen_hex (packet
, &hi
);
3046 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3047 paddress (lo
), paddress (hi
));
3048 tframe
= find_next_traceframe_in_range (lo
, hi
, 1, &tfnum
);
3050 else if (strncmp (packet
, "outside:", strlen ("outside:")) == 0)
3052 packet
+= strlen ("outside:");
3053 packet
= unpack_varlen_hex (packet
, &lo
);
3055 unpack_varlen_hex (packet
, &hi
);
3056 trace_debug ("Want to find next traceframe "
3057 "outside the range 0x%s to 0x%s",
3058 paddress (lo
), paddress (hi
));
3059 tframe
= find_next_traceframe_in_range (lo
, hi
, 0, &tfnum
);
3061 else if (strncmp (packet
, "tdp:", strlen ("tdp:")) == 0)
3063 packet
+= strlen ("tdp:");
3064 unpack_varlen_hex (packet
, &num
);
3066 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum
);
3067 tframe
= find_next_traceframe_by_tracepoint (tpnum
, &tfnum
);
3071 unpack_varlen_hex (packet
, &frame
);
3072 tfnum
= (int) frame
;
3075 trace_debug ("Want to stop looking at traceframes");
3076 current_traceframe
= -1;
3080 trace_debug ("Want to look at traceframe %d", tfnum
);
3081 tframe
= find_traceframe (tfnum
);
3086 current_traceframe
= tfnum
;
3087 sprintf (own_buf
, "F%xT%x", tfnum
, tframe
->tpnum
);
3090 sprintf (own_buf
, "F-1");
3094 cmd_qtstatus (char *packet
)
3096 char *stop_reason_rsp
= NULL
;
3098 trace_debug ("Returning trace status as %d, stop reason %s",
3099 tracing
, tracing_stop_reason
);
3101 if (in_process_agent_loaded ())
3105 upload_fast_traceframes ();
3110 stop_reason_rsp
= (char *) tracing_stop_reason
;
3112 /* The user visible error string in terror needs to be hex encoded.
3113 We leave it as plain string in `tracepoint_stop_reason' to ease
3115 if (strncmp (stop_reason_rsp
, "terror:", strlen ("terror:")) == 0)
3117 const char *result_name
;
3121 result_name
= stop_reason_rsp
+ strlen ("terror:");
3122 hexstr_len
= strlen (result_name
) * 2;
3123 p
= stop_reason_rsp
= alloca (strlen ("terror:") + hexstr_len
+ 1);
3124 strcpy (p
, "terror:");
3126 convert_int_to_ascii ((gdb_byte
*) result_name
, p
, strlen (result_name
));
3132 "tframes:%x;tcreated:%x;"
3133 "tfree:%x;tsize:%s;"
3137 stop_reason_rsp
, tracing_stop_tpnum
,
3138 traceframe_count
, traceframes_created
,
3139 free_space (), phex_nz (trace_buffer_hi
- trace_buffer_lo
, 0),
3140 circular_trace_buffer
,
3141 disconnected_tracing
);
3144 /* State variables to help return all the tracepoint bits. */
3145 static struct tracepoint
*cur_tpoint
;
3146 static int cur_action
;
3147 static int cur_step_action
;
3148 static struct source_string
*cur_source_string
;
3149 static struct trace_state_variable
*cur_tsv
;
3151 /* Compose a response that is an imitation of the syntax by which the
3152 tracepoint was originally downloaded. */
3155 response_tracepoint (char *packet
, struct tracepoint
*tpoint
)
3159 sprintf (packet
, "T%x:%s:%c:%lx:%lx", tpoint
->number
,
3160 paddress (tpoint
->address
),
3161 (tpoint
->enabled
? 'E' : 'D'), tpoint
->step_count
,
3162 tpoint
->pass_count
);
3163 if (tpoint
->type
== fast_tracepoint
)
3164 sprintf (packet
+ strlen (packet
), ":F%x", tpoint
->orig_size
);
3165 else if (tpoint
->type
== static_tracepoint
)
3166 sprintf (packet
+ strlen (packet
), ":S");
3170 buf
= unparse_agent_expr (tpoint
->cond
);
3171 sprintf (packet
+ strlen (packet
), ":X%x,%s",
3172 tpoint
->cond
->length
, buf
);
3177 /* Compose a response that is an imitation of the syntax by which the
3178 tracepoint action was originally downloaded (with the difference
3179 that due to the way we store the actions, this will output a packet
3180 per action, while GDB could have combined more than one action
3184 response_action (char *packet
, struct tracepoint
*tpoint
,
3185 char *taction
, int step
)
3187 sprintf (packet
, "%c%x:%s:%s",
3188 (step
? 'S' : 'A'), tpoint
->number
, paddress (tpoint
->address
),
3192 /* Compose a response that is an imitation of the syntax by which the
3193 tracepoint source piece was originally downloaded. */
3196 response_source (char *packet
,
3197 struct tracepoint
*tpoint
, struct source_string
*src
)
3202 len
= strlen (src
->str
);
3203 buf
= alloca (len
* 2 + 1);
3204 convert_int_to_ascii ((gdb_byte
*) src
->str
, buf
, len
);
3206 sprintf (packet
, "Z%x:%s:%s:%x:%x:%s",
3207 tpoint
->number
, paddress (tpoint
->address
),
3208 src
->type
, 0, len
, buf
);
3211 /* Return the first piece of tracepoint definition, and initialize the
3212 state machine that will iterate through all the tracepoint
3216 cmd_qtfp (char *packet
)
3218 trace_debug ("Returning first tracepoint definition piece");
3220 cur_tpoint
= tracepoints
;
3221 cur_action
= cur_step_action
= -1;
3222 cur_source_string
= NULL
;
3225 response_tracepoint (packet
, cur_tpoint
);
3227 strcpy (packet
, "l");
3230 /* Return additional pieces of tracepoint definition. Each action and
3231 stepping action must go into its own packet, because of packet size
3232 limits, and so we use state variables to deliver one piece at a
3236 cmd_qtsp (char *packet
)
3238 trace_debug ("Returning subsequent tracepoint definition piece");
3242 /* This case would normally never occur, but be prepared for
3244 strcpy (packet
, "l");
3246 else if (cur_action
< cur_tpoint
->numactions
- 1)
3249 response_action (packet
, cur_tpoint
,
3250 cur_tpoint
->actions_str
[cur_action
], 0);
3252 else if (cur_step_action
< cur_tpoint
->num_step_actions
- 1)
3255 response_action (packet
, cur_tpoint
,
3256 cur_tpoint
->step_actions_str
[cur_step_action
], 1);
3258 else if ((cur_source_string
3259 ? cur_source_string
->next
3260 : cur_tpoint
->source_strings
))
3262 if (cur_source_string
)
3263 cur_source_string
= cur_source_string
->next
;
3265 cur_source_string
= cur_tpoint
->source_strings
;
3266 response_source (packet
, cur_tpoint
, cur_source_string
);
3270 cur_tpoint
= cur_tpoint
->next
;
3271 cur_action
= cur_step_action
= -1;
3272 cur_source_string
= NULL
;
3274 response_tracepoint (packet
, cur_tpoint
);
3276 strcpy (packet
, "l");
3280 /* Compose a response that is an imitation of the syntax by which the
3281 trace state variable was originally downloaded. */
3284 response_tsv (char *packet
, struct trace_state_variable
*tsv
)
3286 char *buf
= (char *) "";
3291 namelen
= strlen (tsv
->name
);
3292 buf
= alloca (namelen
* 2 + 1);
3293 convert_int_to_ascii ((gdb_byte
*) tsv
->name
, buf
, namelen
);
3296 sprintf (packet
, "%x:%s:%x:%s", tsv
->number
, phex_nz (tsv
->initial_value
, 0),
3297 tsv
->getter
? 1 : 0, buf
);
3300 /* Return the first trace state variable definition, and initialize
3301 the state machine that will iterate through all the tsv bits. */
3304 cmd_qtfv (char *packet
)
3306 trace_debug ("Returning first trace state variable definition");
3308 cur_tsv
= trace_state_variables
;
3311 response_tsv (packet
, cur_tsv
);
3313 strcpy (packet
, "l");
3316 /* Return additional trace state variable definitions. */
3319 cmd_qtsv (char *packet
)
3321 trace_debug ("Returning first trace state variable definition");
3325 /* This case would normally never occur, but be prepared for
3327 strcpy (packet
, "l");
3331 cur_tsv
= cur_tsv
->next
;
3333 response_tsv (packet
, cur_tsv
);
3335 strcpy (packet
, "l");
3338 strcpy (packet
, "l");
3341 /* Return the first static tracepoint marker, and initialize the state
3342 machine that will iterate through all the static tracepoints
3346 cmd_qtfstm (char *packet
)
3348 if (!maybe_write_ipa_ust_not_loaded (packet
))
3349 run_inferior_command (packet
);
3352 /* Return additional static tracepoints markers. */
3355 cmd_qtsstm (char *packet
)
3357 if (!maybe_write_ipa_ust_not_loaded (packet
))
3358 run_inferior_command (packet
);
3361 /* Return the definition of the static tracepoint at a given address.
3362 Result packet is the same as qTsST's. */
3365 cmd_qtstmat (char *packet
)
3367 if (!maybe_write_ipa_ust_not_loaded (packet
))
3368 run_inferior_command (packet
);
3371 /* Respond to qTBuffer packet with a block of raw data from the trace
3372 buffer. GDB may ask for a lot, but we are allowed to reply with
3373 only as much as will fit within packet limits or whatever. */
3376 cmd_qtbuffer (char *own_buf
)
3378 ULONGEST offset
, num
, tot
;
3380 char *packet
= own_buf
;
3382 packet
+= strlen ("qTBuffer:");
3384 packet
= unpack_varlen_hex (packet
, &offset
);
3385 ++packet
; /* skip a comma */
3386 unpack_varlen_hex (packet
, &num
);
3388 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3389 (int) num
, pulongest (offset
));
3391 tot
= (trace_buffer_hi
- trace_buffer_lo
) - free_space ();
3393 /* If we're right at the end, reply specially that we're done. */
3396 strcpy (own_buf
, "l");
3400 /* Object to any other out-of-bounds request. */
3403 write_enn (own_buf
);
3407 /* Compute the pointer corresponding to the given offset, accounting
3409 tbp
= trace_buffer_start
+ offset
;
3410 if (tbp
>= trace_buffer_wrap
)
3411 tbp
-= (trace_buffer_wrap
- trace_buffer_lo
);
3413 /* Trim to the remaining bytes if we're close to the end. */
3414 if (num
> tot
- offset
)
3417 /* Trim to available packet size. */
3418 if (num
>= (PBUFSIZ
- 16) / 2 )
3419 num
= (PBUFSIZ
- 16) / 2;
3421 convert_int_to_ascii (tbp
, own_buf
, num
);
3422 own_buf
[num
] = '\0';
3426 cmd_bigqtbuffer (char *own_buf
)
3429 char *packet
= own_buf
;
3431 packet
+= strlen ("QTBuffer:");
3433 if (strncmp ("circular:", packet
, strlen ("circular:")) == 0)
3435 packet
+= strlen ("circular:");
3436 unpack_varlen_hex (packet
, &val
);
3437 circular_trace_buffer
= val
;
3438 trace_debug ("Trace buffer is now %s",
3439 circular_trace_buffer
? "circular" : "linear");
3443 write_enn (own_buf
);
3447 handle_tracepoint_general_set (char *packet
)
3449 if (strcmp ("QTinit", packet
) == 0)
3451 cmd_qtinit (packet
);
3454 else if (strncmp ("QTDP:", packet
, strlen ("QTDP:")) == 0)
3459 else if (strncmp ("QTDPsrc:", packet
, strlen ("QTDPsrc:")) == 0)
3461 cmd_qtdpsrc (packet
);
3464 else if (strncmp ("QTDV:", packet
, strlen ("QTDV:")) == 0)
3469 else if (strncmp ("QTro:", packet
, strlen ("QTro:")) == 0)
3474 else if (strcmp ("QTStart", packet
) == 0)
3476 cmd_qtstart (packet
);
3479 else if (strcmp ("QTStop", packet
) == 0)
3481 cmd_qtstop (packet
);
3484 else if (strncmp ("QTDisconnected:", packet
,
3485 strlen ("QTDisconnected:")) == 0)
3487 cmd_qtdisconnected (packet
);
3490 else if (strncmp ("QTFrame:", packet
, strlen ("QTFrame:")) == 0)
3492 cmd_qtframe (packet
);
3495 else if (strncmp ("QTBuffer:", packet
, strlen ("QTBuffer:")) == 0)
3497 cmd_bigqtbuffer (packet
);
3505 handle_tracepoint_query (char *packet
)
3507 if (strcmp ("qTStatus", packet
) == 0)
3509 cmd_qtstatus (packet
);
3512 else if (strcmp ("qTfP", packet
) == 0)
3517 else if (strcmp ("qTsP", packet
) == 0)
3522 else if (strcmp ("qTfV", packet
) == 0)
3527 else if (strcmp ("qTsV", packet
) == 0)
3532 else if (strncmp ("qTV:", packet
, strlen ("qTV:")) == 0)
3537 else if (strncmp ("qTBuffer:", packet
, strlen ("qTBuffer:")) == 0)
3539 cmd_qtbuffer (packet
);
3542 else if (strcmp ("qTfSTM", packet
) == 0)
3544 cmd_qtfstm (packet
);
3547 else if (strcmp ("qTsSTM", packet
) == 0)
3549 cmd_qtsstm (packet
);
3552 else if (strncmp ("qTSTMat:", packet
, strlen ("qTSTMat:")) == 0)
3554 cmd_qtstmat (packet
);
3562 #ifndef IN_PROCESS_AGENT
3564 /* Call this when thread TINFO has hit the tracepoint defined by
3565 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3566 action. This adds a while-stepping collecting state item to the
3567 threads' collecting state list, so that we can keep track of
3568 multiple simultaneous while-stepping actions being collected by the
3569 same thread. This can happen in cases like:
3571 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3573 ff0003 INSN3 <-- TP2, collect $regs
3574 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3577 Notice that when instruction INSN5 is reached, the while-stepping
3578 actions of both TP1 and TP3 are still being collected, and that TP2
3579 had been collected meanwhile. The whole range of ff0001-ff0005
3580 should be single-stepped, due to at least TP1's while-stepping
3581 action covering the whole range. */
3584 add_while_stepping_state (struct thread_info
*tinfo
,
3585 int tp_number
, CORE_ADDR tp_address
)
3587 struct wstep_state
*wstep
;
3589 wstep
= xmalloc (sizeof (*wstep
));
3590 wstep
->next
= tinfo
->while_stepping
;
3592 wstep
->tp_number
= tp_number
;
3593 wstep
->tp_address
= tp_address
;
3594 wstep
->current_step
= 0;
3596 tinfo
->while_stepping
= wstep
;
3599 /* Release the while-stepping collecting state WSTEP. */
3602 release_while_stepping_state (struct wstep_state
*wstep
)
3607 /* Release all while-stepping collecting states currently associated
3608 with thread TINFO. */
3611 release_while_stepping_state_list (struct thread_info
*tinfo
)
3613 struct wstep_state
*head
;
3615 while (tinfo
->while_stepping
)
3617 head
= tinfo
->while_stepping
;
3618 tinfo
->while_stepping
= head
->next
;
3619 release_while_stepping_state (head
);
3623 /* If TINFO was handling a 'while-stepping' action, the step has
3624 finished, so collect any step data needed, and check if any more
3625 steps are required. Return true if the thread was indeed
3626 collecting tracepoint data, false otherwise. */
3629 tracepoint_finished_step (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3631 struct tracepoint
*tpoint
;
3632 struct wstep_state
*wstep
;
3633 struct wstep_state
**wstep_link
;
3634 struct trap_tracepoint_ctx ctx
;
3636 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3638 if (in_process_agent_loaded ())
3639 upload_fast_traceframes ();
3641 /* Check if we were indeed collecting data for one of more
3642 tracepoints with a 'while-stepping' count. */
3643 if (tinfo
->while_stepping
== NULL
)
3648 /* We're not even tracing anymore. Stop this thread from
3650 release_while_stepping_state_list (tinfo
);
3652 /* The thread had stopped due to a single-step request indeed
3653 explained by a tracepoint. */
3657 wstep
= tinfo
->while_stepping
;
3658 wstep_link
= &tinfo
->while_stepping
;
3660 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3661 target_pid_to_str (tinfo
->entry
.id
),
3662 wstep
->tp_number
, paddress (wstep
->tp_address
));
3664 ctx
.base
.type
= trap_tracepoint
;
3665 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3667 while (wstep
!= NULL
)
3669 tpoint
= find_tracepoint (wstep
->tp_number
, wstep
->tp_address
);
3672 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3673 wstep
->tp_number
, paddress (wstep
->tp_address
),
3674 target_pid_to_str (tinfo
->entry
.id
));
3677 *wstep_link
= wstep
->next
;
3678 release_while_stepping_state (wstep
);
3679 wstep
= *wstep_link
;
3683 /* We've just finished one step. */
3684 ++wstep
->current_step
;
3687 collect_data_at_step ((struct tracepoint_hit_ctx
*) &ctx
,
3688 stop_pc
, tpoint
, wstep
->current_step
);
3690 if (wstep
->current_step
>= tpoint
->step_count
)
3692 /* The requested numbers of steps have occurred. */
3693 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3694 target_pid_to_str (tinfo
->entry
.id
),
3695 wstep
->tp_number
, paddress (wstep
->tp_address
));
3697 /* Unlink the wstep. */
3698 *wstep_link
= wstep
->next
;
3699 release_while_stepping_state (wstep
);
3700 wstep
= *wstep_link
;
3702 /* Only check the hit count now, which ensure that we do all
3703 our stepping before stopping the run. */
3704 if (tpoint
->pass_count
> 0
3705 && tpoint
->hit_count
>= tpoint
->pass_count
3706 && stopping_tracepoint
== NULL
)
3707 stopping_tracepoint
= tpoint
;
3711 /* Keep single-stepping until the requested numbers of steps
3713 wstep_link
= &wstep
->next
;
3714 wstep
= *wstep_link
;
3717 if (stopping_tracepoint
3718 || trace_buffer_is_full
3719 || expr_eval_result
!= expr_eval_no_error
)
3729 /* Handle any internal tracing control breakpoint hits. That means,
3730 pull traceframes from the IPA to our buffer, and syncing both
3731 tracing agents when the IPA's tracing stops for some reason. */
3734 handle_tracepoint_bkpts (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3736 /* Pull in fast tracepoint trace frames from the inferior in-process
3737 agent's buffer into our buffer. */
3739 if (!in_process_agent_loaded ())
3742 upload_fast_traceframes ();
3744 /* Check if the in-process agent had decided we should stop
3746 if (stop_pc
== ipa_sym_addrs
.addr_stop_tracing
)
3748 int ipa_trace_buffer_is_full
;
3749 CORE_ADDR ipa_stopping_tracepoint
;
3750 int ipa_expr_eval_result
;
3751 CORE_ADDR ipa_error_tracepoint
;
3753 trace_debug ("lib stopped at stop_tracing");
3755 read_inferior_integer (ipa_sym_addrs
.addr_trace_buffer_is_full
,
3756 &ipa_trace_buffer_is_full
);
3758 read_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
,
3759 &ipa_stopping_tracepoint
);
3760 write_inferior_data_pointer (ipa_sym_addrs
.addr_stopping_tracepoint
, 0);
3762 read_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
,
3763 &ipa_error_tracepoint
);
3764 write_inferior_data_pointer (ipa_sym_addrs
.addr_error_tracepoint
, 0);
3766 read_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
,
3767 &ipa_expr_eval_result
);
3768 write_inferior_integer (ipa_sym_addrs
.addr_expr_eval_result
, 0);
3770 trace_debug ("lib: trace_buffer_is_full: %d, "
3771 "stopping_tracepoint: %s, "
3772 "ipa_expr_eval_result: %d, "
3773 "error_tracepoint: %s, ",
3774 ipa_trace_buffer_is_full
,
3775 paddress (ipa_stopping_tracepoint
),
3776 ipa_expr_eval_result
,
3777 paddress (ipa_error_tracepoint
));
3781 if (ipa_trace_buffer_is_full
)
3782 trace_debug ("lib stopped due to full buffer.");
3783 if (ipa_stopping_tracepoint
)
3784 trace_debug ("lib stopped due to tpoint");
3785 if (ipa_stopping_tracepoint
)
3786 trace_debug ("lib stopped due to error");
3789 if (ipa_stopping_tracepoint
!= 0)
3792 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint
);
3794 else if (ipa_expr_eval_result
!= expr_eval_no_error
)
3796 expr_eval_result
= ipa_expr_eval_result
;
3798 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint
);
3803 else if (stop_pc
== ipa_sym_addrs
.addr_flush_trace_buffer
)
3805 trace_debug ("lib stopped at flush_trace_buffer");
3812 /* Return true if TINFO just hit a tracepoint. Collect data if
3816 tracepoint_was_hit (struct thread_info
*tinfo
, CORE_ADDR stop_pc
)
3818 struct tracepoint
*tpoint
;
3820 struct trap_tracepoint_ctx ctx
;
3822 /* Not tracing, don't handle. */
3826 ctx
.base
.type
= trap_tracepoint
;
3827 ctx
.regcache
= get_thread_regcache (tinfo
, 1);
3829 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
3831 /* Note that we collect fast tracepoints here as well. We'll
3832 step over the fast tracepoint jump later, which avoids the
3834 if (tpoint
->enabled
&& stop_pc
== tpoint
->address
)
3836 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3837 target_pid_to_str (tinfo
->entry
.id
),
3838 tpoint
->number
, paddress (tpoint
->address
));
3840 /* Test the condition if present, and collect if true. */
3842 || (condition_true_at_tracepoint
3843 ((struct tracepoint_hit_ctx
*) &ctx
, tpoint
)))
3844 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
3847 if (stopping_tracepoint
3848 || trace_buffer_is_full
3849 || expr_eval_result
!= expr_eval_no_error
)
3853 /* If the tracepoint had a 'while-stepping' action, then set
3854 the thread to collect this tracepoint on the following
3856 else if (tpoint
->step_count
> 0)
3858 add_while_stepping_state (tinfo
,
3859 tpoint
->number
, tpoint
->address
);
3871 #if defined IN_PROCESS_AGENT && defined HAVE_UST
3872 struct ust_marker_data
;
3873 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
3875 struct tracepoint
*tpoint
,
3876 struct traceframe
*tframe
);
3879 /* Create a trace frame for the hit of the given tracepoint in the
3883 collect_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
, CORE_ADDR stop_pc
,
3884 struct tracepoint
*tpoint
)
3886 struct traceframe
*tframe
;
3889 /* Only count it as a hit when we actually collect data. */
3890 tpoint
->hit_count
++;
3892 /* If we've exceeded a defined pass count, record the event for
3893 later, and finish the collection for this hit. This test is only
3894 for nonstepping tracepoints, stepping tracepoints test at the end
3895 of their while-stepping loop. */
3896 if (tpoint
->pass_count
> 0
3897 && tpoint
->hit_count
>= tpoint
->pass_count
3898 && tpoint
->step_count
== 0
3899 && stopping_tracepoint
== NULL
)
3900 stopping_tracepoint
= tpoint
;
3902 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3903 tpoint
->number
, paddress (tpoint
->address
), tpoint
->hit_count
);
3905 tframe
= add_traceframe (tpoint
);
3909 for (acti
= 0; acti
< tpoint
->numactions
; ++acti
)
3911 #ifndef IN_PROCESS_AGENT
3912 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
3913 tpoint
->number
, paddress (tpoint
->address
),
3914 tpoint
->actions_str
[acti
]);
3917 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
3918 tpoint
->actions
[acti
]);
3921 finish_traceframe (tframe
);
3924 if (tframe
== NULL
&& tracing
)
3925 trace_buffer_is_full
= 1;
3928 #ifndef IN_PROCESS_AGENT
3931 collect_data_at_step (struct tracepoint_hit_ctx
*ctx
,
3933 struct tracepoint
*tpoint
, int current_step
)
3935 struct traceframe
*tframe
;
3938 trace_debug ("Making new step traceframe for "
3939 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
3940 tpoint
->number
, paddress (tpoint
->address
),
3941 current_step
, tpoint
->step_count
,
3944 tframe
= add_traceframe (tpoint
);
3948 for (acti
= 0; acti
< tpoint
->num_step_actions
; ++acti
)
3950 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
3951 tpoint
->number
, paddress (tpoint
->address
),
3952 tpoint
->step_actions_str
[acti
]);
3954 do_action_at_tracepoint (ctx
, stop_pc
, tpoint
, tframe
,
3955 tpoint
->step_actions
[acti
]);
3958 finish_traceframe (tframe
);
3961 if (tframe
== NULL
&& tracing
)
3962 trace_buffer_is_full
= 1;
3967 static struct regcache
*
3968 get_context_regcache (struct tracepoint_hit_ctx
*ctx
)
3970 struct regcache
*regcache
= NULL
;
3972 #ifdef IN_PROCESS_AGENT
3973 if (ctx
->type
== fast_tracepoint
)
3975 struct fast_tracepoint_ctx
*fctx
= (struct fast_tracepoint_ctx
*) ctx
;
3976 if (!fctx
->regcache_initted
)
3978 fctx
->regcache_initted
= 1;
3979 init_register_cache (&fctx
->regcache
, fctx
->regspace
);
3980 supply_regblock (&fctx
->regcache
, NULL
);
3981 supply_fast_tracepoint_registers (&fctx
->regcache
, fctx
->regs
);
3983 regcache
= &fctx
->regcache
;
3986 if (ctx
->type
== static_tracepoint
)
3988 struct static_tracepoint_ctx
*sctx
3989 = (struct static_tracepoint_ctx
*) ctx
;
3991 if (!sctx
->regcache_initted
)
3993 sctx
->regcache_initted
= 1;
3994 init_register_cache (&sctx
->regcache
, sctx
->regspace
);
3995 supply_regblock (&sctx
->regcache
, NULL
);
3996 /* Pass down the tracepoint address, because REGS doesn't
3997 include the PC, but we know what it must have been. */
3998 supply_static_tracepoint_registers (&sctx
->regcache
,
3999 (const unsigned char *)
4001 sctx
->tpoint
->address
);
4003 regcache
= &sctx
->regcache
;
4007 if (ctx
->type
== trap_tracepoint
)
4009 struct trap_tracepoint_ctx
*tctx
= (struct trap_tracepoint_ctx
*) ctx
;
4010 regcache
= tctx
->regcache
;
4014 gdb_assert (regcache
!= NULL
);
4020 do_action_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4022 struct tracepoint
*tpoint
,
4023 struct traceframe
*tframe
,
4024 struct tracepoint_action
*taction
)
4026 enum eval_result_type err
;
4028 switch (taction
->type
)
4032 struct collect_memory_action
*maction
;
4034 maction
= (struct collect_memory_action
*) taction
;
4036 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4037 pulongest (maction
->len
),
4038 paddress (maction
->addr
), maction
->basereg
);
4039 /* (should use basereg) */
4040 agent_mem_read (tframe
, NULL
,
4041 (CORE_ADDR
) maction
->addr
, maction
->len
);
4046 unsigned char *regspace
;
4047 struct regcache tregcache
;
4048 struct regcache
*context_regcache
;
4051 trace_debug ("Want to collect registers");
4053 /* Collect all registers for now. */
4054 regspace
= add_traceframe_block (tframe
,
4055 1 + register_cache_size ());
4056 if (regspace
== NULL
)
4058 trace_debug ("Trace buffer block allocation failed, skipping");
4061 /* Identify a register block. */
4064 context_regcache
= get_context_regcache (ctx
);
4066 /* Wrap the regblock in a register cache (in the stack, we
4067 don't want to malloc here). */
4068 init_register_cache (&tregcache
, regspace
+ 1);
4070 /* Copy the register data to the regblock. */
4071 regcache_cpy (&tregcache
, context_regcache
);
4073 #ifndef IN_PROCESS_AGENT
4074 /* On some platforms, trap-based tracepoints will have the PC
4075 pointing to the next instruction after the trap, but we
4076 don't want the user or GDB trying to guess whether the
4077 saved PC needs adjusting; so always record the adjusted
4078 stop_pc. Note that we can't use tpoint->address instead,
4079 since it will be wrong for while-stepping actions. This
4080 adjustment is a nop for fast tracepoints collected from the
4081 in-process lib (but not if GDBserver is collecting one
4082 preemptively), since the PC had already been adjusted to
4083 contain the tracepoint's address by the jump pad. */
4084 trace_debug ("Storing stop pc (0x%s) in regblock",
4085 paddress (tpoint
->address
));
4087 /* This changes the regblock, not the thread's
4089 regcache_write_pc (&tregcache
, stop_pc
);
4095 struct eval_expr_action
*eaction
;
4097 eaction
= (struct eval_expr_action
*) taction
;
4099 trace_debug ("Want to evaluate expression");
4101 err
= eval_agent_expr (ctx
, tframe
, eaction
->expr
, NULL
);
4103 if (err
!= expr_eval_no_error
)
4105 record_tracepoint_error (tpoint
, "action expression", err
);
4112 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4113 trace_debug ("Want to collect static trace data");
4114 collect_ust_data_at_tracepoint (ctx
, stop_pc
,
4117 trace_debug ("warning: collecting static trace data, "
4118 "but static tracepoints are not supported");
4123 trace_debug ("unknown trace action '%c', ignoring", taction
->type
);
4129 condition_true_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
4130 struct tracepoint
*tpoint
)
4133 enum eval_result_type err
;
4135 /* Presently, gdbserver doesn't run compiled conditions, only the
4136 IPA does. If the program stops at a fast tracepoint's address
4137 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4138 gdbserver preemptively collect the fast tracepoint. Later, on
4139 resume, gdbserver steps over the fast tracepoint like it steps
4140 over breakpoints, so that the IPA doesn't see that fast
4141 tracepoint. This avoids double collects of fast tracepoints in
4142 that stopping scenario. Having gdbserver itself handle the fast
4143 tracepoint gives the user a consistent view of when fast or trap
4144 tracepoints are collected, compared to an alternative where only
4145 trap tracepoints are collected on stop, and fast tracepoints on
4146 resume. When a fast tracepoint is being processed by gdbserver,
4147 it is always the non-compiled condition expression that is
4149 #ifdef IN_PROCESS_AGENT
4150 if (tpoint
->compiled_cond
)
4151 err
= ((condfn
) (uintptr_t) (tpoint
->compiled_cond
)) (ctx
, &value
);
4154 err
= eval_agent_expr (ctx
, NULL
, tpoint
->cond
, &value
);
4156 if (err
!= expr_eval_no_error
)
4158 record_tracepoint_error (tpoint
, "condition", err
);
4159 /* The error case must return false. */
4163 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4164 tpoint
->number
, paddress (tpoint
->address
),
4166 return (value
? 1 : 0);
4169 #ifndef IN_PROCESS_AGENT
4171 /* The packet form of an agent expression consists of an 'X', number
4172 of bytes in expression, a comma, and then the bytes. */
4174 static struct agent_expr
*
4175 parse_agent_expr (char **actparm
)
4177 char *act
= *actparm
;
4179 struct agent_expr
*aexpr
;
4181 ++act
; /* skip the X */
4182 act
= unpack_varlen_hex (act
, &xlen
);
4183 ++act
; /* skip a comma */
4184 aexpr
= xmalloc (sizeof (struct agent_expr
));
4185 aexpr
->length
= xlen
;
4186 aexpr
->bytes
= xmalloc (xlen
);
4187 convert_ascii_to_int (act
, aexpr
->bytes
, xlen
);
4188 *actparm
= act
+ (xlen
* 2);
4192 /* Convert the bytes of an agent expression back into hex digits, so
4193 they can be printed or uploaded. This allocates the buffer,
4194 callers should free when they are done with it. */
4197 unparse_agent_expr (struct agent_expr
*aexpr
)
4201 rslt
= xmalloc (2 * aexpr
->length
+ 1);
4202 convert_int_to_ascii (aexpr
->bytes
, rslt
, aexpr
->length
);
4208 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4211 gdb_agent_op_name (int op
)
4213 if (op
< 0 || op
>= gdb_agent_op_last
|| gdb_agent_op_names
[op
] == NULL
)
4215 return gdb_agent_op_names
[op
];
4218 /* The agent expression evaluator, as specified by the GDB docs. It
4219 returns 0 if everything went OK, and a nonzero error code
4222 static enum eval_result_type
4223 eval_agent_expr (struct tracepoint_hit_ctx
*ctx
,
4224 struct traceframe
*tframe
,
4225 struct agent_expr
*aexpr
,
4229 #define STACK_MAX 100
4230 ULONGEST stack
[STACK_MAX
], top
;
4235 /* This union is a convenient way to convert representations. For
4236 now, assume a standard architecture where the hardware integer
4237 types have 8, 16, 32, 64 bit types. A more robust solution would
4238 be to import stdint.h from gnulib. */
4243 unsigned char bytes
[1];
4248 unsigned char bytes
[2];
4253 unsigned char bytes
[4];
4258 unsigned char bytes
[8];
4263 if (aexpr
->length
== 0)
4265 trace_debug ("empty agent expression");
4266 return expr_eval_empty_expression
;
4269 /* Cache the stack top in its own variable. Much of the time we can
4270 operate on this variable, rather than dinking with the stack. It
4271 needs to be copied to the stack when sp changes. */
4276 op
= aexpr
->bytes
[pc
++];
4278 trace_debug ("About to interpret byte 0x%x", op
);
4282 case gdb_agent_op_add
:
4286 case gdb_agent_op_sub
:
4287 top
= stack
[--sp
] - top
;
4290 case gdb_agent_op_mul
:
4294 case gdb_agent_op_div_signed
:
4297 trace_debug ("Attempted to divide by zero");
4298 return expr_eval_divide_by_zero
;
4300 top
= ((LONGEST
) stack
[--sp
]) / ((LONGEST
) top
);
4303 case gdb_agent_op_div_unsigned
:
4306 trace_debug ("Attempted to divide by zero");
4307 return expr_eval_divide_by_zero
;
4309 top
= stack
[--sp
] / top
;
4312 case gdb_agent_op_rem_signed
:
4315 trace_debug ("Attempted to divide by zero");
4316 return expr_eval_divide_by_zero
;
4318 top
= ((LONGEST
) stack
[--sp
]) % ((LONGEST
) top
);
4321 case gdb_agent_op_rem_unsigned
:
4324 trace_debug ("Attempted to divide by zero");
4325 return expr_eval_divide_by_zero
;
4327 top
= stack
[--sp
] % top
;
4330 case gdb_agent_op_lsh
:
4331 top
= stack
[--sp
] << top
;
4334 case gdb_agent_op_rsh_signed
:
4335 top
= ((LONGEST
) stack
[--sp
]) >> top
;
4338 case gdb_agent_op_rsh_unsigned
:
4339 top
= stack
[--sp
] >> top
;
4342 case gdb_agent_op_trace
:
4343 agent_mem_read (tframe
,
4344 NULL
, (CORE_ADDR
) stack
[--sp
], (ULONGEST
) top
);
4349 case gdb_agent_op_trace_quick
:
4350 arg
= aexpr
->bytes
[pc
++];
4351 agent_mem_read (tframe
, NULL
, (CORE_ADDR
) top
, (ULONGEST
) arg
);
4354 case gdb_agent_op_log_not
:
4358 case gdb_agent_op_bit_and
:
4362 case gdb_agent_op_bit_or
:
4366 case gdb_agent_op_bit_xor
:
4370 case gdb_agent_op_bit_not
:
4374 case gdb_agent_op_equal
:
4375 top
= (stack
[--sp
] == top
);
4378 case gdb_agent_op_less_signed
:
4379 top
= (((LONGEST
) stack
[--sp
]) < ((LONGEST
) top
));
4382 case gdb_agent_op_less_unsigned
:
4383 top
= (stack
[--sp
] < top
);
4386 case gdb_agent_op_ext
:
4387 arg
= aexpr
->bytes
[pc
++];
4388 if (arg
< (sizeof (LONGEST
) * 8))
4390 LONGEST mask
= 1 << (arg
- 1);
4391 top
&= ((LONGEST
) 1 << arg
) - 1;
4392 top
= (top
^ mask
) - mask
;
4396 case gdb_agent_op_ref8
:
4397 agent_mem_read (tframe
, cnv
.u8
.bytes
, (CORE_ADDR
) top
, 1);
4401 case gdb_agent_op_ref16
:
4402 agent_mem_read (tframe
, cnv
.u16
.bytes
, (CORE_ADDR
) top
, 2);
4406 case gdb_agent_op_ref32
:
4407 agent_mem_read (tframe
, cnv
.u32
.bytes
, (CORE_ADDR
) top
, 4);
4411 case gdb_agent_op_ref64
:
4412 agent_mem_read (tframe
, cnv
.u64
.bytes
, (CORE_ADDR
) top
, 8);
4416 case gdb_agent_op_if_goto
:
4418 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4425 case gdb_agent_op_goto
:
4426 pc
= (aexpr
->bytes
[pc
] << 8) + (aexpr
->bytes
[pc
+ 1]);
4429 case gdb_agent_op_const8
:
4430 /* Flush the cached stack top. */
4432 top
= aexpr
->bytes
[pc
++];
4435 case gdb_agent_op_const16
:
4436 /* Flush the cached stack top. */
4438 top
= aexpr
->bytes
[pc
++];
4439 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4442 case gdb_agent_op_const32
:
4443 /* Flush the cached stack top. */
4445 top
= aexpr
->bytes
[pc
++];
4446 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4447 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4448 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4451 case gdb_agent_op_const64
:
4452 /* Flush the cached stack top. */
4454 top
= aexpr
->bytes
[pc
++];
4455 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4456 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4457 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4458 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4459 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4460 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4461 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
4464 case gdb_agent_op_reg
:
4465 /* Flush the cached stack top. */
4467 arg
= aexpr
->bytes
[pc
++];
4468 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4471 struct regcache
*regcache
;
4473 regcache
= get_context_regcache (ctx
);
4475 switch (register_size (regnum
))
4478 collect_register (regcache
, regnum
, cnv
.u64
.bytes
);
4482 collect_register (regcache
, regnum
, cnv
.u32
.bytes
);
4486 collect_register (regcache
, regnum
, cnv
.u16
.bytes
);
4490 collect_register (regcache
, regnum
, cnv
.u8
.bytes
);
4494 internal_error (__FILE__
, __LINE__
,
4495 "unhandled register size");
4500 case gdb_agent_op_end
:
4501 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4502 sp
, pulongest (top
));
4507 /* This should be an error */
4508 trace_debug ("Stack is empty, nothing to return");
4509 return expr_eval_empty_stack
;
4513 return expr_eval_no_error
;
4515 case gdb_agent_op_dup
:
4519 case gdb_agent_op_pop
:
4524 case gdb_agent_op_pick
:
4525 arg
= aexpr
->bytes
[pc
++];
4527 top
= stack
[sp
- arg
];
4531 case gdb_agent_op_rot
:
4533 ULONGEST tem
= stack
[sp
- 1];
4535 stack
[sp
- 1] = stack
[sp
- 2];
4536 stack
[sp
- 2] = top
;
4541 case gdb_agent_op_zero_ext
:
4542 arg
= aexpr
->bytes
[pc
++];
4543 if (arg
< (sizeof (LONGEST
) * 8))
4544 top
&= ((LONGEST
) 1 << arg
) - 1;
4547 case gdb_agent_op_swap
:
4548 /* Interchange top two stack elements, making sure top gets
4549 copied back onto stack. */
4551 top
= stack
[sp
- 1];
4552 stack
[sp
- 1] = stack
[sp
];
4555 case gdb_agent_op_getv
:
4556 /* Flush the cached stack top. */
4558 arg
= aexpr
->bytes
[pc
++];
4559 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4560 top
= get_trace_state_variable_value (arg
);
4563 case gdb_agent_op_setv
:
4564 arg
= aexpr
->bytes
[pc
++];
4565 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4566 set_trace_state_variable_value (arg
, top
);
4567 /* Note that we leave the value on the stack, for the
4568 benefit of later/enclosing expressions. */
4571 case gdb_agent_op_tracev
:
4572 arg
= aexpr
->bytes
[pc
++];
4573 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
4574 agent_tsv_read (tframe
, arg
);
4577 /* GDB never (currently) generates any of these ops. */
4578 case gdb_agent_op_float
:
4579 case gdb_agent_op_ref_float
:
4580 case gdb_agent_op_ref_double
:
4581 case gdb_agent_op_ref_long_double
:
4582 case gdb_agent_op_l_to_d
:
4583 case gdb_agent_op_d_to_l
:
4584 case gdb_agent_op_trace16
:
4585 trace_debug ("Agent expression op 0x%x valid, but not handled",
4587 /* If ever GDB generates any of these, we don't have the
4588 option of ignoring. */
4592 trace_debug ("Agent expression op 0x%x not recognized", op
);
4593 /* Don't struggle on, things will just get worse. */
4594 return expr_eval_unrecognized_opcode
;
4597 /* Check for stack badness. */
4598 if (sp
>= (STACK_MAX
- 1))
4600 trace_debug ("Expression stack overflow");
4601 return expr_eval_stack_overflow
;
4606 trace_debug ("Expression stack underflow");
4607 return expr_eval_stack_underflow
;
4610 trace_debug ("Op %s -> sp=%d, top=0x%s",
4611 gdb_agent_op_name (op
), sp
, pulongest (top
));
4615 /* Do memory copies for bytecodes. */
4616 /* Do the recording of memory blocks for actions and bytecodes. */
4619 agent_mem_read (struct traceframe
*tframe
,
4620 unsigned char *to
, CORE_ADDR from
, ULONGEST len
)
4622 unsigned char *mspace
;
4623 ULONGEST remaining
= len
;
4624 unsigned short blocklen
;
4626 /* If a 'to' buffer is specified, use it. */
4629 read_inferior_memory (from
, to
, len
);
4633 /* Otherwise, create a new memory block in the trace buffer. */
4634 while (remaining
> 0)
4638 blocklen
= (remaining
> 65535 ? 65535 : remaining
);
4639 sp
= 1 + sizeof (from
) + sizeof (blocklen
) + blocklen
;
4640 mspace
= add_traceframe_block (tframe
, sp
);
4643 /* Identify block as a memory block. */
4646 /* Record address and size. */
4647 memcpy (mspace
, &from
, sizeof (from
));
4648 mspace
+= sizeof (from
);
4649 memcpy (mspace
, &blocklen
, sizeof (blocklen
));
4650 mspace
+= sizeof (blocklen
);
4651 /* Record the memory block proper. */
4652 read_inferior_memory (from
, mspace
, blocklen
);
4653 trace_debug ("%d bytes recorded", blocklen
);
4654 remaining
-= blocklen
;
4660 /* Record the value of a trace state variable. */
4663 agent_tsv_read (struct traceframe
*tframe
, int n
)
4665 unsigned char *vspace
;
4668 vspace
= add_traceframe_block (tframe
,
4669 1 + sizeof (n
) + sizeof (LONGEST
));
4672 /* Identify block as a variable. */
4674 /* Record variable's number and value. */
4675 memcpy (vspace
+ 1, &n
, sizeof (n
));
4676 val
= get_trace_state_variable_value (n
);
4677 memcpy (vspace
+ 1 + sizeof (n
), &val
, sizeof (val
));
4678 trace_debug ("Variable %d recorded", n
);
4682 #ifndef IN_PROCESS_AGENT
4684 /* Callback for traceframe_walk_blocks, used to find a given block
4685 type in a traceframe. */
4688 match_blocktype (char blocktype
, unsigned char *dataptr
, void *data
)
4690 char *wantedp
= data
;
4692 if (*wantedp
== blocktype
)
4698 /* Walk over all traceframe blocks of the traceframe buffer starting
4699 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4700 block found, passing in DATA unmodified. If CALLBACK returns true,
4701 this returns a pointer to where the block is found. Returns NULL
4702 if no callback call returned true, indicating that all blocks have
4705 static unsigned char *
4706 traceframe_walk_blocks (unsigned char *database
, unsigned int datasize
,
4708 int (*callback
) (char blocktype
,
4709 unsigned char *dataptr
,
4713 unsigned char *dataptr
;
4717 trace_debug ("traceframe %d has no data", tfnum
);
4721 /* Iterate through a traceframe's blocks, looking for a block of the
4723 for (dataptr
= database
;
4724 dataptr
< database
+ datasize
;
4728 unsigned short mlen
;
4730 if (dataptr
== trace_buffer_wrap
)
4732 /* Adjust to reflect wrapping part of the frame around to
4734 datasize
= dataptr
- database
;
4735 dataptr
= database
= trace_buffer_lo
;
4738 blocktype
= *dataptr
++;
4740 if ((*callback
) (blocktype
, dataptr
, data
))
4746 /* Skip over the registers block. */
4747 dataptr
+= register_cache_size ();
4750 /* Skip over the memory block. */
4751 dataptr
+= sizeof (CORE_ADDR
);
4752 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4753 dataptr
+= (sizeof (mlen
) + mlen
);
4756 /* Skip over the TSV block. */
4757 dataptr
+= (sizeof (int) + sizeof (LONGEST
));
4760 /* Skip over the static trace data block. */
4761 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4762 dataptr
+= (sizeof (mlen
) + mlen
);
4765 trace_debug ("traceframe %d has unknown block type 0x%x",
4774 /* Look for the block of type TYPE_WANTED in the trameframe starting
4775 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4778 static unsigned char *
4779 traceframe_find_block_type (unsigned char *database
, unsigned int datasize
,
4780 int tfnum
, char type_wanted
)
4782 return traceframe_walk_blocks (database
, datasize
, tfnum
,
4783 match_blocktype
, &type_wanted
);
4786 static unsigned char *
4787 traceframe_find_regblock (struct traceframe
*tframe
, int tfnum
)
4789 unsigned char *regblock
;
4791 regblock
= traceframe_find_block_type (tframe
->data
,
4795 if (regblock
== NULL
)
4796 trace_debug ("traceframe %d has no register data", tfnum
);
4801 /* Get registers from a traceframe. */
4804 fetch_traceframe_registers (int tfnum
, struct regcache
*regcache
, int regnum
)
4806 unsigned char *dataptr
;
4807 struct tracepoint
*tpoint
;
4808 struct traceframe
*tframe
;
4810 tframe
= find_traceframe (tfnum
);
4814 trace_debug ("traceframe %d not found", tfnum
);
4818 dataptr
= traceframe_find_regblock (tframe
, tfnum
);
4819 if (dataptr
== NULL
)
4821 /* Mark registers unavailable. */
4822 supply_regblock (regcache
, NULL
);
4824 /* We can generally guess at a PC, although this will be
4825 misleading for while-stepping frames and multi-location
4827 tpoint
= find_next_tracepoint_by_number (NULL
, tframe
->tpnum
);
4829 regcache_write_pc (regcache
, tpoint
->address
);
4832 supply_regblock (regcache
, dataptr
);
4838 traceframe_get_pc (struct traceframe
*tframe
)
4840 struct regcache regcache
;
4841 unsigned char *dataptr
;
4843 dataptr
= traceframe_find_regblock (tframe
, -1);
4844 if (dataptr
== NULL
)
4847 init_register_cache (®cache
, dataptr
);
4848 return regcache_read_pc (®cache
);
4851 /* Read a requested block of memory from a trace frame. */
4854 traceframe_read_mem (int tfnum
, CORE_ADDR addr
,
4855 unsigned char *buf
, ULONGEST length
,
4858 struct traceframe
*tframe
;
4859 unsigned char *database
, *dataptr
;
4860 unsigned int datasize
;
4862 unsigned short mlen
;
4864 trace_debug ("traceframe_read_mem");
4866 tframe
= find_traceframe (tfnum
);
4870 trace_debug ("traceframe %d not found", tfnum
);
4874 datasize
= tframe
->data_size
;
4875 database
= dataptr
= &tframe
->data
[0];
4877 /* Iterate through a traceframe's blocks, looking for memory. */
4878 while ((dataptr
= traceframe_find_block_type (dataptr
,
4880 - (dataptr
- database
),
4881 tfnum
, 'M')) != NULL
)
4883 memcpy (&maddr
, dataptr
, sizeof (maddr
));
4884 dataptr
+= sizeof (maddr
);
4885 memcpy (&mlen
, dataptr
, sizeof (mlen
));
4886 dataptr
+= sizeof (mlen
);
4887 trace_debug ("traceframe %d has %d bytes at %s",
4888 tfnum
, mlen
, paddress (maddr
));
4890 /* If the block includes the first part of the desired range,
4891 return as much it has; GDB will re-request the remainder,
4892 which might be in a different block of this trace frame. */
4893 if (maddr
<= addr
&& addr
< (maddr
+ mlen
))
4895 ULONGEST amt
= (maddr
+ mlen
) - addr
;
4899 memcpy (buf
, dataptr
+ (addr
- maddr
), amt
);
4904 /* Skip over this block. */
4908 trace_debug ("traceframe %d has no memory data for the desired region",
4916 traceframe_read_tsv (int tsvnum
, LONGEST
*val
)
4919 struct traceframe
*tframe
;
4920 unsigned char *database
, *dataptr
;
4921 unsigned int datasize
;
4924 trace_debug ("traceframe_read_tsv");
4926 tfnum
= current_traceframe
;
4930 trace_debug ("no current traceframe");
4934 tframe
= find_traceframe (tfnum
);
4938 trace_debug ("traceframe %d not found", tfnum
);
4942 datasize
= tframe
->data_size
;
4943 database
= dataptr
= &tframe
->data
[0];
4945 /* Iterate through a traceframe's blocks, looking for the tsv. */
4946 while ((dataptr
= traceframe_find_block_type (dataptr
,
4948 - (dataptr
- database
),
4949 tfnum
, 'V')) != NULL
)
4951 memcpy (&vnum
, dataptr
, sizeof (vnum
));
4952 dataptr
+= sizeof (vnum
);
4954 trace_debug ("traceframe %d has variable %d", tfnum
, vnum
);
4956 /* Check that this is the variable we want. */
4959 memcpy (val
, dataptr
, sizeof (*val
));
4963 /* Skip over this block. */
4964 dataptr
+= sizeof (LONGEST
);
4967 trace_debug ("traceframe %d has no data for variable %d",
4972 /* Read a requested block of static tracepoint data from a trace
4976 traceframe_read_sdata (int tfnum
, ULONGEST offset
,
4977 unsigned char *buf
, ULONGEST length
,
4980 struct traceframe
*tframe
;
4981 unsigned char *database
, *dataptr
;
4982 unsigned int datasize
;
4983 unsigned short mlen
;
4985 trace_debug ("traceframe_read_sdata");
4987 tframe
= find_traceframe (tfnum
);
4991 trace_debug ("traceframe %d not found", tfnum
);
4995 datasize
= tframe
->data_size
;
4996 database
= &tframe
->data
[0];
4998 /* Iterate through a traceframe's blocks, looking for static
5000 dataptr
= traceframe_find_block_type (database
, datasize
,
5002 if (dataptr
!= NULL
)
5004 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5005 dataptr
+= sizeof (mlen
);
5008 if (offset
+ length
> mlen
)
5009 length
= mlen
- offset
;
5011 memcpy (buf
, dataptr
, length
);
5019 trace_debug ("traceframe %d has no static trace data", tfnum
);
5025 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5026 object. DATA is pointer to a struct buffer holding the
5027 traceframe-info object being built. */
5030 build_traceframe_info_xml (char blocktype
, unsigned char *dataptr
, void *data
)
5032 struct buffer
*buffer
= data
;
5038 unsigned short mlen
;
5041 memcpy (&maddr
, dataptr
, sizeof (maddr
));
5042 dataptr
+= sizeof (maddr
);
5043 memcpy (&mlen
, dataptr
, sizeof (mlen
));
5044 dataptr
+= sizeof (mlen
);
5045 buffer_xml_printf (buffer
,
5046 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5047 paddress (maddr
), phex_nz (mlen
, sizeof (mlen
)));
5057 warning ("Unhandled trace block type (%d) '%c ' "
5058 "while building trace frame info.",
5059 blocktype
, blocktype
);
5066 /* Build a traceframe-info object for traceframe number TFNUM into
5070 traceframe_read_info (int tfnum
, struct buffer
*buffer
)
5072 struct traceframe
*tframe
;
5074 trace_debug ("traceframe_read_info");
5076 tframe
= find_traceframe (tfnum
);
5080 trace_debug ("traceframe %d not found", tfnum
);
5084 buffer_grow_str (buffer
, "<traceframe-info>\n");
5085 traceframe_walk_blocks (tframe
->data
, tframe
->data_size
,
5086 tfnum
, build_traceframe_info_xml
, buffer
);
5087 buffer_grow_str0 (buffer
, "</traceframe-info>\n");
5091 /* Return the first fast tracepoint whose jump pad contains PC. */
5093 static struct tracepoint
*
5094 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc
)
5096 struct tracepoint
*tpoint
;
5098 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5099 if (tpoint
->type
== fast_tracepoint
)
5100 if (tpoint
->jump_pad
<= pc
&& pc
< tpoint
->jump_pad_end
)
5106 /* Return GDBserver's tracepoint that matches the IP Agent's
5107 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5110 static struct tracepoint
*
5111 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj
)
5113 struct tracepoint
*tpoint
;
5115 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
5116 if (tpoint
->type
== fast_tracepoint
)
5117 if (tpoint
->obj_addr_on_target
== ipa_tpoint_obj
)
5125 /* The type of the object that is used to synchronize fast tracepoint
5128 typedef struct collecting_t
5130 /* The fast tracepoint number currently collecting. */
5133 /* A number that GDBserver can use to identify the thread that is
5134 presently holding the collect lock. This need not (and usually
5135 is not) the thread id, as getting the current thread ID usually
5136 requires a system call, which we want to avoid like the plague.
5137 Usually this is thread's TCB, found in the TLS (pseudo-)
5138 register, which is readable with a single insn on several
5140 uintptr_t thread_area
;
5143 #ifndef IN_PROCESS_AGENT
5146 force_unlock_trace_buffer (void)
5148 write_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
, 0);
5151 /* Check if the thread identified by THREAD_AREA which is stopped at
5152 STOP_PC, is presently locking the fast tracepoint collection, and
5153 if so, gather some status of said collection. Returns 0 if the
5154 thread isn't collecting or in the jump pad at all. 1, if in the
5155 jump pad (or within gdb_collect) and hasn't executed the adjusted
5156 original insn yet (can set a breakpoint there and run to it). 2,
5157 if presently executing the adjusted original insn --- in which
5158 case, if we want to move the thread out of the jump pad, we need to
5159 single-step it until this function returns 0. */
5162 fast_tracepoint_collecting (CORE_ADDR thread_area
,
5164 struct fast_tpoint_collect_status
*status
)
5166 CORE_ADDR ipa_collecting
;
5167 CORE_ADDR ipa_gdb_jump_pad_buffer
, ipa_gdb_jump_pad_buffer_end
;
5168 struct tracepoint
*tpoint
;
5169 int needs_breakpoint
;
5171 /* The thread THREAD_AREA is either:
5173 0. not collecting at all, not within the jump pad, or within
5174 gdb_collect or one of its callees.
5176 1. in the jump pad and haven't reached gdb_collect
5178 2. within gdb_collect (out of the jump pad) (collect is set)
5180 3. we're in the jump pad, after gdb_collect having returned,
5181 possibly executing the adjusted insns.
5183 For cases 1 and 3, `collecting' may or not be set. The jump pad
5184 doesn't have any complicated jump logic, so we can tell if the
5185 thread is executing the adjust original insn or not by just
5186 matching STOP_PC with known jump pad addresses. If we it isn't
5187 yet executing the original insn, set a breakpoint there, and let
5188 the thread run to it, so to quickly step over a possible (many
5189 insns) gdb_collect call. Otherwise, or when the breakpoint is
5190 hit, only a few (small number of) insns are left to be executed
5191 in the jump pad. Single-step the thread until it leaves the
5196 needs_breakpoint
= 0;
5197 trace_debug ("fast_tracepoint_collecting");
5199 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer
,
5200 &ipa_gdb_jump_pad_buffer
))
5201 fatal ("error extracting `gdb_jump_pad_buffer'");
5202 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_jump_pad_buffer_end
,
5203 &ipa_gdb_jump_pad_buffer_end
))
5204 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5206 if (ipa_gdb_jump_pad_buffer
<= stop_pc
5207 && stop_pc
< ipa_gdb_jump_pad_buffer_end
)
5209 /* We can tell which tracepoint(s) the thread is collecting by
5210 matching the jump pad address back to the tracepoint. */
5211 tpoint
= fast_tracepoint_from_jump_pad_address (stop_pc
);
5214 warning ("in jump pad, but no matching tpoint?");
5219 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5221 tpoint
->number
, paddress (tpoint
->address
),
5222 paddress (tpoint
->jump_pad
),
5223 paddress (tpoint
->jump_pad_end
),
5224 paddress (tpoint
->adjusted_insn_addr
),
5225 paddress (tpoint
->adjusted_insn_addr_end
));
5228 /* Definitely in the jump pad. May or may not need
5229 fast-exit-jump-pad breakpoint. */
5230 if (tpoint
->jump_pad
<= stop_pc
5231 && stop_pc
< tpoint
->adjusted_insn_addr
)
5232 needs_breakpoint
= 1;
5236 collecting_t ipa_collecting_obj
;
5238 /* If `collecting' is set/locked, then the THREAD_AREA thread
5239 may or not be the one holding the lock. We have to read the
5240 lock to find out. */
5242 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_collecting
,
5245 trace_debug ("fast_tracepoint_collecting:"
5246 " failed reading 'collecting' in the inferior");
5250 if (!ipa_collecting
)
5252 trace_debug ("fast_tracepoint_collecting: not collecting"
5253 " (and nobody is).");
5257 /* Some thread is collecting. Check which. */
5258 if (read_inferior_memory (ipa_collecting
,
5259 (unsigned char *) &ipa_collecting_obj
,
5260 sizeof (ipa_collecting_obj
)) != 0)
5263 if (ipa_collecting_obj
.thread_area
!= thread_area
)
5265 trace_debug ("fast_tracepoint_collecting: not collecting "
5266 "(another thread is)");
5271 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj
.tpoint
);
5274 warning ("fast_tracepoint_collecting: collecting, "
5275 "but tpoint %s not found?",
5276 paddress ((CORE_ADDR
) ipa_collecting_obj
.tpoint
));
5280 /* The thread is within `gdb_collect', skip over the rest of
5281 fast tracepoint collection quickly using a breakpoint. */
5282 needs_breakpoint
= 1;
5285 /* The caller wants a bit of status detail. */
5288 status
->tpoint_num
= tpoint
->number
;
5289 status
->tpoint_addr
= tpoint
->address
;
5290 status
->adjusted_insn_addr
= tpoint
->adjusted_insn_addr
;
5291 status
->adjusted_insn_addr_end
= tpoint
->adjusted_insn_addr_end
;
5294 if (needs_breakpoint
)
5296 /* Hasn't executed the original instruction yet. Set breakpoint
5297 there, and wait till it's hit, then single-step until exiting
5301 fast_tracepoint_collecting, returning continue-until-break at %s",
5302 paddress (tpoint
->adjusted_insn_addr
));
5304 return 1; /* continue */
5308 /* Just single-step until exiting the jump pad. */
5310 trace_debug ("fast_tracepoint_collecting, returning "
5311 "need-single-step (%s-%s)",
5312 paddress (tpoint
->adjusted_insn_addr
),
5313 paddress (tpoint
->adjusted_insn_addr_end
));
5315 return 2; /* single-step */
5321 #ifdef IN_PROCESS_AGENT
5323 /* The global fast tracepoint collect lock. Points to a collecting_t
5324 object built on the stack by the jump pad, if presently locked;
5325 NULL if it isn't locked. Note that this lock *must* be set while
5326 executing any *function other than the jump pad. See
5327 fast_tracepoint_collecting. */
5328 static collecting_t
* ATTR_USED collecting
;
5330 /* This routine, called from the jump pad (in asm) is designed to be
5331 called from the jump pads of fast tracepoints, thus it is on the
5334 IP_AGENT_EXPORT
void ATTR_USED
5335 gdb_collect (struct tracepoint
*tpoint
, unsigned char *regs
)
5337 struct fast_tracepoint_ctx ctx
;
5339 /* Don't do anything until the trace run is completely set up. */
5343 ctx
.base
.type
= fast_tracepoint
;
5345 ctx
.regcache_initted
= 0;
5346 ctx
.tpoint
= tpoint
;
5348 /* Wrap the regblock in a register cache (in the stack, we don't
5349 want to malloc here). */
5350 ctx
.regspace
= alloca (register_cache_size ());
5351 if (ctx
.regspace
== NULL
)
5353 trace_debug ("Trace buffer block allocation failed, skipping");
5357 /* Test the condition if present, and collect if true. */
5358 if (tpoint
->cond
== NULL
5359 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5362 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
5363 tpoint
->address
, tpoint
);
5365 /* Note that this will cause original insns to be written back
5366 to where we jumped from, but that's OK because we're jumping
5367 back to the next whole instruction. This will go badly if
5368 instruction restoration is not atomic though. */
5369 if (stopping_tracepoint
5370 || trace_buffer_is_full
5371 || expr_eval_result
!= expr_eval_no_error
)
5376 /* If there was a condition and it evaluated to false, the only
5377 way we would stop tracing is if there was an error during
5378 condition expression evaluation. */
5379 if (expr_eval_result
!= expr_eval_no_error
)
5386 #ifndef IN_PROCESS_AGENT
5388 /* Bytecode compilation. */
5390 CORE_ADDR current_insn_ptr
;
5394 struct bytecode_address
5399 /* Offset and size of field to be modified in the goto block. */
5400 int from_offset
, from_size
;
5401 struct bytecode_address
*next
;
5402 } *bytecode_address_table
;
5405 get_raw_reg_func_addr (void)
5407 return ipa_sym_addrs
.addr_get_raw_reg
;
5411 emit_prologue (void)
5413 target_emit_ops ()->emit_prologue ();
5417 emit_epilogue (void)
5419 target_emit_ops ()->emit_epilogue ();
5425 target_emit_ops ()->emit_add ();
5431 target_emit_ops ()->emit_sub ();
5437 target_emit_ops ()->emit_mul ();
5443 target_emit_ops ()->emit_lsh ();
5447 emit_rsh_signed (void)
5449 target_emit_ops ()->emit_rsh_signed ();
5453 emit_rsh_unsigned (void)
5455 target_emit_ops ()->emit_rsh_unsigned ();
5461 target_emit_ops ()->emit_ext (arg
);
5467 target_emit_ops ()->emit_log_not ();
5473 target_emit_ops ()->emit_bit_and ();
5479 target_emit_ops ()->emit_bit_or ();
5485 target_emit_ops ()->emit_bit_xor ();
5491 target_emit_ops ()->emit_bit_not ();
5497 target_emit_ops ()->emit_equal ();
5501 emit_less_signed (void)
5503 target_emit_ops ()->emit_less_signed ();
5507 emit_less_unsigned (void)
5509 target_emit_ops ()->emit_less_unsigned ();
5515 target_emit_ops ()->emit_ref (size
);
5519 emit_if_goto (int *offset_p
, int *size_p
)
5521 target_emit_ops ()->emit_if_goto (offset_p
, size_p
);
5525 emit_goto (int *offset_p
, int *size_p
)
5527 target_emit_ops ()->emit_goto (offset_p
, size_p
);
5531 write_goto_address (CORE_ADDR from
, CORE_ADDR to
, int size
)
5533 target_emit_ops ()->write_goto_address (from
, to
, size
);
5537 emit_const (LONGEST num
)
5539 target_emit_ops ()->emit_const (num
);
5545 target_emit_ops ()->emit_reg (reg
);
5551 target_emit_ops ()->emit_pop ();
5555 emit_stack_flush (void)
5557 target_emit_ops ()->emit_stack_flush ();
5561 emit_zero_ext (int arg
)
5563 target_emit_ops ()->emit_zero_ext (arg
);
5569 target_emit_ops ()->emit_swap ();
5573 emit_stack_adjust (int n
)
5575 target_emit_ops ()->emit_stack_adjust (n
);
5578 /* FN's prototype is `LONGEST(*fn)(int)'. */
5581 emit_int_call_1 (CORE_ADDR fn
, int arg1
)
5583 target_emit_ops ()->emit_int_call_1 (fn
, arg1
);
5586 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5589 emit_void_call_2 (CORE_ADDR fn
, int arg1
)
5591 target_emit_ops ()->emit_void_call_2 (fn
, arg1
);
5594 static enum eval_result_type
compile_bytecodes (struct agent_expr
*aexpr
);
5597 compile_tracepoint_condition (struct tracepoint
*tpoint
,
5598 CORE_ADDR
*jump_entry
)
5600 CORE_ADDR entry_point
= *jump_entry
;
5601 enum eval_result_type err
;
5603 trace_debug ("Starting condition compilation for tracepoint %d\n",
5606 /* Initialize the global pointer to the code being built. */
5607 current_insn_ptr
= *jump_entry
;
5611 err
= compile_bytecodes (tpoint
->cond
);
5613 if (err
== expr_eval_no_error
)
5617 /* Record the beginning of the compiled code. */
5618 tpoint
->compiled_cond
= entry_point
;
5620 trace_debug ("Condition compilation for tracepoint %d complete\n",
5625 /* Leave the unfinished code in situ, but don't point to it. */
5627 tpoint
->compiled_cond
= 0;
5629 trace_debug ("Condition compilation for tracepoint %d failed, "
5631 tpoint
->number
, err
);
5634 /* Update the code pointer passed in. Note that we do this even if
5635 the compile fails, so that we can look at the partial results
5636 instead of letting them be overwritten. */
5637 *jump_entry
= current_insn_ptr
;
5639 /* Leave a gap, to aid dump decipherment. */
5643 /* Given an agent expression, turn it into native code. */
5645 static enum eval_result_type
5646 compile_bytecodes (struct agent_expr
*aexpr
)
5652 /* This is only used to build 64-bit value for constants. */
5654 struct bytecode_address
*aentry
, *aentry2
;
5659 trace_debug ("Cannot compile op 0x%x\n", op); \
5660 return expr_eval_unhandled_opcode; \
5663 if (aexpr
->length
== 0)
5665 trace_debug ("empty agent expression\n");
5666 return expr_eval_empty_expression
;
5669 bytecode_address_table
= NULL
;
5673 op
= aexpr
->bytes
[pc
];
5675 trace_debug ("About to compile op 0x%x, pc=%d\n", op
, pc
);
5677 /* Record the compiled-code address of the bytecode, for use by
5678 jump instructions. */
5679 aentry
= xmalloc (sizeof (struct bytecode_address
));
5681 aentry
->address
= current_insn_ptr
;
5682 aentry
->goto_pc
= -1;
5683 aentry
->from_offset
= aentry
->from_size
= 0;
5684 aentry
->next
= bytecode_address_table
;
5685 bytecode_address_table
= aentry
;
5693 case gdb_agent_op_add
:
5697 case gdb_agent_op_sub
:
5701 case gdb_agent_op_mul
:
5705 case gdb_agent_op_div_signed
:
5709 case gdb_agent_op_div_unsigned
:
5713 case gdb_agent_op_rem_signed
:
5717 case gdb_agent_op_rem_unsigned
:
5721 case gdb_agent_op_lsh
:
5725 case gdb_agent_op_rsh_signed
:
5729 case gdb_agent_op_rsh_unsigned
:
5730 emit_rsh_unsigned ();
5733 case gdb_agent_op_trace
:
5737 case gdb_agent_op_trace_quick
:
5741 case gdb_agent_op_log_not
:
5745 case gdb_agent_op_bit_and
:
5749 case gdb_agent_op_bit_or
:
5753 case gdb_agent_op_bit_xor
:
5757 case gdb_agent_op_bit_not
:
5761 case gdb_agent_op_equal
:
5765 case gdb_agent_op_less_signed
:
5766 emit_less_signed ();
5769 case gdb_agent_op_less_unsigned
:
5770 emit_less_unsigned ();
5773 case gdb_agent_op_ext
:
5774 arg
= aexpr
->bytes
[pc
++];
5775 if (arg
< (sizeof (LONGEST
) * 8))
5779 case gdb_agent_op_ref8
:
5783 case gdb_agent_op_ref16
:
5787 case gdb_agent_op_ref32
:
5791 case gdb_agent_op_ref64
:
5795 case gdb_agent_op_if_goto
:
5796 arg
= aexpr
->bytes
[pc
++];
5797 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5798 aentry
->goto_pc
= arg
;
5799 emit_if_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5802 case gdb_agent_op_goto
:
5803 arg
= aexpr
->bytes
[pc
++];
5804 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5805 aentry
->goto_pc
= arg
;
5806 emit_goto (&(aentry
->from_offset
), &(aentry
->from_size
));
5809 case gdb_agent_op_const8
:
5810 emit_stack_flush ();
5811 top
= aexpr
->bytes
[pc
++];
5815 case gdb_agent_op_const16
:
5816 emit_stack_flush ();
5817 top
= aexpr
->bytes
[pc
++];
5818 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5822 case gdb_agent_op_const32
:
5823 emit_stack_flush ();
5824 top
= aexpr
->bytes
[pc
++];
5825 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5826 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5827 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5831 case gdb_agent_op_const64
:
5832 emit_stack_flush ();
5833 top
= aexpr
->bytes
[pc
++];
5834 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5835 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5836 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5837 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5838 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5839 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5840 top
= (top
<< 8) + aexpr
->bytes
[pc
++];
5844 case gdb_agent_op_reg
:
5845 emit_stack_flush ();
5846 arg
= aexpr
->bytes
[pc
++];
5847 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5851 case gdb_agent_op_end
:
5852 trace_debug ("At end of expression\n");
5854 /* Assume there is one stack element left, and that it is
5855 cached in "top" where emit_epilogue can get to it. */
5856 emit_stack_adjust (1);
5861 case gdb_agent_op_dup
:
5862 /* In our design, dup is equivalent to stack flushing. */
5863 emit_stack_flush ();
5866 case gdb_agent_op_pop
:
5870 case gdb_agent_op_zero_ext
:
5871 arg
= aexpr
->bytes
[pc
++];
5872 if (arg
< (sizeof (LONGEST
) * 8))
5873 emit_zero_ext (arg
);
5876 case gdb_agent_op_swap
:
5880 case gdb_agent_op_getv
:
5881 emit_stack_flush ();
5882 arg
= aexpr
->bytes
[pc
++];
5883 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5884 emit_int_call_1 (ipa_sym_addrs
.addr_get_trace_state_variable_value
,
5888 case gdb_agent_op_setv
:
5889 arg
= aexpr
->bytes
[pc
++];
5890 arg
= (arg
<< 8) + aexpr
->bytes
[pc
++];
5891 emit_void_call_2 (ipa_sym_addrs
.addr_set_trace_state_variable_value
,
5895 case gdb_agent_op_tracev
:
5899 /* GDB never (currently) generates any of these ops. */
5900 case gdb_agent_op_float
:
5901 case gdb_agent_op_ref_float
:
5902 case gdb_agent_op_ref_double
:
5903 case gdb_agent_op_ref_long_double
:
5904 case gdb_agent_op_l_to_d
:
5905 case gdb_agent_op_d_to_l
:
5906 case gdb_agent_op_trace16
:
5911 trace_debug ("Agent expression op 0x%x not recognized\n", op
);
5912 /* Don't struggle on, things will just get worse. */
5913 return expr_eval_unrecognized_opcode
;
5916 /* This catches errors that occur in target-specific code
5920 trace_debug ("Error %d while emitting code for %s\n",
5921 emit_error
, gdb_agent_op_name (op
));
5922 return expr_eval_unhandled_opcode
;
5925 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op
));
5928 /* Now fill in real addresses as goto destinations. */
5929 for (aentry
= bytecode_address_table
; aentry
; aentry
= aentry
->next
)
5933 if (aentry
->goto_pc
< 0)
5936 /* Find the location that we are going to, and call back into
5937 target-specific code to write the actual address or
5939 for (aentry2
= bytecode_address_table
; aentry2
; aentry2
= aentry2
->next
)
5941 if (aentry2
->pc
== aentry
->goto_pc
)
5943 trace_debug ("Want to jump from %s to %s\n",
5944 paddress (aentry
->address
),
5945 paddress (aentry2
->address
));
5946 write_goto_address (aentry
->address
+ aentry
->from_offset
,
5947 aentry2
->address
, aentry
->from_size
);
5953 /* Error out if we didn't find a destination. */
5956 trace_debug ("Destination of goto %d not found\n",
5958 return expr_eval_invalid_goto
;
5962 return expr_eval_no_error
;
5965 /* We'll need to adjust these when we consider bi-arch setups, and big
5969 write_inferior_data_ptr (CORE_ADDR where
, CORE_ADDR ptr
)
5971 return write_inferior_memory (where
,
5972 (unsigned char *) &ptr
, sizeof (void *));
5975 /* The base pointer of the IPA's heap. This is the only memory the
5976 IPA is allowed to use. The IPA should _not_ call the inferior's
5977 `malloc' during operation. That'd be slow, and, most importantly,
5978 it may not be safe. We may be collecting a tracepoint in a signal
5979 handler, for example. */
5980 static CORE_ADDR target_tp_heap
;
5982 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5986 target_malloc (ULONGEST size
)
5990 if (target_tp_heap
== 0)
5992 /* We have the pointer *address*, need what it points to. */
5993 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_gdb_tp_heap_buffer
,
5995 fatal ("could get target heap head pointer");
5998 ptr
= target_tp_heap
;
5999 target_tp_heap
+= size
;
6001 /* Pad to 8-byte alignment. */
6002 target_tp_heap
= ((target_tp_heap
+ 7) & ~0x7);
6008 download_agent_expr (struct agent_expr
*expr
)
6010 CORE_ADDR expr_addr
;
6011 CORE_ADDR expr_bytes
;
6013 expr_addr
= target_malloc (sizeof (*expr
));
6014 write_inferior_memory (expr_addr
, (unsigned char *) expr
, sizeof (*expr
));
6016 expr_bytes
= target_malloc (expr
->length
);
6017 write_inferior_data_ptr (expr_addr
+ offsetof (struct agent_expr
, bytes
),
6019 write_inferior_memory (expr_bytes
, expr
->bytes
, expr
->length
);
6024 /* Align V up to N bits. */
6025 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6028 download_tracepoints (void)
6030 CORE_ADDR tpptr
= 0, prev_tpptr
= 0;
6031 struct tracepoint
*tpoint
;
6033 /* Start out empty. */
6034 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, 0);
6036 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6038 struct tracepoint target_tracepoint
;
6040 if (tpoint
->type
!= fast_tracepoint
6041 && tpoint
->type
!= static_tracepoint
)
6044 /* Maybe download a compiled condition. */
6045 if (tpoint
->cond
!= NULL
&& target_emit_ops () != NULL
)
6047 CORE_ADDR jentry
, jump_entry
;
6049 jentry
= jump_entry
= get_jump_space_head ();
6051 if (tpoint
->cond
!= NULL
)
6053 /* Pad to 8-byte alignment. (needed?) */
6054 /* Actually this should be left for the target to
6056 jentry
= UALIGN (jentry
, 8);
6058 compile_tracepoint_condition (tpoint
, &jentry
);
6061 /* Pad to 8-byte alignment. */
6062 jentry
= UALIGN (jentry
, 8);
6063 claim_jump_space (jentry
- jump_entry
);
6066 target_tracepoint
= *tpoint
;
6069 tpptr
= target_malloc (sizeof (*tpoint
));
6070 tpoint
->obj_addr_on_target
= tpptr
;
6072 if (tpoint
== tracepoints
)
6074 /* First object in list, set the head pointer in the
6076 write_inferior_data_ptr (ipa_sym_addrs
.addr_tracepoints
, tpptr
);
6080 write_inferior_data_ptr (prev_tpptr
+ offsetof (struct tracepoint
,
6085 /* Write the whole object. We'll fix up its pointers in a bit.
6086 Assume no next for now. This is fixed up above on the next
6087 iteration, if there's any. */
6088 target_tracepoint
.next
= NULL
;
6089 /* Need to clear this here too, since we're downloading the
6090 tracepoints before clearing our own copy. */
6091 target_tracepoint
.hit_count
= 0;
6093 write_inferior_memory (tpptr
, (unsigned char *) &target_tracepoint
,
6094 sizeof (target_tracepoint
));
6097 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6099 download_agent_expr (tpoint
->cond
));
6101 if (tpoint
->numactions
)
6104 CORE_ADDR actions_array
;
6106 /* The pointers array. */
6108 = target_malloc (sizeof (*tpoint
->actions
) * tpoint
->numactions
);
6109 write_inferior_data_ptr (tpptr
+ offsetof (struct tracepoint
,
6113 /* Now for each pointer, download the action. */
6114 for (i
= 0; i
< tpoint
->numactions
; i
++)
6116 CORE_ADDR ipa_action
= 0;
6117 struct tracepoint_action
*action
= tpoint
->actions
[i
];
6119 switch (action
->type
)
6123 = target_malloc (sizeof (struct collect_memory_action
));
6124 write_inferior_memory (ipa_action
,
6125 (unsigned char *) action
,
6126 sizeof (struct collect_memory_action
));
6130 = target_malloc (sizeof (struct collect_registers_action
));
6131 write_inferior_memory (ipa_action
,
6132 (unsigned char *) action
,
6133 sizeof (struct collect_registers_action
));
6138 struct eval_expr_action
*eaction
6139 = (struct eval_expr_action
*) action
;
6141 ipa_action
= target_malloc (sizeof (*eaction
));
6142 write_inferior_memory (ipa_action
,
6143 (unsigned char *) eaction
,
6146 expr
= download_agent_expr (eaction
->expr
);
6147 write_inferior_data_ptr
6148 (ipa_action
+ offsetof (struct eval_expr_action
, expr
),
6153 ipa_action
= target_malloc
6154 (sizeof (struct collect_static_trace_data_action
));
6155 write_inferior_memory
6157 (unsigned char *) action
,
6158 sizeof (struct collect_static_trace_data_action
));
6161 trace_debug ("unknown trace action '%c', ignoring",
6166 if (ipa_action
!= 0)
6167 write_inferior_data_ptr
6168 (actions_array
+ i
* sizeof (sizeof (*tpoint
->actions
)),
6176 download_trace_state_variables (void)
6178 CORE_ADDR ptr
= 0, prev_ptr
= 0;
6179 struct trace_state_variable
*tsv
;
6181 /* Start out empty. */
6182 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
, 0);
6184 for (tsv
= trace_state_variables
; tsv
!= NULL
; tsv
= tsv
->next
)
6186 struct trace_state_variable target_tsv
;
6188 /* TSV's with a getter have been initialized equally in both the
6189 inferior and GDBserver. Skip them. */
6190 if (tsv
->getter
!= NULL
)
6196 ptr
= target_malloc (sizeof (*tsv
));
6198 if (tsv
== trace_state_variables
)
6200 /* First object in list, set the head pointer in the
6203 write_inferior_data_ptr (ipa_sym_addrs
.addr_trace_state_variables
,
6208 write_inferior_data_ptr (prev_ptr
6209 + offsetof (struct trace_state_variable
,
6214 /* Write the whole object. We'll fix up its pointers in a bit.
6215 Assume no next, fixup when needed. */
6216 target_tsv
.next
= NULL
;
6218 write_inferior_memory (ptr
, (unsigned char *) &target_tsv
,
6219 sizeof (target_tsv
));
6221 if (tsv
->name
!= NULL
)
6223 size_t size
= strlen (tsv
->name
) + 1;
6224 CORE_ADDR name_addr
= target_malloc (size
);
6225 write_inferior_memory (name_addr
,
6226 (unsigned char *) tsv
->name
, size
);
6227 write_inferior_data_ptr (ptr
6228 + offsetof (struct trace_state_variable
,
6233 if (tsv
->getter
!= NULL
)
6235 fatal ("what to do with these?");
6241 /* Fixup the next pointer in the last item in the list. */
6242 write_inferior_data_ptr (prev_ptr
6243 + offsetof (struct trace_state_variable
,
6248 /* Upload complete trace frames out of the IP Agent's trace buffer
6249 into GDBserver's trace buffer. This always uploads either all or
6250 no trace frames. This is the counter part of
6251 `trace_alloc_trace_buffer'. See its description of the atomic
6252 synching mechanism. */
6255 upload_fast_traceframes (void)
6257 unsigned int ipa_traceframe_read_count
, ipa_traceframe_write_count
;
6258 unsigned int ipa_traceframe_read_count_racy
, ipa_traceframe_write_count_racy
;
6260 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl
;
6261 unsigned int curr_tbctrl_idx
;
6262 unsigned int ipa_trace_buffer_ctrl_curr
;
6263 unsigned int ipa_trace_buffer_ctrl_curr_old
;
6264 CORE_ADDR ipa_trace_buffer_ctrl_addr
;
6265 struct breakpoint
*about_to_request_buffer_space_bkpt
;
6266 CORE_ADDR ipa_trace_buffer_lo
;
6267 CORE_ADDR ipa_trace_buffer_hi
;
6269 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6270 &ipa_traceframe_read_count_racy
))
6272 /* This will happen in most targets if the current thread is
6277 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6278 &ipa_traceframe_write_count_racy
))
6281 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6282 ipa_traceframe_write_count_racy
6283 - ipa_traceframe_read_count_racy
,
6284 ipa_traceframe_write_count_racy
,
6285 ipa_traceframe_read_count_racy
);
6287 if (ipa_traceframe_write_count_racy
== ipa_traceframe_read_count_racy
)
6290 about_to_request_buffer_space_bkpt
6291 = set_breakpoint_at (ipa_sym_addrs
.addr_about_to_request_buffer_space
,
6294 if (read_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6295 &ipa_trace_buffer_ctrl_curr
))
6298 ipa_trace_buffer_ctrl_curr_old
= ipa_trace_buffer_ctrl_curr
;
6300 curr_tbctrl_idx
= ipa_trace_buffer_ctrl_curr
& ~GDBSERVER_FLUSH_COUNT_MASK
;
6303 unsigned int prev
, counter
;
6305 /* Update the token, with new counters, and the GDBserver stamp
6306 bit. Alway reuse the current TBC index. */
6307 prev
= ipa_trace_buffer_ctrl_curr
& 0x0007ff00;
6308 counter
= (prev
+ 0x100) & 0x0007ff00;
6310 ipa_trace_buffer_ctrl_curr
= (0x80000000
6316 if (write_inferior_uinteger (ipa_sym_addrs
.addr_trace_buffer_ctrl_curr
,
6317 ipa_trace_buffer_ctrl_curr
))
6320 trace_debug ("Lib: Committed %08x -> %08x",
6321 ipa_trace_buffer_ctrl_curr_old
,
6322 ipa_trace_buffer_ctrl_curr
);
6324 /* Re-read these, now that we've installed the
6325 `about_to_request_buffer_space' breakpoint/lock. A thread could
6326 have finished a traceframe between the last read of these
6327 counters and setting the breakpoint above. If we start
6328 uploading, we never want to leave this function with
6329 traceframe_read_count != 0, otherwise, GDBserver could end up
6330 incrementing the counter tokens more than once (due to event loop
6331 nesting), which would break the IP agent's "effective" detection
6332 (see trace_alloc_trace_buffer). */
6333 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_read_count
,
6334 &ipa_traceframe_read_count
))
6336 if (read_inferior_uinteger (ipa_sym_addrs
.addr_traceframe_write_count
,
6337 &ipa_traceframe_write_count
))
6342 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6343 ipa_traceframe_write_count
- ipa_traceframe_read_count
,
6344 ipa_traceframe_write_count
, ipa_traceframe_read_count
);
6346 if (ipa_traceframe_write_count
!= ipa_traceframe_write_count_racy
6347 || ipa_traceframe_read_count
!= ipa_traceframe_read_count_racy
)
6348 trace_debug ("note that ipa_traceframe_count's parts changed");
6351 /* Get the address of the current TBC object (the IP agent has an
6352 array of 3 such objects). The index is stored in the TBC
6354 ipa_trace_buffer_ctrl_addr
= ipa_sym_addrs
.addr_trace_buffer_ctrl
;
6355 ipa_trace_buffer_ctrl_addr
6356 += sizeof (struct ipa_trace_buffer_control
) * curr_tbctrl_idx
;
6358 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6359 (unsigned char *) &ipa_trace_buffer_ctrl
,
6360 sizeof (struct ipa_trace_buffer_control
)))
6363 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_lo
,
6364 &ipa_trace_buffer_lo
))
6366 if (read_inferior_data_pointer (ipa_sym_addrs
.addr_trace_buffer_hi
,
6367 &ipa_trace_buffer_hi
))
6370 /* Offsets are easier to grok for debugging than raw addresses,
6371 especially for the small trace buffer sizes that are useful for
6373 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6374 "endfree=%d wrap=%d hi=%d",
6376 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6377 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6378 (int) (ipa_trace_buffer_ctrl
.end_free
- ipa_trace_buffer_lo
),
6379 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6380 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6382 /* Note that the IPA's buffer is always circular. */
6384 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6386 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6387 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6389 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6390 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6391 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6392 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6395 tf
= IPA_FIRST_TRACEFRAME ();
6397 while (ipa_traceframe_write_count
- ipa_traceframe_read_count
)
6399 struct tracepoint
*tpoint
;
6400 struct traceframe
*tframe
;
6401 unsigned char *block
;
6402 struct traceframe ipa_tframe
;
6404 if (read_inferior_memory (tf
, (unsigned char *) &ipa_tframe
,
6405 offsetof (struct traceframe
, data
)))
6406 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf
));
6408 if (ipa_tframe
.tpnum
== 0)
6409 fatal ("Uploading: No (more) fast traceframes, but "
6410 "ipa_traceframe_count == %u??\n",
6411 ipa_traceframe_write_count
- ipa_traceframe_read_count
);
6413 /* Note that this will be incorrect for multi-location
6415 tpoint
= find_next_tracepoint_by_number (NULL
, ipa_tframe
.tpnum
);
6417 tframe
= add_traceframe (tpoint
);
6420 trace_buffer_is_full
= 1;
6421 trace_debug ("Uploading: trace buffer is full");
6425 /* Copy the whole set of blocks in one go for now. FIXME:
6426 split this in smaller blocks. */
6427 block
= add_traceframe_block (tframe
, ipa_tframe
.data_size
);
6430 if (read_inferior_memory (tf
6431 + offsetof (struct traceframe
, data
),
6432 block
, ipa_tframe
.data_size
))
6433 error ("Uploading: Couldn't read traceframe data at %s\n",
6434 paddress (tf
+ offsetof (struct traceframe
, data
)));
6437 trace_debug ("Uploading: traceframe didn't fit");
6438 finish_traceframe (tframe
);
6441 tf
= IPA_NEXT_TRACEFRAME (tf
, &ipa_tframe
);
6443 /* If we freed the traceframe that wrapped around, go back
6444 to the non-wrap case. */
6445 if (tf
< ipa_trace_buffer_ctrl
.start
)
6447 trace_debug ("Lib: Discarding past the wraparound");
6448 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6450 ipa_trace_buffer_ctrl
.start
= tf
;
6451 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_ctrl
.start
;
6452 ++ipa_traceframe_read_count
;
6454 if (ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.free
6455 && ipa_trace_buffer_ctrl
.start
== ipa_trace_buffer_ctrl
.end_free
)
6457 trace_debug ("Lib: buffer is fully empty. "
6458 "Trace buffer [%d] start=%d free=%d endfree=%d",
6460 (int) (ipa_trace_buffer_ctrl
.start
6461 - ipa_trace_buffer_lo
),
6462 (int) (ipa_trace_buffer_ctrl
.free
6463 - ipa_trace_buffer_lo
),
6464 (int) (ipa_trace_buffer_ctrl
.end_free
6465 - ipa_trace_buffer_lo
));
6467 ipa_trace_buffer_ctrl
.start
= ipa_trace_buffer_lo
;
6468 ipa_trace_buffer_ctrl
.free
= ipa_trace_buffer_lo
;
6469 ipa_trace_buffer_ctrl
.end_free
= ipa_trace_buffer_hi
;
6470 ipa_trace_buffer_ctrl
.wrap
= ipa_trace_buffer_hi
;
6473 trace_debug ("Uploaded a traceframe\n"
6474 "Lib: Trace buffer [%d] start=%d free=%d "
6475 "endfree=%d wrap=%d hi=%d",
6477 (int) (ipa_trace_buffer_ctrl
.start
- ipa_trace_buffer_lo
),
6478 (int) (ipa_trace_buffer_ctrl
.free
- ipa_trace_buffer_lo
),
6479 (int) (ipa_trace_buffer_ctrl
.end_free
6480 - ipa_trace_buffer_lo
),
6481 (int) (ipa_trace_buffer_ctrl
.wrap
- ipa_trace_buffer_lo
),
6482 (int) (ipa_trace_buffer_hi
- ipa_trace_buffer_lo
));
6485 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr
,
6486 (unsigned char *) &ipa_trace_buffer_ctrl
,
6487 sizeof (struct ipa_trace_buffer_control
)))
6490 write_inferior_integer (ipa_sym_addrs
.addr_traceframe_read_count
,
6491 ipa_traceframe_read_count
);
6493 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx
);
6496 cancel_breakpoints ();
6498 delete_breakpoint (about_to_request_buffer_space_bkpt
);
6499 about_to_request_buffer_space_bkpt
= NULL
;
6503 if (trace_buffer_is_full
)
6508 #ifdef IN_PROCESS_AGENT
6510 IP_AGENT_EXPORT
int ust_loaded
;
6511 IP_AGENT_EXPORT
char cmd_buf
[CMD_BUF_SIZE
];
6515 /* Static tracepoints. */
6517 /* UST puts a "struct tracepoint" in the global namespace, which
6518 conflicts with our tracepoint. Arguably, being a library, it
6519 shouldn't take ownership of such a generic name. We work around it
6521 #define tracepoint ust_tracepoint
6522 #include <ust/ust.h>
6525 extern int serialize_to_text (char *outbuf
, int bufsize
,
6526 const char *fmt
, va_list ap
);
6528 #define GDB_PROBE_NAME "gdb"
6530 /* We dynamically search for the UST symbols instead of linking them
6531 in. This lets the user decide if the application uses static
6532 tracepoints, instead of always pulling libust.so in. This vector
6533 holds pointers to all functions we care about. */
6537 int (*serialize_to_text
) (char *outbuf
, int bufsize
,
6538 const char *fmt
, va_list ap
);
6540 int (*ltt_probe_register
) (struct ltt_available_probe
*pdata
);
6541 int (*ltt_probe_unregister
) (struct ltt_available_probe
*pdata
);
6543 int (*ltt_marker_connect
) (const char *channel
, const char *mname
,
6545 int (*ltt_marker_disconnect
) (const char *channel
, const char *mname
,
6548 void (*marker_iter_start
) (struct marker_iter
*iter
);
6549 void (*marker_iter_next
) (struct marker_iter
*iter
);
6550 void (*marker_iter_stop
) (struct marker_iter
*iter
);
6551 void (*marker_iter_reset
) (struct marker_iter
*iter
);
6556 /* Cast through typeof to catch incompatible API changes. Since UST
6557 only builds with gcc, we can freely use gcc extensions here
6559 #define GET_UST_SYM(SYM) \
6562 if (ust_ops.SYM == NULL) \
6563 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6564 if (ust_ops.SYM == NULL) \
6568 #define USTF(SYM) ust_ops.SYM
6570 /* Get pointers to all libust.so functions we care about. */
6575 GET_UST_SYM (serialize_to_text
);
6577 GET_UST_SYM (ltt_probe_register
);
6578 GET_UST_SYM (ltt_probe_unregister
);
6579 GET_UST_SYM (ltt_marker_connect
);
6580 GET_UST_SYM (ltt_marker_disconnect
);
6582 GET_UST_SYM (marker_iter_start
);
6583 GET_UST_SYM (marker_iter_next
);
6584 GET_UST_SYM (marker_iter_stop
);
6585 GET_UST_SYM (marker_iter_reset
);
6591 /* Given an UST marker, return the matching gdb static tracepoint.
6592 The match is done by address. */
6594 static struct tracepoint
*
6595 ust_marker_to_static_tracepoint (const struct marker
*mdata
)
6597 struct tracepoint
*tpoint
;
6599 for (tpoint
= tracepoints
; tpoint
; tpoint
= tpoint
->next
)
6601 if (!tpoint
->enabled
|| tpoint
->type
!= static_tracepoint
)
6604 if (tpoint
->address
== (uintptr_t) mdata
->location
)
6611 /* The probe function we install on lttng/ust markers. Whenever a
6612 probed ust marker is hit, this function is called. This is similar
6613 to gdb_collect, only for static tracepoints, instead of fast
6617 gdb_probe (const struct marker
*mdata
, void *probe_private
,
6618 struct registers
*regs
, void *call_private
,
6619 const char *fmt
, va_list *args
)
6621 struct tracepoint
*tpoint
;
6622 struct static_tracepoint_ctx ctx
;
6624 /* Don't do anything until the trace run is completely set up. */
6627 trace_debug ("gdb_probe: not tracing\n");
6631 ctx
.base
.type
= static_tracepoint
;
6632 ctx
.regcache_initted
= 0;
6637 /* Wrap the regblock in a register cache (in the stack, we don't
6638 want to malloc here). */
6639 ctx
.regspace
= alloca (register_cache_size ());
6640 if (ctx
.regspace
== NULL
)
6642 trace_debug ("Trace buffer block allocation failed, skipping");
6646 tpoint
= ust_marker_to_static_tracepoint (mdata
);
6649 trace_debug ("gdb_probe: marker not known: "
6650 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6651 mdata
->location
, mdata
->channel
,
6652 mdata
->name
, mdata
->format
);
6656 ctx
.tpoint
= tpoint
;
6658 trace_debug ("gdb_probe: collecting marker: "
6659 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6660 mdata
->location
, mdata
->channel
,
6661 mdata
->name
, mdata
->format
);
6663 /* Test the condition if present, and collect if true. */
6664 if (tpoint
->cond
== NULL
6665 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6668 collect_data_at_tracepoint ((struct tracepoint_hit_ctx
*) &ctx
,
6669 tpoint
->address
, tpoint
);
6671 if (stopping_tracepoint
6672 || trace_buffer_is_full
6673 || expr_eval_result
!= expr_eval_no_error
)
6678 /* If there was a condition and it evaluated to false, the only
6679 way we would stop tracing is if there was an error during
6680 condition expression evaluation. */
6681 if (expr_eval_result
!= expr_eval_no_error
)
6686 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6687 static tracepoint string data. This is a string passed to the
6688 tracing library by the user, at the time of the tracepoint marker
6689 call. E.g., in the UST marker call:
6691 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6693 the collected data is "str FOOBAZ".
6697 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx
*ctx
,
6699 struct tracepoint
*tpoint
,
6700 struct traceframe
*tframe
)
6702 struct static_tracepoint_ctx
*umd
= (struct static_tracepoint_ctx
*) ctx
;
6703 unsigned char *bufspace
;
6706 unsigned short blocklen
;
6710 trace_debug ("Wanted to collect static trace data, "
6711 "but there's no static trace data");
6715 va_copy (copy
, *umd
->args
);
6716 size
= USTF(serialize_to_text
) (NULL
, 0, umd
->fmt
, copy
);
6719 trace_debug ("Want to collect ust data");
6721 /* 'S' + size + string */
6722 bufspace
= add_traceframe_block (tframe
,
6723 1 + sizeof (blocklen
) + size
+ 1);
6724 if (bufspace
== NULL
)
6726 trace_debug ("Trace buffer block allocation failed, skipping");
6730 /* Identify a static trace data block. */
6733 blocklen
= size
+ 1;
6734 memcpy (bufspace
+ 1, &blocklen
, sizeof (blocklen
));
6736 va_copy (copy
, *umd
->args
);
6737 USTF(serialize_to_text
) ((char *) bufspace
+ 1 + sizeof (blocklen
),
6738 size
+ 1, umd
->fmt
, copy
);
6741 trace_debug ("Storing static tracepoint data in regblock: %s",
6742 bufspace
+ 1 + sizeof (blocklen
));
6745 /* The probe to register with lttng/ust. */
6746 static struct ltt_available_probe gdb_ust_probe
=
6753 #endif /* HAVE_UST */
6754 #endif /* IN_PROCESS_AGENT */
6758 #include <sys/socket.h>
6761 #ifndef UNIX_PATH_MAX
6762 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6765 /* Where we put the socked used for synchronization. */
6766 #define SOCK_DIR P_tmpdir
6768 #endif /* HAVE_UST */
6770 #ifndef IN_PROCESS_AGENT
6775 gdb_ust_connect_sync_socket (int pid
)
6777 struct sockaddr_un addr
;
6779 char path
[UNIX_PATH_MAX
];
6781 res
= xsnprintf (path
, UNIX_PATH_MAX
, "%s/gdb_ust%d", SOCK_DIR
, pid
);
6782 if (res
>= UNIX_PATH_MAX
)
6784 trace_debug ("string overflow allocating socket name");
6788 res
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6791 warning ("error opening sync socket: %s\n", strerror (errno
));
6795 addr
.sun_family
= AF_UNIX
;
6797 res
= xsnprintf (addr
.sun_path
, UNIX_PATH_MAX
, "%s", path
);
6798 if (res
>= UNIX_PATH_MAX
)
6800 warning ("string overflow allocating socket name\n");
6805 res
= connect (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
6808 warning ("error connecting sync socket (%s): %s. "
6809 "Make sure the directory exists and that it is writable.",
6810 path
, strerror (errno
));
6818 /* Resume thread PTID. */
6821 resume_thread (ptid_t ptid
)
6823 struct thread_resume resume_info
;
6825 resume_info
.thread
= ptid
;
6826 resume_info
.kind
= resume_continue
;
6827 resume_info
.sig
= TARGET_SIGNAL_0
;
6828 (*the_target
->resume
) (&resume_info
, 1);
6831 /* Stop thread PTID. */
6834 stop_thread (ptid_t ptid
)
6836 struct thread_resume resume_info
;
6838 resume_info
.thread
= ptid
;
6839 resume_info
.kind
= resume_stop
;
6840 resume_info
.sig
= TARGET_SIGNAL_0
;
6841 (*the_target
->resume
) (&resume_info
, 1);
6844 /* Ask the in-process agent to run a command. Since we don't want to
6845 have to handle the IPA hitting breakpoints while running the
6846 command, we pause all threads, remove all breakpoints, and then set
6847 the helper thread re-running. We communicate with the helper
6848 thread by means of direct memory xfering, and a socket for
6852 run_inferior_command (char *cmd
)
6856 int pid
= ptid_get_pid (current_inferior
->entry
.id
);
6858 ptid_t ptid
= null_ptid
;
6860 trace_debug ("run_inferior_command: running: %s", cmd
);
6863 uninsert_all_breakpoints ();
6865 if (read_inferior_integer (ipa_sym_addrs
.addr_helper_thread_id
, &tid
))
6867 warning ("Error reading helper thread's id in lib");
6873 warning ("helper thread not initialized yet");
6877 if (write_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
6878 (unsigned char *) cmd
, strlen (cmd
) + 1))
6880 warning ("Error writing command");
6884 ptid
= ptid_build (pid
, tid
, 0);
6886 resume_thread (ptid
);
6888 fd
= gdb_ust_connect_sync_socket (pid
);
6894 trace_debug ("signalling helper thread");
6898 ret
= write (fd
, buf
, 1);
6899 } while (ret
== -1 && errno
== EINTR
);
6901 trace_debug ("waiting for helper thread's response");
6905 ret
= read (fd
, buf
, 1);
6906 } while (ret
== -1 && errno
== EINTR
);
6910 trace_debug ("helper thread's response received");
6915 /* Need to read response with the inferior stopped. */
6916 if (!ptid_equal (ptid
, null_ptid
))
6918 int was_non_stop
= non_stop
;
6919 struct target_waitstatus status
;
6923 mywait (ptid
, &status
, 0, 0);
6924 non_stop
= was_non_stop
;
6929 if (read_inferior_memory (ipa_sym_addrs
.addr_cmd_buf
,
6930 (unsigned char *) cmd
, CMD_BUF_SIZE
))
6932 warning ("Error reading command response");
6937 trace_debug ("run_inferior_command: response: %s", cmd
);
6941 reinsert_all_breakpoints ();
6947 #else /* HAVE_UST */
6950 run_inferior_command (char *cmd
)
6955 #endif /* HAVE_UST */
6957 #else /* !IN_PROCESS_AGENT */
6959 /* Thread ID of the helper thread. GDBserver reads this to know which
6960 is the help thread. This is an LWP id on Linux. */
6961 int helper_thread_id
;
6966 init_named_socket (const char *name
)
6969 struct sockaddr_un addr
;
6971 result
= fd
= socket (PF_UNIX
, SOCK_STREAM
, 0);
6974 warning ("socket creation failed: %s", strerror (errno
));
6978 addr
.sun_family
= AF_UNIX
;
6980 strncpy (addr
.sun_path
, name
, UNIX_PATH_MAX
);
6981 addr
.sun_path
[UNIX_PATH_MAX
- 1] = '\0';
6983 result
= access (name
, F_OK
);
6987 result
= unlink (name
);
6990 warning ("unlink failed: %s", strerror (errno
));
6994 warning ("socket %s already exists; overwriting", name
);
6997 result
= bind (fd
, (struct sockaddr
*) &addr
, sizeof (addr
));
7000 warning ("bind failed: %s", strerror (errno
));
7005 result
= listen (fd
, 1);
7008 warning ("listen: %s", strerror (errno
));
7017 gdb_ust_socket_init (void)
7020 char name
[UNIX_PATH_MAX
];
7022 result
= xsnprintf (name
, UNIX_PATH_MAX
, "%s/gdb_ust%d",
7023 SOCK_DIR
, getpid ());
7024 if (result
>= UNIX_PATH_MAX
)
7026 trace_debug ("string overflow allocating socket name");
7030 fd
= init_named_socket (name
);
7032 warning ("Error initializing named socket (%s) for communication with the "
7033 "ust helper thread. Check that directory exists and that it "
7034 "is writable.", name
);
7039 /* Return an hexstr version of the STR C string, fit for sending to
7043 cstr_to_hexstr (const char *str
)
7045 int len
= strlen (str
);
7046 char *hexstr
= xmalloc (len
* 2 + 1);
7047 convert_int_to_ascii ((gdb_byte
*) str
, hexstr
, len
);
7051 /* The next marker to be returned on a qTsSTM command. */
7052 static const struct marker
*next_st
;
7054 /* Returns the first known marker. */
7059 struct marker_iter iter
;
7061 USTF(marker_iter_reset
) (&iter
);
7062 USTF(marker_iter_start
) (&iter
);
7067 /* Returns the marker following M. */
7069 const struct marker
*
7070 next_marker (const struct marker
*m
)
7072 struct marker_iter iter
;
7074 USTF(marker_iter_reset
) (&iter
);
7075 USTF(marker_iter_start
) (&iter
);
7077 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7079 if (iter
.marker
== m
)
7081 USTF(marker_iter_next
) (&iter
);
7089 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7093 response_ust_marker (char *packet
, const struct marker
*st
)
7095 char *strid
, *format
, *tmp
;
7097 next_st
= next_marker (st
);
7099 tmp
= xmalloc (strlen (st
->channel
) + 1 +
7100 strlen (st
->name
) + 1);
7101 sprintf (tmp
, "%s/%s", st
->channel
, st
->name
);
7103 strid
= cstr_to_hexstr (tmp
);
7106 format
= cstr_to_hexstr (st
->format
);
7108 sprintf (packet
, "m%s:%s:%s",
7109 paddress ((uintptr_t) st
->location
),
7117 /* Return the first static tracepoint, and initialize the state
7118 machine that will iterate through all the static tracepoints. */
7121 cmd_qtfstm (char *packet
)
7123 trace_debug ("Returning first trace state variable definition");
7125 if (first_marker ())
7126 response_ust_marker (packet
, first_marker ());
7128 strcpy (packet
, "l");
7131 /* Return additional trace state variable definitions. */
7134 cmd_qtsstm (char *packet
)
7136 trace_debug ("Returning static tracepoint");
7139 response_ust_marker (packet
, next_st
);
7141 strcpy (packet
, "l");
7144 /* Disconnect the GDB probe from a marker at a given address. */
7147 unprobe_marker_at (char *packet
)
7151 struct marker_iter iter
;
7153 p
+= sizeof ("unprobe_marker_at:") - 1;
7155 p
= unpack_varlen_hex (p
, &address
);
7157 USTF(marker_iter_reset
) (&iter
);
7158 USTF(marker_iter_start
) (&iter
);
7159 for (; iter
.marker
!= NULL
; USTF(marker_iter_next
) (&iter
))
7160 if ((uintptr_t ) iter
.marker
->location
== address
)
7164 result
= USTF(ltt_marker_disconnect
) (iter
.marker
->channel
,
7168 warning ("could not disable marker %s/%s",
7169 iter
.marker
->channel
, iter
.marker
->name
);
7174 /* Connect the GDB probe to a marker at a given address. */
7177 probe_marker_at (char *packet
)
7181 struct marker_iter iter
;
7184 p
+= sizeof ("probe_marker_at:") - 1;
7186 p
= unpack_varlen_hex (p
, &address
);
7188 USTF(marker_iter_reset
) (&iter
);
7190 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7192 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7193 if ((uintptr_t ) m
->location
== address
)
7197 trace_debug ("found marker for address. "
7198 "ltt_marker_connect (marker = %s/%s)",
7199 m
->channel
, m
->name
);
7201 result
= USTF(ltt_marker_connect
) (m
->channel
, m
->name
,
7203 if (result
&& result
!= -EEXIST
)
7204 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7205 m
->channel
, m
->name
, -result
);
7209 sprintf (packet
, "E.could not connect marker: channel=%s, name=%s",
7210 m
->channel
, m
->name
);
7214 strcpy (packet
, "OK");
7218 sprintf (packet
, "E.no marker found at 0x%s", paddress (address
));
7223 cmd_qtstmat (char *packet
)
7227 struct marker_iter iter
;
7230 p
+= sizeof ("qTSTMat:") - 1;
7232 p
= unpack_varlen_hex (p
, &address
);
7234 USTF(marker_iter_reset
) (&iter
);
7236 for (USTF(marker_iter_start
) (&iter
), m
= iter
.marker
;
7238 USTF(marker_iter_next
) (&iter
), m
= iter
.marker
)
7239 if ((uintptr_t ) m
->location
== address
)
7241 response_ust_marker (packet
, m
);
7245 strcpy (packet
, "l");
7250 gdb_ust_thread (void *arg
)
7256 listen_fd
= gdb_ust_socket_init ();
7259 if (helper_thread_id
== 0)
7260 helper_thread_id
= syscall (SYS_gettid
);
7263 if (listen_fd
== -1)
7265 warning ("could not create sync socket\n");
7272 struct sockaddr_un sockaddr
;
7277 tmp
= sizeof (sockaddr
);
7281 fd
= accept (listen_fd
, &sockaddr
, &tmp
);
7283 /* It seems an ERESTARTSYS can escape out of accept. */
7284 while (fd
== -512 || (fd
== -1 && errno
== EINTR
));
7288 warning ("Accept returned %d, error: %s\n",
7289 fd
, strerror (errno
));
7295 ret
= read (fd
, buf
, 1);
7296 } while (ret
== -1 && errno
== EINTR
);
7300 warning ("reading socket (fd=%d) failed with %s",
7301 fd
, strerror (errno
));
7308 if (strcmp ("qTfSTM", cmd_buf
) == 0)
7310 cmd_qtfstm (cmd_buf
);
7312 else if (strcmp ("qTsSTM", cmd_buf
) == 0)
7314 cmd_qtsstm (cmd_buf
);
7316 else if (strncmp ("unprobe_marker_at:",
7318 sizeof ("unprobe_marker_at:") - 1) == 0)
7320 unprobe_marker_at (cmd_buf
);
7322 else if (strncmp ("probe_marker_at:",
7324 sizeof ("probe_marker_at:") - 1) == 0)
7326 probe_marker_at (cmd_buf
);
7328 else if (strncmp ("qTSTMat:",
7330 sizeof ("qTSTMat:") - 1) == 0)
7332 cmd_qtstmat (cmd_buf
);
7334 else if (strcmp (cmd_buf
, "help") == 0)
7336 strcpy (cmd_buf
, "for help, press F1\n");
7339 strcpy (cmd_buf
, "");
7363 /* We want the helper thread to be as transparent as possible, so
7364 have it inherit an all-signals-blocked mask. */
7366 sigfillset (&new_mask
);
7367 res
= pthread_sigmask (SIG_SETMASK
, &new_mask
, &orig_mask
);
7369 fatal ("pthread_sigmask (1) failed: %s", strerror (res
));
7371 res
= pthread_create (&thread
,
7376 res
= pthread_sigmask (SIG_SETMASK
, &orig_mask
, NULL
);
7378 fatal ("pthread_sigmask (2) failed: %s", strerror (res
));
7380 while (helper_thread_id
== 0)
7383 USTF(ltt_probe_register
) (&gdb_ust_probe
);
7386 #endif /* HAVE_UST */
7388 #include <sys/mman.h>
7391 IP_AGENT_EXPORT
char *gdb_tp_heap_buffer
;
7392 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer
;
7393 IP_AGENT_EXPORT
char *gdb_jump_pad_buffer_end
;
7395 static void __attribute__ ((constructor
))
7396 initialize_tracepoint_ftlib (void)
7398 initialize_tracepoint ();
7405 #endif /* IN_PROCESS_AGENT */
7408 tsv_get_timestamp (void)
7412 if (gettimeofday (&tv
, 0) != 0)
7415 return (LONGEST
) tv
.tv_sec
* 1000000 + tv
.tv_usec
;
7419 initialize_tracepoint (void)
7421 /* There currently no way to change the buffer size. */
7422 const int sizeOfBuffer
= 5 * 1024 * 1024;
7423 unsigned char *buf
= xmalloc (sizeOfBuffer
);
7424 init_trace_buffer (buf
, sizeOfBuffer
);
7426 /* Wire trace state variable 1 to be the timestamp. This will be
7427 uploaded to GDB upon connection and become one of its trace state
7428 variables. (In case you're wondering, if GDB already has a trace
7429 variable numbered 1, it will be renumbered.) */
7430 create_trace_state_variable (1, 0);
7431 set_trace_state_variable_name (1, "trace_timestamp");
7432 set_trace_state_variable_getter (1, tsv_get_timestamp
);
7434 #ifdef IN_PROCESS_AGENT
7437 pagesize
= sysconf (_SC_PAGE_SIZE
);
7441 gdb_tp_heap_buffer
= xmalloc (5 * 1024 * 1024);
7443 /* Allocate scratch buffer aligned on a page boundary. */
7444 gdb_jump_pad_buffer
= memalign (pagesize
, pagesize
* 20);
7445 gdb_jump_pad_buffer_end
= gdb_jump_pad_buffer
+ pagesize
* 20;
7447 /* Make it writable and executable. */
7448 if (mprotect (gdb_jump_pad_buffer
, pagesize
* 20,
7449 PROT_READ
| PROT_WRITE
| PROT_EXEC
) != 0)
7451 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
7452 gdb_jump_pad_buffer
, pagesize
* 20, strerror (errno
));
7455 initialize_low_tracepoint ();