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 gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
114 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
115 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
116 # define collecting gdb_agent_collecting
117 # define gdb_collect gdb_agent_gdb_collect
118 # define stop_tracing gdb_agent_stop_tracing
119 # define flush_trace_buffer gdb_agent_flush_trace_buffer
120 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
121 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
122 # define stopping_tracepoint gdb_agent_stopping_tracepoint
123 # define expr_eval_result gdb_agent_expr_eval_result
124 # define error_tracepoint gdb_agent_error_tracepoint
125 # define tracepoints gdb_agent_tracepoints
126 # define tracing gdb_agent_tracing
127 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
128 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
129 # define trace_buffer_lo gdb_agent_trace_buffer_lo
130 # define trace_buffer_hi gdb_agent_trace_buffer_hi
131 # define traceframe_read_count gdb_agent_traceframe_read_count
132 # define traceframe_write_count gdb_agent_traceframe_write_count
133 # define traceframes_created gdb_agent_traceframes_created
134 # define trace_state_variables gdb_agent_trace_state_variables
135 # define get_raw_reg gdb_agent_get_raw_reg
136 # define get_trace_state_variable_value \
137 gdb_agent_get_trace_state_variable_value
138 # define set_trace_state_variable_value \
139 gdb_agent_set_trace_state_variable_value
140 # define ust_loaded gdb_agent_ust_loaded
141 # define helper_thread_id gdb_agent_helper_thread_id
142 # define cmd_buf gdb_agent_cmd_buf
145 #ifndef IN_PROCESS_AGENT
147 /* Addresses of in-process agent's symbols GDBserver cares about. */
149 struct ipa_sym_addresses
151 CORE_ADDR addr_gdb_tp_heap_buffer;
152 CORE_ADDR addr_gdb_jump_pad_buffer;
153 CORE_ADDR addr_gdb_jump_pad_buffer_end;
154 CORE_ADDR addr_gdb_trampoline_buffer;
155 CORE_ADDR addr_gdb_trampoline_buffer_end;
156 CORE_ADDR addr_gdb_trampoline_buffer_error;
157 CORE_ADDR addr_collecting;
158 CORE_ADDR addr_gdb_collect;
159 CORE_ADDR addr_stop_tracing;
160 CORE_ADDR addr_flush_trace_buffer;
161 CORE_ADDR addr_about_to_request_buffer_space;
162 CORE_ADDR addr_trace_buffer_is_full;
163 CORE_ADDR addr_stopping_tracepoint;
164 CORE_ADDR addr_expr_eval_result;
165 CORE_ADDR addr_error_tracepoint;
166 CORE_ADDR addr_tracepoints;
167 CORE_ADDR addr_tracing;
168 CORE_ADDR addr_trace_buffer_ctrl;
169 CORE_ADDR addr_trace_buffer_ctrl_curr;
170 CORE_ADDR addr_trace_buffer_lo;
171 CORE_ADDR addr_trace_buffer_hi;
172 CORE_ADDR addr_traceframe_read_count;
173 CORE_ADDR addr_traceframe_write_count;
174 CORE_ADDR addr_traceframes_created;
175 CORE_ADDR addr_trace_state_variables;
176 CORE_ADDR addr_get_raw_reg;
177 CORE_ADDR addr_get_trace_state_variable_value;
178 CORE_ADDR addr_set_trace_state_variable_value;
179 CORE_ADDR addr_ust_loaded;
180 CORE_ADDR addr_helper_thread_id;
181 CORE_ADDR addr_cmd_buf;
184 #define STRINGIZE_1(STR) #STR
185 #define STRINGIZE(STR) STRINGIZE_1(STR)
186 #define IPA_SYM(SYM) \
188 STRINGIZE (gdb_agent_ ## SYM), \
189 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
198 IPA_SYM(gdb_tp_heap_buffer),
199 IPA_SYM(gdb_jump_pad_buffer),
200 IPA_SYM(gdb_jump_pad_buffer_end),
201 IPA_SYM(gdb_trampoline_buffer),
202 IPA_SYM(gdb_trampoline_buffer_end),
203 IPA_SYM(gdb_trampoline_buffer_error),
205 IPA_SYM(gdb_collect),
206 IPA_SYM(stop_tracing),
207 IPA_SYM(flush_trace_buffer),
208 IPA_SYM(about_to_request_buffer_space),
209 IPA_SYM(trace_buffer_is_full),
210 IPA_SYM(stopping_tracepoint),
211 IPA_SYM(expr_eval_result),
212 IPA_SYM(error_tracepoint),
213 IPA_SYM(tracepoints),
215 IPA_SYM(trace_buffer_ctrl),
216 IPA_SYM(trace_buffer_ctrl_curr),
217 IPA_SYM(trace_buffer_lo),
218 IPA_SYM(trace_buffer_hi),
219 IPA_SYM(traceframe_read_count),
220 IPA_SYM(traceframe_write_count),
221 IPA_SYM(traceframes_created),
222 IPA_SYM(trace_state_variables),
223 IPA_SYM(get_raw_reg),
224 IPA_SYM(get_trace_state_variable_value),
225 IPA_SYM(set_trace_state_variable_value),
227 IPA_SYM(helper_thread_id),
231 struct ipa_sym_addresses ipa_sym_addrs;
233 int all_tracepoint_symbols_looked_up;
236 in_process_agent_loaded (void)
238 return all_tracepoint_symbols_looked_up;
241 static int read_inferior_integer (CORE_ADDR symaddr, int *val);
243 /* Returns true if both the in-process agent library and the static
244 tracepoints libraries are loaded in the inferior. */
247 in_process_agent_loaded_ust (void)
251 if (!in_process_agent_loaded ())
253 warning ("In-process agent not loaded");
257 if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
259 warning ("Error reading ust_loaded in lib");
267 write_e_ipa_not_loaded (char *buffer)
270 "E.In-process agent library not loaded in process. "
271 "Fast and static tracepoints unavailable.");
274 /* Write an error to BUFFER indicating that UST isn't loaded in the
278 write_e_ust_not_loaded (char *buffer)
282 "E.UST library not loaded in process. "
283 "Static tracepoints unavailable.");
285 sprintf (buffer, "E.GDBserver was built without static tracepoints support");
289 /* If the in-process agent library isn't loaded in the inferior, write
290 an error to BUFFER, and return 1. Otherwise, return 0. */
293 maybe_write_ipa_not_loaded (char *buffer)
295 if (!in_process_agent_loaded ())
297 write_e_ipa_not_loaded (buffer);
303 /* If the in-process agent library and the ust (static tracepoints)
304 library aren't loaded in the inferior, write an error to BUFFER,
305 and return 1. Otherwise, return 0. */
308 maybe_write_ipa_ust_not_loaded (char *buffer)
310 if (!in_process_agent_loaded ())
312 write_e_ipa_not_loaded (buffer);
315 else if (!in_process_agent_loaded_ust ())
317 write_e_ust_not_loaded (buffer);
323 /* Cache all future symbols that the tracepoints module might request.
324 We can not request symbols at arbitrary states in the remote
325 protocol, only when the client tells us that new symbols are
326 available. So when we load the in-process library, make sure to
327 check the entire list. */
330 tracepoint_look_up_symbols (void)
334 if (all_tracepoint_symbols_looked_up)
337 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
340 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
342 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
345 fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
350 all_tracepoint_symbols_looked_up = 1;
355 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
356 of the "stop_tracing" function. When this breakpoint is hit,
357 tracing stopped in the IPA for some reason. E.g., due to
358 tracepoint reaching the pass count, hitting conditional expression
359 evaluation error, etc.
361 The IPA's trace buffer is never in circular tracing mode: instead,
362 GDBserver's is, and whenever the in-process buffer fills, it calls
363 "flush_trace_buffer", which triggers an internal breakpoint.
364 GDBserver reacts to this breakpoint by pulling the meanwhile
365 collected data. Old frames discarding is always handled on the
368 #ifdef IN_PROCESS_AGENT
369 int debug_threads = 0;
372 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
374 memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
378 /* Call this in the functions where GDBserver places a breakpoint, so
379 that the compiler doesn't try to be clever and skip calling the
380 function at all. This is necessary, even if we tell the compiler
381 to not inline said functions. */
383 #if defined(__GNUC__)
384 # define UNKNOWN_SIDE_EFFECTS() asm ("")
386 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
389 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
392 /* GDBserver places breakpoint here. */
393 UNKNOWN_SIDE_EFFECTS();
396 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
397 flush_trace_buffer (void)
399 /* GDBserver places breakpoint here. */
400 UNKNOWN_SIDE_EFFECTS();
405 #ifndef IN_PROCESS_AGENT
407 tracepoint_handler (CORE_ADDR address)
409 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
414 /* Breakpoint at "stop_tracing" in the inferior lib. */
415 struct breakpoint *stop_tracing_bkpt;
416 static int stop_tracing_handler (CORE_ADDR);
418 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
419 struct breakpoint *flush_trace_buffer_bkpt;
420 static int flush_trace_buffer_handler (CORE_ADDR);
422 static void download_tracepoints (void);
423 static void download_trace_state_variables (void);
424 static void upload_fast_traceframes (void);
426 static int run_inferior_command (char *cmd);
429 read_inferior_integer (CORE_ADDR symaddr, int *val)
431 return read_inferior_memory (symaddr, (unsigned char *) val,
436 read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
438 return read_inferior_memory (symaddr, (unsigned char *) val,
443 read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
445 void *pval = (void *) (uintptr_t) val;
448 ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
449 *val = (uintptr_t) pval;
454 write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
456 void *pval = (void *) (uintptr_t) val;
457 return write_inferior_memory (symaddr,
458 (unsigned char *) &pval, sizeof (pval));
462 write_inferior_integer (CORE_ADDR symaddr, int val)
464 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
468 write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
470 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
475 /* This enum must exactly match what is documented in
476 gdb/doc/agentexpr.texi, including all the numerical values. */
480 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
481 gdb_agent_op_ ## NAME = VALUE,
487 static const char *gdb_agent_op_names [gdb_agent_op_last] =
490 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
495 static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
498 #define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
507 unsigned char *bytes;
510 /* Base action. Concrete actions inherit this. */
512 struct tracepoint_action
517 /* An 'M' (collect memory) action. */
518 struct collect_memory_action
520 struct tracepoint_action base;
527 /* An 'R' (collect registers) action. */
529 struct collect_registers_action
531 struct tracepoint_action base;
534 /* An 'X' (evaluate expression) action. */
536 struct eval_expr_action
538 struct tracepoint_action base;
540 struct agent_expr *expr;
543 /* An 'L' (collect static trace data) action. */
544 struct collect_static_trace_data_action
546 struct tracepoint_action base;
549 /* This structure describes a piece of the source-level definition of
550 the tracepoint. The contents are not interpreted by the target,
551 but preserved verbatim for uploading upon reconnection. */
555 /* The type of string, such as "cond" for a conditional. */
558 /* The source-level string itself. For the sake of target
559 debugging, we store it in plaintext, even though it is always
560 transmitted in hex. */
563 /* Link to the next one in the list. We link them in the order
564 received, in case some make up an ordered list of commands or
566 struct source_string *next;
571 /* Trap based tracepoint. */
574 /* A fast tracepoint implemented with a jump instead of a trap. */
577 /* A static tracepoint, implemented by a program call into a tracing
582 struct tracepoint_hit_ctx;
584 typedef enum eval_result_type (*condfn) (struct tracepoint_hit_ctx *,
587 /* The definition of a tracepoint. */
589 /* Tracepoints may have multiple locations, each at a different
590 address. This can occur with optimizations, template
591 instantiation, etc. Since the locations may be in different
592 scopes, the conditions and actions may be different for each
593 location. Our target version of tracepoints is more like GDB's
594 notion of "breakpoint locations", but we have almost nothing that
595 is not per-location, so we bother having two kinds of objects. The
596 key consequence is that numbers are not unique, and that it takes
597 both number and address to identify a tracepoint uniquely. */
601 /* The number of the tracepoint, as specified by GDB. Several
602 tracepoint objects here may share a number. */
605 /* Address at which the tracepoint is supposed to trigger. Several
606 tracepoints may share an address. */
609 /* Tracepoint type. */
610 enum tracepoint_type type;
612 /* True if the tracepoint is currently enabled. */
615 /* The number of single steps that will be performed after each
619 /* The number of times the tracepoint may be hit before it will
620 terminate the entire tracing run. */
623 /* Pointer to the agent expression that is the tracepoint's
624 conditional, or NULL if the tracepoint is unconditional. */
625 struct agent_expr *cond;
627 /* The list of actions to take when the tracepoint triggers. */
629 struct tracepoint_action **actions;
631 /* Count of the times we've hit this tracepoint during the run.
632 Note that while-stepping steps are not counted as "hits". */
635 /* Cached sum of the sizes of traceframes created by this point. */
636 long traceframe_usage;
638 CORE_ADDR compiled_cond;
640 /* Link to the next tracepoint in the list. */
641 struct tracepoint *next;
643 #ifndef IN_PROCESS_AGENT
644 /* The list of actions to take when the tracepoint triggers, in
645 string/packet form. */
648 /* The collection of strings that describe the tracepoint as it was
649 entered into GDB. These are not used by the target, but are
650 reported back to GDB upon reconnection. */
651 struct source_string *source_strings;
653 /* The number of bytes displaced by fast tracepoints. It may subsume
654 multiple instructions, for multi-byte fast tracepoints. This
655 field is only valid for fast tracepoints. */
658 /* Only for fast tracepoints. */
659 CORE_ADDR obj_addr_on_target;
661 /* Address range where the original instruction under a fast
662 tracepoint was relocated to. (_end is actually one byte past
664 CORE_ADDR adjusted_insn_addr;
665 CORE_ADDR adjusted_insn_addr_end;
667 /* The address range of the piece of the jump pad buffer that was
668 assigned to this fast tracepoint. (_end is actually one byte
671 CORE_ADDR jump_pad_end;
673 /* The address range of the piece of the trampoline buffer that was
674 assigned to this fast tracepoint. (_end is actually one byte
676 CORE_ADDR trampoline;
677 CORE_ADDR trampoline_end;
679 /* The list of actions to take while in a stepping loop. These
680 fields are only valid for patch-based tracepoints. */
681 int num_step_actions;
682 struct tracepoint_action **step_actions;
683 /* Same, but in string/packet form. */
684 char **step_actions_str;
686 /* Handle returned by the breakpoint or tracepoint module when we
687 inserted the trap or jump, or hooked into a static tracepoint.
688 NULL if we haven't inserted it yet. */
694 #ifndef IN_PROCESS_AGENT
696 /* Given `while-stepping', a thread may be collecting data for more
697 than one tracepoint simultaneously. On the other hand, the same
698 tracepoint with a while-stepping action may be hit by more than one
699 thread simultaneously (but not quite, each thread could be handling
700 a different step). Each thread holds a list of these objects,
701 representing the current step of each while-stepping action being
706 struct wstep_state *next;
708 /* The tracepoint number. */
710 /* The tracepoint's address. */
711 CORE_ADDR tp_address;
713 /* The number of the current step in this 'while-stepping'
720 /* The linked list of all tracepoints. Marked explicitly as used as
721 the in-process library doesn't use it for the fast tracepoints
723 IP_AGENT_EXPORT struct tracepoint *tracepoints ATTR_USED;
725 #ifndef IN_PROCESS_AGENT
727 /* Pointer to the last tracepoint in the list, new tracepoints are
728 linked in at the end. */
730 static struct tracepoint *last_tracepoint;
733 /* The first tracepoint to exceed its pass count. */
735 IP_AGENT_EXPORT struct tracepoint *stopping_tracepoint;
737 /* True if the trace buffer is full or otherwise no longer usable. */
739 IP_AGENT_EXPORT int trace_buffer_is_full;
741 /* Enumeration of the different kinds of things that can happen during
742 agent expression evaluation. */
744 enum eval_result_type
747 expr_eval_empty_expression,
748 expr_eval_empty_stack,
749 expr_eval_stack_overflow,
750 expr_eval_stack_underflow,
751 expr_eval_unhandled_opcode,
752 expr_eval_unrecognized_opcode,
753 expr_eval_divide_by_zero,
754 expr_eval_invalid_goto
757 static enum eval_result_type expr_eval_result = expr_eval_no_error;
759 #ifndef IN_PROCESS_AGENT
761 static const char *eval_result_names[] =
763 "terror:in the attic", /* this should never be reported */
764 "terror:empty expression",
765 "terror:empty stack",
766 "terror:stack overflow",
767 "terror:stack underflow",
768 "terror:unhandled opcode",
769 "terror:unrecognized opcode",
770 "terror:divide by zero"
775 /* The tracepoint in which the error occurred. */
777 static struct tracepoint *error_tracepoint;
779 struct trace_state_variable
781 /* This is the name of the variable as used in GDB. The target
782 doesn't use the name, but needs to have it for saving and
783 reconnection purposes. */
786 /* This number identifies the variable uniquely. Numbers may be
787 assigned either by the target (in the case of builtin variables),
788 or by GDB, and are presumed unique during the course of a trace
792 /* The variable's initial value, a 64-bit signed integer always. */
793 LONGEST initial_value;
795 /* The variable's value, a 64-bit signed integer always. */
798 /* Pointer to a getter function, used to supply computed values. */
799 LONGEST (*getter) (void);
801 /* Link to the next variable. */
802 struct trace_state_variable *next;
805 /* Linked list of all trace state variables. */
807 #ifdef IN_PROCESS_AGENT
808 struct trace_state_variable *alloced_trace_state_variables;
811 IP_AGENT_EXPORT struct trace_state_variable *trace_state_variables;
813 /* The results of tracing go into a fixed-size space known as the
814 "trace buffer". Because usage follows a limited number of
815 patterns, we manage it ourselves rather than with malloc. Basic
816 rules are that we create only one trace frame at a time, each is
817 variable in size, they are never moved once created, and we only
818 discard if we are doing a circular buffer, and then only the oldest
819 ones. Each trace frame includes its own size, so we don't need to
820 link them together, and the trace frame number is relative to the
821 first one, so we don't need to record numbers. A trace frame also
822 records the number of the tracepoint that created it. The data
823 itself is a series of blocks, each introduced by a single character
824 and with a defined format. Each type of block has enough
825 type/length info to allow scanners to jump quickly from one block
826 to the next without reading each byte in the block. */
828 /* Trace buffer management would be simple - advance a free pointer
829 from beginning to end, then stop - were it not for the circular
830 buffer option, which is a useful way to prevent a trace run from
831 stopping prematurely because the buffer filled up. In the circular
832 case, the location of the first trace frame (trace_buffer_start)
833 moves as old trace frames are discarded. Also, since we grow trace
834 frames incrementally as actions are performed, we wrap around to
835 the beginning of the trace buffer. This is per-block, so each
836 block within a trace frame remains contiguous. Things get messy
837 when the wrapped-around trace frame is the one being discarded; the
838 free space ends up in two parts at opposite ends of the buffer. */
841 # if defined(__GNUC__)
842 # define ATTR_PACKED __attribute__ ((packed))
844 # define ATTR_PACKED /* nothing */
848 /* The data collected at a tracepoint hit. This object should be as
849 small as possible, since there may be a great many of them. We do
850 not need to keep a frame number, because they are all sequential
851 and there are no deletions; so the Nth frame in the buffer is
852 always frame number N. */
856 /* Number of the tracepoint that collected this traceframe. A value
857 of 0 indicates the current end of the trace buffer. We make this
858 a 16-bit field because it's never going to happen that GDB's
859 numbering of tracepoints reaches 32,000. */
862 /* The size of the data in this trace frame. We limit this to 32
863 bits, even on a 64-bit target, because it's just implausible that
864 one is validly going to collect 4 gigabytes of data at a single
866 unsigned int data_size : 32;
868 /* The base of the trace data, which is contiguous from this point. */
869 unsigned char data[0];
873 /* The traceframe to be used as the source of data to send back to
874 GDB. A value of -1 means to get data from the live program. */
876 int current_traceframe = -1;
878 /* This flag is true if the trace buffer is circular, meaning that
879 when it fills, the oldest trace frames are discarded in order to
882 #ifndef IN_PROCESS_AGENT
883 static int circular_trace_buffer;
886 /* Pointer to the block of memory that traceframes all go into. */
888 static unsigned char *trace_buffer_lo;
890 /* Pointer to the end of the trace buffer, more precisely to the byte
891 after the end of the buffer. */
893 static unsigned char *trace_buffer_hi;
895 /* Control structure holding the read/write/etc. pointers into the
896 trace buffer. We need more than one of these to implement a
897 transaction-like mechanism to garantees that both GDBserver and the
898 in-process agent can try to change the trace buffer
901 struct trace_buffer_control
903 /* Pointer to the first trace frame in the buffer. In the
904 non-circular case, this is equal to trace_buffer_lo, otherwise it
905 moves around in the buffer. */
906 unsigned char *start;
908 /* Pointer to the free part of the trace buffer. Note that we clear
909 several bytes at and after this pointer, so that traceframe
910 scans/searches terminate properly. */
913 /* Pointer to the byte after the end of the free part. Note that
914 this may be smaller than trace_buffer_free in the circular case,
915 and means that the free part is in two pieces. Initially it is
916 equal to trace_buffer_hi, then is generally equivalent to
917 trace_buffer_start. */
918 unsigned char *end_free;
920 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
921 this is the point at which the trace data breaks, and resumes at
926 /* Same as above, to be used by GDBserver when updating the in-process
928 struct ipa_trace_buffer_control
937 /* We have possibly both GDBserver and an inferior thread accessing
938 the same IPA trace buffer memory. The IPA is the producer (tries
939 to put new frames in the buffer), while GDBserver occasionally
940 consumes them, that is, flushes the IPA's buffer into its own
941 buffer. Both sides need to update the trace buffer control
942 pointers (current head, tail, etc.). We can't use a global lock to
943 synchronize the accesses, as otherwise we could deadlock GDBserver
944 (if the thread holding the lock stops for a signal, say). So
945 instead of that, we use a transaction scheme where GDBserver writes
946 always prevail over the IPAs writes, and, we have the IPA detect
947 the commit failure/overwrite, and retry the whole attempt. This is
948 mainly implemented by having a global token object that represents
949 who wrote last to the buffer control structure. We need to freeze
950 any inferior writing to the buffer while GDBserver touches memory,
951 so that the inferior can correctly detect that GDBserver had been
952 there, otherwise, it could mistakingly think its commit was
953 successful; that's implemented by simply having GDBserver set a
954 breakpoint the inferior hits if it is the critical region.
956 There are three cycling trace buffer control structure copies
957 (buffer head, tail, etc.), with the token object including an index
958 indicating which is current live copy. The IPA tentatively builds
959 an updated copy in a non-current control structure, while GDBserver
960 always clobbers the current version directly. The IPA then tries
961 to atomically "commit" its version; if GDBserver clobbered the
962 structure meanwhile, that will fail, and the IPA restarts the
965 Listing the step in further detail, we have:
967 In-process agent (producer):
969 - passes by `about_to_request_buffer_space' breakpoint/lock
971 - reads current token, extracts current trace buffer control index,
972 and starts tentatively updating the rightmost one (0->1, 1->2,
973 2->0). Note that only one inferior thread is executing this code
974 at any given time, due to an outer lock in the jump pads.
976 - updates counters, and tries to commit the token.
978 - passes by second `about_to_request_buffer_space' breakpoint/lock,
979 leaving the sync region.
981 - checks if the update was effective.
983 - if trace buffer was found full, hits flush_trace_buffer
984 breakpoint, and restarts later afterwards.
986 GDBserver (consumer):
988 - sets `about_to_request_buffer_space' breakpoint/lock.
990 - updates the token unconditionally, using the current buffer
991 control index, since it knows that the IP agent always writes to
992 the rightmost, and due to the breakpoint, at most one IP thread
993 can try to update the trace buffer concurrently to GDBserver, so
994 there will be no danger of trace buffer control index wrap making
995 the IPA write to the same index as GDBserver.
997 - flushes the IP agent's trace buffer completely, and updates the
998 current trace buffer control structure. GDBserver *always* wins.
1000 - removes the `about_to_request_buffer_space' breakpoint.
1002 The token is stored in the `trace_buffer_ctrl_curr' variable.
1003 Internally, it's bits are defined as:
1005 |-------------+-----+-------------+--------+-------------+--------------|
1006 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1007 |-------------+-----+-------------+--------+-------------+--------------|
1008 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1009 |-------------+-----+-------------+--------+-------------+--------------|
1011 GSB - GDBserver Stamp Bit
1012 PC - Previous Counter
1013 CC - Current Counter
1014 TBCI - Trace Buffer Control Index
1017 An IPA update of `trace_buffer_ctrl_curr' does:
1019 - read CC from the current token, save as PC.
1021 - atomically tries to write PC+1,CC
1023 A GDBserver update of `trace_buffer_ctrl_curr' does:
1025 - reads PC and CC from the current token.
1030 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1031 for the counters described below. The cleared bits are used to
1032 hold the index of the items of the `trace_buffer_ctrl' array that
1034 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1036 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1037 counter, and the `current' counter. */
1039 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1040 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1042 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1043 always stamps this bit as set. */
1044 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1046 #ifdef IN_PROCESS_AGENT
1047 IP_AGENT_EXPORT struct trace_buffer_control trace_buffer_ctrl[3];
1048 IP_AGENT_EXPORT unsigned int trace_buffer_ctrl_curr;
1050 # define TRACE_BUFFER_CTRL_CURR \
1051 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1055 /* The GDBserver side agent only needs one instance of this object, as
1056 it doesn't need to sync with itself. Define it as array anyway so
1057 that the rest of the code base doesn't need to care for the
1059 struct trace_buffer_control trace_buffer_ctrl[1];
1060 # define TRACE_BUFFER_CTRL_CURR 0
1063 /* These are convenience macros used to access the current trace
1064 buffer control in effect. */
1065 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1066 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1067 #define trace_buffer_end_free \
1068 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1069 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1072 /* Macro that returns a pointer to the first traceframe in the buffer. */
1074 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1076 /* Macro that returns a pointer to the next traceframe in the buffer.
1077 If the computed location is beyond the wraparound point, subtract
1078 the offset of the wraparound. */
1080 #define NEXT_TRACEFRAME_1(TF) \
1081 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1083 #define NEXT_TRACEFRAME(TF) \
1084 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1085 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1086 ? (trace_buffer_wrap - trace_buffer_lo) \
1089 /* The difference between these counters represents the total number
1090 of complete traceframes present in the trace buffer. The IP agent
1091 writes to the write count, GDBserver writes to read count. */
1093 IP_AGENT_EXPORT unsigned int traceframe_write_count;
1094 IP_AGENT_EXPORT unsigned int traceframe_read_count;
1096 /* Convenience macro. */
1098 #define traceframe_count \
1099 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1101 /* The count of all traceframes created in the current run, including
1102 ones that were discarded to make room. */
1104 IP_AGENT_EXPORT int traceframes_created;
1106 #ifndef IN_PROCESS_AGENT
1108 /* Read-only regions are address ranges whose contents don't change,
1109 and so can be read from target memory even while looking at a trace
1110 frame. Without these, disassembly for instance will likely fail,
1111 because the program code is not usually collected into a trace
1112 frame. This data structure does not need to be very complicated or
1113 particularly efficient, it's only going to be used occasionally,
1114 and only by some commands. */
1116 struct readonly_region
1118 /* The bounds of the region. */
1119 CORE_ADDR start, end;
1121 /* Link to the next one. */
1122 struct readonly_region *next;
1125 /* Linked list of readonly regions. This list stays in effect from
1126 one tstart to the next. */
1128 static struct readonly_region *readonly_regions;
1132 /* The global that controls tracing overall. */
1134 IP_AGENT_EXPORT int tracing;
1136 #ifndef IN_PROCESS_AGENT
1138 /* Controls whether tracing should continue after GDB disconnects. */
1140 int disconnected_tracing;
1142 /* The reason for the last tracing run to have stopped. We initialize
1143 to a distinct string so that GDB can distinguish between "stopped
1144 after running" and "stopped because never run" cases. */
1146 static const char *tracing_stop_reason = "tnotrun";
1148 static int tracing_stop_tpnum;
1150 /* 64-bit timestamps for the trace run's start and finish, expressed
1151 in microseconds from the Unix epoch. */
1153 LONGEST tracing_start_time;
1154 LONGEST tracing_stop_time;
1156 /* The (optional) user-supplied name of the user that started the run.
1157 This is an arbitrary string, and may be NULL. */
1159 char *tracing_user_name;
1161 /* Optional user-supplied text describing the run. This is
1162 an arbitrary string, and may be NULL. */
1164 char *tracing_notes;
1166 /* Optional user-supplied text explaining a tstop command. This is an
1167 arbitrary string, and may be NULL. */
1169 char *tracing_stop_note;
1173 /* Functions local to this file. */
1175 /* Base "class" for tracepoint type specific data to be passed down to
1176 collect_data_at_tracepoint. */
1177 struct tracepoint_hit_ctx
1179 enum tracepoint_type type;
1182 #ifdef IN_PROCESS_AGENT
1184 /* Fast/jump tracepoint specific data to be passed down to
1185 collect_data_at_tracepoint. */
1186 struct fast_tracepoint_ctx
1188 struct tracepoint_hit_ctx base;
1190 struct regcache regcache;
1191 int regcache_initted;
1192 unsigned char *regspace;
1194 unsigned char *regs;
1195 struct tracepoint *tpoint;
1198 /* Static tracepoint specific data to be passed down to
1199 collect_data_at_tracepoint. */
1200 struct static_tracepoint_ctx
1202 struct tracepoint_hit_ctx base;
1204 /* The regcache corresponding to the registers state at the time of
1205 the tracepoint hit. Initialized lazily, from REGS. */
1206 struct regcache regcache;
1207 int regcache_initted;
1209 /* The buffer space REGCACHE above uses. We use a separate buffer
1210 instead of letting the regcache malloc for both signal safety and
1211 performance reasons; this is allocated on the stack instead. */
1212 unsigned char *regspace;
1214 /* The register buffer as passed on by lttng/ust. */
1215 struct registers *regs;
1217 /* The "printf" formatter and the args the user passed to the marker
1218 call. We use this to be able to collect "static trace data"
1223 /* The GDB tracepoint matching the probed marker that was "hit". */
1224 struct tracepoint *tpoint;
1229 /* Static tracepoint specific data to be passed down to
1230 collect_data_at_tracepoint. */
1231 struct trap_tracepoint_ctx
1233 struct tracepoint_hit_ctx base;
1235 struct regcache *regcache;
1240 #ifndef IN_PROCESS_AGENT
1241 static struct agent_expr *parse_agent_expr (char **actparm);
1242 static char *unparse_agent_expr (struct agent_expr *aexpr);
1244 static enum eval_result_type eval_agent_expr (struct tracepoint_hit_ctx *ctx,
1245 struct traceframe *tframe,
1246 struct agent_expr *aexpr,
1249 static int agent_mem_read (struct traceframe *tframe,
1250 unsigned char *to, CORE_ADDR from, ULONGEST len);
1251 static int agent_mem_read_string (struct traceframe *tframe,
1252 unsigned char *to, CORE_ADDR from,
1254 static int agent_tsv_read (struct traceframe *tframe, int n);
1256 #ifndef IN_PROCESS_AGENT
1257 static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1258 static int traceframe_read_tsv (int num, LONGEST *val);
1261 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1262 struct tracepoint *tpoint);
1264 #ifndef IN_PROCESS_AGENT
1265 static void clear_readonly_regions (void);
1266 static void clear_installed_tracepoints (void);
1269 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1271 struct tracepoint *tpoint);
1272 #ifndef IN_PROCESS_AGENT
1273 static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1275 struct tracepoint *tpoint, int current_step);
1276 static void compile_tracepoint_condition (struct tracepoint *tpoint,
1277 CORE_ADDR *jump_entry);
1279 static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1281 struct tracepoint *tpoint,
1282 struct traceframe *tframe,
1283 struct tracepoint_action *taction);
1285 #ifndef IN_PROCESS_AGENT
1286 static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1288 static void install_tracepoint (struct tracepoint *, char *own_buf);
1289 static void download_tracepoint (struct tracepoint *);
1290 static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
1293 static LONGEST get_timestamp (void);
1295 #if defined(__GNUC__)
1296 # define memory_barrier() asm volatile ("" : : : "memory")
1298 # define memory_barrier() do {} while (0)
1301 /* We only build the IPA if this builtin is supported, and there are
1302 no uses of this in GDBserver itself, so we're safe in defining this
1304 #define cmpxchg(mem, oldval, newval) \
1305 __sync_val_compare_and_swap (mem, oldval, newval)
1307 /* The size in bytes of the buffer used to talk to the IPA helper
1309 #define CMD_BUF_SIZE 1024
1311 /* Record that an error occurred during expression evaluation. */
1314 record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1315 enum eval_result_type rtype)
1317 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1318 tpoint->number, paddress (tpoint->address), which, rtype);
1320 #ifdef IN_PROCESS_AGENT
1321 /* Only record the first error we get. */
1322 if (cmpxchg (&expr_eval_result,
1324 rtype) != expr_eval_no_error)
1327 if (expr_eval_result != expr_eval_no_error)
1331 error_tracepoint = tpoint;
1334 /* Trace buffer management. */
1337 clear_trace_buffer (void)
1339 trace_buffer_start = trace_buffer_lo;
1340 trace_buffer_free = trace_buffer_lo;
1341 trace_buffer_end_free = trace_buffer_hi;
1342 trace_buffer_wrap = trace_buffer_hi;
1343 /* A traceframe with zeroed fields marks the end of trace data. */
1344 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1345 ((struct traceframe *) trace_buffer_free)->data_size = 0;
1346 traceframe_read_count = traceframe_write_count = 0;
1347 traceframes_created = 0;
1350 #ifndef IN_PROCESS_AGENT
1353 clear_inferior_trace_buffer (void)
1355 CORE_ADDR ipa_trace_buffer_lo;
1356 CORE_ADDR ipa_trace_buffer_hi;
1357 struct traceframe ipa_traceframe = { 0 };
1358 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1360 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1361 &ipa_trace_buffer_lo);
1362 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1363 &ipa_trace_buffer_hi);
1365 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1366 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1367 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1368 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1370 /* A traceframe with zeroed fields marks the end of trace data. */
1371 write_inferior_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1372 (unsigned char *) &ipa_trace_buffer_ctrl,
1373 sizeof (ipa_trace_buffer_ctrl));
1375 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1377 /* A traceframe with zeroed fields marks the end of trace data. */
1378 write_inferior_memory (ipa_trace_buffer_lo,
1379 (unsigned char *) &ipa_traceframe,
1380 sizeof (ipa_traceframe));
1382 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1383 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1384 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1390 init_trace_buffer (unsigned char *buf, int bufsize)
1392 trace_buffer_lo = buf;
1393 trace_buffer_hi = trace_buffer_lo + bufsize;
1395 clear_trace_buffer ();
1398 #ifdef IN_PROCESS_AGENT
1400 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
1401 about_to_request_buffer_space (void)
1403 /* GDBserver places breakpoint here while it goes about to flush
1404 data at random times. */
1405 UNKNOWN_SIDE_EFFECTS();
1410 /* Carve out a piece of the trace buffer, returning NULL in case of
1414 trace_buffer_alloc (size_t amt)
1416 unsigned char *rslt;
1417 struct trace_buffer_control *tbctrl;
1419 #ifdef IN_PROCESS_AGENT
1420 unsigned int prev, prev_filtered;
1421 unsigned int commit_count;
1422 unsigned int commit;
1423 unsigned int readout;
1425 struct traceframe *oldest;
1426 unsigned char *new_start;
1429 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1430 (long) amt, (long) sizeof (struct traceframe));
1432 /* Account for the EOB marker. */
1433 amt += sizeof (struct traceframe);
1435 #ifdef IN_PROCESS_AGENT
1439 /* Read the current token and extract the index to try to write to,
1440 storing it in CURR. */
1441 prev = trace_buffer_ctrl_curr;
1442 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1443 curr = prev_filtered + 1;
1447 about_to_request_buffer_space ();
1449 /* Start out with a copy of the current state. GDBserver may be
1450 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1451 be able to commit anyway if that happens. */
1452 trace_buffer_ctrl[curr]
1453 = trace_buffer_ctrl[prev_filtered];
1454 trace_debug ("trying curr=%u", curr);
1456 /* The GDBserver's agent doesn't need all that syncing, and always
1457 updates TCB 0 (there's only one, mind you). */
1460 tbctrl = &trace_buffer_ctrl[curr];
1462 /* Offsets are easier to grok for debugging than raw addresses,
1463 especially for the small trace buffer sizes that are useful for
1465 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1467 (int) (tbctrl->start - trace_buffer_lo),
1468 (int) (tbctrl->free - trace_buffer_lo),
1469 (int) (tbctrl->end_free - trace_buffer_lo),
1470 (int) (tbctrl->wrap - trace_buffer_lo),
1471 (int) (trace_buffer_hi - trace_buffer_lo));
1473 /* The algorithm here is to keep trying to get a contiguous block of
1474 the requested size, possibly discarding older traceframes to free
1475 up space. Since free space might come in one or two pieces,
1476 depending on whether discarded traceframes wrapped around at the
1477 high end of the buffer, we test both pieces after each
1481 /* First, if we have two free parts, try the upper one first. */
1482 if (tbctrl->end_free < tbctrl->free)
1484 if (tbctrl->free + amt <= trace_buffer_hi)
1485 /* We have enough in the upper part. */
1489 /* Our high part of free space wasn't enough. Give up
1490 on it for now, set wraparound. We will recover the
1491 space later, if/when the wrapped-around traceframe is
1493 trace_debug ("Upper part too small, setting wraparound");
1494 tbctrl->wrap = tbctrl->free;
1495 tbctrl->free = trace_buffer_lo;
1499 /* The normal case. */
1500 if (tbctrl->free + amt <= tbctrl->end_free)
1503 #ifdef IN_PROCESS_AGENT
1504 /* The IP Agent's buffer is always circular. It isn't used
1505 currently, but `circular_trace_buffer' could represent
1506 GDBserver's mode. If we didn't find space, ask GDBserver to
1509 flush_trace_buffer ();
1513 trace_debug ("gdbserver flushed buffer, retrying");
1517 /* GDBserver cancelled the tracing. Bail out as well. */
1520 /* If we're here, then neither part is big enough, and
1521 non-circular trace buffers are now full. */
1522 if (!circular_trace_buffer)
1524 trace_debug ("Not enough space in the trace buffer");
1528 trace_debug ("Need more space in the trace buffer");
1530 /* If we have a circular buffer, we can try discarding the
1531 oldest traceframe and see if that helps. */
1532 oldest = FIRST_TRACEFRAME ();
1533 if (oldest->tpnum == 0)
1535 /* Not good; we have no traceframes to free. Perhaps we're
1536 asking for a block that is larger than the buffer? In
1537 any case, give up. */
1538 trace_debug ("No traceframes to discard");
1542 /* We don't run this code in the in-process agent currently.
1543 E.g., we could leave the in-process agent in autonomous
1544 circular mode if we only have fast tracepoints. If we do
1545 that, then this bit becomes racy with GDBserver, which also
1546 writes to this counter. */
1547 --traceframe_write_count;
1549 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1550 /* If we freed the traceframe that wrapped around, go back
1551 to the non-wrap case. */
1552 if (new_start < tbctrl->start)
1554 trace_debug ("Discarding past the wraparound");
1555 tbctrl->wrap = trace_buffer_hi;
1557 tbctrl->start = new_start;
1558 tbctrl->end_free = tbctrl->start;
1560 trace_debug ("Discarded a traceframe\n"
1561 "Trace buffer [%d], start=%d free=%d "
1562 "endfree=%d wrap=%d hi=%d",
1564 (int) (tbctrl->start - trace_buffer_lo),
1565 (int) (tbctrl->free - trace_buffer_lo),
1566 (int) (tbctrl->end_free - trace_buffer_lo),
1567 (int) (tbctrl->wrap - trace_buffer_lo),
1568 (int) (trace_buffer_hi - trace_buffer_lo));
1570 /* Now go back around the loop. The discard might have resulted
1571 in either one or two pieces of free space, so we want to try
1572 both before freeing any more traceframes. */
1576 /* If we get here, we know we can provide the asked-for space. */
1578 rslt = tbctrl->free;
1580 /* Adjust the request back down, now that we know we have space for
1581 the marker, but don't commit to AMT yet, we may still need to
1582 restart the operation if GDBserver touches the trace buffer
1583 (obviously only important in the in-process agent's version). */
1584 tbctrl->free += (amt - sizeof (struct traceframe));
1586 /* Or not. If GDBserver changed the trace buffer behind our back,
1587 we get to restart a new allocation attempt. */
1589 #ifdef IN_PROCESS_AGENT
1590 /* Build the tentative token. */
1591 commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100)
1592 & GDBSERVER_FLUSH_COUNT_MASK_CURR);
1593 commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12)
1597 /* Try to commit it. */
1598 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1599 if (readout != prev)
1601 trace_debug ("GDBserver has touched the trace buffer, restarting."
1602 " (prev=%08x, commit=%08x, readout=%08x)",
1603 prev, commit, readout);
1607 /* Hold your horses here. Even if that change was committed,
1608 GDBserver could come in, and clobber it. We need to hold to be
1609 able to tell if GDBserver clobbers before or after we committed
1610 the change. Whenever GDBserver goes about touching the IPA
1611 buffer, it sets a breakpoint in this routine, so we have a sync
1613 about_to_request_buffer_space ();
1615 /* Check if the change has been effective, even if GDBserver stopped
1616 us at the breakpoint. */
1619 unsigned int refetch;
1623 refetch = trace_buffer_ctrl_curr;
1625 if (refetch == commit
1626 || ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
1629 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1630 "readout=%08x, refetch=%08x)",
1631 prev, commit, readout, refetch);
1635 trace_debug ("GDBserver has touched the trace buffer, not effective."
1636 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1637 prev, commit, readout, refetch);
1643 /* We have a new piece of the trace buffer. Hurray! */
1645 /* Add an EOB marker just past this allocation. */
1646 ((struct traceframe *) tbctrl->free)->tpnum = 0;
1647 ((struct traceframe *) tbctrl->free)->data_size = 0;
1649 /* Adjust the request back down, now that we know we have space for
1651 amt -= sizeof (struct traceframe);
1655 trace_debug ("Allocated %d bytes", (int) amt);
1656 trace_debug ("Trace buffer [%d] start=%d free=%d "
1657 "endfree=%d wrap=%d hi=%d",
1659 (int) (tbctrl->start - trace_buffer_lo),
1660 (int) (tbctrl->free - trace_buffer_lo),
1661 (int) (tbctrl->end_free - trace_buffer_lo),
1662 (int) (tbctrl->wrap - trace_buffer_lo),
1663 (int) (trace_buffer_hi - trace_buffer_lo));
1669 #ifndef IN_PROCESS_AGENT
1671 /* Return the total free space. This is not necessarily the largest
1672 block we can allocate, because of the two-part case. */
1677 if (trace_buffer_free <= trace_buffer_end_free)
1678 return trace_buffer_end_free - trace_buffer_free;
1680 return ((trace_buffer_end_free - trace_buffer_lo)
1681 + (trace_buffer_hi - trace_buffer_free));
1684 /* An 'S' in continuation packets indicates remainder are for
1687 static int seen_step_action_flag;
1689 /* Create a tracepoint (location) with given number and address. Add this
1690 new tracepoint to list and sort this list. */
1692 static struct tracepoint *
1693 add_tracepoint (int num, CORE_ADDR addr)
1695 struct tracepoint *tpoint, **tp_next;
1697 tpoint = xmalloc (sizeof (struct tracepoint));
1698 tpoint->number = num;
1699 tpoint->address = addr;
1700 tpoint->numactions = 0;
1701 tpoint->actions = NULL;
1702 tpoint->actions_str = NULL;
1703 tpoint->cond = NULL;
1704 tpoint->num_step_actions = 0;
1705 tpoint->step_actions = NULL;
1706 tpoint->step_actions_str = NULL;
1707 /* Start all off as regular (slow) tracepoints. */
1708 tpoint->type = trap_tracepoint;
1709 tpoint->orig_size = -1;
1710 tpoint->source_strings = NULL;
1711 tpoint->compiled_cond = 0;
1712 tpoint->handle = NULL;
1713 tpoint->next = NULL;
1715 /* Find a place to insert this tracepoint into list in order to keep
1716 the tracepoint list still in the ascending order. There may be
1717 multiple tracepoints at the same address as TPOINT's, and this
1718 guarantees TPOINT is inserted after all the tracepoints which are
1719 set at the same address. For example, fast tracepoints A, B, C are
1720 set at the same address, and D is to be insert at the same place as
1723 -->| A |--> | B |-->| C |->...
1725 One jump pad was created for tracepoint A, B, and C, and the target
1726 address of A is referenced/used in jump pad. So jump pad will let
1727 inferior jump to A. If D is inserted in front of A, like this,
1729 -->| D |-->| A |--> | B |-->| C |->...
1731 without updating jump pad, D is not reachable during collect, which
1732 is wrong. As we can see, the order of B, C and D doesn't matter, but
1733 A should always be the `first' one. */
1734 for (tp_next = &tracepoints;
1735 (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
1736 tp_next = &(*tp_next)->next)
1738 tpoint->next = *tp_next;
1740 last_tracepoint = tpoint;
1742 seen_step_action_flag = 0;
1747 #ifndef IN_PROCESS_AGENT
1749 /* Return the tracepoint with the given number and address, or NULL. */
1751 static struct tracepoint *
1752 find_tracepoint (int id, CORE_ADDR addr)
1754 struct tracepoint *tpoint;
1756 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1757 if (tpoint->number == id && tpoint->address == addr)
1763 /* There may be several tracepoints with the same number (because they
1764 are "locations", in GDB parlance); return the next one after the
1765 given tracepoint, or search from the beginning of the list if the
1766 first argument is NULL. */
1768 static struct tracepoint *
1769 find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1771 struct tracepoint *tpoint;
1774 tpoint = prev_tp->next;
1776 tpoint = tracepoints;
1777 for (; tpoint; tpoint = tpoint->next)
1778 if (tpoint->number == num)
1787 save_string (const char *str, size_t len)
1791 s = xmalloc (len + 1);
1792 memcpy (s, str, len);
1798 /* Append another action to perform when the tracepoint triggers. */
1801 add_tracepoint_action (struct tracepoint *tpoint, char *packet)
1807 seen_step_action_flag = 1;
1815 char *act_start = act;
1816 struct tracepoint_action *action = NULL;
1822 struct collect_memory_action *maction;
1826 maction = xmalloc (sizeof *maction);
1827 maction->base.type = *act;
1828 action = &maction->base;
1831 is_neg = (*act == '-');
1834 act = unpack_varlen_hex (act, &basereg);
1836 act = unpack_varlen_hex (act, &maction->addr);
1838 act = unpack_varlen_hex (act, &maction->len);
1839 maction->basereg = (is_neg
1842 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1843 pulongest (maction->len),
1844 paddress (maction->addr), maction->basereg);
1849 struct collect_registers_action *raction;
1851 raction = xmalloc (sizeof *raction);
1852 raction->base.type = *act;
1853 action = &raction->base;
1855 trace_debug ("Want to collect registers");
1857 /* skip past hex digits of mask for now */
1858 while (isxdigit(*act))
1864 struct collect_static_trace_data_action *raction;
1866 raction = xmalloc (sizeof *raction);
1867 raction->base.type = *act;
1868 action = &raction->base;
1870 trace_debug ("Want to collect static trace data");
1875 trace_debug ("Unexpected step action, ignoring");
1880 struct eval_expr_action *xaction;
1882 xaction = xmalloc (sizeof (*xaction));
1883 xaction->base.type = *act;
1884 action = &xaction->base;
1886 trace_debug ("Want to evaluate expression");
1887 xaction->expr = parse_agent_expr (&act);
1891 trace_debug ("unknown trace action '%c', ignoring...", *act);
1900 if (seen_step_action_flag)
1902 tpoint->num_step_actions++;
1904 tpoint->step_actions
1905 = xrealloc (tpoint->step_actions,
1906 (sizeof (*tpoint->step_actions)
1907 * tpoint->num_step_actions));
1908 tpoint->step_actions_str
1909 = xrealloc (tpoint->step_actions_str,
1910 (sizeof (*tpoint->step_actions_str)
1911 * tpoint->num_step_actions));
1912 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
1913 tpoint->step_actions_str[tpoint->num_step_actions - 1]
1914 = save_string (act_start, act - act_start);
1918 tpoint->numactions++;
1920 = xrealloc (tpoint->actions,
1921 sizeof (*tpoint->actions) * tpoint->numactions);
1923 = xrealloc (tpoint->actions_str,
1924 sizeof (*tpoint->actions_str) * tpoint->numactions);
1925 tpoint->actions[tpoint->numactions - 1] = action;
1926 tpoint->actions_str[tpoint->numactions - 1]
1927 = save_string (act_start, act - act_start);
1934 /* Find or create a trace state variable with the given number. */
1936 static struct trace_state_variable *
1937 get_trace_state_variable (int num)
1939 struct trace_state_variable *tsv;
1941 #ifdef IN_PROCESS_AGENT
1942 /* Search for an existing variable. */
1943 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
1944 if (tsv->number == num)
1948 /* Search for an existing variable. */
1949 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1950 if (tsv->number == num)
1956 /* Find or create a trace state variable with the given number. */
1958 static struct trace_state_variable *
1959 create_trace_state_variable (int num, int gdb)
1961 struct trace_state_variable *tsv;
1963 tsv = get_trace_state_variable (num);
1967 /* Create a new variable. */
1968 tsv = xmalloc (sizeof (struct trace_state_variable));
1970 tsv->initial_value = 0;
1974 #ifdef IN_PROCESS_AGENT
1977 tsv->next = alloced_trace_state_variables;
1978 alloced_trace_state_variables = tsv;
1983 tsv->next = trace_state_variables;
1984 trace_state_variables = tsv;
1989 IP_AGENT_EXPORT LONGEST
1990 get_trace_state_variable_value (int num)
1992 struct trace_state_variable *tsv;
1994 tsv = get_trace_state_variable (num);
1998 trace_debug ("No trace state variable %d, skipping value get", num);
2002 /* Call a getter function if we have one. While it's tempting to
2003 set up something to only call the getter once per tracepoint hit,
2004 it could run afoul of thread races. Better to let the getter
2005 handle it directly, if necessary to worry about it. */
2007 tsv->value = (tsv->getter) ();
2009 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
2010 num, plongest (tsv->value));
2015 IP_AGENT_EXPORT void
2016 set_trace_state_variable_value (int num, LONGEST val)
2018 struct trace_state_variable *tsv;
2020 tsv = get_trace_state_variable (num);
2024 trace_debug ("No trace state variable %d, skipping value set", num);
2032 set_trace_state_variable_name (int num, const char *name)
2034 struct trace_state_variable *tsv;
2036 tsv = get_trace_state_variable (num);
2040 trace_debug ("No trace state variable %d, skipping name set", num);
2044 tsv->name = (char *) name;
2048 set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
2050 struct trace_state_variable *tsv;
2052 tsv = get_trace_state_variable (num);
2056 trace_debug ("No trace state variable %d, skipping getter set", num);
2060 tsv->getter = getter;
2063 /* Add a raw traceframe for the given tracepoint. */
2065 static struct traceframe *
2066 add_traceframe (struct tracepoint *tpoint)
2068 struct traceframe *tframe;
2070 tframe = trace_buffer_alloc (sizeof (struct traceframe));
2075 tframe->tpnum = tpoint->number;
2076 tframe->data_size = 0;
2081 /* Add a block to the traceframe currently being worked on. */
2083 static unsigned char *
2084 add_traceframe_block (struct traceframe *tframe, int amt)
2086 unsigned char *block;
2091 block = trace_buffer_alloc (amt);
2096 tframe->data_size += amt;
2101 /* Flag that the current traceframe is finished. */
2104 finish_traceframe (struct traceframe *tframe)
2106 ++traceframe_write_count;
2107 ++traceframes_created;
2110 #ifndef IN_PROCESS_AGENT
2112 /* Given a traceframe number NUM, find the NUMth traceframe in the
2115 static struct traceframe *
2116 find_traceframe (int num)
2118 struct traceframe *tframe;
2121 for (tframe = FIRST_TRACEFRAME ();
2123 tframe = NEXT_TRACEFRAME (tframe))
2134 get_traceframe_address (struct traceframe *tframe)
2137 struct tracepoint *tpoint;
2139 addr = traceframe_get_pc (tframe);
2144 /* Fallback strategy, will be incorrect for while-stepping frames
2145 and multi-location tracepoints. */
2146 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2147 return tpoint->address;
2150 /* Search for the next traceframe whose address is inside or outside
2153 static struct traceframe *
2154 find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2157 struct traceframe *tframe;
2160 *tfnump = current_traceframe + 1;
2161 tframe = find_traceframe (*tfnump);
2162 /* The search is not supposed to wrap around. */
2169 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2171 tfaddr = get_traceframe_address (tframe);
2173 ? (lo <= tfaddr && tfaddr <= hi)
2174 : (lo > tfaddr || tfaddr > hi))
2183 /* Search for the next traceframe recorded by the given tracepoint.
2184 Note that for multi-location tracepoints, this will find whatever
2185 location appears first. */
2187 static struct traceframe *
2188 find_next_traceframe_by_tracepoint (int num, int *tfnump)
2190 struct traceframe *tframe;
2192 *tfnump = current_traceframe + 1;
2193 tframe = find_traceframe (*tfnump);
2194 /* The search is not supposed to wrap around. */
2201 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2203 if (tframe->tpnum == num)
2214 #ifndef IN_PROCESS_AGENT
2216 /* Clear all past trace state. */
2219 cmd_qtinit (char *packet)
2221 struct trace_state_variable *tsv, *prev, *next;
2223 /* Make sure we don't try to read from a trace frame. */
2224 current_traceframe = -1;
2226 trace_debug ("Initializing the trace");
2228 clear_installed_tracepoints ();
2229 clear_readonly_regions ();
2232 last_tracepoint = NULL;
2234 /* Clear out any leftover trace state variables. Ones with target
2235 defined getters should be kept however. */
2237 tsv = trace_state_variables;
2240 trace_debug ("Looking at var %d", tsv->number);
2241 if (tsv->getter == NULL)
2247 trace_state_variables = next;
2248 trace_debug ("Deleting var %d", tsv->number);
2259 clear_trace_buffer ();
2260 clear_inferior_trace_buffer ();
2265 /* Unprobe the UST marker at ADDRESS. */
2268 unprobe_marker_at (CORE_ADDR address)
2270 char cmd[CMD_BUF_SIZE];
2272 sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
2273 run_inferior_command (cmd);
2276 /* Restore the program to its pre-tracing state. This routine may be called
2277 in error situations, so it needs to be careful about only restoring
2278 from known-valid bits. */
2281 clear_installed_tracepoints (void)
2283 struct tracepoint *tpoint;
2284 struct tracepoint *prev_stpoint;
2287 cancel_breakpoints ();
2289 prev_stpoint = NULL;
2291 /* Restore any bytes overwritten by tracepoints. */
2292 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2294 /* Catch the case where we might try to remove a tracepoint that
2295 was never actually installed. */
2296 if (tpoint->handle == NULL)
2298 trace_debug ("Tracepoint %d at 0x%s was "
2299 "never installed, nothing to clear",
2300 tpoint->number, paddress (tpoint->address));
2304 switch (tpoint->type)
2306 case trap_tracepoint:
2307 delete_breakpoint (tpoint->handle);
2309 case fast_tracepoint:
2310 delete_fast_tracepoint_jump (tpoint->handle);
2312 case static_tracepoint:
2313 if (prev_stpoint != NULL
2314 && prev_stpoint->address == tpoint->address)
2315 /* Nothing to do. We already unprobed a tracepoint set at
2316 this marker address (and there can only be one probe
2321 unprobe_marker_at (tpoint->address);
2322 prev_stpoint = tpoint;
2327 tpoint->handle = NULL;
2333 /* Parse a packet that defines a tracepoint. */
2336 cmd_qtdp (char *own_buf)
2339 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2340 int trail_hyphen = 0;
2344 struct tracepoint *tpoint;
2346 char *packet = own_buf;
2348 packet += strlen ("QTDP:");
2350 /* A hyphen at the beginning marks a packet specifying actions for a
2351 tracepoint already supplied. */
2358 packet = unpack_varlen_hex (packet, &num);
2359 ++packet; /* skip a colon */
2360 packet = unpack_varlen_hex (packet, &addr);
2361 ++packet; /* skip a colon */
2363 /* See if we already have this tracepoint. */
2364 tpoint = find_tracepoint (num, addr);
2368 /* Duplicate tracepoints are never allowed. */
2371 trace_debug ("Tracepoint error: tracepoint %d"
2372 " at 0x%s already exists",
2373 (int) num, paddress (addr));
2374 write_enn (own_buf);
2378 tpoint = add_tracepoint (num, addr);
2380 tpoint->enabled = (*packet == 'E');
2381 ++packet; /* skip 'E' */
2382 ++packet; /* skip a colon */
2383 packet = unpack_varlen_hex (packet, &count);
2384 tpoint->step_count = count;
2385 ++packet; /* skip a colon */
2386 packet = unpack_varlen_hex (packet, &count);
2387 tpoint->pass_count = count;
2388 /* See if we have any of the additional optional fields. */
2389 while (*packet == ':')
2394 tpoint->type = fast_tracepoint;
2396 packet = unpack_varlen_hex (packet, &count);
2397 tpoint->orig_size = count;
2399 else if (*packet == 'S')
2401 tpoint->type = static_tracepoint;
2404 else if (*packet == 'X')
2406 actparm = (char *) packet;
2407 tpoint->cond = parse_agent_expr (&actparm);
2410 else if (*packet == '-')
2412 else if (*packet == '\0')
2415 trace_debug ("Unknown optional tracepoint field");
2420 trace_debug ("Also has actions\n");
2423 trace_debug ("Defined %stracepoint %d at 0x%s, "
2424 "enabled %d step %ld pass %ld",
2425 tpoint->type == fast_tracepoint ? "fast "
2427 tpoint->number, paddress (tpoint->address), tpoint->enabled,
2428 tpoint->step_count, tpoint->pass_count);
2431 add_tracepoint_action (tpoint, packet);
2434 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2435 (int) num, paddress (addr));
2436 write_enn (own_buf);
2440 /* Install tracepoint during tracing only once for each tracepoint location.
2441 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2442 determine the last QTDP packet for one tracepoint location by checking
2443 trailing hyphen in QTDP packet. */
2444 if (tracing && !trail_hyphen)
2446 /* Pause all threads temporarily while we patch tracepoints. */
2449 /* download_tracepoint will update global `tracepoints'
2450 list, so it is unsafe to leave threads in jump pad. */
2451 stabilize_threads ();
2453 /* Freeze threads. */
2456 download_tracepoint (tpoint);
2457 install_tracepoint (tpoint, own_buf);
2467 cmd_qtdpsrc (char *own_buf)
2469 ULONGEST num, addr, start, slen;
2470 struct tracepoint *tpoint;
2471 char *packet = own_buf;
2472 char *saved, *srctype, *src;
2474 struct source_string *last, *newlast;
2476 packet += strlen ("QTDPsrc:");
2478 packet = unpack_varlen_hex (packet, &num);
2479 ++packet; /* skip a colon */
2480 packet = unpack_varlen_hex (packet, &addr);
2481 ++packet; /* skip a colon */
2483 /* See if we already have this tracepoint. */
2484 tpoint = find_tracepoint (num, addr);
2488 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2489 (int) num, paddress (addr));
2490 write_enn (own_buf);
2495 packet = strchr (packet, ':');
2496 srctype = xmalloc (packet - saved + 1);
2497 memcpy (srctype, saved, packet - saved);
2498 srctype[packet - saved] = '\0';
2500 packet = unpack_varlen_hex (packet, &start);
2501 ++packet; /* skip a colon */
2502 packet = unpack_varlen_hex (packet, &slen);
2503 ++packet; /* skip a colon */
2504 src = xmalloc (slen + 1);
2505 nbytes = unhexify (src, packet, strlen (packet) / 2);
2508 newlast = xmalloc (sizeof (struct source_string));
2509 newlast->type = srctype;
2511 newlast->next = NULL;
2512 /* Always add a source string to the end of the list;
2513 this keeps sequences of actions/commands in the right
2515 if (tpoint->source_strings)
2517 for (last = tpoint->source_strings; last->next; last = last->next)
2519 last->next = newlast;
2522 tpoint->source_strings = newlast;
2528 cmd_qtdv (char *own_buf)
2530 ULONGEST num, val, builtin;
2533 struct trace_state_variable *tsv;
2534 char *packet = own_buf;
2536 packet += strlen ("QTDV:");
2538 packet = unpack_varlen_hex (packet, &num);
2539 ++packet; /* skip a colon */
2540 packet = unpack_varlen_hex (packet, &val);
2541 ++packet; /* skip a colon */
2542 packet = unpack_varlen_hex (packet, &builtin);
2543 ++packet; /* skip a colon */
2545 nbytes = strlen (packet) / 2;
2546 varname = xmalloc (nbytes + 1);
2547 nbytes = unhexify (varname, packet, nbytes);
2548 varname[nbytes] = '\0';
2550 tsv = create_trace_state_variable (num, 1);
2551 tsv->initial_value = (LONGEST) val;
2552 tsv->name = varname;
2554 set_trace_state_variable_value (num, (LONGEST) val);
2560 cmd_qtenable_disable (char *own_buf, int enable)
2562 char *packet = own_buf;
2564 struct tracepoint *tp;
2566 packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2567 packet = unpack_varlen_hex (packet, &num);
2568 ++packet; /* skip a colon */
2569 packet = unpack_varlen_hex (packet, &addr);
2571 tp = find_tracepoint (num, addr);
2575 if ((enable && tp->enabled) || (!enable && !tp->enabled))
2577 trace_debug ("Tracepoint %d at 0x%s is already %s",
2578 (int) num, paddress (addr),
2579 enable ? "enabled" : "disabled");
2584 trace_debug ("%s tracepoint %d at 0x%s",
2585 enable ? "Enabling" : "Disabling",
2586 (int) num, paddress (addr));
2588 tp->enabled = enable;
2590 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2593 int offset = offsetof (struct tracepoint, enabled);
2594 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2596 ret = prepare_to_access_memory ();
2599 trace_debug ("Failed to temporarily stop inferior threads");
2600 write_enn (own_buf);
2604 ret = write_inferior_integer (obj_addr, enable);
2605 done_accessing_memory ();
2609 trace_debug ("Cannot write enabled flag into "
2610 "inferior process memory");
2611 write_enn (own_buf);
2620 trace_debug ("Tracepoint %d at 0x%s not found",
2621 (int) num, paddress (addr));
2622 write_enn (own_buf);
2627 cmd_qtv (char *own_buf)
2632 char *packet = own_buf;
2634 packet += strlen ("qTV:");
2635 unpack_varlen_hex (packet, &num);
2637 if (current_traceframe >= 0)
2639 err = traceframe_read_tsv ((int) num, &val);
2642 strcpy (own_buf, "U");
2646 /* Only make tsv's be undefined before the first trace run. After a
2647 trace run is over, the user might want to see the last value of
2648 the tsv, and it might not be available in a traceframe. */
2649 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2651 strcpy (own_buf, "U");
2655 val = get_trace_state_variable_value (num);
2657 sprintf (own_buf, "V%s", phex_nz (val, 0));
2660 /* Clear out the list of readonly regions. */
2663 clear_readonly_regions (void)
2665 struct readonly_region *roreg;
2667 while (readonly_regions)
2669 roreg = readonly_regions;
2670 readonly_regions = readonly_regions->next;
2675 /* Parse the collection of address ranges whose contents GDB believes
2676 to be unchanging and so can be read directly from target memory
2677 even while looking at a traceframe. */
2680 cmd_qtro (char *own_buf)
2682 ULONGEST start, end;
2683 struct readonly_region *roreg;
2684 char *packet = own_buf;
2686 trace_debug ("Want to mark readonly regions");
2688 clear_readonly_regions ();
2690 packet += strlen ("QTro");
2692 while (*packet == ':')
2694 ++packet; /* skip a colon */
2695 packet = unpack_varlen_hex (packet, &start);
2696 ++packet; /* skip a comma */
2697 packet = unpack_varlen_hex (packet, &end);
2698 roreg = xmalloc (sizeof (struct readonly_region));
2699 roreg->start = start;
2701 roreg->next = readonly_regions;
2702 readonly_regions = roreg;
2703 trace_debug ("Added readonly region from 0x%s to 0x%s",
2704 paddress (roreg->start), paddress (roreg->end));
2710 /* Test to see if the given range is in our list of readonly ranges.
2711 We only test for being entirely within a range, GDB is not going to
2712 send a single memory packet that spans multiple regions. */
2715 in_readonly_region (CORE_ADDR addr, ULONGEST length)
2717 struct readonly_region *roreg;
2719 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2720 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2726 /* The maximum size of a jump pad entry. */
2727 static const int max_jump_pad_size = 0x100;
2729 static CORE_ADDR gdb_jump_pad_head;
2731 /* Return the address of the next free jump space. */
2734 get_jump_space_head (void)
2736 if (gdb_jump_pad_head == 0)
2738 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2739 &gdb_jump_pad_head))
2740 fatal ("error extracting jump_pad_buffer");
2743 return gdb_jump_pad_head;
2746 /* Reserve USED bytes from the jump space. */
2749 claim_jump_space (ULONGEST used)
2751 trace_debug ("claim_jump_space reserves %s bytes at %s",
2752 pulongest (used), paddress (gdb_jump_pad_head));
2753 gdb_jump_pad_head += used;
2756 static CORE_ADDR trampoline_buffer_head = 0;
2757 static CORE_ADDR trampoline_buffer_tail;
2759 /* Reserve USED bytes from the trampoline buffer and return the
2760 address of the start of the reserved space in TRAMPOLINE. Returns
2761 non-zero if the space is successfully claimed. */
2764 claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
2766 if (!trampoline_buffer_head)
2768 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
2769 &trampoline_buffer_tail))
2771 fatal ("error extracting trampoline_buffer");
2775 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2776 &trampoline_buffer_head))
2778 fatal ("error extracting trampoline_buffer_end");
2783 /* Start claiming space from the top of the trampoline space. If
2784 the space is located at the bottom of the virtual address space,
2785 this reduces the possibility that corruption will occur if a null
2786 pointer is used to write to memory. */
2787 if (trampoline_buffer_head - trampoline_buffer_tail < used)
2789 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2794 trampoline_buffer_head -= used;
2796 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2797 pulongest (used), paddress (trampoline_buffer_head));
2799 *trampoline = trampoline_buffer_head;
2803 /* Returns non-zero if there is space allocated for use in trampolines
2804 for fast tracepoints. */
2807 have_fast_tracepoint_trampoline_buffer (char *buf)
2809 CORE_ADDR trampoline_end, errbuf;
2811 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2814 fatal ("error extracting trampoline_buffer_end");
2821 strcpy (buf, "was claiming");
2822 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
2825 fatal ("error extracting errbuf");
2829 read_inferior_memory (errbuf, (unsigned char *) buf, 100);
2832 return trampoline_end != 0;
2835 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2836 the command fails, or 0 otherwise. If the command ran
2837 successfully, but probing the marker failed, ERROUT will be filled
2838 with the error to reply to GDB, and -1 is also returned. This
2839 allows directly passing IPA errors to GDB. */
2842 probe_marker_at (CORE_ADDR address, char *errout)
2844 char cmd[CMD_BUF_SIZE];
2847 sprintf (cmd, "probe_marker_at:%s", paddress (address));
2848 err = run_inferior_command (cmd);
2854 strcpy (errout, cmd);
2863 clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
2865 to->jump_pad = from->jump_pad;
2866 to->jump_pad_end = from->jump_pad_end;
2867 to->trampoline = from->trampoline;
2868 to->trampoline_end = from->trampoline_end;
2869 to->adjusted_insn_addr = from->adjusted_insn_addr;
2870 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
2871 to->handle = from->handle;
2873 gdb_assert (from->handle);
2874 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
2877 #define MAX_JUMP_SIZE 20
2879 /* Install fast tracepoint. Return 0 if successful, otherwise return
2883 install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
2885 CORE_ADDR jentry, jump_entry;
2886 CORE_ADDR trampoline;
2887 ULONGEST trampoline_size;
2889 /* The jump to the jump pad of the last fast tracepoint
2891 unsigned char fjump[MAX_JUMP_SIZE];
2892 ULONGEST fjump_size;
2894 if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
2896 trace_debug ("Requested a fast tracepoint on an instruction "
2897 "that is of less than the minimum length.");
2901 jentry = jump_entry = get_jump_space_head ();
2904 trampoline_size = 0;
2906 /* Install the jump pad. */
2907 err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
2909 ipa_sym_addrs.addr_gdb_collect,
2910 ipa_sym_addrs.addr_collecting,
2913 &trampoline, &trampoline_size,
2915 &tpoint->adjusted_insn_addr,
2916 &tpoint->adjusted_insn_addr_end,
2923 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
2926 if (tpoint->handle != NULL)
2928 tpoint->jump_pad = jump_entry;
2929 tpoint->jump_pad_end = jentry;
2930 tpoint->trampoline = trampoline;
2931 tpoint->trampoline_end = trampoline + trampoline_size;
2933 /* Pad to 8-byte alignment. */
2934 jentry = ((jentry + 7) & ~0x7);
2935 claim_jump_space (jentry - jump_entry);
2942 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2945 install_tracepoint (struct tracepoint *tpoint, char *own_buf)
2947 tpoint->handle = NULL;
2950 if (tpoint->type == trap_tracepoint)
2952 /* Tracepoints are installed as memory breakpoints. Just go
2953 ahead and install the trap. The breakpoints module
2954 handles duplicated breakpoints, and the memory read
2955 routine handles un-patching traps from memory reads. */
2956 tpoint->handle = set_breakpoint_at (tpoint->address,
2957 tracepoint_handler);
2959 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
2961 struct tracepoint *tp;
2963 if (!in_process_agent_loaded ())
2965 trace_debug ("Requested a %s tracepoint, but fast "
2966 "tracepoints aren't supported.",
2967 tpoint->type == static_tracepoint ? "static" : "fast");
2968 write_e_ipa_not_loaded (own_buf);
2971 if (tpoint->type == static_tracepoint && !in_process_agent_loaded_ust ())
2973 trace_debug ("Requested a static tracepoint, but static "
2974 "tracepoints are not supported.");
2975 write_e_ust_not_loaded (own_buf);
2979 /* Find another fast or static tracepoint at the same address. */
2980 for (tp = tracepoints; tp; tp = tp->next)
2982 if (tp->address == tpoint->address && tp->type == tpoint->type
2983 && tp->number != tpoint->number)
2987 if (tpoint->type == fast_tracepoint)
2989 if (tp) /* TPOINT is installed at the same address as TP. */
2990 clone_fast_tracepoint (tpoint, tp);
2992 install_fast_tracepoint (tpoint, own_buf);
2997 tpoint->handle = (void *) -1;
3000 if (probe_marker_at (tpoint->address, own_buf) == 0)
3001 tpoint->handle = (void *) -1;
3007 internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
3009 if (tpoint->handle == NULL)
3011 if (*own_buf == '\0')
3012 write_enn (own_buf);
3019 cmd_qtstart (char *packet)
3021 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
3023 trace_debug ("Starting the trace");
3025 /* Pause all threads temporarily while we patch tracepoints. */
3028 /* Get threads out of jump pads. Safe to do here, since this is a
3029 top level command. And, required to do here, since we're
3030 deleting/rewriting jump pads. */
3032 stabilize_threads ();
3034 /* Freeze threads. */
3037 /* Sync the fast tracepoints list in the inferior ftlib. */
3038 if (in_process_agent_loaded ())
3040 download_tracepoints ();
3041 download_trace_state_variables ();
3044 /* No previous fast tpoint yet. */
3045 prev_ftpoint = NULL;
3047 /* No previous static tpoint yet. */
3048 prev_stpoint = NULL;
3052 /* Install tracepoints. */
3053 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3055 /* Ensure all the hit counts start at zero. */
3056 tpoint->hit_count = 0;
3057 tpoint->traceframe_usage = 0;
3059 if (tpoint->type == trap_tracepoint)
3061 /* Tracepoints are installed as memory breakpoints. Just go
3062 ahead and install the trap. The breakpoints module
3063 handles duplicated breakpoints, and the memory read
3064 routine handles un-patching traps from memory reads. */
3065 tpoint->handle = set_breakpoint_at (tpoint->address,
3066 tracepoint_handler);
3068 else if (tpoint->type == fast_tracepoint)
3070 if (maybe_write_ipa_not_loaded (packet))
3072 trace_debug ("Requested a fast tracepoint, but fast "
3073 "tracepoints aren't supported.");
3077 if (prev_ftpoint != NULL && prev_ftpoint->address == tpoint->address)
3078 clone_fast_tracepoint (tpoint, prev_ftpoint);
3081 if (install_fast_tracepoint (tpoint, packet) == 0)
3082 prev_ftpoint = tpoint;
3085 else if (tpoint->type == static_tracepoint)
3087 if (maybe_write_ipa_ust_not_loaded (packet))
3089 trace_debug ("Requested a static tracepoint, but static "
3090 "tracepoints are not supported.");
3094 /* Can only probe a given marker once. */
3095 if (prev_stpoint != NULL && prev_stpoint->address == tpoint->address)
3097 tpoint->handle = (void *) -1;
3101 if (probe_marker_at (tpoint->address, packet) == 0)
3103 tpoint->handle = (void *) -1;
3105 /* So that we can handle multiple static tracepoints
3106 at the same address easily. */
3107 prev_stpoint = tpoint;
3112 /* Any failure in the inner loop is sufficient cause to give
3114 if (tpoint->handle == NULL)
3118 /* Any error in tracepoint insertion is unacceptable; better to
3119 address the problem now, than end up with a useless or misleading
3123 clear_installed_tracepoints ();
3124 if (*packet == '\0')
3130 stopping_tracepoint = NULL;
3131 trace_buffer_is_full = 0;
3132 expr_eval_result = expr_eval_no_error;
3133 error_tracepoint = NULL;
3134 tracing_start_time = get_timestamp ();
3136 /* Tracing is now active, hits will now start being logged. */
3139 if (in_process_agent_loaded ())
3141 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
3142 fatal ("Error setting tracing variable in lib");
3144 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3146 fatal ("Error clearing stopping_tracepoint variable in lib");
3148 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
3149 fatal ("Error clearing trace_buffer_is_full variable in lib");
3151 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3152 stop_tracing_handler);
3153 if (stop_tracing_bkpt == NULL)
3154 error ("Error setting stop_tracing breakpoint");
3156 flush_trace_buffer_bkpt
3157 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3158 flush_trace_buffer_handler);
3159 if (flush_trace_buffer_bkpt == NULL)
3160 error ("Error setting flush_trace_buffer breakpoint");
3168 /* End a tracing run, filling in a stop reason to report back to GDB,
3169 and removing the tracepoints from the code. */
3176 trace_debug ("Tracing is already off, ignoring");
3180 trace_debug ("Stopping the trace");
3182 /* Pause all threads before removing fast jumps from memory,
3183 breakpoints, and touching IPA state variables (inferior memory).
3184 Some thread may hit the internal tracing breakpoints, or be
3185 collecting this moment, but that's ok, we don't release the
3186 tpoint object's memory or the jump pads here (we only do that
3187 when we're sure we can move all threads out of the jump pads).
3188 We can't now, since we may be getting here due to the inferior
3189 agent calling us. */
3191 /* Since we're removing breakpoints, cancel breakpoint hits,
3192 possibly related to the breakpoints we're about to delete. */
3193 cancel_breakpoints ();
3195 /* Stop logging. Tracepoints can still be hit, but they will not be
3198 if (in_process_agent_loaded ())
3200 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
3201 fatal ("Error clearing tracing variable in lib");
3204 tracing_stop_time = get_timestamp ();
3205 tracing_stop_reason = "t???";
3206 tracing_stop_tpnum = 0;
3207 if (stopping_tracepoint)
3209 trace_debug ("Stopping the trace because "
3210 "tracepoint %d was hit %ld times",
3211 stopping_tracepoint->number,
3212 stopping_tracepoint->pass_count);
3213 tracing_stop_reason = "tpasscount";
3214 tracing_stop_tpnum = stopping_tracepoint->number;
3216 else if (trace_buffer_is_full)
3218 trace_debug ("Stopping the trace because the trace buffer is full");
3219 tracing_stop_reason = "tfull";
3221 else if (expr_eval_result != expr_eval_no_error)
3223 trace_debug ("Stopping the trace because of an expression eval error");
3224 tracing_stop_reason = eval_result_names[expr_eval_result];
3225 tracing_stop_tpnum = error_tracepoint->number;
3227 #ifndef IN_PROCESS_AGENT
3228 else if (!gdb_connected ())
3230 trace_debug ("Stopping the trace because GDB disconnected");
3231 tracing_stop_reason = "tdisconnected";
3236 trace_debug ("Stopping the trace because of a tstop command");
3237 tracing_stop_reason = "tstop";
3240 stopping_tracepoint = NULL;
3241 error_tracepoint = NULL;
3243 /* Clear out the tracepoints. */
3244 clear_installed_tracepoints ();
3246 if (in_process_agent_loaded ())
3248 /* Pull in fast tracepoint trace frames from the inferior lib
3249 buffer into our buffer, even if our buffer is already full,
3250 because we want to present the full number of created frames
3251 in addition to what fit in the trace buffer. */
3252 upload_fast_traceframes ();
3255 if (stop_tracing_bkpt != NULL)
3257 delete_breakpoint (stop_tracing_bkpt);
3258 stop_tracing_bkpt = NULL;
3261 if (flush_trace_buffer_bkpt != NULL)
3263 delete_breakpoint (flush_trace_buffer_bkpt);
3264 flush_trace_buffer_bkpt = NULL;
3271 stop_tracing_handler (CORE_ADDR addr)
3273 trace_debug ("lib hit stop_tracing");
3275 /* Don't actually handle it here. When we stop tracing we remove
3276 breakpoints from the inferior, and that is not allowed in a
3277 breakpoint handler (as the caller is walking the breakpoint
3283 flush_trace_buffer_handler (CORE_ADDR addr)
3285 trace_debug ("lib hit flush_trace_buffer");
3290 cmd_qtstop (char *packet)
3297 cmd_qtdisconnected (char *own_buf)
3300 char *packet = own_buf;
3302 packet += strlen ("QTDisconnected:");
3304 unpack_varlen_hex (packet, &setting);
3308 disconnected_tracing = setting;
3312 cmd_qtframe (char *own_buf)
3314 ULONGEST frame, pc, lo, hi, num;
3316 struct traceframe *tframe;
3317 char *packet = own_buf;
3319 packet += strlen ("QTFrame:");
3321 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3323 packet += strlen ("pc:");
3324 unpack_varlen_hex (packet, &pc);
3325 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3326 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3328 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3330 packet += strlen ("range:");
3331 packet = unpack_varlen_hex (packet, &lo);
3333 unpack_varlen_hex (packet, &hi);
3334 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3335 paddress (lo), paddress (hi));
3336 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3338 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3340 packet += strlen ("outside:");
3341 packet = unpack_varlen_hex (packet, &lo);
3343 unpack_varlen_hex (packet, &hi);
3344 trace_debug ("Want to find next traceframe "
3345 "outside the range 0x%s to 0x%s",
3346 paddress (lo), paddress (hi));
3347 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3349 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3351 packet += strlen ("tdp:");
3352 unpack_varlen_hex (packet, &num);
3354 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3355 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3359 unpack_varlen_hex (packet, &frame);
3360 tfnum = (int) frame;
3363 trace_debug ("Want to stop looking at traceframes");
3364 current_traceframe = -1;
3368 trace_debug ("Want to look at traceframe %d", tfnum);
3369 tframe = find_traceframe (tfnum);
3374 current_traceframe = tfnum;
3375 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3378 sprintf (own_buf, "F-1");
3382 cmd_qtstatus (char *packet)
3384 char *stop_reason_rsp = NULL;
3385 char *buf1, *buf2, *buf3, *str;
3388 /* Translate the plain text of the notes back into hex for
3391 str = (tracing_user_name ? tracing_user_name : "");
3392 slen = strlen (str);
3393 buf1 = (char *) alloca (slen * 2 + 1);
3394 hexify (buf1, str, slen);
3396 str = (tracing_notes ? tracing_notes : "");
3397 slen = strlen (str);
3398 buf2 = (char *) alloca (slen * 2 + 1);
3399 hexify (buf2, str, slen);
3401 str = (tracing_stop_note ? tracing_stop_note : "");
3402 slen = strlen (str);
3403 buf3 = (char *) alloca (slen * 2 + 1);
3404 hexify (buf3, str, slen);
3406 trace_debug ("Returning trace status as %d, stop reason %s",
3407 tracing, tracing_stop_reason);
3409 if (in_process_agent_loaded ())
3413 upload_fast_traceframes ();
3418 stop_reason_rsp = (char *) tracing_stop_reason;
3420 /* The user visible error string in terror needs to be hex encoded.
3421 We leave it as plain string in `tracing_stop_reason' to ease
3423 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3425 const char *result_name;
3429 result_name = stop_reason_rsp + strlen ("terror:");
3430 hexstr_len = strlen (result_name) * 2;
3431 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3432 strcpy (p, "terror:");
3434 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3437 /* If this was a forced stop, include any stop note that was supplied. */
3438 if (strcmp (stop_reason_rsp, "tstop") == 0)
3440 stop_reason_rsp = alloca (strlen ("tstop:") + strlen (buf3) + 1);
3441 strcpy (stop_reason_rsp, "tstop:");
3442 strcat (stop_reason_rsp, buf3);
3448 "tframes:%x;tcreated:%x;"
3449 "tfree:%x;tsize:%s;"
3452 "starttime:%s;stoptime:%s;"
3453 "username:%s:;notes:%s:",
3455 stop_reason_rsp, tracing_stop_tpnum,
3456 traceframe_count, traceframes_created,
3457 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3458 circular_trace_buffer,
3459 disconnected_tracing,
3460 plongest (tracing_start_time), plongest (tracing_stop_time),
3465 cmd_qtp (char *own_buf)
3468 struct tracepoint *tpoint;
3469 char *packet = own_buf;
3471 packet += strlen ("qTP:");
3473 packet = unpack_varlen_hex (packet, &num);
3474 ++packet; /* skip a colon */
3475 packet = unpack_varlen_hex (packet, &addr);
3477 /* See if we already have this tracepoint. */
3478 tpoint = find_tracepoint (num, addr);
3482 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3483 (int) num, paddress (addr));
3484 write_enn (own_buf);
3488 sprintf (own_buf, "V%lx:%lx", tpoint->hit_count, tpoint->traceframe_usage);
3491 /* State variables to help return all the tracepoint bits. */
3492 static struct tracepoint *cur_tpoint;
3493 static int cur_action;
3494 static int cur_step_action;
3495 static struct source_string *cur_source_string;
3496 static struct trace_state_variable *cur_tsv;
3498 /* Compose a response that is an imitation of the syntax by which the
3499 tracepoint was originally downloaded. */
3502 response_tracepoint (char *packet, struct tracepoint *tpoint)
3506 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
3507 paddress (tpoint->address),
3508 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3509 tpoint->pass_count);
3510 if (tpoint->type == fast_tracepoint)
3511 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
3512 else if (tpoint->type == static_tracepoint)
3513 sprintf (packet + strlen (packet), ":S");
3517 buf = unparse_agent_expr (tpoint->cond);
3518 sprintf (packet + strlen (packet), ":X%x,%s",
3519 tpoint->cond->length, buf);
3524 /* Compose a response that is an imitation of the syntax by which the
3525 tracepoint action was originally downloaded (with the difference
3526 that due to the way we store the actions, this will output a packet
3527 per action, while GDB could have combined more than one action
3531 response_action (char *packet, struct tracepoint *tpoint,
3532 char *taction, int step)
3534 sprintf (packet, "%c%x:%s:%s",
3535 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3539 /* Compose a response that is an imitation of the syntax by which the
3540 tracepoint source piece was originally downloaded. */
3543 response_source (char *packet,
3544 struct tracepoint *tpoint, struct source_string *src)
3549 len = strlen (src->str);
3550 buf = alloca (len * 2 + 1);
3551 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3553 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3554 tpoint->number, paddress (tpoint->address),
3555 src->type, 0, len, buf);
3558 /* Return the first piece of tracepoint definition, and initialize the
3559 state machine that will iterate through all the tracepoint
3563 cmd_qtfp (char *packet)
3565 trace_debug ("Returning first tracepoint definition piece");
3567 cur_tpoint = tracepoints;
3568 cur_action = cur_step_action = -1;
3569 cur_source_string = NULL;
3572 response_tracepoint (packet, cur_tpoint);
3574 strcpy (packet, "l");
3577 /* Return additional pieces of tracepoint definition. Each action and
3578 stepping action must go into its own packet, because of packet size
3579 limits, and so we use state variables to deliver one piece at a
3583 cmd_qtsp (char *packet)
3585 trace_debug ("Returning subsequent tracepoint definition piece");
3589 /* This case would normally never occur, but be prepared for
3591 strcpy (packet, "l");
3593 else if (cur_action < cur_tpoint->numactions - 1)
3596 response_action (packet, cur_tpoint,
3597 cur_tpoint->actions_str[cur_action], 0);
3599 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3602 response_action (packet, cur_tpoint,
3603 cur_tpoint->step_actions_str[cur_step_action], 1);
3605 else if ((cur_source_string
3606 ? cur_source_string->next
3607 : cur_tpoint->source_strings))
3609 if (cur_source_string)
3610 cur_source_string = cur_source_string->next;
3612 cur_source_string = cur_tpoint->source_strings;
3613 response_source (packet, cur_tpoint, cur_source_string);
3617 cur_tpoint = cur_tpoint->next;
3618 cur_action = cur_step_action = -1;
3619 cur_source_string = NULL;
3621 response_tracepoint (packet, cur_tpoint);
3623 strcpy (packet, "l");
3627 /* Compose a response that is an imitation of the syntax by which the
3628 trace state variable was originally downloaded. */
3631 response_tsv (char *packet, struct trace_state_variable *tsv)
3633 char *buf = (char *) "";
3638 namelen = strlen (tsv->name);
3639 buf = alloca (namelen * 2 + 1);
3640 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3643 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3644 tsv->getter ? 1 : 0, buf);
3647 /* Return the first trace state variable definition, and initialize
3648 the state machine that will iterate through all the tsv bits. */
3651 cmd_qtfv (char *packet)
3653 trace_debug ("Returning first trace state variable definition");
3655 cur_tsv = trace_state_variables;
3658 response_tsv (packet, cur_tsv);
3660 strcpy (packet, "l");
3663 /* Return additional trace state variable definitions. */
3666 cmd_qtsv (char *packet)
3668 trace_debug ("Returning first trace state variable definition");
3672 /* This case would normally never occur, but be prepared for
3674 strcpy (packet, "l");
3678 cur_tsv = cur_tsv->next;
3680 response_tsv (packet, cur_tsv);
3682 strcpy (packet, "l");
3685 strcpy (packet, "l");
3688 /* Return the first static tracepoint marker, and initialize the state
3689 machine that will iterate through all the static tracepoints
3693 cmd_qtfstm (char *packet)
3695 if (!maybe_write_ipa_ust_not_loaded (packet))
3696 run_inferior_command (packet);
3699 /* Return additional static tracepoints markers. */
3702 cmd_qtsstm (char *packet)
3704 if (!maybe_write_ipa_ust_not_loaded (packet))
3705 run_inferior_command (packet);
3708 /* Return the definition of the static tracepoint at a given address.
3709 Result packet is the same as qTsST's. */
3712 cmd_qtstmat (char *packet)
3714 if (!maybe_write_ipa_ust_not_loaded (packet))
3715 run_inferior_command (packet);
3718 /* Return the minimum instruction size needed for fast tracepoints as a
3719 hexadecimal number. */
3722 cmd_qtminftpilen (char *packet)
3724 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
3727 /* Respond to qTBuffer packet with a block of raw data from the trace
3728 buffer. GDB may ask for a lot, but we are allowed to reply with
3729 only as much as will fit within packet limits or whatever. */
3732 cmd_qtbuffer (char *own_buf)
3734 ULONGEST offset, num, tot;
3736 char *packet = own_buf;
3738 packet += strlen ("qTBuffer:");
3740 packet = unpack_varlen_hex (packet, &offset);
3741 ++packet; /* skip a comma */
3742 unpack_varlen_hex (packet, &num);
3744 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3745 (int) num, pulongest (offset));
3747 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3749 /* If we're right at the end, reply specially that we're done. */
3752 strcpy (own_buf, "l");
3756 /* Object to any other out-of-bounds request. */
3759 write_enn (own_buf);
3763 /* Compute the pointer corresponding to the given offset, accounting
3765 tbp = trace_buffer_start + offset;
3766 if (tbp >= trace_buffer_wrap)
3767 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3769 /* Trim to the remaining bytes if we're close to the end. */
3770 if (num > tot - offset)
3773 /* Trim to available packet size. */
3774 if (num >= (PBUFSIZ - 16) / 2 )
3775 num = (PBUFSIZ - 16) / 2;
3777 convert_int_to_ascii (tbp, own_buf, num);
3778 own_buf[num] = '\0';
3782 cmd_bigqtbuffer (char *own_buf)
3785 char *packet = own_buf;
3787 packet += strlen ("QTBuffer:");
3789 if (strncmp ("circular:", packet, strlen ("circular:")) == 0)
3791 packet += strlen ("circular:");
3792 unpack_varlen_hex (packet, &val);
3793 circular_trace_buffer = val;
3794 trace_debug ("Trace buffer is now %s",
3795 circular_trace_buffer ? "circular" : "linear");
3799 write_enn (own_buf);
3803 cmd_qtnotes (char *own_buf)
3806 char *saved, *user, *notes, *stopnote;
3807 char *packet = own_buf;
3809 packet += strlen ("QTNotes:");
3813 if (strncmp ("user:", packet, strlen ("user:")) == 0)
3815 packet += strlen ("user:");
3817 packet = strchr (packet, ';');
3818 nbytes = (packet - saved) / 2;
3819 user = xmalloc (nbytes + 1);
3820 nbytes = unhexify (user, saved, nbytes);
3821 user[nbytes] = '\0';
3822 ++packet; /* skip the semicolon */
3823 trace_debug ("User is '%s'", user);
3824 tracing_user_name = user;
3826 else if (strncmp ("notes:", packet, strlen ("notes:")) == 0)
3828 packet += strlen ("notes:");
3830 packet = strchr (packet, ';');
3831 nbytes = (packet - saved) / 2;
3832 notes = xmalloc (nbytes + 1);
3833 nbytes = unhexify (notes, saved, nbytes);
3834 notes[nbytes] = '\0';
3835 ++packet; /* skip the semicolon */
3836 trace_debug ("Notes is '%s'", notes);
3837 tracing_notes = notes;
3839 else if (strncmp ("tstop:", packet, strlen ("tstop:")) == 0)
3841 packet += strlen ("tstop:");
3843 packet = strchr (packet, ';');
3844 nbytes = (packet - saved) / 2;
3845 stopnote = xmalloc (nbytes + 1);
3846 nbytes = unhexify (stopnote, saved, nbytes);
3847 stopnote[nbytes] = '\0';
3848 ++packet; /* skip the semicolon */
3849 trace_debug ("tstop note is '%s'", stopnote);
3850 tracing_stop_note = stopnote;
3860 handle_tracepoint_general_set (char *packet)
3862 if (strcmp ("QTinit", packet) == 0)
3864 cmd_qtinit (packet);
3867 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3872 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3874 cmd_qtdpsrc (packet);
3877 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
3879 cmd_qtenable_disable (packet, 1);
3882 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
3884 cmd_qtenable_disable (packet, 0);
3887 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3892 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3897 else if (strcmp ("QTStart", packet) == 0)
3899 cmd_qtstart (packet);
3902 else if (strcmp ("QTStop", packet) == 0)
3904 cmd_qtstop (packet);
3907 else if (strncmp ("QTDisconnected:", packet,
3908 strlen ("QTDisconnected:")) == 0)
3910 cmd_qtdisconnected (packet);
3913 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
3915 cmd_qtframe (packet);
3918 else if (strncmp ("QTBuffer:", packet, strlen ("QTBuffer:")) == 0)
3920 cmd_bigqtbuffer (packet);
3923 else if (strncmp ("QTNotes:", packet, strlen ("QTNotes:")) == 0)
3925 cmd_qtnotes (packet);
3933 handle_tracepoint_query (char *packet)
3935 if (strcmp ("qTStatus", packet) == 0)
3937 cmd_qtstatus (packet);
3940 else if (strncmp ("qTP:", packet, strlen ("qTP:")) == 0)
3945 else if (strcmp ("qTfP", packet) == 0)
3950 else if (strcmp ("qTsP", packet) == 0)
3955 else if (strcmp ("qTfV", packet) == 0)
3960 else if (strcmp ("qTsV", packet) == 0)
3965 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
3970 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
3972 cmd_qtbuffer (packet);
3975 else if (strcmp ("qTfSTM", packet) == 0)
3977 cmd_qtfstm (packet);
3980 else if (strcmp ("qTsSTM", packet) == 0)
3982 cmd_qtsstm (packet);
3985 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
3987 cmd_qtstmat (packet);
3990 else if (strcmp ("qTMinFTPILen", packet) == 0)
3992 cmd_qtminftpilen (packet);
4000 #ifndef IN_PROCESS_AGENT
4002 /* Call this when thread TINFO has hit the tracepoint defined by
4003 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
4004 action. This adds a while-stepping collecting state item to the
4005 threads' collecting state list, so that we can keep track of
4006 multiple simultaneous while-stepping actions being collected by the
4007 same thread. This can happen in cases like:
4009 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
4011 ff0003 INSN3 <-- TP2, collect $regs
4012 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
4015 Notice that when instruction INSN5 is reached, the while-stepping
4016 actions of both TP1 and TP3 are still being collected, and that TP2
4017 had been collected meanwhile. The whole range of ff0001-ff0005
4018 should be single-stepped, due to at least TP1's while-stepping
4019 action covering the whole range. */
4022 add_while_stepping_state (struct thread_info *tinfo,
4023 int tp_number, CORE_ADDR tp_address)
4025 struct wstep_state *wstep;
4027 wstep = xmalloc (sizeof (*wstep));
4028 wstep->next = tinfo->while_stepping;
4030 wstep->tp_number = tp_number;
4031 wstep->tp_address = tp_address;
4032 wstep->current_step = 0;
4034 tinfo->while_stepping = wstep;
4037 /* Release the while-stepping collecting state WSTEP. */
4040 release_while_stepping_state (struct wstep_state *wstep)
4045 /* Release all while-stepping collecting states currently associated
4046 with thread TINFO. */
4049 release_while_stepping_state_list (struct thread_info *tinfo)
4051 struct wstep_state *head;
4053 while (tinfo->while_stepping)
4055 head = tinfo->while_stepping;
4056 tinfo->while_stepping = head->next;
4057 release_while_stepping_state (head);
4061 /* If TINFO was handling a 'while-stepping' action, the step has
4062 finished, so collect any step data needed, and check if any more
4063 steps are required. Return true if the thread was indeed
4064 collecting tracepoint data, false otherwise. */
4067 tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
4069 struct tracepoint *tpoint;
4070 struct wstep_state *wstep;
4071 struct wstep_state **wstep_link;
4072 struct trap_tracepoint_ctx ctx;
4074 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4076 if (in_process_agent_loaded ())
4077 upload_fast_traceframes ();
4079 /* Check if we were indeed collecting data for one of more
4080 tracepoints with a 'while-stepping' count. */
4081 if (tinfo->while_stepping == NULL)
4086 /* We're not even tracing anymore. Stop this thread from
4088 release_while_stepping_state_list (tinfo);
4090 /* The thread had stopped due to a single-step request indeed
4091 explained by a tracepoint. */
4095 wstep = tinfo->while_stepping;
4096 wstep_link = &tinfo->while_stepping;
4098 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4099 target_pid_to_str (tinfo->entry.id),
4100 wstep->tp_number, paddress (wstep->tp_address));
4102 ctx.base.type = trap_tracepoint;
4103 ctx.regcache = get_thread_regcache (tinfo, 1);
4105 while (wstep != NULL)
4107 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
4110 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4111 wstep->tp_number, paddress (wstep->tp_address),
4112 target_pid_to_str (tinfo->entry.id));
4115 *wstep_link = wstep->next;
4116 release_while_stepping_state (wstep);
4117 wstep = *wstep_link;
4121 /* We've just finished one step. */
4122 ++wstep->current_step;
4125 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
4126 stop_pc, tpoint, wstep->current_step);
4128 if (wstep->current_step >= tpoint->step_count)
4130 /* The requested numbers of steps have occurred. */
4131 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4132 target_pid_to_str (tinfo->entry.id),
4133 wstep->tp_number, paddress (wstep->tp_address));
4135 /* Unlink the wstep. */
4136 *wstep_link = wstep->next;
4137 release_while_stepping_state (wstep);
4138 wstep = *wstep_link;
4140 /* Only check the hit count now, which ensure that we do all
4141 our stepping before stopping the run. */
4142 if (tpoint->pass_count > 0
4143 && tpoint->hit_count >= tpoint->pass_count
4144 && stopping_tracepoint == NULL)
4145 stopping_tracepoint = tpoint;
4149 /* Keep single-stepping until the requested numbers of steps
4151 wstep_link = &wstep->next;
4152 wstep = *wstep_link;
4155 if (stopping_tracepoint
4156 || trace_buffer_is_full
4157 || expr_eval_result != expr_eval_no_error)
4167 /* Handle any internal tracing control breakpoint hits. That means,
4168 pull traceframes from the IPA to our buffer, and syncing both
4169 tracing agents when the IPA's tracing stops for some reason. */
4172 handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4174 /* Pull in fast tracepoint trace frames from the inferior in-process
4175 agent's buffer into our buffer. */
4177 if (!in_process_agent_loaded ())
4180 upload_fast_traceframes ();
4182 /* Check if the in-process agent had decided we should stop
4184 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4186 int ipa_trace_buffer_is_full;
4187 CORE_ADDR ipa_stopping_tracepoint;
4188 int ipa_expr_eval_result;
4189 CORE_ADDR ipa_error_tracepoint;
4191 trace_debug ("lib stopped at stop_tracing");
4193 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4194 &ipa_trace_buffer_is_full);
4196 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4197 &ipa_stopping_tracepoint);
4198 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4200 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4201 &ipa_error_tracepoint);
4202 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4204 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4205 &ipa_expr_eval_result);
4206 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4208 trace_debug ("lib: trace_buffer_is_full: %d, "
4209 "stopping_tracepoint: %s, "
4210 "ipa_expr_eval_result: %d, "
4211 "error_tracepoint: %s, ",
4212 ipa_trace_buffer_is_full,
4213 paddress (ipa_stopping_tracepoint),
4214 ipa_expr_eval_result,
4215 paddress (ipa_error_tracepoint));
4219 if (ipa_trace_buffer_is_full)
4220 trace_debug ("lib stopped due to full buffer.");
4221 if (ipa_stopping_tracepoint)
4222 trace_debug ("lib stopped due to tpoint");
4223 if (ipa_stopping_tracepoint)
4224 trace_debug ("lib stopped due to error");
4227 if (ipa_stopping_tracepoint != 0)
4230 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4232 else if (ipa_expr_eval_result != expr_eval_no_error)
4234 expr_eval_result = ipa_expr_eval_result;
4236 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4241 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4243 trace_debug ("lib stopped at flush_trace_buffer");
4250 /* Return true if TINFO just hit a tracepoint. Collect data if
4254 tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4256 struct tracepoint *tpoint;
4258 struct trap_tracepoint_ctx ctx;
4260 /* Not tracing, don't handle. */
4264 ctx.base.type = trap_tracepoint;
4265 ctx.regcache = get_thread_regcache (tinfo, 1);
4267 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4269 /* Note that we collect fast tracepoints here as well. We'll
4270 step over the fast tracepoint jump later, which avoids the
4271 double collect. However, we don't collect for static
4272 tracepoints here, because UST markers are compiled in program,
4273 and probes will be executed in program. So static tracepoints
4274 are collected there. */
4275 if (tpoint->enabled && stop_pc == tpoint->address
4276 && tpoint->type != static_tracepoint)
4278 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4279 target_pid_to_str (tinfo->entry.id),
4280 tpoint->number, paddress (tpoint->address));
4282 /* Test the condition if present, and collect if true. */
4284 || (condition_true_at_tracepoint
4285 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4286 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4289 if (stopping_tracepoint
4290 || trace_buffer_is_full
4291 || expr_eval_result != expr_eval_no_error)
4295 /* If the tracepoint had a 'while-stepping' action, then set
4296 the thread to collect this tracepoint on the following
4298 else if (tpoint->step_count > 0)
4300 add_while_stepping_state (tinfo,
4301 tpoint->number, tpoint->address);
4313 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4314 struct ust_marker_data;
4315 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4317 struct tracepoint *tpoint,
4318 struct traceframe *tframe);
4321 /* Create a trace frame for the hit of the given tracepoint in the
4325 collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4326 struct tracepoint *tpoint)
4328 struct traceframe *tframe;
4331 /* Only count it as a hit when we actually collect data. */
4332 tpoint->hit_count++;
4334 /* If we've exceeded a defined pass count, record the event for
4335 later, and finish the collection for this hit. This test is only
4336 for nonstepping tracepoints, stepping tracepoints test at the end
4337 of their while-stepping loop. */
4338 if (tpoint->pass_count > 0
4339 && tpoint->hit_count >= tpoint->pass_count
4340 && tpoint->step_count == 0
4341 && stopping_tracepoint == NULL)
4342 stopping_tracepoint = tpoint;
4344 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4345 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4347 tframe = add_traceframe (tpoint);
4351 for (acti = 0; acti < tpoint->numactions; ++acti)
4353 #ifndef IN_PROCESS_AGENT
4354 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4355 tpoint->number, paddress (tpoint->address),
4356 tpoint->actions_str[acti]);
4359 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4360 tpoint->actions[acti]);
4363 finish_traceframe (tframe);
4366 if (tframe == NULL && tracing)
4367 trace_buffer_is_full = 1;
4370 #ifndef IN_PROCESS_AGENT
4373 collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4375 struct tracepoint *tpoint, int current_step)
4377 struct traceframe *tframe;
4380 trace_debug ("Making new step traceframe for "
4381 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4382 tpoint->number, paddress (tpoint->address),
4383 current_step, tpoint->step_count,
4386 tframe = add_traceframe (tpoint);
4390 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4392 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4393 tpoint->number, paddress (tpoint->address),
4394 tpoint->step_actions_str[acti]);
4396 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4397 tpoint->step_actions[acti]);
4400 finish_traceframe (tframe);
4403 if (tframe == NULL && tracing)
4404 trace_buffer_is_full = 1;
4409 static struct regcache *
4410 get_context_regcache (struct tracepoint_hit_ctx *ctx)
4412 struct regcache *regcache = NULL;
4414 #ifdef IN_PROCESS_AGENT
4415 if (ctx->type == fast_tracepoint)
4417 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4418 if (!fctx->regcache_initted)
4420 fctx->regcache_initted = 1;
4421 init_register_cache (&fctx->regcache, fctx->regspace);
4422 supply_regblock (&fctx->regcache, NULL);
4423 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4425 regcache = &fctx->regcache;
4428 if (ctx->type == static_tracepoint)
4430 struct static_tracepoint_ctx *sctx
4431 = (struct static_tracepoint_ctx *) ctx;
4433 if (!sctx->regcache_initted)
4435 sctx->regcache_initted = 1;
4436 init_register_cache (&sctx->regcache, sctx->regspace);
4437 supply_regblock (&sctx->regcache, NULL);
4438 /* Pass down the tracepoint address, because REGS doesn't
4439 include the PC, but we know what it must have been. */
4440 supply_static_tracepoint_registers (&sctx->regcache,
4441 (const unsigned char *)
4443 sctx->tpoint->address);
4445 regcache = &sctx->regcache;
4449 if (ctx->type == trap_tracepoint)
4451 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4452 regcache = tctx->regcache;
4456 gdb_assert (regcache != NULL);
4462 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4464 struct tracepoint *tpoint,
4465 struct traceframe *tframe,
4466 struct tracepoint_action *taction)
4468 enum eval_result_type err;
4470 switch (taction->type)
4474 struct collect_memory_action *maction;
4476 maction = (struct collect_memory_action *) taction;
4478 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4479 pulongest (maction->len),
4480 paddress (maction->addr), maction->basereg);
4481 /* (should use basereg) */
4482 agent_mem_read (tframe, NULL,
4483 (CORE_ADDR) maction->addr, maction->len);
4488 unsigned char *regspace;
4489 struct regcache tregcache;
4490 struct regcache *context_regcache;
4493 trace_debug ("Want to collect registers");
4495 /* Collect all registers for now. */
4496 regspace = add_traceframe_block (tframe,
4497 1 + register_cache_size ());
4498 if (regspace == NULL)
4500 trace_debug ("Trace buffer block allocation failed, skipping");
4503 /* Identify a register block. */
4506 context_regcache = get_context_regcache (ctx);
4508 /* Wrap the regblock in a register cache (in the stack, we
4509 don't want to malloc here). */
4510 init_register_cache (&tregcache, regspace + 1);
4512 /* Copy the register data to the regblock. */
4513 regcache_cpy (&tregcache, context_regcache);
4515 #ifndef IN_PROCESS_AGENT
4516 /* On some platforms, trap-based tracepoints will have the PC
4517 pointing to the next instruction after the trap, but we
4518 don't want the user or GDB trying to guess whether the
4519 saved PC needs adjusting; so always record the adjusted
4520 stop_pc. Note that we can't use tpoint->address instead,
4521 since it will be wrong for while-stepping actions. This
4522 adjustment is a nop for fast tracepoints collected from the
4523 in-process lib (but not if GDBserver is collecting one
4524 preemptively), since the PC had already been adjusted to
4525 contain the tracepoint's address by the jump pad. */
4526 trace_debug ("Storing stop pc (0x%s) in regblock",
4527 paddress (tpoint->address));
4529 /* This changes the regblock, not the thread's
4531 regcache_write_pc (&tregcache, stop_pc);
4537 struct eval_expr_action *eaction;
4539 eaction = (struct eval_expr_action *) taction;
4541 trace_debug ("Want to evaluate expression");
4543 err = eval_agent_expr (ctx, tframe, eaction->expr, NULL);
4545 if (err != expr_eval_no_error)
4547 record_tracepoint_error (tpoint, "action expression", err);
4554 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4555 trace_debug ("Want to collect static trace data");
4556 collect_ust_data_at_tracepoint (ctx, stop_pc,
4559 trace_debug ("warning: collecting static trace data, "
4560 "but static tracepoints are not supported");
4565 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4571 condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4572 struct tracepoint *tpoint)
4575 enum eval_result_type err;
4577 /* Presently, gdbserver doesn't run compiled conditions, only the
4578 IPA does. If the program stops at a fast tracepoint's address
4579 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4580 gdbserver preemptively collect the fast tracepoint. Later, on
4581 resume, gdbserver steps over the fast tracepoint like it steps
4582 over breakpoints, so that the IPA doesn't see that fast
4583 tracepoint. This avoids double collects of fast tracepoints in
4584 that stopping scenario. Having gdbserver itself handle the fast
4585 tracepoint gives the user a consistent view of when fast or trap
4586 tracepoints are collected, compared to an alternative where only
4587 trap tracepoints are collected on stop, and fast tracepoints on
4588 resume. When a fast tracepoint is being processed by gdbserver,
4589 it is always the non-compiled condition expression that is
4591 #ifdef IN_PROCESS_AGENT
4592 if (tpoint->compiled_cond)
4593 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4596 err = eval_agent_expr (ctx, NULL, tpoint->cond, &value);
4598 if (err != expr_eval_no_error)
4600 record_tracepoint_error (tpoint, "condition", err);
4601 /* The error case must return false. */
4605 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4606 tpoint->number, paddress (tpoint->address),
4608 return (value ? 1 : 0);
4611 #ifndef IN_PROCESS_AGENT
4613 /* The packet form of an agent expression consists of an 'X', number
4614 of bytes in expression, a comma, and then the bytes. */
4616 static struct agent_expr *
4617 parse_agent_expr (char **actparm)
4619 char *act = *actparm;
4621 struct agent_expr *aexpr;
4623 ++act; /* skip the X */
4624 act = unpack_varlen_hex (act, &xlen);
4625 ++act; /* skip a comma */
4626 aexpr = xmalloc (sizeof (struct agent_expr));
4627 aexpr->length = xlen;
4628 aexpr->bytes = xmalloc (xlen);
4629 convert_ascii_to_int (act, aexpr->bytes, xlen);
4630 *actparm = act + (xlen * 2);
4634 /* Convert the bytes of an agent expression back into hex digits, so
4635 they can be printed or uploaded. This allocates the buffer,
4636 callers should free when they are done with it. */
4639 unparse_agent_expr (struct agent_expr *aexpr)
4643 rslt = xmalloc (2 * aexpr->length + 1);
4644 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
4650 /* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4653 gdb_agent_op_name (int op)
4655 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
4657 return gdb_agent_op_names[op];
4660 /* The agent expression evaluator, as specified by the GDB docs. It
4661 returns 0 if everything went OK, and a nonzero error code
4664 static enum eval_result_type
4665 eval_agent_expr (struct tracepoint_hit_ctx *ctx,
4666 struct traceframe *tframe,
4667 struct agent_expr *aexpr,
4671 #define STACK_MAX 100
4672 ULONGEST stack[STACK_MAX], top;
4677 /* This union is a convenient way to convert representations. For
4678 now, assume a standard architecture where the hardware integer
4679 types have 8, 16, 32, 64 bit types. A more robust solution would
4680 be to import stdint.h from gnulib. */
4685 unsigned char bytes[1];
4690 unsigned char bytes[2];
4695 unsigned char bytes[4];
4700 unsigned char bytes[8];
4705 if (aexpr->length == 0)
4707 trace_debug ("empty agent expression");
4708 return expr_eval_empty_expression;
4711 /* Cache the stack top in its own variable. Much of the time we can
4712 operate on this variable, rather than dinking with the stack. It
4713 needs to be copied to the stack when sp changes. */
4718 op = aexpr->bytes[pc++];
4720 trace_debug ("About to interpret byte 0x%x", op);
4724 case gdb_agent_op_add:
4728 case gdb_agent_op_sub:
4729 top = stack[--sp] - top;
4732 case gdb_agent_op_mul:
4736 case gdb_agent_op_div_signed:
4739 trace_debug ("Attempted to divide by zero");
4740 return expr_eval_divide_by_zero;
4742 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
4745 case gdb_agent_op_div_unsigned:
4748 trace_debug ("Attempted to divide by zero");
4749 return expr_eval_divide_by_zero;
4751 top = stack[--sp] / top;
4754 case gdb_agent_op_rem_signed:
4757 trace_debug ("Attempted to divide by zero");
4758 return expr_eval_divide_by_zero;
4760 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
4763 case gdb_agent_op_rem_unsigned:
4766 trace_debug ("Attempted to divide by zero");
4767 return expr_eval_divide_by_zero;
4769 top = stack[--sp] % top;
4772 case gdb_agent_op_lsh:
4773 top = stack[--sp] << top;
4776 case gdb_agent_op_rsh_signed:
4777 top = ((LONGEST) stack[--sp]) >> top;
4780 case gdb_agent_op_rsh_unsigned:
4781 top = stack[--sp] >> top;
4784 case gdb_agent_op_trace:
4785 agent_mem_read (tframe,
4786 NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top);
4791 case gdb_agent_op_trace_quick:
4792 arg = aexpr->bytes[pc++];
4793 agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg);
4796 case gdb_agent_op_log_not:
4800 case gdb_agent_op_bit_and:
4804 case gdb_agent_op_bit_or:
4808 case gdb_agent_op_bit_xor:
4812 case gdb_agent_op_bit_not:
4816 case gdb_agent_op_equal:
4817 top = (stack[--sp] == top);
4820 case gdb_agent_op_less_signed:
4821 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
4824 case gdb_agent_op_less_unsigned:
4825 top = (stack[--sp] < top);
4828 case gdb_agent_op_ext:
4829 arg = aexpr->bytes[pc++];
4830 if (arg < (sizeof (LONGEST) * 8))
4832 LONGEST mask = 1 << (arg - 1);
4833 top &= ((LONGEST) 1 << arg) - 1;
4834 top = (top ^ mask) - mask;
4838 case gdb_agent_op_ref8:
4839 agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1);
4843 case gdb_agent_op_ref16:
4844 agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2);
4848 case gdb_agent_op_ref32:
4849 agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4);
4853 case gdb_agent_op_ref64:
4854 agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8);
4858 case gdb_agent_op_if_goto:
4860 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4867 case gdb_agent_op_goto:
4868 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4871 case gdb_agent_op_const8:
4872 /* Flush the cached stack top. */
4874 top = aexpr->bytes[pc++];
4877 case gdb_agent_op_const16:
4878 /* Flush the cached stack top. */
4880 top = aexpr->bytes[pc++];
4881 top = (top << 8) + aexpr->bytes[pc++];
4884 case gdb_agent_op_const32:
4885 /* Flush the cached stack top. */
4887 top = aexpr->bytes[pc++];
4888 top = (top << 8) + aexpr->bytes[pc++];
4889 top = (top << 8) + aexpr->bytes[pc++];
4890 top = (top << 8) + aexpr->bytes[pc++];
4893 case gdb_agent_op_const64:
4894 /* Flush the cached stack top. */
4896 top = aexpr->bytes[pc++];
4897 top = (top << 8) + aexpr->bytes[pc++];
4898 top = (top << 8) + aexpr->bytes[pc++];
4899 top = (top << 8) + aexpr->bytes[pc++];
4900 top = (top << 8) + aexpr->bytes[pc++];
4901 top = (top << 8) + aexpr->bytes[pc++];
4902 top = (top << 8) + aexpr->bytes[pc++];
4903 top = (top << 8) + aexpr->bytes[pc++];
4906 case gdb_agent_op_reg:
4907 /* Flush the cached stack top. */
4909 arg = aexpr->bytes[pc++];
4910 arg = (arg << 8) + aexpr->bytes[pc++];
4913 struct regcache *regcache;
4915 regcache = get_context_regcache (ctx);
4917 switch (register_size (regnum))
4920 collect_register (regcache, regnum, cnv.u64.bytes);
4924 collect_register (regcache, regnum, cnv.u32.bytes);
4928 collect_register (regcache, regnum, cnv.u16.bytes);
4932 collect_register (regcache, regnum, cnv.u8.bytes);
4936 internal_error (__FILE__, __LINE__,
4937 "unhandled register size");
4942 case gdb_agent_op_end:
4943 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4944 sp, pulongest (top));
4949 /* This should be an error */
4950 trace_debug ("Stack is empty, nothing to return");
4951 return expr_eval_empty_stack;
4955 return expr_eval_no_error;
4957 case gdb_agent_op_dup:
4961 case gdb_agent_op_pop:
4966 case gdb_agent_op_pick:
4967 arg = aexpr->bytes[pc++];
4969 top = stack[sp - arg];
4973 case gdb_agent_op_rot:
4975 ULONGEST tem = stack[sp - 1];
4977 stack[sp - 1] = stack[sp - 2];
4978 stack[sp - 2] = top;
4983 case gdb_agent_op_zero_ext:
4984 arg = aexpr->bytes[pc++];
4985 if (arg < (sizeof (LONGEST) * 8))
4986 top &= ((LONGEST) 1 << arg) - 1;
4989 case gdb_agent_op_swap:
4990 /* Interchange top two stack elements, making sure top gets
4991 copied back onto stack. */
4993 top = stack[sp - 1];
4994 stack[sp - 1] = stack[sp];
4997 case gdb_agent_op_getv:
4998 /* Flush the cached stack top. */
5000 arg = aexpr->bytes[pc++];
5001 arg = (arg << 8) + aexpr->bytes[pc++];
5002 top = get_trace_state_variable_value (arg);
5005 case gdb_agent_op_setv:
5006 arg = aexpr->bytes[pc++];
5007 arg = (arg << 8) + aexpr->bytes[pc++];
5008 set_trace_state_variable_value (arg, top);
5009 /* Note that we leave the value on the stack, for the
5010 benefit of later/enclosing expressions. */
5013 case gdb_agent_op_tracev:
5014 arg = aexpr->bytes[pc++];
5015 arg = (arg << 8) + aexpr->bytes[pc++];
5016 agent_tsv_read (tframe, arg);
5019 case gdb_agent_op_tracenz:
5020 agent_mem_read_string (tframe, NULL, (CORE_ADDR) stack[--sp],
5026 /* GDB never (currently) generates any of these ops. */
5027 case gdb_agent_op_float:
5028 case gdb_agent_op_ref_float:
5029 case gdb_agent_op_ref_double:
5030 case gdb_agent_op_ref_long_double:
5031 case gdb_agent_op_l_to_d:
5032 case gdb_agent_op_d_to_l:
5033 case gdb_agent_op_trace16:
5034 trace_debug ("Agent expression op 0x%x valid, but not handled",
5036 /* If ever GDB generates any of these, we don't have the
5037 option of ignoring. */
5041 trace_debug ("Agent expression op 0x%x not recognized", op);
5042 /* Don't struggle on, things will just get worse. */
5043 return expr_eval_unrecognized_opcode;
5046 /* Check for stack badness. */
5047 if (sp >= (STACK_MAX - 1))
5049 trace_debug ("Expression stack overflow");
5050 return expr_eval_stack_overflow;
5055 trace_debug ("Expression stack underflow");
5056 return expr_eval_stack_underflow;
5059 trace_debug ("Op %s -> sp=%d, top=0x%s",
5060 gdb_agent_op_name (op), sp, pulongest (top));
5064 /* Do memory copies for bytecodes. */
5065 /* Do the recording of memory blocks for actions and bytecodes. */
5068 agent_mem_read (struct traceframe *tframe,
5069 unsigned char *to, CORE_ADDR from, ULONGEST len)
5071 unsigned char *mspace;
5072 ULONGEST remaining = len;
5073 unsigned short blocklen;
5075 /* If a 'to' buffer is specified, use it. */
5078 read_inferior_memory (from, to, len);
5082 /* Otherwise, create a new memory block in the trace buffer. */
5083 while (remaining > 0)
5087 blocklen = (remaining > 65535 ? 65535 : remaining);
5088 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
5089 mspace = add_traceframe_block (tframe, sp);
5092 /* Identify block as a memory block. */
5095 /* Record address and size. */
5096 memcpy (mspace, &from, sizeof (from));
5097 mspace += sizeof (from);
5098 memcpy (mspace, &blocklen, sizeof (blocklen));
5099 mspace += sizeof (blocklen);
5100 /* Record the memory block proper. */
5101 read_inferior_memory (from, mspace, blocklen);
5102 trace_debug ("%d bytes recorded", blocklen);
5103 remaining -= blocklen;
5110 agent_mem_read_string (struct traceframe *tframe,
5111 unsigned char *to, CORE_ADDR from, ULONGEST len)
5113 unsigned char *buf, *mspace;
5114 ULONGEST remaining = len;
5115 unsigned short blocklen, i;
5117 /* To save a bit of space, block lengths are 16-bit, so break large
5118 requests into multiple blocks. Bordering on overkill for strings,
5119 but it could happen that someone specifies a large max length. */
5120 while (remaining > 0)
5124 blocklen = (remaining > 65535 ? 65535 : remaining);
5125 /* We want working space to accumulate nonzero bytes, since
5126 traceframes must have a predecided size (otherwise it gets
5127 harder to wrap correctly for the circular case, etc). */
5128 buf = (unsigned char *) xmalloc (blocklen + 1);
5129 for (i = 0; i < blocklen; ++i)
5131 /* Read the string one byte at a time, in case the string is
5132 at the end of a valid memory area - we don't want a
5133 correctly-terminated string to engender segvio
5135 read_inferior_memory (from + i, buf + i, 1);
5140 /* Make sure outer loop stops now too. */
5141 remaining = blocklen;
5145 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
5146 mspace = add_traceframe_block (tframe, sp);
5152 /* Identify block as a memory block. */
5155 /* Record address and size. */
5156 memcpy ((void *) mspace, (void *) &from, sizeof (from));
5157 mspace += sizeof (from);
5158 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
5159 mspace += sizeof (blocklen);
5160 /* Copy the string contents. */
5161 memcpy ((void *) mspace, (void *) buf, blocklen);
5162 remaining -= blocklen;
5169 /* Record the value of a trace state variable. */
5172 agent_tsv_read (struct traceframe *tframe, int n)
5174 unsigned char *vspace;
5177 vspace = add_traceframe_block (tframe,
5178 1 + sizeof (n) + sizeof (LONGEST));
5181 /* Identify block as a variable. */
5183 /* Record variable's number and value. */
5184 memcpy (vspace + 1, &n, sizeof (n));
5185 val = get_trace_state_variable_value (n);
5186 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
5187 trace_debug ("Variable %d recorded", n);
5191 #ifndef IN_PROCESS_AGENT
5193 /* Callback for traceframe_walk_blocks, used to find a given block
5194 type in a traceframe. */
5197 match_blocktype (char blocktype, unsigned char *dataptr, void *data)
5199 char *wantedp = data;
5201 if (*wantedp == blocktype)
5207 /* Walk over all traceframe blocks of the traceframe buffer starting
5208 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5209 block found, passing in DATA unmodified. If CALLBACK returns true,
5210 this returns a pointer to where the block is found. Returns NULL
5211 if no callback call returned true, indicating that all blocks have
5214 static unsigned char *
5215 traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
5217 int (*callback) (char blocktype,
5218 unsigned char *dataptr,
5222 unsigned char *dataptr;
5226 trace_debug ("traceframe %d has no data", tfnum);
5230 /* Iterate through a traceframe's blocks, looking for a block of the
5232 for (dataptr = database;
5233 dataptr < database + datasize;
5237 unsigned short mlen;
5239 if (dataptr == trace_buffer_wrap)
5241 /* Adjust to reflect wrapping part of the frame around to
5243 datasize = dataptr - database;
5244 dataptr = database = trace_buffer_lo;
5247 blocktype = *dataptr++;
5249 if ((*callback) (blocktype, dataptr, data))
5255 /* Skip over the registers block. */
5256 dataptr += register_cache_size ();
5259 /* Skip over the memory block. */
5260 dataptr += sizeof (CORE_ADDR);
5261 memcpy (&mlen, dataptr, sizeof (mlen));
5262 dataptr += (sizeof (mlen) + mlen);
5265 /* Skip over the TSV block. */
5266 dataptr += (sizeof (int) + sizeof (LONGEST));
5269 /* Skip over the static trace data block. */
5270 memcpy (&mlen, dataptr, sizeof (mlen));
5271 dataptr += (sizeof (mlen) + mlen);
5274 trace_debug ("traceframe %d has unknown block type 0x%x",
5283 /* Look for the block of type TYPE_WANTED in the trameframe starting
5284 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5287 static unsigned char *
5288 traceframe_find_block_type (unsigned char *database, unsigned int datasize,
5289 int tfnum, char type_wanted)
5291 return traceframe_walk_blocks (database, datasize, tfnum,
5292 match_blocktype, &type_wanted);
5295 static unsigned char *
5296 traceframe_find_regblock (struct traceframe *tframe, int tfnum)
5298 unsigned char *regblock;
5300 regblock = traceframe_find_block_type (tframe->data,
5304 if (regblock == NULL)
5305 trace_debug ("traceframe %d has no register data", tfnum);
5310 /* Get registers from a traceframe. */
5313 fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
5315 unsigned char *dataptr;
5316 struct tracepoint *tpoint;
5317 struct traceframe *tframe;
5319 tframe = find_traceframe (tfnum);
5323 trace_debug ("traceframe %d not found", tfnum);
5327 dataptr = traceframe_find_regblock (tframe, tfnum);
5328 if (dataptr == NULL)
5330 /* Mark registers unavailable. */
5331 supply_regblock (regcache, NULL);
5333 /* We can generally guess at a PC, although this will be
5334 misleading for while-stepping frames and multi-location
5336 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
5338 regcache_write_pc (regcache, tpoint->address);
5341 supply_regblock (regcache, dataptr);
5347 traceframe_get_pc (struct traceframe *tframe)
5349 struct regcache regcache;
5350 unsigned char *dataptr;
5352 dataptr = traceframe_find_regblock (tframe, -1);
5353 if (dataptr == NULL)
5356 init_register_cache (®cache, dataptr);
5357 return regcache_read_pc (®cache);
5360 /* Read a requested block of memory from a trace frame. */
5363 traceframe_read_mem (int tfnum, CORE_ADDR addr,
5364 unsigned char *buf, ULONGEST length,
5367 struct traceframe *tframe;
5368 unsigned char *database, *dataptr;
5369 unsigned int datasize;
5371 unsigned short mlen;
5373 trace_debug ("traceframe_read_mem");
5375 tframe = find_traceframe (tfnum);
5379 trace_debug ("traceframe %d not found", tfnum);
5383 datasize = tframe->data_size;
5384 database = dataptr = &tframe->data[0];
5386 /* Iterate through a traceframe's blocks, looking for memory. */
5387 while ((dataptr = traceframe_find_block_type (dataptr,
5389 - (dataptr - database),
5390 tfnum, 'M')) != NULL)
5392 memcpy (&maddr, dataptr, sizeof (maddr));
5393 dataptr += sizeof (maddr);
5394 memcpy (&mlen, dataptr, sizeof (mlen));
5395 dataptr += sizeof (mlen);
5396 trace_debug ("traceframe %d has %d bytes at %s",
5397 tfnum, mlen, paddress (maddr));
5399 /* If the block includes the first part of the desired range,
5400 return as much it has; GDB will re-request the remainder,
5401 which might be in a different block of this trace frame. */
5402 if (maddr <= addr && addr < (maddr + mlen))
5404 ULONGEST amt = (maddr + mlen) - addr;
5408 memcpy (buf, dataptr + (addr - maddr), amt);
5413 /* Skip over this block. */
5417 trace_debug ("traceframe %d has no memory data for the desired region",
5425 traceframe_read_tsv (int tsvnum, LONGEST *val)
5428 struct traceframe *tframe;
5429 unsigned char *database, *dataptr;
5430 unsigned int datasize;
5433 trace_debug ("traceframe_read_tsv");
5435 tfnum = current_traceframe;
5439 trace_debug ("no current traceframe");
5443 tframe = find_traceframe (tfnum);
5447 trace_debug ("traceframe %d not found", tfnum);
5451 datasize = tframe->data_size;
5452 database = dataptr = &tframe->data[0];
5454 /* Iterate through a traceframe's blocks, looking for the tsv. */
5455 while ((dataptr = traceframe_find_block_type (dataptr,
5457 - (dataptr - database),
5458 tfnum, 'V')) != NULL)
5460 memcpy (&vnum, dataptr, sizeof (vnum));
5461 dataptr += sizeof (vnum);
5463 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
5465 /* Check that this is the variable we want. */
5468 memcpy (val, dataptr, sizeof (*val));
5472 /* Skip over this block. */
5473 dataptr += sizeof (LONGEST);
5476 trace_debug ("traceframe %d has no data for variable %d",
5481 /* Read a requested block of static tracepoint data from a trace
5485 traceframe_read_sdata (int tfnum, ULONGEST offset,
5486 unsigned char *buf, ULONGEST length,
5489 struct traceframe *tframe;
5490 unsigned char *database, *dataptr;
5491 unsigned int datasize;
5492 unsigned short mlen;
5494 trace_debug ("traceframe_read_sdata");
5496 tframe = find_traceframe (tfnum);
5500 trace_debug ("traceframe %d not found", tfnum);
5504 datasize = tframe->data_size;
5505 database = &tframe->data[0];
5507 /* Iterate through a traceframe's blocks, looking for static
5509 dataptr = traceframe_find_block_type (database, datasize,
5511 if (dataptr != NULL)
5513 memcpy (&mlen, dataptr, sizeof (mlen));
5514 dataptr += sizeof (mlen);
5517 if (offset + length > mlen)
5518 length = mlen - offset;
5520 memcpy (buf, dataptr, length);
5528 trace_debug ("traceframe %d has no static trace data", tfnum);
5534 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5535 object. DATA is pointer to a struct buffer holding the
5536 traceframe-info object being built. */
5539 build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5541 struct buffer *buffer = data;
5547 unsigned short mlen;
5550 memcpy (&maddr, dataptr, sizeof (maddr));
5551 dataptr += sizeof (maddr);
5552 memcpy (&mlen, dataptr, sizeof (mlen));
5553 dataptr += sizeof (mlen);
5554 buffer_xml_printf (buffer,
5555 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5556 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5566 warning ("Unhandled trace block type (%d) '%c ' "
5567 "while building trace frame info.",
5568 blocktype, blocktype);
5575 /* Build a traceframe-info object for traceframe number TFNUM into
5579 traceframe_read_info (int tfnum, struct buffer *buffer)
5581 struct traceframe *tframe;
5583 trace_debug ("traceframe_read_info");
5585 tframe = find_traceframe (tfnum);
5589 trace_debug ("traceframe %d not found", tfnum);
5593 buffer_grow_str (buffer, "<traceframe-info>\n");
5594 traceframe_walk_blocks (tframe->data, tframe->data_size,
5595 tfnum, build_traceframe_info_xml, buffer);
5596 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5600 /* Return the first fast tracepoint whose jump pad contains PC. */
5602 static struct tracepoint *
5603 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5605 struct tracepoint *tpoint;
5607 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5608 if (tpoint->type == fast_tracepoint)
5609 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5615 /* Return the first fast tracepoint whose trampoline contains PC. */
5617 static struct tracepoint *
5618 fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5620 struct tracepoint *tpoint;
5622 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5624 if (tpoint->type == fast_tracepoint
5625 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5632 /* Return GDBserver's tracepoint that matches the IP Agent's
5633 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5636 static struct tracepoint *
5637 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5639 struct tracepoint *tpoint;
5641 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5642 if (tpoint->type == fast_tracepoint)
5643 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5651 /* The type of the object that is used to synchronize fast tracepoint
5654 typedef struct collecting_t
5656 /* The fast tracepoint number currently collecting. */
5659 /* A number that GDBserver can use to identify the thread that is
5660 presently holding the collect lock. This need not (and usually
5661 is not) the thread id, as getting the current thread ID usually
5662 requires a system call, which we want to avoid like the plague.
5663 Usually this is thread's TCB, found in the TLS (pseudo-)
5664 register, which is readable with a single insn on several
5666 uintptr_t thread_area;
5669 #ifndef IN_PROCESS_AGENT
5672 force_unlock_trace_buffer (void)
5674 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5677 /* Check if the thread identified by THREAD_AREA which is stopped at
5678 STOP_PC, is presently locking the fast tracepoint collection, and
5679 if so, gather some status of said collection. Returns 0 if the
5680 thread isn't collecting or in the jump pad at all. 1, if in the
5681 jump pad (or within gdb_collect) and hasn't executed the adjusted
5682 original insn yet (can set a breakpoint there and run to it). 2,
5683 if presently executing the adjusted original insn --- in which
5684 case, if we want to move the thread out of the jump pad, we need to
5685 single-step it until this function returns 0. */
5688 fast_tracepoint_collecting (CORE_ADDR thread_area,
5690 struct fast_tpoint_collect_status *status)
5692 CORE_ADDR ipa_collecting;
5693 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
5694 CORE_ADDR ipa_gdb_trampoline_buffer;
5695 CORE_ADDR ipa_gdb_trampoline_buffer_end;
5696 struct tracepoint *tpoint;
5697 int needs_breakpoint;
5699 /* The thread THREAD_AREA is either:
5701 0. not collecting at all, not within the jump pad, or within
5702 gdb_collect or one of its callees.
5704 1. in the jump pad and haven't reached gdb_collect
5706 2. within gdb_collect (out of the jump pad) (collect is set)
5708 3. we're in the jump pad, after gdb_collect having returned,
5709 possibly executing the adjusted insns.
5711 For cases 1 and 3, `collecting' may or not be set. The jump pad
5712 doesn't have any complicated jump logic, so we can tell if the
5713 thread is executing the adjust original insn or not by just
5714 matching STOP_PC with known jump pad addresses. If we it isn't
5715 yet executing the original insn, set a breakpoint there, and let
5716 the thread run to it, so to quickly step over a possible (many
5717 insns) gdb_collect call. Otherwise, or when the breakpoint is
5718 hit, only a few (small number of) insns are left to be executed
5719 in the jump pad. Single-step the thread until it leaves the
5724 needs_breakpoint = 0;
5725 trace_debug ("fast_tracepoint_collecting");
5727 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5728 &ipa_gdb_jump_pad_buffer))
5729 fatal ("error extracting `gdb_jump_pad_buffer'");
5730 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5731 &ipa_gdb_jump_pad_buffer_end))
5732 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5734 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5735 &ipa_gdb_trampoline_buffer))
5736 fatal ("error extracting `gdb_trampoline_buffer'");
5737 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5738 &ipa_gdb_trampoline_buffer_end))
5739 fatal ("error extracting `gdb_trampoline_buffer_end'");
5741 if (ipa_gdb_jump_pad_buffer <= stop_pc
5742 && stop_pc < ipa_gdb_jump_pad_buffer_end)
5744 /* We can tell which tracepoint(s) the thread is collecting by
5745 matching the jump pad address back to the tracepoint. */
5746 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5749 warning ("in jump pad, but no matching tpoint?");
5754 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5756 tpoint->number, paddress (tpoint->address),
5757 paddress (tpoint->jump_pad),
5758 paddress (tpoint->jump_pad_end),
5759 paddress (tpoint->adjusted_insn_addr),
5760 paddress (tpoint->adjusted_insn_addr_end));
5763 /* Definitely in the jump pad. May or may not need
5764 fast-exit-jump-pad breakpoint. */
5765 if (tpoint->jump_pad <= stop_pc
5766 && stop_pc < tpoint->adjusted_insn_addr)
5767 needs_breakpoint = 1;
5769 else if (ipa_gdb_trampoline_buffer <= stop_pc
5770 && stop_pc < ipa_gdb_trampoline_buffer_end)
5772 /* We can tell which tracepoint(s) the thread is collecting by
5773 matching the trampoline address back to the tracepoint. */
5774 tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5777 warning ("in trampoline, but no matching tpoint?");
5782 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5783 tpoint->number, paddress (tpoint->address),
5784 paddress (tpoint->trampoline),
5785 paddress (tpoint->trampoline_end));
5788 /* Have not reached jump pad yet, but treat the trampoline as a
5789 part of the jump pad that is before the adjusted original
5791 needs_breakpoint = 1;
5795 collecting_t ipa_collecting_obj;
5797 /* If `collecting' is set/locked, then the THREAD_AREA thread
5798 may or not be the one holding the lock. We have to read the
5799 lock to find out. */
5801 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5804 trace_debug ("fast_tracepoint_collecting:"
5805 " failed reading 'collecting' in the inferior");
5809 if (!ipa_collecting)
5811 trace_debug ("fast_tracepoint_collecting: not collecting"
5812 " (and nobody is).");
5816 /* Some thread is collecting. Check which. */
5817 if (read_inferior_memory (ipa_collecting,
5818 (unsigned char *) &ipa_collecting_obj,
5819 sizeof (ipa_collecting_obj)) != 0)
5822 if (ipa_collecting_obj.thread_area != thread_area)
5824 trace_debug ("fast_tracepoint_collecting: not collecting "
5825 "(another thread is)");
5830 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5833 warning ("fast_tracepoint_collecting: collecting, "
5834 "but tpoint %s not found?",
5835 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5839 /* The thread is within `gdb_collect', skip over the rest of
5840 fast tracepoint collection quickly using a breakpoint. */
5841 needs_breakpoint = 1;
5844 /* The caller wants a bit of status detail. */
5847 status->tpoint_num = tpoint->number;
5848 status->tpoint_addr = tpoint->address;
5849 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5850 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5853 if (needs_breakpoint)
5855 /* Hasn't executed the original instruction yet. Set breakpoint
5856 there, and wait till it's hit, then single-step until exiting
5860 fast_tracepoint_collecting, returning continue-until-break at %s",
5861 paddress (tpoint->adjusted_insn_addr));
5863 return 1; /* continue */
5867 /* Just single-step until exiting the jump pad. */
5869 trace_debug ("fast_tracepoint_collecting, returning "
5870 "need-single-step (%s-%s)",
5871 paddress (tpoint->adjusted_insn_addr),
5872 paddress (tpoint->adjusted_insn_addr_end));
5874 return 2; /* single-step */
5880 #ifdef IN_PROCESS_AGENT
5882 /* The global fast tracepoint collect lock. Points to a collecting_t
5883 object built on the stack by the jump pad, if presently locked;
5884 NULL if it isn't locked. Note that this lock *must* be set while
5885 executing any *function other than the jump pad. See
5886 fast_tracepoint_collecting. */
5887 static collecting_t * ATTR_USED collecting;
5889 /* This routine, called from the jump pad (in asm) is designed to be
5890 called from the jump pads of fast tracepoints, thus it is on the
5893 IP_AGENT_EXPORT void ATTR_USED
5894 gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5896 struct fast_tracepoint_ctx ctx;
5898 /* Don't do anything until the trace run is completely set up. */
5902 ctx.base.type = fast_tracepoint;
5904 ctx.regcache_initted = 0;
5905 /* Wrap the regblock in a register cache (in the stack, we don't
5906 want to malloc here). */
5907 ctx.regspace = alloca (register_cache_size ());
5908 if (ctx.regspace == NULL)
5910 trace_debug ("Trace buffer block allocation failed, skipping");
5914 for (ctx.tpoint = tpoint;
5915 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5916 ctx.tpoint = ctx.tpoint->next)
5918 if (!ctx.tpoint->enabled)
5921 /* Multiple tracepoints of different types, such as fast tracepoint and
5922 static tracepoint, can be set at the same address. */
5923 if (ctx.tpoint->type != tpoint->type)
5926 /* Test the condition if present, and collect if true. */
5927 if (ctx.tpoint->cond == NULL
5928 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5931 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5932 ctx.tpoint->address, ctx.tpoint);
5934 /* Note that this will cause original insns to be written back
5935 to where we jumped from, but that's OK because we're jumping
5936 back to the next whole instruction. This will go badly if
5937 instruction restoration is not atomic though. */
5938 if (stopping_tracepoint
5939 || trace_buffer_is_full
5940 || expr_eval_result != expr_eval_no_error)
5948 /* If there was a condition and it evaluated to false, the only
5949 way we would stop tracing is if there was an error during
5950 condition expression evaluation. */
5951 if (expr_eval_result != expr_eval_no_error)
5962 #ifndef IN_PROCESS_AGENT
5964 /* Bytecode compilation. */
5966 CORE_ADDR current_insn_ptr;
5970 struct bytecode_address
5975 /* Offset and size of field to be modified in the goto block. */
5976 int from_offset, from_size;
5977 struct bytecode_address *next;
5978 } *bytecode_address_table;
5981 get_raw_reg_func_addr (void)
5983 return ipa_sym_addrs.addr_get_raw_reg;
5987 emit_prologue (void)
5989 target_emit_ops ()->emit_prologue ();
5993 emit_epilogue (void)
5995 target_emit_ops ()->emit_epilogue ();
6001 target_emit_ops ()->emit_add ();
6007 target_emit_ops ()->emit_sub ();
6013 target_emit_ops ()->emit_mul ();
6019 target_emit_ops ()->emit_lsh ();
6023 emit_rsh_signed (void)
6025 target_emit_ops ()->emit_rsh_signed ();
6029 emit_rsh_unsigned (void)
6031 target_emit_ops ()->emit_rsh_unsigned ();
6037 target_emit_ops ()->emit_ext (arg);
6043 target_emit_ops ()->emit_log_not ();
6049 target_emit_ops ()->emit_bit_and ();
6055 target_emit_ops ()->emit_bit_or ();
6061 target_emit_ops ()->emit_bit_xor ();
6067 target_emit_ops ()->emit_bit_not ();
6073 target_emit_ops ()->emit_equal ();
6077 emit_less_signed (void)
6079 target_emit_ops ()->emit_less_signed ();
6083 emit_less_unsigned (void)
6085 target_emit_ops ()->emit_less_unsigned ();
6091 target_emit_ops ()->emit_ref (size);
6095 emit_if_goto (int *offset_p, int *size_p)
6097 target_emit_ops ()->emit_if_goto (offset_p, size_p);
6101 emit_goto (int *offset_p, int *size_p)
6103 target_emit_ops ()->emit_goto (offset_p, size_p);
6107 write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
6109 target_emit_ops ()->write_goto_address (from, to, size);
6113 emit_const (LONGEST num)
6115 target_emit_ops ()->emit_const (num);
6121 target_emit_ops ()->emit_reg (reg);
6127 target_emit_ops ()->emit_pop ();
6131 emit_stack_flush (void)
6133 target_emit_ops ()->emit_stack_flush ();
6137 emit_zero_ext (int arg)
6139 target_emit_ops ()->emit_zero_ext (arg);
6145 target_emit_ops ()->emit_swap ();
6149 emit_stack_adjust (int n)
6151 target_emit_ops ()->emit_stack_adjust (n);
6154 /* FN's prototype is `LONGEST(*fn)(int)'. */
6157 emit_int_call_1 (CORE_ADDR fn, int arg1)
6159 target_emit_ops ()->emit_int_call_1 (fn, arg1);
6162 /* FN's prototype is `void(*fn)(int,LONGEST)'. */
6165 emit_void_call_2 (CORE_ADDR fn, int arg1)
6167 target_emit_ops ()->emit_void_call_2 (fn, arg1);
6171 emit_eq_goto (int *offset_p, int *size_p)
6173 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
6177 emit_ne_goto (int *offset_p, int *size_p)
6179 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
6183 emit_lt_goto (int *offset_p, int *size_p)
6185 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
6189 emit_ge_goto (int *offset_p, int *size_p)
6191 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
6195 emit_gt_goto (int *offset_p, int *size_p)
6197 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
6201 emit_le_goto (int *offset_p, int *size_p)
6203 target_emit_ops ()->emit_le_goto (offset_p, size_p);
6206 static enum eval_result_type compile_bytecodes (struct agent_expr *aexpr);
6209 compile_tracepoint_condition (struct tracepoint *tpoint,
6210 CORE_ADDR *jump_entry)
6212 CORE_ADDR entry_point = *jump_entry;
6213 enum eval_result_type err;
6215 trace_debug ("Starting condition compilation for tracepoint %d\n",
6218 /* Initialize the global pointer to the code being built. */
6219 current_insn_ptr = *jump_entry;
6223 err = compile_bytecodes (tpoint->cond);
6225 if (err == expr_eval_no_error)
6229 /* Record the beginning of the compiled code. */
6230 tpoint->compiled_cond = entry_point;
6232 trace_debug ("Condition compilation for tracepoint %d complete\n",
6237 /* Leave the unfinished code in situ, but don't point to it. */
6239 tpoint->compiled_cond = 0;
6241 trace_debug ("Condition compilation for tracepoint %d failed, "
6243 tpoint->number, err);
6246 /* Update the code pointer passed in. Note that we do this even if
6247 the compile fails, so that we can look at the partial results
6248 instead of letting them be overwritten. */
6249 *jump_entry = current_insn_ptr;
6251 /* Leave a gap, to aid dump decipherment. */
6255 /* Scan an agent expression for any evidence that the given PC is the
6256 target of a jump bytecode in the expression. */
6259 is_goto_target (struct agent_expr *aexpr, int pc)
6264 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
6266 op = aexpr->bytes[i];
6268 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
6270 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
6279 /* Given an agent expression, turn it into native code. */
6281 static enum eval_result_type
6282 compile_bytecodes (struct agent_expr *aexpr)
6286 unsigned char op, next_op;
6288 /* This is only used to build 64-bit value for constants. */
6290 struct bytecode_address *aentry, *aentry2;
6295 trace_debug ("Cannot compile op 0x%x\n", op); \
6296 return expr_eval_unhandled_opcode; \
6299 if (aexpr->length == 0)
6301 trace_debug ("empty agent expression\n");
6302 return expr_eval_empty_expression;
6305 bytecode_address_table = NULL;
6309 op = aexpr->bytes[pc];
6311 trace_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
6313 /* Record the compiled-code address of the bytecode, for use by
6314 jump instructions. */
6315 aentry = xmalloc (sizeof (struct bytecode_address));
6317 aentry->address = current_insn_ptr;
6318 aentry->goto_pc = -1;
6319 aentry->from_offset = aentry->from_size = 0;
6320 aentry->next = bytecode_address_table;
6321 bytecode_address_table = aentry;
6329 case gdb_agent_op_add:
6333 case gdb_agent_op_sub:
6337 case gdb_agent_op_mul:
6341 case gdb_agent_op_div_signed:
6345 case gdb_agent_op_div_unsigned:
6349 case gdb_agent_op_rem_signed:
6353 case gdb_agent_op_rem_unsigned:
6357 case gdb_agent_op_lsh:
6361 case gdb_agent_op_rsh_signed:
6365 case gdb_agent_op_rsh_unsigned:
6366 emit_rsh_unsigned ();
6369 case gdb_agent_op_trace:
6373 case gdb_agent_op_trace_quick:
6377 case gdb_agent_op_log_not:
6381 case gdb_agent_op_bit_and:
6385 case gdb_agent_op_bit_or:
6389 case gdb_agent_op_bit_xor:
6393 case gdb_agent_op_bit_not:
6397 case gdb_agent_op_equal:
6398 next_op = aexpr->bytes[pc];
6399 if (next_op == gdb_agent_op_if_goto
6400 && !is_goto_target (aexpr, pc)
6401 && target_emit_ops ()->emit_eq_goto)
6403 trace_debug ("Combining equal & if_goto");
6406 arg = aexpr->bytes[pc++];
6407 arg = (arg << 8) + aexpr->bytes[pc++];
6408 aentry->goto_pc = arg;
6409 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
6411 else if (next_op == gdb_agent_op_log_not
6412 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6413 && !is_goto_target (aexpr, pc + 1)
6414 && target_emit_ops ()->emit_ne_goto)
6416 trace_debug ("Combining equal & log_not & if_goto");
6419 arg = aexpr->bytes[pc++];
6420 arg = (arg << 8) + aexpr->bytes[pc++];
6421 aentry->goto_pc = arg;
6422 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
6428 case gdb_agent_op_less_signed:
6429 next_op = aexpr->bytes[pc];
6430 if (next_op == gdb_agent_op_if_goto
6431 && !is_goto_target (aexpr, pc))
6433 trace_debug ("Combining less_signed & if_goto");
6436 arg = aexpr->bytes[pc++];
6437 arg = (arg << 8) + aexpr->bytes[pc++];
6438 aentry->goto_pc = arg;
6439 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
6441 else if (next_op == gdb_agent_op_log_not
6442 && !is_goto_target (aexpr, pc)
6443 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6444 && !is_goto_target (aexpr, pc + 1))
6446 trace_debug ("Combining less_signed & log_not & if_goto");
6449 arg = aexpr->bytes[pc++];
6450 arg = (arg << 8) + aexpr->bytes[pc++];
6451 aentry->goto_pc = arg;
6452 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
6455 emit_less_signed ();
6458 case gdb_agent_op_less_unsigned:
6459 emit_less_unsigned ();
6462 case gdb_agent_op_ext:
6463 arg = aexpr->bytes[pc++];
6464 if (arg < (sizeof (LONGEST) * 8))
6468 case gdb_agent_op_ref8:
6472 case gdb_agent_op_ref16:
6476 case gdb_agent_op_ref32:
6480 case gdb_agent_op_ref64:
6484 case gdb_agent_op_if_goto:
6485 arg = aexpr->bytes[pc++];
6486 arg = (arg << 8) + aexpr->bytes[pc++];
6487 aentry->goto_pc = arg;
6488 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
6491 case gdb_agent_op_goto:
6492 arg = aexpr->bytes[pc++];
6493 arg = (arg << 8) + aexpr->bytes[pc++];
6494 aentry->goto_pc = arg;
6495 emit_goto (&(aentry->from_offset), &(aentry->from_size));
6498 case gdb_agent_op_const8:
6499 emit_stack_flush ();
6500 top = aexpr->bytes[pc++];
6504 case gdb_agent_op_const16:
6505 emit_stack_flush ();
6506 top = aexpr->bytes[pc++];
6507 top = (top << 8) + aexpr->bytes[pc++];
6511 case gdb_agent_op_const32:
6512 emit_stack_flush ();
6513 top = aexpr->bytes[pc++];
6514 top = (top << 8) + aexpr->bytes[pc++];
6515 top = (top << 8) + aexpr->bytes[pc++];
6516 top = (top << 8) + aexpr->bytes[pc++];
6520 case gdb_agent_op_const64:
6521 emit_stack_flush ();
6522 top = aexpr->bytes[pc++];
6523 top = (top << 8) + aexpr->bytes[pc++];
6524 top = (top << 8) + aexpr->bytes[pc++];
6525 top = (top << 8) + aexpr->bytes[pc++];
6526 top = (top << 8) + aexpr->bytes[pc++];
6527 top = (top << 8) + aexpr->bytes[pc++];
6528 top = (top << 8) + aexpr->bytes[pc++];
6529 top = (top << 8) + aexpr->bytes[pc++];
6533 case gdb_agent_op_reg:
6534 emit_stack_flush ();
6535 arg = aexpr->bytes[pc++];
6536 arg = (arg << 8) + aexpr->bytes[pc++];
6540 case gdb_agent_op_end:
6541 trace_debug ("At end of expression\n");
6543 /* Assume there is one stack element left, and that it is
6544 cached in "top" where emit_epilogue can get to it. */
6545 emit_stack_adjust (1);
6550 case gdb_agent_op_dup:
6551 /* In our design, dup is equivalent to stack flushing. */
6552 emit_stack_flush ();
6555 case gdb_agent_op_pop:
6559 case gdb_agent_op_zero_ext:
6560 arg = aexpr->bytes[pc++];
6561 if (arg < (sizeof (LONGEST) * 8))
6562 emit_zero_ext (arg);
6565 case gdb_agent_op_swap:
6566 next_op = aexpr->bytes[pc];
6567 /* Detect greater-than comparison sequences. */
6568 if (next_op == gdb_agent_op_less_signed
6569 && !is_goto_target (aexpr, pc)
6570 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6571 && !is_goto_target (aexpr, pc + 1))
6573 trace_debug ("Combining swap & less_signed & if_goto");
6576 arg = aexpr->bytes[pc++];
6577 arg = (arg << 8) + aexpr->bytes[pc++];
6578 aentry->goto_pc = arg;
6579 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
6581 else if (next_op == gdb_agent_op_less_signed
6582 && !is_goto_target (aexpr, pc)
6583 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
6584 && !is_goto_target (aexpr, pc + 1)
6585 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
6586 && !is_goto_target (aexpr, pc + 2))
6588 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6591 arg = aexpr->bytes[pc++];
6592 arg = (arg << 8) + aexpr->bytes[pc++];
6593 aentry->goto_pc = arg;
6594 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
6600 case gdb_agent_op_getv:
6601 emit_stack_flush ();
6602 arg = aexpr->bytes[pc++];
6603 arg = (arg << 8) + aexpr->bytes[pc++];
6604 emit_int_call_1 (ipa_sym_addrs.addr_get_trace_state_variable_value,
6608 case gdb_agent_op_setv:
6609 arg = aexpr->bytes[pc++];
6610 arg = (arg << 8) + aexpr->bytes[pc++];
6611 emit_void_call_2 (ipa_sym_addrs.addr_set_trace_state_variable_value,
6615 case gdb_agent_op_tracev:
6619 /* GDB never (currently) generates any of these ops. */
6620 case gdb_agent_op_float:
6621 case gdb_agent_op_ref_float:
6622 case gdb_agent_op_ref_double:
6623 case gdb_agent_op_ref_long_double:
6624 case gdb_agent_op_l_to_d:
6625 case gdb_agent_op_d_to_l:
6626 case gdb_agent_op_trace16:
6631 trace_debug ("Agent expression op 0x%x not recognized\n", op);
6632 /* Don't struggle on, things will just get worse. */
6633 return expr_eval_unrecognized_opcode;
6636 /* This catches errors that occur in target-specific code
6640 trace_debug ("Error %d while emitting code for %s\n",
6641 emit_error, gdb_agent_op_name (op));
6642 return expr_eval_unhandled_opcode;
6645 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op));
6648 /* Now fill in real addresses as goto destinations. */
6649 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
6653 if (aentry->goto_pc < 0)
6656 /* Find the location that we are going to, and call back into
6657 target-specific code to write the actual address or
6659 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
6661 if (aentry2->pc == aentry->goto_pc)
6663 trace_debug ("Want to jump from %s to %s\n",
6664 paddress (aentry->address),
6665 paddress (aentry2->address));
6666 write_goto_address (aentry->address + aentry->from_offset,
6667 aentry2->address, aentry->from_size);
6673 /* Error out if we didn't find a destination. */
6676 trace_debug ("Destination of goto %d not found\n",
6678 return expr_eval_invalid_goto;
6682 return expr_eval_no_error;
6685 /* We'll need to adjust these when we consider bi-arch setups, and big
6689 write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
6691 return write_inferior_memory (where,
6692 (unsigned char *) &ptr, sizeof (void *));
6695 /* The base pointer of the IPA's heap. This is the only memory the
6696 IPA is allowed to use. The IPA should _not_ call the inferior's
6697 `malloc' during operation. That'd be slow, and, most importantly,
6698 it may not be safe. We may be collecting a tracepoint in a signal
6699 handler, for example. */
6700 static CORE_ADDR target_tp_heap;
6702 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6706 target_malloc (ULONGEST size)
6710 if (target_tp_heap == 0)
6712 /* We have the pointer *address*, need what it points to. */
6713 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
6715 fatal ("could get target heap head pointer");
6718 ptr = target_tp_heap;
6719 target_tp_heap += size;
6721 /* Pad to 8-byte alignment. */
6722 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
6728 download_agent_expr (struct agent_expr *expr)
6730 CORE_ADDR expr_addr;
6731 CORE_ADDR expr_bytes;
6733 expr_addr = target_malloc (sizeof (*expr));
6734 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
6736 expr_bytes = target_malloc (expr->length);
6737 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
6739 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
6744 /* Align V up to N bits. */
6745 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6747 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6750 download_tracepoint_1 (struct tracepoint *tpoint)
6752 struct tracepoint target_tracepoint;
6753 CORE_ADDR tpptr = 0;
6755 gdb_assert (tpoint->type == fast_tracepoint
6756 || tpoint->type == static_tracepoint);
6758 if (tpoint->cond != NULL && target_emit_ops () != NULL)
6760 CORE_ADDR jentry, jump_entry;
6762 jentry = jump_entry = get_jump_space_head ();
6764 if (tpoint->cond != NULL)
6766 /* Pad to 8-byte alignment. (needed?) */
6767 /* Actually this should be left for the target to
6769 jentry = UALIGN (jentry, 8);
6771 compile_tracepoint_condition (tpoint, &jentry);
6774 /* Pad to 8-byte alignment. */
6775 jentry = UALIGN (jentry, 8);
6776 claim_jump_space (jentry - jump_entry);
6779 target_tracepoint = *tpoint;
6781 tpptr = target_malloc (sizeof (*tpoint));
6782 tpoint->obj_addr_on_target = tpptr;
6784 /* Write the whole object. We'll fix up its pointers in a bit.
6785 Assume no next for now. This is fixed up above on the next
6786 iteration, if there's any. */
6787 target_tracepoint.next = NULL;
6788 /* Need to clear this here too, since we're downloading the
6789 tracepoints before clearing our own copy. */
6790 target_tracepoint.hit_count = 0;
6792 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
6793 sizeof (target_tracepoint));
6796 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6798 download_agent_expr (tpoint->cond));
6800 if (tpoint->numactions)
6803 CORE_ADDR actions_array;
6805 /* The pointers array. */
6807 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
6808 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6812 /* Now for each pointer, download the action. */
6813 for (i = 0; i < tpoint->numactions; i++)
6815 CORE_ADDR ipa_action = 0;
6816 struct tracepoint_action *action = tpoint->actions[i];
6818 switch (action->type)
6822 = target_malloc (sizeof (struct collect_memory_action));
6823 write_inferior_memory (ipa_action,
6824 (unsigned char *) action,
6825 sizeof (struct collect_memory_action));
6829 = target_malloc (sizeof (struct collect_registers_action));
6830 write_inferior_memory (ipa_action,
6831 (unsigned char *) action,
6832 sizeof (struct collect_registers_action));
6837 struct eval_expr_action *eaction
6838 = (struct eval_expr_action *) action;
6840 ipa_action = target_malloc (sizeof (*eaction));
6841 write_inferior_memory (ipa_action,
6842 (unsigned char *) eaction,
6845 expr = download_agent_expr (eaction->expr);
6846 write_inferior_data_ptr
6847 (ipa_action + offsetof (struct eval_expr_action, expr),
6852 ipa_action = target_malloc
6853 (sizeof (struct collect_static_trace_data_action));
6854 write_inferior_memory
6856 (unsigned char *) action,
6857 sizeof (struct collect_static_trace_data_action));
6860 trace_debug ("unknown trace action '%c', ignoring",
6865 if (ipa_action != 0)
6866 write_inferior_data_ptr
6867 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
6874 download_tracepoint (struct tracepoint *tpoint)
6876 struct tracepoint *tp, *tp_prev;
6878 if (tpoint->type != fast_tracepoint
6879 && tpoint->type != static_tracepoint)
6882 download_tracepoint_1 (tpoint);
6884 /* Find the previous entry of TPOINT, which is fast tracepoint or
6885 static tracepoint. */
6887 for (tp = tracepoints; tp != tpoint; tp = tp->next)
6889 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
6895 CORE_ADDR tp_prev_target_next_addr;
6897 /* Insert TPOINT after TP_PREV in IPA. */
6898 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
6899 + offsetof (struct tracepoint, next),
6900 &tp_prev_target_next_addr))
6901 fatal ("error reading `tp_prev->next'");
6903 /* tpoint->next = tp_prev->next */
6904 write_inferior_data_ptr (tpoint->obj_addr_on_target
6905 + offsetof (struct tracepoint, next),
6906 tp_prev_target_next_addr);
6907 /* tp_prev->next = tpoint */
6908 write_inferior_data_ptr (tp_prev->obj_addr_on_target
6909 + offsetof (struct tracepoint, next),
6910 tpoint->obj_addr_on_target);
6913 /* First object in list, set the head pointer in the
6915 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
6916 tpoint->obj_addr_on_target);
6921 download_tracepoints (void)
6923 CORE_ADDR tpptr = 0, prev_tpptr = 0;
6924 struct tracepoint *tpoint;
6926 /* Start out empty. */
6927 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
6929 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6931 if (tpoint->type != fast_tracepoint
6932 && tpoint->type != static_tracepoint)
6937 download_tracepoint_1 (tpoint);
6939 tpptr = tpoint->obj_addr_on_target;
6941 if (tpoint == tracepoints)
6943 /* First object in list, set the head pointer in the
6945 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
6949 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
6957 download_trace_state_variables (void)
6959 CORE_ADDR ptr = 0, prev_ptr = 0;
6960 struct trace_state_variable *tsv;
6962 /* Start out empty. */
6963 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
6965 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
6967 struct trace_state_variable target_tsv;
6969 /* TSV's with a getter have been initialized equally in both the
6970 inferior and GDBserver. Skip them. */
6971 if (tsv->getter != NULL)
6977 ptr = target_malloc (sizeof (*tsv));
6979 if (tsv == trace_state_variables)
6981 /* First object in list, set the head pointer in the
6984 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
6989 write_inferior_data_ptr (prev_ptr
6990 + offsetof (struct trace_state_variable,
6995 /* Write the whole object. We'll fix up its pointers in a bit.
6996 Assume no next, fixup when needed. */
6997 target_tsv.next = NULL;
6999 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
7000 sizeof (target_tsv));
7002 if (tsv->name != NULL)
7004 size_t size = strlen (tsv->name) + 1;
7005 CORE_ADDR name_addr = target_malloc (size);
7006 write_inferior_memory (name_addr,
7007 (unsigned char *) tsv->name, size);
7008 write_inferior_data_ptr (ptr
7009 + offsetof (struct trace_state_variable,
7014 if (tsv->getter != NULL)
7016 fatal ("what to do with these?");
7022 /* Fixup the next pointer in the last item in the list. */
7023 write_inferior_data_ptr (prev_ptr
7024 + offsetof (struct trace_state_variable,
7029 /* Upload complete trace frames out of the IP Agent's trace buffer
7030 into GDBserver's trace buffer. This always uploads either all or
7031 no trace frames. This is the counter part of
7032 `trace_alloc_trace_buffer'. See its description of the atomic
7033 synching mechanism. */
7036 upload_fast_traceframes (void)
7038 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
7039 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
7041 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
7042 unsigned int curr_tbctrl_idx;
7043 unsigned int ipa_trace_buffer_ctrl_curr;
7044 unsigned int ipa_trace_buffer_ctrl_curr_old;
7045 CORE_ADDR ipa_trace_buffer_ctrl_addr;
7046 struct breakpoint *about_to_request_buffer_space_bkpt;
7047 CORE_ADDR ipa_trace_buffer_lo;
7048 CORE_ADDR ipa_trace_buffer_hi;
7050 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
7051 &ipa_traceframe_read_count_racy))
7053 /* This will happen in most targets if the current thread is
7058 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
7059 &ipa_traceframe_write_count_racy))
7062 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
7063 ipa_traceframe_write_count_racy
7064 - ipa_traceframe_read_count_racy,
7065 ipa_traceframe_write_count_racy,
7066 ipa_traceframe_read_count_racy);
7068 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
7071 about_to_request_buffer_space_bkpt
7072 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
7075 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
7076 &ipa_trace_buffer_ctrl_curr))
7079 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
7081 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
7084 unsigned int prev, counter;
7086 /* Update the token, with new counters, and the GDBserver stamp
7087 bit. Alway reuse the current TBC index. */
7088 prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
7089 counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
7091 ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
7097 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
7098 ipa_trace_buffer_ctrl_curr))
7101 trace_debug ("Lib: Committed %08x -> %08x",
7102 ipa_trace_buffer_ctrl_curr_old,
7103 ipa_trace_buffer_ctrl_curr);
7105 /* Re-read these, now that we've installed the
7106 `about_to_request_buffer_space' breakpoint/lock. A thread could
7107 have finished a traceframe between the last read of these
7108 counters and setting the breakpoint above. If we start
7109 uploading, we never want to leave this function with
7110 traceframe_read_count != 0, otherwise, GDBserver could end up
7111 incrementing the counter tokens more than once (due to event loop
7112 nesting), which would break the IP agent's "effective" detection
7113 (see trace_alloc_trace_buffer). */
7114 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
7115 &ipa_traceframe_read_count))
7117 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
7118 &ipa_traceframe_write_count))
7123 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
7124 ipa_traceframe_write_count - ipa_traceframe_read_count,
7125 ipa_traceframe_write_count, ipa_traceframe_read_count);
7127 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
7128 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
7129 trace_debug ("note that ipa_traceframe_count's parts changed");
7132 /* Get the address of the current TBC object (the IP agent has an
7133 array of 3 such objects). The index is stored in the TBC
7135 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
7136 ipa_trace_buffer_ctrl_addr
7137 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
7139 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
7140 (unsigned char *) &ipa_trace_buffer_ctrl,
7141 sizeof (struct ipa_trace_buffer_control)))
7144 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
7145 &ipa_trace_buffer_lo))
7147 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
7148 &ipa_trace_buffer_hi))
7151 /* Offsets are easier to grok for debugging than raw addresses,
7152 especially for the small trace buffer sizes that are useful for
7154 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
7155 "endfree=%d wrap=%d hi=%d",
7157 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
7158 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
7159 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
7160 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
7161 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
7163 /* Note that the IPA's buffer is always circular. */
7165 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
7167 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
7168 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
7170 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
7171 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
7172 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
7173 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
7176 tf = IPA_FIRST_TRACEFRAME ();
7178 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
7180 struct tracepoint *tpoint;
7181 struct traceframe *tframe;
7182 unsigned char *block;
7183 struct traceframe ipa_tframe;
7185 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
7186 offsetof (struct traceframe, data)))
7187 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
7189 if (ipa_tframe.tpnum == 0)
7190 fatal ("Uploading: No (more) fast traceframes, but "
7191 "ipa_traceframe_count == %u??\n",
7192 ipa_traceframe_write_count - ipa_traceframe_read_count);
7194 /* Note that this will be incorrect for multi-location
7196 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
7198 tframe = add_traceframe (tpoint);
7201 trace_buffer_is_full = 1;
7202 trace_debug ("Uploading: trace buffer is full");
7206 /* Copy the whole set of blocks in one go for now. FIXME:
7207 split this in smaller blocks. */
7208 block = add_traceframe_block (tframe, ipa_tframe.data_size);
7211 if (read_inferior_memory (tf
7212 + offsetof (struct traceframe, data),
7213 block, ipa_tframe.data_size))
7214 error ("Uploading: Couldn't read traceframe data at %s\n",
7215 paddress (tf + offsetof (struct traceframe, data)));
7218 trace_debug ("Uploading: traceframe didn't fit");
7219 finish_traceframe (tframe);
7222 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
7224 /* If we freed the traceframe that wrapped around, go back
7225 to the non-wrap case. */
7226 if (tf < ipa_trace_buffer_ctrl.start)
7228 trace_debug ("Lib: Discarding past the wraparound");
7229 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
7231 ipa_trace_buffer_ctrl.start = tf;
7232 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
7233 ++ipa_traceframe_read_count;
7235 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
7236 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
7238 trace_debug ("Lib: buffer is fully empty. "
7239 "Trace buffer [%d] start=%d free=%d endfree=%d",
7241 (int) (ipa_trace_buffer_ctrl.start
7242 - ipa_trace_buffer_lo),
7243 (int) (ipa_trace_buffer_ctrl.free
7244 - ipa_trace_buffer_lo),
7245 (int) (ipa_trace_buffer_ctrl.end_free
7246 - ipa_trace_buffer_lo));
7248 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
7249 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
7250 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
7251 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
7254 trace_debug ("Uploaded a traceframe\n"
7255 "Lib: Trace buffer [%d] start=%d free=%d "
7256 "endfree=%d wrap=%d hi=%d",
7258 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
7259 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
7260 (int) (ipa_trace_buffer_ctrl.end_free
7261 - ipa_trace_buffer_lo),
7262 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
7263 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
7266 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
7267 (unsigned char *) &ipa_trace_buffer_ctrl,
7268 sizeof (struct ipa_trace_buffer_control)))
7271 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
7272 ipa_traceframe_read_count);
7274 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
7277 cancel_breakpoints ();
7279 delete_breakpoint (about_to_request_buffer_space_bkpt);
7280 about_to_request_buffer_space_bkpt = NULL;
7284 if (trace_buffer_is_full)
7289 #ifdef IN_PROCESS_AGENT
7291 IP_AGENT_EXPORT int ust_loaded;
7292 IP_AGENT_EXPORT char cmd_buf[CMD_BUF_SIZE];
7296 /* Static tracepoints. */
7298 /* UST puts a "struct tracepoint" in the global namespace, which
7299 conflicts with our tracepoint. Arguably, being a library, it
7300 shouldn't take ownership of such a generic name. We work around it
7302 #define tracepoint ust_tracepoint
7303 #include <ust/ust.h>
7306 extern int serialize_to_text (char *outbuf, int bufsize,
7307 const char *fmt, va_list ap);
7309 #define GDB_PROBE_NAME "gdb"
7311 /* We dynamically search for the UST symbols instead of linking them
7312 in. This lets the user decide if the application uses static
7313 tracepoints, instead of always pulling libust.so in. This vector
7314 holds pointers to all functions we care about. */
7318 int (*serialize_to_text) (char *outbuf, int bufsize,
7319 const char *fmt, va_list ap);
7321 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
7322 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
7324 int (*ltt_marker_connect) (const char *channel, const char *mname,
7326 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
7329 void (*marker_iter_start) (struct marker_iter *iter);
7330 void (*marker_iter_next) (struct marker_iter *iter);
7331 void (*marker_iter_stop) (struct marker_iter *iter);
7332 void (*marker_iter_reset) (struct marker_iter *iter);
7337 /* Cast through typeof to catch incompatible API changes. Since UST
7338 only builds with gcc, we can freely use gcc extensions here
7340 #define GET_UST_SYM(SYM) \
7343 if (ust_ops.SYM == NULL) \
7344 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
7345 if (ust_ops.SYM == NULL) \
7349 #define USTF(SYM) ust_ops.SYM
7351 /* Get pointers to all libust.so functions we care about. */
7356 GET_UST_SYM (serialize_to_text);
7358 GET_UST_SYM (ltt_probe_register);
7359 GET_UST_SYM (ltt_probe_unregister);
7360 GET_UST_SYM (ltt_marker_connect);
7361 GET_UST_SYM (ltt_marker_disconnect);
7363 GET_UST_SYM (marker_iter_start);
7364 GET_UST_SYM (marker_iter_next);
7365 GET_UST_SYM (marker_iter_stop);
7366 GET_UST_SYM (marker_iter_reset);
7372 /* Given an UST marker, return the matching gdb static tracepoint.
7373 The match is done by address. */
7375 static struct tracepoint *
7376 ust_marker_to_static_tracepoint (const struct marker *mdata)
7378 struct tracepoint *tpoint;
7380 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
7382 if (tpoint->type != static_tracepoint)
7385 if (tpoint->address == (uintptr_t) mdata->location)
7392 /* The probe function we install on lttng/ust markers. Whenever a
7393 probed ust marker is hit, this function is called. This is similar
7394 to gdb_collect, only for static tracepoints, instead of fast
7398 gdb_probe (const struct marker *mdata, void *probe_private,
7399 struct registers *regs, void *call_private,
7400 const char *fmt, va_list *args)
7402 struct tracepoint *tpoint;
7403 struct static_tracepoint_ctx ctx;
7405 /* Don't do anything until the trace run is completely set up. */
7408 trace_debug ("gdb_probe: not tracing\n");
7412 ctx.base.type = static_tracepoint;
7413 ctx.regcache_initted = 0;
7418 /* Wrap the regblock in a register cache (in the stack, we don't
7419 want to malloc here). */
7420 ctx.regspace = alloca (register_cache_size ());
7421 if (ctx.regspace == NULL)
7423 trace_debug ("Trace buffer block allocation failed, skipping");
7427 tpoint = ust_marker_to_static_tracepoint (mdata);
7430 trace_debug ("gdb_probe: marker not known: "
7431 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7432 mdata->location, mdata->channel,
7433 mdata->name, mdata->format);
7437 if (!tpoint->enabled)
7439 trace_debug ("gdb_probe: tracepoint disabled");
7443 ctx.tpoint = tpoint;
7445 trace_debug ("gdb_probe: collecting marker: "
7446 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7447 mdata->location, mdata->channel,
7448 mdata->name, mdata->format);
7450 /* Test the condition if present, and collect if true. */
7451 if (tpoint->cond == NULL
7452 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
7455 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
7456 tpoint->address, tpoint);
7458 if (stopping_tracepoint
7459 || trace_buffer_is_full
7460 || expr_eval_result != expr_eval_no_error)
7465 /* If there was a condition and it evaluated to false, the only
7466 way we would stop tracing is if there was an error during
7467 condition expression evaluation. */
7468 if (expr_eval_result != expr_eval_no_error)
7473 /* Called if the gdb static tracepoint requested collecting "$_sdata",
7474 static tracepoint string data. This is a string passed to the
7475 tracing library by the user, at the time of the tracepoint marker
7476 call. E.g., in the UST marker call:
7478 trace_mark (ust, bar33, "str %s", "FOOBAZ");
7480 the collected data is "str FOOBAZ".
7484 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
7486 struct tracepoint *tpoint,
7487 struct traceframe *tframe)
7489 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
7490 unsigned char *bufspace;
7493 unsigned short blocklen;
7497 trace_debug ("Wanted to collect static trace data, "
7498 "but there's no static trace data");
7502 va_copy (copy, *umd->args);
7503 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
7506 trace_debug ("Want to collect ust data");
7508 /* 'S' + size + string */
7509 bufspace = add_traceframe_block (tframe,
7510 1 + sizeof (blocklen) + size + 1);
7511 if (bufspace == NULL)
7513 trace_debug ("Trace buffer block allocation failed, skipping");
7517 /* Identify a static trace data block. */
7520 blocklen = size + 1;
7521 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
7523 va_copy (copy, *umd->args);
7524 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
7525 size + 1, umd->fmt, copy);
7528 trace_debug ("Storing static tracepoint data in regblock: %s",
7529 bufspace + 1 + sizeof (blocklen));
7532 /* The probe to register with lttng/ust. */
7533 static struct ltt_available_probe gdb_ust_probe =
7540 #endif /* HAVE_UST */
7541 #endif /* IN_PROCESS_AGENT */
7545 #include <sys/socket.h>
7548 #ifndef UNIX_PATH_MAX
7549 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
7552 /* Where we put the socked used for synchronization. */
7553 #define SOCK_DIR P_tmpdir
7555 #endif /* HAVE_UST */
7557 #ifndef IN_PROCESS_AGENT
7562 gdb_ust_connect_sync_socket (int pid)
7564 struct sockaddr_un addr;
7566 char path[UNIX_PATH_MAX];
7568 res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", SOCK_DIR, pid);
7569 if (res >= UNIX_PATH_MAX)
7571 trace_debug ("string overflow allocating socket name");
7575 res = fd = socket (PF_UNIX, SOCK_STREAM, 0);
7578 warning ("error opening sync socket: %s\n", strerror (errno));
7582 addr.sun_family = AF_UNIX;
7584 res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
7585 if (res >= UNIX_PATH_MAX)
7587 warning ("string overflow allocating socket name\n");
7592 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
7595 warning ("error connecting sync socket (%s): %s. "
7596 "Make sure the directory exists and that it is writable.",
7597 path, strerror (errno));
7605 /* Resume thread PTID. */
7608 resume_thread (ptid_t ptid)
7610 struct thread_resume resume_info;
7612 resume_info.thread = ptid;
7613 resume_info.kind = resume_continue;
7614 resume_info.sig = TARGET_SIGNAL_0;
7615 (*the_target->resume) (&resume_info, 1);
7618 /* Stop thread PTID. */
7621 stop_thread (ptid_t ptid)
7623 struct thread_resume resume_info;
7625 resume_info.thread = ptid;
7626 resume_info.kind = resume_stop;
7627 resume_info.sig = TARGET_SIGNAL_0;
7628 (*the_target->resume) (&resume_info, 1);
7631 /* Ask the in-process agent to run a command. Since we don't want to
7632 have to handle the IPA hitting breakpoints while running the
7633 command, we pause all threads, remove all breakpoints, and then set
7634 the helper thread re-running. We communicate with the helper
7635 thread by means of direct memory xfering, and a socket for
7639 run_inferior_command (char *cmd)
7643 int pid = ptid_get_pid (current_inferior->entry.id);
7645 ptid_t ptid = null_ptid;
7647 trace_debug ("run_inferior_command: running: %s", cmd);
7650 uninsert_all_breakpoints ();
7652 if (read_inferior_integer (ipa_sym_addrs.addr_helper_thread_id, &tid))
7654 warning ("Error reading helper thread's id in lib");
7660 warning ("helper thread not initialized yet");
7664 if (write_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
7665 (unsigned char *) cmd, strlen (cmd) + 1))
7667 warning ("Error writing command");
7671 ptid = ptid_build (pid, tid, 0);
7673 resume_thread (ptid);
7675 fd = gdb_ust_connect_sync_socket (pid);
7681 trace_debug ("signalling helper thread");
7685 ret = write (fd, buf, 1);
7686 } while (ret == -1 && errno == EINTR);
7688 trace_debug ("waiting for helper thread's response");
7692 ret = read (fd, buf, 1);
7693 } while (ret == -1 && errno == EINTR);
7697 trace_debug ("helper thread's response received");
7702 /* Need to read response with the inferior stopped. */
7703 if (!ptid_equal (ptid, null_ptid))
7705 int was_non_stop = non_stop;
7706 struct target_waitstatus status;
7710 mywait (ptid, &status, 0, 0);
7711 non_stop = was_non_stop;
7716 if (read_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
7717 (unsigned char *) cmd, CMD_BUF_SIZE))
7719 warning ("Error reading command response");
7724 trace_debug ("run_inferior_command: response: %s", cmd);
7728 reinsert_all_breakpoints ();
7734 #else /* HAVE_UST */
7737 run_inferior_command (char *cmd)
7742 #endif /* HAVE_UST */
7744 #else /* !IN_PROCESS_AGENT */
7746 /* Thread ID of the helper thread. GDBserver reads this to know which
7747 is the help thread. This is an LWP id on Linux. */
7748 int helper_thread_id;
7753 init_named_socket (const char *name)
7756 struct sockaddr_un addr;
7758 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
7761 warning ("socket creation failed: %s", strerror (errno));
7765 addr.sun_family = AF_UNIX;
7767 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
7768 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
7770 result = access (name, F_OK);
7774 result = unlink (name);
7777 warning ("unlink failed: %s", strerror (errno));
7781 warning ("socket %s already exists; overwriting", name);
7784 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
7787 warning ("bind failed: %s", strerror (errno));
7792 result = listen (fd, 1);
7795 warning ("listen: %s", strerror (errno));
7804 gdb_ust_socket_init (void)
7807 char name[UNIX_PATH_MAX];
7809 result = xsnprintf (name, UNIX_PATH_MAX, "%s/gdb_ust%d",
7810 SOCK_DIR, getpid ());
7811 if (result >= UNIX_PATH_MAX)
7813 trace_debug ("string overflow allocating socket name");
7817 fd = init_named_socket (name);
7819 warning ("Error initializing named socket (%s) for communication with the "
7820 "ust helper thread. Check that directory exists and that it "
7821 "is writable.", name);
7826 /* Return an hexstr version of the STR C string, fit for sending to
7830 cstr_to_hexstr (const char *str)
7832 int len = strlen (str);
7833 char *hexstr = xmalloc (len * 2 + 1);
7834 convert_int_to_ascii ((gdb_byte *) str, hexstr, len);
7838 /* The next marker to be returned on a qTsSTM command. */
7839 static const struct marker *next_st;
7841 /* Returns the first known marker. */
7846 struct marker_iter iter;
7848 USTF(marker_iter_reset) (&iter);
7849 USTF(marker_iter_start) (&iter);
7854 /* Returns the marker following M. */
7856 const struct marker *
7857 next_marker (const struct marker *m)
7859 struct marker_iter iter;
7861 USTF(marker_iter_reset) (&iter);
7862 USTF(marker_iter_start) (&iter);
7864 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7866 if (iter.marker == m)
7868 USTF(marker_iter_next) (&iter);
7876 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7880 response_ust_marker (char *packet, const struct marker *st)
7882 char *strid, *format, *tmp;
7884 next_st = next_marker (st);
7886 tmp = xmalloc (strlen (st->channel) + 1 +
7887 strlen (st->name) + 1);
7888 sprintf (tmp, "%s/%s", st->channel, st->name);
7890 strid = cstr_to_hexstr (tmp);
7893 format = cstr_to_hexstr (st->format);
7895 sprintf (packet, "m%s:%s:%s",
7896 paddress ((uintptr_t) st->location),
7904 /* Return the first static tracepoint, and initialize the state
7905 machine that will iterate through all the static tracepoints. */
7908 cmd_qtfstm (char *packet)
7910 trace_debug ("Returning first trace state variable definition");
7912 if (first_marker ())
7913 response_ust_marker (packet, first_marker ());
7915 strcpy (packet, "l");
7918 /* Return additional trace state variable definitions. */
7921 cmd_qtsstm (char *packet)
7923 trace_debug ("Returning static tracepoint");
7926 response_ust_marker (packet, next_st);
7928 strcpy (packet, "l");
7931 /* Disconnect the GDB probe from a marker at a given address. */
7934 unprobe_marker_at (char *packet)
7938 struct marker_iter iter;
7940 p += sizeof ("unprobe_marker_at:") - 1;
7942 p = unpack_varlen_hex (p, &address);
7944 USTF(marker_iter_reset) (&iter);
7945 USTF(marker_iter_start) (&iter);
7946 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7947 if ((uintptr_t ) iter.marker->location == address)
7951 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
7955 warning ("could not disable marker %s/%s",
7956 iter.marker->channel, iter.marker->name);
7961 /* Connect the GDB probe to a marker at a given address. */
7964 probe_marker_at (char *packet)
7968 struct marker_iter iter;
7971 p += sizeof ("probe_marker_at:") - 1;
7973 p = unpack_varlen_hex (p, &address);
7975 USTF(marker_iter_reset) (&iter);
7977 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7979 USTF(marker_iter_next) (&iter), m = iter.marker)
7980 if ((uintptr_t ) m->location == address)
7984 trace_debug ("found marker for address. "
7985 "ltt_marker_connect (marker = %s/%s)",
7986 m->channel, m->name);
7988 result = USTF(ltt_marker_connect) (m->channel, m->name,
7990 if (result && result != -EEXIST)
7991 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7992 m->channel, m->name, -result);
7996 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
7997 m->channel, m->name);
8001 strcpy (packet, "OK");
8005 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
8010 cmd_qtstmat (char *packet)
8014 struct marker_iter iter;
8017 p += sizeof ("qTSTMat:") - 1;
8019 p = unpack_varlen_hex (p, &address);
8021 USTF(marker_iter_reset) (&iter);
8023 for (USTF(marker_iter_start) (&iter), m = iter.marker;
8025 USTF(marker_iter_next) (&iter), m = iter.marker)
8026 if ((uintptr_t ) m->location == address)
8028 response_ust_marker (packet, m);
8032 strcpy (packet, "l");
8036 #include <sys/syscall.h>
8039 gdb_ust_thread (void *arg)
8045 listen_fd = gdb_ust_socket_init ();
8047 if (helper_thread_id == 0)
8048 helper_thread_id = syscall (SYS_gettid);
8050 if (listen_fd == -1)
8052 warning ("could not create sync socket\n");
8059 struct sockaddr_un sockaddr;
8064 tmp = sizeof (sockaddr);
8068 fd = accept (listen_fd, &sockaddr, &tmp);
8070 /* It seems an ERESTARTSYS can escape out of accept. */
8071 while (fd == -512 || (fd == -1 && errno == EINTR));
8075 warning ("Accept returned %d, error: %s\n",
8076 fd, strerror (errno));
8082 ret = read (fd, buf, 1);
8083 } while (ret == -1 && errno == EINTR);
8087 warning ("reading socket (fd=%d) failed with %s",
8088 fd, strerror (errno));
8095 if (strcmp ("qTfSTM", cmd_buf) == 0)
8097 cmd_qtfstm (cmd_buf);
8099 else if (strcmp ("qTsSTM", cmd_buf) == 0)
8101 cmd_qtsstm (cmd_buf);
8103 else if (strncmp ("unprobe_marker_at:",
8105 sizeof ("unprobe_marker_at:") - 1) == 0)
8107 unprobe_marker_at (cmd_buf);
8109 else if (strncmp ("probe_marker_at:",
8111 sizeof ("probe_marker_at:") - 1) == 0)
8113 probe_marker_at (cmd_buf);
8115 else if (strncmp ("qTSTMat:",
8117 sizeof ("qTSTMat:") - 1) == 0)
8119 cmd_qtstmat (cmd_buf);
8121 else if (strcmp (cmd_buf, "help") == 0)
8123 strcpy (cmd_buf, "for help, press F1\n");
8126 strcpy (cmd_buf, "");
8129 /* Fix compiler's warning: ignoring return value of 'write'. */
8130 ret = write (fd, buf, 1);
8151 /* We want the helper thread to be as transparent as possible, so
8152 have it inherit an all-signals-blocked mask. */
8154 sigfillset (&new_mask);
8155 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
8157 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
8159 res = pthread_create (&thread,
8164 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
8166 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
8168 while (helper_thread_id == 0)
8171 USTF(ltt_probe_register) (&gdb_ust_probe);
8174 #endif /* HAVE_UST */
8176 #include <sys/mman.h>
8179 IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
8180 IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
8181 IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
8182 IP_AGENT_EXPORT char *gdb_trampoline_buffer;
8183 IP_AGENT_EXPORT char *gdb_trampoline_buffer_end;
8184 IP_AGENT_EXPORT char *gdb_trampoline_buffer_error;
8186 /* Record the result of getting buffer space for fast tracepoint
8187 trampolines. Any error message is copied, since caller may not be
8188 using persistent storage. */
8191 set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
8193 gdb_trampoline_buffer = (char *) (uintptr_t) begin;
8194 gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
8196 strncpy (gdb_trampoline_buffer_error, errmsg, 99);
8198 strcpy (gdb_trampoline_buffer_error, "no buffer passed");
8201 static void __attribute__ ((constructor))
8202 initialize_tracepoint_ftlib (void)
8204 initialize_tracepoint ();
8211 #endif /* IN_PROCESS_AGENT */
8213 /* Return a timestamp, expressed as microseconds of the usual Unix
8214 time. (As the result is a 64-bit number, it will not overflow any
8218 get_timestamp (void)
8222 if (gettimeofday (&tv, 0) != 0)
8225 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
8229 initialize_tracepoint (void)
8231 /* There currently no way to change the buffer size. */
8232 const int sizeOfBuffer = 5 * 1024 * 1024;
8233 unsigned char *buf = xmalloc (sizeOfBuffer);
8234 init_trace_buffer (buf, sizeOfBuffer);
8236 /* Wire trace state variable 1 to be the timestamp. This will be
8237 uploaded to GDB upon connection and become one of its trace state
8238 variables. (In case you're wondering, if GDB already has a trace
8239 variable numbered 1, it will be renumbered.) */
8240 create_trace_state_variable (1, 0);
8241 set_trace_state_variable_name (1, "trace_timestamp");
8242 set_trace_state_variable_getter (1, get_timestamp);
8244 #ifdef IN_PROCESS_AGENT
8247 pagesize = sysconf (_SC_PAGE_SIZE);
8251 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
8253 /* Allocate scratch buffer aligned on a page boundary. */
8254 gdb_jump_pad_buffer = memalign (pagesize, pagesize * 20);
8255 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * 20;
8257 /* Make it writable and executable. */
8258 if (mprotect (gdb_jump_pad_buffer, pagesize * 20,
8259 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
8261 initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
8262 gdb_jump_pad_buffer, pagesize * 20, strerror (errno));
8265 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
8267 /* It's not a fatal error for something to go wrong with trampoline
8268 buffer setup, but it can be mysterious, so create a channel to
8269 report back on what went wrong, using a fixed size since we may
8270 not be able to allocate space later when the problem occurs. */
8271 gdb_trampoline_buffer_error = xmalloc (IPA_BUFSIZ);
8273 strcpy (gdb_trampoline_buffer_error, "No errors reported");
8275 initialize_low_tracepoint ();