Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,22 +234,35 @@ jobs:
cmake -S .. -DDatadog_ROOT=$LIBDD_OUTPUT_FOLDER
cmake --build .
fi
- name: "Test building CXX bindings (Unix)"
- name: "Test building CXX bindings - Crashinfo (Unix)"
shell: bash
if: matrix.platform != 'windows-latest'
run: |
set -e
cd examples/cxx
./build-and-run-crashinfo.sh
- name: "Test building CXX bindings - Profiling (Unix)"
shell: bash
if: matrix.platform != 'windows-latest'
run: |
set -e
cd examples/cxx
./build-profiling.sh
- name: "Setup MSVC (Windows)"
if: matrix.platform == 'windows-latest'
uses: ilammy/msvc-dev-cmd@v1
- name: "Test building CXX bindings (Windows)"
- name: "Test building CXX bindings - Crashinfo (Windows)"
shell: pwsh
if: matrix.platform == 'windows-latest'
run: |
cd examples/cxx
.\build-and-run-crashinfo.ps1
- name: "Test building CXX bindings - Profiling (Windows)"
shell: pwsh
if: matrix.platform == 'windows-latest'
run: |
cd examples/cxx
.\build-profiling.ps1

cross-centos7:
name: build and test using cross - on centos7
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ docker-sync.yml
libtest.so
libtest_cpp.so
examples/cxx/crashinfo
examples/cxx/crashinfo.exe
examples/cxx/profiling
examples/cxx/profiling.exe
examples/cxx/owned_sample
examples/cxx/owned_sample.exe
profile.pprof
profile_owned_sample.pprof
70 changes: 70 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

151 changes: 55 additions & 96 deletions examples/cxx/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
# CXX Bindings Example for libdd-crashtracker
# CXX Bindings Examples

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.
This directory contains C++ examples demonstrating the CXX bindings for libdatadog components.

## Features
CXX bindings provide a safer and more idiomatic C++ API compared to the traditional C FFI bindings, with automatic memory management and exception handling.

The CXX bindings provide access to:
## Examples

### Core Types
### Crashtracker (`crashinfo.cpp`)

Demonstrates building crash reports using the CXX bindings for `libdd-crashtracker`.

**Build and run:**

Unix (Linux/macOS):
```bash
./build-and-run-crashinfo.sh
```

Windows:
```powershell
.\build-and-run-crashinfo.ps1
```

**Key features:**
- Type-safe crash report builder API
- Support for stack traces, frames, and metadata
- Process and OS information
- Automatic memory management
- Exception-based error handling

**Core Types:**
- `CrashInfoBuilder` - Builder for constructing crash information
- `StackFrame` - Individual stack frame with debug info and addresses
- `StackTrace` - Collection of stack frames
Expand All @@ -15,115 +38,51 @@ The CXX bindings provide access to:
- `ProcInfo` - Process information
- `OsInfo` - Operating system information

### Enums
- `ErrorKind` - Type of error (Panic, UnhandledException, UnixSignal)
- `BuildIdType` - Build ID format (GNU, GO, PDB, SHA1)
- `FileType` - Binary file format (APK, ELF, PE)
**Enums:**
- `CxxErrorKind` - Type of error (Panic, UnhandledException, UnixSignal)
- `CxxBuildIdType` - Build ID format (GNU, GO, PDB, SHA1)
- `CxxFileType` - Binary file format (APK, ELF, PE)

### Key API
### Profiling (`profiling.cpp`)

**Object Creation:**
```cpp
auto builder = CrashInfoBuilder::create();
auto frame = StackFrame::create();
auto stacktrace = StackTrace::create();
```
Demonstrates building profiling data using the CXX bindings for `libdd-profiling`.

**CrashInfoBuilder Methods:**
- `set_kind(CxxErrorKind)` - Set error type (Panic, UnhandledException, UnixSignal)
- `with_message(String)` - Set error message
- `with_counter(String, i64)` - Add a named counter
- `with_log_message(String, bool)` - Add a log message
- `with_fingerprint(String)` - Set crash fingerprint
- `with_incomplete(bool)` - Mark as incomplete
- `set_metadata(Metadata)` - Set library metadata
- `set_proc_info(ProcInfo)` - Set process information
- `set_os_info(OsInfo)` - Set OS information
- `add_stack(Box<StackTrace>)` - Add a stack trace
- `with_timestamp_now()` - Set current timestamp
- `with_file(String)` - Add a file to the report

