ファイル
rocm-systems/source/lib/omnitrace-dl/main.c
T

164 行
4.8 KiB
C
Raw 通常表示 履歴

2022-09-26 07:52:14 -05:00
// MIT License
//
// Copyright (c) 2022 Advanced Micro Devices, Inc. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#define _GNU_SOURCE
2022-10-21 17:39:08 -05:00
#define OMNITRACE_PUBLIC_API __attribute__((visibility("default")));
#define OMNITRACE_HIDDEN_API __attribute__((visibility("hidden")));
2022-09-26 07:52:14 -05:00
#include <dlfcn.h>
2022-11-13 10:42:14 -06:00
#include <stdbool.h>
2022-09-26 07:52:14 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2022-11-13 10:42:14 -06:00
#include <unistd.h>
2022-09-26 07:52:14 -05:00
2022-10-21 17:39:08 -05:00
//
// local type definitions
//
typedef int (*start_main_t)(int (*)(int, char**, char**), int, char**,
int (*)(int, char**, char**), void (*)(void), void (*)(void),
void*);
//
// local variables
//
static int (*main_real)(int, char**, char**); // Trampoline for the real main()
//
// local function declarations
//
int
omnitrace_main(int, char**, char**) OMNITRACE_HIDDEN_API;
int
omnitrace_libc_start_main(int (*)(int, char**, char**), int, char**,
2022-11-13 10:42:14 -06:00
int (*)(int, char**, char**), void (*)(void), void (*)(void),
void*) OMNITRACE_HIDDEN_API;
2022-10-21 17:39:08 -05:00
int
__libc_start_main(int (*)(int, char**, char**), int, char**, int (*)(int, char**, char**),
void (*)(void), void (*)(void), void*) OMNITRACE_PUBLIC_API;
//
// external function declarations
//
2022-09-30 10:47:07 -05:00
extern int
omnitrace_preload_library(void);
2022-09-26 07:52:14 -05:00
extern void
omnitrace_finalize(void);
extern void
omnitrace_push_trace(const char* name);
extern void
omnitrace_pop_trace(const char* name);
extern void
omnitrace_init_tooling(void);
extern void
omnitrace_init(const char*, bool, const char*);
2022-10-21 17:39:08 -05:00
//
// local function definitions
//
2022-09-26 07:52:14 -05:00
int
omnitrace_main(int argc, char** argv, char** envp)
{
// prevent re-entry
static int _reentry = 0;
if(_reentry > 0) return -1;
_reentry = 1;
// set the relevant environment variables
// omnitrace_update_env(&envp);
const char* mode = getenv("OMNITRACE_MODE");
omnitrace_init(mode ? mode : "sampling", false, argv[0]);
omnitrace_init_tooling();
2023-01-24 18:53:23 -06:00
omnitrace_push_trace(basename(argv[0]));
2022-09-26 07:52:14 -05:00
int ret = main_real(argc, argv, envp);
2023-01-24 18:53:23 -06:00
omnitrace_pop_trace(basename(argv[0]));
2022-09-26 07:52:14 -05:00
omnitrace_finalize();
return ret;
}
int
2022-10-21 17:39:08 -05:00
omnitrace_libc_start_main(int (*_main)(int, char**, char**), int _argc, char** _argv,
2022-11-13 10:42:14 -06:00
int (*_init)(int, char**, char**), void (*_fini)(void),
void (*_rtld_fini)(void), void* _stack_end)
2022-09-26 07:52:14 -05:00
{
2022-09-30 10:47:07 -05:00
int _preload = omnitrace_preload_library();
2022-09-26 07:52:14 -05:00
// prevent re-entry
static int _reentry = 0;
if(_reentry > 0) return -1;
_reentry = 1;
// get the address of this function
void* _this_func = __builtin_return_address(0);
// Save the real main function address
main_real = _main;
// Find the real __libc_start_main()
2022-10-21 17:39:08 -05:00
start_main_t user_main = dlsym(RTLD_NEXT, "__libc_start_main");
2022-09-26 07:52:14 -05:00
2022-09-30 10:47:07 -05:00
// disable future LD_PRELOADs
setenv("OMNITRACE_PRELOAD", "0", 1);
2022-09-26 07:52:14 -05:00
if(user_main && user_main != _this_func)
{
2022-09-30 10:47:07 -05:00
if(_preload == 0)
{
// call original main
return user_main(main_real, _argc, _argv, _init, _fini, _rtld_fini,
_stack_end);
}
else
{
2022-09-26 07:52:14 -05:00
// call omnitrace main function wrapper
2022-09-30 10:47:07 -05:00
return user_main(omnitrace_main, _argc, _argv, _init, _fini, _rtld_fini,
_stack_end);
}
2022-09-26 07:52:14 -05:00
}
else
{
fputs("Error! omnitrace could not find __libc_start_main!", stderr);
return -1;
}
}
2022-10-21 17:39:08 -05:00
int
__libc_start_main(int (*_main)(int, char**, char**), int _argc, char** _argv,
int (*_init)(int, char**, char**), void (*_fini)(void),
void (*_rtld_fini)(void), void* _stack_end)
{
return omnitrace_libc_start_main(_main, _argc, _argv, _init, _fini, _rtld_fini,
2022-11-13 10:42:14 -06:00
_stack_end);
2022-10-21 17:39:08 -05:00
}