1/* Copyright (C) 2021 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 <ucontext.h>
25#include <signal.h>
26
27#include "gp-defs.h"
28#include "data_pckts.h"
29#include "libcol_util.h"
30#include "collector_module.h"
31
32#define GETRELTIME()    (__collector_gethrtime() - __collector_start_time)
33
34extern hrtime_t __collector_start_time;
35
36/* ========================================================== */
37/* -------  internal function prototypes ----------------- */
38/* These will not be exported from libcollector.so */
39struct DataHandle;
40struct Heap;
41extern struct DataHandle *__collector_create_handle (char*);
42extern void __collector_delete_handle (struct DataHandle*);
43extern int __collector_write_record (struct DataHandle*, Common_packet*);
44extern int __collector_write_packet (struct DataHandle*, CM_Packet*);
45extern int __collector_write_string (struct DataHandle*, char*, int);
46extern FrameInfo __collector_get_frame_info (hrtime_t, int, void *);
47extern FrameInfo __collector_getUID (CM_Array *arg, FrameInfo uid);
48extern int __collector_getStackTrace (void *buf, int size, void *bptr, void *eptr, void *arg);
49extern void *__collector_ext_return_address (unsigned level);
50extern void __collector_mmap_fork_child_cleanup ();
51
52extern int __collector_ext_mmap_install (int);
53extern int __collector_ext_mmap_deinstall (int);
54extern int __collector_ext_update_map_segments (void);
55extern int __collector_check_segment (unsigned long addr,
56				      unsigned long *base,
57				      unsigned long *end, int maxnretries);
58extern int __collector_check_readable_segment (unsigned long addr,
59					       unsigned long *base,
60					       unsigned long *end, int maxnretries);
61extern int __collector_ext_line_init (int * pfollow_this_experiment,
62				      const char * progspec,
63				      const char *progname);
64extern int __collector_ext_line_install (char *, const char *);
65extern void __collector_ext_line_close ();
66extern void __collector_ext_unwind_init (int);
67extern void __collector_ext_unwind_close ();
68extern int __collector_ext_jstack_unwind (char*, int, ucontext_t *);
69extern void __collector_ext_dispatcher_fork_child_cleanup ();
70extern void __collector_ext_unwind_key_init (int isPthread, void * stack);
71extern void __collector_ext_dispatcher_tsd_create_key ();
72extern void __collector_ext_dispatcher_thread_timer_suspend ();
73extern int __collector_ext_dispatcher_thread_timer_resume ();
74extern int __collector_ext_dispatcher_install ();
75extern void __collector_ext_dispatcher_suspend ();
76extern void __collector_ext_dispatcher_restart ();
77extern void __collector_ext_dispatcher_deinstall ();
78extern void __collector_ext_usage_sample (Smpl_type type, char *name);
79extern void __collector_ext_profile_handler (siginfo_t *, ucontext_t *);
80extern int __collector_ext_clone_pthread (int (*fn)(void *), void *child_stack, int flags, void *arg,
81					  va_list va /* pid_t *ptid, struct user_desc *tlspid_t *" ctid" */);
82
83/* D-light related functions */
84extern int __collector_sigprof_install ();
85extern int __collector_ext_hwc_active ();
86extern void __collector_ext_hwc_check (siginfo_t *, ucontext_t *);
87extern int __collector_ext_hwc_lwp_init ();
88extern void __collector_ext_hwc_lwp_fini ();
89extern int __collector_ext_hwc_lwp_suspend ();
90extern int __collector_ext_hwc_lwp_resume ();
91extern int (*__collector_VM_ReadByteInstruction)(unsigned char *);
92extern int (*__collector_omp_stack_trace)(char*, int, hrtime_t, void*);
93extern hrtime_t (*__collector_gethrtime)();
94extern int (*__collector_mpi_stack_trace)(char*, int, hrtime_t);
95extern int __collector_open_experiment (const char *exp, const char *par, sp_origin_t origin);
96extern void __collector_suspend_experiment (char *why);
97extern void __collector_resume_experiment ();
98extern void __collector_clean_state ();
99extern void __collector_close_experiment ();
100extern void __collector_terminate_expt ();
101extern void __collector_terminate_hook ();
102extern void __collector_sample (char *name);
103extern void __collector_pause ();
104extern void __collector_pause_m ();
105extern void __collector_resume ();
106extern int collector_sigemt_sigaction (const struct sigaction*,
107				       struct sigaction*);
108extern int collector_sigchld_sigaction (const struct sigaction*,
109					struct sigaction*);
110
111extern int
112__collector_log_write (char *format, ...) __attribute__ ((format (printf, 1, 2)));
113
114/* -------  internal global data ----------------- */
115/* These will not be exported from libcollector.so */
116extern struct Heap *__collector_heap;
117
118/* experiment state flag  */
119typedef enum
120{
121  EXP_INIT, EXP_OPEN, EXP_PAUSED, EXP_CLOSED
122} sp_state_t;
123extern volatile sp_state_t __collector_expstate;
124
125/* global flag, defines whether target is threaded or not
126 *   if set, put _lwp_self() for thread id instead of thr_self()
127 *	in output packets; should be set before any data packets
128 *	are written, i.e., before signal handlers are installed.
129 */
130extern int __collector_no_threads;
131extern int __collector_libthread_T1; /* T1 or not T1 */
132extern int __collector_sample_sig; /* set to signal used to trigger a sample */
133extern int __collector_sample_sig_warn; /* if 1, warning given on target use */
134extern int __collector_pause_sig; /* set to signal used to toggle pause-resume */
135extern int __collector_pause_sig_warn; /* if 1, warning given on target use */
136extern hrtime_t __collector_delay_start;
137extern int __collector_exp_active;
138
139/* global hrtime_t for next periodic sample */
140extern hrtime_t __collector_next_sample;
141extern int __collector_sample_period;
142
143/* global hrtime_t for experiment termination (-t) */
144extern hrtime_t __collector_terminate_time;
145extern int __collector_terminate_duration;
146extern char __collector_exp_dir_name[];
147extern int __collector_java_mode;
148extern int __collector_java_asyncgetcalltrace_loaded;
149extern int __collector_jprofile_start_attach ();
150
151/* --------- information controlling debug tracing ------------- */
152
153/* global flag, defines level of trace information */
154extern void __collector_dlog (int, int, char *, ...) __attribute__ ((format (printf, 3, 4)));
155
156#define STR(x)  ((x) ? (x) : "NULL")
157
158// To set collector_debug_opt use:
159//   SP_COLLECTOR_DEBUG=4 ; export SP_COLLECTOR_DEBUG ; collect ...
160enum
161{
162  SP_DUMP_TIME      = 1,
163  SP_DUMP_FLAG      = 2,
164  SP_DUMP_JAVA      = 4,
165  SP_DUMP_NOHEADER  = 8,
166  SP_DUMP_UNWIND    = 16,
167  SP_DUMP_STACK     = 32,
168};
169
170#ifndef DEBUG
171#define DprintfT(flag, ...)
172#define tprintf(...)
173#define Tprintf(...)
174#define TprintfT(...)
175
176#else
177#define DprintfT(flag, ...)  __collector_dlog(SP_DUMP_FLAG | (flag), 0, __VA_ARGS__ )
178#define tprintf(...)  __collector_dlog( SP_DUMP_NOHEADER, __VA_ARGS__ )
179#define Tprintf(...)  __collector_dlog( 0, __VA_ARGS__ )
180#define TprintfT(...) __collector_dlog( SP_DUMP_TIME, __VA_ARGS__ )
181
182#endif /* DEBUG */
183
184// To find the glibc version:
185//   objdump -T /lib*/*so /lib*/*/*.so | grep popen
186// IMPORTANT: The GLIBC_* versions below must match those in mapfile.<variant>
187 #if ARCH(Aarch64)
188  #define SYS_LIBC_NAME                 "libc.so.6"
189  #define SYS_PTHREAD_CREATE_VERSION    "GLIBC_2.17"
190  #define SYS_DLOPEN_VERSION            "GLIBC_2.17"
191  #define SYS_POPEN_VERSION             "GLIBC_2.17"
192  #define SYS_FOPEN_X_VERSION           "GLIBC_2.17"
193  #define SYS_FGETPOS_X_VERSION         "GLIBC_2.17"
194
195#elif ARCH(Intel)
196  #define SYS_LIBC_NAME                 "libc.so.6"
197  #define SYS_POSIX_SPAWN_VERSION       "GLIBC_2.15"
198  #if WSIZE(32)
199   #define SYS_PTHREAD_CREATE_VERSION   "GLIBC_2.1"
200   #define SYS_DLOPEN_VERSION           "GLIBC_2.1"
201   #define SYS_POPEN_VERSION            "GLIBC_2.1"
202   #define SYS_TIMER_X_VERSION          "GLIBC_2.2"
203   #define SYS_FOPEN_X_VERSION          "GLIBC_2.1"
204   #define SYS_FGETPOS_X_VERSION        "GLIBC_2.2"
205   #define SYS_FGETPOS64_X_VERSION      "GLIBC_2.2"
206   #define SYS_OPEN64_X_VERSION         "GLIBC_2.2"
207   #define SYS_PREAD_X_VERSION          "GLIBC_2.2"
208   #define SYS_PWRITE_X_VERSION         "GLIBC_2.2"
209   #define SYS_PWRITE64_X_VERSION       "GLIBC_2.2"
210  #else /* WSIZE(64) */
211   #define SYS_PTHREAD_CREATE_VERSION   "GLIBC_2.2.5"
212   #define SYS_DLOPEN_VERSION           "GLIBC_2.2.5"
213   #define SYS_POPEN_VERSION            "GLIBC_2.2.5"
214   #define SYS_TIMER_X_VERSION          "GLIBC_2.3.3"
215   #define SYS_FOPEN_X_VERSION          "GLIBC_2.2.5"
216   #define SYS_FGETPOS_X_VERSION        "GLIBC_2.2.5"
217  #endif
218
219 #elif ARCH(SPARC)
220  #define SYS_LIBC_NAME                 "libc.so.6"
221  #define SYS_DLOPEN_VERSION            "GLIBC_2.1"
222  #if WSIZE(32)
223   #define SYS_PTHREAD_CREATE_VERSION   "GLIBC_2.1"
224   #define SYS_POPEN_VERSION            "GLIBC_2.1"
225   #define SYS_FOPEN_X_VERSION          "GLIBC_2.1"
226   #define SYS_FGETPOS_X_VERSION        "GLIBC_2.2"
227  #else /* WSIZE(64) */
228   #define SYS_PTHREAD_CREATE_VERSION   "GLIBC_2.2"
229   #define SYS_POPEN_VERSION            "GLIBC_2.2"
230   #define SYS_TIMER_X_VERSION          "GLIBC_2.3.3"
231   #define SYS_FOPEN_X_VERSION          "GLIBC_2.2"
232   #define SYS_FGETPOS_X_VERSION        "GLIBC_2.2"
233  #endif
234 #endif
235
236#endif
237