line.tsx 13 KB

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