nativeFrame.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. function getStatus() {
  167. // this is the status of image that belongs to this frame
  168. if (!image) {
  169. return undefined;
  170. }
  171. const combinedStatus = combineStatus(image.debug_status, image.unwind_status);
  172. switch (combinedStatus) {
  173. case 'unused':
  174. return undefined;
  175. case 'found':
  176. return 'success';
  177. default:
  178. return 'error';
  179. }
  180. }
  181. function handleGoToImagesLoaded(e: MouseEvent) {
  182. e.stopPropagation(); // to prevent collapsing if collapsible
  183. if (frame.instructionAddr) {
  184. const searchTerm =
  185. !(!frame.addrMode || frame.addrMode === 'abs') && image
  186. ? `${image.debug_id}!${frame.instructionAddr}`
  187. : frame.instructionAddr;
  188. DebugMetaStore.updateFilter(searchTerm);
  189. }
  190. scrollToElement('#images-loaded');
  191. }
  192. function handleToggleContext(e: MouseEvent) {
  193. if (!expandable) {
  194. return;
  195. }
  196. e.preventDefault();
  197. setExpanded(!expanded);
  198. }
  199. const relativeAddress = getRelativeAddress();
  200. const addressTooltip = getAddressTooltip();
  201. const functionName = getFunctionName();
  202. const status = getStatus();
  203. return (
  204. <StackTraceFrame data-test-id="stack-trace-frame">
  205. <StrictClick onClick={handleToggleContext}>
  206. <RowHeader
  207. expandable={expandable}
  208. expanded={expanded}
  209. isInAppFrame={frame.inApp}
  210. isSubFrame={!!isSubFrame}
  211. >
  212. <SymbolicatorIcon>
  213. {status === 'error' ? (
  214. <Tooltip
  215. title={t(
  216. 'This frame has missing debug files and could not be symbolicated'
  217. )}
  218. >
  219. <IconFileBroken size="sm" color="errorText" />
  220. </Tooltip>
  221. ) : status === undefined ? (
  222. <Tooltip
  223. title={t(
  224. 'This frame has an unknown problem and could not be symbolicated'
  225. )}
  226. >
  227. <IconWarning size="sm" color="warningText" />
  228. </Tooltip>
  229. ) : null}
  230. </SymbolicatorIcon>
  231. <div>
  232. {!fullStackTrace && !expanded && leadsToApp && (
  233. <Fragment>
  234. <PackageNote>
  235. {getLeadHint({event, hasNextFrame: defined(nextFrame)})}
  236. </PackageNote>
  237. </Fragment>
  238. )}
  239. <Tooltip
  240. title={frame.package ?? t('Go to images loaded')}
  241. position="bottom"
  242. containerDisplayMode="inline-flex"
  243. delay={tooltipDelay}
  244. >
  245. <Package>
  246. {frame.package ? trimPackage(frame.package) : `<${t('unknown')}>`}
  247. </Package>
  248. </Tooltip>
  249. </div>
  250. <AddressCellWrapper>
  251. <AddressCell onClick={packageClickable ? handleGoToImagesLoaded : undefined}>
  252. <Tooltip
  253. title={addressTooltip}
  254. disabled={!(foundByStackScanning || inlineFrame)}
  255. delay={tooltipDelay}
  256. >
  257. {!relativeAddress || absolute ? frame.instructionAddr : relativeAddress}
  258. </Tooltip>
  259. </AddressCell>
  260. </AddressCellWrapper>
  261. <FunctionNameCell>
  262. {functionName ? (
  263. <AnnotatedText value={functionName.value} meta={functionName.meta} />
  264. ) : (
  265. `<${t('unknown')}>`
  266. )}{' '}
  267. {frame.filename && (
  268. <Tooltip
  269. title={frame.absPath}
  270. disabled={!(defined(frame.absPath) && frame.absPath !== frame.filename)}
  271. delay={tooltipDelay}
  272. >
  273. <FileName>
  274. {'('}
  275. {absoluteFilePaths ? frame.absPath : frame.filename}
  276. {frame.lineNo && `:${frame.lineNo}`}
  277. {')'}
  278. </FileName>
  279. </Tooltip>
  280. )}
  281. </FunctionNameCell>
  282. <GroupingCell>
  283. {isUsedForGrouping && (
  284. <Tooltip title={t('This frame is repeated in every event of this issue')}>
  285. <IconRefresh size="sm" color="textColor" />
  286. </Tooltip>
  287. )}
  288. </GroupingCell>
  289. {hiddenFrameCount ? (
  290. <ShowHideButton
  291. analyticsEventName="Stacktrace Frames: toggled"
  292. analyticsEventKey="stacktrace_frames.toggled"
  293. analyticsParams={{
  294. frame_count: hiddenFrameCount,
  295. is_frame_expanded: isShowFramesToggleExpanded,
  296. }}
  297. size="xs"
  298. borderless
  299. onClick={e => {
  300. onShowFramesToggle?.(e);
  301. }}
  302. >
  303. {isShowFramesToggleExpanded
  304. ? tn('Hide %s more frame', 'Hide %s more frames', hiddenFrameCount)
  305. : tn('Show %s more frame', 'Show %s more frames', hiddenFrameCount)}
  306. </ShowHideButton>
  307. ) : null}
  308. <TypeCell>{frame.inApp ? <Tag type="info">{t('In App')}</Tag> : null}</TypeCell>
  309. <ExpandCell>
  310. {expandable && (
  311. <ToggleButton
  312. size="zero"
  313. title={t('Toggle Context')}
  314. aria-label={t('Toggle Context')}
  315. tooltipProps={isHoverPreviewed ? {delay: SLOW_TOOLTIP_DELAY} : undefined}
  316. icon={
  317. <IconChevron legacySize="8px" direction={expanded ? 'up' : 'down'} />
  318. }
  319. />
  320. )}
  321. </ExpandCell>
  322. </RowHeader>
  323. </StrictClick>
  324. {expanded && (
  325. <Registers
  326. frame={frame}
  327. event={event}
  328. registers={registers}
  329. components={components}
  330. hasContextSource={hasContextSource(frame)}
  331. hasContextVars={hasContextVars(frame)}
  332. hasContextRegisters={hasContextRegisters(registers)}
  333. emptySourceNotation={emptySourceNotation}
  334. hasAssembly={hasAssembly(frame, platform)}
  335. expandable={expandable}
  336. isExpanded={expanded}
  337. registersMeta={registersMeta}
  338. frameMeta={frameMeta}
  339. />
  340. )}
  341. </StackTraceFrame>
  342. );
  343. }
  344. export default withSentryAppComponents(NativeFrame, {componentType: 'stacktrace-link'});
  345. const AddressCellWrapper = styled('div')`
  346. display: flex;
  347. `;
  348. const AddressCell = styled('div')`
  349. font-family: ${p => p.theme.text.familyMono};
  350. ${p => p.onClick && `cursor: pointer`};
  351. ${p => p.onClick && `color:` + p.theme.linkColor};
  352. `;
  353. const FunctionNameCell = styled('div')`
  354. word-break: break-all;
  355. @media (max-width: ${p => p.theme.breakpoints.small}) {
  356. grid-column: 2/6;
  357. }
  358. `;
  359. const GroupingCell = styled('div')`
  360. @media (max-width: ${p => p.theme.breakpoints.small}) {
  361. grid-row: 2/3;
  362. }
  363. `;
  364. const TypeCell = styled('div')`
  365. @media (max-width: ${p => p.theme.breakpoints.small}) {
  366. grid-column: 5/6;
  367. grid-row: 1/2;
  368. }
  369. `;
  370. const ExpandCell = styled('div')`
  371. @media (max-width: ${p => p.theme.breakpoints.small}) {
  372. grid-column: 6/7;
  373. grid-row: 1/2;
  374. }
  375. `;
  376. const ToggleButton = styled(Button)`
  377. width: 16px;
  378. height: 16px;
  379. `;
  380. const Registers = styled(Context)`
  381. border-bottom: 1px solid ${p => p.theme.border};
  382. padding: 0;
  383. margin: 0;
  384. `;
  385. const PackageNote = styled('div')`
  386. color: ${p => p.theme.subText};
  387. font-size: ${p => p.theme.fontSizeExtraSmall};
  388. `;
  389. const Package = styled('span')`
  390. white-space: nowrap;
  391. overflow: hidden;
  392. text-overflow: ellipsis;
  393. width: 100%;
  394. padding-right: 2px; /* Needed to prevent text cropping with italic font */
  395. `;
  396. const FileName = styled('span')`
  397. color: ${p => p.theme.subText};
  398. border-bottom: 1px dashed ${p => p.theme.border};
  399. `;
  400. const RowHeader = styled('span')<{
  401. expandable: boolean;
  402. expanded: boolean;
  403. isInAppFrame: boolean;
  404. isSubFrame: boolean;
  405. }>`
  406. display: grid;
  407. grid-template-columns: repeat(2, auto) 1fr repeat(2, auto) ${space(2)};
  408. grid-template-rows: repeat(2, auto);
  409. align-items: center;
  410. align-content: center;
  411. column-gap: ${space(1)};
  412. background-color: ${p =>
  413. !p.isInAppFrame && p.isSubFrame
  414. ? `${p.theme.surface100}`
  415. : `${p.theme.bodyBackground}`};
  416. font-size: ${p => p.theme.codeFontSize};
  417. padding: ${space(1)};
  418. color: ${p => (!p.isInAppFrame ? p.theme.subText : '')};
  419. font-style: ${p => (!p.isInAppFrame ? 'italic' : '')};
  420. ${p => p.expandable && `cursor: pointer;`};
  421. @media (min-width: ${p => p.theme.breakpoints.small}) {
  422. grid-template-columns: auto 150px 120px 4fr repeat(2, auto) ${space(2)};
  423. padding: ${space(0.5)} ${space(1.5)};
  424. min-height: 32px;
  425. }
  426. `;
  427. const StackTraceFrame = styled('li')`
  428. :not(:last-child) {
  429. ${RowHeader} {
  430. border-bottom: 1px solid ${p => p.theme.border};
  431. }
  432. }
  433. `;
  434. const SymbolicatorIcon = styled('div')`
  435. width: ${p => p.theme.iconSizes.sm};
  436. `;
  437. const ShowHideButton = styled(Button)`
  438. color: ${p => p.theme.subText};
  439. font-style: italic;
  440. font-weight: normal;
  441. padding: ${space(0.25)} ${space(0.5)};
  442. &:hover {
  443. color: ${p => p.theme.subText};
  444. }
  445. `;