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)
325 if (all_tracepoint_symbols_looked_up)
328 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
331 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
333 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
336 fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
341 all_tracepoint_symbols_looked_up = 1;
346 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
347 of the "stop_tracing" function. When this breakpoint is hit,
348 tracing stopped in the IPA for some reason. E.g., due to
349 tracepoint reaching the pass count, hitting conditional expression
350 evaluation error, etc.
352 The IPA's trace buffer is never in circular tracing mode: instead,
353 GDBserver's is, and whenever the in-process buffer fills, it calls
354 "flush_trace_buffer", which triggers an internal breakpoint.
355 GDBserver reacts to this breakpoint by pulling the meanwhile
356 collected data. Old frames discarding is always handled on the
359 #ifdef IN_PROCESS_AGENT
360 int debug_threads = 0;
363 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
365 memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
369 /* Call this in the functions where GDBserver places a breakpoint, so
370 that the compiler doesn't try to be clever and skip calling the
371 function at all. This is necessary, even if we tell the compiler
372 to not inline said functions. */
374 #if defined(__GNUC__)
375 # define UNKNOWN_SIDE_EFFECTS() asm ("")
377 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
380 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
383 /* GDBserver places breakpoint here. */
384 UNKNOWN_SIDE_EFFECTS();
387 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
388 flush_trace_buffer (void)
390 /* GDBserver places breakpoint here. */
391 UNKNOWN_SIDE_EFFECTS();
396 #ifndef IN_PROCESS_AGENT
398 tracepoint_handler (CORE_ADDR address)
400 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
405 /* Breakpoint at "stop_tracing" in the inferior lib. */
406 struct breakpoint *stop_tracing_bkpt;
407 static int stop_tracing_handler (CORE_ADDR);
409 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
410 struct breakpoint *flush_trace_buffer_bkpt;
411 static int flush_trace_buffer_handler (CORE_ADDR);
413 static void download_tracepoints (void);
414 static void download_trace_state_variables (void);
415 static void upload_fast_traceframes (void);
417 static int run_inferior_command (char *cmd);
420 read_inferior_integer (CORE_ADDR symaddr, int *val)
422 return read_inferior_memory (symaddr, (unsigned char *) val,
427 read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
429 return read_inferior_memory (symaddr, (unsigned char *) val,
434 read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
436 void *pval = (void *) (uintptr_t) val;
439 ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
440 *val = (uintptr_t) pval;
445 write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
447 void *pval = (void *) (uintptr_t) val;
448 return write_inferior_memory (symaddr,
449 (unsigned char *) &pval, sizeof (pval));
453 write_inferior_integer (CORE_ADDR symaddr, int val)
455 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
459 write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
461 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
466 /* This enum must exactly match what is documented in
467 gdb/doc/agentexpr.texi, including all the numerical values. */
471 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
472 gdb_agent_op_ ## NAME = VALUE,
478 static const char *gdb_agent_op_names [gdb_agent_op_last] =
481 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
486 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
489 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
498 unsigned char *bytes;
501 /* Base action. Concrete actions inherit this. */
503 struct tracepoint_action
508 /* An 'M' (collect memory) action. */
509 struct collect_memory_action
511 struct tracepoint_action base;
518 /* An 'R' (collect registers) action. */
520 struct collect_registers_action
522 struct tracepoint_action base;
525 /* An 'X' (evaluate expression) action. */
527 struct eval_expr_action
529 struct tracepoint_action base;
531 struct agent_expr *expr;
534 /* An 'L' (collect static trace data) action. */
535 struct collect_static_trace_data_action
537 struct tracepoint_action base;
540 /* This structure describes a piece of the source-level definition of
541 the tracepoint. The contents are not interpreted by the target,
542 but preserved verbatim for uploading upon reconnection. */
546 /* The type of string, such as "cond" for a conditional. */
549 /* The source-level string itself. For the sake of target
550 debugging, we store it in plaintext, even though it is always
551 transmitted in hex. */
554 /* Link to the next one in the list. We link them in the order
555 received, in case some make up an ordered list of commands or
557 struct source_string *next;
562 /* Trap based tracepoint. */
565 /* A fast tracepoint implemented with a jump instead of a trap. */
568 /* A static tracepoint, implemented by a program call into a tracing
573 struct tracepoint_hit_ctx;
575 typedef enum eval_result_type (*condfn) (struct tracepoint_hit_ctx *,
578 /* The definition of a tracepoint. */
580 /* Tracepoints may have multiple locations, each at a different
581 address. This can occur with optimizations, template
582 instantiation, etc. Since the locations may be in different
583 scopes, the conditions and actions may be different for each
584 location. Our target version of tracepoints is more like GDB's
585 notion of "breakpoint locations", but we have almost nothing that
586 is not per-location, so we bother having two kinds of objects. The
587 key consequence is that numbers are not unique, and that it takes
588 both number and address to identify a tracepoint uniquely. */
592 /* The number of the tracepoint, as specified by GDB. Several
593 tracepoint objects here may share a number. */
596 /* Address at which the tracepoint is supposed to trigger. Several
597 tracepoints may share an address. */
600 /* Tracepoint type. */
601 enum tracepoint_type type;
603 /* True if the tracepoint is currently enabled. */
606 /* The number of single steps that will be performed after each
610 /* The number of times the tracepoint may be hit before it will
611 terminate the entire tracing run. */
614 /* Pointer to the agent expression that is the tracepoint's
615 conditional, or NULL if the tracepoint is unconditional. */
616 struct agent_expr *cond;
618 /* The list of actions to take when the tracepoint triggers. */
620 struct tracepoint_action **actions;
622 /* Count of the times we've hit this tracepoint during the run.
623 Note that while-stepping steps are not counted as "hits". */
626 CORE_ADDR compiled_cond;
628 /* Link to the next tracepoint in the list. */
629 struct tracepoint *next;
631 #ifndef IN_PROCESS_AGENT
632 /* The list of actions to take when the tracepoint triggers, in
633 string/packet form. */
636 /* The collection of strings that describe the tracepoint as it was
637 entered into GDB. These are not used by the target, but are
638 reported back to GDB upon reconnection. */
639 struct source_string *source_strings;
641 /* The number of bytes displaced by fast tracepoints. It may subsume
642 multiple instructions, for multi-byte fast tracepoints. This
643 field is only valid for fast tracepoints. */
646 /* Only for fast tracepoints. */
647 CORE_ADDR obj_addr_on_target;
649 /* Address range where the original instruction under a fast
650 tracepoint was relocated to. (_end is actually one byte past
652 CORE_ADDR adjusted_insn_addr;
653 CORE_ADDR adjusted_insn_addr_end;
655 /* The address range of the piece of the jump pad buffer that was
656 assigned to this fast tracepoint. (_end is actually one byte
659 CORE_ADDR jump_pad_end;
661 /* The list of actions to take while in a stepping loop. These
662 fields are only valid for patch-based tracepoints. */
663 int num_step_actions;
664 struct tracepoint_action **step_actions;
665 /* Same, but in string/packet form. */
666 char **step_actions_str;
668 /* Handle returned by the breakpoint or tracepoint module when we
669 inserted the trap or jump, or hooked into a static tracepoint.
670 NULL if we haven't inserted it yet. */
676 #ifndef IN_PROCESS_AGENT
678 /* Given `while-stepping', a thread may be collecting data for more
679 than one tracepoint simultaneously. On the other hand, the same
680 tracepoint with a while-stepping action may be hit by more than one
681 thread simultaneously (but not quite, each thread could be handling
682 a different step). Each thread holds a list of these objects,
683 representing the current step of each while-stepping action being
688 struct wstep_state *next;
690 /* The tracepoint number. */
692 /* The tracepoint's address. */
693 CORE_ADDR tp_address;
695 /* The number of the current step in this 'while-stepping'
702 /* The linked list of all tracepoints. Marked explicitly as used as
703 the in-process library doesn't use it for the fast tracepoints
705 IP_AGENT_EXPORT struct tracepoint *tracepoints ATTR_USED;
707 #ifndef IN_PROCESS_AGENT
709 /* Pointer to the last tracepoint in the list, new tracepoints are
710 linked in at the end. */
712 static struct tracepoint *last_tracepoint;
715 /* The first tracepoint to exceed its pass count. */
717 IP_AGENT_EXPORT struct tracepoint *stopping_tracepoint;
719 /* True if the trace buffer is full or otherwise no longer usable. */
721 IP_AGENT_EXPORT int trace_buffer_is_full;
723 /* Enumeration of the different kinds of things that can happen during
724 agent expression evaluation. */
726 enum eval_result_type
729 expr_eval_empty_expression,
730 expr_eval_empty_stack,
731 expr_eval_stack_overflow,
732 expr_eval_stack_underflow,
733 expr_eval_unhandled_opcode,
734 expr_eval_unrecognized_opcode,
735 expr_eval_divide_by_zero,
736 expr_eval_invalid_goto
739 static enum eval_result_type expr_eval_result = expr_eval_no_error;
741 #ifndef IN_PROCESS_AGENT
743 static const char *eval_result_names[] =
745 "terror:in the attic", /* this should never be reported */
746 "terror:empty expression",
747 "terror:empty stack",
748 "terror:stack overflow",
749 "terror:stack underflow",
750 "terror:unhandled opcode",
751 "terror:unrecognized opcode",
752 "terror:divide by zero"
757 /* The tracepoint in which the error occurred. */
759 static struct tracepoint *error_tracepoint;
761 struct trace_state_variable
763 /* This is the name of the variable as used in GDB. The target
764 doesn't use the name, but needs to have it for saving and
765 reconnection purposes. */
768 /* This number identifies the variable uniquely. Numbers may be
769 assigned either by the target (in the case of builtin variables),
770 or by GDB, and are presumed unique during the course of a trace
774 /* The variable's initial value, a 64-bit signed integer always. */
775 LONGEST initial_value;
777 /* The variable's value, a 64-bit signed integer always. */
780 /* Pointer to a getter function, used to supply computed values. */
781 LONGEST (*getter) (void);
783 /* Link to the next variable. */
784 struct trace_state_variable *next;
787 /* Linked list of all trace state variables. */
789 #ifdef IN_PROCESS_AGENT
790 struct trace_state_variable *alloced_trace_state_variables;
793 IP_AGENT_EXPORT struct trace_state_variable *trace_state_variables;
795 /* The results of tracing go into a fixed-size space known as the
796 "trace buffer". Because usage follows a limited number of
797 patterns, we manage it ourselves rather than with malloc. Basic
798 rules are that we create only one trace frame at a time, each is
799 variable in size, they are never moved once created, and we only
800 discard if we are doing a circular buffer, and then only the oldest
801 ones. Each trace frame includes its own size, so we don't need to
802 link them together, and the trace frame number is relative to the
803 first one, so we don't need to record numbers. A trace frame also
804 records the number of the tracepoint that created it. The data
805 itself is a series of blocks, each introduced by a single character
806 and with a defined format. Each type of block has enough
807 type/length info to allow scanners to jump quickly from one block
808 to the next without reading each byte in the block. */
810 /* Trace buffer management would be simple - advance a free pointer
811 from beginning to end, then stop - were it not for the circular
812 buffer option, which is a useful way to prevent a trace run from
813 stopping prematurely because the buffer filled up. In the circular
814 case, the location of the first trace frame (trace_buffer_start)
815 moves as old trace frames are discarded. Also, since we grow trace
816 frames incrementally as actions are performed, we wrap around to
817 the beginning of the trace buffer. This is per-block, so each
818 block within a trace frame remains contiguous. Things get messy
819 when the wrapped-around trace frame is the one being discarded; the
820 free space ends up in two parts at opposite ends of the buffer. */
823 # if defined(__GNUC__)
824 # define ATTR_PACKED __attribute__ ((packed))
826 # define ATTR_PACKED /* nothing */
830 /* The data collected at a tracepoint hit. This object should be as
831 small as possible, since there may be a great many of them. We do
832 not need to keep a frame number, because they are all sequential
833 and there are no deletions; so the Nth frame in the buffer is
834 always frame number N. */
838 /* Number of the tracepoint that collected this traceframe. A value
839 of 0 indicates the current end of the trace buffer. We make this
840 a 16-bit field because it's never going to happen that GDB's
841 numbering of tracepoints reaches 32,000. */
844 /* The size of the data in this trace frame. We limit this to 32
845 bits, even on a 64-bit target, because it's just implausible that
846 one is validly going to collect 4 gigabytes of data at a single
848 unsigned int data_size : 32;
850 /* The base of the trace data, which is contiguous from this point. */
851 unsigned char data[0];
855 /* The traceframe to be used as the source of data to send back to
856 GDB. A value of -1 means to get data from the live program. */
858 int current_traceframe = -1;
860 /* This flag is true if the trace buffer is circular, meaning that
861 when it fills, the oldest trace frames are discarded in order to
864 #ifndef IN_PROCESS_AGENT
865 static int circular_trace_buffer;
868 /* Pointer to the block of memory that traceframes all go into. */
870 static unsigned char *trace_buffer_lo;
872 /* Pointer to the end of the trace buffer, more precisely to the byte
873 after the end of the buffer. */
875 static unsigned char *trace_buffer_hi;
877 /* Control structure holding the read/write/etc. pointers into the
878 trace buffer. We need more than one of these to implement a
879 transaction-like mechanism to garantees that both GDBserver and the
880 in-process agent can try to change the trace buffer
883 struct trace_buffer_control
885 /* Pointer to the first trace frame in the buffer. In the
886 non-circular case, this is equal to trace_buffer_lo, otherwise it
887 moves around in the buffer. */
888 unsigned char *start;
890 /* Pointer to the free part of the trace buffer. Note that we clear
891 several bytes at and after this pointer, so that traceframe
892 scans/searches terminate properly. */
895 /* Pointer to the byte after the end of the free part. Note that
896 this may be smaller than trace_buffer_free in the circular case,
897 and means that the free part is in two pieces. Initially it is
898 equal to trace_buffer_hi, then is generally equivalent to
899 trace_buffer_start. */
900 unsigned char *end_free;
902 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
903 this is the point at which the trace data breaks, and resumes at
908 /* Same as above, to be used by GDBserver when updating the in-process
910 struct ipa_trace_buffer_control
919 /* We have possibly both GDBserver and an inferior thread accessing
920 the same IPA trace buffer memory. The IPA is the producer (tries
921 to put new frames in the buffer), while GDBserver occasionally
922 consumes them, that is, flushes the IPA's buffer into its own
923 buffer. Both sides need to update the trace buffer control
924 pointers (current head, tail, etc.). We can't use a global lock to
925 synchronize the accesses, as otherwise we could deadlock GDBserver
926 (if the thread holding the lock stops for a signal, say). So
927 instead of that, we use a transaction scheme where GDBserver writes
928 always prevail over the IPAs writes, and, we have the IPA detect
929 the commit failure/overwrite, and retry the whole attempt. This is
930 mainly implemented by having a global token object that represents
931 who wrote last to the buffer control structure. We need to freeze
932 any inferior writing to the buffer while GDBserver touches memory,
933 so that the inferior can correctly detect that GDBserver had been
934 there, otherwise, it could mistakingly think its commit was
935 successful; that's implemented by simply having GDBserver set a
936 breakpoint the inferior hits if it is the critical region.
938 There are three cycling trace buffer control structure copies
939 (buffer head, tail, etc.), with the token object including an index
940 indicating which is current live copy. The IPA tentatively builds
941 an updated copy in a non-current control structure, while GDBserver
942 always clobbers the current version directly. The IPA then tries
943 to atomically "commit" its version; if GDBserver clobbered the
944 structure meanwhile, that will fail, and the IPA restarts the
947 Listing the step in further detail, we have:
949 In-process agent (producer):
951 - passes by `about_to_request_buffer_space' breakpoint/lock
953 - reads current token, extracts current trace buffer control index,
954 and starts tentatively updating the rightmost one (0->1, 1->2,
955 2->0). Note that only one inferior thread is executing this code
956 at any given time, due to an outer lock in the jump pads.
958 - updates counters, and tries to commit the token.
960 - passes by second `about_to_request_buffer_space' breakpoint/lock,
961 leaving the sync region.
963 - checks if the update was effective.
965 - if trace buffer was found full, hits flush_trace_buffer
966 breakpoint, and restarts later afterwards.
968 GDBserver (consumer):
970 - sets `about_to_request_buffer_space' breakpoint/lock.
972 - updates the token unconditionally, using the current buffer
973 control index, since it knows that the IP agent always writes to
974 the rightmost, and due to the breakpoint, at most one IP thread
975 can try to update the trace buffer concurrently to GDBserver, so
976 there will be no danger of trace buffer control index wrap making
977 the IPA write to the same index as GDBserver.
979 - flushes the IP agent's trace buffer completely, and updates the
980 current trace buffer control structure. GDBserver *always* wins.
982 - removes the `about_to_request_buffer_space' breakpoint.
984 The token is stored in the `trace_buffer_ctrl_curr' variable.
985 Internally, it's bits are defined as:
987 |-------------+-----+-------------+--------+-------------+--------------|
988 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
989 |-------------+-----+-------------+--------+-------------+--------------|
990 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
991 |-------------+-----+-------------+--------+-------------+--------------|
993 GSB - GDBserver Stamp Bit
994 PC - Previous Counter
996 TBCI - Trace Buffer Control Index
999 An IPA update of `trace_buffer_ctrl_curr' does:
1001 - read CC from the current token, save as PC.
1003 - atomically tries to write PC+1,CC
1005 A GDBserver update of `trace_buffer_ctrl_curr' does:
1007 - reads PC and CC from the current token.
1012 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1013 for the counters described below. The cleared bits are used to
1014 hold the index of the items of the `trace_buffer_ctrl' array that
1016 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1018 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1019 counter, and the `current' counter. */
1021 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1022 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1024 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1025 always stamps this bit as set. */
1026 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1028 #ifdef IN_PROCESS_AGENT
1029 IP_AGENT_EXPORT struct trace_buffer_control trace_buffer_ctrl[3];
1030 IP_AGENT_EXPORT unsigned int trace_buffer_ctrl_curr;
1032 # define TRACE_BUFFER_CTRL_CURR \
1033 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1037 /* The GDBserver side agent only needs one instance of this object, as
1038 it doesn't need to sync with itself. Define it as array anyway so
1039 that the rest of the code base doesn't need to care for the
1041 struct trace_buffer_control trace_buffer_ctrl[1];
1042 # define TRACE_BUFFER_CTRL_CURR 0
1045 /* These are convenience macros used to access the current trace
1046 buffer control in effect. */
1047 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1048 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1049 #define trace_buffer_end_free \
1050 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1051 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1054 /* Macro that returns a pointer to the first traceframe in the buffer. */
1056 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1058 /* Macro that returns a pointer to the next traceframe in the buffer.
1059 If the computed location is beyond the wraparound point, subtract
1060 the offset of the wraparound. */
1062 #define NEXT_TRACEFRAME_1(TF) \
1063 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1065 #define NEXT_TRACEFRAME(TF) \
1066 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1067 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1068 ? (trace_buffer_wrap - trace_buffer_lo) \
1071 /* The difference between these counters represents the total number
1072 of complete traceframes present in the trace buffer. The IP agent
1073 writes to the write count, GDBserver writes to read count. */
1075 IP_AGENT_EXPORT unsigned int traceframe_write_count;
1076 IP_AGENT_EXPORT unsigned int traceframe_read_count;
1078 /* Convenience macro. */
1080 #define traceframe_count \
1081 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1083 /* The count of all traceframes created in the current run, including
1084 ones that were discarded to make room. */
1086 IP_AGENT_EXPORT int traceframes_created;
1088 #ifndef IN_PROCESS_AGENT
1090 /* Read-only regions are address ranges whose contents don't change,
1091 and so can be read from target memory even while looking at a trace
1092 frame. Without these, disassembly for instance will likely fail,
1093 because the program code is not usually collected into a trace
1094 frame. This data structure does not need to be very complicated or
1095 particularly efficient, it's only going to be used occasionally,
1096 and only by some commands. */
1098 struct readonly_region
1100 /* The bounds of the region. */
1101 CORE_ADDR start, end;
1103 /* Link to the next one. */
1104 struct readonly_region *next;
1107 /* Linked list of readonly regions. This list stays in effect from
1108 one tstart to the next. */
1110 static struct readonly_region *readonly_regions;
1114 /* The global that controls tracing overall. */
1116 IP_AGENT_EXPORT int tracing;
1118 #ifndef IN_PROCESS_AGENT
1120 /* Controls whether tracing should continue after GDB disconnects. */
1122 int disconnected_tracing;
1124 /* The reason for the last tracing run to have stopped. We initialize
1125 to a distinct string so that GDB can distinguish between "stopped
1126 after running" and "stopped because never run" cases. */
1128 static const char *tracing_stop_reason = "tnotrun";
1130 static int tracing_stop_tpnum;
1134 /* Functions local to this file. */
1136 /* Base "class" for tracepoint type specific data to be passed down to
1137 collect_data_at_tracepoint. */
1138 struct tracepoint_hit_ctx
1140 enum tracepoint_type type;
1143 #ifdef IN_PROCESS_AGENT
1145 /* Fast/jump tracepoint specific data to be passed down to
1146 collect_data_at_tracepoint. */
1147 struct fast_tracepoint_ctx
1149 struct tracepoint_hit_ctx base;
1151 struct regcache regcache;
1152 int regcache_initted;
1153 unsigned char *regspace;
1155 unsigned char *regs;
1156 struct tracepoint *tpoint;
1159 /* Static tracepoint specific data to be passed down to
1160 collect_data_at_tracepoint. */
1161 struct static_tracepoint_ctx
1163 struct tracepoint_hit_ctx base;
1165 /* The regcache corresponding to the registers state at the time of
1166 the tracepoint hit. Initialized lazily, from REGS. */
1167 struct regcache regcache;
1168 int regcache_initted;
1170 /* The buffer space REGCACHE above uses. We use a separate buffer
1171 instead of letting the regcache malloc for both signal safety and
1172 performance reasons; this is allocated on the stack instead. */
1173 unsigned char *regspace;
1175 /* The register buffer as passed on by lttng/ust. */
1176 struct registers *regs;
1178 /* The "printf" formatter and the args the user passed to the marker
1179 call. We use this to be able to collect "static trace data"
1184 /* The GDB tracepoint matching the probed marker that was "hit". */
1185 struct tracepoint *tpoint;
1190 /* Static tracepoint specific data to be passed down to
1191 collect_data_at_tracepoint. */
1192 struct trap_tracepoint_ctx
1194 struct tracepoint_hit_ctx base;
1196 struct regcache *regcache;
1201 #ifndef IN_PROCESS_AGENT
1202 static struct agent_expr *parse_agent_expr (char **actparm);
1203 static char *unparse_agent_expr (struct agent_expr *aexpr);
1205 static enum eval_result_type eval_agent_expr (struct tracepoint_hit_ctx *ctx,
1206 struct traceframe *tframe,
1207 struct agent_expr *aexpr,
1210 static int agent_mem_read (struct traceframe *tframe,
1211 unsigned char *to, CORE_ADDR from, ULONGEST len);
1212 static int agent_mem_read_string (struct traceframe *tframe,
1213 unsigned char *to, CORE_ADDR from,
1215 static int agent_tsv_read (struct traceframe *tframe, int n);
1217 #ifndef IN_PROCESS_AGENT
1218 static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1219 static int traceframe_read_tsv (int num, LONGEST *val);
1222 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1223 struct tracepoint *tpoint);
1225 #ifndef IN_PROCESS_AGENT
1226 static void clear_readonly_regions (void);
1227 static void clear_installed_tracepoints (void);
1230 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1232 struct tracepoint *tpoint);
1233 #ifndef IN_PROCESS_AGENT
1234 static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1236 struct tracepoint *tpoint, int current_step);
1237 static void compile_tracepoint_condition (struct tracepoint *tpoint,
1238 CORE_ADDR *jump_entry);
1240 static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1242 struct tracepoint *tpoint,
1243 struct traceframe *tframe,
1244 struct tracepoint_action *taction);
1246 #ifndef IN_PROCESS_AGENT
1247 static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1249 static void install_tracepoint (struct tracepoint *, char *own_buf);
1250 static void download_tracepoint (struct tracepoint *);
1251 static int install_fast_tracepoint (struct tracepoint *);
1254 #if defined(__GNUC__)
1255 # define memory_barrier() asm volatile ("" : : : "memory")
1257 # define memory_barrier() do {} while (0)
1260 /* We only build the IPA if this builtin is supported, and there are
1261 no uses of this in GDBserver itself, so we're safe in defining this
1263 #define cmpxchg(mem, oldval, newval) \
1264 __sync_val_compare_and_swap (mem, oldval, newval)
1266 /* The size in bytes of the buffer used to talk to the IPA helper
1268 #define CMD_BUF_SIZE 1024
1270 /* Record that an error occurred during expression evaluation. */
1273 record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1274 enum eval_result_type rtype)
1276 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1277 tpoint->number, paddress (tpoint->address), which, rtype);
1279 #ifdef IN_PROCESS_AGENT
1280 /* Only record the first error we get. */
1281 if (cmpxchg (&expr_eval_result,
1283 rtype) != expr_eval_no_error)
1286 if (expr_eval_result != expr_eval_no_error)
1290 error_tracepoint = tpoint;
1293 /* Trace buffer management. */
1296 clear_trace_buffer (void)
1298 trace_buffer_start = trace_buffer_lo;
1299 trace_buffer_free = trace_buffer_lo;
1300 trace_buffer_end_free = trace_buffer_hi;
1301 trace_buffer_wrap = trace_buffer_hi;
1302 /* A traceframe with zeroed fields marks the end of trace data. */
1303 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1304 ((struct traceframe *) trace_buffer_free)->data_size = 0;
1305 traceframe_read_count = traceframe_write_count = 0;
1306 traceframes_created = 0;
1309 #ifndef IN_PROCESS_AGENT
1312 clear_inferior_trace_buffer (void)
1314 CORE_ADDR ipa_trace_buffer_lo;
1315 CORE_ADDR ipa_trace_buffer_hi;
1316 struct traceframe ipa_traceframe = { 0 };
1317 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1319 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1320 &ipa_trace_buffer_lo);
1321 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1322 &ipa_trace_buffer_hi);
1324 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1325 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1326 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1327 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1329 /* A traceframe with zeroed fields marks the end of trace data. */
1330 write_inferior_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1331 (unsigned char *) &ipa_trace_buffer_ctrl,
1332 sizeof (ipa_trace_buffer_ctrl));
1334 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1336 /* A traceframe with zeroed fields marks the end of trace data. */
1337 write_inferior_memory (ipa_trace_buffer_lo,
1338 (unsigned char *) &ipa_traceframe,
1339 sizeof (ipa_traceframe));
1341 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1342 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1343 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1349 init_trace_buffer (unsigned char *buf, int bufsize)
1351 trace_buffer_lo = buf;
1352 trace_buffer_hi = trace_buffer_lo + bufsize;
1354 clear_trace_buffer ();
1357 #ifdef IN_PROCESS_AGENT
1359 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
1360 about_to_request_buffer_space (void)
1362 /* GDBserver places breakpoint here while it goes about to flush
1363 data at random times. */
1364 UNKNOWN_SIDE_EFFECTS();
1369 /* Carve out a piece of the trace buffer, returning NULL in case of
1373 trace_buffer_alloc (size_t amt)
1375 unsigned char *rslt;
1376 struct trace_buffer_control *tbctrl;
1378 #ifdef IN_PROCESS_AGENT
1379 unsigned int prev, prev_filtered;
1380 unsigned int commit_count;
1381 unsigned int commit;
1382 unsigned int readout;
1384 struct traceframe *oldest;
1385 unsigned char *new_start;
1388 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1389 (long) amt, (long) sizeof (struct traceframe));
1391 /* Account for the EOB marker. */
1392 amt += sizeof (struct traceframe);
1394 #ifdef IN_PROCESS_AGENT
1398 /* Read the current token and extract the index to try to write to,
1399 storing it in CURR. */
1400 prev = trace_buffer_ctrl_curr;
1401 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1402 curr = prev_filtered + 1;
1406 about_to_request_buffer_space ();
1408 /* Start out with a copy of the current state. GDBserver may be
1409 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1410 be able to commit anyway if that happens. */
1411 trace_buffer_ctrl[curr]
1412 = trace_buffer_ctrl[prev_filtered];
1413 trace_debug ("trying curr=%u", curr);
1415 /* The GDBserver's agent doesn't need all that syncing, and always
1416 updates TCB 0 (there's only one, mind you). */
1419 tbctrl = &trace_buffer_ctrl[curr];
1421 /* Offsets are easier to grok for debugging than raw addresses,
1422 especially for the small trace buffer sizes that are useful for
1424 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1426 (int) (tbctrl->start - trace_buffer_lo),
1427 (int) (tbctrl->free - trace_buffer_lo),
1428 (int) (tbctrl->end_free - trace_buffer_lo),
1429 (int) (tbctrl->wrap - trace_buffer_lo),
1430 (int) (trace_buffer_hi - trace_buffer_lo));
1432 /* The algorithm here is to keep trying to get a contiguous block of
1433 the requested size, possibly discarding older traceframes to free
1434 up space. Since free space might come in one or two pieces,
1435 depending on whether discarded traceframes wrapped around at the
1436 high end of the buffer, we test both pieces after each
1440 /* First, if we have two free parts, try the upper one first. */
1441 if (tbctrl->end_free < tbctrl->free)
1443 if (tbctrl->free + amt <= trace_buffer_hi)
1444 /* We have enough in the upper part. */
1448 /* Our high part of free space wasn't enough. Give up
1449 on it for now, set wraparound. We will recover the
1450 space later, if/when the wrapped-around traceframe is
1452 trace_debug ("Upper part too small, setting wraparound");
1453 tbctrl->wrap = tbctrl->free;
1454 tbctrl->free = trace_buffer_lo;
1458 /* The normal case. */
1459 if (tbctrl->free + amt <= tbctrl->end_free)
1462 #ifdef IN_PROCESS_AGENT
1463 /* The IP Agent's buffer is always circular. It isn't used
1464 currently, but `circular_trace_buffer' could represent
1465 GDBserver's mode. If we didn't find space, ask GDBserver to
1468 flush_trace_buffer ();
1472 trace_debug ("gdbserver flushed buffer, retrying");
1476 /* GDBserver cancelled the tracing. Bail out as well. */
1479 /* If we're here, then neither part is big enough, and
1480 non-circular trace buffers are now full. */
1481 if (!circular_trace_buffer)
1483 trace_debug ("Not enough space in the trace buffer");
1487 trace_debug ("Need more space in the trace buffer");
1489 /* If we have a circular buffer, we can try discarding the
1490 oldest traceframe and see if that helps. */
1491 oldest = FIRST_TRACEFRAME ();
1492 if (oldest->tpnum == 0)
1494 /* Not good; we have no traceframes to free. Perhaps we're
1495 asking for a block that is larger than the buffer? In
1496 any case, give up. */
1497 trace_debug ("No traceframes to discard");
1501 /* We don't run this code in the in-process agent currently.
1502 E.g., we could leave the in-process agent in autonomous
1503 circular mode if we only have fast tracepoints. If we do
1504 that, then this bit becomes racy with GDBserver, which also
1505 writes to this counter. */
1506 --traceframe_write_count;
1508 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1509 /* If we freed the traceframe that wrapped around, go back
1510 to the non-wrap case. */
1511 if (new_start < tbctrl->start)
1513 trace_debug ("Discarding past the wraparound");
1514 tbctrl->wrap = trace_buffer_hi;
1516 tbctrl->start = new_start;
1517 tbctrl->end_free = tbctrl->start;
1519 trace_debug ("Discarded a traceframe\n"
1520 "Trace buffer [%d], start=%d free=%d "
1521 "endfree=%d wrap=%d hi=%d",
1523 (int) (tbctrl->start - trace_buffer_lo),
1524 (int) (tbctrl->free - trace_buffer_lo),
1525 (int) (tbctrl->end_free - trace_buffer_lo),
1526 (int) (tbctrl->wrap - trace_buffer_lo),
1527 (int) (trace_buffer_hi - trace_buffer_lo));
1529 /* Now go back around the loop. The discard might have resulted
1530 in either one or two pieces of free space, so we want to try
1531 both before freeing any more traceframes. */
1535 /* If we get here, we know we can provide the asked-for space. */
1537 rslt = tbctrl->free;
1539 /* Adjust the request back down, now that we know we have space for
1540 the marker, but don't commit to AMT yet, we may still need to
1541 restart the operation if GDBserver touches the trace buffer
1542 (obviously only important in the in-process agent's version). */
1543 tbctrl->free += (amt - sizeof (struct traceframe));
1545 /* Or not. If GDBserver changed the trace buffer behind our back,
1546 we get to restart a new allocation attempt. */
1548 #ifdef IN_PROCESS_AGENT
1549 /* Build the tentative token. */
1550 commit_count = (((prev & 0x0007ff00) + 0x100) & 0x0007ff00);
1551 commit = (((prev & 0x0007ff00) << 12)
1555 /* Try to commit it. */
1556 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1557 if (readout != prev)
1559 trace_debug ("GDBserver has touched the trace buffer, restarting."
1560 " (prev=%08x, commit=%08x, readout=%08x)",
1561 prev, commit, readout);
1565 /* Hold your horses here. Even if that change was committed,
1566 GDBserver could come in, and clobber it. We need to hold to be
1567 able to tell if GDBserver clobbers before or after we committed
1568 the change. Whenever GDBserver goes about touching the IPA
1569 buffer, it sets a breakpoint in this routine, so we have a sync
1571 about_to_request_buffer_space ();
1573 /* Check if the change has been effective, even if GDBserver stopped
1574 us at the breakpoint. */
1577 unsigned int refetch;
1581 refetch = trace_buffer_ctrl_curr;
1583 if ((refetch == commit
1584 || ((refetch & 0x7ff00000) >> 12) == commit_count))
1587 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1588 "readout=%08x, refetch=%08x)",
1589 prev, commit, readout, refetch);
1593 trace_debug ("GDBserver has touched the trace buffer, not effective."
1594 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1595 prev, commit, readout, refetch);
1601 /* We have a new piece of the trace buffer. Hurray! */
1603 /* Add an EOB marker just past this allocation. */
1604 ((struct traceframe *) tbctrl->free)->tpnum = 0;
1605 ((struct traceframe *) tbctrl->free)->data_size = 0;
1607 /* Adjust the request back down, now that we know we have space for
1609 amt -= sizeof (struct traceframe);
1613 trace_debug ("Allocated %d bytes", (int) amt);
1614 trace_debug ("Trace buffer [%d] start=%d free=%d "
1615 "endfree=%d wrap=%d hi=%d",
1617 (int) (tbctrl->start - trace_buffer_lo),
1618 (int) (tbctrl->free - trace_buffer_lo),
1619 (int) (tbctrl->end_free - trace_buffer_lo),
1620 (int) (tbctrl->wrap - trace_buffer_lo),
1621 (int) (trace_buffer_hi - trace_buffer_lo));
1627 #ifndef IN_PROCESS_AGENT
1629 /* Return the total free space. This is not necessarily the largest
1630 block we can allocate, because of the two-part case. */
1635 if (trace_buffer_free <= trace_buffer_end_free)
1636 return trace_buffer_end_free - trace_buffer_free;
1638 return ((trace_buffer_end_free - trace_buffer_lo)
1639 + (trace_buffer_hi - trace_buffer_free));
1642 /* An 'S' in continuation packets indicates remainder are for
1645 static int seen_step_action_flag;
1647 /* Create a tracepoint (location) with given number and address. Add this
1648 new tracepoint to list and sort this list. */
1650 static struct tracepoint *
1651 add_tracepoint (int num, CORE_ADDR addr)
1653 struct tracepoint *tpoint, **tp_next;
1655 tpoint = xmalloc (sizeof (struct tracepoint));
1656 tpoint->number = num;
1657 tpoint->address = addr;
1658 tpoint->numactions = 0;
1659 tpoint->actions = NULL;
1660 tpoint->actions_str = NULL;
1661 tpoint->cond = NULL;
1662 tpoint->num_step_actions = 0;
1663 tpoint->step_actions = NULL;
1664 tpoint->step_actions_str = NULL;
1665 /* Start all off as regular (slow) tracepoints. */
1666 tpoint->type = trap_tracepoint;
1667 tpoint->orig_size = -1;
1668 tpoint->source_strings = NULL;
1669 tpoint->compiled_cond = 0;
1670 tpoint->handle = NULL;
1671 tpoint->next = NULL;
1673 /* Find a place to insert this tracepoint into list in order to keep
1674 the tracepoint list still in the ascending order. There may be
1675 multiple tracepoints at the same address as TPOINT's, and this
1676 guarantees TPOINT is inserted after all the tracepoints which are
1677 set at the same address. For example, fast tracepoints A, B, C are
1678 set at the same address, and D is to be insert at the same place as
1681 -->| A |--> | B |-->| C |->...
1683 One jump pad was created for tracepoint A, B, and C, and the target
1684 address of A is referenced/used in jump pad. So jump pad will let
1685 inferior jump to A. If D is inserted in front of A, like this,
1687 -->| D |-->| A |--> | B |-->| C |->...
1689 without updating jump pad, D is not reachable during collect, which
1690 is wrong. As we can see, the order of B, C and D doesn't matter, but
1691 A should always be the `first' one. */
1692 for (tp_next = &tracepoints;
1693 (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
1694 tp_next = &(*tp_next)->next)
1696 tpoint->next = *tp_next;
1698 last_tracepoint = tpoint;
1700 seen_step_action_flag = 0;
1705 #ifndef IN_PROCESS_AGENT
1707 /* Return the tracepoint with the given number and address, or NULL. */
1709 static struct tracepoint *
1710 find_tracepoint (int id, CORE_ADDR addr)
1712 struct tracepoint *tpoint;
1714 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1715 if (tpoint->number == id && tpoint->address == addr)
1721 /* There may be several tracepoints with the same number (because they
1722 are "locations", in GDB parlance); return the next one after the
1723 given tracepoint, or search from the beginning of the list if the
1724 first argument is NULL. */
1726 static struct tracepoint *
1727 find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1729 struct tracepoint *tpoint;
1732 tpoint = prev_tp->next;
1734 tpoint = tracepoints;
1735 for (; tpoint; tpoint = tpoint->next)
1736 if (tpoint->number == num)
1745 save_string (const char *str, size_t len)
1749 s = xmalloc (len + 1);
1750 memcpy (s, str, len);
1756 /* Append another action to perform when the tracepoint triggers. */
1759 add_tracepoint_action (struct tracepoint *tpoint, char *packet)
1765 seen_step_action_flag = 1;
1773 char *act_start = act;
1774 struct tracepoint_action *action = NULL;
1780 struct collect_memory_action *maction;
1784 maction = xmalloc (sizeof *maction);
1785 maction->base.type = *act;
1786 action = &maction->base;
1789 is_neg = (*act == '-');
1792 act = unpack_varlen_hex (act, &basereg);
1794 act = unpack_varlen_hex (act, &maction->addr);
1796 act = unpack_varlen_hex (act, &maction->len);
1797 maction->basereg = (is_neg
1800 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1801 pulongest (maction->len),
1802 paddress (maction->addr), maction->basereg);
1807 struct collect_registers_action *raction;
1809 raction = xmalloc (sizeof *raction);
1810 raction->base.type = *act;
1811 action = &raction->base;
1813 trace_debug ("Want to collect registers");
1815 /* skip past hex digits of mask for now */
1816 while (isxdigit(*act))
1822 struct collect_static_trace_data_action *raction;
1824 raction = xmalloc (sizeof *raction);
1825 raction->base.type = *act;
1826 action = &raction->base;
1828 trace_debug ("Want to collect static trace data");
1833 trace_debug ("Unexpected step action, ignoring");
1838 struct eval_expr_action *xaction;
1840 xaction = xmalloc (sizeof (*xaction));
1841 xaction->base.type = *act;
1842 action = &xaction->base;
1844 trace_debug ("Want to evaluate expression");
1845 xaction->expr = parse_agent_expr (&act);
1849 trace_debug ("unknown trace action '%c', ignoring...", *act);
1858 if (seen_step_action_flag)
1860 tpoint->num_step_actions++;
1862 tpoint->step_actions
1863 = xrealloc (tpoint->step_actions,
1864 (sizeof (*tpoint->step_actions)
1865 * tpoint->num_step_actions));
1866 tpoint->step_actions_str
1867 = xrealloc (tpoint->step_actions_str,
1868 (sizeof (*tpoint->step_actions_str)
1869 * tpoint->num_step_actions));
1870 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
1871 tpoint->step_actions_str[tpoint->num_step_actions - 1]
1872 = save_string (act_start, act - act_start);
1876 tpoint->numactions++;
1878 = xrealloc (tpoint->actions,
1879 sizeof (*tpoint->actions) * tpoint->numactions);
1881 = xrealloc (tpoint->actions_str,
1882 sizeof (*tpoint->actions_str) * tpoint->numactions);
1883 tpoint->actions[tpoint->numactions - 1] = action;
1884 tpoint->actions_str[tpoint->numactions - 1]
1885 = save_string (act_start, act - act_start);
1892 /* Find or create a trace state variable with the given number. */
1894 static struct trace_state_variable *
1895 get_trace_state_variable (int num)
1897 struct trace_state_variable *tsv;
1899 #ifdef IN_PROCESS_AGENT
1900 /* Search for an existing variable. */
1901 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
1902 if (tsv->number == num)
1906 /* Search for an existing variable. */
1907 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1908 if (tsv->number == num)
1914 /* Find or create a trace state variable with the given number. */
1916 static struct trace_state_variable *
1917 create_trace_state_variable (int num, int gdb)
1919 struct trace_state_variable *tsv;
1921 tsv = get_trace_state_variable (num);
1925 /* Create a new variable. */
1926 tsv = xmalloc (sizeof (struct trace_state_variable));
1928 tsv->initial_value = 0;
1932 #ifdef IN_PROCESS_AGENT
1935 tsv->next = alloced_trace_state_variables;
1936 alloced_trace_state_variables = tsv;
1941 tsv->next = trace_state_variables;
1942 trace_state_variables = tsv;
1947 IP_AGENT_EXPORT LONGEST
1948 get_trace_state_variable_value (int num)
1950 struct trace_state_variable *tsv;
1952 tsv = get_trace_state_variable (num);
1956 trace_debug ("No trace state variable %d, skipping value get", num);
1960 /* Call a getter function if we have one. While it's tempting to
1961 set up something to only call the getter once per tracepoint hit,
1962 it could run afoul of thread races. Better to let the getter
1963 handle it directly, if necessary to worry about it. */
1965 tsv->value = (tsv->getter) ();
1967 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1968 num, plongest (tsv->value));
1973 IP_AGENT_EXPORT void
1974 set_trace_state_variable_value (int num, LONGEST val)
1976 struct trace_state_variable *tsv;
1978 tsv = get_trace_state_variable (num);
1982 trace_debug ("No trace state variable %d, skipping value set", num);
1990 set_trace_state_variable_name (int num, const char *name)
1992 struct trace_state_variable *tsv;
1994 tsv = get_trace_state_variable (num);
1998 trace_debug ("No trace state variable %d, skipping name set", num);
2002 tsv->name = (char *) name;
2006 set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
2008 struct trace_state_variable *tsv;
2010 tsv = get_trace_state_variable (num);
2014 trace_debug ("No trace state variable %d, skipping getter set", num);
2018 tsv->getter = getter;
2021 /* Add a raw traceframe for the given tracepoint. */
2023 static struct traceframe *
2024 add_traceframe (struct tracepoint *tpoint)
2026 struct traceframe *tframe;
2028 tframe = trace_buffer_alloc (sizeof (struct traceframe));
2033 tframe->tpnum = tpoint->number;
2034 tframe->data_size = 0;
2039 /* Add a block to the traceframe currently being worked on. */
2041 static unsigned char *
2042 add_traceframe_block (struct traceframe *tframe, int amt)
2044 unsigned char *block;
2049 block = trace_buffer_alloc (amt);
2054 tframe->data_size += amt;
2059 /* Flag that the current traceframe is finished. */
2062 finish_traceframe (struct traceframe *tframe)
2064 ++traceframe_write_count;
2065 ++traceframes_created;
2068 #ifndef IN_PROCESS_AGENT
2070 /* Given a traceframe number NUM, find the NUMth traceframe in the
2073 static struct traceframe *
2074 find_traceframe (int num)
2076 struct traceframe *tframe;
2079 for (tframe = FIRST_TRACEFRAME ();
2081 tframe = NEXT_TRACEFRAME (tframe))
2092 get_traceframe_address (struct traceframe *tframe)
2095 struct tracepoint *tpoint;
2097 addr = traceframe_get_pc (tframe);
2102 /* Fallback strategy, will be incorrect for while-stepping frames
2103 and multi-location tracepoints. */
2104 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2105 return tpoint->address;
2108 /* Search for the next traceframe whose address is inside or outside
2111 static struct traceframe *
2112 find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2115 struct traceframe *tframe;
2118 *tfnump = current_traceframe + 1;
2119 tframe = find_traceframe (*tfnump);
2120 /* The search is not supposed to wrap around. */
2127 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2129 tfaddr = get_traceframe_address (tframe);
2131 ? (lo <= tfaddr && tfaddr <= hi)
2132 : (lo > tfaddr || tfaddr > hi))
2141 /* Search for the next traceframe recorded by the given tracepoint.
2142 Note that for multi-location tracepoints, this will find whatever
2143 location appears first. */
2145 static struct traceframe *
2146 find_next_traceframe_by_tracepoint (int num, int *tfnump)
2148 struct traceframe *tframe;
2150 *tfnump = current_traceframe + 1;
2151 tframe = find_traceframe (*tfnump);
2152 /* The search is not supposed to wrap around. */
2159 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2161 if (tframe->tpnum == num)
2172 #ifndef IN_PROCESS_AGENT
2174 /* Clear all past trace state. */
2177 cmd_qtinit (char *packet)
2179 struct trace_state_variable *tsv, *prev, *next;
2181 /* Make sure we don't try to read from a trace frame. */
2182 current_traceframe = -1;
2184 trace_debug ("Initializing the trace");
2186 clear_installed_tracepoints ();
2187 clear_readonly_regions ();
2190 last_tracepoint = NULL;
2192 /* Clear out any leftover trace state variables. Ones with target
2193 defined getters should be kept however. */
2195 tsv = trace_state_variables;
2198 trace_debug ("Looking at var %d", tsv->number);
2199 if (tsv->getter == NULL)
2205 trace_state_variables = next;
2206 trace_debug ("Deleting var %d", tsv->number);
2217 clear_trace_buffer ();
2218 clear_inferior_trace_buffer ();
2223 /* Unprobe the UST marker at ADDRESS. */
2226 unprobe_marker_at (CORE_ADDR address)
2228 char cmd[CMD_BUF_SIZE];
2230 sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
2231 run_inferior_command (cmd);
2234 /* Restore the program to its pre-tracing state. This routine may be called
2235 in error situations, so it needs to be careful about only restoring
2236 from known-valid bits. */
2239 clear_installed_tracepoints (void)
2241 struct tracepoint *tpoint;
2242 struct tracepoint *prev_stpoint;
2245 cancel_breakpoints ();
2247 prev_stpoint = NULL;
2249 /* Restore any bytes overwritten by tracepoints. */
2250 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2252 /* Catch the case where we might try to remove a tracepoint that
2253 was never actually installed. */
2254 if (tpoint->handle == NULL)
2256 trace_debug ("Tracepoint %d at 0x%s was "
2257 "never installed, nothing to clear",
2258 tpoint->number, paddress (tpoint->address));
2262 switch (tpoint->type)
2264 case trap_tracepoint:
2265 delete_breakpoint (tpoint->handle);
2267 case fast_tracepoint:
2268 delete_fast_tracepoint_jump (tpoint->handle);
2270 case static_tracepoint:
2271 if (prev_stpoint != NULL
2272 && prev_stpoint->address == tpoint->address)
2273 /* Nothing to do. We already unprobed a tracepoint set at
2274 this marker address (and there can only be one probe
2279 unprobe_marker_at (tpoint->address);
2280 prev_stpoint = tpoint;
2285 tpoint->handle = NULL;
2291 /* Parse a packet that defines a tracepoint. */
2294 cmd_qtdp (char *own_buf)
2297 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2298 int trail_hyphen = 0;
2302 struct tracepoint *tpoint;
2304 char *packet = own_buf;
2306 packet += strlen ("QTDP:");
2308 /* A hyphen at the beginning marks a packet specifying actions for a
2309 tracepoint already supplied. */
2316 packet = unpack_varlen_hex (packet, &num);
2317 ++packet; /* skip a colon */
2318 packet = unpack_varlen_hex (packet, &addr);
2319 ++packet; /* skip a colon */
2321 /* See if we already have this tracepoint. */
2322 tpoint = find_tracepoint (num, addr);
2326 /* Duplicate tracepoints are never allowed. */
2329 trace_debug ("Tracepoint error: tracepoint %d"
2330 " at 0x%s already exists",
2331 (int) num, paddress (addr));
2332 write_enn (own_buf);
2336 tpoint = add_tracepoint (num, addr);
2338 tpoint->enabled = (*packet == 'E');
2339 ++packet; /* skip 'E' */
2340 ++packet; /* skip a colon */
2341 packet = unpack_varlen_hex (packet, &count);
2342 tpoint->step_count = count;
2343 ++packet; /* skip a colon */
2344 packet = unpack_varlen_hex (packet, &count);
2345 tpoint->pass_count = count;
2346 /* See if we have any of the additional optional fields. */
2347 while (*packet == ':')
2352 tpoint->type = fast_tracepoint;
2354 packet = unpack_varlen_hex (packet, &count);
2355 tpoint->orig_size = count;
2357 else if (*packet == 'S')
2359 tpoint->type = static_tracepoint;
2362 else if (*packet == 'X')
2364 actparm = (char *) packet;
2365 tpoint->cond = parse_agent_expr (&actparm);
2368 else if (*packet == '-')
2370 else if (*packet == '\0')
2373 trace_debug ("Unknown optional tracepoint field");
2378 trace_debug ("Also has actions\n");
2381 trace_debug ("Defined %stracepoint %d at 0x%s, "
2382 "enabled %d step %ld pass %ld",
2383 tpoint->type == fast_tracepoint ? "fast "
2385 tpoint->number, paddress (tpoint->address), tpoint->enabled,
2386 tpoint->step_count, tpoint->pass_count);
2389 add_tracepoint_action (tpoint, packet);
2392 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2393 (int) num, paddress (addr));
2394 write_enn (own_buf);
2398 /* Install tracepoint during tracing only once for each tracepoint location.
2399 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2400 determine the last QTDP packet for one tracepoint location by checking
2401 trailing hyphen in QTDP packet. */
2402 if (tracing && !trail_hyphen)
2404 /* Pause all threads temporarily while we patch tracepoints. */
2407 /* download_tracepoint will update global `tracepoints'
2408 list, so it is unsafe to leave threads in jump pad. */
2409 stabilize_threads ();
2411 /* Freeze threads. */
2414 download_tracepoint (tpoint);
2415 install_tracepoint (tpoint, own_buf);
2425 cmd_qtdpsrc (char *own_buf)
2427 ULONGEST num, addr, start, slen;
2428 struct tracepoint *tpoint;
2429 char *packet = own_buf;
2430 char *saved, *srctype, *src;
2432 struct source_string *last, *newlast;
2434 packet += strlen ("QTDPsrc:");
2436 packet = unpack_varlen_hex (packet, &num);
2437 ++packet; /* skip a colon */
2438 packet = unpack_varlen_hex (packet, &addr);
2439 ++packet; /* skip a colon */
2441 /* See if we already have this tracepoint. */
2442 tpoint = find_tracepoint (num, addr);
2446 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2447 (int) num, paddress (addr));
2448 write_enn (own_buf);
2453 packet = strchr (packet, ':');
2454 srctype = xmalloc (packet - saved + 1);
2455 memcpy (srctype, saved, packet - saved);
2456 srctype[packet - saved] = '\0';
2458 packet = unpack_varlen_hex (packet, &start);
2459 ++packet; /* skip a colon */
2460 packet = unpack_varlen_hex (packet, &slen);
2461 ++packet; /* skip a colon */
2462 src = xmalloc (slen + 1);
2463 nbytes = unhexify (src, packet, strlen (packet) / 2);
2466 newlast = xmalloc (sizeof (struct source_string));
2467 newlast->type = srctype;
2469 newlast->next = NULL;
2470 /* Always add a source string to the end of the list;
2471 this keeps sequences of actions/commands in the right
2473 if (tpoint->source_strings)
2475 for (last = tpoint->source_strings; last->next; last = last->next)
2477 last->next = newlast;
2480 tpoint->source_strings = newlast;
2486 cmd_qtdv (char *own_buf)
2488 ULONGEST num, val, builtin;
2491 struct trace_state_variable *tsv;
2492 char *packet = own_buf;
2494 packet += strlen ("QTDV:");
2496 packet = unpack_varlen_hex (packet, &num);
2497 ++packet; /* skip a colon */
2498 packet = unpack_varlen_hex (packet, &val);
2499 ++packet; /* skip a colon */
2500 packet = unpack_varlen_hex (packet, &builtin);
2501 ++packet; /* skip a colon */
2503 nbytes = strlen (packet) / 2;
2504 varname = xmalloc (nbytes + 1);
2505 nbytes = unhexify (varname, packet, nbytes);
2506 varname[nbytes] = '\0';
2508 tsv = create_trace_state_variable (num, 1);
2509 tsv->initial_value = (LONGEST) val;
2510 tsv->name = varname;
2512 set_trace_state_variable_value (num, (LONGEST) val);
2518 cmd_qtenable_disable (char *own_buf, int enable)
2520 char *packet = own_buf;
2522 struct tracepoint *tp;
2524 packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2525 packet = unpack_varlen_hex (packet, &num);
2526 ++packet; /* skip a colon */
2527 packet = unpack_varlen_hex (packet, &addr);
2529 tp = find_tracepoint (num, addr);
2533 if ((enable && tp->enabled) || (!enable && !tp->enabled))
2535 trace_debug ("Tracepoint %d at 0x%s is already %s",
2536 (int) num, paddress (addr),
2537 enable ? "enabled" : "disabled");
2542 trace_debug ("%s tracepoint %d at 0x%s",
2543 enable ? "Enabling" : "Disabling",
2544 (int) num, paddress (addr));
2546 tp->enabled = enable;
2548 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2551 int offset = offsetof (struct tracepoint, enabled);
2552 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2554 ret = prepare_to_access_memory ();
2557 trace_debug ("Failed to temporarily stop inferior threads");
2558 write_enn (own_buf);
2562 ret = write_inferior_integer (obj_addr, enable);
2563 done_accessing_memory ();
2567 trace_debug ("Cannot write enabled flag into "
2568 "inferior process memory");
2569 write_enn (own_buf);
2578 trace_debug ("Tracepoint %d at 0x%s not found",
2579 (int) num, paddress (addr));
2580 write_enn (own_buf);
2585 cmd_qtv (char *own_buf)
2590 char *packet = own_buf;
2592 packet += strlen ("qTV:");
2593 unpack_varlen_hex (packet, &num);
2595 if (current_traceframe >= 0)
2597 err = traceframe_read_tsv ((int) num, &val);
2600 strcpy (own_buf, "U");
2604 /* Only make tsv's be undefined before the first trace run. After a
2605 trace run is over, the user might want to see the last value of
2606 the tsv, and it might not be available in a traceframe. */
2607 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2609 strcpy (own_buf, "U");
2613 val = get_trace_state_variable_value (num);
2615 sprintf (own_buf, "V%s", phex_nz (val, 0));
2618 /* Clear out the list of readonly regions. */
2621 clear_readonly_regions (void)
2623 struct readonly_region *roreg;
2625 while (readonly_regions)
2627 roreg = readonly_regions;
2628 readonly_regions = readonly_regions->next;
2633 /* Parse the collection of address ranges whose contents GDB believes
2634 to be unchanging and so can be read directly from target memory
2635 even while looking at a traceframe. */
2638 cmd_qtro (char *own_buf)
2640 ULONGEST start, end;
2641 struct readonly_region *roreg;
2642 char *packet = own_buf;
2644 trace_debug ("Want to mark readonly regions");
2646 clear_readonly_regions ();
2648 packet += strlen ("QTro");
2650 while (*packet == ':')
2652 ++packet; /* skip a colon */
2653 packet = unpack_varlen_hex (packet, &start);
2654 ++packet; /* skip a comma */
2655 packet = unpack_varlen_hex (packet, &end);
2656 roreg = xmalloc (sizeof (struct readonly_region));
2657 roreg->start = start;
2659 roreg->next = readonly_regions;
2660 readonly_regions = roreg;
2661 trace_debug ("Added readonly region from 0x%s to 0x%s",
2662 paddress (roreg->start), paddress (roreg->end));
2668 /* Test to see if the given range is in our list of readonly ranges.
2669 We only test for being entirely within a range, GDB is not going to
2670 send a single memory packet that spans multiple regions. */
2673 in_readonly_region (CORE_ADDR addr, ULONGEST length)
2675 struct readonly_region *roreg;
2677 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2678 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2684 /* The maximum size of a jump pad entry. */
2685 static const int max_jump_pad_size = 0x100;
2687 static CORE_ADDR gdb_jump_pad_head;
2689 /* Return the address of the next free jump space. */
2692 get_jump_space_head (void)
2694 if (gdb_jump_pad_head == 0)
2696 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2697 &gdb_jump_pad_head))
2698 fatal ("error extracting jump_pad_buffer");
2701 return gdb_jump_pad_head;
2704 /* Reserve USED bytes from the jump space. */
2707 claim_jump_space (ULONGEST used)
2709 trace_debug ("claim_jump_space reserves %s bytes at %s",
2710 pulongest (used), paddress (gdb_jump_pad_head));
2711 gdb_jump_pad_head += used;
2714 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2715 the command fails, or 0 otherwise. If the command ran
2716 successfully, but probing the marker failed, ERROUT will be filled
2717 with the error to reply to GDB, and -1 is also returned. This
2718 allows directly passing IPA errors to GDB. */
2721 probe_marker_at (CORE_ADDR address, char *errout)
2723 char cmd[CMD_BUF_SIZE];
2726 sprintf (cmd, "probe_marker_at:%s", paddress (address));
2727 err = run_inferior_command (cmd);
2733 strcpy (errout, cmd);
2742 clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
2744 to->jump_pad = from->jump_pad;
2745 to->jump_pad_end = from->jump_pad_end;
2746 to->adjusted_insn_addr = from->adjusted_insn_addr;
2747 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
2748 to->handle = from->handle;
2750 gdb_assert (from->handle);
2751 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
2754 #define MAX_JUMP_SIZE 20
2756 /* Install fast tracepoint. Return 0 if successful, otherwise return
2760 install_fast_tracepoint (struct tracepoint *tpoint)
2762 CORE_ADDR jentry, jump_entry;
2764 /* The jump to the jump pad of the last fast tracepoint
2766 unsigned char fjump[MAX_JUMP_SIZE];
2767 ULONGEST fjump_size;
2769 jentry = jump_entry = get_jump_space_head ();
2771 /* Install the jump pad. */
2772 err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
2774 ipa_sym_addrs.addr_gdb_collect,
2775 ipa_sym_addrs.addr_collecting,
2777 &jentry, fjump, &fjump_size,
2778 &tpoint->adjusted_insn_addr,
2779 &tpoint->adjusted_insn_addr_end);
2785 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
2788 if (tpoint->handle != NULL)
2790 tpoint->jump_pad = jump_entry;
2791 tpoint->jump_pad_end = jentry;
2793 /* Pad to 8-byte alignment. */
2794 jentry = ((jentry + 7) & ~0x7);
2795 claim_jump_space (jentry - jump_entry);
2802 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2805 install_tracepoint (struct tracepoint *tpoint, char *own_buf)
2807 tpoint->handle = NULL;
2810 if (tpoint->type == trap_tracepoint)
2812 /* Tracepoints are installed as memory breakpoints. Just go
2813 ahead and install the trap. The breakpoints module
2814 handles duplicated breakpoints, and the memory read
2815 routine handles un-patching traps from memory reads. */
2816 tpoint->handle = set_breakpoint_at (tpoint->address,
2817 tracepoint_handler);
2819 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
2821 struct tracepoint *tp;
2823 if (!in_process_agent_loaded ())
2825 trace_debug ("Requested a %s tracepoint, but fast "
2826 "tracepoints aren't supported.",
2827 tpoint->type == static_tracepoint ? "static" : "fast");
2828 write_e_ipa_not_loaded (own_buf);
2831 if (tpoint->type == static_tracepoint && !in_process_agent_loaded_ust ())
2833 trace_debug ("Requested a static tracepoint, but static "
2834 "tracepoints are not supported.");
2835 write_e_ust_not_loaded (own_buf);
2839 /* Find another fast or static tracepoint at the same address. */
2840 for (tp = tracepoints; tp; tp = tp->next)
2842 if (tp->address == tpoint->address && tp->type == tpoint->type
2843 && tp->number != tpoint->number)
2847 if (tpoint->type == fast_tracepoint)
2849 if (tp) /* TPOINT is installed at the same address as TP. */
2850 clone_fast_tracepoint (tpoint, tp);
2852 install_fast_tracepoint (tpoint);
2857 tpoint->handle = (void *) -1;
2860 if (probe_marker_at (tpoint->address, own_buf) == 0)
2861 tpoint->handle = (void *) -1;
2867 internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
2869 if (tpoint->handle == NULL)
2871 if (*own_buf == '\0')
2872 write_enn (own_buf);
2879 cmd_qtstart (char *packet)
2881 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
2883 trace_debug ("Starting the trace");
2885 /* Pause all threads temporarily while we patch tracepoints. */
2888 /* Get threads out of jump pads. Safe to do here, since this is a
2889 top level command. And, required to do here, since we're
2890 deleting/rewriting jump pads. */
2892 stabilize_threads ();
2894 /* Freeze threads. */
2897 /* Sync the fast tracepoints list in the inferior ftlib. */
2898 if (in_process_agent_loaded ())
2900 download_tracepoints ();
2901 download_trace_state_variables ();
2904 /* No previous fast tpoint yet. */
2905 prev_ftpoint = NULL;
2907 /* No previous static tpoint yet. */
2908 prev_stpoint = NULL;
2912 /* Install tracepoints. */
2913 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2915 /* Ensure all the hit counts start at zero. */
2916 tpoint->hit_count = 0;
2918 if (tpoint->type == trap_tracepoint)
2920 /* Tracepoints are installed as memory breakpoints. Just go
2921 ahead and install the trap. The breakpoints module
2922 handles duplicated breakpoints, and the memory read
2923 routine handles un-patching traps from memory reads. */
2924 tpoint->handle = set_breakpoint_at (tpoint->address,
2925 tracepoint_handler);
2927 else if (tpoint->type == fast_tracepoint)
2929 if (maybe_write_ipa_not_loaded (packet))
2931 trace_debug ("Requested a fast tracepoint, but fast "
2932 "tracepoints aren't supported.");
2936 if (prev_ftpoint != NULL && prev_ftpoint->address == tpoint->address)
2937 clone_fast_tracepoint (tpoint, prev_ftpoint);
2940 if (install_fast_tracepoint (tpoint) == 0)
2941 prev_ftpoint = tpoint;
2945 else if (tpoint->type == static_tracepoint)
2947 if (maybe_write_ipa_ust_not_loaded (packet))
2949 trace_debug ("Requested a static tracepoint, but static "
2950 "tracepoints are not supported.");
2954 /* Can only probe a given marker once. */
2955 if (prev_stpoint != NULL && prev_stpoint->address == tpoint->address)
2957 tpoint->handle = (void *) -1;
2961 if (probe_marker_at (tpoint->address, packet) == 0)
2963 tpoint->handle = (void *) -1;
2965 /* So that we can handle multiple static tracepoints
2966 at the same address easily. */
2967 prev_stpoint = tpoint;
2972 /* Any failure in the inner loop is sufficient cause to give
2974 if (tpoint->handle == NULL)
2978 /* Any error in tracepoint insertion is unacceptable; better to
2979 address the problem now, than end up with a useless or misleading
2983 clear_installed_tracepoints ();
2984 if (*packet == '\0')
2990 stopping_tracepoint = NULL;
2991 trace_buffer_is_full = 0;
2992 expr_eval_result = expr_eval_no_error;
2993 error_tracepoint = NULL;
2995 /* Tracing is now active, hits will now start being logged. */
2998 if (in_process_agent_loaded ())
3000 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
3001 fatal ("Error setting tracing variable in lib");
3003 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3005 fatal ("Error clearing stopping_tracepoint variable in lib");
3007 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
3008 fatal ("Error clearing trace_buffer_is_full variable in lib");
3010 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3011 stop_tracing_handler);
3012 if (stop_tracing_bkpt == NULL)
3013 error ("Error setting stop_tracing breakpoint");
3015 flush_trace_buffer_bkpt
3016 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3017 flush_trace_buffer_handler);
3018 if (flush_trace_buffer_bkpt == NULL)
3019 error ("Error setting flush_trace_buffer breakpoint");
3027 /* End a tracing run, filling in a stop reason to report back to GDB,
3028 and removing the tracepoints from the code. */
3035 trace_debug ("Tracing is already off, ignoring");
3039 trace_debug ("Stopping the trace");
3041 /* Pause all threads before removing fast jumps from memory,
3042 breakpoints, and touching IPA state variables (inferior memory).
3043 Some thread may hit the internal tracing breakpoints, or be
3044 collecting this moment, but that's ok, we don't release the
3045 tpoint object's memory or the jump pads here (we only do that
3046 when we're sure we can move all threads out of the jump pads).
3047 We can't now, since we may be getting here due to the inferior
3048 agent calling us. */
3050 /* Since we're removing breakpoints, cancel breakpoint hits,
3051 possibly related to the breakpoints we're about to delete. */
3052 cancel_breakpoints ();
3054 /* Stop logging. Tracepoints can still be hit, but they will not be
3057 if (in_process_agent_loaded ())
3059 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
3060 fatal ("Error clearing tracing variable in lib");
3063 tracing_stop_reason = "t???";
3064 tracing_stop_tpnum = 0;
3065 if (stopping_tracepoint)
3067 trace_debug ("Stopping the trace because "
3068 "tracepoint %d was hit %ld times",
3069 stopping_tracepoint->number,
3070 stopping_tracepoint->pass_count);
3071 tracing_stop_reason = "tpasscount";
3072 tracing_stop_tpnum = stopping_tracepoint->number;
3074 else if (trace_buffer_is_full)
3076 trace_debug ("Stopping the trace because the trace buffer is full");
3077 tracing_stop_reason = "tfull";
3079 else if (expr_eval_result != expr_eval_no_error)
3081 trace_debug ("Stopping the trace because of an expression eval error");
3082 tracing_stop_reason = eval_result_names[expr_eval_result];
3083 tracing_stop_tpnum = error_tracepoint->number;
3085 #ifndef IN_PROCESS_AGENT
3086 else if (!gdb_connected ())
3088 trace_debug ("Stopping the trace because GDB disconnected");
3089 tracing_stop_reason = "tdisconnected";
3094 trace_debug ("Stopping the trace because of a tstop command");
3095 tracing_stop_reason = "tstop";
3098 stopping_tracepoint = NULL;
3099 error_tracepoint = NULL;
3101 /* Clear out the tracepoints. */
3102 clear_installed_tracepoints ();
3104 if (in_process_agent_loaded ())
3106 /* Pull in fast tracepoint trace frames from the inferior lib
3107 buffer into our buffer, even if our buffer is already full,
3108 because we want to present the full number of created frames
3109 in addition to what fit in the trace buffer. */
3110 upload_fast_traceframes ();
3113 if (stop_tracing_bkpt != NULL)
3115 delete_breakpoint (stop_tracing_bkpt);
3116 stop_tracing_bkpt = NULL;
3119 if (flush_trace_buffer_bkpt != NULL)
3121 delete_breakpoint (flush_trace_buffer_bkpt);
3122 flush_trace_buffer_bkpt = NULL;
3129 stop_tracing_handler (CORE_ADDR addr)
3131 trace_debug ("lib hit stop_tracing");
3133 /* Don't actually handle it here. When we stop tracing we remove
3134 breakpoints from the inferior, and that is not allowed in a
3135 breakpoint handler (as the caller is walking the breakpoint
3141 flush_trace_buffer_handler (CORE_ADDR addr)
3143 trace_debug ("lib hit flush_trace_buffer");
3148 cmd_qtstop (char *packet)
3155 cmd_qtdisconnected (char *own_buf)
3158 char *packet = own_buf;
3160 packet += strlen ("QTDisconnected:");
3162 unpack_varlen_hex (packet, &setting);
3166 disconnected_tracing = setting;
3170 cmd_qtframe (char *own_buf)
3172 ULONGEST frame, pc, lo, hi, num;
3174 struct traceframe *tframe;
3175 char *packet = own_buf;
3177 packet += strlen ("QTFrame:");
3179 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3181 packet += strlen ("pc:");
3182 unpack_varlen_hex (packet, &pc);
3183 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3184 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3186 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3188 packet += strlen ("range:");
3189 packet = unpack_varlen_hex (packet, &lo);
3191 unpack_varlen_hex (packet, &hi);
3192 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3193 paddress (lo), paddress (hi));
3194 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3196 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3198 packet += strlen ("outside:");
3199 packet = unpack_varlen_hex (packet, &lo);
3201 unpack_varlen_hex (packet, &hi);
3202 trace_debug ("Want to find next traceframe "
3203 "outside the range 0x%s to 0x%s",
3204 paddress (lo), paddress (hi));
3205 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3207 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3209 packet += strlen ("tdp:");
3210 unpack_varlen_hex (packet, &num);
3212 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3213 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3217 unpack_varlen_hex (packet, &frame);
3218 tfnum = (int) frame;
3221 trace_debug ("Want to stop looking at traceframes");
3222 current_traceframe = -1;
3226 trace_debug ("Want to look at traceframe %d", tfnum);
3227 tframe = find_traceframe (tfnum);
3232 current_traceframe = tfnum;
3233 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3236 sprintf (own_buf, "F-1");
3240 cmd_qtstatus (char *packet)
3242 char *stop_reason_rsp = NULL;
3244 trace_debug ("Returning trace status as %d, stop reason %s",
3245 tracing, tracing_stop_reason);
3247 if (in_process_agent_loaded ())
3251 upload_fast_traceframes ();
3256 stop_reason_rsp = (char *) tracing_stop_reason;
3258 /* The user visible error string in terror needs to be hex encoded.
3259 We leave it as plain string in `tracepoint_stop_reason' to ease
3261 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3263 const char *result_name;
3267 result_name = stop_reason_rsp + strlen ("terror:");
3268 hexstr_len = strlen (result_name) * 2;
3269 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3270 strcpy (p, "terror:");
3272 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3278 "tframes:%x;tcreated:%x;"
3279 "tfree:%x;tsize:%s;"
3283 stop_reason_rsp, tracing_stop_tpnum,
3284 traceframe_count, traceframes_created,
3285 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3286 circular_trace_buffer,
3287 disconnected_tracing);
3290 /* State variables to help return all the tracepoint bits. */
3291 static struct tracepoint *cur_tpoint;
3292 static int cur_action;
3293 static int cur_step_action;
3294 static struct source_string *cur_source_string;
3295 static struct trace_state_variable *cur_tsv;
3297 /* Compose a response that is an imitation of the syntax by which the
3298 tracepoint was originally downloaded. */
3301 response_tracepoint (char *packet, struct tracepoint *tpoint)
3305 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
3306 paddress (tpoint->address),
3307 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3308 tpoint->pass_count);
3309 if (tpoint->type == fast_tracepoint)
3310 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
3311 else if (tpoint->type == static_tracepoint)
3312 sprintf (packet + strlen (packet), ":S");
3316 buf = unparse_agent_expr (tpoint->cond);
3317 sprintf (packet + strlen (packet), ":X%x,%s",
3318 tpoint->cond->length, buf);
3323 /* Compose a response that is an imitation of the syntax by which the
3324 tracepoint action was originally downloaded (with the difference
3325 that due to the way we store the actions, this will output a packet
3326 per action, while GDB could have combined more than one action
3330 response_action (char *packet, struct tracepoint *tpoint,
3331 char *taction, int step)
3333 sprintf (packet, "%c%x:%s:%s",
3334 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3338 /* Compose a response that is an imitation of the syntax by which the
3339 tracepoint source piece was originally downloaded. */
3342 response_source (char *packet,
3343 struct tracepoint *tpoint, struct source_string *src)
3348 len = strlen (src->str);
3349 buf = alloca (len * 2 + 1);
3350 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3352 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3353 tpoint->number, paddress (tpoint->address),
3354 src->type, 0, len, buf);
3357 /* Return the first piece of tracepoint definition, and initialize the
3358 state machine that will iterate through all the tracepoint
3362 cmd_qtfp (char *packet)
3364 trace_debug ("Returning first tracepoint definition piece");
3366 cur_tpoint = tracepoints;
3367 cur_action = cur_step_action = -1;
3368 cur_source_string = NULL;
3371 response_tracepoint (packet, cur_tpoint);
3373 strcpy (packet, "l");
3376 /* Return additional pieces of tracepoint definition. Each action and
3377 stepping action must go into its own packet, because of packet size
3378 limits, and so we use state variables to deliver one piece at a
3382 cmd_qtsp (char *packet)
3384 trace_debug ("Returning subsequent tracepoint definition piece");
3388 /* This case would normally never occur, but be prepared for
3390 strcpy (packet, "l");
3392 else if (cur_action < cur_tpoint->numactions - 1)
3395 response_action (packet, cur_tpoint,
3396 cur_tpoint->actions_str[cur_action], 0);
3398 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3401 response_action (packet, cur_tpoint,
3402 cur_tpoint->step_actions_str[cur_step_action], 1);
3404 else if ((cur_source_string
3405 ? cur_source_string->next
3406 : cur_tpoint->source_strings))
3408 if (cur_source_string)
3409 cur_source_string = cur_source_string->next;
3411 cur_source_string = cur_tpoint->source_strings;
3412 response_source (packet, cur_tpoint, cur_source_string);
3416 cur_tpoint = cur_tpoint->next;
3417 cur_action = cur_step_action = -1;
3418 cur_source_string = NULL;
3420 response_tracepoint (packet, cur_tpoint);
3422 strcpy (packet, "l");
3426 /* Compose a response that is an imitation of the syntax by which the
3427 trace state variable was originally downloaded. */
3430 response_tsv (char *packet, struct trace_state_variable *tsv)
3432 char *buf = (char *) "";
3437 namelen = strlen (tsv->name);
3438 buf = alloca (namelen * 2 + 1);
3439 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3442 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3443 tsv->getter ? 1 : 0, buf);
3446 /* Return the first trace state variable definition, and initialize
3447 the state machine that will iterate through all the tsv bits. */
3450 cmd_qtfv (char *packet)
3452 trace_debug ("Returning first trace state variable definition");
3454 cur_tsv = trace_state_variables;
3457 response_tsv (packet, cur_tsv);
3459 strcpy (packet, "l");
3462 /* Return additional trace state variable definitions. */
3465 cmd_qtsv (char *packet)
3467 trace_debug ("Returning first trace state variable definition");
3471 /* This case would normally never occur, but be prepared for
3473 strcpy (packet, "l");
3477 cur_tsv = cur_tsv->next;
3479 response_tsv (packet, cur_tsv);
3481 strcpy (packet, "l");
3484 strcpy (packet, "l");
3487 /* Return the first static tracepoint marker, and initialize the state
3488 machine that will iterate through all the static tracepoints
3492 cmd_qtfstm (char *packet)
3494 if (!maybe_write_ipa_ust_not_loaded (packet))
3495 run_inferior_command (packet);
3498 /* Return additional static tracepoints markers. */
3501 cmd_qtsstm (char *packet)
3503 if (!maybe_write_ipa_ust_not_loaded (packet))
3504 run_inferior_command (packet);
3507 /* Return the definition of the static tracepoint at a given address.
3508 Result packet is the same as qTsST's. */
3511 cmd_qtstmat (char *packet)
3513 if (!maybe_write_ipa_ust_not_loaded (packet))
3514 run_inferior_command (packet);
3517 /* Respond to qTBuffer packet with a block of raw data from the trace
3518 buffer. GDB may ask for a lot, but we are allowed to reply with
3519 only as much as will fit within packet limits or whatever. */
3522 cmd_qtbuffer (char *own_buf)
3524 ULONGEST offset, num, tot;
3526 char *packet = own_buf;
3528 packet += strlen ("qTBuffer:");
3530 packet = unpack_varlen_hex (packet, &offset);
3531 ++packet; /* skip a comma */
3532 unpack_varlen_hex (packet, &num);
3534 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3535 (int) num, pulongest (offset));
3537 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3539 /* If we're right at the end, reply specially that we're done. */
3542 strcpy (own_buf, "l");
3546 /* Object to any other out-of-bounds request. */
3549 write_enn (own_buf);
3553 /* Compute the pointer corresponding to the given offset, accounting
3555 tbp = trace_buffer_start + offset;
3556 if (tbp >= trace_buffer_wrap)
3557 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3559 /* Trim to the remaining bytes if we're close to the end. */
3560 if (num > tot - offset)
3563 /* Trim to available packet size. */
3564 if (num >= (PBUFSIZ - 16) / 2 )
3565 num = (PBUFSIZ - 16) / 2;
3567 convert_int_to_ascii (tbp, own_buf, num);
3568 own_buf[num] = '\0';
3572 cmd_bigqtbuffer (char *own_buf)
3575 char *packet = own_buf;
3577 packet += strlen ("QTBuffer:");
3579 if (strncmp ("circular:", packet, strlen ("circular:")) == 0)
3581 packet += strlen ("circular:");
3582 unpack_varlen_hex (packet, &val);
3583 circular_trace_buffer = val;
3584 trace_debug ("Trace buffer is now %s",
3585 circular_trace_buffer ? "circular" : "linear");
3589 write_enn (own_buf);
3593 handle_tracepoint_general_set (char *packet)
3595 if (strcmp ("QTinit", packet) == 0)
3597 cmd_qtinit (packet);
3600 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3605 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3607 cmd_qtdpsrc (packet);
3610 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
3612 cmd_qtenable_disable (packet, 1);
3615 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
3617 cmd_qtenable_disable (packet, 0);
3620 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3625 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3630 else if (strcmp ("QTStart", packet) == 0)
3632 cmd_qtstart (packet);
3635 else if (strcmp ("QTStop", packet) == 0)
3637 cmd_qtstop (packet);
3640 else if (strncmp ("QTDisconnected:", packet,
3641 strlen ("QTDisconnected:")) == 0)
3643 cmd_qtdisconnected (packet);
3646 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
3648 cmd_qtframe (packet);
3651 else if (strncmp ("QTBuffer:", packet, strlen ("QTBuffer:")) == 0)
3653 cmd_bigqtbuffer (packet);
3661 handle_tracepoint_query (char *packet)
3663 if (strcmp ("qTStatus", packet) == 0)
3665 cmd_qtstatus (packet);
3668 else if (strcmp ("qTfP", packet) == 0)
3673 else if (strcmp ("qTsP", packet) == 0)
3678 else if (strcmp ("qTfV", packet) == 0)
3683 else if (strcmp ("qTsV", packet) == 0)
3688 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
3693 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
3695 cmd_qtbuffer (packet);
3698 else if (strcmp ("qTfSTM", packet) == 0)
3700 cmd_qtfstm (packet);
3703 else if (strcmp ("qTsSTM", packet) == 0)
3705 cmd_qtsstm (packet);
3708 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
3710 cmd_qtstmat (packet);
3718 #ifndef IN_PROCESS_AGENT
3720 /* Call this when thread TINFO has hit the tracepoint defined by
3721 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3722 action. This adds a while-stepping collecting state item to the
3723 threads' collecting state list, so that we can keep track of
3724 multiple simultaneous while-stepping actions being collected by the
3725 same thread. This can happen in cases like:
3727 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3729 ff0003 INSN3 <-- TP2, collect $regs
3730 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3733 Notice that when instruction INSN5 is reached, the while-stepping
3734 actions of both TP1 and TP3 are still being collected, and that TP2
3735 had been collected meanwhile. The whole range of ff0001-ff0005
3736 should be single-stepped, due to at least TP1's while-stepping
3737 action covering the whole range. */
3740 add_while_stepping_state (struct thread_info *tinfo,
3741 int tp_number, CORE_ADDR tp_address)
3743 struct wstep_state *wstep;
3745 wstep = xmalloc (sizeof (*wstep));
3746 wstep->next = tinfo->while_stepping;
3748 wstep->tp_number = tp_number;
3749 wstep->tp_address = tp_address;
3750 wstep->current_step = 0;
3752 tinfo->while_stepping = wstep;
3755 /* Release the while-stepping collecting state WSTEP. */
3758 release_while_stepping_state (struct wstep_state *wstep)
3763 /* Release all while-stepping collecting states currently associated
3764 with thread TINFO. */
3767 release_while_stepping_state_list (struct thread_info *tinfo)
3769 struct wstep_state *head;
3771 while (tinfo->while_stepping)
3773 head = tinfo->while_stepping;
3774 tinfo->while_stepping = head->next;
3775 release_while_stepping_state (head);
3779 /* If TINFO was handling a 'while-stepping' action, the step has
3780 finished, so collect any step data needed, and check if any more
3781 steps are required. Return true if the thread was indeed
3782 collecting tracepoint data, false otherwise. */
3785 tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
3787 struct tracepoint *tpoint;
3788 struct wstep_state *wstep;
3789 struct wstep_state **wstep_link;
3790 struct trap_tracepoint_ctx ctx;
3792 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3794 if (in_process_agent_loaded ())
3795 upload_fast_traceframes ();
3797 /* Check if we were indeed collecting data for one of more
3798 tracepoints with a 'while-stepping' count. */
3799 if (tinfo->while_stepping == NULL)
3804 /* We're not even tracing anymore. Stop this thread from
3806 release_while_stepping_state_list (tinfo);
3808 /* The thread had stopped due to a single-step request indeed
3809 explained by a tracepoint. */
3813 wstep = tinfo->while_stepping;
3814 wstep_link = &tinfo->while_stepping;
3816 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3817 target_pid_to_str (tinfo->entry.id),
3818 wstep->tp_number, paddress (wstep->tp_address));
3820 ctx.base.type = trap_tracepoint;
3821 ctx.regcache = get_thread_regcache (tinfo, 1);
3823 while (wstep != NULL)
3825 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
3828 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3829 wstep->tp_number, paddress (wstep->tp_address),
3830 target_pid_to_str (tinfo->entry.id));
3833 *wstep_link = wstep->next;
3834 release_while_stepping_state (wstep);
3835 wstep = *wstep_link;
3839 /* We've just finished one step. */
3840 ++wstep->current_step;
3843 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
3844 stop_pc, tpoint, wstep->current_step);
3846 if (wstep->current_step >= tpoint->step_count)
3848 /* The requested numbers of steps have occurred. */
3849 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3850 target_pid_to_str (tinfo->entry.id),
3851 wstep->tp_number, paddress (wstep->tp_address));
3853 /* Unlink the wstep. */
3854 *wstep_link = wstep->next;
3855 release_while_stepping_state (wstep);
3856 wstep = *wstep_link;
3858 /* Only check the hit count now, which ensure that we do all
3859 our stepping before stopping the run. */
3860 if (tpoint->pass_count > 0
3861 && tpoint->hit_count >= tpoint->pass_count
3862 && stopping_tracepoint == NULL)
3863 stopping_tracepoint = tpoint;
3867 /* Keep single-stepping until the requested numbers of steps
3869 wstep_link = &wstep->next;
3870 wstep = *wstep_link;
3873 if (stopping_tracepoint
3874 || trace_buffer_is_full
3875 || expr_eval_result != expr_eval_no_error)
3885 /* Handle any internal tracing control breakpoint hits. That means,
3886 pull traceframes from the IPA to our buffer, and syncing both
3887 tracing agents when the IPA's tracing stops for some reason. */
3890 handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
3892 /* Pull in fast tracepoint trace frames from the inferior in-process
3893 agent's buffer into our buffer. */
3895 if (!in_process_agent_loaded ())
3898 upload_fast_traceframes ();
3900 /* Check if the in-process agent had decided we should stop
3902 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
3904 int ipa_trace_buffer_is_full;
3905 CORE_ADDR ipa_stopping_tracepoint;
3906 int ipa_expr_eval_result;
3907 CORE_ADDR ipa_error_tracepoint;
3909 trace_debug ("lib stopped at stop_tracing");
3911 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
3912 &ipa_trace_buffer_is_full);
3914 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3915 &ipa_stopping_tracepoint);
3916 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
3918 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
3919 &ipa_error_tracepoint);
3920 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
3922 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
3923 &ipa_expr_eval_result);
3924 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
3926 trace_debug ("lib: trace_buffer_is_full: %d, "
3927 "stopping_tracepoint: %s, "
3928 "ipa_expr_eval_result: %d, "
3929 "error_tracepoint: %s, ",
3930 ipa_trace_buffer_is_full,
3931 paddress (ipa_stopping_tracepoint),
3932 ipa_expr_eval_result,
3933 paddress (ipa_error_tracepoint));
3937 if (ipa_trace_buffer_is_full)
3938 trace_debug ("lib stopped due to full buffer.");
3939 if (ipa_stopping_tracepoint)
3940 trace_debug ("lib stopped due to tpoint");
3941 if (ipa_stopping_tracepoint)
3942 trace_debug ("lib stopped due to error");
3945 if (ipa_stopping_tracepoint != 0)
3948 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
3950 else if (ipa_expr_eval_result != expr_eval_no_error)
3952 expr_eval_result = ipa_expr_eval_result;
3954 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
3959 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
3961 trace_debug ("lib stopped at flush_trace_buffer");
3968 /* Return true if TINFO just hit a tracepoint. Collect data if
3972 tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
3974 struct tracepoint *tpoint;
3976 struct trap_tracepoint_ctx ctx;
3978 /* Not tracing, don't handle. */
3982 ctx.base.type = trap_tracepoint;
3983 ctx.regcache = get_thread_regcache (tinfo, 1);
3985 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3987 /* Note that we collect fast tracepoints here as well. We'll
3988 step over the fast tracepoint jump later, which avoids the
3990 if (tpoint->enabled && stop_pc == tpoint->address)
3992 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3993 target_pid_to_str (tinfo->entry.id),
3994 tpoint->number, paddress (tpoint->address));
3996 /* Test the condition if present, and collect if true. */
3998 || (condition_true_at_tracepoint
3999 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4000 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4003 if (stopping_tracepoint
4004 || trace_buffer_is_full
4005 || expr_eval_result != expr_eval_no_error)
4009 /* If the tracepoint had a 'while-stepping' action, then set
4010 the thread to collect this tracepoint on the following
4012 else if (tpoint->step_count > 0)
4014 add_while_stepping_state (tinfo,
4015 tpoint->number, tpoint->address);
4027 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4028 struct ust_marker_data;
4029 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4031 struct tracepoint *tpoint,
4032 struct traceframe *tframe);
4035 /* Create a trace frame for the hit of the given tracepoint in the
4039 collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4040 struct tracepoint *tpoint)
4042 struct traceframe *tframe;
4045 /* Only count it as a hit when we actually collect data. */
4046 tpoint->hit_count++;
4048 /* If we've exceeded a defined pass count, record the event for
4049 later, and finish the collection for this hit. This test is only
4050 for nonstepping tracepoints, stepping tracepoints test at the end
4051 of their while-stepping loop. */
4052 if (tpoint->pass_count > 0
4053 && tpoint->hit_count >= tpoint->pass_count
4054 && tpoint->step_count == 0
4055 && stopping_tracepoint == NULL)
4056 stopping_tracepoint = tpoint;
4058 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4059 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4061 tframe = add_traceframe (tpoint);
4065 for (acti = 0; acti < tpoint->numactions; ++acti)
4067 #ifndef IN_PROCESS_AGENT
4068 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4069 tpoint->number, paddress (tpoint->address),
4070 tpoint->actions_str[acti]);
4073 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4074 tpoint->actions[acti]);
4077 finish_traceframe (tframe);
4080 if (tframe == NULL && tracing)
4081 trace_buffer_is_full = 1;
4084 #ifndef IN_PROCESS_AGENT
4087 collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4089 struct tracepoint *tpoint, int current_step)
4091 struct traceframe *tframe;
4094 trace_debug ("Making new step traceframe for "
4095 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4096 tpoint->number, paddress (tpoint->address),
4097 current_step, tpoint->step_count,
4100 tframe = add_traceframe (tpoint);
4104 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4106 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4107 tpoint->number, paddress (tpoint->address),
4108 tpoint->step_actions_str[acti]);
4110 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4111 tpoint->step_actions[acti]);
4114 finish_traceframe (tframe);
4117 if (tframe == NULL && tracing)
4118 trace_buffer_is_full = 1;
4123 static struct regcache *
4124 get_context_regcache (struct tracepoint_hit_ctx *ctx)
4126 struct regcache *regcache = NULL;
4128 #ifdef IN_PROCESS_AGENT
4129 if (ctx->type == fast_tracepoint)
4131 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4132 if (!fctx->regcache_initted)
4134 fctx->regcache_initted = 1;
4135 init_register_cache (&fctx->regcache, fctx->regspace);
4136 supply_regblock (&fctx->regcache, NULL);
4137 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4139 regcache = &fctx->regcache;
4142 if (ctx->type == static_tracepoint)
4144 struct static_tracepoint_ctx *sctx
4145 = (struct static_tracepoint_ctx *) ctx;
4147 if (!sctx->regcache_initted)
4149 sctx->regcache_initted = 1;
4150 init_register_cache (&sctx->regcache, sctx->regspace);
4151 supply_regblock (&sctx->regcache, NULL);
4152 /* Pass down the tracepoint address, because REGS doesn't
4153 include the PC, but we know what it must have been. */
4154 supply_static_tracepoint_registers (&sctx->regcache,
4155 (const unsigned char *)
4157 sctx->tpoint->address);
4159 regcache = &sctx->regcache;
4163 if (ctx->type == trap_tracepoint)
4165 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4166 regcache = tctx->regcache;
4170 gdb_assert (regcache != NULL);
4176 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4178 struct tracepoint *tpoint,
4179 struct traceframe *tframe,
4180 struct tracepoint_action *taction)
4182 enum eval_result_type err;
4184 switch (taction->type)
4188 struct collect_memory_action *maction;
4190 maction = (struct collect_memory_action *) taction;
4192 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4193 pulongest (maction->len),
4194 paddress (maction->addr), maction->basereg);
4195 /* (should use basereg) */
4196 agent_mem_read (tframe, NULL,
4197 (CORE_ADDR) maction->addr, maction->len);
4202 unsigned char *regspace;
4203 struct regcache tregcache;
4204 struct regcache *context_regcache;
4207 trace_debug ("Want to collect registers");
4209 /* Collect all registers for now. */
4210 regspace = add_traceframe_block (tframe,
4211 1 + register_cache_size ());
4212 if (regspace == NULL)
4214 trace_debug ("Trace buffer block allocation failed, skipping");
4217 /* Identify a register block. */
4220 context_regcache = get_context_regcache (ctx);
4222 /* Wrap the regblock in a register cache (in the stack, we
4223 don't want to malloc here). */
4224 init_register_cache (&tregcache, regspace + 1);
4226 /* Copy the register data to the regblock. */
4227 regcache_cpy (&tregcache, context_regcache);
4229 #ifndef IN_PROCESS_AGENT
4230 /* On some platforms, trap-based tracepoints will have the PC
4231 pointing to the next instruction after the trap, but we
4232 don't want the user or GDB trying to guess whether the
4233 saved PC needs adjusting; so always record the adjusted
4234 stop_pc. Note that we can't use tpoint->address instead,
4235 since it will be wrong for while-stepping actions. This
4236 adjustment is a nop for fast tracepoints collected from the
4237 in-process lib (but not if GDBserver is collecting one
4238 preemptively), since the PC had already been adjusted to
4239 contain the tracepoint's address by the jump pad. */
4240 trace_debug ("Storing stop pc (0x%s) in regblock",
4241 paddress (tpoint->address));
4243 /* This changes the regblock, not the thread's
4245 regcache_write_pc (&tregcache, stop_pc);
4251 struct eval_expr_action *eaction;
4253 eaction = (struct eval_expr_action *) taction;
4255 trace_debug ("Want to evaluate expression");
4257 err = eval_agent_expr (ctx, tframe, eaction->expr, NULL);
4259 if (err != expr_eval_no_error)
4261 record_tracepoint_error (tpoint, "action expression", err);
4268 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4269 trace_debug ("Want to collect static trace data");
4270 collect_ust_data_at_tracepoint (ctx, stop_pc,
4273 trace_debug ("warning: collecting static trace data, "
4274 "but static tracepoints are not supported");
4279 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4285 condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4286 struct tracepoint *tpoint)
4289 enum eval_result_type err;
4291 /* Presently, gdbserver doesn't run compiled conditions, only the
4292 IPA does. If the program stops at a fast tracepoint's address
4293 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4294 gdbserver preemptively collect the fast tracepoint. Later, on
4295 resume, gdbserver steps over the fast tracepoint like it steps
4296 over breakpoints, so that the IPA doesn't see that fast
4297 tracepoint. This avoids double collects of fast tracepoints in
4298 that stopping scenario. Having gdbserver itself handle the fast
4299 tracepoint gives the user a consistent view of when fast or trap
4300 tracepoints are collected, compared to an alternative where only
4301 trap tracepoints are collected on stop, and fast tracepoints on
4302 resume. When a fast tracepoint is being processed by gdbserver,
4303 it is always the non-compiled condition expression that is
4305 #ifdef IN_PROCESS_AGENT
4306 if (tpoint->compiled_cond)
4307 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4310 err = eval_agent_expr (ctx, NULL, tpoint->cond, &value);
4312 if (err != expr_eval_no_error)
4314 record_tracepoint_error (tpoint, "condition", err);
4315 /* The error case must return false. */
4319 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4320 tpoint->number, paddress (tpoint->address),
4322 return (value ? 1 : 0);
4325 #ifndef IN_PROCESS_AGENT
4327 /* The packet form of an agent expression consists of an 'X', number
4328 of bytes in expression, a comma, and then the bytes. */
4330 static struct agent_expr *
4331 parse_agent_expr (char **actparm)
4333 char *act = *actparm;
4335 struct agent_expr *aexpr;
4337 ++act; /* skip the X */
4338 act = unpack_varlen_hex (act, &xlen);
4339 ++act; /* skip a comma */
4340 aexpr = xmalloc (sizeof (struct agent_expr));
4341 aexpr->length = xlen;
4342 aexpr->bytes = xmalloc (xlen);
4343 convert_ascii_to_int (act, aexpr->bytes, xlen);
4344 *actparm = act + (xlen * 2);
4348 /* Convert the bytes of an agent expression back into hex digits, so
4349 they can be printed or uploaded. This allocates the buffer,
4350 callers should free when they are done with it. */
4353 unparse_agent_expr (struct agent_expr *aexpr)
4357 rslt = xmalloc (2 * aexpr->length + 1);
4358 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
4364 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4367 gdb_agent_op_name (int op)
4369 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
4371 return gdb_agent_op_names[op];
4374 /* The agent expression evaluator, as specified by the GDB docs. It
4375 returns 0 if everything went OK, and a nonzero error code
4378 static enum eval_result_type
4379 eval_agent_expr (struct tracepoint_hit_ctx *ctx,
4380 struct traceframe *tframe,
4381 struct agent_expr *aexpr,
4385 #define STACK_MAX 100
4386 ULONGEST stack[STACK_MAX], top;
4391 /* This union is a convenient way to convert representations. For
4392 now, assume a standard architecture where the hardware integer
4393 types have 8, 16, 32, 64 bit types. A more robust solution would
4394 be to import stdint.h from gnulib. */
4399 unsigned char bytes[1];
4404 unsigned char bytes[2];
4409 unsigned char bytes[4];
4414 unsigned char bytes[8];
4419 if (aexpr->length == 0)
4421 trace_debug ("empty agent expression");
4422 return expr_eval_empty_expression;
4425 /* Cache the stack top in its own variable. Much of the time we can
4426 operate on this variable, rather than dinking with the stack. It
4427 needs to be copied to the stack when sp changes. */
4432 op = aexpr->bytes[pc++];
4434 trace_debug ("About to interpret byte 0x%x", op);
4438 case gdb_agent_op_add:
4442 case gdb_agent_op_sub:
4443 top = stack[--sp] - top;
4446 case gdb_agent_op_mul:
4450 case gdb_agent_op_div_signed:
4453 trace_debug ("Attempted to divide by zero");
4454 return expr_eval_divide_by_zero;
4456 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
4459 case gdb_agent_op_div_unsigned:
4462 trace_debug ("Attempted to divide by zero");
4463 return expr_eval_divide_by_zero;
4465 top = stack[--sp] / top;
4468 case gdb_agent_op_rem_signed:
4471 trace_debug ("Attempted to divide by zero");
4472 return expr_eval_divide_by_zero;
4474 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
4477 case gdb_agent_op_rem_unsigned:
4480 trace_debug ("Attempted to divide by zero");
4481 return expr_eval_divide_by_zero;
4483 top = stack[--sp] % top;
4486 case gdb_agent_op_lsh:
4487 top = stack[--sp] << top;
4490 case gdb_agent_op_rsh_signed:
4491 top = ((LONGEST) stack[--sp]) >> top;
4494 case gdb_agent_op_rsh_unsigned:
4495 top = stack[--sp] >> top;
4498 case gdb_agent_op_trace:
4499 agent_mem_read (tframe,
4500 NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top);
4505 case gdb_agent_op_trace_quick:
4506 arg = aexpr->bytes[pc++];
4507 agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg);
4510 case gdb_agent_op_log_not:
4514 case gdb_agent_op_bit_and:
4518 case gdb_agent_op_bit_or:
4522 case gdb_agent_op_bit_xor:
4526 case gdb_agent_op_bit_not:
4530 case gdb_agent_op_equal:
4531 top = (stack[--sp] == top);
4534 case gdb_agent_op_less_signed:
4535 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
4538 case gdb_agent_op_less_unsigned:
4539 top = (stack[--sp] < top);
4542 case gdb_agent_op_ext:
4543 arg = aexpr->bytes[pc++];
4544 if (arg < (sizeof (LONGEST) * 8))
4546 LONGEST mask = 1 << (arg - 1);
4547 top &= ((LONGEST) 1 << arg) - 1;
4548 top = (top ^ mask) - mask;
4552 case gdb_agent_op_ref8:
4553 agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1);
4557 case gdb_agent_op_ref16:
4558 agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2);
4562 case gdb_agent_op_ref32:
4563 agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4);
4567 case gdb_agent_op_ref64:
4568 agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8);
4572 case gdb_agent_op_if_goto:
4574 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4581 case gdb_agent_op_goto:
4582 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4585 case gdb_agent_op_const8:
4586 /* Flush the cached stack top. */
4588 top = aexpr->bytes[pc++];
4591 case gdb_agent_op_const16:
4592 /* Flush the cached stack top. */
4594 top = aexpr->bytes[pc++];
4595 top = (top << 8) + aexpr->bytes[pc++];
4598 case gdb_agent_op_const32:
4599 /* Flush the cached stack top. */
4601 top = aexpr->bytes[pc++];
4602 top = (top << 8) + aexpr->bytes[pc++];
4603 top = (top << 8) + aexpr->bytes[pc++];
4604 top = (top << 8) + aexpr->bytes[pc++];
4607 case gdb_agent_op_const64:
4608 /* Flush the cached stack top. */
4610 top = aexpr->bytes[pc++];
4611 top = (top << 8) + aexpr->bytes[pc++];
4612 top = (top << 8) + aexpr->bytes[pc++];
4613 top = (top << 8) + aexpr->bytes[pc++];
4614 top = (top << 8) + aexpr->bytes[pc++];
4615 top = (top << 8) + aexpr->bytes[pc++];
4616 top = (top << 8) + aexpr->bytes[pc++];
4617 top = (top << 8) + aexpr->bytes[pc++];
4620 case gdb_agent_op_reg:
4621 /* Flush the cached stack top. */
4623 arg = aexpr->bytes[pc++];
4624 arg = (arg << 8) + aexpr->bytes[pc++];
4627 struct regcache *regcache;
4629 regcache = get_context_regcache (ctx);
4631 switch (register_size (regnum))
4634 collect_register (regcache, regnum, cnv.u64.bytes);
4638 collect_register (regcache, regnum, cnv.u32.bytes);
4642 collect_register (regcache, regnum, cnv.u16.bytes);
4646 collect_register (regcache, regnum, cnv.u8.bytes);
4650 internal_error (__FILE__, __LINE__,
4651 "unhandled register size");
4656 case gdb_agent_op_end:
4657 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4658 sp, pulongest (top));
4663 /* This should be an error */
4664 trace_debug ("Stack is empty, nothing to return");
4665 return expr_eval_empty_stack;
4669 return expr_eval_no_error;
4671 case gdb_agent_op_dup:
4675 case gdb_agent_op_pop:
4680 case gdb_agent_op_pick:
4681 arg = aexpr->bytes[pc++];
4683 top = stack[sp - arg];
4687 case gdb_agent_op_rot:
4689 ULONGEST tem = stack[sp - 1];
4691 stack[sp - 1] = stack[sp - 2];
4692 stack[sp - 2] = top;
4697 case gdb_agent_op_zero_ext:
4698 arg = aexpr->bytes[pc++];
4699 if (arg < (sizeof (LONGEST) * 8))
4700 top &= ((LONGEST) 1 << arg) - 1;
4703 case gdb_agent_op_swap:
4704 /* Interchange top two stack elements, making sure top gets
4705 copied back onto stack. */
4707 top = stack[sp - 1];
4708 stack[sp - 1] = stack[sp];
4711 case gdb_agent_op_getv:
4712 /* Flush the cached stack top. */
4714 arg = aexpr->bytes[pc++];
4715 arg = (arg << 8) + aexpr->bytes[pc++];
4716 top = get_trace_state_variable_value (arg);
4719 case gdb_agent_op_setv:
4720 arg = aexpr->bytes[pc++];
4721 arg = (arg << 8) + aexpr->bytes[pc++];
4722 set_trace_state_variable_value (arg, top);
4723 /* Note that we leave the value on the stack, for the
4724 benefit of later/enclosing expressions. */
4727 case gdb_agent_op_tracev:
4728 arg = aexpr->bytes[pc++];
4729 arg = (arg << 8) + aexpr->bytes[pc++];
4730 agent_tsv_read (tframe, arg);
4733 case gdb_agent_op_tracenz:
4734 agent_mem_read_string (tframe, NULL, (CORE_ADDR) stack[--sp],
4740 /* GDB never (currently) generates any of these ops. */
4741 case gdb_agent_op_float:
4742 case gdb_agent_op_ref_float:
4743 case gdb_agent_op_ref_double:
4744 case gdb_agent_op_ref_long_double:
4745 case gdb_agent_op_l_to_d:
4746 case gdb_agent_op_d_to_l:
4747 case gdb_agent_op_trace16:
4748 trace_debug ("Agent expression op 0x%x valid, but not handled",
4750 /* If ever GDB generates any of these, we don't have the
4751 option of ignoring. */
4755 trace_debug ("Agent expression op 0x%x not recognized", op);
4756 /* Don't struggle on, things will just get worse. */
4757 return expr_eval_unrecognized_opcode;
4760 /* Check for stack badness. */
4761 if (sp >= (STACK_MAX - 1))
4763 trace_debug ("Expression stack overflow");
4764 return expr_eval_stack_overflow;
4769 trace_debug ("Expression stack underflow");
4770 return expr_eval_stack_underflow;
4773 trace_debug ("Op %s -> sp=%d, top=0x%s",
4774 gdb_agent_op_name (op), sp, pulongest (top));
4778 /* Do memory copies for bytecodes. */
4779 /* Do the recording of memory blocks for actions and bytecodes. */
4782 agent_mem_read (struct traceframe *tframe,
4783 unsigned char *to, CORE_ADDR from, ULONGEST len)
4785 unsigned char *mspace;
4786 ULONGEST remaining = len;
4787 unsigned short blocklen;
4789 /* If a 'to' buffer is specified, use it. */
4792 read_inferior_memory (from, to, len);
4796 /* Otherwise, create a new memory block in the trace buffer. */
4797 while (remaining > 0)
4801 blocklen = (remaining > 65535 ? 65535 : remaining);
4802 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4803 mspace = add_traceframe_block (tframe, sp);
4806 /* Identify block as a memory block. */
4809 /* Record address and size. */
4810 memcpy (mspace, &from, sizeof (from));
4811 mspace += sizeof (from);
4812 memcpy (mspace, &blocklen, sizeof (blocklen));
4813 mspace += sizeof (blocklen);
4814 /* Record the memory block proper. */
4815 read_inferior_memory (from, mspace, blocklen);
4816 trace_debug ("%d bytes recorded", blocklen);
4817 remaining -= blocklen;
4824 agent_mem_read_string (struct traceframe *tframe,
4825 unsigned char *to, CORE_ADDR from, ULONGEST len)
4827 unsigned char *buf, *mspace;
4828 ULONGEST remaining = len;
4829 unsigned short blocklen, i;
4831 /* To save a bit of space, block lengths are 16-bit, so break large
4832 requests into multiple blocks. Bordering on overkill for strings,
4833 but it could happen that someone specifies a large max length. */
4834 while (remaining > 0)
4838 blocklen = (remaining > 65535 ? 65535 : remaining);
4839 /* We want working space to accumulate nonzero bytes, since
4840 traceframes must have a predecided size (otherwise it gets
4841 harder to wrap correctly for the circular case, etc). */
4842 buf = (unsigned char *) xmalloc (blocklen + 1);
4843 for (i = 0; i < blocklen; ++i)
4845 /* Read the string one byte at a time, in case the string is
4846 at the end of a valid memory area - we don't want a
4847 correctly-terminated string to engender segvio
4849 read_inferior_memory (from + i, buf + i, 1);
4854 /* Make sure outer loop stops now too. */
4855 remaining = blocklen;
4859 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4860 mspace = add_traceframe_block (tframe, sp);
4866 /* Identify block as a memory block. */
4869 /* Record address and size. */
4870 memcpy ((void *) mspace, (void *) &from, sizeof (from));
4871 mspace += sizeof (from);
4872 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
4873 mspace += sizeof (blocklen);
4874 /* Copy the string contents. */
4875 memcpy ((void *) mspace, (void *) buf, blocklen);
4876 remaining -= blocklen;
4883 /* Record the value of a trace state variable. */
4886 agent_tsv_read (struct traceframe *tframe, int n)
4888 unsigned char *vspace;
4891 vspace = add_traceframe_block (tframe,
4892 1 + sizeof (n) + sizeof (LONGEST));
4895 /* Identify block as a variable. */
4897 /* Record variable's number and value. */
4898 memcpy (vspace + 1, &n, sizeof (n));
4899 val = get_trace_state_variable_value (n);
4900 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
4901 trace_debug ("Variable %d recorded", n);
4905 #ifndef IN_PROCESS_AGENT
4907 /* Callback for traceframe_walk_blocks, used to find a given block
4908 type in a traceframe. */
4911 match_blocktype (char blocktype, unsigned char *dataptr, void *data)
4913 char *wantedp = data;
4915 if (*wantedp == blocktype)
4921 /* Walk over all traceframe blocks of the traceframe buffer starting
4922 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4923 block found, passing in DATA unmodified. If CALLBACK returns true,
4924 this returns a pointer to where the block is found. Returns NULL
4925 if no callback call returned true, indicating that all blocks have
4928 static unsigned char *
4929 traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
4931 int (*callback) (char blocktype,
4932 unsigned char *dataptr,
4936 unsigned char *dataptr;
4940 trace_debug ("traceframe %d has no data", tfnum);
4944 /* Iterate through a traceframe's blocks, looking for a block of the
4946 for (dataptr = database;
4947 dataptr < database + datasize;
4951 unsigned short mlen;
4953 if (dataptr == trace_buffer_wrap)
4955 /* Adjust to reflect wrapping part of the frame around to
4957 datasize = dataptr - database;
4958 dataptr = database = trace_buffer_lo;
4961 blocktype = *dataptr++;
4963 if ((*callback) (blocktype, dataptr, data))
4969 /* Skip over the registers block. */
4970 dataptr += register_cache_size ();
4973 /* Skip over the memory block. */
4974 dataptr += sizeof (CORE_ADDR);
4975 memcpy (&mlen, dataptr, sizeof (mlen));
4976 dataptr += (sizeof (mlen) + mlen);
4979 /* Skip over the TSV block. */
4980 dataptr += (sizeof (int) + sizeof (LONGEST));
4983 /* Skip over the static trace data block. */
4984 memcpy (&mlen, dataptr, sizeof (mlen));
4985 dataptr += (sizeof (mlen) + mlen);
4988 trace_debug ("traceframe %d has unknown block type 0x%x",
4997 /* Look for the block of type TYPE_WANTED in the trameframe starting
4998 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5001 static unsigned char *
5002 traceframe_find_block_type (unsigned char *database, unsigned int datasize,
5003 int tfnum, char type_wanted)
5005 return traceframe_walk_blocks (database, datasize, tfnum,
5006 match_blocktype, &type_wanted);
5009 static unsigned char *
5010 traceframe_find_regblock (struct traceframe *tframe, int tfnum)
5012 unsigned char *regblock;
5014 regblock = traceframe_find_block_type (tframe->data,
5018 if (regblock == NULL)
5019 trace_debug ("traceframe %d has no register data", tfnum);
5024 /* Get registers from a traceframe. */
5027 fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
5029 unsigned char *dataptr;
5030 struct tracepoint *tpoint;
5031 struct traceframe *tframe;
5033 tframe = find_traceframe (tfnum);
5037 trace_debug ("traceframe %d not found", tfnum);
5041 dataptr = traceframe_find_regblock (tframe, tfnum);
5042 if (dataptr == NULL)
5044 /* Mark registers unavailable. */
5045 supply_regblock (regcache, NULL);
5047 /* We can generally guess at a PC, although this will be
5048 misleading for while-stepping frames and multi-location
5050 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
5052 regcache_write_pc (regcache, tpoint->address);
5055 supply_regblock (regcache, dataptr);
5061 traceframe_get_pc (struct traceframe *tframe)
5063 struct regcache regcache;
5064 unsigned char *dataptr;
5066 dataptr = traceframe_find_regblock (tframe, -1);
5067 if (dataptr == NULL)
5070 init_register_cache (®cache, dataptr);
5071 return regcache_read_pc (®cache);
5074 /* Read a requested block of memory from a trace frame. */
5077 traceframe_read_mem (int tfnum, CORE_ADDR addr,
5078 unsigned char *buf, ULONGEST length,
5081 struct traceframe *tframe;
5082 unsigned char *database, *dataptr;
5083 unsigned int datasize;
5085 unsigned short mlen;
5087 trace_debug ("traceframe_read_mem");
5089 tframe = find_traceframe (tfnum);
5093 trace_debug ("traceframe %d not found", tfnum);
5097 datasize = tframe->data_size;
5098 database = dataptr = &tframe->data[0];
5100 /* Iterate through a traceframe's blocks, looking for memory. */
5101 while ((dataptr = traceframe_find_block_type (dataptr,
5103 - (dataptr - database),
5104 tfnum, 'M')) != NULL)
5106 memcpy (&maddr, dataptr, sizeof (maddr));
5107 dataptr += sizeof (maddr);
5108 memcpy (&mlen, dataptr, sizeof (mlen));
5109 dataptr += sizeof (mlen);
5110 trace_debug ("traceframe %d has %d bytes at %s",
5111 tfnum, mlen, paddress (maddr));
5113 /* If the block includes the first part of the desired range,
5114 return as much it has; GDB will re-request the remainder,
5115 which might be in a different block of this trace frame. */
5116 if (maddr <= addr && addr < (maddr + mlen))
5118 ULONGEST amt = (maddr + mlen) - addr;
5122 memcpy (buf, dataptr + (addr - maddr), amt);
5127 /* Skip over this block. */
5131 trace_debug ("traceframe %d has no memory data for the desired region",
5139 traceframe_read_tsv (int tsvnum, LONGEST *val)
5142 struct traceframe *tframe;
5143 unsigned char *database, *dataptr;
5144 unsigned int datasize;
5147 trace_debug ("traceframe_read_tsv");
5149 tfnum = current_traceframe;
5153 trace_debug ("no current traceframe");
5157 tframe = find_traceframe (tfnum);
5161 trace_debug ("traceframe %d not found", tfnum);
5165 datasize = tframe->data_size;
5166 database = dataptr = &tframe->data[0];
5168 /* Iterate through a traceframe's blocks, looking for the tsv. */
5169 while ((dataptr = traceframe_find_block_type (dataptr,
5171 - (dataptr - database),
5172 tfnum, 'V')) != NULL)
5174 memcpy (&vnum, dataptr, sizeof (vnum));
5175 dataptr += sizeof (vnum);
5177 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
5179 /* Check that this is the variable we want. */
5182 memcpy (val, dataptr, sizeof (*val));
5186 /* Skip over this block. */
5187 dataptr += sizeof (LONGEST);
5190 trace_debug ("traceframe %d has no data for variable %d",
5195 /* Read a requested block of static tracepoint data from a trace
5199 traceframe_read_sdata (int tfnum, ULONGEST offset,
5200 unsigned char *buf, ULONGEST length,
5203 struct traceframe *tframe;
5204 unsigned char *database, *dataptr;
5205 unsigned int datasize;
5206 unsigned short mlen;
5208 trace_debug ("traceframe_read_sdata");
5210 tframe = find_traceframe (tfnum);
5214 trace_debug ("traceframe %d not found", tfnum);
5218 datasize = tframe->data_size;
5219 database = &tframe->data[0];
5221 /* Iterate through a traceframe's blocks, looking for static
5223 dataptr = traceframe_find_block_type (database, datasize,
5225 if (dataptr != NULL)
5227 memcpy (&mlen, dataptr, sizeof (mlen));
5228 dataptr += sizeof (mlen);
5231 if (offset + length > mlen)
5232 length = mlen - offset;
5234 memcpy (buf, dataptr, length);
5242 trace_debug ("traceframe %d has no static trace data", tfnum);
5248 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5249 object. DATA is pointer to a struct buffer holding the
5250 traceframe-info object being built. */
5253 build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5255 struct buffer *buffer = data;
5261 unsigned short mlen;
5264 memcpy (&maddr, dataptr, sizeof (maddr));
5265 dataptr += sizeof (maddr);
5266 memcpy (&mlen, dataptr, sizeof (mlen));
5267 dataptr += sizeof (mlen);
5268 buffer_xml_printf (buffer,
5269 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5270 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5280 warning ("Unhandled trace block type (%d) '%c ' "
5281 "while building trace frame info.",
5282 blocktype, blocktype);
5289 /* Build a traceframe-info object for traceframe number TFNUM into
5293 traceframe_read_info (int tfnum, struct buffer *buffer)
5295 struct traceframe *tframe;
5297 trace_debug ("traceframe_read_info");
5299 tframe = find_traceframe (tfnum);
5303 trace_debug ("traceframe %d not found", tfnum);
5307 buffer_grow_str (buffer, "<traceframe-info>\n");
5308 traceframe_walk_blocks (tframe->data, tframe->data_size,
5309 tfnum, build_traceframe_info_xml, buffer);
5310 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5314 /* Return the first fast tracepoint whose jump pad contains PC. */
5316 static struct tracepoint *
5317 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5319 struct tracepoint *tpoint;
5321 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5322 if (tpoint->type == fast_tracepoint)
5323 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5329 /* Return GDBserver's tracepoint that matches the IP Agent's
5330 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5333 static struct tracepoint *
5334 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5336 struct tracepoint *tpoint;
5338 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5339 if (tpoint->type == fast_tracepoint)
5340 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5348 /* The type of the object that is used to synchronize fast tracepoint
5351 typedef struct collecting_t
5353 /* The fast tracepoint number currently collecting. */
5356 /* A number that GDBserver can use to identify the thread that is
5357 presently holding the collect lock. This need not (and usually
5358 is not) the thread id, as getting the current thread ID usually
5359 requires a system call, which we want to avoid like the plague.
5360 Usually this is thread's TCB, found in the TLS (pseudo-)
5361 register, which is readable with a single insn on several
5363 uintptr_t thread_area;
5366 #ifndef IN_PROCESS_AGENT
5369 force_unlock_trace_buffer (void)
5371 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5374 /* Check if the thread identified by THREAD_AREA which is stopped at
5375 STOP_PC, is presently locking the fast tracepoint collection, and
5376 if so, gather some status of said collection. Returns 0 if the
5377 thread isn't collecting or in the jump pad at all. 1, if in the
5378 jump pad (or within gdb_collect) and hasn't executed the adjusted
5379 original insn yet (can set a breakpoint there and run to it). 2,
5380 if presently executing the adjusted original insn --- in which
5381 case, if we want to move the thread out of the jump pad, we need to
5382 single-step it until this function returns 0. */
5385 fast_tracepoint_collecting (CORE_ADDR thread_area,
5387 struct fast_tpoint_collect_status *status)
5389 CORE_ADDR ipa_collecting;
5390 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
5391 struct tracepoint *tpoint;
5392 int needs_breakpoint;
5394 /* The thread THREAD_AREA is either:
5396 0. not collecting at all, not within the jump pad, or within
5397 gdb_collect or one of its callees.
5399 1. in the jump pad and haven't reached gdb_collect
5401 2. within gdb_collect (out of the jump pad) (collect is set)
5403 3. we're in the jump pad, after gdb_collect having returned,
5404 possibly executing the adjusted insns.
5406 For cases 1 and 3, `collecting' may or not be set. The jump pad
5407 doesn't have any complicated jump logic, so we can tell if the
5408 thread is executing the adjust original insn or not by just
5409 matching STOP_PC with known jump pad addresses. If we it isn't
5410 yet executing the original insn, set a breakpoint there, and let
5411 the thread run to it, so to quickly step over a possible (many
5412 insns) gdb_collect call. Otherwise, or when the breakpoint is
5413 hit, only a few (small number of) insns are left to be executed
5414 in the jump pad. Single-step the thread until it leaves the
5419 needs_breakpoint = 0;
5420 trace_debug ("fast_tracepoint_collecting");
5422 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5423 &ipa_gdb_jump_pad_buffer))
5424 fatal ("error extracting `gdb_jump_pad_buffer'");
5425 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5426 &ipa_gdb_jump_pad_buffer_end))
5427 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5429 if (ipa_gdb_jump_pad_buffer <= stop_pc
5430 && stop_pc < ipa_gdb_jump_pad_buffer_end)
5432 /* We can tell which tracepoint(s) the thread is collecting by
5433 matching the jump pad address back to the tracepoint. */
5434 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5437 warning ("in jump pad, but no matching tpoint?");
5442 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5444 tpoint->number, paddress (tpoint->address),
5445 paddress (tpoint->jump_pad),
5446 paddress (tpoint->jump_pad_end),
5447 paddress (tpoint->adjusted_insn_addr),
5448 paddress (tpoint->adjusted_insn_addr_end));
5451 /* Definitely in the jump pad. May or may not need
5452 fast-exit-jump-pad breakpoint. */
5453 if (tpoint->jump_pad <= stop_pc
5454 && stop_pc < tpoint->adjusted_insn_addr)
5455 needs_breakpoint = 1;
5459 collecting_t ipa_collecting_obj;
5461 /* If `collecting' is set/locked, then the THREAD_AREA thread
5462 may or not be the one holding the lock. We have to read the
5463 lock to find out. */
5465 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5468 trace_debug ("fast_tracepoint_collecting:"
5469 " failed reading 'collecting' in the inferior");
5473 if (!ipa_collecting)
5475 trace_debug ("fast_tracepoint_collecting: not collecting"
5476 " (and nobody is).");
5480 /* Some thread is collecting. Check which. */
5481 if (read_inferior_memory (ipa_collecting,
5482 (unsigned char *) &ipa_collecting_obj,
5483 sizeof (ipa_collecting_obj)) != 0)
5486 if (ipa_collecting_obj.thread_area != thread_area)
5488 trace_debug ("fast_tracepoint_collecting: not collecting "
5489 "(another thread is)");
5494 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5497 warning ("fast_tracepoint_collecting: collecting, "
5498 "but tpoint %s not found?",
5499 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5503 /* The thread is within `gdb_collect', skip over the rest of
5504 fast tracepoint collection quickly using a breakpoint. */
5505 needs_breakpoint = 1;
5508 /* The caller wants a bit of status detail. */
5511 status->tpoint_num = tpoint->number;
5512 status->tpoint_addr = tpoint->address;
5513 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5514 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5517 if (needs_breakpoint)
5519 /* Hasn't executed the original instruction yet. Set breakpoint
5520 there, and wait till it's hit, then single-step until exiting
5524 fast_tracepoint_collecting, returning continue-until-break at %s",
5525 paddress (tpoint->adjusted_insn_addr));
5527 return 1; /* continue */
5531 /* Just single-step until exiting the jump pad. */
5533 trace_debug ("fast_tracepoint_collecting, returning "
5534 "need-single-step (%s-%s)",
5535 paddress (tpoint->adjusted_insn_addr),
5536 paddress (tpoint->adjusted_insn_addr_end));
5538 return 2; /* single-step */
5544 #ifdef IN_PROCESS_AGENT
5546 /* The global fast tracepoint collect lock. Points to a collecting_t
5547 object built on the stack by the jump pad, if presently locked;
5548 NULL if it isn't locked. Note that this lock *must* be set while
5549 executing any *function other than the jump pad. See
5550 fast_tracepoint_collecting. */
5551 static collecting_t * ATTR_USED collecting;
5553 /* This routine, called from the jump pad (in asm) is designed to be
5554 called from the jump pads of fast tracepoints, thus it is on the
5557 IP_AGENT_EXPORT void ATTR_USED
5558 gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5560 struct fast_tracepoint_ctx ctx;
5562 /* Don't do anything until the trace run is completely set up. */
5566 ctx.base.type = fast_tracepoint;
5568 ctx.regcache_initted = 0;
5569 /* Wrap the regblock in a register cache (in the stack, we don't
5570 want to malloc here). */
5571 ctx.regspace = alloca (register_cache_size ());
5572 if (ctx.regspace == NULL)
5574 trace_debug ("Trace buffer block allocation failed, skipping");
5578 for (ctx.tpoint = tpoint;
5579 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5580 ctx.tpoint = ctx.tpoint->next)
5582 if (!ctx.tpoint->enabled)
5585 /* Multiple tracepoints of different types, such as fast tracepoint and
5586 static tracepoint, can be set at the same address. */
5587 if (ctx.tpoint->type != tpoint->type)
5590 /* Test the condition if present, and collect if true. */
5591 if (ctx.tpoint->cond == NULL
5592 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5595 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5596 ctx.tpoint->address, ctx.tpoint);
5598 /* Note that this will cause original insns to be written back
5599 to where we jumped from, but that's OK because we're jumping
5600 back to the next whole instruction. This will go badly if
5601 instruction restoration is not atomic though. */
5602 if (stopping_tracepoint
5603 || trace_buffer_is_full
5604 || expr_eval_result != expr_eval_no_error)
5612 /* If there was a condition and it evaluated to false, the only
5613 way we would stop tracing is if there was an error during
5614 condition expression evaluation. */
5615 if (expr_eval_result != expr_eval_no_error)
5626 #ifndef IN_PROCESS_AGENT
5628 /* Bytecode compilation. */
5630 CORE_ADDR current_insn_ptr;
5634 struct bytecode_address
5639 /* Offset and size of field to be modified in the goto block. */
5640 int from_offset, from_size;
5641 struct bytecode_address *next;
5642 } *bytecode_address_table;
5645 get_raw_reg_func_addr (void)
5647 return ipa_sym_addrs.addr_get_raw_reg;
5651 emit_prologue (void)
5653 target_emit_ops ()->emit_prologue ();
5657 emit_epilogue (void)
5659 target_emit_ops ()->emit_epilogue ();
5665 target_emit_ops ()->emit_add ();
5671 target_emit_ops ()->emit_sub ();
5677 target_emit_ops ()->emit_mul ();
5683 target_emit_ops ()->emit_lsh ();
5687 emit_rsh_signed (void)
5689 target_emit_ops ()->emit_rsh_signed ();
5693 emit_rsh_unsigned (void)
5695 target_emit_ops ()->emit_rsh_unsigned ();
5701 target_emit_ops ()->emit_ext (arg);
5707 target_emit_ops ()->emit_log_not ();
5713 target_emit_ops ()->emit_bit_and ();
5719 target_emit_ops ()->emit_bit_or ();
5725 target_emit_ops ()->emit_bit_xor ();
5731 target_emit_ops ()->emit_bit_not ();
5737 target_emit_ops ()->emit_equal ();
5741 emit_less_signed (void)
5743 target_emit_ops ()->emit_less_signed ();
5747 emit_less_unsigned (void)
5749 target_emit_ops ()->emit_less_unsigned ();
5755 target_emit_ops ()->emit_ref (size);
5759 emit_if_goto (int *offset_p, int *size_p)
5761 target_emit_ops ()->emit_if_goto (offset_p, size_p);
5765 emit_goto (int *offset_p, int *size_p)
5767 target_emit_ops ()->emit_goto (offset_p, size_p);
5771 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
5773 target_emit_ops ()->write_goto_address (from, to, size);
5777 emit_const (LONGEST num)
5779 target_emit_ops ()->emit_const (num);
5785 target_emit_ops ()->emit_reg (reg);
5791 target_emit_ops ()->emit_pop ();
5795 emit_stack_flush (void)
5797 target_emit_ops ()->emit_stack_flush ();
5801 emit_zero_ext (int arg)
5803 target_emit_ops ()->emit_zero_ext (arg);
5809 target_emit_ops ()->emit_swap ();
5813 emit_stack_adjust (int n)
5815 target_emit_ops ()->emit_stack_adjust (n);
5818 /* FN's prototype is `LONGEST(*fn)(int)'. */
5821 emit_int_call_1 (CORE_ADDR fn, int arg1)
5823 target_emit_ops ()->emit_int_call_1 (fn, arg1);
5826 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
5829 emit_void_call_2 (CORE_ADDR fn, int arg1)
5831 target_emit_ops ()->emit_void_call_2 (fn, arg1);
5835 emit_eq_goto (int *offset_p, int *size_p)
5837 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
5841 emit_ne_goto (int *offset_p, int *size_p)
5843 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
5847 emit_lt_goto (int *offset_p, int *size_p)
5849 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
5853 emit_ge_goto (int *offset_p, int *size_p)
5855 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
5859 emit_gt_goto (int *offset_p, int *size_p)
5861 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
5865 emit_le_goto (int *offset_p, int *size_p)
5867 target_emit_ops ()->emit_le_goto (offset_p, size_p);
5870 static enum eval_result_type compile_bytecodes (struct agent_expr *aexpr);
5873 compile_tracepoint_condition (struct tracepoint *tpoint,
5874 CORE_ADDR *jump_entry)
5876 CORE_ADDR entry_point = *jump_entry;
5877 enum eval_result_type err;
5879 trace_debug ("Starting condition compilation for tracepoint %d\n",
5882 /* Initialize the global pointer to the code being built. */
5883 current_insn_ptr = *jump_entry;
5887 err = compile_bytecodes (tpoint->cond);
5889 if (err == expr_eval_no_error)
5893 /* Record the beginning of the compiled code. */
5894 tpoint->compiled_cond = entry_point;
5896 trace_debug ("Condition compilation for tracepoint %d complete\n",
5901 /* Leave the unfinished code in situ, but don't point to it. */
5903 tpoint->compiled_cond = 0;
5905 trace_debug ("Condition compilation for tracepoint %d failed, "
5907 tpoint->number, err);
5910 /* Update the code pointer passed in. Note that we do this even if
5911 the compile fails, so that we can look at the partial results
5912 instead of letting them be overwritten. */
5913 *jump_entry = current_insn_ptr;
5915 /* Leave a gap, to aid dump decipherment. */
5919 /* Scan an agent expression for any evidence that the given PC is the
5920 target of a jump bytecode in the expression. */
5923 is_goto_target (struct agent_expr *aexpr, int pc)
5928 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
5930 op = aexpr->bytes[i];
5932 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
5934 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
5943 /* Given an agent expression, turn it into native code. */
5945 static enum eval_result_type
5946 compile_bytecodes (struct agent_expr *aexpr)
5950 unsigned char op, next_op;
5952 /* This is only used to build 64-bit value for constants. */
5954 struct bytecode_address *aentry, *aentry2;
5959 trace_debug ("Cannot compile op 0x%x\n", op); \
5960 return expr_eval_unhandled_opcode; \
5963 if (aexpr->length == 0)
5965 trace_debug ("empty agent expression\n");
5966 return expr_eval_empty_expression;
5969 bytecode_address_table = NULL;
5973 op = aexpr->bytes[pc];
5975 trace_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
5977 /* Record the compiled-code address of the bytecode, for use by
5978 jump instructions. */
5979 aentry = xmalloc (sizeof (struct bytecode_address));
5981 aentry->address = current_insn_ptr;
5982 aentry->goto_pc = -1;
5983 aentry->from_offset = aentry->from_size = 0;
5984 aentry->next = bytecode_address_table;
5985 bytecode_address_table = aentry;
5993 case gdb_agent_op_add:
5997 case gdb_agent_op_sub:
6001 case gdb_agent_op_mul:
6005 case gdb_agent_op_div_signed:
6009 case gdb_agent_op_div_unsigned:
6013 case gdb_agent_op_rem_signed:
6017 case gdb_agent_op_rem_unsigned:
6021 case gdb_agent_op_lsh:
6025 case gdb_agent_op_rsh_signed:
6029 case gdb_agent_op_rsh_unsigned:
6030 emit_rsh_unsigned ();
6033 case gdb_agent_op_trace:
6037 case gdb_agent_op_trace_quick:
6041 case gdb_agent_op_log_not:
6045 case gdb_agent_op_bit_and:
6049 case gdb_agent_op_bit_or:
6053 case gdb_agent_op_bit_xor:
6057 case gdb_agent_op_bit_not:
6061 case gdb_agent_op_equal:
6062 next_op = aexpr->bytes[pc];
6063 if (next_op == gdb_agent_op_if_goto
6064 && !is_goto_target (aexpr, pc)
6065 && target_emit_ops ()->emit_eq_goto)
6067 trace_debug ("Combining equal & if_goto");
6070 arg = aexpr->bytes[pc++];
6071 arg = (arg << 8) + aexpr->bytes[pc++];
6072 aentry->goto_pc = arg;
6073 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
6075 else if (next_op == gdb_agent_op_log_not
6076 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6077 && !is_goto_target (aexpr, pc + 1)
6078 && target_emit_ops ()->emit_ne_goto)
6080 trace_debug ("Combining equal & log_not & if_goto");
6083 arg = aexpr->bytes[pc++];
6084 arg = (arg << 8) + aexpr->bytes[pc++];
6085 aentry->goto_pc = arg;
6086 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
6092 case gdb_agent_op_less_signed:
6093 next_op = aexpr->bytes[pc];
6094 if (next_op == gdb_agent_op_if_goto
6095 && !is_goto_target (aexpr, pc))
6097 trace_debug ("Combining less_signed & if_goto");
6100 arg = aexpr->bytes[pc++];
6101 arg = (arg << 8) + aexpr->bytes[pc++];
6102 aentry->goto_pc = arg;
6103 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
6105 else if (next_op == gdb_agent_op_log_not
6106 && !is_goto_target (aexpr, pc)
6107 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6108 && !is_goto_target (aexpr, pc + 1))
6110 trace_debug ("Combining less_signed & log_not & if_goto");
6113 arg = aexpr->bytes[pc++];
6114 arg = (arg << 8) + aexpr->bytes[pc++];
6115 aentry->goto_pc = arg;
6116 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
6119 emit_less_signed ();
6122 case gdb_agent_op_less_unsigned:
6123 emit_less_unsigned ();
6126 case gdb_agent_op_ext:
6127 arg = aexpr->bytes[pc++];
6128 if (arg < (sizeof (LONGEST) * 8))
6132 case gdb_agent_op_ref8:
6136 case gdb_agent_op_ref16:
6140 case gdb_agent_op_ref32:
6144 case gdb_agent_op_ref64:
6148 case gdb_agent_op_if_goto:
6149 arg = aexpr->bytes[pc++];
6150 arg = (arg << 8) + aexpr->bytes[pc++];
6151 aentry->goto_pc = arg;
6152 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
6155 case gdb_agent_op_goto:
6156 arg = aexpr->bytes[pc++];
6157 arg = (arg << 8) + aexpr->bytes[pc++];
6158 aentry->goto_pc = arg;
6159 emit_goto (&(aentry->from_offset), &(aentry->from_size));
6162 case gdb_agent_op_const8:
6163 emit_stack_flush ();
6164 top = aexpr->bytes[pc++];
6168 case gdb_agent_op_const16:
6169 emit_stack_flush ();
6170 top = aexpr->bytes[pc++];
6171 top = (top << 8) + aexpr->bytes[pc++];
6175 case gdb_agent_op_const32:
6176 emit_stack_flush ();
6177 top = aexpr->bytes[pc++];
6178 top = (top << 8) + aexpr->bytes[pc++];
6179 top = (top << 8) + aexpr->bytes[pc++];
6180 top = (top << 8) + aexpr->bytes[pc++];
6184 case gdb_agent_op_const64:
6185 emit_stack_flush ();
6186 top = aexpr->bytes[pc++];
6187 top = (top << 8) + aexpr->bytes[pc++];
6188 top = (top << 8) + aexpr->bytes[pc++];
6189 top = (top << 8) + aexpr->bytes[pc++];
6190 top = (top << 8) + aexpr->bytes[pc++];
6191 top = (top << 8) + aexpr->bytes[pc++];
6192 top = (top << 8) + aexpr->bytes[pc++];
6193 top = (top << 8) + aexpr->bytes[pc++];
6197 case gdb_agent_op_reg:
6198 emit_stack_flush ();
6199 arg = aexpr->bytes[pc++];
6200 arg = (arg << 8) + aexpr->bytes[pc++];
6204 case gdb_agent_op_end:
6205 trace_debug ("At end of expression\n");
6207 /* Assume there is one stack element left, and that it is
6208 cached in "top" where emit_epilogue can get to it. */
6209 emit_stack_adjust (1);
6214 case gdb_agent_op_dup:
6215 /* In our design, dup is equivalent to stack flushing. */
6216 emit_stack_flush ();
6219 case gdb_agent_op_pop:
6223 case gdb_agent_op_zero_ext:
6224 arg = aexpr->bytes[pc++];
6225 if (arg < (sizeof (LONGEST) * 8))
6226 emit_zero_ext (arg);
6229 case gdb_agent_op_swap:
6230 next_op = aexpr->bytes[pc];
6231 /* Detect greater-than comparison sequences. */
6232 if (next_op == gdb_agent_op_less_signed
6233 && !is_goto_target (aexpr, pc)
6234 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6235 && !is_goto_target (aexpr, pc + 1))
6237 trace_debug ("Combining swap & less_signed & if_goto");
6240 arg = aexpr->bytes[pc++];
6241 arg = (arg << 8) + aexpr->bytes[pc++];
6242 aentry->goto_pc = arg;
6243 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
6245 else if (next_op == gdb_agent_op_less_signed
6246 && !is_goto_target (aexpr, pc)
6247 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
6248 && !is_goto_target (aexpr, pc + 1)
6249 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
6250 && !is_goto_target (aexpr, pc + 2))
6252 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6255 arg = aexpr->bytes[pc++];
6256 arg = (arg << 8) + aexpr->bytes[pc++];
6257 aentry->goto_pc = arg;
6258 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
6264 case gdb_agent_op_getv:
6265 emit_stack_flush ();
6266 arg = aexpr->bytes[pc++];
6267 arg = (arg << 8) + aexpr->bytes[pc++];
6268 emit_int_call_1 (ipa_sym_addrs.addr_get_trace_state_variable_value,
6272 case gdb_agent_op_setv:
6273 arg = aexpr->bytes[pc++];
6274 arg = (arg << 8) + aexpr->bytes[pc++];
6275 emit_void_call_2 (ipa_sym_addrs.addr_set_trace_state_variable_value,
6279 case gdb_agent_op_tracev:
6283 /* GDB never (currently) generates any of these ops. */
6284 case gdb_agent_op_float:
6285 case gdb_agent_op_ref_float:
6286 case gdb_agent_op_ref_double:
6287 case gdb_agent_op_ref_long_double:
6288 case gdb_agent_op_l_to_d:
6289 case gdb_agent_op_d_to_l:
6290 case gdb_agent_op_trace16:
6295 trace_debug ("Agent expression op 0x%x not recognized\n", op);
6296 /* Don't struggle on, things will just get worse. */
6297 return expr_eval_unrecognized_opcode;
6300 /* This catches errors that occur in target-specific code
6304 trace_debug ("Error %d while emitting code for %s\n",
6305 emit_error, gdb_agent_op_name (op));
6306 return expr_eval_unhandled_opcode;
6309 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op));
6312 /* Now fill in real addresses as goto destinations. */
6313 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
6317 if (aentry->goto_pc < 0)
6320 /* Find the location that we are going to, and call back into
6321 target-specific code to write the actual address or
6323 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
6325 if (aentry2->pc == aentry->goto_pc)
6327 trace_debug ("Want to jump from %s to %s\n",
6328 paddress (aentry->address),
6329 paddress (aentry2->address));
6330 write_goto_address (aentry->address + aentry->from_offset,
6331 aentry2->address, aentry->from_size);
6337 /* Error out if we didn't find a destination. */
6340 trace_debug ("Destination of goto %d not found\n",
6342 return expr_eval_invalid_goto;
6346 return expr_eval_no_error;
6349 /* We'll need to adjust these when we consider bi-arch setups, and big
6353 write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
6355 return write_inferior_memory (where,
6356 (unsigned char *) &ptr, sizeof (void *));
6359 /* The base pointer of the IPA's heap. This is the only memory the
6360 IPA is allowed to use. The IPA should _not_ call the inferior's
6361 `malloc' during operation. That'd be slow, and, most importantly,
6362 it may not be safe. We may be collecting a tracepoint in a signal
6363 handler, for example. */
6364 static CORE_ADDR target_tp_heap;
6366 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6370 target_malloc (ULONGEST size)
6374 if (target_tp_heap == 0)
6376 /* We have the pointer *address*, need what it points to. */
6377 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
6379 fatal ("could get target heap head pointer");
6382 ptr = target_tp_heap;
6383 target_tp_heap += size;
6385 /* Pad to 8-byte alignment. */
6386 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
6392 download_agent_expr (struct agent_expr *expr)
6394 CORE_ADDR expr_addr;
6395 CORE_ADDR expr_bytes;
6397 expr_addr = target_malloc (sizeof (*expr));
6398 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
6400 expr_bytes = target_malloc (expr->length);
6401 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
6403 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
6408 /* Align V up to N bits. */
6409 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6411 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6414 download_tracepoint_1 (struct tracepoint *tpoint)
6416 struct tracepoint target_tracepoint;
6417 CORE_ADDR tpptr = 0;
6419 gdb_assert (tpoint->type == fast_tracepoint
6420 || tpoint->type == static_tracepoint);
6422 if (tpoint->cond != NULL && target_emit_ops () != NULL)
6424 CORE_ADDR jentry, jump_entry;
6426 jentry = jump_entry = get_jump_space_head ();
6428 if (tpoint->cond != NULL)
6430 /* Pad to 8-byte alignment. (needed?) */
6431 /* Actually this should be left for the target to
6433 jentry = UALIGN (jentry, 8);
6435 compile_tracepoint_condition (tpoint, &jentry);
6438 /* Pad to 8-byte alignment. */
6439 jentry = UALIGN (jentry, 8);
6440 claim_jump_space (jentry - jump_entry);
6443 target_tracepoint = *tpoint;
6445 tpptr = target_malloc (sizeof (*tpoint));
6446 tpoint->obj_addr_on_target = tpptr;
6448 /* Write the whole object. We'll fix up its pointers in a bit.
6449 Assume no next for now. This is fixed up above on the next
6450 iteration, if there's any. */
6451 target_tracepoint.next = NULL;
6452 /* Need to clear this here too, since we're downloading the
6453 tracepoints before clearing our own copy. */
6454 target_tracepoint.hit_count = 0;
6456 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
6457 sizeof (target_tracepoint));
6460 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6462 download_agent_expr (tpoint->cond));
6464 if (tpoint->numactions)
6467 CORE_ADDR actions_array;
6469 /* The pointers array. */
6471 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
6472 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6476 /* Now for each pointer, download the action. */
6477 for (i = 0; i < tpoint->numactions; i++)
6479 CORE_ADDR ipa_action = 0;
6480 struct tracepoint_action *action = tpoint->actions[i];
6482 switch (action->type)
6486 = target_malloc (sizeof (struct collect_memory_action));
6487 write_inferior_memory (ipa_action,
6488 (unsigned char *) action,
6489 sizeof (struct collect_memory_action));
6493 = target_malloc (sizeof (struct collect_registers_action));
6494 write_inferior_memory (ipa_action,
6495 (unsigned char *) action,
6496 sizeof (struct collect_registers_action));
6501 struct eval_expr_action *eaction
6502 = (struct eval_expr_action *) action;
6504 ipa_action = target_malloc (sizeof (*eaction));
6505 write_inferior_memory (ipa_action,
6506 (unsigned char *) eaction,
6509 expr = download_agent_expr (eaction->expr);
6510 write_inferior_data_ptr
6511 (ipa_action + offsetof (struct eval_expr_action, expr),
6516 ipa_action = target_malloc
6517 (sizeof (struct collect_static_trace_data_action));
6518 write_inferior_memory
6520 (unsigned char *) action,
6521 sizeof (struct collect_static_trace_data_action));
6524 trace_debug ("unknown trace action '%c', ignoring",
6529 if (ipa_action != 0)
6530 write_inferior_data_ptr
6531 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
6538 download_tracepoint (struct tracepoint *tpoint)
6540 struct tracepoint *tp, *tp_prev;
6542 if (tpoint->type != fast_tracepoint
6543 && tpoint->type != static_tracepoint)
6546 download_tracepoint_1 (tpoint);
6548 /* Find the previous entry of TPOINT, which is fast tracepoint or
6549 static tracepoint. */
6551 for (tp = tracepoints; tp != tpoint; tp = tp->next)
6553 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
6559 CORE_ADDR tp_prev_target_next_addr;
6561 /* Insert TPOINT after TP_PREV in IPA. */
6562 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
6563 + offsetof (struct tracepoint, next),
6564 &tp_prev_target_next_addr))
6565 fatal ("error reading `tp_prev->next'");
6567 /* tpoint->next = tp_prev->next */
6568 write_inferior_data_ptr (tpoint->obj_addr_on_target
6569 + offsetof (struct tracepoint, next),
6570 tp_prev_target_next_addr);
6571 /* tp_prev->next = tpoint */
6572 write_inferior_data_ptr (tp_prev->obj_addr_on_target
6573 + offsetof (struct tracepoint, next),
6574 tpoint->obj_addr_on_target);
6577 /* First object in list, set the head pointer in the
6579 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
6580 tpoint->obj_addr_on_target);
6585 download_tracepoints (void)
6587 CORE_ADDR tpptr = 0, prev_tpptr = 0;
6588 struct tracepoint *tpoint;
6590 /* Start out empty. */
6591 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
6593 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6595 if (tpoint->type != fast_tracepoint
6596 && tpoint->type != static_tracepoint)
6601 download_tracepoint_1 (tpoint);
6603 tpptr = tpoint->obj_addr_on_target;
6605 if (tpoint == tracepoints)
6607 /* First object in list, set the head pointer in the
6609 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
6613 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
6621 download_trace_state_variables (void)
6623 CORE_ADDR ptr = 0, prev_ptr = 0;
6624 struct trace_state_variable *tsv;
6626 /* Start out empty. */
6627 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
6629 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
6631 struct trace_state_variable target_tsv;
6633 /* TSV's with a getter have been initialized equally in both the
6634 inferior and GDBserver. Skip them. */
6635 if (tsv->getter != NULL)
6641 ptr = target_malloc (sizeof (*tsv));
6643 if (tsv == trace_state_variables)
6645 /* First object in list, set the head pointer in the
6648 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
6653 write_inferior_data_ptr (prev_ptr
6654 + offsetof (struct trace_state_variable,
6659 /* Write the whole object. We'll fix up its pointers in a bit.
6660 Assume no next, fixup when needed. */
6661 target_tsv.next = NULL;
6663 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
6664 sizeof (target_tsv));
6666 if (tsv->name != NULL)
6668 size_t size = strlen (tsv->name) + 1;
6669 CORE_ADDR name_addr = target_malloc (size);
6670 write_inferior_memory (name_addr,
6671 (unsigned char *) tsv->name, size);
6672 write_inferior_data_ptr (ptr
6673 + offsetof (struct trace_state_variable,
6678 if (tsv->getter != NULL)
6680 fatal ("what to do with these?");
6686 /* Fixup the next pointer in the last item in the list. */
6687 write_inferior_data_ptr (prev_ptr
6688 + offsetof (struct trace_state_variable,
6693 /* Upload complete trace frames out of the IP Agent's trace buffer
6694 into GDBserver's trace buffer. This always uploads either all or
6695 no trace frames. This is the counter part of
6696 `trace_alloc_trace_buffer'. See its description of the atomic
6697 synching mechanism. */
6700 upload_fast_traceframes (void)
6702 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
6703 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
6705 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
6706 unsigned int curr_tbctrl_idx;
6707 unsigned int ipa_trace_buffer_ctrl_curr;
6708 unsigned int ipa_trace_buffer_ctrl_curr_old;
6709 CORE_ADDR ipa_trace_buffer_ctrl_addr;
6710 struct breakpoint *about_to_request_buffer_space_bkpt;
6711 CORE_ADDR ipa_trace_buffer_lo;
6712 CORE_ADDR ipa_trace_buffer_hi;
6714 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6715 &ipa_traceframe_read_count_racy))
6717 /* This will happen in most targets if the current thread is
6722 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6723 &ipa_traceframe_write_count_racy))
6726 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
6727 ipa_traceframe_write_count_racy
6728 - ipa_traceframe_read_count_racy,
6729 ipa_traceframe_write_count_racy,
6730 ipa_traceframe_read_count_racy);
6732 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
6735 about_to_request_buffer_space_bkpt
6736 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
6739 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6740 &ipa_trace_buffer_ctrl_curr))
6743 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
6745 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
6748 unsigned int prev, counter;
6750 /* Update the token, with new counters, and the GDBserver stamp
6751 bit. Alway reuse the current TBC index. */
6752 prev = ipa_trace_buffer_ctrl_curr & 0x0007ff00;
6753 counter = (prev + 0x100) & 0x0007ff00;
6755 ipa_trace_buffer_ctrl_curr = (0x80000000
6761 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6762 ipa_trace_buffer_ctrl_curr))
6765 trace_debug ("Lib: Committed %08x -> %08x",
6766 ipa_trace_buffer_ctrl_curr_old,
6767 ipa_trace_buffer_ctrl_curr);
6769 /* Re-read these, now that we've installed the
6770 `about_to_request_buffer_space' breakpoint/lock. A thread could
6771 have finished a traceframe between the last read of these
6772 counters and setting the breakpoint above. If we start
6773 uploading, we never want to leave this function with
6774 traceframe_read_count != 0, otherwise, GDBserver could end up
6775 incrementing the counter tokens more than once (due to event loop
6776 nesting), which would break the IP agent's "effective" detection
6777 (see trace_alloc_trace_buffer). */
6778 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6779 &ipa_traceframe_read_count))
6781 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6782 &ipa_traceframe_write_count))
6787 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6788 ipa_traceframe_write_count - ipa_traceframe_read_count,
6789 ipa_traceframe_write_count, ipa_traceframe_read_count);
6791 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
6792 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
6793 trace_debug ("note that ipa_traceframe_count's parts changed");
6796 /* Get the address of the current TBC object (the IP agent has an
6797 array of 3 such objects). The index is stored in the TBC
6799 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
6800 ipa_trace_buffer_ctrl_addr
6801 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
6803 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
6804 (unsigned char *) &ipa_trace_buffer_ctrl,
6805 sizeof (struct ipa_trace_buffer_control)))
6808 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
6809 &ipa_trace_buffer_lo))
6811 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6812 &ipa_trace_buffer_hi))
6815 /* Offsets are easier to grok for debugging than raw addresses,
6816 especially for the small trace buffer sizes that are useful for
6818 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6819 "endfree=%d wrap=%d hi=%d",
6821 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6822 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6823 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6824 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6825 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6827 /* Note that the IPA's buffer is always circular. */
6829 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6831 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6832 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6834 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6835 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6836 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6837 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6840 tf = IPA_FIRST_TRACEFRAME ();
6842 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6844 struct tracepoint *tpoint;
6845 struct traceframe *tframe;
6846 unsigned char *block;
6847 struct traceframe ipa_tframe;
6849 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6850 offsetof (struct traceframe, data)))
6851 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6853 if (ipa_tframe.tpnum == 0)
6854 fatal ("Uploading: No (more) fast traceframes, but "
6855 "ipa_traceframe_count == %u??\n",
6856 ipa_traceframe_write_count - ipa_traceframe_read_count);
6858 /* Note that this will be incorrect for multi-location
6860 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6862 tframe = add_traceframe (tpoint);
6865 trace_buffer_is_full = 1;
6866 trace_debug ("Uploading: trace buffer is full");
6870 /* Copy the whole set of blocks in one go for now. FIXME:
6871 split this in smaller blocks. */
6872 block = add_traceframe_block (tframe, ipa_tframe.data_size);
6875 if (read_inferior_memory (tf
6876 + offsetof (struct traceframe, data),
6877 block, ipa_tframe.data_size))
6878 error ("Uploading: Couldn't read traceframe data at %s\n",
6879 paddress (tf + offsetof (struct traceframe, data)));
6882 trace_debug ("Uploading: traceframe didn't fit");
6883 finish_traceframe (tframe);
6886 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6888 /* If we freed the traceframe that wrapped around, go back
6889 to the non-wrap case. */
6890 if (tf < ipa_trace_buffer_ctrl.start)
6892 trace_debug ("Lib: Discarding past the wraparound");
6893 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6895 ipa_trace_buffer_ctrl.start = tf;
6896 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6897 ++ipa_traceframe_read_count;
6899 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6900 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6902 trace_debug ("Lib: buffer is fully empty. "
6903 "Trace buffer [%d] start=%d free=%d endfree=%d",
6905 (int) (ipa_trace_buffer_ctrl.start
6906 - ipa_trace_buffer_lo),
6907 (int) (ipa_trace_buffer_ctrl.free
6908 - ipa_trace_buffer_lo),
6909 (int) (ipa_trace_buffer_ctrl.end_free
6910 - ipa_trace_buffer_lo));
6912 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6913 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6914 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6915 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6918 trace_debug ("Uploaded a traceframe\n"
6919 "Lib: Trace buffer [%d] start=%d free=%d "
6920 "endfree=%d wrap=%d hi=%d",
6922 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6923 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6924 (int) (ipa_trace_buffer_ctrl.end_free
6925 - ipa_trace_buffer_lo),
6926 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6927 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6930 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6931 (unsigned char *) &ipa_trace_buffer_ctrl,
6932 sizeof (struct ipa_trace_buffer_control)))
6935 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6936 ipa_traceframe_read_count);
6938 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6941 cancel_breakpoints ();
6943 delete_breakpoint (about_to_request_buffer_space_bkpt);
6944 about_to_request_buffer_space_bkpt = NULL;
6948 if (trace_buffer_is_full)
6953 #ifdef IN_PROCESS_AGENT
6955 IP_AGENT_EXPORT int ust_loaded;
6956 IP_AGENT_EXPORT char cmd_buf[CMD_BUF_SIZE];
6960 /* Static tracepoints. */
6962 /* UST puts a "struct tracepoint" in the global namespace, which
6963 conflicts with our tracepoint. Arguably, being a library, it
6964 shouldn't take ownership of such a generic name. We work around it
6966 #define tracepoint ust_tracepoint
6967 #include <ust/ust.h>
6970 extern int serialize_to_text (char *outbuf, int bufsize,
6971 const char *fmt, va_list ap);
6973 #define GDB_PROBE_NAME "gdb"
6975 /* We dynamically search for the UST symbols instead of linking them
6976 in. This lets the user decide if the application uses static
6977 tracepoints, instead of always pulling libust.so in. This vector
6978 holds pointers to all functions we care about. */
6982 int (*serialize_to_text) (char *outbuf, int bufsize,
6983 const char *fmt, va_list ap);
6985 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6986 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6988 int (*ltt_marker_connect) (const char *channel, const char *mname,
6990 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6993 void (*marker_iter_start) (struct marker_iter *iter);
6994 void (*marker_iter_next) (struct marker_iter *iter);
6995 void (*marker_iter_stop) (struct marker_iter *iter);
6996 void (*marker_iter_reset) (struct marker_iter *iter);
7001 /* Cast through typeof to catch incompatible API changes. Since UST
7002 only builds with gcc, we can freely use gcc extensions here
7004 #define GET_UST_SYM(SYM) \
7007 if (ust_ops.SYM == NULL) \
7008 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
7009 if (ust_ops.SYM == NULL) \
7013 #define USTF(SYM) ust_ops.SYM
7015 /* Get pointers to all libust.so functions we care about. */
7020 GET_UST_SYM (serialize_to_text);
7022 GET_UST_SYM (ltt_probe_register);
7023 GET_UST_SYM (ltt_probe_unregister);
7024 GET_UST_SYM (ltt_marker_connect);
7025 GET_UST_SYM (ltt_marker_disconnect);
7027 GET_UST_SYM (marker_iter_start);
7028 GET_UST_SYM (marker_iter_next);
7029 GET_UST_SYM (marker_iter_stop);
7030 GET_UST_SYM (marker_iter_reset);
7036 /* Given an UST marker, return the matching gdb static tracepoint.
7037 The match is done by address. */
7039 static struct tracepoint *
7040 ust_marker_to_static_tracepoint (const struct marker *mdata)
7042 struct tracepoint *tpoint;
7044 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
7046 if (tpoint->type != static_tracepoint)
7049 if (tpoint->address == (uintptr_t) mdata->location)
7056 /* The probe function we install on lttng/ust markers. Whenever a
7057 probed ust marker is hit, this function is called. This is similar
7058 to gdb_collect, only for static tracepoints, instead of fast
7062 gdb_probe (const struct marker *mdata, void *probe_private,
7063 struct registers *regs, void *call_private,
7064 const char *fmt, va_list *args)
7066 struct tracepoint *tpoint;
7067 struct static_tracepoint_ctx ctx;
7069 /* Don't do anything until the trace run is completely set up. */
7072 trace_debug ("gdb_probe: not tracing\n");
7076 ctx.base.type = static_tracepoint;
7077 ctx.regcache_initted = 0;
7082 /* Wrap the regblock in a register cache (in the stack, we don't
7083 want to malloc here). */
7084 ctx.regspace = alloca (register_cache_size ());
7085 if (ctx.regspace == NULL)
7087 trace_debug ("Trace buffer block allocation failed, skipping");
7091 tpoint = ust_marker_to_static_tracepoint (mdata);
7094 trace_debug ("gdb_probe: marker not known: "
7095 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7096 mdata->location, mdata->channel,
7097 mdata->name, mdata->format);
7101 if (!tpoint->enabled)
7103 trace_debug ("gdb_probe: tracepoint disabled");
7107 ctx.tpoint = tpoint;
7109 trace_debug ("gdb_probe: collecting marker: "
7110 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7111 mdata->location, mdata->channel,
7112 mdata->name, mdata->format);
7114 /* Test the condition if present, and collect if true. */
7115 if (tpoint->cond == NULL
7116 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
7119 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
7120 tpoint->address, tpoint);
7122 if (stopping_tracepoint
7123 || trace_buffer_is_full
7124 || expr_eval_result != expr_eval_no_error)
7129 /* If there was a condition and it evaluated to false, the only
7130 way we would stop tracing is if there was an error during
7131 condition expression evaluation. */
7132 if (expr_eval_result != expr_eval_no_error)
7137 /* Called if the gdb static tracepoint requested collecting "$_sdata",
7138 static tracepoint string data. This is a string passed to the
7139 tracing library by the user, at the time of the tracepoint marker
7140 call. E.g., in the UST marker call:
7142 trace_mark (ust, bar33, "str %s", "FOOBAZ");
7144 the collected data is "str FOOBAZ".
7148 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
7150 struct tracepoint *tpoint,
7151 struct traceframe *tframe)
7153 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
7154 unsigned char *bufspace;
7157 unsigned short blocklen;
7161 trace_debug ("Wanted to collect static trace data, "
7162 "but there's no static trace data");
7166 va_copy (copy, *umd->args);
7167 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
7170 trace_debug ("Want to collect ust data");
7172 /* 'S' + size + string */
7173 bufspace = add_traceframe_block (tframe,
7174 1 + sizeof (blocklen) + size + 1);
7175 if (bufspace == NULL)
7177 trace_debug ("Trace buffer block allocation failed, skipping");
7181 /* Identify a static trace data block. */
7184 blocklen = size + 1;
7185 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
7187 va_copy (copy, *umd->args);
7188 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
7189 size + 1, umd->fmt, copy);
7192 trace_debug ("Storing static tracepoint data in regblock: %s",
7193 bufspace + 1 + sizeof (blocklen));
7196 /* The probe to register with lttng/ust. */
7197 static struct ltt_available_probe gdb_ust_probe =
7204 #endif /* HAVE_UST */
7205 #endif /* IN_PROCESS_AGENT */
7209 #include <sys/socket.h>
7212 #ifndef UNIX_PATH_MAX
7213 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
7216 /* Where we put the socked used for synchronization. */
7217 #define SOCK_DIR P_tmpdir
7219 #endif /* HAVE_UST */
7221 #ifndef IN_PROCESS_AGENT
7226 gdb_ust_connect_sync_socket (int pid)
7228 struct sockaddr_un addr;
7230 char path[UNIX_PATH_MAX];
7232 res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", SOCK_DIR, pid);
7233 if (res >= UNIX_PATH_MAX)
7235 trace_debug ("string overflow allocating socket name");
7239 res = fd = socket (PF_UNIX, SOCK_STREAM, 0);
7242 warning ("error opening sync socket: %s\n", strerror (errno));
7246 addr.sun_family = AF_UNIX;
7248 res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
7249 if (res >= UNIX_PATH_MAX)
7251 warning ("string overflow allocating socket name\n");
7256 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
7259 warning ("error connecting sync socket (%s): %s. "
7260 "Make sure the directory exists and that it is writable.",
7261 path, strerror (errno));
7269 /* Resume thread PTID. */
7272 resume_thread (ptid_t ptid)
7274 struct thread_resume resume_info;
7276 resume_info.thread = ptid;
7277 resume_info.kind = resume_continue;
7278 resume_info.sig = TARGET_SIGNAL_0;
7279 (*the_target->resume) (&resume_info, 1);
7282 /* Stop thread PTID. */
7285 stop_thread (ptid_t ptid)
7287 struct thread_resume resume_info;
7289 resume_info.thread = ptid;
7290 resume_info.kind = resume_stop;
7291 resume_info.sig = TARGET_SIGNAL_0;
7292 (*the_target->resume) (&resume_info, 1);
7295 /* Ask the in-process agent to run a command. Since we don't want to
7296 have to handle the IPA hitting breakpoints while running the
7297 command, we pause all threads, remove all breakpoints, and then set
7298 the helper thread re-running. We communicate with the helper
7299 thread by means of direct memory xfering, and a socket for
7303 run_inferior_command (char *cmd)
7307 int pid = ptid_get_pid (current_inferior->entry.id);
7309 ptid_t ptid = null_ptid;
7311 trace_debug ("run_inferior_command: running: %s", cmd);
7314 uninsert_all_breakpoints ();
7316 if (read_inferior_integer (ipa_sym_addrs.addr_helper_thread_id, &tid))
7318 warning ("Error reading helper thread's id in lib");
7324 warning ("helper thread not initialized yet");
7328 if (write_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
7329 (unsigned char *) cmd, strlen (cmd) + 1))
7331 warning ("Error writing command");
7335 ptid = ptid_build (pid, tid, 0);
7337 resume_thread (ptid);
7339 fd = gdb_ust_connect_sync_socket (pid);
7345 trace_debug ("signalling helper thread");
7349 ret = write (fd, buf, 1);
7350 } while (ret == -1 && errno == EINTR);
7352 trace_debug ("waiting for helper thread's response");
7356 ret = read (fd, buf, 1);
7357 } while (ret == -1 && errno == EINTR);
7361 trace_debug ("helper thread's response received");
7366 /* Need to read response with the inferior stopped. */
7367 if (!ptid_equal (ptid, null_ptid))
7369 int was_non_stop = non_stop;
7370 struct target_waitstatus status;
7374 mywait (ptid, &status, 0, 0);
7375 non_stop = was_non_stop;
7380 if (read_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
7381 (unsigned char *) cmd, CMD_BUF_SIZE))
7383 warning ("Error reading command response");
7388 trace_debug ("run_inferior_command: response: %s", cmd);
7392 reinsert_all_breakpoints ();
7398 #else /* HAVE_UST */
7401 run_inferior_command (char *cmd)
7406 #endif /* HAVE_UST */
7408 #else /* !IN_PROCESS_AGENT */
7410 /* Thread ID of the helper thread. GDBserver reads this to know which
7411 is the help thread. This is an LWP id on Linux. */
7412 int helper_thread_id;
7417 init_named_socket (const char *name)
7420 struct sockaddr_un addr;
7422 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
7425 warning ("socket creation failed: %s", strerror (errno));
7429 addr.sun_family = AF_UNIX;
7431 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
7432 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
7434 result = access (name, F_OK);
7438 result = unlink (name);
7441 warning ("unlink failed: %s", strerror (errno));
7445 warning ("socket %s already exists; overwriting", name);
7448 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
7451 warning ("bind failed: %s", strerror (errno));
7456 result = listen (fd, 1);
7459 warning ("listen: %s", strerror (errno));
7468 gdb_ust_socket_init (void)
7471 char name[UNIX_PATH_MAX];
7473 result = xsnprintf (name, UNIX_PATH_MAX, "%s/gdb_ust%d",
7474 SOCK_DIR, getpid ());
7475 if (result >= UNIX_PATH_MAX)
7477 trace_debug ("string overflow allocating socket name");
7481 fd = init_named_socket (name);
7483 warning ("Error initializing named socket (%s) for communication with the "
7484 "ust helper thread. Check that directory exists and that it "
7485 "is writable.", name);
7490 /* Return an hexstr version of the STR C string, fit for sending to
7494 cstr_to_hexstr (const char *str)
7496 int len = strlen (str);
7497 char *hexstr = xmalloc (len * 2 + 1);
7498 convert_int_to_ascii ((gdb_byte *) str, hexstr, len);
7502 /* The next marker to be returned on a qTsSTM command. */
7503 static const struct marker *next_st;
7505 /* Returns the first known marker. */
7510 struct marker_iter iter;
7512 USTF(marker_iter_reset) (&iter);
7513 USTF(marker_iter_start) (&iter);
7518 /* Returns the marker following M. */
7520 const struct marker *
7521 next_marker (const struct marker *m)
7523 struct marker_iter iter;
7525 USTF(marker_iter_reset) (&iter);
7526 USTF(marker_iter_start) (&iter);
7528 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7530 if (iter.marker == m)
7532 USTF(marker_iter_next) (&iter);
7540 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7544 response_ust_marker (char *packet, const struct marker *st)
7546 char *strid, *format, *tmp;
7548 next_st = next_marker (st);
7550 tmp = xmalloc (strlen (st->channel) + 1 +
7551 strlen (st->name) + 1);
7552 sprintf (tmp, "%s/%s", st->channel, st->name);
7554 strid = cstr_to_hexstr (tmp);
7557 format = cstr_to_hexstr (st->format);
7559 sprintf (packet, "m%s:%s:%s",
7560 paddress ((uintptr_t) st->location),
7568 /* Return the first static tracepoint, and initialize the state
7569 machine that will iterate through all the static tracepoints. */
7572 cmd_qtfstm (char *packet)
7574 trace_debug ("Returning first trace state variable definition");
7576 if (first_marker ())
7577 response_ust_marker (packet, first_marker ());
7579 strcpy (packet, "l");
7582 /* Return additional trace state variable definitions. */
7585 cmd_qtsstm (char *packet)
7587 trace_debug ("Returning static tracepoint");
7590 response_ust_marker (packet, next_st);
7592 strcpy (packet, "l");
7595 /* Disconnect the GDB probe from a marker at a given address. */
7598 unprobe_marker_at (char *packet)
7602 struct marker_iter iter;
7604 p += sizeof ("unprobe_marker_at:") - 1;
7606 p = unpack_varlen_hex (p, &address);
7608 USTF(marker_iter_reset) (&iter);
7609 USTF(marker_iter_start) (&iter);
7610 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7611 if ((uintptr_t ) iter.marker->location == address)
7615 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
7619 warning ("could not disable marker %s/%s",
7620 iter.marker->channel, iter.marker->name);
7625 /* Connect the GDB probe to a marker at a given address. */
7628 probe_marker_at (char *packet)
7632 struct marker_iter iter;
7635 p += sizeof ("probe_marker_at:") - 1;
7637 p = unpack_varlen_hex (p, &address);
7639 USTF(marker_iter_reset) (&iter);
7641 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7643 USTF(marker_iter_next) (&iter), m = iter.marker)
7644 if ((uintptr_t ) m->location == address)
7648 trace_debug ("found marker for address. "
7649 "ltt_marker_connect (marker = %s/%s)",
7650 m->channel, m->name);
7652 result = USTF(ltt_marker_connect) (m->channel, m->name,
7654 if (result && result != -EEXIST)
7655 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7656 m->channel, m->name, -result);
7660 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
7661 m->channel, m->name);
7665 strcpy (packet, "OK");
7669 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
7674 cmd_qtstmat (char *packet)
7678 struct marker_iter iter;
7681 p += sizeof ("qTSTMat:") - 1;
7683 p = unpack_varlen_hex (p, &address);
7685 USTF(marker_iter_reset) (&iter);
7687 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7689 USTF(marker_iter_next) (&iter), m = iter.marker)
7690 if ((uintptr_t ) m->location == address)
7692 response_ust_marker (packet, m);
7696 strcpy (packet, "l");
7701 gdb_ust_thread (void *arg)
7707 listen_fd = gdb_ust_socket_init ();
7710 if (helper_thread_id == 0)
7711 helper_thread_id = syscall (SYS_gettid);
7714 if (listen_fd == -1)
7716 warning ("could not create sync socket\n");
7723 struct sockaddr_un sockaddr;
7728 tmp = sizeof (sockaddr);
7732 fd = accept (listen_fd, &sockaddr, &tmp);
7734 /* It seems an ERESTARTSYS can escape out of accept. */
7735 while (fd == -512 || (fd == -1 && errno == EINTR));
7739 warning ("Accept returned %d, error: %s\n",
7740 fd, strerror (errno));
7746 ret = read (fd, buf, 1);
7747 } while (ret == -1 && errno == EINTR);
7751 warning ("reading socket (fd=%d) failed with %s",
7752 fd, strerror (errno));
7759 if (strcmp ("qTfSTM", cmd_buf) == 0)
7761 cmd_qtfstm (cmd_buf);
7763 else if (strcmp ("qTsSTM", cmd_buf) == 0)
7765 cmd_qtsstm (cmd_buf);
7767 else if (strncmp ("unprobe_marker_at:",
7769 sizeof ("unprobe_marker_at:") - 1) == 0)
7771 unprobe_marker_at (cmd_buf);
7773 else if (strncmp ("probe_marker_at:",
7775 sizeof ("probe_marker_at:") - 1) == 0)
7777 probe_marker_at (cmd_buf);
7779 else if (strncmp ("qTSTMat:",
7781 sizeof ("qTSTMat:") - 1) == 0)
7783 cmd_qtstmat (cmd_buf);
7785 else if (strcmp (cmd_buf, "help") == 0)
7787 strcpy (cmd_buf, "for help, press F1\n");
7790 strcpy (cmd_buf, "");
7814 /* We want the helper thread to be as transparent as possible, so
7815 have it inherit an all-signals-blocked mask. */
7817 sigfillset (&new_mask);
7818 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
7820 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
7822 res = pthread_create (&thread,
7827 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
7829 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
7831 while (helper_thread_id == 0)
7834 USTF(ltt_probe_register) (&gdb_ust_probe);
7837 #endif /* HAVE_UST */
7839 #include <sys/mman.h>
7842 IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
7843 IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
7844 IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
7846 static void __attribute__ ((constructor))
7847 initialize_tracepoint_ftlib (void)
7849 initialize_tracepoint ();
7856 #endif /* IN_PROCESS_AGENT */
7859 tsv_get_timestamp (void)
7863 if (gettimeofday (&tv, 0) != 0)
7866 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
7870 initialize_tracepoint (void)
7872 /* There currently no way to change the buffer size. */
7873 const int sizeOfBuffer = 5 * 1024 * 1024;
7874 unsigned char *buf = xmalloc (sizeOfBuffer);
7875 init_trace_buffer (buf, sizeOfBuffer);
7877 /* Wire trace state variable 1 to be the timestamp. This will be
7878 uploaded to GDB upon connection and become one of its trace state
7879 variables. (In case you're wondering, if GDB already has a trace
7880 variable numbered 1, it will be renumbered.) */
7881 create_trace_state_variable (1, 0);
7882 set_trace_state_variable_name (1, "trace_timestamp");
7883 set_trace_state_variable_getter (1, tsv_get_timestamp);
7885 #ifdef IN_PROCESS_AGENT
7888 pagesize = sysconf (_SC_PAGE_SIZE);
7892 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
7894 /* Allocate scratch buffer aligned on a page boundary. */
7895 gdb_jump_pad_buffer = memalign (pagesize, pagesize * 20);
7896 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * 20;
7898 /* Make it writable and executable. */
7899 if (mprotect (gdb_jump_pad_buffer, pagesize * 20,
7900 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
7902 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
7903 gdb_jump_pad_buffer, pagesize * 20, strerror (errno));
7906 initialize_low_tracepoint ();