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