From b7e7c65b17f886d2ba93b0fc464f6c4fa7f9918c Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Wed, 11 Nov 2026 18:32:48 -0300 Subject: [PATCH 1/2] fix(ios): send debug images with native exception frames so Sentry symbolicates them The throw-site stack chained from `nativeException` was built from `callStackSymbols` only: names resolved on the device and no `debug_meta` images, so Sentry could not symbolicate those frames against dSYMs. Release app executables are fully stripped, so their frames already showed wrong or missing names, and NativeScript runtimes that strip local symbols (NativeScript/ios#556) do the same for the runtime's internal functions. * Read `callStackReturnAddresses` alongside `callStackSymbols` and attach `image_addr`/`package` from sentry-cocoa's binary image cache to each frame. * Add one `macho` debug image per binary to `event.debug_meta.images`. * Mark the stack with `instruction_addr_adjustment: all`, since every address is a return address. * Keep the device names as the fallback when the image cache is empty (native SDK not started) or no dSYM is uploaded. --- src/sentry/integrations/nativeexception.ts | 103 +++++++++++++++++++-- 1 file changed, 94 insertions(+), 9 deletions(-) diff --git a/src/sentry/integrations/nativeexception.ts b/src/sentry/integrations/nativeexception.ts index e584857..1fd7b1a 100644 --- a/src/sentry/integrations/nativeexception.ts +++ b/src/sentry/integrations/nativeexception.ts @@ -1,4 +1,4 @@ -import type { Event, EventHint, Integration, StackFrame } from '@sentry/core'; +import type { DebugImage, Event, EventHint, Integration, StackFrame, Stacktrace } from '@sentry/core'; export const INTEGRATION_NAME = 'NativeException'; @@ -13,6 +13,8 @@ export interface NativeExceptionLike { userInfo?: unknown; /** NSException only — symbolicated frames captured at the native @throw site. */ callStackSymbols?: { count: number; objectAtIndex(index: number): string }; + /** NSException only — the same frames as raw return addresses, index-aligned with `callStackSymbols`. */ + callStackReturnAddresses?: { count: number; objectAtIndex(index: number): number }; /** NSError only */ domain?: string; code?: number; @@ -55,25 +57,108 @@ export function nativeExceptionOf(value: unknown): NativeExceptionLike | undefin // callStackSymbols line shape: "3 CoreFoundation 0x00000001804ae0f8 __exceptionPreprocess + 172" const NATIVE_STACK_FRAME_RE = /^\s*\d+\s+(\S+)\s+(0x[0-9a-fA-F]+)\s+(.+)$/; -function enrichWithIosException(event: Event, native: NativeExceptionLike): void { +/** Frame fields Sentry's native symbolication reads that `StackFrame` does not declare. */ +interface CocoaStackFrame extends StackFrame { + package?: string; + image_addr?: string; +} + +function formatAddress(address: number): string { + return `0x${address.toString(16).padStart(16, '0')}`; +} + +function binaryImageCache(): SentryBinaryImageCache | undefined { + try { + return typeof SentryDependencies !== 'undefined' ? SentryDependencies.binaryImageCache : undefined; + } catch { + return undefined; + } +} + +/** + * Builds the throw-site frames plus a debug image for every binary they touch. + * Names from `callStackSymbols` come from the device's symbol table, which is + * wrong or missing for stripped binaries (release app executables, the + * NativeScript runtime's internal functions). With the image list, Sentry + * symbolicates the addresses against the uploaded dSYMs instead; the device + * names stay as the fallback when no image or dSYM is found. + */ +function iosThrowSiteStack(native: NativeExceptionLike): { frames: CocoaStackFrame[]; images: DebugImage[] } | undefined { const symbols = native.callStackSymbols; - if (symbols) { - const frames: StackFrame[] = []; - for (let i = 0; i < symbols.count; i++) { + const addresses = native.callStackReturnAddresses; + const count = Math.max(symbols?.count ?? 0, addresses?.count ?? 0); + if (!count) { + return undefined; + } + // Empty until the native SDK has started; frames then fall back to device names. + const cache = binaryImageCache(); + const images = new Map(); + const frames: CocoaStackFrame[] = []; + for (let i = 0; i < count; i++) { + const frame: CocoaStackFrame = { platform: 'cocoa' }; + if (symbols && i < symbols.count) { const line = String(symbols.objectAtIndex(i)); const match = NATIVE_STACK_FRAME_RE.exec(line); - frames.push(match ? { platform: 'cocoa', module: match[1], instruction_addr: match[2], function: match[3] } : { platform: 'cocoa', function: line }); + if (match) { + frame.module = match[1]; + frame.instruction_addr = match[2]; + frame.function = match[3]; + } else { + frame.function = line; + } } - // callStackSymbols is newest-frame-first; Sentry wants oldest first. - frames.reverse(); + const address = addresses && i < addresses.count ? Number(addresses.objectAtIndex(i)) : 0; + if (address > 0) { + frame.instruction_addr = formatAddress(address); + const image = cache?.imageByAddress(address); + if (image) { + frame.package = String(image.name); + frame.image_addr = formatAddress(image.address); + if (!images.has(image.address) && image.uuid) { + images.set(image.address, { + type: 'macho', + debug_id: String(image.uuid), + image_addr: frame.image_addr, + image_size: Number(image.size), + code_file: frame.package + }); + } + } + } + frames.push(frame); + } + // Both arrays are newest-frame-first; Sentry wants oldest first. + frames.reverse(); + return { frames, images: [...images.values()] }; +} +function addDebugImages(event: Event, images: DebugImage[]): void { + if (!images.length) { + return; + } + event.debug_meta ??= {}; + event.debug_meta.images ??= []; + const known = new Set(event.debug_meta.images.map((image) => ('image_addr' in image ? image.image_addr : undefined))); + for (const image of images) { + if (!('image_addr' in image) || !known.has(image.image_addr)) { + event.debug_meta.images.push(image); + } + } +} + +function enrichWithIosException(event: Event, native: NativeExceptionLike): void { + const stack = iosThrowSiteStack(native); + if (stack) { event.exception ??= {}; event.exception.values ??= []; event.exception.values.unshift({ type: native.name !== undefined ? String(native.name) : 'NSException', value: String(native.reason ?? native.name ?? 'NSException'), - stacktrace: { frames } + // Every address is a return address, including the newest frame, so the + // symbolicator must step each one back into its call instruction. + stacktrace: { frames: stack.frames, instruction_addr_adjustment: 'all' } as Stacktrace }); + addDebugImages(event, stack.images); } const context: Record = {}; From cf935ef5183de93c4efe63504d5ee6978e4d0d8e Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Wed, 18 Nov 2026 13:54:23 -0300 Subject: [PATCH 2/2] fix(ios): send native frame names without their offset `callStackSymbols` lines end in "symbol + offset". Sentry trims a frame's `function` to a bare symbol name, so frames it could not symbolicate showed only the offset ("172" instead of "__exceptionPreprocess"). * Split the device name into `function` and `symbol_addr` (the line's own address minus the offset). --- src/sentry/integrations/nativeexception.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/sentry/integrations/nativeexception.ts b/src/sentry/integrations/nativeexception.ts index 1fd7b1a..e166f99 100644 --- a/src/sentry/integrations/nativeexception.ts +++ b/src/sentry/integrations/nativeexception.ts @@ -57,10 +57,15 @@ export function nativeExceptionOf(value: unknown): NativeExceptionLike | undefin // callStackSymbols line shape: "3 CoreFoundation 0x00000001804ae0f8 __exceptionPreprocess + 172" const NATIVE_STACK_FRAME_RE = /^\s*\d+\s+(\S+)\s+(0x[0-9a-fA-F]+)\s+(.+)$/; +// "__exceptionPreprocess + 172". Sentry trims `function` down to a bare symbol +// name, so sending the combined string shows only the offset ("172"). +const SYMBOL_OFFSET_RE = /^(.*\S)\s+\+\s+(\d+)$/; + /** Frame fields Sentry's native symbolication reads that `StackFrame` does not declare. */ interface CocoaStackFrame extends StackFrame { package?: string; image_addr?: string; + symbol_addr?: string; } function formatAddress(address: number): string { @@ -102,7 +107,15 @@ function iosThrowSiteStack(native: NativeExceptionLike): { frames: CocoaStackFra if (match) { frame.module = match[1]; frame.instruction_addr = match[2]; - frame.function = match[3]; + const symbol = SYMBOL_OFFSET_RE.exec(match[3]); + if (symbol) { + frame.function = symbol[1]; + // The offset is relative to this line's own address, which can + // differ from the matching `callStackReturnAddresses` entry. + frame.symbol_addr = formatAddress(parseInt(match[2], 16) - Number(symbol[2])); + } else { + frame.function = match[3]; + } } else { frame.function = line; }