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