1 /* Copyright (C) 2021-2024 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. */
21 /* C and Fortran stubs for collector API */
26 #include "collectorAPI.h"
27 #include "gp-experiment.h"
29 static void (*__real_collector_sample
)(const char *) = NULL
;
30 static void (*__real_collector_pause
)() = NULL
;
31 static void (*__real_collector_resume
)() = NULL
;
32 static void (*__real_collector_terminate_expt
)() = NULL
;
33 static void (*__real_collector_func_load
)(const char *, const char *,
34 const char *, void *, int, int, Lineno
*) = NULL
;
35 static void (*__real_collector_func_unload
)(void *) = NULL
;
37 #define INIT_API if (init_API == 0) collectorAPI_initAPI()
38 #define NULL_PTR(x) (__real_##x == NULL)
39 #define CALL_REAL(x) (__real_##x)
40 #define CALL_IF_REAL(x) INIT_API; if (!NULL_PTR(x)) CALL_REAL(x)
42 static int init_API
= 0;
45 collectorAPI_initAPI (void)
47 void *libcollector
= dlopen (SP_LIBCOLLECTOR_NAME
, RTLD_NOLOAD
);
48 if (libcollector
== NULL
)
49 libcollector
= RTLD_DEFAULT
;
50 __real_collector_sample
= dlsym (libcollector
, "__collector_sample");
51 __real_collector_pause
= dlsym (libcollector
, "__collector_pause");
52 __real_collector_resume
= dlsym (libcollector
, "__collector_resume");
53 __real_collector_terminate_expt
= dlsym (libcollector
, "__collector_terminate_expt");
54 __real_collector_func_load
= dlsym (libcollector
, "__collector_func_load");
55 __real_collector_func_unload
= dlsym (libcollector
, "__collector_func_unload");
59 /* initialization -- init section routine */
60 static void collectorAPI_init () __attribute__ ((constructor
));
63 collectorAPI_init (void)
65 collectorAPI_initAPI ();
70 collector_pause (void)
72 CALL_IF_REAL (collector_pause
)();
76 collector_resume (void)
78 CALL_IF_REAL (collector_resume
)();
82 collector_sample (const char *name
)
84 CALL_IF_REAL (collector_sample
)(name
);
88 collector_terminate_expt (void)
90 CALL_IF_REAL (collector_terminate_expt
)();
94 collector_func_load (const char *name
, const char *alias
, const char *sourcename
,
95 void *vaddr
, int size
, int lntsize
, Lineno
*lntable
)
97 CALL_IF_REAL (collector_func_load
)(name
, alias
, sourcename
,
98 vaddr
, size
, lntsize
, lntable
);
102 collector_func_unload (void *vaddr
)
104 CALL_IF_REAL (collector_func_unload
)(vaddr
);
109 collector_pause_ (void)
111 CALL_IF_REAL (collector_pause
)();
115 collector_resume_ (void)
117 CALL_IF_REAL (collector_resume
)();
121 collector_terminate_expt_ (void)
123 CALL_IF_REAL (collector_terminate_expt
)();
127 collector_sample_ (char *name
, long name_length
)
130 if (!NULL_PTR (collector_sample
))
132 char name_string
[256];
133 long length
= sizeof (name_string
) - 1;
134 if (name_length
< length
)
135 length
= name_length
;
136 for (long i
= 0; i
< length
; i
++)
137 name_string
[i
] = name
[i
];
138 name_string
[length
] = '\0';
139 CALL_REAL (collector_sample
)(name_string
);