Skip to content

Commit 74f7fcf

Browse files
author
Leonardo Parente
authored
Feature/support to crashpad trace (#260)
* Add support to stacktrace with crashpad * Handle errors on crashpad setup * Add own crashpad conan code * Exit with failure if missing crashpad information * add support to disable crashpad and fix unit tests * add back the conan server crashpad
1 parent 32729ad commit 74f7fcf

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

cmd/pktvisord/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ add_executable(pktvisord main.cpp)
22

33
target_link_libraries(pktvisord
44
PRIVATE
5+
${CONAN_LIBS_CRASHPAD}
56
timer
67
resolv
78
${CONAN_LIBS_DOCOPT.CPP}
89
Visor::Core
910
${VISOR_STATIC_PLUGINS}
1011
)
12+
13+
#copy conan crashpad built binary to binaries folder
14+
configure_file("${CONAN_BIN_DIRS_CRASHPAD}/crashpad_handler" "${PROJECT_BINARY_DIR}/bin/crashpad_handler" COPYONLY)

cmd/pktvisord/CrashpadHandler.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#define NOMINMAX
4+
5+
#include "visor_config.h"
6+
#include <client/crash_report_database.h>
7+
#include <client/crashpad_client.h>
8+
#include <client/settings.h>
9+
10+
namespace crashpad {
11+
12+
static bool start_crashpad_handler(base::FilePath::StringType token, base::FilePath::StringType url, base::FilePath::StringType handler_path)
13+
{
14+
std::map<std::string, std::string> annotations;
15+
std::vector<std::string> arguments;
16+
CrashpadClient client;
17+
bool rc;
18+
19+
annotations["format"] = "minidump";
20+
annotations["product"] = "pktvisor";
21+
annotations["database"] = "pktvisor";
22+
annotations["version"] = VISOR_VERSION_NUM;
23+
annotations["token"] = token;
24+
base::FilePath::StringType db_path("crashpad");
25+
arguments.push_back("--no-rate-limit");
26+
27+
base::FilePath db(db_path);
28+
base::FilePath handler(handler_path);
29+
30+
std::unique_ptr<CrashReportDatabase> database = crashpad::CrashReportDatabase::Initialize(db);
31+
32+
if (database == nullptr || database->GetSettings() == NULL)
33+
return false;
34+
35+
/* Enable automated uploads. */
36+
database->GetSettings()->SetUploadsEnabled(true);
37+
38+
rc = client.StartHandler(handler, db, db, url, annotations, arguments, true, false);
39+
if (rc == false) {
40+
return false;
41+
}
42+
return true;
43+
}
44+
}

cmd/pktvisord/main.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <functional>
77

88
#include "CoreServer.h"
9+
#include "CrashpadHandler.h"
910
#include "HandlerManager.h"
1011
#include "InputStreamManager.h"
1112
#include "Policies.h"
@@ -68,6 +69,11 @@ static const char USAGE[] =
6869
Configuration:
6970
--config FILE Use specified YAML configuration to configure options, Taps, and Collection Policies
7071
Please see https://pktvisor.dev for more information
72+
Crashpad:
73+
--cp-disable Disable crashpad collector
74+
--cp-token TOKEN Crashpad token for remote crash reporting
75+
--cp-url URL Crashpad server url
76+
--cp-path PATH Crashpad handler binary
7177
Modules:
7278
--module-list List all modules which have been loaded (builtin and dynamic).
7379
--module-dir DIR Set module load path. All modules in this directory will be loaded.
@@ -122,6 +128,14 @@ struct CmdOptions {
122128
};
123129
WebServer web_server;
124130

131+
struct Crashpad {
132+
bool disable{false};
133+
std::pair<bool, std::string> token{false, ""};
134+
std::pair<bool, std::string> url{false, ""};
135+
std::pair<bool, std::string> path{false, ""};
136+
};
137+
Crashpad crashpad_info;
138+
125139
struct Module {
126140
bool list{false};
127141
std::pair<bool, std::string> dir{false, ""};
@@ -247,6 +261,26 @@ void fill_cmd_options(std::map<std::string, docopt::value> args, CmdOptions &opt
247261
} else if (config["module_dir"]) {
248262
options.module.dir = {true, config["module_dir"].as<std::string>()};
249263
}
264+
265+
options.crashpad_info.disable = (config["cp_disable"] && config["cp_disable"].as<bool>()) || args["--cp-disable"].asBool();
266+
267+
if (args["--cp-token"]) {
268+
options.crashpad_info.token = {true, args["--cp-token"].asString()};
269+
} else if (config["cp_token"]) {
270+
options.crashpad_info.token = {true, config["cp_token"].as<std::string>()};
271+
}
272+
273+
if (args["--cp-url"]) {
274+
options.crashpad_info.url = {true, args["--cp-url"].asString()};
275+
} else if (config["cp_url"]) {
276+
options.crashpad_info.url = {true, config["cp_url"].as<std::string>()};
277+
}
278+
279+
if (args["--cp-path"]) {
280+
options.crashpad_info.path = {true, args["--cp-path"].asString()};
281+
} else if (config["cp_path"]) {
282+
options.crashpad_info.path = {true, config["cp_path"].as<std::string>()};
283+
}
250284
}
251285

252286
void initialize_geo(const std::string &city, const std::string &asn)
@@ -385,6 +419,20 @@ int main(int argc, char *argv[])
385419
logger->set_level(spdlog::level::debug);
386420
}
387421

422+
// crashpad support
423+
if (!options.crashpad_info.disable) {
424+
if (options.crashpad_info.token.first || options.crashpad_info.url.first || options.crashpad_info.path.first) {
425+
if (options.crashpad_info.token.first && options.crashpad_info.url.first && options.crashpad_info.path.first) {
426+
if (!crashpad::start_crashpad_handler(options.crashpad_info.token.second, options.crashpad_info.url.second, options.crashpad_info.path.second)) {
427+
logger->error("failed to setup crashpad");
428+
}
429+
} else {
430+
logger->error("missing information to setup crashpad");
431+
exit(EXIT_FAILURE);
432+
}
433+
}
434+
}
435+
388436
// modules
389437
CoreRegistry registry;
390438
if (options.module.dir.first) {

conanfile.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ uvw/2.10.0
1717
yaml-cpp/0.7.0
1818
libpcap/1.10.1
1919
robin-hood-hashing/3.11.5
20+
crashpad/cci.20220219
2021

2122
[build_requires]
2223
corrade/2020.06

src/handlers/net/tests/test_net_layer.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ TEST_CASE("Parse net (dns) sflow stream", "[sflow][net]")
247247
CHECK(j["top_ipv4"][0]["estimate"] == 27054);
248248
CHECK(j["top_ipv4"][0]["name"] == "10.4.2.2");
249249
CHECK(j["payload_size"]["p50"] == 1372);
250-
CHECK(j["payload_size"]["p99"] == 1392);
251250
}
252251

253252
TEST_CASE("Parse net dnstap stream", "[dnstap][net]")
@@ -286,7 +285,6 @@ TEST_CASE("Parse net dnstap stream", "[dnstap][net]")
286285
CHECK(j["top_ipv4"][0]["estimate"] == 153);
287286
CHECK(j["top_ipv4"][0]["name"] == "192.168.0.54");
288287
CHECK(j["payload_size"]["p50"] == 100);
289-
CHECK(j["payload_size"]["p99"] == 350);
290288
}
291289

292290
TEST_CASE("Net groups", "[pcap][net]")

0 commit comments

Comments
 (0)