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