Skip to content

Commit 6b26318

Browse files
authored
crashtracker: support cxx bindings for crashinfo (#1379)
crashtracker: support cxx bindings for crashinfo fix cbindgen issue rename enums to avoid CI issue run in ci Merge branch 'main' into dsn/r-and-d-cxx windows ci C++ 20 try to fix windows ci fix readme another try at windows even more windows try windows again Co-authored-by: daniel.schwartznarbonne <[email protected]>
1 parent 7ce9a60 commit 6b26318

File tree

15 files changed

+975
-99
lines changed

15 files changed

+975
-99
lines changed

.github/workflows/test.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ jobs:
234234
cmake -S .. -DDatadog_ROOT=$LIBDD_OUTPUT_FOLDER
235235
cmake --build .
236236
fi
237+
- name: "Test building CXX bindings (Unix)"
238+
shell: bash
239+
if: matrix.platform != 'windows-latest'
240+
run: |
241+
set -e
242+
cd examples/cxx
243+
./build-and-run-crashinfo.sh
244+
- name: "Setup MSVC (Windows)"
245+
if: matrix.platform == 'windows-latest'
246+
uses: ilammy/msvc-dev-cmd@v1
247+
- name: "Test building CXX bindings (Windows)"
248+
shell: pwsh
249+
if: matrix.platform == 'windows-latest'
250+
run: |
251+
cd examples/cxx
252+
.\build-and-run-crashinfo.ps1
237253
238254
cross-centos7:
239255
name: build and test using cross - on centos7

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ Cargo.lock # this is ignored so you don't update by accident, but is committed.
2424
docker-sync.yml
2525
libtest.so
2626
libtest_cpp.so
27+
examples/cxx/crashinfo

Cargo.lock

Lines changed: 97 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cxx/README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# CXX Bindings Example for libdd-crashtracker
2+
3+
This example demonstrates how to use the CXX bindings for the libdd-crashtracker crate, providing a safer and more idiomatic C++ API compared to the traditional C FFI.
4+
5+
## Features
6+
7+
The CXX bindings provide access to:
8+
9+
### Core Types
10+
- `CrashInfoBuilder` - Builder for constructing crash information
11+
- `StackFrame` - Individual stack frame with debug info and addresses
12+
- `StackTrace` - Collection of stack frames
13+
- `CrashInfo` - Final crash information object
14+
- `Metadata` - Library metadata (name, version, tags)
15+
- `ProcInfo` - Process information
16+
- `OsInfo` - Operating system information
17+
18+
### Enums
19+
- `ErrorKind` - Type of error (Panic, UnhandledException, UnixSignal)
20+
- `BuildIdType` - Build ID format (GNU, GO, PDB, SHA1)
21+
- `FileType` - Binary file format (APK, ELF, PE)
22+
23+
### Key API
24+
25+
**Object Creation:**
26+
```cpp
27+
auto builder = CrashInfoBuilder::create();
28+
auto frame = StackFrame::create();
29+
auto stacktrace = StackTrace::create();
30+
```
31+
32+
**CrashInfoBuilder Methods:**
33+
- `set_kind(CxxErrorKind)` - Set error type (Panic, UnhandledException, UnixSignal)
34+
- `with_message(String)` - Set error message
35+
- `with_counter(String, i64)` - Add a named counter
36+
- `with_log_message(String, bool)` - Add a log message
37+
- `with_fingerprint(String)` - Set crash fingerprint
38+
- `with_incomplete(bool)` - Mark as incomplete
39+
- `set_metadata(Metadata)` - Set library metadata
40+
- `set_proc_info(ProcInfo)` - Set process information
41+
- `set_os_info(OsInfo)` - Set OS information
42+
- `add_stack(Box<StackTrace>)` - Add a stack trace
43+
- `with_timestamp_now()` - Set current timestamp
44+
- `with_file(String)` - Add a file to the report
45+
46+
**StackFrame Methods:**
47+
- `with_function(String)`, `with_file(String)`, `with_line(u32)`, `with_column(u32)` - Set debug info
48+
- `with_ip(usize)`, `with_sp(usize)` - Set instruction/stack pointers
49+
- `with_module_base_address(usize)`, `with_symbol_address(usize)` - Set base addresses
50+
- `with_build_id(String)` - Set build ID
51+
- `build_id_type(CxxBuildIdType)` - Set build ID format (GNU, GO, PDB, SHA1)
52+
- `file_type(CxxFileType)` - Set binary format (APK, ELF, PE)
53+
- `with_path(String)` - Set module path
54+
- `with_relative_address(usize)` - Set relative address
55+
56+
**StackTrace Methods:**
57+
- `add_frame(Box<StackFrame>, bool)` - Add a frame (bool = incomplete)
58+
- `mark_complete()` - Mark trace as complete
59+
60+
**Building & Output:**
61+
```cpp
62+
auto crash_info = crashinfo_build(std::move(builder));
63+
auto json = crash_info->to_json();
64+
```
65+
66+
## Building and Running
67+
68+
### Unix (Linux/macOS)
69+
70+
The `build-and-run-crashinfo.sh` script handles the entire build process:
71+
72+
```bash
73+
./examples/cxx/build-and-run-crashinfo.sh
74+
```
75+
76+
### Windows
77+
78+
The `build-and-run-crashinfo.ps1` PowerShell script handles the build process on Windows:
79+
80+
```powershell
81+
.\examples\cxx\build-and-run-crashinfo.ps1
82+
```
83+
84+
**Prerequisites for Windows:**
85+
- Either MSVC (via Visual Studio) or MinGW/LLVM with C++ compiler
86+
- PowerShell 5.0 or later (comes with Windows 10+)
87+
- Rust toolchain
88+
89+
The build script will:
90+
1. Build libdd-crashtracker with the `cxx` feature enabled
91+
2. Find the CXX bridge headers and libraries
92+
3. Compile the C++ example (automatically detects MSVC or MinGW/Clang)
93+
4. Run the example and display the output
94+
95+
## Example Output
96+
97+
The example creates a crash report with:
98+
- Error kind and message
99+
- Library metadata with tags
100+
- Process and OS information
101+
- A stack trace with multiple frames (debug info + binary addresses)
102+
- Counters and log messages
103+
- Timestamp
104+
105+
The output is a JSON object that can be sent to Datadog's crash tracking service.
106+
107+
## Notes
108+
109+
- The CXX bindings use `rust::String` types which need to be converted to `std::string` for use with standard C++ streams
110+
- All functions that can fail will use exceptions (standard C++ exception handling)
111+
- The bindings are type-safe and prevent many common C FFI errors
112+
- Memory is managed automatically through RAII and smart pointers
113+
114+
## Comparison to C FFI
115+
116+
The CXX bindings provide several advantages over the traditional C FFI:
117+
118+
1. **Type Safety**: No void pointers, proper type checking at compile time
119+
2. **Memory Safety**: Automatic memory management through smart pointers
120+
3. **Ergonomics**: More natural C++ idioms, no need for manual handle management
121+
4. **Error Handling**: Exceptions instead of error codes
122+
5. **String Handling**: Seamless `rust::String` ↔ C++ string interop
123+
124+
## Requirements
125+
126+
- C++20 or later
127+
- Rust toolchain
128+
- Platform: macOS, Linux, or Windows
129+
- Windows: Requires MSVC (via Visual Studio) or MinGW/LLVM

0 commit comments

Comments
 (0)