line.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 {STACKTRACE_PREVIEW_TOOLTIP_DELAY} from 'sentry/components/stacktracePreview';
  7. import StrictClick from 'sentry/components/strictClick';
  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={
  169. isHoverPreviewed ? {delay: STACKTRACE_PREVIEW_TOOLTIP_DELAY} : undefined
  170. }
  171. onClick={this.toggleContext}
  172. >
  173. <IconChevron direction={isExpanded ? 'up' : 'down'} size="8px" />
  174. </ToggleContextButton>
  175. </ToggleContextButtonWrapper>
  176. );
  177. }
  178. leadsToApp() {
  179. const {data, nextFrame} = this.props;
  180. return !data.inApp && ((nextFrame && nextFrame.inApp) || !nextFrame);
  181. }
  182. isFoundByStackScanning() {
  183. const {data} = this.props;
  184. return data.trust === 'scan' || data.trust === 'cfi-scan';
  185. }
  186. renderLeadHint() {
  187. const {isExpanded} = this.state;
  188. if (isExpanded) {
  189. return null;
  190. }
  191. const leadsToApp = this.leadsToApp();
  192. if (!leadsToApp) {
  193. return null;
  194. }
  195. const {nextFrame} = this.props;
  196. return !nextFrame ? (
  197. <LeadHint className="leads-to-app-hint" width="115px">
  198. {t('Crashed in non-app')}
  199. {': '}
  200. </LeadHint>
  201. ) : (
  202. <LeadHint className="leads-to-app-hint">
  203. {t('Called from')}
  204. {': '}
  205. </LeadHint>
  206. );
  207. }
  208. renderRepeats() {
  209. const timesRepeated = this.props.timesRepeated;
  210. if (timesRepeated && timesRepeated > 0) {
  211. return (
  212. <RepeatedFrames
  213. title={`Frame repeated ${timesRepeated} time${timesRepeated === 1 ? '' : 's'}`}
  214. >
  215. <RepeatedContent>
  216. <StyledIconRefresh />
  217. <span>{timesRepeated}</span>
  218. </RepeatedContent>
  219. </RepeatedFrames>
  220. );
  221. }
  222. return null;
  223. }
  224. renderDefaultLine() {
  225. const {isHoverPreviewed} = this.props;
  226. return (
  227. <StrictClick onClick={this.isExpandable() ? this.toggleContext : undefined}>
  228. <DefaultLine className="title" data-test-id="title">
  229. <VertCenterWrapper>
  230. <div>
  231. {this.renderLeadHint()}
  232. <DefaultTitle
  233. frame={this.props.data}
  234. platform={this.props.platform ?? 'other'}
  235. isHoverPreviewed={isHoverPreviewed}
  236. meta={this.props.frameMeta}
  237. />
  238. </div>
  239. {this.renderRepeats()}
  240. </VertCenterWrapper>
  241. {this.renderExpander()}
  242. </DefaultLine>
  243. </StrictClick>
  244. );
  245. }
  246. renderNativeLine() {
  247. const {
  248. data,
  249. showingAbsoluteAddress,
  250. onAddressToggle,
  251. onFunctionNameToggle,
  252. image,
  253. maxLengthOfRelativeAddress,
  254. includeSystemFrames,
  255. isFrameAfterLastNonApp,
  256. showCompleteFunctionName,
  257. isHoverPreviewed,
  258. } = this.props;
  259. const leadHint = this.renderLeadHint();
  260. const packageStatus = this.packageStatus();
  261. return (
  262. <StrictClick onClick={this.isExpandable() ? this.toggleContext : undefined}>
  263. <DefaultLine className="title as-table" data-test-id="title">
  264. <NativeLineContent isFrameAfterLastNonApp={!!isFrameAfterLastNonApp}>
  265. <PackageInfo>
  266. {leadHint}
  267. <PackageLink
  268. includeSystemFrames={!!includeSystemFrames}
  269. withLeadHint={leadHint !== null}
  270. packagePath={data.package}
  271. onClick={this.scrollToImage}
  272. isClickable={this.shouldShowLinkToImage()}
  273. isHoverPreviewed={isHoverPreviewed}
  274. >
  275. {!isHoverPreviewed && (
  276. <PackageStatus
  277. status={packageStatus}
  278. tooltip={t('Go to Images Loaded')}
  279. />
  280. )}
  281. </PackageLink>
  282. </PackageInfo>
  283. {data.instructionAddr && (
  284. <TogglableAddress
  285. address={data.instructionAddr}
  286. startingAddress={image ? image.image_addr ?? null : null}
  287. isAbsolute={!!showingAbsoluteAddress}
  288. isFoundByStackScanning={this.isFoundByStackScanning()}
  289. isInlineFrame={!!this.isInlineFrame()}
  290. onToggle={onAddressToggle}
  291. relativeAddressMaxlength={maxLengthOfRelativeAddress}
  292. isHoverPreviewed={isHoverPreviewed}
  293. />
  294. )}
  295. <Symbol
  296. frame={data}
  297. showCompleteFunctionName={!!showCompleteFunctionName}
  298. onFunctionNameToggle={onFunctionNameToggle}
  299. isHoverPreviewed={isHoverPreviewed}
  300. />
  301. </NativeLineContent>
  302. {this.renderExpander()}
  303. </DefaultLine>
  304. </StrictClick>
  305. );
  306. }
  307. renderLine() {
  308. switch (this.getPlatform()) {
  309. case 'objc':
  310. // fallthrough
  311. case 'cocoa':
  312. // fallthrough
  313. case 'native':
  314. return this.renderNativeLine();
  315. default:
  316. return this.renderDefaultLine();
  317. }
  318. }
  319. render() {
  320. const data = this.props.data;
  321. const className = classNames({
  322. frame: true,
  323. 'is-expandable': this.isExpandable(),
  324. expanded: this.state.isExpanded,
  325. collapsed: !this.state.isExpanded,
  326. 'system-frame': !data.inApp,
  327. 'frame-errors': data.errors,
  328. 'leads-to-app': this.leadsToApp(),
  329. });
  330. const props = {className};
  331. return (
  332. <StyledLi {...props}>
  333. {this.renderLine()}
  334. <Context
  335. frame={data}
  336. event={this.props.event}
  337. registers={this.props.registers}
  338. components={this.props.components}
  339. hasContextSource={hasContextSource(data)}
  340. hasContextVars={hasContextVars(data)}
  341. hasContextRegisters={hasContextRegisters(this.props.registers)}
  342. emptySourceNotation={this.props.emptySourceNotation}
  343. hasAssembly={hasAssembly(data, this.props.platform)}
  344. expandable={this.isExpandable()}
  345. isExpanded={this.state.isExpanded}
  346. registersMeta={this.props.registersMeta}
  347. frameMeta={this.props.frameMeta}
  348. />
  349. </StyledLi>
  350. );
  351. }
  352. }
  353. export default withOrganization(
  354. withSentryAppComponents(Line, {componentType: 'stacktrace-link'})
  355. );
  356. const PackageInfo = styled('div')`
  357. display: grid;
  358. grid-template-columns: auto 1fr;
  359. order: 2;
  360. align-items: flex-start;
  361. @media (min-width: ${props => props.theme.breakpoints.small}) {
  362. order: 0;
  363. }
  364. `;
  365. const RepeatedFrames = styled('div')`
  366. display: inline-block;
  367. border-radius: 50px;
  368. padding: 1px 3px;
  369. margin-left: ${space(1)};
  370. border-width: thin;
  371. border-style: solid;
  372. border-color: ${p => p.theme.pink200};
  373. color: ${p => p.theme.pink300};
  374. background-color: ${p => p.theme.backgroundSecondary};
  375. white-space: nowrap;
  376. `;
  377. const VertCenterWrapper = styled('div')`
  378. display: flex;
  379. align-items: center;
  380. `;
  381. const RepeatedContent = styled(VertCenterWrapper)`
  382. justify-content: center;
  383. `;
  384. const NativeLineContent = styled('div')<{isFrameAfterLastNonApp: boolean}>`
  385. display: grid;
  386. flex: 1;
  387. gap: ${space(0.5)};
  388. grid-template-columns: ${p =>
  389. `minmax(${p.isFrameAfterLastNonApp ? '167px' : '117px'}, auto) 1fr`};
  390. align-items: center;
  391. justify-content: flex-start;
  392. @media (min-width: ${props => props.theme.breakpoints.small}) {
  393. grid-template-columns:
  394. ${p => (p.isFrameAfterLastNonApp ? '200px' : '150px')} minmax(117px, auto)
  395. 1fr;
  396. }
  397. @media (min-width: ${props => props.theme.breakpoints.large}) and (max-width: ${props =>
  398. props.theme.breakpoints.xlarge}) {
  399. grid-template-columns:
  400. ${p => (p.isFrameAfterLastNonApp ? '180px' : '140px')} minmax(117px, auto)
  401. 1fr;
  402. }
  403. `;
  404. const DefaultLine = styled('div')`
  405. display: grid;
  406. grid-template-columns: 1fr auto;
  407. align-items: center;
  408. `;
  409. const StyledIconRefresh = styled(IconRefresh)`
  410. margin-right: ${space(0.25)};
  411. `;
  412. const LeadHint = styled('div')<{width?: string}>`
  413. ${p => p.theme.overflowEllipsis}
  414. max-width: ${p => (p.width ? p.width : '67px')}
  415. `;
  416. const ToggleContextButtonWrapper = styled('span')`
  417. margin-left: ${space(1)};
  418. `;
  419. // the Button's label has the padding of 3px because the button size has to be 16x16 px.
  420. const ToggleContextButton = styled(Button)`
  421. span:first-child {
  422. padding: 3px;
  423. }
  424. `;
  425. const StyledLi = styled('li')`
  426. ${PackageStatusIcon} {
  427. flex-shrink: 0;
  428. }
  429. :hover {
  430. ${PackageStatusIcon} {
  431. visibility: visible;
  432. }
  433. ${AddressToggleIcon} {
  434. visibility: visible;
  435. }
  436. ${FunctionNameToggleIcon} {
  437. visibility: visible;
  438. }
  439. }
  440. `;