xref: /netbsd-src/external/gpl3/binutils/dist/gprofng/libcollector/collector.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
2    Contributed by Oracle.
3 
4    This file is part of GNU Binutils.
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, or (at your option)
9    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, write to the Free Software
18    Foundation, 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #ifndef _COLLECTOR_H
22 #define _COLLECTOR_H
23 
24 #include <signal.h>
25 
26 #include "gp-defs.h"
27 #include "data_pckts.h"
28 #include "libcol_util.h"
29 #include "collector_module.h"
30 
31 #define GETRELTIME()    (__collector_gethrtime() - __collector_start_time)
32 #define CALL_REAL(x)	(__real_##x)
33 #define NULL_PTR(x)	(__real_##x == NULL)
34 
35 #define SYS_LIBC_NAME   "libc.so.6"
36 
37 #ifdef __has_attribute
38 #if __has_attribute (__symver__)
39 #define SYMVER_ATTRIBUTE(sym, symver)  __attribute__ ((__symver__ (#symver)))
40 #endif
41 #endif
42 #ifndef SYMVER_ATTRIBUTE
43 # define SYMVER_ATTRIBUTE(sym, symver)  __asm__(".symver " #sym "," #symver);
44 #endif
45 
46 #if defined(__MUSL_LIBC)
47 #define dlvsym(f, nm, v)  dlsym (f, nm)
48 #define SIGEV_THREAD_ID   4
49 #define DCL_FUNC_VER(REAL_DCL, sym, ver)
50 #else
51 #define DCL_FUNC_VER(REAL_DCL, sym, ver) \
52   SYMVER_ATTRIBUTE (__collector_ ## sym, ver) \
53   REAL_DCL (__collector_ ## sym)
54 #endif
55 
56 extern hrtime_t __collector_start_time;
57 
58 /* ========================================================== */
59 /* -------  internal function prototypes ----------------- */
60 /* These will not be exported from libcollector.so */
61 struct DataHandle;
62 struct Heap;
63 extern struct DataHandle *__collector_create_handle (char*);
64 extern void __collector_delete_handle (struct DataHandle*);
65 extern int __collector_write_record (struct DataHandle*, Common_packet*);
66 extern int __collector_write_packet (struct DataHandle*, CM_Packet*);
67 extern int __collector_write_string (struct DataHandle*, char*, int);
68 extern FrameInfo __collector_get_frame_info (hrtime_t, int, void *);
69 extern FrameInfo __collector_getUID (CM_Array *arg, FrameInfo uid);
70 extern int __collector_getStackTrace (void *buf, int size, void *bptr,
71 				      void *eptr, void *arg);
72 extern void *__collector_ext_return_address (unsigned level);
73 extern void __collector_mmap_fork_child_cleanup ();
74 
75 extern int __collector_ext_mmap_install (int);
76 extern int __collector_ext_mmap_deinstall (int);
77 extern int __collector_ext_update_map_segments (void);
78 extern int __collector_check_segment (unsigned long addr,
79 				      unsigned long *base,
80 				      unsigned long *end, int maxnretries);
81 extern int __collector_check_readable_segment (unsigned long addr,
82 					       unsigned long *base,
83 					       unsigned long *end, int maxnretries);
84 extern int __collector_ext_line_init (int * pfollow_this_experiment,
85 				      const char * progspec,
86 				      const char *progname);
87 extern int __collector_ext_line_install (char *, const char *);
88 extern void __collector_ext_line_close ();
89 extern void __collector_ext_unwind_init (int);
90 extern void __collector_ext_unwind_close ();
91 extern int __collector_ext_jstack_unwind (char*, int, ucontext_t *);
92 extern void __collector_ext_dispatcher_fork_child_cleanup ();
93 extern void __collector_ext_unwind_key_init (int isPthread, void * stack);
94 extern void __collector_ext_dispatcher_tsd_create_key ();
95 extern void __collector_ext_dispatcher_thread_timer_suspend ();
96 extern int __collector_ext_dispatcher_thread_timer_resume ();
97 extern int __collector_ext_dispatcher_install ();
98 extern void __collector_ext_dispatcher_suspend ();
99 extern void __collector_ext_dispatcher_restart ();
100 extern void __collector_ext_dispatcher_deinstall ();
101 extern void __collector_ext_usage_sample (Smpl_type type, char *name);
102 extern void __collector_ext_profile_handler (siginfo_t *, ucontext_t *);
103 extern int __collector_ext_clone_pthread (int (*fn)(void *), void *child_stack,
104 					  int flags, void *arg, va_list va);
105 extern int __collector_sigprof_install ();
106 extern int __collector_ext_hwc_active ();
107 extern void __collector_ext_hwc_check (siginfo_t *, ucontext_t *);
108 extern int __collector_ext_hwc_lwp_init ();
109 extern void __collector_ext_hwc_lwp_fini ();
110 extern int __collector_ext_hwc_lwp_suspend ();
111 extern int __collector_ext_hwc_lwp_resume ();
112 extern int (*__collector_VM_ReadByteInstruction)(unsigned char *);
113 extern int (*__collector_omp_stack_trace)(char*, int, hrtime_t, void*);
114 extern hrtime_t (*__collector_gethrtime)();
115 extern int (*__collector_mpi_stack_trace)(char*, int, hrtime_t);
116 extern int __collector_open_experiment (const char *exp, const char *par,
117 					sp_origin_t origin);
118 extern void __collector_suspend_experiment (char *why);
119 extern void __collector_resume_experiment ();
120 extern void __collector_clean_state ();
121 extern void __collector_close_experiment ();
122 extern void __collector_terminate_expt ();
123 extern void __collector_terminate_hook ();
124 extern void __collector_sample (char *name);
125 extern void __collector_pause ();
126 extern void __collector_pause_m ();
127 extern void __collector_resume ();
128 extern int collector_sigemt_sigaction (const struct sigaction*,
129 				       struct sigaction*);
130 extern int collector_sigchld_sigaction (const struct sigaction*,
131 					struct sigaction*);
132 
133 extern int
134 __collector_log_write (char *format, ...) __attribute__ ((format (printf, 1, 2)));
135 
136 /* -------  internal global data ----------------- */
137 /* These will not be exported from libcollector.so */
138 extern struct Heap *__collector_heap;
139 
140 /* experiment state flag  */
141 typedef enum
142 {
143   EXP_INIT, EXP_OPEN, EXP_PAUSED, EXP_CLOSED
144 } sp_state_t;
145 extern volatile sp_state_t __collector_expstate;
146 
147 /* global flag, defines whether target is threaded or not
148  *   if set, put _lwp_self() for thread id instead of thr_self()
149  *	in output packets; should be set before any data packets
150  *	are written, i.e., before signal handlers are installed.
151  */
152 extern int __collector_no_threads;
153 extern int __collector_libthread_T1; /* T1 or not T1 */
154 extern int __collector_sample_sig; /* set to signal used to trigger a sample */
155 extern int __collector_sample_sig_warn; /* if 1, warning given on target use */
156 extern int __collector_pause_sig; /* set to signal used to toggle pause-resume */
157 extern int __collector_pause_sig_warn; /* if 1, warning given on target use */
158 extern hrtime_t __collector_delay_start;
159 extern int __collector_exp_active;
160 
161 /* global hrtime_t for next periodic sample */
162 extern hrtime_t __collector_next_sample;
163 extern int __collector_sample_period;
164 
165 /* global hrtime_t for experiment termination (-t) */
166 extern hrtime_t __collector_terminate_time;
167 extern int __collector_terminate_duration;
168 extern char __collector_exp_dir_name[];
169 extern int __collector_java_mode;
170 extern int __collector_java_asyncgetcalltrace_loaded;
171 extern int __collector_jprofile_start_attach ();
172 
173 /* --------- information controlling debug tracing ------------- */
174 
175 /* global flag, defines level of trace information */
176 extern void __collector_dlog (int, int, char *, ...) __attribute__ ((format (printf, 3, 4)));
177 
178 #define STR(x)  ((x) ? (x) : "NULL")
179 
180 // To set collector_debug_opt use:
181 //   SP_COLLECTOR_DEBUG=4 ; export SP_COLLECTOR_DEBUG ; collect ...
182 enum
183 {
184   SP_DUMP_TIME      = 1,
185   SP_DUMP_FLAG      = 2,
186   SP_DUMP_JAVA      = 4,
187   SP_DUMP_NOHEADER  = 8,
188   SP_DUMP_UNWIND    = 16,
189   SP_DUMP_STACK     = 32,
190 };
191 
192 /* TprintfT(<level>,...) definitions.  Adjust per module as needed */
193 enum
194 {
195   DBG_LT0 = 0, // for high-level configuration, unexpected errors/warnings
196   DBG_LTT = 0, // for interposition on GLIBC functions
197   DBG_LT1 = 1, // for configuration details, warnings
198   DBG_LT2 = 2,
199   DBG_LT3 = 3,
200   DBG_LT4 = 4
201 };
202 
203 #ifndef DEBUG
204 #define DprintfT(flag, ...)
205 #define tprintf(...)
206 #define Tprintf(...)
207 #define TprintfT(...)
208 
209 #else
210 #define DprintfT(flag, ...)  __collector_dlog(SP_DUMP_FLAG | (flag), 0, __VA_ARGS__ )
211 #define tprintf(...)  __collector_dlog( SP_DUMP_NOHEADER, __VA_ARGS__ )
212 #define Tprintf(...)  __collector_dlog( 0, __VA_ARGS__ )
213 #define TprintfT(...) __collector_dlog( SP_DUMP_TIME, __VA_ARGS__ )
214 
215 #endif /* DEBUG */
216 
217 #endif
218