DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Imprisoning the Panic
  • Concurrency in Rust: Writing Safe and Efficient Code
  • Rust, WASM, and Edge: Next-Level Performance
  • Segmentation Violation and How Rust Helps Overcome It

Trending

  • Multi-Scale Feature Learning in CNN and U-Net Architectures
  • Data Contracts as the "Circuit Breaker" for Model Reliability
  • How to Format Articles for DZone
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  1. DZone
  2. Coding
  3. Languages
  4. Debugging a Segfault in Rust

Debugging a Segfault in Rust

By 
Liz Krane user avatar
Liz Krane
·
Jan Michael Auer user avatar
Jan Michael Auer
·
Jul. 09, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.8K Views

Join the DZone community and get the full member experience.

Join For Free

In this Sentry programming session, we looked at using our error monitoring platform to help debug one of our own native products: Symbolicator. This service is responsible for processing native crash reports. Just like any other application, it might crash — and when that happens, we need to gain insights into why.

Crashing Rust

Symbolicator is written in Rust, a language that is loved for its memory-safety. On top of that, the Rust standard library offers extremely well-designed APIs for explicit error handling, making unchecked exceptions a thing of the past. Yet, there are quite a few reasons why a Rust program can still crash, including:

  • The use of unsafe, often used for absolutely necessary optimization.
  • Interop with C and C++, which is inherently unsafe.
  • Stack overflows or out-of-memory situations.

We chose the first option to introduce a very reliable and unfortunately too well-known crash: The infamous segmentation fault. Look at this beauty:

Rust
xxxxxxxxxx
1
 
1
unsafe { *(0 as *mut u32) = 42; }


This is a slightly modified version of the code we used in the recorded session. It dereferences the null pointer and tries to assign it a value of 42. Totally unsafe, and guaranteed to crash. Fun fact: depending on the optimization level, this line might either cause a segmentation fault or an illegal instruction.

Symbolicator already uses our Rust SDK. It is able to report errors and panics to Sentry, can record breadcrumbs, and can even manage concurrent scopes. However, it cannot handle hard application crashes. For that, we need to haul out the big guns.

Using the Native SDK

Our Native SDK to the rescue! We chose the Crashpad distribution, which creates a minidump when the application crashes. The SDK builds a dynamic library that can be linked to our Rust application. To instruct Cargo, we added a few lines to the build script:

Rust
 




xxxxxxxxxx
1


 
1
println!("cargo:rustc-link-lib=sentry_crashpad");
2
println!("cargo:rustc-link-search=path/to/lib");
3
println!("cargo:rustc-cdylib-link-arg=-Wl,-rpath,@executable_path/.");



This tells the build system to link the dynamic library of our SDK, where to find it during the build, and where to find it at runtime. From now on, the dynamic library needs to be distributed alongside the main executable.

The Native SDK exposes a C API that is declared in the sentry.h header file. We chose to compile a small C file that initializes the SDK and exports a single function to our Rust code:

C
xxxxxxxxxx
1
10
 
1
#include <sentry.h>
2
3
void init_native(void)
4
{
5
    sentry_options_t *options = sentry_options_new();
6
    sentry_options_set_dsn(options, "__SENTRY_DSN__");
7
    sentry_options_set_debug(options, 1);
8
    sentry_options_set_handler_path(options, "crashpad_handler");
9
    sentry_init();
10
}


To build and link this program, we added a call to the cc crate in our build script:

Rust
xxxxxxxxxx
1
 
1
cc::Build::new()
2
    .file("native/src/init.c")
3
    .include("native/include")
4
    .compile("native")


Now, we are able to call the C function to initialize the Native SDK directly from Rust code. To do so, we declared the function signature in an extern "C" block. Calling a C function requires the use of unsafe, since the compiler can no longer reason about the side effects and contents of the function’s body.

Rust
xxxxxxxxxx
1
 
1
extern "C" {
2
    fn init_native();
3
}
4
5
unsafe {
6
    init_native();
7
}


Alternatively, you can call functions from the Native SDK directly. This requires FFI bindings for each function, which can be created conveniently using the bindgen crate at build time. It generates a Rust file from the C header. While we did not do this in our coding session — to keep it short — we generally recommend to use Sentry’s API directly from Rust.

Wrapping Up

Finally, we could upload debug information of our new Symbolicator build to Sentry. This is normally done as part of our deployment process, but since we were not going to deploy this obviously broken version of Symbolicator — not that we’ve ever done that! — we had to upload manually using sentry-cli (paths abbreviated for readability):

Shell
xxxxxxxxxx
1
 
1
$ sentry-cli upload-dif --include-sources symbolicator symbolicator.dSYM


The --include-sources flag makes source code available to Sentry. In the live session, we also used --wait to ensure that the files are processed by Sentry before we crash for the next time. Now, once the application crashes, the SDK sends a Minidump crash report to Sentry, which points right to the bug. See for yourself:

With debug information available, Sentry resolves Rust function names and even source context. In this case, you can clearly see how the stack trace points to our unsafe line of code. What a shame that not all issues will be this easy to debug.

Adding the Native SDK to one of our production Rust services was a fun exercise for us. We hope that you enjoyed this short excursion into the world of Rust with us, and would love to learn about your use cases. Targeting C and C++, Android NDK, and virtually any other language that compiles to machine code, the sentry-native is becoming one of our most versatile SDKs.

Rust (programming language)

Published at DZone with permission of Liz Krane. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Imprisoning the Panic
  • Concurrency in Rust: Writing Safe and Efficient Code
  • Rust, WASM, and Edge: Next-Level Performance
  • Segmentation Violation and How Rust Helps Overcome It

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook