Skip to content

Commit 6ac7e25

Browse files
authored
[k2] add sync backtrace (#1340)
1 parent 8996205 commit 6ac7e25

4 files changed

Lines changed: 44 additions & 13 deletions

File tree

runtime-light/k2-platform/k2-api.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <ctime>
1111
#include <expected>
1212
#include <memory>
13+
#include <span>
1314
#include <string_view>
1415
#include <sys/utsname.h>
1516
#include <utility>
@@ -288,4 +289,8 @@ inline int32_t code_segment_offset(uint64_t* offset) noexcept {
288289
return k2_code_segment_offset(offset);
289290
}
290291

292+
inline size_t backtrace(std::span<void*> buffer) noexcept {
293+
return k2_backtrace(buffer.data(), buffer.size());
294+
}
295+
291296
} // namespace k2

runtime-light/k2-platform/k2-header.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ struct SymbolInfo {
483483
* Note: name and filename in symbol_info are **not** null-terminated.
484484
* @param `addr` pointer to code instruction
485485
* @param `symbol_info` structure stores buffers where the name and file name will be written. The buffer
486-
* lens must staisfy `name_len >= k2_symbol_name_len and filename_len >= k2_symbol_filename_len`
486+
* lens must satisfy `name_len >= k2_symbol_name_len and filename_len >= k2_symbol_filename_len`
487487
* `errno` options:
488488
* `EINVAL` => `addr` symbol can't be resolved
489489
* `ENODATA` => there is no debug information for the image
@@ -547,6 +547,13 @@ int32_t k2_iconv(size_t* result, void* iconv_cd, char** inbuf, size_t* inbytesle
547547
*/
548548
size_t k2_stderr_write(size_t data_len, const void* data);
549549

550+
/**
551+
* Analogue for libc backtrace. Returns a backtrace of the calling component
552+
*
553+
* @return backtrace size on success, `0` otherwise
554+
*/
555+
size_t k2_backtrace(void** buffer, size_t size);
556+
550557
#ifdef __cplusplus
551558
}
552559
#endif

runtime-light/stdlib/diagnostics/backtrace.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
// Distributed under the GPL v3 License, see LICENSE.notice.txt
44

55
#include <cstddef>
6-
#include <cstdint>
76
#include <iterator>
87
#include <span>
98

109
#include "runtime-light/coroutine/async-stack.h"
1110
#include "runtime-light/coroutine/coroutine-state.h"
11+
#include "runtime-light/k2-platform/k2-api.h"
12+
#include "runtime-light/state/instance-state.h"
1213
#include "runtime-light/stdlib/diagnostics/backtrace.h"
1314
#include "runtime-light/utils/logs.h"
1415

@@ -40,17 +41,19 @@ size_t async_frames(std::span<void*> addresses, kphp::coro::async_stack_frame* t
4041

4142
} // namespace
4243

43-
namespace kphp::diagnostic {
44+
namespace kphp::diagnostic::impl {
4445

45-
size_t backtrace(std::span<void*> addresses) noexcept {
46-
auto& async_stack_root{CoroutineInstanceState::get().coroutine_stack_root};
46+
size_t async_backtrace(std::span<void*> addresses) noexcept {
47+
if (const auto* instance_state{k2::instance_state()}; instance_state != nullptr) [[likely]] {
48+
const auto& async_stack_root{instance_state->coroutine_instance_state.coroutine_stack_root};
4749

48-
auto* const start_sync_frame{reinterpret_cast<kphp::coro::stack_frame*>(STACK_FRAME_ADDRESS)};
49-
auto* const stop_sync_frame{async_stack_root.stop_sync_stack_frame};
50+
auto* const start_sync_frame{reinterpret_cast<kphp::coro::stack_frame*>(STACK_FRAME_ADDRESS)};
51+
auto* const stop_sync_frame{async_stack_root.stop_sync_stack_frame};
5052

51-
const size_t num_sync_frames{sync_frames(addresses, start_sync_frame, stop_sync_frame)};
52-
const size_t num_async_frames{async_frames(addresses.subspan(num_sync_frames), async_stack_root.top_async_stack_frame)};
53-
return num_sync_frames + num_async_frames;
53+
const size_t num_sync_frames{sync_frames(addresses, start_sync_frame, stop_sync_frame)};
54+
const size_t num_async_frames{async_frames(addresses.subspan(num_sync_frames), async_stack_root.top_async_stack_frame)};
55+
return num_sync_frames + num_async_frames;
56+
}
57+
return 0;
5458
}
55-
56-
} // namespace kphp::diagnostic
59+
} // namespace kphp::diagnostic::impl

runtime-light/stdlib/diagnostics/backtrace.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,23 @@
1515

1616
namespace kphp::diagnostic {
1717

18-
size_t backtrace(std::span<void*> addresses) noexcept;
18+
namespace impl {
19+
20+
size_t async_backtrace(std::span<void*> addresses) noexcept;
21+
22+
inline size_t sync_backtrace(std::span<void*> addresses) noexcept {
23+
return k2::backtrace(addresses);
24+
}
25+
26+
} // namespace impl
27+
28+
inline size_t backtrace(std::span<void*> addresses) noexcept {
29+
size_t async_num_frames{impl::async_backtrace(addresses)};
30+
if (async_num_frames != 0) [[likely]] {
31+
return async_num_frames;
32+
}
33+
return impl::sync_backtrace(addresses);
34+
}
1935

2036
inline auto backtrace_addresses(std::span<void* const> addresses) noexcept {
2137
uint64_t code_segment_offset{};

0 commit comments

Comments
 (0)