assert() is traditionally disabled in release builds, but not in
scylladb. This hasn't caused problems so far, but the latest abseil
release includes a commit [1] that causes a 1000 insn/op regression when
NDEBUG is not defined.
Clearly, we must move towards a build system where NDEBUG is defined in
release builds. But we can't just define it blindly without vetting
all the assert() calls, as some were written with the expectation that
they are enabled in release mode.
To solve the conundrum, change all assert() calls to a new SCYLLA_ASSERT()
macro in utils/assert.hh. This macro is always defined and is not conditional
on NDEBUG, so we can later (after vetting Seastar) enable NDEBUG in release
mode.
[1] 66ef711d68
Closes scylladb/scylladb#20006
83 lines
1.9 KiB
C++
83 lines
1.9 KiB
C++
/*
|
|
* Copyright (C) 2019-present ScyllaDB
|
|
*/
|
|
|
|
#include "utils/assert.hh"
|
|
#include "build_id.hh"
|
|
#include <fmt/ostream.h>
|
|
#include <link.h>
|
|
#include <seastar/core/align.hh>
|
|
#include <sstream>
|
|
#include <cassert>
|
|
|
|
using namespace seastar;
|
|
|
|
static const Elf64_Nhdr* get_nt_build_id(dl_phdr_info* info) {
|
|
auto base = info->dlpi_addr;
|
|
const auto* h = info->dlpi_phdr;
|
|
auto num_headers = info->dlpi_phnum;
|
|
for (int i = 0; i != num_headers; ++i, ++h) {
|
|
if (h->p_type != PT_NOTE) {
|
|
continue;
|
|
}
|
|
|
|
auto* p = reinterpret_cast<const char*>(base + h->p_vaddr);
|
|
auto* e = p + h->p_memsz;
|
|
while (p != e) {
|
|
const auto* n = reinterpret_cast<const Elf64_Nhdr*>(p);
|
|
if (n->n_type == NT_GNU_BUILD_ID) {
|
|
return n;
|
|
}
|
|
|
|
p += sizeof(Elf64_Nhdr);
|
|
|
|
p += n->n_namesz;
|
|
p = align_up(p, 4);
|
|
|
|
p += n->n_descsz;
|
|
p = align_up(p, 4);
|
|
}
|
|
}
|
|
|
|
SCYLLA_ASSERT(0 && "no NT_GNU_BUILD_ID note");
|
|
}
|
|
|
|
static int callback(dl_phdr_info* info, size_t size, void* data) {
|
|
std::string& ret = *(std::string*)data;
|
|
std::ostringstream os;
|
|
|
|
// The first DSO is always the main program, which has an empty name.
|
|
SCYLLA_ASSERT(strlen(info->dlpi_name) == 0);
|
|
|
|
auto* n = get_nt_build_id(info);
|
|
auto* p = reinterpret_cast<const unsigned char*>(n);
|
|
|
|
p += sizeof(Elf64_Nhdr);
|
|
|
|
p += n->n_namesz;
|
|
p = align_up(p, 4);
|
|
|
|
auto* desc = p;
|
|
auto* desc_end = p + n->n_descsz;
|
|
while (desc < desc_end) {
|
|
fmt::print(os, "{:02x}", *desc++);
|
|
}
|
|
ret = os.str();
|
|
return 1;
|
|
}
|
|
|
|
static std::string really_get_build_id() {
|
|
std::string ret;
|
|
int r = dl_iterate_phdr(callback, &ret);
|
|
SCYLLA_ASSERT(r == 1);
|
|
return ret;
|
|
}
|
|
|
|
std::string get_build_id() {
|
|
static thread_local std::string cache;
|
|
if (cache.empty()) {
|
|
cache = really_get_build_id();
|
|
}
|
|
return cache;
|
|
}
|