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