nativeFrame.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import {Fragment, MouseEvent, useContext, useState} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import scrollToElement from 'scroll-to-element';
  5. import Button from 'sentry/components/button';
  6. import {
  7. hasAssembly,
  8. hasContextRegisters,
  9. hasContextSource,
  10. hasContextVars,
  11. isDotnet,
  12. isExpandable,
  13. trimPackage,
  14. } from 'sentry/components/events/interfaces/frame/utils';
  15. import {formatAddress, parseAddress} from 'sentry/components/events/interfaces/utils';
  16. import {AnnotatedText} from 'sentry/components/events/meta/annotatedText';
  17. import {TraceEventDataSectionContext} from 'sentry/components/events/traceEventDataSection';
  18. import StrictClick from 'sentry/components/strictClick';
  19. import Tooltip from 'sentry/components/tooltip';
  20. import {SLOW_TOOLTIP_DELAY} from 'sentry/constants';
  21. import {IconChevron} from 'sentry/icons/iconChevron';
  22. import {IconInfo} from 'sentry/icons/iconInfo';
  23. import {IconQuestion} from 'sentry/icons/iconQuestion';
  24. import {IconWarning} from 'sentry/icons/iconWarning';
  25. import {t} 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 {ColorOrAlias} from 'sentry/utils/theme';
  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. image?: React.ComponentProps<typeof DebugImage>['image'];
  47. includeSystemFrames?: boolean;
  48. isExpanded?: boolean;
  49. isHoverPreviewed?: boolean;
  50. isOnlyFrame?: boolean;
  51. maxLengthOfRelativeAddress?: number;
  52. nextFrame?: Frame;
  53. prevFrame?: Frame;
  54. registersMeta?: Record<any, any>;
  55. };
  56. function NativeFrame({
  57. frame,
  58. nextFrame,
  59. prevFrame,
  60. includeSystemFrames,
  61. isUsedForGrouping,
  62. maxLengthOfRelativeAddress,
  63. image,
  64. registers,
  65. isOnlyFrame,
  66. event,
  67. components,
  68. isExpanded,
  69. platform,
  70. registersMeta,
  71. frameMeta,
  72. emptySourceNotation = false,
  73. /**
  74. * Is the stack trace being previewed in a hovercard?
  75. */
  76. isHoverPreviewed = false,
  77. }: Props) {
  78. const traceEventDataSectionContext = useContext(TraceEventDataSectionContext);
  79. const absolute = traceEventDataSectionContext?.display.includes('absolute-addresses');
  80. const fullStackTrace = traceEventDataSectionContext?.fullStackTrace;
  81. const fullFunctionName = traceEventDataSectionContext?.display.includes(
  82. 'verbose-function-names'
  83. );
  84. const absoluteFilePaths =
  85. traceEventDataSectionContext?.display.includes('absolute-file-paths');
  86. const tooltipDelay = isHoverPreviewed ? SLOW_TOOLTIP_DELAY : undefined;
  87. const foundByStackScanning = frame.trust === 'scan' || frame.trust === 'cfi-scan';
  88. const startingAddress = image ? image.image_addr : null;
  89. const packageClickable =
  90. !!frame.symbolicatorStatus &&
  91. frame.symbolicatorStatus !== SymbolicatorStatus.UNKNOWN_IMAGE &&
  92. !isHoverPreviewed;
  93. const leadsToApp = !frame.inApp && ((nextFrame && nextFrame.inApp) || !nextFrame);
  94. const expandable =
  95. !leadsToApp || includeSystemFrames
  96. ? isExpandable({
  97. frame,
  98. registers,
  99. platform,
  100. emptySourceNotation,
  101. isOnlyFrame,
  102. })
  103. : false;
  104. const inlineFrame =
  105. prevFrame &&
  106. platform === (prevFrame.platform || platform) &&
  107. frame.instructionAddr === prevFrame.instructionAddr;
  108. const functionNameHiddenDetails =
  109. defined(frame.rawFunction) &&
  110. defined(frame.function) &&
  111. frame.function !== frame.rawFunction;
  112. const [expanded, setExpanded] = useState(expandable ? isExpanded ?? false : false);
  113. function getRelativeAddress() {
  114. if (!startingAddress) {
  115. return '';
  116. }
  117. const relativeAddress = formatAddress(
  118. parseAddress(frame.instructionAddr) - parseAddress(startingAddress),
  119. maxLengthOfRelativeAddress
  120. );
  121. return `+${relativeAddress}`;
  122. }
  123. function getAddressTooltip() {
  124. if (inlineFrame && foundByStackScanning) {
  125. return t('Inline frame, found by stack scanning');
  126. }
  127. if (inlineFrame) {
  128. return t('Inline frame');
  129. }
  130. if (foundByStackScanning) {
  131. return t('Found by stack scanning');
  132. }
  133. return undefined;
  134. }
  135. function getFunctionName() {
  136. if (functionNameHiddenDetails && fullFunctionName && frame.rawFunction) {
  137. return {
  138. value: frame.rawFunction,
  139. meta: frameMeta?.rawFunction?.[''],
  140. };
  141. }
  142. if (frame.function) {
  143. return {
  144. value: frame.function,
  145. meta: frameMeta?.function?.[''],
  146. };
  147. }
  148. return undefined;
  149. }
  150. function getStatus() {
  151. // this is the status of image that belongs to this frame
  152. if (!image) {
  153. return undefined;
  154. }
  155. const combinedStatus = combineStatus(image.debug_status, image.unwind_status);
  156. switch (combinedStatus) {
  157. case 'unused':
  158. return undefined;
  159. case 'found':
  160. return 'success';
  161. default:
  162. return 'error';
  163. }
  164. }
  165. function handleGoToImagesLoaded(e: MouseEvent) {
  166. e.stopPropagation(); // to prevent collapsing if collapsible
  167. if (frame.instructionAddr) {
  168. const searchTerm =
  169. !(!frame.addrMode || frame.addrMode === 'abs') && image
  170. ? `${image.debug_id}!${frame.instructionAddr}`
  171. : frame.instructionAddr;
  172. DebugMetaStore.updateFilter(searchTerm);
  173. }
  174. scrollToElement('#images-loaded');
  175. }
  176. function handleToggleContext(e: MouseEvent) {
  177. if (!expandable) {
  178. return;
  179. }
  180. e.preventDefault();
  181. setExpanded(!expanded);
  182. }
  183. const relativeAddress = getRelativeAddress();
  184. const addressTooltip = getAddressTooltip();
  185. const functionName = getFunctionName();
  186. const status = getStatus();
  187. return (
  188. <GridRow
  189. inApp={frame.inApp}
  190. expandable={expandable}
  191. expanded={expanded}
  192. className="frame"
  193. data-test-id="stack-trace-frame"
  194. >
  195. <StrictClick onClick={handleToggleContext}>
  196. <StrictClickContent>
  197. <StatusCell>
  198. {(status === 'error' || status === undefined) &&
  199. (packageClickable ? (
  200. <PackageStatusButton
  201. onClick={handleGoToImagesLoaded}
  202. title={t('Go to images loaded')}
  203. aria-label={t('Go to images loaded')}
  204. icon={
  205. status === 'error' ? (
  206. <IconQuestion size="sm" color="errorText" />
  207. ) : (
  208. <IconWarning size="sm" color="errorText" />
  209. )
  210. }
  211. size="zero"
  212. borderless
  213. />
  214. ) : status === 'error' ? (
  215. <IconQuestion size="sm" color="errorText" />
  216. ) : (
  217. <IconWarning size="sm" color="errorText" />
  218. ))}
  219. </StatusCell>
  220. <PackageCell>
  221. {!fullStackTrace && !expanded && leadsToApp && (
  222. <Fragment>
  223. {!nextFrame ? t('Crashed in non-app') : t('Called from')}
  224. {':'}&nbsp;
  225. </Fragment>
  226. )}
  227. <span>
  228. <Tooltip
  229. title={frame.package ?? t('Go to images loaded')}
  230. delay={tooltipDelay}
  231. disabled={frame.package ? false : !packageClickable}
  232. containerDisplayMode="inline-flex"
  233. >
  234. <Package
  235. color={
  236. status === undefined || status === 'error'
  237. ? 'errorText'
  238. : packageClickable
  239. ? 'linkColor'
  240. : undefined
  241. }
  242. onClick={packageClickable ? handleGoToImagesLoaded : undefined}
  243. >
  244. {frame.package ? trimPackage(frame.package) : `<${t('unknown')}>`}
  245. </Package>
  246. </Tooltip>
  247. </span>
  248. </PackageCell>
  249. <AddressCell>
  250. <Tooltip
  251. title={addressTooltip}
  252. disabled={!(foundByStackScanning || inlineFrame)}
  253. delay={tooltipDelay}
  254. >
  255. {!relativeAddress || absolute ? frame.instructionAddr : relativeAddress}
  256. </Tooltip>
  257. </AddressCell>
  258. <GroupingCell>
  259. {isUsedForGrouping && (
  260. <Tooltip
  261. title={t('This frame appears in all other events related to this issue')}
  262. containerDisplayMode="inline-flex"
  263. >
  264. <IconInfo size="sm" color="subText" />
  265. </Tooltip>
  266. )}
  267. </GroupingCell>
  268. <FunctionNameCell>
  269. <FunctionName>
  270. {functionName ? (
  271. <AnnotatedText value={functionName.value} meta={functionName.meta} />
  272. ) : (
  273. `<${t('unknown')}>`
  274. )}
  275. </FunctionName>
  276. {frame.filename && (
  277. <Tooltip
  278. title={frame.absPath}
  279. disabled={!(defined(frame.absPath) && frame.absPath !== frame.filename)}
  280. delay={tooltipDelay}
  281. containerDisplayMode="inline-flex"
  282. >
  283. <FileName>
  284. {'('}
  285. {absoluteFilePaths ? frame.absPath : frame.filename}
  286. {frame.lineNo && `:${frame.lineNo}`}
  287. {')'}
  288. </FileName>
  289. </Tooltip>
  290. )}
  291. </FunctionNameCell>
  292. <ExpandCell>
  293. {expandable && (
  294. <ToggleButton
  295. size="zero"
  296. css={isDotnet(platform) && {display: 'block !important'}} // remove important once we get rid of css files
  297. title={t('Toggle Context')}
  298. aria-label={t('Toggle Context')}
  299. tooltipProps={isHoverPreviewed ? {delay: SLOW_TOOLTIP_DELAY} : undefined}
  300. icon={<IconChevron size="8px" direction={expanded ? 'up' : 'down'} />}
  301. />
  302. )}
  303. </ExpandCell>
  304. </StrictClickContent>
  305. </StrictClick>
  306. <RegistersCell>
  307. {expanded && (
  308. <Registers
  309. frame={frame}
  310. event={event}
  311. registers={registers}
  312. components={components}
  313. hasContextSource={hasContextSource(frame)}
  314. hasContextVars={hasContextVars(frame)}
  315. hasContextRegisters={hasContextRegisters(registers)}
  316. emptySourceNotation={emptySourceNotation}
  317. hasAssembly={hasAssembly(frame, platform)}
  318. expandable={expandable}
  319. isExpanded={expanded}
  320. registersMeta={registersMeta}
  321. frameMeta={frameMeta}
  322. />
  323. )}
  324. </RegistersCell>
  325. </GridRow>
  326. );
  327. }
  328. export default withSentryAppComponents(NativeFrame, {componentType: 'stacktrace-link'});
  329. const Cell = styled('div')`
  330. padding: ${space(0.5)};
  331. display: flex;
  332. flex-wrap: wrap;
  333. word-break: break-all;
  334. align-items: flex-start;
  335. `;
  336. const StatusCell = styled(Cell)`
  337. @media (max-width: ${p => p.theme.breakpoints.small}) {
  338. grid-column: 1/1;
  339. grid-row: 1/1;
  340. }
  341. `;
  342. const PackageCell = styled(Cell)`
  343. color: ${p => p.theme.subText};
  344. @media (max-width: ${p => p.theme.breakpoints.small}) {
  345. grid-column: 2/2;
  346. grid-row: 1/1;
  347. display: grid;
  348. grid-template-columns: 1fr;
  349. grid-template-rows: repeat(2, auto);
  350. }
  351. `;
  352. const AddressCell = styled(Cell)`
  353. font-family: ${p => p.theme.text.familyMono};
  354. @media (max-width: ${p => p.theme.breakpoints.small}) {
  355. grid-column: 3/3;
  356. grid-row: 1/1;
  357. }
  358. `;
  359. const GroupingCell = styled(Cell)`
  360. @media (max-width: ${p => p.theme.breakpoints.small}) {
  361. grid-column: 1/1;
  362. grid-row: 2/2;
  363. }
  364. `;
  365. const FunctionNameCell = styled(Cell)`
  366. color: ${p => p.theme.textColor};
  367. @media (max-width: ${p => p.theme.breakpoints.small}) {
  368. grid-column: 2/-1;
  369. grid-row: 2/2;
  370. }
  371. `;
  372. const ExpandCell = styled(Cell)``;
  373. const RegistersCell = styled('div')`
  374. grid-column: 1/-1;
  375. margin-left: -${space(0.5)};
  376. margin-right: -${space(0.5)};
  377. margin-bottom: -${space(0.5)};
  378. cursor: default;
  379. `;
  380. const Registers = styled(Context)`
  381. padding: 0;
  382. margin: 0;
  383. `;
  384. const ToggleButton = styled(Button)`
  385. width: 16px;
  386. height: 16px;
  387. `;
  388. const Package = styled('span')<{color?: ColorOrAlias}>`
  389. border-bottom: 1px dashed ${p => p.theme.border};
  390. ${p => p.color && `color: ${p.theme[p.color]}`};
  391. ${p => p.onClick && `cursor: pointer;`}
  392. `;
  393. const FunctionName = styled('div')`
  394. color: ${p => p.theme.headingColor};
  395. margin-right: ${space(1)};
  396. `;
  397. const FileName = styled('span')`
  398. color: ${p => p.theme.subText};
  399. border-bottom: 1px dashed ${p => p.theme.border};
  400. `;
  401. const PackageStatusButton = styled(Button)`
  402. padding: 0;
  403. border: none;
  404. `;
  405. const GridRow = styled('li')<{expandable: boolean; expanded: boolean; inApp: boolean}>`
  406. ${p => p.expandable && `cursor: pointer;`};
  407. ${p => p.inApp && `background: ${p.theme.bodyBackground};`};
  408. ${p =>
  409. !p.inApp &&
  410. css`
  411. color: ${p.theme.subText};
  412. ${FunctionName} {
  413. color: ${p.theme.subText};
  414. }
  415. ${FunctionNameCell} {
  416. color: ${p.theme.subText};
  417. }
  418. `};
  419. display: grid;
  420. align-items: flex-start;
  421. padding: ${space(0.5)};
  422. :not(:last-child) {
  423. border-bottom: 1px solid ${p => p.theme.border};
  424. }
  425. && {
  426. border-top: 0;
  427. }
  428. grid-template-columns: 24px 132px 138px 24px 1fr 24px;
  429. grid-template-rows: 1fr;
  430. @media (max-width: ${p => p.theme.breakpoints.small}) {
  431. grid-template-columns: 24px auto minmax(138px, 1fr) 24px;
  432. grid-template-rows: repeat(2, auto);
  433. }
  434. `;
  435. const StrictClickContent = styled('div')`
  436. display: contents;
  437. `;