blob: 1fd082f542b6dcfa11d97be79bc80c1b7d0c5631 [file] [log] [blame]
Florian Mayerca4749a2024-02-14 12:55:46 -08001/*
2 * Copyright (C) 2024 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#pragma once
30
31/**
32 * @file android/crash_detail.h
33 * @brief Attach extra information to android crashes.
34 */
35
36#include <stddef.h>
37#include <stdint.h>
38#include <string.h>
39#include <sys/cdefs.h>
40
41__BEGIN_DECLS
42
43typedef struct crash_detail_t crash_detail_t;
44
45/**
46 * Register a new buffer to get logged into tombstones for crashes.
47 *
48 * It will be added to both the tombstone proto in the crash_detail field, and
49 * in the tombstone text format.
50 *
51 * Tombstone proto definition:
52 * https://cs.android.com/android/platform/superproject/main/+/main:system/core/debuggerd/proto/tombstone.proto
53 *
54 * The lifetime of name and data has to be valid until the program crashes, or until
55 * android_crash_detail_unregister is called.
56 *
57 * Example usage:
58 * const char* stageName = "garbage_collection";
59 * crash_detail_t* cd = android_crash_detail_register("stage", stageName, strlen(stageName));
60 * do_garbage_collection();
61 * android_crash_detail_unregister(cd);
62 *
63 * If this example crashes in do_garbage_collection, a line will show up in the textual representation of the tombstone:
64 * Extra crash detail: stage: 'garbage_collection'
65 *
66 * Introduced in API 35.
67 *
68 * \param name identifying name for this extra data.
69 * this should generally be a human-readable debug string, but we are treating
70 * it as arbitrary bytes because it could be corrupted by the crash.
71 * \param name_size number of bytes of the buffer pointed to by name
72 * \param data a buffer containing the extra detail bytes, if null the crash detail
73 * is disabled until android_crash_detail_replace_data replaces it with
74 * a non-null pointer.
75 * \param data_size number of bytes of the buffer pointed to by data
76 *
77 * \return a handle to the extra crash detail.
78 */
79crash_detail_t* _Nullable android_crash_detail_register(
80 const void* _Nonnull name, size_t name_size, const void* _Nullable data, size_t data_size) __INTRODUCED_IN(35);
81
82/**
83 * Unregister crash detail from being logged into tombstones.
84 *
85 * After this function returns, the lifetime of the objects crash_detail was
86 * constructed from no longer needs to be valid.
87 *
88 * Introduced in API 35.
89 *
90 * \param crash_detail the crash_detail that should be removed.
91 */
92void android_crash_detail_unregister(crash_detail_t* _Nonnull crash_detail) __INTRODUCED_IN(35);
93
94/**
95 * Replace data of crash detail.
96 *
97 * This is more efficient than using android_crash_detail_unregister followed by
98 * android_crash_detail_register. If you very frequently need to swap out the data,
99 * you can hold onto the crash_detail.
100 *
101 * Introduced in API 35.
102 *
103 * \param data the new buffer containing the extra detail bytes, or null to disable until
104 * android_crash_detail_replace_data is called again with non-null data.
105 * \param data_size the number of bytes of the buffer pointed to by data.
106 */
107void android_crash_detail_replace_data(crash_detail_t* _Nonnull crash_detail, const void* _Nullable data, size_t data_size) __INTRODUCED_IN(35);
108
109/**
110 * Replace name of crash detail.
111 *
112 * This is more efficient than using android_crash_detail_unregister followed by
113 * android_crash_detail_register. If you very frequently need to swap out the name,
114 * you can hold onto the crash_detail.
115 *
116 * Introduced in API 35.
117 *
118 * \param name identifying name for this extra data.
119 * \param name_size number of bytes of the buffer pointed to by name
120 */
121void android_crash_detail_replace_name(crash_detail_t* _Nonnull crash_detail, const void* _Nonnull name, size_t name_size) __INTRODUCED_IN(35);
122
123__END_DECLS