forked from getsentry/sentry-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
392 lines (330 loc) · 11.2 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# define NOMINMAX
# define _CRT_SECURE_NO_WARNINGS
#endif
#include "sentry.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef NDEBUG
# undef NDEBUG
#endif
#include <assert.h>
#ifdef SENTRY_PLATFORM_WINDOWS
# include <synchapi.h>
# define sleep_s(SECONDS) Sleep((SECONDS)*1000)
#else
# include <signal.h>
# include <unistd.h>
# define sleep_s(SECONDS) sleep(SECONDS)
#endif
static sentry_value_t
before_send_callback(sentry_value_t event, void *hint, void *closure)
{
(void)hint;
(void)closure;
// make our mark on the event
sentry_value_set_by_key(
event, "adapted_by", sentry_value_new_string("before_send"));
// tell the backend to proceed with the event
return event;
}
static sentry_value_t
discarding_before_send_callback(sentry_value_t event, void *hint, void *closure)
{
(void)hint;
(void)closure;
// discard event and signal backend to stop further processing
sentry_value_decref(event);
return sentry_value_new_null();
}
static sentry_value_t
discarding_on_crash_callback(
const sentry_ucontext_t *uctx, sentry_value_t event, void *closure)
{
(void)uctx;
(void)closure;
// discard event and signal backend to stop further processing
sentry_value_decref(event);
return sentry_value_new_null();
}
static sentry_value_t
on_crash_callback(
const sentry_ucontext_t *uctx, sentry_value_t event, void *closure)
{
(void)uctx;
(void)closure;
// tell the backend to retain the event
return event;
}
static void
print_envelope(sentry_envelope_t *envelope, void *unused_state)
{
(void)unused_state;
size_t size_out = 0;
char *s = sentry_envelope_serialize(envelope, &size_out);
printf("%s", s);
sentry_free(s);
sentry_envelope_free(envelope);
}
static bool
has_arg(int argc, char **argv, const char *arg)
{
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], arg) == 0) {
return true;
}
}
return false;
}
#ifdef CRASHPAD_WER_ENABLED
int
call_rffe_many_times()
{
RaiseFailFastException(NULL, NULL, 0);
RaiseFailFastException(NULL, NULL, 0);
RaiseFailFastException(NULL, NULL, 0);
RaiseFailFastException(NULL, NULL, 0);
return 1;
}
typedef int (*crash_func)();
void
indirect_call(crash_func func)
{
// This code always generates CFG guards.
func();
}
static void
trigger_stack_buffer_overrun()
{
// Call into the middle of the Crashy function.
crash_func func = (crash_func)((uintptr_t)(call_rffe_many_times) + 16);
__try {
// Generates a STATUS_STACK_BUFFER_OVERRUN exception if CFG triggers.
indirect_call(func);
} __except (EXCEPTION_EXECUTE_HANDLER) {
// CFG fast fail should never be caught.
printf(
"If you see me, then CFG wasn't enabled (compile with /guard:cf)");
}
// Should only reach here if CFG is disabled.
abort();
}
static void
trigger_fastfail_crash()
{
// this bypasses WINDOWS SEH and will only be caught with the crashpad WER
// module enabled
__fastfail(77);
}
#endif // CRASHPAD_WER_ENABLED
#ifdef SENTRY_PLATFORM_AIX
// AIX has a null page mapped to the bottom of memory, which means null derefs
// don't segfault. try dereferencing the top of memory instead; the top nibble
// seems to be unusable.
static void *invalid_mem = (void *)0xFFFFFFFFFFFFFF9B; // -100 for memset
#else
static void *invalid_mem = (void *)1;
#endif
static void
trigger_crash()
{
memset((char *)invalid_mem, 1, 100);
}
int
main(int argc, char **argv)
{
sentry_options_t *options = sentry_options_new();
if (has_arg(argc, argv, "disable-backend")) {
sentry_options_set_backend(options, NULL);
}
// this is an example. for real usage, make sure to set this explicitly to
// an app specific cache location.
sentry_options_set_database_path(options, ".sentry-native");
sentry_options_set_auto_session_tracking(options, false);
sentry_options_set_symbolize_stacktraces(options, true);
sentry_options_set_environment(options, "development");
// sentry defaults this to the `SENTRY_RELEASE` env variable
if (!has_arg(argc, argv, "release-env")) {
sentry_options_set_release(options, "test-example-release");
}
if (has_arg(argc, argv, "log")) {
sentry_options_set_debug(options, 1);
}
if (has_arg(argc, argv, "attachment")) {
// assuming the example / test is run directly from the cmake build
// directory
sentry_options_add_attachment(options, "./CMakeCache.txt");
}
if (has_arg(argc, argv, "stdout")) {
sentry_options_set_transport(
options, sentry_transport_new(print_envelope));
}
if (has_arg(argc, argv, "capture-transaction")) {
sentry_options_set_traces_sample_rate(options, 1.0);
}
if (has_arg(argc, argv, "child-spans")) {
sentry_options_set_max_spans(options, 5);
}
if (has_arg(argc, argv, "before-send")) {
sentry_options_set_before_send(options, before_send_callback, NULL);
}
if (has_arg(argc, argv, "discarding-before-send")) {
sentry_options_set_before_send(
options, discarding_before_send_callback, NULL);
}
if (has_arg(argc, argv, "on-crash")) {
sentry_options_set_on_crash(options, on_crash_callback, NULL);
}
if (has_arg(argc, argv, "discarding-on-crash")) {
sentry_options_set_on_crash(
options, discarding_on_crash_callback, NULL);
}
if (has_arg(argc, argv, "override-sdk-name")) {
sentry_options_set_sdk_name(options, "sentry.native.android.flutter");
}
sentry_init(options);
if (!has_arg(argc, argv, "no-setup")) {
sentry_set_transaction("test-transaction");
sentry_set_level(SENTRY_LEVEL_WARNING);
sentry_set_extra("extra stuff", sentry_value_new_string("some value"));
sentry_set_extra("…unicode key…",
// https://xkcd.com/1813/ :-)
sentry_value_new_string("őá…–🤮🚀¿ 한글 테스트"));
sentry_set_tag("expected-tag", "some value");
sentry_set_tag("not-expected-tag", "some value");
sentry_remove_tag("not-expected-tag");
sentry_value_t context = sentry_value_new_object();
sentry_value_set_by_key(
context, "type", sentry_value_new_string("runtime"));
sentry_value_set_by_key(
context, "name", sentry_value_new_string("testing-runtime"));
sentry_set_context("runtime", context);
sentry_value_t user = sentry_value_new_object();
sentry_value_set_by_key(user, "id", sentry_value_new_int32(42));
sentry_value_set_by_key(
user, "username", sentry_value_new_string("some_name"));
sentry_set_user(user);
sentry_value_t default_crumb
= sentry_value_new_breadcrumb(NULL, "default level is info");
sentry_add_breadcrumb(default_crumb);
sentry_value_t debug_crumb
= sentry_value_new_breadcrumb("http", "debug crumb");
sentry_value_set_by_key(
debug_crumb, "category", sentry_value_new_string("example!"));
sentry_value_set_by_key(
debug_crumb, "level", sentry_value_new_string("debug"));
sentry_add_breadcrumb(debug_crumb);
sentry_value_t nl_crumb
= sentry_value_new_breadcrumb(NULL, "lf\ncrlf\r\nlf\n...");
sentry_value_set_by_key(
nl_crumb, "category", sentry_value_new_string("something else"));
sentry_add_breadcrumb(nl_crumb);
}
if (has_arg(argc, argv, "start-session")) {
sentry_start_session();
}
if (has_arg(argc, argv, "overflow-breadcrumbs")) {
for (size_t i = 0; i < 101; i++) {
char buffer[4];
snprintf(buffer, 4, "%zu", i);
sentry_add_breadcrumb(sentry_value_new_breadcrumb(0, buffer));
}
}
if (has_arg(argc, argv, "capture-multiple")) {
for (size_t i = 0; i < 10; i++) {
char buffer[10];
snprintf(buffer, 10, "Event #%zu", i);
sentry_value_t event = sentry_value_new_message_event(
SENTRY_LEVEL_INFO, NULL, buffer);
sentry_capture_event(event);
}
}
if (has_arg(argc, argv, "reinstall")) {
sentry_reinstall_backend();
}
if (has_arg(argc, argv, "sleep")) {
sleep_s(10);
}
if (has_arg(argc, argv, "crash")) {
trigger_crash();
}
#ifdef CRASHPAD_WER_ENABLED
if (has_arg(argc, argv, "fastfail")) {
trigger_fastfail_crash();
}
if (has_arg(argc, argv, "stack-buffer-overrun")) {
trigger_stack_buffer_overrun();
}
#endif
if (has_arg(argc, argv, "assert")) {
assert(0);
}
if (has_arg(argc, argv, "abort")) {
abort();
}
#ifdef SENTRY_PLATFORM_UNIX
if (has_arg(argc, argv, "raise")) {
raise(SIGSEGV);
}
if (has_arg(argc, argv, "kill")) {
kill(getpid(), SIGSEGV);
}
#endif
if (has_arg(argc, argv, "capture-event")) {
sentry_value_t event = sentry_value_new_message_event(
SENTRY_LEVEL_INFO, "my-logger", "Hello World!");
if (has_arg(argc, argv, "add-stacktrace")) {
sentry_event_value_add_stacktrace(event, NULL, 0);
}
sentry_capture_event(event);
}
if (has_arg(argc, argv, "capture-exception")) {
sentry_value_t exc = sentry_value_new_exception(
"ParseIntError", "invalid digit found in string");
if (has_arg(argc, argv, "add-stacktrace")) {
sentry_value_set_stacktrace(exc, NULL, 0);
}
sentry_value_t event = sentry_value_new_event();
sentry_event_add_exception(event, exc);
sentry_capture_event(event);
}
if (has_arg(argc, argv, "capture-transaction")) {
sentry_transaction_context_t *tx_ctx
= sentry_transaction_context_new("little.teapot",
"Short and stout here is my handle and here is my spout");
if (has_arg(argc, argv, "unsample-tx")) {
sentry_transaction_context_set_sampled(tx_ctx, 0);
}
sentry_transaction_t *tx
= sentry_transaction_start(tx_ctx, sentry_value_new_null());
if (has_arg(argc, argv, "error-status")) {
sentry_transaction_set_status(
tx, SENTRY_SPAN_STATUS_INTERNAL_ERROR);
}
if (has_arg(argc, argv, "child-spans")) {
sentry_span_t *child
= sentry_transaction_start_child(tx, "littler.teapot", NULL);
sentry_span_t *grandchild
= sentry_span_start_child(child, "littlest.teapot", NULL);
if (has_arg(argc, argv, "error-status")) {
sentry_span_set_status(child, SENTRY_SPAN_STATUS_NOT_FOUND);
sentry_span_set_status(
grandchild, SENTRY_SPAN_STATUS_ALREADY_EXISTS);
}
sentry_span_finish(grandchild);
sentry_span_finish(child);
}
sentry_transaction_finish(tx);
}
// make sure everything flushes
sentry_close();
if (has_arg(argc, argv, "sleep-after-shutdown")) {
sleep_s(1);
}
if (has_arg(argc, argv, "crash-after-shutdown")) {
trigger_crash();
}
}