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