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