From 8256861a58be90072d2c06f8c8204d0c053e522d Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Wed, 22 Apr 2026 11:57:51 +0300 Subject: [PATCH 1/8] add ppid, cr2, regs --- common/server/crash-dump.cpp | 12 +++--------- common/server/crash-dump.h | 13 +++++++++++++ server/json-logger.cpp | 21 ++++++++++++++++++++- server/php-engine-vars.cpp | 1 + server/php-engine-vars.h | 1 + server/php-engine.cpp | 1 + server/php-master.cpp | 1 + 7 files changed, 40 insertions(+), 10 deletions(-) diff --git a/common/server/crash-dump.cpp b/common/server/crash-dump.cpp index 176ee93649..e1a7a5c97d 100644 --- a/common/server/crash-dump.cpp +++ b/common/server/crash-dump.cpp @@ -14,12 +14,6 @@ #include "common/ucontext/ucontext-portable.h" #include -struct crash_dump_buffer { - char scratchpad[1024]; - size_t position; -}; -using crash_dump_buffer_t = struct crash_dump_buffer; - static inline char crash_dump_half_byte_char(uint8_t hb) { if (hb <= 9) { return '0' + hb; @@ -50,7 +44,7 @@ static inline void crash_dump_write_uint64(uint64_t value, crash_dump_buffer_t* crash_dump_write_uint32(static_cast(value & 0xFFFFFFFF), buffer); } -[[maybe_unused]] static inline void crash_dump_write_reg(const char* reg_name, size_t reg_name_size, uint64_t reg_value, crash_dump_buffer_t* buffer) { +[[maybe_unused]] void crash_dump_write_reg(const char* reg_name, size_t reg_name_size, uint64_t reg_value, crash_dump_buffer_t* buffer) { assert(reg_name_size + buffer->position <= sizeof(buffer->scratchpad)); memcpy(&buffer->scratchpad[buffer->position], reg_name, reg_name_size); buffer->position += reg_name_size; @@ -67,7 +61,7 @@ static inline void crash_dump_write_uint64(uint64_t value, crash_dump_buffer_t* // Keep in mind that: // * `ucontext_t_portable` -- using for more efficient user context manipulations (e.g. `swapcontext`, `getcontext`, `setcontext`, etc) // * `ucontext_t` -- using in signal handlers for machine state extracting in debug purposes. -static inline void crash_dump_prepare_registers([[maybe_unused]] crash_dump_buffer_t* buffer, [[maybe_unused]] void* ucontext) { +void crash_dump_prepare_registers([[maybe_unused]] crash_dump_buffer_t* buffer, [[maybe_unused]] void* ucontext) { #ifdef __x86_64__ #ifdef __APPLE__ const auto* uc = static_cast(ucontext); @@ -130,7 +124,7 @@ void crash_dump_write(void* ucontext) { kwrite(STDERR_FILENO, header, sizeof(header) - 1); static crash_dump_buffer_t buffer; - buffer.position = 0; + buffer.reset(); crash_dump_prepare_registers(&buffer, ucontext); assert(buffer.position < sizeof(buffer.scratchpad)); diff --git a/common/server/crash-dump.h b/common/server/crash-dump.h index 53c1bf3d13..3e3b1a5a4e 100644 --- a/common/server/crash-dump.h +++ b/common/server/crash-dump.h @@ -3,5 +3,18 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #pragma once +#include +#include +struct crash_dump_buffer_t { + char scratchpad[1024]{}; + size_t position{0}; + + void reset() { + position = 0; + } +}; + +void crash_dump_write_reg(const char* reg_name, size_t reg_name_size, uint64_t reg_value, crash_dump_buffer_t* buffer); +void crash_dump_prepare_registers(crash_dump_buffer_t* buffer, void* ucontext); void crash_dump_write(void* ucontext); diff --git a/server/json-logger.cpp b/server/json-logger.cpp index 1459c0e539..89fcb95be2 100644 --- a/server/json-logger.cpp +++ b/server/json-logger.cpp @@ -6,10 +6,13 @@ #include #include #include +#include #include +#include #include "common/algorithms/find.h" #include "common/fast-backtrace.h" +#include "common/server/crash-dump.h" #include "common/wrappers/likely.h" #include "common/ucontext/ucontext-portable.h" #include "runtime/kphp-backtrace.h" @@ -348,12 +351,28 @@ void JsonLogger::write_general_info(JsonBuffer *json_out_it, int type, int64_t c json_out_it->append_key("logname_id").append_integer(logname_id); } json_out_it->append_key("pid").append_integer(pid); + json_out_it->append_key("ppid").append_integer(ppid); json_out_it->append_key("cluster").append_string(vk::singleton::get().get_cluster_name()); json_out_it->append_raw(uncaught ? R"json("uncaught":true)json" : R"json("uncaught":false)json"); json_out_it->finish<'}'>(); if (extra_info_available_) { - json_out_it->append_key("extra_info").start<'{'>().append_raw(extra_info_).finish<'}'>(); + json_out_it->append_key("extra_info").start<'{'>().append_raw(extra_info_); + if (ucontext_t ucp{}; getcontext(std::addressof(ucp)) != -1) { + #define LITERAL_WITH_LENGTH(literal) literal, sizeof(literal) - 1 + crash_dump_buffer_t buffer{}; + + crash_dump_write_reg(LITERAL_WITH_LENGTH("0x"), ucp.uc_mcontext.gregs[REG_CR2], std::addressof(buffer)); + assert(buffer.position < sizeof(buffer.scratchpad)); + json_out_it->append_key("CR2 register").append_string(std::string_view{buffer.scratchpad, buffer.position}); + + buffer.reset(); + + crash_dump_prepare_registers(std::addressof(buffer), std::addressof(ucp)); + assert(buffer.position < sizeof(buffer.scratchpad)); + json_out_it->append_key("registers").append_string(std::string_view{buffer.scratchpad, buffer.position}); + } + json_out_it->finish<'}'>(); } } diff --git a/server/php-engine-vars.cpp b/server/php-engine-vars.cpp index 6ee5eb9bc1..3afe969565 100644 --- a/server/php-engine-vars.cpp +++ b/server/php-engine-vars.cpp @@ -22,6 +22,7 @@ double oom_handling_memory_ratio = 0.00; int worker_id = -1; int pid = -1; +int ppid = -1; int master_pid = -1; ProcessType process_type = ProcessType::master; diff --git a/server/php-engine-vars.h b/server/php-engine-vars.h index f194e74c7a..6111ad7cd8 100644 --- a/server/php-engine-vars.h +++ b/server/php-engine-vars.h @@ -32,6 +32,7 @@ extern double oom_handling_memory_ratio; extern int worker_id; extern int pid; +extern int ppid; extern int master_pid; extern ProcessType process_type; diff --git a/server/php-engine.cpp b/server/php-engine.cpp index 16e18646e0..ab6221f29f 100644 --- a/server/php-engine.cpp +++ b/server/php-engine.cpp @@ -2381,6 +2381,7 @@ void init_default() { now = (int)time(nullptr); pid = getpid(); + ppid = getppid(); master_pid = getpid(); // RPC part PID.port = -1; // TODO: get rid of this? diff --git a/server/php-master.cpp b/server/php-master.cpp index e0df4879da..6b50a183f0 100644 --- a/server/php-master.cpp +++ b/server/php-master.cpp @@ -638,6 +638,7 @@ int run_worker(WorkerType worker_type) { //verbosity = 0; verbosity = initial_verbosity; pid = getpid(); + ppid = getppid(); master_sfd = -1; if (worker_type == WorkerType::job_worker) { From 4213e51506457d230ae8daf672e61d125a885e22 Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Wed, 22 Apr 2026 12:03:38 +0300 Subject: [PATCH 2/8] fmt --- common/server/crash-dump.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/server/crash-dump.h b/common/server/crash-dump.h index 3e3b1a5a4e..ef3562614f 100644 --- a/common/server/crash-dump.h +++ b/common/server/crash-dump.h @@ -3,8 +3,8 @@ // Distributed under the GPL v3 License, see LICENSE.notice.txt #pragma once -#include #include +#include struct crash_dump_buffer_t { char scratchpad[1024]{}; From 4a79957ff356a3b17fb2d054974f9bb01f4eb77e Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Wed, 22 Apr 2026 12:29:42 +0300 Subject: [PATCH 3/8] fix for apple devices --- server/json-logger.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/json-logger.cpp b/server/json-logger.cpp index 89fcb95be2..10aad44cd5 100644 --- a/server/json-logger.cpp +++ b/server/json-logger.cpp @@ -362,11 +362,13 @@ void JsonLogger::write_general_info(JsonBuffer *json_out_it, int type, int64_t c #define LITERAL_WITH_LENGTH(literal) literal, sizeof(literal) - 1 crash_dump_buffer_t buffer{}; +#if defined(__x86_64__) && !defined(__APPLE__) crash_dump_write_reg(LITERAL_WITH_LENGTH("0x"), ucp.uc_mcontext.gregs[REG_CR2], std::addressof(buffer)); assert(buffer.position < sizeof(buffer.scratchpad)); json_out_it->append_key("CR2 register").append_string(std::string_view{buffer.scratchpad, buffer.position}); buffer.reset(); +#endif crash_dump_prepare_registers(std::addressof(buffer), std::addressof(ucp)); assert(buffer.position < sizeof(buffer.scratchpad)); From ada1851de0f2045e254de8c493e862d04065a818 Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Wed, 22 Apr 2026 14:53:40 +0300 Subject: [PATCH 4/8] fix --- common/server/crash-dump.cpp | 6 ++---- common/server/crash-dump.h | 3 ++- server/json-logger.cpp | 13 +++++++------ server/json-logger.h | 4 ++-- server/signal-handlers.cpp | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/common/server/crash-dump.cpp b/common/server/crash-dump.cpp index e1a7a5c97d..5d8bc4e0e0 100644 --- a/common/server/crash-dump.cpp +++ b/common/server/crash-dump.cpp @@ -61,7 +61,7 @@ static inline void crash_dump_write_uint64(uint64_t value, crash_dump_buffer_t* // Keep in mind that: // * `ucontext_t_portable` -- using for more efficient user context manipulations (e.g. `swapcontext`, `getcontext`, `setcontext`, etc) // * `ucontext_t` -- using in signal handlers for machine state extracting in debug purposes. -void crash_dump_prepare_registers([[maybe_unused]] crash_dump_buffer_t* buffer, [[maybe_unused]] void* ucontext) { +void crash_dump_prepare_registers([[maybe_unused]] crash_dump_buffer_t* buffer, [[maybe_unused]] const ucontext_t* uc) { #ifdef __x86_64__ #ifdef __APPLE__ const auto* uc = static_cast(ucontext); @@ -87,8 +87,6 @@ void crash_dump_prepare_registers([[maybe_unused]] crash_dump_buffer_t* buffer, crash_dump_write_reg(LITERAL_WITH_LENGTH("R14=0x"), uc->uc_mcontext->__ss.__r14, buffer); crash_dump_write_reg(LITERAL_WITH_LENGTH("R15=0x"), uc->uc_mcontext->__ss.__r15, buffer); #else - const auto* uc = static_cast(ucontext); - crash_dump_write_reg(LITERAL_WITH_LENGTH("RIP=0x"), uc->uc_mcontext.gregs[REG_RIP], buffer); crash_dump_write_reg(LITERAL_WITH_LENGTH("RSP=0x"), uc->uc_mcontext.gregs[REG_RSP], buffer); crash_dump_write_reg(LITERAL_WITH_LENGTH("RBP=0x"), uc->uc_mcontext.gregs[REG_RBP], buffer); @@ -125,7 +123,7 @@ void crash_dump_write(void* ucontext) { static crash_dump_buffer_t buffer; buffer.reset(); - crash_dump_prepare_registers(&buffer, ucontext); + crash_dump_prepare_registers(&buffer, static_cast(ucontext)); assert(buffer.position < sizeof(buffer.scratchpad)); buffer.scratchpad[buffer.position++] = '\n'; diff --git a/common/server/crash-dump.h b/common/server/crash-dump.h index ef3562614f..9afb40e148 100644 --- a/common/server/crash-dump.h +++ b/common/server/crash-dump.h @@ -5,6 +5,7 @@ #pragma once #include #include +#include struct crash_dump_buffer_t { char scratchpad[1024]{}; @@ -16,5 +17,5 @@ struct crash_dump_buffer_t { }; void crash_dump_write_reg(const char* reg_name, size_t reg_name_size, uint64_t reg_value, crash_dump_buffer_t* buffer); -void crash_dump_prepare_registers(crash_dump_buffer_t* buffer, void* ucontext); +void crash_dump_prepare_registers(crash_dump_buffer_t* buffer, const ucontext_t* uc); void crash_dump_write(void* ucontext); diff --git a/server/json-logger.cpp b/server/json-logger.cpp index 10aad44cd5..2108772fc9 100644 --- a/server/json-logger.cpp +++ b/server/json-logger.cpp @@ -262,7 +262,7 @@ void JsonLogger::write_log_with_demangled_backtrace(vk::string_view message,int } void JsonLogger::write_log(vk::string_view message, int type, int64_t created_at, - void *const *trace, int64_t trace_size, bool uncaught) noexcept { + void *const *trace, int64_t trace_size, bool uncaught, void* ucontext) noexcept { if (json_log_fd_ <= 0) { return; } @@ -272,7 +272,7 @@ void JsonLogger::write_log(vk::string_view message, int type, int64_t created_at } assert(json_out_it != buffers_.end()); - write_general_info(json_out_it, type, created_at, uncaught); + write_general_info(json_out_it, type, created_at, uncaught, ucontext); json_out_it->append_key("trace").start<'['>(); for (int64_t i = 0; i < trace_size; i++) { @@ -327,7 +327,7 @@ void JsonLogger::reset_buffers() noexcept { } } -void JsonLogger::write_general_info(JsonBuffer *json_out_it, int type, int64_t created_at, bool uncaught) { +void JsonLogger::write_general_info(JsonBuffer *json_out_it, int type, int64_t created_at, bool uncaught, void* ucontext) { json_out_it->append_key("version").append_integer(release_version_); json_out_it->append_key("hostname").append_string(hostname_); json_out_it->append_key("type").append_integer(type); @@ -358,19 +358,20 @@ void JsonLogger::write_general_info(JsonBuffer *json_out_it, int type, int64_t c if (extra_info_available_) { json_out_it->append_key("extra_info").start<'{'>().append_raw(extra_info_); - if (ucontext_t ucp{}; getcontext(std::addressof(ucp)) != -1) { + if (ucontext != nullptr) { #define LITERAL_WITH_LENGTH(literal) literal, sizeof(literal) - 1 + const auto* ucp = static_cast(ucontext); crash_dump_buffer_t buffer{}; #if defined(__x86_64__) && !defined(__APPLE__) - crash_dump_write_reg(LITERAL_WITH_LENGTH("0x"), ucp.uc_mcontext.gregs[REG_CR2], std::addressof(buffer)); + crash_dump_write_reg(LITERAL_WITH_LENGTH("0x"), ucp->uc_mcontext.gregs[REG_CR2], std::addressof(buffer)); assert(buffer.position < sizeof(buffer.scratchpad)); json_out_it->append_key("CR2 register").append_string(std::string_view{buffer.scratchpad, buffer.position}); buffer.reset(); #endif - crash_dump_prepare_registers(std::addressof(buffer), std::addressof(ucp)); + crash_dump_prepare_registers(std::addressof(buffer), ucp); assert(buffer.position < sizeof(buffer.scratchpad)); json_out_it->append_key("registers").append_string(std::string_view{buffer.scratchpad, buffer.position}); } diff --git a/server/json-logger.h b/server/json-logger.h index da56fc0883..305e87417f 100644 --- a/server/json-logger.h +++ b/server/json-logger.h @@ -47,7 +47,7 @@ class JsonLogger : vk::not_copyable { // ATTENTION: this functions are used in signal handlers, therefore they are expected to be safe for them // Details: https://man7.org/linux/man-pages/man7/signal-safety.7.html // todo: functions bellow use backtrace which isn't async-signal safety - void write_log(vk::string_view message, int type, int64_t created_at, void *const *trace, int64_t trace_size, bool uncaught) noexcept; + void write_log(vk::string_view message, int type, int64_t created_at, void *const *trace, int64_t trace_size, bool uncaught, void* ucontext = nullptr) noexcept; void write_log_with_backtrace(vk::string_view message, int type) noexcept; void write_log_with_script_backtrace(vk::string_view message, int type) noexcept; @@ -112,6 +112,6 @@ class JsonLogger : vk::not_copyable { }; std::array buffers_; - void write_general_info(JsonBuffer * json_out_it, int type, int64_t created_at, bool uncaught); + void write_general_info(JsonBuffer * json_out_it, int type, int64_t created_at, bool uncaught, void* ucontext = nullptr); }; diff --git a/server/signal-handlers.cpp b/server/signal-handlers.cpp index 422010eb49..7bfb3fcfd3 100644 --- a/server/signal-handlers.cpp +++ b/server/signal-handlers.cpp @@ -195,7 +195,7 @@ void sigsegv_handler(int signum, siginfo_t *info, void *ucontext) { } } else { const char *msg = signum == SIGBUS ? "SIGBUS terminating program" : "SIGSEGV terminating program"; - vk::singleton::get().write_log(msg, static_cast(ServerLog::Critical), cur_time, trace, trace_size, true); + vk::singleton::get().write_log(msg, static_cast(ServerLog::Critical), cur_time, trace, trace_size, true, ucontext); vk::singleton::get().fsync_log_file(); write_str(2, "Error -2: Segmentation fault\n"); print_http_data(); @@ -206,7 +206,7 @@ void sigsegv_handler(int signum, siginfo_t *info, void *ucontext) { } } -void sigabrt_handler(int, siginfo_t *info, void *) { +void sigabrt_handler(int, siginfo_t *info, void *ucontext) { const int64_t cur_time = time(nullptr); void *trace[64]; const int trace_size = backtrace(trace, 64); @@ -214,7 +214,7 @@ void sigabrt_handler(int, siginfo_t *info, void *) { if (msg.empty()) { msg = "SIGABRT terminating program"; } - vk::singleton::get().write_log(msg, static_cast(ServerLog::Critical), cur_time, trace, trace_size, true); + vk::singleton::get().write_log(msg, static_cast(ServerLog::Critical), cur_time, trace, trace_size, true, ucontext); vk::singleton::get().fsync_log_file(); print_prologue(cur_time); From 0831fcb3b5599c2a8d543dc17832c6062eaed291 Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Wed, 22 Apr 2026 16:13:41 +0300 Subject: [PATCH 5/8] fix python tests --- tests/python/lib/kphp_server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/python/lib/kphp_server.py b/tests/python/lib/kphp_server.py index 113f70a571..902c52c89d 100644 --- a/tests/python/lib/kphp_server.py +++ b/tests/python/lib/kphp_server.py @@ -85,6 +85,7 @@ def _process_json_log(self, log_record): del log_record["tags"]["logname_id"] del log_record["tags"]["process_type"] del log_record["tags"]["pid"] + del log_record["tags"]["ppid"] if not got_tags.get("cluster", ""): raise RuntimeError("Got an empty cluster in json log: {}".format(got_tags)) del log_record["tags"]["cluster"] From 12e50feb5ac07b5967e7b01dc221e6816846f93e Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Wed, 22 Apr 2026 17:40:14 +0300 Subject: [PATCH 6/8] fixes --- common/server/crash-dump.h | 5 +++++ server/json-logger.cpp | 10 ++++------ tests/python/lib/kphp_server.py | 5 +++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/common/server/crash-dump.h b/common/server/crash-dump.h index 9afb40e148..e6fd88e829 100644 --- a/common/server/crash-dump.h +++ b/common/server/crash-dump.h @@ -5,6 +5,7 @@ #pragma once #include #include +#include #include struct crash_dump_buffer_t { @@ -14,6 +15,10 @@ struct crash_dump_buffer_t { void reset() { position = 0; } + + std::string_view get_content() const noexcept { + return std::string_view{scratchpad, position}; + } }; void crash_dump_write_reg(const char* reg_name, size_t reg_name_size, uint64_t reg_value, crash_dump_buffer_t* buffer); diff --git a/server/json-logger.cpp b/server/json-logger.cpp index 2108772fc9..955c91cd98 100644 --- a/server/json-logger.cpp +++ b/server/json-logger.cpp @@ -359,21 +359,19 @@ void JsonLogger::write_general_info(JsonBuffer *json_out_it, int type, int64_t c if (extra_info_available_) { json_out_it->append_key("extra_info").start<'{'>().append_raw(extra_info_); if (ucontext != nullptr) { - #define LITERAL_WITH_LENGTH(literal) literal, sizeof(literal) - 1 + static constexpr std::string_view hex_prefix{"0x"}; const auto* ucp = static_cast(ucontext); crash_dump_buffer_t buffer{}; #if defined(__x86_64__) && !defined(__APPLE__) - crash_dump_write_reg(LITERAL_WITH_LENGTH("0x"), ucp->uc_mcontext.gregs[REG_CR2], std::addressof(buffer)); - assert(buffer.position < sizeof(buffer.scratchpad)); - json_out_it->append_key("CR2 register").append_string(std::string_view{buffer.scratchpad, buffer.position}); + crash_dump_write_reg(hex_prefix.data(), hex_prefix.size(), ucp->uc_mcontext.gregs[REG_CR2], std::addressof(buffer)); + json_out_it->append_key("CR2 register").append_string(buffer.get_content()); buffer.reset(); #endif crash_dump_prepare_registers(std::addressof(buffer), ucp); - assert(buffer.position < sizeof(buffer.scratchpad)); - json_out_it->append_key("registers").append_string(std::string_view{buffer.scratchpad, buffer.position}); + json_out_it->append_key("registers").append_string(buffer.get_content()); } json_out_it->finish<'}'>(); } diff --git a/tests/python/lib/kphp_server.py b/tests/python/lib/kphp_server.py index 902c52c89d..d91182dbb1 100644 --- a/tests/python/lib/kphp_server.py +++ b/tests/python/lib/kphp_server.py @@ -89,4 +89,9 @@ def _process_json_log(self, log_record): if not got_tags.get("cluster", ""): raise RuntimeError("Got an empty cluster in json log: {}".format(got_tags)) del log_record["tags"]["cluster"] + + if log_record.get("extra_info"): + log_record["extra_info"].pop("CR2 register", None) + log_record["extra_info"].pop("registers", None) + return log_record From 7298b549758ff40f0018fc0661fe1aca588ea030 Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Wed, 22 Apr 2026 17:47:59 +0300 Subject: [PATCH 7/8] fix --- common/server/crash-dump.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/server/crash-dump.h b/common/server/crash-dump.h index e6fd88e829..3c83781277 100644 --- a/common/server/crash-dump.h +++ b/common/server/crash-dump.h @@ -8,8 +8,8 @@ #include #include -struct crash_dump_buffer_t { - char scratchpad[1024]{}; +struct crash_dump_buffer_t { // NOLINT + char scratchpad[1024]; size_t position{0}; void reset() { From 64c0a49dca5510145731be0951cd83440c3ba503 Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Wed, 22 Apr 2026 17:57:59 +0300 Subject: [PATCH 8/8] fix --- common/server/crash-dump.cpp | 4 ++-- common/server/crash-dump.h | 5 ----- server/json-logger.cpp | 8 ++------ 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/common/server/crash-dump.cpp b/common/server/crash-dump.cpp index 5d8bc4e0e0..0a714ce1cc 100644 --- a/common/server/crash-dump.cpp +++ b/common/server/crash-dump.cpp @@ -44,7 +44,7 @@ static inline void crash_dump_write_uint64(uint64_t value, crash_dump_buffer_t* crash_dump_write_uint32(static_cast(value & 0xFFFFFFFF), buffer); } -[[maybe_unused]] void crash_dump_write_reg(const char* reg_name, size_t reg_name_size, uint64_t reg_value, crash_dump_buffer_t* buffer) { +[[maybe_unused]] static inline void crash_dump_write_reg(const char* reg_name, size_t reg_name_size, uint64_t reg_value, crash_dump_buffer_t* buffer) { assert(reg_name_size + buffer->position <= sizeof(buffer->scratchpad)); memcpy(&buffer->scratchpad[buffer->position], reg_name, reg_name_size); buffer->position += reg_name_size; @@ -122,7 +122,7 @@ void crash_dump_write(void* ucontext) { kwrite(STDERR_FILENO, header, sizeof(header) - 1); static crash_dump_buffer_t buffer; - buffer.reset(); + buffer.position = 0; crash_dump_prepare_registers(&buffer, static_cast(ucontext)); assert(buffer.position < sizeof(buffer.scratchpad)); diff --git a/common/server/crash-dump.h b/common/server/crash-dump.h index 3c83781277..c67131525f 100644 --- a/common/server/crash-dump.h +++ b/common/server/crash-dump.h @@ -12,15 +12,10 @@ struct crash_dump_buffer_t { // NOLINT char scratchpad[1024]; size_t position{0}; - void reset() { - position = 0; - } - std::string_view get_content() const noexcept { return std::string_view{scratchpad, position}; } }; -void crash_dump_write_reg(const char* reg_name, size_t reg_name_size, uint64_t reg_value, crash_dump_buffer_t* buffer); void crash_dump_prepare_registers(crash_dump_buffer_t* buffer, const ucontext_t* uc); void crash_dump_write(void* ucontext); diff --git a/server/json-logger.cpp b/server/json-logger.cpp index 955c91cd98..8a6a170fad 100644 --- a/server/json-logger.cpp +++ b/server/json-logger.cpp @@ -359,17 +359,13 @@ void JsonLogger::write_general_info(JsonBuffer *json_out_it, int type, int64_t c if (extra_info_available_) { json_out_it->append_key("extra_info").start<'{'>().append_raw(extra_info_); if (ucontext != nullptr) { - static constexpr std::string_view hex_prefix{"0x"}; const auto* ucp = static_cast(ucontext); - crash_dump_buffer_t buffer{}; #if defined(__x86_64__) && !defined(__APPLE__) - crash_dump_write_reg(hex_prefix.data(), hex_prefix.size(), ucp->uc_mcontext.gregs[REG_CR2], std::addressof(buffer)); - json_out_it->append_key("CR2 register").append_string(buffer.get_content()); - - buffer.reset(); + json_out_it->append_key("CR2 register").append_hex_as_string(ucp->uc_mcontext.gregs[REG_CR2]); #endif + crash_dump_buffer_t buffer{}; crash_dump_prepare_registers(std::addressof(buffer), ucp); json_out_it->append_key("registers").append_string(buffer.get_content()); }