-
Notifications
You must be signed in to change notification settings - Fork 5
Scuffed Linux support #4
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
Open
JulianGmp
wants to merge
3
commits into
TylerGlaiel:main
Choose a base branch
from
JulianGmp:linux_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| bin/ | ||
| build/ | ||
| test_logs/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,42 @@ | ||
| # Crashlogs | ||
| A simple way to output stack traces when a program crashes in C++, using the new C++23 <stacktrace> header | ||
| currently windows-only, but if anyone wants to add mac or linux support I'll merge it in | ||
| A simple way to output stack traces when a program crashes in C++, using the new C++23 `<stacktrace>` header. | ||
| Works on Windows. Kind of works on Linux. | ||
|
|
||
| usage: include the files in your project, then call | ||
| glaiel::crashlogs::begin_monitoring(); | ||
| to enable crash handling (probably do this at the start of your program) | ||
| ## Usage | ||
|
|
||
| when the program crashes, it will save a timestamped stack trace to the folder specified with | ||
| glaiel::crashlogs::set_crashlog_folder(folder_path); | ||
| include the files in your project, then call | ||
| Call `glaiel::crashlogs::begin_monitoring` to enable crash handling (probablly do this at the start of your program). | ||
| Monitoring and writing the crashlog is done in a worker thread. | ||
|
|
||
| when the program crashes, it will save a timestamped stack trace to the folder specified with `glaiel::crashlogs::set_crashlog_folder(folder_path);` | ||
|
|
||
| some additional customizability and callbacks are documented in the header file | ||
|
|
||
|
|
||
| note: the stack trace outputted will include a bunch of error handling stuff at the top. It would be nice to skip printing the first X stack trace entries, but how many to skip seems kinda dependent on optimization settings and which condition triggered the error handler, so I did not bother with that yet. | ||
|
|
||
| <!-- TODO add example output (preferably from windows) --> | ||
|
|
||
| ### Linux (GCC) support | ||
|
|
||
| *Note that at the time of writing GCC (13.1.1) and its implementation of the C++ standard library consider the backtrace feature to be experimental!* | ||
| This means that you need to manually link against `stdc++_libbacktrace`. | ||
|
|
||
| Also the backtraces currently look like this | ||
| ``` | ||
| Received signal 11 SIGSEGV | ||
| 0# at :32588 | ||
| 1# at :32588 | ||
| 2# at :32588 | ||
| 3# at :32588 | ||
| 4# at :32588 | ||
| 5# at :32588 | ||
| 6# at :32588 | ||
| 7# | ||
| ``` | ||
| like they're pretty useless, I'd stick to a different solution until GCC has finished the backtrace feature fully. | ||
|
|
||
| ### Testing | ||
|
|
||
| `TestTool.cpp` is a very simple command line tool to test a few different cases. | ||
| It will enable the monitoring and delibrately crash so you can manually verify the handler works. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // A very simple CLI tool to manually test the different cases of Crashlog. | ||
| // It's not really possible to unit test crashing, so this will have to suffice. | ||
|
|
||
| #include <iostream> | ||
| #include <thread> | ||
| #include <optional> | ||
| #include <string_view> | ||
|
|
||
| #include <csignal> | ||
|
|
||
| #include "../crashlogs.h" | ||
|
|
||
| enum class TestType | ||
| { | ||
| Segfault, | ||
| Abort, | ||
| Terminate, | ||
| IllegalInstruction, | ||
| UnhandledException, | ||
| StackOverflow, | ||
| }; | ||
|
|
||
| // forward declarations so we can use these in main | ||
| void usage(const char *arg0); | ||
| [[nodiscard]] std::optional<TestType> testTypeFromString(std::string_view str); | ||
| void causeStackOverflow(int val); | ||
|
|
||
| int main(int argc, char **argv) | ||
| { | ||
| if (argc != 2) | ||
| { | ||
| usage(argv[0]); | ||
| return 1; | ||
| } | ||
| const auto testTypeParsed = testTypeFromString(argv[1]); | ||
| if (!testTypeParsed.has_value()) | ||
| { | ||
| std::cerr << "Cannot parse `" << argv[1] << "` as a valid test type!\n" | ||
| << std::endl; | ||
| usage(argv[0]); | ||
| return 2; | ||
| } | ||
| const TestType testType = testTypeParsed.value(); | ||
|
|
||
| std::cout << "Initializing crashlogs" << std::endl; | ||
|
|
||
| glaiel::crashlogs::set_crashlog_folder("./test_logs"); | ||
| glaiel::crashlogs::begin_monitoring(); | ||
|
|
||
| std::cout << "Gonna wait a second..." << std::endl; | ||
|
|
||
| for (int cnt = 0; cnt < 10; cnt++) | ||
| { | ||
| std::cout << "."; | ||
| std::cout.flush(); | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
| } | ||
| std::cout << std::endl; | ||
|
|
||
| switch (testType) | ||
| { | ||
| case TestType::Segfault: | ||
| { | ||
| std::cout << "Causing Segfault" << std::endl; | ||
| int *a = nullptr; | ||
| int b = *a + 1; | ||
| } | ||
| break; | ||
| case TestType::Abort: | ||
| { | ||
| std::cout << "Causing Abort" << std::endl; | ||
| std::abort(); | ||
| } | ||
| break; | ||
| case TestType::Terminate: | ||
| { | ||
| std::cout << "Causing Terminate" << std::endl; | ||
| std::terminate(); | ||
| } | ||
| break; | ||
| case TestType::IllegalInstruction: | ||
| { | ||
| std::cout << "Causing IllegalInstruction" << std::endl; | ||
| // no idea how to cause this delibrately so I'm just raising the signal manually | ||
| std::raise(SIGILL); | ||
| } | ||
| break; | ||
| case TestType::UnhandledException: | ||
| { | ||
| std::cout << "Causing UnhandledException" << std::endl; | ||
| throw std::logic_error("Whoops"); | ||
| } | ||
| break; | ||
| case TestType::StackOverflow: | ||
| { | ||
| std::cout << "Causing StackOverflow" << std::endl; | ||
| causeStackOverflow(0); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| void usage(const char *arg0) | ||
| { | ||
| std::cerr << "Usage: \n"; | ||
| std::cerr << "\t" << arg0 << " <TestType>\n"; | ||
| std::cerr << "\n" | ||
| "\tWith valid test types:\n" | ||
| "\t - 'Segfault'\n" | ||
| "\t - 'Abort'\n" | ||
| "\t - 'Terminate'\n" | ||
| "\t - 'IllegalInstruction'\n" | ||
| "\t - 'UnhandledException'\n" | ||
| "\t - 'StackOverflow'" | ||
| << std::endl; | ||
| } | ||
|
|
||
| std::optional<TestType> testTypeFromString(std::string_view str) | ||
| { | ||
| if (str == "Segfault" || str == "segfault") | ||
| return TestType::Segfault; | ||
| if (str == "Abort" || str == "abort") | ||
| return TestType::Abort; | ||
| if (str == "Terminate" || str == "terminate") | ||
| return TestType::Terminate; | ||
| if (str == "IllegalInstruction" || str == "illegalinstruction") | ||
| return TestType::IllegalInstruction; | ||
| if (str == "UnhandledException" || str == "unhandledexception") | ||
| return TestType::UnhandledException; | ||
| if (str == "StackOverflow" || str == "stackoverflow") | ||
| return TestType::StackOverflow; | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| void causeStackOverflow(int val) | ||
| { | ||
| causeStackOverflow(val + 1); | ||
| std::cout << " " << val; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.