]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/tracepoint.c
daily update
[thirdparty/binutils-gdb.git] / gdb / gdbserver / tracepoint.c
CommitLineData
219f2f23 1/* Tracepoint code for remote server for GDB.
7b6bb8da 2 Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
219f2f23
PA
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19#include "server.h"
20#include <ctype.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <sys/time.h>
fa593d66 24#include <stddef.h>
fa593d66
PA
25#if HAVE_STDINT_H
26#include <stdint.h>
27#endif
219f2f23 28
61adf464 29/* This file is built for both GDBserver, and the in-process
fa593d66
PA
30 agent (IPA), a shared library that includes a tracing agent that is
31 loaded by the inferior to support fast tracepoints. Fast
32 tracepoints (or more accurately, jump based tracepoints) are
33 implemented by patching the tracepoint location with a jump into a
34 small trampoline function whose job is to save the register state,
35 call the in-process tracing agent, and then execute the original
36 instruction that was under the tracepoint jump (possibly adjusted,
37 if PC-relative, or some such).
38
39 The current synchronization design is pull based. That means,
40 GDBserver does most of the work, by peeking/poking at the inferior
41 agent's memory directly for downloading tracepoint and associated
42 objects, and for uploading trace frames. Whenever the IPA needs
43 something from GDBserver (trace buffer is full, tracing stopped for
44 some reason, etc.) the IPA calls a corresponding hook function
45 where GDBserver has placed a breakpoint.
46
47 Each of the agents has its own trace buffer. When browsing the
48 trace frames built from slow and fast tracepoints from GDB (tfind
49 mode), there's no guarantee the user is seeing the trace frames in
50 strict chronological creation order, although, GDBserver tries to
51 keep the order relatively reasonable, by syncing the trace buffers
52 at appropriate times.
53
54*/
55
56static void trace_vdebug (const char *, ...) ATTR_FORMAT (printf, 1, 2);
219f2f23
PA
57
58static void
fa593d66 59trace_vdebug (const char *fmt, ...)
219f2f23
PA
60{
61 char buf[1024];
62 va_list ap;
63
64 va_start (ap, fmt);
65 vsprintf (buf, fmt, ap);
66 fprintf (stderr, "gdbserver/tracepoint: %s\n", buf);
67 va_end (ap);
68}
69
fa593d66 70#define trace_debug_1(level, fmt, args...) \
219f2f23 71 do { \
fa593d66
PA
72 if (level <= debug_threads) \
73 trace_vdebug ((fmt), ##args); \
219f2f23
PA
74 } while (0)
75
fa593d66
PA
76#define trace_debug(FMT, args...) \
77 trace_debug_1 (1, FMT, ##args)
78
79#if defined(__GNUC__)
80# define ATTR_USED __attribute__((used))
81# define ATTR_NOINLINE __attribute__((noinline))
82# define ATTR_CONSTRUCTOR __attribute__ ((constructor))
83#else
84# define ATTR_USED
85# define ATTR_NOINLINE
86# define ATTR_CONSTRUCTOR
87#endif
88
89/* Make sure the functions the IPA needs to export (symbols GDBserver
90 needs to query GDB about) are exported. */
91
92#ifdef IN_PROCESS_AGENT
93# if defined _WIN32 || defined __CYGWIN__
94# define IP_AGENT_EXPORT __declspec(dllexport) ATTR_USED
95# else
96# if __GNUC__ >= 4
97# define IP_AGENT_EXPORT \
98 __attribute__ ((visibility("default"))) ATTR_USED
99# else
100# define IP_AGENT_EXPORT ATTR_USED
101# endif
102# endif
103#else
104# define IP_AGENT_EXPORT
105#endif
106
107/* Prefix exported symbols, for good citizenship. All the symbols
108 that need exporting are defined in this module. */
109#ifdef IN_PROCESS_AGENT
110# define gdb_tp_heap_buffer gdb_agent_gdb_tp_heap_buffer
111# define gdb_jump_pad_buffer gdb_agent_gdb_jump_pad_buffer
112# define gdb_jump_pad_buffer_end gdb_agent_gdb_jump_pad_buffer_end
405f8e94
SS
113# define gdb_trampoline_buffer gdb_agent_gdb_trampoline_buffer
114# define gdb_trampoline_buffer_end gdb_agent_gdb_trampoline_buffer_end
115# define gdb_trampoline_buffer_error gdb_agent_gdb_trampoline_buffer_error
fa593d66
PA
116# define collecting gdb_agent_collecting
117# define gdb_collect gdb_agent_gdb_collect
118# define stop_tracing gdb_agent_stop_tracing
119# define flush_trace_buffer gdb_agent_flush_trace_buffer
120# define about_to_request_buffer_space gdb_agent_about_to_request_buffer_space
121# define trace_buffer_is_full gdb_agent_trace_buffer_is_full
122# define stopping_tracepoint gdb_agent_stopping_tracepoint
123# define expr_eval_result gdb_agent_expr_eval_result
124# define error_tracepoint gdb_agent_error_tracepoint
125# define tracepoints gdb_agent_tracepoints
126# define tracing gdb_agent_tracing
127# define trace_buffer_ctrl gdb_agent_trace_buffer_ctrl
128# define trace_buffer_ctrl_curr gdb_agent_trace_buffer_ctrl_curr
129# define trace_buffer_lo gdb_agent_trace_buffer_lo
130# define trace_buffer_hi gdb_agent_trace_buffer_hi
131# define traceframe_read_count gdb_agent_traceframe_read_count
132# define traceframe_write_count gdb_agent_traceframe_write_count
133# define traceframes_created gdb_agent_traceframes_created
134# define trace_state_variables gdb_agent_trace_state_variables
6a271cae 135# define get_raw_reg gdb_agent_get_raw_reg
493e2a69
MS
136# define get_trace_state_variable_value \
137 gdb_agent_get_trace_state_variable_value
138# define set_trace_state_variable_value \
139 gdb_agent_set_trace_state_variable_value
0fb4aa4b
PA
140# define ust_loaded gdb_agent_ust_loaded
141# define helper_thread_id gdb_agent_helper_thread_id
142# define cmd_buf gdb_agent_cmd_buf
fa593d66
PA
143#endif
144
145#ifndef IN_PROCESS_AGENT
146
147/* Addresses of in-process agent's symbols GDBserver cares about. */
148
149struct ipa_sym_addresses
150{
151 CORE_ADDR addr_gdb_tp_heap_buffer;
152 CORE_ADDR addr_gdb_jump_pad_buffer;
153 CORE_ADDR addr_gdb_jump_pad_buffer_end;
405f8e94
SS
154 CORE_ADDR addr_gdb_trampoline_buffer;
155 CORE_ADDR addr_gdb_trampoline_buffer_end;
156 CORE_ADDR addr_gdb_trampoline_buffer_error;
fa593d66
PA
157 CORE_ADDR addr_collecting;
158 CORE_ADDR addr_gdb_collect;
159 CORE_ADDR addr_stop_tracing;
160 CORE_ADDR addr_flush_trace_buffer;
161 CORE_ADDR addr_about_to_request_buffer_space;
162 CORE_ADDR addr_trace_buffer_is_full;
163 CORE_ADDR addr_stopping_tracepoint;
164 CORE_ADDR addr_expr_eval_result;
165 CORE_ADDR addr_error_tracepoint;
166 CORE_ADDR addr_tracepoints;
167 CORE_ADDR addr_tracing;
168 CORE_ADDR addr_trace_buffer_ctrl;
169 CORE_ADDR addr_trace_buffer_ctrl_curr;
170 CORE_ADDR addr_trace_buffer_lo;
171 CORE_ADDR addr_trace_buffer_hi;
172 CORE_ADDR addr_traceframe_read_count;
173 CORE_ADDR addr_traceframe_write_count;
174 CORE_ADDR addr_traceframes_created;
175 CORE_ADDR addr_trace_state_variables;
6a271cae
PA
176 CORE_ADDR addr_get_raw_reg;
177 CORE_ADDR addr_get_trace_state_variable_value;
178 CORE_ADDR addr_set_trace_state_variable_value;
0fb4aa4b
PA
179 CORE_ADDR addr_ust_loaded;
180 CORE_ADDR addr_helper_thread_id;
181 CORE_ADDR addr_cmd_buf;
fa593d66
PA
182};
183
184#define STRINGIZE_1(STR) #STR
185#define STRINGIZE(STR) STRINGIZE_1(STR)
493e2a69 186#define IPA_SYM(SYM) \
fa593d66
PA
187 { \
188 STRINGIZE (gdb_agent_ ## SYM), \
189 offsetof (struct ipa_sym_addresses, addr_ ## SYM) \
190 }
191
192static struct
193{
194 const char *name;
195 int offset;
196 int required;
197} symbol_list[] = {
198 IPA_SYM(gdb_tp_heap_buffer),
199 IPA_SYM(gdb_jump_pad_buffer),
200 IPA_SYM(gdb_jump_pad_buffer_end),
405f8e94
SS
201 IPA_SYM(gdb_trampoline_buffer),
202 IPA_SYM(gdb_trampoline_buffer_end),
203 IPA_SYM(gdb_trampoline_buffer_error),
fa593d66
PA
204 IPA_SYM(collecting),
205 IPA_SYM(gdb_collect),
206 IPA_SYM(stop_tracing),
207 IPA_SYM(flush_trace_buffer),
208 IPA_SYM(about_to_request_buffer_space),
209 IPA_SYM(trace_buffer_is_full),
210 IPA_SYM(stopping_tracepoint),
211 IPA_SYM(expr_eval_result),
212 IPA_SYM(error_tracepoint),
213 IPA_SYM(tracepoints),
214 IPA_SYM(tracing),
215 IPA_SYM(trace_buffer_ctrl),
216 IPA_SYM(trace_buffer_ctrl_curr),
217 IPA_SYM(trace_buffer_lo),
218 IPA_SYM(trace_buffer_hi),
219 IPA_SYM(traceframe_read_count),
220 IPA_SYM(traceframe_write_count),
221 IPA_SYM(traceframes_created),
222 IPA_SYM(trace_state_variables),
6a271cae
PA
223 IPA_SYM(get_raw_reg),
224 IPA_SYM(get_trace_state_variable_value),
225 IPA_SYM(set_trace_state_variable_value),
0fb4aa4b
PA
226 IPA_SYM(ust_loaded),
227 IPA_SYM(helper_thread_id),
228 IPA_SYM(cmd_buf),
fa593d66
PA
229};
230
231struct ipa_sym_addresses ipa_sym_addrs;
232
233int all_tracepoint_symbols_looked_up;
234
235int
236in_process_agent_loaded (void)
237{
238 return all_tracepoint_symbols_looked_up;
239}
240
241static int read_inferior_integer (CORE_ADDR symaddr, int *val);
242
0fb4aa4b
PA
243/* Returns true if both the in-process agent library and the static
244 tracepoints libraries are loaded in the inferior. */
245
246static int
247in_process_agent_loaded_ust (void)
248{
249 int loaded = 0;
250
251 if (!in_process_agent_loaded ())
252 {
253 warning ("In-process agent not loaded");
254 return 0;
255 }
256
257 if (read_inferior_integer (ipa_sym_addrs.addr_ust_loaded, &loaded))
258 {
259 warning ("Error reading ust_loaded in lib");
260 return 0;
261 }
262
263 return loaded;
264}
265
fa593d66
PA
266static void
267write_e_ipa_not_loaded (char *buffer)
268{
269 sprintf (buffer,
270 "E.In-process agent library not loaded in process. "
0fb4aa4b
PA
271 "Fast and static tracepoints unavailable.");
272}
273
274/* Write an error to BUFFER indicating that UST isn't loaded in the
275 inferior. */
276
277static void
278write_e_ust_not_loaded (char *buffer)
279{
280#ifdef HAVE_UST
281 sprintf (buffer,
282 "E.UST library not loaded in process. "
283 "Static tracepoints unavailable.");
284#else
285 sprintf (buffer, "E.GDBserver was built without static tracepoints support");
286#endif
fa593d66
PA
287}
288
0fb4aa4b
PA
289/* If the in-process agent library isn't loaded in the inferior, write
290 an error to BUFFER, and return 1. Otherwise, return 0. */
291
fa593d66
PA
292static int
293maybe_write_ipa_not_loaded (char *buffer)
294{
295 if (!in_process_agent_loaded ())
296 {
297 write_e_ipa_not_loaded (buffer);
298 return 1;
299 }
300 return 0;
301}
302
0fb4aa4b
PA
303/* If the in-process agent library and the ust (static tracepoints)
304 library aren't loaded in the inferior, write an error to BUFFER,
305 and return 1. Otherwise, return 0. */
306
307static int
308maybe_write_ipa_ust_not_loaded (char *buffer)
309{
310 if (!in_process_agent_loaded ())
311 {
312 write_e_ipa_not_loaded (buffer);
313 return 1;
314 }
315 else if (!in_process_agent_loaded_ust ())
316 {
317 write_e_ust_not_loaded (buffer);
318 return 1;
319 }
320 return 0;
321}
322
fa593d66
PA
323/* Cache all future symbols that the tracepoints module might request.
324 We can not request symbols at arbitrary states in the remote
325 protocol, only when the client tells us that new symbols are
326 available. So when we load the in-process library, make sure to
327 check the entire list. */
328
329void
330tracepoint_look_up_symbols (void)
331{
fa593d66
PA
332 int i;
333
334 if (all_tracepoint_symbols_looked_up)
335 return;
336
fa593d66
PA
337 for (i = 0; i < sizeof (symbol_list) / sizeof (symbol_list[0]); i++)
338 {
339 CORE_ADDR *addrp =
340 (CORE_ADDR *) ((char *) &ipa_sym_addrs + symbol_list[i].offset);
341
342 if (look_up_one_symbol (symbol_list[i].name, addrp, 1) == 0)
343 {
344 if (debug_threads)
345 fprintf (stderr, "symbol `%s' not found\n", symbol_list[i].name);
2275a1a7 346 return;
fa593d66
PA
347 }
348 }
349
2275a1a7 350 all_tracepoint_symbols_looked_up = 1;
fa593d66
PA
351}
352
353#endif
354
355/* GDBserver places a breakpoint on the IPA's version (which is a nop)
356 of the "stop_tracing" function. When this breakpoint is hit,
357 tracing stopped in the IPA for some reason. E.g., due to
358 tracepoint reaching the pass count, hitting conditional expression
359 evaluation error, etc.
360
361 The IPA's trace buffer is never in circular tracing mode: instead,
362 GDBserver's is, and whenever the in-process buffer fills, it calls
363 "flush_trace_buffer", which triggers an internal breakpoint.
364 GDBserver reacts to this breakpoint by pulling the meanwhile
365 collected data. Old frames discarding is always handled on the
366 GDBserver side. */
367
368#ifdef IN_PROCESS_AGENT
369int debug_threads = 0;
370
371int
372read_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
373{
374 memcpy (myaddr, (void *) (uintptr_t) memaddr, len);
375 return 0;
376}
377
378/* Call this in the functions where GDBserver places a breakpoint, so
379 that the compiler doesn't try to be clever and skip calling the
380 function at all. This is necessary, even if we tell the compiler
381 to not inline said functions. */
382
383#if defined(__GNUC__)
384# define UNKNOWN_SIDE_EFFECTS() asm ("")
385#else
386# define UNKNOWN_SIDE_EFFECTS() do {} while (0)
387#endif
388
389IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
390stop_tracing (void)
391{
392 /* GDBserver places breakpoint here. */
393 UNKNOWN_SIDE_EFFECTS();
394}
395
396IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
397flush_trace_buffer (void)
398{
399 /* GDBserver places breakpoint here. */
400 UNKNOWN_SIDE_EFFECTS();
401}
402
403#endif
404
405#ifndef IN_PROCESS_AGENT
219f2f23
PA
406static int
407tracepoint_handler (CORE_ADDR address)
408{
409 trace_debug ("tracepoint_handler: tracepoint at 0x%s hit",
410 paddress (address));
411 return 0;
412}
413
fa593d66
PA
414/* Breakpoint at "stop_tracing" in the inferior lib. */
415struct breakpoint *stop_tracing_bkpt;
416static int stop_tracing_handler (CORE_ADDR);
417
418/* Breakpoint at "flush_trace_buffer" in the inferior lib. */
419struct breakpoint *flush_trace_buffer_bkpt;
420static int flush_trace_buffer_handler (CORE_ADDR);
421
422static void download_tracepoints (void);
423static void download_trace_state_variables (void);
424static void upload_fast_traceframes (void);
425
0fb4aa4b
PA
426static int run_inferior_command (char *cmd);
427
fa593d66
PA
428static int
429read_inferior_integer (CORE_ADDR symaddr, int *val)
430{
431 return read_inferior_memory (symaddr, (unsigned char *) val,
432 sizeof (*val));
433}
434
435static int
436read_inferior_uinteger (CORE_ADDR symaddr, unsigned int *val)
437{
438 return read_inferior_memory (symaddr, (unsigned char *) val,
439 sizeof (*val));
440}
441
442static int
443read_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR *val)
444{
445 void *pval = (void *) (uintptr_t) val;
446 int ret;
447
448 ret = read_inferior_memory (symaddr, (unsigned char *) &pval, sizeof (pval));
449 *val = (uintptr_t) pval;
450 return ret;
451}
452
453static int
454write_inferior_data_pointer (CORE_ADDR symaddr, CORE_ADDR val)
455{
456 void *pval = (void *) (uintptr_t) val;
457 return write_inferior_memory (symaddr,
458 (unsigned char *) &pval, sizeof (pval));
459}
460
461static int
462write_inferior_integer (CORE_ADDR symaddr, int val)
463{
464 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
465}
466
467static int
468write_inferior_uinteger (CORE_ADDR symaddr, unsigned int val)
469{
470 return write_inferior_memory (symaddr, (unsigned char *) &val, sizeof (val));
471}
472
473#endif
474
219f2f23
PA
475/* This enum must exactly match what is documented in
476 gdb/doc/agentexpr.texi, including all the numerical values. */
477
478enum gdb_agent_op
479 {
94d5e490
TT
480#define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) \
481 gdb_agent_op_ ## NAME = VALUE,
482#include "ax.def"
483#undef DEFOP
219f2f23
PA
484 gdb_agent_op_last
485 };
486
487static const char *gdb_agent_op_names [gdb_agent_op_last] =
488 {
94d5e490
TT
489 "?undef?"
490#define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , # NAME
491#include "ax.def"
492#undef DEFOP
219f2f23
PA
493 };
494
6b9801d4
SS
495static const unsigned char gdb_agent_op_sizes [gdb_agent_op_last] =
496 {
497 0
498#define DEFOP(NAME, SIZE, DATA_SIZE, CONSUMED, PRODUCED, VALUE) , SIZE
499#include "ax.def"
500#undef DEFOP
501 };
502
219f2f23
PA
503struct agent_expr
504{
505 int length;
506
507 unsigned char *bytes;
508};
509
510/* Base action. Concrete actions inherit this. */
511
512struct tracepoint_action
513{
514 char type;
515};
516
517/* An 'M' (collect memory) action. */
518struct collect_memory_action
519{
520 struct tracepoint_action base;
521
522 ULONGEST addr;
523 ULONGEST len;
524 int basereg;
525};
526
527/* An 'R' (collect registers) action. */
528
529struct collect_registers_action
530{
531 struct tracepoint_action base;
532};
533
534/* An 'X' (evaluate expression) action. */
535
536struct eval_expr_action
537{
538 struct tracepoint_action base;
539
540 struct agent_expr *expr;
541};
542
0fb4aa4b
PA
543/* An 'L' (collect static trace data) action. */
544struct collect_static_trace_data_action
545{
546 struct tracepoint_action base;
547};
548
219f2f23
PA
549/* This structure describes a piece of the source-level definition of
550 the tracepoint. The contents are not interpreted by the target,
551 but preserved verbatim for uploading upon reconnection. */
552
553struct source_string
554{
555 /* The type of string, such as "cond" for a conditional. */
556 char *type;
557
558 /* The source-level string itself. For the sake of target
559 debugging, we store it in plaintext, even though it is always
560 transmitted in hex. */
561 char *str;
562
563 /* Link to the next one in the list. We link them in the order
564 received, in case some make up an ordered list of commands or
565 some such. */
566 struct source_string *next;
567};
568
fa593d66
PA
569enum tracepoint_type
570{
571 /* Trap based tracepoint. */
572 trap_tracepoint,
573
574 /* A fast tracepoint implemented with a jump instead of a trap. */
575 fast_tracepoint,
0fb4aa4b
PA
576
577 /* A static tracepoint, implemented by a program call into a tracing
578 library. */
579 static_tracepoint
fa593d66
PA
580};
581
219f2f23
PA
582struct tracepoint_hit_ctx;
583
6a271cae
PA
584typedef enum eval_result_type (*condfn) (struct tracepoint_hit_ctx *,
585 ULONGEST *);
586
219f2f23
PA
587/* The definition of a tracepoint. */
588
589/* Tracepoints may have multiple locations, each at a different
590 address. This can occur with optimizations, template
591 instantiation, etc. Since the locations may be in different
592 scopes, the conditions and actions may be different for each
593 location. Our target version of tracepoints is more like GDB's
594 notion of "breakpoint locations", but we have almost nothing that
595 is not per-location, so we bother having two kinds of objects. The
596 key consequence is that numbers are not unique, and that it takes
597 both number and address to identify a tracepoint uniquely. */
598
599struct tracepoint
600{
601 /* The number of the tracepoint, as specified by GDB. Several
602 tracepoint objects here may share a number. */
603 int number;
604
605 /* Address at which the tracepoint is supposed to trigger. Several
606 tracepoints may share an address. */
607 CORE_ADDR address;
608
fa593d66
PA
609 /* Tracepoint type. */
610 enum tracepoint_type type;
611
219f2f23
PA
612 /* True if the tracepoint is currently enabled. */
613 int enabled;
614
615 /* The number of single steps that will be performed after each
616 tracepoint hit. */
617 long step_count;
618
619 /* The number of times the tracepoint may be hit before it will
620 terminate the entire tracing run. */
621 long pass_count;
622
623 /* Pointer to the agent expression that is the tracepoint's
624 conditional, or NULL if the tracepoint is unconditional. */
625 struct agent_expr *cond;
626
627 /* The list of actions to take when the tracepoint triggers. */
628 int numactions;
629 struct tracepoint_action **actions;
219f2f23
PA
630
631 /* Count of the times we've hit this tracepoint during the run.
632 Note that while-stepping steps are not counted as "hits". */
633 long hit_count;
634
6a271cae
PA
635 CORE_ADDR compiled_cond;
636
fa593d66
PA
637 /* Link to the next tracepoint in the list. */
638 struct tracepoint *next;
639
640#ifndef IN_PROCESS_AGENT
641 /* The list of actions to take when the tracepoint triggers, in
642 string/packet form. */
643 char **actions_str;
644
219f2f23
PA
645 /* The collection of strings that describe the tracepoint as it was
646 entered into GDB. These are not used by the target, but are
647 reported back to GDB upon reconnection. */
648 struct source_string *source_strings;
649
fa593d66
PA
650 /* The number of bytes displaced by fast tracepoints. It may subsume
651 multiple instructions, for multi-byte fast tracepoints. This
652 field is only valid for fast tracepoints. */
653 int orig_size;
654
655 /* Only for fast tracepoints. */
656 CORE_ADDR obj_addr_on_target;
657
658 /* Address range where the original instruction under a fast
659 tracepoint was relocated to. (_end is actually one byte past
660 the end). */
661 CORE_ADDR adjusted_insn_addr;
662 CORE_ADDR adjusted_insn_addr_end;
663
664 /* The address range of the piece of the jump pad buffer that was
665 assigned to this fast tracepoint. (_end is actually one byte
666 past the end).*/
667 CORE_ADDR jump_pad;
668 CORE_ADDR jump_pad_end;
669
405f8e94
SS
670 /* The address range of the piece of the trampoline buffer that was
671 assigned to this fast tracepoint. (_end is actually one byte
672 past the end). */
673 CORE_ADDR trampoline;
674 CORE_ADDR trampoline_end;
675
fa593d66
PA
676 /* The list of actions to take while in a stepping loop. These
677 fields are only valid for patch-based tracepoints. */
678 int num_step_actions;
679 struct tracepoint_action **step_actions;
680 /* Same, but in string/packet form. */
681 char **step_actions_str;
682
683 /* Handle returned by the breakpoint or tracepoint module when we
0fb4aa4b
PA
684 inserted the trap or jump, or hooked into a static tracepoint.
685 NULL if we haven't inserted it yet. */
219f2f23 686 void *handle;
fa593d66 687#endif
219f2f23 688
219f2f23
PA
689};
690
fa593d66
PA
691#ifndef IN_PROCESS_AGENT
692
219f2f23
PA
693/* Given `while-stepping', a thread may be collecting data for more
694 than one tracepoint simultaneously. On the other hand, the same
695 tracepoint with a while-stepping action may be hit by more than one
696 thread simultaneously (but not quite, each thread could be handling
697 a different step). Each thread holds a list of these objects,
698 representing the current step of each while-stepping action being
699 collected. */
700
701struct wstep_state
702{
703 struct wstep_state *next;
704
705 /* The tracepoint number. */
706 int tp_number;
707 /* The tracepoint's address. */
708 CORE_ADDR tp_address;
709
710 /* The number of the current step in this 'while-stepping'
711 action. */
712 long current_step;
713};
714
fa593d66
PA
715#endif
716
717/* The linked list of all tracepoints. Marked explicitly as used as
718 the in-process library doesn't use it for the fast tracepoints
719 support. */
720IP_AGENT_EXPORT struct tracepoint *tracepoints ATTR_USED;
219f2f23 721
fa593d66 722#ifndef IN_PROCESS_AGENT
219f2f23
PA
723
724/* Pointer to the last tracepoint in the list, new tracepoints are
725 linked in at the end. */
726
727static struct tracepoint *last_tracepoint;
fa593d66 728#endif
219f2f23
PA
729
730/* The first tracepoint to exceed its pass count. */
731
fa593d66 732IP_AGENT_EXPORT struct tracepoint *stopping_tracepoint;
219f2f23
PA
733
734/* True if the trace buffer is full or otherwise no longer usable. */
735
fa593d66 736IP_AGENT_EXPORT int trace_buffer_is_full;
219f2f23
PA
737
738/* Enumeration of the different kinds of things that can happen during
739 agent expression evaluation. */
740
741enum eval_result_type
742 {
743 expr_eval_no_error,
744 expr_eval_empty_expression,
745 expr_eval_empty_stack,
746 expr_eval_stack_overflow,
747 expr_eval_stack_underflow,
748 expr_eval_unhandled_opcode,
749 expr_eval_unrecognized_opcode,
750 expr_eval_divide_by_zero,
751 expr_eval_invalid_goto
752 };
753
754static enum eval_result_type expr_eval_result = expr_eval_no_error;
755
fa593d66
PA
756#ifndef IN_PROCESS_AGENT
757
219f2f23
PA
758static const char *eval_result_names[] =
759 {
760 "terror:in the attic", /* this should never be reported */
761 "terror:empty expression",
762 "terror:empty stack",
763 "terror:stack overflow",
764 "terror:stack underflow",
765 "terror:unhandled opcode",
766 "terror:unrecognized opcode",
767 "terror:divide by zero"
768 };
769
fa593d66
PA
770#endif
771
219f2f23
PA
772/* The tracepoint in which the error occurred. */
773
774static struct tracepoint *error_tracepoint;
775
776struct trace_state_variable
777{
778 /* This is the name of the variable as used in GDB. The target
779 doesn't use the name, but needs to have it for saving and
780 reconnection purposes. */
781 char *name;
782
783 /* This number identifies the variable uniquely. Numbers may be
784 assigned either by the target (in the case of builtin variables),
785 or by GDB, and are presumed unique during the course of a trace
786 experiment. */
787 int number;
788
789 /* The variable's initial value, a 64-bit signed integer always. */
790 LONGEST initial_value;
791
792 /* The variable's value, a 64-bit signed integer always. */
793 LONGEST value;
794
795 /* Pointer to a getter function, used to supply computed values. */
796 LONGEST (*getter) (void);
797
798 /* Link to the next variable. */
799 struct trace_state_variable *next;
800};
801
802/* Linked list of all trace state variables. */
803
fa593d66
PA
804#ifdef IN_PROCESS_AGENT
805struct trace_state_variable *alloced_trace_state_variables;
806#endif
807
808IP_AGENT_EXPORT struct trace_state_variable *trace_state_variables;
219f2f23
PA
809
810/* The results of tracing go into a fixed-size space known as the
811 "trace buffer". Because usage follows a limited number of
812 patterns, we manage it ourselves rather than with malloc. Basic
813 rules are that we create only one trace frame at a time, each is
814 variable in size, they are never moved once created, and we only
815 discard if we are doing a circular buffer, and then only the oldest
816 ones. Each trace frame includes its own size, so we don't need to
817 link them together, and the trace frame number is relative to the
818 first one, so we don't need to record numbers. A trace frame also
819 records the number of the tracepoint that created it. The data
820 itself is a series of blocks, each introduced by a single character
821 and with a defined format. Each type of block has enough
822 type/length info to allow scanners to jump quickly from one block
823 to the next without reading each byte in the block. */
824
825/* Trace buffer management would be simple - advance a free pointer
826 from beginning to end, then stop - were it not for the circular
827 buffer option, which is a useful way to prevent a trace run from
828 stopping prematurely because the buffer filled up. In the circular
829 case, the location of the first trace frame (trace_buffer_start)
830 moves as old trace frames are discarded. Also, since we grow trace
831 frames incrementally as actions are performed, we wrap around to
832 the beginning of the trace buffer. This is per-block, so each
833 block within a trace frame remains contiguous. Things get messy
834 when the wrapped-around trace frame is the one being discarded; the
835 free space ends up in two parts at opposite ends of the buffer. */
836
837#ifndef ATTR_PACKED
838# if defined(__GNUC__)
839# define ATTR_PACKED __attribute__ ((packed))
840# else
841# define ATTR_PACKED /* nothing */
842# endif
843#endif
844
845/* The data collected at a tracepoint hit. This object should be as
846 small as possible, since there may be a great many of them. We do
847 not need to keep a frame number, because they are all sequential
848 and there are no deletions; so the Nth frame in the buffer is
849 always frame number N. */
850
851struct traceframe
852{
853 /* Number of the tracepoint that collected this traceframe. A value
854 of 0 indicates the current end of the trace buffer. We make this
855 a 16-bit field because it's never going to happen that GDB's
856 numbering of tracepoints reaches 32,000. */
857 int tpnum : 16;
858
859 /* The size of the data in this trace frame. We limit this to 32
860 bits, even on a 64-bit target, because it's just implausible that
861 one is validly going to collect 4 gigabytes of data at a single
862 tracepoint hit. */
863 unsigned int data_size : 32;
864
865 /* The base of the trace data, which is contiguous from this point. */
866 unsigned char data[0];
867
fa593d66 868} ATTR_PACKED;
219f2f23
PA
869
870/* The traceframe to be used as the source of data to send back to
871 GDB. A value of -1 means to get data from the live program. */
872
873int current_traceframe = -1;
874
875/* This flag is true if the trace buffer is circular, meaning that
876 when it fills, the oldest trace frames are discarded in order to
877 make room. */
878
fa593d66 879#ifndef IN_PROCESS_AGENT
219f2f23 880static int circular_trace_buffer;
fa593d66 881#endif
219f2f23
PA
882
883/* Pointer to the block of memory that traceframes all go into. */
884
885static unsigned char *trace_buffer_lo;
886
887/* Pointer to the end of the trace buffer, more precisely to the byte
888 after the end of the buffer. */
889
890static unsigned char *trace_buffer_hi;
891
fa593d66
PA
892/* Control structure holding the read/write/etc. pointers into the
893 trace buffer. We need more than one of these to implement a
894 transaction-like mechanism to garantees that both GDBserver and the
895 in-process agent can try to change the trace buffer
896 simultaneously. */
897
898struct trace_buffer_control
899{
900 /* Pointer to the first trace frame in the buffer. In the
901 non-circular case, this is equal to trace_buffer_lo, otherwise it
902 moves around in the buffer. */
903 unsigned char *start;
904
905 /* Pointer to the free part of the trace buffer. Note that we clear
906 several bytes at and after this pointer, so that traceframe
907 scans/searches terminate properly. */
908 unsigned char *free;
909
910 /* Pointer to the byte after the end of the free part. Note that
911 this may be smaller than trace_buffer_free in the circular case,
912 and means that the free part is in two pieces. Initially it is
913 equal to trace_buffer_hi, then is generally equivalent to
914 trace_buffer_start. */
915 unsigned char *end_free;
916
917 /* Pointer to the wraparound. If not equal to trace_buffer_hi, then
918 this is the point at which the trace data breaks, and resumes at
919 trace_buffer_lo. */
920 unsigned char *wrap;
921};
922
923/* Same as above, to be used by GDBserver when updating the in-process
924 agent. */
925struct ipa_trace_buffer_control
926{
927 uintptr_t start;
928 uintptr_t free;
929 uintptr_t end_free;
930 uintptr_t wrap;
931};
932
933
934/* We have possibly both GDBserver and an inferior thread accessing
935 the same IPA trace buffer memory. The IPA is the producer (tries
936 to put new frames in the buffer), while GDBserver occasionally
937 consumes them, that is, flushes the IPA's buffer into its own
938 buffer. Both sides need to update the trace buffer control
939 pointers (current head, tail, etc.). We can't use a global lock to
940 synchronize the accesses, as otherwise we could deadlock GDBserver
941 (if the thread holding the lock stops for a signal, say). So
942 instead of that, we use a transaction scheme where GDBserver writes
943 always prevail over the IPAs writes, and, we have the IPA detect
944 the commit failure/overwrite, and retry the whole attempt. This is
945 mainly implemented by having a global token object that represents
946 who wrote last to the buffer control structure. We need to freeze
947 any inferior writing to the buffer while GDBserver touches memory,
948 so that the inferior can correctly detect that GDBserver had been
949 there, otherwise, it could mistakingly think its commit was
950 successful; that's implemented by simply having GDBserver set a
951 breakpoint the inferior hits if it is the critical region.
952
953 There are three cycling trace buffer control structure copies
954 (buffer head, tail, etc.), with the token object including an index
955 indicating which is current live copy. The IPA tentatively builds
956 an updated copy in a non-current control structure, while GDBserver
957 always clobbers the current version directly. The IPA then tries
958 to atomically "commit" its version; if GDBserver clobbered the
959 structure meanwhile, that will fail, and the IPA restarts the
960 allocation process.
961
962 Listing the step in further detail, we have:
963
964 In-process agent (producer):
965
966 - passes by `about_to_request_buffer_space' breakpoint/lock
967
968 - reads current token, extracts current trace buffer control index,
969 and starts tentatively updating the rightmost one (0->1, 1->2,
970 2->0). Note that only one inferior thread is executing this code
971 at any given time, due to an outer lock in the jump pads.
219f2f23 972
fa593d66 973 - updates counters, and tries to commit the token.
219f2f23 974
fa593d66
PA
975 - passes by second `about_to_request_buffer_space' breakpoint/lock,
976 leaving the sync region.
219f2f23 977
fa593d66 978 - checks if the update was effective.
219f2f23 979
fa593d66
PA
980 - if trace buffer was found full, hits flush_trace_buffer
981 breakpoint, and restarts later afterwards.
219f2f23 982
fa593d66 983 GDBserver (consumer):
219f2f23 984
fa593d66
PA
985 - sets `about_to_request_buffer_space' breakpoint/lock.
986
987 - updates the token unconditionally, using the current buffer
988 control index, since it knows that the IP agent always writes to
989 the rightmost, and due to the breakpoint, at most one IP thread
990 can try to update the trace buffer concurrently to GDBserver, so
991 there will be no danger of trace buffer control index wrap making
992 the IPA write to the same index as GDBserver.
993
994 - flushes the IP agent's trace buffer completely, and updates the
995 current trace buffer control structure. GDBserver *always* wins.
996
997 - removes the `about_to_request_buffer_space' breakpoint.
998
999The token is stored in the `trace_buffer_ctrl_curr' variable.
1000Internally, it's bits are defined as:
1001
1002 |-------------+-----+-------------+--------+-------------+--------------|
1003 | Bit offsets | 31 | 30 - 20 | 19 | 18-8 | 7-0 |
1004 |-------------+-----+-------------+--------+-------------+--------------|
1005 | What | GSB | PC (11-bit) | unused | CC (11-bit) | TBCI (8-bit) |
1006 |-------------+-----+-------------+--------+-------------+--------------|
1007
1008 GSB - GDBserver Stamp Bit
1009 PC - Previous Counter
1010 CC - Current Counter
1011 TBCI - Trace Buffer Control Index
1012
1013
1014An IPA update of `trace_buffer_ctrl_curr' does:
1015
1016 - read CC from the current token, save as PC.
1017 - updates pointers
1018 - atomically tries to write PC+1,CC
1019
1020A GDBserver update of `trace_buffer_ctrl_curr' does:
1021
1022 - reads PC and CC from the current token.
1023 - updates pointers
1024 - writes GSB,PC,CC
1025*/
1026
1027/* These are the bits of `trace_buffer_ctrl_curr' that are reserved
1028 for the counters described below. The cleared bits are used to
1029 hold the index of the items of the `trace_buffer_ctrl' array that
1030 is "current". */
1031#define GDBSERVER_FLUSH_COUNT_MASK 0xfffffff0
1032
1033/* `trace_buffer_ctrl_curr' contains two counters. The `previous'
1034 counter, and the `current' counter. */
1035
1036#define GDBSERVER_FLUSH_COUNT_MASK_PREV 0x7ff00000
1037#define GDBSERVER_FLUSH_COUNT_MASK_CURR 0x0007ff00
1038
1039/* When GDBserver update the IP agent's `trace_buffer_ctrl_curr', it
1040 always stamps this bit as set. */
1041#define GDBSERVER_UPDATED_FLUSH_COUNT_BIT 0x80000000
1042
1043#ifdef IN_PROCESS_AGENT
1044IP_AGENT_EXPORT struct trace_buffer_control trace_buffer_ctrl[3];
1045IP_AGENT_EXPORT unsigned int trace_buffer_ctrl_curr;
1046
1047# define TRACE_BUFFER_CTRL_CURR \
1048 (trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK)
1049
1050#else
1051
1052/* The GDBserver side agent only needs one instance of this object, as
1053 it doesn't need to sync with itself. Define it as array anyway so
1054 that the rest of the code base doesn't need to care for the
1055 difference. */
1056struct trace_buffer_control trace_buffer_ctrl[1];
1057# define TRACE_BUFFER_CTRL_CURR 0
1058#endif
1059
1060/* These are convenience macros used to access the current trace
1061 buffer control in effect. */
1062#define trace_buffer_start (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].start)
1063#define trace_buffer_free (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].free)
1064#define trace_buffer_end_free \
1065 (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].end_free)
1066#define trace_buffer_wrap (trace_buffer_ctrl[TRACE_BUFFER_CTRL_CURR].wrap)
219f2f23 1067
219f2f23
PA
1068
1069/* Macro that returns a pointer to the first traceframe in the buffer. */
1070
1071#define FIRST_TRACEFRAME() ((struct traceframe *) trace_buffer_start)
1072
1073/* Macro that returns a pointer to the next traceframe in the buffer.
1074 If the computed location is beyond the wraparound point, subtract
1075 the offset of the wraparound. */
1076
1077#define NEXT_TRACEFRAME_1(TF) \
1078 (((unsigned char *) (TF)) + sizeof (struct traceframe) + (TF)->data_size)
1079
1080#define NEXT_TRACEFRAME(TF) \
1081 ((struct traceframe *) (NEXT_TRACEFRAME_1 (TF) \
1082 - ((NEXT_TRACEFRAME_1 (TF) >= trace_buffer_wrap) \
1083 ? (trace_buffer_wrap - trace_buffer_lo) \
1084 : 0)))
1085
1086/* The difference between these counters represents the total number
fa593d66
PA
1087 of complete traceframes present in the trace buffer. The IP agent
1088 writes to the write count, GDBserver writes to read count. */
219f2f23 1089
fa593d66
PA
1090IP_AGENT_EXPORT unsigned int traceframe_write_count;
1091IP_AGENT_EXPORT unsigned int traceframe_read_count;
219f2f23
PA
1092
1093/* Convenience macro. */
1094
1095#define traceframe_count \
1096 ((unsigned int) (traceframe_write_count - traceframe_read_count))
1097
1098/* The count of all traceframes created in the current run, including
1099 ones that were discarded to make room. */
1100
fa593d66
PA
1101IP_AGENT_EXPORT int traceframes_created;
1102
1103#ifndef IN_PROCESS_AGENT
219f2f23
PA
1104
1105/* Read-only regions are address ranges whose contents don't change,
1106 and so can be read from target memory even while looking at a trace
1107 frame. Without these, disassembly for instance will likely fail,
1108 because the program code is not usually collected into a trace
1109 frame. This data structure does not need to be very complicated or
1110 particularly efficient, it's only going to be used occasionally,
1111 and only by some commands. */
1112
1113struct readonly_region
1114{
1115 /* The bounds of the region. */
1116 CORE_ADDR start, end;
1117
1118 /* Link to the next one. */
1119 struct readonly_region *next;
1120};
1121
1122/* Linked list of readonly regions. This list stays in effect from
1123 one tstart to the next. */
1124
1125static struct readonly_region *readonly_regions;
1126
fa593d66
PA
1127#endif
1128
219f2f23
PA
1129/* The global that controls tracing overall. */
1130
fa593d66
PA
1131IP_AGENT_EXPORT int tracing;
1132
1133#ifndef IN_PROCESS_AGENT
8336d594
PA
1134
1135/* Controls whether tracing should continue after GDB disconnects. */
1136
1137int disconnected_tracing;
219f2f23
PA
1138
1139/* The reason for the last tracing run to have stopped. We initialize
1140 to a distinct string so that GDB can distinguish between "stopped
1141 after running" and "stopped because never run" cases. */
1142
1143static const char *tracing_stop_reason = "tnotrun";
1144
1145static int tracing_stop_tpnum;
1146
fa593d66
PA
1147#endif
1148
219f2f23
PA
1149/* Functions local to this file. */
1150
1151/* Base "class" for tracepoint type specific data to be passed down to
fa593d66 1152 collect_data_at_tracepoint. */
219f2f23
PA
1153struct tracepoint_hit_ctx
1154{
fa593d66 1155 enum tracepoint_type type;
219f2f23
PA
1156};
1157
fa593d66
PA
1158#ifdef IN_PROCESS_AGENT
1159
1160/* Fast/jump tracepoint specific data to be passed down to
219f2f23 1161 collect_data_at_tracepoint. */
fa593d66
PA
1162struct fast_tracepoint_ctx
1163{
1164 struct tracepoint_hit_ctx base;
1165
1166 struct regcache regcache;
1167 int regcache_initted;
1168 unsigned char *regspace;
1169
1170 unsigned char *regs;
1171 struct tracepoint *tpoint;
1172};
219f2f23 1173
0fb4aa4b
PA
1174/* Static tracepoint specific data to be passed down to
1175 collect_data_at_tracepoint. */
1176struct static_tracepoint_ctx
1177{
1178 struct tracepoint_hit_ctx base;
1179
1180 /* The regcache corresponding to the registers state at the time of
1181 the tracepoint hit. Initialized lazily, from REGS. */
1182 struct regcache regcache;
1183 int regcache_initted;
1184
1185 /* The buffer space REGCACHE above uses. We use a separate buffer
1186 instead of letting the regcache malloc for both signal safety and
1187 performance reasons; this is allocated on the stack instead. */
1188 unsigned char *regspace;
1189
1190 /* The register buffer as passed on by lttng/ust. */
1191 struct registers *regs;
1192
1193 /* The "printf" formatter and the args the user passed to the marker
1194 call. We use this to be able to collect "static trace data"
1195 ($_sdata). */
1196 const char *fmt;
1197 va_list *args;
1198
1199 /* The GDB tracepoint matching the probed marker that was "hit". */
1200 struct tracepoint *tpoint;
1201};
1202
fa593d66
PA
1203#else
1204
1205/* Static tracepoint specific data to be passed down to
1206 collect_data_at_tracepoint. */
219f2f23
PA
1207struct trap_tracepoint_ctx
1208{
1209 struct tracepoint_hit_ctx base;
1210
1211 struct regcache *regcache;
1212};
1213
fa593d66
PA
1214#endif
1215
1216#ifndef IN_PROCESS_AGENT
219f2f23
PA
1217static struct agent_expr *parse_agent_expr (char **actparm);
1218static char *unparse_agent_expr (struct agent_expr *aexpr);
fa593d66 1219#endif
219f2f23
PA
1220static enum eval_result_type eval_agent_expr (struct tracepoint_hit_ctx *ctx,
1221 struct traceframe *tframe,
1222 struct agent_expr *aexpr,
1223 ULONGEST *rslt);
1224
1225static int agent_mem_read (struct traceframe *tframe,
1226 unsigned char *to, CORE_ADDR from, ULONGEST len);
3065dfb6
SS
1227static int agent_mem_read_string (struct traceframe *tframe,
1228 unsigned char *to, CORE_ADDR from,
1229 ULONGEST len);
219f2f23
PA
1230static int agent_tsv_read (struct traceframe *tframe, int n);
1231
fa593d66 1232#ifndef IN_PROCESS_AGENT
219f2f23
PA
1233static CORE_ADDR traceframe_get_pc (struct traceframe *tframe);
1234static int traceframe_read_tsv (int num, LONGEST *val);
fa593d66 1235#endif
219f2f23
PA
1236
1237static int condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1238 struct tracepoint *tpoint);
1239
fa593d66 1240#ifndef IN_PROCESS_AGENT
219f2f23
PA
1241static void clear_readonly_regions (void);
1242static void clear_installed_tracepoints (void);
fa593d66 1243#endif
219f2f23
PA
1244
1245static void collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1246 CORE_ADDR stop_pc,
1247 struct tracepoint *tpoint);
fa593d66 1248#ifndef IN_PROCESS_AGENT
219f2f23
PA
1249static void collect_data_at_step (struct tracepoint_hit_ctx *ctx,
1250 CORE_ADDR stop_pc,
1251 struct tracepoint *tpoint, int current_step);
6a271cae
PA
1252static void compile_tracepoint_condition (struct tracepoint *tpoint,
1253 CORE_ADDR *jump_entry);
fa593d66 1254#endif
219f2f23
PA
1255static void do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
1256 CORE_ADDR stop_pc,
1257 struct tracepoint *tpoint,
1258 struct traceframe *tframe,
1259 struct tracepoint_action *taction);
1260
fa593d66
PA
1261#ifndef IN_PROCESS_AGENT
1262static struct tracepoint *fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR);
1e4d1764
YQ
1263
1264static void install_tracepoint (struct tracepoint *, char *own_buf);
1265static void download_tracepoint (struct tracepoint *);
405f8e94 1266static int install_fast_tracepoint (struct tracepoint *, char *errbuf);
fa593d66
PA
1267#endif
1268
1269#if defined(__GNUC__)
1270# define memory_barrier() asm volatile ("" : : : "memory")
1271#else
1272# define memory_barrier() do {} while (0)
1273#endif
1274
1275/* We only build the IPA if this builtin is supported, and there are
1276 no uses of this in GDBserver itself, so we're safe in defining this
1277 unconditionally. */
1278#define cmpxchg(mem, oldval, newval) \
1279 __sync_val_compare_and_swap (mem, oldval, newval)
1280
0fb4aa4b
PA
1281/* The size in bytes of the buffer used to talk to the IPA helper
1282 thread. */
1283#define CMD_BUF_SIZE 1024
1284
219f2f23
PA
1285/* Record that an error occurred during expression evaluation. */
1286
1287static void
1288record_tracepoint_error (struct tracepoint *tpoint, const char *which,
1289 enum eval_result_type rtype)
1290{
1291 trace_debug ("Tracepoint %d at %s %s eval reports error %d",
1292 tpoint->number, paddress (tpoint->address), which, rtype);
1293
fa593d66
PA
1294#ifdef IN_PROCESS_AGENT
1295 /* Only record the first error we get. */
1296 if (cmpxchg (&expr_eval_result,
1297 expr_eval_no_error,
1298 rtype) != expr_eval_no_error)
1299 return;
1300#else
1301 if (expr_eval_result != expr_eval_no_error)
1302 return;
1303#endif
1304
219f2f23
PA
1305 error_tracepoint = tpoint;
1306}
1307
1308/* Trace buffer management. */
1309
1310static void
1311clear_trace_buffer (void)
1312{
1313 trace_buffer_start = trace_buffer_lo;
1314 trace_buffer_free = trace_buffer_lo;
1315 trace_buffer_end_free = trace_buffer_hi;
1316 trace_buffer_wrap = trace_buffer_hi;
1317 /* A traceframe with zeroed fields marks the end of trace data. */
1318 ((struct traceframe *) trace_buffer_free)->tpnum = 0;
1319 ((struct traceframe *) trace_buffer_free)->data_size = 0;
1320 traceframe_read_count = traceframe_write_count = 0;
1321 traceframes_created = 0;
1322}
1323
fa593d66
PA
1324#ifndef IN_PROCESS_AGENT
1325
1326static void
1327clear_inferior_trace_buffer (void)
1328{
1329 CORE_ADDR ipa_trace_buffer_lo;
1330 CORE_ADDR ipa_trace_buffer_hi;
1331 struct traceframe ipa_traceframe = { 0 };
1332 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
1333
1334 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
1335 &ipa_trace_buffer_lo);
1336 read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
1337 &ipa_trace_buffer_hi);
1338
1339 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
1340 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
1341 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
1342 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
1343
1344 /* A traceframe with zeroed fields marks the end of trace data. */
1345 write_inferior_memory (ipa_sym_addrs.addr_trace_buffer_ctrl,
1346 (unsigned char *) &ipa_trace_buffer_ctrl,
1347 sizeof (ipa_trace_buffer_ctrl));
1348
1349 write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr, 0);
1350
1351 /* A traceframe with zeroed fields marks the end of trace data. */
1352 write_inferior_memory (ipa_trace_buffer_lo,
1353 (unsigned char *) &ipa_traceframe,
1354 sizeof (ipa_traceframe));
1355
1356 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count, 0);
1357 write_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count, 0);
1358 write_inferior_integer (ipa_sym_addrs.addr_traceframes_created, 0);
1359}
1360
1361#endif
1362
219f2f23
PA
1363static void
1364init_trace_buffer (unsigned char *buf, int bufsize)
1365{
1366 trace_buffer_lo = buf;
1367 trace_buffer_hi = trace_buffer_lo + bufsize;
1368
1369 clear_trace_buffer ();
1370}
1371
fa593d66
PA
1372#ifdef IN_PROCESS_AGENT
1373
1374IP_AGENT_EXPORT void ATTR_USED ATTR_NOINLINE
1375about_to_request_buffer_space (void)
1376{
1377 /* GDBserver places breakpoint here while it goes about to flush
1378 data at random times. */
1379 UNKNOWN_SIDE_EFFECTS();
1380}
1381
1382#endif
1383
219f2f23
PA
1384/* Carve out a piece of the trace buffer, returning NULL in case of
1385 failure. */
1386
1387static void *
1388trace_buffer_alloc (size_t amt)
1389{
1390 unsigned char *rslt;
fa593d66
PA
1391 struct trace_buffer_control *tbctrl;
1392 unsigned int curr;
1393#ifdef IN_PROCESS_AGENT
1394 unsigned int prev, prev_filtered;
1395 unsigned int commit_count;
1396 unsigned int commit;
1397 unsigned int readout;
1398#else
219f2f23
PA
1399 struct traceframe *oldest;
1400 unsigned char *new_start;
fa593d66 1401#endif
219f2f23
PA
1402
1403 trace_debug ("Want to allocate %ld+%ld bytes in trace buffer",
1404 (long) amt, (long) sizeof (struct traceframe));
1405
1406 /* Account for the EOB marker. */
1407 amt += sizeof (struct traceframe);
1408
fa593d66
PA
1409#ifdef IN_PROCESS_AGENT
1410 again:
1411 memory_barrier ();
1412
1413 /* Read the current token and extract the index to try to write to,
1414 storing it in CURR. */
1415 prev = trace_buffer_ctrl_curr;
1416 prev_filtered = prev & ~GDBSERVER_FLUSH_COUNT_MASK;
1417 curr = prev_filtered + 1;
1418 if (curr > 2)
1419 curr = 0;
1420
1421 about_to_request_buffer_space ();
1422
1423 /* Start out with a copy of the current state. GDBserver may be
1424 midway writing to the PREV_FILTERED TBC, but, that's OK, we won't
1425 be able to commit anyway if that happens. */
1426 trace_buffer_ctrl[curr]
1427 = trace_buffer_ctrl[prev_filtered];
1428 trace_debug ("trying curr=%u", curr);
1429#else
1430 /* The GDBserver's agent doesn't need all that syncing, and always
1431 updates TCB 0 (there's only one, mind you). */
1432 curr = 0;
1433#endif
1434 tbctrl = &trace_buffer_ctrl[curr];
1435
219f2f23
PA
1436 /* Offsets are easier to grok for debugging than raw addresses,
1437 especially for the small trace buffer sizes that are useful for
1438 testing. */
fa593d66
PA
1439 trace_debug ("Trace buffer [%d] start=%d free=%d endfree=%d wrap=%d hi=%d",
1440 curr,
1441 (int) (tbctrl->start - trace_buffer_lo),
1442 (int) (tbctrl->free - trace_buffer_lo),
1443 (int) (tbctrl->end_free - trace_buffer_lo),
1444 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1445 (int) (trace_buffer_hi - trace_buffer_lo));
1446
1447 /* The algorithm here is to keep trying to get a contiguous block of
1448 the requested size, possibly discarding older traceframes to free
1449 up space. Since free space might come in one or two pieces,
1450 depending on whether discarded traceframes wrapped around at the
1451 high end of the buffer, we test both pieces after each
1452 discard. */
1453 while (1)
1454 {
1455 /* First, if we have two free parts, try the upper one first. */
fa593d66 1456 if (tbctrl->end_free < tbctrl->free)
219f2f23 1457 {
fa593d66 1458 if (tbctrl->free + amt <= trace_buffer_hi)
219f2f23
PA
1459 /* We have enough in the upper part. */
1460 break;
1461 else
1462 {
1463 /* Our high part of free space wasn't enough. Give up
1464 on it for now, set wraparound. We will recover the
1465 space later, if/when the wrapped-around traceframe is
1466 discarded. */
1467 trace_debug ("Upper part too small, setting wraparound");
fa593d66
PA
1468 tbctrl->wrap = tbctrl->free;
1469 tbctrl->free = trace_buffer_lo;
219f2f23
PA
1470 }
1471 }
1472
1473 /* The normal case. */
fa593d66 1474 if (tbctrl->free + amt <= tbctrl->end_free)
219f2f23
PA
1475 break;
1476
fa593d66
PA
1477#ifdef IN_PROCESS_AGENT
1478 /* The IP Agent's buffer is always circular. It isn't used
1479 currently, but `circular_trace_buffer' could represent
1480 GDBserver's mode. If we didn't find space, ask GDBserver to
1481 flush. */
1482
1483 flush_trace_buffer ();
1484 memory_barrier ();
1485 if (tracing)
1486 {
1487 trace_debug ("gdbserver flushed buffer, retrying");
1488 goto again;
1489 }
1490
1491 /* GDBserver cancelled the tracing. Bail out as well. */
1492 return NULL;
1493#else
219f2f23
PA
1494 /* If we're here, then neither part is big enough, and
1495 non-circular trace buffers are now full. */
1496 if (!circular_trace_buffer)
1497 {
1498 trace_debug ("Not enough space in the trace buffer");
1499 return NULL;
1500 }
1501
1502 trace_debug ("Need more space in the trace buffer");
1503
1504 /* If we have a circular buffer, we can try discarding the
1505 oldest traceframe and see if that helps. */
1506 oldest = FIRST_TRACEFRAME ();
1507 if (oldest->tpnum == 0)
1508 {
1509 /* Not good; we have no traceframes to free. Perhaps we're
1510 asking for a block that is larger than the buffer? In
1511 any case, give up. */
1512 trace_debug ("No traceframes to discard");
1513 return NULL;
1514 }
1515
fa593d66
PA
1516 /* We don't run this code in the in-process agent currently.
1517 E.g., we could leave the in-process agent in autonomous
1518 circular mode if we only have fast tracepoints. If we do
1519 that, then this bit becomes racy with GDBserver, which also
1520 writes to this counter. */
219f2f23
PA
1521 --traceframe_write_count;
1522
1523 new_start = (unsigned char *) NEXT_TRACEFRAME (oldest);
1524 /* If we freed the traceframe that wrapped around, go back
1525 to the non-wrap case. */
fa593d66 1526 if (new_start < tbctrl->start)
219f2f23
PA
1527 {
1528 trace_debug ("Discarding past the wraparound");
fa593d66 1529 tbctrl->wrap = trace_buffer_hi;
219f2f23 1530 }
fa593d66
PA
1531 tbctrl->start = new_start;
1532 tbctrl->end_free = tbctrl->start;
219f2f23
PA
1533
1534 trace_debug ("Discarded a traceframe\n"
fa593d66
PA
1535 "Trace buffer [%d], start=%d free=%d "
1536 "endfree=%d wrap=%d hi=%d",
1537 curr,
1538 (int) (tbctrl->start - trace_buffer_lo),
1539 (int) (tbctrl->free - trace_buffer_lo),
1540 (int) (tbctrl->end_free - trace_buffer_lo),
1541 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1542 (int) (trace_buffer_hi - trace_buffer_lo));
1543
1544 /* Now go back around the loop. The discard might have resulted
1545 in either one or two pieces of free space, so we want to try
1546 both before freeing any more traceframes. */
fa593d66 1547#endif
219f2f23
PA
1548 }
1549
1550 /* If we get here, we know we can provide the asked-for space. */
1551
fa593d66 1552 rslt = tbctrl->free;
219f2f23
PA
1553
1554 /* Adjust the request back down, now that we know we have space for
fa593d66
PA
1555 the marker, but don't commit to AMT yet, we may still need to
1556 restart the operation if GDBserver touches the trace buffer
1557 (obviously only important in the in-process agent's version). */
1558 tbctrl->free += (amt - sizeof (struct traceframe));
1559
1560 /* Or not. If GDBserver changed the trace buffer behind our back,
1561 we get to restart a new allocation attempt. */
1562
1563#ifdef IN_PROCESS_AGENT
1564 /* Build the tentative token. */
1565 commit_count = (((prev & 0x0007ff00) + 0x100) & 0x0007ff00);
1566 commit = (((prev & 0x0007ff00) << 12)
1567 | commit_count
1568 | curr);
1569
1570 /* Try to commit it. */
1571 readout = cmpxchg (&trace_buffer_ctrl_curr, prev, commit);
1572 if (readout != prev)
1573 {
1574 trace_debug ("GDBserver has touched the trace buffer, restarting."
1575 " (prev=%08x, commit=%08x, readout=%08x)",
1576 prev, commit, readout);
1577 goto again;
1578 }
219f2f23 1579
fa593d66
PA
1580 /* Hold your horses here. Even if that change was committed,
1581 GDBserver could come in, and clobber it. We need to hold to be
1582 able to tell if GDBserver clobbers before or after we committed
1583 the change. Whenever GDBserver goes about touching the IPA
1584 buffer, it sets a breakpoint in this routine, so we have a sync
1585 point here. */
1586 about_to_request_buffer_space ();
219f2f23 1587
fa593d66
PA
1588 /* Check if the change has been effective, even if GDBserver stopped
1589 us at the breakpoint. */
219f2f23 1590
fa593d66
PA
1591 {
1592 unsigned int refetch;
219f2f23 1593
fa593d66
PA
1594 memory_barrier ();
1595
1596 refetch = trace_buffer_ctrl_curr;
1597
1598 if ((refetch == commit
1599 || ((refetch & 0x7ff00000) >> 12) == commit_count))
1600 {
1601 /* effective */
1602 trace_debug ("change is effective: (prev=%08x, commit=%08x, "
1603 "readout=%08x, refetch=%08x)",
1604 prev, commit, readout, refetch);
1605 }
1606 else
1607 {
1608 trace_debug ("GDBserver has touched the trace buffer, not effective."
1609 " (prev=%08x, commit=%08x, readout=%08x, refetch=%08x)",
1610 prev, commit, readout, refetch);
1611 goto again;
1612 }
1613 }
1614#endif
1615
1616 /* We have a new piece of the trace buffer. Hurray! */
1617
1618 /* Add an EOB marker just past this allocation. */
1619 ((struct traceframe *) tbctrl->free)->tpnum = 0;
1620 ((struct traceframe *) tbctrl->free)->data_size = 0;
1621
1622 /* Adjust the request back down, now that we know we have space for
1623 the marker. */
1624 amt -= sizeof (struct traceframe);
1625
1626 if (debug_threads)
1627 {
219f2f23 1628 trace_debug ("Allocated %d bytes", (int) amt);
fa593d66
PA
1629 trace_debug ("Trace buffer [%d] start=%d free=%d "
1630 "endfree=%d wrap=%d hi=%d",
1631 curr,
1632 (int) (tbctrl->start - trace_buffer_lo),
1633 (int) (tbctrl->free - trace_buffer_lo),
1634 (int) (tbctrl->end_free - trace_buffer_lo),
1635 (int) (tbctrl->wrap - trace_buffer_lo),
219f2f23
PA
1636 (int) (trace_buffer_hi - trace_buffer_lo));
1637 }
1638
1639 return rslt;
1640}
1641
fa593d66
PA
1642#ifndef IN_PROCESS_AGENT
1643
219f2f23
PA
1644/* Return the total free space. This is not necessarily the largest
1645 block we can allocate, because of the two-part case. */
1646
1647static int
1648free_space (void)
1649{
1650 if (trace_buffer_free <= trace_buffer_end_free)
1651 return trace_buffer_end_free - trace_buffer_free;
1652 else
1653 return ((trace_buffer_end_free - trace_buffer_lo)
1654 + (trace_buffer_hi - trace_buffer_free));
1655}
1656
1657/* An 'S' in continuation packets indicates remainder are for
1658 while-stepping. */
1659
1660static int seen_step_action_flag;
1661
1e4d1764
YQ
1662/* Create a tracepoint (location) with given number and address. Add this
1663 new tracepoint to list and sort this list. */
219f2f23
PA
1664
1665static struct tracepoint *
1666add_tracepoint (int num, CORE_ADDR addr)
1667{
1e4d1764 1668 struct tracepoint *tpoint, **tp_next;
219f2f23
PA
1669
1670 tpoint = xmalloc (sizeof (struct tracepoint));
1671 tpoint->number = num;
1672 tpoint->address = addr;
1673 tpoint->numactions = 0;
1674 tpoint->actions = NULL;
1675 tpoint->actions_str = NULL;
1676 tpoint->cond = NULL;
1677 tpoint->num_step_actions = 0;
1678 tpoint->step_actions = NULL;
1679 tpoint->step_actions_str = NULL;
fa593d66
PA
1680 /* Start all off as regular (slow) tracepoints. */
1681 tpoint->type = trap_tracepoint;
1682 tpoint->orig_size = -1;
219f2f23 1683 tpoint->source_strings = NULL;
6a271cae 1684 tpoint->compiled_cond = 0;
219f2f23
PA
1685 tpoint->handle = NULL;
1686 tpoint->next = NULL;
1687
1e4d1764
YQ
1688 /* Find a place to insert this tracepoint into list in order to keep
1689 the tracepoint list still in the ascending order. There may be
1690 multiple tracepoints at the same address as TPOINT's, and this
1691 guarantees TPOINT is inserted after all the tracepoints which are
1692 set at the same address. For example, fast tracepoints A, B, C are
1693 set at the same address, and D is to be insert at the same place as
1694 well,
1695
1696 -->| A |--> | B |-->| C |->...
1697
1698 One jump pad was created for tracepoint A, B, and C, and the target
1699 address of A is referenced/used in jump pad. So jump pad will let
1700 inferior jump to A. If D is inserted in front of A, like this,
1701
1702 -->| D |-->| A |--> | B |-->| C |->...
1703
1704 without updating jump pad, D is not reachable during collect, which
1705 is wrong. As we can see, the order of B, C and D doesn't matter, but
1706 A should always be the `first' one. */
1707 for (tp_next = &tracepoints;
1708 (*tp_next) != NULL && (*tp_next)->address <= tpoint->address;
1709 tp_next = &(*tp_next)->next)
1710 ;
1711 tpoint->next = *tp_next;
1712 *tp_next = tpoint;
219f2f23
PA
1713 last_tracepoint = tpoint;
1714
1715 seen_step_action_flag = 0;
1716
1717 return tpoint;
1718}
1719
fa593d66
PA
1720#ifndef IN_PROCESS_AGENT
1721
219f2f23
PA
1722/* Return the tracepoint with the given number and address, or NULL. */
1723
1724static struct tracepoint *
1725find_tracepoint (int id, CORE_ADDR addr)
1726{
1727 struct tracepoint *tpoint;
1728
1729 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
1730 if (tpoint->number == id && tpoint->address == addr)
1731 return tpoint;
1732
1733 return NULL;
1734}
1735
1736/* There may be several tracepoints with the same number (because they
1737 are "locations", in GDB parlance); return the next one after the
1738 given tracepoint, or search from the beginning of the list if the
1739 first argument is NULL. */
1740
1741static struct tracepoint *
1742find_next_tracepoint_by_number (struct tracepoint *prev_tp, int num)
1743{
1744 struct tracepoint *tpoint;
1745
1746 if (prev_tp)
1747 tpoint = prev_tp->next;
1748 else
1749 tpoint = tracepoints;
1750 for (; tpoint; tpoint = tpoint->next)
1751 if (tpoint->number == num)
1752 return tpoint;
1753
1754 return NULL;
1755}
1756
fa593d66
PA
1757#endif
1758
219f2f23
PA
1759static char *
1760save_string (const char *str, size_t len)
1761{
1762 char *s;
1763
1764 s = xmalloc (len + 1);
1765 memcpy (s, str, len);
1766 s[len] = '\0';
1767
1768 return s;
1769}
1770
1771/* Append another action to perform when the tracepoint triggers. */
1772
1773static void
1774add_tracepoint_action (struct tracepoint *tpoint, char *packet)
1775{
1776 char *act;
1777
1778 if (*packet == 'S')
1779 {
1780 seen_step_action_flag = 1;
1781 ++packet;
1782 }
1783
1784 act = packet;
1785
1786 while (*act)
1787 {
1788 char *act_start = act;
1789 struct tracepoint_action *action = NULL;
1790
1791 switch (*act)
1792 {
1793 case 'M':
1794 {
1795 struct collect_memory_action *maction;
1796 ULONGEST basereg;
1797 int is_neg;
1798
1799 maction = xmalloc (sizeof *maction);
1800 maction->base.type = *act;
1801 action = &maction->base;
1802
1803 ++act;
1804 is_neg = (*act == '-');
1805 if (*act == '-')
1806 ++act;
1807 act = unpack_varlen_hex (act, &basereg);
1808 ++act;
1809 act = unpack_varlen_hex (act, &maction->addr);
1810 ++act;
1811 act = unpack_varlen_hex (act, &maction->len);
1812 maction->basereg = (is_neg
1813 ? - (int) basereg
1814 : (int) basereg);
1815 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
1816 pulongest (maction->len),
1817 paddress (maction->addr), maction->basereg);
1818 break;
1819 }
1820 case 'R':
1821 {
1822 struct collect_registers_action *raction;
1823
1824 raction = xmalloc (sizeof *raction);
1825 raction->base.type = *act;
1826 action = &raction->base;
1827
1828 trace_debug ("Want to collect registers");
1829 ++act;
1830 /* skip past hex digits of mask for now */
1831 while (isxdigit(*act))
1832 ++act;
1833 break;
1834 }
0fb4aa4b
PA
1835 case 'L':
1836 {
1837 struct collect_static_trace_data_action *raction;
1838
1839 raction = xmalloc (sizeof *raction);
1840 raction->base.type = *act;
1841 action = &raction->base;
1842
1843 trace_debug ("Want to collect static trace data");
1844 ++act;
1845 break;
1846 }
219f2f23
PA
1847 case 'S':
1848 trace_debug ("Unexpected step action, ignoring");
1849 ++act;
1850 break;
1851 case 'X':
1852 {
1853 struct eval_expr_action *xaction;
1854
1855 xaction = xmalloc (sizeof (*xaction));
1856 xaction->base.type = *act;
1857 action = &xaction->base;
1858
1859 trace_debug ("Want to evaluate expression");
1860 xaction->expr = parse_agent_expr (&act);
1861 break;
1862 }
1863 default:
1864 trace_debug ("unknown trace action '%c', ignoring...", *act);
1865 break;
1866 case '-':
1867 break;
1868 }
1869
1870 if (action == NULL)
1871 break;
1872
1873 if (seen_step_action_flag)
1874 {
1875 tpoint->num_step_actions++;
1876
1877 tpoint->step_actions
1878 = xrealloc (tpoint->step_actions,
1879 (sizeof (*tpoint->step_actions)
1880 * tpoint->num_step_actions));
1881 tpoint->step_actions_str
1882 = xrealloc (tpoint->step_actions_str,
1883 (sizeof (*tpoint->step_actions_str)
1884 * tpoint->num_step_actions));
1885 tpoint->step_actions[tpoint->num_step_actions - 1] = action;
1886 tpoint->step_actions_str[tpoint->num_step_actions - 1]
1887 = save_string (act_start, act - act_start);
1888 }
1889 else
1890 {
1891 tpoint->numactions++;
1892 tpoint->actions
1893 = xrealloc (tpoint->actions,
1894 sizeof (*tpoint->actions) * tpoint->numactions);
1895 tpoint->actions_str
1896 = xrealloc (tpoint->actions_str,
1897 sizeof (*tpoint->actions_str) * tpoint->numactions);
1898 tpoint->actions[tpoint->numactions - 1] = action;
1899 tpoint->actions_str[tpoint->numactions - 1]
1900 = save_string (act_start, act - act_start);
1901 }
1902 }
1903}
1904
fa593d66
PA
1905#endif
1906
219f2f23
PA
1907/* Find or create a trace state variable with the given number. */
1908
1909static struct trace_state_variable *
1910get_trace_state_variable (int num)
1911{
1912 struct trace_state_variable *tsv;
1913
fa593d66
PA
1914#ifdef IN_PROCESS_AGENT
1915 /* Search for an existing variable. */
1916 for (tsv = alloced_trace_state_variables; tsv; tsv = tsv->next)
1917 if (tsv->number == num)
1918 return tsv;
1919#endif
1920
219f2f23
PA
1921 /* Search for an existing variable. */
1922 for (tsv = trace_state_variables; tsv; tsv = tsv->next)
1923 if (tsv->number == num)
1924 return tsv;
1925
1926 return NULL;
1927}
1928
1929/* Find or create a trace state variable with the given number. */
1930
1931static struct trace_state_variable *
fa593d66 1932create_trace_state_variable (int num, int gdb)
219f2f23
PA
1933{
1934 struct trace_state_variable *tsv;
1935
1936 tsv = get_trace_state_variable (num);
1937 if (tsv != NULL)
1938 return tsv;
1939
1940 /* Create a new variable. */
1941 tsv = xmalloc (sizeof (struct trace_state_variable));
1942 tsv->number = num;
1943 tsv->initial_value = 0;
1944 tsv->value = 0;
1945 tsv->getter = NULL;
1946 tsv->name = NULL;
fa593d66
PA
1947#ifdef IN_PROCESS_AGENT
1948 if (!gdb)
1949 {
1950 tsv->next = alloced_trace_state_variables;
1951 alloced_trace_state_variables = tsv;
1952 }
1953 else
1954#endif
1955 {
1956 tsv->next = trace_state_variables;
1957 trace_state_variables = tsv;
1958 }
219f2f23
PA
1959 return tsv;
1960}
1961
6a271cae 1962IP_AGENT_EXPORT LONGEST
219f2f23
PA
1963get_trace_state_variable_value (int num)
1964{
1965 struct trace_state_variable *tsv;
1966
1967 tsv = get_trace_state_variable (num);
1968
1969 if (!tsv)
1970 {
1971 trace_debug ("No trace state variable %d, skipping value get", num);
1972 return 0;
1973 }
1974
1975 /* Call a getter function if we have one. While it's tempting to
1976 set up something to only call the getter once per tracepoint hit,
1977 it could run afoul of thread races. Better to let the getter
1978 handle it directly, if necessary to worry about it. */
1979 if (tsv->getter)
1980 tsv->value = (tsv->getter) ();
1981
1982 trace_debug ("get_trace_state_variable_value(%d) ==> %s",
1983 num, plongest (tsv->value));
1984
1985 return tsv->value;
1986}
1987
6a271cae 1988IP_AGENT_EXPORT void
219f2f23
PA
1989set_trace_state_variable_value (int num, LONGEST val)
1990{
1991 struct trace_state_variable *tsv;
1992
1993 tsv = get_trace_state_variable (num);
1994
1995 if (!tsv)
1996 {
1997 trace_debug ("No trace state variable %d, skipping value set", num);
1998 return;
1999 }
2000
2001 tsv->value = val;
2002}
2003
2004static void
2005set_trace_state_variable_name (int num, const char *name)
2006{
2007 struct trace_state_variable *tsv;
2008
2009 tsv = get_trace_state_variable (num);
2010
2011 if (!tsv)
2012 {
2013 trace_debug ("No trace state variable %d, skipping name set", num);
2014 return;
2015 }
2016
2017 tsv->name = (char *) name;
2018}
2019
2020static void
2021set_trace_state_variable_getter (int num, LONGEST (*getter) (void))
2022{
2023 struct trace_state_variable *tsv;
2024
2025 tsv = get_trace_state_variable (num);
2026
2027 if (!tsv)
2028 {
2029 trace_debug ("No trace state variable %d, skipping getter set", num);
2030 return;
2031 }
2032
2033 tsv->getter = getter;
2034}
2035
2036/* Add a raw traceframe for the given tracepoint. */
2037
2038static struct traceframe *
2039add_traceframe (struct tracepoint *tpoint)
2040{
2041 struct traceframe *tframe;
2042
2043 tframe = trace_buffer_alloc (sizeof (struct traceframe));
2044
2045 if (tframe == NULL)
2046 return NULL;
2047
2048 tframe->tpnum = tpoint->number;
2049 tframe->data_size = 0;
2050
2051 return tframe;
2052}
2053
2054/* Add a block to the traceframe currently being worked on. */
2055
2056static unsigned char *
2057add_traceframe_block (struct traceframe *tframe, int amt)
2058{
2059 unsigned char *block;
2060
2061 if (!tframe)
2062 return NULL;
2063
2064 block = trace_buffer_alloc (amt);
2065
2066 if (!block)
2067 return NULL;
2068
2069 tframe->data_size += amt;
2070
2071 return block;
2072}
2073
2074/* Flag that the current traceframe is finished. */
2075
2076static void
2077finish_traceframe (struct traceframe *tframe)
2078{
2079 ++traceframe_write_count;
2080 ++traceframes_created;
2081}
2082
fa593d66
PA
2083#ifndef IN_PROCESS_AGENT
2084
219f2f23
PA
2085/* Given a traceframe number NUM, find the NUMth traceframe in the
2086 buffer. */
2087
2088static struct traceframe *
2089find_traceframe (int num)
2090{
2091 struct traceframe *tframe;
2092 int tfnum = 0;
2093
2094 for (tframe = FIRST_TRACEFRAME ();
2095 tframe->tpnum != 0;
2096 tframe = NEXT_TRACEFRAME (tframe))
2097 {
2098 if (tfnum == num)
2099 return tframe;
2100 ++tfnum;
2101 }
2102
2103 return NULL;
2104}
2105
2106static CORE_ADDR
2107get_traceframe_address (struct traceframe *tframe)
2108{
2109 CORE_ADDR addr;
2110 struct tracepoint *tpoint;
2111
2112 addr = traceframe_get_pc (tframe);
2113
2114 if (addr)
2115 return addr;
2116
2117 /* Fallback strategy, will be incorrect for while-stepping frames
2118 and multi-location tracepoints. */
2119 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
2120 return tpoint->address;
2121}
2122
2123/* Search for the next traceframe whose address is inside or outside
2124 the given range. */
2125
2126static struct traceframe *
2127find_next_traceframe_in_range (CORE_ADDR lo, CORE_ADDR hi, int inside_p,
2128 int *tfnump)
2129{
2130 struct traceframe *tframe;
2131 CORE_ADDR tfaddr;
2132
2133 *tfnump = current_traceframe + 1;
2134 tframe = find_traceframe (*tfnump);
2135 /* The search is not supposed to wrap around. */
2136 if (!tframe)
2137 {
2138 *tfnump = -1;
2139 return NULL;
2140 }
2141
2142 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2143 {
2144 tfaddr = get_traceframe_address (tframe);
2145 if (inside_p
2146 ? (lo <= tfaddr && tfaddr <= hi)
2147 : (lo > tfaddr || tfaddr > hi))
2148 return tframe;
2149 ++*tfnump;
2150 }
2151
2152 *tfnump = -1;
2153 return NULL;
2154}
2155
2156/* Search for the next traceframe recorded by the given tracepoint.
2157 Note that for multi-location tracepoints, this will find whatever
2158 location appears first. */
2159
2160static struct traceframe *
2161find_next_traceframe_by_tracepoint (int num, int *tfnump)
2162{
2163 struct traceframe *tframe;
2164
2165 *tfnump = current_traceframe + 1;
2166 tframe = find_traceframe (*tfnump);
2167 /* The search is not supposed to wrap around. */
2168 if (!tframe)
2169 {
2170 *tfnump = -1;
2171 return NULL;
2172 }
2173
2174 for (; tframe->tpnum != 0; tframe = NEXT_TRACEFRAME (tframe))
2175 {
2176 if (tframe->tpnum == num)
2177 return tframe;
2178 ++*tfnump;
2179 }
2180
2181 *tfnump = -1;
2182 return NULL;
2183}
2184
fa593d66
PA
2185#endif
2186
2187#ifndef IN_PROCESS_AGENT
2188
219f2f23
PA
2189/* Clear all past trace state. */
2190
2191static void
2192cmd_qtinit (char *packet)
2193{
2194 struct trace_state_variable *tsv, *prev, *next;
2195
2196 /* Make sure we don't try to read from a trace frame. */
2197 current_traceframe = -1;
2198
2199 trace_debug ("Initializing the trace");
2200
2201 clear_installed_tracepoints ();
2202 clear_readonly_regions ();
2203
2204 tracepoints = NULL;
2205 last_tracepoint = NULL;
2206
2207 /* Clear out any leftover trace state variables. Ones with target
2208 defined getters should be kept however. */
2209 prev = NULL;
2210 tsv = trace_state_variables;
2211 while (tsv)
2212 {
2213 trace_debug ("Looking at var %d", tsv->number);
2214 if (tsv->getter == NULL)
2215 {
2216 next = tsv->next;
2217 if (prev)
2218 prev->next = next;
2219 else
2220 trace_state_variables = next;
2221 trace_debug ("Deleting var %d", tsv->number);
2222 free (tsv);
2223 tsv = next;
2224 }
2225 else
2226 {
2227 prev = tsv;
2228 tsv = tsv->next;
2229 }
2230 }
2231
2232 clear_trace_buffer ();
fa593d66 2233 clear_inferior_trace_buffer ();
219f2f23
PA
2234
2235 write_ok (packet);
2236}
2237
0fb4aa4b
PA
2238/* Unprobe the UST marker at ADDRESS. */
2239
2240static void
2241unprobe_marker_at (CORE_ADDR address)
2242{
2243 char cmd[CMD_BUF_SIZE];
2244
2245 sprintf (cmd, "unprobe_marker_at:%s", paddress (address));
2246 run_inferior_command (cmd);
2247}
2248
219f2f23
PA
2249/* Restore the program to its pre-tracing state. This routine may be called
2250 in error situations, so it needs to be careful about only restoring
2251 from known-valid bits. */
2252
2253static void
2254clear_installed_tracepoints (void)
2255{
2256 struct tracepoint *tpoint;
2257 struct tracepoint *prev_stpoint;
2258
7984d532
PA
2259 pause_all (1);
2260 cancel_breakpoints ();
2261
219f2f23
PA
2262 prev_stpoint = NULL;
2263
2264 /* Restore any bytes overwritten by tracepoints. */
2265 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2266 {
219f2f23
PA
2267 /* Catch the case where we might try to remove a tracepoint that
2268 was never actually installed. */
2269 if (tpoint->handle == NULL)
2270 {
2271 trace_debug ("Tracepoint %d at 0x%s was "
2272 "never installed, nothing to clear",
2273 tpoint->number, paddress (tpoint->address));
2274 continue;
2275 }
2276
fa593d66
PA
2277 switch (tpoint->type)
2278 {
2279 case trap_tracepoint:
2280 delete_breakpoint (tpoint->handle);
2281 break;
2282 case fast_tracepoint:
2283 delete_fast_tracepoint_jump (tpoint->handle);
2284 break;
0fb4aa4b
PA
2285 case static_tracepoint:
2286 if (prev_stpoint != NULL
2287 && prev_stpoint->address == tpoint->address)
2288 /* Nothing to do. We already unprobed a tracepoint set at
2289 this marker address (and there can only be one probe
2290 per marker). */
2291 ;
2292 else
2293 {
2294 unprobe_marker_at (tpoint->address);
2295 prev_stpoint = tpoint;
2296 }
2297 break;
fa593d66
PA
2298 }
2299
219f2f23
PA
2300 tpoint->handle = NULL;
2301 }
7984d532
PA
2302
2303 unpause_all (1);
219f2f23
PA
2304}
2305
2306/* Parse a packet that defines a tracepoint. */
2307
2308static void
2309cmd_qtdp (char *own_buf)
2310{
2311 int tppacket;
1e4d1764
YQ
2312 /* Whether there is a trailing hyphen at the end of the QTDP packet. */
2313 int trail_hyphen = 0;
219f2f23
PA
2314 ULONGEST num;
2315 ULONGEST addr;
2316 ULONGEST count;
2317 struct tracepoint *tpoint;
2318 char *actparm;
2319 char *packet = own_buf;
2320
2321 packet += strlen ("QTDP:");
2322
2323 /* A hyphen at the beginning marks a packet specifying actions for a
2324 tracepoint already supplied. */
2325 tppacket = 1;
2326 if (*packet == '-')
2327 {
2328 tppacket = 0;
2329 ++packet;
2330 }
2331 packet = unpack_varlen_hex (packet, &num);
2332 ++packet; /* skip a colon */
2333 packet = unpack_varlen_hex (packet, &addr);
2334 ++packet; /* skip a colon */
2335
2336 /* See if we already have this tracepoint. */
2337 tpoint = find_tracepoint (num, addr);
2338
2339 if (tppacket)
2340 {
2341 /* Duplicate tracepoints are never allowed. */
2342 if (tpoint)
2343 {
2344 trace_debug ("Tracepoint error: tracepoint %d"
2345 " at 0x%s already exists",
2346 (int) num, paddress (addr));
2347 write_enn (own_buf);
2348 return;
2349 }
2350
2351 tpoint = add_tracepoint (num, addr);
2352
2353 tpoint->enabled = (*packet == 'E');
2354 ++packet; /* skip 'E' */
2355 ++packet; /* skip a colon */
2356 packet = unpack_varlen_hex (packet, &count);
2357 tpoint->step_count = count;
2358 ++packet; /* skip a colon */
2359 packet = unpack_varlen_hex (packet, &count);
2360 tpoint->pass_count = count;
2361 /* See if we have any of the additional optional fields. */
2362 while (*packet == ':')
2363 {
2364 ++packet;
fa593d66
PA
2365 if (*packet == 'F')
2366 {
2367 tpoint->type = fast_tracepoint;
2368 ++packet;
2369 packet = unpack_varlen_hex (packet, &count);
2370 tpoint->orig_size = count;
2371 }
0fb4aa4b
PA
2372 else if (*packet == 'S')
2373 {
2374 tpoint->type = static_tracepoint;
2375 ++packet;
2376 }
fa593d66 2377 else if (*packet == 'X')
219f2f23
PA
2378 {
2379 actparm = (char *) packet;
2380 tpoint->cond = parse_agent_expr (&actparm);
2381 packet = actparm;
2382 }
2383 else if (*packet == '-')
2384 break;
2385 else if (*packet == '\0')
2386 break;
2387 else
2388 trace_debug ("Unknown optional tracepoint field");
2389 }
2390 if (*packet == '-')
1e4d1764
YQ
2391 {
2392 trail_hyphen = 1;
2393 trace_debug ("Also has actions\n");
2394 }
219f2f23 2395
fa593d66 2396 trace_debug ("Defined %stracepoint %d at 0x%s, "
219f2f23 2397 "enabled %d step %ld pass %ld",
fa593d66
PA
2398 tpoint->type == fast_tracepoint ? "fast "
2399 : "",
2400 tpoint->number, paddress (tpoint->address), tpoint->enabled,
219f2f23
PA
2401 tpoint->step_count, tpoint->pass_count);
2402 }
2403 else if (tpoint)
2404 add_tracepoint_action (tpoint, packet);
2405 else
2406 {
2407 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2408 (int) num, paddress (addr));
2409 write_enn (own_buf);
2410 return;
2411 }
2412
1e4d1764
YQ
2413 /* Install tracepoint during tracing only once for each tracepoint location.
2414 For each tracepoint loc, GDB may send multiple QTDP packets, and we can
2415 determine the last QTDP packet for one tracepoint location by checking
2416 trailing hyphen in QTDP packet. */
2417 if (tracing && !trail_hyphen)
2418 {
2419 /* Pause all threads temporarily while we patch tracepoints. */
2420 pause_all (0);
2421
2422 /* download_tracepoint will update global `tracepoints'
2423 list, so it is unsafe to leave threads in jump pad. */
2424 stabilize_threads ();
2425
2426 /* Freeze threads. */
2427 pause_all (1);
2428
2429 download_tracepoint (tpoint);
2430 install_tracepoint (tpoint, own_buf);
2431
2432 unpause_all (1);
2433 return;
2434 }
2435
219f2f23
PA
2436 write_ok (own_buf);
2437}
2438
2439static void
2440cmd_qtdpsrc (char *own_buf)
2441{
2442 ULONGEST num, addr, start, slen;
2443 struct tracepoint *tpoint;
2444 char *packet = own_buf;
2445 char *saved, *srctype, *src;
2446 size_t nbytes;
2447 struct source_string *last, *newlast;
2448
2449 packet += strlen ("QTDPsrc:");
2450
2451 packet = unpack_varlen_hex (packet, &num);
2452 ++packet; /* skip a colon */
2453 packet = unpack_varlen_hex (packet, &addr);
2454 ++packet; /* skip a colon */
2455
2456 /* See if we already have this tracepoint. */
2457 tpoint = find_tracepoint (num, addr);
2458
2459 if (!tpoint)
2460 {
2461 trace_debug ("Tracepoint error: tracepoint %d at 0x%s not found",
2462 (int) num, paddress (addr));
2463 write_enn (own_buf);
2464 return;
2465 }
2466
2467 saved = packet;
2468 packet = strchr (packet, ':');
2469 srctype = xmalloc (packet - saved + 1);
2470 memcpy (srctype, saved, packet - saved);
2471 srctype[packet - saved] = '\0';
2472 ++packet;
2473 packet = unpack_varlen_hex (packet, &start);
2474 ++packet; /* skip a colon */
2475 packet = unpack_varlen_hex (packet, &slen);
2476 ++packet; /* skip a colon */
2477 src = xmalloc (slen + 1);
2478 nbytes = unhexify (src, packet, strlen (packet) / 2);
2479 src[nbytes] = '\0';
2480
2481 newlast = xmalloc (sizeof (struct source_string));
2482 newlast->type = srctype;
2483 newlast->str = src;
2484 newlast->next = NULL;
2485 /* Always add a source string to the end of the list;
2486 this keeps sequences of actions/commands in the right
2487 order. */
2488 if (tpoint->source_strings)
2489 {
2490 for (last = tpoint->source_strings; last->next; last = last->next)
2491 ;
2492 last->next = newlast;
2493 }
2494 else
2495 tpoint->source_strings = newlast;
2496
2497 write_ok (own_buf);
2498}
2499
2500static void
2501cmd_qtdv (char *own_buf)
2502{
2503 ULONGEST num, val, builtin;
2504 char *varname;
2505 size_t nbytes;
2506 struct trace_state_variable *tsv;
2507 char *packet = own_buf;
2508
2509 packet += strlen ("QTDV:");
2510
2511 packet = unpack_varlen_hex (packet, &num);
2512 ++packet; /* skip a colon */
2513 packet = unpack_varlen_hex (packet, &val);
2514 ++packet; /* skip a colon */
2515 packet = unpack_varlen_hex (packet, &builtin);
2516 ++packet; /* skip a colon */
2517
2518 nbytes = strlen (packet) / 2;
2519 varname = xmalloc (nbytes + 1);
2520 nbytes = unhexify (varname, packet, nbytes);
2521 varname[nbytes] = '\0';
2522
fa593d66 2523 tsv = create_trace_state_variable (num, 1);
219f2f23
PA
2524 tsv->initial_value = (LONGEST) val;
2525 tsv->name = varname;
2526
2527 set_trace_state_variable_value (num, (LONGEST) val);
2528
2529 write_ok (own_buf);
2530}
2531
d248b706
KY
2532static void
2533cmd_qtenable_disable (char *own_buf, int enable)
2534{
2535 char *packet = own_buf;
2536 ULONGEST num, addr;
2537 struct tracepoint *tp;
2538
2539 packet += strlen (enable ? "QTEnable:" : "QTDisable:");
2540 packet = unpack_varlen_hex (packet, &num);
2541 ++packet; /* skip a colon */
2542 packet = unpack_varlen_hex (packet, &addr);
2543
2544 tp = find_tracepoint (num, addr);
2545
2546 if (tp)
2547 {
2548 if ((enable && tp->enabled) || (!enable && !tp->enabled))
2549 {
2550 trace_debug ("Tracepoint %d at 0x%s is already %s",
2551 (int) num, paddress (addr),
2552 enable ? "enabled" : "disabled");
2553 write_ok (own_buf);
2554 return;
2555 }
2556
2557 trace_debug ("%s tracepoint %d at 0x%s",
2558 enable ? "Enabling" : "Disabling",
2559 (int) num, paddress (addr));
2560
2561 tp->enabled = enable;
2562
2563 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
2564 {
2565 int ret;
2566 int offset = offsetof (struct tracepoint, enabled);
2567 CORE_ADDR obj_addr = tp->obj_addr_on_target + offset;
2568
2569 ret = prepare_to_access_memory ();
2570 if (ret)
2571 {
2572 trace_debug ("Failed to temporarily stop inferior threads");
2573 write_enn (own_buf);
2574 return;
2575 }
2576
2577 ret = write_inferior_integer (obj_addr, enable);
2578 done_accessing_memory ();
2579
2580 if (ret)
2581 {
2582 trace_debug ("Cannot write enabled flag into "
2583 "inferior process memory");
2584 write_enn (own_buf);
2585 return;
2586 }
2587 }
2588
2589 write_ok (own_buf);
2590 }
2591 else
2592 {
2593 trace_debug ("Tracepoint %d at 0x%s not found",
2594 (int) num, paddress (addr));
2595 write_enn (own_buf);
2596 }
2597}
2598
219f2f23
PA
2599static void
2600cmd_qtv (char *own_buf)
2601{
2602 ULONGEST num;
2603 LONGEST val;
2604 int err;
2605 char *packet = own_buf;
2606
2607 packet += strlen ("qTV:");
f8f67713 2608 unpack_varlen_hex (packet, &num);
219f2f23
PA
2609
2610 if (current_traceframe >= 0)
2611 {
2612 err = traceframe_read_tsv ((int) num, &val);
2613 if (err)
2614 {
2615 strcpy (own_buf, "U");
2616 return;
2617 }
2618 }
2619 /* Only make tsv's be undefined before the first trace run. After a
2620 trace run is over, the user might want to see the last value of
2621 the tsv, and it might not be available in a traceframe. */
2622 else if (!tracing && strcmp (tracing_stop_reason, "tnotrun") == 0)
2623 {
2624 strcpy (own_buf, "U");
2625 return;
2626 }
2627 else
2628 val = get_trace_state_variable_value (num);
2629
2630 sprintf (own_buf, "V%s", phex_nz (val, 0));
2631}
2632
2633/* Clear out the list of readonly regions. */
2634
2635static void
2636clear_readonly_regions (void)
2637{
2638 struct readonly_region *roreg;
2639
2640 while (readonly_regions)
2641 {
2642 roreg = readonly_regions;
2643 readonly_regions = readonly_regions->next;
2644 free (roreg);
2645 }
2646}
2647
2648/* Parse the collection of address ranges whose contents GDB believes
2649 to be unchanging and so can be read directly from target memory
2650 even while looking at a traceframe. */
2651
2652static void
2653cmd_qtro (char *own_buf)
2654{
2655 ULONGEST start, end;
2656 struct readonly_region *roreg;
2657 char *packet = own_buf;
2658
2659 trace_debug ("Want to mark readonly regions");
2660
2661 clear_readonly_regions ();
2662
2663 packet += strlen ("QTro");
2664
2665 while (*packet == ':')
2666 {
2667 ++packet; /* skip a colon */
2668 packet = unpack_varlen_hex (packet, &start);
2669 ++packet; /* skip a comma */
2670 packet = unpack_varlen_hex (packet, &end);
2671 roreg = xmalloc (sizeof (struct readonly_region));
2672 roreg->start = start;
2673 roreg->end = end;
2674 roreg->next = readonly_regions;
2675 readonly_regions = roreg;
2676 trace_debug ("Added readonly region from 0x%s to 0x%s",
2677 paddress (roreg->start), paddress (roreg->end));
2678 }
2679
2680 write_ok (own_buf);
2681}
2682
2683/* Test to see if the given range is in our list of readonly ranges.
2684 We only test for being entirely within a range, GDB is not going to
2685 send a single memory packet that spans multiple regions. */
2686
2687int
2688in_readonly_region (CORE_ADDR addr, ULONGEST length)
2689{
2690 struct readonly_region *roreg;
2691
2692 for (roreg = readonly_regions; roreg; roreg = roreg->next)
2693 if (roreg->start <= addr && (addr + length - 1) <= roreg->end)
2694 return 1;
2695
2696 return 0;
2697}
2698
fa593d66
PA
2699/* The maximum size of a jump pad entry. */
2700static const int max_jump_pad_size = 0x100;
2701
2702static CORE_ADDR gdb_jump_pad_head;
2703
2704/* Return the address of the next free jump space. */
2705
2706static CORE_ADDR
2707get_jump_space_head (void)
2708{
2709 if (gdb_jump_pad_head == 0)
2710 {
2711 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
2712 &gdb_jump_pad_head))
2713 fatal ("error extracting jump_pad_buffer");
2714 }
2715
2716 return gdb_jump_pad_head;
2717}
2718
2719/* Reserve USED bytes from the jump space. */
2720
2721static void
2722claim_jump_space (ULONGEST used)
2723{
2724 trace_debug ("claim_jump_space reserves %s bytes at %s",
2725 pulongest (used), paddress (gdb_jump_pad_head));
2726 gdb_jump_pad_head += used;
2727}
2728
405f8e94
SS
2729static CORE_ADDR trampoline_buffer_head = 0;
2730static CORE_ADDR trampoline_buffer_tail;
2731
2732/* Reserve USED bytes from the trampoline buffer and return the
2733 address of the start of the reserved space in TRAMPOLINE. Returns
2734 non-zero if the space is successfully claimed. */
2735
2736int
2737claim_trampoline_space (ULONGEST used, CORE_ADDR *trampoline)
2738{
2739 if (!trampoline_buffer_head)
2740 {
2741 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
2742 &trampoline_buffer_tail))
2743 {
2744 fatal ("error extracting trampoline_buffer");
2745 return 0;
2746 }
2747
2748 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2749 &trampoline_buffer_head))
2750 {
2751 fatal ("error extracting trampoline_buffer_end");
2752 return 0;
2753 }
2754 }
2755
2756 /* Start claiming space from the top of the trampoline space. If
2757 the space is located at the bottom of the virtual address space,
2758 this reduces the possibility that corruption will occur if a null
2759 pointer is used to write to memory. */
2760 if (trampoline_buffer_head - trampoline_buffer_tail < used)
2761 {
2762 trace_debug ("claim_trampoline_space failed to reserve %s bytes",
2763 pulongest (used));
2764 return 0;
2765 }
2766
2767 trampoline_buffer_head -= used;
2768
2769 trace_debug ("claim_trampoline_space reserves %s bytes at %s",
2770 pulongest (used), paddress (trampoline_buffer_head));
2771
2772 *trampoline = trampoline_buffer_head;
2773 return 1;
2774}
2775
2776/* Returns non-zero if there is space allocated for use in trampolines
2777 for fast tracepoints. */
2778
2779int
2780have_fast_tracepoint_trampoline_buffer (char *buf)
2781{
2782 CORE_ADDR trampoline_end, errbuf;
2783
2784 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
2785 &trampoline_end))
2786 {
2787 fatal ("error extracting trampoline_buffer_end");
2788 return 0;
2789 }
2790
2791 if (buf)
2792 {
2793 buf[0] = '\0';
2794 strcpy (buf, "was claiming");
2795 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_error,
2796 &errbuf))
2797 {
2798 fatal ("error extracting errbuf");
2799 return 0;
2800 }
2801
2802 read_inferior_memory (errbuf, (unsigned char *) buf, 100);
2803 }
2804
2805 return trampoline_end != 0;
2806}
2807
0fb4aa4b
PA
2808/* Ask the IPA to probe the marker at ADDRESS. Returns -1 if running
2809 the command fails, or 0 otherwise. If the command ran
2810 successfully, but probing the marker failed, ERROUT will be filled
2811 with the error to reply to GDB, and -1 is also returned. This
2812 allows directly passing IPA errors to GDB. */
2813
2814static int
2815probe_marker_at (CORE_ADDR address, char *errout)
2816{
2817 char cmd[CMD_BUF_SIZE];
2818 int err;
2819
2820 sprintf (cmd, "probe_marker_at:%s", paddress (address));
2821 err = run_inferior_command (cmd);
2822
2823 if (err == 0)
2824 {
2825 if (*cmd == 'E')
2826 {
2827 strcpy (errout, cmd);
2828 return -1;
2829 }
2830 }
2831
2832 return err;
2833}
2834
219f2f23 2835static void
5c73ff4e 2836clone_fast_tracepoint (struct tracepoint *to, const struct tracepoint *from)
219f2f23 2837{
5c73ff4e
YQ
2838 to->jump_pad = from->jump_pad;
2839 to->jump_pad_end = from->jump_pad_end;
405f8e94
SS
2840 to->trampoline = from->trampoline;
2841 to->trampoline_end = from->trampoline_end;
5c73ff4e
YQ
2842 to->adjusted_insn_addr = from->adjusted_insn_addr;
2843 to->adjusted_insn_addr_end = from->adjusted_insn_addr_end;
2844 to->handle = from->handle;
fa593d66 2845
5c73ff4e
YQ
2846 gdb_assert (from->handle);
2847 inc_ref_fast_tracepoint_jump ((struct fast_tracepoint_jump *) from->handle);
2848}
2849
2850#define MAX_JUMP_SIZE 20
2851
2852/* Install fast tracepoint. Return 0 if successful, otherwise return
2853 non-zero. */
2854
2855static int
405f8e94 2856install_fast_tracepoint (struct tracepoint *tpoint, char *errbuf)
5c73ff4e
YQ
2857{
2858 CORE_ADDR jentry, jump_entry;
405f8e94
SS
2859 CORE_ADDR trampoline;
2860 ULONGEST trampoline_size;
5c73ff4e 2861 int err = 0;
fa593d66
PA
2862 /* The jump to the jump pad of the last fast tracepoint
2863 installed. */
2864 unsigned char fjump[MAX_JUMP_SIZE];
2865 ULONGEST fjump_size;
219f2f23 2866
405f8e94
SS
2867 if (tpoint->orig_size < target_get_min_fast_tracepoint_insn_len ())
2868 {
2869 trace_debug ("Requested a fast tracepoint on an instruction "
2870 "that is of less than the minimum length.");
2871 return 0;
2872 }
2873
5c73ff4e
YQ
2874 jentry = jump_entry = get_jump_space_head ();
2875
405f8e94
SS
2876 trampoline = 0;
2877 trampoline_size = 0;
2878
5c73ff4e
YQ
2879 /* Install the jump pad. */
2880 err = install_fast_tracepoint_jump_pad (tpoint->obj_addr_on_target,
2881 tpoint->address,
2882 ipa_sym_addrs.addr_gdb_collect,
2883 ipa_sym_addrs.addr_collecting,
2884 tpoint->orig_size,
405f8e94
SS
2885 &jentry,
2886 &trampoline, &trampoline_size,
2887 fjump, &fjump_size,
5c73ff4e 2888 &tpoint->adjusted_insn_addr,
405f8e94
SS
2889 &tpoint->adjusted_insn_addr_end,
2890 errbuf);
5c73ff4e
YQ
2891
2892 if (err)
2893 return 1;
2894
2895 /* Wire it in. */
2896 tpoint->handle = set_fast_tracepoint_jump (tpoint->address, fjump,
2897 fjump_size);
2898
2899 if (tpoint->handle != NULL)
2900 {
2901 tpoint->jump_pad = jump_entry;
2902 tpoint->jump_pad_end = jentry;
405f8e94
SS
2903 tpoint->trampoline = trampoline;
2904 tpoint->trampoline_end = trampoline + trampoline_size;
5c73ff4e
YQ
2905
2906 /* Pad to 8-byte alignment. */
2907 jentry = ((jentry + 7) & ~0x7);
2908 claim_jump_space (jentry - jump_entry);
2909 }
2910
2911 return 0;
2912}
2913
1e4d1764
YQ
2914
2915/* Install tracepoint TPOINT, and write reply message in OWN_BUF. */
2916
2917static void
2918install_tracepoint (struct tracepoint *tpoint, char *own_buf)
2919{
2920 tpoint->handle = NULL;
2921 *own_buf = '\0';
2922
2923 if (tpoint->type == trap_tracepoint)
2924 {
2925 /* Tracepoints are installed as memory breakpoints. Just go
2926 ahead and install the trap. The breakpoints module
2927 handles duplicated breakpoints, and the memory read
2928 routine handles un-patching traps from memory reads. */
2929 tpoint->handle = set_breakpoint_at (tpoint->address,
2930 tracepoint_handler);
2931 }
2932 else if (tpoint->type == fast_tracepoint || tpoint->type == static_tracepoint)
2933 {
2934 struct tracepoint *tp;
2935
2936 if (!in_process_agent_loaded ())
2937 {
2938 trace_debug ("Requested a %s tracepoint, but fast "
2939 "tracepoints aren't supported.",
2940 tpoint->type == static_tracepoint ? "static" : "fast");
2941 write_e_ipa_not_loaded (own_buf);
2942 return;
2943 }
2944 if (tpoint->type == static_tracepoint && !in_process_agent_loaded_ust ())
2945 {
2946 trace_debug ("Requested a static tracepoint, but static "
2947 "tracepoints are not supported.");
2948 write_e_ust_not_loaded (own_buf);
2949 return;
2950 }
2951
2952 /* Find another fast or static tracepoint at the same address. */
2953 for (tp = tracepoints; tp; tp = tp->next)
2954 {
2955 if (tp->address == tpoint->address && tp->type == tpoint->type
2956 && tp->number != tpoint->number)
2957 break;
2958 }
2959
2960 if (tpoint->type == fast_tracepoint)
2961 {
2962 if (tp) /* TPOINT is installed at the same address as TP. */
2963 clone_fast_tracepoint (tpoint, tp);
2964 else
405f8e94 2965 install_fast_tracepoint (tpoint, own_buf);
1e4d1764
YQ
2966 }
2967 else
2968 {
2969 if (tp)
2970 tpoint->handle = (void *) -1;
2971 else
2972 {
2973 if (probe_marker_at (tpoint->address, own_buf) == 0)
2974 tpoint->handle = (void *) -1;
2975 }
2976 }
2977
2978 }
2979 else
2980 internal_error (__FILE__, __LINE__, "Unknown tracepoint type");
2981
2982 if (tpoint->handle == NULL)
2983 {
2984 if (*own_buf == '\0')
2985 write_enn (own_buf);
2986 }
2987 else
2988 write_ok (own_buf);
2989}
2990
5c73ff4e
YQ
2991static void
2992cmd_qtstart (char *packet)
2993{
2994 struct tracepoint *tpoint, *prev_ftpoint, *prev_stpoint;
2995
219f2f23
PA
2996 trace_debug ("Starting the trace");
2997
7984d532 2998 /* Pause all threads temporarily while we patch tracepoints. */
fa593d66
PA
2999 pause_all (0);
3000
3001 /* Get threads out of jump pads. Safe to do here, since this is a
3002 top level command. And, required to do here, since we're
3003 deleting/rewriting jump pads. */
3004
3005 stabilize_threads ();
3006
3007 /* Freeze threads. */
7984d532
PA
3008 pause_all (1);
3009
fa593d66
PA
3010 /* Sync the fast tracepoints list in the inferior ftlib. */
3011 if (in_process_agent_loaded ())
3012 {
3013 download_tracepoints ();
3014 download_trace_state_variables ();
3015 }
3016
3017 /* No previous fast tpoint yet. */
3018 prev_ftpoint = NULL;
3019
0fb4aa4b
PA
3020 /* No previous static tpoint yet. */
3021 prev_stpoint = NULL;
3022
fa593d66
PA
3023 *packet = '\0';
3024
219f2f23
PA
3025 /* Install tracepoints. */
3026 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3027 {
3028 /* Ensure all the hit counts start at zero. */
3029 tpoint->hit_count = 0;
3030
fa593d66
PA
3031 if (tpoint->type == trap_tracepoint)
3032 {
fa593d66
PA
3033 /* Tracepoints are installed as memory breakpoints. Just go
3034 ahead and install the trap. The breakpoints module
3035 handles duplicated breakpoints, and the memory read
3036 routine handles un-patching traps from memory reads. */
3037 tpoint->handle = set_breakpoint_at (tpoint->address,
3038 tracepoint_handler);
3039 }
3040 else if (tpoint->type == fast_tracepoint)
3041 {
fa593d66
PA
3042 if (maybe_write_ipa_not_loaded (packet))
3043 {
3044 trace_debug ("Requested a fast tracepoint, but fast "
3045 "tracepoints aren't supported.");
3046 break;
3047 }
3048
3049 if (prev_ftpoint != NULL && prev_ftpoint->address == tpoint->address)
5c73ff4e 3050 clone_fast_tracepoint (tpoint, prev_ftpoint);
fa593d66
PA
3051 else
3052 {
405f8e94 3053 if (install_fast_tracepoint (tpoint, packet) == 0)
5c73ff4e 3054 prev_ftpoint = tpoint;
fa593d66
PA
3055 }
3056 }
0fb4aa4b
PA
3057 else if (tpoint->type == static_tracepoint)
3058 {
3059 if (maybe_write_ipa_ust_not_loaded (packet))
3060 {
3061 trace_debug ("Requested a static tracepoint, but static "
3062 "tracepoints are not supported.");
3063 break;
3064 }
3065
3066 /* Can only probe a given marker once. */
3067 if (prev_stpoint != NULL && prev_stpoint->address == tpoint->address)
3068 {
3069 tpoint->handle = (void *) -1;
3070 }
3071 else
3072 {
3073 if (probe_marker_at (tpoint->address, packet) == 0)
3074 {
3075 tpoint->handle = (void *) -1;
3076
3077 /* So that we can handle multiple static tracepoints
3078 at the same address easily. */
3079 prev_stpoint = tpoint;
3080 }
3081 }
3082 }
fa593d66
PA
3083
3084 /* Any failure in the inner loop is sufficient cause to give
3085 up. */
219f2f23
PA
3086 if (tpoint->handle == NULL)
3087 break;
3088 }
3089
3090 /* Any error in tracepoint insertion is unacceptable; better to
3091 address the problem now, than end up with a useless or misleading
3092 trace run. */
3093 if (tpoint != NULL)
3094 {
3095 clear_installed_tracepoints ();
3096 if (*packet == '\0')
3097 write_enn (packet);
7984d532 3098 unpause_all (1);
219f2f23
PA
3099 return;
3100 }
3101
3102 stopping_tracepoint = NULL;
3103 trace_buffer_is_full = 0;
3104 expr_eval_result = expr_eval_no_error;
3105 error_tracepoint = NULL;
3106
3107 /* Tracing is now active, hits will now start being logged. */
3108 tracing = 1;
3109
fa593d66
PA
3110 if (in_process_agent_loaded ())
3111 {
3112 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
3113 fatal ("Error setting tracing variable in lib");
3114
3115 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3116 0))
3117 fatal ("Error clearing stopping_tracepoint variable in lib");
3118
3119 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
3120 fatal ("Error clearing trace_buffer_is_full variable in lib");
3121
3122 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
3123 stop_tracing_handler);
3124 if (stop_tracing_bkpt == NULL)
3125 error ("Error setting stop_tracing breakpoint");
3126
3127 flush_trace_buffer_bkpt
3128 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
3129 flush_trace_buffer_handler);
3130 if (flush_trace_buffer_bkpt == NULL)
3131 error ("Error setting flush_trace_buffer breakpoint");
3132 }
3133
7984d532
PA
3134 unpause_all (1);
3135
219f2f23
PA
3136 write_ok (packet);
3137}
3138
3139/* End a tracing run, filling in a stop reason to report back to GDB,
3140 and removing the tracepoints from the code. */
3141
8336d594 3142void
219f2f23
PA
3143stop_tracing (void)
3144{
3145 if (!tracing)
3146 {
3147 trace_debug ("Tracing is already off, ignoring");
3148 return;
3149 }
3150
3151 trace_debug ("Stopping the trace");
3152
fa593d66
PA
3153 /* Pause all threads before removing fast jumps from memory,
3154 breakpoints, and touching IPA state variables (inferior memory).
3155 Some thread may hit the internal tracing breakpoints, or be
3156 collecting this moment, but that's ok, we don't release the
3157 tpoint object's memory or the jump pads here (we only do that
3158 when we're sure we can move all threads out of the jump pads).
3159 We can't now, since we may be getting here due to the inferior
3160 agent calling us. */
7984d532
PA
3161 pause_all (1);
3162 /* Since we're removing breakpoints, cancel breakpoint hits,
3163 possibly related to the breakpoints we're about to delete. */
3164 cancel_breakpoints ();
3165
219f2f23
PA
3166 /* Stop logging. Tracepoints can still be hit, but they will not be
3167 recorded. */
3168 tracing = 0;
fa593d66
PA
3169 if (in_process_agent_loaded ())
3170 {
3171 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
3172 fatal ("Error clearing tracing variable in lib");
3173 }
219f2f23
PA
3174
3175 tracing_stop_reason = "t???";
3176 tracing_stop_tpnum = 0;
3177 if (stopping_tracepoint)
3178 {
3179 trace_debug ("Stopping the trace because "
3180 "tracepoint %d was hit %ld times",
3181 stopping_tracepoint->number,
3182 stopping_tracepoint->pass_count);
3183 tracing_stop_reason = "tpasscount";
3184 tracing_stop_tpnum = stopping_tracepoint->number;
3185 }
3186 else if (trace_buffer_is_full)
3187 {
3188 trace_debug ("Stopping the trace because the trace buffer is full");
3189 tracing_stop_reason = "tfull";
3190 }
3191 else if (expr_eval_result != expr_eval_no_error)
3192 {
3193 trace_debug ("Stopping the trace because of an expression eval error");
3194 tracing_stop_reason = eval_result_names[expr_eval_result];
3195 tracing_stop_tpnum = error_tracepoint->number;
3196 }
fa593d66 3197#ifndef IN_PROCESS_AGENT
8336d594
PA
3198 else if (!gdb_connected ())
3199 {
3200 trace_debug ("Stopping the trace because GDB disconnected");
3201 tracing_stop_reason = "tdisconnected";
3202 }
fa593d66 3203#endif
219f2f23
PA
3204 else
3205 {
3206 trace_debug ("Stopping the trace because of a tstop command");
3207 tracing_stop_reason = "tstop";
3208 }
3209
3210 stopping_tracepoint = NULL;
3211 error_tracepoint = NULL;
3212
3213 /* Clear out the tracepoints. */
3214 clear_installed_tracepoints ();
7984d532 3215
fa593d66
PA
3216 if (in_process_agent_loaded ())
3217 {
3218 /* Pull in fast tracepoint trace frames from the inferior lib
3219 buffer into our buffer, even if our buffer is already full,
3220 because we want to present the full number of created frames
3221 in addition to what fit in the trace buffer. */
3222 upload_fast_traceframes ();
3223 }
3224
3225 if (stop_tracing_bkpt != NULL)
3226 {
3227 delete_breakpoint (stop_tracing_bkpt);
3228 stop_tracing_bkpt = NULL;
3229 }
3230
3231 if (flush_trace_buffer_bkpt != NULL)
3232 {
3233 delete_breakpoint (flush_trace_buffer_bkpt);
3234 flush_trace_buffer_bkpt = NULL;
3235 }
3236
7984d532 3237 unpause_all (1);
219f2f23
PA
3238}
3239
fa593d66
PA
3240static int
3241stop_tracing_handler (CORE_ADDR addr)
3242{
3243 trace_debug ("lib hit stop_tracing");
3244
3245 /* Don't actually handle it here. When we stop tracing we remove
3246 breakpoints from the inferior, and that is not allowed in a
3247 breakpoint handler (as the caller is walking the breakpoint
3248 list). */
3249 return 0;
3250}
3251
3252static int
3253flush_trace_buffer_handler (CORE_ADDR addr)
3254{
3255 trace_debug ("lib hit flush_trace_buffer");
3256 return 0;
3257}
3258
219f2f23
PA
3259static void
3260cmd_qtstop (char *packet)
3261{
3262 stop_tracing ();
3263 write_ok (packet);
3264}
3265
8336d594
PA
3266static void
3267cmd_qtdisconnected (char *own_buf)
3268{
3269 ULONGEST setting;
3270 char *packet = own_buf;
3271
3272 packet += strlen ("QTDisconnected:");
3273
3274 unpack_varlen_hex (packet, &setting);
3275
3276 write_ok (own_buf);
3277
3278 disconnected_tracing = setting;
3279}
3280
219f2f23
PA
3281static void
3282cmd_qtframe (char *own_buf)
3283{
3284 ULONGEST frame, pc, lo, hi, num;
3285 int tfnum, tpnum;
3286 struct traceframe *tframe;
3287 char *packet = own_buf;
3288
3289 packet += strlen ("QTFrame:");
3290
3291 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3292 {
3293 packet += strlen ("pc:");
f8f67713 3294 unpack_varlen_hex (packet, &pc);
219f2f23
PA
3295 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3296 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3297 }
3298 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3299 {
3300 packet += strlen ("range:");
3301 packet = unpack_varlen_hex (packet, &lo);
3302 ++packet;
f8f67713 3303 unpack_varlen_hex (packet, &hi);
219f2f23
PA
3304 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3305 paddress (lo), paddress (hi));
3306 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3307 }
3308 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3309 {
3310 packet += strlen ("outside:");
3311 packet = unpack_varlen_hex (packet, &lo);
3312 ++packet;
f8f67713 3313 unpack_varlen_hex (packet, &hi);
219f2f23
PA
3314 trace_debug ("Want to find next traceframe "
3315 "outside the range 0x%s to 0x%s",
3316 paddress (lo), paddress (hi));
3317 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3318 }
3319 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3320 {
3321 packet += strlen ("tdp:");
f8f67713 3322 unpack_varlen_hex (packet, &num);
219f2f23
PA
3323 tpnum = (int) num;
3324 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3325 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3326 }
3327 else
3328 {
3329 unpack_varlen_hex (packet, &frame);
3330 tfnum = (int) frame;
3331 if (tfnum == -1)
3332 {
3333 trace_debug ("Want to stop looking at traceframes");
3334 current_traceframe = -1;
3335 write_ok (own_buf);
3336 return;
3337 }
3338 trace_debug ("Want to look at traceframe %d", tfnum);
3339 tframe = find_traceframe (tfnum);
3340 }
3341
3342 if (tframe)
3343 {
3344 current_traceframe = tfnum;
3345 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3346 }
3347 else
3348 sprintf (own_buf, "F-1");
3349}
3350
3351static void
3352cmd_qtstatus (char *packet)
3353{
3354 char *stop_reason_rsp = NULL;
3355
3356 trace_debug ("Returning trace status as %d, stop reason %s",
3357 tracing, tracing_stop_reason);
3358
fa593d66
PA
3359 if (in_process_agent_loaded ())
3360 {
3361 pause_all (1);
3362
3363 upload_fast_traceframes ();
3364
3365 unpause_all (1);
3366 }
3367
219f2f23
PA
3368 stop_reason_rsp = (char *) tracing_stop_reason;
3369
3370 /* The user visible error string in terror needs to be hex encoded.
3371 We leave it as plain string in `tracepoint_stop_reason' to ease
3372 debugging. */
3373 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3374 {
3375 const char *result_name;
3376 int hexstr_len;
3377 char *p;
3378
3379 result_name = stop_reason_rsp + strlen ("terror:");
3380 hexstr_len = strlen (result_name) * 2;
3381 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3382 strcpy (p, "terror:");
3383 p += strlen (p);
3384 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3385 }
3386
8336d594
PA
3387 sprintf (packet,
3388 "T%d;"
3389 "%s:%x;"
3390 "tframes:%x;tcreated:%x;"
3391 "tfree:%x;tsize:%s;"
3392 "circular:%d;"
3393 "disconn:%d",
623ccd72 3394 tracing ? 1 : 0,
219f2f23
PA
3395 stop_reason_rsp, tracing_stop_tpnum,
3396 traceframe_count, traceframes_created,
8336d594
PA
3397 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3398 circular_trace_buffer,
3399 disconnected_tracing);
219f2f23
PA
3400}
3401
3402/* State variables to help return all the tracepoint bits. */
3403static struct tracepoint *cur_tpoint;
3404static int cur_action;
3405static int cur_step_action;
3406static struct source_string *cur_source_string;
3407static struct trace_state_variable *cur_tsv;
3408
3409/* Compose a response that is an imitation of the syntax by which the
3410 tracepoint was originally downloaded. */
3411
3412static void
3413response_tracepoint (char *packet, struct tracepoint *tpoint)
3414{
3415 char *buf;
3416
3417 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
3418 paddress (tpoint->address),
3419 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3420 tpoint->pass_count);
fa593d66
PA
3421 if (tpoint->type == fast_tracepoint)
3422 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
0fb4aa4b
PA
3423 else if (tpoint->type == static_tracepoint)
3424 sprintf (packet + strlen (packet), ":S");
219f2f23
PA
3425
3426 if (tpoint->cond)
3427 {
3428 buf = unparse_agent_expr (tpoint->cond);
3429 sprintf (packet + strlen (packet), ":X%x,%s",
3430 tpoint->cond->length, buf);
3431 free (buf);
3432 }
3433}
3434
3435/* Compose a response that is an imitation of the syntax by which the
3436 tracepoint action was originally downloaded (with the difference
3437 that due to the way we store the actions, this will output a packet
3438 per action, while GDB could have combined more than one action
3439 per-packet. */
3440
3441static void
3442response_action (char *packet, struct tracepoint *tpoint,
3443 char *taction, int step)
3444{
3445 sprintf (packet, "%c%x:%s:%s",
3446 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3447 taction);
3448}
3449
3450/* Compose a response that is an imitation of the syntax by which the
3451 tracepoint source piece was originally downloaded. */
3452
3453static void
3454response_source (char *packet,
3455 struct tracepoint *tpoint, struct source_string *src)
3456{
3457 char *buf;
3458 int len;
3459
3460 len = strlen (src->str);
3461 buf = alloca (len * 2 + 1);
3462 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3463
3464 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3465 tpoint->number, paddress (tpoint->address),
3466 src->type, 0, len, buf);
3467}
3468
3469/* Return the first piece of tracepoint definition, and initialize the
3470 state machine that will iterate through all the tracepoint
3471 bits. */
3472
3473static void
3474cmd_qtfp (char *packet)
3475{
3476 trace_debug ("Returning first tracepoint definition piece");
3477
3478 cur_tpoint = tracepoints;
3479 cur_action = cur_step_action = -1;
3480 cur_source_string = NULL;
3481
3482 if (cur_tpoint)
3483 response_tracepoint (packet, cur_tpoint);
3484 else
3485 strcpy (packet, "l");
3486}
3487
3488/* Return additional pieces of tracepoint definition. Each action and
3489 stepping action must go into its own packet, because of packet size
3490 limits, and so we use state variables to deliver one piece at a
3491 time. */
3492
3493static void
3494cmd_qtsp (char *packet)
3495{
3496 trace_debug ("Returning subsequent tracepoint definition piece");
3497
3498 if (!cur_tpoint)
3499 {
3500 /* This case would normally never occur, but be prepared for
3501 GDB misbehavior. */
3502 strcpy (packet, "l");
3503 }
3504 else if (cur_action < cur_tpoint->numactions - 1)
3505 {
3506 ++cur_action;
3507 response_action (packet, cur_tpoint,
3508 cur_tpoint->actions_str[cur_action], 0);
3509 }
3510 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3511 {
3512 ++cur_step_action;
3513 response_action (packet, cur_tpoint,
3514 cur_tpoint->step_actions_str[cur_step_action], 1);
3515 }
3516 else if ((cur_source_string
3517 ? cur_source_string->next
3518 : cur_tpoint->source_strings))
3519 {
3520 if (cur_source_string)
3521 cur_source_string = cur_source_string->next;
3522 else
3523 cur_source_string = cur_tpoint->source_strings;
3524 response_source (packet, cur_tpoint, cur_source_string);
3525 }
3526 else
3527 {
3528 cur_tpoint = cur_tpoint->next;
3529 cur_action = cur_step_action = -1;
3530 cur_source_string = NULL;
3531 if (cur_tpoint)
3532 response_tracepoint (packet, cur_tpoint);
3533 else
3534 strcpy (packet, "l");
3535 }
3536}
3537
3538/* Compose a response that is an imitation of the syntax by which the
3539 trace state variable was originally downloaded. */
3540
3541static void
3542response_tsv (char *packet, struct trace_state_variable *tsv)
3543{
3544 char *buf = (char *) "";
3545 int namelen;
3546
3547 if (tsv->name)
3548 {
3549 namelen = strlen (tsv->name);
3550 buf = alloca (namelen * 2 + 1);
3551 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3552 }
3553
3554 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3555 tsv->getter ? 1 : 0, buf);
3556}
3557
3558/* Return the first trace state variable definition, and initialize
3559 the state machine that will iterate through all the tsv bits. */
3560
3561static void
3562cmd_qtfv (char *packet)
3563{
3564 trace_debug ("Returning first trace state variable definition");
3565
3566 cur_tsv = trace_state_variables;
3567
3568 if (cur_tsv)
3569 response_tsv (packet, cur_tsv);
3570 else
3571 strcpy (packet, "l");
3572}
3573
3574/* Return additional trace state variable definitions. */
3575
3576static void
3577cmd_qtsv (char *packet)
3578{
3579 trace_debug ("Returning first trace state variable definition");
3580
3581 if (!cur_tpoint)
3582 {
3583 /* This case would normally never occur, but be prepared for
3584 GDB misbehavior. */
3585 strcpy (packet, "l");
3586 }
3587 else if (cur_tsv)
3588 {
3589 cur_tsv = cur_tsv->next;
3590 if (cur_tsv)
3591 response_tsv (packet, cur_tsv);
3592 else
3593 strcpy (packet, "l");
3594 }
3595 else
3596 strcpy (packet, "l");
3597}
3598
0fb4aa4b
PA
3599/* Return the first static tracepoint marker, and initialize the state
3600 machine that will iterate through all the static tracepoints
3601 markers. */
3602
3603static void
3604cmd_qtfstm (char *packet)
3605{
3606 if (!maybe_write_ipa_ust_not_loaded (packet))
3607 run_inferior_command (packet);
3608}
3609
3610/* Return additional static tracepoints markers. */
3611
3612static void
3613cmd_qtsstm (char *packet)
3614{
3615 if (!maybe_write_ipa_ust_not_loaded (packet))
3616 run_inferior_command (packet);
3617}
3618
3619/* Return the definition of the static tracepoint at a given address.
3620 Result packet is the same as qTsST's. */
3621
3622static void
3623cmd_qtstmat (char *packet)
3624{
3625 if (!maybe_write_ipa_ust_not_loaded (packet))
3626 run_inferior_command (packet);
3627}
3628
405f8e94
SS
3629/* Return the minimum instruction size needed for fast tracepoints as a
3630 hexadecimal number. */
3631
3632static void
3633cmd_qtminftpilen (char *packet)
3634{
3635 sprintf (packet, "%x", target_get_min_fast_tracepoint_insn_len ());
3636}
3637
219f2f23
PA
3638/* Respond to qTBuffer packet with a block of raw data from the trace
3639 buffer. GDB may ask for a lot, but we are allowed to reply with
3640 only as much as will fit within packet limits or whatever. */
3641
3642static void
3643cmd_qtbuffer (char *own_buf)
3644{
3645 ULONGEST offset, num, tot;
3646 unsigned char *tbp;
3647 char *packet = own_buf;
3648
3649 packet += strlen ("qTBuffer:");
3650
3651 packet = unpack_varlen_hex (packet, &offset);
3652 ++packet; /* skip a comma */
f8f67713 3653 unpack_varlen_hex (packet, &num);
219f2f23
PA
3654
3655 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3656 (int) num, pulongest (offset));
3657
3658 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3659
3660 /* If we're right at the end, reply specially that we're done. */
3661 if (offset == tot)
3662 {
3663 strcpy (own_buf, "l");
3664 return;
3665 }
3666
3667 /* Object to any other out-of-bounds request. */
3668 if (offset > tot)
3669 {
3670 write_enn (own_buf);
3671 return;
3672 }
3673
3674 /* Compute the pointer corresponding to the given offset, accounting
3675 for wraparound. */
3676 tbp = trace_buffer_start + offset;
3677 if (tbp >= trace_buffer_wrap)
3678 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3679
3680 /* Trim to the remaining bytes if we're close to the end. */
3681 if (num > tot - offset)
3682 num = tot - offset;
3683
3684 /* Trim to available packet size. */
3685 if (num >= (PBUFSIZ - 16) / 2 )
3686 num = (PBUFSIZ - 16) / 2;
3687
3688 convert_int_to_ascii (tbp, own_buf, num);
3689 own_buf[num] = '\0';
3690}
3691
3692static void
3693cmd_bigqtbuffer (char *own_buf)
3694{
3695 ULONGEST val;
3696 char *packet = own_buf;
3697
3698 packet += strlen ("QTBuffer:");
3699
3700 if (strncmp ("circular:", packet, strlen ("circular:")) == 0)
3701 {
3702 packet += strlen ("circular:");
f8f67713 3703 unpack_varlen_hex (packet, &val);
219f2f23
PA
3704 circular_trace_buffer = val;
3705 trace_debug ("Trace buffer is now %s",
3706 circular_trace_buffer ? "circular" : "linear");
3707 write_ok (own_buf);
3708 }
3709 else
3710 write_enn (own_buf);
3711}
3712
3713int
3714handle_tracepoint_general_set (char *packet)
3715{
3716 if (strcmp ("QTinit", packet) == 0)
3717 {
3718 cmd_qtinit (packet);
3719 return 1;
3720 }
3721 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3722 {
3723 cmd_qtdp (packet);
3724 return 1;
3725 }
3726 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3727 {
3728 cmd_qtdpsrc (packet);
3729 return 1;
3730 }
d248b706
KY
3731 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
3732 {
3733 cmd_qtenable_disable (packet, 1);
3734 return 1;
3735 }
3736 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
3737 {
3738 cmd_qtenable_disable (packet, 0);
3739 return 1;
3740 }
219f2f23
PA
3741 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3742 {
3743 cmd_qtdv (packet);
3744 return 1;
3745 }
3746 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3747 {
3748 cmd_qtro (packet);
3749 return 1;
3750 }
3751 else if (strcmp ("QTStart", packet) == 0)
3752 {
3753 cmd_qtstart (packet);
3754 return 1;
3755 }
3756 else if (strcmp ("QTStop", packet) == 0)
3757 {
3758 cmd_qtstop (packet);
3759 return 1;
3760 }
8336d594
PA
3761 else if (strncmp ("QTDisconnected:", packet,
3762 strlen ("QTDisconnected:")) == 0)
3763 {
3764 cmd_qtdisconnected (packet);
3765 return 1;
3766 }
219f2f23
PA
3767 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
3768 {
3769 cmd_qtframe (packet);
3770 return 1;
3771 }
3772 else if (strncmp ("QTBuffer:", packet, strlen ("QTBuffer:")) == 0)
3773 {
3774 cmd_bigqtbuffer (packet);
3775 return 1;
3776 }
3777
3778 return 0;
3779}
3780
3781int
3782handle_tracepoint_query (char *packet)
3783{
3784 if (strcmp ("qTStatus", packet) == 0)
3785 {
3786 cmd_qtstatus (packet);
3787 return 1;
3788 }
3789 else if (strcmp ("qTfP", packet) == 0)
3790 {
3791 cmd_qtfp (packet);
3792 return 1;
3793 }
3794 else if (strcmp ("qTsP", packet) == 0)
3795 {
3796 cmd_qtsp (packet);
3797 return 1;
3798 }
3799 else if (strcmp ("qTfV", packet) == 0)
3800 {
3801 cmd_qtfv (packet);
3802 return 1;
3803 }
3804 else if (strcmp ("qTsV", packet) == 0)
3805 {
3806 cmd_qtsv (packet);
3807 return 1;
3808 }
3809 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
3810 {
3811 cmd_qtv (packet);
3812 return 1;
3813 }
3814 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
3815 {
3816 cmd_qtbuffer (packet);
3817 return 1;
3818 }
0fb4aa4b
PA
3819 else if (strcmp ("qTfSTM", packet) == 0)
3820 {
3821 cmd_qtfstm (packet);
3822 return 1;
3823 }
3824 else if (strcmp ("qTsSTM", packet) == 0)
3825 {
3826 cmd_qtsstm (packet);
3827 return 1;
3828 }
3829 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
3830 {
3831 cmd_qtstmat (packet);
3832 return 1;
3833 }
405f8e94
SS
3834 else if (strcmp ("qTMinFTPILen", packet) == 0)
3835 {
3836 cmd_qtminftpilen (packet);
3837 return 1;
3838 }
219f2f23
PA
3839
3840 return 0;
3841}
3842
fa593d66
PA
3843#endif
3844#ifndef IN_PROCESS_AGENT
3845
219f2f23
PA
3846/* Call this when thread TINFO has hit the tracepoint defined by
3847 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3848 action. This adds a while-stepping collecting state item to the
3849 threads' collecting state list, so that we can keep track of
3850 multiple simultaneous while-stepping actions being collected by the
3851 same thread. This can happen in cases like:
3852
3853 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3854 ff0002 INSN2
3855 ff0003 INSN3 <-- TP2, collect $regs
3856 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3857 ff0005 INSN5
3858
3859 Notice that when instruction INSN5 is reached, the while-stepping
3860 actions of both TP1 and TP3 are still being collected, and that TP2
3861 had been collected meanwhile. The whole range of ff0001-ff0005
3862 should be single-stepped, due to at least TP1's while-stepping
3863 action covering the whole range. */
3864
3865static void
3866add_while_stepping_state (struct thread_info *tinfo,
3867 int tp_number, CORE_ADDR tp_address)
3868{
3869 struct wstep_state *wstep;
3870
3871 wstep = xmalloc (sizeof (*wstep));
3872 wstep->next = tinfo->while_stepping;
3873
3874 wstep->tp_number = tp_number;
3875 wstep->tp_address = tp_address;
3876 wstep->current_step = 0;
3877
3878 tinfo->while_stepping = wstep;
3879}
3880
3881/* Release the while-stepping collecting state WSTEP. */
3882
3883static void
3884release_while_stepping_state (struct wstep_state *wstep)
3885{
3886 free (wstep);
3887}
3888
3889/* Release all while-stepping collecting states currently associated
3890 with thread TINFO. */
3891
3892void
3893release_while_stepping_state_list (struct thread_info *tinfo)
3894{
3895 struct wstep_state *head;
3896
3897 while (tinfo->while_stepping)
3898 {
3899 head = tinfo->while_stepping;
3900 tinfo->while_stepping = head->next;
3901 release_while_stepping_state (head);
3902 }
3903}
3904
3905/* If TINFO was handling a 'while-stepping' action, the step has
3906 finished, so collect any step data needed, and check if any more
3907 steps are required. Return true if the thread was indeed
3908 collecting tracepoint data, false otherwise. */
3909
3910int
3911tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
3912{
3913 struct tracepoint *tpoint;
3914 struct wstep_state *wstep;
3915 struct wstep_state **wstep_link;
3916 struct trap_tracepoint_ctx ctx;
3917
fa593d66
PA
3918 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3919 our buffer. */
3920 if (in_process_agent_loaded ())
3921 upload_fast_traceframes ();
3922
219f2f23
PA
3923 /* Check if we were indeed collecting data for one of more
3924 tracepoints with a 'while-stepping' count. */
3925 if (tinfo->while_stepping == NULL)
3926 return 0;
3927
3928 if (!tracing)
3929 {
3930 /* We're not even tracing anymore. Stop this thread from
3931 collecting. */
3932 release_while_stepping_state_list (tinfo);
3933
3934 /* The thread had stopped due to a single-step request indeed
3935 explained by a tracepoint. */
3936 return 1;
3937 }
3938
3939 wstep = tinfo->while_stepping;
3940 wstep_link = &tinfo->while_stepping;
3941
3942 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3943 target_pid_to_str (tinfo->entry.id),
3944 wstep->tp_number, paddress (wstep->tp_address));
3945
fa593d66 3946 ctx.base.type = trap_tracepoint;
219f2f23
PA
3947 ctx.regcache = get_thread_regcache (tinfo, 1);
3948
3949 while (wstep != NULL)
3950 {
3951 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
3952 if (tpoint == NULL)
3953 {
3954 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3955 wstep->tp_number, paddress (wstep->tp_address),
3956 target_pid_to_str (tinfo->entry.id));
3957
3958 /* Unlink. */
3959 *wstep_link = wstep->next;
3960 release_while_stepping_state (wstep);
4f269b12 3961 wstep = *wstep_link;
219f2f23
PA
3962 continue;
3963 }
3964
3965 /* We've just finished one step. */
3966 ++wstep->current_step;
3967
3968 /* Collect data. */
3969 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
3970 stop_pc, tpoint, wstep->current_step);
3971
3972 if (wstep->current_step >= tpoint->step_count)
3973 {
3974 /* The requested numbers of steps have occurred. */
3975 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3976 target_pid_to_str (tinfo->entry.id),
3977 wstep->tp_number, paddress (wstep->tp_address));
3978
3979 /* Unlink the wstep. */
3980 *wstep_link = wstep->next;
3981 release_while_stepping_state (wstep);
3982 wstep = *wstep_link;
3983
3984 /* Only check the hit count now, which ensure that we do all
3985 our stepping before stopping the run. */
3986 if (tpoint->pass_count > 0
3987 && tpoint->hit_count >= tpoint->pass_count
3988 && stopping_tracepoint == NULL)
3989 stopping_tracepoint = tpoint;
3990 }
3991 else
3992 {
3993 /* Keep single-stepping until the requested numbers of steps
3994 have occurred. */
3995 wstep_link = &wstep->next;
3996 wstep = *wstep_link;
3997 }
3998
3999 if (stopping_tracepoint
4000 || trace_buffer_is_full
4001 || expr_eval_result != expr_eval_no_error)
4002 {
4003 stop_tracing ();
4004 break;
4005 }
4006 }
4007
4008 return 1;
4009}
4010
fa593d66
PA
4011/* Handle any internal tracing control breakpoint hits. That means,
4012 pull traceframes from the IPA to our buffer, and syncing both
4013 tracing agents when the IPA's tracing stops for some reason. */
4014
4015int
4016handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
4017{
4018 /* Pull in fast tracepoint trace frames from the inferior in-process
4019 agent's buffer into our buffer. */
4020
4021 if (!in_process_agent_loaded ())
4022 return 0;
4023
4024 upload_fast_traceframes ();
4025
4026 /* Check if the in-process agent had decided we should stop
4027 tracing. */
4028 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
4029 {
4030 int ipa_trace_buffer_is_full;
4031 CORE_ADDR ipa_stopping_tracepoint;
4032 int ipa_expr_eval_result;
4033 CORE_ADDR ipa_error_tracepoint;
4034
4035 trace_debug ("lib stopped at stop_tracing");
4036
4037 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
4038 &ipa_trace_buffer_is_full);
4039
4040 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
4041 &ipa_stopping_tracepoint);
4042 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
4043
4044 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
4045 &ipa_error_tracepoint);
4046 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
4047
4048 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
4049 &ipa_expr_eval_result);
4050 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
4051
4052 trace_debug ("lib: trace_buffer_is_full: %d, "
4053 "stopping_tracepoint: %s, "
4054 "ipa_expr_eval_result: %d, "
4055 "error_tracepoint: %s, ",
4056 ipa_trace_buffer_is_full,
4057 paddress (ipa_stopping_tracepoint),
4058 ipa_expr_eval_result,
4059 paddress (ipa_error_tracepoint));
4060
4061 if (debug_threads)
4062 {
4063 if (ipa_trace_buffer_is_full)
4064 trace_debug ("lib stopped due to full buffer.");
4065 if (ipa_stopping_tracepoint)
4066 trace_debug ("lib stopped due to tpoint");
4067 if (ipa_stopping_tracepoint)
4068 trace_debug ("lib stopped due to error");
4069 }
4070
4071 if (ipa_stopping_tracepoint != 0)
4072 {
4073 stopping_tracepoint
4074 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
4075 }
4076 else if (ipa_expr_eval_result != expr_eval_no_error)
4077 {
4078 expr_eval_result = ipa_expr_eval_result;
4079 error_tracepoint
4080 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
4081 }
4082 stop_tracing ();
4083 return 1;
4084 }
4085 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
4086 {
4087 trace_debug ("lib stopped at flush_trace_buffer");
4088 return 1;
4089 }
4090
4091 return 0;
4092}
4093
219f2f23
PA
4094/* Return true if TINFO just hit a tracepoint. Collect data if
4095 so. */
4096
4097int
4098tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
4099{
4100 struct tracepoint *tpoint;
4101 int ret = 0;
4102 struct trap_tracepoint_ctx ctx;
4103
4104 /* Not tracing, don't handle. */
4105 if (!tracing)
4106 return 0;
4107
fa593d66 4108 ctx.base.type = trap_tracepoint;
219f2f23
PA
4109 ctx.regcache = get_thread_regcache (tinfo, 1);
4110
4111 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
4112 {
fa593d66
PA
4113 /* Note that we collect fast tracepoints here as well. We'll
4114 step over the fast tracepoint jump later, which avoids the
4115 double collect. */
219f2f23
PA
4116 if (tpoint->enabled && stop_pc == tpoint->address)
4117 {
4118 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
4119 target_pid_to_str (tinfo->entry.id),
4120 tpoint->number, paddress (tpoint->address));
4121
4122 /* Test the condition if present, and collect if true. */
4123 if (!tpoint->cond
4124 || (condition_true_at_tracepoint
4125 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
4126 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
4127 stop_pc, tpoint);
4128
4129 if (stopping_tracepoint
4130 || trace_buffer_is_full
4131 || expr_eval_result != expr_eval_no_error)
4132 {
4133 stop_tracing ();
4134 }
4135 /* If the tracepoint had a 'while-stepping' action, then set
4136 the thread to collect this tracepoint on the following
4137 single-steps. */
4138 else if (tpoint->step_count > 0)
4139 {
4140 add_while_stepping_state (tinfo,
4141 tpoint->number, tpoint->address);
4142 }
4143
4144 ret = 1;
4145 }
4146 }
4147
4148 return ret;
4149}
4150
fa593d66
PA
4151#endif
4152
0fb4aa4b
PA
4153#if defined IN_PROCESS_AGENT && defined HAVE_UST
4154struct ust_marker_data;
4155static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4156 CORE_ADDR stop_pc,
4157 struct tracepoint *tpoint,
4158 struct traceframe *tframe);
4159#endif
4160
219f2f23
PA
4161/* Create a trace frame for the hit of the given tracepoint in the
4162 given thread. */
4163
4164static void
4165collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
4166 struct tracepoint *tpoint)
4167{
4168 struct traceframe *tframe;
4169 int acti;
4170
4171 /* Only count it as a hit when we actually collect data. */
4172 tpoint->hit_count++;
4173
4174 /* If we've exceeded a defined pass count, record the event for
4175 later, and finish the collection for this hit. This test is only
4176 for nonstepping tracepoints, stepping tracepoints test at the end
4177 of their while-stepping loop. */
4178 if (tpoint->pass_count > 0
4179 && tpoint->hit_count >= tpoint->pass_count
4180 && tpoint->step_count == 0
4181 && stopping_tracepoint == NULL)
4182 stopping_tracepoint = tpoint;
4183
4184 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
4185 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
4186
4187 tframe = add_traceframe (tpoint);
4188
4189 if (tframe)
4190 {
4191 for (acti = 0; acti < tpoint->numactions; ++acti)
4192 {
fa593d66 4193#ifndef IN_PROCESS_AGENT
219f2f23
PA
4194 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
4195 tpoint->number, paddress (tpoint->address),
4196 tpoint->actions_str[acti]);
fa593d66 4197#endif
219f2f23
PA
4198
4199 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4200 tpoint->actions[acti]);
4201 }
4202
4203 finish_traceframe (tframe);
4204 }
4205
4206 if (tframe == NULL && tracing)
4207 trace_buffer_is_full = 1;
4208}
4209
fa593d66
PA
4210#ifndef IN_PROCESS_AGENT
4211
219f2f23
PA
4212static void
4213collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4214 CORE_ADDR stop_pc,
4215 struct tracepoint *tpoint, int current_step)
4216{
4217 struct traceframe *tframe;
4218 int acti;
4219
4220 trace_debug ("Making new step traceframe for "
4221 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4222 tpoint->number, paddress (tpoint->address),
4223 current_step, tpoint->step_count,
4224 tpoint->hit_count);
4225
4226 tframe = add_traceframe (tpoint);
4227
4228 if (tframe)
4229 {
4230 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4231 {
4232 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4233 tpoint->number, paddress (tpoint->address),
4234 tpoint->step_actions_str[acti]);
4235
4236 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4237 tpoint->step_actions[acti]);
4238 }
4239
4240 finish_traceframe (tframe);
4241 }
4242
4243 if (tframe == NULL && tracing)
4244 trace_buffer_is_full = 1;
4245}
4246
fa593d66
PA
4247#endif
4248
219f2f23
PA
4249static struct regcache *
4250get_context_regcache (struct tracepoint_hit_ctx *ctx)
4251{
fa593d66
PA
4252 struct regcache *regcache = NULL;
4253
4254#ifdef IN_PROCESS_AGENT
4255 if (ctx->type == fast_tracepoint)
4256 {
4257 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4258 if (!fctx->regcache_initted)
4259 {
4260 fctx->regcache_initted = 1;
4261 init_register_cache (&fctx->regcache, fctx->regspace);
4262 supply_regblock (&fctx->regcache, NULL);
4263 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4264 }
4265 regcache = &fctx->regcache;
4266 }
0fb4aa4b
PA
4267#ifdef HAVE_UST
4268 if (ctx->type == static_tracepoint)
4269 {
493e2a69
MS
4270 struct static_tracepoint_ctx *sctx
4271 = (struct static_tracepoint_ctx *) ctx;
4272
0fb4aa4b
PA
4273 if (!sctx->regcache_initted)
4274 {
4275 sctx->regcache_initted = 1;
4276 init_register_cache (&sctx->regcache, sctx->regspace);
4277 supply_regblock (&sctx->regcache, NULL);
4278 /* Pass down the tracepoint address, because REGS doesn't
4279 include the PC, but we know what it must have been. */
4280 supply_static_tracepoint_registers (&sctx->regcache,
4281 (const unsigned char *)
4282 sctx->regs,
4283 sctx->tpoint->address);
4284 }
4285 regcache = &sctx->regcache;
4286 }
4287#endif
fa593d66
PA
4288#else
4289 if (ctx->type == trap_tracepoint)
4290 {
4291 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4292 regcache = tctx->regcache;
4293 }
4294#endif
219f2f23
PA
4295
4296 gdb_assert (regcache != NULL);
4297
4298 return regcache;
4299}
4300
4301static void
4302do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4303 CORE_ADDR stop_pc,
4304 struct tracepoint *tpoint,
4305 struct traceframe *tframe,
4306 struct tracepoint_action *taction)
4307{
4308 enum eval_result_type err;
4309
4310 switch (taction->type)
4311 {
4312 case 'M':
4313 {
4314 struct collect_memory_action *maction;
4315
4316 maction = (struct collect_memory_action *) taction;
4317
4318 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4319 pulongest (maction->len),
4320 paddress (maction->addr), maction->basereg);
4321 /* (should use basereg) */
4322 agent_mem_read (tframe, NULL,
4323 (CORE_ADDR) maction->addr, maction->len);
4324 break;
4325 }
4326 case 'R':
4327 {
219f2f23
PA
4328 unsigned char *regspace;
4329 struct regcache tregcache;
4330 struct regcache *context_regcache;
4331
219f2f23
PA
4332
4333 trace_debug ("Want to collect registers");
4334
4335 /* Collect all registers for now. */
4336 regspace = add_traceframe_block (tframe,
4337 1 + register_cache_size ());
4338 if (regspace == NULL)
4339 {
4340 trace_debug ("Trace buffer block allocation failed, skipping");
4341 break;
4342 }
4343 /* Identify a register block. */
4344 *regspace = 'R';
4345
4346 context_regcache = get_context_regcache (ctx);
4347
4348 /* Wrap the regblock in a register cache (in the stack, we
4349 don't want to malloc here). */
4350 init_register_cache (&tregcache, regspace + 1);
4351
4352 /* Copy the register data to the regblock. */
4353 regcache_cpy (&tregcache, context_regcache);
4354
fa593d66 4355#ifndef IN_PROCESS_AGENT
219f2f23
PA
4356 /* On some platforms, trap-based tracepoints will have the PC
4357 pointing to the next instruction after the trap, but we
4358 don't want the user or GDB trying to guess whether the
4359 saved PC needs adjusting; so always record the adjusted
4360 stop_pc. Note that we can't use tpoint->address instead,
fa593d66
PA
4361 since it will be wrong for while-stepping actions. This
4362 adjustment is a nop for fast tracepoints collected from the
4363 in-process lib (but not if GDBserver is collecting one
4364 preemptively), since the PC had already been adjusted to
4365 contain the tracepoint's address by the jump pad. */
219f2f23
PA
4366 trace_debug ("Storing stop pc (0x%s) in regblock",
4367 paddress (tpoint->address));
4368
4369 /* This changes the regblock, not the thread's
4370 regcache. */
4371 regcache_write_pc (&tregcache, stop_pc);
fa593d66 4372#endif
219f2f23
PA
4373 }
4374 break;
4375 case 'X':
4376 {
4377 struct eval_expr_action *eaction;
4378
4379 eaction = (struct eval_expr_action *) taction;
4380
4381 trace_debug ("Want to evaluate expression");
4382
4383 err = eval_agent_expr (ctx, tframe, eaction->expr, NULL);
4384
4385 if (err != expr_eval_no_error)
4386 {
4387 record_tracepoint_error (tpoint, "action expression", err);
4388 return;
4389 }
4390 }
4391 break;
0fb4aa4b
PA
4392 case 'L':
4393 {
4394#if defined IN_PROCESS_AGENT && defined HAVE_UST
4395 trace_debug ("Want to collect static trace data");
4396 collect_ust_data_at_tracepoint (ctx, stop_pc,
4397 tpoint, tframe);
4398#else
4399 trace_debug ("warning: collecting static trace data, "
4400 "but static tracepoints are not supported");
4401#endif
4402 }
4403 break;
219f2f23
PA
4404 default:
4405 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4406 break;
4407 }
4408}
4409
4410static int
4411condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4412 struct tracepoint *tpoint)
4413{
4414 ULONGEST value = 0;
4415 enum eval_result_type err;
4416
c6beb2cb
PA
4417 /* Presently, gdbserver doesn't run compiled conditions, only the
4418 IPA does. If the program stops at a fast tracepoint's address
4419 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4420 gdbserver preemptively collect the fast tracepoint. Later, on
4421 resume, gdbserver steps over the fast tracepoint like it steps
4422 over breakpoints, so that the IPA doesn't see that fast
4423 tracepoint. This avoids double collects of fast tracepoints in
4424 that stopping scenario. Having gdbserver itself handle the fast
4425 tracepoint gives the user a consistent view of when fast or trap
4426 tracepoints are collected, compared to an alternative where only
4427 trap tracepoints are collected on stop, and fast tracepoints on
4428 resume. When a fast tracepoint is being processed by gdbserver,
4429 it is always the non-compiled condition expression that is
4430 used. */
4431#ifdef IN_PROCESS_AGENT
6a271cae
PA
4432 if (tpoint->compiled_cond)
4433 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4434 else
c6beb2cb 4435#endif
6a271cae 4436 err = eval_agent_expr (ctx, NULL, tpoint->cond, &value);
219f2f23
PA
4437
4438 if (err != expr_eval_no_error)
4439 {
4440 record_tracepoint_error (tpoint, "condition", err);
4441 /* The error case must return false. */
4442 return 0;
4443 }
4444
4445 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4446 tpoint->number, paddress (tpoint->address),
4447 pulongest (value));
4448 return (value ? 1 : 0);
4449}
4450
fa593d66
PA
4451#ifndef IN_PROCESS_AGENT
4452
219f2f23
PA
4453/* The packet form of an agent expression consists of an 'X', number
4454 of bytes in expression, a comma, and then the bytes. */
4455
4456static struct agent_expr *
4457parse_agent_expr (char **actparm)
4458{
4459 char *act = *actparm;
4460 ULONGEST xlen;
4461 struct agent_expr *aexpr;
4462
4463 ++act; /* skip the X */
4464 act = unpack_varlen_hex (act, &xlen);
4465 ++act; /* skip a comma */
4466 aexpr = xmalloc (sizeof (struct agent_expr));
4467 aexpr->length = xlen;
4468 aexpr->bytes = xmalloc (xlen);
4469 convert_ascii_to_int (act, aexpr->bytes, xlen);
4470 *actparm = act + (xlen * 2);
4471 return aexpr;
4472}
4473
4474/* Convert the bytes of an agent expression back into hex digits, so
4475 they can be printed or uploaded. This allocates the buffer,
4476 callers should free when they are done with it. */
4477
4478static char *
4479unparse_agent_expr (struct agent_expr *aexpr)
4480{
4481 char *rslt;
4482
4483 rslt = xmalloc (2 * aexpr->length + 1);
4484 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
4485 return rslt;
4486}
4487
fa593d66
PA
4488#endif
4489
94d5e490
TT
4490/* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4491
4492static const char *
4493gdb_agent_op_name (int op)
4494{
4495 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
4496 return "?undef?";
4497 return gdb_agent_op_names[op];
4498}
4499
219f2f23
PA
4500/* The agent expression evaluator, as specified by the GDB docs. It
4501 returns 0 if everything went OK, and a nonzero error code
4502 otherwise. */
4503
4504static enum eval_result_type
4505eval_agent_expr (struct tracepoint_hit_ctx *ctx,
4506 struct traceframe *tframe,
4507 struct agent_expr *aexpr,
4508 ULONGEST *rslt)
4509{
4510 int pc = 0;
4511#define STACK_MAX 100
4512 ULONGEST stack[STACK_MAX], top;
4513 int sp = 0;
4514 unsigned char op;
4515 int arg;
4516
4517 /* This union is a convenient way to convert representations. For
4518 now, assume a standard architecture where the hardware integer
4519 types have 8, 16, 32, 64 bit types. A more robust solution would
4520 be to import stdint.h from gnulib. */
4521 union
4522 {
4523 union
4524 {
4525 unsigned char bytes[1];
4526 unsigned char val;
4527 } u8;
4528 union
4529 {
4530 unsigned char bytes[2];
4531 unsigned short val;
4532 } u16;
4533 union
4534 {
4535 unsigned char bytes[4];
4536 unsigned int val;
4537 } u32;
4538 union
4539 {
4540 unsigned char bytes[8];
4541 ULONGEST val;
4542 } u64;
4543 } cnv;
4544
4545 if (aexpr->length == 0)
4546 {
4547 trace_debug ("empty agent expression");
4548 return expr_eval_empty_expression;
4549 }
4550
4551 /* Cache the stack top in its own variable. Much of the time we can
4552 operate on this variable, rather than dinking with the stack. It
4553 needs to be copied to the stack when sp changes. */
4554 top = 0;
4555
4556 while (1)
4557 {
4558 op = aexpr->bytes[pc++];
4559
4560 trace_debug ("About to interpret byte 0x%x", op);
4561
4562 switch (op)
4563 {
4564 case gdb_agent_op_add:
4565 top += stack[--sp];
4566 break;
4567
4568 case gdb_agent_op_sub:
4569 top = stack[--sp] - top;
4570 break;
4571
4572 case gdb_agent_op_mul:
4573 top *= stack[--sp];
4574 break;
4575
4576 case gdb_agent_op_div_signed:
4577 if (top == 0)
4578 {
4579 trace_debug ("Attempted to divide by zero");
4580 return expr_eval_divide_by_zero;
4581 }
4582 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
4583 break;
4584
4585 case gdb_agent_op_div_unsigned:
4586 if (top == 0)
4587 {
4588 trace_debug ("Attempted to divide by zero");
4589 return expr_eval_divide_by_zero;
4590 }
4591 top = stack[--sp] / top;
4592 break;
4593
4594 case gdb_agent_op_rem_signed:
4595 if (top == 0)
4596 {
4597 trace_debug ("Attempted to divide by zero");
4598 return expr_eval_divide_by_zero;
4599 }
4600 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
4601 break;
4602
4603 case gdb_agent_op_rem_unsigned:
4604 if (top == 0)
4605 {
4606 trace_debug ("Attempted to divide by zero");
4607 return expr_eval_divide_by_zero;
4608 }
4609 top = stack[--sp] % top;
4610 break;
4611
4612 case gdb_agent_op_lsh:
4613 top = stack[--sp] << top;
4614 break;
4615
4616 case gdb_agent_op_rsh_signed:
4617 top = ((LONGEST) stack[--sp]) >> top;
4618 break;
4619
4620 case gdb_agent_op_rsh_unsigned:
4621 top = stack[--sp] >> top;
4622 break;
4623
4624 case gdb_agent_op_trace:
4625 agent_mem_read (tframe,
4626 NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top);
4627 if (--sp >= 0)
4628 top = stack[sp];
4629 break;
4630
4631 case gdb_agent_op_trace_quick:
4632 arg = aexpr->bytes[pc++];
4633 agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg);
4634 break;
4635
4636 case gdb_agent_op_log_not:
4637 top = !top;
4638 break;
4639
4640 case gdb_agent_op_bit_and:
4641 top &= stack[--sp];
4642 break;
4643
4644 case gdb_agent_op_bit_or:
4645 top |= stack[--sp];
4646 break;
4647
4648 case gdb_agent_op_bit_xor:
4649 top ^= stack[--sp];
4650 break;
4651
4652 case gdb_agent_op_bit_not:
4653 top = ~top;
4654 break;
4655
4656 case gdb_agent_op_equal:
4657 top = (stack[--sp] == top);
4658 break;
4659
4660 case gdb_agent_op_less_signed:
4661 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
4662 break;
4663
4664 case gdb_agent_op_less_unsigned:
4665 top = (stack[--sp] < top);
4666 break;
4667
4668 case gdb_agent_op_ext:
4669 arg = aexpr->bytes[pc++];
4670 if (arg < (sizeof (LONGEST) * 8))
4671 {
4672 LONGEST mask = 1 << (arg - 1);
4673 top &= ((LONGEST) 1 << arg) - 1;
4674 top = (top ^ mask) - mask;
4675 }
4676 break;
4677
4678 case gdb_agent_op_ref8:
4679 agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1);
4680 top = cnv.u8.val;
4681 break;
4682
4683 case gdb_agent_op_ref16:
4684 agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2);
4685 top = cnv.u16.val;
4686 break;
4687
4688 case gdb_agent_op_ref32:
4689 agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4);
4690 top = cnv.u32.val;
4691 break;
4692
4693 case gdb_agent_op_ref64:
4694 agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8);
4695 top = cnv.u64.val;
4696 break;
4697
4698 case gdb_agent_op_if_goto:
4699 if (top)
4700 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4701 else
4702 pc += 2;
4703 if (--sp >= 0)
4704 top = stack[sp];
4705 break;
4706
4707 case gdb_agent_op_goto:
4708 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4709 break;
4710
4711 case gdb_agent_op_const8:
4712 /* Flush the cached stack top. */
4713 stack[sp++] = top;
4714 top = aexpr->bytes[pc++];
4715 break;
4716
4717 case gdb_agent_op_const16:
4718 /* Flush the cached stack top. */
4719 stack[sp++] = top;
4720 top = aexpr->bytes[pc++];
4721 top = (top << 8) + aexpr->bytes[pc++];
4722 break;
4723
4724 case gdb_agent_op_const32:
4725 /* Flush the cached stack top. */
4726 stack[sp++] = top;
4727 top = aexpr->bytes[pc++];
4728 top = (top << 8) + aexpr->bytes[pc++];
4729 top = (top << 8) + aexpr->bytes[pc++];
4730 top = (top << 8) + aexpr->bytes[pc++];
4731 break;
4732
4733 case gdb_agent_op_const64:
4734 /* Flush the cached stack top. */
4735 stack[sp++] = top;
4736 top = aexpr->bytes[pc++];
4737 top = (top << 8) + aexpr->bytes[pc++];
4738 top = (top << 8) + aexpr->bytes[pc++];
4739 top = (top << 8) + aexpr->bytes[pc++];
4740 top = (top << 8) + aexpr->bytes[pc++];
4741 top = (top << 8) + aexpr->bytes[pc++];
4742 top = (top << 8) + aexpr->bytes[pc++];
4743 top = (top << 8) + aexpr->bytes[pc++];
4744 break;
4745
4746 case gdb_agent_op_reg:
4747 /* Flush the cached stack top. */
4748 stack[sp++] = top;
4749 arg = aexpr->bytes[pc++];
4750 arg = (arg << 8) + aexpr->bytes[pc++];
4751 {
4752 int regnum = arg;
4753 struct regcache *regcache;
4754
4755 regcache = get_context_regcache (ctx);
4756
4757 switch (register_size (regnum))
4758 {
4759 case 8:
4760 collect_register (regcache, regnum, cnv.u64.bytes);
4761 top = cnv.u64.val;
4762 break;
4763 case 4:
4764 collect_register (regcache, regnum, cnv.u32.bytes);
4765 top = cnv.u32.val;
4766 break;
4767 case 2:
4768 collect_register (regcache, regnum, cnv.u16.bytes);
4769 top = cnv.u16.val;
4770 break;
4771 case 1:
4772 collect_register (regcache, regnum, cnv.u8.bytes);
4773 top = cnv.u8.val;
4774 break;
4775 default:
4776 internal_error (__FILE__, __LINE__,
4777 "unhandled register size");
4778 }
4779 }
4780 break;
4781
4782 case gdb_agent_op_end:
4783 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4784 sp, pulongest (top));
4785 if (rslt)
4786 {
4787 if (sp <= 0)
4788 {
4789 /* This should be an error */
4790 trace_debug ("Stack is empty, nothing to return");
4791 return expr_eval_empty_stack;
4792 }
4793 *rslt = top;
4794 }
4795 return expr_eval_no_error;
4796
4797 case gdb_agent_op_dup:
4798 stack[sp++] = top;
4799 break;
4800
4801 case gdb_agent_op_pop:
4802 if (--sp >= 0)
4803 top = stack[sp];
4804 break;
4805
c7f96d2b
TT
4806 case gdb_agent_op_pick:
4807 arg = aexpr->bytes[pc++];
4808 stack[sp] = top;
4809 top = stack[sp - arg];
4810 ++sp;
4811 break;
4812
4813 case gdb_agent_op_rot:
4814 {
4815 ULONGEST tem = stack[sp - 1];
4816
4817 stack[sp - 1] = stack[sp - 2];
4818 stack[sp - 2] = top;
4819 top = tem;
4820 }
4821 break;
4822
219f2f23
PA
4823 case gdb_agent_op_zero_ext:
4824 arg = aexpr->bytes[pc++];
4825 if (arg < (sizeof (LONGEST) * 8))
4826 top &= ((LONGEST) 1 << arg) - 1;
4827 break;
4828
4829 case gdb_agent_op_swap:
4830 /* Interchange top two stack elements, making sure top gets
4831 copied back onto stack. */
4832 stack[sp] = top;
4833 top = stack[sp - 1];
4834 stack[sp - 1] = stack[sp];
4835 break;
4836
4837 case gdb_agent_op_getv:
4838 /* Flush the cached stack top. */
4839 stack[sp++] = top;
4840 arg = aexpr->bytes[pc++];
4841 arg = (arg << 8) + aexpr->bytes[pc++];
4842 top = get_trace_state_variable_value (arg);
4843 break;
4844
4845 case gdb_agent_op_setv:
4846 arg = aexpr->bytes[pc++];
4847 arg = (arg << 8) + aexpr->bytes[pc++];
4848 set_trace_state_variable_value (arg, top);
4849 /* Note that we leave the value on the stack, for the
4850 benefit of later/enclosing expressions. */
4851 break;
4852
4853 case gdb_agent_op_tracev:
4854 arg = aexpr->bytes[pc++];
4855 arg = (arg << 8) + aexpr->bytes[pc++];
4856 agent_tsv_read (tframe, arg);
4857 break;
4858
3065dfb6
SS
4859 case gdb_agent_op_tracenz:
4860 agent_mem_read_string (tframe, NULL, (CORE_ADDR) stack[--sp],
4861 (ULONGEST) top);
4862 if (--sp >= 0)
4863 top = stack[sp];
4864 break;
4865
219f2f23
PA
4866 /* GDB never (currently) generates any of these ops. */
4867 case gdb_agent_op_float:
4868 case gdb_agent_op_ref_float:
4869 case gdb_agent_op_ref_double:
4870 case gdb_agent_op_ref_long_double:
4871 case gdb_agent_op_l_to_d:
4872 case gdb_agent_op_d_to_l:
4873 case gdb_agent_op_trace16:
4874 trace_debug ("Agent expression op 0x%x valid, but not handled",
4875 op);
4876 /* If ever GDB generates any of these, we don't have the
4877 option of ignoring. */
4878 return 1;
4879
4880 default:
4881 trace_debug ("Agent expression op 0x%x not recognized", op);
4882 /* Don't struggle on, things will just get worse. */
4883 return expr_eval_unrecognized_opcode;
4884 }
4885
4886 /* Check for stack badness. */
4887 if (sp >= (STACK_MAX - 1))
4888 {
4889 trace_debug ("Expression stack overflow");
4890 return expr_eval_stack_overflow;
4891 }
4892
4893 if (sp < 0)
4894 {
4895 trace_debug ("Expression stack underflow");
4896 return expr_eval_stack_underflow;
4897 }
4898
4899 trace_debug ("Op %s -> sp=%d, top=0x%s",
94d5e490 4900 gdb_agent_op_name (op), sp, pulongest (top));
219f2f23
PA
4901 }
4902}
4903
4904/* Do memory copies for bytecodes. */
4905/* Do the recording of memory blocks for actions and bytecodes. */
4906
4907static int
4908agent_mem_read (struct traceframe *tframe,
4909 unsigned char *to, CORE_ADDR from, ULONGEST len)
4910{
4911 unsigned char *mspace;
4912 ULONGEST remaining = len;
4913 unsigned short blocklen;
4914
4915 /* If a 'to' buffer is specified, use it. */
4916 if (to != NULL)
4917 {
4918 read_inferior_memory (from, to, len);
4919 return 0;
4920 }
4921
4922 /* Otherwise, create a new memory block in the trace buffer. */
4923 while (remaining > 0)
4924 {
4925 size_t sp;
4926
4927 blocklen = (remaining > 65535 ? 65535 : remaining);
4928 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4929 mspace = add_traceframe_block (tframe, sp);
4930 if (mspace == NULL)
4931 return 1;
4932 /* Identify block as a memory block. */
4933 *mspace = 'M';
4934 ++mspace;
4935 /* Record address and size. */
4936 memcpy (mspace, &from, sizeof (from));
4937 mspace += sizeof (from);
4938 memcpy (mspace, &blocklen, sizeof (blocklen));
4939 mspace += sizeof (blocklen);
4940 /* Record the memory block proper. */
4941 read_inferior_memory (from, mspace, blocklen);
4942 trace_debug ("%d bytes recorded", blocklen);
4943 remaining -= blocklen;
4944 from += blocklen;
4945 }
4946 return 0;
4947}
4948
3065dfb6
SS
4949static int
4950agent_mem_read_string (struct traceframe *tframe,
4951 unsigned char *to, CORE_ADDR from, ULONGEST len)
4952{
4953 unsigned char *buf, *mspace;
4954 ULONGEST remaining = len;
4955 unsigned short blocklen, i;
4956
4957 /* To save a bit of space, block lengths are 16-bit, so break large
4958 requests into multiple blocks. Bordering on overkill for strings,
4959 but it could happen that someone specifies a large max length. */
4960 while (remaining > 0)
4961 {
4962 size_t sp;
4963
4964 blocklen = (remaining > 65535 ? 65535 : remaining);
4965 /* We want working space to accumulate nonzero bytes, since
4966 traceframes must have a predecided size (otherwise it gets
4967 harder to wrap correctly for the circular case, etc). */
4968 buf = (unsigned char *) xmalloc (blocklen + 1);
4969 for (i = 0; i < blocklen; ++i)
4970 {
4971 /* Read the string one byte at a time, in case the string is
4972 at the end of a valid memory area - we don't want a
4973 correctly-terminated string to engender segvio
4974 complaints. */
4975 read_inferior_memory (from + i, buf + i, 1);
4976
4977 if (buf[i] == '\0')
4978 {
4979 blocklen = i + 1;
4980 /* Make sure outer loop stops now too. */
4981 remaining = blocklen;
4982 break;
4983 }
4984 }
4985 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4986 mspace = add_traceframe_block (tframe, sp);
4987 if (mspace == NULL)
4988 {
4989 xfree (buf);
4990 return 1;
4991 }
4992 /* Identify block as a memory block. */
4993 *mspace = 'M';
4994 ++mspace;
4995 /* Record address and size. */
4996 memcpy ((void *) mspace, (void *) &from, sizeof (from));
4997 mspace += sizeof (from);
4998 memcpy ((void *) mspace, (void *) &blocklen, sizeof (blocklen));
4999 mspace += sizeof (blocklen);
5000 /* Copy the string contents. */
5001 memcpy ((void *) mspace, (void *) buf, blocklen);
5002 remaining -= blocklen;
5003 from += blocklen;
5004 xfree (buf);
5005 }
5006 return 0;
5007}
5008
219f2f23
PA
5009/* Record the value of a trace state variable. */
5010
5011static int
5012agent_tsv_read (struct traceframe *tframe, int n)
5013{
5014 unsigned char *vspace;
5015 LONGEST val;
5016
5017 vspace = add_traceframe_block (tframe,
5018 1 + sizeof (n) + sizeof (LONGEST));
5019 if (vspace == NULL)
5020 return 1;
5021 /* Identify block as a variable. */
5022 *vspace = 'V';
5023 /* Record variable's number and value. */
5024 memcpy (vspace + 1, &n, sizeof (n));
5025 val = get_trace_state_variable_value (n);
5026 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
5027 trace_debug ("Variable %d recorded", n);
5028 return 0;
5029}
5030
fa593d66
PA
5031#ifndef IN_PROCESS_AGENT
5032
b3b9301e
PA
5033/* Callback for traceframe_walk_blocks, used to find a given block
5034 type in a traceframe. */
5035
5036static int
5037match_blocktype (char blocktype, unsigned char *dataptr, void *data)
5038{
5039 char *wantedp = data;
5040
5041 if (*wantedp == blocktype)
5042 return 1;
5043
5044 return 0;
5045}
5046
5047/* Walk over all traceframe blocks of the traceframe buffer starting
5048 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
5049 block found, passing in DATA unmodified. If CALLBACK returns true,
5050 this returns a pointer to where the block is found. Returns NULL
5051 if no callback call returned true, indicating that all blocks have
5052 been walked. */
5053
219f2f23 5054static unsigned char *
b3b9301e
PA
5055traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
5056 int tfnum,
5057 int (*callback) (char blocktype,
5058 unsigned char *dataptr,
5059 void *data),
5060 void *data)
219f2f23
PA
5061{
5062 unsigned char *dataptr;
5063
5064 if (datasize == 0)
5065 {
5066 trace_debug ("traceframe %d has no data", tfnum);
5067 return NULL;
5068 }
5069
5070 /* Iterate through a traceframe's blocks, looking for a block of the
5071 requested type. */
5072 for (dataptr = database;
5073 dataptr < database + datasize;
5074 /* nothing */)
5075 {
5076 char blocktype;
5077 unsigned short mlen;
5078
5079 if (dataptr == trace_buffer_wrap)
5080 {
5081 /* Adjust to reflect wrapping part of the frame around to
5082 the beginning. */
5083 datasize = dataptr - database;
5084 dataptr = database = trace_buffer_lo;
5085 }
b3b9301e 5086
219f2f23
PA
5087 blocktype = *dataptr++;
5088
b3b9301e 5089 if ((*callback) (blocktype, dataptr, data))
219f2f23
PA
5090 return dataptr;
5091
5092 switch (blocktype)
5093 {
5094 case 'R':
5095 /* Skip over the registers block. */
5096 dataptr += register_cache_size ();
5097 break;
5098 case 'M':
5099 /* Skip over the memory block. */
5100 dataptr += sizeof (CORE_ADDR);
5101 memcpy (&mlen, dataptr, sizeof (mlen));
5102 dataptr += (sizeof (mlen) + mlen);
5103 break;
219f2f23
PA
5104 case 'V':
5105 /* Skip over the TSV block. */
5106 dataptr += (sizeof (int) + sizeof (LONGEST));
5107 break;
0fb4aa4b
PA
5108 case 'S':
5109 /* Skip over the static trace data block. */
5110 memcpy (&mlen, dataptr, sizeof (mlen));
5111 dataptr += (sizeof (mlen) + mlen);
5112 break;
219f2f23
PA
5113 default:
5114 trace_debug ("traceframe %d has unknown block type 0x%x",
5115 tfnum, blocktype);
5116 return NULL;
5117 }
5118 }
5119
5120 return NULL;
5121}
5122
b3b9301e
PA
5123/* Look for the block of type TYPE_WANTED in the trameframe starting
5124 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
5125 number. */
5126
5127static unsigned char *
5128traceframe_find_block_type (unsigned char *database, unsigned int datasize,
5129 int tfnum, char type_wanted)
5130{
5131 return traceframe_walk_blocks (database, datasize, tfnum,
5132 match_blocktype, &type_wanted);
5133}
5134
219f2f23
PA
5135static unsigned char *
5136traceframe_find_regblock (struct traceframe *tframe, int tfnum)
5137{
5138 unsigned char *regblock;
5139
5140 regblock = traceframe_find_block_type (tframe->data,
5141 tframe->data_size,
5142 tfnum, 'R');
5143
5144 if (regblock == NULL)
5145 trace_debug ("traceframe %d has no register data", tfnum);
5146
5147 return regblock;
5148}
5149
5150/* Get registers from a traceframe. */
5151
5152int
5153fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
5154{
5155 unsigned char *dataptr;
5156 struct tracepoint *tpoint;
5157 struct traceframe *tframe;
5158
5159 tframe = find_traceframe (tfnum);
5160
5161 if (tframe == NULL)
5162 {
5163 trace_debug ("traceframe %d not found", tfnum);
5164 return 1;
5165 }
5166
5167 dataptr = traceframe_find_regblock (tframe, tfnum);
5168 if (dataptr == NULL)
5169 {
1c79eb8a 5170 /* Mark registers unavailable. */
219f2f23
PA
5171 supply_regblock (regcache, NULL);
5172
5173 /* We can generally guess at a PC, although this will be
5174 misleading for while-stepping frames and multi-location
5175 tracepoints. */
5176 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
5177 if (tpoint != NULL)
5178 regcache_write_pc (regcache, tpoint->address);
5179 }
5180 else
5181 supply_regblock (regcache, dataptr);
5182
5183 return 0;
5184}
5185
5186static CORE_ADDR
5187traceframe_get_pc (struct traceframe *tframe)
5188{
5189 struct regcache regcache;
5190 unsigned char *dataptr;
5191
5192 dataptr = traceframe_find_regblock (tframe, -1);
5193 if (dataptr == NULL)
5194 return 0;
5195
5196 init_register_cache (&regcache, dataptr);
5197 return regcache_read_pc (&regcache);
5198}
5199
5200/* Read a requested block of memory from a trace frame. */
5201
5202int
5203traceframe_read_mem (int tfnum, CORE_ADDR addr,
5204 unsigned char *buf, ULONGEST length,
5205 ULONGEST *nbytes)
5206{
5207 struct traceframe *tframe;
5208 unsigned char *database, *dataptr;
5209 unsigned int datasize;
5210 CORE_ADDR maddr;
5211 unsigned short mlen;
5212
5213 trace_debug ("traceframe_read_mem");
5214
5215 tframe = find_traceframe (tfnum);
5216
5217 if (!tframe)
5218 {
5219 trace_debug ("traceframe %d not found", tfnum);
5220 return 1;
5221 }
5222
5223 datasize = tframe->data_size;
5224 database = dataptr = &tframe->data[0];
5225
5226 /* Iterate through a traceframe's blocks, looking for memory. */
5227 while ((dataptr = traceframe_find_block_type (dataptr,
493e2a69
MS
5228 datasize
5229 - (dataptr - database),
219f2f23
PA
5230 tfnum, 'M')) != NULL)
5231 {
5232 memcpy (&maddr, dataptr, sizeof (maddr));
5233 dataptr += sizeof (maddr);
5234 memcpy (&mlen, dataptr, sizeof (mlen));
5235 dataptr += sizeof (mlen);
5236 trace_debug ("traceframe %d has %d bytes at %s",
5237 tfnum, mlen, paddress (maddr));
5238
764880b7
PA
5239 /* If the block includes the first part of the desired range,
5240 return as much it has; GDB will re-request the remainder,
5241 which might be in a different block of this trace frame. */
5242 if (maddr <= addr && addr < (maddr + mlen))
219f2f23 5243 {
764880b7
PA
5244 ULONGEST amt = (maddr + mlen) - addr;
5245 if (amt > length)
5246 amt = length;
5247
5248 memcpy (buf, dataptr + (addr - maddr), amt);
5249 *nbytes = amt;
219f2f23
PA
5250 return 0;
5251 }
5252
5253 /* Skip over this block. */
5254 dataptr += mlen;
5255 }
5256
5257 trace_debug ("traceframe %d has no memory data for the desired region",
5258 tfnum);
5259
5260 *nbytes = 0;
5261 return 0;
5262}
5263
5264static int
5265traceframe_read_tsv (int tsvnum, LONGEST *val)
5266{
5267 int tfnum;
5268 struct traceframe *tframe;
5269 unsigned char *database, *dataptr;
5270 unsigned int datasize;
5271 int vnum;
5272
5273 trace_debug ("traceframe_read_tsv");
5274
5275 tfnum = current_traceframe;
5276
5277 if (tfnum < 0)
5278 {
5279 trace_debug ("no current traceframe");
5280 return 1;
5281 }
5282
5283 tframe = find_traceframe (tfnum);
5284
5285 if (tframe == NULL)
5286 {
5287 trace_debug ("traceframe %d not found", tfnum);
5288 return 1;
5289 }
5290
5291 datasize = tframe->data_size;
5292 database = dataptr = &tframe->data[0];
5293
5294 /* Iterate through a traceframe's blocks, looking for the tsv. */
5295 while ((dataptr = traceframe_find_block_type (dataptr,
493e2a69
MS
5296 datasize
5297 - (dataptr - database),
219f2f23
PA
5298 tfnum, 'V')) != NULL)
5299 {
5300 memcpy (&vnum, dataptr, sizeof (vnum));
5301 dataptr += sizeof (vnum);
5302
5303 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
5304
5305 /* Check that this is the variable we want. */
5306 if (tsvnum == vnum)
5307 {
5308 memcpy (val, dataptr, sizeof (*val));
5309 return 0;
5310 }
5311
5312 /* Skip over this block. */
5313 dataptr += sizeof (LONGEST);
5314 }
5315
5316 trace_debug ("traceframe %d has no data for variable %d",
5317 tfnum, tsvnum);
5318 return 1;
5319}
5320
0fb4aa4b
PA
5321/* Read a requested block of static tracepoint data from a trace
5322 frame. */
5323
5324int
5325traceframe_read_sdata (int tfnum, ULONGEST offset,
5326 unsigned char *buf, ULONGEST length,
5327 ULONGEST *nbytes)
5328{
5329 struct traceframe *tframe;
5330 unsigned char *database, *dataptr;
5331 unsigned int datasize;
5332 unsigned short mlen;
5333
5334 trace_debug ("traceframe_read_sdata");
5335
5336 tframe = find_traceframe (tfnum);
5337
5338 if (!tframe)
5339 {
5340 trace_debug ("traceframe %d not found", tfnum);
5341 return 1;
5342 }
5343
5344 datasize = tframe->data_size;
5345 database = &tframe->data[0];
5346
5347 /* Iterate through a traceframe's blocks, looking for static
5348 tracepoint data. */
5349 dataptr = traceframe_find_block_type (database, datasize,
5350 tfnum, 'S');
5351 if (dataptr != NULL)
5352 {
5353 memcpy (&mlen, dataptr, sizeof (mlen));
5354 dataptr += sizeof (mlen);
5355 if (offset < mlen)
5356 {
5357 if (offset + length > mlen)
5358 length = mlen - offset;
5359
5360 memcpy (buf, dataptr, length);
5361 *nbytes = length;
5362 }
5363 else
5364 *nbytes = 0;
5365 return 0;
5366 }
5367
5368 trace_debug ("traceframe %d has no static trace data", tfnum);
5369
5370 *nbytes = 0;
5371 return 0;
5372}
5373
b3b9301e
PA
5374/* Callback for traceframe_walk_blocks. Builds a traceframe-info
5375 object. DATA is pointer to a struct buffer holding the
5376 traceframe-info object being built. */
5377
5378static int
5379build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5380{
5381 struct buffer *buffer = data;
5382
5383 switch (blocktype)
5384 {
5385 case 'M':
5386 {
5387 unsigned short mlen;
5388 CORE_ADDR maddr;
5389
5390 memcpy (&maddr, dataptr, sizeof (maddr));
5391 dataptr += sizeof (maddr);
5392 memcpy (&mlen, dataptr, sizeof (mlen));
5393 dataptr += sizeof (mlen);
5394 buffer_xml_printf (buffer,
5395 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5396 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5397 break;
5398 }
5399 case 'V':
5400 case 'R':
5401 case 'S':
5402 {
5403 break;
5404 }
5405 default:
5406 warning ("Unhandled trace block type (%d) '%c ' "
5407 "while building trace frame info.",
5408 blocktype, blocktype);
5409 break;
5410 }
5411
5412 return 0;
5413}
5414
5415/* Build a traceframe-info object for traceframe number TFNUM into
5416 BUFFER. */
5417
5418int
5419traceframe_read_info (int tfnum, struct buffer *buffer)
5420{
5421 struct traceframe *tframe;
5422
5423 trace_debug ("traceframe_read_info");
5424
5425 tframe = find_traceframe (tfnum);
5426
5427 if (!tframe)
5428 {
5429 trace_debug ("traceframe %d not found", tfnum);
5430 return 1;
5431 }
5432
5433 buffer_grow_str (buffer, "<traceframe-info>\n");
5434 traceframe_walk_blocks (tframe->data, tframe->data_size,
5435 tfnum, build_traceframe_info_xml, buffer);
5436 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5437 return 0;
5438}
5439
fa593d66
PA
5440/* Return the first fast tracepoint whose jump pad contains PC. */
5441
5442static struct tracepoint *
5443fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5444{
5445 struct tracepoint *tpoint;
5446
5447 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5448 if (tpoint->type == fast_tracepoint)
5449 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5450 return tpoint;
5451
5452 return NULL;
5453}
5454
405f8e94
SS
5455/* Return the first fast tracepoint whose trampoline contains PC. */
5456
5457static struct tracepoint *
5458fast_tracepoint_from_trampoline_address (CORE_ADDR pc)
5459{
5460 struct tracepoint *tpoint;
5461
5462 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5463 {
5464 if (tpoint->type == fast_tracepoint
5465 && tpoint->trampoline <= pc && pc < tpoint->trampoline_end)
5466 return tpoint;
5467 }
5468
5469 return NULL;
5470}
5471
fa593d66
PA
5472/* Return GDBserver's tracepoint that matches the IP Agent's
5473 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5474 address space. */
5475
5476static struct tracepoint *
5477fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5478{
5479 struct tracepoint *tpoint;
5480
5481 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5482 if (tpoint->type == fast_tracepoint)
5483 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5484 return tpoint;
5485
5486 return NULL;
5487}
5488
5489#endif
5490
5491/* The type of the object that is used to synchronize fast tracepoint
5492 collection. */
5493
5494typedef struct collecting_t
5495{
5496 /* The fast tracepoint number currently collecting. */
5497 uintptr_t tpoint;
5498
5499 /* A number that GDBserver can use to identify the thread that is
5500 presently holding the collect lock. This need not (and usually
5501 is not) the thread id, as getting the current thread ID usually
5502 requires a system call, which we want to avoid like the plague.
5503 Usually this is thread's TCB, found in the TLS (pseudo-)
5504 register, which is readable with a single insn on several
5505 architectures. */
5506 uintptr_t thread_area;
5507} collecting_t;
5508
5509#ifndef IN_PROCESS_AGENT
5510
5511void
5512force_unlock_trace_buffer (void)
5513{
5514 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5515}
5516
5517/* Check if the thread identified by THREAD_AREA which is stopped at
5518 STOP_PC, is presently locking the fast tracepoint collection, and
5519 if so, gather some status of said collection. Returns 0 if the
5520 thread isn't collecting or in the jump pad at all. 1, if in the
5521 jump pad (or within gdb_collect) and hasn't executed the adjusted
5522 original insn yet (can set a breakpoint there and run to it). 2,
5523 if presently executing the adjusted original insn --- in which
5524 case, if we want to move the thread out of the jump pad, we need to
5525 single-step it until this function returns 0. */
5526
5527int
5528fast_tracepoint_collecting (CORE_ADDR thread_area,
5529 CORE_ADDR stop_pc,
5530 struct fast_tpoint_collect_status *status)
5531{
5532 CORE_ADDR ipa_collecting;
5533 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
405f8e94
SS
5534 CORE_ADDR ipa_gdb_trampoline_buffer;
5535 CORE_ADDR ipa_gdb_trampoline_buffer_end;
fa593d66
PA
5536 struct tracepoint *tpoint;
5537 int needs_breakpoint;
5538
5539 /* The thread THREAD_AREA is either:
5540
5541 0. not collecting at all, not within the jump pad, or within
5542 gdb_collect or one of its callees.
5543
5544 1. in the jump pad and haven't reached gdb_collect
5545
5546 2. within gdb_collect (out of the jump pad) (collect is set)
5547
5548 3. we're in the jump pad, after gdb_collect having returned,
5549 possibly executing the adjusted insns.
5550
5551 For cases 1 and 3, `collecting' may or not be set. The jump pad
5552 doesn't have any complicated jump logic, so we can tell if the
5553 thread is executing the adjust original insn or not by just
5554 matching STOP_PC with known jump pad addresses. If we it isn't
5555 yet executing the original insn, set a breakpoint there, and let
5556 the thread run to it, so to quickly step over a possible (many
5557 insns) gdb_collect call. Otherwise, or when the breakpoint is
5558 hit, only a few (small number of) insns are left to be executed
5559 in the jump pad. Single-step the thread until it leaves the
5560 jump pad. */
5561
5562 again:
5563 tpoint = NULL;
5564 needs_breakpoint = 0;
5565 trace_debug ("fast_tracepoint_collecting");
5566
5567 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5568 &ipa_gdb_jump_pad_buffer))
5569 fatal ("error extracting `gdb_jump_pad_buffer'");
5570 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5571 &ipa_gdb_jump_pad_buffer_end))
5572 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5573
405f8e94
SS
5574 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer,
5575 &ipa_gdb_trampoline_buffer))
5576 fatal ("error extracting `gdb_trampoline_buffer'");
5577 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_trampoline_buffer_end,
5578 &ipa_gdb_trampoline_buffer_end))
5579 fatal ("error extracting `gdb_trampoline_buffer_end'");
5580
493e2a69
MS
5581 if (ipa_gdb_jump_pad_buffer <= stop_pc
5582 && stop_pc < ipa_gdb_jump_pad_buffer_end)
fa593d66
PA
5583 {
5584 /* We can tell which tracepoint(s) the thread is collecting by
5585 matching the jump pad address back to the tracepoint. */
5586 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5587 if (tpoint == NULL)
5588 {
5589 warning ("in jump pad, but no matching tpoint?");
5590 return 0;
5591 }
5592 else
5593 {
5594 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5595 "adj_insn(%s, %s)",
5596 tpoint->number, paddress (tpoint->address),
5597 paddress (tpoint->jump_pad),
5598 paddress (tpoint->jump_pad_end),
5599 paddress (tpoint->adjusted_insn_addr),
5600 paddress (tpoint->adjusted_insn_addr_end));
5601 }
5602
5603 /* Definitely in the jump pad. May or may not need
5604 fast-exit-jump-pad breakpoint. */
5605 if (tpoint->jump_pad <= stop_pc
5606 && stop_pc < tpoint->adjusted_insn_addr)
5607 needs_breakpoint = 1;
5608 }
405f8e94
SS
5609 else if (ipa_gdb_trampoline_buffer <= stop_pc
5610 && stop_pc < ipa_gdb_trampoline_buffer_end)
5611 {
5612 /* We can tell which tracepoint(s) the thread is collecting by
5613 matching the trampoline address back to the tracepoint. */
5614 tpoint = fast_tracepoint_from_trampoline_address (stop_pc);
5615 if (tpoint == NULL)
5616 {
5617 warning ("in trampoline, but no matching tpoint?");
5618 return 0;
5619 }
5620 else
5621 {
5622 trace_debug ("in trampoline of tpoint (%d, %s); trampoline(%s, %s)",
5623 tpoint->number, paddress (tpoint->address),
5624 paddress (tpoint->trampoline),
5625 paddress (tpoint->trampoline_end));
5626 }
5627
5628 /* Have not reached jump pad yet, but treat the trampoline as a
5629 part of the jump pad that is before the adjusted original
5630 instruction. */
5631 needs_breakpoint = 1;
5632 }
fa593d66
PA
5633 else
5634 {
5635 collecting_t ipa_collecting_obj;
5636
5637 /* If `collecting' is set/locked, then the THREAD_AREA thread
5638 may or not be the one holding the lock. We have to read the
5639 lock to find out. */
5640
5641 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5642 &ipa_collecting))
5643 {
5644 trace_debug ("fast_tracepoint_collecting:"
5645 " failed reading 'collecting' in the inferior");
5646 return 0;
5647 }
5648
5649 if (!ipa_collecting)
5650 {
5651 trace_debug ("fast_tracepoint_collecting: not collecting"
5652 " (and nobody is).");
5653 return 0;
5654 }
5655
5656 /* Some thread is collecting. Check which. */
5657 if (read_inferior_memory (ipa_collecting,
5658 (unsigned char *) &ipa_collecting_obj,
5659 sizeof (ipa_collecting_obj)) != 0)
5660 goto again;
5661
5662 if (ipa_collecting_obj.thread_area != thread_area)
5663 {
5664 trace_debug ("fast_tracepoint_collecting: not collecting "
5665 "(another thread is)");
5666 return 0;
5667 }
5668
5669 tpoint
5670 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5671 if (tpoint == NULL)
5672 {
5673 warning ("fast_tracepoint_collecting: collecting, "
5674 "but tpoint %s not found?",
5675 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5676 return 0;
5677 }
5678
5679 /* The thread is within `gdb_collect', skip over the rest of
5680 fast tracepoint collection quickly using a breakpoint. */
5681 needs_breakpoint = 1;
5682 }
5683
5684 /* The caller wants a bit of status detail. */
5685 if (status != NULL)
5686 {
5687 status->tpoint_num = tpoint->number;
5688 status->tpoint_addr = tpoint->address;
5689 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5690 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5691 }
5692
5693 if (needs_breakpoint)
5694 {
5695 /* Hasn't executed the original instruction yet. Set breakpoint
5696 there, and wait till it's hit, then single-step until exiting
5697 the jump pad. */
5698
5699 trace_debug ("\
5700fast_tracepoint_collecting, returning continue-until-break at %s",
5701 paddress (tpoint->adjusted_insn_addr));
5702
5703 return 1; /* continue */
5704 }
5705 else
5706 {
5707 /* Just single-step until exiting the jump pad. */
5708
5709 trace_debug ("fast_tracepoint_collecting, returning "
5710 "need-single-step (%s-%s)",
5711 paddress (tpoint->adjusted_insn_addr),
5712 paddress (tpoint->adjusted_insn_addr_end));
5713
5714 return 2; /* single-step */
5715 }
5716}
5717
5718#endif
5719
5720#ifdef IN_PROCESS_AGENT
5721
5722/* The global fast tracepoint collect lock. Points to a collecting_t
5723 object built on the stack by the jump pad, if presently locked;
5724 NULL if it isn't locked. Note that this lock *must* be set while
5725 executing any *function other than the jump pad. See
5726 fast_tracepoint_collecting. */
5727static collecting_t * ATTR_USED collecting;
5728
5729/* This routine, called from the jump pad (in asm) is designed to be
5730 called from the jump pads of fast tracepoints, thus it is on the
5731 critical path. */
5732
5733IP_AGENT_EXPORT void ATTR_USED
5734gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5735{
5736 struct fast_tracepoint_ctx ctx;
5737
5738 /* Don't do anything until the trace run is completely set up. */
5739 if (!tracing)
5740 return;
5741
5742 ctx.base.type = fast_tracepoint;
5743 ctx.regs = regs;
5744 ctx.regcache_initted = 0;
fa593d66
PA
5745 /* Wrap the regblock in a register cache (in the stack, we don't
5746 want to malloc here). */
5747 ctx.regspace = alloca (register_cache_size ());
5748 if (ctx.regspace == NULL)
5749 {
5750 trace_debug ("Trace buffer block allocation failed, skipping");
5751 return;
5752 }
5753
a59306a3
YQ
5754 for (ctx.tpoint = tpoint;
5755 ctx.tpoint != NULL && ctx.tpoint->address == tpoint->address;
5756 ctx.tpoint = ctx.tpoint->next)
fa593d66 5757 {
a59306a3
YQ
5758 if (!ctx.tpoint->enabled)
5759 continue;
fa593d66 5760
a59306a3
YQ
5761 /* Multiple tracepoints of different types, such as fast tracepoint and
5762 static tracepoint, can be set at the same address. */
5763 if (ctx.tpoint->type != tpoint->type)
5764 continue;
5765
5766 /* Test the condition if present, and collect if true. */
5767 if (ctx.tpoint->cond == NULL
5768 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5769 ctx.tpoint))
5770 {
5771 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5772 ctx.tpoint->address, ctx.tpoint);
5773
5774 /* Note that this will cause original insns to be written back
5775 to where we jumped from, but that's OK because we're jumping
5776 back to the next whole instruction. This will go badly if
5777 instruction restoration is not atomic though. */
5778 if (stopping_tracepoint
5779 || trace_buffer_is_full
5780 || expr_eval_result != expr_eval_no_error)
5781 {
5782 stop_tracing ();
5783 break;
5784 }
5785 }
5786 else
5787 {
5788 /* If there was a condition and it evaluated to false, the only
5789 way we would stop tracing is if there was an error during
5790 condition expression evaluation. */
5791 if (expr_eval_result != expr_eval_no_error)
5792 {
5793 stop_tracing ();
5794 break;
5795 }
5796 }
fa593d66
PA
5797 }
5798}
5799
5800#endif
5801
5802#ifndef IN_PROCESS_AGENT
5803
6a271cae
PA
5804/* Bytecode compilation. */
5805
5806CORE_ADDR current_insn_ptr;
5807
5808int emit_error;
5809
5810struct bytecode_address
5811{
5812 int pc;
5813 CORE_ADDR address;
5814 int goto_pc;
5815 /* Offset and size of field to be modified in the goto block. */
5816 int from_offset, from_size;
5817 struct bytecode_address *next;
5818} *bytecode_address_table;
5819
5820CORE_ADDR
5821get_raw_reg_func_addr (void)
5822{
5823 return ipa_sym_addrs.addr_get_raw_reg;
5824}
5825
5826static void
5827emit_prologue (void)
5828{
5829 target_emit_ops ()->emit_prologue ();
5830}
5831
5832static void
5833emit_epilogue (void)
5834{
5835 target_emit_ops ()->emit_epilogue ();
5836}
5837
5838static void
5839emit_add (void)
5840{
5841 target_emit_ops ()->emit_add ();
5842}
5843
5844static void
5845emit_sub (void)
5846{
5847 target_emit_ops ()->emit_sub ();
5848}
5849
5850static void
5851emit_mul (void)
5852{
5853 target_emit_ops ()->emit_mul ();
5854}
5855
5856static void
5857emit_lsh (void)
5858{
5859 target_emit_ops ()->emit_lsh ();
5860}
5861
5862static void
5863emit_rsh_signed (void)
5864{
5865 target_emit_ops ()->emit_rsh_signed ();
5866}
5867
5868static void
5869emit_rsh_unsigned (void)
5870{
5871 target_emit_ops ()->emit_rsh_unsigned ();
5872}
5873
5874static void
5875emit_ext (int arg)
5876{
5877 target_emit_ops ()->emit_ext (arg);
5878}
5879
5880static void
5881emit_log_not (void)
5882{
5883 target_emit_ops ()->emit_log_not ();
5884}
5885
5886static void
5887emit_bit_and (void)
5888{
5889 target_emit_ops ()->emit_bit_and ();
5890}
5891
5892static void
5893emit_bit_or (void)
5894{
5895 target_emit_ops ()->emit_bit_or ();
5896}
5897
5898static void
5899emit_bit_xor (void)
5900{
5901 target_emit_ops ()->emit_bit_xor ();
5902}
5903
5904static void
5905emit_bit_not (void)
5906{
5907 target_emit_ops ()->emit_bit_not ();
5908}
5909
5910static void
5911emit_equal (void)
5912{
5913 target_emit_ops ()->emit_equal ();
5914}
5915
5916static void
5917emit_less_signed (void)
5918{
5919 target_emit_ops ()->emit_less_signed ();
5920}
5921
5922static void
5923emit_less_unsigned (void)
5924{
5925 target_emit_ops ()->emit_less_unsigned ();
5926}
5927
5928static void
5929emit_ref (int size)
5930{
5931 target_emit_ops ()->emit_ref (size);
5932}
5933
5934static void
5935emit_if_goto (int *offset_p, int *size_p)
5936{
5937 target_emit_ops ()->emit_if_goto (offset_p, size_p);
5938}
5939
5940static void
5941emit_goto (int *offset_p, int *size_p)
5942{
5943 target_emit_ops ()->emit_goto (offset_p, size_p);
5944}
5945
5946static void
5947write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
5948{
5949 target_emit_ops ()->write_goto_address (from, to, size);
5950}
5951
5952static void
4e29fb54 5953emit_const (LONGEST num)
6a271cae
PA
5954{
5955 target_emit_ops ()->emit_const (num);
5956}
5957
5958static void
5959emit_reg (int reg)
5960{
5961 target_emit_ops ()->emit_reg (reg);
5962}
5963
5964static void
5965emit_pop (void)
5966{
5967 target_emit_ops ()->emit_pop ();
5968}
5969
5970static void
5971emit_stack_flush (void)
5972{
5973 target_emit_ops ()->emit_stack_flush ();
5974}
5975
5976static void
5977emit_zero_ext (int arg)
5978{
5979 target_emit_ops ()->emit_zero_ext (arg);
5980}
5981
5982static void
5983emit_swap (void)
5984{
5985 target_emit_ops ()->emit_swap ();
5986}
5987
5988static void
5989emit_stack_adjust (int n)
5990{
5991 target_emit_ops ()->emit_stack_adjust (n);
5992}
5993
5994/* FN's prototype is `LONGEST(*fn)(int)'. */
5995
5996static void
5997emit_int_call_1 (CORE_ADDR fn, int arg1)
5998{
5999 target_emit_ops ()->emit_int_call_1 (fn, arg1);
6000}
6001
4e29fb54 6002/* FN's prototype is `void(*fn)(int,LONGEST)'. */
6a271cae
PA
6003
6004static void
6005emit_void_call_2 (CORE_ADDR fn, int arg1)
6006{
6007 target_emit_ops ()->emit_void_call_2 (fn, arg1);
6008}
6009
6b9801d4
SS
6010static void
6011emit_eq_goto (int *offset_p, int *size_p)
6012{
6013 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
6014}
6015
6016static void
6017emit_ne_goto (int *offset_p, int *size_p)
6018{
6019 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
6020}
6021
6022static void
6023emit_lt_goto (int *offset_p, int *size_p)
6024{
6025 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
6026}
6027
6028static void
6029emit_ge_goto (int *offset_p, int *size_p)
6030{
6031 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
6032}
6033
6034static void
6035emit_gt_goto (int *offset_p, int *size_p)
6036{
6037 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
6038}
6039
6040static void
6041emit_le_goto (int *offset_p, int *size_p)
6042{
6043 target_emit_ops ()->emit_le_goto (offset_p, size_p);
6044}
6045
6a271cae
PA
6046static enum eval_result_type compile_bytecodes (struct agent_expr *aexpr);
6047
6048static void
493e2a69
MS
6049compile_tracepoint_condition (struct tracepoint *tpoint,
6050 CORE_ADDR *jump_entry)
6a271cae
PA
6051{
6052 CORE_ADDR entry_point = *jump_entry;
6053 enum eval_result_type err;
6054
6055 trace_debug ("Starting condition compilation for tracepoint %d\n",
6056 tpoint->number);
6057
6058 /* Initialize the global pointer to the code being built. */
6059 current_insn_ptr = *jump_entry;
6060
6061 emit_prologue ();
6062
6063 err = compile_bytecodes (tpoint->cond);
6064
6065 if (err == expr_eval_no_error)
6066 {
6067 emit_epilogue ();
6068
6069 /* Record the beginning of the compiled code. */
6070 tpoint->compiled_cond = entry_point;
6071
6072 trace_debug ("Condition compilation for tracepoint %d complete\n",
6073 tpoint->number);
6074 }
6075 else
6076 {
6077 /* Leave the unfinished code in situ, but don't point to it. */
6078
6079 tpoint->compiled_cond = 0;
6080
6081 trace_debug ("Condition compilation for tracepoint %d failed, "
6082 "error code %d",
6083 tpoint->number, err);
6084 }
6085
6086 /* Update the code pointer passed in. Note that we do this even if
6087 the compile fails, so that we can look at the partial results
6088 instead of letting them be overwritten. */
6089 *jump_entry = current_insn_ptr;
6090
6091 /* Leave a gap, to aid dump decipherment. */
6092 *jump_entry += 16;
6093}
6094
6b9801d4
SS
6095/* Scan an agent expression for any evidence that the given PC is the
6096 target of a jump bytecode in the expression. */
6097
6098int
6099is_goto_target (struct agent_expr *aexpr, int pc)
6100{
6101 int i;
6102 unsigned char op;
6103
6104 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
6105 {
6106 op = aexpr->bytes[i];
6107
6108 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
6109 {
6110 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
6111 if (target == pc)
6112 return 1;
6113 }
6114 }
6115
6116 return 0;
6117}
6118
6a271cae
PA
6119/* Given an agent expression, turn it into native code. */
6120
6121static enum eval_result_type
6122compile_bytecodes (struct agent_expr *aexpr)
6123{
6124 int pc = 0;
6125 int done = 0;
6b9801d4 6126 unsigned char op, next_op;
6a271cae
PA
6127 int arg;
6128 /* This is only used to build 64-bit value for constants. */
6129 ULONGEST top;
6130 struct bytecode_address *aentry, *aentry2;
6131
6132#define UNHANDLED \
6133 do \
6134 { \
6135 trace_debug ("Cannot compile op 0x%x\n", op); \
6136 return expr_eval_unhandled_opcode; \
6137 } while (0)
6138
6139 if (aexpr->length == 0)
6140 {
6141 trace_debug ("empty agent expression\n");
6142 return expr_eval_empty_expression;
6143 }
6144
6145 bytecode_address_table = NULL;
6146
6147 while (!done)
6148 {
6149 op = aexpr->bytes[pc];
6150
6151 trace_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
6152
6153 /* Record the compiled-code address of the bytecode, for use by
6154 jump instructions. */
6155 aentry = xmalloc (sizeof (struct bytecode_address));
6156 aentry->pc = pc;
6157 aentry->address = current_insn_ptr;
6158 aentry->goto_pc = -1;
6159 aentry->from_offset = aentry->from_size = 0;
6160 aentry->next = bytecode_address_table;
6161 bytecode_address_table = aentry;
6162
6163 ++pc;
6164
6165 emit_error = 0;
6166
6167 switch (op)
6168 {
6169 case gdb_agent_op_add:
6170 emit_add ();
6171 break;
6172
6173 case gdb_agent_op_sub:
6174 emit_sub ();
6175 break;
6176
6177 case gdb_agent_op_mul:
6178 emit_mul ();
6179 break;
6180
6181 case gdb_agent_op_div_signed:
6182 UNHANDLED;
6183 break;
6184
6185 case gdb_agent_op_div_unsigned:
6186 UNHANDLED;
6187 break;
6188
6189 case gdb_agent_op_rem_signed:
6190 UNHANDLED;
6191 break;
6192
6193 case gdb_agent_op_rem_unsigned:
6194 UNHANDLED;
6195 break;
6196
6197 case gdb_agent_op_lsh:
6198 emit_lsh ();
6199 break;
6200
6201 case gdb_agent_op_rsh_signed:
6202 emit_rsh_signed ();
6203 break;
6204
6205 case gdb_agent_op_rsh_unsigned:
6206 emit_rsh_unsigned ();
6207 break;
6208
6209 case gdb_agent_op_trace:
6210 UNHANDLED;
6211 break;
6212
6213 case gdb_agent_op_trace_quick:
6214 UNHANDLED;
6215 break;
6216
6217 case gdb_agent_op_log_not:
6218 emit_log_not ();
6219 break;
6220
6221 case gdb_agent_op_bit_and:
6222 emit_bit_and ();
6223 break;
6224
6225 case gdb_agent_op_bit_or:
6226 emit_bit_or ();
6227 break;
6228
6229 case gdb_agent_op_bit_xor:
6230 emit_bit_xor ();
6231 break;
6232
6233 case gdb_agent_op_bit_not:
6234 emit_bit_not ();
6235 break;
6236
6237 case gdb_agent_op_equal:
6b9801d4
SS
6238 next_op = aexpr->bytes[pc];
6239 if (next_op == gdb_agent_op_if_goto
6240 && !is_goto_target (aexpr, pc)
6241 && target_emit_ops ()->emit_eq_goto)
6242 {
6243 trace_debug ("Combining equal & if_goto");
6244 pc += 1;
6245 aentry->pc = pc;
6246 arg = aexpr->bytes[pc++];
6247 arg = (arg << 8) + aexpr->bytes[pc++];
6248 aentry->goto_pc = arg;
6249 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
6250 }
6251 else if (next_op == gdb_agent_op_log_not
6252 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6253 && !is_goto_target (aexpr, pc + 1)
6254 && target_emit_ops ()->emit_ne_goto)
6255 {
6256 trace_debug ("Combining equal & log_not & if_goto");
6257 pc += 2;
6258 aentry->pc = pc;
6259 arg = aexpr->bytes[pc++];
6260 arg = (arg << 8) + aexpr->bytes[pc++];
6261 aentry->goto_pc = arg;
6262 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
6263 }
6264 else
6265 emit_equal ();
6a271cae
PA
6266 break;
6267
6268 case gdb_agent_op_less_signed:
6b9801d4
SS
6269 next_op = aexpr->bytes[pc];
6270 if (next_op == gdb_agent_op_if_goto
6271 && !is_goto_target (aexpr, pc))
6272 {
6273 trace_debug ("Combining less_signed & if_goto");
6274 pc += 1;
6275 aentry->pc = pc;
6276 arg = aexpr->bytes[pc++];
6277 arg = (arg << 8) + aexpr->bytes[pc++];
6278 aentry->goto_pc = arg;
6279 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
6280 }
6281 else if (next_op == gdb_agent_op_log_not
6282 && !is_goto_target (aexpr, pc)
6283 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6284 && !is_goto_target (aexpr, pc + 1))
6285 {
6286 trace_debug ("Combining less_signed & log_not & if_goto");
6287 pc += 2;
6288 aentry->pc = pc;
6289 arg = aexpr->bytes[pc++];
6290 arg = (arg << 8) + aexpr->bytes[pc++];
6291 aentry->goto_pc = arg;
6292 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
6293 }
6294 else
6295 emit_less_signed ();
6a271cae
PA
6296 break;
6297
6298 case gdb_agent_op_less_unsigned:
6299 emit_less_unsigned ();
6300 break;
6301
6302 case gdb_agent_op_ext:
6303 arg = aexpr->bytes[pc++];
6304 if (arg < (sizeof (LONGEST) * 8))
6305 emit_ext (arg);
6306 break;
6307
6308 case gdb_agent_op_ref8:
6309 emit_ref (1);
6310 break;
6311
6312 case gdb_agent_op_ref16:
6313 emit_ref (2);
6314 break;
6315
6316 case gdb_agent_op_ref32:
6317 emit_ref (4);
6318 break;
6319
6320 case gdb_agent_op_ref64:
6321 emit_ref (8);
6322 break;
6323
6324 case gdb_agent_op_if_goto:
6325 arg = aexpr->bytes[pc++];
6326 arg = (arg << 8) + aexpr->bytes[pc++];
6327 aentry->goto_pc = arg;
6328 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
6329 break;
6330
6331 case gdb_agent_op_goto:
6332 arg = aexpr->bytes[pc++];
6333 arg = (arg << 8) + aexpr->bytes[pc++];
6334 aentry->goto_pc = arg;
6335 emit_goto (&(aentry->from_offset), &(aentry->from_size));
6336 break;
6337
6338 case gdb_agent_op_const8:
6339 emit_stack_flush ();
6340 top = aexpr->bytes[pc++];
6341 emit_const (top);
6342 break;
6343
6344 case gdb_agent_op_const16:
6345 emit_stack_flush ();
6346 top = aexpr->bytes[pc++];
6347 top = (top << 8) + aexpr->bytes[pc++];
6348 emit_const (top);
6349 break;
6350
6351 case gdb_agent_op_const32:
6352 emit_stack_flush ();
6353 top = aexpr->bytes[pc++];
6354 top = (top << 8) + aexpr->bytes[pc++];
6355 top = (top << 8) + aexpr->bytes[pc++];
6356 top = (top << 8) + aexpr->bytes[pc++];
6357 emit_const (top);
6358 break;
6359
6360 case gdb_agent_op_const64:
6361 emit_stack_flush ();
6362 top = aexpr->bytes[pc++];
6363 top = (top << 8) + aexpr->bytes[pc++];
6364 top = (top << 8) + aexpr->bytes[pc++];
6365 top = (top << 8) + aexpr->bytes[pc++];
6366 top = (top << 8) + aexpr->bytes[pc++];
6367 top = (top << 8) + aexpr->bytes[pc++];
6368 top = (top << 8) + aexpr->bytes[pc++];
6369 top = (top << 8) + aexpr->bytes[pc++];
6370 emit_const (top);
6371 break;
6372
6373 case gdb_agent_op_reg:
6374 emit_stack_flush ();
6375 arg = aexpr->bytes[pc++];
6376 arg = (arg << 8) + aexpr->bytes[pc++];
6377 emit_reg (arg);
6378 break;
6379
6380 case gdb_agent_op_end:
6381 trace_debug ("At end of expression\n");
6382
6383 /* Assume there is one stack element left, and that it is
6384 cached in "top" where emit_epilogue can get to it. */
6385 emit_stack_adjust (1);
6386
6387 done = 1;
6388 break;
6389
6390 case gdb_agent_op_dup:
6391 /* In our design, dup is equivalent to stack flushing. */
6392 emit_stack_flush ();
6393 break;
6394
6395 case gdb_agent_op_pop:
6396 emit_pop ();
6397 break;
6398
6399 case gdb_agent_op_zero_ext:
6400 arg = aexpr->bytes[pc++];
6401 if (arg < (sizeof (LONGEST) * 8))
6402 emit_zero_ext (arg);
6403 break;
6404
6405 case gdb_agent_op_swap:
6b9801d4
SS
6406 next_op = aexpr->bytes[pc];
6407 /* Detect greater-than comparison sequences. */
6408 if (next_op == gdb_agent_op_less_signed
6409 && !is_goto_target (aexpr, pc)
6410 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6411 && !is_goto_target (aexpr, pc + 1))
6412 {
6413 trace_debug ("Combining swap & less_signed & if_goto");
6414 pc += 2;
6415 aentry->pc = pc;
6416 arg = aexpr->bytes[pc++];
6417 arg = (arg << 8) + aexpr->bytes[pc++];
6418 aentry->goto_pc = arg;
6419 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
6420 }
6421 else if (next_op == gdb_agent_op_less_signed
6422 && !is_goto_target (aexpr, pc)
6423 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
6424 && !is_goto_target (aexpr, pc + 1)
6425 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
6426 && !is_goto_target (aexpr, pc + 2))
6427 {
6428 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6429 pc += 3;
6430 aentry->pc = pc;
6431 arg = aexpr->bytes[pc++];
6432 arg = (arg << 8) + aexpr->bytes[pc++];
6433 aentry->goto_pc = arg;
6434 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
6435 }
6436 else
6437 emit_swap ();
6a271cae
PA
6438 break;
6439
6440 case gdb_agent_op_getv:
6441 emit_stack_flush ();
6442 arg = aexpr->bytes[pc++];
6443 arg = (arg << 8) + aexpr->bytes[pc++];
6444 emit_int_call_1 (ipa_sym_addrs.addr_get_trace_state_variable_value,
6445 arg);
6446 break;
6447
6448 case gdb_agent_op_setv:
6449 arg = aexpr->bytes[pc++];
6450 arg = (arg << 8) + aexpr->bytes[pc++];
6451 emit_void_call_2 (ipa_sym_addrs.addr_set_trace_state_variable_value,
6452 arg);
6453 break;
6454
6455 case gdb_agent_op_tracev:
6456 UNHANDLED;
6457 break;
6458
6459 /* GDB never (currently) generates any of these ops. */
6460 case gdb_agent_op_float:
6461 case gdb_agent_op_ref_float:
6462 case gdb_agent_op_ref_double:
6463 case gdb_agent_op_ref_long_double:
6464 case gdb_agent_op_l_to_d:
6465 case gdb_agent_op_d_to_l:
6466 case gdb_agent_op_trace16:
6467 UNHANDLED;
6468 break;
6469
6470 default:
6471 trace_debug ("Agent expression op 0x%x not recognized\n", op);
6472 /* Don't struggle on, things will just get worse. */
6473 return expr_eval_unrecognized_opcode;
6474 }
6475
6476 /* This catches errors that occur in target-specific code
6477 emission. */
6478 if (emit_error)
6479 {
6480 trace_debug ("Error %d while emitting code for %s\n",
94d5e490 6481 emit_error, gdb_agent_op_name (op));
6a271cae
PA
6482 return expr_eval_unhandled_opcode;
6483 }
6484
94d5e490 6485 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op));
6a271cae
PA
6486 }
6487
6488 /* Now fill in real addresses as goto destinations. */
6489 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
6490 {
6491 int written = 0;
6492
6493 if (aentry->goto_pc < 0)
6494 continue;
6495
6496 /* Find the location that we are going to, and call back into
6497 target-specific code to write the actual address or
6498 displacement. */
6499 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
6500 {
6501 if (aentry2->pc == aentry->goto_pc)
6502 {
6503 trace_debug ("Want to jump from %s to %s\n",
6504 paddress (aentry->address),
6505 paddress (aentry2->address));
6506 write_goto_address (aentry->address + aentry->from_offset,
6507 aentry2->address, aentry->from_size);
6508 written = 1;
6509 break;
6510 }
6511 }
6512
6513 /* Error out if we didn't find a destination. */
6514 if (!written)
6515 {
6516 trace_debug ("Destination of goto %d not found\n",
6517 aentry->goto_pc);
6518 return expr_eval_invalid_goto;
6519 }
6520 }
6521
6522 return expr_eval_no_error;
6523}
6524
fa593d66
PA
6525/* We'll need to adjust these when we consider bi-arch setups, and big
6526 endian machines. */
6527
6528static int
6529write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
6530{
6531 return write_inferior_memory (where,
6532 (unsigned char *) &ptr, sizeof (void *));
6533}
6534
6535/* The base pointer of the IPA's heap. This is the only memory the
6536 IPA is allowed to use. The IPA should _not_ call the inferior's
6537 `malloc' during operation. That'd be slow, and, most importantly,
6538 it may not be safe. We may be collecting a tracepoint in a signal
6539 handler, for example. */
6540static CORE_ADDR target_tp_heap;
6541
6542/* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6543 to 8 bytes. */
6544
6545static CORE_ADDR
6546target_malloc (ULONGEST size)
6547{
6548 CORE_ADDR ptr;
6549
6550 if (target_tp_heap == 0)
6551 {
6552 /* We have the pointer *address*, need what it points to. */
6553 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
6554 &target_tp_heap))
6555 fatal ("could get target heap head pointer");
6556 }
6557
6558 ptr = target_tp_heap;
6559 target_tp_heap += size;
6560
6561 /* Pad to 8-byte alignment. */
6562 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
6563
6564 return ptr;
6565}
6566
6567static CORE_ADDR
6568download_agent_expr (struct agent_expr *expr)
6569{
6570 CORE_ADDR expr_addr;
6571 CORE_ADDR expr_bytes;
6572
6573 expr_addr = target_malloc (sizeof (*expr));
6574 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
6575
6576 expr_bytes = target_malloc (expr->length);
6577 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
6578 expr_bytes);
6579 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
6580
6581 return expr_addr;
6582}
6583
6584/* Align V up to N bits. */
6585#define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6586
5c73ff4e
YQ
6587/* Sync tracepoint with IPA, but leave maintenance of linked list to caller. */
6588
fa593d66 6589static void
5c73ff4e 6590download_tracepoint_1 (struct tracepoint *tpoint)
fa593d66 6591{
5c73ff4e
YQ
6592 struct tracepoint target_tracepoint;
6593 CORE_ADDR tpptr = 0;
fa593d66 6594
5c73ff4e
YQ
6595 gdb_assert (tpoint->type == fast_tracepoint
6596 || tpoint->type == static_tracepoint);
fa593d66 6597
5c73ff4e 6598 if (tpoint->cond != NULL && target_emit_ops () != NULL)
fa593d66 6599 {
5c73ff4e 6600 CORE_ADDR jentry, jump_entry;
fa593d66 6601
5c73ff4e 6602 jentry = jump_entry = get_jump_space_head ();
fa593d66 6603
5c73ff4e 6604 if (tpoint->cond != NULL)
6a271cae 6605 {
5c73ff4e
YQ
6606 /* Pad to 8-byte alignment. (needed?) */
6607 /* Actually this should be left for the target to
6608 decide. */
6609 jentry = UALIGN (jentry, 8);
6610
6611 compile_tracepoint_condition (tpoint, &jentry);
6612 }
6613
6614 /* Pad to 8-byte alignment. */
6615 jentry = UALIGN (jentry, 8);
6616 claim_jump_space (jentry - jump_entry);
6617 }
6a271cae 6618
5c73ff4e 6619 target_tracepoint = *tpoint;
6a271cae 6620
5c73ff4e
YQ
6621 tpptr = target_malloc (sizeof (*tpoint));
6622 tpoint->obj_addr_on_target = tpptr;
6623
6624 /* Write the whole object. We'll fix up its pointers in a bit.
6625 Assume no next for now. This is fixed up above on the next
6626 iteration, if there's any. */
6627 target_tracepoint.next = NULL;
6628 /* Need to clear this here too, since we're downloading the
6629 tracepoints before clearing our own copy. */
6630 target_tracepoint.hit_count = 0;
6631
6632 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
6633 sizeof (target_tracepoint));
6634
6635 if (tpoint->cond)
6636 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6637 cond),
6638 download_agent_expr (tpoint->cond));
6639
6640 if (tpoint->numactions)
6641 {
6642 int i;
6643 CORE_ADDR actions_array;
6644
6645 /* The pointers array. */
6646 actions_array
6647 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
6648 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6649 actions),
6650 actions_array);
6651
6652 /* Now for each pointer, download the action. */
6653 for (i = 0; i < tpoint->numactions; i++)
6654 {
6655 CORE_ADDR ipa_action = 0;
6656 struct tracepoint_action *action = tpoint->actions[i];
6657
6658 switch (action->type)
6a271cae 6659 {
5c73ff4e
YQ
6660 case 'M':
6661 ipa_action
6662 = target_malloc (sizeof (struct collect_memory_action));
6663 write_inferior_memory (ipa_action,
6664 (unsigned char *) action,
6665 sizeof (struct collect_memory_action));
6666 break;
6667 case 'R':
6668 ipa_action
6669 = target_malloc (sizeof (struct collect_registers_action));
6670 write_inferior_memory (ipa_action,
6671 (unsigned char *) action,
6672 sizeof (struct collect_registers_action));
6673 break;
6674 case 'X':
6675 {
6676 CORE_ADDR expr;
6677 struct eval_expr_action *eaction
6678 = (struct eval_expr_action *) action;
6a271cae 6679
5c73ff4e
YQ
6680 ipa_action = target_malloc (sizeof (*eaction));
6681 write_inferior_memory (ipa_action,
6682 (unsigned char *) eaction,
6683 sizeof (*eaction));
6684
6685 expr = download_agent_expr (eaction->expr);
6686 write_inferior_data_ptr
6687 (ipa_action + offsetof (struct eval_expr_action, expr),
6688 expr);
6689 break;
6690 }
6691 case 'L':
6692 ipa_action = target_malloc
6693 (sizeof (struct collect_static_trace_data_action));
6694 write_inferior_memory
6695 (ipa_action,
6696 (unsigned char *) action,
6697 sizeof (struct collect_static_trace_data_action));
6698 break;
6699 default:
6700 trace_debug ("unknown trace action '%c', ignoring",
6701 action->type);
6702 break;
6a271cae
PA
6703 }
6704
5c73ff4e
YQ
6705 if (ipa_action != 0)
6706 write_inferior_data_ptr
6707 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
6708 ipa_action);
6a271cae 6709 }
5c73ff4e
YQ
6710 }
6711}
6a271cae 6712
1e4d1764
YQ
6713static void
6714download_tracepoint (struct tracepoint *tpoint)
6715{
6716 struct tracepoint *tp, *tp_prev;
6717
6718 if (tpoint->type != fast_tracepoint
6719 && tpoint->type != static_tracepoint)
6720 return;
6721
6722 download_tracepoint_1 (tpoint);
6723
6724 /* Find the previous entry of TPOINT, which is fast tracepoint or
6725 static tracepoint. */
6726 tp_prev = NULL;
6727 for (tp = tracepoints; tp != tpoint; tp = tp->next)
6728 {
6729 if (tp->type == fast_tracepoint || tp->type == static_tracepoint)
6730 tp_prev = tp;
6731 }
6732
6733 if (tp_prev)
6734 {
6735 CORE_ADDR tp_prev_target_next_addr;
6736
6737 /* Insert TPOINT after TP_PREV in IPA. */
6738 if (read_inferior_data_pointer (tp_prev->obj_addr_on_target
6739 + offsetof (struct tracepoint, next),
6740 &tp_prev_target_next_addr))
6741 fatal ("error reading `tp_prev->next'");
6742
6743 /* tpoint->next = tp_prev->next */
6744 write_inferior_data_ptr (tpoint->obj_addr_on_target
6745 + offsetof (struct tracepoint, next),
6746 tp_prev_target_next_addr);
6747 /* tp_prev->next = tpoint */
6748 write_inferior_data_ptr (tp_prev->obj_addr_on_target
6749 + offsetof (struct tracepoint, next),
6750 tpoint->obj_addr_on_target);
6751 }
6752 else
6753 /* First object in list, set the head pointer in the
6754 inferior. */
6755 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints,
6756 tpoint->obj_addr_on_target);
6757
6758}
6759
5c73ff4e
YQ
6760static void
6761download_tracepoints (void)
6762{
6763 CORE_ADDR tpptr = 0, prev_tpptr = 0;
6764 struct tracepoint *tpoint;
6765
6766 /* Start out empty. */
6767 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
6768
6769 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6770 {
6771 if (tpoint->type != fast_tracepoint
6772 && tpoint->type != static_tracepoint)
6773 continue;
fa593d66
PA
6774
6775 prev_tpptr = tpptr;
5c73ff4e
YQ
6776
6777 download_tracepoint_1 (tpoint);
6778
6779 tpptr = tpoint->obj_addr_on_target;
fa593d66
PA
6780
6781 if (tpoint == tracepoints)
6782 {
6783 /* First object in list, set the head pointer in the
6784 inferior. */
6785 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
6786 }
6787 else
6788 {
6789 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
6790 next),
6791 tpptr);
6792 }
fa593d66
PA
6793 }
6794}
6795
6796static void
6797download_trace_state_variables (void)
6798{
6799 CORE_ADDR ptr = 0, prev_ptr = 0;
6800 struct trace_state_variable *tsv;
6801
6802 /* Start out empty. */
6803 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
6804
6805 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
6806 {
6807 struct trace_state_variable target_tsv;
6808
6809 /* TSV's with a getter have been initialized equally in both the
6810 inferior and GDBserver. Skip them. */
6811 if (tsv->getter != NULL)
6812 continue;
6813
6814 target_tsv = *tsv;
6815
6816 prev_ptr = ptr;
6817 ptr = target_malloc (sizeof (*tsv));
6818
6819 if (tsv == trace_state_variables)
6820 {
6821 /* First object in list, set the head pointer in the
6822 inferior. */
6823
6824 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
6825 ptr);
6826 }
6827 else
6828 {
6829 write_inferior_data_ptr (prev_ptr
6830 + offsetof (struct trace_state_variable,
6831 next),
6832 ptr);
6833 }
6834
6835 /* Write the whole object. We'll fix up its pointers in a bit.
6836 Assume no next, fixup when needed. */
6837 target_tsv.next = NULL;
6838
6839 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
6840 sizeof (target_tsv));
6841
6842 if (tsv->name != NULL)
6843 {
6844 size_t size = strlen (tsv->name) + 1;
6845 CORE_ADDR name_addr = target_malloc (size);
6846 write_inferior_memory (name_addr,
6847 (unsigned char *) tsv->name, size);
6848 write_inferior_data_ptr (ptr
6849 + offsetof (struct trace_state_variable,
6850 name),
6851 name_addr);
6852 }
6853
6854 if (tsv->getter != NULL)
6855 {
6856 fatal ("what to do with these?");
6857 }
6858 }
6859
6860 if (prev_ptr != 0)
6861 {
6862 /* Fixup the next pointer in the last item in the list. */
493e2a69
MS
6863 write_inferior_data_ptr (prev_ptr
6864 + offsetof (struct trace_state_variable,
6865 next), 0);
fa593d66
PA
6866 }
6867}
6868
6869/* Upload complete trace frames out of the IP Agent's trace buffer
6870 into GDBserver's trace buffer. This always uploads either all or
6871 no trace frames. This is the counter part of
6872 `trace_alloc_trace_buffer'. See its description of the atomic
6873 synching mechanism. */
6874
6875static void
6876upload_fast_traceframes (void)
6877{
6878 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
6879 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
6880 CORE_ADDR tf;
6881 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
6882 unsigned int curr_tbctrl_idx;
6883 unsigned int ipa_trace_buffer_ctrl_curr;
6884 unsigned int ipa_trace_buffer_ctrl_curr_old;
6885 CORE_ADDR ipa_trace_buffer_ctrl_addr;
6886 struct breakpoint *about_to_request_buffer_space_bkpt;
6887 CORE_ADDR ipa_trace_buffer_lo;
6888 CORE_ADDR ipa_trace_buffer_hi;
6889
6890 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6891 &ipa_traceframe_read_count_racy))
6892 {
6893 /* This will happen in most targets if the current thread is
6894 running. */
6895 return;
6896 }
6897
6898 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6899 &ipa_traceframe_write_count_racy))
6900 return;
6901
6902 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
493e2a69
MS
6903 ipa_traceframe_write_count_racy
6904 - ipa_traceframe_read_count_racy,
6905 ipa_traceframe_write_count_racy,
6906 ipa_traceframe_read_count_racy);
fa593d66
PA
6907
6908 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
6909 return;
6910
6911 about_to_request_buffer_space_bkpt
6912 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
6913 NULL);
6914
6915 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6916 &ipa_trace_buffer_ctrl_curr))
6917 return;
6918
6919 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
6920
6921 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
6922
6923 {
6924 unsigned int prev, counter;
6925
6926 /* Update the token, with new counters, and the GDBserver stamp
6927 bit. Alway reuse the current TBC index. */
6928 prev = ipa_trace_buffer_ctrl_curr & 0x0007ff00;
6929 counter = (prev + 0x100) & 0x0007ff00;
6930
6931 ipa_trace_buffer_ctrl_curr = (0x80000000
6932 | (prev << 12)
6933 | counter
6934 | curr_tbctrl_idx);
6935 }
6936
6937 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6938 ipa_trace_buffer_ctrl_curr))
6939 return;
6940
6941 trace_debug ("Lib: Committed %08x -> %08x",
6942 ipa_trace_buffer_ctrl_curr_old,
6943 ipa_trace_buffer_ctrl_curr);
6944
6945 /* Re-read these, now that we've installed the
6946 `about_to_request_buffer_space' breakpoint/lock. A thread could
6947 have finished a traceframe between the last read of these
6948 counters and setting the breakpoint above. If we start
6949 uploading, we never want to leave this function with
6950 traceframe_read_count != 0, otherwise, GDBserver could end up
6951 incrementing the counter tokens more than once (due to event loop
6952 nesting), which would break the IP agent's "effective" detection
6953 (see trace_alloc_trace_buffer). */
6954 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6955 &ipa_traceframe_read_count))
6956 return;
6957 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6958 &ipa_traceframe_write_count))
6959 return;
6960
6961 if (debug_threads)
6962 {
6963 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6964 ipa_traceframe_write_count - ipa_traceframe_read_count,
6965 ipa_traceframe_write_count, ipa_traceframe_read_count);
6966
6967 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
6968 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
6969 trace_debug ("note that ipa_traceframe_count's parts changed");
6970 }
6971
6972 /* Get the address of the current TBC object (the IP agent has an
6973 array of 3 such objects). The index is stored in the TBC
6974 token. */
6975 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
6976 ipa_trace_buffer_ctrl_addr
6977 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
6978
6979 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
6980 (unsigned char *) &ipa_trace_buffer_ctrl,
6981 sizeof (struct ipa_trace_buffer_control)))
6982 return;
6983
6984 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
6985 &ipa_trace_buffer_lo))
6986 return;
6987 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6988 &ipa_trace_buffer_hi))
6989 return;
6990
6991 /* Offsets are easier to grok for debugging than raw addresses,
6992 especially for the small trace buffer sizes that are useful for
6993 testing. */
6994 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6995 "endfree=%d wrap=%d hi=%d",
6996 curr_tbctrl_idx,
6997 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6998 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6999 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
7000 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
7001 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
7002
7003 /* Note that the IPA's buffer is always circular. */
7004
7005#define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
7006
7007#define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
7008 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
7009
7010#define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
7011 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
7012 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
7013 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
7014 : 0))
7015
7016 tf = IPA_FIRST_TRACEFRAME ();
7017
7018 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
7019 {
7020 struct tracepoint *tpoint;
7021 struct traceframe *tframe;
7022 unsigned char *block;
7023 struct traceframe ipa_tframe;
7024
7025 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
7026 offsetof (struct traceframe, data)))
7027 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
7028
7029 if (ipa_tframe.tpnum == 0)
7030 fatal ("Uploading: No (more) fast traceframes, but "
7031 "ipa_traceframe_count == %u??\n",
7032 ipa_traceframe_write_count - ipa_traceframe_read_count);
7033
7034 /* Note that this will be incorrect for multi-location
7035 tracepoints... */
7036 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
7037
7038 tframe = add_traceframe (tpoint);
7039 if (tframe == NULL)
7040 {
7041 trace_buffer_is_full = 1;
7042 trace_debug ("Uploading: trace buffer is full");
7043 }
7044 else
7045 {
7046 /* Copy the whole set of blocks in one go for now. FIXME:
7047 split this in smaller blocks. */
7048 block = add_traceframe_block (tframe, ipa_tframe.data_size);
7049 if (block != NULL)
7050 {
493e2a69
MS
7051 if (read_inferior_memory (tf
7052 + offsetof (struct traceframe, data),
fa593d66
PA
7053 block, ipa_tframe.data_size))
7054 error ("Uploading: Couldn't read traceframe data at %s\n",
7055 paddress (tf + offsetof (struct traceframe, data)));
7056 }
7057
7058 trace_debug ("Uploading: traceframe didn't fit");
7059 finish_traceframe (tframe);
7060 }
7061
7062 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
7063
7064 /* If we freed the traceframe that wrapped around, go back
7065 to the non-wrap case. */
7066 if (tf < ipa_trace_buffer_ctrl.start)
7067 {
7068 trace_debug ("Lib: Discarding past the wraparound");
7069 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
7070 }
7071 ipa_trace_buffer_ctrl.start = tf;
7072 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
7073 ++ipa_traceframe_read_count;
7074
7075 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
7076 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
7077 {
7078 trace_debug ("Lib: buffer is fully empty. "
7079 "Trace buffer [%d] start=%d free=%d endfree=%d",
7080 curr_tbctrl_idx,
7081 (int) (ipa_trace_buffer_ctrl.start
7082 - ipa_trace_buffer_lo),
7083 (int) (ipa_trace_buffer_ctrl.free
7084 - ipa_trace_buffer_lo),
7085 (int) (ipa_trace_buffer_ctrl.end_free
7086 - ipa_trace_buffer_lo));
7087
7088 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
7089 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
7090 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
7091 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
7092 }
7093
7094 trace_debug ("Uploaded a traceframe\n"
7095 "Lib: Trace buffer [%d] start=%d free=%d "
7096 "endfree=%d wrap=%d hi=%d",
7097 curr_tbctrl_idx,
7098 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
7099 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
493e2a69
MS
7100 (int) (ipa_trace_buffer_ctrl.end_free
7101 - ipa_trace_buffer_lo),
fa593d66
PA
7102 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
7103 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
7104 }
7105
7106 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
7107 (unsigned char *) &ipa_trace_buffer_ctrl,
7108 sizeof (struct ipa_trace_buffer_control)))
7109 return;
7110
7111 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
7112 ipa_traceframe_read_count);
7113
7114 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
7115
7116 pause_all (1);
7117 cancel_breakpoints ();
7118
7119 delete_breakpoint (about_to_request_buffer_space_bkpt);
7120 about_to_request_buffer_space_bkpt = NULL;
7121
7122 unpause_all (1);
7123
7124 if (trace_buffer_is_full)
7125 stop_tracing ();
7126}
7127#endif
7128
7129#ifdef IN_PROCESS_AGENT
7130
0fb4aa4b
PA
7131IP_AGENT_EXPORT int ust_loaded;
7132IP_AGENT_EXPORT char cmd_buf[CMD_BUF_SIZE];
fa593d66 7133
0fb4aa4b 7134#ifdef HAVE_UST
fa593d66 7135
0fb4aa4b
PA
7136/* Static tracepoints. */
7137
7138/* UST puts a "struct tracepoint" in the global namespace, which
7139 conflicts with our tracepoint. Arguably, being a library, it
7140 shouldn't take ownership of such a generic name. We work around it
7141 here. */
7142#define tracepoint ust_tracepoint
7143#include <ust/ust.h>
7144#undef tracepoint
7145
7146extern int serialize_to_text (char *outbuf, int bufsize,
7147 const char *fmt, va_list ap);
7148
7149#define GDB_PROBE_NAME "gdb"
7150
7151/* We dynamically search for the UST symbols instead of linking them
7152 in. This lets the user decide if the application uses static
7153 tracepoints, instead of always pulling libust.so in. This vector
7154 holds pointers to all functions we care about. */
7155
7156static struct
fa593d66 7157{
0fb4aa4b
PA
7158 int (*serialize_to_text) (char *outbuf, int bufsize,
7159 const char *fmt, va_list ap);
7160
7161 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
7162 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
7163
7164 int (*ltt_marker_connect) (const char *channel, const char *mname,
7165 const char *pname);
7166 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
7167 const char *pname);
7168
7169 void (*marker_iter_start) (struct marker_iter *iter);
7170 void (*marker_iter_next) (struct marker_iter *iter);
7171 void (*marker_iter_stop) (struct marker_iter *iter);
7172 void (*marker_iter_reset) (struct marker_iter *iter);
7173} ust_ops;
7174
7175#include <dlfcn.h>
7176
7177/* Cast through typeof to catch incompatible API changes. Since UST
7178 only builds with gcc, we can freely use gcc extensions here
7179 too. */
7180#define GET_UST_SYM(SYM) \
7181 do \
7182 { \
7183 if (ust_ops.SYM == NULL) \
7184 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
7185 if (ust_ops.SYM == NULL) \
7186 return 0; \
7187 } while (0)
7188
7189#define USTF(SYM) ust_ops.SYM
7190
7191/* Get pointers to all libust.so functions we care about. */
7192
7193static int
7194dlsym_ust (void)
7195{
7196 GET_UST_SYM (serialize_to_text);
7197
7198 GET_UST_SYM (ltt_probe_register);
7199 GET_UST_SYM (ltt_probe_unregister);
7200 GET_UST_SYM (ltt_marker_connect);
7201 GET_UST_SYM (ltt_marker_disconnect);
7202
7203 GET_UST_SYM (marker_iter_start);
7204 GET_UST_SYM (marker_iter_next);
7205 GET_UST_SYM (marker_iter_stop);
7206 GET_UST_SYM (marker_iter_reset);
7207
7208 ust_loaded = 1;
7209 return 1;
fa593d66
PA
7210}
7211
0fb4aa4b
PA
7212/* Given an UST marker, return the matching gdb static tracepoint.
7213 The match is done by address. */
7214
7215static struct tracepoint *
7216ust_marker_to_static_tracepoint (const struct marker *mdata)
7217{
7218 struct tracepoint *tpoint;
7219
7220 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
7221 {
d248b706 7222 if (tpoint->type != static_tracepoint)
0fb4aa4b
PA
7223 continue;
7224
7225 if (tpoint->address == (uintptr_t) mdata->location)
7226 return tpoint;
7227 }
7228
7229 return NULL;
7230}
7231
7232/* The probe function we install on lttng/ust markers. Whenever a
7233 probed ust marker is hit, this function is called. This is similar
7234 to gdb_collect, only for static tracepoints, instead of fast
7235 tracepoints. */
7236
7237static void
7238gdb_probe (const struct marker *mdata, void *probe_private,
7239 struct registers *regs, void *call_private,
7240 const char *fmt, va_list *args)
7241{
7242 struct tracepoint *tpoint;
7243 struct static_tracepoint_ctx ctx;
7244
7245 /* Don't do anything until the trace run is completely set up. */
7246 if (!tracing)
7247 {
7248 trace_debug ("gdb_probe: not tracing\n");
7249 return;
7250 }
7251
7252 ctx.base.type = static_tracepoint;
7253 ctx.regcache_initted = 0;
7254 ctx.regs = regs;
7255 ctx.fmt = fmt;
7256 ctx.args = args;
7257
7258 /* Wrap the regblock in a register cache (in the stack, we don't
7259 want to malloc here). */
7260 ctx.regspace = alloca (register_cache_size ());
7261 if (ctx.regspace == NULL)
7262 {
7263 trace_debug ("Trace buffer block allocation failed, skipping");
7264 return;
7265 }
7266
7267 tpoint = ust_marker_to_static_tracepoint (mdata);
7268 if (tpoint == NULL)
7269 {
7270 trace_debug ("gdb_probe: marker not known: "
7271 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7272 mdata->location, mdata->channel,
7273 mdata->name, mdata->format);
7274 return;
7275 }
7276
d248b706
KY
7277 if (!tpoint->enabled)
7278 {
7279 trace_debug ("gdb_probe: tracepoint disabled");
7280 return;
7281 }
7282
0fb4aa4b
PA
7283 ctx.tpoint = tpoint;
7284
7285 trace_debug ("gdb_probe: collecting marker: "
7286 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
7287 mdata->location, mdata->channel,
7288 mdata->name, mdata->format);
7289
7290 /* Test the condition if present, and collect if true. */
7291 if (tpoint->cond == NULL
7292 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
7293 tpoint))
7294 {
7295 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
7296 tpoint->address, tpoint);
7297
7298 if (stopping_tracepoint
7299 || trace_buffer_is_full
7300 || expr_eval_result != expr_eval_no_error)
7301 stop_tracing ();
7302 }
7303 else
7304 {
7305 /* If there was a condition and it evaluated to false, the only
7306 way we would stop tracing is if there was an error during
7307 condition expression evaluation. */
7308 if (expr_eval_result != expr_eval_no_error)
7309 stop_tracing ();
7310 }
7311}
7312
7313/* Called if the gdb static tracepoint requested collecting "$_sdata",
7314 static tracepoint string data. This is a string passed to the
7315 tracing library by the user, at the time of the tracepoint marker
7316 call. E.g., in the UST marker call:
7317
7318 trace_mark (ust, bar33, "str %s", "FOOBAZ");
7319
7320 the collected data is "str FOOBAZ".
7321*/
7322
7323static void
7324collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
7325 CORE_ADDR stop_pc,
7326 struct tracepoint *tpoint,
7327 struct traceframe *tframe)
7328{
7329 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
7330 unsigned char *bufspace;
7331 int size;
7332 va_list copy;
7333 unsigned short blocklen;
7334
7335 if (umd == NULL)
7336 {
7337 trace_debug ("Wanted to collect static trace data, "
7338 "but there's no static trace data");
7339 return;
7340 }
7341
7342 va_copy (copy, *umd->args);
7343 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
7344 va_end (copy);
7345
7346 trace_debug ("Want to collect ust data");
7347
7348 /* 'S' + size + string */
7349 bufspace = add_traceframe_block (tframe,
7350 1 + sizeof (blocklen) + size + 1);
7351 if (bufspace == NULL)
7352 {
7353 trace_debug ("Trace buffer block allocation failed, skipping");
7354 return;
7355 }
7356
7357 /* Identify a static trace data block. */
7358 *bufspace = 'S';
7359
7360 blocklen = size + 1;
7361 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
7362
7363 va_copy (copy, *umd->args);
7364 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
7365 size + 1, umd->fmt, copy);
7366 va_end (copy);
7367
7368 trace_debug ("Storing static tracepoint data in regblock: %s",
7369 bufspace + 1 + sizeof (blocklen));
7370}
7371
7372/* The probe to register with lttng/ust. */
7373static struct ltt_available_probe gdb_ust_probe =
7374 {
7375 GDB_PROBE_NAME,
7376 NULL,
7377 gdb_probe,
7378 };
7379
7380#endif /* HAVE_UST */
7381#endif /* IN_PROCESS_AGENT */
7382
7383#ifdef HAVE_UST
7384
7385#include <sys/socket.h>
7386#include <sys/un.h>
7387
7388#ifndef UNIX_PATH_MAX
7389#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
7390#endif
7391
7392/* Where we put the socked used for synchronization. */
7393#define SOCK_DIR P_tmpdir
7394
7395#endif /* HAVE_UST */
7396
7397#ifndef IN_PROCESS_AGENT
7398
7399#ifdef HAVE_UST
7400
7401static int
7402gdb_ust_connect_sync_socket (int pid)
7403{
7404 struct sockaddr_un addr;
7405 int res, fd;
7406 char path[UNIX_PATH_MAX];
7407
6cebaf6e 7408 res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", SOCK_DIR, pid);
0fb4aa4b
PA
7409 if (res >= UNIX_PATH_MAX)
7410 {
7411 trace_debug ("string overflow allocating socket name");
7412 return -1;
7413 }
7414
7415 res = fd = socket (PF_UNIX, SOCK_STREAM, 0);
7416 if (res == -1)
7417 {
7418 warning ("error opening sync socket: %s\n", strerror (errno));
7419 return -1;
7420 }
7421
7422 addr.sun_family = AF_UNIX;
7423
6cebaf6e 7424 res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
0fb4aa4b
PA
7425 if (res >= UNIX_PATH_MAX)
7426 {
7427 warning ("string overflow allocating socket name\n");
7428 close (fd);
7429 return -1;
7430 }
7431
7432 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
7433 if (res == -1)
7434 {
7435 warning ("error connecting sync socket (%s): %s. "
7436 "Make sure the directory exists and that it is writable.",
7437 path, strerror (errno));
7438 close (fd);
7439 return -1;
7440 }
7441
7442 return fd;
7443}
7444
7445/* Resume thread PTID. */
7446
7447static void
7448resume_thread (ptid_t ptid)
7449{
7450 struct thread_resume resume_info;
7451
7452 resume_info.thread = ptid;
7453 resume_info.kind = resume_continue;
7454 resume_info.sig = TARGET_SIGNAL_0;
7455 (*the_target->resume) (&resume_info, 1);
7456}
7457
7458/* Stop thread PTID. */
7459
7460static void
7461stop_thread (ptid_t ptid)
7462{
7463 struct thread_resume resume_info;
7464
7465 resume_info.thread = ptid;
7466 resume_info.kind = resume_stop;
7467 resume_info.sig = TARGET_SIGNAL_0;
7468 (*the_target->resume) (&resume_info, 1);
7469}
7470
7471/* Ask the in-process agent to run a command. Since we don't want to
7472 have to handle the IPA hitting breakpoints while running the
7473 command, we pause all threads, remove all breakpoints, and then set
7474 the helper thread re-running. We communicate with the helper
7475 thread by means of direct memory xfering, and a socket for
7476 synchronization. */
7477
7478static int
7479run_inferior_command (char *cmd)
7480{
7481 int err = -1;
7482 int fd = -1;
7483 int pid = ptid_get_pid (current_inferior->entry.id);
7484 int tid;
7485 ptid_t ptid = null_ptid;
7486
7487 trace_debug ("run_inferior_command: running: %s", cmd);
7488
7489 pause_all (0);
7490 uninsert_all_breakpoints ();
7491
7492 if (read_inferior_integer (ipa_sym_addrs.addr_helper_thread_id, &tid))
7493 {
7494 warning ("Error reading helper thread's id in lib");
7495 goto out;
7496 }
7497
7498 if (tid == 0)
7499 {
7500 warning ("helper thread not initialized yet");
7501 goto out;
7502 }
7503
7504 if (write_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
7505 (unsigned char *) cmd, strlen (cmd) + 1))
7506 {
7507 warning ("Error writing command");
7508 goto out;
7509 }
7510
7511 ptid = ptid_build (pid, tid, 0);
7512
7513 resume_thread (ptid);
7514
7515 fd = gdb_ust_connect_sync_socket (pid);
7516 if (fd >= 0)
7517 {
7518 char buf[1] = "";
7519 int ret;
7520
7521 trace_debug ("signalling helper thread");
7522
7523 do
7524 {
7525 ret = write (fd, buf, 1);
7526 } while (ret == -1 && errno == EINTR);
7527
7528 trace_debug ("waiting for helper thread's response");
7529
7530 do
7531 {
7532 ret = read (fd, buf, 1);
7533 } while (ret == -1 && errno == EINTR);
7534
7535 close (fd);
7536
7537 trace_debug ("helper thread's response received");
7538 }
7539
7540 out:
7541
7542 /* Need to read response with the inferior stopped. */
7543 if (!ptid_equal (ptid, null_ptid))
7544 {
7545 int was_non_stop = non_stop;
7546 struct target_waitstatus status;
7547
7548 stop_thread (ptid);
7549 non_stop = 1;
7550 mywait (ptid, &status, 0, 0);
7551 non_stop = was_non_stop;
7552 }
7553
7554 if (fd >= 0)
7555 {
7556 if (read_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
7557 (unsigned char *) cmd, CMD_BUF_SIZE))
7558 {
7559 warning ("Error reading command response");
7560 }
7561 else
7562 {
7563 err = 0;
7564 trace_debug ("run_inferior_command: response: %s", cmd);
7565 }
7566 }
7567
7568 reinsert_all_breakpoints ();
7569 unpause_all (0);
7570
7571 return err;
7572}
7573
7574#else /* HAVE_UST */
7575
7576static int
7577run_inferior_command (char *cmd)
7578{
7579 return -1;
7580}
7581
7582#endif /* HAVE_UST */
7583
7584#else /* !IN_PROCESS_AGENT */
7585
7586/* Thread ID of the helper thread. GDBserver reads this to know which
7587 is the help thread. This is an LWP id on Linux. */
7588int helper_thread_id;
7589
7590#ifdef HAVE_UST
7591
7592static int
7593init_named_socket (const char *name)
7594{
7595 int result, fd;
7596 struct sockaddr_un addr;
7597
7598 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
7599 if (result == -1)
7600 {
7601 warning ("socket creation failed: %s", strerror (errno));
7602 return -1;
7603 }
7604
7605 addr.sun_family = AF_UNIX;
7606
7607 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
7608 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
7609
7610 result = access (name, F_OK);
7611 if (result == 0)
7612 {
7613 /* File exists. */
7614 result = unlink (name);
7615 if (result == -1)
7616 {
7617 warning ("unlink failed: %s", strerror (errno));
7618 close (fd);
7619 return -1;
7620 }
7621 warning ("socket %s already exists; overwriting", name);
7622 }
7623
7624 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
7625 if (result == -1)
7626 {
7627 warning ("bind failed: %s", strerror (errno));
7628 close (fd);
7629 return -1;
7630 }
7631
7632 result = listen (fd, 1);
7633 if (result == -1)
7634 {
7635 warning ("listen: %s", strerror (errno));
7636 close (fd);
7637 return -1;
7638 }
7639
7640 return fd;
7641}
7642
7643static int
7644gdb_ust_socket_init (void)
7645{
7646 int result, fd;
7647 char name[UNIX_PATH_MAX];
7648
6cebaf6e 7649 result = xsnprintf (name, UNIX_PATH_MAX, "%s/gdb_ust%d",
7650 SOCK_DIR, getpid ());
0fb4aa4b
PA
7651 if (result >= UNIX_PATH_MAX)
7652 {
7653 trace_debug ("string overflow allocating socket name");
7654 return -1;
7655 }
7656
7657 fd = init_named_socket (name);
7658 if (fd < 0)
7659 warning ("Error initializing named socket (%s) for communication with the "
7660 "ust helper thread. Check that directory exists and that it "
7661 "is writable.", name);
7662
7663 return fd;
7664}
7665
7666/* Return an hexstr version of the STR C string, fit for sending to
7667 GDB. */
7668
7669static char *
7670cstr_to_hexstr (const char *str)
7671{
7672 int len = strlen (str);
7673 char *hexstr = xmalloc (len * 2 + 1);
7674 convert_int_to_ascii ((gdb_byte *) str, hexstr, len);
7675 return hexstr;
7676}
7677
7678/* The next marker to be returned on a qTsSTM command. */
7679static const struct marker *next_st;
7680
7681/* Returns the first known marker. */
7682
7683struct marker *
7684first_marker (void)
7685{
7686 struct marker_iter iter;
7687
7688 USTF(marker_iter_reset) (&iter);
7689 USTF(marker_iter_start) (&iter);
7690
7691 return iter.marker;
7692}
7693
7694/* Returns the marker following M. */
7695
7696const struct marker *
7697next_marker (const struct marker *m)
7698{
7699 struct marker_iter iter;
7700
7701 USTF(marker_iter_reset) (&iter);
7702 USTF(marker_iter_start) (&iter);
7703
7704 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7705 {
7706 if (iter.marker == m)
7707 {
7708 USTF(marker_iter_next) (&iter);
7709 return iter.marker;
7710 }
7711 }
7712
7713 return NULL;
7714}
7715
7716/* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7717 packets. */
7718
7719static void
7720response_ust_marker (char *packet, const struct marker *st)
7721{
7722 char *strid, *format, *tmp;
7723
7724 next_st = next_marker (st);
7725
7726 tmp = xmalloc (strlen (st->channel) + 1 +
7727 strlen (st->name) + 1);
7728 sprintf (tmp, "%s/%s", st->channel, st->name);
7729
7730 strid = cstr_to_hexstr (tmp);
7731 free (tmp);
7732
7733 format = cstr_to_hexstr (st->format);
7734
7735 sprintf (packet, "m%s:%s:%s",
7736 paddress ((uintptr_t) st->location),
7737 strid,
7738 format);
7739
7740 free (strid);
7741 free (format);
7742}
7743
7744/* Return the first static tracepoint, and initialize the state
7745 machine that will iterate through all the static tracepoints. */
7746
7747static void
7748cmd_qtfstm (char *packet)
7749{
7750 trace_debug ("Returning first trace state variable definition");
7751
7752 if (first_marker ())
7753 response_ust_marker (packet, first_marker ());
7754 else
7755 strcpy (packet, "l");
7756}
7757
7758/* Return additional trace state variable definitions. */
7759
7760static void
7761cmd_qtsstm (char *packet)
7762{
7763 trace_debug ("Returning static tracepoint");
7764
7765 if (next_st)
7766 response_ust_marker (packet, next_st);
7767 else
7768 strcpy (packet, "l");
7769}
7770
7771/* Disconnect the GDB probe from a marker at a given address. */
7772
7773static void
7774unprobe_marker_at (char *packet)
7775{
7776 char *p = packet;
7777 ULONGEST address;
7778 struct marker_iter iter;
7779
7780 p += sizeof ("unprobe_marker_at:") - 1;
7781
7782 p = unpack_varlen_hex (p, &address);
7783
7784 USTF(marker_iter_reset) (&iter);
7785 USTF(marker_iter_start) (&iter);
7786 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7787 if ((uintptr_t ) iter.marker->location == address)
7788 {
7789 int result;
7790
7791 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
7792 iter.marker->name,
7793 GDB_PROBE_NAME);
7794 if (result < 0)
7795 warning ("could not disable marker %s/%s",
7796 iter.marker->channel, iter.marker->name);
7797 break;
7798 }
7799}
7800
7801/* Connect the GDB probe to a marker at a given address. */
7802
7803static int
7804probe_marker_at (char *packet)
7805{
7806 char *p = packet;
7807 ULONGEST address;
7808 struct marker_iter iter;
7809 struct marker *m;
7810
7811 p += sizeof ("probe_marker_at:") - 1;
7812
7813 p = unpack_varlen_hex (p, &address);
7814
7815 USTF(marker_iter_reset) (&iter);
7816
7817 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7818 m != NULL;
7819 USTF(marker_iter_next) (&iter), m = iter.marker)
7820 if ((uintptr_t ) m->location == address)
7821 {
7822 int result;
7823
7824 trace_debug ("found marker for address. "
7825 "ltt_marker_connect (marker = %s/%s)",
7826 m->channel, m->name);
7827
493e2a69
MS
7828 result = USTF(ltt_marker_connect) (m->channel, m->name,
7829 GDB_PROBE_NAME);
0fb4aa4b
PA
7830 if (result && result != -EEXIST)
7831 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7832 m->channel, m->name, -result);
7833
7834 if (result < 0)
7835 {
7836 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
7837 m->channel, m->name);
7838 return -1;
7839 }
7840
7841 strcpy (packet, "OK");
7842 return 0;
7843 }
7844
7845 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
7846 return -1;
7847}
7848
7849static int
7850cmd_qtstmat (char *packet)
7851{
7852 char *p = packet;
7853 ULONGEST address;
7854 struct marker_iter iter;
7855 struct marker *m;
7856
7857 p += sizeof ("qTSTMat:") - 1;
7858
7859 p = unpack_varlen_hex (p, &address);
7860
7861 USTF(marker_iter_reset) (&iter);
7862
7863 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7864 m != NULL;
7865 USTF(marker_iter_next) (&iter), m = iter.marker)
7866 if ((uintptr_t ) m->location == address)
7867 {
7868 response_ust_marker (packet, m);
7869 return 0;
7870 }
7871
7872 strcpy (packet, "l");
7873 return -1;
7874}
7875
7876static void *
7877gdb_ust_thread (void *arg)
7878{
7879 int listen_fd;
7880
7881 while (1)
7882 {
7883 listen_fd = gdb_ust_socket_init ();
7884
7885#ifdef SYS_gettid
7886 if (helper_thread_id == 0)
7887 helper_thread_id = syscall (SYS_gettid);
7888#endif
7889
7890 if (listen_fd == -1)
7891 {
7892 warning ("could not create sync socket\n");
7893 break;
7894 }
7895
7896 while (1)
7897 {
7898 socklen_t tmp;
7899 struct sockaddr_un sockaddr;
7900 int fd;
7901 char buf[1];
7902 int ret;
7903
7904 tmp = sizeof (sockaddr);
7905
7906 do
7907 {
7908 fd = accept (listen_fd, &sockaddr, &tmp);
7909 }
7910 /* It seems an ERESTARTSYS can escape out of accept. */
7911 while (fd == -512 || (fd == -1 && errno == EINTR));
7912
7913 if (fd < 0)
7914 {
7915 warning ("Accept returned %d, error: %s\n",
7916 fd, strerror (errno));
7917 break;
7918 }
7919
7920 do
7921 {
7922 ret = read (fd, buf, 1);
7923 } while (ret == -1 && errno == EINTR);
7924
7925 if (ret == -1)
7926 {
7927 warning ("reading socket (fd=%d) failed with %s",
7928 fd, strerror (errno));
7929 close (fd);
7930 break;
7931 }
7932
7933 if (cmd_buf[0])
7934 {
7935 if (strcmp ("qTfSTM", cmd_buf) == 0)
7936 {
7937 cmd_qtfstm (cmd_buf);
7938 }
7939 else if (strcmp ("qTsSTM", cmd_buf) == 0)
7940 {
7941 cmd_qtsstm (cmd_buf);
7942 }
7943 else if (strncmp ("unprobe_marker_at:",
7944 cmd_buf,
7945 sizeof ("unprobe_marker_at:") - 1) == 0)
7946 {
7947 unprobe_marker_at (cmd_buf);
7948 }
7949 else if (strncmp ("probe_marker_at:",
7950 cmd_buf,
7951 sizeof ("probe_marker_at:") - 1) == 0)
7952 {
7953 probe_marker_at (cmd_buf);
7954 }
7955 else if (strncmp ("qTSTMat:",
7956 cmd_buf,
7957 sizeof ("qTSTMat:") - 1) == 0)
7958 {
7959 cmd_qtstmat (cmd_buf);
7960 }
7961 else if (strcmp (cmd_buf, "help") == 0)
7962 {
7963 strcpy (cmd_buf, "for help, press F1\n");
7964 }
7965 else
7966 strcpy (cmd_buf, "");
7967 }
7968
7969 write (fd, buf, 1);
7970 close (fd);
7971 }
7972 }
7973
7974 return NULL;
7975}
7976
7977#include <signal.h>
7978
7979static void
7980gdb_ust_init (void)
7981{
7982 int res;
7983 pthread_t thread;
7984 sigset_t new_mask;
7985 sigset_t orig_mask;
7986
7987 if (!dlsym_ust ())
7988 return;
7989
7990 /* We want the helper thread to be as transparent as possible, so
7991 have it inherit an all-signals-blocked mask. */
7992
7993 sigfillset (&new_mask);
7994 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
7995 if (res)
7996 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
7997
7998 res = pthread_create (&thread,
7999 NULL,
8000 gdb_ust_thread,
8001 NULL);
8002
8003 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
8004 if (res)
8005 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
8006
8007 while (helper_thread_id == 0)
8008 usleep (1);
8009
8010 USTF(ltt_probe_register) (&gdb_ust_probe);
8011}
8012
8013#endif /* HAVE_UST */
8014
8015#include <sys/mman.h>
8016#include <fcntl.h>
8017
8018IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
8019IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
8020IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
405f8e94
SS
8021IP_AGENT_EXPORT char *gdb_trampoline_buffer;
8022IP_AGENT_EXPORT char *gdb_trampoline_buffer_end;
8023IP_AGENT_EXPORT char *gdb_trampoline_buffer_error;
8024
8025/* Record the result of getting buffer space for fast tracepoint
8026 trampolines. Any error message is copied, since caller may not be
8027 using persistent storage. */
8028
8029void
8030set_trampoline_buffer_space (CORE_ADDR begin, CORE_ADDR end, char *errmsg)
8031{
8032 gdb_trampoline_buffer = (char *) (uintptr_t) begin;
8033 gdb_trampoline_buffer_end = (char *) (uintptr_t) end;
8034 if (errmsg)
8035 strncpy (gdb_trampoline_buffer_error, errmsg, 99);
8036 else
8037 strcpy (gdb_trampoline_buffer_error, "no buffer passed");
8038}
0fb4aa4b
PA
8039
8040static void __attribute__ ((constructor))
8041initialize_tracepoint_ftlib (void)
8042{
8043 initialize_tracepoint ();
8044
8045#ifdef HAVE_UST
8046 gdb_ust_init ();
8047#endif
8048}
8049
8050#endif /* IN_PROCESS_AGENT */
fa593d66 8051
219f2f23
PA
8052static LONGEST
8053tsv_get_timestamp (void)
8054{
8055 struct timeval tv;
8056
8057 if (gettimeofday (&tv, 0) != 0)
8058 return -1;
8059 else
8060 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
8061}
8062
8063void
8064initialize_tracepoint (void)
8065{
8066 /* There currently no way to change the buffer size. */
8067 const int sizeOfBuffer = 5 * 1024 * 1024;
8068 unsigned char *buf = xmalloc (sizeOfBuffer);
8069 init_trace_buffer (buf, sizeOfBuffer);
8070
8071 /* Wire trace state variable 1 to be the timestamp. This will be
8072 uploaded to GDB upon connection and become one of its trace state
8073 variables. (In case you're wondering, if GDB already has a trace
8074 variable numbered 1, it will be renumbered.) */
fa593d66 8075 create_trace_state_variable (1, 0);
219f2f23
PA
8076 set_trace_state_variable_name (1, "trace_timestamp");
8077 set_trace_state_variable_getter (1, tsv_get_timestamp);
fa593d66
PA
8078
8079#ifdef IN_PROCESS_AGENT
8080 {
8081 int pagesize;
8082 pagesize = sysconf (_SC_PAGE_SIZE);
8083 if (pagesize == -1)
8084 fatal ("sysconf");
8085
8086 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
8087
8088 /* Allocate scratch buffer aligned on a page boundary. */
8089 gdb_jump_pad_buffer = memalign (pagesize, pagesize * 20);
8090 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * 20;
8091
8092 /* Make it writable and executable. */
8093 if (mprotect (gdb_jump_pad_buffer, pagesize * 20,
8094 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
8095 fatal ("\
8096initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
8097 gdb_jump_pad_buffer, pagesize * 20, strerror (errno));
8098 }
8099
405f8e94
SS
8100 gdb_trampoline_buffer = gdb_trampoline_buffer_end = 0;
8101
8102 /* It's not a fatal error for something to go wrong with trampoline
8103 buffer setup, but it can be mysterious, so create a channel to
8104 report back on what went wrong, using a fixed size since we may
8105 not be able to allocate space later when the problem occurs. */
8106 gdb_trampoline_buffer_error = xmalloc (IPA_BUFSIZ);
8107
8108 strcpy (gdb_trampoline_buffer_error, "No errors reported");
8109
fa593d66
PA
8110 initialize_low_tracepoint ();
8111#endif
219f2f23 8112}