From 4d959460e1fdb8f003542a6b99f849ad1fc5e8fc Mon Sep 17 00:00:00 2001 From: David Galiffi Date: Wed, 24 Sep 2025 13:52:34 -0400 Subject: [PATCH] Add ROCPROFSYS_PATH variable to environment (#1103) * Add ROCPROFSYS_ROOT to the env for sample * Add env for causal * Add env for instrument * Check for null and address memory leak Signed-off-by: David Galiffi --------- Signed-off-by: David Galiffi --- .../source/bin/rocprof-sys-causal/impl.cpp | 35 ++++++++++------- .../rocprof-sys-causal/rocprof-sys-causal.hpp | 3 ++ .../source/bin/rocprof-sys-sample/impl.cpp | 34 +++++++++------- .../rocprof-sys-sample/rocprof-sys-sample.hpp | 3 ++ .../source/lib/core/argparse.cpp | 39 ++++++++++++------- 5 files changed, 72 insertions(+), 42 deletions(-) diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp index 5d8ba62667..25e62004e7 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/impl.cpp @@ -247,31 +247,38 @@ prepare_environment_for_run(std::vector& _env) get_realpath(get_internal_libpath("librocprof-sys-dl.so"))), true); update_env(_env, "ROCPROFSYS_SCRIPT_DIR", get_internal_script_path()); + update_env(_env, "ROCPROFSYS_ROOT", get_rocprofsys_root()); } } +std::string +get_rocprofsys_root(void) +{ + char* _tmp = realpath("/proc/self/exe", nullptr); + std::string _exe = (_tmp) ? std::string{ _tmp } : std::string{}; + + if(_tmp) free(_tmp); + + auto _pos = _exe.find_last_of('/'); + auto _dir = std::string{ "./" }; + + if(_pos != std::string::npos) _dir = _exe.substr(0, _pos); + + return rocprofsys::common::join("/", _dir, ".."); +} + std::string get_internal_libpath(const std::string& _lib) { - auto _exe = std::string_view{ realpath("/proc/self/exe", nullptr) }; - auto _pos = _exe.find_last_of('/'); - auto _dir = std::string{ "./" }; - if(_pos != std::string_view::npos) _dir = _exe.substr(0, _pos); - return rocprofsys::common::join("/", _dir, "..", "lib", _lib); + auto _root = get_rocprofsys_root(); + return rocprofsys::common::join("/", _root, "lib", _lib); } std::string get_internal_script_path(void) { - auto _exe = std::string_view{ realpath("/proc/self/exe", nullptr) }; - auto _pos = _exe.find_last_of('/'); - auto _dir = std::string{ "./" }; - if(_pos != std::string_view::npos) _dir = _exe.substr(0, _pos); - - auto _script_dir = get_realpath( - rocprofsys::common::join("/", _dir, "..", "libexec", "rocprofiler-systems")); - - return _script_dir; + auto _root = get_rocprofsys_root(); + return rocprofsys::common::join("/", _root, "libexec", "rocprofiler-systems"); } void diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.hpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.hpp index 726011bcdd..d8535532d3 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.hpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-causal/rocprof-sys-causal.hpp @@ -52,6 +52,9 @@ prepare_command_for_run(char*, std::vector&); void prepare_environment_for_run(std::vector&); +std::string +get_rocprofsys_root(void); + std::string get_internal_libpath(const std::string& _lib); diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp index ce005f1d8b..8fb47207ae 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/impl.cpp @@ -125,7 +125,9 @@ get_initial_environment() auto _dl_libpath = get_realpath(get_internal_libpath("librocprof-sys-dl.so")); auto _omni_libpath = get_realpath(get_internal_libpath("librocprof-sys.so")); auto _libexecpath = get_realpath(get_internal_script_path()); + auto _rootpath = get_realpath(get_rocprofsys_root()); + update_env(_env, "ROCPROFSYS_ROOT", _rootpath, UPD_REPLACE); update_env(_env, "LD_PRELOAD", _dl_libpath, UPD_APPEND); update_env(_env, "LD_LIBRARY_PATH", tim::filepath::dirname(_dl_libpath), UPD_APPEND); update_env(_env, "ROCPROFSYS_SCRIPT_PATH", _libexecpath, UPD_REPLACE); @@ -138,27 +140,33 @@ get_initial_environment() } std::string -get_internal_libpath(const std::string& _lib) +get_rocprofsys_root(void) { - auto _exe = std::string_view{ realpath("/proc/self/exe", nullptr) }; + char* _tmp = realpath("/proc/self/exe", nullptr); + std::string _exe = (_tmp) ? std::string{ _tmp } : std::string{}; + + if(_tmp) free(_tmp); + auto _pos = _exe.find_last_of('/'); auto _dir = std::string{ "./" }; - if(_pos != std::string_view::npos) _dir = _exe.substr(0, _pos); - return rocprofsys::common::join("/", _dir, "..", "lib", _lib); + + if(_pos != std::string::npos) _dir = _exe.substr(0, _pos); + + return rocprofsys::common::join("/", _dir, ".."); +} + +std::string +get_internal_libpath(const std::string& _lib) +{ + auto _root = get_rocprofsys_root(); + return rocprofsys::common::join("/", _root, "lib", _lib); } std::string get_internal_script_path(void) { - auto _exe = std::string_view{ realpath("/proc/self/exe", nullptr) }; - auto _pos = _exe.find_last_of('/'); - auto _dir = std::string{ "./" }; - if(_pos != std::string_view::npos) _dir = _exe.substr(0, _pos); - - auto _script_dir = - rocprofsys::common::join("/", _dir, "..", "libexec", "rocprofiler-systems"); - - return _script_dir; + auto _root = get_rocprofsys_root(); + return rocprofsys::common::join("/", _root, "libexec", "rocprofiler-systems"); } void diff --git a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.hpp b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.hpp index 8dfad88a3e..3ff6198567 100644 --- a/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.hpp +++ b/projects/rocprofiler-systems/source/bin/rocprof-sys-sample/rocprof-sys-sample.hpp @@ -46,6 +46,9 @@ print_updated_environment(std::vector _env); std::vector get_initial_environment(); +std::string +get_rocprofsys_root(void); + std::string get_internal_libpath(const std::string& _lib); diff --git a/projects/rocprofiler-systems/source/lib/core/argparse.cpp b/projects/rocprofiler-systems/source/lib/core/argparse.cpp index 03e0b9f6ef..5a457269d2 100644 --- a/projects/rocprofiler-systems/source/lib/core/argparse.cpp +++ b/projects/rocprofiler-systems/source/lib/core/argparse.cpp @@ -164,28 +164,34 @@ remove_env(parser_data& _data, std::string_view _env_var) } } +std::string +get_rocprofsys_root(void) +{ + char* _tmp = realpath("/proc/self/exe", nullptr); + std::string _exe = (_tmp) ? std::string{ _tmp } : std::string{}; + + if(_tmp) free(_tmp); + + auto _pos = _exe.find_last_of('/'); + auto _dir = std::string{ "./" }; + + if(_pos != std::string::npos) _dir = _exe.substr(0, _pos); + + return rocprofsys::common::join("/", _dir, ".."); +} + std::string get_internal_libpath(const std::string& _lib) { - auto _exe = filepath::realpath("/proc/self/exe", nullptr, false); - auto _pos = _exe.find_last_of('/'); - auto _dir = filepath::get_cwd(); - if(_pos != std::string_view::npos) _dir = _exe.substr(0, _pos); - return filepath::realpath(rocprofsys::common::join("/", _dir, "..", "lib", _lib), - nullptr, false); + auto _root = get_rocprofsys_root(); + return rocprofsys::common::join("/", _root, "lib", _lib); } + std::string get_internal_script_path(void) { - auto _exe = std::string_view{ realpath("/proc/self/exe", nullptr) }; - auto _pos = _exe.find_last_of('/'); - auto _dir = std::string{ "./" }; - if(_pos != std::string_view::npos) _dir = _exe.substr(0, _pos); - - auto _script_dir = - rocprofsys::common::join("/", _dir, "..", "libexec", "rocprofiler-systems"); - - return _script_dir; + auto _root = get_rocprofsys_root(); + return rocprofsys::common::join("/", _root, "libexec", "rocprofiler-systems"); } } // namespace @@ -239,6 +245,9 @@ init_parser(parser_data& _data) auto _libexecpath = get_realpath(get_internal_script_path()); update_env(_data, "ROCPROFSYS_SCRIPT_PATH", _libexecpath, UPD_REPLACE); + auto _rootpath = get_realpath(get_rocprofsys_root()); + update_env(_data, "ROCPROFSYS_ROOT", _rootpath, UPD_REPLACE); + return _data; }