Skip to content

Commit 5849cba

Browse files
authored
[libc] Add line numbers to libc utility error messages (#94010)
Summary: Currently we just print the error as seen, this makes it difficult if something goes wrong to know where it failed. This patch just adds in line numbers to all the error handling routines so you can trace it back.
1 parent fc21387 commit 5849cba

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

libc/utils/gpu/loader/Loader.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#include "utils/gpu/server/llvmlibc_rpc_server.h"
1313

14-
#include "llvm-libc-types/rpc_opcodes_t.h"
1514
#include "include/llvm-libc-types/test_rpc_opcodes_t.h"
15+
#include "llvm-libc-types/rpc_opcodes_t.h"
1616

1717
#include <cstddef>
1818
#include <cstdint>
@@ -98,14 +98,17 @@ void *copy_environment(char **envp, Allocator alloc) {
9898
return copy_argument_vector(envc, envp, alloc);
9999
}
100100

101-
inline void handle_error(const char *msg) {
102-
fprintf(stderr, "%s\n", msg);
101+
inline void handle_error_impl(const char *file, int32_t line, const char *msg) {
102+
fprintf(stderr, "%s:%d:0: Error: %s\n", file, line, msg);
103103
exit(EXIT_FAILURE);
104104
}
105105

106-
inline void handle_error(rpc_status_t) {
107-
handle_error("Failure in the RPC server\n");
106+
inline void handle_error_impl(const char *file, int32_t line,
107+
rpc_status_t err) {
108+
fprintf(stderr, "%s:%d:0: Error: %d\n", file, line, err);
109+
exit(EXIT_FAILURE);
108110
}
111+
#define handle_error(X) handle_error_impl(__FILE__, __LINE__, X)
109112

110113
template <uint32_t lane_size>
111114
inline void register_rpc_callbacks(rpc_device_t device) {

libc/utils/gpu/loader/amdgpu/Loader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ struct implicit_args_t {
4848
};
4949

5050
/// Print the error code and exit if \p code indicates an error.
51-
static void handle_error(hsa_status_t code) {
51+
static void handle_error_impl(const char *file, int32_t line,
52+
hsa_status_t code) {
5253
if (code == HSA_STATUS_SUCCESS || code == HSA_STATUS_INFO_BREAK)
5354
return;
5455

5556
const char *desc;
5657
if (hsa_status_string(code, &desc) != HSA_STATUS_SUCCESS)
5758
desc = "Unknown error";
58-
fprintf(stderr, "%s\n", desc);
59+
fprintf(stderr, "%s:%d:0: Error: %s\n", file, line, desc);
5960
exit(EXIT_FAILURE);
6061
}
6162

libc/utils/gpu/loader/nvptx/Loader.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
using namespace llvm;
3030
using namespace object;
3131

32-
static void handle_error(CUresult err) {
32+
static void handle_error_impl(const char *file, int32_t line, CUresult err) {
3333
if (err == CUDA_SUCCESS)
3434
return;
3535

3636
const char *err_str = nullptr;
3737
CUresult result = cuGetErrorString(err, &err_str);
3838
if (result != CUDA_SUCCESS)
39-
fprintf(stderr, "Unknown Error\n");
39+
fprintf(stderr, "%s:%d:0: Unknown Error\n", file, line);
4040
else
41-
fprintf(stderr, "%s\n", err_str);
41+
fprintf(stderr, "%s:%d:0: Error: %s\n", file, line, err_str);
4242
exit(1);
4343
}
4444

0 commit comments

Comments
 (0)