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