Skip to content

Commit 42975b4

Browse files
authored
[k2] add backtrace in exception (#1336)
1 parent 3d770a3 commit 42975b4

3 files changed

Lines changed: 35 additions & 9 deletions

File tree

runtime-light/state/instance-state.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void InstanceState::init_script_execution() noexcept {
7979
[](kphp::coro::task<> script_task) noexcept -> kphp::coro::task<> {
8080
co_await script_task;
8181
if (auto exception{std::move(ForkInstanceState::get().current_info().get().thrown_exception)}; !exception.is_null()) [[unlikely]] {
82-
kphp::log::error("unhandled exception '{}' at {}:{}", exception.get_class(), exception.get()->$file.c_str(), exception.get()->$line);
82+
kphp::log::error("unhandled exception {}", std::move(exception));
8383
}
8484
},
8585
std::move(script_task));

runtime-light/stdlib/diagnostics/error-handling-functions.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,32 @@
66

77
#include <array>
88
#include <cstddef>
9+
#include <cstdint>
910
#include <format>
11+
#include <span>
12+
#include <utility>
1013

1114
#include "runtime-common/core/runtime-core.h"
1215
#include "runtime-light/k2-platform/k2-api.h"
1316
#include "runtime-light/stdlib/diagnostics/backtrace.h"
1417
#include "runtime-light/utils/logs.h"
1518

1619
namespace error_handling_impl_ {
20+
inline constexpr std::string_view FUNCTION_KEY = "function";
21+
inline constexpr std::string_view FILENAME_KEY = "file";
22+
inline constexpr std::string_view LINE_KEY = "line";
1723

1824
inline array<array<string>> format_backtrace_symbols(std::span<void* const> backtrace) noexcept {
25+
1926
auto resolved_backtrace{kphp::diagnostic::backtrace_symbols(backtrace)};
2027
if (resolved_backtrace.empty()) {
2128
return {};
2229
}
2330

2431
array<array<string>> backtrace_symbols{array_size{static_cast<int64_t>(backtrace.size()), true}};
25-
const string function_key{"function"};
26-
const string filename_key{"file"};
27-
const string line_key{"line"};
32+
const string function_key{FUNCTION_KEY.data(), FUNCTION_KEY.size()};
33+
const string filename_key{FILENAME_KEY.data(), FILENAME_KEY.size()};
34+
const string line_key{LINE_KEY.data(), LINE_KEY.size()};
2835

2936
for (const k2::SymbolInfo& symbol_info : resolved_backtrace) {
3037
array<string> frame_info{array_size{3, false}};
@@ -47,11 +54,12 @@ inline array<array<string>> format_backtrace_addresses(std::span<void* const> ba
4754
}
4855

4956
array<array<string>> backtrace_addresses{array_size{static_cast<int64_t>(backtrace.size()), true}};
50-
const string function_key{"function"};
57+
const string function_key{FUNCTION_KEY.data(), FUNCTION_KEY.size()};
5158

5259
for (const auto& address : resolved_backtrace) {
5360
std::array<char, LOG_BUFFER_SIZE> log_buffer{};
54-
const auto [_, recorded]{std::format_to_n(log_buffer.data(), log_buffer.size() - 1, "{}", address)};
61+
const auto [out, recorded]{std::format_to_n(log_buffer.data(), log_buffer.size() - 1, "{}", address)};
62+
*out = '\0';
5563
array<string> frame_info{array_size{1, false}};
5664
frame_info.set_value(function_key, string{log_buffer.data(), static_cast<string::size_type>(recorded)});
5765

runtime-light/stdlib/diagnostics/exception-types.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44

55
#pragma once
66

7+
#include <array>
78
#include <cstddef>
89
#include <cstdint>
910
#include <cstring>
11+
#include <format>
12+
#include <span>
1013
#include <string_view>
14+
#include <utility>
1115

1216
#include "common/algorithms/hashes.h"
1317
#include "runtime-common/core/class-instance/refcountable-php-classes.h"
1418
#include "runtime-common/core/runtime-core.h"
1519
#include "runtime-common/stdlib/visitors/memory-visitors.h"
20+
#include "runtime-light/stdlib/diagnostics/backtrace.h"
21+
#include "runtime-light/stdlib/diagnostics/error-handling-functions.h"
1622
#include "runtime-light/stdlib/visitors/array-visitors.h"
1723
#include "runtime-light/utils/logs.h"
1824

@@ -71,12 +77,24 @@ struct C$Throwable : public refcountable_polymorphic_php_classes_virt<> {
7177

7278
using Throwable = class_instance<C$Throwable>;
7379

80+
template<>
81+
struct std::formatter<Throwable> {
82+
template<typename ParseContext>
83+
constexpr auto parse(ParseContext& ctx) const noexcept {
84+
return ctx.begin();
85+
}
86+
87+
template<typename FmtContext>
88+
auto format(const Throwable& e, FmtContext& ctx) const noexcept {
89+
format_to(ctx.out(), "'{}' at {}:{}", e->$message.c_str(), e->$file.c_str(), e->$line);
90+
return ctx.out();
91+
}
92+
};
93+
7494
// ================================================================================================
7595

7696
namespace exception_impl_ {
7797

78-
inline constexpr int32_t backtrace_size_limit = 64;
79-
8098
inline string exception_trace_as_string(const Throwable& e) noexcept {
8199
auto& static_SB{RuntimeContext::get().static_SB.clean()};
82100
for (int64_t i = 0; i < e->trace.count(); ++i) {
@@ -89,7 +107,7 @@ inline string exception_trace_as_string(const Throwable& e) noexcept {
89107
inline void exception_initialize(const Throwable& e, const string& message, int64_t code) noexcept {
90108
e->$message = message;
91109
e->$code = code;
92-
kphp::log::warning("exception backtrace is not yet supported"); // TODO
110+
e->trace = f$debug_backtrace();
93111
}
94112

95113
} // namespace exception_impl_

0 commit comments

Comments
 (0)