1 /* Copyright (C) 2021-2023 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_MODULE_H
22 #define _COLLECTOR_MODULE_H
23
24 #include <sys/types.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <ucontext.h>
29 #include <dirent.h>
30
31 #include "gp-defs.h"
32
33 struct stat;
34 struct tm;
35
36 #define COLLECTOR_MODULE_ERR ((CollectorModule)-1)
37
38 /* ------- libc interface ----------------- */
39 /* the fields in this structure are in alphabetical order.
40 * If you add any, please put it in the right place */
41 typedef struct CollectorUtilFuncs
42 {
43 int (*access)();
44 int (*atoi)(const char *nptr);
45 void *(*calloc)(size_t nelem, size_t elsize);
46 int (*clearenv)(void);
47 int (*close)(int);
48 int (*closedir)();
49 int (*execv)(const char *path, char *const argv[]);
50 void (*exit)(int status);
51 int (*fclose)(FILE *stream);
52 int (*fcntl)(int fd, int cmd, ...);
53 char *(*fgets)(char *s, int n, FILE *stream);
54 FILE *(*fopen)(const char *filename, const char *mode);
55 pid_t (*vfork)();
56 int (*fprintf)(FILE *stream, const char *format, ...);
57 void (*free)(void *ptr);
58 int (*fstat)(int fd, struct stat *buf);
59 int (*getcontext)(ucontext_t *ucp);
60 int (*getcpuid)();
61 char *(*getcwd)(char *buf, size_t size);
62 char *(*getenv)(const char *name);
63 struct tm *(*gmtime_r)(const time_t *clock, struct tm *res);
64 int (*ioctl)(int d, int request, ...);
65 off_t (*lseek)(int fd, off_t offset, int whence);
66 void *(*malloc)(size_t size);
67 void *(*memset)(void *s1, int c, size_t n);
68 int (*mkdir)();
69 time_t (*mktime)(struct tm *timeptr);
70 void *(*mmap)(void *, size_t, int, int, int, off_t);
71 void *(*mmap64_)();
72 int (*munmap)();
73 int (*open)(const char *, int, ...);
74 int (*open_bare)(const char *, int, ...);
75 DIR *(*opendir)();
76 int (*pclose)(FILE *stream);
77 FILE *(*popen)(const char *command, const char *mode);
78 int (*putenv)(char *string);
79 ssize_t (*pwrite)();
80 ssize_t (*pwrite64_)();
81 ssize_t (*read)();
82 int (*setenv)(const char *name, const char *value, int overwrite);
83 int (*sigfillset)(sigset_t *set);
84 int (*sigprocmask)(int how, const sigset_t *set, sigset_t *oldset);
85 int (*snprintf)(char *str, size_t size, const char *format, ...);
86 int (*stack_getbounds)();
87 char *(*strchr)(const char *name, int c);
88 int (*strcmp)(const char *s1, const char *s2);
89 int (*strcpy)(const char *s1, const char *s2);
90 char *(*libc_strdup)(const char *s1); // Don't use "strdup" because it is a macro in gcc
91 char *(*strerror)(int errnum);
92 int (*strerror_r)(int errnum, char *strerrbuf, size_t buflen);
93 size_t (*strlcat)(char *dest, const char *src, size_t dstsize);
94 size_t (*strlcpy)(char *dest, const char *src, size_t dstsize);
95 size_t (*strlen)(const char *string);
96 int (*strncmp)(const char *s1, const char *s2, size_t n);
97 size_t (*strncpy)(char *dst, const char *src, size_t dstsize);
98 size_t (*strspn)(const char *s1, const char *s2);
99 char *(*strrchr)(const char *name, int c);
100 char *(*strstr)(const char *s1, const char *s2);
101 long int (*strtol)(const char *nptr, char **endptr, int base);
102 long long int (*strtoll)(const char *nptr, char **endptr, int base);
103 unsigned long int (*strtoul)(const char *nptr, char **endptr, int base);
104 unsigned long long int (*strtoull)(const char *nptr, char **endptr, int base);
105 int (*symlink)(const char *s1, const char *s2);
106 int (*syscall)(int number, ...);
107 long (*sysconf)(int name);
108 long (*sysinfo)(int command, char *buf, long count);
109 time_t (*time)(time_t *tloc);
110 int (*unsetenv)(const char *name);
111 int (*vsnprintf)(char *str, size_t size, const char *format, va_list ap);
112 pid_t (*waitpid)(pid_t pid, int *stat_loc, int options);
113 ssize_t (*write)();
114 double (*atof)();
115 void *n_a;
116 } CollectorUtilFuncs;
117
118 extern CollectorUtilFuncs __collector_util_funcs;
119 extern int __collector_dlsym_guard;
120
121 #define CALL_UTIL(x) __collector_util_funcs.x
122
123 /* The following constants define the meaning of the "void *arg"
124 * argument of getFrameInfo().
125 */
126 /* arg is a pointer to ucontext_t, walk the stack described by it */
127 #define FRINFO_FROM_UC 1
128 /* walk the current stack starting from the frame containing arg */
129 #define FRINFO_FROM_STACK 2
130 /* walk the current stack starting from the caller of the frame containing arg */
131 #define FRINFO_FROM_STACK_ARG 3
132 /* arg is a pc, process a stack containing just that pc */
133 #define FRINFO_FROM_PC 4
134 /* arg is of type CM_Array describing a stack image */
135 #define FRINFO_FROM_ARRAY 5
136 #define FRINFO_NO_OMP_INFO 0x80000000
137 #define FRINFO_NO_WALK 0x40000000
138
139 typedef struct CM_Array
140 {
141 unsigned int length; /* in bytes, not including length */
142 void *bytes;
143 } CM_Array;
144
145 // Interface with libcollector.so:
146 typedef enum
147 {
148 SP_ORIGIN_FORK = -1,
149 SP_ORIGIN_LIBCOL_INIT = 0,
150 SP_ORIGIN_DBX_ATTACH = 1,
151 SP_ORIGIN_GENEXP = 2,
152 SP_ORIGIN_KERNEL = 3,
153 SP_ORIGIN_DTRACE = 4,
154 SP_ORIGIN_COLLECT = 5
155 } sp_origin_t;
156
157 struct Heap;
158 struct Common_packet;
159 struct CM_Packet;
160 struct ModuleInterface;
161
162 typedef long long HiResTime;
163 typedef int CollectorModule;
164 typedef unsigned long long FrameInfo;
165 typedef struct CollectorInterface
166 {
167 /* General services */
168 CollectorModule (*registerModule)(struct ModuleInterface*);
169 const char *(*getParams)();
170 const char *(*getExpDir)();
171 int (*writeLog)(char *format, ...);
172 FrameInfo (*getFrameInfo)(CollectorModule modl, HiResTime ts, int mode, void *arg);
173 FrameInfo (*getUID)(CM_Array *arg);
174 FrameInfo (*getUID2)(CM_Array *arg, FrameInfo uid);
175 int (*getStackTrace)(void *buf, int size, void *bptr, void *eptr, void *arg);
176 int (*writeMetaData)(CollectorModule modl, char *format, ...);
177
178 /* writeDataRecord ensures that the header is filled in, and then calls writeDataPacket */
179 int (*writeDataRecord)(CollectorModule modl, struct Common_packet *pckt);
180 int (*writeDataPacket)(CollectorModule modl, struct CM_Packet *pckt);
181 void (*write_sample)(char *name);
182 void (*get_progspec)(char *retstr, int tmp_sz, char *namestr, int name_sz);
183 int (*open_experiment)(const char *exp, const char *params, sp_origin_t origin);
184 HiResTime (*getHiResTime)();
185
186 /* Dynamic memory allocation service */
187 struct Heap *(*newHeap)();
188 void (*deleteHeap)(struct Heap *heap);
189 void *(*allocCSize)(struct Heap *heap, unsigned sz, int log);
190 void (*freeCSize)(struct Heap *heap, void *ptr, unsigned sz);
191 void *(*allocVSize)(struct Heap *heap, unsigned sz);
192 void *(*reallocVSize)(struct Heap *heap, void *ptr, unsigned newsz);
193
194 /* Thread specific data service */
195 unsigned (*createKey)(size_t sz, void (*init)(void*), void (*fini)(void*));
196 void *(*getKey)(unsigned key);
197
198 /* Debugging services */
199 void (*writeDebugInfo)(int, int, char *, ...) __attribute__ ((format (printf, 3, 4)));
200 } CollectorInterface;
201
202 typedef struct ModuleInterface
203 {
204 char *description;
205 int (*initInterface)(CollectorInterface*);
206 int (*openExperiment)(const char *);
207 int (*startDataCollection)();
208 int (*stopDataCollection)();
209 int (*closeExperiment)();
210 int (*detachExperiment)(); /* called from fork-child before openExperiment() */
211 } ModuleInterface;
212
213 typedef CollectorModule (*RegModuleFunc)(ModuleInterface*);
214 typedef void (*ModuleInitFunc)(CollectorInterface*);
215
216 #ifdef __cplusplus
217 extern "C"
218 {
219 #endif
220 CollectorModule __collector_register_module (ModuleInterface *modint);
221 #ifdef __cplusplus
222 }
223 #endif
224
225 #endif /* _COLLECTOR_MODULE_H */