**StackFrame Methods:**
- `with_function(String)`, `with_file(String)`, `with_line(u32)`, `with_column(u32)` - Set debug info
- `with_ip(usize)`, `with_sp(usize)` - Set instruction/stack pointers
- `with_module_base_address(usize)`, `with_symbol_address(usize)` - Set base addresses
- `with_build_id(String)` - Set build ID
- `build_id_type(CxxBuildIdType)` - Set build ID format (GNU, GO, PDB, SHA1)
- `file_type(CxxFileType)` - Set binary format (APK, ELF, PE)
- `with_path(String)` - Set module path
- `with_relative_address(usize)` - Set relative address

**StackTrace Methods:**
- `add_frame(Box<StackFrame>, bool)` - Add a frame (bool = incomplete)
- `mark_complete()` - Mark trace as complete

**Building & Output:**
```cpp
auto crash_info = crashinfo_build(std::move(builder));
auto json = crash_info->to_json();
```

## Building and Running

### Unix (Linux/macOS)

The `build-and-run-crashinfo.sh` script handles the entire build process:
**Build and run:**

Unix (Linux/macOS):
```bash
./examples/cxx/build-and-run-crashinfo.sh
./build-profiling.sh
```

### Windows

The `build-and-run-crashinfo.ps1` PowerShell script handles the build process on Windows:

Windows:
```powershell
.\examples\cxx\build-and-run-crashinfo.ps1
.\build-profiling.ps1
```

**Prerequisites for Windows:**
- Either MSVC (via Visual Studio) or MinGW/LLVM with C++ compiler
- PowerShell 5.0 or later (comes with Windows 10+)
- Rust toolchain

The build script will:
1. Build libdd-crashtracker with the `cxx` feature enabled
2. Find the CXX bridge headers and libraries
3. Compile the C++ example (automatically detects MSVC or MinGW/Clang)
4. Run the example and display the output

## Example Output

The example creates a crash report with:
- Error kind and message
- Library metadata with tags
- Process and OS information
- A stack trace with multiple frames (debug info + binary addresses)
- Counters and log messages
- Timestamp

The output is a JSON object that can be sent to Datadog's crash tracking service.

## Notes
**Key features:**
- Type-safe API for building profiles
- Support for samples, locations, mappings, and labels
- String interning for efficient memory usage
- Pprof format serialization with zstd compression
- Automatic memory management
- Exception-based error handling
- Modern C++20 syntax with designated initializers and `std::format`

- The CXX bindings use `rust::String` types which need to be converted to `std::string` for use with standard C++ streams
- All functions that can fail will use exceptions (standard C++ exception handling)
- The bindings are type-safe and prevent many common C FFI errors
- Memory is managed automatically through RAII and smart pointers
## Build Scripts

## Comparison to C FFI
The examples use a consolidated build system:

The CXX bindings provide several advantages over the traditional C FFI:
- **Unix (Linux/macOS)**: `build-and-run.sh <crate-name> <example-name>`
- **Windows**: `build-and-run.ps1 -CrateName <crate-name> -ExampleName <example-name>`

1. **Type Safety**: No void pointers, proper type checking at compile time
2. **Memory Safety**: Automatic memory management through smart pointers
3. **Ergonomics**: More natural C++ idioms, no need for manual handle management
4. **Error Handling**: Exceptions instead of error codes
5. **String Handling**: Seamless `rust::String` ↔ C++ string interop
Convenience wrappers are provided for each example:
- `build-and-run-crashinfo.sh` / `build-and-run-crashinfo.ps1`
- `build-profiling.sh` / `build-profiling.ps1`

## Requirements

- C++20 or later
- Rust toolchain
- C++ compiler (clang++ or g++)
- Platform: macOS, Linux, or Windows
- Windows: Requires MSVC (via Visual Studio) or MinGW/LLVM
Loading
Loading