1 /* Copyright (C) 2021-2023 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
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)
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.
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. */
27 #include "data_pckts.h"
28 #include "libcol_util.h"
29 #include "collector_module.h"
31 #define GETRELTIME() (__collector_gethrtime() - __collector_start_time)
32 #define CALL_REAL(x) (__real_##x)
33 #define NULL_PTR(x) (__real_##x == NULL)
35 #define SYS_LIBC_NAME "libc.so.6"
37 #ifdef __has_attribute
38 #if __has_attribute (__symver__)
39 #define SYMVER_ATTRIBUTE(sym, symver) __attribute__ ((__symver__ (#symver)))
42 #ifndef SYMVER_ATTRIBUTE
43 # define SYMVER_ATTRIBUTE(sym, symver) __asm__(".symver " #sym "," #symver);
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)
51 #define DCL_FUNC_VER(REAL_DCL, sym, ver) \
52 SYMVER_ATTRIBUTE (__collector_ ## sym, ver) \
53 REAL_DCL (__collector_ ## sym)
56 extern hrtime_t __collector_start_time
;
58 /* ========================================================== */
59 /* ------- internal function prototypes ----------------- */
60 /* These will not be exported from libcollector.so */
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 ();
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
,
80 unsigned long *end
, int maxnretries
);
81 extern int __collector_check_readable_segment (unsigned long addr
,
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
,
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
*,
130 extern int collector_sigchld_sigaction (const struct sigaction
*,
134 __collector_log_write (char *format
, ...) __attribute__ ((format (printf
, 1, 2)));
136 /* ------- internal global data ----------------- */
137 /* These will not be exported from libcollector.so */
138 extern struct Heap
*__collector_heap
;
140 /* experiment state flag */
143 EXP_INIT
, EXP_OPEN
, EXP_PAUSED
, EXP_CLOSED
145 extern volatile sp_state_t __collector_expstate
;
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.
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
;
161 /* global hrtime_t for next periodic sample */
162 extern hrtime_t __collector_next_sample
;
163 extern int __collector_sample_period
;
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 ();
173 /* --------- information controlling debug tracing ------------- */
175 /* global flag, defines level of trace information */
176 extern void __collector_dlog (int, int, char *, ...) __attribute__ ((format (printf
, 3, 4)));
178 #define STR(x) ((x) ? (x) : "NULL")
180 // To set collector_debug_opt use:
181 // SP_COLLECTOR_DEBUG=4 ; export SP_COLLECTOR_DEBUG ; collect ...
187 SP_DUMP_NOHEADER
= 8,
192 /* TprintfT(<level>,...) definitions. Adjust per module as needed */
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
204 #define DprintfT(flag, ...)
207 #define TprintfT(...)
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__ )