1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /* This file is built for both GDBserver, and the in-process
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
59 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf, 1, 2);
62 trace_vdebug (const char *fmt, ...)
68 vsprintf (buf, fmt, ap);
69 fprintf (stderr, PROG "/tracepoint: %s\n", buf);
73 #define trace_debug_1(level, fmt, args...) \
75 if (level <= debug_threads) \
76 trace_vdebug ((fmt), ##args); \
79 #define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
83 # define ATTR_USED __attribute__((used))
84 # define ATTR_NOINLINE __attribute__((noinline))
85 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
88 # define ATTR_NOINLINE
89 # define ATTR_CONSTRUCTOR
92 /* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
95 #ifdef IN_PROCESS_AGENT
96 # if defined _WIN32 || defined __CYGWIN__
97 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
100 # define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
103 # define IP_AGENT_EXPORT ATTR_USED
107 # define IP_AGENT_EXPORT
110 /* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112 #ifdef IN_PROCESS_AGENT
113 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
116 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
117 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
118 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
119 # define collecting gdb_agent_collecting
120 # define gdb_collect gdb_agent_gdb_collect
121 # define stop_tracing gdb_agent_stop_tracing
122 # define flush_trace_buffer gdb_agent_flush_trace_buffer
123 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
124 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
125 # define stopping_tracepoint gdb_agent_stopping_tracepoint
126 # define expr_eval_result gdb_agent_expr_eval_result
127 # define error_tracepoint gdb_agent_error_tracepoint
128 # define tracepoints gdb_agent_tracepoints
129 # define tracing gdb_agent_tracing
130 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
131 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
132 # define trace_buffer_lo gdb_agent_trace_buffer_lo
133 # define trace_buffer_hi gdb_agent_trace_buffer_hi
134 # define traceframe_read_count gdb_agent_traceframe_read_count
135 # define traceframe_write_count gdb_agent_traceframe_write_count
136 # define traceframes_created gdb_agent_traceframes_created
137 # define trace_state_variables gdb_agent_trace_state_variables
138 # define get_raw_reg gdb_agent_get_raw_reg
139 # define get_trace_state_variable_value \
140 gdb_agent_get_trace_state_variable_value
141 # define set_trace_state_variable_value \
142 gdb_agent_set_trace_state_variable_value
143 # define ust_loaded gdb_agent_ust_loaded
144 # define helper_thread_id gdb_agent_helper_thread_id
145 # define cmd_buf gdb_agent_cmd_buf
148 #ifndef IN_PROCESS_AGENT
150 /* Addresses of in-process agent's symbols GDBserver cares about. */
152 struct ipa_sym_addresses
154 CORE_ADDR addr_gdb_tp_heap_buffer;
155 CORE_ADDR addr_gdb_jump_pad_buffer;
156 CORE_ADDR addr_gdb_jump_pad_buffer_end;
157 CORE_ADDR addr_gdb_trampoline_buffer;
158 CORE_ADDR addr_gdb_trampoline_buffer_end;
159 CORE_ADDR addr_gdb_trampoline_buffer_error;
160 CORE_ADDR addr_collecting;
161 CORE_ADDR addr_gdb_collect;
162 CORE_ADDR addr_stop_tracing;
163 CORE_ADDR addr_flush_trace_buffer;
164 CORE_ADDR addr_about_to_request_buffer_space;
165 CORE_ADDR addr_trace_buffer_is_full;
166 CORE_ADDR addr_stopping_tracepoint;
167 CORE_ADDR addr_expr_eval_result;
168 CORE_ADDR addr_error_tracepoint;
169 CORE_ADDR addr_tracepoints;
170 CORE_ADDR addr_tracing;
171 CORE_ADDR addr_trace_buffer_ctrl;
172 CORE_ADDR addr_trace_buffer_ctrl_curr;
173 CORE_ADDR addr_trace_buffer_lo;
174 CORE_ADDR addr_trace_buffer_hi;
175 CORE_ADDR addr_traceframe_read_count;
176 CORE_ADDR addr_traceframe_write_count;
177 CORE_ADDR addr_traceframes_created;
178 CORE_ADDR addr_trace_state_variables;
179 CORE_ADDR addr_get_raw_reg;
180 CORE_ADDR addr_get_trace_state_variable_value;
181 CORE_ADDR addr_set_trace_state_variable_value;
182 CORE_ADDR addr_ust_loaded;
191 IPA_SYM(gdb_tp_heap_buffer),
192 IPA_SYM(gdb_jump_pad_buffer),
193 IPA_SYM(gdb_jump_pad_buffer_end),
194 IPA_SYM(gdb_trampoline_buffer),
195 IPA_SYM(gdb_trampoline_buffer_end),
196 IPA_SYM(gdb_trampoline_buffer_error),
198 IPA_SYM(gdb_collect),
199 IPA_SYM(stop_tracing),
200 IPA_SYM(flush_trace_buffer),
201 IPA_SYM(about_to_request_buffer_space),
202 IPA_SYM(trace_buffer_is_full),
203 IPA_SYM(stopping_tracepoint),
204 IPA_SYM(expr_eval_result),
205 IPA_SYM(error_tracepoint),
206 IPA_SYM(tracepoints),
208 IPA_SYM(trace_buffer_ctrl),
209 IPA_SYM(trace_buffer_ctrl_curr),
210 IPA_SYM(trace_buffer_lo),
211 IPA_SYM(trace_buffer_hi),
212 IPA_SYM(traceframe_read_count),
213 IPA_SYM(traceframe_write_count),
214 IPA_SYM(traceframes_created),
215 IPA_SYM(trace_state_variables),
216 IPA_SYM(get_raw_reg),
217 IPA_SYM(get_trace_state_variable_value),
218 IPA_SYM(set_trace_state_variable_value),
222 static struct ipa_sym_addresses ipa_sym_addrs;
224 int all_tracepoint_symbols_looked_up;
227 in_process_agent_loaded (void)
229 return all_tracepoint_symbols_looked_up;
232 static int read_inferior_integer (CORE_ADDR symaddr, int *val);
234 /* Returns true if both the in-process agent library and the static
235 tracepoints libraries are loaded in the inferior, and agent has
236 capability on static tracepoints. */
239 in_process_agent_supports_ust (void)
243 if (!in_process_agent_loaded ())
245 warning ("In-process agent not loaded");
249 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE))
251 /* Agent understands static tracepoint, then check whether UST is in
252 fact loaded in the inferior. */
253 if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
255 warning ("Error reading ust_loaded in lib");
266 write_e_ipa_not_loaded (char *buffer)
269 "E.In-process agent library not loaded in process. "
270 "Fast and static tracepoints unavailable.");
273 /* Write an error to BUFFER indicating that UST isn't loaded in the
277 write_e_ust_not_loaded (char *buffer)
281 "E.UST library not loaded in process. "
282 "Static tracepoints unavailable.");
284 sprintf (buffer, "E.GDBserver was built without static tracepoints support");
288 /* If the in-process agent library isn't loaded in the inferior, write
289 an error to BUFFER, and return 1. Otherwise, return 0. */
292 maybe_write_ipa_not_loaded (char *buffer)
294 if (!in_process_agent_loaded ())
296 write_e_ipa_not_loaded (buffer);
302 /* If the in-process agent library and the ust (static tracepoints)
303 library aren't loaded in the inferior, write an error to BUFFER,
304 and return 1. Otherwise, return 0. */
307 maybe_write_ipa_ust_not_loaded (char *buffer)
309 if (!in_process_agent_loaded ())
311 write_e_ipa_not_loaded (buffer);
314 else if (!in_process_agent_supports_ust ())
316 write_e_ust_not_loaded (buffer);
322 /* Cache all future symbols that the tracepoints module might request.
323 We can not request symbols at arbitrary states in the remote
324 protocol, only when the client tells us that new symbols are
325 available. So when we load the in-process library, make sure to
326 check the entire list. */
329 tracepoint_look_up_symbols (void)
333 if (all_tracepoint_symbols_looked_up)
336 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
339 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
341 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
344 fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
349 if (agent_look_up_symbols () != 0)
352 all_tracepoint_symbols_looked_up = 1;
357 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
358 of the "stop_tracing" function. When this breakpoint is hit,
359 tracing stopped in the IPA for some reason. E.g., due to
360 tracepoint reaching the pass count, hitting conditional expression
361 evaluation error, etc.
363 The IPA's trace buffer is never in circular tracing mode: instead,
364 GDBserver's is, and whenever the in-process buffer fills, it calls
365 "flush_trace_buffer", which triggers an internal breakpoint.
366 GDBserver reacts to this breakpoint by pulling the meanwhile
367 collected data. Old frames discarding is always handled on the
370 #ifdef IN_PROCESS_AGENT
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 /* Base action. Concrete actions inherit this. */
477 struct tracepoint_action
482 /* An 'M' (collect memory) action. */
483 struct collect_memory_action
485 struct tracepoint_action base;
492 /* An 'R' (collect registers) action. */
494 struct collect_registers_action
496 struct tracepoint_action base;
499 /* An 'X' (evaluate expression) action. */
501 struct eval_expr_action
503 struct tracepoint_action base;
505 struct agent_expr *expr;
508 /* An 'L' (collect static trace data) action. */
509 struct collect_static_trace_data_action
511 struct tracepoint_action base;
514 /* This structure describes a piece of the source-level definition of
515 the tracepoint. The contents are not interpreted by the target,
516 but preserved verbatim for uploading upon reconnection. */
520 /* The type of string, such as "cond" for a conditional. */
523 /* The source-level string itself. For the sake of target
524 debugging, we store it in plaintext, even though it is always
525 transmitted in hex. */
528 /* Link to the next one in the list. We link them in the order
529 received, in case some make up an ordered list of commands or
531 struct source_string *next;
536 /* Trap based tracepoint. */
539 /* A fast tracepoint implemented with a jump instead of a trap. */
542 /* A static tracepoint, implemented by a program call into a tracing
547 struct tracepoint_hit_ctx;
549 typedef enum eval_result_type (*condfn) (struct tracepoint_hit_ctx *,
552 /* The definition of a tracepoint. */
554 /* Tracepoints may have multiple locations, each at a different
555 address. This can occur with optimizations, template
556 instantiation, etc. Since the locations may be in different
557 scopes, the conditions and actions may be different for each
558 location. Our target version of tracepoints is more like GDB's
559 notion of "breakpoint locations", but we have almost nothing that
560 is not per-location, so we bother having two kinds of objects. The
561 key consequence is that numbers are not unique, and that it takes
562 both number and address to identify a tracepoint uniquely. */
566 /* The number of the tracepoint, as specified by GDB. Several
567 tracepoint objects here may share a number. */
570 /* Address at which the tracepoint is supposed to trigger. Several
571 tracepoints may share an address. */
574 /* Tracepoint type. */
575 enum tracepoint_type type;
577 /* True if the tracepoint is currently enabled. */
580 /* The number of single steps that will be performed after each
584 /* The number of times the tracepoint may be hit before it will
585 terminate the entire tracing run. */
588 /* Pointer to the agent expression that is the tracepoint's
589 conditional, or NULL if the tracepoint is unconditional. */
590 struct agent_expr *cond;
592 /* The list of actions to take when the tracepoint triggers. */
594 struct tracepoint_action **actions;
596 /* Count of the times we've hit this tracepoint during the run.
597 Note that while-stepping steps are not counted as "hits". */
600 /* Cached sum of the sizes of traceframes created by this point. */
601 long traceframe_usage;
603 CORE_ADDR compiled_cond;
605 /* Link to the next tracepoint in the list. */
606 struct tracepoint *next;
608 #ifndef IN_PROCESS_AGENT
609 /* The list of actions to take when the tracepoint triggers, in
610 string/packet form. */
613 /* The collection of strings that describe the tracepoint as it was
614 entered into GDB. These are not used by the target, but are
615 reported back to GDB upon reconnection. */
616 struct source_string *source_strings;
618 /* The number of bytes displaced by fast tracepoints. It may subsume
619 multiple instructions, for multi-byte fast tracepoints. This
620 field is only valid for fast tracepoints. */
623 /* Only for fast tracepoints. */
624 CORE_ADDR obj_addr_on_target;
626 /* Address range where the original instruction under a fast
627 tracepoint was relocated to. (_end is actually one byte past
629 CORE_ADDR adjusted_insn_addr;
630 CORE_ADDR adjusted_insn_addr_end;
632 /* The address range of the piece of the jump pad buffer that was
633 assigned to this fast tracepoint. (_end is actually one byte
636 CORE_ADDR jump_pad_end;
638 /* The address range of the piece of the trampoline buffer that was
639 assigned to this fast tracepoint. (_end is actually one byte
641 CORE_ADDR trampoline;
642 CORE_ADDR trampoline_end;
644 /* The list of actions to take while in a stepping loop. These
645 fields are only valid for patch-based tracepoints. */
646 int num_step_actions;
647 struct tracepoint_action **step_actions;
648 /* Same, but in string/packet form. */
649 char **step_actions_str;
651 /* Handle returned by the breakpoint or tracepoint module when we
652 inserted the trap or jump, or hooked into a static tracepoint.
653 NULL if we haven't inserted it yet. */
659 #ifndef IN_PROCESS_AGENT
661 /* Given `while-stepping', a thread may be collecting data for more
662 than one tracepoint simultaneously. On the other hand, the same
663 tracepoint with a while-stepping action may be hit by more than one
664 thread simultaneously (but not quite, each thread could be handling
665 a different step). Each thread holds a list of these objects,
666 representing the current step of each while-stepping action being
671 struct wstep_state *next;
673 /* The tracepoint number. */
675 /* The tracepoint's address. */
676 CORE_ADDR tp_address;
678 /* The number of the current step in this 'while-stepping'
685 /* The linked list of all tracepoints. Marked explicitly as used as
686 the in-process library doesn't use it for the fast tracepoints
688 IP_AGENT_EXPORT struct tracepoint *tracepoints ATTR_USED;
690 #ifndef IN_PROCESS_AGENT
692 /* Pointer to the last tracepoint in the list, new tracepoints are
693 linked in at the end. */
695 static struct tracepoint *last_tracepoint;
698 /* The first tracepoint to exceed its pass count. */
700 IP_AGENT_EXPORT struct tracepoint *stopping_tracepoint;
702 /* True if the trace buffer is full or otherwise no longer usable. */
704 IP_AGENT_EXPORT int trace_buffer_is_full;
706 static enum eval_result_type expr_eval_result = expr_eval_no_error;
708 #ifndef IN_PROCESS_AGENT
710 static const char *eval_result_names[] =
712 "terror:in the attic", /* this should never be reported */
713 "terror:empty expression",
714 "terror:empty stack",
715 "terror:stack overflow",
716 "terror:stack underflow",
717 "terror:unhandled opcode",
718 "terror:unrecognized opcode",
719 "terror:divide by zero"
724 /* The tracepoint in which the error occurred. */
726 static struct tracepoint *error_tracepoint;
728 struct trace_state_variable
730 /* This is the name of the variable as used in GDB. The target
731 doesn't use the name, but needs to have it for saving and
732 reconnection purposes. */
735 /* This number identifies the variable uniquely. Numbers may be
736 assigned either by the target (in the case of builtin variables),
737 or by GDB, and are presumed unique during the course of a trace
741 /* The variable's initial value, a 64-bit signed integer always. */
742 LONGEST initial_value;
744 /* The variable's value, a 64-bit signed integer always. */
747 /* Pointer to a getter function, used to supply computed values. */
748 LONGEST (*getter) (void);
750 /* Link to the next variable. */
751 struct trace_state_variable *next;
754 /* Linked list of all trace state variables. */
756 #ifdef IN_PROCESS_AGENT
757 struct trace_state_variable *alloced_trace_state_variables;
760 IP_AGENT_EXPORT struct trace_state_variable *trace_state_variables;
762 /* The results of tracing go into a fixed-size space known as the
763 "trace buffer". Because usage follows a limited number of
764 patterns, we manage it ourselves rather than with malloc. Basic
765 rules are that we create only one trace frame at a time, each is
766 variable in size, they are never moved once created, and we only
767 discard if we are doing a circular buffer, and then only the oldest
768 ones. Each trace frame includes its own size, so we don't need to
769 link them together, and the trace frame number is relative to the
770 first one, so we don't need to record numbers. A trace frame also
771 records the number of the tracepoint that created it. The data
772 itself is a series of blocks, each introduced by a single character
773 and with a defined format. Each type of block has enough
774 type/length info to allow scanners to jump quickly from one block
775 to the next without reading each byte in the block. */
777 /* Trace buffer management would be simple - advance a free pointer
778 from beginning to end, then stop - were it not for the circular
779 buffer option, which is a useful way to prevent a trace run from
780 stopping prematurely because the buffer filled up. In the circular
781 case, the location of the first trace frame (trace_buffer_start)
782 moves as old trace frames are discarded. Also, since we grow trace
783 frames incrementally as actions are performed, we wrap around to
784 the beginning of the trace buffer. This is per-block, so each
785 block within a trace frame remains contiguous. Things get messy
786 when the wrapped-around trace frame is the one being discarded; the
787 free space ends up in two parts at opposite ends of the buffer. */
790 # if defined(__GNUC__)
791 # define ATTR_PACKED __attribute__ ((packed))
793 # define ATTR_PACKED /* nothing */
797 /* The data collected at a tracepoint hit. This object should be as
798 small as possible, since there may be a great many of them. We do
799 not need to keep a frame number, because they are all sequential
800 and there are no deletions; so the Nth frame in the buffer is
801 always frame number N. */
805 /* Number of the tracepoint that collected this traceframe. A value
806 of 0 indicates the current end of the trace buffer. We make this
807 a 16-bit field because it's never going to happen that GDB's
808 numbering of tracepoints reaches 32,000. */
811 /* The size of the data in this trace frame. We limit this to 32
812 bits, even on a 64-bit target, because it's just implausible that
813 one is validly going to collect 4 gigabytes of data at a single
815 unsigned int data_size : 32;
817 /* The base of the trace data, which is contiguous from this point. */
818 unsigned char data[0];
822 /* The traceframe to be used as the source of data to send back to
823 GDB. A value of -1 means to get data from the live program. */
825 int current_traceframe = -1;
827 /* This flag is true if the trace buffer is circular, meaning that
828 when it fills, the oldest trace frames are discarded in order to
831 #ifndef IN_PROCESS_AGENT
832 static int circular_trace_buffer;
835 /* Pointer to the block of memory that traceframes all go into. */
837 static unsigned char *trace_buffer_lo;
839 /* Pointer to the end of the trace buffer, more precisely to the byte
840 after the end of the buffer. */
842 static unsigned char *trace_buffer_hi;
844 /* Control structure holding the read/write/etc. pointers into the
845 trace buffer. We need more than one of these to implement a
846 transaction-like mechanism to garantees that both GDBserver and the
847 in-process agent can try to change the trace buffer
850 struct trace_buffer_control
852 /* Pointer to the first trace frame in the buffer. In the
853 non-circular case, this is equal to trace_buffer_lo, otherwise it
854 moves around in the buffer. */
855 unsigned char *start;
857 /* Pointer to the free part of the trace buffer. Note that we clear
858 several bytes at and after this pointer, so that traceframe
859 scans/searches terminate properly. */
862 /* Pointer to the byte after the end of the free part. Note that
863 this may be smaller than trace_buffer_free in the circular case,
864 and means that the free part is in two pieces. Initially it is
865 equal to trace_buffer_hi, then is generally equivalent to
866 trace_buffer_start. */
867 unsigned char *end_free;
869 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
870 this is the point at which the trace data breaks, and resumes at
875 /* Same as above, to be used by GDBserver when updating the in-process
877 struct ipa_trace_buffer_control
886 /* We have possibly both GDBserver and an inferior thread accessing
887 the same IPA trace buffer memory. The IPA is the producer (tries
888 to put new frames in the buffer), while GDBserver occasionally
889 consumes them, that is, flushes the IPA's buffer into its own
890 buffer. Both sides need to update the trace buffer control
891 pointers (current head, tail, etc.). We can't use a global lock to
892 synchronize the accesses, as otherwise we could deadlock GDBserver
893 (if the thread holding the lock stops for a signal, say). So
894 instead of that, we use a transaction scheme where GDBserver writes
895 always prevail over the IPAs writes, and, we have the IPA detect
896 the commit failure/overwrite, and retry the whole attempt. This is
897 mainly implemented by having a global token object that represents
898 who wrote last to the buffer control structure. We need to freeze
899 any inferior writing to the buffer while GDBserver touches memory,
900 so that the inferior can correctly detect that GDBserver had been
901 there, otherwise, it could mistakingly think its commit was
902 successful; that's implemented by simply having GDBserver set a
903 breakpoint the inferior hits if it is the critical region.
905 There are three cycling trace buffer control structure copies
906 (buffer head, tail, etc.), with the token object including an index
907 indicating which is current live copy. The IPA tentatively builds
908 an updated copy in a non-current control structure, while GDBserver
909 always clobbers the current version directly. The IPA then tries
910 to atomically "commit" its version; if GDBserver clobbered the
911 structure meanwhile, that will fail, and the IPA restarts the
914 Listing the step in further detail, we have:
916 In-process agent (producer):
918 - passes by `about_to_request_buffer_space' breakpoint/lock
920 - reads current token, extracts current trace buffer control index,
921 and starts tentatively updating the rightmost one (0->1, 1->2,
922 2->0). Note that only one inferior thread is executing this code
923 at any given time, due to an outer lock in the jump pads.
925 - updates counters, and tries to commit the token.
927 - passes by second `about_to_request_buffer_space' breakpoint/lock,
928 leaving the sync region.
930 - checks if the update was effective.
932 - if trace buffer was found full, hits flush_trace_buffer
933 breakpoint, and restarts later afterwards.
935 GDBserver (consumer):
937 - sets `about_to_request_buffer_space' breakpoint/lock.
939 - updates the token unconditionally, using the current buffer
940 control index, since it knows that the IP agent always writes to
941 the rightmost, and due to the breakpoint, at most one IP thread
942 can try to update the trace buffer concurrently to GDBserver, so
943 there will be no danger of trace buffer control index wrap making
944 the IPA write to the same index as GDBserver.
946 - flushes the IP agent's trace buffer completely, and updates the
947 current trace buffer control structure. GDBserver *always* wins.
949 - removes the `about_to_request_buffer_space' breakpoint.
951 The token is stored in the `trace_buffer_ctrl_curr' variable.
952 Internally, it's bits are defined as:
954 |-------------+-----+-------------+--------+-------------+--------------|
955 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
956 |-------------+-----+-------------+--------+-------------+--------------|
957 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
958 |-------------+-----+-------------+--------+-------------+--------------|
960 GSB - GDBserver Stamp Bit
961 PC - Previous Counter
963 TBCI - Trace Buffer Control Index
966 An IPA update of `trace_buffer_ctrl_curr' does:
968 - read CC from the current token, save as PC.
970 - atomically tries to write PC+1,CC
972 A GDBserver update of `trace_buffer_ctrl_curr' does:
974 - reads PC and CC from the current token.
979 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
980 for the counters described below. The cleared bits are used to
981 hold the index of the items of the `trace_buffer_ctrl' array that
983 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
985 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
986 counter, and the `current' counter. */
988 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
989 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
991 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
992 always stamps this bit as set. */
993 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
995 #ifdef IN_PROCESS_AGENT
996 IP_AGENT_EXPORT struct trace_buffer_control trace_buffer_ctrl[3];
997 IP_AGENT_EXPORT unsigned int trace_buffer_ctrl_curr;
999 # define TRACE_BUFFER_CTRL_CURR \
1000 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1004 /* The GDBserver side agent only needs one instance of this object, as
1005 it doesn't need to sync with itself. Define it as array anyway so
1006 that the rest of the code base doesn't need to care for the
1008 struct trace_buffer_control trace_buffer_ctrl[1];
1009 # define TRACE_BUFFER_CTRL_CURR 0
1012 /* These are convenience macros used to access the current trace
1013 buffer control in effect. */
1014 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1015 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1016 #define trace_buffer_end_free \
1017 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1018 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1021 /* Macro that returns a pointer to the first traceframe in the buffer. */
1023 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1025 /* Macro that returns a pointer to the next traceframe in the buffer.
1026 If the computed location is beyond the wraparound point, subtract
1027 the offset of the wraparound. */
1029 #define NEXT_TRACEFRAME_1(TF) \
1030 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1032 #define NEXT_TRACEFRAME(TF) \
1033 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1034 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1035 ? (trace_buffer_wrap - trace_buffer_lo) \
1038 /* The difference between these counters represents the total number
1039 of complete traceframes present in the trace buffer. The IP agent
1040 writes to the write count, GDBserver writes to read count. */
1042 IP_AGENT_EXPORT unsigned int traceframe_write_count;
1043 IP_AGENT_EXPORT unsigned int traceframe_read_count;
1045 /* Convenience macro. */
1047 #define traceframe_count \
1048 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1050 /* The count of all traceframes created in the current run, including
1051 ones that were discarded to make room. */
1053 IP_AGENT_EXPORT int traceframes_created;
1055 #ifndef IN_PROCESS_AGENT
1057 /* Read-only regions are address ranges whose contents don't change,
1058 and so can be read from target memory even while looking at a trace
1059 frame. Without these, disassembly for instance will likely fail,
1060 because the program code is not usually collected into a trace
1061 frame. This data structure does not need to be very complicated or
1062 particularly efficient, it's only going to be used occasionally,
1063 and only by some commands. */
1065 struct readonly_region
1067 /* The bounds of the region. */
1068 CORE_ADDR start, end;
1070 /* Link to the next one. */
1071 struct readonly_region *next;
1074 /* Linked list of readonly regions. This list stays in effect from
1075 one tstart to the next. */
1077 static struct readonly_region *readonly_regions;
1081 /* The global that controls tracing overall. */
1083 IP_AGENT_EXPORT int tracing;
1085 #ifndef IN_PROCESS_AGENT
1087 /* Controls whether tracing should continue after GDB disconnects. */
1089 int disconnected_tracing;
1091 /* The reason for the last tracing run to have stopped. We initialize
1092 to a distinct string so that GDB can distinguish between "stopped
1093 after running" and "stopped because never run" cases. */
1095 static const char *tracing_stop_reason = "tnotrun";
1097 static int tracing_stop_tpnum;
1099 /* 64-bit timestamps for the trace run's start and finish, expressed
1100 in microseconds from the Unix epoch. */
1102 LONGEST tracing_start_time;
1103 LONGEST tracing_stop_time;
1105 /* The (optional) user-supplied name of the user that started the run.
1106 This is an arbitrary string, and may be NULL. */
1108 char *tracing_user_name;
1110 /* Optional user-supplied text describing the run. This is
1111 an arbitrary string, and may be NULL. */
1113 char *tracing_notes;
1115 /* Optional user-supplied text explaining a tstop command. This is an
1116 arbitrary string, and may be NULL. */
1118 char *tracing_stop_note;
1122 /* Functions local to this file. */
1124 /* Base "class" for tracepoint type specific data to be passed down to
1125 collect_data_at_tracepoint. */
1126 struct tracepoint_hit_ctx
1128 enum tracepoint_type type;
1131 #ifdef IN_PROCESS_AGENT
1133 /* Fast/jump tracepoint specific data to be passed down to
1134 collect_data_at_tracepoint. */
1135 struct fast_tracepoint_ctx
1137 struct tracepoint_hit_ctx base;
1139 struct regcache regcache;
1140 int regcache_initted;
1141 unsigned char *regspace;
1143 unsigned char *regs;
1144 struct tracepoint *tpoint;
1147 /* Static tracepoint specific data to be passed down to
1148 collect_data_at_tracepoint. */
1149 struct static_tracepoint_ctx
1151 struct tracepoint_hit_ctx base;
1153 /* The regcache corresponding to the registers state at the time of
1154 the tracepoint hit. Initialized lazily, from REGS. */
1155 struct regcache regcache;
1156 int regcache_initted;
1158 /* The buffer space REGCACHE above uses. We use a separate buffer
1159 instead of letting the regcache malloc for both signal safety and
1160 performance reasons; this is allocated on the stack instead. */
1161 unsigned char *regspace;
1163 /* The register buffer as passed on by lttng/ust. */
1164 struct registers *regs;
1166 /* The "printf" formatter and the args the user passed to the marker
1167 call. We use this to be able to collect "static trace data"
1172 /* The GDB tracepoint matching the probed marker that was "hit". */
1173 struct tracepoint *tpoint;
1178 /* Static tracepoint specific data to be passed down to
1179 collect_data_at_tracepoint. */
1180 struct trap_tracepoint_ctx
1182 struct tracepoint_hit_ctx base;
1184 struct regcache *regcache;
1189 static enum eval_result_type
1190 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx *ctx,
1191 struct traceframe *tframe,
1192 struct agent_expr *aexpr,
1195 #ifndef IN_PROCESS_AGENT
1196 static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1197 static int traceframe_read_tsv (int num, LONGEST *val);
1200 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1201 struct tracepoint *tpoint);
1203 #ifndef IN_PROCESS_AGENT
1204 static void clear_readonly_regions (void);
1205 static void clear_installed_tracepoints (void);
1208 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1210 struct tracepoint *tpoint);
1211 #ifndef IN_PROCESS_AGENT
1212 static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1214 struct tracepoint *tpoint, int current_step);
1215 static void compile_tracepoint_condition (struct tracepoint *tpoint,
1216 CORE_ADDR *jump_entry);
1218 static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1220 struct tracepoint *tpoint,
1221 struct traceframe *tframe,
1222 struct tracepoint_action *taction);
1224 #ifndef IN_PROCESS_AGENT
1225 static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1227 static void install_tracepoint (struct tracepoint *, char *own_buf);
1228 static void download_tracepoint (struct tracepoint *);
1229 static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
1232 static LONGEST get_timestamp (void);
1234 #if defined(__GNUC__)
1235 # define memory_barrier() asm volatile ("" : : : "memory")
1237 # define memory_barrier() do {} while (0)
1240 /* We only build the IPA if this builtin is supported, and there are
1241 no uses of this in GDBserver itself, so we're safe in defining this
1243 #define cmpxchg(mem, oldval, newval) \
1244 __sync_val_compare_and_swap (mem, oldval, newval)
1246 /* Record that an error occurred during expression evaluation. */
1249 record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1250 enum eval_result_type rtype)
1252 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1253 tpoint->number, paddress (tpoint->address), which, rtype);
1255 #ifdef IN_PROCESS_AGENT
1256 /* Only record the first error we get. */
1257 if (cmpxchg (&expr_eval_result,
1259 rtype) != expr_eval_no_error)
1262 if (expr_eval_result != expr_eval_no_error)
1266 error_tracepoint = tpoint;
1269 /* Trace buffer management. */
1272 clear_trace_buffer (void)
1274 trace_buffer_start = trace_buffer_lo;
1275 trace_buffer_free = trace_buffer_lo;
1276 trace_buffer_end_free = trace_buffer_hi;
1277 trace_buffer_wrap = trace_buffer_hi;
1278 /* A traceframe with zeroed fields marks the end of trace data. */
1279 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1280 ((struct traceframe *) trace_buffer_free)->data_size = 0;
1281 traceframe_read_count = traceframe_write_count = 0;
1282 traceframes_created = 0;
1285 #ifndef IN_PROCESS_AGENT
1288 clear_inferior_trace_buffer (void)
1290 CORE_ADDR ipa_trace_buffer_lo;
1291 CORE_ADDR ipa_trace_buffer_hi;
1292 struct traceframe ipa_traceframe = { 0 };
1293 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1295 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1296 &ipa_trace_buffer_lo);
1297 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1298 &ipa_trace_buffer_hi);
1300 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1301 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1302 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1303 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1305 /* A traceframe with zeroed fields marks the end of trace data. */
1306 write_inferior_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1307 (unsigned char *) &ipa_trace_buffer_ctrl,
1308 sizeof (ipa_trace_buffer_ctrl));
1310 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1312 /* A traceframe with zeroed fields marks the end of trace data. */
1313 write_inferior_memory (ipa_trace_buffer_lo,
1314 (unsigned char *) &ipa_traceframe,
1315 sizeof (ipa_traceframe));
1317 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1318 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1319 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1325 init_trace_buffer (unsigned char *buf, int bufsize)
1327 trace_buffer_lo = buf;
1328 trace_buffer_hi = trace_buffer_lo + bufsize;
1330 clear_trace_buffer ();
1333 #ifdef IN_PROCESS_AGENT
1335 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
1336 about_to_request_buffer_space (void)
1338 /* GDBserver places breakpoint here while it goes about to flush
1339 data at random times. */
1340 UNKNOWN_SIDE_EFFECTS();
1345 /* Carve out a piece of the trace buffer, returning NULL in case of
1349 trace_buffer_alloc (size_t amt)
1351 unsigned char *rslt;
1352 struct trace_buffer_control *tbctrl;
1354 #ifdef IN_PROCESS_AGENT
1355 unsigned int prev, prev_filtered;
1356 unsigned int commit_count;
1357 unsigned int commit;
1358 unsigned int readout;
1360 struct traceframe *oldest;
1361 unsigned char *new_start;
1364 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1365 (long) amt, (long) sizeof (struct traceframe));
1367 /* Account for the EOB marker. */
1368 amt += sizeof (struct traceframe);
1370 #ifdef IN_PROCESS_AGENT
1374 /* Read the current token and extract the index to try to write to,
1375 storing it in CURR. */
1376 prev = trace_buffer_ctrl_curr;
1377 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1378 curr = prev_filtered + 1;
1382 about_to_request_buffer_space ();
1384 /* Start out with a copy of the current state. GDBserver may be
1385 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1386 be able to commit anyway if that happens. */
1387 trace_buffer_ctrl[curr]
1388 = trace_buffer_ctrl[prev_filtered];
1389 trace_debug ("trying curr=%u", curr);
1391 /* The GDBserver's agent doesn't need all that syncing, and always
1392 updates TCB 0 (there's only one, mind you). */
1395 tbctrl = &trace_buffer_ctrl[curr];
1397 /* Offsets are easier to grok for debugging than raw addresses,
1398 especially for the small trace buffer sizes that are useful for
1400 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1402 (int) (tbctrl->start - trace_buffer_lo),
1403 (int) (tbctrl->free - trace_buffer_lo),
1404 (int) (tbctrl->end_free - trace_buffer_lo),
1405 (int) (tbctrl->wrap - trace_buffer_lo),
1406 (int) (trace_buffer_hi - trace_buffer_lo));
1408 /* The algorithm here is to keep trying to get a contiguous block of
1409 the requested size, possibly discarding older traceframes to free
1410 up space. Since free space might come in one or two pieces,
1411 depending on whether discarded traceframes wrapped around at the
1412 high end of the buffer, we test both pieces after each
1416 /* First, if we have two free parts, try the upper one first. */
1417 if (tbctrl->end_free < tbctrl->free)
1419 if (tbctrl->free + amt <= trace_buffer_hi)
1420 /* We have enough in the upper part. */
1424 /* Our high part of free space wasn't enough. Give up
1425 on it for now, set wraparound. We will recover the
1426 space later, if/when the wrapped-around traceframe is
1428 trace_debug ("Upper part too small, setting wraparound");
1429 tbctrl->wrap = tbctrl->free;
1430 tbctrl->free = trace_buffer_lo;
1434 /* The normal case. */
1435 if (tbctrl->free + amt <= tbctrl->end_free)
1438 #ifdef IN_PROCESS_AGENT
1439 /* The IP Agent's buffer is always circular. It isn't used
1440 currently, but `circular_trace_buffer' could represent
1441 GDBserver's mode. If we didn't find space, ask GDBserver to
1444 flush_trace_buffer ();
1448 trace_debug ("gdbserver flushed buffer, retrying");
1452 /* GDBserver cancelled the tracing. Bail out as well. */
1455 /* If we're here, then neither part is big enough, and
1456 non-circular trace buffers are now full. */
1457 if (!circular_trace_buffer)
1459 trace_debug ("Not enough space in the trace buffer");
1463 trace_debug ("Need more space in the trace buffer");
1465 /* If we have a circular buffer, we can try discarding the
1466 oldest traceframe and see if that helps. */
1467 oldest = FIRST_TRACEFRAME ();
1468 if (oldest->tpnum == 0)
1470 /* Not good; we have no traceframes to free. Perhaps we're
1471 asking for a block that is larger than the buffer? In
1472 any case, give up. */
1473 trace_debug ("No traceframes to discard");
1477 /* We don't run this code in the in-process agent currently.
1478 E.g., we could leave the in-process agent in autonomous
1479 circular mode if we only have fast tracepoints. If we do
1480 that, then this bit becomes racy with GDBserver, which also
1481 writes to this counter. */
1482 --traceframe_write_count;
1484 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1485 /* If we freed the traceframe that wrapped around, go back
1486 to the non-wrap case. */
1487 if (new_start < tbctrl->start)
1489 trace_debug ("Discarding past the wraparound");
1490 tbctrl->wrap = trace_buffer_hi;
1492 tbctrl->start = new_start;
1493 tbctrl->end_free = tbctrl->start;
1495 trace_debug ("Discarded a traceframe\n"
1496 "Trace buffer [%d], start=%d free=%d "
1497 "endfree=%d wrap=%d hi=%d",
1499 (int) (tbctrl->start - trace_buffer_lo),
1500 (int) (tbctrl->free - trace_buffer_lo),
1501 (int) (tbctrl->end_free - trace_buffer_lo),
1502 (int) (tbctrl->wrap - trace_buffer_lo),
1503 (int) (trace_buffer_hi - trace_buffer_lo));
1505 /* Now go back around the loop. The discard might have resulted
1506 in either one or two pieces of free space, so we want to try
1507 both before freeing any more traceframes. */
1511 /* If we get here, we know we can provide the asked-for space. */
1513 rslt = tbctrl->free;
1515 /* Adjust the request back down, now that we know we have space for
1516 the marker, but don't commit to AMT yet, we may still need to
1517 restart the operation if GDBserver touches the trace buffer
1518 (obviously only important in the in-process agent's version). */
1519 tbctrl->free += (amt - sizeof (struct traceframe));
1521 /* Or not. If GDBserver changed the trace buffer behind our back,
1522 we get to restart a new allocation attempt. */
1524 #ifdef IN_PROCESS_AGENT
1525 /* Build the tentative token. */
1526 commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100)
1527 & GDBSERVER_FLUSH_COUNT_MASK_CURR);
1528 commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12)
1532 /* Try to commit it. */
1533 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1534 if (readout != prev)
1536 trace_debug ("GDBserver has touched the trace buffer, restarting."
1537 " (prev=%08x, commit=%08x, readout=%08x)",
1538 prev, commit, readout);
1542 /* Hold your horses here. Even if that change was committed,
1543 GDBserver could come in, and clobber it. We need to hold to be
1544 able to tell if GDBserver clobbers before or after we committed
1545 the change. Whenever GDBserver goes about touching the IPA
1546 buffer, it sets a breakpoint in this routine, so we have a sync
1548 about_to_request_buffer_space ();
1550 /* Check if the change has been effective, even if GDBserver stopped
1551 us at the breakpoint. */
1554 unsigned int refetch;
1558 refetch = trace_buffer_ctrl_curr;
1560 if (refetch == commit
1561 || ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
1564 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1565 "readout=%08x, refetch=%08x)",
1566 prev, commit, readout, refetch);
1570 trace_debug ("GDBserver has touched the trace buffer, not effective."
1571 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1572 prev, commit, readout, refetch);
1578 /* We have a new piece of the trace buffer. Hurray! */
1580 /* Add an EOB marker just past this allocation. */
1581 ((struct traceframe *) tbctrl->free)->tpnum = 0;
1582 ((struct traceframe *) tbctrl->free)->data_size = 0;
1584 /* Adjust the request back down, now that we know we have space for
1586 amt -= sizeof (struct traceframe);
1590 trace_debug ("Allocated %d bytes", (int) amt);
1591 trace_debug ("Trace buffer [%d] start=%d free=%d "
1592 "endfree=%d wrap=%d hi=%d",
1594 (int) (tbctrl->start - trace_buffer_lo),
1595 (int) (tbctrl->free - trace_buffer_lo),
1596 (int) (tbctrl->end_free - trace_buffer_lo),
1597 (int) (tbctrl->wrap - trace_buffer_lo),
1598 (int) (trace_buffer_hi - trace_buffer_lo));
1604 #ifndef IN_PROCESS_AGENT
1606 /* Return the total free space. This is not necessarily the largest
1607 block we can allocate, because of the two-part case. */
1612 if (trace_buffer_free <= trace_buffer_end_free)
1613 return trace_buffer_end_free - trace_buffer_free;
1615 return ((trace_buffer_end_free - trace_buffer_lo)
1616 + (trace_buffer_hi - trace_buffer_free));
1619 /* An 'S' in continuation packets indicates remainder are for
1622 static int seen_step_action_flag;
1624 /* Create a tracepoint (location) with given number and address. Add this
1625 new tracepoint to list and sort this list. */
1627 static struct tracepoint *
1628 add_tracepoint (int num, CORE_ADDR addr)
1630 struct tracepoint *tpoint, **tp_next;
1632 tpoint = xmalloc (sizeof (struct tracepoint));
1633 tpoint->number = num;
1634 tpoint->address = addr;
1635 tpoint->numactions = 0;
1636 tpoint->actions = NULL;
1637 tpoint->actions_str = NULL;
1638 tpoint->cond = NULL;
1639 tpoint->num_step_actions = 0;
1640 tpoint->step_actions = NULL;
1641 tpoint->step_actions_str = NULL;
1642 /* Start all off as regular (slow) tracepoints. */
1643 tpoint->type = trap_tracepoint;
1644 tpoint->orig_size = -1;
1645 tpoint->source_strings = NULL;
1646 tpoint->compiled_cond = 0;
1647 tpoint->handle = NULL;
1648 tpoint->next = NULL;
1650 /* Find a place to insert this tracepoint into list in order to keep
1651 the tracepoint list still in the ascending order. There may be
1652 multiple tracepoints at the same address as TPOINT's, and this
1653 guarantees TPOINT is inserted after all the tracepoints which are
1654 set at the same address. For example, fast tracepoints A, B, C are
1655 set at the same address, and D is to be insert at the same place as
1658 -->| A |--> | B |-->| C |->...
1660 One jump pad was created for tracepoint A, B, and C, and the target
1661 address of A is referenced/used in jump pad. So jump pad will let
1662 inferior jump to A. If D is inserted in front of A, like this,
1664 -->| D |-->| A |--> | B |-->| C |->...
1666 without updating jump pad, D is not reachable during collect, which
1667 is wrong. As we can see, the order of B, C and D doesn't matter, but
1668 A should always be the `first' one. */
1669 for (tp_next = &tracepoints;
1670 (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
1671 tp_next = &(*tp_next)->next)
1673 tpoint->next = *tp_next;
1675 last_tracepoint = tpoint;
1677 seen_step_action_flag = 0;
1682 #ifndef IN_PROCESS_AGENT
1684 /* Return the tracepoint with the given number and address, or NULL. */
1686 static struct tracepoint *
1687 find_tracepoint (int id, CORE_ADDR addr)
1689 struct tracepoint *tpoint;
1691 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1692 if (tpoint->number == id && tpoint->address == addr)
1698 /* There may be several tracepoints with the same number (because they
1699 are "locations", in GDB parlance); return the next one after the
1700 given tracepoint, or search from the beginning of the list if the
1701 first argument is NULL. */
1703 static struct tracepoint *
1704 find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1706 struct tracepoint *tpoint;
1709 tpoint = prev_tp->next;
1711 tpoint = tracepoints;
1712 for (; tpoint; tpoint = tpoint->next)
1713 if (tpoint->number == num)
1722 save_string (const char *str, size_t len)
1726 s = xmalloc (len + 1);
1727 memcpy (s, str, len);
1733 /* Append another action to perform when the tracepoint triggers. */
1736 add_tracepoint_action (struct tracepoint *tpoint, char *packet)
1742 seen_step_action_flag = 1;
1750 char *act_start = act;
1751 struct tracepoint_action *action = NULL;
1757 struct collect_memory_action *maction;
1761 maction = xmalloc (sizeof *maction);
1762 maction->base.type = *act;
1763 action = &maction->base;
1766 is_neg = (*act == '-');
1769 act = unpack_varlen_hex (act, &basereg);
1771 act = unpack_varlen_hex (act, &maction->addr);
1773 act = unpack_varlen_hex (act, &maction->len);
1774 maction->basereg = (is_neg
1777 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1778 pulongest (maction->len),
1779 paddress (maction->addr), maction->basereg);
1784 struct collect_registers_action *raction;
1786 raction = xmalloc (sizeof *raction);
1787 raction->base.type = *act;
1788 action = &raction->base;
1790 trace_debug ("Want to collect registers");
1792 /* skip past hex digits of mask for now */
1793 while (isxdigit(*act))
1799 struct collect_static_trace_data_action *raction;
1801 raction = xmalloc (sizeof *raction);
1802 raction->base.type = *act;
1803 action = &raction->base;
1805 trace_debug ("Want to collect static trace data");
1810 trace_debug ("Unexpected step action, ignoring");
1815 struct eval_expr_action *xaction;
1817 xaction = xmalloc (sizeof (*xaction));
1818 xaction->base.type = *act;
1819 action = &xaction->base;
1821 trace_debug ("Want to evaluate expression");
1822 xaction->expr = gdb_parse_agent_expr (&act);
1826 trace_debug ("unknown trace action '%c', ignoring...", *act);
1835 if (seen_step_action_flag)
1837 tpoint->num_step_actions++;
1839 tpoint->step_actions
1840 = xrealloc (tpoint->step_actions,
1841 (sizeof (*tpoint->step_actions)
1842 * tpoint->num_step_actions));
1843 tpoint->step_actions_str
1844 = xrealloc (tpoint->step_actions_str,
1845 (sizeof (*tpoint->step_actions_str)
1846 * tpoint->num_step_actions));
1847 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
1848 tpoint->step_actions_str[tpoint->num_step_actions - 1]
1849 = save_string (act_start, act - act_start);
1853 tpoint->numactions++;
1855 = xrealloc (tpoint->actions,
1856 sizeof (*tpoint->actions) * tpoint->numactions);
1858 = xrealloc (tpoint->actions_str,
1859 sizeof (*tpoint->actions_str) * tpoint->numactions);
1860 tpoint->actions[tpoint->numactions - 1] = action;
1861 tpoint->actions_str[tpoint->numactions - 1]
1862 = save_string (act_start, act - act_start);
1869 /* Find or create a trace state variable with the given number. */
1871 static struct trace_state_variable *
1872 get_trace_state_variable (int num)
1874 struct trace_state_variable *tsv;
1876 #ifdef IN_PROCESS_AGENT
1877 /* Search for an existing variable. */
1878 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
1879 if (tsv->number == num)
1883 /* Search for an existing variable. */
1884 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1885 if (tsv->number == num)
1891 /* Find or create a trace state variable with the given number. */
1893 static struct trace_state_variable *
1894 create_trace_state_variable (int num, int gdb)
1896 struct trace_state_variable *tsv;
1898 tsv = get_trace_state_variable (num);
1902 /* Create a new variable. */
1903 tsv = xmalloc (sizeof (struct trace_state_variable));
1905 tsv->initial_value = 0;
1909 #ifdef IN_PROCESS_AGENT
1912 tsv->next = alloced_trace_state_variables;
1913 alloced_trace_state_variables = tsv;
1918 tsv->next = trace_state_variables;
1919 trace_state_variables = tsv;
1924 IP_AGENT_EXPORT LONGEST
1925 get_trace_state_variable_value (int num)
1927 struct trace_state_variable *tsv;
1929 tsv = get_trace_state_variable (num);
1933 trace_debug ("No trace state variable %d, skipping value get", num);
1937 /* Call a getter function if we have one. While it's tempting to
1938 set up something to only call the getter once per tracepoint hit,
1939 it could run afoul of thread races. Better to let the getter
1940 handle it directly, if necessary to worry about it. */
1942 tsv->value = (tsv->getter) ();
1944 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1945 num, plongest (tsv->value));
1950 IP_AGENT_EXPORT void
1951 set_trace_state_variable_value (int num, LONGEST val)
1953 struct trace_state_variable *tsv;
1955 tsv = get_trace_state_variable (num);
1959 trace_debug ("No trace state variable %d, skipping value set", num);
1967 agent_get_trace_state_variable_value (int num)
1969 return get_trace_state_variable_value (num);
1973 agent_set_trace_state_variable_value (int num, LONGEST val)
1975 set_trace_state_variable_value (num, val);
1979 set_trace_state_variable_name (int num, const char *name)
1981 struct trace_state_variable *tsv;
1983 tsv = get_trace_state_variable (num);
1987 trace_debug ("No trace state variable %d, skipping name set", num);
1991 tsv->name = (char *) name;
1995 set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
1997 struct trace_state_variable *tsv;
1999 tsv = get_trace_state_variable (num);
2003 trace_debug ("No trace state variable %d, skipping getter set", num);
2007 tsv->getter = getter;
2010 /* Add a raw traceframe for the given tracepoint. */
2012 static struct traceframe *
2013 add_traceframe (struct tracepoint *tpoint)
2015 struct traceframe *tframe;
2017 tframe = trace_buffer_alloc (sizeof (struct traceframe));
2022 tframe->tpnum = tpoint->number;
2023 tframe->data_size = 0;
2028 /* Add a block to the traceframe currently being worked on. */
2030 static unsigned char *
2031 add_traceframe_block (struct traceframe *tframe, int amt)
2033 unsigned char *block;
2038 block = trace_buffer_alloc (amt);
2043 tframe->data_size += amt;
2048 /* Flag that the current traceframe is finished. */
2051 finish_traceframe (struct traceframe *tframe)
2053 ++traceframe_write_count;
2054 ++traceframes_created;
2057 #ifndef IN_PROCESS_AGENT
2059 /* Given a traceframe number NUM, find the NUMth traceframe in the
2062 static struct traceframe *
2063 find_traceframe (int num)
2065 struct traceframe *tframe;
2068 for (tframe = FIRST_TRACEFRAME ();
2070 tframe = NEXT_TRACEFRAME (tframe))
2081 get_traceframe_address (struct traceframe *tframe)
2084 struct tracepoint *tpoint;
2086 addr = traceframe_get_pc (tframe);
2091 /* Fallback strategy, will be incorrect for while-stepping frames
2092 and multi-location tracepoints. */
2093 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2094 return tpoint->address;
2097 /* Search for the next traceframe whose address is inside or outside
2100 static struct traceframe *
2101 find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2104 struct traceframe *tframe;
2107 *tfnump = current_traceframe + 1;
2108 tframe = find_traceframe (*tfnump);
2109 /* The search is not supposed to wrap around. */
2116 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2118 tfaddr = get_traceframe_address (tframe);
2120 ? (lo <= tfaddr && tfaddr <= hi)
2121 : (lo > tfaddr || tfaddr > hi))
2130 /* Search for the next traceframe recorded by the given tracepoint.
2131 Note that for multi-location tracepoints, this will find whatever
2132 location appears first. */
2134 static struct traceframe *
2135 find_next_traceframe_by_tracepoint (int num, int *tfnump)
2137 struct traceframe *tframe;
2139 *tfnump = current_traceframe + 1;
2140 tframe = find_traceframe (*tfnump);
2141 /* The search is not supposed to wrap around. */
2148 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2150 if (tframe->tpnum == num)
2161 #ifndef IN_PROCESS_AGENT
2163 /* Clear all past trace state. */
2166 cmd_qtinit (char *packet)
2168 struct trace_state_variable *tsv, *prev, *next;
2170 /* Make sure we don't try to read from a trace frame. */
2171 current_traceframe = -1;
2173 trace_debug ("Initializing the trace");
2175 clear_installed_tracepoints ();
2176 clear_readonly_regions ();
2179 last_tracepoint = NULL;
2181 /* Clear out any leftover trace state variables. Ones with target
2182 defined getters should be kept however. */
2184 tsv = trace_state_variables;
2187 trace_debug ("Looking at var %d", tsv->number);
2188 if (tsv->getter == NULL)
2194 trace_state_variables = next;
2195 trace_debug ("Deleting var %d", tsv->number);
2206 clear_trace_buffer ();
2207 clear_inferior_trace_buffer ();
2212 /* Unprobe the UST marker at ADDRESS. */
2215 unprobe_marker_at (CORE_ADDR address)
2217 char cmd[IPA_CMD_BUF_SIZE];
2219 sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
2220 run_inferior_command (cmd);
2223 /* Restore the program to its pre-tracing state. This routine may be called
2224 in error situations, so it needs to be careful about only restoring
2225 from known-valid bits. */
2228 clear_installed_tracepoints (void)
2230 struct tracepoint *tpoint;
2231 struct tracepoint *prev_stpoint;
2234 cancel_breakpoints ();
2236 prev_stpoint = NULL;
2238 /* Restore any bytes overwritten by tracepoints. */
2239 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2241 /* Catch the case where we might try to remove a tracepoint that
2242 was never actually installed. */
2243 if (tpoint->handle == NULL)
2245 trace_debug ("Tracepoint %d at 0x%s was "
2246 "never installed, nothing to clear",
2247 tpoint->number, paddress (tpoint->address));
2251 switch (tpoint->type)
2253 case trap_tracepoint:
2254 delete_breakpoint (tpoint->handle);
2256 case fast_tracepoint:
2257 delete_fast_tracepoint_jump (tpoint->handle);
2259 case static_tracepoint:
2260 if (prev_stpoint != NULL
2261 && prev_stpoint->address == tpoint->address)
2262 /* Nothing to do. We already unprobed a tracepoint set at
2263 this marker address (and there can only be one probe
2268 unprobe_marker_at (tpoint->address);
2269 prev_stpoint = tpoint;
2274 tpoint->handle = NULL;
2280 /* Parse a packet that defines a tracepoint. */
2283 cmd_qtdp (char *own_buf)
2286 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2287 int trail_hyphen = 0;
2291 struct tracepoint *tpoint;
2293 char *packet = own_buf;
2295 packet += strlen ("QTDP:");
2297 /* A hyphen at the beginning marks a packet specifying actions for a
2298 tracepoint already supplied. */
2305 packet = unpack_varlen_hex (packet, &num);
2306 ++packet; /* skip a colon */
2307 packet = unpack_varlen_hex (packet, &addr);
2308 ++packet; /* skip a colon */
2310 /* See if we already have this tracepoint. */
2311 tpoint = find_tracepoint (num, addr);
2315 /* Duplicate tracepoints are never allowed. */
2318 trace_debug ("Tracepoint error: tracepoint %d"
2319 " at 0x%s already exists",
2320 (int) num, paddress (addr));
2321 write_enn (own_buf);
2325 tpoint = add_tracepoint (num, addr);
2327 tpoint->enabled = (*packet == 'E');
2328 ++packet; /* skip 'E' */
2329 ++packet; /* skip a colon */
2330 packet = unpack_varlen_hex (packet, &count);
2331 tpoint->step_count = count;
2332 ++packet; /* skip a colon */
2333 packet = unpack_varlen_hex (packet, &count);
2334 tpoint->pass_count = count;
2335 /* See if we have any of the additional optional fields. */
2336 while (*packet == ':')
2341 tpoint->type = fast_tracepoint;
2343 packet = unpack_varlen_hex (packet, &count);
2344 tpoint->orig_size = count;
2346 else if (*packet == 'S')
2348 tpoint->type = static_tracepoint;
2351 else if (*packet == 'X')
2353 actparm = (char *) packet;
2354 tpoint->cond = gdb_parse_agent_expr (&actparm);
2357 else if (*packet == '-')
2359 else if (*packet == '\0')
2362 trace_debug ("Unknown optional tracepoint field");
2367 trace_debug ("Also has actions\n");
2370 trace_debug ("Defined %stracepoint %d at 0x%s, "
2371 "enabled %d step %ld pass %ld",
2372 tpoint->type == fast_tracepoint ? "fast "
2373 : tpoint->type == static_tracepoint ? "static " : "",
2374 tpoint->number, paddress (tpoint->address), tpoint->enabled,
2375 tpoint->step_count, tpoint->pass_count);
2378 add_tracepoint_action (tpoint, packet);
2381 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2382 (int) num, paddress (addr));
2383 write_enn (own_buf);
2387 /* Install tracepoint during tracing only once for each tracepoint location.
2388 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2389 determine the last QTDP packet for one tracepoint location by checking
2390 trailing hyphen in QTDP packet. */
2391 if (tracing && !trail_hyphen)
2393 /* Pause all threads temporarily while we patch tracepoints. */
2396 /* download_tracepoint will update global `tracepoints'
2397 list, so it is unsafe to leave threads in jump pad. */
2398 stabilize_threads ();
2400 /* Freeze threads. */
2403 download_tracepoint (tpoint);
2404 install_tracepoint (tpoint, own_buf);
2414 cmd_qtdpsrc (char *own_buf)
2416 ULONGEST num, addr, start, slen;
2417 struct tracepoint *tpoint;
2418 char *packet = own_buf;
2419 char *saved, *srctype, *src;
2421 struct source_string *last, *newlast;
2423 packet += strlen ("QTDPsrc:");
2425 packet = unpack_varlen_hex (packet, &num);
2426 ++packet; /* skip a colon */
2427 packet = unpack_varlen_hex (packet, &addr);
2428 ++packet; /* skip a colon */
2430 /* See if we already have this tracepoint. */
2431 tpoint = find_tracepoint (num, addr);
2435 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2436 (int) num, paddress (addr));
2437 write_enn (own_buf);
2442 packet = strchr (packet, ':');
2443 srctype = xmalloc (packet - saved + 1);
2444 memcpy (srctype, saved, packet - saved);
2445 srctype[packet - saved] = '\0';
2447 packet = unpack_varlen_hex (packet, &start);
2448 ++packet; /* skip a colon */
2449 packet = unpack_varlen_hex (packet, &slen);
2450 ++packet; /* skip a colon */
2451 src = xmalloc (slen + 1);
2452 nbytes = unhexify (src, packet, strlen (packet) / 2);
2455 newlast = xmalloc (sizeof (struct source_string));
2456 newlast->type = srctype;
2458 newlast->next = NULL;
2459 /* Always add a source string to the end of the list;
2460 this keeps sequences of actions/commands in the right
2462 if (tpoint->source_strings)
2464 for (last = tpoint->source_strings; last->next; last = last->next)
2466 last->next = newlast;
2469 tpoint->source_strings = newlast;
2475 cmd_qtdv (char *own_buf)
2477 ULONGEST num, val, builtin;
2480 struct trace_state_variable *tsv;
2481 char *packet = own_buf;
2483 packet += strlen ("QTDV:");
2485 packet = unpack_varlen_hex (packet, &num);
2486 ++packet; /* skip a colon */
2487 packet = unpack_varlen_hex (packet, &val);
2488 ++packet; /* skip a colon */
2489 packet = unpack_varlen_hex (packet, &builtin);
2490 ++packet; /* skip a colon */
2492 nbytes = strlen (packet) / 2;
2493 varname = xmalloc (nbytes + 1);
2494 nbytes = unhexify (varname, packet, nbytes);
2495 varname[nbytes] = '\0';
2497 tsv = create_trace_state_variable (num, 1);
2498 tsv->initial_value = (LONGEST) val;
2499 tsv->name = varname;
2501 set_trace_state_variable_value (num, (LONGEST) val);
2507 cmd_qtenable_disable (char *own_buf, int enable)
2509 char *packet = own_buf;
2511 struct tracepoint *tp;
2513 packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2514 packet = unpack_varlen_hex (packet, &num);
2515 ++packet; /* skip a colon */
2516 packet = unpack_varlen_hex (packet, &addr);
2518 tp = find_tracepoint (num, addr);
2522 if ((enable && tp->enabled) || (!enable && !tp->enabled))
2524 trace_debug ("Tracepoint %d at 0x%s is already %s",
2525 (int) num, paddress (addr),
2526 enable ? "enabled" : "disabled");
2531 trace_debug ("%s tracepoint %d at 0x%s",
2532 enable ? "Enabling" : "Disabling",
2533 (int) num, paddress (addr));
2535 tp->enabled = enable;
2537 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2540 int offset = offsetof (struct tracepoint, enabled);
2541 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2543 ret = prepare_to_access_memory ();
2546 trace_debug ("Failed to temporarily stop inferior threads");
2547 write_enn (own_buf);
2551 ret = write_inferior_integer (obj_addr, enable);
2552 done_accessing_memory ();
2556 trace_debug ("Cannot write enabled flag into "
2557 "inferior process memory");
2558 write_enn (own_buf);
2567 trace_debug ("Tracepoint %d at 0x%s not found",
2568 (int) num, paddress (addr));
2569 write_enn (own_buf);
2574 cmd_qtv (char *own_buf)
2579 char *packet = own_buf;
2581 packet += strlen ("qTV:");
2582 unpack_varlen_hex (packet, &num);
2584 if (current_traceframe >= 0)
2586 err = traceframe_read_tsv ((int) num, &val);
2589 strcpy (own_buf, "U");
2593 /* Only make tsv's be undefined before the first trace run. After a
2594 trace run is over, the user might want to see the last value of
2595 the tsv, and it might not be available in a traceframe. */
2596 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2598 strcpy (own_buf, "U");
2602 val = get_trace_state_variable_value (num);
2604 sprintf (own_buf, "V%s", phex_nz (val, 0));
2607 /* Clear out the list of readonly regions. */
2610 clear_readonly_regions (void)
2612 struct readonly_region *roreg;
2614 while (readonly_regions)
2616 roreg = readonly_regions;
2617 readonly_regions = readonly_regions->next;
2622 /* Parse the collection of address ranges whose contents GDB believes
2623 to be unchanging and so can be read directly from target memory
2624 even while looking at a traceframe. */
2627 cmd_qtro (char *own_buf)
2629 ULONGEST start, end;
2630 struct readonly_region *roreg;
2631 char *packet = own_buf;
2633 trace_debug ("Want to mark readonly regions");
2635 clear_readonly_regions ();
2637 packet += strlen ("QTro");
2639 while (*packet == ':')
2641 ++packet; /* skip a colon */
2642 packet = unpack_varlen_hex (packet, &start);
2643 ++packet; /* skip a comma */
2644 packet = unpack_varlen_hex (packet, &end);
2645 roreg = xmalloc (sizeof (struct readonly_region));
2646 roreg->start = start;
2648 roreg->next = readonly_regions;
2649 readonly_regions = roreg;
2650 trace_debug ("Added readonly region from 0x%s to 0x%s",
2651 paddress (roreg->start), paddress (roreg->end));
2657 /* Test to see if the given range is in our list of readonly ranges.
2658 We only test for being entirely within a range, GDB is not going to
2659 send a single memory packet that spans multiple regions. */
2662 in_readonly_region (CORE_ADDR addr, ULONGEST length)
2664 struct readonly_region *roreg;
2666 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2667 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2673 /* The maximum size of a jump pad entry. */
2674 static const int max_jump_pad_size = 0x100;
2676 static CORE_ADDR gdb_jump_pad_head;
2678 /* Return the address of the next free jump space. */
2681 get_jump_space_head (void)
2683 if (gdb_jump_pad_head == 0)
2685 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2686 &gdb_jump_pad_head))
2687 fatal ("error extracting jump_pad_buffer");
2690 return gdb_jump_pad_head;
2693 /* Reserve USED bytes from the jump space. */
2696 claim_jump_space (ULONGEST used)
2698 trace_debug ("claim_jump_space reserves %s bytes at %s",
2699 pulongest (used), paddress (gdb_jump_pad_head));
2700 gdb_jump_pad_head += used;
2703 static CORE_ADDR trampoline_buffer_head = 0;
2704 static CORE_ADDR trampoline_buffer_tail;
2706 /* Reserve USED bytes from the trampoline buffer and return the
2707 address of the start of the reserved space in TRAMPOLINE. Returns
2708 non-zero if the space is successfully claimed. */
2711 claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
2713 if (!trampoline_buffer_head)
2715 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
2716 &trampoline_buffer_tail))
2718 fatal ("error extracting trampoline_buffer");
2722 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2723 &trampoline_buffer_head))
2725 fatal ("error extracting trampoline_buffer_end");
2730 /* Start claiming space from the top of the trampoline space. If
2731 the space is located at the bottom of the virtual address space,
2732 this reduces the possibility that corruption will occur if a null
2733 pointer is used to write to memory. */
2734 if (trampoline_buffer_head - trampoline_buffer_tail < used)
2736 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2741 trampoline_buffer_head -= used;
2743 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2744 pulongest (used), paddress (trampoline_buffer_head));
2746 *trampoline = trampoline_buffer_head;
2750 /* Returns non-zero if there is space allocated for use in trampolines
2751 for fast tracepoints. */
2754 have_fast_tracepoint_trampoline_buffer (char *buf)
2756 CORE_ADDR trampoline_end, errbuf;
2758 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2761 fatal ("error extracting trampoline_buffer_end");
2768 strcpy (buf, "was claiming");
2769 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
2772 fatal ("error extracting errbuf");
2776 read_inferior_memory (errbuf, (unsigned char *) buf, 100);
2779 return trampoline_end != 0;
2782 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2783 the command fails, or 0 otherwise. If the command ran
2784 successfully, but probing the marker failed, ERROUT will be filled
2785 with the error to reply to GDB, and -1 is also returned. This
2786 allows directly passing IPA errors to GDB. */
2789 probe_marker_at (CORE_ADDR address, char *errout)
2791 char cmd[IPA_CMD_BUF_SIZE];
2794 sprintf (cmd, "probe_marker_at:%s", paddress (address));
2795 err = run_inferior_command (cmd);
2801 strcpy (errout, cmd);
2810 clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
2812 to->jump_pad = from->jump_pad;
2813 to->jump_pad_end = from->jump_pad_end;
2814 to->trampoline = from->trampoline;
2815 to->trampoline_end = from->trampoline_end;
2816 to->adjusted_insn_addr = from->adjusted_insn_addr;
2817 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
2818 to->handle = from->handle;
2820 gdb_assert (from->handle);
2821 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
2824 #define MAX_JUMP_SIZE 20
2826 /* Install fast tracepoint. Return 0 if successful, otherwise return
2830 install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
2832 CORE_ADDR jentry, jump_entry;
2833 CORE_ADDR trampoline;
2834 ULONGEST trampoline_size;
2836 /* The jump to the jump pad of the last fast tracepoint
2838 unsigned char fjump[MAX_JUMP_SIZE];
2839 ULONGEST fjump_size;
2841 if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
2843 trace_debug ("Requested a fast tracepoint on an instruction "
2844 "that is of less than the minimum length.");
2848 jentry = jump_entry = get_jump_space_head ();
2851 trampoline_size = 0;
2853 /* Install the jump pad. */
2854 err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
2856 ipa_sym_addrs.addr_gdb_collect,
2857 ipa_sym_addrs.addr_collecting,
2860 &trampoline, &trampoline_size,
2862 &tpoint->adjusted_insn_addr,
2863 &tpoint->adjusted_insn_addr_end,
2870 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
2873 if (tpoint->handle != NULL)
2875 tpoint->jump_pad = jump_entry;
2876 tpoint->jump_pad_end = jentry;
2877 tpoint->trampoline = trampoline;
2878 tpoint->trampoline_end = trampoline + trampoline_size;
2880 /* Pad to 8-byte alignment. */
2881 jentry = ((jentry + 7) & ~0x7);
2882 claim_jump_space (jentry - jump_entry);
2889 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2892 install_tracepoint (struct tracepoint *tpoint, char *own_buf)
2894 tpoint->handle = NULL;
2897 if (tpoint->type == trap_tracepoint)
2899 /* Tracepoints are installed as memory breakpoints. Just go
2900 ahead and install the trap. The breakpoints module
2901 handles duplicated breakpoints, and the memory read
2902 routine handles un-patching traps from memory reads. */
2903 tpoint->handle = set_breakpoint_at (tpoint->address,
2904 tracepoint_handler);
2906 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
2908 struct tracepoint *tp;
2910 if (!in_process_agent_loaded ())
2912 trace_debug ("Requested a %s tracepoint, but fast "
2913 "tracepoints aren't supported.",
2914 tpoint->type == static_tracepoint ? "static" : "fast");
2915 write_e_ipa_not_loaded (own_buf);
2918 if (tpoint->type == static_tracepoint
2919 && !in_process_agent_supports_ust ())
2921 trace_debug ("Requested a static tracepoint, but static "
2922 "tracepoints are not supported.");
2923 write_e_ust_not_loaded (own_buf);
2927 /* Find another fast or static tracepoint at the same address. */
2928 for (tp = tracepoints; tp; tp = tp->next)
2930 if (tp->address == tpoint->address && tp->type == tpoint->type
2931 && tp->number != tpoint->number)
2935 if (tpoint->type == fast_tracepoint)
2937 if (tp) /* TPOINT is installed at the same address as TP. */
2938 clone_fast_tracepoint (tpoint, tp);
2940 install_fast_tracepoint (tpoint, own_buf);
2945 tpoint->handle = (void *) -1;
2948 if (probe_marker_at (tpoint->address, own_buf) == 0)
2949 tpoint->handle = (void *) -1;
2955 internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
2957 if (tpoint->handle == NULL)
2959 if (*own_buf == '\0')
2960 write_enn (own_buf);
2967 cmd_qtstart (char *packet)
2969 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
2971 trace_debug ("Starting the trace");
2973 /* Pause all threads temporarily while we patch tracepoints. */
2976 /* Get threads out of jump pads. Safe to do here, since this is a
2977 top level command. And, required to do here, since we're
2978 deleting/rewriting jump pads. */
2980 stabilize_threads ();
2982 /* Freeze threads. */
2985 /* Sync the fast tracepoints list in the inferior ftlib. */
2986 if (in_process_agent_loaded ())
2988 download_tracepoints ();
2989 download_trace_state_variables ();
2992 /* No previous fast tpoint yet. */
2993 prev_ftpoint = NULL;
2995 /* No previous static tpoint yet. */
2996 prev_stpoint = NULL;
3000 /* Install tracepoints. */
3001 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3003 /* Ensure all the hit counts start at zero. */
3004 tpoint->hit_count = 0;
3005 tpoint->traceframe_usage = 0;
3007 if (tpoint->type == trap_tracepoint)
3009 /* Tracepoints are installed as memory breakpoints. Just go
3010 ahead and install the trap. The breakpoints module
3011 handles duplicated breakpoints, and the memory read
3012 routine handles un-patching traps from memory reads. */
3013 tpoint->handle = set_breakpoint_at (tpoint->address,
3014 tracepoint_handler);
3016 else if (tpoint->type == fast_tracepoint)
3018 if (maybe_write_ipa_not_loaded (packet))
3020 trace_debug ("Requested a fast tracepoint, but fast "
3021 "tracepoints aren't supported.");
3025 if (prev_ftpoint != NULL && prev_ftpoint->address == tpoint->address)
3026 clone_fast_tracepoint (tpoint, prev_ftpoint);
3029 if (install_fast_tracepoint (tpoint, packet) == 0)
3030 prev_ftpoint = tpoint;
3033 else if (tpoint->type == static_tracepoint)
3035 if (maybe_write_ipa_ust_not_loaded (packet))
3037 trace_debug ("Requested a static tracepoint, but static "
3038 "tracepoints are not supported.");
3042 /* Can only probe a given marker once. */
3043 if (prev_stpoint != NULL && prev_stpoint->address == tpoint->address)
3045 tpoint->handle = (void *) -1;
3049 if (probe_marker_at (tpoint->address, packet) == 0)
3051 tpoint->handle = (void *) -1;
3053 /* So that we can handle multiple static tracepoints
3054 at the same address easily. */
3055 prev_stpoint = tpoint;
3060 /* Any failure in the inner loop is sufficient cause to give
3062 if (tpoint->handle == NULL)
3066 /* Any error in tracepoint insertion is unacceptable; better to
3067 address the problem now, than end up with a useless or misleading
3071 clear_installed_tracepoints ();
3072 if (*packet == '\0')
3078 stopping_tracepoint = NULL;
3079 trace_buffer_is_full = 0;
3080 expr_eval_result = expr_eval_no_error;
3081 error_tracepoint = NULL;
3082 tracing_start_time = get_timestamp ();
3084 /* Tracing is now active, hits will now start being logged. */
3087 if (in_process_agent_loaded ())
3089 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
3090 fatal ("Error setting tracing variable in lib");
3092 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3094 fatal ("Error clearing stopping_tracepoint variable in lib");
3096 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
3097 fatal ("Error clearing trace_buffer_is_full variable in lib");
3099 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3100 stop_tracing_handler);
3101 if (stop_tracing_bkpt == NULL)
3102 error ("Error setting stop_tracing breakpoint");
3104 flush_trace_buffer_bkpt
3105 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3106 flush_trace_buffer_handler);
3107 if (flush_trace_buffer_bkpt == NULL)
3108 error ("Error setting flush_trace_buffer breakpoint");
3116 /* End a tracing run, filling in a stop reason to report back to GDB,
3117 and removing the tracepoints from the code. */
3124 trace_debug ("Tracing is already off, ignoring");
3128 trace_debug ("Stopping the trace");
3130 /* Pause all threads before removing fast jumps from memory,
3131 breakpoints, and touching IPA state variables (inferior memory).
3132 Some thread may hit the internal tracing breakpoints, or be
3133 collecting this moment, but that's ok, we don't release the
3134 tpoint object's memory or the jump pads here (we only do that
3135 when we're sure we can move all threads out of the jump pads).
3136 We can't now, since we may be getting here due to the inferior
3137 agent calling us. */
3139 /* Since we're removing breakpoints, cancel breakpoint hits,
3140 possibly related to the breakpoints we're about to delete. */
3141 cancel_breakpoints ();
3143 /* Stop logging. Tracepoints can still be hit, but they will not be
3146 if (in_process_agent_loaded ())
3148 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
3149 fatal ("Error clearing tracing variable in lib");
3152 tracing_stop_time = get_timestamp ();
3153 tracing_stop_reason = "t???";
3154 tracing_stop_tpnum = 0;
3155 if (stopping_tracepoint)
3157 trace_debug ("Stopping the trace because "
3158 "tracepoint %d was hit %ld times",
3159 stopping_tracepoint->number,
3160 stopping_tracepoint->pass_count);
3161 tracing_stop_reason = "tpasscount";
3162 tracing_stop_tpnum = stopping_tracepoint->number;
3164 else if (trace_buffer_is_full)
3166 trace_debug ("Stopping the trace because the trace buffer is full");
3167 tracing_stop_reason = "tfull";
3169 else if (expr_eval_result != expr_eval_no_error)
3171 trace_debug ("Stopping the trace because of an expression eval error");
3172 tracing_stop_reason = eval_result_names[expr_eval_result];
3173 tracing_stop_tpnum = error_tracepoint->number;
3175 #ifndef IN_PROCESS_AGENT
3176 else if (!gdb_connected ())
3178 trace_debug ("Stopping the trace because GDB disconnected");
3179 tracing_stop_reason = "tdisconnected";
3184 trace_debug ("Stopping the trace because of a tstop command");
3185 tracing_stop_reason = "tstop";
3188 stopping_tracepoint = NULL;
3189 error_tracepoint = NULL;
3191 /* Clear out the tracepoints. */
3192 clear_installed_tracepoints ();
3194 if (in_process_agent_loaded ())
3196 /* Pull in fast tracepoint trace frames from the inferior lib
3197 buffer into our buffer, even if our buffer is already full,
3198 because we want to present the full number of created frames
3199 in addition to what fit in the trace buffer. */
3200 upload_fast_traceframes ();
3203 if (stop_tracing_bkpt != NULL)
3205 delete_breakpoint (stop_tracing_bkpt);
3206 stop_tracing_bkpt = NULL;
3209 if (flush_trace_buffer_bkpt != NULL)
3211 delete_breakpoint (flush_trace_buffer_bkpt);
3212 flush_trace_buffer_bkpt = NULL;
3219 stop_tracing_handler (CORE_ADDR addr)
3221 trace_debug ("lib hit stop_tracing");
3223 /* Don't actually handle it here. When we stop tracing we remove
3224 breakpoints from the inferior, and that is not allowed in a
3225 breakpoint handler (as the caller is walking the breakpoint
3231 flush_trace_buffer_handler (CORE_ADDR addr)
3233 trace_debug ("lib hit flush_trace_buffer");
3238 cmd_qtstop (char *packet)
3245 cmd_qtdisconnected (char *own_buf)
3248 char *packet = own_buf;
3250 packet += strlen ("QTDisconnected:");
3252 unpack_varlen_hex (packet, &setting);
3256 disconnected_tracing = setting;
3260 cmd_qtframe (char *own_buf)
3262 ULONGEST frame, pc, lo, hi, num;
3264 struct traceframe *tframe;
3265 char *packet = own_buf;
3267 packet += strlen ("QTFrame:");
3269 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3271 packet += strlen ("pc:");
3272 unpack_varlen_hex (packet, &pc);
3273 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3274 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3276 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3278 packet += strlen ("range:");
3279 packet = unpack_varlen_hex (packet, &lo);
3281 unpack_varlen_hex (packet, &hi);
3282 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3283 paddress (lo), paddress (hi));
3284 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3286 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3288 packet += strlen ("outside:");
3289 packet = unpack_varlen_hex (packet, &lo);
3291 unpack_varlen_hex (packet, &hi);
3292 trace_debug ("Want to find next traceframe "
3293 "outside the range 0x%s to 0x%s",
3294 paddress (lo), paddress (hi));
3295 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3297 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3299 packet += strlen ("tdp:");
3300 unpack_varlen_hex (packet, &num);
3302 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3303 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3307 unpack_varlen_hex (packet, &frame);
3308 tfnum = (int) frame;
3311 trace_debug ("Want to stop looking at traceframes");
3312 current_traceframe = -1;
3316 trace_debug ("Want to look at traceframe %d", tfnum);
3317 tframe = find_traceframe (tfnum);
3322 current_traceframe = tfnum;
3323 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3326 sprintf (own_buf, "F-1");
3330 cmd_qtstatus (char *packet)
3332 char *stop_reason_rsp = NULL;
3333 char *buf1, *buf2, *buf3, *str;
3336 /* Translate the plain text of the notes back into hex for
3339 str = (tracing_user_name ? tracing_user_name : "");
3340 slen = strlen (str);
3341 buf1 = (char *) alloca (slen * 2 + 1);
3342 hexify (buf1, str, slen);
3344 str = (tracing_notes ? tracing_notes : "");
3345 slen = strlen (str);
3346 buf2 = (char *) alloca (slen * 2 + 1);
3347 hexify (buf2, str, slen);
3349 str = (tracing_stop_note ? tracing_stop_note : "");
3350 slen = strlen (str);
3351 buf3 = (char *) alloca (slen * 2 + 1);
3352 hexify (buf3, str, slen);
3354 trace_debug ("Returning trace status as %d, stop reason %s",
3355 tracing, tracing_stop_reason);
3357 if (in_process_agent_loaded ())
3361 upload_fast_traceframes ();
3366 stop_reason_rsp = (char *) tracing_stop_reason;
3368 /* The user visible error string in terror needs to be hex encoded.
3369 We leave it as plain string in `tracing_stop_reason' to ease
3371 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3373 const char *result_name;
3377 result_name = stop_reason_rsp + strlen ("terror:");
3378 hexstr_len = strlen (result_name) * 2;
3379 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3380 strcpy (p, "terror:");
3382 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3385 /* If this was a forced stop, include any stop note that was supplied. */
3386 if (strcmp (stop_reason_rsp, "tstop") == 0)
3388 stop_reason_rsp = alloca (strlen ("tstop:") + strlen (buf3) + 1);
3389 strcpy (stop_reason_rsp, "tstop:");
3390 strcat (stop_reason_rsp, buf3);
3396 "tframes:%x;tcreated:%x;"
3397 "tfree:%x;tsize:%s;"
3400 "starttime:%s;stoptime:%s;"
3401 "username:%s:;notes:%s:",
3403 stop_reason_rsp, tracing_stop_tpnum,
3404 traceframe_count, traceframes_created,
3405 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3406 circular_trace_buffer,
3407 disconnected_tracing,
3408 plongest (tracing_start_time), plongest (tracing_stop_time),
3413 cmd_qtp (char *own_buf)
3416 struct tracepoint *tpoint;
3417 char *packet = own_buf;
3419 packet += strlen ("qTP:");
3421 packet = unpack_varlen_hex (packet, &num);
3422 ++packet; /* skip a colon */
3423 packet = unpack_varlen_hex (packet, &addr);
3425 /* See if we already have this tracepoint. */
3426 tpoint = find_tracepoint (num, addr);
3430 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3431 (int) num, paddress (addr));
3432 write_enn (own_buf);
3436 sprintf (own_buf, "V%lx:%lx", tpoint->hit_count, tpoint->traceframe_usage);
3439 /* State variables to help return all the tracepoint bits. */
3440 static struct tracepoint *cur_tpoint;
3441 static int cur_action;
3442 static int cur_step_action;
3443 static struct source_string *cur_source_string;
3444 static struct trace_state_variable *cur_tsv;
3446 /* Compose a response that is an imitation of the syntax by which the
3447 tracepoint was originally downloaded. */
3450 response_tracepoint (char *packet, struct tracepoint *tpoint)
3454 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
3455 paddress (tpoint->address),
3456 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3457 tpoint->pass_count);
3458 if (tpoint->type == fast_tracepoint)
3459 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
3460 else if (tpoint->type == static_tracepoint)
3461 sprintf (packet + strlen (packet), ":S");
3465 buf = gdb_unparse_agent_expr (tpoint->cond);
3466 sprintf (packet + strlen (packet), ":X%x,%s",
3467 tpoint->cond->length, buf);
3472 /* Compose a response that is an imitation of the syntax by which the
3473 tracepoint action was originally downloaded (with the difference
3474 that due to the way we store the actions, this will output a packet
3475 per action, while GDB could have combined more than one action
3479 response_action (char *packet, struct tracepoint *tpoint,
3480 char *taction, int step)
3482 sprintf (packet, "%c%x:%s:%s",
3483 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3487 /* Compose a response that is an imitation of the syntax by which the
3488 tracepoint source piece was originally downloaded. */
3491 response_source (char *packet,
3492 struct tracepoint *tpoint, struct source_string *src)
3497 len = strlen (src->str);
3498 buf = alloca (len * 2 + 1);
3499 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3501 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3502 tpoint->number, paddress (tpoint->address),
3503 src->type, 0, len, buf);
3506 /* Return the first piece of tracepoint definition, and initialize the
3507 state machine that will iterate through all the tracepoint
3511 cmd_qtfp (char *packet)
3513 trace_debug ("Returning first tracepoint definition piece");
3515 cur_tpoint = tracepoints;
3516 cur_action = cur_step_action = -1;
3517 cur_source_string = NULL;
3520 response_tracepoint (packet, cur_tpoint);
3522 strcpy (packet, "l");
3525 /* Return additional pieces of tracepoint definition. Each action and
3526 stepping action must go into its own packet, because of packet size
3527 limits, and so we use state variables to deliver one piece at a
3531 cmd_qtsp (char *packet)
3533 trace_debug ("Returning subsequent tracepoint definition piece");
3537 /* This case would normally never occur, but be prepared for
3539 strcpy (packet, "l");
3541 else if (cur_action < cur_tpoint->numactions - 1)
3544 response_action (packet, cur_tpoint,
3545 cur_tpoint->actions_str[cur_action], 0);
3547 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3550 response_action (packet, cur_tpoint,
3551 cur_tpoint->step_actions_str[cur_step_action], 1);
3553 else if ((cur_source_string
3554 ? cur_source_string->next
3555 : cur_tpoint->source_strings))
3557 if (cur_source_string)
3558 cur_source_string = cur_source_string->next;
3560 cur_source_string = cur_tpoint->source_strings;
3561 response_source (packet, cur_tpoint, cur_source_string);
3565 cur_tpoint = cur_tpoint->next;
3566 cur_action = cur_step_action = -1;
3567 cur_source_string = NULL;
3569 response_tracepoint (packet, cur_tpoint);
3571 strcpy (packet, "l");
3575 /* Compose a response that is an imitation of the syntax by which the
3576 trace state variable was originally downloaded. */
3579 response_tsv (char *packet, struct trace_state_variable *tsv)
3581 char *buf = (char *) "";
3586 namelen = strlen (tsv->name);
3587 buf = alloca (namelen * 2 + 1);
3588 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3591 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3592 tsv->getter ? 1 : 0, buf);
3595 /* Return the first trace state variable definition, and initialize
3596 the state machine that will iterate through all the tsv bits. */
3599 cmd_qtfv (char *packet)
3601 trace_debug ("Returning first trace state variable definition");
3603 cur_tsv = trace_state_variables;
3606 response_tsv (packet, cur_tsv);
3608 strcpy (packet, "l");
3611 /* Return additional trace state variable definitions. */
3614 cmd_qtsv (char *packet)
3616 trace_debug ("Returning first trace state variable definition");
3620 /* This case would normally never occur, but be prepared for
3622 strcpy (packet, "l");
3626 cur_tsv = cur_tsv->next;
3628 response_tsv (packet, cur_tsv);
3630 strcpy (packet, "l");
3633 strcpy (packet, "l");
3636 /* Return the first static tracepoint marker, and initialize the state
3637 machine that will iterate through all the static tracepoints
3641 cmd_qtfstm (char *packet)
3643 if (!maybe_write_ipa_ust_not_loaded (packet))
3644 run_inferior_command (packet);
3647 /* Return additional static tracepoints markers. */
3650 cmd_qtsstm (char *packet)
3652 if (!maybe_write_ipa_ust_not_loaded (packet))
3653 run_inferior_command (packet);
3656 /* Return the definition of the static tracepoint at a given address.
3657 Result packet is the same as qTsST's. */
3660 cmd_qtstmat (char *packet)
3662 if (!maybe_write_ipa_ust_not_loaded (packet))
3663 run_inferior_command (packet);
3666 /* Return the minimum instruction size needed for fast tracepoints as a
3667 hexadecimal number. */
3670 cmd_qtminftpilen (char *packet)
3672 if (current_inferior == NULL)
3674 /* Indicate that the minimum length is currently unknown. */
3675 strcpy (packet, "0");
3679 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
3682 /* Respond to qTBuffer packet with a block of raw data from the trace
3683 buffer. GDB may ask for a lot, but we are allowed to reply with
3684 only as much as will fit within packet limits or whatever. */
3687 cmd_qtbuffer (char *own_buf)
3689 ULONGEST offset, num, tot;
3691 char *packet = own_buf;
3693 packet += strlen ("qTBuffer:");
3695 packet = unpack_varlen_hex (packet, &offset);
3696 ++packet; /* skip a comma */
3697 unpack_varlen_hex (packet, &num);
3699 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3700 (int) num, pulongest (offset));
3702 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3704 /* If we're right at the end, reply specially that we're done. */
3707 strcpy (own_buf, "l");
3711 /* Object to any other out-of-bounds request. */
3714 write_enn (own_buf);
3718 /* Compute the pointer corresponding to the given offset, accounting
3720 tbp = trace_buffer_start + offset;
3721 if (tbp >= trace_buffer_wrap)
3722 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3724 /* Trim to the remaining bytes if we're close to the end. */
3725 if (num > tot - offset)
3728 /* Trim to available packet size. */
3729 if (num >= (PBUFSIZ - 16) / 2 )
3730 num = (PBUFSIZ - 16) / 2;
3732 convert_int_to_ascii (tbp, own_buf, num);
3733 own_buf[num] = '\0';
3737 cmd_bigqtbuffer_circular (char *own_buf)
3740 char *packet = own_buf;
3742 packet += strlen ("QTBuffer:circular:");
3744 unpack_varlen_hex (packet, &val);
3745 circular_trace_buffer = val;
3746 trace_debug ("Trace buffer is now %s",
3747 circular_trace_buffer ? "circular" : "linear");
3752 cmd_qtnotes (char *own_buf)
3755 char *saved, *user, *notes, *stopnote;
3756 char *packet = own_buf;
3758 packet += strlen ("QTNotes:");
3762 if (strncmp ("user:", packet, strlen ("user:")) == 0)
3764 packet += strlen ("user:");
3766 packet = strchr (packet, ';');
3767 nbytes = (packet - saved) / 2;
3768 user = xmalloc (nbytes + 1);
3769 nbytes = unhexify (user, saved, nbytes);
3770 user[nbytes] = '\0';
3771 ++packet; /* skip the semicolon */
3772 trace_debug ("User is '%s'", user);
3773 tracing_user_name = user;
3775 else if (strncmp ("notes:", packet, strlen ("notes:")) == 0)
3777 packet += strlen ("notes:");
3779 packet = strchr (packet, ';');
3780 nbytes = (packet - saved) / 2;
3781 notes = xmalloc (nbytes + 1);
3782 nbytes = unhexify (notes, saved, nbytes);
3783 notes[nbytes] = '\0';
3784 ++packet; /* skip the semicolon */
3785 trace_debug ("Notes is '%s'", notes);
3786 tracing_notes = notes;
3788 else if (strncmp ("tstop:", packet, strlen ("tstop:")) == 0)
3790 packet += strlen ("tstop:");
3792 packet = strchr (packet, ';');
3793 nbytes = (packet - saved) / 2;
3794 stopnote = xmalloc (nbytes + 1);
3795 nbytes = unhexify (stopnote, saved, nbytes);
3796 stopnote[nbytes] = '\0';
3797 ++packet; /* skip the semicolon */
3798 trace_debug ("tstop note is '%s'", stopnote);
3799 tracing_stop_note = stopnote;
3809 handle_tracepoint_general_set (char *packet)
3811 if (strcmp ("QTinit", packet) == 0)
3813 cmd_qtinit (packet);
3816 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3821 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3823 cmd_qtdpsrc (packet);
3826 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
3828 cmd_qtenable_disable (packet, 1);
3831 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
3833 cmd_qtenable_disable (packet, 0);
3836 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3841 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3846 else if (strcmp ("QTStart", packet) == 0)
3848 cmd_qtstart (packet);
3851 else if (strcmp ("QTStop", packet) == 0)
3853 cmd_qtstop (packet);
3856 else if (strncmp ("QTDisconnected:", packet,
3857 strlen ("QTDisconnected:")) == 0)
3859 cmd_qtdisconnected (packet);
3862 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
3864 cmd_qtframe (packet);
3867 else if (strncmp ("QTBuffer:circular:", packet, strlen ("QTBuffer:circular:")) == 0)
3869 cmd_bigqtbuffer_circular (packet);
3872 else if (strncmp ("QTNotes:", packet, strlen ("QTNotes:")) == 0)
3874 cmd_qtnotes (packet);
3882 handle_tracepoint_query (char *packet)
3884 if (strcmp ("qTStatus", packet) == 0)
3886 cmd_qtstatus (packet);
3889 else if (strncmp ("qTP:", packet, strlen ("qTP:")) == 0)
3894 else if (strcmp ("qTfP", packet) == 0)
3899 else if (strcmp ("qTsP", packet) == 0)
3904 else if (strcmp ("qTfV", packet) == 0)
3909 else if (strcmp ("qTsV", packet) == 0)
3914 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
3919 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
3921 cmd_qtbuffer (packet);
3924 else if (strcmp ("qTfSTM", packet) == 0)
3926 cmd_qtfstm (packet);
3929 else if (strcmp ("qTsSTM", packet) == 0)
3931 cmd_qtsstm (packet);
3934 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
3936 cmd_qtstmat (packet);
3939 else if (strcmp ("qTMinFTPILen", packet) == 0)
3941 cmd_qtminftpilen (packet);
3949 #ifndef IN_PROCESS_AGENT
3951 /* Call this when thread TINFO has hit the tracepoint defined by
3952 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3953 action. This adds a while-stepping collecting state item to the
3954 threads' collecting state list, so that we can keep track of
3955 multiple simultaneous while-stepping actions being collected by the
3956 same thread. This can happen in cases like:
3958 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3960 ff0003 INSN3 <-- TP2, collect $regs
3961 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3964 Notice that when instruction INSN5 is reached, the while-stepping
3965 actions of both TP1 and TP3 are still being collected, and that TP2
3966 had been collected meanwhile. The whole range of ff0001-ff0005
3967 should be single-stepped, due to at least TP1's while-stepping
3968 action covering the whole range. */
3971 add_while_stepping_state (struct thread_info *tinfo,
3972 int tp_number, CORE_ADDR tp_address)
3974 struct wstep_state *wstep;
3976 wstep = xmalloc (sizeof (*wstep));
3977 wstep->next = tinfo->while_stepping;
3979 wstep->tp_number = tp_number;
3980 wstep->tp_address = tp_address;
3981 wstep->current_step = 0;
3983 tinfo->while_stepping = wstep;
3986 /* Release the while-stepping collecting state WSTEP. */
3989 release_while_stepping_state (struct wstep_state *wstep)
3994 /* Release all while-stepping collecting states currently associated
3995 with thread TINFO. */
3998 release_while_stepping_state_list (struct thread_info *tinfo)
4000 struct wstep_state *head;
4002 while (tinfo->while_stepping)
4004 head = tinfo->while_stepping;
4005 tinfo->while_stepping = head->next;
4006 release_while_stepping_state (head);
4010 /* If TINFO was handling a 'while-stepping' action, the step has
4011 finished, so collect any step data needed, and check if any more
4012 steps are required. Return true if the thread was indeed
4013 collecting tracepoint data, false otherwise. */
4016 tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
4018 struct tracepoint *tpoint;
4019 struct wstep_state *wstep;
4020 struct wstep_state **wstep_link;
4021 struct trap_tracepoint_ctx ctx;
4023 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4025 if (in_process_agent_loaded ())
4026 upload_fast_traceframes ();
4028 /* Check if we were indeed collecting data for one of more
4029 tracepoints with a 'while-stepping' count. */
4030 if (tinfo->while_stepping == NULL)
4035 /* We're not even tracing anymore. Stop this thread from
4037 release_while_stepping_state_list (tinfo);
4039 /* The thread had stopped due to a single-step request indeed
4040 explained by a tracepoint. */
4044 wstep = tinfo->while_stepping;
4045 wstep_link = &tinfo->while_stepping;
4047 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4048 target_pid_to_str (tinfo->entry.id),
4049 wstep->tp_number, paddress (wstep->tp_address));
4051 ctx.base.type = trap_tracepoint;
4052 ctx.regcache = get_thread_regcache (tinfo, 1);
4054 while (wstep != NULL)
4056 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
4059 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4060 wstep->tp_number, paddress (wstep->tp_address),
4061 target_pid_to_str (tinfo->entry.id));
4064 *wstep_link = wstep->next;
4065 release_while_stepping_state (wstep);
4066 wstep = *wstep_link;
4070 /* We've just finished one step. */
4071 ++wstep->current_step;
4074 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
4075 stop_pc, tpoint, wstep->current_step);
4077 if (wstep->current_step >= tpoint->step_count)
4079 /* The requested numbers of steps have occurred. */
4080 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4081 target_pid_to_str (tinfo->entry.id),
4082 wstep->tp_number, paddress (wstep->tp_address));
4084 /* Unlink the wstep. */
4085 *wstep_link = wstep->next;
4086 release_while_stepping_state (wstep);
4087 wstep = *wstep_link;
4089 /* Only check the hit count now, which ensure that we do all
4090 our stepping before stopping the run. */
4091 if (tpoint->pass_count > 0
4092 && tpoint->hit_count >= tpoint->pass_count
4093 && stopping_tracepoint == NULL)
4094 stopping_tracepoint = tpoint;
4098 /* Keep single-stepping until the requested numbers of steps
4100 wstep_link = &wstep->next;
4101 wstep = *wstep_link;
4104 if (stopping_tracepoint
4105 || trace_buffer_is_full
4106 || expr_eval_result != expr_eval_no_error)
4116 /* Handle any internal tracing control breakpoint hits. That means,
4117 pull traceframes from the IPA to our buffer, and syncing both
4118 tracing agents when the IPA's tracing stops for some reason. */
4121 handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4123 /* Pull in fast tracepoint trace frames from the inferior in-process
4124 agent's buffer into our buffer. */
4126 if (!in_process_agent_loaded ())
4129 upload_fast_traceframes ();
4131 /* Check if the in-process agent had decided we should stop
4133 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4135 int ipa_trace_buffer_is_full;
4136 CORE_ADDR ipa_stopping_tracepoint;
4137 int ipa_expr_eval_result;
4138 CORE_ADDR ipa_error_tracepoint;
4140 trace_debug ("lib stopped at stop_tracing");
4142 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4143 &ipa_trace_buffer_is_full);
4145 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4146 &ipa_stopping_tracepoint);
4147 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4149 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4150 &ipa_error_tracepoint);
4151 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4153 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4154 &ipa_expr_eval_result);
4155 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4157 trace_debug ("lib: trace_buffer_is_full: %d, "
4158 "stopping_tracepoint: %s, "
4159 "ipa_expr_eval_result: %d, "
4160 "error_tracepoint: %s, ",
4161 ipa_trace_buffer_is_full,
4162 paddress (ipa_stopping_tracepoint),
4163 ipa_expr_eval_result,
4164 paddress (ipa_error_tracepoint));
4168 if (ipa_trace_buffer_is_full)
4169 trace_debug ("lib stopped due to full buffer.");
4170 if (ipa_stopping_tracepoint)
4171 trace_debug ("lib stopped due to tpoint");
4172 if (ipa_stopping_tracepoint)
4173 trace_debug ("lib stopped due to error");
4176 if (ipa_stopping_tracepoint != 0)
4179 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4181 else if (ipa_expr_eval_result != expr_eval_no_error)
4183 expr_eval_result = ipa_expr_eval_result;
4185 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4190 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4192 trace_debug ("lib stopped at flush_trace_buffer");
4199 /* Return true if TINFO just hit a tracepoint. Collect data if
4203 tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4205 struct tracepoint *tpoint;
4207 struct trap_tracepoint_ctx ctx;
4209 /* Not tracing, don't handle. */
4213 ctx.base.type = trap_tracepoint;
4214 ctx.regcache = get_thread_regcache (tinfo, 1);
4216 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4218 /* Note that we collect fast tracepoints here as well. We'll
4219 step over the fast tracepoint jump later, which avoids the
4220 double collect. However, we don't collect for static
4221 tracepoints here, because UST markers are compiled in program,
4222 and probes will be executed in program. So static tracepoints
4223 are collected there. */
4224 if (tpoint->enabled && stop_pc == tpoint->address
4225 && tpoint->type != static_tracepoint)
4227 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4228 target_pid_to_str (tinfo->entry.id),
4229 tpoint->number, paddress (tpoint->address));
4231 /* Test the condition if present, and collect if true. */
4233 || (condition_true_at_tracepoint
4234 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4235 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4238 if (stopping_tracepoint
4239 || trace_buffer_is_full
4240 || expr_eval_result != expr_eval_no_error)
4244 /* If the tracepoint had a 'while-stepping' action, then set
4245 the thread to collect this tracepoint on the following
4247 else if (tpoint->step_count > 0)
4249 add_while_stepping_state (tinfo,
4250 tpoint->number, tpoint->address);
4262 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4263 struct ust_marker_data;
4264 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4265 struct traceframe *tframe);
4268 /* Create a trace frame for the hit of the given tracepoint in the
4272 collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4273 struct tracepoint *tpoint)
4275 struct traceframe *tframe;
4278 /* Only count it as a hit when we actually collect data. */
4279 tpoint->hit_count++;
4281 /* If we've exceeded a defined pass count, record the event for
4282 later, and finish the collection for this hit. This test is only
4283 for nonstepping tracepoints, stepping tracepoints test at the end
4284 of their while-stepping loop. */
4285 if (tpoint->pass_count > 0
4286 && tpoint->hit_count >= tpoint->pass_count
4287 && tpoint->step_count == 0
4288 && stopping_tracepoint == NULL)
4289 stopping_tracepoint = tpoint;
4291 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4292 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4294 tframe = add_traceframe (tpoint);
4298 for (acti = 0; acti < tpoint->numactions; ++acti)
4300 #ifndef IN_PROCESS_AGENT
4301 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4302 tpoint->number, paddress (tpoint->address),
4303 tpoint->actions_str[acti]);
4306 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4307 tpoint->actions[acti]);
4310 finish_traceframe (tframe);
4313 if (tframe == NULL && tracing)
4314 trace_buffer_is_full = 1;
4317 #ifndef IN_PROCESS_AGENT
4320 collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4322 struct tracepoint *tpoint, int current_step)
4324 struct traceframe *tframe;
4327 trace_debug ("Making new step traceframe for "
4328 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4329 tpoint->number, paddress (tpoint->address),
4330 current_step, tpoint->step_count,
4333 tframe = add_traceframe (tpoint);
4337 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4339 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4340 tpoint->number, paddress (tpoint->address),
4341 tpoint->step_actions_str[acti]);
4343 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4344 tpoint->step_actions[acti]);
4347 finish_traceframe (tframe);
4350 if (tframe == NULL && tracing)
4351 trace_buffer_is_full = 1;
4356 static struct regcache *
4357 get_context_regcache (struct tracepoint_hit_ctx *ctx)
4359 struct regcache *regcache = NULL;
4361 #ifdef IN_PROCESS_AGENT
4362 if (ctx->type == fast_tracepoint)
4364 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4365 if (!fctx->regcache_initted)
4367 fctx->regcache_initted = 1;
4368 init_register_cache (&fctx->regcache, fctx->regspace);
4369 supply_regblock (&fctx->regcache, NULL);
4370 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4372 regcache = &fctx->regcache;
4375 if (ctx->type == static_tracepoint)
4377 struct static_tracepoint_ctx *sctx
4378 = (struct static_tracepoint_ctx *) ctx;
4380 if (!sctx->regcache_initted)
4382 sctx->regcache_initted = 1;
4383 init_register_cache (&sctx->regcache, sctx->regspace);
4384 supply_regblock (&sctx->regcache, NULL);
4385 /* Pass down the tracepoint address, because REGS doesn't
4386 include the PC, but we know what it must have been. */
4387 supply_static_tracepoint_registers (&sctx->regcache,
4388 (const unsigned char *)
4390 sctx->tpoint->address);
4392 regcache = &sctx->regcache;
4396 if (ctx->type == trap_tracepoint)
4398 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4399 regcache = tctx->regcache;
4403 gdb_assert (regcache != NULL);
4409 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4411 struct tracepoint *tpoint,
4412 struct traceframe *tframe,
4413 struct tracepoint_action *taction)
4415 enum eval_result_type err;
4417 switch (taction->type)
4421 struct collect_memory_action *maction;
4423 maction = (struct collect_memory_action *) taction;
4425 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4426 pulongest (maction->len),
4427 paddress (maction->addr), maction->basereg);
4428 /* (should use basereg) */
4429 agent_mem_read (tframe, NULL,
4430 (CORE_ADDR) maction->addr, maction->len);
4435 unsigned char *regspace;
4436 struct regcache tregcache;
4437 struct regcache *context_regcache;
4440 trace_debug ("Want to collect registers");
4442 /* Collect all registers for now. */
4443 regspace = add_traceframe_block (tframe,
4444 1 + register_cache_size ());
4445 if (regspace == NULL)
4447 trace_debug ("Trace buffer block allocation failed, skipping");
4450 /* Identify a register block. */
4453 context_regcache = get_context_regcache (ctx);
4455 /* Wrap the regblock in a register cache (in the stack, we
4456 don't want to malloc here). */
4457 init_register_cache (&tregcache, regspace + 1);
4459 /* Copy the register data to the regblock. */
4460 regcache_cpy (&tregcache, context_regcache);
4462 #ifndef IN_PROCESS_AGENT
4463 /* On some platforms, trap-based tracepoints will have the PC
4464 pointing to the next instruction after the trap, but we
4465 don't want the user or GDB trying to guess whether the
4466 saved PC needs adjusting; so always record the adjusted
4467 stop_pc. Note that we can't use tpoint->address instead,
4468 since it will be wrong for while-stepping actions. This
4469 adjustment is a nop for fast tracepoints collected from the
4470 in-process lib (but not if GDBserver is collecting one
4471 preemptively), since the PC had already been adjusted to
4472 contain the tracepoint's address by the jump pad. */
4473 trace_debug ("Storing stop pc (0x%s) in regblock",
4474 paddress (stop_pc));
4476 /* This changes the regblock, not the thread's
4478 regcache_write_pc (&tregcache, stop_pc);
4484 struct eval_expr_action *eaction;
4486 eaction = (struct eval_expr_action *) taction;
4488 trace_debug ("Want to evaluate expression");
4490 err = eval_tracepoint_agent_expr (ctx, tframe, eaction->expr, NULL);
4492 if (err != expr_eval_no_error)
4494 record_tracepoint_error (tpoint, "action expression", err);
4501 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4502 trace_debug ("Want to collect static trace data");
4503 collect_ust_data_at_tracepoint (ctx, tframe);
4505 trace_debug ("warning: collecting static trace data, "
4506 "but static tracepoints are not supported");
4511 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4517 condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4518 struct tracepoint *tpoint)
4521 enum eval_result_type err;
4523 /* Presently, gdbserver doesn't run compiled conditions, only the
4524 IPA does. If the program stops at a fast tracepoint's address
4525 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4526 gdbserver preemptively collect the fast tracepoint. Later, on
4527 resume, gdbserver steps over the fast tracepoint like it steps
4528 over breakpoints, so that the IPA doesn't see that fast
4529 tracepoint. This avoids double collects of fast tracepoints in
4530 that stopping scenario. Having gdbserver itself handle the fast
4531 tracepoint gives the user a consistent view of when fast or trap
4532 tracepoints are collected, compared to an alternative where only
4533 trap tracepoints are collected on stop, and fast tracepoints on
4534 resume. When a fast tracepoint is being processed by gdbserver,
4535 it is always the non-compiled condition expression that is
4537 #ifdef IN_PROCESS_AGENT
4538 if (tpoint->compiled_cond)
4539 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4542 err = eval_tracepoint_agent_expr (ctx, NULL, tpoint->cond, &value);
4544 if (err != expr_eval_no_error)
4546 record_tracepoint_error (tpoint, "condition", err);
4547 /* The error case must return false. */
4551 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4552 tpoint->number, paddress (tpoint->address),
4554 return (value ? 1 : 0);
4557 /* Evaluates a tracepoint agent expression with context CTX,
4558 traceframe TFRAME, agent expression AEXPR and store the
4561 static enum eval_result_type
4562 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx *ctx,
4563 struct traceframe *tframe,
4564 struct agent_expr *aexpr,
4567 struct regcache *regcache;
4568 regcache = get_context_regcache (ctx);
4570 return gdb_eval_agent_expr (regcache, tframe, aexpr, rslt);
4573 /* Do memory copies for bytecodes. */
4574 /* Do the recording of memory blocks for actions and bytecodes. */
4577 agent_mem_read (struct traceframe *tframe,
4578 unsigned char *to, CORE_ADDR from, ULONGEST len)
4580 unsigned char *mspace;
4581 ULONGEST remaining = len;
4582 unsigned short blocklen;
4584 /* If a 'to' buffer is specified, use it. */
4587 read_inferior_memory (from, to, len);
4591 /* Otherwise, create a new memory block in the trace buffer. */
4592 while (remaining > 0)
4596 blocklen = (remaining > 65535 ? 65535 : remaining);
4597 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4598 mspace = add_traceframe_block (tframe, sp);
4601 /* Identify block as a memory block. */
4604 /* Record address and size. */
4605 memcpy (mspace, &from, sizeof (from));
4606 mspace += sizeof (from);
4607 memcpy (mspace, &blocklen, sizeof (blocklen));
4608 mspace += sizeof (blocklen);
4609 /* Record the memory block proper. */
4610 read_inferior_memory (from, mspace, blocklen);
4611 trace_debug ("%d bytes recorded", blocklen);
4612 remaining -= blocklen;
4619 agent_mem_read_string (struct traceframe *tframe,
4620 unsigned char *to, CORE_ADDR from, ULONGEST len)
4622 unsigned char *buf, *mspace;
4623 ULONGEST remaining = len;
4624 unsigned short blocklen, i;
4626 /* To save a bit of space, block lengths are 16-bit, so break large
4627 requests into multiple blocks. Bordering on overkill for strings,
4628 but it could happen that someone specifies a large max length. */
4629 while (remaining > 0)
4633 blocklen = (remaining > 65535 ? 65535 : remaining);
4634 /* We want working space to accumulate nonzero bytes, since
4635 traceframes must have a predecided size (otherwise it gets
4636 harder to wrap correctly for the circular case, etc). */
4637 buf = (unsigned char *) xmalloc (blocklen + 1);
4638 for (i = 0; i < blocklen; ++i)
4640 /* Read the string one byte at a time, in case the string is
4641 at the end of a valid memory area - we don't want a
4642 correctly-terminated string to engender segvio
4644 read_inferior_memory (from + i, buf + i, 1);
4649 /* Make sure outer loop stops now too. */
4650 remaining = blocklen;
4654 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4655 mspace = add_traceframe_block (tframe, sp);
4661 /* Identify block as a memory block. */
4664 /* Record address and size. */
4665 memcpy ((void *) mspace, (void *) &from, sizeof (from));
4666 mspace += sizeof (from);
4667 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
4668 mspace += sizeof (blocklen);
4669 /* Copy the string contents. */
4670 memcpy ((void *) mspace, (void *) buf, blocklen);
4671 remaining -= blocklen;
4678 /* Record the value of a trace state variable. */
4681 agent_tsv_read (struct traceframe *tframe, int n)
4683 unsigned char *vspace;
4686 vspace = add_traceframe_block (tframe,
4687 1 + sizeof (n) + sizeof (LONGEST));
4690 /* Identify block as a variable. */
4692 /* Record variable's number and value. */
4693 memcpy (vspace + 1, &n, sizeof (n));
4694 val = get_trace_state_variable_value (n);
4695 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
4696 trace_debug ("Variable %d recorded", n);
4700 #ifndef IN_PROCESS_AGENT
4702 /* Callback for traceframe_walk_blocks, used to find a given block
4703 type in a traceframe. */
4706 match_blocktype (char blocktype, unsigned char *dataptr, void *data)
4708 char *wantedp = data;
4710 if (*wantedp == blocktype)
4716 /* Walk over all traceframe blocks of the traceframe buffer starting
4717 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4718 block found, passing in DATA unmodified. If CALLBACK returns true,
4719 this returns a pointer to where the block is found. Returns NULL
4720 if no callback call returned true, indicating that all blocks have
4723 static unsigned char *
4724 traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
4726 int (*callback) (char blocktype,
4727 unsigned char *dataptr,
4731 unsigned char *dataptr;
4735 trace_debug ("traceframe %d has no data", tfnum);
4739 /* Iterate through a traceframe's blocks, looking for a block of the
4741 for (dataptr = database;
4742 dataptr < database + datasize;
4746 unsigned short mlen;
4748 if (dataptr == trace_buffer_wrap)
4750 /* Adjust to reflect wrapping part of the frame around to
4752 datasize = dataptr - database;
4753 dataptr = database = trace_buffer_lo;
4756 blocktype = *dataptr++;
4758 if ((*callback) (blocktype, dataptr, data))
4764 /* Skip over the registers block. */
4765 dataptr += register_cache_size ();
4768 /* Skip over the memory block. */
4769 dataptr += sizeof (CORE_ADDR);
4770 memcpy (&mlen, dataptr, sizeof (mlen));
4771 dataptr += (sizeof (mlen) + mlen);
4774 /* Skip over the TSV block. */
4775 dataptr += (sizeof (int) + sizeof (LONGEST));
4778 /* Skip over the static trace data block. */
4779 memcpy (&mlen, dataptr, sizeof (mlen));
4780 dataptr += (sizeof (mlen) + mlen);
4783 trace_debug ("traceframe %d has unknown block type 0x%x",
4792 /* Look for the block of type TYPE_WANTED in the trameframe starting
4793 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4796 static unsigned char *
4797 traceframe_find_block_type (unsigned char *database, unsigned int datasize,
4798 int tfnum, char type_wanted)
4800 return traceframe_walk_blocks (database, datasize, tfnum,
4801 match_blocktype, &type_wanted);
4804 static unsigned char *
4805 traceframe_find_regblock (struct traceframe *tframe, int tfnum)
4807 unsigned char *regblock;
4809 regblock = traceframe_find_block_type (tframe->data,
4813 if (regblock == NULL)
4814 trace_debug ("traceframe %d has no register data", tfnum);
4819 /* Get registers from a traceframe. */
4822 fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
4824 unsigned char *dataptr;
4825 struct tracepoint *tpoint;
4826 struct traceframe *tframe;
4828 tframe = find_traceframe (tfnum);
4832 trace_debug ("traceframe %d not found", tfnum);
4836 dataptr = traceframe_find_regblock (tframe, tfnum);
4837 if (dataptr == NULL)
4839 /* Mark registers unavailable. */
4840 supply_regblock (regcache, NULL);
4842 /* We can generally guess at a PC, although this will be
4843 misleading for while-stepping frames and multi-location
4845 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
4847 regcache_write_pc (regcache, tpoint->address);
4850 supply_regblock (regcache, dataptr);
4856 traceframe_get_pc (struct traceframe *tframe)
4858 struct regcache regcache;
4859 unsigned char *dataptr;
4861 dataptr = traceframe_find_regblock (tframe, -1);
4862 if (dataptr == NULL)
4865 init_register_cache (®cache, dataptr);
4866 return regcache_read_pc (®cache);
4869 /* Read a requested block of memory from a trace frame. */
4872 traceframe_read_mem (int tfnum, CORE_ADDR addr,
4873 unsigned char *buf, ULONGEST length,
4876 struct traceframe *tframe;
4877 unsigned char *database, *dataptr;
4878 unsigned int datasize;
4880 unsigned short mlen;
4882 trace_debug ("traceframe_read_mem");
4884 tframe = find_traceframe (tfnum);
4888 trace_debug ("traceframe %d not found", tfnum);
4892 datasize = tframe->data_size;
4893 database = dataptr = &tframe->data[0];
4895 /* Iterate through a traceframe's blocks, looking for memory. */
4896 while ((dataptr = traceframe_find_block_type (dataptr,
4898 - (dataptr - database),
4899 tfnum, 'M')) != NULL)
4901 memcpy (&maddr, dataptr, sizeof (maddr));
4902 dataptr += sizeof (maddr);
4903 memcpy (&mlen, dataptr, sizeof (mlen));
4904 dataptr += sizeof (mlen);
4905 trace_debug ("traceframe %d has %d bytes at %s",
4906 tfnum, mlen, paddress (maddr));
4908 /* If the block includes the first part of the desired range,
4909 return as much it has; GDB will re-request the remainder,
4910 which might be in a different block of this trace frame. */
4911 if (maddr <= addr && addr < (maddr + mlen))
4913 ULONGEST amt = (maddr + mlen) - addr;
4917 memcpy (buf, dataptr + (addr - maddr), amt);
4922 /* Skip over this block. */
4926 trace_debug ("traceframe %d has no memory data for the desired region",
4934 traceframe_read_tsv (int tsvnum, LONGEST *val)
4937 struct traceframe *tframe;
4938 unsigned char *database, *dataptr;
4939 unsigned int datasize;
4942 trace_debug ("traceframe_read_tsv");
4944 tfnum = current_traceframe;
4948 trace_debug ("no current traceframe");
4952 tframe = find_traceframe (tfnum);
4956 trace_debug ("traceframe %d not found", tfnum);
4960 datasize = tframe->data_size;
4961 database = dataptr = &tframe->data[0];
4963 /* Iterate through a traceframe's blocks, looking for the tsv. */
4964 while ((dataptr = traceframe_find_block_type (dataptr,
4966 - (dataptr - database),
4967 tfnum, 'V')) != NULL)
4969 memcpy (&vnum, dataptr, sizeof (vnum));
4970 dataptr += sizeof (vnum);
4972 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
4974 /* Check that this is the variable we want. */
4977 memcpy (val, dataptr, sizeof (*val));
4981 /* Skip over this block. */
4982 dataptr += sizeof (LONGEST);
4985 trace_debug ("traceframe %d has no data for variable %d",
4990 /* Read a requested block of static tracepoint data from a trace
4994 traceframe_read_sdata (int tfnum, ULONGEST offset,
4995 unsigned char *buf, ULONGEST length,
4998 struct traceframe *tframe;
4999 unsigned char *database, *dataptr;
5000 unsigned int datasize;
5001 unsigned short mlen;
5003 trace_debug ("traceframe_read_sdata");
5005 tframe = find_traceframe (tfnum);
5009 trace_debug ("traceframe %d not found", tfnum);
5013 datasize = tframe->data_size;
5014 database = &tframe->data[0];
5016 /* Iterate through a traceframe's blocks, looking for static
5018 dataptr = traceframe_find_block_type (database, datasize,
5020 if (dataptr != NULL)
5022 memcpy (&mlen, dataptr, sizeof (mlen));
5023 dataptr += sizeof (mlen);
5026 if (offset + length > mlen)
5027 length = mlen - offset;
5029 memcpy (buf, dataptr, length);
5037 trace_debug ("traceframe %d has no static trace data", tfnum);
5043 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5044 object. DATA is pointer to a struct buffer holding the
5045 traceframe-info object being built. */
5048 build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5050 struct buffer *buffer = data;
5056 unsigned short mlen;
5059 memcpy (&maddr, dataptr, sizeof (maddr));
5060 dataptr += sizeof (maddr);
5061 memcpy (&mlen, dataptr, sizeof (mlen));
5062 dataptr += sizeof (mlen);
5063 buffer_xml_printf (buffer,
5064 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5065 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5075 warning ("Unhandled trace block type (%d) '%c ' "
5076 "while building trace frame info.",
5077 blocktype, blocktype);
5084 /* Build a traceframe-info object for traceframe number TFNUM into
5088 traceframe_read_info (int tfnum, struct buffer *buffer)
5090 struct traceframe *tframe;
5092 trace_debug ("traceframe_read_info");
5094 tframe = find_traceframe (tfnum);
5098 trace_debug ("traceframe %d not found", tfnum);
5102 buffer_grow_str (buffer, "<traceframe-info>\n");
5103 traceframe_walk_blocks (tframe->data, tframe->data_size,
5104 tfnum, build_traceframe_info_xml, buffer);
5105 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5109 /* Return the first fast tracepoint whose jump pad contains PC. */
5111 static struct tracepoint *
5112 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5114 struct tracepoint *tpoint;
5116 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5117 if (tpoint->type == fast_tracepoint)
5118 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5124 /* Return the first fast tracepoint whose trampoline contains PC. */
5126 static struct tracepoint *
5127 fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5129 struct tracepoint *tpoint;
5131 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5133 if (tpoint->type == fast_tracepoint
5134 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5141 /* Return GDBserver's tracepoint that matches the IP Agent's
5142 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5145 static struct tracepoint *
5146 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5148 struct tracepoint *tpoint;
5150 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5151 if (tpoint->type == fast_tracepoint)
5152 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5160 /* The type of the object that is used to synchronize fast tracepoint
5163 typedef struct collecting_t
5165 /* The fast tracepoint number currently collecting. */
5168 /* A number that GDBserver can use to identify the thread that is
5169 presently holding the collect lock. This need not (and usually
5170 is not) the thread id, as getting the current thread ID usually
5171 requires a system call, which we want to avoid like the plague.
5172 Usually this is thread's TCB, found in the TLS (pseudo-)
5173 register, which is readable with a single insn on several
5175 uintptr_t thread_area;
5178 #ifndef IN_PROCESS_AGENT
5181 force_unlock_trace_buffer (void)
5183 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5186 /* Check if the thread identified by THREAD_AREA which is stopped at
5187 STOP_PC, is presently locking the fast tracepoint collection, and
5188 if so, gather some status of said collection. Returns 0 if the
5189 thread isn't collecting or in the jump pad at all. 1, if in the
5190 jump pad (or within gdb_collect) and hasn't executed the adjusted
5191 original insn yet (can set a breakpoint there and run to it). 2,
5192 if presently executing the adjusted original insn --- in which
5193 case, if we want to move the thread out of the jump pad, we need to
5194 single-step it until this function returns 0. */
5197 fast_tracepoint_collecting (CORE_ADDR thread_area,
5199 struct fast_tpoint_collect_status *status)
5201 CORE_ADDR ipa_collecting;
5202 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
5203 CORE_ADDR ipa_gdb_trampoline_buffer;
5204 CORE_ADDR ipa_gdb_trampoline_buffer_end;
5205 struct tracepoint *tpoint;
5206 int needs_breakpoint;
5208 /* The thread THREAD_AREA is either:
5210 0. not collecting at all, not within the jump pad, or within
5211 gdb_collect or one of its callees.
5213 1. in the jump pad and haven't reached gdb_collect
5215 2. within gdb_collect (out of the jump pad) (collect is set)
5217 3. we're in the jump pad, after gdb_collect having returned,
5218 possibly executing the adjusted insns.
5220 For cases 1 and 3, `collecting' may or not be set. The jump pad
5221 doesn't have any complicated jump logic, so we can tell if the
5222 thread is executing the adjust original insn or not by just
5223 matching STOP_PC with known jump pad addresses. If we it isn't
5224 yet executing the original insn, set a breakpoint there, and let
5225 the thread run to it, so to quickly step over a possible (many
5226 insns) gdb_collect call. Otherwise, or when the breakpoint is
5227 hit, only a few (small number of) insns are left to be executed
5228 in the jump pad. Single-step the thread until it leaves the
5233 needs_breakpoint = 0;
5234 trace_debug ("fast_tracepoint_collecting");
5236 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5237 &ipa_gdb_jump_pad_buffer))
5238 fatal ("error extracting `gdb_jump_pad_buffer'");
5239 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5240 &ipa_gdb_jump_pad_buffer_end))
5241 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5243 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5244 &ipa_gdb_trampoline_buffer))
5245 fatal ("error extracting `gdb_trampoline_buffer'");
5246 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5247 &ipa_gdb_trampoline_buffer_end))
5248 fatal ("error extracting `gdb_trampoline_buffer_end'");
5250 if (ipa_gdb_jump_pad_buffer <= stop_pc
5251 && stop_pc < ipa_gdb_jump_pad_buffer_end)
5253 /* We can tell which tracepoint(s) the thread is collecting by
5254 matching the jump pad address back to the tracepoint. */
5255 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5258 warning ("in jump pad, but no matching tpoint?");
5263 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5265 tpoint->number, paddress (tpoint->address),
5266 paddress (tpoint->jump_pad),
5267 paddress (tpoint->jump_pad_end),
5268 paddress (tpoint->adjusted_insn_addr),
5269 paddress (tpoint->adjusted_insn_addr_end));
5272 /* Definitely in the jump pad. May or may not need
5273 fast-exit-jump-pad breakpoint. */
5274 if (tpoint->jump_pad <= stop_pc
5275 && stop_pc < tpoint->adjusted_insn_addr)
5276 needs_breakpoint = 1;
5278 else if (ipa_gdb_trampoline_buffer <= stop_pc
5279 && stop_pc < ipa_gdb_trampoline_buffer_end)
5281 /* We can tell which tracepoint(s) the thread is collecting by
5282 matching the trampoline address back to the tracepoint. */
5283 tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5286 warning ("in trampoline, but no matching tpoint?");
5291 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5292 tpoint->number, paddress (tpoint->address),
5293 paddress (tpoint->trampoline),
5294 paddress (tpoint->trampoline_end));
5297 /* Have not reached jump pad yet, but treat the trampoline as a
5298 part of the jump pad that is before the adjusted original
5300 needs_breakpoint = 1;
5304 collecting_t ipa_collecting_obj;
5306 /* If `collecting' is set/locked, then the THREAD_AREA thread
5307 may or not be the one holding the lock. We have to read the
5308 lock to find out. */
5310 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5313 trace_debug ("fast_tracepoint_collecting:"
5314 " failed reading 'collecting' in the inferior");
5318 if (!ipa_collecting)
5320 trace_debug ("fast_tracepoint_collecting: not collecting"
5321 " (and nobody is).");
5325 /* Some thread is collecting. Check which. */
5326 if (read_inferior_memory (ipa_collecting,
5327 (unsigned char *) &ipa_collecting_obj,
5328 sizeof (ipa_collecting_obj)) != 0)
5331 if (ipa_collecting_obj.thread_area != thread_area)
5333 trace_debug ("fast_tracepoint_collecting: not collecting "
5334 "(another thread is)");
5339 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5342 warning ("fast_tracepoint_collecting: collecting, "
5343 "but tpoint %s not found?",
5344 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5348 /* The thread is within `gdb_collect', skip over the rest of
5349 fast tracepoint collection quickly using a breakpoint. */
5350 needs_breakpoint = 1;
5353 /* The caller wants a bit of status detail. */
5356 status->tpoint_num = tpoint->number;
5357 status->tpoint_addr = tpoint->address;
5358 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5359 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5362 if (needs_breakpoint)
5364 /* Hasn't executed the original instruction yet. Set breakpoint
5365 there, and wait till it's hit, then single-step until exiting
5369 fast_tracepoint_collecting, returning continue-until-break at %s",
5370 paddress (tpoint->adjusted_insn_addr));
5372 return 1; /* continue */
5376 /* Just single-step until exiting the jump pad. */
5378 trace_debug ("fast_tracepoint_collecting, returning "
5379 "need-single-step (%s-%s)",
5380 paddress (tpoint->adjusted_insn_addr),
5381 paddress (tpoint->adjusted_insn_addr_end));
5383 return 2; /* single-step */
5389 #ifdef IN_PROCESS_AGENT
5391 /* The global fast tracepoint collect lock. Points to a collecting_t
5392 object built on the stack by the jump pad, if presently locked;
5393 NULL if it isn't locked. Note that this lock *must* be set while
5394 executing any *function other than the jump pad. See
5395 fast_tracepoint_collecting. */
5396 static collecting_t * ATTR_USED collecting;
5398 /* This routine, called from the jump pad (in asm) is designed to be
5399 called from the jump pads of fast tracepoints, thus it is on the
5402 IP_AGENT_EXPORT void ATTR_USED
5403 gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5405 struct fast_tracepoint_ctx ctx;
5407 /* Don't do anything until the trace run is completely set up. */
5411 ctx.base.type = fast_tracepoint;
5413 ctx.regcache_initted = 0;
5414 /* Wrap the regblock in a register cache (in the stack, we don't
5415 want to malloc here). */
5416 ctx.regspace = alloca (register_cache_size ());
5417 if (ctx.regspace == NULL)
5419 trace_debug ("Trace buffer block allocation failed, skipping");
5423 for (ctx.tpoint = tpoint;
5424 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5425 ctx.tpoint = ctx.tpoint->next)
5427 if (!ctx.tpoint->enabled)
5430 /* Multiple tracepoints of different types, such as fast tracepoint and
5431 static tracepoint, can be set at the same address. */
5432 if (ctx.tpoint->type != tpoint->type)
5435 /* Test the condition if present, and collect if true. */
5436 if (ctx.tpoint->cond == NULL
5437 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5440 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5441 ctx.tpoint->address, ctx.tpoint);
5443 /* Note that this will cause original insns to be written back
5444 to where we jumped from, but that's OK because we're jumping
5445 back to the next whole instruction. This will go badly if
5446 instruction restoration is not atomic though. */
5447 if (stopping_tracepoint
5448 || trace_buffer_is_full
5449 || expr_eval_result != expr_eval_no_error)
5457 /* If there was a condition and it evaluated to false, the only
5458 way we would stop tracing is if there was an error during
5459 condition expression evaluation. */
5460 if (expr_eval_result != expr_eval_no_error)
5471 #ifndef IN_PROCESS_AGENT
5474 get_raw_reg_func_addr (void)
5476 return ipa_sym_addrs.addr_get_raw_reg;
5480 get_get_tsv_func_addr (void)
5482 return ipa_sym_addrs.addr_get_trace_state_variable_value;
5486 get_set_tsv_func_addr (void)
5488 return ipa_sym_addrs.addr_set_trace_state_variable_value;
5492 compile_tracepoint_condition (struct tracepoint *tpoint,
5493 CORE_ADDR *jump_entry)
5495 CORE_ADDR entry_point = *jump_entry;
5496 enum eval_result_type err;
5498 trace_debug ("Starting condition compilation for tracepoint %d\n",
5501 /* Initialize the global pointer to the code being built. */
5502 current_insn_ptr = *jump_entry;
5506 err = compile_bytecodes (tpoint->cond);
5508 if (err == expr_eval_no_error)
5512 /* Record the beginning of the compiled code. */
5513 tpoint->compiled_cond = entry_point;
5515 trace_debug ("Condition compilation for tracepoint %d complete\n",
5520 /* Leave the unfinished code in situ, but don't point to it. */
5522 tpoint->compiled_cond = 0;
5524 trace_debug ("Condition compilation for tracepoint %d failed, "
5526 tpoint->number, err);
5529 /* Update the code pointer passed in. Note that we do this even if
5530 the compile fails, so that we can look at the partial results
5531 instead of letting them be overwritten. */
5532 *jump_entry = current_insn_ptr;
5534 /* Leave a gap, to aid dump decipherment. */
5538 /* We'll need to adjust these when we consider bi-arch setups, and big
5542 write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
5544 return write_inferior_memory (where,
5545 (unsigned char *) &ptr, sizeof (void *));
5548 /* The base pointer of the IPA's heap. This is the only memory the
5549 IPA is allowed to use. The IPA should _not_ call the inferior's
5550 `malloc' during operation. That'd be slow, and, most importantly,
5551 it may not be safe. We may be collecting a tracepoint in a signal
5552 handler, for example. */
5553 static CORE_ADDR target_tp_heap;
5555 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5559 target_malloc (ULONGEST size)
5563 if (target_tp_heap == 0)
5565 /* We have the pointer *address*, need what it points to. */
5566 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5568 fatal ("could get target heap head pointer");
5571 ptr = target_tp_heap;
5572 target_tp_heap += size;
5574 /* Pad to 8-byte alignment. */
5575 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5581 download_agent_expr (struct agent_expr *expr)
5583 CORE_ADDR expr_addr;
5584 CORE_ADDR expr_bytes;
5586 expr_addr = target_malloc (sizeof (*expr));
5587 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5589 expr_bytes = target_malloc (expr->length);
5590 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
5592 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
5597 /* Align V up to N bits. */
5598 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5600 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5603 download_tracepoint_1 (struct tracepoint *tpoint)
5605 struct tracepoint target_tracepoint;
5606 CORE_ADDR tpptr = 0;
5608 gdb_assert (tpoint->type == fast_tracepoint
5609 || tpoint->type == static_tracepoint);
5611 if (tpoint->cond != NULL && target_emit_ops () != NULL)
5613 CORE_ADDR jentry, jump_entry;
5615 jentry = jump_entry = get_jump_space_head ();
5617 if (tpoint->cond != NULL)
5619 /* Pad to 8-byte alignment. (needed?) */
5620 /* Actually this should be left for the target to
5622 jentry = UALIGN (jentry, 8);
5624 compile_tracepoint_condition (tpoint, &jentry);
5627 /* Pad to 8-byte alignment. */
5628 jentry = UALIGN (jentry, 8);
5629 claim_jump_space (jentry - jump_entry);
5632 target_tracepoint = *tpoint;
5634 tpptr = target_malloc (sizeof (*tpoint));
5635 tpoint->obj_addr_on_target = tpptr;
5637 /* Write the whole object. We'll fix up its pointers in a bit.
5638 Assume no next for now. This is fixed up above on the next
5639 iteration, if there's any. */
5640 target_tracepoint.next = NULL;
5641 /* Need to clear this here too, since we're downloading the
5642 tracepoints before clearing our own copy. */
5643 target_tracepoint.hit_count = 0;
5645 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
5646 sizeof (target_tracepoint));
5649 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5651 download_agent_expr (tpoint->cond));
5653 if (tpoint->numactions)
5656 CORE_ADDR actions_array;
5658 /* The pointers array. */
5660 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
5661 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5665 /* Now for each pointer, download the action. */
5666 for (i = 0; i < tpoint->numactions; i++)
5668 CORE_ADDR ipa_action = 0;
5669 struct tracepoint_action *action = tpoint->actions[i];
5671 switch (action->type)
5675 = target_malloc (sizeof (struct collect_memory_action));
5676 write_inferior_memory (ipa_action,
5677 (unsigned char *) action,
5678 sizeof (struct collect_memory_action));
5682 = target_malloc (sizeof (struct collect_registers_action));
5683 write_inferior_memory (ipa_action,
5684 (unsigned char *) action,
5685 sizeof (struct collect_registers_action));
5690 struct eval_expr_action *eaction
5691 = (struct eval_expr_action *) action;
5693 ipa_action = target_malloc (sizeof (*eaction));
5694 write_inferior_memory (ipa_action,
5695 (unsigned char *) eaction,
5698 expr = download_agent_expr (eaction->expr);
5699 write_inferior_data_ptr
5700 (ipa_action + offsetof (struct eval_expr_action, expr),
5705 ipa_action = target_malloc
5706 (sizeof (struct collect_static_trace_data_action));
5707 write_inferior_memory
5709 (unsigned char *) action,
5710 sizeof (struct collect_static_trace_data_action));
5713 trace_debug ("unknown trace action '%c', ignoring",
5718 if (ipa_action != 0)
5719 write_inferior_data_ptr
5720 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
5727 download_tracepoint (struct tracepoint *tpoint)
5729 struct tracepoint *tp, *tp_prev;
5731 if (tpoint->type != fast_tracepoint
5732 && tpoint->type != static_tracepoint)
5735 download_tracepoint_1 (tpoint);
5737 /* Find the previous entry of TPOINT, which is fast tracepoint or
5738 static tracepoint. */
5740 for (tp = tracepoints; tp != tpoint; tp = tp->next)
5742 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
5748 CORE_ADDR tp_prev_target_next_addr;
5750 /* Insert TPOINT after TP_PREV in IPA. */
5751 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
5752 + offsetof (struct tracepoint, next),
5753 &tp_prev_target_next_addr))
5754 fatal ("error reading `tp_prev->next'");
5756 /* tpoint->next = tp_prev->next */
5757 write_inferior_data_ptr (tpoint->obj_addr_on_target
5758 + offsetof (struct tracepoint, next),
5759 tp_prev_target_next_addr);
5760 /* tp_prev->next = tpoint */
5761 write_inferior_data_ptr (tp_prev->obj_addr_on_target
5762 + offsetof (struct tracepoint, next),
5763 tpoint->obj_addr_on_target);
5766 /* First object in list, set the head pointer in the
5768 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
5769 tpoint->obj_addr_on_target);
5774 download_tracepoints (void)
5776 CORE_ADDR tpptr = 0, prev_tpptr = 0;
5777 struct tracepoint *tpoint;
5779 /* Start out empty. */
5780 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
5782 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5784 if (tpoint->type != fast_tracepoint
5785 && tpoint->type != static_tracepoint)
5790 download_tracepoint_1 (tpoint);
5792 tpptr = tpoint->obj_addr_on_target;
5794 if (tpoint == tracepoints)
5796 /* First object in list, set the head pointer in the
5798 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
5802 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
5810 download_trace_state_variables (void)
5812 CORE_ADDR ptr = 0, prev_ptr = 0;
5813 struct trace_state_variable *tsv;
5815 /* Start out empty. */
5816 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
5818 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
5820 struct trace_state_variable target_tsv;
5822 /* TSV's with a getter have been initialized equally in both the
5823 inferior and GDBserver. Skip them. */
5824 if (tsv->getter != NULL)
5830 ptr = target_malloc (sizeof (*tsv));
5832 if (tsv == trace_state_variables)
5834 /* First object in list, set the head pointer in the
5837 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
5842 write_inferior_data_ptr (prev_ptr
5843 + offsetof (struct trace_state_variable,
5848 /* Write the whole object. We'll fix up its pointers in a bit.
5849 Assume no next, fixup when needed. */
5850 target_tsv.next = NULL;
5852 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
5853 sizeof (target_tsv));
5855 if (tsv->name != NULL)
5857 size_t size = strlen (tsv->name) + 1;
5858 CORE_ADDR name_addr = target_malloc (size);
5859 write_inferior_memory (name_addr,
5860 (unsigned char *) tsv->name, size);
5861 write_inferior_data_ptr (ptr
5862 + offsetof (struct trace_state_variable,
5867 if (tsv->getter != NULL)
5869 fatal ("what to do with these?");
5875 /* Fixup the next pointer in the last item in the list. */
5876 write_inferior_data_ptr (prev_ptr
5877 + offsetof (struct trace_state_variable,
5882 /* Upload complete trace frames out of the IP Agent's trace buffer
5883 into GDBserver's trace buffer. This always uploads either all or
5884 no trace frames. This is the counter part of
5885 `trace_alloc_trace_buffer'. See its description of the atomic
5886 synching mechanism. */
5889 upload_fast_traceframes (void)
5891 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
5892 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
5894 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
5895 unsigned int curr_tbctrl_idx;
5896 unsigned int ipa_trace_buffer_ctrl_curr;
5897 unsigned int ipa_trace_buffer_ctrl_curr_old;
5898 CORE_ADDR ipa_trace_buffer_ctrl_addr;
5899 struct breakpoint *about_to_request_buffer_space_bkpt;
5900 CORE_ADDR ipa_trace_buffer_lo;
5901 CORE_ADDR ipa_trace_buffer_hi;
5903 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5904 &ipa_traceframe_read_count_racy))
5906 /* This will happen in most targets if the current thread is
5911 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5912 &ipa_traceframe_write_count_racy))
5915 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5916 ipa_traceframe_write_count_racy
5917 - ipa_traceframe_read_count_racy,
5918 ipa_traceframe_write_count_racy,
5919 ipa_traceframe_read_count_racy);
5921 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
5924 about_to_request_buffer_space_bkpt
5925 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
5928 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5929 &ipa_trace_buffer_ctrl_curr))
5932 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
5934 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
5937 unsigned int prev, counter;
5939 /* Update the token, with new counters, and the GDBserver stamp
5940 bit. Alway reuse the current TBC index. */
5941 prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
5942 counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
5944 ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
5950 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5951 ipa_trace_buffer_ctrl_curr))
5954 trace_debug ("Lib: Committed %08x -> %08x",
5955 ipa_trace_buffer_ctrl_curr_old,
5956 ipa_trace_buffer_ctrl_curr);
5958 /* Re-read these, now that we've installed the
5959 `about_to_request_buffer_space' breakpoint/lock. A thread could
5960 have finished a traceframe between the last read of these
5961 counters and setting the breakpoint above. If we start
5962 uploading, we never want to leave this function with
5963 traceframe_read_count != 0, otherwise, GDBserver could end up
5964 incrementing the counter tokens more than once (due to event loop
5965 nesting), which would break the IP agent's "effective" detection
5966 (see trace_alloc_trace_buffer). */
5967 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5968 &ipa_traceframe_read_count))
5970 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5971 &ipa_traceframe_write_count))
5976 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5977 ipa_traceframe_write_count - ipa_traceframe_read_count,
5978 ipa_traceframe_write_count, ipa_traceframe_read_count);
5980 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
5981 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
5982 trace_debug ("note that ipa_traceframe_count's parts changed");
5985 /* Get the address of the current TBC object (the IP agent has an
5986 array of 3 such objects). The index is stored in the TBC
5988 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
5989 ipa_trace_buffer_ctrl_addr
5990 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
5992 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
5993 (unsigned char *) &ipa_trace_buffer_ctrl,
5994 sizeof (struct ipa_trace_buffer_control)))
5997 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
5998 &ipa_trace_buffer_lo))
6000 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6001 &ipa_trace_buffer_hi))
6004 /* Offsets are easier to grok for debugging than raw addresses,
6005 especially for the small trace buffer sizes that are useful for
6007 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6008 "endfree=%d wrap=%d hi=%d",
6010 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6011 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6012 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6013 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6014 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6016 /* Note that the IPA's buffer is always circular. */
6018 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6020 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6021 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6023 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6024 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6025 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6026 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6029 tf = IPA_FIRST_TRACEFRAME ();
6031 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6033 struct tracepoint *tpoint;
6034 struct traceframe *tframe;
6035 unsigned char *block;
6036 struct traceframe ipa_tframe;
6038 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6039 offsetof (struct traceframe, data)))
6040 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6042 if (ipa_tframe.tpnum == 0)
6043 fatal ("Uploading: No (more) fast traceframes, but "
6044 "ipa_traceframe_count == %u??\n",
6045 ipa_traceframe_write_count - ipa_traceframe_read_count);
6047 /* Note that this will be incorrect for multi-location
6049 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6051 tframe = add_traceframe (tpoint);
6054 trace_buffer_is_full = 1;
6055 trace_debug ("Uploading: trace buffer is full");
6059 /* Copy the whole set of blocks in one go for now. FIXME:
6060 split this in smaller blocks. */
6061 block = add_traceframe_block (tframe, ipa_tframe.data_size);
6064 if (read_inferior_memory (tf
6065 + offsetof (struct traceframe, data),
6066 block, ipa_tframe.data_size))
6067 error ("Uploading: Couldn't read traceframe data at %s\n",
6068 paddress (tf + offsetof (struct traceframe, data)));
6071 trace_debug ("Uploading: traceframe didn't fit");
6072 finish_traceframe (tframe);
6075 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6077 /* If we freed the traceframe that wrapped around, go back
6078 to the non-wrap case. */
6079 if (tf < ipa_trace_buffer_ctrl.start)
6081 trace_debug ("Lib: Discarding past the wraparound");
6082 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6084 ipa_trace_buffer_ctrl.start = tf;
6085 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6086 ++ipa_traceframe_read_count;
6088 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6089 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6091 trace_debug ("Lib: buffer is fully empty. "
6092 "Trace buffer [%d] start=%d free=%d endfree=%d",
6094 (int) (ipa_trace_buffer_ctrl.start
6095 - ipa_trace_buffer_lo),
6096 (int) (ipa_trace_buffer_ctrl.free
6097 - ipa_trace_buffer_lo),
6098 (int) (ipa_trace_buffer_ctrl.end_free
6099 - ipa_trace_buffer_lo));
6101 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6102 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6103 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6104 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6107 trace_debug ("Uploaded a traceframe\n"
6108 "Lib: Trace buffer [%d] start=%d free=%d "
6109 "endfree=%d wrap=%d hi=%d",
6111 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6112 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6113 (int) (ipa_trace_buffer_ctrl.end_free
6114 - ipa_trace_buffer_lo),
6115 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6116 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6119 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6120 (unsigned char *) &ipa_trace_buffer_ctrl,
6121 sizeof (struct ipa_trace_buffer_control)))
6124 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6125 ipa_traceframe_read_count);
6127 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6130 cancel_breakpoints ();
6132 delete_breakpoint (about_to_request_buffer_space_bkpt);
6133 about_to_request_buffer_space_bkpt = NULL;
6137 if (trace_buffer_is_full)
6142 #ifdef IN_PROCESS_AGENT
6144 IP_AGENT_EXPORT int ust_loaded;
6145 IP_AGENT_EXPORT char cmd_buf[IPA_CMD_BUF_SIZE];
6149 /* Static tracepoints. */
6151 /* UST puts a "struct tracepoint" in the global namespace, which
6152 conflicts with our tracepoint. Arguably, being a library, it
6153 shouldn't take ownership of such a generic name. We work around it
6155 #define tracepoint ust_tracepoint
6156 #include <ust/ust.h>
6159 extern int serialize_to_text (char *outbuf, int bufsize,
6160 const char *fmt, va_list ap);
6162 #define GDB_PROBE_NAME "gdb"
6164 /* We dynamically search for the UST symbols instead of linking them
6165 in. This lets the user decide if the application uses static
6166 tracepoints, instead of always pulling libust.so in. This vector
6167 holds pointers to all functions we care about. */
6171 int (*serialize_to_text) (char *outbuf, int bufsize,
6172 const char *fmt, va_list ap);
6174 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6175 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6177 int (*ltt_marker_connect) (const char *channel, const char *mname,
6179 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6182 void (*marker_iter_start) (struct marker_iter *iter);
6183 void (*marker_iter_next) (struct marker_iter *iter);
6184 void (*marker_iter_stop) (struct marker_iter *iter);
6185 void (*marker_iter_reset) (struct marker_iter *iter);
6190 /* Cast through typeof to catch incompatible API changes. Since UST
6191 only builds with gcc, we can freely use gcc extensions here
6193 #define GET_UST_SYM(SYM) \
6196 if (ust_ops.SYM == NULL) \
6197 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6198 if (ust_ops.SYM == NULL) \
6202 #define USTF(SYM) ust_ops.SYM
6204 /* Get pointers to all libust.so functions we care about. */
6209 GET_UST_SYM (serialize_to_text);
6211 GET_UST_SYM (ltt_probe_register);
6212 GET_UST_SYM (ltt_probe_unregister);
6213 GET_UST_SYM (ltt_marker_connect);
6214 GET_UST_SYM (ltt_marker_disconnect);
6216 GET_UST_SYM (marker_iter_start);
6217 GET_UST_SYM (marker_iter_next);
6218 GET_UST_SYM (marker_iter_stop);
6219 GET_UST_SYM (marker_iter_reset);
6225 /* Given an UST marker, return the matching gdb static tracepoint.
6226 The match is done by address. */
6228 static struct tracepoint *
6229 ust_marker_to_static_tracepoint (const struct marker *mdata)
6231 struct tracepoint *tpoint;
6233 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6235 if (tpoint->type != static_tracepoint)
6238 if (tpoint->address == (uintptr_t) mdata->location)
6245 /* The probe function we install on lttng/ust markers. Whenever a
6246 probed ust marker is hit, this function is called. This is similar
6247 to gdb_collect, only for static tracepoints, instead of fast
6251 gdb_probe (const struct marker *mdata, void *probe_private,
6252 struct registers *regs, void *call_private,
6253 const char *fmt, va_list *args)
6255 struct tracepoint *tpoint;
6256 struct static_tracepoint_ctx ctx;
6258 /* Don't do anything until the trace run is completely set up. */
6261 trace_debug ("gdb_probe: not tracing\n");
6265 ctx.base.type = static_tracepoint;
6266 ctx.regcache_initted = 0;
6271 /* Wrap the regblock in a register cache (in the stack, we don't
6272 want to malloc here). */
6273 ctx.regspace = alloca (register_cache_size ());
6274 if (ctx.regspace == NULL)
6276 trace_debug ("Trace buffer block allocation failed, skipping");
6280 tpoint = ust_marker_to_static_tracepoint (mdata);
6283 trace_debug ("gdb_probe: marker not known: "
6284 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6285 mdata->location, mdata->channel,
6286 mdata->name, mdata->format);
6290 if (!tpoint->enabled)
6292 trace_debug ("gdb_probe: tracepoint disabled");
6296 ctx.tpoint = tpoint;
6298 trace_debug ("gdb_probe: collecting marker: "
6299 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6300 mdata->location, mdata->channel,
6301 mdata->name, mdata->format);
6303 /* Test the condition if present, and collect if true. */
6304 if (tpoint->cond == NULL
6305 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6308 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6309 tpoint->address, tpoint);
6311 if (stopping_tracepoint
6312 || trace_buffer_is_full
6313 || expr_eval_result != expr_eval_no_error)
6318 /* If there was a condition and it evaluated to false, the only
6319 way we would stop tracing is if there was an error during
6320 condition expression evaluation. */
6321 if (expr_eval_result != expr_eval_no_error)
6326 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6327 static tracepoint string data. This is a string passed to the
6328 tracing library by the user, at the time of the tracepoint marker
6329 call. E.g., in the UST marker call:
6331 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6333 the collected data is "str FOOBAZ".
6337 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
6338 struct traceframe *tframe)
6340 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
6341 unsigned char *bufspace;
6344 unsigned short blocklen;
6348 trace_debug ("Wanted to collect static trace data, "
6349 "but there's no static trace data");
6353 va_copy (copy, *umd->args);
6354 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
6357 trace_debug ("Want to collect ust data");
6359 /* 'S' + size + string */
6360 bufspace = add_traceframe_block (tframe,
6361 1 + sizeof (blocklen) + size + 1);
6362 if (bufspace == NULL)
6364 trace_debug ("Trace buffer block allocation failed, skipping");
6368 /* Identify a static trace data block. */
6371 blocklen = size + 1;
6372 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
6374 va_copy (copy, *umd->args);
6375 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
6376 size + 1, umd->fmt, copy);
6379 trace_debug ("Storing static tracepoint data in regblock: %s",
6380 bufspace + 1 + sizeof (blocklen));
6383 /* The probe to register with lttng/ust. */
6384 static struct ltt_available_probe gdb_ust_probe =
6391 #endif /* HAVE_UST */
6392 #endif /* IN_PROCESS_AGENT */
6394 #ifndef IN_PROCESS_AGENT
6396 /* Ask the in-process agent to run a command. Since we don't want to
6397 have to handle the IPA hitting breakpoints while running the
6398 command, we pause all threads, remove all breakpoints, and then set
6399 the helper thread re-running. We communicate with the helper
6400 thread by means of direct memory xfering, and a socket for
6404 run_inferior_command (char *cmd)
6407 int pid = ptid_get_pid (current_inferior->entry.id);
6409 trace_debug ("run_inferior_command: running: %s", cmd);
6412 uninsert_all_breakpoints ();
6414 err = agent_run_command (pid, (const char *) cmd);
6416 reinsert_all_breakpoints ();
6422 #else /* !IN_PROCESS_AGENT */
6424 #include <sys/socket.h>
6427 #ifndef UNIX_PATH_MAX
6428 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6431 /* Where we put the socked used for synchronization. */
6432 #define SOCK_DIR P_tmpdir
6434 /* Thread ID of the helper thread. GDBserver reads this to know which
6435 is the help thread. This is an LWP id on Linux. */
6436 int helper_thread_id;
6439 init_named_socket (const char *name)
6442 struct sockaddr_un addr;
6444 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
6447 warning ("socket creation failed: %s", strerror (errno));
6451 addr.sun_family = AF_UNIX;
6453 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
6454 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
6456 result = access (name, F_OK);
6460 result = unlink (name);
6463 warning ("unlink failed: %s", strerror (errno));
6467 warning ("socket %s already exists; overwriting", name);
6470 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
6473 warning ("bind failed: %s", strerror (errno));
6478 result = listen (fd, 1);
6481 warning ("listen: %s", strerror (errno));
6490 gdb_agent_socket_init (void)
6493 char name[UNIX_PATH_MAX];
6495 result = xsnprintf (name, UNIX_PATH_MAX, "%s/gdb_ust%d",
6496 SOCK_DIR, getpid ());
6497 if (result >= UNIX_PATH_MAX)
6499 trace_debug ("string overflow allocating socket name");
6503 fd = init_named_socket (name);
6505 warning ("Error initializing named socket (%s) for communication with the "
6506 "ust helper thread. Check that directory exists and that it "
6507 "is writable.", name);
6514 /* The next marker to be returned on a qTsSTM command. */
6515 static const struct marker *next_st;
6517 /* Returns the first known marker. */
6522 struct marker_iter iter;
6524 USTF(marker_iter_reset) (&iter);
6525 USTF(marker_iter_start) (&iter);
6530 /* Returns the marker following M. */
6532 const struct marker *
6533 next_marker (const struct marker *m)
6535 struct marker_iter iter;
6537 USTF(marker_iter_reset) (&iter);
6538 USTF(marker_iter_start) (&iter);
6540 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6542 if (iter.marker == m)
6544 USTF(marker_iter_next) (&iter);
6552 /* Return an hexstr version of the STR C string, fit for sending to
6556 cstr_to_hexstr (const char *str)
6558 int len = strlen (str);
6559 char *hexstr = xmalloc (len * 2 + 1);
6560 convert_int_to_ascii ((gdb_byte *) str, hexstr, len);
6564 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6568 response_ust_marker (char *packet, const struct marker *st)
6570 char *strid, *format, *tmp;
6572 next_st = next_marker (st);
6574 tmp = xmalloc (strlen (st->channel) + 1 +
6575 strlen (st->name) + 1);
6576 sprintf (tmp, "%s/%s", st->channel, st->name);
6578 strid = cstr_to_hexstr (tmp);
6581 format = cstr_to_hexstr (st->format);
6583 sprintf (packet, "m%s:%s:%s",
6584 paddress ((uintptr_t) st->location),
6592 /* Return the first static tracepoint, and initialize the state
6593 machine that will iterate through all the static tracepoints. */
6596 cmd_qtfstm (char *packet)
6598 trace_debug ("Returning first trace state variable definition");
6600 if (first_marker ())
6601 response_ust_marker (packet, first_marker ());
6603 strcpy (packet, "l");
6606 /* Return additional trace state variable definitions. */
6609 cmd_qtsstm (char *packet)
6611 trace_debug ("Returning static tracepoint");
6614 response_ust_marker (packet, next_st);
6616 strcpy (packet, "l");
6619 /* Disconnect the GDB probe from a marker at a given address. */
6622 unprobe_marker_at (char *packet)
6626 struct marker_iter iter;
6628 p += sizeof ("unprobe_marker_at:") - 1;
6630 p = unpack_varlen_hex (p, &address);
6632 USTF(marker_iter_reset) (&iter);
6633 USTF(marker_iter_start) (&iter);
6634 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6635 if ((uintptr_t ) iter.marker->location == address)
6639 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
6643 warning ("could not disable marker %s/%s",
6644 iter.marker->channel, iter.marker->name);
6649 /* Connect the GDB probe to a marker at a given address. */
6652 probe_marker_at (char *packet)
6656 struct marker_iter iter;
6659 p += sizeof ("probe_marker_at:") - 1;
6661 p = unpack_varlen_hex (p, &address);
6663 USTF(marker_iter_reset) (&iter);
6665 for (USTF(marker_iter_start) (&iter), m = iter.marker;
6667 USTF(marker_iter_next) (&iter), m = iter.marker)
6668 if ((uintptr_t ) m->location == address)
6672 trace_debug ("found marker for address. "
6673 "ltt_marker_connect (marker = %s/%s)",
6674 m->channel, m->name);
6676 result = USTF(ltt_marker_connect) (m->channel, m->name,
6678 if (result && result != -EEXIST)
6679 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6680 m->channel, m->name, -result);
6684 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
6685 m->channel, m->name);
6689 strcpy (packet, "OK");
6693 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
6698 cmd_qtstmat (char *packet)
6702 struct marker_iter iter;
6705 p += sizeof ("qTSTMat:") - 1;
6707 p = unpack_varlen_hex (p, &address);
6709 USTF(marker_iter_reset) (&iter);
6711 for (USTF(marker_iter_start) (&iter), m = iter.marker;
6713 USTF(marker_iter_next) (&iter), m = iter.marker)
6714 if ((uintptr_t ) m->location == address)
6716 response_ust_marker (packet, m);
6720 strcpy (packet, "l");
6730 USTF(ltt_probe_register) (&gdb_ust_probe);
6733 #endif /* HAVE_UST */
6735 #include <sys/syscall.h>
6737 /* Helper thread of agent. */
6740 gdb_agent_helper_thread (void *arg)
6746 listen_fd = gdb_agent_socket_init ();
6748 if (helper_thread_id == 0)
6749 helper_thread_id = syscall (SYS_gettid);
6751 if (listen_fd == -1)
6753 warning ("could not create sync socket\n");
6760 struct sockaddr_un sockaddr;
6765 tmp = sizeof (sockaddr);
6769 fd = accept (listen_fd, &sockaddr, &tmp);
6771 /* It seems an ERESTARTSYS can escape out of accept. */
6772 while (fd == -512 || (fd == -1 && errno == EINTR));
6776 warning ("Accept returned %d, error: %s\n",
6777 fd, strerror (errno));
6783 ret = read (fd, buf, 1);
6784 } while (ret == -1 && errno == EINTR);
6788 warning ("reading socket (fd=%d) failed with %s",
6789 fd, strerror (errno));
6797 if (strcmp ("qTfSTM", cmd_buf) == 0)
6799 cmd_qtfstm (cmd_buf);
6801 else if (strcmp ("qTsSTM", cmd_buf) == 0)
6803 cmd_qtsstm (cmd_buf);
6805 else if (strncmp ("unprobe_marker_at:",
6807 sizeof ("unprobe_marker_at:") - 1) == 0)
6809 unprobe_marker_at (cmd_buf);
6811 else if (strncmp ("probe_marker_at:",
6813 sizeof ("probe_marker_at:") - 1) == 0)
6815 probe_marker_at (cmd_buf);
6817 else if (strncmp ("qTSTMat:",
6819 sizeof ("qTSTMat:") - 1) == 0)
6821 cmd_qtstmat (cmd_buf);
6823 #endif /* HAVE_UST */
6826 /* Fix compiler's warning: ignoring return value of 'write'. */
6827 ret = write (fd, buf, 1);
6836 #include <pthread.h>
6838 IP_AGENT_EXPORT int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
6841 gdb_agent_init (void)
6848 /* We want the helper thread to be as transparent as possible, so
6849 have it inherit an all-signals-blocked mask. */
6851 sigfillset (&new_mask);
6852 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
6854 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
6856 res = pthread_create (&thread,
6858 gdb_agent_helper_thread,
6861 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
6863 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
6865 while (helper_thread_id == 0)
6873 #include <sys/mman.h>
6876 IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
6877 IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
6878 IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
6879 IP_AGENT_EXPORT char *gdb_trampoline_buffer;
6880 IP_AGENT_EXPORT char *gdb_trampoline_buffer_end;
6881 IP_AGENT_EXPORT char *gdb_trampoline_buffer_error;
6883 /* Record the result of getting buffer space for fast tracepoint
6884 trampolines. Any error message is copied, since caller may not be
6885 using persistent storage. */
6888 set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
6890 gdb_trampoline_buffer = (char *) (uintptr_t) begin;
6891 gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
6893 strncpy (gdb_trampoline_buffer_error, errmsg, 99);
6895 strcpy (gdb_trampoline_buffer_error, "no buffer passed");
6898 static void __attribute__ ((constructor))
6899 initialize_tracepoint_ftlib (void)
6901 initialize_tracepoint ();
6906 #endif /* IN_PROCESS_AGENT */
6908 /* Return a timestamp, expressed as microseconds of the usual Unix
6909 time. (As the result is a 64-bit number, it will not overflow any
6913 get_timestamp (void)
6917 if (gettimeofday (&tv, 0) != 0)
6920 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
6924 initialize_tracepoint (void)
6926 /* There currently no way to change the buffer size. */
6927 const int sizeOfBuffer = 5 * 1024 * 1024;
6928 unsigned char *buf = xmalloc (sizeOfBuffer);
6929 init_trace_buffer (buf, sizeOfBuffer);
6931 /* Wire trace state variable 1 to be the timestamp. This will be
6932 uploaded to GDB upon connection and become one of its trace state
6933 variables. (In case you're wondering, if GDB already has a trace
6934 variable numbered 1, it will be renumbered.) */
6935 create_trace_state_variable (1, 0);
6936 set_trace_state_variable_name (1, "trace_timestamp");
6937 set_trace_state_variable_getter (1, get_timestamp);
6939 #ifdef IN_PROCESS_AGENT
6944 pagesize = sysconf (_SC_PAGE_SIZE);
6948 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
6950 #define SCRATCH_BUFFER_NPAGES 20
6952 /* Allocate scratch buffer aligned on a page boundary, at a low
6953 address (close to the main executable's code). */
6954 for (addr = pagesize; addr != 0; addr += pagesize)
6956 gdb_jump_pad_buffer = mmap ((void *) addr, pagesize * SCRATCH_BUFFER_NPAGES,
6957 PROT_READ | PROT_WRITE | PROT_EXEC,
6958 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
6960 if (gdb_jump_pad_buffer != MAP_FAILED)
6966 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
6969 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * SCRATCH_BUFFER_NPAGES;
6972 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
6974 /* It's not a fatal error for something to go wrong with trampoline
6975 buffer setup, but it can be mysterious, so create a channel to
6976 report back on what went wrong, using a fixed size since we may
6977 not be able to allocate space later when the problem occurs. */
6978 gdb_trampoline_buffer_error = xmalloc (IPA_BUFSIZ);
6980 strcpy (gdb_trampoline_buffer_error, "No errors reported");
6982 initialize_low_tracepoint ();