daily update
[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 CORE_ADDR jump_entry;
2744
2745 /* The jump to the jump pad of the last fast tracepoint
2746 installed. */
2747 unsigned char fjump[MAX_JUMP_SIZE];
2748 ULONGEST fjump_size;
219f2f23
PA
2749
2750 trace_debug ("Starting the trace");
2751
fa593d66
PA
2752 /* Sort tracepoints by ascending address. This makes installing
2753 fast tracepoints at the same address easier to handle. */
2754 sort_tracepoints ();
219f2f23 2755
7984d532 2756 /* Pause all threads temporarily while we patch tracepoints. */
fa593d66
PA
2757 pause_all (0);
2758
2759 /* Get threads out of jump pads. Safe to do here, since this is a
2760 top level command. And, required to do here, since we're
2761 deleting/rewriting jump pads. */
2762
2763 stabilize_threads ();
2764
2765 /* Freeze threads. */
7984d532
PA
2766 pause_all (1);
2767
fa593d66
PA
2768 /* Sync the fast tracepoints list in the inferior ftlib. */
2769 if (in_process_agent_loaded ())
2770 {
2771 download_tracepoints ();
2772 download_trace_state_variables ();
2773 }
2774
2775 /* No previous fast tpoint yet. */
2776 prev_ftpoint = NULL;
2777
0fb4aa4b
PA
2778 /* No previous static tpoint yet. */
2779 prev_stpoint = NULL;
2780
fa593d66
PA
2781 *packet = '\0';
2782
219f2f23
PA
2783 /* Install tracepoints. */
2784 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
2785 {
2786 /* Ensure all the hit counts start at zero. */
2787 tpoint->hit_count = 0;
2788
fa593d66
PA
2789 if (tpoint->type == trap_tracepoint)
2790 {
fa593d66
PA
2791 /* Tracepoints are installed as memory breakpoints. Just go
2792 ahead and install the trap. The breakpoints module
2793 handles duplicated breakpoints, and the memory read
2794 routine handles un-patching traps from memory reads. */
2795 tpoint->handle = set_breakpoint_at (tpoint->address,
2796 tracepoint_handler);
2797 }
2798 else if (tpoint->type == fast_tracepoint)
2799 {
fa593d66
PA
2800 if (maybe_write_ipa_not_loaded (packet))
2801 {
2802 trace_debug ("Requested a fast tracepoint, but fast "
2803 "tracepoints aren't supported.");
2804 break;
2805 }
2806
2807 if (prev_ftpoint != NULL && prev_ftpoint->address == tpoint->address)
2808 {
2809 tpoint->handle = set_fast_tracepoint_jump (tpoint->address,
2810 fjump,
2811 fjump_size);
2812 tpoint->jump_pad = prev_ftpoint->jump_pad;
2813 tpoint->jump_pad_end = prev_ftpoint->jump_pad_end;
2814 tpoint->adjusted_insn_addr = prev_ftpoint->adjusted_insn_addr;
2815 tpoint->adjusted_insn_addr_end
2816 = prev_ftpoint->adjusted_insn_addr_end;
2817 }
2818 else
2819 {
2820 CORE_ADDR jentry;
2821 int err = 0;
2822
2823 prev_ftpoint = NULL;
2824
2825 jentry = jump_entry = get_jump_space_head ();
2826
2827 /* Install the jump pad. */
2828 err = install_fast_tracepoint_jump_pad
2829 (tpoint->obj_addr_on_target,
2830 tpoint->address,
2831 ipa_sym_addrs.addr_gdb_collect,
2832 ipa_sym_addrs.addr_collecting,
2833 tpoint->orig_size,
2834 &jentry,
2835 fjump, &fjump_size,
2836 &tpoint->adjusted_insn_addr,
2837 &tpoint->adjusted_insn_addr_end);
2838
2839 /* Wire it in. */
2840 if (!err)
2841 tpoint->handle = set_fast_tracepoint_jump (tpoint->address,
2842 fjump, fjump_size);
2843
2844 if (tpoint->handle != NULL)
2845 {
2846 tpoint->jump_pad = jump_entry;
2847 tpoint->jump_pad_end = jentry;
219f2f23 2848
fa593d66
PA
2849 /* Pad to 8-byte alignment. */
2850 jentry = ((jentry + 7) & ~0x7);
2851 claim_jump_space (jentry - jump_entry);
219f2f23 2852
fa593d66
PA
2853 /* So that we can handle multiple fast tracepoints
2854 at the same address easily. */
2855 prev_ftpoint = tpoint;
2856 }
2857 }
2858 }
0fb4aa4b
PA
2859 else if (tpoint->type == static_tracepoint)
2860 {
2861 if (maybe_write_ipa_ust_not_loaded (packet))
2862 {
2863 trace_debug ("Requested a static tracepoint, but static "
2864 "tracepoints are not supported.");
2865 break;
2866 }
2867
2868 /* Can only probe a given marker once. */
2869 if (prev_stpoint != NULL && prev_stpoint->address == tpoint->address)
2870 {
2871 tpoint->handle = (void *) -1;
2872 }
2873 else
2874 {
2875 if (probe_marker_at (tpoint->address, packet) == 0)
2876 {
2877 tpoint->handle = (void *) -1;
2878
2879 /* So that we can handle multiple static tracepoints
2880 at the same address easily. */
2881 prev_stpoint = tpoint;
2882 }
2883 }
2884 }
fa593d66
PA
2885
2886 /* Any failure in the inner loop is sufficient cause to give
2887 up. */
219f2f23
PA
2888 if (tpoint->handle == NULL)
2889 break;
2890 }
2891
2892 /* Any error in tracepoint insertion is unacceptable; better to
2893 address the problem now, than end up with a useless or misleading
2894 trace run. */
2895 if (tpoint != NULL)
2896 {
2897 clear_installed_tracepoints ();
2898 if (*packet == '\0')
2899 write_enn (packet);
7984d532 2900 unpause_all (1);
219f2f23
PA
2901 return;
2902 }
2903
2904 stopping_tracepoint = NULL;
2905 trace_buffer_is_full = 0;
2906 expr_eval_result = expr_eval_no_error;
2907 error_tracepoint = NULL;
2908
2909 /* Tracing is now active, hits will now start being logged. */
2910 tracing = 1;
2911
fa593d66
PA
2912 if (in_process_agent_loaded ())
2913 {
2914 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 1))
2915 fatal ("Error setting tracing variable in lib");
2916
2917 if (write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
2918 0))
2919 fatal ("Error clearing stopping_tracepoint variable in lib");
2920
2921 if (write_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full, 0))
2922 fatal ("Error clearing trace_buffer_is_full variable in lib");
2923
2924 stop_tracing_bkpt = set_breakpoint_at (ipa_sym_addrs.addr_stop_tracing,
2925 stop_tracing_handler);
2926 if (stop_tracing_bkpt == NULL)
2927 error ("Error setting stop_tracing breakpoint");
2928
2929 flush_trace_buffer_bkpt
2930 = set_breakpoint_at (ipa_sym_addrs.addr_flush_trace_buffer,
2931 flush_trace_buffer_handler);
2932 if (flush_trace_buffer_bkpt == NULL)
2933 error ("Error setting flush_trace_buffer breakpoint");
2934 }
2935
7984d532
PA
2936 unpause_all (1);
2937
219f2f23
PA
2938 write_ok (packet);
2939}
2940
2941/* End a tracing run, filling in a stop reason to report back to GDB,
2942 and removing the tracepoints from the code. */
2943
8336d594 2944void
219f2f23
PA
2945stop_tracing (void)
2946{
2947 if (!tracing)
2948 {
2949 trace_debug ("Tracing is already off, ignoring");
2950 return;
2951 }
2952
2953 trace_debug ("Stopping the trace");
2954
fa593d66
PA
2955 /* Pause all threads before removing fast jumps from memory,
2956 breakpoints, and touching IPA state variables (inferior memory).
2957 Some thread may hit the internal tracing breakpoints, or be
2958 collecting this moment, but that's ok, we don't release the
2959 tpoint object's memory or the jump pads here (we only do that
2960 when we're sure we can move all threads out of the jump pads).
2961 We can't now, since we may be getting here due to the inferior
2962 agent calling us. */
7984d532
PA
2963 pause_all (1);
2964 /* Since we're removing breakpoints, cancel breakpoint hits,
2965 possibly related to the breakpoints we're about to delete. */
2966 cancel_breakpoints ();
2967
219f2f23
PA
2968 /* Stop logging. Tracepoints can still be hit, but they will not be
2969 recorded. */
2970 tracing = 0;
fa593d66
PA
2971 if (in_process_agent_loaded ())
2972 {
2973 if (write_inferior_integer (ipa_sym_addrs.addr_tracing, 0))
2974 fatal ("Error clearing tracing variable in lib");
2975 }
219f2f23
PA
2976
2977 tracing_stop_reason = "t???";
2978 tracing_stop_tpnum = 0;
2979 if (stopping_tracepoint)
2980 {
2981 trace_debug ("Stopping the trace because "
2982 "tracepoint %d was hit %ld times",
2983 stopping_tracepoint->number,
2984 stopping_tracepoint->pass_count);
2985 tracing_stop_reason = "tpasscount";
2986 tracing_stop_tpnum = stopping_tracepoint->number;
2987 }
2988 else if (trace_buffer_is_full)
2989 {
2990 trace_debug ("Stopping the trace because the trace buffer is full");
2991 tracing_stop_reason = "tfull";
2992 }
2993 else if (expr_eval_result != expr_eval_no_error)
2994 {
2995 trace_debug ("Stopping the trace because of an expression eval error");
2996 tracing_stop_reason = eval_result_names[expr_eval_result];
2997 tracing_stop_tpnum = error_tracepoint->number;
2998 }
fa593d66 2999#ifndef IN_PROCESS_AGENT
8336d594
PA
3000 else if (!gdb_connected ())
3001 {
3002 trace_debug ("Stopping the trace because GDB disconnected");
3003 tracing_stop_reason = "tdisconnected";
3004 }
fa593d66 3005#endif
219f2f23
PA
3006 else
3007 {
3008 trace_debug ("Stopping the trace because of a tstop command");
3009 tracing_stop_reason = "tstop";
3010 }
3011
3012 stopping_tracepoint = NULL;
3013 error_tracepoint = NULL;
3014
3015 /* Clear out the tracepoints. */
3016 clear_installed_tracepoints ();
7984d532 3017
fa593d66
PA
3018 if (in_process_agent_loaded ())
3019 {
3020 /* Pull in fast tracepoint trace frames from the inferior lib
3021 buffer into our buffer, even if our buffer is already full,
3022 because we want to present the full number of created frames
3023 in addition to what fit in the trace buffer. */
3024 upload_fast_traceframes ();
3025 }
3026
3027 if (stop_tracing_bkpt != NULL)
3028 {
3029 delete_breakpoint (stop_tracing_bkpt);
3030 stop_tracing_bkpt = NULL;
3031 }
3032
3033 if (flush_trace_buffer_bkpt != NULL)
3034 {
3035 delete_breakpoint (flush_trace_buffer_bkpt);
3036 flush_trace_buffer_bkpt = NULL;
3037 }
3038
7984d532 3039 unpause_all (1);
219f2f23
PA
3040}
3041
fa593d66
PA
3042static int
3043stop_tracing_handler (CORE_ADDR addr)
3044{
3045 trace_debug ("lib hit stop_tracing");
3046
3047 /* Don't actually handle it here. When we stop tracing we remove
3048 breakpoints from the inferior, and that is not allowed in a
3049 breakpoint handler (as the caller is walking the breakpoint
3050 list). */
3051 return 0;
3052}
3053
3054static int
3055flush_trace_buffer_handler (CORE_ADDR addr)
3056{
3057 trace_debug ("lib hit flush_trace_buffer");
3058 return 0;
3059}
3060
219f2f23
PA
3061static void
3062cmd_qtstop (char *packet)
3063{
3064 stop_tracing ();
3065 write_ok (packet);
3066}
3067
8336d594
PA
3068static void
3069cmd_qtdisconnected (char *own_buf)
3070{
3071 ULONGEST setting;
3072 char *packet = own_buf;
3073
3074 packet += strlen ("QTDisconnected:");
3075
3076 unpack_varlen_hex (packet, &setting);
3077
3078 write_ok (own_buf);
3079
3080 disconnected_tracing = setting;
3081}
3082
219f2f23
PA
3083static void
3084cmd_qtframe (char *own_buf)
3085{
3086 ULONGEST frame, pc, lo, hi, num;
3087 int tfnum, tpnum;
3088 struct traceframe *tframe;
3089 char *packet = own_buf;
3090
3091 packet += strlen ("QTFrame:");
3092
3093 if (strncmp (packet, "pc:", strlen ("pc:")) == 0)
3094 {
3095 packet += strlen ("pc:");
f8f67713 3096 unpack_varlen_hex (packet, &pc);
219f2f23
PA
3097 trace_debug ("Want to find next traceframe at pc=0x%s", paddress (pc));
3098 tframe = find_next_traceframe_in_range (pc, pc, 1, &tfnum);
3099 }
3100 else if (strncmp (packet, "range:", strlen ("range:")) == 0)
3101 {
3102 packet += strlen ("range:");
3103 packet = unpack_varlen_hex (packet, &lo);
3104 ++packet;
f8f67713 3105 unpack_varlen_hex (packet, &hi);
219f2f23
PA
3106 trace_debug ("Want to find next traceframe in the range 0x%s to 0x%s",
3107 paddress (lo), paddress (hi));
3108 tframe = find_next_traceframe_in_range (lo, hi, 1, &tfnum);
3109 }
3110 else if (strncmp (packet, "outside:", strlen ("outside:")) == 0)
3111 {
3112 packet += strlen ("outside:");
3113 packet = unpack_varlen_hex (packet, &lo);
3114 ++packet;
f8f67713 3115 unpack_varlen_hex (packet, &hi);
219f2f23
PA
3116 trace_debug ("Want to find next traceframe "
3117 "outside the range 0x%s to 0x%s",
3118 paddress (lo), paddress (hi));
3119 tframe = find_next_traceframe_in_range (lo, hi, 0, &tfnum);
3120 }
3121 else if (strncmp (packet, "tdp:", strlen ("tdp:")) == 0)
3122 {
3123 packet += strlen ("tdp:");
f8f67713 3124 unpack_varlen_hex (packet, &num);
219f2f23
PA
3125 tpnum = (int) num;
3126 trace_debug ("Want to find next traceframe for tracepoint %d", tpnum);
3127 tframe = find_next_traceframe_by_tracepoint (tpnum, &tfnum);
3128 }
3129 else
3130 {
3131 unpack_varlen_hex (packet, &frame);
3132 tfnum = (int) frame;
3133 if (tfnum == -1)
3134 {
3135 trace_debug ("Want to stop looking at traceframes");
3136 current_traceframe = -1;
3137 write_ok (own_buf);
3138 return;
3139 }
3140 trace_debug ("Want to look at traceframe %d", tfnum);
3141 tframe = find_traceframe (tfnum);
3142 }
3143
3144 if (tframe)
3145 {
3146 current_traceframe = tfnum;
3147 sprintf (own_buf, "F%xT%x", tfnum, tframe->tpnum);
3148 }
3149 else
3150 sprintf (own_buf, "F-1");
3151}
3152
3153static void
3154cmd_qtstatus (char *packet)
3155{
3156 char *stop_reason_rsp = NULL;
3157
3158 trace_debug ("Returning trace status as %d, stop reason %s",
3159 tracing, tracing_stop_reason);
3160
fa593d66
PA
3161 if (in_process_agent_loaded ())
3162 {
3163 pause_all (1);
3164
3165 upload_fast_traceframes ();
3166
3167 unpause_all (1);
3168 }
3169
219f2f23
PA
3170 stop_reason_rsp = (char *) tracing_stop_reason;
3171
3172 /* The user visible error string in terror needs to be hex encoded.
3173 We leave it as plain string in `tracepoint_stop_reason' to ease
3174 debugging. */
3175 if (strncmp (stop_reason_rsp, "terror:", strlen ("terror:")) == 0)
3176 {
3177 const char *result_name;
3178 int hexstr_len;
3179 char *p;
3180
3181 result_name = stop_reason_rsp + strlen ("terror:");
3182 hexstr_len = strlen (result_name) * 2;
3183 p = stop_reason_rsp = alloca (strlen ("terror:") + hexstr_len + 1);
3184 strcpy (p, "terror:");
3185 p += strlen (p);
3186 convert_int_to_ascii ((gdb_byte *) result_name, p, strlen (result_name));
3187 }
3188
8336d594
PA
3189 sprintf (packet,
3190 "T%d;"
3191 "%s:%x;"
3192 "tframes:%x;tcreated:%x;"
3193 "tfree:%x;tsize:%s;"
3194 "circular:%d;"
3195 "disconn:%d",
623ccd72 3196 tracing ? 1 : 0,
219f2f23
PA
3197 stop_reason_rsp, tracing_stop_tpnum,
3198 traceframe_count, traceframes_created,
8336d594
PA
3199 free_space (), phex_nz (trace_buffer_hi - trace_buffer_lo, 0),
3200 circular_trace_buffer,
3201 disconnected_tracing);
219f2f23
PA
3202}
3203
3204/* State variables to help return all the tracepoint bits. */
3205static struct tracepoint *cur_tpoint;
3206static int cur_action;
3207static int cur_step_action;
3208static struct source_string *cur_source_string;
3209static struct trace_state_variable *cur_tsv;
3210
3211/* Compose a response that is an imitation of the syntax by which the
3212 tracepoint was originally downloaded. */
3213
3214static void
3215response_tracepoint (char *packet, struct tracepoint *tpoint)
3216{
3217 char *buf;
3218
3219 sprintf (packet, "T%x:%s:%c:%lx:%lx", tpoint->number,
3220 paddress (tpoint->address),
3221 (tpoint->enabled ? 'E' : 'D'), tpoint->step_count,
3222 tpoint->pass_count);
fa593d66
PA
3223 if (tpoint->type == fast_tracepoint)
3224 sprintf (packet + strlen (packet), ":F%x", tpoint->orig_size);
0fb4aa4b
PA
3225 else if (tpoint->type == static_tracepoint)
3226 sprintf (packet + strlen (packet), ":S");
219f2f23
PA
3227
3228 if (tpoint->cond)
3229 {
3230 buf = unparse_agent_expr (tpoint->cond);
3231 sprintf (packet + strlen (packet), ":X%x,%s",
3232 tpoint->cond->length, buf);
3233 free (buf);
3234 }
3235}
3236
3237/* Compose a response that is an imitation of the syntax by which the
3238 tracepoint action was originally downloaded (with the difference
3239 that due to the way we store the actions, this will output a packet
3240 per action, while GDB could have combined more than one action
3241 per-packet. */
3242
3243static void
3244response_action (char *packet, struct tracepoint *tpoint,
3245 char *taction, int step)
3246{
3247 sprintf (packet, "%c%x:%s:%s",
3248 (step ? 'S' : 'A'), tpoint->number, paddress (tpoint->address),
3249 taction);
3250}
3251
3252/* Compose a response that is an imitation of the syntax by which the
3253 tracepoint source piece was originally downloaded. */
3254
3255static void
3256response_source (char *packet,
3257 struct tracepoint *tpoint, struct source_string *src)
3258{
3259 char *buf;
3260 int len;
3261
3262 len = strlen (src->str);
3263 buf = alloca (len * 2 + 1);
3264 convert_int_to_ascii ((gdb_byte *) src->str, buf, len);
3265
3266 sprintf (packet, "Z%x:%s:%s:%x:%x:%s",
3267 tpoint->number, paddress (tpoint->address),
3268 src->type, 0, len, buf);
3269}
3270
3271/* Return the first piece of tracepoint definition, and initialize the
3272 state machine that will iterate through all the tracepoint
3273 bits. */
3274
3275static void
3276cmd_qtfp (char *packet)
3277{
3278 trace_debug ("Returning first tracepoint definition piece");
3279
3280 cur_tpoint = tracepoints;
3281 cur_action = cur_step_action = -1;
3282 cur_source_string = NULL;
3283
3284 if (cur_tpoint)
3285 response_tracepoint (packet, cur_tpoint);
3286 else
3287 strcpy (packet, "l");
3288}
3289
3290/* Return additional pieces of tracepoint definition. Each action and
3291 stepping action must go into its own packet, because of packet size
3292 limits, and so we use state variables to deliver one piece at a
3293 time. */
3294
3295static void
3296cmd_qtsp (char *packet)
3297{
3298 trace_debug ("Returning subsequent tracepoint definition piece");
3299
3300 if (!cur_tpoint)
3301 {
3302 /* This case would normally never occur, but be prepared for
3303 GDB misbehavior. */
3304 strcpy (packet, "l");
3305 }
3306 else if (cur_action < cur_tpoint->numactions - 1)
3307 {
3308 ++cur_action;
3309 response_action (packet, cur_tpoint,
3310 cur_tpoint->actions_str[cur_action], 0);
3311 }
3312 else if (cur_step_action < cur_tpoint->num_step_actions - 1)
3313 {
3314 ++cur_step_action;
3315 response_action (packet, cur_tpoint,
3316 cur_tpoint->step_actions_str[cur_step_action], 1);
3317 }
3318 else if ((cur_source_string
3319 ? cur_source_string->next
3320 : cur_tpoint->source_strings))
3321 {
3322 if (cur_source_string)
3323 cur_source_string = cur_source_string->next;
3324 else
3325 cur_source_string = cur_tpoint->source_strings;
3326 response_source (packet, cur_tpoint, cur_source_string);
3327 }
3328 else
3329 {
3330 cur_tpoint = cur_tpoint->next;
3331 cur_action = cur_step_action = -1;
3332 cur_source_string = NULL;
3333 if (cur_tpoint)
3334 response_tracepoint (packet, cur_tpoint);
3335 else
3336 strcpy (packet, "l");
3337 }
3338}
3339
3340/* Compose a response that is an imitation of the syntax by which the
3341 trace state variable was originally downloaded. */
3342
3343static void
3344response_tsv (char *packet, struct trace_state_variable *tsv)
3345{
3346 char *buf = (char *) "";
3347 int namelen;
3348
3349 if (tsv->name)
3350 {
3351 namelen = strlen (tsv->name);
3352 buf = alloca (namelen * 2 + 1);
3353 convert_int_to_ascii ((gdb_byte *) tsv->name, buf, namelen);
3354 }
3355
3356 sprintf (packet, "%x:%s:%x:%s", tsv->number, phex_nz (tsv->initial_value, 0),
3357 tsv->getter ? 1 : 0, buf);
3358}
3359
3360/* Return the first trace state variable definition, and initialize
3361 the state machine that will iterate through all the tsv bits. */
3362
3363static void
3364cmd_qtfv (char *packet)
3365{
3366 trace_debug ("Returning first trace state variable definition");
3367
3368 cur_tsv = trace_state_variables;
3369
3370 if (cur_tsv)
3371 response_tsv (packet, cur_tsv);
3372 else
3373 strcpy (packet, "l");
3374}
3375
3376/* Return additional trace state variable definitions. */
3377
3378static void
3379cmd_qtsv (char *packet)
3380{
3381 trace_debug ("Returning first trace state variable definition");
3382
3383 if (!cur_tpoint)
3384 {
3385 /* This case would normally never occur, but be prepared for
3386 GDB misbehavior. */
3387 strcpy (packet, "l");
3388 }
3389 else if (cur_tsv)
3390 {
3391 cur_tsv = cur_tsv->next;
3392 if (cur_tsv)
3393 response_tsv (packet, cur_tsv);
3394 else
3395 strcpy (packet, "l");
3396 }
3397 else
3398 strcpy (packet, "l");
3399}
3400
0fb4aa4b
PA
3401/* Return the first static tracepoint marker, and initialize the state
3402 machine that will iterate through all the static tracepoints
3403 markers. */
3404
3405static void
3406cmd_qtfstm (char *packet)
3407{
3408 if (!maybe_write_ipa_ust_not_loaded (packet))
3409 run_inferior_command (packet);
3410}
3411
3412/* Return additional static tracepoints markers. */
3413
3414static void
3415cmd_qtsstm (char *packet)
3416{
3417 if (!maybe_write_ipa_ust_not_loaded (packet))
3418 run_inferior_command (packet);
3419}
3420
3421/* Return the definition of the static tracepoint at a given address.
3422 Result packet is the same as qTsST's. */
3423
3424static void
3425cmd_qtstmat (char *packet)
3426{
3427 if (!maybe_write_ipa_ust_not_loaded (packet))
3428 run_inferior_command (packet);
3429}
3430
219f2f23
PA
3431/* Respond to qTBuffer packet with a block of raw data from the trace
3432 buffer. GDB may ask for a lot, but we are allowed to reply with
3433 only as much as will fit within packet limits or whatever. */
3434
3435static void
3436cmd_qtbuffer (char *own_buf)
3437{
3438 ULONGEST offset, num, tot;
3439 unsigned char *tbp;
3440 char *packet = own_buf;
3441
3442 packet += strlen ("qTBuffer:");
3443
3444 packet = unpack_varlen_hex (packet, &offset);
3445 ++packet; /* skip a comma */
f8f67713 3446 unpack_varlen_hex (packet, &num);
219f2f23
PA
3447
3448 trace_debug ("Want to get trace buffer, %d bytes at offset 0x%s",
3449 (int) num, pulongest (offset));
3450
3451 tot = (trace_buffer_hi - trace_buffer_lo) - free_space ();
3452
3453 /* If we're right at the end, reply specially that we're done. */
3454 if (offset == tot)
3455 {
3456 strcpy (own_buf, "l");
3457 return;
3458 }
3459
3460 /* Object to any other out-of-bounds request. */
3461 if (offset > tot)
3462 {
3463 write_enn (own_buf);
3464 return;
3465 }
3466
3467 /* Compute the pointer corresponding to the given offset, accounting
3468 for wraparound. */
3469 tbp = trace_buffer_start + offset;
3470 if (tbp >= trace_buffer_wrap)
3471 tbp -= (trace_buffer_wrap - trace_buffer_lo);
3472
3473 /* Trim to the remaining bytes if we're close to the end. */
3474 if (num > tot - offset)
3475 num = tot - offset;
3476
3477 /* Trim to available packet size. */
3478 if (num >= (PBUFSIZ - 16) / 2 )
3479 num = (PBUFSIZ - 16) / 2;
3480
3481 convert_int_to_ascii (tbp, own_buf, num);
3482 own_buf[num] = '\0';
3483}
3484
3485static void
3486cmd_bigqtbuffer (char *own_buf)
3487{
3488 ULONGEST val;
3489 char *packet = own_buf;
3490
3491 packet += strlen ("QTBuffer:");
3492
3493 if (strncmp ("circular:", packet, strlen ("circular:")) == 0)
3494 {
3495 packet += strlen ("circular:");
f8f67713 3496 unpack_varlen_hex (packet, &val);
219f2f23
PA
3497 circular_trace_buffer = val;
3498 trace_debug ("Trace buffer is now %s",
3499 circular_trace_buffer ? "circular" : "linear");
3500 write_ok (own_buf);
3501 }
3502 else
3503 write_enn (own_buf);
3504}
3505
3506int
3507handle_tracepoint_general_set (char *packet)
3508{
3509 if (strcmp ("QTinit", packet) == 0)
3510 {
3511 cmd_qtinit (packet);
3512 return 1;
3513 }
3514 else if (strncmp ("QTDP:", packet, strlen ("QTDP:")) == 0)
3515 {
3516 cmd_qtdp (packet);
3517 return 1;
3518 }
3519 else if (strncmp ("QTDPsrc:", packet, strlen ("QTDPsrc:")) == 0)
3520 {
3521 cmd_qtdpsrc (packet);
3522 return 1;
3523 }
d248b706
KY
3524 else if (strncmp ("QTEnable:", packet, strlen ("QTEnable:")) == 0)
3525 {
3526 cmd_qtenable_disable (packet, 1);
3527 return 1;
3528 }
3529 else if (strncmp ("QTDisable:", packet, strlen ("QTDisable:")) == 0)
3530 {
3531 cmd_qtenable_disable (packet, 0);
3532 return 1;
3533 }
219f2f23
PA
3534 else if (strncmp ("QTDV:", packet, strlen ("QTDV:")) == 0)
3535 {
3536 cmd_qtdv (packet);
3537 return 1;
3538 }
3539 else if (strncmp ("QTro:", packet, strlen ("QTro:")) == 0)
3540 {
3541 cmd_qtro (packet);
3542 return 1;
3543 }
3544 else if (strcmp ("QTStart", packet) == 0)
3545 {
3546 cmd_qtstart (packet);
3547 return 1;
3548 }
3549 else if (strcmp ("QTStop", packet) == 0)
3550 {
3551 cmd_qtstop (packet);
3552 return 1;
3553 }
8336d594
PA
3554 else if (strncmp ("QTDisconnected:", packet,
3555 strlen ("QTDisconnected:")) == 0)
3556 {
3557 cmd_qtdisconnected (packet);
3558 return 1;
3559 }
219f2f23
PA
3560 else if (strncmp ("QTFrame:", packet, strlen ("QTFrame:")) == 0)
3561 {
3562 cmd_qtframe (packet);
3563 return 1;
3564 }
3565 else if (strncmp ("QTBuffer:", packet, strlen ("QTBuffer:")) == 0)
3566 {
3567 cmd_bigqtbuffer (packet);
3568 return 1;
3569 }
3570
3571 return 0;
3572}
3573
3574int
3575handle_tracepoint_query (char *packet)
3576{
3577 if (strcmp ("qTStatus", packet) == 0)
3578 {
3579 cmd_qtstatus (packet);
3580 return 1;
3581 }
3582 else if (strcmp ("qTfP", packet) == 0)
3583 {
3584 cmd_qtfp (packet);
3585 return 1;
3586 }
3587 else if (strcmp ("qTsP", packet) == 0)
3588 {
3589 cmd_qtsp (packet);
3590 return 1;
3591 }
3592 else if (strcmp ("qTfV", packet) == 0)
3593 {
3594 cmd_qtfv (packet);
3595 return 1;
3596 }
3597 else if (strcmp ("qTsV", packet) == 0)
3598 {
3599 cmd_qtsv (packet);
3600 return 1;
3601 }
3602 else if (strncmp ("qTV:", packet, strlen ("qTV:")) == 0)
3603 {
3604 cmd_qtv (packet);
3605 return 1;
3606 }
3607 else if (strncmp ("qTBuffer:", packet, strlen ("qTBuffer:")) == 0)
3608 {
3609 cmd_qtbuffer (packet);
3610 return 1;
3611 }
0fb4aa4b
PA
3612 else if (strcmp ("qTfSTM", packet) == 0)
3613 {
3614 cmd_qtfstm (packet);
3615 return 1;
3616 }
3617 else if (strcmp ("qTsSTM", packet) == 0)
3618 {
3619 cmd_qtsstm (packet);
3620 return 1;
3621 }
3622 else if (strncmp ("qTSTMat:", packet, strlen ("qTSTMat:")) == 0)
3623 {
3624 cmd_qtstmat (packet);
3625 return 1;
3626 }
219f2f23
PA
3627
3628 return 0;
3629}
3630
fa593d66
PA
3631#endif
3632#ifndef IN_PROCESS_AGENT
3633
219f2f23
PA
3634/* Call this when thread TINFO has hit the tracepoint defined by
3635 TP_NUMBER and TP_ADDRESS, and that tracepoint has a while-stepping
3636 action. This adds a while-stepping collecting state item to the
3637 threads' collecting state list, so that we can keep track of
3638 multiple simultaneous while-stepping actions being collected by the
3639 same thread. This can happen in cases like:
3640
3641 ff0001 INSN1 <-- TP1, while-stepping 10 collect $regs
3642 ff0002 INSN2
3643 ff0003 INSN3 <-- TP2, collect $regs
3644 ff0004 INSN4 <-- TP3, while-stepping 10 collect $regs
3645 ff0005 INSN5
3646
3647 Notice that when instruction INSN5 is reached, the while-stepping
3648 actions of both TP1 and TP3 are still being collected, and that TP2
3649 had been collected meanwhile. The whole range of ff0001-ff0005
3650 should be single-stepped, due to at least TP1's while-stepping
3651 action covering the whole range. */
3652
3653static void
3654add_while_stepping_state (struct thread_info *tinfo,
3655 int tp_number, CORE_ADDR tp_address)
3656{
3657 struct wstep_state *wstep;
3658
3659 wstep = xmalloc (sizeof (*wstep));
3660 wstep->next = tinfo->while_stepping;
3661
3662 wstep->tp_number = tp_number;
3663 wstep->tp_address = tp_address;
3664 wstep->current_step = 0;
3665
3666 tinfo->while_stepping = wstep;
3667}
3668
3669/* Release the while-stepping collecting state WSTEP. */
3670
3671static void
3672release_while_stepping_state (struct wstep_state *wstep)
3673{
3674 free (wstep);
3675}
3676
3677/* Release all while-stepping collecting states currently associated
3678 with thread TINFO. */
3679
3680void
3681release_while_stepping_state_list (struct thread_info *tinfo)
3682{
3683 struct wstep_state *head;
3684
3685 while (tinfo->while_stepping)
3686 {
3687 head = tinfo->while_stepping;
3688 tinfo->while_stepping = head->next;
3689 release_while_stepping_state (head);
3690 }
3691}
3692
3693/* If TINFO was handling a 'while-stepping' action, the step has
3694 finished, so collect any step data needed, and check if any more
3695 steps are required. Return true if the thread was indeed
3696 collecting tracepoint data, false otherwise. */
3697
3698int
3699tracepoint_finished_step (struct thread_info *tinfo, CORE_ADDR stop_pc)
3700{
3701 struct tracepoint *tpoint;
3702 struct wstep_state *wstep;
3703 struct wstep_state **wstep_link;
3704 struct trap_tracepoint_ctx ctx;
3705
fa593d66
PA
3706 /* Pull in fast tracepoint trace frames from the inferior lib buffer into
3707 our buffer. */
3708 if (in_process_agent_loaded ())
3709 upload_fast_traceframes ();
3710
219f2f23
PA
3711 /* Check if we were indeed collecting data for one of more
3712 tracepoints with a 'while-stepping' count. */
3713 if (tinfo->while_stepping == NULL)
3714 return 0;
3715
3716 if (!tracing)
3717 {
3718 /* We're not even tracing anymore. Stop this thread from
3719 collecting. */
3720 release_while_stepping_state_list (tinfo);
3721
3722 /* The thread had stopped due to a single-step request indeed
3723 explained by a tracepoint. */
3724 return 1;
3725 }
3726
3727 wstep = tinfo->while_stepping;
3728 wstep_link = &tinfo->while_stepping;
3729
3730 trace_debug ("Thread %s finished a single-step for tracepoint %d at 0x%s",
3731 target_pid_to_str (tinfo->entry.id),
3732 wstep->tp_number, paddress (wstep->tp_address));
3733
fa593d66 3734 ctx.base.type = trap_tracepoint;
219f2f23
PA
3735 ctx.regcache = get_thread_regcache (tinfo, 1);
3736
3737 while (wstep != NULL)
3738 {
3739 tpoint = find_tracepoint (wstep->tp_number, wstep->tp_address);
3740 if (tpoint == NULL)
3741 {
3742 trace_debug ("NO TRACEPOINT %d at 0x%s FOR THREAD %s!",
3743 wstep->tp_number, paddress (wstep->tp_address),
3744 target_pid_to_str (tinfo->entry.id));
3745
3746 /* Unlink. */
3747 *wstep_link = wstep->next;
3748 release_while_stepping_state (wstep);
4f269b12 3749 wstep = *wstep_link;
219f2f23
PA
3750 continue;
3751 }
3752
3753 /* We've just finished one step. */
3754 ++wstep->current_step;
3755
3756 /* Collect data. */
3757 collect_data_at_step ((struct tracepoint_hit_ctx *) &ctx,
3758 stop_pc, tpoint, wstep->current_step);
3759
3760 if (wstep->current_step >= tpoint->step_count)
3761 {
3762 /* The requested numbers of steps have occurred. */
3763 trace_debug ("Thread %s done stepping for tracepoint %d at 0x%s",
3764 target_pid_to_str (tinfo->entry.id),
3765 wstep->tp_number, paddress (wstep->tp_address));
3766
3767 /* Unlink the wstep. */
3768 *wstep_link = wstep->next;
3769 release_while_stepping_state (wstep);
3770 wstep = *wstep_link;
3771
3772 /* Only check the hit count now, which ensure that we do all
3773 our stepping before stopping the run. */
3774 if (tpoint->pass_count > 0
3775 && tpoint->hit_count >= tpoint->pass_count
3776 && stopping_tracepoint == NULL)
3777 stopping_tracepoint = tpoint;
3778 }
3779 else
3780 {
3781 /* Keep single-stepping until the requested numbers of steps
3782 have occurred. */
3783 wstep_link = &wstep->next;
3784 wstep = *wstep_link;
3785 }
3786
3787 if (stopping_tracepoint
3788 || trace_buffer_is_full
3789 || expr_eval_result != expr_eval_no_error)
3790 {
3791 stop_tracing ();
3792 break;
3793 }
3794 }
3795
3796 return 1;
3797}
3798
fa593d66
PA
3799/* Handle any internal tracing control breakpoint hits. That means,
3800 pull traceframes from the IPA to our buffer, and syncing both
3801 tracing agents when the IPA's tracing stops for some reason. */
3802
3803int
3804handle_tracepoint_bkpts (struct thread_info *tinfo, CORE_ADDR stop_pc)
3805{
3806 /* Pull in fast tracepoint trace frames from the inferior in-process
3807 agent's buffer into our buffer. */
3808
3809 if (!in_process_agent_loaded ())
3810 return 0;
3811
3812 upload_fast_traceframes ();
3813
3814 /* Check if the in-process agent had decided we should stop
3815 tracing. */
3816 if (stop_pc == ipa_sym_addrs.addr_stop_tracing)
3817 {
3818 int ipa_trace_buffer_is_full;
3819 CORE_ADDR ipa_stopping_tracepoint;
3820 int ipa_expr_eval_result;
3821 CORE_ADDR ipa_error_tracepoint;
3822
3823 trace_debug ("lib stopped at stop_tracing");
3824
3825 read_inferior_integer (ipa_sym_addrs.addr_trace_buffer_is_full,
3826 &ipa_trace_buffer_is_full);
3827
3828 read_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint,
3829 &ipa_stopping_tracepoint);
3830 write_inferior_data_pointer (ipa_sym_addrs.addr_stopping_tracepoint, 0);
3831
3832 read_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint,
3833 &ipa_error_tracepoint);
3834 write_inferior_data_pointer (ipa_sym_addrs.addr_error_tracepoint, 0);
3835
3836 read_inferior_integer (ipa_sym_addrs.addr_expr_eval_result,
3837 &ipa_expr_eval_result);
3838 write_inferior_integer (ipa_sym_addrs.addr_expr_eval_result, 0);
3839
3840 trace_debug ("lib: trace_buffer_is_full: %d, "
3841 "stopping_tracepoint: %s, "
3842 "ipa_expr_eval_result: %d, "
3843 "error_tracepoint: %s, ",
3844 ipa_trace_buffer_is_full,
3845 paddress (ipa_stopping_tracepoint),
3846 ipa_expr_eval_result,
3847 paddress (ipa_error_tracepoint));
3848
3849 if (debug_threads)
3850 {
3851 if (ipa_trace_buffer_is_full)
3852 trace_debug ("lib stopped due to full buffer.");
3853 if (ipa_stopping_tracepoint)
3854 trace_debug ("lib stopped due to tpoint");
3855 if (ipa_stopping_tracepoint)
3856 trace_debug ("lib stopped due to error");
3857 }
3858
3859 if (ipa_stopping_tracepoint != 0)
3860 {
3861 stopping_tracepoint
3862 = fast_tracepoint_from_ipa_tpoint_address (ipa_stopping_tracepoint);
3863 }
3864 else if (ipa_expr_eval_result != expr_eval_no_error)
3865 {
3866 expr_eval_result = ipa_expr_eval_result;
3867 error_tracepoint
3868 = fast_tracepoint_from_ipa_tpoint_address (ipa_error_tracepoint);
3869 }
3870 stop_tracing ();
3871 return 1;
3872 }
3873 else if (stop_pc == ipa_sym_addrs.addr_flush_trace_buffer)
3874 {
3875 trace_debug ("lib stopped at flush_trace_buffer");
3876 return 1;
3877 }
3878
3879 return 0;
3880}
3881
219f2f23
PA
3882/* Return true if TINFO just hit a tracepoint. Collect data if
3883 so. */
3884
3885int
3886tracepoint_was_hit (struct thread_info *tinfo, CORE_ADDR stop_pc)
3887{
3888 struct tracepoint *tpoint;
3889 int ret = 0;
3890 struct trap_tracepoint_ctx ctx;
3891
3892 /* Not tracing, don't handle. */
3893 if (!tracing)
3894 return 0;
3895
fa593d66 3896 ctx.base.type = trap_tracepoint;
219f2f23
PA
3897 ctx.regcache = get_thread_regcache (tinfo, 1);
3898
3899 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
3900 {
fa593d66
PA
3901 /* Note that we collect fast tracepoints here as well. We'll
3902 step over the fast tracepoint jump later, which avoids the
3903 double collect. */
219f2f23
PA
3904 if (tpoint->enabled && stop_pc == tpoint->address)
3905 {
3906 trace_debug ("Thread %s at address of tracepoint %d at 0x%s",
3907 target_pid_to_str (tinfo->entry.id),
3908 tpoint->number, paddress (tpoint->address));
3909
3910 /* Test the condition if present, and collect if true. */
3911 if (!tpoint->cond
3912 || (condition_true_at_tracepoint
3913 ((struct tracepoint_hit_ctx *) &ctx, tpoint)))
3914 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
3915 stop_pc, tpoint);
3916
3917 if (stopping_tracepoint
3918 || trace_buffer_is_full
3919 || expr_eval_result != expr_eval_no_error)
3920 {
3921 stop_tracing ();
3922 }
3923 /* If the tracepoint had a 'while-stepping' action, then set
3924 the thread to collect this tracepoint on the following
3925 single-steps. */
3926 else if (tpoint->step_count > 0)
3927 {
3928 add_while_stepping_state (tinfo,
3929 tpoint->number, tpoint->address);
3930 }
3931
3932 ret = 1;
3933 }
3934 }
3935
3936 return ret;
3937}
3938
fa593d66
PA
3939#endif
3940
0fb4aa4b
PA
3941#if defined IN_PROCESS_AGENT && defined HAVE_UST
3942struct ust_marker_data;
3943static void collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
3944 CORE_ADDR stop_pc,
3945 struct tracepoint *tpoint,
3946 struct traceframe *tframe);
3947#endif
3948
219f2f23
PA
3949/* Create a trace frame for the hit of the given tracepoint in the
3950 given thread. */
3951
3952static void
3953collect_data_at_tracepoint (struct tracepoint_hit_ctx *ctx, CORE_ADDR stop_pc,
3954 struct tracepoint *tpoint)
3955{
3956 struct traceframe *tframe;
3957 int acti;
3958
3959 /* Only count it as a hit when we actually collect data. */
3960 tpoint->hit_count++;
3961
3962 /* If we've exceeded a defined pass count, record the event for
3963 later, and finish the collection for this hit. This test is only
3964 for nonstepping tracepoints, stepping tracepoints test at the end
3965 of their while-stepping loop. */
3966 if (tpoint->pass_count > 0
3967 && tpoint->hit_count >= tpoint->pass_count
3968 && tpoint->step_count == 0
3969 && stopping_tracepoint == NULL)
3970 stopping_tracepoint = tpoint;
3971
3972 trace_debug ("Making new traceframe for tracepoint %d at 0x%s, hit %ld",
3973 tpoint->number, paddress (tpoint->address), tpoint->hit_count);
3974
3975 tframe = add_traceframe (tpoint);
3976
3977 if (tframe)
3978 {
3979 for (acti = 0; acti < tpoint->numactions; ++acti)
3980 {
fa593d66 3981#ifndef IN_PROCESS_AGENT
219f2f23
PA
3982 trace_debug ("Tracepoint %d at 0x%s about to do action '%s'",
3983 tpoint->number, paddress (tpoint->address),
3984 tpoint->actions_str[acti]);
fa593d66 3985#endif
219f2f23
PA
3986
3987 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
3988 tpoint->actions[acti]);
3989 }
3990
3991 finish_traceframe (tframe);
3992 }
3993
3994 if (tframe == NULL && tracing)
3995 trace_buffer_is_full = 1;
3996}
3997
fa593d66
PA
3998#ifndef IN_PROCESS_AGENT
3999
219f2f23
PA
4000static void
4001collect_data_at_step (struct tracepoint_hit_ctx *ctx,
4002 CORE_ADDR stop_pc,
4003 struct tracepoint *tpoint, int current_step)
4004{
4005 struct traceframe *tframe;
4006 int acti;
4007
4008 trace_debug ("Making new step traceframe for "
4009 "tracepoint %d at 0x%s, step %d of %ld, hit %ld",
4010 tpoint->number, paddress (tpoint->address),
4011 current_step, tpoint->step_count,
4012 tpoint->hit_count);
4013
4014 tframe = add_traceframe (tpoint);
4015
4016 if (tframe)
4017 {
4018 for (acti = 0; acti < tpoint->num_step_actions; ++acti)
4019 {
4020 trace_debug ("Tracepoint %d at 0x%s about to do step action '%s'",
4021 tpoint->number, paddress (tpoint->address),
4022 tpoint->step_actions_str[acti]);
4023
4024 do_action_at_tracepoint (ctx, stop_pc, tpoint, tframe,
4025 tpoint->step_actions[acti]);
4026 }
4027
4028 finish_traceframe (tframe);
4029 }
4030
4031 if (tframe == NULL && tracing)
4032 trace_buffer_is_full = 1;
4033}
4034
fa593d66
PA
4035#endif
4036
219f2f23
PA
4037static struct regcache *
4038get_context_regcache (struct tracepoint_hit_ctx *ctx)
4039{
fa593d66
PA
4040 struct regcache *regcache = NULL;
4041
4042#ifdef IN_PROCESS_AGENT
4043 if (ctx->type == fast_tracepoint)
4044 {
4045 struct fast_tracepoint_ctx *fctx = (struct fast_tracepoint_ctx *) ctx;
4046 if (!fctx->regcache_initted)
4047 {
4048 fctx->regcache_initted = 1;
4049 init_register_cache (&fctx->regcache, fctx->regspace);
4050 supply_regblock (&fctx->regcache, NULL);
4051 supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
4052 }
4053 regcache = &fctx->regcache;
4054 }
0fb4aa4b
PA
4055#ifdef HAVE_UST
4056 if (ctx->type == static_tracepoint)
4057 {
493e2a69
MS
4058 struct static_tracepoint_ctx *sctx
4059 = (struct static_tracepoint_ctx *) ctx;
4060
0fb4aa4b
PA
4061 if (!sctx->regcache_initted)
4062 {
4063 sctx->regcache_initted = 1;
4064 init_register_cache (&sctx->regcache, sctx->regspace);
4065 supply_regblock (&sctx->regcache, NULL);
4066 /* Pass down the tracepoint address, because REGS doesn't
4067 include the PC, but we know what it must have been. */
4068 supply_static_tracepoint_registers (&sctx->regcache,
4069 (const unsigned char *)
4070 sctx->regs,
4071 sctx->tpoint->address);
4072 }
4073 regcache = &sctx->regcache;
4074 }
4075#endif
fa593d66
PA
4076#else
4077 if (ctx->type == trap_tracepoint)
4078 {
4079 struct trap_tracepoint_ctx *tctx = (struct trap_tracepoint_ctx *) ctx;
4080 regcache = tctx->regcache;
4081 }
4082#endif
219f2f23
PA
4083
4084 gdb_assert (regcache != NULL);
4085
4086 return regcache;
4087}
4088
4089static void
4090do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4091 CORE_ADDR stop_pc,
4092 struct tracepoint *tpoint,
4093 struct traceframe *tframe,
4094 struct tracepoint_action *taction)
4095{
4096 enum eval_result_type err;
4097
4098 switch (taction->type)
4099 {
4100 case 'M':
4101 {
4102 struct collect_memory_action *maction;
4103
4104 maction = (struct collect_memory_action *) taction;
4105
4106 trace_debug ("Want to collect %s bytes at 0x%s (basereg %d)",
4107 pulongest (maction->len),
4108 paddress (maction->addr), maction->basereg);
4109 /* (should use basereg) */
4110 agent_mem_read (tframe, NULL,
4111 (CORE_ADDR) maction->addr, maction->len);
4112 break;
4113 }
4114 case 'R':
4115 {
219f2f23
PA
4116 unsigned char *regspace;
4117 struct regcache tregcache;
4118 struct regcache *context_regcache;
4119
219f2f23
PA
4120
4121 trace_debug ("Want to collect registers");
4122
4123 /* Collect all registers for now. */
4124 regspace = add_traceframe_block (tframe,
4125 1 + register_cache_size ());
4126 if (regspace == NULL)
4127 {
4128 trace_debug ("Trace buffer block allocation failed, skipping");
4129 break;
4130 }
4131 /* Identify a register block. */
4132 *regspace = 'R';
4133
4134 context_regcache = get_context_regcache (ctx);
4135
4136 /* Wrap the regblock in a register cache (in the stack, we
4137 don't want to malloc here). */
4138 init_register_cache (&tregcache, regspace + 1);
4139
4140 /* Copy the register data to the regblock. */
4141 regcache_cpy (&tregcache, context_regcache);
4142
fa593d66 4143#ifndef IN_PROCESS_AGENT
219f2f23
PA
4144 /* On some platforms, trap-based tracepoints will have the PC
4145 pointing to the next instruction after the trap, but we
4146 don't want the user or GDB trying to guess whether the
4147 saved PC needs adjusting; so always record the adjusted
4148 stop_pc. Note that we can't use tpoint->address instead,
fa593d66
PA
4149 since it will be wrong for while-stepping actions. This
4150 adjustment is a nop for fast tracepoints collected from the
4151 in-process lib (but not if GDBserver is collecting one
4152 preemptively), since the PC had already been adjusted to
4153 contain the tracepoint's address by the jump pad. */
219f2f23
PA
4154 trace_debug ("Storing stop pc (0x%s) in regblock",
4155 paddress (tpoint->address));
4156
4157 /* This changes the regblock, not the thread's
4158 regcache. */
4159 regcache_write_pc (&tregcache, stop_pc);
fa593d66 4160#endif
219f2f23
PA
4161 }
4162 break;
4163 case 'X':
4164 {
4165 struct eval_expr_action *eaction;
4166
4167 eaction = (struct eval_expr_action *) taction;
4168
4169 trace_debug ("Want to evaluate expression");
4170
4171 err = eval_agent_expr (ctx, tframe, eaction->expr, NULL);
4172
4173 if (err != expr_eval_no_error)
4174 {
4175 record_tracepoint_error (tpoint, "action expression", err);
4176 return;
4177 }
4178 }
4179 break;
0fb4aa4b
PA
4180 case 'L':
4181 {
4182#if defined IN_PROCESS_AGENT && defined HAVE_UST
4183 trace_debug ("Want to collect static trace data");
4184 collect_ust_data_at_tracepoint (ctx, stop_pc,
4185 tpoint, tframe);
4186#else
4187 trace_debug ("warning: collecting static trace data, "
4188 "but static tracepoints are not supported");
4189#endif
4190 }
4191 break;
219f2f23
PA
4192 default:
4193 trace_debug ("unknown trace action '%c', ignoring", taction->type);
4194 break;
4195 }
4196}
4197
4198static int
4199condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
4200 struct tracepoint *tpoint)
4201{
4202 ULONGEST value = 0;
4203 enum eval_result_type err;
4204
c6beb2cb
PA
4205 /* Presently, gdbserver doesn't run compiled conditions, only the
4206 IPA does. If the program stops at a fast tracepoint's address
4207 (e.g., due to a breakpoint, trap tracepoint, or stepping),
4208 gdbserver preemptively collect the fast tracepoint. Later, on
4209 resume, gdbserver steps over the fast tracepoint like it steps
4210 over breakpoints, so that the IPA doesn't see that fast
4211 tracepoint. This avoids double collects of fast tracepoints in
4212 that stopping scenario. Having gdbserver itself handle the fast
4213 tracepoint gives the user a consistent view of when fast or trap
4214 tracepoints are collected, compared to an alternative where only
4215 trap tracepoints are collected on stop, and fast tracepoints on
4216 resume. When a fast tracepoint is being processed by gdbserver,
4217 it is always the non-compiled condition expression that is
4218 used. */
4219#ifdef IN_PROCESS_AGENT
6a271cae
PA
4220 if (tpoint->compiled_cond)
4221 err = ((condfn) (uintptr_t) (tpoint->compiled_cond)) (ctx, &value);
4222 else
c6beb2cb 4223#endif
6a271cae 4224 err = eval_agent_expr (ctx, NULL, tpoint->cond, &value);
219f2f23
PA
4225
4226 if (err != expr_eval_no_error)
4227 {
4228 record_tracepoint_error (tpoint, "condition", err);
4229 /* The error case must return false. */
4230 return 0;
4231 }
4232
4233 trace_debug ("Tracepoint %d at 0x%s condition evals to %s",
4234 tpoint->number, paddress (tpoint->address),
4235 pulongest (value));
4236 return (value ? 1 : 0);
4237}
4238
fa593d66
PA
4239#ifndef IN_PROCESS_AGENT
4240
219f2f23
PA
4241/* The packet form of an agent expression consists of an 'X', number
4242 of bytes in expression, a comma, and then the bytes. */
4243
4244static struct agent_expr *
4245parse_agent_expr (char **actparm)
4246{
4247 char *act = *actparm;
4248 ULONGEST xlen;
4249 struct agent_expr *aexpr;
4250
4251 ++act; /* skip the X */
4252 act = unpack_varlen_hex (act, &xlen);
4253 ++act; /* skip a comma */
4254 aexpr = xmalloc (sizeof (struct agent_expr));
4255 aexpr->length = xlen;
4256 aexpr->bytes = xmalloc (xlen);
4257 convert_ascii_to_int (act, aexpr->bytes, xlen);
4258 *actparm = act + (xlen * 2);
4259 return aexpr;
4260}
4261
4262/* Convert the bytes of an agent expression back into hex digits, so
4263 they can be printed or uploaded. This allocates the buffer,
4264 callers should free when they are done with it. */
4265
4266static char *
4267unparse_agent_expr (struct agent_expr *aexpr)
4268{
4269 char *rslt;
4270
4271 rslt = xmalloc (2 * aexpr->length + 1);
4272 convert_int_to_ascii (aexpr->bytes, rslt, aexpr->length);
4273 return rslt;
4274}
4275
fa593d66
PA
4276#endif
4277
94d5e490
TT
4278/* A wrapper for gdb_agent_op_names that does some bounds-checking. */
4279
4280static const char *
4281gdb_agent_op_name (int op)
4282{
4283 if (op < 0 || op >= gdb_agent_op_last || gdb_agent_op_names[op] == NULL)
4284 return "?undef?";
4285 return gdb_agent_op_names[op];
4286}
4287
219f2f23
PA
4288/* The agent expression evaluator, as specified by the GDB docs. It
4289 returns 0 if everything went OK, and a nonzero error code
4290 otherwise. */
4291
4292static enum eval_result_type
4293eval_agent_expr (struct tracepoint_hit_ctx *ctx,
4294 struct traceframe *tframe,
4295 struct agent_expr *aexpr,
4296 ULONGEST *rslt)
4297{
4298 int pc = 0;
4299#define STACK_MAX 100
4300 ULONGEST stack[STACK_MAX], top;
4301 int sp = 0;
4302 unsigned char op;
4303 int arg;
4304
4305 /* This union is a convenient way to convert representations. For
4306 now, assume a standard architecture where the hardware integer
4307 types have 8, 16, 32, 64 bit types. A more robust solution would
4308 be to import stdint.h from gnulib. */
4309 union
4310 {
4311 union
4312 {
4313 unsigned char bytes[1];
4314 unsigned char val;
4315 } u8;
4316 union
4317 {
4318 unsigned char bytes[2];
4319 unsigned short val;
4320 } u16;
4321 union
4322 {
4323 unsigned char bytes[4];
4324 unsigned int val;
4325 } u32;
4326 union
4327 {
4328 unsigned char bytes[8];
4329 ULONGEST val;
4330 } u64;
4331 } cnv;
4332
4333 if (aexpr->length == 0)
4334 {
4335 trace_debug ("empty agent expression");
4336 return expr_eval_empty_expression;
4337 }
4338
4339 /* Cache the stack top in its own variable. Much of the time we can
4340 operate on this variable, rather than dinking with the stack. It
4341 needs to be copied to the stack when sp changes. */
4342 top = 0;
4343
4344 while (1)
4345 {
4346 op = aexpr->bytes[pc++];
4347
4348 trace_debug ("About to interpret byte 0x%x", op);
4349
4350 switch (op)
4351 {
4352 case gdb_agent_op_add:
4353 top += stack[--sp];
4354 break;
4355
4356 case gdb_agent_op_sub:
4357 top = stack[--sp] - top;
4358 break;
4359
4360 case gdb_agent_op_mul:
4361 top *= stack[--sp];
4362 break;
4363
4364 case gdb_agent_op_div_signed:
4365 if (top == 0)
4366 {
4367 trace_debug ("Attempted to divide by zero");
4368 return expr_eval_divide_by_zero;
4369 }
4370 top = ((LONGEST) stack[--sp]) / ((LONGEST) top);
4371 break;
4372
4373 case gdb_agent_op_div_unsigned:
4374 if (top == 0)
4375 {
4376 trace_debug ("Attempted to divide by zero");
4377 return expr_eval_divide_by_zero;
4378 }
4379 top = stack[--sp] / top;
4380 break;
4381
4382 case gdb_agent_op_rem_signed:
4383 if (top == 0)
4384 {
4385 trace_debug ("Attempted to divide by zero");
4386 return expr_eval_divide_by_zero;
4387 }
4388 top = ((LONGEST) stack[--sp]) % ((LONGEST) top);
4389 break;
4390
4391 case gdb_agent_op_rem_unsigned:
4392 if (top == 0)
4393 {
4394 trace_debug ("Attempted to divide by zero");
4395 return expr_eval_divide_by_zero;
4396 }
4397 top = stack[--sp] % top;
4398 break;
4399
4400 case gdb_agent_op_lsh:
4401 top = stack[--sp] << top;
4402 break;
4403
4404 case gdb_agent_op_rsh_signed:
4405 top = ((LONGEST) stack[--sp]) >> top;
4406 break;
4407
4408 case gdb_agent_op_rsh_unsigned:
4409 top = stack[--sp] >> top;
4410 break;
4411
4412 case gdb_agent_op_trace:
4413 agent_mem_read (tframe,
4414 NULL, (CORE_ADDR) stack[--sp], (ULONGEST) top);
4415 if (--sp >= 0)
4416 top = stack[sp];
4417 break;
4418
4419 case gdb_agent_op_trace_quick:
4420 arg = aexpr->bytes[pc++];
4421 agent_mem_read (tframe, NULL, (CORE_ADDR) top, (ULONGEST) arg);
4422 break;
4423
4424 case gdb_agent_op_log_not:
4425 top = !top;
4426 break;
4427
4428 case gdb_agent_op_bit_and:
4429 top &= stack[--sp];
4430 break;
4431
4432 case gdb_agent_op_bit_or:
4433 top |= stack[--sp];
4434 break;
4435
4436 case gdb_agent_op_bit_xor:
4437 top ^= stack[--sp];
4438 break;
4439
4440 case gdb_agent_op_bit_not:
4441 top = ~top;
4442 break;
4443
4444 case gdb_agent_op_equal:
4445 top = (stack[--sp] == top);
4446 break;
4447
4448 case gdb_agent_op_less_signed:
4449 top = (((LONGEST) stack[--sp]) < ((LONGEST) top));
4450 break;
4451
4452 case gdb_agent_op_less_unsigned:
4453 top = (stack[--sp] < top);
4454 break;
4455
4456 case gdb_agent_op_ext:
4457 arg = aexpr->bytes[pc++];
4458 if (arg < (sizeof (LONGEST) * 8))
4459 {
4460 LONGEST mask = 1 << (arg - 1);
4461 top &= ((LONGEST) 1 << arg) - 1;
4462 top = (top ^ mask) - mask;
4463 }
4464 break;
4465
4466 case gdb_agent_op_ref8:
4467 agent_mem_read (tframe, cnv.u8.bytes, (CORE_ADDR) top, 1);
4468 top = cnv.u8.val;
4469 break;
4470
4471 case gdb_agent_op_ref16:
4472 agent_mem_read (tframe, cnv.u16.bytes, (CORE_ADDR) top, 2);
4473 top = cnv.u16.val;
4474 break;
4475
4476 case gdb_agent_op_ref32:
4477 agent_mem_read (tframe, cnv.u32.bytes, (CORE_ADDR) top, 4);
4478 top = cnv.u32.val;
4479 break;
4480
4481 case gdb_agent_op_ref64:
4482 agent_mem_read (tframe, cnv.u64.bytes, (CORE_ADDR) top, 8);
4483 top = cnv.u64.val;
4484 break;
4485
4486 case gdb_agent_op_if_goto:
4487 if (top)
4488 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4489 else
4490 pc += 2;
4491 if (--sp >= 0)
4492 top = stack[sp];
4493 break;
4494
4495 case gdb_agent_op_goto:
4496 pc = (aexpr->bytes[pc] << 8) + (aexpr->bytes[pc + 1]);
4497 break;
4498
4499 case gdb_agent_op_const8:
4500 /* Flush the cached stack top. */
4501 stack[sp++] = top;
4502 top = aexpr->bytes[pc++];
4503 break;
4504
4505 case gdb_agent_op_const16:
4506 /* Flush the cached stack top. */
4507 stack[sp++] = top;
4508 top = aexpr->bytes[pc++];
4509 top = (top << 8) + aexpr->bytes[pc++];
4510 break;
4511
4512 case gdb_agent_op_const32:
4513 /* Flush the cached stack top. */
4514 stack[sp++] = top;
4515 top = aexpr->bytes[pc++];
4516 top = (top << 8) + aexpr->bytes[pc++];
4517 top = (top << 8) + aexpr->bytes[pc++];
4518 top = (top << 8) + aexpr->bytes[pc++];
4519 break;
4520
4521 case gdb_agent_op_const64:
4522 /* Flush the cached stack top. */
4523 stack[sp++] = top;
4524 top = aexpr->bytes[pc++];
4525 top = (top << 8) + aexpr->bytes[pc++];
4526 top = (top << 8) + aexpr->bytes[pc++];
4527 top = (top << 8) + aexpr->bytes[pc++];
4528 top = (top << 8) + aexpr->bytes[pc++];
4529 top = (top << 8) + aexpr->bytes[pc++];
4530 top = (top << 8) + aexpr->bytes[pc++];
4531 top = (top << 8) + aexpr->bytes[pc++];
4532 break;
4533
4534 case gdb_agent_op_reg:
4535 /* Flush the cached stack top. */
4536 stack[sp++] = top;
4537 arg = aexpr->bytes[pc++];
4538 arg = (arg << 8) + aexpr->bytes[pc++];
4539 {
4540 int regnum = arg;
4541 struct regcache *regcache;
4542
4543 regcache = get_context_regcache (ctx);
4544
4545 switch (register_size (regnum))
4546 {
4547 case 8:
4548 collect_register (regcache, regnum, cnv.u64.bytes);
4549 top = cnv.u64.val;
4550 break;
4551 case 4:
4552 collect_register (regcache, regnum, cnv.u32.bytes);
4553 top = cnv.u32.val;
4554 break;
4555 case 2:
4556 collect_register (regcache, regnum, cnv.u16.bytes);
4557 top = cnv.u16.val;
4558 break;
4559 case 1:
4560 collect_register (regcache, regnum, cnv.u8.bytes);
4561 top = cnv.u8.val;
4562 break;
4563 default:
4564 internal_error (__FILE__, __LINE__,
4565 "unhandled register size");
4566 }
4567 }
4568 break;
4569
4570 case gdb_agent_op_end:
4571 trace_debug ("At end of expression, sp=%d, stack top cache=0x%s",
4572 sp, pulongest (top));
4573 if (rslt)
4574 {
4575 if (sp <= 0)
4576 {
4577 /* This should be an error */
4578 trace_debug ("Stack is empty, nothing to return");
4579 return expr_eval_empty_stack;
4580 }
4581 *rslt = top;
4582 }
4583 return expr_eval_no_error;
4584
4585 case gdb_agent_op_dup:
4586 stack[sp++] = top;
4587 break;
4588
4589 case gdb_agent_op_pop:
4590 if (--sp >= 0)
4591 top = stack[sp];
4592 break;
4593
c7f96d2b
TT
4594 case gdb_agent_op_pick:
4595 arg = aexpr->bytes[pc++];
4596 stack[sp] = top;
4597 top = stack[sp - arg];
4598 ++sp;
4599 break;
4600
4601 case gdb_agent_op_rot:
4602 {
4603 ULONGEST tem = stack[sp - 1];
4604
4605 stack[sp - 1] = stack[sp - 2];
4606 stack[sp - 2] = top;
4607 top = tem;
4608 }
4609 break;
4610
219f2f23
PA
4611 case gdb_agent_op_zero_ext:
4612 arg = aexpr->bytes[pc++];
4613 if (arg < (sizeof (LONGEST) * 8))
4614 top &= ((LONGEST) 1 << arg) - 1;
4615 break;
4616
4617 case gdb_agent_op_swap:
4618 /* Interchange top two stack elements, making sure top gets
4619 copied back onto stack. */
4620 stack[sp] = top;
4621 top = stack[sp - 1];
4622 stack[sp - 1] = stack[sp];
4623 break;
4624
4625 case gdb_agent_op_getv:
4626 /* Flush the cached stack top. */
4627 stack[sp++] = top;
4628 arg = aexpr->bytes[pc++];
4629 arg = (arg << 8) + aexpr->bytes[pc++];
4630 top = get_trace_state_variable_value (arg);
4631 break;
4632
4633 case gdb_agent_op_setv:
4634 arg = aexpr->bytes[pc++];
4635 arg = (arg << 8) + aexpr->bytes[pc++];
4636 set_trace_state_variable_value (arg, top);
4637 /* Note that we leave the value on the stack, for the
4638 benefit of later/enclosing expressions. */
4639 break;
4640
4641 case gdb_agent_op_tracev:
4642 arg = aexpr->bytes[pc++];
4643 arg = (arg << 8) + aexpr->bytes[pc++];
4644 agent_tsv_read (tframe, arg);
4645 break;
4646
4647 /* GDB never (currently) generates any of these ops. */
4648 case gdb_agent_op_float:
4649 case gdb_agent_op_ref_float:
4650 case gdb_agent_op_ref_double:
4651 case gdb_agent_op_ref_long_double:
4652 case gdb_agent_op_l_to_d:
4653 case gdb_agent_op_d_to_l:
4654 case gdb_agent_op_trace16:
4655 trace_debug ("Agent expression op 0x%x valid, but not handled",
4656 op);
4657 /* If ever GDB generates any of these, we don't have the
4658 option of ignoring. */
4659 return 1;
4660
4661 default:
4662 trace_debug ("Agent expression op 0x%x not recognized", op);
4663 /* Don't struggle on, things will just get worse. */
4664 return expr_eval_unrecognized_opcode;
4665 }
4666
4667 /* Check for stack badness. */
4668 if (sp >= (STACK_MAX - 1))
4669 {
4670 trace_debug ("Expression stack overflow");
4671 return expr_eval_stack_overflow;
4672 }
4673
4674 if (sp < 0)
4675 {
4676 trace_debug ("Expression stack underflow");
4677 return expr_eval_stack_underflow;
4678 }
4679
4680 trace_debug ("Op %s -> sp=%d, top=0x%s",
94d5e490 4681 gdb_agent_op_name (op), sp, pulongest (top));
219f2f23
PA
4682 }
4683}
4684
4685/* Do memory copies for bytecodes. */
4686/* Do the recording of memory blocks for actions and bytecodes. */
4687
4688static int
4689agent_mem_read (struct traceframe *tframe,
4690 unsigned char *to, CORE_ADDR from, ULONGEST len)
4691{
4692 unsigned char *mspace;
4693 ULONGEST remaining = len;
4694 unsigned short blocklen;
4695
4696 /* If a 'to' buffer is specified, use it. */
4697 if (to != NULL)
4698 {
4699 read_inferior_memory (from, to, len);
4700 return 0;
4701 }
4702
4703 /* Otherwise, create a new memory block in the trace buffer. */
4704 while (remaining > 0)
4705 {
4706 size_t sp;
4707
4708 blocklen = (remaining > 65535 ? 65535 : remaining);
4709 sp = 1 + sizeof (from) + sizeof (blocklen) + blocklen;
4710 mspace = add_traceframe_block (tframe, sp);
4711 if (mspace == NULL)
4712 return 1;
4713 /* Identify block as a memory block. */
4714 *mspace = 'M';
4715 ++mspace;
4716 /* Record address and size. */
4717 memcpy (mspace, &from, sizeof (from));
4718 mspace += sizeof (from);
4719 memcpy (mspace, &blocklen, sizeof (blocklen));
4720 mspace += sizeof (blocklen);
4721 /* Record the memory block proper. */
4722 read_inferior_memory (from, mspace, blocklen);
4723 trace_debug ("%d bytes recorded", blocklen);
4724 remaining -= blocklen;
4725 from += blocklen;
4726 }
4727 return 0;
4728}
4729
4730/* Record the value of a trace state variable. */
4731
4732static int
4733agent_tsv_read (struct traceframe *tframe, int n)
4734{
4735 unsigned char *vspace;
4736 LONGEST val;
4737
4738 vspace = add_traceframe_block (tframe,
4739 1 + sizeof (n) + sizeof (LONGEST));
4740 if (vspace == NULL)
4741 return 1;
4742 /* Identify block as a variable. */
4743 *vspace = 'V';
4744 /* Record variable's number and value. */
4745 memcpy (vspace + 1, &n, sizeof (n));
4746 val = get_trace_state_variable_value (n);
4747 memcpy (vspace + 1 + sizeof (n), &val, sizeof (val));
4748 trace_debug ("Variable %d recorded", n);
4749 return 0;
4750}
4751
fa593d66
PA
4752#ifndef IN_PROCESS_AGENT
4753
b3b9301e
PA
4754/* Callback for traceframe_walk_blocks, used to find a given block
4755 type in a traceframe. */
4756
4757static int
4758match_blocktype (char blocktype, unsigned char *dataptr, void *data)
4759{
4760 char *wantedp = data;
4761
4762 if (*wantedp == blocktype)
4763 return 1;
4764
4765 return 0;
4766}
4767
4768/* Walk over all traceframe blocks of the traceframe buffer starting
4769 at DATABASE, of DATASIZE bytes long, and call CALLBACK for each
4770 block found, passing in DATA unmodified. If CALLBACK returns true,
4771 this returns a pointer to where the block is found. Returns NULL
4772 if no callback call returned true, indicating that all blocks have
4773 been walked. */
4774
219f2f23 4775static unsigned char *
b3b9301e
PA
4776traceframe_walk_blocks (unsigned char *database, unsigned int datasize,
4777 int tfnum,
4778 int (*callback) (char blocktype,
4779 unsigned char *dataptr,
4780 void *data),
4781 void *data)
219f2f23
PA
4782{
4783 unsigned char *dataptr;
4784
4785 if (datasize == 0)
4786 {
4787 trace_debug ("traceframe %d has no data", tfnum);
4788 return NULL;
4789 }
4790
4791 /* Iterate through a traceframe's blocks, looking for a block of the
4792 requested type. */
4793 for (dataptr = database;
4794 dataptr < database + datasize;
4795 /* nothing */)
4796 {
4797 char blocktype;
4798 unsigned short mlen;
4799
4800 if (dataptr == trace_buffer_wrap)
4801 {
4802 /* Adjust to reflect wrapping part of the frame around to
4803 the beginning. */
4804 datasize = dataptr - database;
4805 dataptr = database = trace_buffer_lo;
4806 }
b3b9301e 4807
219f2f23
PA
4808 blocktype = *dataptr++;
4809
b3b9301e 4810 if ((*callback) (blocktype, dataptr, data))
219f2f23
PA
4811 return dataptr;
4812
4813 switch (blocktype)
4814 {
4815 case 'R':
4816 /* Skip over the registers block. */
4817 dataptr += register_cache_size ();
4818 break;
4819 case 'M':
4820 /* Skip over the memory block. */
4821 dataptr += sizeof (CORE_ADDR);
4822 memcpy (&mlen, dataptr, sizeof (mlen));
4823 dataptr += (sizeof (mlen) + mlen);
4824 break;
219f2f23
PA
4825 case 'V':
4826 /* Skip over the TSV block. */
4827 dataptr += (sizeof (int) + sizeof (LONGEST));
4828 break;
0fb4aa4b
PA
4829 case 'S':
4830 /* Skip over the static trace data block. */
4831 memcpy (&mlen, dataptr, sizeof (mlen));
4832 dataptr += (sizeof (mlen) + mlen);
4833 break;
219f2f23
PA
4834 default:
4835 trace_debug ("traceframe %d has unknown block type 0x%x",
4836 tfnum, blocktype);
4837 return NULL;
4838 }
4839 }
4840
4841 return NULL;
4842}
4843
b3b9301e
PA
4844/* Look for the block of type TYPE_WANTED in the trameframe starting
4845 at DATABASE of DATASIZE bytes long. TFNUM is the traceframe
4846 number. */
4847
4848static unsigned char *
4849traceframe_find_block_type (unsigned char *database, unsigned int datasize,
4850 int tfnum, char type_wanted)
4851{
4852 return traceframe_walk_blocks (database, datasize, tfnum,
4853 match_blocktype, &type_wanted);
4854}
4855
219f2f23
PA
4856static unsigned char *
4857traceframe_find_regblock (struct traceframe *tframe, int tfnum)
4858{
4859 unsigned char *regblock;
4860
4861 regblock = traceframe_find_block_type (tframe->data,
4862 tframe->data_size,
4863 tfnum, 'R');
4864
4865 if (regblock == NULL)
4866 trace_debug ("traceframe %d has no register data", tfnum);
4867
4868 return regblock;
4869}
4870
4871/* Get registers from a traceframe. */
4872
4873int
4874fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
4875{
4876 unsigned char *dataptr;
4877 struct tracepoint *tpoint;
4878 struct traceframe *tframe;
4879
4880 tframe = find_traceframe (tfnum);
4881
4882 if (tframe == NULL)
4883 {
4884 trace_debug ("traceframe %d not found", tfnum);
4885 return 1;
4886 }
4887
4888 dataptr = traceframe_find_regblock (tframe, tfnum);
4889 if (dataptr == NULL)
4890 {
1c79eb8a 4891 /* Mark registers unavailable. */
219f2f23
PA
4892 supply_regblock (regcache, NULL);
4893
4894 /* We can generally guess at a PC, although this will be
4895 misleading for while-stepping frames and multi-location
4896 tracepoints. */
4897 tpoint = find_next_tracepoint_by_number (NULL, tframe->tpnum);
4898 if (tpoint != NULL)
4899 regcache_write_pc (regcache, tpoint->address);
4900 }
4901 else
4902 supply_regblock (regcache, dataptr);
4903
4904 return 0;
4905}
4906
4907static CORE_ADDR
4908traceframe_get_pc (struct traceframe *tframe)
4909{
4910 struct regcache regcache;
4911 unsigned char *dataptr;
4912
4913 dataptr = traceframe_find_regblock (tframe, -1);
4914 if (dataptr == NULL)
4915 return 0;
4916
4917 init_register_cache (&regcache, dataptr);
4918 return regcache_read_pc (&regcache);
4919}
4920
4921/* Read a requested block of memory from a trace frame. */
4922
4923int
4924traceframe_read_mem (int tfnum, CORE_ADDR addr,
4925 unsigned char *buf, ULONGEST length,
4926 ULONGEST *nbytes)
4927{
4928 struct traceframe *tframe;
4929 unsigned char *database, *dataptr;
4930 unsigned int datasize;
4931 CORE_ADDR maddr;
4932 unsigned short mlen;
4933
4934 trace_debug ("traceframe_read_mem");
4935
4936 tframe = find_traceframe (tfnum);
4937
4938 if (!tframe)
4939 {
4940 trace_debug ("traceframe %d not found", tfnum);
4941 return 1;
4942 }
4943
4944 datasize = tframe->data_size;
4945 database = dataptr = &tframe->data[0];
4946
4947 /* Iterate through a traceframe's blocks, looking for memory. */
4948 while ((dataptr = traceframe_find_block_type (dataptr,
493e2a69
MS
4949 datasize
4950 - (dataptr - database),
219f2f23
PA
4951 tfnum, 'M')) != NULL)
4952 {
4953 memcpy (&maddr, dataptr, sizeof (maddr));
4954 dataptr += sizeof (maddr);
4955 memcpy (&mlen, dataptr, sizeof (mlen));
4956 dataptr += sizeof (mlen);
4957 trace_debug ("traceframe %d has %d bytes at %s",
4958 tfnum, mlen, paddress (maddr));
4959
764880b7
PA
4960 /* If the block includes the first part of the desired range,
4961 return as much it has; GDB will re-request the remainder,
4962 which might be in a different block of this trace frame. */
4963 if (maddr <= addr && addr < (maddr + mlen))
219f2f23 4964 {
764880b7
PA
4965 ULONGEST amt = (maddr + mlen) - addr;
4966 if (amt > length)
4967 amt = length;
4968
4969 memcpy (buf, dataptr + (addr - maddr), amt);
4970 *nbytes = amt;
219f2f23
PA
4971 return 0;
4972 }
4973
4974 /* Skip over this block. */
4975 dataptr += mlen;
4976 }
4977
4978 trace_debug ("traceframe %d has no memory data for the desired region",
4979 tfnum);
4980
4981 *nbytes = 0;
4982 return 0;
4983}
4984
4985static int
4986traceframe_read_tsv (int tsvnum, LONGEST *val)
4987{
4988 int tfnum;
4989 struct traceframe *tframe;
4990 unsigned char *database, *dataptr;
4991 unsigned int datasize;
4992 int vnum;
4993
4994 trace_debug ("traceframe_read_tsv");
4995
4996 tfnum = current_traceframe;
4997
4998 if (tfnum < 0)
4999 {
5000 trace_debug ("no current traceframe");
5001 return 1;
5002 }
5003
5004 tframe = find_traceframe (tfnum);
5005
5006 if (tframe == NULL)
5007 {
5008 trace_debug ("traceframe %d not found", tfnum);
5009 return 1;
5010 }
5011
5012 datasize = tframe->data_size;
5013 database = dataptr = &tframe->data[0];
5014
5015 /* Iterate through a traceframe's blocks, looking for the tsv. */
5016 while ((dataptr = traceframe_find_block_type (dataptr,
493e2a69
MS
5017 datasize
5018 - (dataptr - database),
219f2f23
PA
5019 tfnum, 'V')) != NULL)
5020 {
5021 memcpy (&vnum, dataptr, sizeof (vnum));
5022 dataptr += sizeof (vnum);
5023
5024 trace_debug ("traceframe %d has variable %d", tfnum, vnum);
5025
5026 /* Check that this is the variable we want. */
5027 if (tsvnum == vnum)
5028 {
5029 memcpy (val, dataptr, sizeof (*val));
5030 return 0;
5031 }
5032
5033 /* Skip over this block. */
5034 dataptr += sizeof (LONGEST);
5035 }
5036
5037 trace_debug ("traceframe %d has no data for variable %d",
5038 tfnum, tsvnum);
5039 return 1;
5040}
5041
0fb4aa4b
PA
5042/* Read a requested block of static tracepoint data from a trace
5043 frame. */
5044
5045int
5046traceframe_read_sdata (int tfnum, ULONGEST offset,
5047 unsigned char *buf, ULONGEST length,
5048 ULONGEST *nbytes)
5049{
5050 struct traceframe *tframe;
5051 unsigned char *database, *dataptr;
5052 unsigned int datasize;
5053 unsigned short mlen;
5054
5055 trace_debug ("traceframe_read_sdata");
5056
5057 tframe = find_traceframe (tfnum);
5058
5059 if (!tframe)
5060 {
5061 trace_debug ("traceframe %d not found", tfnum);
5062 return 1;
5063 }
5064
5065 datasize = tframe->data_size;
5066 database = &tframe->data[0];
5067
5068 /* Iterate through a traceframe's blocks, looking for static
5069 tracepoint data. */
5070 dataptr = traceframe_find_block_type (database, datasize,
5071 tfnum, 'S');
5072 if (dataptr != NULL)
5073 {
5074 memcpy (&mlen, dataptr, sizeof (mlen));
5075 dataptr += sizeof (mlen);
5076 if (offset < mlen)
5077 {
5078 if (offset + length > mlen)
5079 length = mlen - offset;
5080
5081 memcpy (buf, dataptr, length);
5082 *nbytes = length;
5083 }
5084 else
5085 *nbytes = 0;
5086 return 0;
5087 }
5088
5089 trace_debug ("traceframe %d has no static trace data", tfnum);
5090
5091 *nbytes = 0;
5092 return 0;
5093}
5094
b3b9301e
PA
5095/* Callback for traceframe_walk_blocks. Builds a traceframe-info
5096 object. DATA is pointer to a struct buffer holding the
5097 traceframe-info object being built. */
5098
5099static int
5100build_traceframe_info_xml (char blocktype, unsigned char *dataptr, void *data)
5101{
5102 struct buffer *buffer = data;
5103
5104 switch (blocktype)
5105 {
5106 case 'M':
5107 {
5108 unsigned short mlen;
5109 CORE_ADDR maddr;
5110
5111 memcpy (&maddr, dataptr, sizeof (maddr));
5112 dataptr += sizeof (maddr);
5113 memcpy (&mlen, dataptr, sizeof (mlen));
5114 dataptr += sizeof (mlen);
5115 buffer_xml_printf (buffer,
5116 "<memory start=\"0x%s\" length=\"0x%s\"/>\n",
5117 paddress (maddr), phex_nz (mlen, sizeof (mlen)));
5118 break;
5119 }
5120 case 'V':
5121 case 'R':
5122 case 'S':
5123 {
5124 break;
5125 }
5126 default:
5127 warning ("Unhandled trace block type (%d) '%c ' "
5128 "while building trace frame info.",
5129 blocktype, blocktype);
5130 break;
5131 }
5132
5133 return 0;
5134}
5135
5136/* Build a traceframe-info object for traceframe number TFNUM into
5137 BUFFER. */
5138
5139int
5140traceframe_read_info (int tfnum, struct buffer *buffer)
5141{
5142 struct traceframe *tframe;
5143
5144 trace_debug ("traceframe_read_info");
5145
5146 tframe = find_traceframe (tfnum);
5147
5148 if (!tframe)
5149 {
5150 trace_debug ("traceframe %d not found", tfnum);
5151 return 1;
5152 }
5153
5154 buffer_grow_str (buffer, "<traceframe-info>\n");
5155 traceframe_walk_blocks (tframe->data, tframe->data_size,
5156 tfnum, build_traceframe_info_xml, buffer);
5157 buffer_grow_str0 (buffer, "</traceframe-info>\n");
5158 return 0;
5159}
5160
fa593d66
PA
5161/* Return the first fast tracepoint whose jump pad contains PC. */
5162
5163static struct tracepoint *
5164fast_tracepoint_from_jump_pad_address (CORE_ADDR pc)
5165{
5166 struct tracepoint *tpoint;
5167
5168 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5169 if (tpoint->type == fast_tracepoint)
5170 if (tpoint->jump_pad <= pc && pc < tpoint->jump_pad_end)
5171 return tpoint;
5172
5173 return NULL;
5174}
5175
5176/* Return GDBserver's tracepoint that matches the IP Agent's
5177 tracepoint object that lives at IPA_TPOINT_OBJ in the IP Agent's
5178 address space. */
5179
5180static struct tracepoint *
5181fast_tracepoint_from_ipa_tpoint_address (CORE_ADDR ipa_tpoint_obj)
5182{
5183 struct tracepoint *tpoint;
5184
5185 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
5186 if (tpoint->type == fast_tracepoint)
5187 if (tpoint->obj_addr_on_target == ipa_tpoint_obj)
5188 return tpoint;
5189
5190 return NULL;
5191}
5192
5193#endif
5194
5195/* The type of the object that is used to synchronize fast tracepoint
5196 collection. */
5197
5198typedef struct collecting_t
5199{
5200 /* The fast tracepoint number currently collecting. */
5201 uintptr_t tpoint;
5202
5203 /* A number that GDBserver can use to identify the thread that is
5204 presently holding the collect lock. This need not (and usually
5205 is not) the thread id, as getting the current thread ID usually
5206 requires a system call, which we want to avoid like the plague.
5207 Usually this is thread's TCB, found in the TLS (pseudo-)
5208 register, which is readable with a single insn on several
5209 architectures. */
5210 uintptr_t thread_area;
5211} collecting_t;
5212
5213#ifndef IN_PROCESS_AGENT
5214
5215void
5216force_unlock_trace_buffer (void)
5217{
5218 write_inferior_data_pointer (ipa_sym_addrs.addr_collecting, 0);
5219}
5220
5221/* Check if the thread identified by THREAD_AREA which is stopped at
5222 STOP_PC, is presently locking the fast tracepoint collection, and
5223 if so, gather some status of said collection. Returns 0 if the
5224 thread isn't collecting or in the jump pad at all. 1, if in the
5225 jump pad (or within gdb_collect) and hasn't executed the adjusted
5226 original insn yet (can set a breakpoint there and run to it). 2,
5227 if presently executing the adjusted original insn --- in which
5228 case, if we want to move the thread out of the jump pad, we need to
5229 single-step it until this function returns 0. */
5230
5231int
5232fast_tracepoint_collecting (CORE_ADDR thread_area,
5233 CORE_ADDR stop_pc,
5234 struct fast_tpoint_collect_status *status)
5235{
5236 CORE_ADDR ipa_collecting;
5237 CORE_ADDR ipa_gdb_jump_pad_buffer, ipa_gdb_jump_pad_buffer_end;
5238 struct tracepoint *tpoint;
5239 int needs_breakpoint;
5240
5241 /* The thread THREAD_AREA is either:
5242
5243 0. not collecting at all, not within the jump pad, or within
5244 gdb_collect or one of its callees.
5245
5246 1. in the jump pad and haven't reached gdb_collect
5247
5248 2. within gdb_collect (out of the jump pad) (collect is set)
5249
5250 3. we're in the jump pad, after gdb_collect having returned,
5251 possibly executing the adjusted insns.
5252
5253 For cases 1 and 3, `collecting' may or not be set. The jump pad
5254 doesn't have any complicated jump logic, so we can tell if the
5255 thread is executing the adjust original insn or not by just
5256 matching STOP_PC with known jump pad addresses. If we it isn't
5257 yet executing the original insn, set a breakpoint there, and let
5258 the thread run to it, so to quickly step over a possible (many
5259 insns) gdb_collect call. Otherwise, or when the breakpoint is
5260 hit, only a few (small number of) insns are left to be executed
5261 in the jump pad. Single-step the thread until it leaves the
5262 jump pad. */
5263
5264 again:
5265 tpoint = NULL;
5266 needs_breakpoint = 0;
5267 trace_debug ("fast_tracepoint_collecting");
5268
5269 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer,
5270 &ipa_gdb_jump_pad_buffer))
5271 fatal ("error extracting `gdb_jump_pad_buffer'");
5272 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_jump_pad_buffer_end,
5273 &ipa_gdb_jump_pad_buffer_end))
5274 fatal ("error extracting `gdb_jump_pad_buffer_end'");
5275
493e2a69
MS
5276 if (ipa_gdb_jump_pad_buffer <= stop_pc
5277 && stop_pc < ipa_gdb_jump_pad_buffer_end)
fa593d66
PA
5278 {
5279 /* We can tell which tracepoint(s) the thread is collecting by
5280 matching the jump pad address back to the tracepoint. */
5281 tpoint = fast_tracepoint_from_jump_pad_address (stop_pc);
5282 if (tpoint == NULL)
5283 {
5284 warning ("in jump pad, but no matching tpoint?");
5285 return 0;
5286 }
5287 else
5288 {
5289 trace_debug ("in jump pad of tpoint (%d, %s); jump_pad(%s, %s); "
5290 "adj_insn(%s, %s)",
5291 tpoint->number, paddress (tpoint->address),
5292 paddress (tpoint->jump_pad),
5293 paddress (tpoint->jump_pad_end),
5294 paddress (tpoint->adjusted_insn_addr),
5295 paddress (tpoint->adjusted_insn_addr_end));
5296 }
5297
5298 /* Definitely in the jump pad. May or may not need
5299 fast-exit-jump-pad breakpoint. */
5300 if (tpoint->jump_pad <= stop_pc
5301 && stop_pc < tpoint->adjusted_insn_addr)
5302 needs_breakpoint = 1;
5303 }
5304 else
5305 {
5306 collecting_t ipa_collecting_obj;
5307
5308 /* If `collecting' is set/locked, then the THREAD_AREA thread
5309 may or not be the one holding the lock. We have to read the
5310 lock to find out. */
5311
5312 if (read_inferior_data_pointer (ipa_sym_addrs.addr_collecting,
5313 &ipa_collecting))
5314 {
5315 trace_debug ("fast_tracepoint_collecting:"
5316 " failed reading 'collecting' in the inferior");
5317 return 0;
5318 }
5319
5320 if (!ipa_collecting)
5321 {
5322 trace_debug ("fast_tracepoint_collecting: not collecting"
5323 " (and nobody is).");
5324 return 0;
5325 }
5326
5327 /* Some thread is collecting. Check which. */
5328 if (read_inferior_memory (ipa_collecting,
5329 (unsigned char *) &ipa_collecting_obj,
5330 sizeof (ipa_collecting_obj)) != 0)
5331 goto again;
5332
5333 if (ipa_collecting_obj.thread_area != thread_area)
5334 {
5335 trace_debug ("fast_tracepoint_collecting: not collecting "
5336 "(another thread is)");
5337 return 0;
5338 }
5339
5340 tpoint
5341 = fast_tracepoint_from_ipa_tpoint_address (ipa_collecting_obj.tpoint);
5342 if (tpoint == NULL)
5343 {
5344 warning ("fast_tracepoint_collecting: collecting, "
5345 "but tpoint %s not found?",
5346 paddress ((CORE_ADDR) ipa_collecting_obj.tpoint));
5347 return 0;
5348 }
5349
5350 /* The thread is within `gdb_collect', skip over the rest of
5351 fast tracepoint collection quickly using a breakpoint. */
5352 needs_breakpoint = 1;
5353 }
5354
5355 /* The caller wants a bit of status detail. */
5356 if (status != NULL)
5357 {
5358 status->tpoint_num = tpoint->number;
5359 status->tpoint_addr = tpoint->address;
5360 status->adjusted_insn_addr = tpoint->adjusted_insn_addr;
5361 status->adjusted_insn_addr_end = tpoint->adjusted_insn_addr_end;
5362 }
5363
5364 if (needs_breakpoint)
5365 {
5366 /* Hasn't executed the original instruction yet. Set breakpoint
5367 there, and wait till it's hit, then single-step until exiting
5368 the jump pad. */
5369
5370 trace_debug ("\
5371fast_tracepoint_collecting, returning continue-until-break at %s",
5372 paddress (tpoint->adjusted_insn_addr));
5373
5374 return 1; /* continue */
5375 }
5376 else
5377 {
5378 /* Just single-step until exiting the jump pad. */
5379
5380 trace_debug ("fast_tracepoint_collecting, returning "
5381 "need-single-step (%s-%s)",
5382 paddress (tpoint->adjusted_insn_addr),
5383 paddress (tpoint->adjusted_insn_addr_end));
5384
5385 return 2; /* single-step */
5386 }
5387}
5388
5389#endif
5390
5391#ifdef IN_PROCESS_AGENT
5392
5393/* The global fast tracepoint collect lock. Points to a collecting_t
5394 object built on the stack by the jump pad, if presently locked;
5395 NULL if it isn't locked. Note that this lock *must* be set while
5396 executing any *function other than the jump pad. See
5397 fast_tracepoint_collecting. */
5398static collecting_t * ATTR_USED collecting;
5399
5400/* This routine, called from the jump pad (in asm) is designed to be
5401 called from the jump pads of fast tracepoints, thus it is on the
5402 critical path. */
5403
5404IP_AGENT_EXPORT void ATTR_USED
5405gdb_collect (struct tracepoint *tpoint, unsigned char *regs)
5406{
5407 struct fast_tracepoint_ctx ctx;
5408
5409 /* Don't do anything until the trace run is completely set up. */
5410 if (!tracing)
5411 return;
5412
d248b706
KY
5413 if (!tpoint->enabled)
5414 return;
5415
fa593d66
PA
5416 ctx.base.type = fast_tracepoint;
5417 ctx.regs = regs;
5418 ctx.regcache_initted = 0;
5419 ctx.tpoint = tpoint;
5420
5421 /* Wrap the regblock in a register cache (in the stack, we don't
5422 want to malloc here). */
5423 ctx.regspace = alloca (register_cache_size ());
5424 if (ctx.regspace == NULL)
5425 {
5426 trace_debug ("Trace buffer block allocation failed, skipping");
5427 return;
5428 }
5429
5430 /* Test the condition if present, and collect if true. */
5431 if (tpoint->cond == NULL
5432 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5433 tpoint))
5434 {
5435 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
5436 tpoint->address, tpoint);
5437
5438 /* Note that this will cause original insns to be written back
5439 to where we jumped from, but that's OK because we're jumping
5440 back to the next whole instruction. This will go badly if
5441 instruction restoration is not atomic though. */
5442 if (stopping_tracepoint
5443 || trace_buffer_is_full
5444 || expr_eval_result != expr_eval_no_error)
5445 stop_tracing ();
5446 }
5447 else
5448 {
5449 /* If there was a condition and it evaluated to false, the only
5450 way we would stop tracing is if there was an error during
5451 condition expression evaluation. */
5452 if (expr_eval_result != expr_eval_no_error)
5453 stop_tracing ();
5454 }
5455}
5456
5457#endif
5458
5459#ifndef IN_PROCESS_AGENT
5460
6a271cae
PA
5461/* Bytecode compilation. */
5462
5463CORE_ADDR current_insn_ptr;
5464
5465int emit_error;
5466
5467struct bytecode_address
5468{
5469 int pc;
5470 CORE_ADDR address;
5471 int goto_pc;
5472 /* Offset and size of field to be modified in the goto block. */
5473 int from_offset, from_size;
5474 struct bytecode_address *next;
5475} *bytecode_address_table;
5476
5477CORE_ADDR
5478get_raw_reg_func_addr (void)
5479{
5480 return ipa_sym_addrs.addr_get_raw_reg;
5481}
5482
5483static void
5484emit_prologue (void)
5485{
5486 target_emit_ops ()->emit_prologue ();
5487}
5488
5489static void
5490emit_epilogue (void)
5491{
5492 target_emit_ops ()->emit_epilogue ();
5493}
5494
5495static void
5496emit_add (void)
5497{
5498 target_emit_ops ()->emit_add ();
5499}
5500
5501static void
5502emit_sub (void)
5503{
5504 target_emit_ops ()->emit_sub ();
5505}
5506
5507static void
5508emit_mul (void)
5509{
5510 target_emit_ops ()->emit_mul ();
5511}
5512
5513static void
5514emit_lsh (void)
5515{
5516 target_emit_ops ()->emit_lsh ();
5517}
5518
5519static void
5520emit_rsh_signed (void)
5521{
5522 target_emit_ops ()->emit_rsh_signed ();
5523}
5524
5525static void
5526emit_rsh_unsigned (void)
5527{
5528 target_emit_ops ()->emit_rsh_unsigned ();
5529}
5530
5531static void
5532emit_ext (int arg)
5533{
5534 target_emit_ops ()->emit_ext (arg);
5535}
5536
5537static void
5538emit_log_not (void)
5539{
5540 target_emit_ops ()->emit_log_not ();
5541}
5542
5543static void
5544emit_bit_and (void)
5545{
5546 target_emit_ops ()->emit_bit_and ();
5547}
5548
5549static void
5550emit_bit_or (void)
5551{
5552 target_emit_ops ()->emit_bit_or ();
5553}
5554
5555static void
5556emit_bit_xor (void)
5557{
5558 target_emit_ops ()->emit_bit_xor ();
5559}
5560
5561static void
5562emit_bit_not (void)
5563{
5564 target_emit_ops ()->emit_bit_not ();
5565}
5566
5567static void
5568emit_equal (void)
5569{
5570 target_emit_ops ()->emit_equal ();
5571}
5572
5573static void
5574emit_less_signed (void)
5575{
5576 target_emit_ops ()->emit_less_signed ();
5577}
5578
5579static void
5580emit_less_unsigned (void)
5581{
5582 target_emit_ops ()->emit_less_unsigned ();
5583}
5584
5585static void
5586emit_ref (int size)
5587{
5588 target_emit_ops ()->emit_ref (size);
5589}
5590
5591static void
5592emit_if_goto (int *offset_p, int *size_p)
5593{
5594 target_emit_ops ()->emit_if_goto (offset_p, size_p);
5595}
5596
5597static void
5598emit_goto (int *offset_p, int *size_p)
5599{
5600 target_emit_ops ()->emit_goto (offset_p, size_p);
5601}
5602
5603static void
5604write_goto_address (CORE_ADDR from, CORE_ADDR to, int size)
5605{
5606 target_emit_ops ()->write_goto_address (from, to, size);
5607}
5608
5609static void
4e29fb54 5610emit_const (LONGEST num)
6a271cae
PA
5611{
5612 target_emit_ops ()->emit_const (num);
5613}
5614
5615static void
5616emit_reg (int reg)
5617{
5618 target_emit_ops ()->emit_reg (reg);
5619}
5620
5621static void
5622emit_pop (void)
5623{
5624 target_emit_ops ()->emit_pop ();
5625}
5626
5627static void
5628emit_stack_flush (void)
5629{
5630 target_emit_ops ()->emit_stack_flush ();
5631}
5632
5633static void
5634emit_zero_ext (int arg)
5635{
5636 target_emit_ops ()->emit_zero_ext (arg);
5637}
5638
5639static void
5640emit_swap (void)
5641{
5642 target_emit_ops ()->emit_swap ();
5643}
5644
5645static void
5646emit_stack_adjust (int n)
5647{
5648 target_emit_ops ()->emit_stack_adjust (n);
5649}
5650
5651/* FN's prototype is `LONGEST(*fn)(int)'. */
5652
5653static void
5654emit_int_call_1 (CORE_ADDR fn, int arg1)
5655{
5656 target_emit_ops ()->emit_int_call_1 (fn, arg1);
5657}
5658
4e29fb54 5659/* FN's prototype is `void(*fn)(int,LONGEST)'. */
6a271cae
PA
5660
5661static void
5662emit_void_call_2 (CORE_ADDR fn, int arg1)
5663{
5664 target_emit_ops ()->emit_void_call_2 (fn, arg1);
5665}
5666
6b9801d4
SS
5667static void
5668emit_eq_goto (int *offset_p, int *size_p)
5669{
5670 target_emit_ops ()->emit_eq_goto (offset_p, size_p);
5671}
5672
5673static void
5674emit_ne_goto (int *offset_p, int *size_p)
5675{
5676 target_emit_ops ()->emit_ne_goto (offset_p, size_p);
5677}
5678
5679static void
5680emit_lt_goto (int *offset_p, int *size_p)
5681{
5682 target_emit_ops ()->emit_lt_goto (offset_p, size_p);
5683}
5684
5685static void
5686emit_ge_goto (int *offset_p, int *size_p)
5687{
5688 target_emit_ops ()->emit_ge_goto (offset_p, size_p);
5689}
5690
5691static void
5692emit_gt_goto (int *offset_p, int *size_p)
5693{
5694 target_emit_ops ()->emit_gt_goto (offset_p, size_p);
5695}
5696
5697static void
5698emit_le_goto (int *offset_p, int *size_p)
5699{
5700 target_emit_ops ()->emit_le_goto (offset_p, size_p);
5701}
5702
6a271cae
PA
5703static enum eval_result_type compile_bytecodes (struct agent_expr *aexpr);
5704
5705static void
493e2a69
MS
5706compile_tracepoint_condition (struct tracepoint *tpoint,
5707 CORE_ADDR *jump_entry)
6a271cae
PA
5708{
5709 CORE_ADDR entry_point = *jump_entry;
5710 enum eval_result_type err;
5711
5712 trace_debug ("Starting condition compilation for tracepoint %d\n",
5713 tpoint->number);
5714
5715 /* Initialize the global pointer to the code being built. */
5716 current_insn_ptr = *jump_entry;
5717
5718 emit_prologue ();
5719
5720 err = compile_bytecodes (tpoint->cond);
5721
5722 if (err == expr_eval_no_error)
5723 {
5724 emit_epilogue ();
5725
5726 /* Record the beginning of the compiled code. */
5727 tpoint->compiled_cond = entry_point;
5728
5729 trace_debug ("Condition compilation for tracepoint %d complete\n",
5730 tpoint->number);
5731 }
5732 else
5733 {
5734 /* Leave the unfinished code in situ, but don't point to it. */
5735
5736 tpoint->compiled_cond = 0;
5737
5738 trace_debug ("Condition compilation for tracepoint %d failed, "
5739 "error code %d",
5740 tpoint->number, err);
5741 }
5742
5743 /* Update the code pointer passed in. Note that we do this even if
5744 the compile fails, so that we can look at the partial results
5745 instead of letting them be overwritten. */
5746 *jump_entry = current_insn_ptr;
5747
5748 /* Leave a gap, to aid dump decipherment. */
5749 *jump_entry += 16;
5750}
5751
6b9801d4
SS
5752/* Scan an agent expression for any evidence that the given PC is the
5753 target of a jump bytecode in the expression. */
5754
5755int
5756is_goto_target (struct agent_expr *aexpr, int pc)
5757{
5758 int i;
5759 unsigned char op;
5760
5761 for (i = 0; i < aexpr->length; i += 1 + gdb_agent_op_sizes[op])
5762 {
5763 op = aexpr->bytes[i];
5764
5765 if (op == gdb_agent_op_goto || op == gdb_agent_op_if_goto)
5766 {
5767 int target = (aexpr->bytes[i + 1] << 8) + aexpr->bytes[i + 2];
5768 if (target == pc)
5769 return 1;
5770 }
5771 }
5772
5773 return 0;
5774}
5775
6a271cae
PA
5776/* Given an agent expression, turn it into native code. */
5777
5778static enum eval_result_type
5779compile_bytecodes (struct agent_expr *aexpr)
5780{
5781 int pc = 0;
5782 int done = 0;
6b9801d4 5783 unsigned char op, next_op;
6a271cae
PA
5784 int arg;
5785 /* This is only used to build 64-bit value for constants. */
5786 ULONGEST top;
5787 struct bytecode_address *aentry, *aentry2;
5788
5789#define UNHANDLED \
5790 do \
5791 { \
5792 trace_debug ("Cannot compile op 0x%x\n", op); \
5793 return expr_eval_unhandled_opcode; \
5794 } while (0)
5795
5796 if (aexpr->length == 0)
5797 {
5798 trace_debug ("empty agent expression\n");
5799 return expr_eval_empty_expression;
5800 }
5801
5802 bytecode_address_table = NULL;
5803
5804 while (!done)
5805 {
5806 op = aexpr->bytes[pc];
5807
5808 trace_debug ("About to compile op 0x%x, pc=%d\n", op, pc);
5809
5810 /* Record the compiled-code address of the bytecode, for use by
5811 jump instructions. */
5812 aentry = xmalloc (sizeof (struct bytecode_address));
5813 aentry->pc = pc;
5814 aentry->address = current_insn_ptr;
5815 aentry->goto_pc = -1;
5816 aentry->from_offset = aentry->from_size = 0;
5817 aentry->next = bytecode_address_table;
5818 bytecode_address_table = aentry;
5819
5820 ++pc;
5821
5822 emit_error = 0;
5823
5824 switch (op)
5825 {
5826 case gdb_agent_op_add:
5827 emit_add ();
5828 break;
5829
5830 case gdb_agent_op_sub:
5831 emit_sub ();
5832 break;
5833
5834 case gdb_agent_op_mul:
5835 emit_mul ();
5836 break;
5837
5838 case gdb_agent_op_div_signed:
5839 UNHANDLED;
5840 break;
5841
5842 case gdb_agent_op_div_unsigned:
5843 UNHANDLED;
5844 break;
5845
5846 case gdb_agent_op_rem_signed:
5847 UNHANDLED;
5848 break;
5849
5850 case gdb_agent_op_rem_unsigned:
5851 UNHANDLED;
5852 break;
5853
5854 case gdb_agent_op_lsh:
5855 emit_lsh ();
5856 break;
5857
5858 case gdb_agent_op_rsh_signed:
5859 emit_rsh_signed ();
5860 break;
5861
5862 case gdb_agent_op_rsh_unsigned:
5863 emit_rsh_unsigned ();
5864 break;
5865
5866 case gdb_agent_op_trace:
5867 UNHANDLED;
5868 break;
5869
5870 case gdb_agent_op_trace_quick:
5871 UNHANDLED;
5872 break;
5873
5874 case gdb_agent_op_log_not:
5875 emit_log_not ();
5876 break;
5877
5878 case gdb_agent_op_bit_and:
5879 emit_bit_and ();
5880 break;
5881
5882 case gdb_agent_op_bit_or:
5883 emit_bit_or ();
5884 break;
5885
5886 case gdb_agent_op_bit_xor:
5887 emit_bit_xor ();
5888 break;
5889
5890 case gdb_agent_op_bit_not:
5891 emit_bit_not ();
5892 break;
5893
5894 case gdb_agent_op_equal:
6b9801d4
SS
5895 next_op = aexpr->bytes[pc];
5896 if (next_op == gdb_agent_op_if_goto
5897 && !is_goto_target (aexpr, pc)
5898 && target_emit_ops ()->emit_eq_goto)
5899 {
5900 trace_debug ("Combining equal & if_goto");
5901 pc += 1;
5902 aentry->pc = pc;
5903 arg = aexpr->bytes[pc++];
5904 arg = (arg << 8) + aexpr->bytes[pc++];
5905 aentry->goto_pc = arg;
5906 emit_eq_goto (&(aentry->from_offset), &(aentry->from_size));
5907 }
5908 else if (next_op == gdb_agent_op_log_not
5909 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
5910 && !is_goto_target (aexpr, pc + 1)
5911 && target_emit_ops ()->emit_ne_goto)
5912 {
5913 trace_debug ("Combining equal & log_not & if_goto");
5914 pc += 2;
5915 aentry->pc = pc;
5916 arg = aexpr->bytes[pc++];
5917 arg = (arg << 8) + aexpr->bytes[pc++];
5918 aentry->goto_pc = arg;
5919 emit_ne_goto (&(aentry->from_offset), &(aentry->from_size));
5920 }
5921 else
5922 emit_equal ();
6a271cae
PA
5923 break;
5924
5925 case gdb_agent_op_less_signed:
6b9801d4
SS
5926 next_op = aexpr->bytes[pc];
5927 if (next_op == gdb_agent_op_if_goto
5928 && !is_goto_target (aexpr, pc))
5929 {
5930 trace_debug ("Combining less_signed & if_goto");
5931 pc += 1;
5932 aentry->pc = pc;
5933 arg = aexpr->bytes[pc++];
5934 arg = (arg << 8) + aexpr->bytes[pc++];
5935 aentry->goto_pc = arg;
5936 emit_lt_goto (&(aentry->from_offset), &(aentry->from_size));
5937 }
5938 else if (next_op == gdb_agent_op_log_not
5939 && !is_goto_target (aexpr, pc)
5940 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
5941 && !is_goto_target (aexpr, pc + 1))
5942 {
5943 trace_debug ("Combining less_signed & log_not & if_goto");
5944 pc += 2;
5945 aentry->pc = pc;
5946 arg = aexpr->bytes[pc++];
5947 arg = (arg << 8) + aexpr->bytes[pc++];
5948 aentry->goto_pc = arg;
5949 emit_ge_goto (&(aentry->from_offset), &(aentry->from_size));
5950 }
5951 else
5952 emit_less_signed ();
6a271cae
PA
5953 break;
5954
5955 case gdb_agent_op_less_unsigned:
5956 emit_less_unsigned ();
5957 break;
5958
5959 case gdb_agent_op_ext:
5960 arg = aexpr->bytes[pc++];
5961 if (arg < (sizeof (LONGEST) * 8))
5962 emit_ext (arg);
5963 break;
5964
5965 case gdb_agent_op_ref8:
5966 emit_ref (1);
5967 break;
5968
5969 case gdb_agent_op_ref16:
5970 emit_ref (2);
5971 break;
5972
5973 case gdb_agent_op_ref32:
5974 emit_ref (4);
5975 break;
5976
5977 case gdb_agent_op_ref64:
5978 emit_ref (8);
5979 break;
5980
5981 case gdb_agent_op_if_goto:
5982 arg = aexpr->bytes[pc++];
5983 arg = (arg << 8) + aexpr->bytes[pc++];
5984 aentry->goto_pc = arg;
5985 emit_if_goto (&(aentry->from_offset), &(aentry->from_size));
5986 break;
5987
5988 case gdb_agent_op_goto:
5989 arg = aexpr->bytes[pc++];
5990 arg = (arg << 8) + aexpr->bytes[pc++];
5991 aentry->goto_pc = arg;
5992 emit_goto (&(aentry->from_offset), &(aentry->from_size));
5993 break;
5994
5995 case gdb_agent_op_const8:
5996 emit_stack_flush ();
5997 top = aexpr->bytes[pc++];
5998 emit_const (top);
5999 break;
6000
6001 case gdb_agent_op_const16:
6002 emit_stack_flush ();
6003 top = aexpr->bytes[pc++];
6004 top = (top << 8) + aexpr->bytes[pc++];
6005 emit_const (top);
6006 break;
6007
6008 case gdb_agent_op_const32:
6009 emit_stack_flush ();
6010 top = aexpr->bytes[pc++];
6011 top = (top << 8) + aexpr->bytes[pc++];
6012 top = (top << 8) + aexpr->bytes[pc++];
6013 top = (top << 8) + aexpr->bytes[pc++];
6014 emit_const (top);
6015 break;
6016
6017 case gdb_agent_op_const64:
6018 emit_stack_flush ();
6019 top = aexpr->bytes[pc++];
6020 top = (top << 8) + aexpr->bytes[pc++];
6021 top = (top << 8) + aexpr->bytes[pc++];
6022 top = (top << 8) + aexpr->bytes[pc++];
6023 top = (top << 8) + aexpr->bytes[pc++];
6024 top = (top << 8) + aexpr->bytes[pc++];
6025 top = (top << 8) + aexpr->bytes[pc++];
6026 top = (top << 8) + aexpr->bytes[pc++];
6027 emit_const (top);
6028 break;
6029
6030 case gdb_agent_op_reg:
6031 emit_stack_flush ();
6032 arg = aexpr->bytes[pc++];
6033 arg = (arg << 8) + aexpr->bytes[pc++];
6034 emit_reg (arg);
6035 break;
6036
6037 case gdb_agent_op_end:
6038 trace_debug ("At end of expression\n");
6039
6040 /* Assume there is one stack element left, and that it is
6041 cached in "top" where emit_epilogue can get to it. */
6042 emit_stack_adjust (1);
6043
6044 done = 1;
6045 break;
6046
6047 case gdb_agent_op_dup:
6048 /* In our design, dup is equivalent to stack flushing. */
6049 emit_stack_flush ();
6050 break;
6051
6052 case gdb_agent_op_pop:
6053 emit_pop ();
6054 break;
6055
6056 case gdb_agent_op_zero_ext:
6057 arg = aexpr->bytes[pc++];
6058 if (arg < (sizeof (LONGEST) * 8))
6059 emit_zero_ext (arg);
6060 break;
6061
6062 case gdb_agent_op_swap:
6b9801d4
SS
6063 next_op = aexpr->bytes[pc];
6064 /* Detect greater-than comparison sequences. */
6065 if (next_op == gdb_agent_op_less_signed
6066 && !is_goto_target (aexpr, pc)
6067 && (aexpr->bytes[pc + 1] == gdb_agent_op_if_goto)
6068 && !is_goto_target (aexpr, pc + 1))
6069 {
6070 trace_debug ("Combining swap & less_signed & if_goto");
6071 pc += 2;
6072 aentry->pc = pc;
6073 arg = aexpr->bytes[pc++];
6074 arg = (arg << 8) + aexpr->bytes[pc++];
6075 aentry->goto_pc = arg;
6076 emit_gt_goto (&(aentry->from_offset), &(aentry->from_size));
6077 }
6078 else if (next_op == gdb_agent_op_less_signed
6079 && !is_goto_target (aexpr, pc)
6080 && (aexpr->bytes[pc + 1] == gdb_agent_op_log_not)
6081 && !is_goto_target (aexpr, pc + 1)
6082 && (aexpr->bytes[pc + 2] == gdb_agent_op_if_goto)
6083 && !is_goto_target (aexpr, pc + 2))
6084 {
6085 trace_debug ("Combining swap & less_signed & log_not & if_goto");
6086 pc += 3;
6087 aentry->pc = pc;
6088 arg = aexpr->bytes[pc++];
6089 arg = (arg << 8) + aexpr->bytes[pc++];
6090 aentry->goto_pc = arg;
6091 emit_le_goto (&(aentry->from_offset), &(aentry->from_size));
6092 }
6093 else
6094 emit_swap ();
6a271cae
PA
6095 break;
6096
6097 case gdb_agent_op_getv:
6098 emit_stack_flush ();
6099 arg = aexpr->bytes[pc++];
6100 arg = (arg << 8) + aexpr->bytes[pc++];
6101 emit_int_call_1 (ipa_sym_addrs.addr_get_trace_state_variable_value,
6102 arg);
6103 break;
6104
6105 case gdb_agent_op_setv:
6106 arg = aexpr->bytes[pc++];
6107 arg = (arg << 8) + aexpr->bytes[pc++];
6108 emit_void_call_2 (ipa_sym_addrs.addr_set_trace_state_variable_value,
6109 arg);
6110 break;
6111
6112 case gdb_agent_op_tracev:
6113 UNHANDLED;
6114 break;
6115
6116 /* GDB never (currently) generates any of these ops. */
6117 case gdb_agent_op_float:
6118 case gdb_agent_op_ref_float:
6119 case gdb_agent_op_ref_double:
6120 case gdb_agent_op_ref_long_double:
6121 case gdb_agent_op_l_to_d:
6122 case gdb_agent_op_d_to_l:
6123 case gdb_agent_op_trace16:
6124 UNHANDLED;
6125 break;
6126
6127 default:
6128 trace_debug ("Agent expression op 0x%x not recognized\n", op);
6129 /* Don't struggle on, things will just get worse. */
6130 return expr_eval_unrecognized_opcode;
6131 }
6132
6133 /* This catches errors that occur in target-specific code
6134 emission. */
6135 if (emit_error)
6136 {
6137 trace_debug ("Error %d while emitting code for %s\n",
94d5e490 6138 emit_error, gdb_agent_op_name (op));
6a271cae
PA
6139 return expr_eval_unhandled_opcode;
6140 }
6141
94d5e490 6142 trace_debug ("Op %s compiled\n", gdb_agent_op_name (op));
6a271cae
PA
6143 }
6144
6145 /* Now fill in real addresses as goto destinations. */
6146 for (aentry = bytecode_address_table; aentry; aentry = aentry->next)
6147 {
6148 int written = 0;
6149
6150 if (aentry->goto_pc < 0)
6151 continue;
6152
6153 /* Find the location that we are going to, and call back into
6154 target-specific code to write the actual address or
6155 displacement. */
6156 for (aentry2 = bytecode_address_table; aentry2; aentry2 = aentry2->next)
6157 {
6158 if (aentry2->pc == aentry->goto_pc)
6159 {
6160 trace_debug ("Want to jump from %s to %s\n",
6161 paddress (aentry->address),
6162 paddress (aentry2->address));
6163 write_goto_address (aentry->address + aentry->from_offset,
6164 aentry2->address, aentry->from_size);
6165 written = 1;
6166 break;
6167 }
6168 }
6169
6170 /* Error out if we didn't find a destination. */
6171 if (!written)
6172 {
6173 trace_debug ("Destination of goto %d not found\n",
6174 aentry->goto_pc);
6175 return expr_eval_invalid_goto;
6176 }
6177 }
6178
6179 return expr_eval_no_error;
6180}
6181
fa593d66
PA
6182/* We'll need to adjust these when we consider bi-arch setups, and big
6183 endian machines. */
6184
6185static int
6186write_inferior_data_ptr (CORE_ADDR where, CORE_ADDR ptr)
6187{
6188 return write_inferior_memory (where,
6189 (unsigned char *) &ptr, sizeof (void *));
6190}
6191
6192/* The base pointer of the IPA's heap. This is the only memory the
6193 IPA is allowed to use. The IPA should _not_ call the inferior's
6194 `malloc' during operation. That'd be slow, and, most importantly,
6195 it may not be safe. We may be collecting a tracepoint in a signal
6196 handler, for example. */
6197static CORE_ADDR target_tp_heap;
6198
6199/* Allocate at least SIZE bytes of memory from the IPA heap, aligned
6200 to 8 bytes. */
6201
6202static CORE_ADDR
6203target_malloc (ULONGEST size)
6204{
6205 CORE_ADDR ptr;
6206
6207 if (target_tp_heap == 0)
6208 {
6209 /* We have the pointer *address*, need what it points to. */
6210 if (read_inferior_data_pointer (ipa_sym_addrs.addr_gdb_tp_heap_buffer,
6211 &target_tp_heap))
6212 fatal ("could get target heap head pointer");
6213 }
6214
6215 ptr = target_tp_heap;
6216 target_tp_heap += size;
6217
6218 /* Pad to 8-byte alignment. */
6219 target_tp_heap = ((target_tp_heap + 7) & ~0x7);
6220
6221 return ptr;
6222}
6223
6224static CORE_ADDR
6225download_agent_expr (struct agent_expr *expr)
6226{
6227 CORE_ADDR expr_addr;
6228 CORE_ADDR expr_bytes;
6229
6230 expr_addr = target_malloc (sizeof (*expr));
6231 write_inferior_memory (expr_addr, (unsigned char *) expr, sizeof (*expr));
6232
6233 expr_bytes = target_malloc (expr->length);
6234 write_inferior_data_ptr (expr_addr + offsetof (struct agent_expr, bytes),
6235 expr_bytes);
6236 write_inferior_memory (expr_bytes, expr->bytes, expr->length);
6237
6238 return expr_addr;
6239}
6240
6241/* Align V up to N bits. */
6242#define UALIGN(V, N) (((V) + ((N) - 1)) & ~((N) - 1))
6243
6244static void
6245download_tracepoints (void)
6246{
6247 CORE_ADDR tpptr = 0, prev_tpptr = 0;
6248 struct tracepoint *tpoint;
6249
6250 /* Start out empty. */
6251 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, 0);
6252
6253 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6254 {
6255 struct tracepoint target_tracepoint;
6256
0fb4aa4b
PA
6257 if (tpoint->type != fast_tracepoint
6258 && tpoint->type != static_tracepoint)
fa593d66
PA
6259 continue;
6260
6a271cae
PA
6261 /* Maybe download a compiled condition. */
6262 if (tpoint->cond != NULL && target_emit_ops () != NULL)
6263 {
6264 CORE_ADDR jentry, jump_entry;
6265
6266 jentry = jump_entry = get_jump_space_head ();
6267
6268 if (tpoint->cond != NULL)
6269 {
6270 /* Pad to 8-byte alignment. (needed?) */
6271 /* Actually this should be left for the target to
6272 decide. */
6273 jentry = UALIGN (jentry, 8);
6274
6275 compile_tracepoint_condition (tpoint, &jentry);
6276 }
6277
6278 /* Pad to 8-byte alignment. */
6279 jentry = UALIGN (jentry, 8);
6280 claim_jump_space (jentry - jump_entry);
6281 }
6282
fa593d66
PA
6283 target_tracepoint = *tpoint;
6284
6285 prev_tpptr = tpptr;
6286 tpptr = target_malloc (sizeof (*tpoint));
6287 tpoint->obj_addr_on_target = tpptr;
6288
6289 if (tpoint == tracepoints)
6290 {
6291 /* First object in list, set the head pointer in the
6292 inferior. */
6293 write_inferior_data_ptr (ipa_sym_addrs.addr_tracepoints, tpptr);
6294 }
6295 else
6296 {
6297 write_inferior_data_ptr (prev_tpptr + offsetof (struct tracepoint,
6298 next),
6299 tpptr);
6300 }
6301
6302 /* Write the whole object. We'll fix up its pointers in a bit.
6303 Assume no next for now. This is fixed up above on the next
6304 iteration, if there's any. */
6305 target_tracepoint.next = NULL;
6306 /* Need to clear this here too, since we're downloading the
6307 tracepoints before clearing our own copy. */
6308 target_tracepoint.hit_count = 0;
6309
6310 write_inferior_memory (tpptr, (unsigned char *) &target_tracepoint,
6311 sizeof (target_tracepoint));
6312
6313 if (tpoint->cond)
6314 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6315 cond),
6316 download_agent_expr (tpoint->cond));
6317
6318 if (tpoint->numactions)
6319 {
6320 int i;
6321 CORE_ADDR actions_array;
6322
6323 /* The pointers array. */
6324 actions_array
6325 = target_malloc (sizeof (*tpoint->actions) * tpoint->numactions);
6326 write_inferior_data_ptr (tpptr + offsetof (struct tracepoint,
6327 actions),
6328 actions_array);
6329
6330 /* Now for each pointer, download the action. */
6331 for (i = 0; i < tpoint->numactions; i++)
6332 {
6333 CORE_ADDR ipa_action = 0;
6334 struct tracepoint_action *action = tpoint->actions[i];
6335
6336 switch (action->type)
6337 {
6338 case 'M':
6339 ipa_action
6340 = target_malloc (sizeof (struct collect_memory_action));
6341 write_inferior_memory (ipa_action,
6342 (unsigned char *) action,
6343 sizeof (struct collect_memory_action));
6344 break;
6345 case 'R':
6346 ipa_action
6347 = target_malloc (sizeof (struct collect_registers_action));
6348 write_inferior_memory (ipa_action,
6349 (unsigned char *) action,
6350 sizeof (struct collect_registers_action));
6351 break;
6352 case 'X':
6353 {
6354 CORE_ADDR expr;
6355 struct eval_expr_action *eaction
6356 = (struct eval_expr_action *) action;
6357
6358 ipa_action = target_malloc (sizeof (*eaction));
6359 write_inferior_memory (ipa_action,
6360 (unsigned char *) eaction,
6361 sizeof (*eaction));
6362
6363 expr = download_agent_expr (eaction->expr);
6364 write_inferior_data_ptr
6365 (ipa_action + offsetof (struct eval_expr_action, expr),
6366 expr);
6367 break;
6368 }
0fb4aa4b
PA
6369 case 'L':
6370 ipa_action = target_malloc
6371 (sizeof (struct collect_static_trace_data_action));
6372 write_inferior_memory
6373 (ipa_action,
6374 (unsigned char *) action,
6375 sizeof (struct collect_static_trace_data_action));
6376 break;
fa593d66
PA
6377 default:
6378 trace_debug ("unknown trace action '%c', ignoring",
6379 action->type);
6380 break;
6381 }
6382
6383 if (ipa_action != 0)
6384 write_inferior_data_ptr
6385 (actions_array + i * sizeof (sizeof (*tpoint->actions)),
6386 ipa_action);
6387 }
6388 }
6389 }
6390}
6391
6392static void
6393download_trace_state_variables (void)
6394{
6395 CORE_ADDR ptr = 0, prev_ptr = 0;
6396 struct trace_state_variable *tsv;
6397
6398 /* Start out empty. */
6399 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables, 0);
6400
6401 for (tsv = trace_state_variables; tsv != NULL; tsv = tsv->next)
6402 {
6403 struct trace_state_variable target_tsv;
6404
6405 /* TSV's with a getter have been initialized equally in both the
6406 inferior and GDBserver. Skip them. */
6407 if (tsv->getter != NULL)
6408 continue;
6409
6410 target_tsv = *tsv;
6411
6412 prev_ptr = ptr;
6413 ptr = target_malloc (sizeof (*tsv));
6414
6415 if (tsv == trace_state_variables)
6416 {
6417 /* First object in list, set the head pointer in the
6418 inferior. */
6419
6420 write_inferior_data_ptr (ipa_sym_addrs.addr_trace_state_variables,
6421 ptr);
6422 }
6423 else
6424 {
6425 write_inferior_data_ptr (prev_ptr
6426 + offsetof (struct trace_state_variable,
6427 next),
6428 ptr);
6429 }
6430
6431 /* Write the whole object. We'll fix up its pointers in a bit.
6432 Assume no next, fixup when needed. */
6433 target_tsv.next = NULL;
6434
6435 write_inferior_memory (ptr, (unsigned char *) &target_tsv,
6436 sizeof (target_tsv));
6437
6438 if (tsv->name != NULL)
6439 {
6440 size_t size = strlen (tsv->name) + 1;
6441 CORE_ADDR name_addr = target_malloc (size);
6442 write_inferior_memory (name_addr,
6443 (unsigned char *) tsv->name, size);
6444 write_inferior_data_ptr (ptr
6445 + offsetof (struct trace_state_variable,
6446 name),
6447 name_addr);
6448 }
6449
6450 if (tsv->getter != NULL)
6451 {
6452 fatal ("what to do with these?");
6453 }
6454 }
6455
6456 if (prev_ptr != 0)
6457 {
6458 /* Fixup the next pointer in the last item in the list. */
493e2a69
MS
6459 write_inferior_data_ptr (prev_ptr
6460 + offsetof (struct trace_state_variable,
6461 next), 0);
fa593d66
PA
6462 }
6463}
6464
6465/* Upload complete trace frames out of the IP Agent's trace buffer
6466 into GDBserver's trace buffer. This always uploads either all or
6467 no trace frames. This is the counter part of
6468 `trace_alloc_trace_buffer'. See its description of the atomic
6469 synching mechanism. */
6470
6471static void
6472upload_fast_traceframes (void)
6473{
6474 unsigned int ipa_traceframe_read_count, ipa_traceframe_write_count;
6475 unsigned int ipa_traceframe_read_count_racy, ipa_traceframe_write_count_racy;
6476 CORE_ADDR tf;
6477 struct ipa_trace_buffer_control ipa_trace_buffer_ctrl;
6478 unsigned int curr_tbctrl_idx;
6479 unsigned int ipa_trace_buffer_ctrl_curr;
6480 unsigned int ipa_trace_buffer_ctrl_curr_old;
6481 CORE_ADDR ipa_trace_buffer_ctrl_addr;
6482 struct breakpoint *about_to_request_buffer_space_bkpt;
6483 CORE_ADDR ipa_trace_buffer_lo;
6484 CORE_ADDR ipa_trace_buffer_hi;
6485
6486 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6487 &ipa_traceframe_read_count_racy))
6488 {
6489 /* This will happen in most targets if the current thread is
6490 running. */
6491 return;
6492 }
6493
6494 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6495 &ipa_traceframe_write_count_racy))
6496 return;
6497
6498 trace_debug ("ipa_traceframe_count (racy area): %d (w=%d, r=%d)",
493e2a69
MS
6499 ipa_traceframe_write_count_racy
6500 - ipa_traceframe_read_count_racy,
6501 ipa_traceframe_write_count_racy,
6502 ipa_traceframe_read_count_racy);
fa593d66
PA
6503
6504 if (ipa_traceframe_write_count_racy == ipa_traceframe_read_count_racy)
6505 return;
6506
6507 about_to_request_buffer_space_bkpt
6508 = set_breakpoint_at (ipa_sym_addrs.addr_about_to_request_buffer_space,
6509 NULL);
6510
6511 if (read_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6512 &ipa_trace_buffer_ctrl_curr))
6513 return;
6514
6515 ipa_trace_buffer_ctrl_curr_old = ipa_trace_buffer_ctrl_curr;
6516
6517 curr_tbctrl_idx = ipa_trace_buffer_ctrl_curr & ~GDBSERVER_FLUSH_COUNT_MASK;
6518
6519 {
6520 unsigned int prev, counter;
6521
6522 /* Update the token, with new counters, and the GDBserver stamp
6523 bit. Alway reuse the current TBC index. */
6524 prev = ipa_trace_buffer_ctrl_curr & 0x0007ff00;
6525 counter = (prev + 0x100) & 0x0007ff00;
6526
6527 ipa_trace_buffer_ctrl_curr = (0x80000000
6528 | (prev << 12)
6529 | counter
6530 | curr_tbctrl_idx);
6531 }
6532
6533 if (write_inferior_uinteger (ipa_sym_addrs.addr_trace_buffer_ctrl_curr,
6534 ipa_trace_buffer_ctrl_curr))
6535 return;
6536
6537 trace_debug ("Lib: Committed %08x -> %08x",
6538 ipa_trace_buffer_ctrl_curr_old,
6539 ipa_trace_buffer_ctrl_curr);
6540
6541 /* Re-read these, now that we've installed the
6542 `about_to_request_buffer_space' breakpoint/lock. A thread could
6543 have finished a traceframe between the last read of these
6544 counters and setting the breakpoint above. If we start
6545 uploading, we never want to leave this function with
6546 traceframe_read_count != 0, otherwise, GDBserver could end up
6547 incrementing the counter tokens more than once (due to event loop
6548 nesting), which would break the IP agent's "effective" detection
6549 (see trace_alloc_trace_buffer). */
6550 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_read_count,
6551 &ipa_traceframe_read_count))
6552 return;
6553 if (read_inferior_uinteger (ipa_sym_addrs.addr_traceframe_write_count,
6554 &ipa_traceframe_write_count))
6555 return;
6556
6557 if (debug_threads)
6558 {
6559 trace_debug ("ipa_traceframe_count (blocked area): %d (w=%d, r=%d)",
6560 ipa_traceframe_write_count - ipa_traceframe_read_count,
6561 ipa_traceframe_write_count, ipa_traceframe_read_count);
6562
6563 if (ipa_traceframe_write_count != ipa_traceframe_write_count_racy
6564 || ipa_traceframe_read_count != ipa_traceframe_read_count_racy)
6565 trace_debug ("note that ipa_traceframe_count's parts changed");
6566 }
6567
6568 /* Get the address of the current TBC object (the IP agent has an
6569 array of 3 such objects). The index is stored in the TBC
6570 token. */
6571 ipa_trace_buffer_ctrl_addr = ipa_sym_addrs.addr_trace_buffer_ctrl;
6572 ipa_trace_buffer_ctrl_addr
6573 += sizeof (struct ipa_trace_buffer_control) * curr_tbctrl_idx;
6574
6575 if (read_inferior_memory (ipa_trace_buffer_ctrl_addr,
6576 (unsigned char *) &ipa_trace_buffer_ctrl,
6577 sizeof (struct ipa_trace_buffer_control)))
6578 return;
6579
6580 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_lo,
6581 &ipa_trace_buffer_lo))
6582 return;
6583 if (read_inferior_data_pointer (ipa_sym_addrs.addr_trace_buffer_hi,
6584 &ipa_trace_buffer_hi))
6585 return;
6586
6587 /* Offsets are easier to grok for debugging than raw addresses,
6588 especially for the small trace buffer sizes that are useful for
6589 testing. */
6590 trace_debug ("Lib: Trace buffer [%d] start=%d free=%d "
6591 "endfree=%d wrap=%d hi=%d",
6592 curr_tbctrl_idx,
6593 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6594 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
6595 (int) (ipa_trace_buffer_ctrl.end_free - ipa_trace_buffer_lo),
6596 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6597 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6598
6599 /* Note that the IPA's buffer is always circular. */
6600
6601#define IPA_FIRST_TRACEFRAME() (ipa_trace_buffer_ctrl.start)
6602
6603#define IPA_NEXT_TRACEFRAME_1(TF, TFOBJ) \
6604 ((TF) + sizeof (struct traceframe) + (TFOBJ)->data_size)
6605
6606#define IPA_NEXT_TRACEFRAME(TF, TFOBJ) \
6607 (IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) \
6608 - ((IPA_NEXT_TRACEFRAME_1 (TF, TFOBJ) >= ipa_trace_buffer_ctrl.wrap) \
6609 ? (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo) \
6610 : 0))
6611
6612 tf = IPA_FIRST_TRACEFRAME ();
6613
6614 while (ipa_traceframe_write_count - ipa_traceframe_read_count)
6615 {
6616 struct tracepoint *tpoint;
6617 struct traceframe *tframe;
6618 unsigned char *block;
6619 struct traceframe ipa_tframe;
6620
6621 if (read_inferior_memory (tf, (unsigned char *) &ipa_tframe,
6622 offsetof (struct traceframe, data)))
6623 error ("Uploading: couldn't read traceframe at %s\n", paddress (tf));
6624
6625 if (ipa_tframe.tpnum == 0)
6626 fatal ("Uploading: No (more) fast traceframes, but "
6627 "ipa_traceframe_count == %u??\n",
6628 ipa_traceframe_write_count - ipa_traceframe_read_count);
6629
6630 /* Note that this will be incorrect for multi-location
6631 tracepoints... */
6632 tpoint = find_next_tracepoint_by_number (NULL, ipa_tframe.tpnum);
6633
6634 tframe = add_traceframe (tpoint);
6635 if (tframe == NULL)
6636 {
6637 trace_buffer_is_full = 1;
6638 trace_debug ("Uploading: trace buffer is full");
6639 }
6640 else
6641 {
6642 /* Copy the whole set of blocks in one go for now. FIXME:
6643 split this in smaller blocks. */
6644 block = add_traceframe_block (tframe, ipa_tframe.data_size);
6645 if (block != NULL)
6646 {
493e2a69
MS
6647 if (read_inferior_memory (tf
6648 + offsetof (struct traceframe, data),
fa593d66
PA
6649 block, ipa_tframe.data_size))
6650 error ("Uploading: Couldn't read traceframe data at %s\n",
6651 paddress (tf + offsetof (struct traceframe, data)));
6652 }
6653
6654 trace_debug ("Uploading: traceframe didn't fit");
6655 finish_traceframe (tframe);
6656 }
6657
6658 tf = IPA_NEXT_TRACEFRAME (tf, &ipa_tframe);
6659
6660 /* If we freed the traceframe that wrapped around, go back
6661 to the non-wrap case. */
6662 if (tf < ipa_trace_buffer_ctrl.start)
6663 {
6664 trace_debug ("Lib: Discarding past the wraparound");
6665 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6666 }
6667 ipa_trace_buffer_ctrl.start = tf;
6668 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_ctrl.start;
6669 ++ipa_traceframe_read_count;
6670
6671 if (ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.free
6672 && ipa_trace_buffer_ctrl.start == ipa_trace_buffer_ctrl.end_free)
6673 {
6674 trace_debug ("Lib: buffer is fully empty. "
6675 "Trace buffer [%d] start=%d free=%d endfree=%d",
6676 curr_tbctrl_idx,
6677 (int) (ipa_trace_buffer_ctrl.start
6678 - ipa_trace_buffer_lo),
6679 (int) (ipa_trace_buffer_ctrl.free
6680 - ipa_trace_buffer_lo),
6681 (int) (ipa_trace_buffer_ctrl.end_free
6682 - ipa_trace_buffer_lo));
6683
6684 ipa_trace_buffer_ctrl.start = ipa_trace_buffer_lo;
6685 ipa_trace_buffer_ctrl.free = ipa_trace_buffer_lo;
6686 ipa_trace_buffer_ctrl.end_free = ipa_trace_buffer_hi;
6687 ipa_trace_buffer_ctrl.wrap = ipa_trace_buffer_hi;
6688 }
6689
6690 trace_debug ("Uploaded a traceframe\n"
6691 "Lib: Trace buffer [%d] start=%d free=%d "
6692 "endfree=%d wrap=%d hi=%d",
6693 curr_tbctrl_idx,
6694 (int) (ipa_trace_buffer_ctrl.start - ipa_trace_buffer_lo),
6695 (int) (ipa_trace_buffer_ctrl.free - ipa_trace_buffer_lo),
493e2a69
MS
6696 (int) (ipa_trace_buffer_ctrl.end_free
6697 - ipa_trace_buffer_lo),
fa593d66
PA
6698 (int) (ipa_trace_buffer_ctrl.wrap - ipa_trace_buffer_lo),
6699 (int) (ipa_trace_buffer_hi - ipa_trace_buffer_lo));
6700 }
6701
6702 if (write_inferior_memory (ipa_trace_buffer_ctrl_addr,
6703 (unsigned char *) &ipa_trace_buffer_ctrl,
6704 sizeof (struct ipa_trace_buffer_control)))
6705 return;
6706
6707 write_inferior_integer (ipa_sym_addrs.addr_traceframe_read_count,
6708 ipa_traceframe_read_count);
6709
6710 trace_debug ("Done uploading traceframes [%d]\n", curr_tbctrl_idx);
6711
6712 pause_all (1);
6713 cancel_breakpoints ();
6714
6715 delete_breakpoint (about_to_request_buffer_space_bkpt);
6716 about_to_request_buffer_space_bkpt = NULL;
6717
6718 unpause_all (1);
6719
6720 if (trace_buffer_is_full)
6721 stop_tracing ();
6722}
6723#endif
6724
6725#ifdef IN_PROCESS_AGENT
6726
0fb4aa4b
PA
6727IP_AGENT_EXPORT int ust_loaded;
6728IP_AGENT_EXPORT char cmd_buf[CMD_BUF_SIZE];
fa593d66 6729
0fb4aa4b 6730#ifdef HAVE_UST
fa593d66 6731
0fb4aa4b
PA
6732/* Static tracepoints. */
6733
6734/* UST puts a "struct tracepoint" in the global namespace, which
6735 conflicts with our tracepoint. Arguably, being a library, it
6736 shouldn't take ownership of such a generic name. We work around it
6737 here. */
6738#define tracepoint ust_tracepoint
6739#include <ust/ust.h>
6740#undef tracepoint
6741
6742extern int serialize_to_text (char *outbuf, int bufsize,
6743 const char *fmt, va_list ap);
6744
6745#define GDB_PROBE_NAME "gdb"
6746
6747/* We dynamically search for the UST symbols instead of linking them
6748 in. This lets the user decide if the application uses static
6749 tracepoints, instead of always pulling libust.so in. This vector
6750 holds pointers to all functions we care about. */
6751
6752static struct
fa593d66 6753{
0fb4aa4b
PA
6754 int (*serialize_to_text) (char *outbuf, int bufsize,
6755 const char *fmt, va_list ap);
6756
6757 int (*ltt_probe_register) (struct ltt_available_probe *pdata);
6758 int (*ltt_probe_unregister) (struct ltt_available_probe *pdata);
6759
6760 int (*ltt_marker_connect) (const char *channel, const char *mname,
6761 const char *pname);
6762 int (*ltt_marker_disconnect) (const char *channel, const char *mname,
6763 const char *pname);
6764
6765 void (*marker_iter_start) (struct marker_iter *iter);
6766 void (*marker_iter_next) (struct marker_iter *iter);
6767 void (*marker_iter_stop) (struct marker_iter *iter);
6768 void (*marker_iter_reset) (struct marker_iter *iter);
6769} ust_ops;
6770
6771#include <dlfcn.h>
6772
6773/* Cast through typeof to catch incompatible API changes. Since UST
6774 only builds with gcc, we can freely use gcc extensions here
6775 too. */
6776#define GET_UST_SYM(SYM) \
6777 do \
6778 { \
6779 if (ust_ops.SYM == NULL) \
6780 ust_ops.SYM = (typeof (&SYM)) dlsym (RTLD_DEFAULT, #SYM); \
6781 if (ust_ops.SYM == NULL) \
6782 return 0; \
6783 } while (0)
6784
6785#define USTF(SYM) ust_ops.SYM
6786
6787/* Get pointers to all libust.so functions we care about. */
6788
6789static int
6790dlsym_ust (void)
6791{
6792 GET_UST_SYM (serialize_to_text);
6793
6794 GET_UST_SYM (ltt_probe_register);
6795 GET_UST_SYM (ltt_probe_unregister);
6796 GET_UST_SYM (ltt_marker_connect);
6797 GET_UST_SYM (ltt_marker_disconnect);
6798
6799 GET_UST_SYM (marker_iter_start);
6800 GET_UST_SYM (marker_iter_next);
6801 GET_UST_SYM (marker_iter_stop);
6802 GET_UST_SYM (marker_iter_reset);
6803
6804 ust_loaded = 1;
6805 return 1;
fa593d66
PA
6806}
6807
0fb4aa4b
PA
6808/* Given an UST marker, return the matching gdb static tracepoint.
6809 The match is done by address. */
6810
6811static struct tracepoint *
6812ust_marker_to_static_tracepoint (const struct marker *mdata)
6813{
6814 struct tracepoint *tpoint;
6815
6816 for (tpoint = tracepoints; tpoint; tpoint = tpoint->next)
6817 {
d248b706 6818 if (tpoint->type != static_tracepoint)
0fb4aa4b
PA
6819 continue;
6820
6821 if (tpoint->address == (uintptr_t) mdata->location)
6822 return tpoint;
6823 }
6824
6825 return NULL;
6826}
6827
6828/* The probe function we install on lttng/ust markers. Whenever a
6829 probed ust marker is hit, this function is called. This is similar
6830 to gdb_collect, only for static tracepoints, instead of fast
6831 tracepoints. */
6832
6833static void
6834gdb_probe (const struct marker *mdata, void *probe_private,
6835 struct registers *regs, void *call_private,
6836 const char *fmt, va_list *args)
6837{
6838 struct tracepoint *tpoint;
6839 struct static_tracepoint_ctx ctx;
6840
6841 /* Don't do anything until the trace run is completely set up. */
6842 if (!tracing)
6843 {
6844 trace_debug ("gdb_probe: not tracing\n");
6845 return;
6846 }
6847
6848 ctx.base.type = static_tracepoint;
6849 ctx.regcache_initted = 0;
6850 ctx.regs = regs;
6851 ctx.fmt = fmt;
6852 ctx.args = args;
6853
6854 /* Wrap the regblock in a register cache (in the stack, we don't
6855 want to malloc here). */
6856 ctx.regspace = alloca (register_cache_size ());
6857 if (ctx.regspace == NULL)
6858 {
6859 trace_debug ("Trace buffer block allocation failed, skipping");
6860 return;
6861 }
6862
6863 tpoint = ust_marker_to_static_tracepoint (mdata);
6864 if (tpoint == NULL)
6865 {
6866 trace_debug ("gdb_probe: marker not known: "
6867 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6868 mdata->location, mdata->channel,
6869 mdata->name, mdata->format);
6870 return;
6871 }
6872
d248b706
KY
6873 if (!tpoint->enabled)
6874 {
6875 trace_debug ("gdb_probe: tracepoint disabled");
6876 return;
6877 }
6878
0fb4aa4b
PA
6879 ctx.tpoint = tpoint;
6880
6881 trace_debug ("gdb_probe: collecting marker: "
6882 "loc:0x%p, ch:\"%s\",n:\"%s\",f:\"%s\"",
6883 mdata->location, mdata->channel,
6884 mdata->name, mdata->format);
6885
6886 /* Test the condition if present, and collect if true. */
6887 if (tpoint->cond == NULL
6888 || condition_true_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6889 tpoint))
6890 {
6891 collect_data_at_tracepoint ((struct tracepoint_hit_ctx *) &ctx,
6892 tpoint->address, tpoint);
6893
6894 if (stopping_tracepoint
6895 || trace_buffer_is_full
6896 || expr_eval_result != expr_eval_no_error)
6897 stop_tracing ();
6898 }
6899 else
6900 {
6901 /* If there was a condition and it evaluated to false, the only
6902 way we would stop tracing is if there was an error during
6903 condition expression evaluation. */
6904 if (expr_eval_result != expr_eval_no_error)
6905 stop_tracing ();
6906 }
6907}
6908
6909/* Called if the gdb static tracepoint requested collecting "$_sdata",
6910 static tracepoint string data. This is a string passed to the
6911 tracing library by the user, at the time of the tracepoint marker
6912 call. E.g., in the UST marker call:
6913
6914 trace_mark (ust, bar33, "str %s", "FOOBAZ");
6915
6916 the collected data is "str FOOBAZ".
6917*/
6918
6919static void
6920collect_ust_data_at_tracepoint (struct tracepoint_hit_ctx *ctx,
6921 CORE_ADDR stop_pc,
6922 struct tracepoint *tpoint,
6923 struct traceframe *tframe)
6924{
6925 struct static_tracepoint_ctx *umd = (struct static_tracepoint_ctx *) ctx;
6926 unsigned char *bufspace;
6927 int size;
6928 va_list copy;
6929 unsigned short blocklen;
6930
6931 if (umd == NULL)
6932 {
6933 trace_debug ("Wanted to collect static trace data, "
6934 "but there's no static trace data");
6935 return;
6936 }
6937
6938 va_copy (copy, *umd->args);
6939 size = USTF(serialize_to_text) (NULL, 0, umd->fmt, copy);
6940 va_end (copy);
6941
6942 trace_debug ("Want to collect ust data");
6943
6944 /* 'S' + size + string */
6945 bufspace = add_traceframe_block (tframe,
6946 1 + sizeof (blocklen) + size + 1);
6947 if (bufspace == NULL)
6948 {
6949 trace_debug ("Trace buffer block allocation failed, skipping");
6950 return;
6951 }
6952
6953 /* Identify a static trace data block. */
6954 *bufspace = 'S';
6955
6956 blocklen = size + 1;
6957 memcpy (bufspace + 1, &blocklen, sizeof (blocklen));
6958
6959 va_copy (copy, *umd->args);
6960 USTF(serialize_to_text) ((char *) bufspace + 1 + sizeof (blocklen),
6961 size + 1, umd->fmt, copy);
6962 va_end (copy);
6963
6964 trace_debug ("Storing static tracepoint data in regblock: %s",
6965 bufspace + 1 + sizeof (blocklen));
6966}
6967
6968/* The probe to register with lttng/ust. */
6969static struct ltt_available_probe gdb_ust_probe =
6970 {
6971 GDB_PROBE_NAME,
6972 NULL,
6973 gdb_probe,
6974 };
6975
6976#endif /* HAVE_UST */
6977#endif /* IN_PROCESS_AGENT */
6978
6979#ifdef HAVE_UST
6980
6981#include <sys/socket.h>
6982#include <sys/un.h>
6983
6984#ifndef UNIX_PATH_MAX
6985#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
6986#endif
6987
6988/* Where we put the socked used for synchronization. */
6989#define SOCK_DIR P_tmpdir
6990
6991#endif /* HAVE_UST */
6992
6993#ifndef IN_PROCESS_AGENT
6994
6995#ifdef HAVE_UST
6996
6997static int
6998gdb_ust_connect_sync_socket (int pid)
6999{
7000 struct sockaddr_un addr;
7001 int res, fd;
7002 char path[UNIX_PATH_MAX];
7003
6cebaf6e 7004 res = xsnprintf (path, UNIX_PATH_MAX, "%s/gdb_ust%d", SOCK_DIR, pid);
0fb4aa4b
PA
7005 if (res >= UNIX_PATH_MAX)
7006 {
7007 trace_debug ("string overflow allocating socket name");
7008 return -1;
7009 }
7010
7011 res = fd = socket (PF_UNIX, SOCK_STREAM, 0);
7012 if (res == -1)
7013 {
7014 warning ("error opening sync socket: %s\n", strerror (errno));
7015 return -1;
7016 }
7017
7018 addr.sun_family = AF_UNIX;
7019
6cebaf6e 7020 res = xsnprintf (addr.sun_path, UNIX_PATH_MAX, "%s", path);
0fb4aa4b
PA
7021 if (res >= UNIX_PATH_MAX)
7022 {
7023 warning ("string overflow allocating socket name\n");
7024 close (fd);
7025 return -1;
7026 }
7027
7028 res = connect (fd, (struct sockaddr *) &addr, sizeof (addr));
7029 if (res == -1)
7030 {
7031 warning ("error connecting sync socket (%s): %s. "
7032 "Make sure the directory exists and that it is writable.",
7033 path, strerror (errno));
7034 close (fd);
7035 return -1;
7036 }
7037
7038 return fd;
7039}
7040
7041/* Resume thread PTID. */
7042
7043static void
7044resume_thread (ptid_t ptid)
7045{
7046 struct thread_resume resume_info;
7047
7048 resume_info.thread = ptid;
7049 resume_info.kind = resume_continue;
7050 resume_info.sig = TARGET_SIGNAL_0;
7051 (*the_target->resume) (&resume_info, 1);
7052}
7053
7054/* Stop thread PTID. */
7055
7056static void
7057stop_thread (ptid_t ptid)
7058{
7059 struct thread_resume resume_info;
7060
7061 resume_info.thread = ptid;
7062 resume_info.kind = resume_stop;
7063 resume_info.sig = TARGET_SIGNAL_0;
7064 (*the_target->resume) (&resume_info, 1);
7065}
7066
7067/* Ask the in-process agent to run a command. Since we don't want to
7068 have to handle the IPA hitting breakpoints while running the
7069 command, we pause all threads, remove all breakpoints, and then set
7070 the helper thread re-running. We communicate with the helper
7071 thread by means of direct memory xfering, and a socket for
7072 synchronization. */
7073
7074static int
7075run_inferior_command (char *cmd)
7076{
7077 int err = -1;
7078 int fd = -1;
7079 int pid = ptid_get_pid (current_inferior->entry.id);
7080 int tid;
7081 ptid_t ptid = null_ptid;
7082
7083 trace_debug ("run_inferior_command: running: %s", cmd);
7084
7085 pause_all (0);
7086 uninsert_all_breakpoints ();
7087
7088 if (read_inferior_integer (ipa_sym_addrs.addr_helper_thread_id, &tid))
7089 {
7090 warning ("Error reading helper thread's id in lib");
7091 goto out;
7092 }
7093
7094 if (tid == 0)
7095 {
7096 warning ("helper thread not initialized yet");
7097 goto out;
7098 }
7099
7100 if (write_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
7101 (unsigned char *) cmd, strlen (cmd) + 1))
7102 {
7103 warning ("Error writing command");
7104 goto out;
7105 }
7106
7107 ptid = ptid_build (pid, tid, 0);
7108
7109 resume_thread (ptid);
7110
7111 fd = gdb_ust_connect_sync_socket (pid);
7112 if (fd >= 0)
7113 {
7114 char buf[1] = "";
7115 int ret;
7116
7117 trace_debug ("signalling helper thread");
7118
7119 do
7120 {
7121 ret = write (fd, buf, 1);
7122 } while (ret == -1 && errno == EINTR);
7123
7124 trace_debug ("waiting for helper thread's response");
7125
7126 do
7127 {
7128 ret = read (fd, buf, 1);
7129 } while (ret == -1 && errno == EINTR);
7130
7131 close (fd);
7132
7133 trace_debug ("helper thread's response received");
7134 }
7135
7136 out:
7137
7138 /* Need to read response with the inferior stopped. */
7139 if (!ptid_equal (ptid, null_ptid))
7140 {
7141 int was_non_stop = non_stop;
7142 struct target_waitstatus status;
7143
7144 stop_thread (ptid);
7145 non_stop = 1;
7146 mywait (ptid, &status, 0, 0);
7147 non_stop = was_non_stop;
7148 }
7149
7150 if (fd >= 0)
7151 {
7152 if (read_inferior_memory (ipa_sym_addrs.addr_cmd_buf,
7153 (unsigned char *) cmd, CMD_BUF_SIZE))
7154 {
7155 warning ("Error reading command response");
7156 }
7157 else
7158 {
7159 err = 0;
7160 trace_debug ("run_inferior_command: response: %s", cmd);
7161 }
7162 }
7163
7164 reinsert_all_breakpoints ();
7165 unpause_all (0);
7166
7167 return err;
7168}
7169
7170#else /* HAVE_UST */
7171
7172static int
7173run_inferior_command (char *cmd)
7174{
7175 return -1;
7176}
7177
7178#endif /* HAVE_UST */
7179
7180#else /* !IN_PROCESS_AGENT */
7181
7182/* Thread ID of the helper thread. GDBserver reads this to know which
7183 is the help thread. This is an LWP id on Linux. */
7184int helper_thread_id;
7185
7186#ifdef HAVE_UST
7187
7188static int
7189init_named_socket (const char *name)
7190{
7191 int result, fd;
7192 struct sockaddr_un addr;
7193
7194 result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
7195 if (result == -1)
7196 {
7197 warning ("socket creation failed: %s", strerror (errno));
7198 return -1;
7199 }
7200
7201 addr.sun_family = AF_UNIX;
7202
7203 strncpy (addr.sun_path, name, UNIX_PATH_MAX);
7204 addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
7205
7206 result = access (name, F_OK);
7207 if (result == 0)
7208 {
7209 /* File exists. */
7210 result = unlink (name);
7211 if (result == -1)
7212 {
7213 warning ("unlink failed: %s", strerror (errno));
7214 close (fd);
7215 return -1;
7216 }
7217 warning ("socket %s already exists; overwriting", name);
7218 }
7219
7220 result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
7221 if (result == -1)
7222 {
7223 warning ("bind failed: %s", strerror (errno));
7224 close (fd);
7225 return -1;
7226 }
7227
7228 result = listen (fd, 1);
7229 if (result == -1)
7230 {
7231 warning ("listen: %s", strerror (errno));
7232 close (fd);
7233 return -1;
7234 }
7235
7236 return fd;
7237}
7238
7239static int
7240gdb_ust_socket_init (void)
7241{
7242 int result, fd;
7243 char name[UNIX_PATH_MAX];
7244
6cebaf6e 7245 result = xsnprintf (name, UNIX_PATH_MAX, "%s/gdb_ust%d",
7246 SOCK_DIR, getpid ());
0fb4aa4b
PA
7247 if (result >= UNIX_PATH_MAX)
7248 {
7249 trace_debug ("string overflow allocating socket name");
7250 return -1;
7251 }
7252
7253 fd = init_named_socket (name);
7254 if (fd < 0)
7255 warning ("Error initializing named socket (%s) for communication with the "
7256 "ust helper thread. Check that directory exists and that it "
7257 "is writable.", name);
7258
7259 return fd;
7260}
7261
7262/* Return an hexstr version of the STR C string, fit for sending to
7263 GDB. */
7264
7265static char *
7266cstr_to_hexstr (const char *str)
7267{
7268 int len = strlen (str);
7269 char *hexstr = xmalloc (len * 2 + 1);
7270 convert_int_to_ascii ((gdb_byte *) str, hexstr, len);
7271 return hexstr;
7272}
7273
7274/* The next marker to be returned on a qTsSTM command. */
7275static const struct marker *next_st;
7276
7277/* Returns the first known marker. */
7278
7279struct marker *
7280first_marker (void)
7281{
7282 struct marker_iter iter;
7283
7284 USTF(marker_iter_reset) (&iter);
7285 USTF(marker_iter_start) (&iter);
7286
7287 return iter.marker;
7288}
7289
7290/* Returns the marker following M. */
7291
7292const struct marker *
7293next_marker (const struct marker *m)
7294{
7295 struct marker_iter iter;
7296
7297 USTF(marker_iter_reset) (&iter);
7298 USTF(marker_iter_start) (&iter);
7299
7300 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7301 {
7302 if (iter.marker == m)
7303 {
7304 USTF(marker_iter_next) (&iter);
7305 return iter.marker;
7306 }
7307 }
7308
7309 return NULL;
7310}
7311
7312/* Compose packet that is the response to the qTsSTM/qTfSTM/qTSTMat
7313 packets. */
7314
7315static void
7316response_ust_marker (char *packet, const struct marker *st)
7317{
7318 char *strid, *format, *tmp;
7319
7320 next_st = next_marker (st);
7321
7322 tmp = xmalloc (strlen (st->channel) + 1 +
7323 strlen (st->name) + 1);
7324 sprintf (tmp, "%s/%s", st->channel, st->name);
7325
7326 strid = cstr_to_hexstr (tmp);
7327 free (tmp);
7328
7329 format = cstr_to_hexstr (st->format);
7330
7331 sprintf (packet, "m%s:%s:%s",
7332 paddress ((uintptr_t) st->location),
7333 strid,
7334 format);
7335
7336 free (strid);
7337 free (format);
7338}
7339
7340/* Return the first static tracepoint, and initialize the state
7341 machine that will iterate through all the static tracepoints. */
7342
7343static void
7344cmd_qtfstm (char *packet)
7345{
7346 trace_debug ("Returning first trace state variable definition");
7347
7348 if (first_marker ())
7349 response_ust_marker (packet, first_marker ());
7350 else
7351 strcpy (packet, "l");
7352}
7353
7354/* Return additional trace state variable definitions. */
7355
7356static void
7357cmd_qtsstm (char *packet)
7358{
7359 trace_debug ("Returning static tracepoint");
7360
7361 if (next_st)
7362 response_ust_marker (packet, next_st);
7363 else
7364 strcpy (packet, "l");
7365}
7366
7367/* Disconnect the GDB probe from a marker at a given address. */
7368
7369static void
7370unprobe_marker_at (char *packet)
7371{
7372 char *p = packet;
7373 ULONGEST address;
7374 struct marker_iter iter;
7375
7376 p += sizeof ("unprobe_marker_at:") - 1;
7377
7378 p = unpack_varlen_hex (p, &address);
7379
7380 USTF(marker_iter_reset) (&iter);
7381 USTF(marker_iter_start) (&iter);
7382 for (; iter.marker != NULL; USTF(marker_iter_next) (&iter))
7383 if ((uintptr_t ) iter.marker->location == address)
7384 {
7385 int result;
7386
7387 result = USTF(ltt_marker_disconnect) (iter.marker->channel,
7388 iter.marker->name,
7389 GDB_PROBE_NAME);
7390 if (result < 0)
7391 warning ("could not disable marker %s/%s",
7392 iter.marker->channel, iter.marker->name);
7393 break;
7394 }
7395}
7396
7397/* Connect the GDB probe to a marker at a given address. */
7398
7399static int
7400probe_marker_at (char *packet)
7401{
7402 char *p = packet;
7403 ULONGEST address;
7404 struct marker_iter iter;
7405 struct marker *m;
7406
7407 p += sizeof ("probe_marker_at:") - 1;
7408
7409 p = unpack_varlen_hex (p, &address);
7410
7411 USTF(marker_iter_reset) (&iter);
7412
7413 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7414 m != NULL;
7415 USTF(marker_iter_next) (&iter), m = iter.marker)
7416 if ((uintptr_t ) m->location == address)
7417 {
7418 int result;
7419
7420 trace_debug ("found marker for address. "
7421 "ltt_marker_connect (marker = %s/%s)",
7422 m->channel, m->name);
7423
493e2a69
MS
7424 result = USTF(ltt_marker_connect) (m->channel, m->name,
7425 GDB_PROBE_NAME);
0fb4aa4b
PA
7426 if (result && result != -EEXIST)
7427 trace_debug ("ltt_marker_connect (marker = %s/%s, errno = %d)",
7428 m->channel, m->name, -result);
7429
7430 if (result < 0)
7431 {
7432 sprintf (packet, "E.could not connect marker: channel=%s, name=%s",
7433 m->channel, m->name);
7434 return -1;
7435 }
7436
7437 strcpy (packet, "OK");
7438 return 0;
7439 }
7440
7441 sprintf (packet, "E.no marker found at 0x%s", paddress (address));
7442 return -1;
7443}
7444
7445static int
7446cmd_qtstmat (char *packet)
7447{
7448 char *p = packet;
7449 ULONGEST address;
7450 struct marker_iter iter;
7451 struct marker *m;
7452
7453 p += sizeof ("qTSTMat:") - 1;
7454
7455 p = unpack_varlen_hex (p, &address);
7456
7457 USTF(marker_iter_reset) (&iter);
7458
7459 for (USTF(marker_iter_start) (&iter), m = iter.marker;
7460 m != NULL;
7461 USTF(marker_iter_next) (&iter), m = iter.marker)
7462 if ((uintptr_t ) m->location == address)
7463 {
7464 response_ust_marker (packet, m);
7465 return 0;
7466 }
7467
7468 strcpy (packet, "l");
7469 return -1;
7470}
7471
7472static void *
7473gdb_ust_thread (void *arg)
7474{
7475 int listen_fd;
7476
7477 while (1)
7478 {
7479 listen_fd = gdb_ust_socket_init ();
7480
7481#ifdef SYS_gettid
7482 if (helper_thread_id == 0)
7483 helper_thread_id = syscall (SYS_gettid);
7484#endif
7485
7486 if (listen_fd == -1)
7487 {
7488 warning ("could not create sync socket\n");
7489 break;
7490 }
7491
7492 while (1)
7493 {
7494 socklen_t tmp;
7495 struct sockaddr_un sockaddr;
7496 int fd;
7497 char buf[1];
7498 int ret;
7499
7500 tmp = sizeof (sockaddr);
7501
7502 do
7503 {
7504 fd = accept (listen_fd, &sockaddr, &tmp);
7505 }
7506 /* It seems an ERESTARTSYS can escape out of accept. */
7507 while (fd == -512 || (fd == -1 && errno == EINTR));
7508
7509 if (fd < 0)
7510 {
7511 warning ("Accept returned %d, error: %s\n",
7512 fd, strerror (errno));
7513 break;
7514 }
7515
7516 do
7517 {
7518 ret = read (fd, buf, 1);
7519 } while (ret == -1 && errno == EINTR);
7520
7521 if (ret == -1)
7522 {
7523 warning ("reading socket (fd=%d) failed with %s",
7524 fd, strerror (errno));
7525 close (fd);
7526 break;
7527 }
7528
7529 if (cmd_buf[0])
7530 {
7531 if (strcmp ("qTfSTM", cmd_buf) == 0)
7532 {
7533 cmd_qtfstm (cmd_buf);
7534 }
7535 else if (strcmp ("qTsSTM", cmd_buf) == 0)
7536 {
7537 cmd_qtsstm (cmd_buf);
7538 }
7539 else if (strncmp ("unprobe_marker_at:",
7540 cmd_buf,
7541 sizeof ("unprobe_marker_at:") - 1) == 0)
7542 {
7543 unprobe_marker_at (cmd_buf);
7544 }
7545 else if (strncmp ("probe_marker_at:",
7546 cmd_buf,
7547 sizeof ("probe_marker_at:") - 1) == 0)
7548 {
7549 probe_marker_at (cmd_buf);
7550 }
7551 else if (strncmp ("qTSTMat:",
7552 cmd_buf,
7553 sizeof ("qTSTMat:") - 1) == 0)
7554 {
7555 cmd_qtstmat (cmd_buf);
7556 }
7557 else if (strcmp (cmd_buf, "help") == 0)
7558 {
7559 strcpy (cmd_buf, "for help, press F1\n");
7560 }
7561 else
7562 strcpy (cmd_buf, "");
7563 }
7564
7565 write (fd, buf, 1);
7566 close (fd);
7567 }
7568 }
7569
7570 return NULL;
7571}
7572
7573#include <signal.h>
7574
7575static void
7576gdb_ust_init (void)
7577{
7578 int res;
7579 pthread_t thread;
7580 sigset_t new_mask;
7581 sigset_t orig_mask;
7582
7583 if (!dlsym_ust ())
7584 return;
7585
7586 /* We want the helper thread to be as transparent as possible, so
7587 have it inherit an all-signals-blocked mask. */
7588
7589 sigfillset (&new_mask);
7590 res = pthread_sigmask (SIG_SETMASK, &new_mask, &orig_mask);
7591 if (res)
7592 fatal ("pthread_sigmask (1) failed: %s", strerror (res));
7593
7594 res = pthread_create (&thread,
7595 NULL,
7596 gdb_ust_thread,
7597 NULL);
7598
7599 res = pthread_sigmask (SIG_SETMASK, &orig_mask, NULL);
7600 if (res)
7601 fatal ("pthread_sigmask (2) failed: %s", strerror (res));
7602
7603 while (helper_thread_id == 0)
7604 usleep (1);
7605
7606 USTF(ltt_probe_register) (&gdb_ust_probe);
7607}
7608
7609#endif /* HAVE_UST */
7610
7611#include <sys/mman.h>
7612#include <fcntl.h>
7613
7614IP_AGENT_EXPORT char *gdb_tp_heap_buffer;
7615IP_AGENT_EXPORT char *gdb_jump_pad_buffer;
7616IP_AGENT_EXPORT char *gdb_jump_pad_buffer_end;
7617
7618static void __attribute__ ((constructor))
7619initialize_tracepoint_ftlib (void)
7620{
7621 initialize_tracepoint ();
7622
7623#ifdef HAVE_UST
7624 gdb_ust_init ();
7625#endif
7626}
7627
7628#endif /* IN_PROCESS_AGENT */
fa593d66 7629
219f2f23
PA
7630static LONGEST
7631tsv_get_timestamp (void)
7632{
7633 struct timeval tv;
7634
7635 if (gettimeofday (&tv, 0) != 0)
7636 return -1;
7637 else
7638 return (LONGEST) tv.tv_sec * 1000000 + tv.tv_usec;
7639}
7640
7641void
7642initialize_tracepoint (void)
7643{
7644 /* There currently no way to change the buffer size. */
7645 const int sizeOfBuffer = 5 * 1024 * 1024;
7646 unsigned char *buf = xmalloc (sizeOfBuffer);
7647 init_trace_buffer (buf, sizeOfBuffer);
7648
7649 /* Wire trace state variable 1 to be the timestamp. This will be
7650 uploaded to GDB upon connection and become one of its trace state
7651 variables. (In case you're wondering, if GDB already has a trace
7652 variable numbered 1, it will be renumbered.) */
fa593d66 7653 create_trace_state_variable (1, 0);
219f2f23
PA
7654 set_trace_state_variable_name (1, "trace_timestamp");
7655 set_trace_state_variable_getter (1, tsv_get_timestamp);
fa593d66
PA
7656
7657#ifdef IN_PROCESS_AGENT
7658 {
7659 int pagesize;
7660 pagesize = sysconf (_SC_PAGE_SIZE);
7661 if (pagesize == -1)
7662 fatal ("sysconf");
7663
7664 gdb_tp_heap_buffer = xmalloc (5 * 1024 * 1024);
7665
7666 /* Allocate scratch buffer aligned on a page boundary. */
7667 gdb_jump_pad_buffer = memalign (pagesize, pagesize * 20);
7668 gdb_jump_pad_buffer_end = gdb_jump_pad_buffer + pagesize * 20;
7669
7670 /* Make it writable and executable. */
7671 if (mprotect (gdb_jump_pad_buffer, pagesize * 20,
7672 PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
7673 fatal ("\
7674initialize_tracepoint: mprotect(%p, %d, PROT_READ|PROT_EXEC) failed with %s",
7675 gdb_jump_pad_buffer, pagesize * 20, strerror (errno));
7676 }
7677
7678 initialize_low_tracepoint ();
7679#endif
219f2f23 7680}
This page took 0.482309 seconds and 4 git commands to generate.