nativeFrame.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. import {Fragment, MouseEvent, useContext, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import scrollToElement from 'scroll-to-element';
  4. import {Button} from 'sentry/components/button';
  5. import {
  6. getLeadHint,
  7. hasAssembly,
  8. hasContextRegisters,
  9. hasContextSource,
  10. hasContextVars,
  11. isExpandable,
  12. trimPackage,
  13. } from 'sentry/components/events/interfaces/frame/utils';
  14. import {formatAddress, parseAddress} from 'sentry/components/events/interfaces/utils';
  15. import {AnnotatedText} from 'sentry/components/events/meta/annotatedText';
  16. import {TraceEventDataSectionContext} from 'sentry/components/events/traceEventDataSection';
  17. import StrictClick from 'sentry/components/strictClick';
  18. import Tag from 'sentry/components/tag';
  19. import {Tooltip} from 'sentry/components/tooltip';
  20. import {SLOW_TOOLTIP_DELAY} from 'sentry/constants';
  21. import {IconChevron} from 'sentry/icons/iconChevron';
  22. import {IconFileBroken} from 'sentry/icons/iconFileBroken';
  23. import {IconRefresh} from 'sentry/icons/iconRefresh';
  24. import {IconWarning} from 'sentry/icons/iconWarning';
  25. import {t, tn} from 'sentry/locale';
  26. import DebugMetaStore from 'sentry/stores/debugMetaStore';
  27. import {space} from 'sentry/styles/space';
  28. import {
  29. Frame,
  30. PlatformKey,
  31. SentryAppComponent,
  32. SentryAppSchemaStacktraceLink,
  33. } from 'sentry/types';
  34. import {Event} from 'sentry/types/event';
  35. import {defined} from 'sentry/utils';
  36. import withSentryAppComponents from 'sentry/utils/withSentryAppComponents';
  37. import DebugImage from './debugMeta/debugImage';
  38. import {combineStatus} from './debugMeta/utils';
  39. import Context from './frame/context';
  40. import {SymbolicatorStatus} from './types';
  41. type Props = {
  42. components: SentryAppComponent<SentryAppSchemaStacktraceLink>[];
  43. event: Event;
  44. frame: Frame;
  45. isUsedForGrouping: boolean;
  46. platform: PlatformKey;
  47. registers: Record<string, string>;
  48. emptySourceNotation?: boolean;
  49. frameMeta?: Record<any, any>;
  50. hiddenFrameCount?: number;
  51. image?: React.ComponentProps<typeof DebugImage>['image'];
  52. includeSystemFrames?: boolean;
  53. isExpanded?: boolean;
  54. isHoverPreviewed?: boolean;
  55. isOnlyFrame?: boolean;
  56. isShowFramesToggleExpanded?: boolean;
  57. /**
  58. * Frames that are hidden under the most recent non-InApp frame
  59. */
  60. isSubFrame?: boolean;
  61. maxLengthOfRelativeAddress?: number;
  62. nextFrame?: Frame;
  63. onShowFramesToggle?: (event: React.MouseEvent<HTMLElement>) => void;
  64. prevFrame?: Frame;
  65. registersMeta?: Record<any, any>;
  66. showStackedFrames?: boolean;
  67. };
  68. function NativeFrame({
  69. frame,
  70. nextFrame,
  71. prevFrame,
  72. includeSystemFrames,
  73. isUsedForGrouping,
  74. maxLengthOfRelativeAddress,
  75. image,
  76. registers,
  77. isOnlyFrame,
  78. event,
  79. components,
  80. hiddenFrameCount,
  81. isShowFramesToggleExpanded,
  82. isSubFrame,
  83. onShowFramesToggle,
  84. isExpanded,
  85. platform,
  86. registersMeta,
  87. frameMeta,
  88. emptySourceNotation = false,
  89. /**
  90. * Is the stack trace being previewed in a hovercard?
  91. */
  92. isHoverPreviewed = false,
  93. }: Props) {
  94. const traceEventDataSectionContext = useContext(TraceEventDataSectionContext);
  95. const absolute = traceEventDataSectionContext?.display.includes('absolute-addresses');
  96. const fullStackTrace = traceEventDataSectionContext?.fullStackTrace;
  97. const fullFunctionName = traceEventDataSectionContext?.display.includes(
  98. 'verbose-function-names'
  99. );
  100. const absoluteFilePaths =
  101. traceEventDataSectionContext?.display.includes('absolute-file-paths');
  102. const tooltipDelay = isHoverPreviewed ? SLOW_TOOLTIP_DELAY : undefined;
  103. const foundByStackScanning = frame.trust === 'scan' || frame.trust === 'cfi-scan';
  104. const startingAddress = image ? image.image_addr : null;
  105. const packageClickable =
  106. !!frame.symbolicatorStatus &&
  107. frame.symbolicatorStatus !== SymbolicatorStatus.UNKNOWN_IMAGE &&
  108. !isHoverPreviewed;
  109. const leadsToApp = !frame.inApp && ((nextFrame && nextFrame.inApp) || !nextFrame);
  110. const expandable =
  111. !leadsToApp || includeSystemFrames
  112. ? isExpandable({
  113. frame,
  114. registers,
  115. platform,
  116. emptySourceNotation,
  117. isOnlyFrame,
  118. })
  119. : false;
  120. const inlineFrame =
  121. prevFrame &&
  122. platform === (prevFrame.platform || platform) &&
  123. frame.instructionAddr === prevFrame.instructionAddr;
  124. const functionNameHiddenDetails =
  125. defined(frame.rawFunction) &&
  126. defined(frame.function) &&
  127. frame.function !== frame.rawFunction;
  128. const [expanded, setExpanded] = useState(expandable ? isExpanded ?? false : false);
  129. function getRelativeAddress() {
  130. if (!startingAddress) {
  131. return '';
  132. }
  133. const relativeAddress = formatAddress(
  134. parseAddress(frame.instructionAddr) - parseAddress(startingAddress),
  135. maxLengthOfRelativeAddress
  136. );
  137. return `+${relativeAddress}`;
  138. }
  139. function getAddressTooltip() {
  140. if (inlineFrame && foundByStackScanning) {
  141. return t('Inline frame, found by stack scanning');
  142. }
  143. if (inlineFrame) {
  144. return t('Inline frame');
  145. }
  146. if (foundByStackScanning) {
  147. return t('Found by stack scanning');
  148. }
  149. return undefined;
  150. }
  151. function getFunctionName() {
  152. if (functionNameHiddenDetails && fullFunctionName && frame.rawFunction) {
  153. return {
  154. value: frame.rawFunction,
  155. meta: frameMeta?.rawFunction?.[''],
  156. };
  157. }
  158. if (frame.function) {
  159. return {
  160. value: frame.function,
  161. meta: frameMeta?.function?.[''],
  162. };
  163. }
  164. return undefined;
  165. }
  166. // this is the status of image that belongs to this frame
  167. function getStatus() {
  168. // If a matching debug image doesn't exist, fall back to symbolicator_status
  169. if (!image) {
  170. switch (frame.symbolicatorStatus) {
  171. case SymbolicatorStatus.SYMBOLICATED:
  172. return 'success';
  173. case SymbolicatorStatus.MISSING:
  174. case SymbolicatorStatus.MALFORMED:
  175. return 'error';
  176. case SymbolicatorStatus.MISSING_SYMBOL:
  177. case SymbolicatorStatus.UNKNOWN_IMAGE:
  178. default:
  179. return undefined;
  180. }
  181. }
  182. const combinedStatus = combineStatus(image.debug_status, image.unwind_status);
  183. switch (combinedStatus) {
  184. case 'unused':
  185. return undefined;
  186. case 'found':
  187. return 'success';
  188. default:
  189. return 'error';
  190. }
  191. }
  192. function handleGoToImagesLoaded(e: MouseEvent) {
  193. e.stopPropagation(); // to prevent collapsing if collapsible
  194. if (frame.instructionAddr) {
  195. const searchTerm =
  196. !(!frame.addrMode || frame.addrMode === 'abs') && image
  197. ? `${image.debug_id}!${frame.instructionAddr}`
  198. : frame.instructionAddr;
  199. DebugMetaStore.updateFilter(searchTerm);
  200. }
  201. scrollToElement('#images-loaded');
  202. }
  203. function handleToggleContext(e: MouseEvent) {
  204. if (!expandable) {
  205. return;
  206. }
  207. e.preventDefault();
  208. setExpanded(!expanded);
  209. }
  210. const relativeAddress = getRelativeAddress();
  211. const addressTooltip = getAddressTooltip();
  212. const functionName = getFunctionName();
  213. const status = getStatus();
  214. return (
  215. <StackTraceFrame data-test-id="stack-trace-frame">
  216. <StrictClick onClick={handleToggleContext}>
  217. <RowHeader
  218. expandable={expandable}
  219. expanded={expanded}
  220. isInAppFrame={frame.inApp}
  221. isSubFrame={!!isSubFrame}
  222. >
  223. <SymbolicatorIcon>
  224. {status === 'error' ? (
  225. <Tooltip
  226. title={t(
  227. 'This frame has missing debug files and could not be symbolicated'
  228. )}
  229. >
  230. <IconFileBroken
  231. size="sm"
  232. color="errorText"
  233. data-test-id="symbolication-error-icon"
  234. />
  235. </Tooltip>
  236. ) : status === undefined ? (
  237. <Tooltip
  238. title={t(
  239. 'This frame has an unknown problem and could not be symbolicated'
  240. )}
  241. >
  242. <IconWarning
  243. size="sm"
  244. color="warningText"
  245. data-test-id="symbolication-warning-icon"
  246. />
  247. </Tooltip>
  248. ) : null}
  249. </SymbolicatorIcon>
  250. <div>
  251. {!fullStackTrace && !expanded && leadsToApp && (
  252. <Fragment>
  253. <PackageNote>
  254. {getLeadHint({event, hasNextFrame: defined(nextFrame)})}
  255. </PackageNote>
  256. </Fragment>
  257. )}
  258. <Tooltip
  259. title={frame.package ?? t('Go to images loaded')}
  260. position="bottom"
  261. containerDisplayMode="inline-flex"
  262. delay={tooltipDelay}
  263. >
  264. <Package>
  265. {frame.package ? trimPackage(frame.package) : `<${t('unknown')}>`}
  266. </Package>
  267. </Tooltip>
  268. </div>
  269. <AddressCellWrapper>
  270. <AddressCell onClick={packageClickable ? handleGoToImagesLoaded : undefined}>
  271. <Tooltip
  272. title={addressTooltip}
  273. disabled={!(foundByStackScanning || inlineFrame)}
  274. delay={tooltipDelay}
  275. >
  276. {!relativeAddress || absolute ? frame.instructionAddr : relativeAddress}
  277. </Tooltip>
  278. </AddressCell>
  279. </AddressCellWrapper>
  280. <FunctionNameCell>
  281. {functionName ? (
  282. <AnnotatedText value={functionName.value} meta={functionName.meta} />
  283. ) : (
  284. `<${t('unknown')}>`
  285. )}{' '}
  286. {frame.filename && (
  287. <Tooltip
  288. title={frame.absPath}
  289. disabled={!(defined(frame.absPath) && frame.absPath !== frame.filename)}
  290. delay={tooltipDelay}
  291. isHoverable
  292. >
  293. <FileName>
  294. {'('}
  295. {absoluteFilePaths ? frame.absPath : frame.filename}
  296. {frame.lineNo && `:${frame.lineNo}`}
  297. {')'}
  298. </FileName>
  299. </Tooltip>
  300. )}
  301. </FunctionNameCell>
  302. <GroupingCell>
  303. {isUsedForGrouping && (
  304. <Tooltip title={t('This frame is repeated in every event of this issue')}>
  305. <IconRefresh size="sm" color="textColor" />
  306. </Tooltip>
  307. )}
  308. </GroupingCell>
  309. {hiddenFrameCount ? (
  310. <ShowHideButton
  311. analyticsEventName="Stacktrace Frames: toggled"
  312. analyticsEventKey="stacktrace_frames.toggled"
  313. analyticsParams={{
  314. frame_count: hiddenFrameCount,
  315. is_frame_expanded: isShowFramesToggleExpanded,
  316. }}
  317. size="xs"
  318. borderless
  319. onClick={e => {
  320. onShowFramesToggle?.(e);
  321. }}
  322. >
  323. {isShowFramesToggleExpanded
  324. ? tn('Hide %s more frame', 'Hide %s more frames', hiddenFrameCount)
  325. : tn('Show %s more frame', 'Show %s more frames', hiddenFrameCount)}
  326. </ShowHideButton>
  327. ) : null}
  328. <TypeCell>{frame.inApp ? <Tag type="info">{t('In App')}</Tag> : null}</TypeCell>
  329. <ExpandCell>
  330. {expandable && (
  331. <ToggleButton
  332. size="zero"
  333. title={t('Toggle Context')}
  334. aria-label={t('Toggle Context')}
  335. tooltipProps={isHoverPreviewed ? {delay: SLOW_TOOLTIP_DELAY} : undefined}
  336. icon={
  337. <IconChevron legacySize="8px" direction={expanded ? 'up' : 'down'} />
  338. }
  339. />
  340. )}
  341. </ExpandCell>
  342. </RowHeader>
  343. </StrictClick>
  344. {expanded && (
  345. <Registers
  346. frame={frame}
  347. event={event}
  348. registers={registers}
  349. components={components}
  350. hasContextSource={hasContextSource(frame)}
  351. hasContextVars={hasContextVars(frame)}
  352. hasContextRegisters={hasContextRegisters(registers)}
  353. emptySourceNotation={emptySourceNotation}
  354. hasAssembly={hasAssembly(frame, platform)}
  355. isExpanded={expanded}
  356. registersMeta={registersMeta}
  357. frameMeta={frameMeta}
  358. />
  359. )}
  360. </StackTraceFrame>
  361. );
  362. }
  363. export default withSentryAppComponents(NativeFrame, {componentType: 'stacktrace-link'});
  364. const AddressCellWrapper = styled('div')`
  365. display: flex;
  366. `;
  367. const AddressCell = styled('div')`
  368. font-family: ${p => p.theme.text.familyMono};
  369. ${p => p.onClick && `cursor: pointer`};
  370. ${p => p.onClick && `color:` + p.theme.linkColor};
  371. `;
  372. const FunctionNameCell = styled('div')`
  373. word-break: break-all;
  374. @media (max-width: ${p => p.theme.breakpoints.small}) {
  375. grid-column: 2/6;
  376. }
  377. `;
  378. const GroupingCell = styled('div')`
  379. @media (max-width: ${p => p.theme.breakpoints.small}) {
  380. grid-row: 2/3;
  381. }
  382. `;
  383. const TypeCell = styled('div')`
  384. @media (max-width: ${p => p.theme.breakpoints.small}) {
  385. grid-column: 5/6;
  386. grid-row: 1/2;
  387. }
  388. `;
  389. const ExpandCell = styled('div')`
  390. @media (max-width: ${p => p.theme.breakpoints.small}) {
  391. grid-column: 6/7;
  392. grid-row: 1/2;
  393. }
  394. `;
  395. const ToggleButton = styled(Button)`
  396. width: 16px;
  397. height: 16px;
  398. `;
  399. const Registers = styled(Context)`
  400. border-bottom: 1px solid ${p => p.theme.border};
  401. padding: 0;
  402. margin: 0;
  403. `;
  404. const PackageNote = styled('div')`
  405. color: ${p => p.theme.subText};
  406. font-size: ${p => p.theme.fontSizeExtraSmall};
  407. `;
  408. const Package = styled('span')`
  409. white-space: nowrap;
  410. overflow: hidden;
  411. text-overflow: ellipsis;
  412. width: 100%;
  413. padding-right: 2px; /* Needed to prevent text cropping with italic font */
  414. `;
  415. const FileName = styled('span')`
  416. color: ${p => p.theme.subText};
  417. border-bottom: 1px dashed ${p => p.theme.border};
  418. `;
  419. const RowHeader = styled('span')<{
  420. expandable: boolean;
  421. expanded: boolean;
  422. isInAppFrame: boolean;
  423. isSubFrame: boolean;
  424. }>`
  425. display: grid;
  426. grid-template-columns: repeat(2, auto) 1fr repeat(2, auto) ${space(2)};
  427. grid-template-rows: repeat(2, auto);
  428. align-items: center;
  429. align-content: center;
  430. column-gap: ${space(1)};
  431. background-color: ${p =>
  432. !p.isInAppFrame && p.isSubFrame
  433. ? `${p.theme.surface100}`
  434. : `${p.theme.bodyBackground}`};
  435. font-size: ${p => p.theme.codeFontSize};
  436. padding: ${space(1)};
  437. color: ${p => (!p.isInAppFrame ? p.theme.subText : '')};
  438. font-style: ${p => (!p.isInAppFrame ? 'italic' : '')};
  439. ${p => p.expandable && `cursor: pointer;`};
  440. @media (min-width: ${p => p.theme.breakpoints.small}) {
  441. grid-template-columns: auto 150px 120px 4fr repeat(2, auto) ${space(2)};
  442. padding: ${space(0.5)} ${space(1.5)};
  443. min-height: 32px;
  444. }
  445. `;
  446. const StackTraceFrame = styled('li')`
  447. :not(:last-child) {
  448. ${RowHeader} {
  449. border-bottom: 1px solid ${p => p.theme.border};
  450. }
  451. }
  452. `;
  453. const SymbolicatorIcon = styled('div')`
  454. width: ${p => p.theme.iconSizes.sm};
  455. `;
  456. const ShowHideButton = styled(Button)`
  457. color: ${p => p.theme.subText};
  458. font-style: italic;
  459. font-weight: normal;
  460. padding: ${space(0.25)} ${space(0.5)};
  461. &:hover {
  462. color: ${p => p.theme.subText};
  463. }
  464. `;