Implement getCallStackFast/Exact for fast call-stack backtrace.

This commit is contained in:
Бранимир Караџић
2025-09-27 12:51:02 -07:00
parent 59d9249854
commit c8128850f8
9 changed files with 908 additions and 69 deletions

View File

@@ -49,7 +49,7 @@ namespace bx
///
WriterI* getDebugOut();
/// Capture current callstack.
/// Capture current callstack fast.
///
/// @param[in] _skip Skip top N stack frames.
/// @param[in] _max Maximum frame to capture.
@@ -57,7 +57,17 @@ namespace bx
///
/// @returns Number of stack frames captured.
///
uint32_t getCallStack(uint32_t _skip, uint32_t _max, uintptr_t* _outStack);
uint32_t getCallStackFast(uint32_t _skip, uint32_t _max, uintptr_t* _outStack);
/// Capture current callstack with slower but more accurate method.
///
/// @param[in] _skip Skip top N stack frames.
/// @param[in] _max Maximum frame to capture.
/// @param[out] _outStack Stack frames array. Must be at least `_max` elements.
///
/// @returns Number of stack frames captured.
///
uint32_t getCallStackExact(uint32_t _skip, uint32_t _max, uintptr_t* _outStack);
/// Write callstack.
///
@@ -68,9 +78,9 @@ namespace bx
///
/// @returns Number of bytes writen to `_writer`.
///
int32_t writeCallstack(WriterI* _writer, uintptr_t* _stack, uint32_t _num, Error* _err);
int32_t writeCallstack(WriterI* _writer, const uintptr_t* _stack, uint32_t _num, Error* _err);
/// Capture call stack, and write it to debug output.
/// Capture call stack with `bx::getCallStackExact`, and write it to debug output.
///
/// @param[in] _skip Skip top N stack frames.
///

View File

@@ -60,6 +60,10 @@ namespace bx
///
FilePath(const StringView& _str);
/// Assign file path from string.
///
FilePath& operator=(const char* _rhs);
/// Assign file path from string.
///
FilePath& operator=(const StringView& _rhs);

View File

@@ -187,6 +187,25 @@ namespace bx
return m_0terminated;
}
inline bool operator==(const StringView& _lhs, const StringView& _rhs)
{
return 0 == strCmp(_lhs, _rhs);
}
inline bool overlap(const StringView& _a, const StringView& _b)
{
return _a.getTerm() > _b.getPtr()
&& _b.getTerm() > _a.getPtr()
;
}
inline bool contain(const StringView& _a, const StringView& _b)
{
return _a.getPtr() <= _b.getPtr()
&& _a.getTerm() >= _b.getTerm()
;
}
template<uint16_t MaxCapacityT>
inline FixedStringT<MaxCapacityT>::FixedStringT()
: m_len(0)

View File

@@ -137,6 +137,15 @@ namespace bx
bool m_0terminated;
};
/// Compare two string views.
bool operator==(const StringView& _lhs, const StringView& _rhs);
/// Returns true if two string views overlap.
bool overlap(const StringView& _a, const StringView& _b);
/// Returns true if string view `_a` contains string view `_b`.
bool contain(const StringView& _a, const StringView& _b);
/// Fixed capacity string.
///
template<uint16_t MaxCapacityT>

View File

@@ -42,7 +42,7 @@ namespace bx
total += write(&smb, "\n\n", &err);
uintptr_t stack[32];
const uint32_t num = getCallStack(2 /* skip self */ + _skip, BX_COUNTOF(stack), stack);
const uint32_t num = getCallStackExact(2 /* skip self */ + _skip, BX_COUNTOF(stack), stack);
total += writeCallstack(&smb, stack, num, &err);
total += write(&smb, &err,

File diff suppressed because it is too large Load Diff

View File

@@ -303,6 +303,12 @@ namespace bx
set(_filePath);
}
FilePath& FilePath::operator=(const char* _rhs)
{
set(_rhs);
return *this;
}
FilePath& FilePath::operator=(const StringView& _rhs)
{
set(_rhs);

View File

@@ -19,7 +19,7 @@ bool testAssertHandler(const bx::Location& _location, uint32_t _skip, const char
bx::printf("\n");
uintptr_t stack[32];
const uint32_t num = bx::getCallStack(2 /* skip self */ + _skip, BX_COUNTOF(stack), stack);
const uint32_t num = bx::getCallStackExact(2 /* skip self */ + _skip, BX_COUNTOF(stack), stack);
bx::writeCallstack(bx::getStdOut(), stack, num, bx::ErrorIgnore{});
// Throwing exceptions is required for testing asserts being trigged.

View File

@@ -45,7 +45,7 @@ TEST_CASE("StringLiteral", "[string]")
REQUIRE(5 == sv.getLength() );
REQUIRE(5 == bx::strLen(sv) );
REQUIRE(0 == bx::strCmp("abvgd", sv) );
REQUIRE("abvgd" == sv);
}
TEST_CASE("stringPrintfTy", "[string]")
@@ -218,6 +218,19 @@ TEST_CASE("strCmpV sort", "[string][sort]")
}
}
TEST_CASE("overlap", "[string]")
{
const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
const bx::StringView quick = bx::strFind(test, "Quick");
REQUIRE(bx::overlap(quick, test) );
const bx::StringView empty;
REQUIRE(!bx::overlap(quick, empty) );
REQUIRE(!bx::overlap(test, empty) );
}
TEST_CASE("strRFind", "[string]")
{
const char* test = "test";