Skip to content

Commit a305221

Browse files
committed
Review comments.
Signed-off-by: fruffy <fruffy@nyu.edu>
1 parent 6b18259 commit a305221

File tree

3 files changed

+38
-39
lines changed

3 files changed

+38
-39
lines changed

lib/alloc_trace.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ std::ostream &operator<<(std::ostream &out, const AllocTrace &at) {
7575
std::sort(sorted.begin(), sorted.end(), [](auto &a, auto &b) { return a.first > b.first; });
7676
#if HAVE_LIBBACKTRACE
7777
if (!global_backtrace_state) {
78-
// TODO: Do not use cstring here?
79-
global_backtrace_state =
80-
backtrace_create_state(cstring(getExecutablePath().c_str()).c_str(), 1, bt_error, &out);
78+
// TODO: Do not use strdup here?
79+
auto executablePath = getExecutablePath();
80+
if (executablePath.empty()) {
81+
executablePath = "<unknown_path>";
82+
}
83+
const char *executableChar = strdup(executablePath.c_str());
84+
global_backtrace_state = backtrace_create_state(executableChar, 1, bt_error, &out);
8185
}
8286
#endif
8387

lib/exename.cpp

+29-35
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,36 @@ limitations under the License.
1919
#include <array>
2020
#include <cstring>
2121
#include <filesystem>
22+
#include <system_error>
2223

2324
namespace P4 {
2425

25-
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
26-
#include <unistd.h>
27-
2826
#include <climits>
2927
#ifdef __APPLE__
28+
#include <unistd.h>
29+
3030
#include <mach-o/dyld.h>
31-
#endif
3231
#elif defined(_WIN32)
3332
#include <windows.h>
33+
#else
34+
#include <unistd.h>
3435
#endif
3536

3637
std::filesystem::path getExecutablePath(const std::filesystem::path &suggestedPath) {
37-
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
38+
#if defined(__APPLE__)
39+
std::array<char, PATH_MAX> buffer{};
40+
uint32_t size = static_cast<uint32_t>(buffer.size());
41+
if (_NSGetExecutablePath(buffer.data(), &size) == 0) {
42+
return buffer.data();
43+
}
44+
#elif defined(_WIN32)
45+
std::array<char, PATH_MAX> buffer{};
46+
// TODO: Do we need to support this?
47+
DWORD size = GetModuleFileNameA(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
48+
if (size > 0 && size < buffer.size()) {
49+
return buffer.data();
50+
}
51+
#else
3852
// Find the path of the executable. We use a number of techniques that may fail or work on
3953
// different systems, and take the first working one we find. Fallback to not overriding the
4054
// compiled-in installation path.
@@ -46,40 +60,20 @@ std::filesystem::path getExecutablePath(const std::filesystem::path &suggestedPa
4660
};
4761

4862
for (const auto &path : paths) {
49-
try {
50-
// std::filesystem::canonical will throw on error if the path is invalid.
51-
// It will also try to resolve symlinks.
52-
return std::filesystem::canonical(path);
53-
} catch (const std::filesystem::filesystem_error &) {
54-
// Ignore and try the next path.
63+
// std::filesystem::canonical will throw on error if the path is invalid.
64+
// It will also try to resolve symlinks.
65+
std::error_code errorCode;
66+
auto canonicalPath = std::filesystem::canonical(path, errorCode);
67+
// Return the path if no error was thrown.
68+
if (!errorCode) {
69+
return canonicalPath;
5570
}
5671
}
57-
#elif defined(__APPLE__)
58-
std::array<char, PATH_MAX> buffer{};
59-
uint32_t size = static_cast<uint32_t>(buffer.size());
60-
if (_NSGetExecutablePath(buffer.data(), &size) == 0) {
61-
return buffer.data();
62-
} else
63-
#elif defined(_WIN32)
64-
std::array<char, PATH_MAX> buffer{};
65-
// TODO: Do we need to support this?
66-
DWORD size = GetModuleFileNameA(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
67-
if (size > 0 && size < buffer.size()) {
68-
return buffer.data();
69-
} else
7072
#endif
71-
if (auto *path = getenv("_")) {
72-
return path;
73-
}
7473
// If the above fails, try to convert suggestedPath to a path.
75-
try {
76-
// std::filesystem::canonical will throw on error if the path is invalid.
77-
// It will also try to resolve symlinks.
78-
return std::filesystem::canonical(suggestedPath);
79-
} catch (const std::filesystem::filesystem_error &) {
80-
// In the case of an error, return an empty path.
81-
return {};
82-
}
74+
std::error_code errorCode;
75+
auto canonicalPath = std::filesystem::canonical(suggestedPath, errorCode);
76+
return errorCode ? std::filesystem::path() : canonicalPath;
8377
}
8478

8579
const char *exename(const char *argv0) {

lib/exename.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ limitations under the License.
2121
namespace P4 {
2222

2323
/// Return the full path to the binary being executed.
24-
/// @returns std::nullopt if unable to determine the path.
24+
/// @returns an empty path if unable to determine the path.
25+
/// TODO: With C++20 replace with std::expected?
2526
std::filesystem::path getExecutablePath(const std::filesystem::path &suggestedPath = "");
2627

2728
/// Attempt to determine the executable name and return a static path to it.

0 commit comments

Comments
 (0)