Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception backtraces #163

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Jsmn/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,33 @@
#include"Jsmn/Parser.hpp"
#include"Util/Str.hpp"

#include <cstdio>
#include <execinfo.h>
#include <cstdlib>
#include <cxxabi.h>

namespace Jsmn {

void print_backtrace() {
constexpr size_t max_frames = 100;
void* addrlist[max_frames];

int stack_depth = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));

if (stack_depth == 0) {
std::fprintf(stderr, "No backtrace available.\n");
return;
}

char** symbollist = backtrace_symbols(addrlist, stack_depth);

for (int i = 0; i < stack_depth; i++) {
std::fprintf(stderr, "%s\n", symbollist[i]);
}

free(symbollist);
}

class Object::Impl {
private:
std::shared_ptr<Detail::ParseResult> parse_result;
Expand Down
6 changes: 5 additions & 1 deletion Jsmn/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ namespace Jsmn { class ParserExposedBuffer; }

namespace Jsmn {

void print_backtrace();

/* Thrown when converting or using as incorrect type. */
/* FIXME: Use a backtrace-catching exception. */
class TypeError : public std::invalid_argument {
public:
TypeError() : std::invalid_argument("Incorrect type.") { }
TypeError() : std::invalid_argument("Incorrect type.") {
print_backtrace();
}
};

/* Represents an object that has been parsed from a JSON string. */
Expand Down