Skip to content

Commit 7506221

Browse files
committed
Add nix crash command for testing crash reporting
1 parent 9bdc61c commit 7506221

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/nix/crash.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "nix/cmd/command.hh"
2+
3+
using namespace nix;
4+
5+
struct CmdCrash : Command
6+
{
7+
std::string type;
8+
9+
CmdCrash()
10+
{
11+
expectArg("type", &type);
12+
}
13+
14+
Category category() override
15+
{
16+
return catUndocumented;
17+
}
18+
19+
std::string description() override
20+
{
21+
return "crash the program to test crash reporting";
22+
}
23+
24+
void run() override
25+
{
26+
if (type == "segfault") {
27+
printError("Triggering a segfault...");
28+
volatile int * p = nullptr;
29+
*p = 123;
30+
}
31+
32+
else if (type == "assert") {
33+
printError("Triggering an assertion failure...");
34+
assert(false && "This is an assertion failure");
35+
}
36+
37+
else if (type == "logic-error") {
38+
printError("Triggering a C++ logic error...");
39+
std::bitset<4>{"012"};
40+
}
41+
42+
else {
43+
throw Error("unknown crash type '%s'", type);
44+
}
45+
}
46+
};
47+
48+
static auto rCrash = registerCommand<CmdCrash>("crash");

src/nix/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ nix_sources = [ config_priv_h ] + files(
6868
'config.cc',
6969
'copy.cc',
7070
'crash-handler.cc',
71+
'crash.cc',
7172
'derivation-add.cc',
7273
'derivation-show.cc',
7374
'derivation.cc',

0 commit comments

Comments
 (0)