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