line.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import classNames from 'classnames';
  4. import scrollToElement from 'scroll-to-element';
  5. import Button from 'sentry/components/button';
  6. import StrictClick from 'sentry/components/strictClick';
  7. import {SLOW_TOOLTIP_DELAY} from 'sentry/constants';
  8. import {IconChevron, IconRefresh} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import DebugMetaStore from 'sentry/stores/debugMetaStore';
  11. import space from 'sentry/styles/space';
  12. import {Frame, Organization, PlatformType, SentryAppComponent} from 'sentry/types';
  13. import {Event} from 'sentry/types/event';
  14. import withOrganization from 'sentry/utils/withOrganization';
  15. import withSentryAppComponents from 'sentry/utils/withSentryAppComponents';
  16. import DebugImage from '../debugMeta/debugImage';
  17. import {combineStatus} from '../debugMeta/utils';
  18. import {SymbolicatorStatus} from '../types';
  19. import Context from './context';
  20. import DefaultTitle from './defaultTitle';
  21. import PackageLink from './packageLink';
  22. import PackageStatus, {PackageStatusIcon} from './packageStatus';
  23. import Symbol, {FunctionNameToggleIcon} from './symbol';
  24. import TogglableAddress, {AddressToggleIcon} from './togglableAddress';
  25. import {
  26. getPlatform,
  27. hasAssembly,
  28. hasContextRegisters,
  29. hasContextSource,
  30. hasContextVars,
  31. isDotnet,
  32. isExpandable,
  33. } from './utils';
  34. type Props = {
  35. components: Array<SentryAppComponent>;
  36. data: Frame;
  37. event: Event;
  38. registers: Record<string, string>;
  39. emptySourceNotation?: boolean;
  40. frameMeta?: Record<any, any>;
  41. image?: React.ComponentProps<typeof DebugImage>['image'];
  42. includeSystemFrames?: boolean;
  43. isExpanded?: boolean;
  44. isFirst?: boolean;
  45. isFrameAfterLastNonApp?: boolean;
  46. /**
  47. * Is the stack trace being previewed in a hovercard?
  48. */
  49. isHoverPreviewed?: boolean;
  50. isOnlyFrame?: boolean;
  51. maxLengthOfRelativeAddress?: number;
  52. nextFrame?: Frame;
  53. onAddressToggle?: (event: React.MouseEvent<SVGElement>) => void;
  54. onFunctionNameToggle?: (event: React.MouseEvent<SVGElement>) => void;
  55. organization?: Organization;
  56. platform?: PlatformType;
  57. prevFrame?: Frame;
  58. registersMeta?: Record<any, any>;
  59. showCompleteFunctionName?: boolean;
  60. showingAbsoluteAddress?: boolean;
  61. timesRepeated?: number;
  62. };
  63. type State = {
  64. isExpanded?: boolean;
  65. };
  66. function makeFilter(
  67. addr: string,
  68. addrMode: string | undefined,
  69. image?: React.ComponentProps<typeof DebugImage>['image']
  70. ): string {
  71. if (!(!addrMode || addrMode === 'abs') && image) {
  72. return `${image.debug_id}!${addr}`;
  73. }
  74. return addr;
  75. }
  76. export class Line extends Component<Props, State> {
  77. static defaultProps = {
  78. isExpanded: false,
  79. emptySourceNotation: false,
  80. isHoverPreviewed: false,
  81. };
  82. // isExpanded can be initialized to true via parent component;
  83. // data synchronization is not important
  84. // https://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html
  85. state: State = {
  86. isExpanded: this.props.isExpanded,
  87. };
  88. toggleContext = evt => {
  89. evt && evt.preventDefault();
  90. this.setState({
  91. isExpanded: !this.state.isExpanded,
  92. });
  93. };
  94. getPlatform() {
  95. // prioritize the frame platform but fall back to the platform
  96. // of the stack trace / exception
  97. return getPlatform(this.props.data.platform, this.props.platform ?? 'other');
  98. }
  99. isInlineFrame() {
  100. return (
  101. this.props.prevFrame &&
  102. this.getPlatform() === (this.props.prevFrame.platform || this.props.platform) &&
  103. this.props.data.instructionAddr === this.props.prevFrame.instructionAddr
  104. );
  105. }
  106. isExpandable() {
  107. const {registers, platform, emptySourceNotation, isOnlyFrame, data} = this.props;
  108. return isExpandable({
  109. frame: data,
  110. registers,
  111. platform,
  112. emptySourceNotation,
  113. isOnlyFrame,
  114. });
  115. }
  116. shouldShowLinkToImage() {
  117. const {isHoverPreviewed, data} = this.props;
  118. const {symbolicatorStatus} = data;
  119. return (
  120. !!symbolicatorStatus &&
  121. symbolicatorStatus !== SymbolicatorStatus.UNKNOWN_IMAGE &&
  122. !isHoverPreviewed
  123. );
  124. }
  125. packageStatus() {
  126. // this is the status of image that belongs to this frame
  127. const {image} = this.props;
  128. if (!image) {
  129. return 'empty';
  130. }
  131. const combinedStatus = combineStatus(image.debug_status, image.unwind_status);
  132. switch (combinedStatus) {
  133. case 'unused':
  134. return 'empty';
  135. case 'found':
  136. return 'success';
  137. default:
  138. return 'error';
  139. }
  140. }
  141. scrollToImage = event => {
  142. event.stopPropagation(); // to prevent collapsing if collapsible
  143. const {instructionAddr, addrMode} = this.props.data;
  144. if (instructionAddr) {
  145. DebugMetaStore.updateFilter(
  146. makeFilter(instructionAddr, addrMode, this.props.image)
  147. );
  148. }
  149. scrollToElement('#images-loaded');
  150. };
  151. preventCollapse = evt => {
  152. evt.stopPropagation();
  153. };
  154. renderExpander() {
  155. if (!this.isExpandable()) {
  156. return null;
  157. }
  158. const {isHoverPreviewed} = this.props;
  159. const {isExpanded} = this.state;
  160. return (
  161. <ToggleContextButtonWrapper>
  162. <ToggleContextButton
  163. className="btn-toggle"
  164. data-test-id={`toggle-button-${isExpanded ? 'expanded' : 'collapsed'}`}
  165. css={isDotnet(this.getPlatform()) && {display: 'block !important'}} // remove important once we get rid of css files
  166. size="zero"
  167. title={t('Toggle Context')}
  168. tooltipProps={isHoverPreviewed ? {delay: SLOW_TOOLTIP_DELAY} : undefined}
  169. onClick={this.toggleContext}
  170. >
  171. <IconChevron direction={isExpanded ? 'up' : 'down'} legacySize="8px" />
  172. </ToggleContextButton>
  173. </ToggleContextButtonWrapper>
  174. );
  175. }
  176. leadsToApp() {
  177. const {data, nextFrame} = this.props;
  178. return !data.inApp && ((nextFrame && nextFrame.inApp) || !nextFrame);
  179. }
  180. isFoundByStackScanning() {
  181. const {data} = this.props;
  182. return data.trust === 'scan' || data.trust === 'cfi-scan';
  183. }
  184. renderLeadHint() {
  185. const {isExpanded} = this.state;
  186. if (isExpanded) {
  187. return null;
  188. }
  189. const leadsToApp = this.leadsToApp();
  190. if (!leadsToApp) {
  191. return null;
  192. }
  193. const {nextFrame} = this.props;
  194. return !nextFrame ? (
  195. <LeadHint className="leads-to-app-hint" width="115px">
  196. {t('Crashed in non-app')}
  197. {': '}
  198. </LeadHint>
  199. ) : (
  200. <LeadHint className="leads-to-app-hint">
  201. {t('Called from')}
  202. {': '}
  203. </LeadHint>
  204. );
  205. }
  206. renderRepeats() {
  207. const timesRepeated = this.props.timesRepeated;
  208. if (timesRepeated && timesRepeated > 0) {
  209. return (
  210. <RepeatedFrames
  211. title={`Frame repeated ${timesRepeated} time${timesRepeated === 1 ? '' : 's'}`}
  212. >
  213. <RepeatedContent>
  214. <StyledIconRefresh />
  215. <span>{timesRepeated}</span>
  216. </RepeatedContent>
  217. </RepeatedFrames>
  218. );
  219. }
  220. return null;
  221. }
  222. renderDefaultLine() {
  223. const {isHoverPreviewed} = this.props;
  224. return (
  225. <StrictClick onClick={this.isExpandable() ? this.toggleContext : undefined}>
  226. <DefaultLine className="title" data-test-id="title">
  227. <VertCenterWrapper>
  228. <div>
  229. {this.renderLeadHint()}
  230. <DefaultTitle
  231. frame={this.props.data}
  232. platform={this.props.platform ?? 'other'}
  233. isHoverPreviewed={isHoverPreviewed}
  234. meta={this.props.frameMeta}
  235. />
  236. </div>
  237. {this.renderRepeats()}
  238. </VertCenterWrapper>
  239. {this.renderExpander()}
  240. </DefaultLine>
  241. </StrictClick>
  242. );
  243. }
  244. renderNativeLine() {
  245. const {
  246. data,
  247. showingAbsoluteAddress,
  248. onAddressToggle,
  249. onFunctionNameToggle,
  250. image,
  251. maxLengthOfRelativeAddress,
  252. includeSystemFrames,
  253. isFrameAfterLastNonApp,
  254. showCompleteFunctionName,
  255. isHoverPreviewed,
  256. } = this.props;
  257. const leadHint = this.renderLeadHint();
  258. const packageStatus = this.packageStatus();
  259. return (
  260. <StrictClick onClick={this.isExpandable() ? this.toggleContext : undefined}>
  261. <DefaultLine className="title as-table" data-test-id="title">
  262. <NativeLineContent isFrameAfterLastNonApp={!!isFrameAfterLastNonApp}>
  263. <PackageInfo>
  264. {leadHint}
  265. <PackageLink
  266. includeSystemFrames={!!includeSystemFrames}
  267. withLeadHint={leadHint !== null}
  268. packagePath={data.package}
  269. onClick={this.scrollToImage}
  270. isClickable={this.shouldShowLinkToImage()}
  271. isHoverPreviewed={isHoverPreviewed}
  272. >
  273. {!isHoverPreviewed && (
  274. <PackageStatus
  275. status={packageStatus}
  276. tooltip={t('Go to Images Loaded')}
  277. />
  278. )}
  279. </PackageLink>
  280. </PackageInfo>
  281. {data.instructionAddr && (
  282. <TogglableAddress
  283. address={data.instructionAddr}
  284. startingAddress={image ? image.image_addr ?? null : null}
  285. isAbsolute={!!showingAbsoluteAddress}
  286. isFoundByStackScanning={this.isFoundByStackScanning()}
  287. isInlineFrame={!!this.isInlineFrame()}
  288. onToggle={onAddressToggle}
  289. relativeAddressMaxlength={maxLengthOfRelativeAddress}
  290. isHoverPreviewed={isHoverPreviewed}
  291. />
  292. )}
  293. <Symbol
  294. frame={data}
  295. showCompleteFunctionName={!!showCompleteFunctionName}
  296. onFunctionNameToggle={onFunctionNameToggle}
  297. isHoverPreviewed={isHoverPreviewed}
  298. />
  299. </NativeLineContent>
  300. {this.renderExpander()}
  301. </DefaultLine>
  302. </StrictClick>
  303. );
  304. }
  305. renderLine() {
  306. switch (this.getPlatform()) {
  307. case 'objc':
  308. // fallthrough
  309. case 'cocoa':
  310. // fallthrough
  311. case 'native':
  312. return this.renderNativeLine();
  313. default:
  314. return this.renderDefaultLine();
  315. }
  316. }
  317. render() {
  318. const data = this.props.data;
  319. const className = classNames({
  320. frame: true,
  321. 'is-expandable': this.isExpandable(),
  322. expanded: this.state.isExpanded,
  323. collapsed: !this.state.isExpanded,
  324. 'system-frame': !data.inApp,
  325. 'frame-errors': data.errors,
  326. 'leads-to-app': this.leadsToApp(),
  327. });
  328. const props = {className};
  329. return (
  330. <StyledLi data-test-id="line" {...props}>
  331. {this.renderLine()}
  332. <Context
  333. frame={data}
  334. event={this.props.event}
  335. registers={this.props.registers}
  336. components={this.props.components}
  337. hasContextSource={hasContextSource(data)}
  338. hasContextVars={hasContextVars(data)}
  339. hasContextRegisters={hasContextRegisters(this.props.registers)}
  340. emptySourceNotation={this.props.emptySourceNotation}
  341. hasAssembly={hasAssembly(data, this.props.platform)}
  342. expandable={this.isExpandable()}
  343. isExpanded={this.state.isExpanded}
  344. registersMeta={this.props.registersMeta}
  345. frameMeta={this.props.frameMeta}
  346. />
  347. </StyledLi>
  348. );
  349. }
  350. }
  351. export default withOrganization(
  352. withSentryAppComponents(Line, {componentType: 'stacktrace-link'})
  353. );
  354. const PackageInfo = styled('div')`
  355. display: grid;
  356. grid-template-columns: auto 1fr;
  357. order: 2;
  358. align-items: flex-start;
  359. @media (min-width: ${props => props.theme.breakpoints.small}) {
  360. order: 0;
  361. }
  362. `;
  363. const RepeatedFrames = styled('div')`
  364. display: inline-block;
  365. border-radius: 50px;
  366. padding: 1px 3px;
  367. margin-left: ${space(1)};
  368. border-width: thin;
  369. border-style: solid;
  370. border-color: ${p => p.theme.pink200};
  371. color: ${p => p.theme.pink400};
  372. background-color: ${p => p.theme.backgroundSecondary};
  373. white-space: nowrap;
  374. `;
  375. const VertCenterWrapper = styled('div')`
  376. display: flex;
  377. align-items: center;
  378. `;
  379. const RepeatedContent = styled(VertCenterWrapper)`
  380. justify-content: center;
  381. `;
  382. const NativeLineContent = styled('div')<{isFrameAfterLastNonApp: boolean}>`
  383. display: grid;
  384. flex: 1;
  385. gap: ${space(0.5)};
  386. grid-template-columns: ${p =>
  387. `minmax(${p.isFrameAfterLastNonApp ? '167px' : '117px'}, auto) 1fr`};
  388. align-items: center;
  389. justify-content: flex-start;
  390. @media (min-width: ${props => props.theme.breakpoints.small}) {
  391. grid-template-columns:
  392. ${p => (p.isFrameAfterLastNonApp ? '200px' : '150px')} minmax(117px, auto)
  393. 1fr;
  394. }
  395. @media (min-width: ${props => props.theme.breakpoints.large}) and (max-width: ${props =>
  396. props.theme.breakpoints.xlarge}) {
  397. grid-template-columns:
  398. ${p => (p.isFrameAfterLastNonApp ? '180px' : '140px')} minmax(117px, auto)
  399. 1fr;
  400. }
  401. `;
  402. const DefaultLine = styled('div')`
  403. display: grid;
  404. grid-template-columns: 1fr auto;
  405. align-items: center;
  406. `;
  407. const StyledIconRefresh = styled(IconRefresh)`
  408. margin-right: ${space(0.25)};
  409. `;
  410. const LeadHint = styled('div')<{width?: string}>`
  411. ${p => p.theme.overflowEllipsis}
  412. max-width: ${p => (p.width ? p.width : '67px')}
  413. `;
  414. const ToggleContextButtonWrapper = styled('span')`
  415. margin-left: ${space(1)};
  416. `;
  417. // the Button's label has the padding of 3px because the button size has to be 16x16 px.
  418. const ToggleContextButton = styled(Button)`
  419. span:first-child {
  420. padding: 3px;
  421. }
  422. `;
  423. const StyledLi = styled('li')`
  424. ${PackageStatusIcon} {
  425. flex-shrink: 0;
  426. }
  427. :hover {
  428. ${PackageStatusIcon} {
  429. visibility: visible;
  430. }
  431. ${AddressToggleIcon} {
  432. visibility: visible;
  433. }
  434. ${FunctionNameToggleIcon} {
  435. visibility: visible;
  436. }
  437. }
  438. `;