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