1 /* Tracepoint code for remote server for GDB.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 /* This file is built for both GDBserver, and the in-process
33 agent (IPA), a shared library that includes a tracing agent that is
34 loaded by the inferior to support fast tracepoints. Fast
35 tracepoints (or more accurately, jump based tracepoints) are
36 implemented by patching the tracepoint location with a jump into a
37 small trampoline function whose job is to save the register state,
38 call the in-process tracing agent, and then execute the original
39 instruction that was under the tracepoint jump (possibly adjusted,
40 if PC-relative, or some such).
42 The current synchronization design is pull based. That means,
43 GDBserver does most of the work, by peeking/poking at the inferior
44 agent's memory directly for downloading tracepoint and associated
45 objects, and for uploading trace frames. Whenever the IPA needs
46 something from GDBserver (trace buffer is full, tracing stopped for
47 some reason, etc.) the IPA calls a corresponding hook function
48 where GDBserver has placed a breakpoint.
50 Each of the agents has its own trace buffer. When browsing the
51 trace frames built from slow and fast tracepoints from GDB (tfind
52 mode), there's no guarantee the user is seeing the trace frames in
53 strict chronological creation order, although, GDBserver tries to
54 keep the order relatively reasonable, by syncing the trace buffers
59 static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf, 1, 2);
62 trace_vdebug (const char *fmt, ...)
68 vsprintf (buf, fmt, ap);
69 fprintf (stderr, PROG "/tracepoint: %s\n", buf);
73 #define trace_debug_1(level, fmt, args...) \
75 if (level <= debug_threads) \
76 trace_vdebug ((fmt), ##args); \
79 #define trace_debug(FMT, args...) \
80 trace_debug_1 (1, FMT, ##args)
83 # define ATTR_USED __attribute__((used))
84 # define ATTR_NOINLINE __attribute__((noinline))
85 # define ATTR_CONSTRUCTOR __attribute__ ((constructor))
88 # define ATTR_NOINLINE
89 # define ATTR_CONSTRUCTOR
92 /* Make sure the functions the IPA needs to export (symbols GDBserver
93 needs to query GDB about) are exported. */
95 #ifdef IN_PROCESS_AGENT
96 # if defined _WIN32 || defined __CYGWIN__
97 # define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
100 # define IP_AGENT_EXPORT \
101 __attribute__ ((visibility("default"))) ATTR_USED
103 # define IP_AGENT_EXPORT ATTR_USED
107 # define IP_AGENT_EXPORT
110 /* Prefix exported symbols, for good citizenship. All the symbols
111 that need exporting are defined in this module. */
112 #ifdef IN_PROCESS_AGENT
113 # define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
114 # define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
115 # define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
116 # define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
117 # define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
118 # define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
119 # define collecting gdb_agent_collecting
120 # define gdb_collect gdb_agent_gdb_collect
121 # define stop_tracing gdb_agent_stop_tracing
122 # define flush_trace_buffer gdb_agent_flush_trace_buffer
123 # define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
124 # define trace_buffer_is_full gdb_agent_trace_buffer_is_full
125 # define stopping_tracepoint gdb_agent_stopping_tracepoint
126 # define expr_eval_result gdb_agent_expr_eval_result
127 # define error_tracepoint gdb_agent_error_tracepoint
128 # define tracepoints gdb_agent_tracepoints
129 # define tracing gdb_agent_tracing
130 # define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
131 # define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
132 # define trace_buffer_lo gdb_agent_trace_buffer_lo
133 # define trace_buffer_hi gdb_agent_trace_buffer_hi
134 # define traceframe_read_count gdb_agent_traceframe_read_count
135 # define traceframe_write_count gdb_agent_traceframe_write_count
136 # define traceframes_created gdb_agent_traceframes_created
137 # define trace_state_variables gdb_agent_trace_state_variables
138 # define get_raw_reg gdb_agent_get_raw_reg
139 # define get_trace_state_variable_value \
140 gdb_agent_get_trace_state_variable_value
141 # define set_trace_state_variable_value \
142 gdb_agent_set_trace_state_variable_value
143 # define ust_loaded gdb_agent_ust_loaded
144 # define helper_thread_id gdb_agent_helper_thread_id
145 # define cmd_buf gdb_agent_cmd_buf
148 #ifndef IN_PROCESS_AGENT
150 /* Addresses of in-process agent's symbols GDBserver cares about. */
152 struct ipa_sym_addresses
154 CORE_ADDR addr_gdb_tp_heap_buffer;
155 CORE_ADDR addr_gdb_jump_pad_buffer;
156 CORE_ADDR addr_gdb_jump_pad_buffer_end;
157 CORE_ADDR addr_gdb_trampoline_buffer;
158 CORE_ADDR addr_gdb_trampoline_buffer_end;
159 CORE_ADDR addr_gdb_trampoline_buffer_error;
160 CORE_ADDR addr_collecting;
161 CORE_ADDR addr_gdb_collect;
162 CORE_ADDR addr_stop_tracing;
163 CORE_ADDR addr_flush_trace_buffer;
164 CORE_ADDR addr_about_to_request_buffer_space;
165 CORE_ADDR addr_trace_buffer_is_full;
166 CORE_ADDR addr_stopping_tracepoint;
167 CORE_ADDR addr_expr_eval_result;
168 CORE_ADDR addr_error_tracepoint;
169 CORE_ADDR addr_tracepoints;
170 CORE_ADDR addr_tracing;
171 CORE_ADDR addr_trace_buffer_ctrl;
172 CORE_ADDR addr_trace_buffer_ctrl_curr;
173 CORE_ADDR addr_trace_buffer_lo;
174 CORE_ADDR addr_trace_buffer_hi;
175 CORE_ADDR addr_traceframe_read_count;
176 CORE_ADDR addr_traceframe_write_count;
177 CORE_ADDR addr_traceframes_created;
178 CORE_ADDR addr_trace_state_variables;
179 CORE_ADDR addr_get_raw_reg;
180 CORE_ADDR addr_get_trace_state_variable_value;
181 CORE_ADDR addr_set_trace_state_variable_value;
182 CORE_ADDR addr_ust_loaded;
191 IPA_SYM(gdb_tp_heap_buffer),
192 IPA_SYM(gdb_jump_pad_buffer),
193 IPA_SYM(gdb_jump_pad_buffer_end),
194 IPA_SYM(gdb_trampoline_buffer),
195 IPA_SYM(gdb_trampoline_buffer_end),
196 IPA_SYM(gdb_trampoline_buffer_error),
198 IPA_SYM(gdb_collect),
199 IPA_SYM(stop_tracing),
200 IPA_SYM(flush_trace_buffer),
201 IPA_SYM(about_to_request_buffer_space),
202 IPA_SYM(trace_buffer_is_full),
203 IPA_SYM(stopping_tracepoint),
204 IPA_SYM(expr_eval_result),
205 IPA_SYM(error_tracepoint),
206 IPA_SYM(tracepoints),
208 IPA_SYM(trace_buffer_ctrl),
209 IPA_SYM(trace_buffer_ctrl_curr),
210 IPA_SYM(trace_buffer_lo),
211 IPA_SYM(trace_buffer_hi),
212 IPA_SYM(traceframe_read_count),
213 IPA_SYM(traceframe_write_count),
214 IPA_SYM(traceframes_created),
215 IPA_SYM(trace_state_variables),
216 IPA_SYM(get_raw_reg),
217 IPA_SYM(get_trace_state_variable_value),
218 IPA_SYM(set_trace_state_variable_value),
222 static struct ipa_sym_addresses ipa_sym_addrs;
224 static int read_inferior_integer (CORE_ADDR symaddr, int *val);
226 /* Returns true if both the in-process agent library and the static
227 tracepoints libraries are loaded in the inferior, and agent has
228 capability on static tracepoints. */
231 in_process_agent_supports_ust (void)
235 if (!agent_loaded_p ())
237 warning ("In-process agent not loaded");
241 if (agent_capability_check (AGENT_CAPA_STATIC_TRACE))
243 /* Agent understands static tracepoint, then check whether UST is in
244 fact loaded in the inferior. */
245 if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
247 warning ("Error reading ust_loaded in lib");
258 write_e_ipa_not_loaded (char *buffer)
261 "E.In-process agent library not loaded in process. "
262 "Fast and static tracepoints unavailable.");
265 /* Write an error to BUFFER indicating that UST isn't loaded in the
269 write_e_ust_not_loaded (char *buffer)
273 "E.UST library not loaded in process. "
274 "Static tracepoints unavailable.");
276 sprintf (buffer, "E.GDBserver was built without static tracepoints support");
280 /* If the in-process agent library isn't loaded in the inferior, write
281 an error to BUFFER, and return 1. Otherwise, return 0. */
284 maybe_write_ipa_not_loaded (char *buffer)
286 if (!agent_loaded_p ())
288 write_e_ipa_not_loaded (buffer);
294 /* If the in-process agent library and the ust (static tracepoints)
295 library aren't loaded in the inferior, write an error to BUFFER,
296 and return 1. Otherwise, return 0. */
299 maybe_write_ipa_ust_not_loaded (char *buffer)
301 if (!agent_loaded_p ())
303 write_e_ipa_not_loaded (buffer);
306 else if (!in_process_agent_supports_ust ())
308 write_e_ust_not_loaded (buffer);
314 /* Cache all future symbols that the tracepoints module might request.
315 We can not request symbols at arbitrary states in the remote
316 protocol, only when the client tells us that new symbols are
317 available. So when we load the in-process library, make sure to
318 check the entire list. */
321 tracepoint_look_up_symbols (void)
325 if (agent_loaded_p ())
328 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
331 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
333 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
336 fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
341 agent_look_up_symbols ();
346 /* GDBserver places a breakpoint on the IPA's version (which is a nop)
347 of the "stop_tracing" function. When this breakpoint is hit,
348 tracing stopped in the IPA for some reason. E.g., due to
349 tracepoint reaching the pass count, hitting conditional expression
350 evaluation error, etc.
352 The IPA's trace buffer is never in circular tracing mode: instead,
353 GDBserver's is, and whenever the in-process buffer fills, it calls
354 "flush_trace_buffer", which triggers an internal breakpoint.
355 GDBserver reacts to this breakpoint by pulling the meanwhile
356 collected data. Old frames discarding is always handled on the
359 #ifdef IN_PROCESS_AGENT
361 read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
363 memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
367 /* Call this in the functions where GDBserver places a breakpoint, so
368 that the compiler doesn't try to be clever and skip calling the
369 function at all. This is necessary, even if we tell the compiler
370 to not inline said functions. */
372 #if defined(__GNUC__)
373 # define UNKNOWN_SIDE_EFFECTS() asm ("")
375 # define UNKNOWN_SIDE_EFFECTS() do {} while (0)
378 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
381 /* GDBserver places breakpoint here. */
382 UNKNOWN_SIDE_EFFECTS();
385 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
386 flush_trace_buffer (void)
388 /* GDBserver places breakpoint here. */
389 UNKNOWN_SIDE_EFFECTS();
394 #ifndef IN_PROCESS_AGENT
396 tracepoint_handler (CORE_ADDR address)
398 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
403 /* Breakpoint at "stop_tracing" in the inferior lib. */
404 struct breakpoint *stop_tracing_bkpt;
405 static int stop_tracing_handler (CORE_ADDR);
407 /* Breakpoint at "flush_trace_buffer" in the inferior lib. */
408 struct breakpoint *flush_trace_buffer_bkpt;
409 static int flush_trace_buffer_handler (CORE_ADDR);
411 static void download_tracepoints (void);
412 static void download_trace_state_variables (void);
413 static void upload_fast_traceframes (void);
415 static int run_inferior_command (char *cmd);
418 read_inferior_integer (CORE_ADDR symaddr, int *val)
420 return read_inferior_memory (symaddr, (unsigned char *) val,
425 read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
427 return read_inferior_memory (symaddr, (unsigned char *) val,
432 read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
434 void *pval = (void *) (uintptr_t) val;
437 ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
438 *val = (uintptr_t) pval;
443 write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
445 void *pval = (void *) (uintptr_t) val;
446 return write_inferior_memory (symaddr,
447 (unsigned char *) &pval, sizeof (pval));
451 write_inferior_integer (CORE_ADDR symaddr, int val)
453 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
457 write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
459 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
464 /* Base action. Concrete actions inherit this. */
466 struct tracepoint_action
471 /* An 'M' (collect memory) action. */
472 struct collect_memory_action
474 struct tracepoint_action base;
481 /* An 'R' (collect registers) action. */
483 struct collect_registers_action
485 struct tracepoint_action base;
488 /* An 'X' (evaluate expression) action. */
490 struct eval_expr_action
492 struct tracepoint_action base;
494 struct agent_expr *expr;
497 /* An 'L' (collect static trace data) action. */
498 struct collect_static_trace_data_action
500 struct tracepoint_action base;
503 /* This structure describes a piece of the source-level definition of
504 the tracepoint. The contents are not interpreted by the target,
505 but preserved verbatim for uploading upon reconnection. */
509 /* The type of string, such as "cond" for a conditional. */
512 /* The source-level string itself. For the sake of target
513 debugging, we store it in plaintext, even though it is always
514 transmitted in hex. */
517 /* Link to the next one in the list. We link them in the order
518 received, in case some make up an ordered list of commands or
520 struct source_string *next;
525 /* Trap based tracepoint. */
528 /* A fast tracepoint implemented with a jump instead of a trap. */
531 /* A static tracepoint, implemented by a program call into a tracing
536 struct tracepoint_hit_ctx;
538 typedef enum eval_result_type (*condfn) (struct tracepoint_hit_ctx *,
541 /* The definition of a tracepoint. */
543 /* Tracepoints may have multiple locations, each at a different
544 address. This can occur with optimizations, template
545 instantiation, etc. Since the locations may be in different
546 scopes, the conditions and actions may be different for each
547 location. Our target version of tracepoints is more like GDB's
548 notion of "breakpoint locations", but we have almost nothing that
549 is not per-location, so we bother having two kinds of objects. The
550 key consequence is that numbers are not unique, and that it takes
551 both number and address to identify a tracepoint uniquely. */
555 /* The number of the tracepoint, as specified by GDB. Several
556 tracepoint objects here may share a number. */
559 /* Address at which the tracepoint is supposed to trigger. Several
560 tracepoints may share an address. */
563 /* Tracepoint type. */
564 enum tracepoint_type type;
566 /* True if the tracepoint is currently enabled. */
569 /* The number of single steps that will be performed after each
573 /* The number of times the tracepoint may be hit before it will
574 terminate the entire tracing run. */
577 /* Pointer to the agent expression that is the tracepoint's
578 conditional, or NULL if the tracepoint is unconditional. */
579 struct agent_expr *cond;
581 /* The list of actions to take when the tracepoint triggers. */
583 struct tracepoint_action **actions;
585 /* Count of the times we've hit this tracepoint during the run.
586 Note that while-stepping steps are not counted as "hits". */
589 /* Cached sum of the sizes of traceframes created by this point. */
590 long traceframe_usage;
592 CORE_ADDR compiled_cond;
594 /* Link to the next tracepoint in the list. */
595 struct tracepoint *next;
597 #ifndef IN_PROCESS_AGENT
598 /* The list of actions to take when the tracepoint triggers, in
599 string/packet form. */
602 /* The collection of strings that describe the tracepoint as it was
603 entered into GDB. These are not used by the target, but are
604 reported back to GDB upon reconnection. */
605 struct source_string *source_strings;
607 /* The number of bytes displaced by fast tracepoints. It may subsume
608 multiple instructions, for multi-byte fast tracepoints. This
609 field is only valid for fast tracepoints. */
612 /* Only for fast tracepoints. */
613 CORE_ADDR obj_addr_on_target;
615 /* Address range where the original instruction under a fast
616 tracepoint was relocated to. (_end is actually one byte past
618 CORE_ADDR adjusted_insn_addr;
619 CORE_ADDR adjusted_insn_addr_end;
621 /* The address range of the piece of the jump pad buffer that was
622 assigned to this fast tracepoint. (_end is actually one byte
625 CORE_ADDR jump_pad_end;
627 /* The address range of the piece of the trampoline buffer that was
628 assigned to this fast tracepoint. (_end is actually one byte
630 CORE_ADDR trampoline;
631 CORE_ADDR trampoline_end;
633 /* The list of actions to take while in a stepping loop. These
634 fields are only valid for patch-based tracepoints. */
635 int num_step_actions;
636 struct tracepoint_action **step_actions;
637 /* Same, but in string/packet form. */
638 char **step_actions_str;
640 /* Handle returned by the breakpoint or tracepoint module when we
641 inserted the trap or jump, or hooked into a static tracepoint.
642 NULL if we haven't inserted it yet. */
648 #ifndef IN_PROCESS_AGENT
650 /* Given `while-stepping', a thread may be collecting data for more
651 than one tracepoint simultaneously. On the other hand, the same
652 tracepoint with a while-stepping action may be hit by more than one
653 thread simultaneously (but not quite, each thread could be handling
654 a different step). Each thread holds a list of these objects,
655 representing the current step of each while-stepping action being
660 struct wstep_state *next;
662 /* The tracepoint number. */
664 /* The tracepoint's address. */
665 CORE_ADDR tp_address;
667 /* The number of the current step in this 'while-stepping'
674 /* The linked list of all tracepoints. Marked explicitly as used as
675 the in-process library doesn't use it for the fast tracepoints
677 IP_AGENT_EXPORT struct tracepoint *tracepoints ATTR_USED;
679 #ifndef IN_PROCESS_AGENT
681 /* Pointer to the last tracepoint in the list, new tracepoints are
682 linked in at the end. */
684 static struct tracepoint *last_tracepoint;
687 /* The first tracepoint to exceed its pass count. */
689 IP_AGENT_EXPORT struct tracepoint *stopping_tracepoint;
691 /* True if the trace buffer is full or otherwise no longer usable. */
693 IP_AGENT_EXPORT int trace_buffer_is_full;
695 static enum eval_result_type expr_eval_result = expr_eval_no_error;
697 #ifndef IN_PROCESS_AGENT
699 static const char *eval_result_names[] =
701 "terror:in the attic", /* this should never be reported */
702 "terror:empty expression",
703 "terror:empty stack",
704 "terror:stack overflow",
705 "terror:stack underflow",
706 "terror:unhandled opcode",
707 "terror:unrecognized opcode",
708 "terror:divide by zero"
713 /* The tracepoint in which the error occurred. */
715 static struct tracepoint *error_tracepoint;
717 struct trace_state_variable
719 /* This is the name of the variable as used in GDB. The target
720 doesn't use the name, but needs to have it for saving and
721 reconnection purposes. */
724 /* This number identifies the variable uniquely. Numbers may be
725 assigned either by the target (in the case of builtin variables),
726 or by GDB, and are presumed unique during the course of a trace
730 /* The variable's initial value, a 64-bit signed integer always. */
731 LONGEST initial_value;
733 /* The variable's value, a 64-bit signed integer always. */
736 /* Pointer to a getter function, used to supply computed values. */
737 LONGEST (*getter) (void);
739 /* Link to the next variable. */
740 struct trace_state_variable *next;
743 /* Linked list of all trace state variables. */
745 #ifdef IN_PROCESS_AGENT
746 struct trace_state_variable *alloced_trace_state_variables;
749 IP_AGENT_EXPORT struct trace_state_variable *trace_state_variables;
751 /* The results of tracing go into a fixed-size space known as the
752 "trace buffer". Because usage follows a limited number of
753 patterns, we manage it ourselves rather than with malloc. Basic
754 rules are that we create only one trace frame at a time, each is
755 variable in size, they are never moved once created, and we only
756 discard if we are doing a circular buffer, and then only the oldest
757 ones. Each trace frame includes its own size, so we don't need to
758 link them together, and the trace frame number is relative to the
759 first one, so we don't need to record numbers. A trace frame also
760 records the number of the tracepoint that created it. The data
761 itself is a series of blocks, each introduced by a single character
762 and with a defined format. Each type of block has enough
763 type/length info to allow scanners to jump quickly from one block
764 to the next without reading each byte in the block. */
766 /* Trace buffer management would be simple - advance a free pointer
767 from beginning to end, then stop - were it not for the circular
768 buffer option, which is a useful way to prevent a trace run from
769 stopping prematurely because the buffer filled up. In the circular
770 case, the location of the first trace frame (trace_buffer_start)
771 moves as old trace frames are discarded. Also, since we grow trace
772 frames incrementally as actions are performed, we wrap around to
773 the beginning of the trace buffer. This is per-block, so each
774 block within a trace frame remains contiguous. Things get messy
775 when the wrapped-around trace frame is the one being discarded; the
776 free space ends up in two parts at opposite ends of the buffer. */
779 # if defined(__GNUC__)
780 # define ATTR_PACKED __attribute__ ((packed))
782 # define ATTR_PACKED /* nothing */
786 /* The data collected at a tracepoint hit. This object should be as
787 small as possible, since there may be a great many of them. We do
788 not need to keep a frame number, because they are all sequential
789 and there are no deletions; so the Nth frame in the buffer is
790 always frame number N. */
794 /* Number of the tracepoint that collected this traceframe. A value
795 of 0 indicates the current end of the trace buffer. We make this
796 a 16-bit field because it's never going to happen that GDB's
797 numbering of tracepoints reaches 32,000. */
800 /* The size of the data in this trace frame. We limit this to 32
801 bits, even on a 64-bit target, because it's just implausible that
802 one is validly going to collect 4 gigabytes of data at a single
804 unsigned int data_size : 32;
806 /* The base of the trace data, which is contiguous from this point. */
807 unsigned char data[0];
811 /* The traceframe to be used as the source of data to send back to
812 GDB. A value of -1 means to get data from the live program. */
814 int current_traceframe = -1;
816 /* This flag is true if the trace buffer is circular, meaning that
817 when it fills, the oldest trace frames are discarded in order to
820 #ifndef IN_PROCESS_AGENT
821 static int circular_trace_buffer;
824 /* Pointer to the block of memory that traceframes all go into. */
826 static unsigned char *trace_buffer_lo;
828 /* Pointer to the end of the trace buffer, more precisely to the byte
829 after the end of the buffer. */
831 static unsigned char *trace_buffer_hi;
833 /* Control structure holding the read/write/etc. pointers into the
834 trace buffer. We need more than one of these to implement a
835 transaction-like mechanism to garantees that both GDBserver and the
836 in-process agent can try to change the trace buffer
839 struct trace_buffer_control
841 /* Pointer to the first trace frame in the buffer. In the
842 non-circular case, this is equal to trace_buffer_lo, otherwise it
843 moves around in the buffer. */
844 unsigned char *start;
846 /* Pointer to the free part of the trace buffer. Note that we clear
847 several bytes at and after this pointer, so that traceframe
848 scans/searches terminate properly. */
851 /* Pointer to the byte after the end of the free part. Note that
852 this may be smaller than trace_buffer_free in the circular case,
853 and means that the free part is in two pieces. Initially it is
854 equal to trace_buffer_hi, then is generally equivalent to
855 trace_buffer_start. */
856 unsigned char *end_free;
858 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
859 this is the point at which the trace data breaks, and resumes at
864 /* Same as above, to be used by GDBserver when updating the in-process
866 struct ipa_trace_buffer_control
875 /* We have possibly both GDBserver and an inferior thread accessing
876 the same IPA trace buffer memory. The IPA is the producer (tries
877 to put new frames in the buffer), while GDBserver occasionally
878 consumes them, that is, flushes the IPA's buffer into its own
879 buffer. Both sides need to update the trace buffer control
880 pointers (current head, tail, etc.). We can't use a global lock to
881 synchronize the accesses, as otherwise we could deadlock GDBserver
882 (if the thread holding the lock stops for a signal, say). So
883 instead of that, we use a transaction scheme where GDBserver writes
884 always prevail over the IPAs writes, and, we have the IPA detect
885 the commit failure/overwrite, and retry the whole attempt. This is
886 mainly implemented by having a global token object that represents
887 who wrote last to the buffer control structure. We need to freeze
888 any inferior writing to the buffer while GDBserver touches memory,
889 so that the inferior can correctly detect that GDBserver had been
890 there, otherwise, it could mistakingly think its commit was
891 successful; that's implemented by simply having GDBserver set a
892 breakpoint the inferior hits if it is the critical region.
894 There are three cycling trace buffer control structure copies
895 (buffer head, tail, etc.), with the token object including an index
896 indicating which is current live copy. The IPA tentatively builds
897 an updated copy in a non-current control structure, while GDBserver
898 always clobbers the current version directly. The IPA then tries
899 to atomically "commit" its version; if GDBserver clobbered the
900 structure meanwhile, that will fail, and the IPA restarts the
903 Listing the step in further detail, we have:
905 In-process agent (producer):
907 - passes by `about_to_request_buffer_space' breakpoint/lock
909 - reads current token, extracts current trace buffer control index,
910 and starts tentatively updating the rightmost one (0->1, 1->2,
911 2->0). Note that only one inferior thread is executing this code
912 at any given time, due to an outer lock in the jump pads.
914 - updates counters, and tries to commit the token.
916 - passes by second `about_to_request_buffer_space' breakpoint/lock,
917 leaving the sync region.
919 - checks if the update was effective.
921 - if trace buffer was found full, hits flush_trace_buffer
922 breakpoint, and restarts later afterwards.
924 GDBserver (consumer):
926 - sets `about_to_request_buffer_space' breakpoint/lock.
928 - updates the token unconditionally, using the current buffer
929 control index, since it knows that the IP agent always writes to
930 the rightmost, and due to the breakpoint, at most one IP thread
931 can try to update the trace buffer concurrently to GDBserver, so
932 there will be no danger of trace buffer control index wrap making
933 the IPA write to the same index as GDBserver.
935 - flushes the IP agent's trace buffer completely, and updates the
936 current trace buffer control structure. GDBserver *always* wins.
938 - removes the `about_to_request_buffer_space' breakpoint.
940 The token is stored in the `trace_buffer_ctrl_curr' variable.
941 Internally, it's bits are defined as:
943 |-------------+-----+-------------+--------+-------------+--------------|
944 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
945 |-------------+-----+-------------+--------+-------------+--------------|
946 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
947 |-------------+-----+-------------+--------+-------------+--------------|
949 GSB - GDBserver Stamp Bit
950 PC - Previous Counter
952 TBCI - Trace Buffer Control Index
955 An IPA update of `trace_buffer_ctrl_curr' does:
957 - read CC from the current token, save as PC.
959 - atomically tries to write PC+1,CC
961 A GDBserver update of `trace_buffer_ctrl_curr' does:
963 - reads PC and CC from the current token.
968 /* These are the bits of `trace_buffer_ctrl_curr' that are reserved
969 for the counters described below. The cleared bits are used to
970 hold the index of the items of the `trace_buffer_ctrl' array that
972 #define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
974 /* `trace_buffer_ctrl_curr' contains two counters. The `previous'
975 counter, and the `current' counter. */
977 #define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
978 #define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
980 /* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
981 always stamps this bit as set. */
982 #define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
984 #ifdef IN_PROCESS_AGENT
985 IP_AGENT_EXPORT struct trace_buffer_control trace_buffer_ctrl[3];
986 IP_AGENT_EXPORT unsigned int trace_buffer_ctrl_curr;
988 # define TRACE_BUFFER_CTRL_CURR \
989 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
993 /* The GDBserver side agent only needs one instance of this object, as
994 it doesn't need to sync with itself. Define it as array anyway so
995 that the rest of the code base doesn't need to care for the
997 struct trace_buffer_control trace_buffer_ctrl[1];
998 # define TRACE_BUFFER_CTRL_CURR 0
1001 /* These are convenience macros used to access the current trace
1002 buffer control in effect. */
1003 #define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1004 #define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1005 #define trace_buffer_end_free \
1006 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1007 #define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
1010 /* Macro that returns a pointer to the first traceframe in the buffer. */
1012 #define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1014 /* Macro that returns a pointer to the next traceframe in the buffer.
1015 If the computed location is beyond the wraparound point, subtract
1016 the offset of the wraparound. */
1018 #define NEXT_TRACEFRAME_1(TF) \
1019 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1021 #define NEXT_TRACEFRAME(TF) \
1022 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1023 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1024 ? (trace_buffer_wrap - trace_buffer_lo) \
1027 /* The difference between these counters represents the total number
1028 of complete traceframes present in the trace buffer. The IP agent
1029 writes to the write count, GDBserver writes to read count. */
1031 IP_AGENT_EXPORT unsigned int traceframe_write_count;
1032 IP_AGENT_EXPORT unsigned int traceframe_read_count;
1034 /* Convenience macro. */
1036 #define traceframe_count \
1037 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1039 /* The count of all traceframes created in the current run, including
1040 ones that were discarded to make room. */
1042 IP_AGENT_EXPORT int traceframes_created;
1044 #ifndef IN_PROCESS_AGENT
1046 /* Read-only regions are address ranges whose contents don't change,
1047 and so can be read from target memory even while looking at a trace
1048 frame. Without these, disassembly for instance will likely fail,
1049 because the program code is not usually collected into a trace
1050 frame. This data structure does not need to be very complicated or
1051 particularly efficient, it's only going to be used occasionally,
1052 and only by some commands. */
1054 struct readonly_region
1056 /* The bounds of the region. */
1057 CORE_ADDR start, end;
1059 /* Link to the next one. */
1060 struct readonly_region *next;
1063 /* Linked list of readonly regions. This list stays in effect from
1064 one tstart to the next. */
1066 static struct readonly_region *readonly_regions;
1070 /* The global that controls tracing overall. */
1072 IP_AGENT_EXPORT int tracing;
1074 #ifndef IN_PROCESS_AGENT
1076 /* Controls whether tracing should continue after GDB disconnects. */
1078 int disconnected_tracing;
1080 /* The reason for the last tracing run to have stopped. We initialize
1081 to a distinct string so that GDB can distinguish between "stopped
1082 after running" and "stopped because never run" cases. */
1084 static const char *tracing_stop_reason = "tnotrun";
1086 static int tracing_stop_tpnum;
1088 /* 64-bit timestamps for the trace run's start and finish, expressed
1089 in microseconds from the Unix epoch. */
1091 LONGEST tracing_start_time;
1092 LONGEST tracing_stop_time;
1094 /* The (optional) user-supplied name of the user that started the run.
1095 This is an arbitrary string, and may be NULL. */
1097 char *tracing_user_name;
1099 /* Optional user-supplied text describing the run. This is
1100 an arbitrary string, and may be NULL. */
1102 char *tracing_notes;
1104 /* Optional user-supplied text explaining a tstop command. This is an
1105 arbitrary string, and may be NULL. */
1107 char *tracing_stop_note;
1111 /* Functions local to this file. */
1113 /* Base "class" for tracepoint type specific data to be passed down to
1114 collect_data_at_tracepoint. */
1115 struct tracepoint_hit_ctx
1117 enum tracepoint_type type;
1120 #ifdef IN_PROCESS_AGENT
1122 /* Fast/jump tracepoint specific data to be passed down to
1123 collect_data_at_tracepoint. */
1124 struct fast_tracepoint_ctx
1126 struct tracepoint_hit_ctx base;
1128 struct regcache regcache;
1129 int regcache_initted;
1130 unsigned char *regspace;
1132 unsigned char *regs;
1133 struct tracepoint *tpoint;
1136 /* Static tracepoint specific data to be passed down to
1137 collect_data_at_tracepoint. */
1138 struct static_tracepoint_ctx
1140 struct tracepoint_hit_ctx base;
1142 /* The regcache corresponding to the registers state at the time of
1143 the tracepoint hit. Initialized lazily, from REGS. */
1144 struct regcache regcache;
1145 int regcache_initted;
1147 /* The buffer space REGCACHE above uses. We use a separate buffer
1148 instead of letting the regcache malloc for both signal safety and
1149 performance reasons; this is allocated on the stack instead. */
1150 unsigned char *regspace;
1152 /* The register buffer as passed on by lttng/ust. */
1153 struct registers *regs;
1155 /* The "printf" formatter and the args the user passed to the marker
1156 call. We use this to be able to collect "static trace data"
1161 /* The GDB tracepoint matching the probed marker that was "hit". */
1162 struct tracepoint *tpoint;
1167 /* Static tracepoint specific data to be passed down to
1168 collect_data_at_tracepoint. */
1169 struct trap_tracepoint_ctx
1171 struct tracepoint_hit_ctx base;
1173 struct regcache *regcache;
1178 static enum eval_result_type
1179 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx *ctx,
1180 struct traceframe *tframe,
1181 struct agent_expr *aexpr,
1184 #ifndef IN_PROCESS_AGENT
1185 static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1186 static int traceframe_read_tsv (int num, LONGEST *val);
1189 static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1190 struct tracepoint *tpoint);
1192 #ifndef IN_PROCESS_AGENT
1193 static void clear_readonly_regions (void);
1194 static void clear_installed_tracepoints (void);
1197 static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1199 struct tracepoint *tpoint);
1200 #ifndef IN_PROCESS_AGENT
1201 static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1203 struct tracepoint *tpoint, int current_step);
1204 static void compile_tracepoint_condition (struct tracepoint *tpoint,
1205 CORE_ADDR *jump_entry);
1207 static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1209 struct tracepoint *tpoint,
1210 struct traceframe *tframe,
1211 struct tracepoint_action *taction);
1213 #ifndef IN_PROCESS_AGENT
1214 static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1216 static void install_tracepoint (struct tracepoint *, char *own_buf);
1217 static void download_tracepoint (struct tracepoint *);
1218 static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
1221 static LONGEST get_timestamp (void);
1223 #if defined(__GNUC__)
1224 # define memory_barrier() asm volatile ("" : : : "memory")
1226 # define memory_barrier() do {} while (0)
1229 /* We only build the IPA if this builtin is supported, and there are
1230 no uses of this in GDBserver itself, so we're safe in defining this
1232 #define cmpxchg(mem, oldval, newval) \
1233 __sync_val_compare_and_swap (mem, oldval, newval)
1235 /* Record that an error occurred during expression evaluation. */
1238 record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1239 enum eval_result_type rtype)
1241 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1242 tpoint->number, paddress (tpoint->address), which, rtype);
1244 #ifdef IN_PROCESS_AGENT
1245 /* Only record the first error we get. */
1246 if (cmpxchg (&expr_eval_result,
1248 rtype) != expr_eval_no_error)
1251 if (expr_eval_result != expr_eval_no_error)
1255 error_tracepoint = tpoint;
1258 /* Trace buffer management. */
1261 clear_trace_buffer (void)
1263 trace_buffer_start = trace_buffer_lo;
1264 trace_buffer_free = trace_buffer_lo;
1265 trace_buffer_end_free = trace_buffer_hi;
1266 trace_buffer_wrap = trace_buffer_hi;
1267 /* A traceframe with zeroed fields marks the end of trace data. */
1268 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1269 ((struct traceframe *) trace_buffer_free)->data_size = 0;
1270 traceframe_read_count = traceframe_write_count = 0;
1271 traceframes_created = 0;
1274 #ifndef IN_PROCESS_AGENT
1277 clear_inferior_trace_buffer (void)
1279 CORE_ADDR ipa_trace_buffer_lo;
1280 CORE_ADDR ipa_trace_buffer_hi;
1281 struct traceframe ipa_traceframe = { 0 };
1282 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1284 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1285 &ipa_trace_buffer_lo);
1286 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1287 &ipa_trace_buffer_hi);
1289 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1290 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1291 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1292 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1294 /* A traceframe with zeroed fields marks the end of trace data. */
1295 write_inferior_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1296 (unsigned char *) &ipa_trace_buffer_ctrl,
1297 sizeof (ipa_trace_buffer_ctrl));
1299 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1301 /* A traceframe with zeroed fields marks the end of trace data. */
1302 write_inferior_memory (ipa_trace_buffer_lo,
1303 (unsigned char *) &ipa_traceframe,
1304 sizeof (ipa_traceframe));
1306 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1307 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1308 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1314 init_trace_buffer (unsigned char *buf, int bufsize)
1316 trace_buffer_lo = buf;
1317 trace_buffer_hi = trace_buffer_lo + bufsize;
1319 clear_trace_buffer ();
1322 #ifdef IN_PROCESS_AGENT
1324 IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
1325 about_to_request_buffer_space (void)
1327 /* GDBserver places breakpoint here while it goes about to flush
1328 data at random times. */
1329 UNKNOWN_SIDE_EFFECTS();
1334 /* Carve out a piece of the trace buffer, returning NULL in case of
1338 trace_buffer_alloc (size_t amt)
1340 unsigned char *rslt;
1341 struct trace_buffer_control *tbctrl;
1343 #ifdef IN_PROCESS_AGENT
1344 unsigned int prev, prev_filtered;
1345 unsigned int commit_count;
1346 unsigned int commit;
1347 unsigned int readout;
1349 struct traceframe *oldest;
1350 unsigned char *new_start;
1353 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1354 (long) amt, (long) sizeof (struct traceframe));
1356 /* Account for the EOB marker. */
1357 amt += sizeof (struct traceframe);
1359 #ifdef IN_PROCESS_AGENT
1363 /* Read the current token and extract the index to try to write to,
1364 storing it in CURR. */
1365 prev = trace_buffer_ctrl_curr;
1366 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1367 curr = prev_filtered + 1;
1371 about_to_request_buffer_space ();
1373 /* Start out with a copy of the current state. GDBserver may be
1374 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1375 be able to commit anyway if that happens. */
1376 trace_buffer_ctrl[curr]
1377 = trace_buffer_ctrl[prev_filtered];
1378 trace_debug ("trying curr=%u", curr);
1380 /* The GDBserver's agent doesn't need all that syncing, and always
1381 updates TCB 0 (there's only one, mind you). */
1384 tbctrl = &trace_buffer_ctrl[curr];
1386 /* Offsets are easier to grok for debugging than raw addresses,
1387 especially for the small trace buffer sizes that are useful for
1389 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1391 (int) (tbctrl->start - trace_buffer_lo),
1392 (int) (tbctrl->free - trace_buffer_lo),
1393 (int) (tbctrl->end_free - trace_buffer_lo),
1394 (int) (tbctrl->wrap - trace_buffer_lo),
1395 (int) (trace_buffer_hi - trace_buffer_lo));
1397 /* The algorithm here is to keep trying to get a contiguous block of
1398 the requested size, possibly discarding older traceframes to free
1399 up space. Since free space might come in one or two pieces,
1400 depending on whether discarded traceframes wrapped around at the
1401 high end of the buffer, we test both pieces after each
1405 /* First, if we have two free parts, try the upper one first. */
1406 if (tbctrl->end_free < tbctrl->free)
1408 if (tbctrl->free + amt <= trace_buffer_hi)
1409 /* We have enough in the upper part. */
1413 /* Our high part of free space wasn't enough. Give up
1414 on it for now, set wraparound. We will recover the
1415 space later, if/when the wrapped-around traceframe is
1417 trace_debug ("Upper part too small, setting wraparound");
1418 tbctrl->wrap = tbctrl->free;
1419 tbctrl->free = trace_buffer_lo;
1423 /* The normal case. */
1424 if (tbctrl->free + amt <= tbctrl->end_free)
1427 #ifdef IN_PROCESS_AGENT
1428 /* The IP Agent's buffer is always circular. It isn't used
1429 currently, but `circular_trace_buffer' could represent
1430 GDBserver's mode. If we didn't find space, ask GDBserver to
1433 flush_trace_buffer ();
1437 trace_debug ("gdbserver flushed buffer, retrying");
1441 /* GDBserver cancelled the tracing. Bail out as well. */
1444 /* If we're here, then neither part is big enough, and
1445 non-circular trace buffers are now full. */
1446 if (!circular_trace_buffer)
1448 trace_debug ("Not enough space in the trace buffer");
1452 trace_debug ("Need more space in the trace buffer");
1454 /* If we have a circular buffer, we can try discarding the
1455 oldest traceframe and see if that helps. */
1456 oldest = FIRST_TRACEFRAME ();
1457 if (oldest->tpnum == 0)
1459 /* Not good; we have no traceframes to free. Perhaps we're
1460 asking for a block that is larger than the buffer? In
1461 any case, give up. */
1462 trace_debug ("No traceframes to discard");
1466 /* We don't run this code in the in-process agent currently.
1467 E.g., we could leave the in-process agent in autonomous
1468 circular mode if we only have fast tracepoints. If we do
1469 that, then this bit becomes racy with GDBserver, which also
1470 writes to this counter. */
1471 --traceframe_write_count;
1473 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1474 /* If we freed the traceframe that wrapped around, go back
1475 to the non-wrap case. */
1476 if (new_start < tbctrl->start)
1478 trace_debug ("Discarding past the wraparound");
1479 tbctrl->wrap = trace_buffer_hi;
1481 tbctrl->start = new_start;
1482 tbctrl->end_free = tbctrl->start;
1484 trace_debug ("Discarded a traceframe\n"
1485 "Trace buffer [%d], start=%d free=%d "
1486 "endfree=%d wrap=%d hi=%d",
1488 (int) (tbctrl->start - trace_buffer_lo),
1489 (int) (tbctrl->free - trace_buffer_lo),
1490 (int) (tbctrl->end_free - trace_buffer_lo),
1491 (int) (tbctrl->wrap - trace_buffer_lo),
1492 (int) (trace_buffer_hi - trace_buffer_lo));
1494 /* Now go back around the loop. The discard might have resulted
1495 in either one or two pieces of free space, so we want to try
1496 both before freeing any more traceframes. */
1500 /* If we get here, we know we can provide the asked-for space. */
1502 rslt = tbctrl->free;
1504 /* Adjust the request back down, now that we know we have space for
1505 the marker, but don't commit to AMT yet, we may still need to
1506 restart the operation if GDBserver touches the trace buffer
1507 (obviously only important in the in-process agent's version). */
1508 tbctrl->free += (amt - sizeof (struct traceframe));
1510 /* Or not. If GDBserver changed the trace buffer behind our back,
1511 we get to restart a new allocation attempt. */
1513 #ifdef IN_PROCESS_AGENT
1514 /* Build the tentative token. */
1515 commit_count = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) + 0x100)
1516 & GDBSERVER_FLUSH_COUNT_MASK_CURR);
1517 commit = (((prev & GDBSERVER_FLUSH_COUNT_MASK_CURR) << 12)
1521 /* Try to commit it. */
1522 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1523 if (readout != prev)
1525 trace_debug ("GDBserver has touched the trace buffer, restarting."
1526 " (prev=%08x, commit=%08x, readout=%08x)",
1527 prev, commit, readout);
1531 /* Hold your horses here. Even if that change was committed,
1532 GDBserver could come in, and clobber it. We need to hold to be
1533 able to tell if GDBserver clobbers before or after we committed
1534 the change. Whenever GDBserver goes about touching the IPA
1535 buffer, it sets a breakpoint in this routine, so we have a sync
1537 about_to_request_buffer_space ();
1539 /* Check if the change has been effective, even if GDBserver stopped
1540 us at the breakpoint. */
1543 unsigned int refetch;
1547 refetch = trace_buffer_ctrl_curr;
1549 if (refetch == commit
1550 || ((refetch & GDBSERVER_FLUSH_COUNT_MASK_PREV) >> 12) == commit_count)
1553 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1554 "readout=%08x, refetch=%08x)",
1555 prev, commit, readout, refetch);
1559 trace_debug ("GDBserver has touched the trace buffer, not effective."
1560 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1561 prev, commit, readout, refetch);
1567 /* We have a new piece of the trace buffer. Hurray! */
1569 /* Add an EOB marker just past this allocation. */
1570 ((struct traceframe *) tbctrl->free)->tpnum = 0;
1571 ((struct traceframe *) tbctrl->free)->data_size = 0;
1573 /* Adjust the request back down, now that we know we have space for
1575 amt -= sizeof (struct traceframe);
1579 trace_debug ("Allocated %d bytes", (int) amt);
1580 trace_debug ("Trace buffer [%d] start=%d free=%d "
1581 "endfree=%d wrap=%d hi=%d",
1583 (int) (tbctrl->start - trace_buffer_lo),
1584 (int) (tbctrl->free - trace_buffer_lo),
1585 (int) (tbctrl->end_free - trace_buffer_lo),
1586 (int) (tbctrl->wrap - trace_buffer_lo),
1587 (int) (trace_buffer_hi - trace_buffer_lo));
1593 #ifndef IN_PROCESS_AGENT
1595 /* Return the total free space. This is not necessarily the largest
1596 block we can allocate, because of the two-part case. */
1601 if (trace_buffer_free <= trace_buffer_end_free)
1602 return trace_buffer_end_free - trace_buffer_free;
1604 return ((trace_buffer_end_free - trace_buffer_lo)
1605 + (trace_buffer_hi - trace_buffer_free));
1608 /* An 'S' in continuation packets indicates remainder are for
1611 static int seen_step_action_flag;
1613 /* Create a tracepoint (location) with given number and address. Add this
1614 new tracepoint to list and sort this list. */
1616 static struct tracepoint *
1617 add_tracepoint (int num, CORE_ADDR addr)
1619 struct tracepoint *tpoint, **tp_next;
1621 tpoint = xmalloc (sizeof (struct tracepoint));
1622 tpoint->number = num;
1623 tpoint->address = addr;
1624 tpoint->numactions = 0;
1625 tpoint->actions = NULL;
1626 tpoint->actions_str = NULL;
1627 tpoint->cond = NULL;
1628 tpoint->num_step_actions = 0;
1629 tpoint->step_actions = NULL;
1630 tpoint->step_actions_str = NULL;
1631 /* Start all off as regular (slow) tracepoints. */
1632 tpoint->type = trap_tracepoint;
1633 tpoint->orig_size = -1;
1634 tpoint->source_strings = NULL;
1635 tpoint->compiled_cond = 0;
1636 tpoint->handle = NULL;
1637 tpoint->next = NULL;
1639 /* Find a place to insert this tracepoint into list in order to keep
1640 the tracepoint list still in the ascending order. There may be
1641 multiple tracepoints at the same address as TPOINT's, and this
1642 guarantees TPOINT is inserted after all the tracepoints which are
1643 set at the same address. For example, fast tracepoints A, B, C are
1644 set at the same address, and D is to be insert at the same place as
1647 -->| A |--> | B |-->| C |->...
1649 One jump pad was created for tracepoint A, B, and C, and the target
1650 address of A is referenced/used in jump pad. So jump pad will let
1651 inferior jump to A. If D is inserted in front of A, like this,
1653 -->| D |-->| A |--> | B |-->| C |->...
1655 without updating jump pad, D is not reachable during collect, which
1656 is wrong. As we can see, the order of B, C and D doesn't matter, but
1657 A should always be the `first' one. */
1658 for (tp_next = &tracepoints;
1659 (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
1660 tp_next = &(*tp_next)->next)
1662 tpoint->next = *tp_next;
1664 last_tracepoint = tpoint;
1666 seen_step_action_flag = 0;
1671 #ifndef IN_PROCESS_AGENT
1673 /* Return the tracepoint with the given number and address, or NULL. */
1675 static struct tracepoint *
1676 find_tracepoint (int id, CORE_ADDR addr)
1678 struct tracepoint *tpoint;
1680 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1681 if (tpoint->number == id && tpoint->address == addr)
1687 /* There may be several tracepoints with the same number (because they
1688 are "locations", in GDB parlance); return the next one after the
1689 given tracepoint, or search from the beginning of the list if the
1690 first argument is NULL. */
1692 static struct tracepoint *
1693 find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1695 struct tracepoint *tpoint;
1698 tpoint = prev_tp->next;
1700 tpoint = tracepoints;
1701 for (; tpoint; tpoint = tpoint->next)
1702 if (tpoint->number == num)
1711 save_string (const char *str, size_t len)
1715 s = xmalloc (len + 1);
1716 memcpy (s, str, len);
1722 /* Append another action to perform when the tracepoint triggers. */
1725 add_tracepoint_action (struct tracepoint *tpoint, char *packet)
1731 seen_step_action_flag = 1;
1739 char *act_start = act;
1740 struct tracepoint_action *action = NULL;
1746 struct collect_memory_action *maction;
1750 maction = xmalloc (sizeof *maction);
1751 maction->base.type = *act;
1752 action = &maction->base;
1755 is_neg = (*act == '-');
1758 act = unpack_varlen_hex (act, &basereg);
1760 act = unpack_varlen_hex (act, &maction->addr);
1762 act = unpack_varlen_hex (act, &maction->len);
1763 maction->basereg = (is_neg
1766 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1767 pulongest (maction->len),
1768 paddress (maction->addr), maction->basereg);
1773 struct collect_registers_action *raction;
1775 raction = xmalloc (sizeof *raction);
1776 raction->base.type = *act;
1777 action = &raction->base;
1779 trace_debug ("Want to collect registers");
1781 /* skip past hex digits of mask for now */
1782 while (isxdigit(*act))
1788 struct collect_static_trace_data_action *raction;
1790 raction = xmalloc (sizeof *raction);
1791 raction->base.type = *act;
1792 action = &raction->base;
1794 trace_debug ("Want to collect static trace data");
1799 trace_debug ("Unexpected step action, ignoring");
1804 struct eval_expr_action *xaction;
1806 xaction = xmalloc (sizeof (*xaction));
1807 xaction->base.type = *act;
1808 action = &xaction->base;
1810 trace_debug ("Want to evaluate expression");
1811 xaction->expr = gdb_parse_agent_expr (&act);
1815 trace_debug ("unknown trace action '%c', ignoring...", *act);
1824 if (seen_step_action_flag)
1826 tpoint->num_step_actions++;
1828 tpoint->step_actions
1829 = xrealloc (tpoint->step_actions,
1830 (sizeof (*tpoint->step_actions)
1831 * tpoint->num_step_actions));
1832 tpoint->step_actions_str
1833 = xrealloc (tpoint->step_actions_str,
1834 (sizeof (*tpoint->step_actions_str)
1835 * tpoint->num_step_actions));
1836 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
1837 tpoint->step_actions_str[tpoint->num_step_actions - 1]
1838 = save_string (act_start, act - act_start);
1842 tpoint->numactions++;
1844 = xrealloc (tpoint->actions,
1845 sizeof (*tpoint->actions) * tpoint->numactions);
1847 = xrealloc (tpoint->actions_str,
1848 sizeof (*tpoint->actions_str) * tpoint->numactions);
1849 tpoint->actions[tpoint->numactions - 1] = action;
1850 tpoint->actions_str[tpoint->numactions - 1]
1851 = save_string (act_start, act - act_start);
1858 /* Find or create a trace state variable with the given number. */
1860 static struct trace_state_variable *
1861 get_trace_state_variable (int num)
1863 struct trace_state_variable *tsv;
1865 #ifdef IN_PROCESS_AGENT
1866 /* Search for an existing variable. */
1867 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
1868 if (tsv->number == num)
1872 /* Search for an existing variable. */
1873 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1874 if (tsv->number == num)
1880 /* Find or create a trace state variable with the given number. */
1882 static struct trace_state_variable *
1883 create_trace_state_variable (int num, int gdb)
1885 struct trace_state_variable *tsv;
1887 tsv = get_trace_state_variable (num);
1891 /* Create a new variable. */
1892 tsv = xmalloc (sizeof (struct trace_state_variable));
1894 tsv->initial_value = 0;
1898 #ifdef IN_PROCESS_AGENT
1901 tsv->next = alloced_trace_state_variables;
1902 alloced_trace_state_variables = tsv;
1907 tsv->next = trace_state_variables;
1908 trace_state_variables = tsv;
1913 IP_AGENT_EXPORT LONGEST
1914 get_trace_state_variable_value (int num)
1916 struct trace_state_variable *tsv;
1918 tsv = get_trace_state_variable (num);
1922 trace_debug ("No trace state variable %d, skipping value get", num);
1926 /* Call a getter function if we have one. While it's tempting to
1927 set up something to only call the getter once per tracepoint hit,
1928 it could run afoul of thread races. Better to let the getter
1929 handle it directly, if necessary to worry about it. */
1931 tsv->value = (tsv->getter) ();
1933 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1934 num, plongest (tsv->value));
1939 IP_AGENT_EXPORT void
1940 set_trace_state_variable_value (int num, LONGEST val)
1942 struct trace_state_variable *tsv;
1944 tsv = get_trace_state_variable (num);
1948 trace_debug ("No trace state variable %d, skipping value set", num);
1956 agent_get_trace_state_variable_value (int num)
1958 return get_trace_state_variable_value (num);
1962 agent_set_trace_state_variable_value (int num, LONGEST val)
1964 set_trace_state_variable_value (num, val);
1968 set_trace_state_variable_name (int num, const char *name)
1970 struct trace_state_variable *tsv;
1972 tsv = get_trace_state_variable (num);
1976 trace_debug ("No trace state variable %d, skipping name set", num);
1980 tsv->name = (char *) name;
1984 set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
1986 struct trace_state_variable *tsv;
1988 tsv = get_trace_state_variable (num);
1992 trace_debug ("No trace state variable %d, skipping getter set", num);
1996 tsv->getter = getter;
1999 /* Add a raw traceframe for the given tracepoint. */
2001 static struct traceframe *
2002 add_traceframe (struct tracepoint *tpoint)
2004 struct traceframe *tframe;
2006 tframe = trace_buffer_alloc (sizeof (struct traceframe));
2011 tframe->tpnum = tpoint->number;
2012 tframe->data_size = 0;
2017 /* Add a block to the traceframe currently being worked on. */
2019 static unsigned char *
2020 add_traceframe_block (struct traceframe *tframe, int amt)
2022 unsigned char *block;
2027 block = trace_buffer_alloc (amt);
2032 tframe->data_size += amt;
2037 /* Flag that the current traceframe is finished. */
2040 finish_traceframe (struct traceframe *tframe)
2042 ++traceframe_write_count;
2043 ++traceframes_created;
2046 #ifndef IN_PROCESS_AGENT
2048 /* Given a traceframe number NUM, find the NUMth traceframe in the
2051 static struct traceframe *
2052 find_traceframe (int num)
2054 struct traceframe *tframe;
2057 for (tframe = FIRST_TRACEFRAME ();
2059 tframe = NEXT_TRACEFRAME (tframe))
2070 get_traceframe_address (struct traceframe *tframe)
2073 struct tracepoint *tpoint;
2075 addr = traceframe_get_pc (tframe);
2080 /* Fallback strategy, will be incorrect for while-stepping frames
2081 and multi-location tracepoints. */
2082 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2083 return tpoint->address;
2086 /* Search for the next traceframe whose address is inside or outside
2089 static struct traceframe *
2090 find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2093 struct traceframe *tframe;
2096 *tfnump = current_traceframe + 1;
2097 tframe = find_traceframe (*tfnump);
2098 /* The search is not supposed to wrap around. */
2105 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2107 tfaddr = get_traceframe_address (tframe);
2109 ? (lo <= tfaddr && tfaddr <= hi)
2110 : (lo > tfaddr || tfaddr > hi))
2119 /* Search for the next traceframe recorded by the given tracepoint.
2120 Note that for multi-location tracepoints, this will find whatever
2121 location appears first. */
2123 static struct traceframe *
2124 find_next_traceframe_by_tracepoint (int num, int *tfnump)
2126 struct traceframe *tframe;
2128 *tfnump = current_traceframe + 1;
2129 tframe = find_traceframe (*tfnump);
2130 /* The search is not supposed to wrap around. */
2137 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2139 if (tframe->tpnum == num)
2150 #ifndef IN_PROCESS_AGENT
2152 /* Clear all past trace state. */
2155 cmd_qtinit (char *packet)
2157 struct trace_state_variable *tsv, *prev, *next;
2159 /* Make sure we don't try to read from a trace frame. */
2160 current_traceframe = -1;
2162 trace_debug ("Initializing the trace");
2164 clear_installed_tracepoints ();
2165 clear_readonly_regions ();
2168 last_tracepoint = NULL;
2170 /* Clear out any leftover trace state variables. Ones with target
2171 defined getters should be kept however. */
2173 tsv = trace_state_variables;
2176 trace_debug ("Looking at var %d", tsv->number);
2177 if (tsv->getter == NULL)
2183 trace_state_variables = next;
2184 trace_debug ("Deleting var %d", tsv->number);
2195 clear_trace_buffer ();
2196 clear_inferior_trace_buffer ();
2201 /* Unprobe the UST marker at ADDRESS. */
2204 unprobe_marker_at (CORE_ADDR address)
2206 char cmd[IPA_CMD_BUF_SIZE];
2208 sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
2209 run_inferior_command (cmd);
2212 /* Restore the program to its pre-tracing state. This routine may be called
2213 in error situations, so it needs to be careful about only restoring
2214 from known-valid bits. */
2217 clear_installed_tracepoints (void)
2219 struct tracepoint *tpoint;
2220 struct tracepoint *prev_stpoint;
2223 cancel_breakpoints ();
2225 prev_stpoint = NULL;
2227 /* Restore any bytes overwritten by tracepoints. */
2228 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2230 /* Catch the case where we might try to remove a tracepoint that
2231 was never actually installed. */
2232 if (tpoint->handle == NULL)
2234 trace_debug ("Tracepoint %d at 0x%s was "
2235 "never installed, nothing to clear",
2236 tpoint->number, paddress (tpoint->address));
2240 switch (tpoint->type)
2242 case trap_tracepoint:
2243 delete_breakpoint (tpoint->handle);
2245 case fast_tracepoint:
2246 delete_fast_tracepoint_jump (tpoint->handle);
2248 case static_tracepoint:
2249 if (prev_stpoint != NULL
2250 && prev_stpoint->address == tpoint->address)
2251 /* Nothing to do. We already unprobed a tracepoint set at
2252 this marker address (and there can only be one probe
2257 unprobe_marker_at (tpoint->address);
2258 prev_stpoint = tpoint;
2263 tpoint->handle = NULL;
2269 /* Parse a packet that defines a tracepoint. */
2272 cmd_qtdp (char *own_buf)
2275 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2276 int trail_hyphen = 0;
2280 struct tracepoint *tpoint;
2282 char *packet = own_buf;
2284 packet += strlen ("QTDP:");
2286 /* A hyphen at the beginning marks a packet specifying actions for a
2287 tracepoint already supplied. */
2294 packet = unpack_varlen_hex (packet, &num);
2295 ++packet; /* skip a colon */
2296 packet = unpack_varlen_hex (packet, &addr);
2297 ++packet; /* skip a colon */
2299 /* See if we already have this tracepoint. */
2300 tpoint = find_tracepoint (num, addr);
2304 /* Duplicate tracepoints are never allowed. */
2307 trace_debug ("Tracepoint error: tracepoint %d"
2308 " at 0x%s already exists",
2309 (int) num, paddress (addr));
2310 write_enn (own_buf);
2314 tpoint = add_tracepoint (num, addr);
2316 tpoint->enabled = (*packet == 'E');
2317 ++packet; /* skip 'E' */
2318 ++packet; /* skip a colon */
2319 packet = unpack_varlen_hex (packet, &count);
2320 tpoint->step_count = count;
2321 ++packet; /* skip a colon */
2322 packet = unpack_varlen_hex (packet, &count);
2323 tpoint->pass_count = count;
2324 /* See if we have any of the additional optional fields. */
2325 while (*packet == ':')
2330 tpoint->type = fast_tracepoint;
2332 packet = unpack_varlen_hex (packet, &count);
2333 tpoint->orig_size = count;
2335 else if (*packet == 'S')
2337 tpoint->type = static_tracepoint;
2340 else if (*packet == 'X')
2342 actparm = (char *) packet;
2343 tpoint->cond = gdb_parse_agent_expr (&actparm);
2346 else if (*packet == '-')
2348 else if (*packet == '\0')
2351 trace_debug ("Unknown optional tracepoint field");
2356 trace_debug ("Also has actions\n");
2359 trace_debug ("Defined %stracepoint %d at 0x%s, "
2360 "enabled %d step %ld pass %ld",
2361 tpoint->type == fast_tracepoint ? "fast "
2362 : tpoint->type == static_tracepoint ? "static " : "",
2363 tpoint->number, paddress (tpoint->address), tpoint->enabled,
2364 tpoint->step_count, tpoint->pass_count);
2367 add_tracepoint_action (tpoint, packet);
2370 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2371 (int) num, paddress (addr));
2372 write_enn (own_buf);
2376 /* Install tracepoint during tracing only once for each tracepoint location.
2377 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2378 determine the last QTDP packet for one tracepoint location by checking
2379 trailing hyphen in QTDP packet. */
2380 if (tracing && !trail_hyphen)
2382 /* Pause all threads temporarily while we patch tracepoints. */
2385 /* download_tracepoint will update global `tracepoints'
2386 list, so it is unsafe to leave threads in jump pad. */
2387 stabilize_threads ();
2389 /* Freeze threads. */
2392 download_tracepoint (tpoint);
2393 install_tracepoint (tpoint, own_buf);
2403 cmd_qtdpsrc (char *own_buf)
2405 ULONGEST num, addr, start, slen;
2406 struct tracepoint *tpoint;
2407 char *packet = own_buf;
2408 char *saved, *srctype, *src;
2410 struct source_string *last, *newlast;
2412 packet += strlen ("QTDPsrc:");
2414 packet = unpack_varlen_hex (packet, &num);
2415 ++packet; /* skip a colon */
2416 packet = unpack_varlen_hex (packet, &addr);
2417 ++packet; /* skip a colon */
2419 /* See if we already have this tracepoint. */
2420 tpoint = find_tracepoint (num, addr);
2424 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2425 (int) num, paddress (addr));
2426 write_enn (own_buf);
2431 packet = strchr (packet, ':');
2432 srctype = xmalloc (packet - saved + 1);
2433 memcpy (srctype, saved, packet - saved);
2434 srctype[packet - saved] = '\0';
2436 packet = unpack_varlen_hex (packet, &start);
2437 ++packet; /* skip a colon */
2438 packet = unpack_varlen_hex (packet, &slen);
2439 ++packet; /* skip a colon */
2440 src = xmalloc (slen + 1);
2441 nbytes = unhexify (src, packet, strlen (packet) / 2);
2444 newlast = xmalloc (sizeof (struct source_string));
2445 newlast->type = srctype;
2447 newlast->next = NULL;
2448 /* Always add a source string to the end of the list;
2449 this keeps sequences of actions/commands in the right
2451 if (tpoint->source_strings)
2453 for (last = tpoint->source_strings; last->next; last = last->next)
2455 last->next = newlast;
2458 tpoint->source_strings = newlast;
2464 cmd_qtdv (char *own_buf)
2466 ULONGEST num, val, builtin;
2469 struct trace_state_variable *tsv;
2470 char *packet = own_buf;
2472 packet += strlen ("QTDV:");
2474 packet = unpack_varlen_hex (packet, &num);
2475 ++packet; /* skip a colon */
2476 packet = unpack_varlen_hex (packet, &val);
2477 ++packet; /* skip a colon */
2478 packet = unpack_varlen_hex (packet, &builtin);
2479 ++packet; /* skip a colon */
2481 nbytes = strlen (packet) / 2;
2482 varname = xmalloc (nbytes + 1);
2483 nbytes = unhexify (varname, packet, nbytes);
2484 varname[nbytes] = '\0';
2486 tsv = create_trace_state_variable (num, 1);
2487 tsv->initial_value = (LONGEST) val;
2488 tsv->name = varname;
2490 set_trace_state_variable_value (num, (LONGEST) val);
2496 cmd_qtenable_disable (char *own_buf, int enable)
2498 char *packet = own_buf;
2500 struct tracepoint *tp;
2502 packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2503 packet = unpack_varlen_hex (packet, &num);
2504 ++packet; /* skip a colon */
2505 packet = unpack_varlen_hex (packet, &addr);
2507 tp = find_tracepoint (num, addr);
2511 if ((enable && tp->enabled) || (!enable && !tp->enabled))
2513 trace_debug ("Tracepoint %d at 0x%s is already %s",
2514 (int) num, paddress (addr),
2515 enable ? "enabled" : "disabled");
2520 trace_debug ("%s tracepoint %d at 0x%s",
2521 enable ? "Enabling" : "Disabling",
2522 (int) num, paddress (addr));
2524 tp->enabled = enable;
2526 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2529 int offset = offsetof (struct tracepoint, enabled);
2530 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2532 ret = prepare_to_access_memory ();
2535 trace_debug ("Failed to temporarily stop inferior threads");
2536 write_enn (own_buf);
2540 ret = write_inferior_integer (obj_addr, enable);
2541 done_accessing_memory ();
2545 trace_debug ("Cannot write enabled flag into "
2546 "inferior process memory");
2547 write_enn (own_buf);
2556 trace_debug ("Tracepoint %d at 0x%s not found",
2557 (int) num, paddress (addr));
2558 write_enn (own_buf);
2563 cmd_qtv (char *own_buf)
2568 char *packet = own_buf;
2570 packet += strlen ("qTV:");
2571 unpack_varlen_hex (packet, &num);
2573 if (current_traceframe >= 0)
2575 err = traceframe_read_tsv ((int) num, &val);
2578 strcpy (own_buf, "U");
2582 /* Only make tsv's be undefined before the first trace run. After a
2583 trace run is over, the user might want to see the last value of
2584 the tsv, and it might not be available in a traceframe. */
2585 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2587 strcpy (own_buf, "U");
2591 val = get_trace_state_variable_value (num);
2593 sprintf (own_buf, "V%s", phex_nz (val, 0));
2596 /* Clear out the list of readonly regions. */
2599 clear_readonly_regions (void)
2601 struct readonly_region *roreg;
2603 while (readonly_regions)
2605 roreg = readonly_regions;
2606 readonly_regions = readonly_regions->next;
2611 /* Parse the collection of address ranges whose contents GDB believes
2612 to be unchanging and so can be read directly from target memory
2613 even while looking at a traceframe. */
2616 cmd_qtro (char *own_buf)
2618 ULONGEST start, end;
2619 struct readonly_region *roreg;
2620 char *packet = own_buf;
2622 trace_debug ("Want to mark readonly regions");
2624 clear_readonly_regions ();
2626 packet += strlen ("QTro");
2628 while (*packet == ':')
2630 ++packet; /* skip a colon */
2631 packet = unpack_varlen_hex (packet, &start);
2632 ++packet; /* skip a comma */
2633 packet = unpack_varlen_hex (packet, &end);
2634 roreg = xmalloc (sizeof (struct readonly_region));
2635 roreg->start = start;
2637 roreg->next = readonly_regions;
2638 readonly_regions = roreg;
2639 trace_debug ("Added readonly region from 0x%s to 0x%s",
2640 paddress (roreg->start), paddress (roreg->end));
2646 /* Test to see if the given range is in our list of readonly ranges.
2647 We only test for being entirely within a range, GDB is not going to
2648 send a single memory packet that spans multiple regions. */
2651 in_readonly_region (CORE_ADDR addr, ULONGEST length)
2653 struct readonly_region *roreg;
2655 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2656 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2662 /* The maximum size of a jump pad entry. */
2663 static const int max_jump_pad_size = 0x100;
2665 static CORE_ADDR gdb_jump_pad_head;
2667 /* Return the address of the next free jump space. */
2670 get_jump_space_head (void)
2672 if (gdb_jump_pad_head == 0)
2674 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2675 &gdb_jump_pad_head))
2676 fatal ("error extracting jump_pad_buffer");
2679 return gdb_jump_pad_head;
2682 /* Reserve USED bytes from the jump space. */
2685 claim_jump_space (ULONGEST used)
2687 trace_debug ("claim_jump_space reserves %s bytes at %s",
2688 pulongest (used), paddress (gdb_jump_pad_head));
2689 gdb_jump_pad_head += used;
2692 static CORE_ADDR trampoline_buffer_head = 0;
2693 static CORE_ADDR trampoline_buffer_tail;
2695 /* Reserve USED bytes from the trampoline buffer and return the
2696 address of the start of the reserved space in TRAMPOLINE. Returns
2697 non-zero if the space is successfully claimed. */
2700 claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
2702 if (!trampoline_buffer_head)
2704 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
2705 &trampoline_buffer_tail))
2707 fatal ("error extracting trampoline_buffer");
2711 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2712 &trampoline_buffer_head))
2714 fatal ("error extracting trampoline_buffer_end");
2719 /* Start claiming space from the top of the trampoline space. If
2720 the space is located at the bottom of the virtual address space,
2721 this reduces the possibility that corruption will occur if a null
2722 pointer is used to write to memory. */
2723 if (trampoline_buffer_head - trampoline_buffer_tail < used)
2725 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2730 trampoline_buffer_head -= used;
2732 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2733 pulongest (used), paddress (trampoline_buffer_head));
2735 *trampoline = trampoline_buffer_head;
2739 /* Returns non-zero if there is space allocated for use in trampolines
2740 for fast tracepoints. */
2743 have_fast_tracepoint_trampoline_buffer (char *buf)
2745 CORE_ADDR trampoline_end, errbuf;
2747 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2750 fatal ("error extracting trampoline_buffer_end");
2757 strcpy (buf, "was claiming");
2758 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
2761 fatal ("error extracting errbuf");
2765 read_inferior_memory (errbuf, (unsigned char *) buf, 100);
2768 return trampoline_end != 0;
2771 /* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2772 the command fails, or 0 otherwise. If the command ran
2773 successfully, but probing the marker failed, ERROUT will be filled
2774 with the error to reply to GDB, and -1 is also returned. This
2775 allows directly passing IPA errors to GDB. */
2778 probe_marker_at (CORE_ADDR address, char *errout)
2780 char cmd[IPA_CMD_BUF_SIZE];
2783 sprintf (cmd, "probe_marker_at:%s", paddress (address));
2784 err = run_inferior_command (cmd);
2790 strcpy (errout, cmd);
2799 clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
2801 to->jump_pad = from->jump_pad;
2802 to->jump_pad_end = from->jump_pad_end;
2803 to->trampoline = from->trampoline;
2804 to->trampoline_end = from->trampoline_end;
2805 to->adjusted_insn_addr = from->adjusted_insn_addr;
2806 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
2807 to->handle = from->handle;
2809 gdb_assert (from->handle);
2810 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
2813 #define MAX_JUMP_SIZE 20
2815 /* Install fast tracepoint. Return 0 if successful, otherwise return
2819 install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
2821 CORE_ADDR jentry, jump_entry;
2822 CORE_ADDR trampoline;
2823 ULONGEST trampoline_size;
2825 /* The jump to the jump pad of the last fast tracepoint
2827 unsigned char fjump[MAX_JUMP_SIZE];
2828 ULONGEST fjump_size;
2830 if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
2832 trace_debug ("Requested a fast tracepoint on an instruction "
2833 "that is of less than the minimum length.");
2837 jentry = jump_entry = get_jump_space_head ();
2840 trampoline_size = 0;
2842 /* Install the jump pad. */
2843 err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
2845 ipa_sym_addrs.addr_gdb_collect,
2846 ipa_sym_addrs.addr_collecting,
2849 &trampoline, &trampoline_size,
2851 &tpoint->adjusted_insn_addr,
2852 &tpoint->adjusted_insn_addr_end,
2859 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
2862 if (tpoint->handle != NULL)
2864 tpoint->jump_pad = jump_entry;
2865 tpoint->jump_pad_end = jentry;
2866 tpoint->trampoline = trampoline;
2867 tpoint->trampoline_end = trampoline + trampoline_size;
2869 /* Pad to 8-byte alignment. */
2870 jentry = ((jentry + 7) & ~0x7);
2871 claim_jump_space (jentry - jump_entry);
2878 /* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2881 install_tracepoint (struct tracepoint *tpoint, char *own_buf)
2883 tpoint->handle = NULL;
2886 if (tpoint->type == trap_tracepoint)
2888 /* Tracepoints are installed as memory breakpoints. Just go
2889 ahead and install the trap. The breakpoints module
2890 handles duplicated breakpoints, and the memory read
2891 routine handles un-patching traps from memory reads. */
2892 tpoint->handle = set_breakpoint_at (tpoint->address,
2893 tracepoint_handler);
2895 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
2897 struct tracepoint *tp;
2899 if (!agent_loaded_p ())
2901 trace_debug ("Requested a %s tracepoint, but fast "
2902 "tracepoints aren't supported.",
2903 tpoint->type == static_tracepoint ? "static" : "fast");
2904 write_e_ipa_not_loaded (own_buf);
2907 if (tpoint->type == static_tracepoint
2908 && !in_process_agent_supports_ust ())
2910 trace_debug ("Requested a static tracepoint, but static "
2911 "tracepoints are not supported.");
2912 write_e_ust_not_loaded (own_buf);
2916 /* Find another fast or static tracepoint at the same address. */
2917 for (tp = tracepoints; tp; tp = tp->next)
2919 if (tp->address == tpoint->address && tp->type == tpoint->type
2920 && tp->number != tpoint->number)
2924 if (tpoint->type == fast_tracepoint)
2926 if (tp) /* TPOINT is installed at the same address as TP. */
2927 clone_fast_tracepoint (tpoint, tp);
2929 install_fast_tracepoint (tpoint, own_buf);
2934 tpoint->handle = (void *) -1;
2937 if (probe_marker_at (tpoint->address, own_buf) == 0)
2938 tpoint->handle = (void *) -1;
2944 internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
2946 if (tpoint->handle == NULL)
2948 if (*own_buf == '\0')
2949 write_enn (own_buf);
2956 cmd_qtstart (char *packet)
2958 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
2960 trace_debug ("Starting the trace");
2962 /* Pause all threads temporarily while we patch tracepoints. */
2965 /* Get threads out of jump pads. Safe to do here, since this is a
2966 top level command. And, required to do here, since we're
2967 deleting/rewriting jump pads. */
2969 stabilize_threads ();
2971 /* Freeze threads. */
2974 /* Sync the fast tracepoints list in the inferior ftlib. */
2975 if (agent_loaded_p ())
2977 download_tracepoints ();
2978 download_trace_state_variables ();
2981 /* No previous fast tpoint yet. */
2982 prev_ftpoint = NULL;
2984 /* No previous static tpoint yet. */
2985 prev_stpoint = NULL;
2989 /* Install tracepoints. */
2990 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2992 /* Ensure all the hit counts start at zero. */
2993 tpoint->hit_count = 0;
2994 tpoint->traceframe_usage = 0;
2996 if (tpoint->type == trap_tracepoint)
2998 /* Tracepoints are installed as memory breakpoints. Just go
2999 ahead and install the trap. The breakpoints module
3000 handles duplicated breakpoints, and the memory read
3001 routine handles un-patching traps from memory reads. */
3002 tpoint->handle = set_breakpoint_at (tpoint->address,
3003 tracepoint_handler);
3005 else if (tpoint->type == fast_tracepoint)
3007 if (maybe_write_ipa_not_loaded (packet))
3009 trace_debug ("Requested a fast tracepoint, but fast "
3010 "tracepoints aren't supported.");
3014 if (prev_ftpoint != NULL && prev_ftpoint->address == tpoint->address)
3015 clone_fast_tracepoint (tpoint, prev_ftpoint);
3018 if (install_fast_tracepoint (tpoint, packet) == 0)
3019 prev_ftpoint = tpoint;
3022 else if (tpoint->type == static_tracepoint)
3024 if (maybe_write_ipa_ust_not_loaded (packet))
3026 trace_debug ("Requested a static tracepoint, but static "
3027 "tracepoints are not supported.");
3031 /* Can only probe a given marker once. */
3032 if (prev_stpoint != NULL && prev_stpoint->address == tpoint->address)
3034 tpoint->handle = (void *) -1;
3038 if (probe_marker_at (tpoint->address, packet) == 0)
3040 tpoint->handle = (void *) -1;
3042 /* So that we can handle multiple static tracepoints
3043 at the same address easily. */
3044 prev_stpoint = tpoint;
3049 /* Any failure in the inner loop is sufficient cause to give
3051 if (tpoint->handle == NULL)
3055 /* Any error in tracepoint insertion is unacceptable; better to
3056 address the problem now, than end up with a useless or misleading
3060 clear_installed_tracepoints ();
3061 if (*packet == '\0')
3067 stopping_tracepoint = NULL;
3068 trace_buffer_is_full = 0;
3069 expr_eval_result = expr_eval_no_error;
3070 error_tracepoint = NULL;
3071 tracing_start_time = get_timestamp ();
3073 /* Tracing is now active, hits will now start being logged. */
3076 if (agent_loaded_p ())
3078 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
3079 fatal ("Error setting tracing variable in lib");
3081 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3083 fatal ("Error clearing stopping_tracepoint variable in lib");
3085 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
3086 fatal ("Error clearing trace_buffer_is_full variable in lib");
3088 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3089 stop_tracing_handler);
3090 if (stop_tracing_bkpt == NULL)
3091 error ("Error setting stop_tracing breakpoint");
3093 flush_trace_buffer_bkpt
3094 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3095 flush_trace_buffer_handler);
3096 if (flush_trace_buffer_bkpt == NULL)
3097 error ("Error setting flush_trace_buffer breakpoint");
3105 /* End a tracing run, filling in a stop reason to report back to GDB,
3106 and removing the tracepoints from the code. */
3113 trace_debug ("Tracing is already off, ignoring");
3117 trace_debug ("Stopping the trace");
3119 /* Pause all threads before removing fast jumps from memory,
3120 breakpoints, and touching IPA state variables (inferior memory).
3121 Some thread may hit the internal tracing breakpoints, or be
3122 collecting this moment, but that's ok, we don't release the
3123 tpoint object's memory or the jump pads here (we only do that
3124 when we're sure we can move all threads out of the jump pads).
3125 We can't now, since we may be getting here due to the inferior
3126 agent calling us. */
3128 /* Since we're removing breakpoints, cancel breakpoint hits,
3129 possibly related to the breakpoints we're about to delete. */
3130 cancel_breakpoints ();
3132 /* Stop logging. Tracepoints can still be hit, but they will not be
3135 if (agent_loaded_p ())
3137 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
3138 fatal ("Error clearing tracing variable in lib");
3141 tracing_stop_time = get_timestamp ();
3142 tracing_stop_reason = "t???";
3143 tracing_stop_tpnum = 0;
3144 if (stopping_tracepoint)
3146 trace_debug ("Stopping the trace because "
3147 "tracepoint %d was hit %ld times",
3148 stopping_tracepoint->number,
3149 stopping_tracepoint->pass_count);
3150 tracing_stop_reason = "tpasscount";
3151 tracing_stop_tpnum = stopping_tracepoint->number;
3153 else if (trace_buffer_is_full)
3155 trace_debug ("Stopping the trace because the trace buffer is full");
3156 tracing_stop_reason = "tfull";
3158 else if (expr_eval_result != expr_eval_no_error)
3160 trace_debug ("Stopping the trace because of an expression eval error");
3161 tracing_stop_reason = eval_result_names[expr_eval_result];
3162 tracing_stop_tpnum = error_tracepoint->number;
3164 #ifndef IN_PROCESS_AGENT
3165 else if (!gdb_connected ())
3167 trace_debug ("Stopping the trace because GDB disconnected");
3168 tracing_stop_reason = "tdisconnected";
3173 trace_debug ("Stopping the trace because of a tstop command");
3174 tracing_stop_reason = "tstop";
3177 stopping_tracepoint = NULL;
3178 error_tracepoint = NULL;
3180 /* Clear out the tracepoints. */
3181 clear_installed_tracepoints ();
3183 if (agent_loaded_p ())
3185 /* Pull in fast tracepoint trace frames from the inferior lib
3186 buffer into our buffer, even if our buffer is already full,
3187 because we want to present the full number of created frames
3188 in addition to what fit in the trace buffer. */
3189 upload_fast_traceframes ();
3192 if (stop_tracing_bkpt != NULL)
3194 delete_breakpoint (stop_tracing_bkpt);
3195 stop_tracing_bkpt = NULL;
3198 if (flush_trace_buffer_bkpt != NULL)
3200 delete_breakpoint (flush_trace_buffer_bkpt);
3201 flush_trace_buffer_bkpt = NULL;
3208 stop_tracing_handler (CORE_ADDR addr)
3210 trace_debug ("lib hit stop_tracing");
3212 /* Don't actually handle it here. When we stop tracing we remove
3213 breakpoints from the inferior, and that is not allowed in a
3214 breakpoint handler (as the caller is walking the breakpoint
3220 flush_trace_buffer_handler (CORE_ADDR addr)
3222 trace_debug ("lib hit flush_trace_buffer");
3227 cmd_qtstop (char *packet)
3234 cmd_qtdisconnected (char *own_buf)
3237 char *packet = own_buf;
3239 packet += strlen ("QTDisconnected:");
3241 unpack_varlen_hex (packet, &setting);
3245 disconnected_tracing = setting;
3249 cmd_qtframe (char *own_buf)
3251 ULONGEST frame, pc, lo, hi, num;
3253 struct traceframe *tframe;
3254 char *packet = own_buf;
3256 packet += strlen ("QTFrame:");
3258 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3260 packet += strlen ("pc:");
3261 unpack_varlen_hex (packet, &pc);
3262 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3263 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3265 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3267 packet += strlen ("range:");
3268 packet = unpack_varlen_hex (packet, &lo);
3270 unpack_varlen_hex (packet, &hi);
3271 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3272 paddress (lo), paddress (hi));
3273 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3275 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3277 packet += strlen ("outside:");
3278 packet = unpack_varlen_hex (packet, &lo);
3280 unpack_varlen_hex (packet, &hi);
3281 trace_debug ("Want to find next traceframe "
3282 "outside the range 0x%s to 0x%s",
3283 paddress (lo), paddress (hi));
3284 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3286 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3288 packet += strlen ("tdp:");
3289 unpack_varlen_hex (packet, &num);
3291 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3292 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3296 unpack_varlen_hex (packet, &frame);
3297 tfnum = (int) frame;
3300 trace_debug ("Want to stop looking at traceframes");
3301 current_traceframe = -1;
3305 trace_debug ("Want to look at traceframe %d", tfnum);
3306 tframe = find_traceframe (tfnum);
3311 current_traceframe = tfnum;
3312 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3315 sprintf (own_buf, "F-1");
3319 cmd_qtstatus (char *packet)
3321 char *stop_reason_rsp = NULL;
3322 char *buf1, *buf2, *buf3, *str;
3325 /* Translate the plain text of the notes back into hex for
3328 str = (tracing_user_name ? tracing_user_name : "");
3329 slen = strlen (str);
3330 buf1 = (char *) alloca (slen * 2 + 1);
3331 hexify (buf1, str, slen);
3333 str = (tracing_notes ? tracing_notes : "");
3334 slen = strlen (str);
3335 buf2 = (char *) alloca (slen * 2 + 1);
3336 hexify (buf2, str, slen);
3338 str = (tracing_stop_note ? tracing_stop_note : "");
3339 slen = strlen (str);
3340 buf3 = (char *) alloca (slen * 2 + 1);
3341 hexify (buf3, str, slen);
3343 trace_debug ("Returning trace status as %d, stop reason %s",
3344 tracing, tracing_stop_reason);
3346 if (agent_loaded_p ())
3350 upload_fast_traceframes ();
3355 stop_reason_rsp = (char *) tracing_stop_reason;
3357 /* The user visible error string in terror needs to be hex encoded.
3358 We leave it as plain string in `tracing_stop_reason' to ease
3360 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3362 const char *result_name;
3366 result_name = stop_reason_rsp + strlen ("terror:");
3367 hexstr_len = strlen (result_name) * 2;
3368 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3369 strcpy (p, "terror:");
3371 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3374 /* If this was a forced stop, include any stop note that was supplied. */
3375 if (strcmp (stop_reason_rsp, "tstop") == 0)
3377 stop_reason_rsp = alloca (strlen ("tstop:") + strlen (buf3) + 1);
3378 strcpy (stop_reason_rsp, "tstop:");
3379 strcat (stop_reason_rsp, buf3);
3385 "tframes:%x;tcreated:%x;"
3386 "tfree:%x;tsize:%s;"
3389 "starttime:%s;stoptime:%s;"
3390 "username:%s:;notes:%s:",
3392 stop_reason_rsp, tracing_stop_tpnum,
3393 traceframe_count, traceframes_created,
3394 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3395 circular_trace_buffer,
3396 disconnected_tracing,
3397 plongest (tracing_start_time), plongest (tracing_stop_time),
3402 cmd_qtp (char *own_buf)
3405 struct tracepoint *tpoint;
3406 char *packet = own_buf;
3408 packet += strlen ("qTP:");
3410 packet = unpack_varlen_hex (packet, &num);
3411 ++packet; /* skip a colon */
3412 packet = unpack_varlen_hex (packet, &addr);
3414 /* See if we already have this tracepoint. */
3415 tpoint = find_tracepoint (num, addr);
3419 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
3420 (int) num, paddress (addr));
3421 write_enn (own_buf);
3425 sprintf (own_buf, "V%lx:%lx", tpoint->hit_count, tpoint->traceframe_usage);
3428 /* State variables to help return all the tracepoint bits. */
3429 static struct tracepoint *cur_tpoint;
3430 static int cur_action;
3431 static int cur_step_action;
3432 static struct source_string *cur_source_string;
3433 static struct trace_state_variable *cur_tsv;
3435 /* Compose a response that is an imitation of the syntax by which the
3436 tracepoint was originally downloaded. */
3439 response_tracepoint (char *packet, struct tracepoint *tpoint)
3443 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
3444 paddress (tpoint->address),
3445 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3446 tpoint->pass_count);
3447 if (tpoint->type == fast_tracepoint)
3448 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
3449 else if (tpoint->type == static_tracepoint)
3450 sprintf (packet + strlen (packet), ":S");
3454 buf = gdb_unparse_agent_expr (tpoint->cond);
3455 sprintf (packet + strlen (packet), ":X%x,%s",
3456 tpoint->cond->length, buf);
3461 /* Compose a response that is an imitation of the syntax by which the
3462 tracepoint action was originally downloaded (with the difference
3463 that due to the way we store the actions, this will output a packet
3464 per action, while GDB could have combined more than one action
3468 response_action (char *packet, struct tracepoint *tpoint,
3469 char *taction, int step)
3471 sprintf (packet, "%c%x:%s:%s",
3472 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3476 /* Compose a response that is an imitation of the syntax by which the
3477 tracepoint source piece was originally downloaded. */
3480 response_source (char *packet,
3481 struct tracepoint *tpoint, struct source_string *src)
3486 len = strlen (src->str);
3487 buf = alloca (len * 2 + 1);
3488 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3490 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3491 tpoint->number, paddress (tpoint->address),
3492 src->type, 0, len, buf);
3495 /* Return the first piece of tracepoint definition, and initialize the
3496 state machine that will iterate through all the tracepoint
3500 cmd_qtfp (char *packet)
3502 trace_debug ("Returning first tracepoint definition piece");
3504 cur_tpoint = tracepoints;
3505 cur_action = cur_step_action = -1;
3506 cur_source_string = NULL;
3509 response_tracepoint (packet, cur_tpoint);
3511 strcpy (packet, "l");
3514 /* Return additional pieces of tracepoint definition. Each action and
3515 stepping action must go into its own packet, because of packet size
3516 limits, and so we use state variables to deliver one piece at a
3520 cmd_qtsp (char *packet)
3522 trace_debug ("Returning subsequent tracepoint definition piece");
3526 /* This case would normally never occur, but be prepared for
3528 strcpy (packet, "l");
3530 else if (cur_action < cur_tpoint->numactions - 1)
3533 response_action (packet, cur_tpoint,
3534 cur_tpoint->actions_str[cur_action], 0);
3536 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3539 response_action (packet, cur_tpoint,
3540 cur_tpoint->step_actions_str[cur_step_action], 1);
3542 else if ((cur_source_string
3543 ? cur_source_string->next
3544 : cur_tpoint->source_strings))
3546 if (cur_source_string)
3547 cur_source_string = cur_source_string->next;
3549 cur_source_string = cur_tpoint->source_strings;
3550 response_source (packet, cur_tpoint, cur_source_string);
3554 cur_tpoint = cur_tpoint->next;
3555 cur_action = cur_step_action = -1;
3556 cur_source_string = NULL;
3558 response_tracepoint (packet, cur_tpoint);
3560 strcpy (packet, "l");
3564 /* Compose a response that is an imitation of the syntax by which the
3565 trace state variable was originally downloaded. */
3568 response_tsv (char *packet, struct trace_state_variable *tsv)
3570 char *buf = (char *) "";
3575 namelen = strlen (tsv->name);
3576 buf = alloca (namelen * 2 + 1);
3577 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3580 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3581 tsv->getter ? 1 : 0, buf);
3584 /* Return the first trace state variable definition, and initialize
3585 the state machine that will iterate through all the tsv bits. */
3588 cmd_qtfv (char *packet)
3590 trace_debug ("Returning first trace state variable definition");
3592 cur_tsv = trace_state_variables;
3595 response_tsv (packet, cur_tsv);
3597 strcpy (packet, "l");
3600 /* Return additional trace state variable definitions. */
3603 cmd_qtsv (char *packet)
3605 trace_debug ("Returning first trace state variable definition");
3609 /* This case would normally never occur, but be prepared for
3611 strcpy (packet, "l");
3615 cur_tsv = cur_tsv->next;
3617 response_tsv (packet, cur_tsv);
3619 strcpy (packet, "l");
3622 strcpy (packet, "l");
3625 /* Return the first static tracepoint marker, and initialize the state
3626 machine that will iterate through all the static tracepoints
3630 cmd_qtfstm (char *packet)
3632 if (!maybe_write_ipa_ust_not_loaded (packet))
3633 run_inferior_command (packet);
3636 /* Return additional static tracepoints markers. */
3639 cmd_qtsstm (char *packet)
3641 if (!maybe_write_ipa_ust_not_loaded (packet))
3642 run_inferior_command (packet);
3645 /* Return the definition of the static tracepoint at a given address.
3646 Result packet is the same as qTsST's. */
3649 cmd_qtstmat (char *packet)
3651 if (!maybe_write_ipa_ust_not_loaded (packet))
3652 run_inferior_command (packet);
3655 /* Return the minimum instruction size needed for fast tracepoints as a
3656 hexadecimal number. */
3659 cmd_qtminftpilen (char *packet)
3661 if (current_inferior == NULL)
3663 /* Indicate that the minimum length is currently unknown. */
3664 strcpy (packet, "0");
3668 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
3671 /* Respond to qTBuffer packet with a block of raw data from the trace
3672 buffer. GDB may ask for a lot, but we are allowed to reply with
3673 only as much as will fit within packet limits or whatever. */
3676 cmd_qtbuffer (char *own_buf)
3678 ULONGEST offset, num, tot;
3680 char *packet = own_buf;
3682 packet += strlen ("qTBuffer:");
3684 packet = unpack_varlen_hex (packet, &offset);
3685 ++packet; /* skip a comma */
3686 unpack_varlen_hex (packet, &num);
3688 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3689 (int) num, pulongest (offset));
3691 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3693 /* If we're right at the end, reply specially that we're done. */
3696 strcpy (own_buf, "l");
3700 /* Object to any other out-of-bounds request. */
3703 write_enn (own_buf);
3707 /* Compute the pointer corresponding to the given offset, accounting
3709 tbp = trace_buffer_start + offset;
3710 if (tbp >= trace_buffer_wrap)
3711 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3713 /* Trim to the remaining bytes if we're close to the end. */
3714 if (num > tot - offset)
3717 /* Trim to available packet size. */
3718 if (num >= (PBUFSIZ - 16) / 2 )
3719 num = (PBUFSIZ - 16) / 2;
3721 convert_int_to_ascii (tbp, own_buf, num);
3722 own_buf[num] = '\0';
3726 cmd_bigqtbuffer_circular (char *own_buf)
3729 char *packet = own_buf;
3731 packet += strlen ("QTBuffer:circular:");
3733 unpack_varlen_hex (packet, &val);
3734 circular_trace_buffer = val;
3735 trace_debug ("Trace buffer is now %s",
3736 circular_trace_buffer ? "circular" : "linear");
3741 cmd_qtnotes (char *own_buf)
3744 char *saved, *user, *notes, *stopnote;
3745 char *packet = own_buf;
3747 packet += strlen ("QTNotes:");
3751 if (strncmp ("user:", packet, strlen ("user:")) == 0)
3753 packet += strlen ("user:");
3755 packet = strchr (packet, ';');
3756 nbytes = (packet - saved) / 2;
3757 user = xmalloc (nbytes + 1);
3758 nbytes = unhexify (user, saved, nbytes);
3759 user[nbytes] = '\0';
3760 ++packet; /* skip the semicolon */
3761 trace_debug ("User is '%s'", user);
3762 tracing_user_name = user;
3764 else if (strncmp ("notes:", packet, strlen ("notes:")) == 0)
3766 packet += strlen ("notes:");
3768 packet = strchr (packet, ';');
3769 nbytes = (packet - saved) / 2;
3770 notes = xmalloc (nbytes + 1);
3771 nbytes = unhexify (notes, saved, nbytes);
3772 notes[nbytes] = '\0';
3773 ++packet; /* skip the semicolon */
3774 trace_debug ("Notes is '%s'", notes);
3775 tracing_notes = notes;
3777 else if (strncmp ("tstop:", packet, strlen ("tstop:")) == 0)
3779 packet += strlen ("tstop:");
3781 packet = strchr (packet, ';');
3782 nbytes = (packet - saved) / 2;
3783 stopnote = xmalloc (nbytes + 1);
3784 nbytes = unhexify (stopnote, saved, nbytes);
3785 stopnote[nbytes] = '\0';
3786 ++packet; /* skip the semicolon */
3787 trace_debug ("tstop note is '%s'", stopnote);
3788 tracing_stop_note = stopnote;
3798 handle_tracepoint_general_set (char *packet)
3800 if (strcmp ("QTinit", packet) == 0)
3802 cmd_qtinit (packet);
3805 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3810 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3812 cmd_qtdpsrc (packet);
3815 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
3817 cmd_qtenable_disable (packet, 1);
3820 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
3822 cmd_qtenable_disable (packet, 0);
3825 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3830 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3835 else if (strcmp ("QTStart", packet) == 0)
3837 cmd_qtstart (packet);
3840 else if (strcmp ("QTStop", packet) == 0)
3842 cmd_qtstop (packet);
3845 else if (strncmp ("QTDisconnected:", packet,
3846 strlen ("QTDisconnected:")) == 0)
3848 cmd_qtdisconnected (packet);
3851 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
3853 cmd_qtframe (packet);
3856 else if (strncmp ("QTBuffer:circular:", packet, strlen ("QTBuffer:circular:")) == 0)
3858 cmd_bigqtbuffer_circular (packet);
3861 else if (strncmp ("QTNotes:", packet, strlen ("QTNotes:")) == 0)
3863 cmd_qtnotes (packet);
3871 handle_tracepoint_query (char *packet)
3873 if (strcmp ("qTStatus", packet) == 0)
3875 cmd_qtstatus (packet);
3878 else if (strncmp ("qTP:", packet, strlen ("qTP:")) == 0)
3883 else if (strcmp ("qTfP", packet) == 0)
3888 else if (strcmp ("qTsP", packet) == 0)
3893 else if (strcmp ("qTfV", packet) == 0)
3898 else if (strcmp ("qTsV", packet) == 0)
3903 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
3908 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
3910 cmd_qtbuffer (packet);
3913 else if (strcmp ("qTfSTM", packet) == 0)
3915 cmd_qtfstm (packet);
3918 else if (strcmp ("qTsSTM", packet) == 0)
3920 cmd_qtsstm (packet);
3923 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
3925 cmd_qtstmat (packet);
3928 else if (strcmp ("qTMinFTPILen", packet) == 0)
3930 cmd_qtminftpilen (packet);
3938 #ifndef IN_PROCESS_AGENT
3940 /* Call this when thread TINFO has hit the tracepoint defined by
3941 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3942 action. This adds a while-stepping collecting state item to the
3943 threads' collecting state list, so that we can keep track of
3944 multiple simultaneous while-stepping actions being collected by the
3945 same thread. This can happen in cases like:
3947 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3949 ff0003 INSN3 <-- TP2, collect $regs
3950 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3953 Notice that when instruction INSN5 is reached, the while-stepping
3954 actions of both TP1 and TP3 are still being collected, and that TP2
3955 had been collected meanwhile. The whole range of ff0001-ff0005
3956 should be single-stepped, due to at least TP1's while-stepping
3957 action covering the whole range. */
3960 add_while_stepping_state (struct thread_info *tinfo,
3961 int tp_number, CORE_ADDR tp_address)
3963 struct wstep_state *wstep;
3965 wstep = xmalloc (sizeof (*wstep));
3966 wstep->next = tinfo->while_stepping;
3968 wstep->tp_number = tp_number;
3969 wstep->tp_address = tp_address;
3970 wstep->current_step = 0;
3972 tinfo->while_stepping = wstep;
3975 /* Release the while-stepping collecting state WSTEP. */
3978 release_while_stepping_state (struct wstep_state *wstep)
3983 /* Release all while-stepping collecting states currently associated
3984 with thread TINFO. */
3987 release_while_stepping_state_list (struct thread_info *tinfo)
3989 struct wstep_state *head;
3991 while (tinfo->while_stepping)
3993 head = tinfo->while_stepping;
3994 tinfo->while_stepping = head->next;
3995 release_while_stepping_state (head);
3999 /* If TINFO was handling a 'while-stepping' action, the step has
4000 finished, so collect any step data needed, and check if any more
4001 steps are required. Return true if the thread was indeed
4002 collecting tracepoint data, false otherwise. */
4005 tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
4007 struct tracepoint *tpoint;
4008 struct wstep_state *wstep;
4009 struct wstep_state **wstep_link;
4010 struct trap_tracepoint_ctx ctx;
4012 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
4014 if (agent_loaded_p ())
4015 upload_fast_traceframes ();
4017 /* Check if we were indeed collecting data for one of more
4018 tracepoints with a 'while-stepping' count. */
4019 if (tinfo->while_stepping == NULL)
4024 /* We're not even tracing anymore. Stop this thread from
4026 release_while_stepping_state_list (tinfo);
4028 /* The thread had stopped due to a single-step request indeed
4029 explained by a tracepoint. */
4033 wstep = tinfo->while_stepping;
4034 wstep_link = &tinfo->while_stepping;
4036 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
4037 target_pid_to_str (tinfo->entry.id),
4038 wstep->tp_number, paddress (wstep->tp_address));
4040 ctx.base.type = trap_tracepoint;
4041 ctx.regcache = get_thread_regcache (tinfo, 1);
4043 while (wstep != NULL)
4045 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
4048 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
4049 wstep->tp_number, paddress (wstep->tp_address),
4050 target_pid_to_str (tinfo->entry.id));
4053 *wstep_link = wstep->next;
4054 release_while_stepping_state (wstep);
4055 wstep = *wstep_link;
4059 /* We've just finished one step. */
4060 ++wstep->current_step;
4063 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
4064 stop_pc, tpoint, wstep->current_step);
4066 if (wstep->current_step >= tpoint->step_count)
4068 /* The requested numbers of steps have occurred. */
4069 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
4070 target_pid_to_str (tinfo->entry.id),
4071 wstep->tp_number, paddress (wstep->tp_address));
4073 /* Unlink the wstep. */
4074 *wstep_link = wstep->next;
4075 release_while_stepping_state (wstep);
4076 wstep = *wstep_link;
4078 /* Only check the hit count now, which ensure that we do all
4079 our stepping before stopping the run. */
4080 if (tpoint->pass_count > 0
4081 && tpoint->hit_count >= tpoint->pass_count
4082 && stopping_tracepoint == NULL)
4083 stopping_tracepoint = tpoint;
4087 /* Keep single-stepping until the requested numbers of steps
4089 wstep_link = &wstep->next;
4090 wstep = *wstep_link;
4093 if (stopping_tracepoint
4094 || trace_buffer_is_full
4095 || expr_eval_result != expr_eval_no_error)
4105 /* Handle any internal tracing control breakpoint hits. That means,
4106 pull traceframes from the IPA to our buffer, and syncing both
4107 tracing agents when the IPA's tracing stops for some reason. */
4110 handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4112 /* Pull in fast tracepoint trace frames from the inferior in-process
4113 agent's buffer into our buffer. */
4115 if (!agent_loaded_p ())
4118 upload_fast_traceframes ();
4120 /* Check if the in-process agent had decided we should stop
4122 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4124 int ipa_trace_buffer_is_full;
4125 CORE_ADDR ipa_stopping_tracepoint;
4126 int ipa_expr_eval_result;
4127 CORE_ADDR ipa_error_tracepoint;
4129 trace_debug ("lib stopped at stop_tracing");
4131 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4132 &ipa_trace_buffer_is_full);
4134 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4135 &ipa_stopping_tracepoint);
4136 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4138 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4139 &ipa_error_tracepoint);
4140 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4142 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4143 &ipa_expr_eval_result);
4144 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4146 trace_debug ("lib: trace_buffer_is_full: %d, "
4147 "stopping_tracepoint: %s, "
4148 "ipa_expr_eval_result: %d, "
4149 "error_tracepoint: %s, ",
4150 ipa_trace_buffer_is_full,
4151 paddress (ipa_stopping_tracepoint),
4152 ipa_expr_eval_result,
4153 paddress (ipa_error_tracepoint));
4157 if (ipa_trace_buffer_is_full)
4158 trace_debug ("lib stopped due to full buffer.");
4159 if (ipa_stopping_tracepoint)
4160 trace_debug ("lib stopped due to tpoint");
4161 if (ipa_stopping_tracepoint)
4162 trace_debug ("lib stopped due to error");
4165 if (ipa_stopping_tracepoint != 0)
4168 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4170 else if (ipa_expr_eval_result != expr_eval_no_error)
4172 expr_eval_result = ipa_expr_eval_result;
4174 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4179 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4181 trace_debug ("lib stopped at flush_trace_buffer");
4188 /* Return true if TINFO just hit a tracepoint. Collect data if
4192 tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4194 struct tracepoint *tpoint;
4196 struct trap_tracepoint_ctx ctx;
4198 /* Not tracing, don't handle. */
4202 ctx.base.type = trap_tracepoint;
4203 ctx.regcache = get_thread_regcache (tinfo, 1);
4205 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4207 /* Note that we collect fast tracepoints here as well. We'll
4208 step over the fast tracepoint jump later, which avoids the
4209 double collect. However, we don't collect for static
4210 tracepoints here, because UST markers are compiled in program,
4211 and probes will be executed in program. So static tracepoints
4212 are collected there. */
4213 if (tpoint->enabled && stop_pc == tpoint->address
4214 && tpoint->type != static_tracepoint)
4216 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4217 target_pid_to_str (tinfo->entry.id),
4218 tpoint->number, paddress (tpoint->address));
4220 /* Test the condition if present, and collect if true. */
4222 || (condition_true_at_tracepoint
4223 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4224 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4227 if (stopping_tracepoint
4228 || trace_buffer_is_full
4229 || expr_eval_result != expr_eval_no_error)
4233 /* If the tracepoint had a 'while-stepping' action, then set
4234 the thread to collect this tracepoint on the following
4236 else if (tpoint->step_count > 0)
4238 add_while_stepping_state (tinfo,
4239 tpoint->number, tpoint->address);
4251 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4252 struct ust_marker_data;
4253 static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4254 struct traceframe *tframe);
4257 /* Create a trace frame for the hit of the given tracepoint in the
4261 collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4262 struct tracepoint *tpoint)
4264 struct traceframe *tframe;
4267 /* Only count it as a hit when we actually collect data. */
4268 tpoint->hit_count++;
4270 /* If we've exceeded a defined pass count, record the event for
4271 later, and finish the collection for this hit. This test is only
4272 for nonstepping tracepoints, stepping tracepoints test at the end
4273 of their while-stepping loop. */
4274 if (tpoint->pass_count > 0
4275 && tpoint->hit_count >= tpoint->pass_count
4276 && tpoint->step_count == 0
4277 && stopping_tracepoint == NULL)
4278 stopping_tracepoint = tpoint;
4280 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4281 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4283 tframe = add_traceframe (tpoint);
4287 for (acti = 0; acti < tpoint->numactions; ++acti)
4289 #ifndef IN_PROCESS_AGENT
4290 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4291 tpoint->number, paddress (tpoint->address),
4292 tpoint->actions_str[acti]);
4295 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4296 tpoint->actions[acti]);
4299 finish_traceframe (tframe);
4302 if (tframe == NULL && tracing)
4303 trace_buffer_is_full = 1;
4306 #ifndef IN_PROCESS_AGENT
4309 collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4311 struct tracepoint *tpoint, int current_step)
4313 struct traceframe *tframe;
4316 trace_debug ("Making new step traceframe for "
4317 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4318 tpoint->number, paddress (tpoint->address),
4319 current_step, tpoint->step_count,
4322 tframe = add_traceframe (tpoint);
4326 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4328 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4329 tpoint->number, paddress (tpoint->address),
4330 tpoint->step_actions_str[acti]);
4332 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4333 tpoint->step_actions[acti]);
4336 finish_traceframe (tframe);
4339 if (tframe == NULL && tracing)
4340 trace_buffer_is_full = 1;
4345 static struct regcache *
4346 get_context_regcache (struct tracepoint_hit_ctx *ctx)
4348 struct regcache *regcache = NULL;
4350 #ifdef IN_PROCESS_AGENT
4351 if (ctx->type == fast_tracepoint)
4353 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4354 if (!fctx->regcache_initted)
4356 fctx->regcache_initted = 1;
4357 init_register_cache (&fctx->regcache, fctx->regspace);
4358 supply_regblock (&fctx->regcache, NULL);
4359 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4361 regcache = &fctx->regcache;
4364 if (ctx->type == static_tracepoint)
4366 struct static_tracepoint_ctx *sctx
4367 = (struct static_tracepoint_ctx *) ctx;
4369 if (!sctx->regcache_initted)
4371 sctx->regcache_initted = 1;
4372 init_register_cache (&sctx->regcache, sctx->regspace);
4373 supply_regblock (&sctx->regcache, NULL);
4374 /* Pass down the tracepoint address, because REGS doesn't
4375 include the PC, but we know what it must have been. */
4376 supply_static_tracepoint_registers (&sctx->regcache,
4377 (const unsigned char *)
4379 sctx->tpoint->address);
4381 regcache = &sctx->regcache;
4385 if (ctx->type == trap_tracepoint)
4387 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4388 regcache = tctx->regcache;
4392 gdb_assert (regcache != NULL);
4398 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4400 struct tracepoint *tpoint,
4401 struct traceframe *tframe,
4402 struct tracepoint_action *taction)
4404 enum eval_result_type err;
4406 switch (taction->type)
4410 struct collect_memory_action *maction;
4412 maction = (struct collect_memory_action *) taction;
4414 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4415 pulongest (maction->len),
4416 paddress (maction->addr), maction->basereg);
4417 /* (should use basereg) */
4418 agent_mem_read (tframe, NULL,
4419 (CORE_ADDR) maction->addr, maction->len);
4424 unsigned char *regspace;
4425 struct regcache tregcache;
4426 struct regcache *context_regcache;
4429 trace_debug ("Want to collect registers");
4431 /* Collect all registers for now. */
4432 regspace = add_traceframe_block (tframe,
4433 1 + register_cache_size ());
4434 if (regspace == NULL)
4436 trace_debug ("Trace buffer block allocation failed, skipping");
4439 /* Identify a register block. */
4442 context_regcache = get_context_regcache (ctx);
4444 /* Wrap the regblock in a register cache (in the stack, we
4445 don't want to malloc here). */
4446 init_register_cache (&tregcache, regspace + 1);
4448 /* Copy the register data to the regblock. */
4449 regcache_cpy (&tregcache, context_regcache);
4451 #ifndef IN_PROCESS_AGENT
4452 /* On some platforms, trap-based tracepoints will have the PC
4453 pointing to the next instruction after the trap, but we
4454 don't want the user or GDB trying to guess whether the
4455 saved PC needs adjusting; so always record the adjusted
4456 stop_pc. Note that we can't use tpoint->address instead,
4457 since it will be wrong for while-stepping actions. This
4458 adjustment is a nop for fast tracepoints collected from the
4459 in-process lib (but not if GDBserver is collecting one
4460 preemptively), since the PC had already been adjusted to
4461 contain the tracepoint's address by the jump pad. */
4462 trace_debug ("Storing stop pc (0x%s) in regblock",
4463 paddress (stop_pc));
4465 /* This changes the regblock, not the thread's
4467 regcache_write_pc (&tregcache, stop_pc);
4473 struct eval_expr_action *eaction;
4475 eaction = (struct eval_expr_action *) taction;
4477 trace_debug ("Want to evaluate expression");
4479 err = eval_tracepoint_agent_expr (ctx, tframe, eaction->expr, NULL);
4481 if (err != expr_eval_no_error)
4483 record_tracepoint_error (tpoint, "action expression", err);
4490 #if defined IN_PROCESS_AGENT && defined HAVE_UST
4491 trace_debug ("Want to collect static trace data");
4492 collect_ust_data_at_tracepoint (ctx, tframe);
4494 trace_debug ("warning: collecting static trace data, "
4495 "but static tracepoints are not supported");
4500 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4506 condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4507 struct tracepoint *tpoint)
4510 enum eval_result_type err;
4512 /* Presently, gdbserver doesn't run compiled conditions, only the
4513 IPA does. If the program stops at a fast tracepoint's address
4514 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4515 gdbserver preemptively collect the fast tracepoint. Later, on
4516 resume, gdbserver steps over the fast tracepoint like it steps
4517 over breakpoints, so that the IPA doesn't see that fast
4518 tracepoint. This avoids double collects of fast tracepoints in
4519 that stopping scenario. Having gdbserver itself handle the fast
4520 tracepoint gives the user a consistent view of when fast or trap
4521 tracepoints are collected, compared to an alternative where only
4522 trap tracepoints are collected on stop, and fast tracepoints on
4523 resume. When a fast tracepoint is being processed by gdbserver,
4524 it is always the non-compiled condition expression that is
4526 #ifdef IN_PROCESS_AGENT
4527 if (tpoint->compiled_cond)
4528 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4531 err = eval_tracepoint_agent_expr (ctx, NULL, tpoint->cond, &value);
4533 if (err != expr_eval_no_error)
4535 record_tracepoint_error (tpoint, "condition", err);
4536 /* The error case must return false. */
4540 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4541 tpoint->number, paddress (tpoint->address),
4543 return (value ? 1 : 0);
4546 /* Evaluates a tracepoint agent expression with context CTX,
4547 traceframe TFRAME, agent expression AEXPR and store the
4550 static enum eval_result_type
4551 eval_tracepoint_agent_expr (struct tracepoint_hit_ctx *ctx,
4552 struct traceframe *tframe,
4553 struct agent_expr *aexpr,
4556 struct regcache *regcache;
4557 regcache = get_context_regcache (ctx);
4559 return gdb_eval_agent_expr (regcache, tframe, aexpr, rslt);
4562 /* Do memory copies for bytecodes. */
4563 /* Do the recording of memory blocks for actions and bytecodes. */
4566 agent_mem_read (struct traceframe *tframe,
4567 unsigned char *to, CORE_ADDR from, ULONGEST len)
4569 unsigned char *mspace;
4570 ULONGEST remaining = len;
4571 unsigned short blocklen;
4573 /* If a 'to' buffer is specified, use it. */
4576 read_inferior_memory (from, to, len);
4580 /* Otherwise, create a new memory block in the trace buffer. */
4581 while (remaining > 0)
4585 blocklen = (remaining > 65535 ? 65535 : remaining);
4586 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4587 mspace = add_traceframe_block (tframe, sp);
4590 /* Identify block as a memory block. */
4593 /* Record address and size. */
4594 memcpy (mspace, &from, sizeof (from));
4595 mspace += sizeof (from);
4596 memcpy (mspace, &blocklen, sizeof (blocklen));
4597 mspace += sizeof (blocklen);
4598 /* Record the memory block proper. */
4599 read_inferior_memory (from, mspace, blocklen);
4600 trace_debug ("%d bytes recorded", blocklen);
4601 remaining -= blocklen;
4608 agent_mem_read_string (struct traceframe *tframe,
4609 unsigned char *to, CORE_ADDR from, ULONGEST len)
4611 unsigned char *buf, *mspace;
4612 ULONGEST remaining = len;
4613 unsigned short blocklen, i;
4615 /* To save a bit of space, block lengths are 16-bit, so break large
4616 requests into multiple blocks. Bordering on overkill for strings,
4617 but it could happen that someone specifies a large max length. */
4618 while (remaining > 0)
4622 blocklen = (remaining > 65535 ? 65535 : remaining);
4623 /* We want working space to accumulate nonzero bytes, since
4624 traceframes must have a predecided size (otherwise it gets
4625 harder to wrap correctly for the circular case, etc). */
4626 buf = (unsigned char *) xmalloc (blocklen + 1);
4627 for (i = 0; i < blocklen; ++i)
4629 /* Read the string one byte at a time, in case the string is
4630 at the end of a valid memory area - we don't want a
4631 correctly-terminated string to engender segvio
4633 read_inferior_memory (from + i, buf + i, 1);
4638 /* Make sure outer loop stops now too. */
4639 remaining = blocklen;
4643 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4644 mspace = add_traceframe_block (tframe, sp);
4650 /* Identify block as a memory block. */
4653 /* Record address and size. */
4654 memcpy ((void *) mspace, (void *) &from, sizeof (from));
4655 mspace += sizeof (from);
4656 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
4657 mspace += sizeof (blocklen);
4658 /* Copy the string contents. */
4659 memcpy ((void *) mspace, (void *) buf, blocklen);
4660 remaining -= blocklen;
4667 /* Record the value of a trace state variable. */
4670 agent_tsv_read (struct traceframe *tframe, int n)
4672 unsigned char *vspace;
4675 vspace = add_traceframe_block (tframe,
4676 1 + sizeof (n) + sizeof (LONGEST));
4679 /* Identify block as a variable. */
4681 /* Record variable's number and value. */
4682 memcpy (vspace + 1, &n, sizeof (n));
4683 val = get_trace_state_variable_value (n);
4684 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
4685 trace_debug ("Variable %d recorded", n);
4689 #ifndef IN_PROCESS_AGENT
4691 /* Callback for traceframe_walk_blocks, used to find a given block
4692 type in a traceframe. */
4695 match_blocktype (char blocktype, unsigned char *dataptr, void *data)
4697 char *wantedp = data;
4699 if (*wantedp == blocktype)
4705 /* Walk over all traceframe blocks of the traceframe buffer starting
4706 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4707 block found, passing in DATA unmodified. If CALLBACK returns true,
4708 this returns a pointer to where the block is found. Returns NULL
4709 if no callback call returned true, indicating that all blocks have
4712 static unsigned char *
4713 traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
4715 int (*callback) (char blocktype,
4716 unsigned char *dataptr,
4720 unsigned char *dataptr;
4724 trace_debug ("traceframe %d has no data", tfnum);
4728 /* Iterate through a traceframe's blocks, looking for a block of the
4730 for (dataptr = database;
4731 dataptr < database + datasize;
4735 unsigned short mlen;
4737 if (dataptr == trace_buffer_wrap)
4739 /* Adjust to reflect wrapping part of the frame around to
4741 datasize = dataptr - database;
4742 dataptr = database = trace_buffer_lo;
4745 blocktype = *dataptr++;
4747 if ((*callback) (blocktype, dataptr, data))
4753 /* Skip over the registers block. */
4754 dataptr += register_cache_size ();
4757 /* Skip over the memory block. */
4758 dataptr += sizeof (CORE_ADDR);
4759 memcpy (&mlen, dataptr, sizeof (mlen));
4760 dataptr += (sizeof (mlen) + mlen);
4763 /* Skip over the TSV block. */
4764 dataptr += (sizeof (int) + sizeof (LONGEST));
4767 /* Skip over the static trace data block. */
4768 memcpy (&mlen, dataptr, sizeof (mlen));
4769 dataptr += (sizeof (mlen) + mlen);
4772 trace_debug ("traceframe %d has unknown block type 0x%x",
4781 /* Look for the block of type TYPE_WANTED in the trameframe starting
4782 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4785 static unsigned char *
4786 traceframe_find_block_type (unsigned char *database, unsigned int datasize,
4787 int tfnum, char type_wanted)
4789 return traceframe_walk_blocks (database, datasize, tfnum,
4790 match_blocktype, &type_wanted);
4793 static unsigned char *
4794 traceframe_find_regblock (struct traceframe *tframe, int tfnum)
4796 unsigned char *regblock;
4798 regblock = traceframe_find_block_type (tframe->data,
4802 if (regblock == NULL)
4803 trace_debug ("traceframe %d has no register data", tfnum);
4808 /* Get registers from a traceframe. */
4811 fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
4813 unsigned char *dataptr;
4814 struct tracepoint *tpoint;
4815 struct traceframe *tframe;
4817 tframe = find_traceframe (tfnum);
4821 trace_debug ("traceframe %d not found", tfnum);
4825 dataptr = traceframe_find_regblock (tframe, tfnum);
4826 if (dataptr == NULL)
4828 /* Mark registers unavailable. */
4829 supply_regblock (regcache, NULL);
4831 /* We can generally guess at a PC, although this will be
4832 misleading for while-stepping frames and multi-location
4834 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
4836 regcache_write_pc (regcache, tpoint->address);
4839 supply_regblock (regcache, dataptr);
4845 traceframe_get_pc (struct traceframe *tframe)
4847 struct regcache regcache;
4848 unsigned char *dataptr;
4850 dataptr = traceframe_find_regblock (tframe, -1);
4851 if (dataptr == NULL)
4854 init_register_cache (®cache, dataptr);
4855 return regcache_read_pc (®cache);
4858 /* Read a requested block of memory from a trace frame. */
4861 traceframe_read_mem (int tfnum, CORE_ADDR addr,
4862 unsigned char *buf, ULONGEST length,
4865 struct traceframe *tframe;
4866 unsigned char *database, *dataptr;
4867 unsigned int datasize;
4869 unsigned short mlen;
4871 trace_debug ("traceframe_read_mem");
4873 tframe = find_traceframe (tfnum);
4877 trace_debug ("traceframe %d not found", tfnum);
4881 datasize = tframe->data_size;
4882 database = dataptr = &tframe->data[0];
4884 /* Iterate through a traceframe's blocks, looking for memory. */
4885 while ((dataptr = traceframe_find_block_type (dataptr,
4887 - (dataptr - database),
4888 tfnum, 'M')) != NULL)
4890 memcpy (&maddr, dataptr, sizeof (maddr));
4891 dataptr += sizeof (maddr);
4892 memcpy (&mlen, dataptr, sizeof (mlen));
4893 dataptr += sizeof (mlen);
4894 trace_debug ("traceframe %d has %d bytes at %s",
4895 tfnum, mlen, paddress (maddr));
4897 /* If the block includes the first part of the desired range,
4898 return as much it has; GDB will re-request the remainder,
4899 which might be in a different block of this trace frame. */
4900 if (maddr <= addr && addr < (maddr + mlen))
4902 ULONGEST amt = (maddr + mlen) - addr;
4906 memcpy (buf, dataptr + (addr - maddr), amt);
4911 /* Skip over this block. */
4915 trace_debug ("traceframe %d has no memory data for the desired region",
4923 traceframe_read_tsv (int tsvnum, LONGEST *val)
4926 struct traceframe *tframe;
4927 unsigned char *database, *dataptr;
4928 unsigned int datasize;
4931 trace_debug ("traceframe_read_tsv");
4933 tfnum = current_traceframe;
4937 trace_debug ("no current traceframe");
4941 tframe = find_traceframe (tfnum);
4945 trace_debug ("traceframe %d not found", tfnum);
4949 datasize = tframe->data_size;
4950 database = dataptr = &tframe->data[0];
4952 /* Iterate through a traceframe's blocks, looking for the tsv. */
4953 while ((dataptr = traceframe_find_block_type (dataptr,
4955 - (dataptr - database),
4956 tfnum, 'V')) != NULL)
4958 memcpy (&vnum, dataptr, sizeof (vnum));
4959 dataptr += sizeof (vnum);
4961 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
4963 /* Check that this is the variable we want. */
4966 memcpy (val, dataptr, sizeof (*val));
4970 /* Skip over this block. */
4971 dataptr += sizeof (LONGEST);
4974 trace_debug ("traceframe %d has no data for variable %d",
4979 /* Read a requested block of static tracepoint data from a trace
4983 traceframe_read_sdata (int tfnum, ULONGEST offset,
4984 unsigned char *buf, ULONGEST length,
4987 struct traceframe *tframe;
4988 unsigned char *database, *dataptr;
4989 unsigned int datasize;
4990 unsigned short mlen;
4992 trace_debug ("traceframe_read_sdata");
4994 tframe = find_traceframe (tfnum);
4998 trace_debug ("traceframe %d not found", tfnum);
5002 datasize = tframe->data_size;
5003 database = &tframe->data[0];
5005 /* Iterate through a traceframe's blocks, looking for static
5007 dataptr = traceframe_find_block_type (database, datasize,
5009 if (dataptr != NULL)
5011 memcpy (&mlen, dataptr, sizeof (mlen));
5012 dataptr += sizeof (mlen);
5015 if (offset + length > mlen)
5016 length = mlen - offset;
5018 memcpy (buf, dataptr, length);
5026 trace_debug ("traceframe %d has no static trace data", tfnum);
5032 /* Callback for traceframe_walk_blocks. Builds a traceframe-info
5033 object. DATA is pointer to a struct buffer holding the
5034 traceframe-info object being built. */
5037 build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5039 struct buffer *buffer = data;
5045 unsigned short mlen;
5048 memcpy (&maddr, dataptr, sizeof (maddr));
5049 dataptr += sizeof (maddr);
5050 memcpy (&mlen, dataptr, sizeof (mlen));
5051 dataptr += sizeof (mlen);
5052 buffer_xml_printf (buffer,
5053 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5054 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5064 warning ("Unhandled trace block type (%d) '%c ' "
5065 "while building trace frame info.",
5066 blocktype, blocktype);
5073 /* Build a traceframe-info object for traceframe number TFNUM into
5077 traceframe_read_info (int tfnum, struct buffer *buffer)
5079 struct traceframe *tframe;
5081 trace_debug ("traceframe_read_info");
5083 tframe = find_traceframe (tfnum);
5087 trace_debug ("traceframe %d not found", tfnum);
5091 buffer_grow_str (buffer, "<traceframe-info>\n");
5092 traceframe_walk_blocks (tframe->data, tframe->data_size,
5093 tfnum, build_traceframe_info_xml, buffer);
5094 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5098 /* Return the first fast tracepoint whose jump pad contains PC. */
5100 static struct tracepoint *
5101 fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5103 struct tracepoint *tpoint;
5105 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5106 if (tpoint->type == fast_tracepoint)
5107 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5113 /* Return the first fast tracepoint whose trampoline contains PC. */
5115 static struct tracepoint *
5116 fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5118 struct tracepoint *tpoint;
5120 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5122 if (tpoint->type == fast_tracepoint
5123 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5130 /* Return GDBserver's tracepoint that matches the IP Agent's
5131 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5134 static struct tracepoint *
5135 fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5137 struct tracepoint *tpoint;
5139 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5140 if (tpoint->type == fast_tracepoint)
5141 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5149 /* The type of the object that is used to synchronize fast tracepoint
5152 typedef struct collecting_t
5154 /* The fast tracepoint number currently collecting. */
5157 /* A number that GDBserver can use to identify the thread that is
5158 presently holding the collect lock. This need not (and usually
5159 is not) the thread id, as getting the current thread ID usually
5160 requires a system call, which we want to avoid like the plague.
5161 Usually this is thread's TCB, found in the TLS (pseudo-)
5162 register, which is readable with a single insn on several
5164 uintptr_t thread_area;
5167 #ifndef IN_PROCESS_AGENT
5170 force_unlock_trace_buffer (void)
5172 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5175 /* Check if the thread identified by THREAD_AREA which is stopped at
5176 STOP_PC, is presently locking the fast tracepoint collection, and
5177 if so, gather some status of said collection. Returns 0 if the
5178 thread isn't collecting or in the jump pad at all. 1, if in the
5179 jump pad (or within gdb_collect) and hasn't executed the adjusted
5180 original insn yet (can set a breakpoint there and run to it). 2,
5181 if presently executing the adjusted original insn --- in which
5182 case, if we want to move the thread out of the jump pad, we need to
5183 single-step it until this function returns 0. */
5186 fast_tracepoint_collecting (CORE_ADDR thread_area,
5188 struct fast_tpoint_collect_status *status)
5190 CORE_ADDR ipa_collecting;
5191 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
5192 CORE_ADDR ipa_gdb_trampoline_buffer;
5193 CORE_ADDR ipa_gdb_trampoline_buffer_end;
5194 struct tracepoint *tpoint;
5195 int needs_breakpoint;
5197 /* The thread THREAD_AREA is either:
5199 0. not collecting at all, not within the jump pad, or within
5200 gdb_collect or one of its callees.
5202 1. in the jump pad and haven't reached gdb_collect
5204 2. within gdb_collect (out of the jump pad) (collect is set)
5206 3. we're in the jump pad, after gdb_collect having returned,
5207 possibly executing the adjusted insns.
5209 For cases 1 and 3, `collecting' may or not be set. The jump pad
5210 doesn't have any complicated jump logic, so we can tell if the
5211 thread is executing the adjust original insn or not by just
5212 matching STOP_PC with known jump pad addresses. If we it isn't
5213 yet executing the original insn, set a breakpoint there, and let
5214 the thread run to it, so to quickly step over a possible (many
5215 insns) gdb_collect call. Otherwise, or when the breakpoint is
5216 hit, only a few (small number of) insns are left to be executed
5217 in the jump pad. Single-step the thread until it leaves the
5222 needs_breakpoint = 0;
5223 trace_debug ("fast_tracepoint_collecting");
5225 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5226 &ipa_gdb_jump_pad_buffer))
5227 fatal ("error extracting `gdb_jump_pad_buffer'");
5228 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5229 &ipa_gdb_jump_pad_buffer_end))
5230 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5232 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5233 &ipa_gdb_trampoline_buffer))
5234 fatal ("error extracting `gdb_trampoline_buffer'");
5235 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5236 &ipa_gdb_trampoline_buffer_end))
5237 fatal ("error extracting `gdb_trampoline_buffer_end'");
5239 if (ipa_gdb_jump_pad_buffer <= stop_pc
5240 && stop_pc < ipa_gdb_jump_pad_buffer_end)
5242 /* We can tell which tracepoint(s) the thread is collecting by
5243 matching the jump pad address back to the tracepoint. */
5244 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5247 warning ("in jump pad, but no matching tpoint?");
5252 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5254 tpoint->number, paddress (tpoint->address),
5255 paddress (tpoint->jump_pad),
5256 paddress (tpoint->jump_pad_end),
5257 paddress (tpoint->adjusted_insn_addr),
5258 paddress (tpoint->adjusted_insn_addr_end));
5261 /* Definitely in the jump pad. May or may not need
5262 fast-exit-jump-pad breakpoint. */
5263 if (tpoint->jump_pad <= stop_pc
5264 && stop_pc < tpoint->adjusted_insn_addr)
5265 needs_breakpoint = 1;
5267 else if (ipa_gdb_trampoline_buffer <= stop_pc
5268 && stop_pc < ipa_gdb_trampoline_buffer_end)
5270 /* We can tell which tracepoint(s) the thread is collecting by
5271 matching the trampoline address back to the tracepoint. */
5272 tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5275 warning ("in trampoline, but no matching tpoint?");
5280 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5281 tpoint->number, paddress (tpoint->address),
5282 paddress (tpoint->trampoline),
5283 paddress (tpoint->trampoline_end));
5286 /* Have not reached jump pad yet, but treat the trampoline as a
5287 part of the jump pad that is before the adjusted original
5289 needs_breakpoint = 1;
5293 collecting_t ipa_collecting_obj;
5295 /* If `collecting' is set/locked, then the THREAD_AREA thread
5296 may or not be the one holding the lock. We have to read the
5297 lock to find out. */
5299 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5302 trace_debug ("fast_tracepoint_collecting:"
5303 " failed reading 'collecting' in the inferior");
5307 if (!ipa_collecting)
5309 trace_debug ("fast_tracepoint_collecting: not collecting"
5310 " (and nobody is).");
5314 /* Some thread is collecting. Check which. */
5315 if (read_inferior_memory (ipa_collecting,
5316 (unsigned char *) &ipa_collecting_obj,
5317 sizeof (ipa_collecting_obj)) != 0)
5320 if (ipa_collecting_obj.thread_area != thread_area)
5322 trace_debug ("fast_tracepoint_collecting: not collecting "
5323 "(another thread is)");
5328 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5331 warning ("fast_tracepoint_collecting: collecting, "
5332 "but tpoint %s not found?",
5333 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5337 /* The thread is within `gdb_collect', skip over the rest of
5338 fast tracepoint collection quickly using a breakpoint. */
5339 needs_breakpoint = 1;
5342 /* The caller wants a bit of status detail. */
5345 status->tpoint_num = tpoint->number;
5346 status->tpoint_addr = tpoint->address;
5347 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5348 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5351 if (needs_breakpoint)
5353 /* Hasn't executed the original instruction yet. Set breakpoint
5354 there, and wait till it's hit, then single-step until exiting
5358 fast_tracepoint_collecting, returning continue-until-break at %s",
5359 paddress (tpoint->adjusted_insn_addr));
5361 return 1; /* continue */
5365 /* Just single-step until exiting the jump pad. */
5367 trace_debug ("fast_tracepoint_collecting, returning "
5368 "need-single-step (%s-%s)",
5369 paddress (tpoint->adjusted_insn_addr),
5370 paddress (tpoint->adjusted_insn_addr_end));
5372 return 2; /* single-step */
5378 #ifdef IN_PROCESS_AGENT
5380 /* The global fast tracepoint collect lock. Points to a collecting_t
5381 object built on the stack by the jump pad, if presently locked;
5382 NULL if it isn't locked. Note that this lock *must* be set while
5383 executing any *function other than the jump pad. See
5384 fast_tracepoint_collecting. */
5385 static collecting_t * ATTR_USED collecting;
5387 /* This routine, called from the jump pad (in asm) is designed to be
5388 called from the jump pads of fast tracepoints, thus it is on the
5391 IP_AGENT_EXPORT void ATTR_USED
5392 gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5394 struct fast_tracepoint_ctx ctx;
5396 /* Don't do anything until the trace run is completely set up. */
5400 ctx.base.type = fast_tracepoint;
5402 ctx.regcache_initted = 0;
5403 /* Wrap the regblock in a register cache (in the stack, we don't
5404 want to malloc here). */
5405 ctx.regspace = alloca (register_cache_size ());
5406 if (ctx.regspace == NULL)
5408 trace_debug ("Trace buffer block allocation failed, skipping");
5412 for (ctx.tpoint = tpoint;
5413 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5414 ctx.tpoint = ctx.tpoint->next)
5416 if (!ctx.tpoint->enabled)
5419 /* Multiple tracepoints of different types, such as fast tracepoint and
5420 static tracepoint, can be set at the same address. */
5421 if (ctx.tpoint->type != tpoint->type)
5424 /* Test the condition if present, and collect if true. */
5425 if (ctx.tpoint->cond == NULL
5426 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5429 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5430 ctx.tpoint->address, ctx.tpoint);
5432 /* Note that this will cause original insns to be written back
5433 to where we jumped from, but that's OK because we're jumping
5434 back to the next whole instruction. This will go badly if
5435 instruction restoration is not atomic though. */
5436 if (stopping_tracepoint
5437 || trace_buffer_is_full
5438 || expr_eval_result != expr_eval_no_error)
5446 /* If there was a condition and it evaluated to false, the only
5447 way we would stop tracing is if there was an error during
5448 condition expression evaluation. */
5449 if (expr_eval_result != expr_eval_no_error)
5460 #ifndef IN_PROCESS_AGENT
5463 get_raw_reg_func_addr (void)
5465 return ipa_sym_addrs.addr_get_raw_reg;
5469 get_get_tsv_func_addr (void)
5471 return ipa_sym_addrs.addr_get_trace_state_variable_value;
5475 get_set_tsv_func_addr (void)
5477 return ipa_sym_addrs.addr_set_trace_state_variable_value;
5481 compile_tracepoint_condition (struct tracepoint *tpoint,
5482 CORE_ADDR *jump_entry)
5484 CORE_ADDR entry_point = *jump_entry;
5485 enum eval_result_type err;
5487 trace_debug ("Starting condition compilation for tracepoint %d\n",
5490 /* Initialize the global pointer to the code being built. */
5491 current_insn_ptr = *jump_entry;
5495 err = compile_bytecodes (tpoint->cond);
5497 if (err == expr_eval_no_error)
5501 /* Record the beginning of the compiled code. */
5502 tpoint->compiled_cond = entry_point;
5504 trace_debug ("Condition compilation for tracepoint %d complete\n",
5509 /* Leave the unfinished code in situ, but don't point to it. */
5511 tpoint->compiled_cond = 0;
5513 trace_debug ("Condition compilation for tracepoint %d failed, "
5515 tpoint->number, err);
5518 /* Update the code pointer passed in. Note that we do this even if
5519 the compile fails, so that we can look at the partial results
5520 instead of letting them be overwritten. */
5521 *jump_entry = current_insn_ptr;
5523 /* Leave a gap, to aid dump decipherment. */
5527 /* We'll need to adjust these when we consider bi-arch setups, and big
5531 write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
5533 return write_inferior_memory (where,
5534 (unsigned char *) &ptr, sizeof (void *));
5537 /* The base pointer of the IPA's heap. This is the only memory the
5538 IPA is allowed to use. The IPA should _not_ call the inferior's
5539 `malloc' during operation. That'd be slow, and, most importantly,
5540 it may not be safe. We may be collecting a tracepoint in a signal
5541 handler, for example. */
5542 static CORE_ADDR target_tp_heap;
5544 /* Allocate at least SIZE bytes of memory from the IPA heap, aligned
5548 target_malloc (ULONGEST size)
5552 if (target_tp_heap == 0)
5554 /* We have the pointer *address*, need what it points to. */
5555 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
5557 fatal ("could get target heap head pointer");
5560 ptr = target_tp_heap;
5561 target_tp_heap += size;
5563 /* Pad to 8-byte alignment. */
5564 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
5570 download_agent_expr (struct agent_expr *expr)
5572 CORE_ADDR expr_addr;
5573 CORE_ADDR expr_bytes;
5575 expr_addr = target_malloc (sizeof (*expr));
5576 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
5578 expr_bytes = target_malloc (expr->length);
5579 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
5581 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
5586 /* Align V up to N bits. */
5587 #define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
5589 /* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
5592 download_tracepoint_1 (struct tracepoint *tpoint)
5594 struct tracepoint target_tracepoint;
5595 CORE_ADDR tpptr = 0;
5597 gdb_assert (tpoint->type == fast_tracepoint
5598 || tpoint->type == static_tracepoint);
5600 if (tpoint->cond != NULL && target_emit_ops () != NULL)
5602 CORE_ADDR jentry, jump_entry;
5604 jentry = jump_entry = get_jump_space_head ();
5606 if (tpoint->cond != NULL)
5608 /* Pad to 8-byte alignment. (needed?) */
5609 /* Actually this should be left for the target to
5611 jentry = UALIGN (jentry, 8);
5613 compile_tracepoint_condition (tpoint, &jentry);
5616 /* Pad to 8-byte alignment. */
5617 jentry = UALIGN (jentry, 8);
5618 claim_jump_space (jentry - jump_entry);
5621 target_tracepoint = *tpoint;
5623 tpptr = target_malloc (sizeof (*tpoint));
5624 tpoint->obj_addr_on_target = tpptr;
5626 /* Write the whole object. We'll fix up its pointers in a bit.
5627 Assume no next for now. This is fixed up above on the next
5628 iteration, if there's any. */
5629 target_tracepoint.next = NULL;
5630 /* Need to clear this here too, since we're downloading the
5631 tracepoints before clearing our own copy. */
5632 target_tracepoint.hit_count = 0;
5634 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
5635 sizeof (target_tracepoint));
5638 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5640 download_agent_expr (tpoint->cond));
5642 if (tpoint->numactions)
5645 CORE_ADDR actions_array;
5647 /* The pointers array. */
5649 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
5650 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
5654 /* Now for each pointer, download the action. */
5655 for (i = 0; i < tpoint->numactions; i++)
5657 CORE_ADDR ipa_action = 0;
5658 struct tracepoint_action *action = tpoint->actions[i];
5660 switch (action->type)
5664 = target_malloc (sizeof (struct collect_memory_action));
5665 write_inferior_memory (ipa_action,
5666 (unsigned char *) action,
5667 sizeof (struct collect_memory_action));
5671 = target_malloc (sizeof (struct collect_registers_action));
5672 write_inferior_memory (ipa_action,
5673 (unsigned char *) action,
5674 sizeof (struct collect_registers_action));
5679 struct eval_expr_action *eaction
5680 = (struct eval_expr_action *) action;
5682 ipa_action = target_malloc (sizeof (*eaction));
5683 write_inferior_memory (ipa_action,
5684 (unsigned char *) eaction,
5687 expr = download_agent_expr (eaction->expr);
5688 write_inferior_data_ptr
5689 (ipa_action + offsetof (struct eval_expr_action, expr),
5694 ipa_action = target_malloc
5695 (sizeof (struct collect_static_trace_data_action));
5696 write_inferior_memory
5698 (unsigned char *) action,
5699 sizeof (struct collect_static_trace_data_action));
5702 trace_debug ("unknown trace action '%c', ignoring",
5707 if (ipa_action != 0)
5708 write_inferior_data_ptr
5709 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
5716 download_tracepoint (struct tracepoint *tpoint)
5718 struct tracepoint *tp, *tp_prev;
5720 if (tpoint->type != fast_tracepoint
5721 && tpoint->type != static_tracepoint)
5724 download_tracepoint_1 (tpoint);
5726 /* Find the previous entry of TPOINT, which is fast tracepoint or
5727 static tracepoint. */
5729 for (tp = tracepoints; tp != tpoint; tp = tp->next)
5731 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
5737 CORE_ADDR tp_prev_target_next_addr;
5739 /* Insert TPOINT after TP_PREV in IPA. */
5740 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
5741 + offsetof (struct tracepoint, next),
5742 &tp_prev_target_next_addr))
5743 fatal ("error reading `tp_prev->next'");
5745 /* tpoint->next = tp_prev->next */
5746 write_inferior_data_ptr (tpoint->obj_addr_on_target
5747 + offsetof (struct tracepoint, next),
5748 tp_prev_target_next_addr);
5749 /* tp_prev->next = tpoint */
5750 write_inferior_data_ptr (tp_prev->obj_addr_on_target
5751 + offsetof (struct tracepoint, next),
5752 tpoint->obj_addr_on_target);
5755 /* First object in list, set the head pointer in the
5757 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
5758 tpoint->obj_addr_on_target);
5763 download_tracepoints (void)
5765 CORE_ADDR tpptr = 0, prev_tpptr = 0;
5766 struct tracepoint *tpoint;
5768 /* Start out empty. */
5769 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
5771 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5773 if (tpoint->type != fast_tracepoint
5774 && tpoint->type != static_tracepoint)
5779 download_tracepoint_1 (tpoint);
5781 tpptr = tpoint->obj_addr_on_target;
5783 if (tpoint == tracepoints)
5785 /* First object in list, set the head pointer in the
5787 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
5791 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
5799 download_trace_state_variables (void)
5801 CORE_ADDR ptr = 0, prev_ptr = 0;
5802 struct trace_state_variable *tsv;
5804 /* Start out empty. */
5805 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
5807 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
5809 struct trace_state_variable target_tsv;
5811 /* TSV's with a getter have been initialized equally in both the
5812 inferior and GDBserver. Skip them. */
5813 if (tsv->getter != NULL)
5819 ptr = target_malloc (sizeof (*tsv));
5821 if (tsv == trace_state_variables)
5823 /* First object in list, set the head pointer in the
5826 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
5831 write_inferior_data_ptr (prev_ptr
5832 + offsetof (struct trace_state_variable,
5837 /* Write the whole object. We'll fix up its pointers in a bit.
5838 Assume no next, fixup when needed. */
5839 target_tsv.next = NULL;
5841 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
5842 sizeof (target_tsv));
5844 if (tsv->name != NULL)
5846 size_t size = strlen (tsv->name) + 1;
5847 CORE_ADDR name_addr = target_malloc (size);
5848 write_inferior_memory (name_addr,
5849 (unsigned char *) tsv->name, size);
5850 write_inferior_data_ptr (ptr
5851 + offsetof (struct trace_state_variable,
5856 if (tsv->getter != NULL)
5858 fatal ("what to do with these?");
5864 /* Fixup the next pointer in the last item in the list. */
5865 write_inferior_data_ptr (prev_ptr
5866 + offsetof (struct trace_state_variable,
5871 /* Upload complete trace frames out of the IP Agent's trace buffer
5872 into GDBserver's trace buffer. This always uploads either all or
5873 no trace frames. This is the counter part of
5874 `trace_alloc_trace_buffer'. See its description of the atomic
5875 synching mechanism. */
5878 upload_fast_traceframes (void)
5880 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
5881 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
5883 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
5884 unsigned int curr_tbctrl_idx;
5885 unsigned int ipa_trace_buffer_ctrl_curr;
5886 unsigned int ipa_trace_buffer_ctrl_curr_old;
5887 CORE_ADDR ipa_trace_buffer_ctrl_addr;
5888 struct breakpoint *about_to_request_buffer_space_bkpt;
5889 CORE_ADDR ipa_trace_buffer_lo;
5890 CORE_ADDR ipa_trace_buffer_hi;
5892 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5893 &ipa_traceframe_read_count_racy))
5895 /* This will happen in most targets if the current thread is
5900 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5901 &ipa_traceframe_write_count_racy))
5904 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
5905 ipa_traceframe_write_count_racy
5906 - ipa_traceframe_read_count_racy,
5907 ipa_traceframe_write_count_racy,
5908 ipa_traceframe_read_count_racy);
5910 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
5913 about_to_request_buffer_space_bkpt
5914 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
5917 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5918 &ipa_trace_buffer_ctrl_curr))
5921 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
5923 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
5926 unsigned int prev, counter;
5928 /* Update the token, with new counters, and the GDBserver stamp
5929 bit. Alway reuse the current TBC index. */
5930 prev = ipa_trace_buffer_ctrl_curr & GDBSERVER_FLUSH_COUNT_MASK_CURR;
5931 counter = (prev + 0x100) & GDBSERVER_FLUSH_COUNT_MASK_CURR;
5933 ipa_trace_buffer_ctrl_curr = (GDBSERVER_UPDATED_FLUSH_COUNT_BIT
5939 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
5940 ipa_trace_buffer_ctrl_curr))
5943 trace_debug ("Lib: Committed %08x -> %08x",
5944 ipa_trace_buffer_ctrl_curr_old,
5945 ipa_trace_buffer_ctrl_curr);
5947 /* Re-read these, now that we've installed the
5948 `about_to_request_buffer_space' breakpoint/lock. A thread could
5949 have finished a traceframe between the last read of these
5950 counters and setting the breakpoint above. If we start
5951 uploading, we never want to leave this function with
5952 traceframe_read_count != 0, otherwise, GDBserver could end up
5953 incrementing the counter tokens more than once (due to event loop
5954 nesting), which would break the IP agent's "effective" detection
5955 (see trace_alloc_trace_buffer). */
5956 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
5957 &ipa_traceframe_read_count))
5959 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
5960 &ipa_traceframe_write_count))
5965 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
5966 ipa_traceframe_write_count - ipa_traceframe_read_count,
5967 ipa_traceframe_write_count, ipa_traceframe_read_count);
5969 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
5970 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
5971 trace_debug ("note that ipa_traceframe_count's parts changed");
5974 /* Get the address of the current TBC object (the IP agent has an
5975 array of 3 such objects). The index is stored in the TBC
5977 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
5978 ipa_trace_buffer_ctrl_addr
5979 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
5981 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
5982 (unsigned char *) &ipa_trace_buffer_ctrl,
5983 sizeof (struct ipa_trace_buffer_control)))
5986 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
5987 &ipa_trace_buffer_lo))
5989 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
5990 &ipa_trace_buffer_hi))
5993 /* Offsets are easier to grok for debugging than raw addresses,
5994 especially for the small trace buffer sizes that are useful for
5996 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
5997 "endfree=%d wrap=%d hi=%d",
5999 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6000 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6001 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6002 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6003 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6005 /* Note that the IPA's buffer is always circular. */
6007 #define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6009 #define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6010 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6012 #define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6013 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6014 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6015 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6018 tf = IPA_FIRST_TRACEFRAME ();
6020 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6022 struct tracepoint *tpoint;
6023 struct traceframe *tframe;
6024 unsigned char *block;
6025 struct traceframe ipa_tframe;
6027 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6028 offsetof (struct traceframe, data)))
6029 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6031 if (ipa_tframe.tpnum == 0)
6032 fatal ("Uploading: No (more) fast traceframes, but "
6033 "ipa_traceframe_count == %u??\n",
6034 ipa_traceframe_write_count - ipa_traceframe_read_count);
6036 /* Note that this will be incorrect for multi-location
6038 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6040 tframe = add_traceframe (tpoint);
6043 trace_buffer_is_full = 1;
6044 trace_debug ("Uploading: trace buffer is full");
6048 /* Copy the whole set of blocks in one go for now. FIXME:
6049 split this in smaller blocks. */
6050 block = add_traceframe_block (tframe, ipa_tframe.data_size);
6053 if (read_inferior_memory (tf
6054 + offsetof (struct traceframe, data),
6055 block, ipa_tframe.data_size))
6056 error ("Uploading: Couldn't read traceframe data at %s\n",
6057 paddress (tf + offsetof (struct traceframe, data)));
6060 trace_debug ("Uploading: traceframe didn't fit");
6061 finish_traceframe (tframe);
6064 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6066 /* If we freed the traceframe that wrapped around, go back
6067 to the non-wrap case. */
6068 if (tf < ipa_trace_buffer_ctrl.start)
6070 trace_debug ("Lib: Discarding past the wraparound");
6071 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6073 ipa_trace_buffer_ctrl.start = tf;
6074 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6075 ++ipa_traceframe_read_count;
6077 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6078 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6080 trace_debug ("Lib: buffer is fully empty. "
6081 "Trace buffer [%d] start=%d free=%d endfree=%d",
6083 (int) (ipa_trace_buffer_ctrl.start
6084 - ipa_trace_buffer_lo),
6085 (int) (ipa_trace_buffer_ctrl.free
6086 - ipa_trace_buffer_lo),
6087 (int) (ipa_trace_buffer_ctrl.end_free
6088 - ipa_trace_buffer_lo));
6090 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6091 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6092 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6093 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6096 trace_debug ("Uploaded a traceframe\n"
6097 "Lib: Trace buffer [%d] start=%d free=%d "
6098 "endfree=%d wrap=%d hi=%d",
6100 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6101 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6102 (int) (ipa_trace_buffer_ctrl.end_free
6103 - ipa_trace_buffer_lo),
6104 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6105 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6108 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6109 (unsigned char *) &ipa_trace_buffer_ctrl,
6110 sizeof (struct ipa_trace_buffer_control)))
6113 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6114 ipa_traceframe_read_count);
6116 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6119 cancel_breakpoints ();
6121 delete_breakpoint (about_to_request_buffer_space_bkpt);
6122 about_to_request_buffer_space_bkpt = NULL;
6126 if (trace_buffer_is_full)
6131 #ifdef IN_PROCESS_AGENT
6133 IP_AGENT_EXPORT int ust_loaded;
6134 IP_AGENT_EXPORT char cmd_buf[IPA_CMD_BUF_SIZE];
6138 /* Static tracepoints. */
6140 /* UST puts a "struct tracepoint" in the global namespace, which
6141 conflicts with our tracepoint. Arguably, being a library, it
6142 shouldn't take ownership of such a generic name. We work around it
6144 #define tracepoint ust_tracepoint
6145 #include <ust/ust.h>
6148 extern int serialize_to_text (char *outbuf, int bufsize,
6149 const char *fmt, va_list ap);
6151 #define GDB_PROBE_NAME "gdb"
6153 /* We dynamically search for the UST symbols instead of linking them
6154 in. This lets the user decide if the application uses static
6155 tracepoints, instead of always pulling libust.so in. This vector
6156 holds pointers to all functions we care about. */
6160 int (*serialize_to_text) (char *outbuf, int bufsize,
6161 const char *fmt, va_list ap);
6163 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6164 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6166 int (*ltt_marker_connect) (const char *channel, const char *mname,
6168 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6171 void (*marker_iter_start) (struct marker_iter *iter);
6172 void (*marker_iter_next) (struct marker_iter *iter);
6173 void (*marker_iter_stop) (struct marker_iter *iter);
6174 void (*marker_iter_reset) (struct marker_iter *iter);
6179 /* Cast through typeof to catch incompatible API changes. Since UST
6180 only builds with gcc, we can freely use gcc extensions here
6182 #define GET_UST_SYM(SYM) \
6185 if (ust_ops.SYM == NULL) \
6186 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6187 if (ust_ops.SYM == NULL) \
6191 #define USTF(SYM) ust_ops.SYM
6193 /* Get pointers to all libust.so functions we care about. */
6198 GET_UST_SYM (serialize_to_text);
6200 GET_UST_SYM (ltt_probe_register);
6201 GET_UST_SYM (ltt_probe_unregister);
6202 GET_UST_SYM (ltt_marker_connect);
6203 GET_UST_SYM (ltt_marker_disconnect);
6205 GET_UST_SYM (marker_iter_start);
6206 GET_UST_SYM (marker_iter_next);
6207 GET_UST_SYM (marker_iter_stop);
6208 GET_UST_SYM (marker_iter_reset);
6214 /* Given an UST marker, return the matching gdb static tracepoint.
6215 The match is done by address. */
6217 static struct tracepoint *
6218 ust_marker_to_static_tracepoint (const struct marker *mdata)
6220 struct tracepoint *tpoint;
6222 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6224 if (tpoint->type != static_tracepoint)
6227 if (tpoint->address == (uintptr_t) mdata->location)
6234 /* The probe function we install on lttng/ust markers. Whenever a
6235 probed ust marker is hit, this function is called. This is similar
6236 to gdb_collect, only for static tracepoints, instead of fast
6240 gdb_probe (const struct marker *mdata, void *probe_private,
6241 struct registers *regs, void *call_private,
6242 const char *fmt, va_list *args)
6244 struct tracepoint *tpoint;
6245 struct static_tracepoint_ctx ctx;
6247 /* Don't do anything until the trace run is completely set up. */
6250 trace_debug ("gdb_probe: not tracing\n");
6254 ctx.base.type = static_tracepoint;
6255 ctx.regcache_initted = 0;
6260 /* Wrap the regblock in a register cache (in the stack, we don't
6261 want to malloc here). */
6262 ctx.regspace = alloca (register_cache_size ());
6263 if (ctx.regspace == NULL)
6265 trace_debug ("Trace buffer block allocation failed, skipping");
6269 tpoint = ust_marker_to_static_tracepoint (mdata);
6272 trace_debug ("gdb_probe: marker not known: "
6273 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6274 mdata->location, mdata->channel,
6275 mdata->name, mdata->format);
6279 if (!tpoint->enabled)
6281 trace_debug ("gdb_probe: tracepoint disabled");
6285 ctx.tpoint = tpoint;
6287 trace_debug ("gdb_probe: collecting marker: "
6288 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6289 mdata->location, mdata->channel,
6290 mdata->name, mdata->format);
6292 /* Test the condition if present, and collect if true. */
6293 if (tpoint->cond == NULL
6294 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6297 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6298 tpoint->address, tpoint);
6300 if (stopping_tracepoint
6301 || trace_buffer_is_full
6302 || expr_eval_result != expr_eval_no_error)
6307 /* If there was a condition and it evaluated to false, the only
6308 way we would stop tracing is if there was an error during
6309 condition expression evaluation. */
6310 if (expr_eval_result != expr_eval_no_error)
6315 /* Called if the gdb static tracepoint requested collecting "$_sdata",
6316 static tracepoint string data. This is a string passed to the
6317 tracing library by the user, at the time of the tracepoint marker
6318 call. E.g., in the UST marker call:
6320 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6322 the collected data is "str FOOBAZ".
6326 collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
6327 struct traceframe *tframe)
6329 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
6330 unsigned char *bufspace;
6333 unsigned short blocklen;
6337 trace_debug ("Wanted to collect static trace data, "
6338 "but there's no static trace data");
6342 va_copy (copy, *umd->args);
6343 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
6346 trace_debug ("Want to collect ust data");
6348 /* 'S' + size + string */
6349 bufspace = add_traceframe_block (tframe,
6350 1 + sizeof (blocklen) + size + 1);
6351 if (bufspace == NULL)
6353 trace_debug ("Trace buffer block allocation failed, skipping");
6357 /* Identify a static trace data block. */
6360 blocklen = size + 1;
6361 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
6363 va_copy (copy, *umd->args);
6364 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
6365 size + 1, umd->fmt, copy);
6368 trace_debug ("Storing static tracepoint data in regblock: %s",
6369 bufspace + 1 + sizeof (blocklen));
6372 /* The probe to register with lttng/ust. */
6373 static struct ltt_available_probe gdb_ust_probe =
6380 #endif /* HAVE_UST */
6381 #endif /* IN_PROCESS_AGENT */
6383 #ifndef IN_PROCESS_AGENT
6385 /* Ask the in-process agent to run a command. Since we don't want to
6386 have to handle the IPA hitting breakpoints while running the
6387 command, we pause all threads, remove all breakpoints, and then set
6388 the helper thread re-running. We communicate with the helper
6389 thread by means of direct memory xfering, and a socket for
6393 run_inferior_command (char *cmd)
6396 int pid = ptid_get_pid (current_inferior->entry.id);
6398 trace_debug ("run_inferior_command: running: %s", cmd);
6401 uninsert_all_breakpoints ();
6403 err = agent_run_command (pid, (const char *) cmd);
6405 reinsert_all_breakpoints ();
6411 #else /* !IN_PROCESS_AGENT */
6413 #include <sys/socket.h>
6416 #ifndef UNIX_PATH_MAX
6417 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6420 /* Where we put the socked used for synchronization. */
6421 #define SOCK_DIR P_tmpdir
6423 /* Thread ID of the helper thread. GDBserver reads this to know which
6424 is the help thread. This is an LWP id on Linux. */
6425 int helper_thread_id;
6428 init_named_socket (const char *name)
6431 struct sockaddr_un addr;
6433 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
6436 warning ("socket creation failed: %s", strerror (errno));
6440 addr.sun_family = AF_UNIX;
6442 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
6443 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
6445 result = access (name, F_OK);
6449 result = unlink (name);
6452 warning ("unlink failed: %s", strerror (errno));
6456 warning ("socket %s already exists; overwriting", name);
6459 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
6462 warning ("bind failed: %s", strerror (errno));
6467 result = listen (fd, 1);
6470 warning ("listen: %s", strerror (errno));
6479 gdb_agent_socket_init (void)
6482 char name[UNIX_PATH_MAX];
6484 result = xsnprintf (name, UNIX_PATH_MAX, "%s/gdb_ust%d",
6485 SOCK_DIR, getpid ());
6486 if (result >= UNIX_PATH_MAX)
6488 trace_debug ("string overflow allocating socket name");
6492 fd = init_named_socket (name);
6494 warning ("Error initializing named socket (%s) for communication with the "
6495 "ust helper thread. Check that directory exists and that it "
6496 "is writable.", name);
6503 /* The next marker to be returned on a qTsSTM command. */
6504 static const struct marker *next_st;
6506 /* Returns the first known marker. */
6511 struct marker_iter iter;
6513 USTF(marker_iter_reset) (&iter);
6514 USTF(marker_iter_start) (&iter);
6519 /* Returns the marker following M. */
6521 const struct marker *
6522 next_marker (const struct marker *m)
6524 struct marker_iter iter;
6526 USTF(marker_iter_reset) (&iter);
6527 USTF(marker_iter_start) (&iter);
6529 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6531 if (iter.marker == m)
6533 USTF(marker_iter_next) (&iter);
6541 /* Return an hexstr version of the STR C string, fit for sending to
6545 cstr_to_hexstr (const char *str)
6547 int len = strlen (str);
6548 char *hexstr = xmalloc (len * 2 + 1);
6549 convert_int_to_ascii ((gdb_byte *) str, hexstr, len);
6553 /* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
6557 response_ust_marker (char *packet, const struct marker *st)
6559 char *strid, *format, *tmp;
6561 next_st = next_marker (st);
6563 tmp = xmalloc (strlen (st->channel) + 1 +
6564 strlen (st->name) + 1);
6565 sprintf (tmp, "%s/%s", st->channel, st->name);
6567 strid = cstr_to_hexstr (tmp);
6570 format = cstr_to_hexstr (st->format);
6572 sprintf (packet, "m%s:%s:%s",
6573 paddress ((uintptr_t) st->location),
6581 /* Return the first static tracepoint, and initialize the state
6582 machine that will iterate through all the static tracepoints. */
6585 cmd_qtfstm (char *packet)
6587 trace_debug ("Returning first trace state variable definition");
6589 if (first_marker ())
6590 response_ust_marker (packet, first_marker ());
6592 strcpy (packet, "l");
6595 /* Return additional trace state variable definitions. */
6598 cmd_qtsstm (char *packet)
6600 trace_debug ("Returning static tracepoint");
6603 response_ust_marker (packet, next_st);
6605 strcpy (packet, "l");
6608 /* Disconnect the GDB probe from a marker at a given address. */
6611 unprobe_marker_at (char *packet)
6615 struct marker_iter iter;
6617 p += sizeof ("unprobe_marker_at:") - 1;
6619 p = unpack_varlen_hex (p, &address);
6621 USTF(marker_iter_reset) (&iter);
6622 USTF(marker_iter_start) (&iter);
6623 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
6624 if ((uintptr_t ) iter.marker->location == address)
6628 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
6632 warning ("could not disable marker %s/%s",
6633 iter.marker->channel, iter.marker->name);
6638 /* Connect the GDB probe to a marker at a given address. */
6641 probe_marker_at (char *packet)
6645 struct marker_iter iter;
6648 p += sizeof ("probe_marker_at:") - 1;
6650 p = unpack_varlen_hex (p, &address);
6652 USTF(marker_iter_reset) (&iter);
6654 for (USTF(marker_iter_start) (&iter), m = iter.marker;
6656 USTF(marker_iter_next) (&iter), m = iter.marker)
6657 if ((uintptr_t ) m->location == address)
6661 trace_debug ("found marker for address. "
6662 "ltt_marker_connect (marker = %s/%s)",
6663 m->channel, m->name);
6665 result = USTF(ltt_marker_connect) (m->channel, m->name,
6667 if (result && result != -EEXIST)
6668 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
6669 m->channel, m->name, -result);
6673 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
6674 m->channel, m->name);
6678 strcpy (packet, "OK");
6682 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
6687 cmd_qtstmat (char *packet)
6691 struct marker_iter iter;
6694 p += sizeof ("qTSTMat:") - 1;
6696 p = unpack_varlen_hex (p, &address);
6698 USTF(marker_iter_reset) (&iter);
6700 for (USTF(marker_iter_start) (&iter), m = iter.marker;
6702 USTF(marker_iter_next) (&iter), m = iter.marker)
6703 if ((uintptr_t ) m->location == address)
6705 response_ust_marker (packet, m);
6709 strcpy (packet, "l");
6719 USTF(ltt_probe_register) (&gdb_ust_probe);
6722 #endif /* HAVE_UST */
6724 #include <sys/syscall.h>
6726 /* Helper thread of agent. */
6729 gdb_agent_helper_thread (void *arg)
6735 listen_fd = gdb_agent_socket_init ();
6737 if (helper_thread_id == 0)
6738 helper_thread_id = syscall (SYS_gettid);
6740 if (listen_fd == -1)
6742 warning ("could not create sync socket\n");
6749 struct sockaddr_un sockaddr;
6754 tmp = sizeof (sockaddr);
6758 fd = accept (listen_fd, &sockaddr, &tmp);
6760 /* It seems an ERESTARTSYS can escape out of accept. */
6761 while (fd == -512 || (fd == -1 && errno == EINTR));
6765 warning ("Accept returned %d, error: %s\n",
6766 fd, strerror (errno));
6772 ret = read (fd, buf, 1);
6773 } while (ret == -1 && errno == EINTR);
6777 warning ("reading socket (fd=%d) failed with %s",
6778 fd, strerror (errno));
6786 if (strcmp ("qTfSTM", cmd_buf) == 0)
6788 cmd_qtfstm (cmd_buf);
6790 else if (strcmp ("qTsSTM", cmd_buf) == 0)
6792 cmd_qtsstm (cmd_buf);
6794 else if (strncmp ("unprobe_marker_at:",
6796 sizeof ("unprobe_marker_at:") - 1) == 0)
6798 unprobe_marker_at (cmd_buf);
6800 else if (strncmp ("probe_marker_at:",
6802 sizeof ("probe_marker_at:") - 1) == 0)
6804 probe_marker_at (cmd_buf);
6806 else if (strncmp ("qTSTMat:",
6808 sizeof ("qTSTMat:") - 1) == 0)
6810 cmd_qtstmat (cmd_buf);
6812 #endif /* HAVE_UST */
6815 /* Fix compiler's warning: ignoring return value of 'write'. */
6816 ret = write (fd, buf, 1);
6825 #include <pthread.h>
6827 IP_AGENT_EXPORT int gdb_agent_capability = AGENT_CAPA_STATIC_TRACE;
6830 gdb_agent_init (void)
6837 /* We want the helper thread to be as transparent as possible, so
6838 have it inherit an all-signals-blocked mask. */
6840 sigfillset (&new_mask);
6841 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
6843 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
6845 res = pthread_create (&thread,
6847 gdb_agent_helper_thread,
6850 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
6852 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
6854 while (helper_thread_id == 0)
6862 #include <sys/mman.h>
6865 IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
6866 IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
6867 IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
6868 IP_AGENT_EXPORT char *gdb_trampoline_buffer;
6869 IP_AGENT_EXPORT char *gdb_trampoline_buffer_end;
6870 IP_AGENT_EXPORT char *gdb_trampoline_buffer_error;
6872 /* Record the result of getting buffer space for fast tracepoint
6873 trampolines. Any error message is copied, since caller may not be
6874 using persistent storage. */
6877 set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
6879 gdb_trampoline_buffer = (char *) (uintptr_t) begin;
6880 gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
6882 strncpy (gdb_trampoline_buffer_error, errmsg, 99);
6884 strcpy (gdb_trampoline_buffer_error, "no buffer passed");
6887 static void __attribute__ ((constructor))
6888 initialize_tracepoint_ftlib (void)
6890 initialize_tracepoint ();
6895 #endif /* IN_PROCESS_AGENT */
6897 /* Return a timestamp, expressed as microseconds of the usual Unix
6898 time. (As the result is a 64-bit number, it will not overflow any
6902 get_timestamp (void)
6906 if (gettimeofday (&tv, 0) != 0)
6909 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
6913 initialize_tracepoint (void)
6915 /* There currently no way to change the buffer size. */
6916 const int sizeOfBuffer = 5 * 1024 * 1024;
6917 unsigned char *buf = xmalloc (sizeOfBuffer);
6918 init_trace_buffer (buf, sizeOfBuffer);
6920 /* Wire trace state variable 1 to be the timestamp. This will be
6921 uploaded to GDB upon connection and become one of its trace state
6922 variables. (In case you're wondering, if GDB already has a trace
6923 variable numbered 1, it will be renumbered.) */
6924 create_trace_state_variable (1, 0);
6925 set_trace_state_variable_name (1, "trace_timestamp");
6926 set_trace_state_variable_getter (1, get_timestamp);
6928 #ifdef IN_PROCESS_AGENT
6933 pagesize = sysconf (_SC_PAGE_SIZE);
6937 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
6939 #define SCRATCH_BUFFER_NPAGES 20
6941 /* Allocate scratch buffer aligned on a page boundary, at a low
6942 address (close to the main executable's code). */
6943 for (addr = pagesize; addr != 0; addr += pagesize)
6945 gdb_jump_pad_buffer = mmap ((void *) addr, pagesize * SCRATCH_BUFFER_NPAGES,
6946 PROT_READ | PROT_WRITE | PROT_EXEC,
6947 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
6949 if (gdb_jump_pad_buffer != MAP_FAILED)
6955 initialize_tracepoint: mmap'ing jump pad buffer failed with %s",
6958 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * SCRATCH_BUFFER_NPAGES;
6961 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
6963 /* It's not a fatal error for something to go wrong with trampoline
6964 buffer setup, but it can be mysterious, so create a channel to
6965 report back on what went wrong, using a fixed size since we may
6966 not be able to allocate space later when the problem occurs. */
6967 gdb_trampoline_buffer_error = xmalloc (IPA_BUFSIZ);
6969 strcpy (gdb_trampoline_buffer_error, "No errors reported");
6971 initialize_low_tracepoint ();