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