moonbit-native-crash

    Build-matched symbol bundles and offline crash diagnosis for MoonBit Native.

    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    9 hours ago
    Downloads
    9

    #MoonBit Native crash diagnostics

    Archive debug symbols with a Native release and use them later to resolve a crash dump offline. The same CLI and MoonBit API read Windows x64 Minidumps and Linux x64 ELF cores on either host.

    #Archive a build

    Keep one image and its debug file for each executable or shared library you publish. On Windows, link with a full PDB and keep it with the matching .exe or .dll. On Linux, link with a GNU Build ID. To create a separate debug file while retaining the Build ID in the shipped image, run the following on the original unstripped executable. Microsoft SymStore · GDB separate debug files

    objcopy --only-keep-debug app app.debug strip --strip-debug app

    From the diagnostics checkout, archive the image and symbols into a new directory. Pass any additional image and symbol pairs in the same command. The directory can hold Windows and Linux entries together.

    moon run cmd --target native -- pack symbols-linux app app.debug libplugin.so libplugin.so.debug moon run cmd --target native -- verify symbols-linux moon run cmd --target native -- analyze symbols-linux crash.core

    moon run cmd --target native -- pack symbols-windows app.exe app.pdb plugin.dll plugin.pdb moon run cmd --target native -- verify symbols-windows moon run cmd --target native -- analyze symbols-windows crash.dmp

    Store the whole directory as a release or CI artifact. When the symbol bundle is unavailable, moon run cmd --target native -- analyze crash.core still reports the faulting thread, exception, loaded modules and addresses. Both formats are detected from the file contents.

    #MoonBit API

    Add "Nanaloveyuki/moonbit-native-crash@0.1.0" to the imports in your moon.mod and "Nanaloveyuki/moonbit-native-crash" @crash to the imports in moon.pkg. To use a local checkout, list your application and the checkout as members of the same moon.work. The API runs on the Native target. analyze accepts dump bytes when the caller already holds a capture in memory.

    async fn diagnose(bundle_dir : String, dump_file : String) -> @crash.Report {
    let bundle = @crash.Bundle::open(bundle_dir)
    @crash.analyze_file(dump_file, bundle~)
    }

    Use @crash.pack with SymbolPair values to create a bundle and Bundle::verify to check the full archive. Bundle::open validates the manifest. Analysis verifies each image and symbol file it uses. @json.to_json(report) produces the same JSON as the CLI.

    #Bundle and report formats

    manifest.json has the schema moonbit-native-crash/symbol-bundle/v1 and an entries array. Every entry contains platform, architecture, module_name, build_id, image, symbols, image_sha256 and symbols_sha256. image and symbols are relative names of the form <platform>-<build_id>.image and <platform>-<build_id>.symbols. The Build ID is lowercase hexadecimal on Linux and a lowercase CodeView GUID followed by a hyphen and decimal Age on Windows. SHA-256 values are lowercase hexadecimal. Matching uses the identity recorded in the dump. Linux cores need to retain the mapped ELF headers and Build ID notes for the affected module.

    The moonbit-native-crash/report/v1 report gives the faulting thread and exception, the loaded modules with half-open address ranges, and frames in fault-first order. Addresses and thread IDs use 0x-prefixed strings in JSON, preserving all 64 bits. A return address may be adjusted by one byte for lookup. The frame’s kind identifies the recovery method. Missing locations are null.

    Windows uses unwind metadata from matching PE images and can fall back to frame pointers. Linux walks the saved x64 frame-pointer chain. Builds that omit frame pointers can have shorter Linux stacks. Linux source lookup reads uncompressed DWARF 2–5 line tables.

    When reading an ELF core from a file, the analyzer seeks directly to the program headers, notes, mapped image headers and stack records it needs. This keeps memory use tied to the diagnostic data even for multi-gigabyte cores.

    Frame statusMeaning
    resolvedAt least a function or a source file and line was found.
    missing_symbolsThe bundle or a needed artifact is absent.
    identity_mismatchThe bundle contains a differently built module of the same name, or a file has a different Build ID or GUID and Age.
    integrity_mismatchThe identity matches and the SHA-256 differs.
    identity_unavailableThe dump has no usable build identity for the module.
    no_moduleThe instruction is outside the recorded module ranges.
    unresolvedThe matching symbols give no location, or a required artifact cannot be read or parsed.

    pack and verify return a nonzero exit status on errors. analyze returns a report for readable dumps even when individual frames are unresolved. A dump error or invalid manifest makes the command fail.

    #Reproduce a capture

    The standalone sample deliberately traps when the capture script enables it. The Orbit sample uses the same trigger inside an Orbit desktop launch. The examples require MoonBit Native and Python 3. Linux capture additionally uses GCC, GDB and GNU binutils. Orbit needs a graphical session and the dependencies of its WebView backend. The CI workflow generates the same inputs, archives the symbols, and analyzes dumps from both platforms. Standalone sample · Orbit sample · CI workflow

    python3 examples/capture.py artifacts/linux --alternate python3 examples/capture.py artifacts/orbit --orbit python3 examples/check.py artifacts/linux artifacts/orbit

    On Windows, run the capture in an x64 Visual Studio developer shell. The capture script uses MOONBIT_NEW_NATIVE=0 so the C backend carries .mbt positions into CodeView. With MoonBit CLI 0.1.20261126 and moonc 1.0.0-rc1+3c0520448, the default Windows object backend omits MoonBit CodeView line records. The C backend reports .mbt paths, and its generated line mappings can drift from the original source. The Windows sample verifies the C fault line. The Linux sample and Orbit build resolve MoonBit call sites against the original .mbt sources.

    python examples/capture.py artifacts/windows --alternate python examples/check.py artifacts/windows

    Minidumps can include stack and process memory. The example scripts write captures to a local directory, and CI captures only the self-contained demonstration apps. Keep application captures in the same access boundary as the process data they contain.

    CrashError

    pub(all) suberror CrashError {
    InvalidManifest(String)
    UnsupportedFormat(String)
    IdentityMismatch(String)
    IntegrityMismatch(String)
    MissingArtifact(String)
    InvalidArtifact(String)
    InvalidInput(String)
    } derive(
    Debug
    )

    impl Show for CrashError

    AddressRange

    pub struct AddressRange {
    start : UInt64
    end : UInt64
    } derive(
    Debug
    )

    Bundle

    pub struct Bundle {
    manifest : Manifest
    // private fields
    }

    Bundle::open

    async fn Bundle::open(directory : String) -> Bundle

    Load the manifest. Artifact checks run on demand during analysis or through verify.

    Bundle::verify

    async fn Bundle::verify(self : Bundle) -> Unit

    Verify both artifact identities and SHA-256 checksums for every manifest entry.

    FrameReport

    pub struct FrameReport {
    address : UInt64
    lookup_address : UInt64
    kind : String
    module_path : String?
    module_offset : UInt64?
    build_id : String?
    status : FrameStatus
    function : String?
    file : String?
    line : Int?
    detail : String?
    } derive(
    Debug
    )

    FrameStatus

    pub(all) enum FrameStatus {
    Resolved
    Unresolved
    NoModule
    MissingSymbols
    IdentityUnavailable
    IdentityMismatch
    IntegrityMismatch
    } derive(Eq,
    Debug
    )

    FrameStatus::name

    fn FrameStatus::name(self : FrameStatus) -> String

    LoadedModule

    pub struct LoadedModule {
    path : String
    base : UInt64
    build_id : String?
    ranges : ReadOnlyArray[AddressRange]
    } derive(
    Debug
    )

    Manifest

    pub struct Manifest {
    schema : String
    entries : ReadOnlyArray[ManifestEntry]
    } derive(
    Debug
    ,
    ToJson
    )

    ManifestEntry

    pub struct ManifestEntry {
    platform : String
    architecture : String
    build_id : String
    module_name : String
    image : String
    image_sha256 : String
    symbols : String
    symbols_sha256 : String
    } derive(
    Debug
    ,
    FromJson
    ,
    ToJson
    )

    Platform

    pub(all) enum Platform {
    Linux
    Windows
    } derive(Eq,
    Debug
    )

    Platform::name

    fn Platform::name(self : Platform) -> String

    Report

    pub struct Report {
    platform : Platform
    architecture : String
    thread_id : UInt64
    exception : String
    modules : ReadOnlyArray[LoadedModule]
    frames : ReadOnlyArray[FrameReport]
    } derive(
    Debug
    )

    SymbolPair

    pub(all) struct SymbolPair {
    image : String
    symbols : String
    } derive(
    Debug
    )

    An image and its corresponding ELF debug file or PDB from one build.

    analyze

    async fn analyze(dump_bytes : Bytes, bundle? : Bundle) -> Report

    Analyze the faulting thread of a Windows x64 Minidump or Linux x64 ELF core.

    analyze_file

    async fn analyze_file(dump_path : String, bundle? : Bundle) -> Report

    pack

    async fn pack(directory : String, pairs : ArrayView[SymbolPair]) -> Bundle

    Build and publish a bundle at a new directory on the same filesystem as its staging files.