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