Skip to content

Commit f3d75a3

Browse files
committed
profiling: Initial C++ API
1 parent 6b26318 commit f3d75a3

File tree

15 files changed

+844
-317
lines changed

15 files changed

+844
-317
lines changed

.github/workflows/test.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,35 @@ jobs:
234234
cmake -S .. -DDatadog_ROOT=$LIBDD_OUTPUT_FOLDER
235235
cmake --build .
236236
fi
237-
- name: "Test building CXX bindings (Unix)"
237+
- name: "Test building CXX bindings - Crashinfo (Unix)"
238238
shell: bash
239239
if: matrix.platform != 'windows-latest'
240240
run: |
241241
set -e
242242
cd examples/cxx
243243
./build-and-run-crashinfo.sh
244+
- name: "Test building CXX bindings - Profiling (Unix)"
245+
shell: bash
246+
if: matrix.platform != 'windows-latest'
247+
run: |
248+
set -e
249+
cd examples/cxx
250+
./build-profiling.sh
244251
- name: "Setup MSVC (Windows)"
245252
if: matrix.platform == 'windows-latest'
246253
uses: ilammy/msvc-dev-cmd@v1
247-
- name: "Test building CXX bindings (Windows)"
254+
- name: "Test building CXX bindings - Crashinfo (Windows)"
248255
shell: pwsh
249256
if: matrix.platform == 'windows-latest'
250257
run: |
251258
cd examples/cxx
252259
.\build-and-run-crashinfo.ps1
260+
- name: "Test building CXX bindings - Profiling (Windows)"
261+
shell: pwsh
262+
if: matrix.platform == 'windows-latest'
263+
run: |
264+
cd examples/cxx
265+
.\build-profiling.ps1
253266
254267
cross-centos7:
255268
name: build and test using cross - on centos7

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ docker-sync.yml
2525
libtest.so
2626
libtest_cpp.so
2727
examples/cxx/crashinfo
28+
examples/cxx/crashinfo.exe
29+
examples/cxx/profiling
30+
examples/cxx/profiling.exe
31+
profile.pprof

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cxx/README.md

Lines changed: 55 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
1-
# CXX Bindings Example for libdd-crashtracker
1+
# CXX Bindings Examples
22

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

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

7-
The CXX bindings provide access to:
7+
## Examples
88

9-
### Core Types
9+
### Crashtracker (`crashinfo.cpp`)
10+
11+
Demonstrates building crash reports using the CXX bindings for `libdd-crashtracker`.
12+
13+
**Build and run:**
14+
15+
Unix (Linux/macOS):
16+
```bash
17+
./build-and-run-crashinfo.sh
18+
```
19+
20+
Windows:
21+
```powershell
22+
.\build-and-run-crashinfo.ps1
23+
```
24+
25+
**Key features:**
26+
- Type-safe crash report builder API
27+
- Support for stack traces, frames, and metadata
28+
- Process and OS information
29+
- Automatic memory management
30+
- Exception-based error handling
31+
32+
**Core Types:**
1033
- `CrashInfoBuilder` - Builder for constructing crash information
1134
- `StackFrame` - Individual stack frame with debug info and addresses
1235
- `StackTrace` - Collection of stack frames
@@ -15,115 +38,51 @@ The CXX bindings provide access to:
1538
- `ProcInfo` - Process information
1639
- `OsInfo` - Operating system information
1740

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)
41+
**Enums:**
42+
- `CxxErrorKind` - Type of error (Panic, UnhandledException, UnixSignal)
43+
- `CxxBuildIdType` - Build ID format (GNU, GO, PDB, SHA1)
44+
- `CxxFileType` - Binary file format (APK, ELF, PE)
2245

23-
### Key API
46+
### Profiling (`profiling.cpp`)
2447

25-
**Object Creation:**
26-
```cpp
27-
auto builder = CrashInfoBuilder::create();
28-
auto frame = StackFrame::create();
29-
auto stacktrace = StackTrace::create();
30-
```
48+
Demonstrates building profiling data using the CXX bindings for `libdd-profiling`.
3149

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:
50+
**Build and run:**
7151

52+
Unix (Linux/macOS):
7253
```bash
73-
./examples/cxx/build-and-run-crashinfo.sh
54+
./build-profiling.sh
7455
```
7556

76-
### Windows
77-
78-
The `build-and-run-crashinfo.ps1` PowerShell script handles the build process on Windows:
79-
57+
Windows:
8058
```powershell
81-
.\examples\cxx\build-and-run-crashinfo.ps1
59+
.\build-profiling.ps1
8260
```
8361

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
62+
**Key features:**
63+
- Type-safe API for building profiles
64+
- Support for samples, locations, mappings, and labels
65+
- String interning for efficient memory usage
66+
- Pprof format serialization with zstd compression
67+
- Automatic memory management
68+
- Exception-based error handling
69+
- Modern C++20 syntax with designated initializers and `std::format`
10870

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
71+
## Build Scripts
11372

114-
## Comparison to C FFI
73+
The examples use a consolidated build system:
11574

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

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
78+
Convenience wrappers are provided for each example:
79+
- `build-and-run-crashinfo.sh` / `build-and-run-crashinfo.ps1`
80+
- `build-profiling.sh` / `build-profiling.ps1`
12381

12482
## Requirements
12583

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

0 commit comments

Comments
 (0)