nativeFrame.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 {Frame, PlatformType, SentryAppComponent} from 'sentry/types';
  29. import {Event} from 'sentry/types/event';
  30. import {defined} from 'sentry/utils';
  31. import useOrganization from 'sentry/utils/useOrganization';
  32. import withSentryAppComponents from 'sentry/utils/withSentryAppComponents';
  33. import DebugImage from './debugMeta/debugImage';
  34. import {combineStatus} from './debugMeta/utils';
  35. import Context from './frame/context';
  36. import {SymbolicatorStatus} from './types';
  37. type Props = {
  38. components: Array<SentryAppComponent>;
  39. event: Event;
  40. frame: Frame;
  41. isUsedForGrouping: boolean;
  42. platform: PlatformType;
  43. registers: Record<string, string>;
  44. emptySourceNotation?: boolean;
  45. frameMeta?: Record<any, any>;
  46. hiddenFrameCount?: number;
  47. image?: React.ComponentProps<typeof DebugImage>['image'];
  48. includeSystemFrames?: boolean;
  49. isExpanded?: boolean;
  50. isHoverPreviewed?: boolean;
  51. isOnlyFrame?: boolean;
  52. isShowFramesToggleExpanded?: boolean;
  53. /**
  54. * Frames that are hidden under the most recent non-InApp frame
  55. */
  56. isSubFrame?: boolean;
  57. maxLengthOfRelativeAddress?: number;
  58. nextFrame?: Frame;
  59. onShowFramesToggle?: (event: React.MouseEvent<HTMLElement>) => void;
  60. prevFrame?: Frame;
  61. registersMeta?: Record<any, any>;
  62. showStackedFrames?: boolean;
  63. };
  64. function NativeFrame({
  65. frame,
  66. nextFrame,
  67. prevFrame,
  68. includeSystemFrames,
  69. isUsedForGrouping,
  70. maxLengthOfRelativeAddress,
  71. image,
  72. registers,
  73. isOnlyFrame,
  74. event,
  75. components,
  76. hiddenFrameCount,
  77. isShowFramesToggleExpanded,
  78. isSubFrame,
  79. onShowFramesToggle,
  80. isExpanded,
  81. platform,
  82. registersMeta,
  83. frameMeta,
  84. emptySourceNotation = false,
  85. /**
  86. * Is the stack trace being previewed in a hovercard?
  87. */
  88. isHoverPreviewed = false,
  89. }: Props) {
  90. const traceEventDataSectionContext = useContext(TraceEventDataSectionContext);
  91. const absolute = traceEventDataSectionContext?.display.includes('absolute-addresses');
  92. const fullStackTrace = traceEventDataSectionContext?.fullStackTrace;
  93. const fullFunctionName = traceEventDataSectionContext?.display.includes(
  94. 'verbose-function-names'
  95. );
  96. const absoluteFilePaths =
  97. traceEventDataSectionContext?.display.includes('absolute-file-paths');
  98. const tooltipDelay = isHoverPreviewed ? SLOW_TOOLTIP_DELAY : undefined;
  99. const foundByStackScanning = frame.trust === 'scan' || frame.trust === 'cfi-scan';
  100. const startingAddress = image ? image.image_addr : null;
  101. const packageClickable =
  102. !!frame.symbolicatorStatus &&
  103. frame.symbolicatorStatus !== SymbolicatorStatus.UNKNOWN_IMAGE &&
  104. !isHoverPreviewed;
  105. const leadsToApp = !frame.inApp && ((nextFrame && nextFrame.inApp) || !nextFrame);
  106. const expandable =
  107. !leadsToApp || includeSystemFrames
  108. ? isExpandable({
  109. frame,
  110. registers,
  111. platform,
  112. emptySourceNotation,
  113. isOnlyFrame,
  114. })
  115. : false;
  116. const inlineFrame =
  117. prevFrame &&
  118. platform === (prevFrame.platform || platform) &&
  119. frame.instructionAddr === prevFrame.instructionAddr;
  120. const functionNameHiddenDetails =
  121. defined(frame.rawFunction) &&
  122. defined(frame.function) &&
  123. frame.function !== frame.rawFunction;
  124. const [expanded, setExpanded] = useState(expandable ? isExpanded ?? false : false);
  125. function getRelativeAddress() {
  126. if (!startingAddress) {
  127. return '';
  128. }
  129. const relativeAddress = formatAddress(
  130. parseAddress(frame.instructionAddr) - parseAddress(startingAddress),
  131. maxLengthOfRelativeAddress
  132. );
  133. return `+${relativeAddress}`;
  134. }
  135. function getAddressTooltip() {
  136. if (inlineFrame && foundByStackScanning) {
  137. return t('Inline frame, found by stack scanning');
  138. }
  139. if (inlineFrame) {
  140. return t('Inline frame');
  141. }
  142. if (foundByStackScanning) {
  143. return t('Found by stack scanning');
  144. }
  145. return undefined;
  146. }
  147. function getFunctionName() {
  148. if (functionNameHiddenDetails && fullFunctionName && frame.rawFunction) {
  149. return {
  150. value: frame.rawFunction,
  151. meta: frameMeta?.rawFunction?.[''],
  152. };
  153. }
  154. if (frame.function) {
  155. return {
  156. value: frame.function,
  157. meta: frameMeta?.function?.[''],
  158. };
  159. }
  160. return undefined;
  161. }
  162. function getStatus() {
  163. // this is the status of image that belongs to this frame
  164. if (!image) {
  165. return undefined;
  166. }
  167. const combinedStatus = combineStatus(image.debug_status, image.unwind_status);
  168. switch (combinedStatus) {
  169. case 'unused':
  170. return undefined;
  171. case 'found':
  172. return 'success';
  173. default:
  174. return 'error';
  175. }
  176. }
  177. function handleGoToImagesLoaded(e: MouseEvent) {
  178. e.stopPropagation(); // to prevent collapsing if collapsible
  179. if (frame.instructionAddr) {
  180. const searchTerm =
  181. !(!frame.addrMode || frame.addrMode === 'abs') && image
  182. ? `${image.debug_id}!${frame.instructionAddr}`
  183. : frame.instructionAddr;
  184. DebugMetaStore.updateFilter(searchTerm);
  185. }
  186. scrollToElement('#images-loaded');
  187. }
  188. function handleToggleContext(e: MouseEvent) {
  189. if (!expandable) {
  190. return;
  191. }
  192. e.preventDefault();
  193. setExpanded(!expanded);
  194. }
  195. const relativeAddress = getRelativeAddress();
  196. const addressTooltip = getAddressTooltip();
  197. const functionName = getFunctionName();
  198. const status = getStatus();
  199. const organization = useOrganization();
  200. const stacktraceChangesEnabled = !!organization?.features.includes(
  201. 'issue-details-stacktrace-improvements'
  202. );
  203. return (
  204. <StackTraceFrame data-test-id="stack-trace-frame">
  205. <StrictClick onClick={handleToggleContext}>
  206. <RowHeader
  207. expandable={expandable}
  208. expanded={expanded}
  209. stacktraceChangesEnabled={stacktraceChangesEnabled && !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. {stacktraceChangesEnabled && 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>
  309. {!frame.inApp ? (
  310. stacktraceChangesEnabled ? null : (
  311. <Tag>{t('System')}</Tag>
  312. )
  313. ) : (
  314. <Tag type="info">{t('In App')}</Tag>
  315. )}
  316. </TypeCell>
  317. <ExpandCell>
  318. {expandable && (
  319. <ToggleButton
  320. size="zero"
  321. title={t('Toggle Context')}
  322. aria-label={t('Toggle Context')}
  323. tooltipProps={isHoverPreviewed ? {delay: SLOW_TOOLTIP_DELAY} : undefined}
  324. icon={
  325. <IconChevron legacySize="8px" direction={expanded ? 'up' : 'down'} />
  326. }
  327. />
  328. )}
  329. </ExpandCell>
  330. </RowHeader>
  331. </StrictClick>
  332. {expanded && (
  333. <Registers
  334. frame={frame}
  335. event={event}
  336. registers={registers}
  337. components={components}
  338. hasContextSource={hasContextSource(frame)}
  339. hasContextVars={hasContextVars(frame)}
  340. hasContextRegisters={hasContextRegisters(registers)}
  341. emptySourceNotation={emptySourceNotation}
  342. hasAssembly={hasAssembly(frame, platform)}
  343. expandable={expandable}
  344. isExpanded={expanded}
  345. registersMeta={registersMeta}
  346. frameMeta={frameMeta}
  347. />
  348. )}
  349. </StackTraceFrame>
  350. );
  351. }
  352. export default withSentryAppComponents(NativeFrame, {componentType: 'stacktrace-link'});
  353. const AddressCellWrapper = styled('div')`
  354. display: flex;
  355. `;
  356. const AddressCell = styled('div')`
  357. font-family: ${p => p.theme.text.familyMono};
  358. ${p => p.onClick && `cursor: pointer`};
  359. ${p => p.onClick && `color:` + p.theme.linkColor};
  360. `;
  361. const FunctionNameCell = styled('div')`
  362. word-break: break-all;
  363. @media (max-width: ${p => p.theme.breakpoints.small}) {
  364. grid-column: 2/6;
  365. }
  366. `;
  367. const GroupingCell = styled('div')`
  368. @media (max-width: ${p => p.theme.breakpoints.small}) {
  369. grid-row: 2/3;
  370. }
  371. `;
  372. const TypeCell = styled('div')`
  373. @media (max-width: ${p => p.theme.breakpoints.small}) {
  374. grid-column: 5/6;
  375. grid-row: 1/2;
  376. }
  377. `;
  378. const ExpandCell = styled('div')`
  379. @media (max-width: ${p => p.theme.breakpoints.small}) {
  380. grid-column: 6/7;
  381. grid-row: 1/2;
  382. }
  383. `;
  384. const ToggleButton = styled(Button)`
  385. width: 16px;
  386. height: 16px;
  387. `;
  388. const Registers = styled(Context)`
  389. border-bottom: 1px solid ${p => p.theme.border};
  390. padding: 0;
  391. margin: 0;
  392. `;
  393. const PackageNote = styled('div')`
  394. color: ${p => p.theme.subText};
  395. font-size: ${p => p.theme.fontSizeExtraSmall};
  396. `;
  397. const Package = styled('span')`
  398. white-space: nowrap;
  399. overflow: hidden;
  400. text-overflow: ellipsis;
  401. width: 100%;
  402. padding-right: 2px; /* Needed to prevent text cropping with italic font */
  403. `;
  404. const FileName = styled('span')`
  405. color: ${p => p.theme.subText};
  406. border-bottom: 1px dashed ${p => p.theme.border};
  407. `;
  408. const RowHeader = styled('span')<{
  409. expandable: boolean;
  410. expanded: boolean;
  411. isSubFrame: boolean;
  412. stacktraceChangesEnabled: boolean;
  413. }>`
  414. display: grid;
  415. grid-template-columns: repeat(2, auto) 1fr repeat(2, auto) ${space(2)};
  416. grid-template-rows: repeat(2, auto);
  417. align-items: center;
  418. align-content: center;
  419. column-gap: ${space(1)};
  420. background-color: ${p =>
  421. p.stacktraceChangesEnabled && p.isSubFrame
  422. ? `${p.theme.surface100}`
  423. : `${p.theme.bodyBackground}`};
  424. font-size: ${p => p.theme.codeFontSize};
  425. padding: ${space(1)};
  426. color: ${p => (p.stacktraceChangesEnabled ? p.theme.subText : '')};
  427. font-style: ${p => (p.stacktraceChangesEnabled ? 'italic' : '')};
  428. ${p => p.expandable && `cursor: pointer;`};
  429. @media (min-width: ${p => p.theme.breakpoints.small}) {
  430. grid-template-columns: auto 150px 120px 4fr repeat(2, auto) ${space(2)};
  431. padding: ${space(0.5)} ${space(1.5)};
  432. min-height: 32px;
  433. }
  434. `;
  435. const StackTraceFrame = styled('li')`
  436. :not(:last-child) {
  437. ${RowHeader} {
  438. border-bottom: 1px solid ${p => p.theme.border};
  439. }
  440. }
  441. `;
  442. const SymbolicatorIcon = styled('div')`
  443. width: ${p => p.theme.iconSizes.sm};
  444. `;
  445. const ShowHideButton = styled(Button)`
  446. color: ${p => p.theme.subText};
  447. font-style: italic;
  448. font-weight: normal;
  449. padding: ${space(0.25)} ${space(0.5)};
  450. &:hover {
  451. color: ${p => p.theme.subText};
  452. }
  453. `;