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