hierarchicalGroupingContent.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import {cloneElement, Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import List from 'sentry/components/list';
  4. import ListItem from 'sentry/components/list/listItem';
  5. import Panel from 'sentry/components/panels/panel';
  6. import {t} from 'sentry/locale';
  7. import {Frame, Group, PlatformType} from 'sentry/types';
  8. import {Event} from 'sentry/types/event';
  9. import {StackTraceMechanism, StacktraceType} from 'sentry/types/stacktrace';
  10. import {defined} from 'sentry/utils';
  11. import Line from '../../frame/line';
  12. import {getImageRange, parseAddress, stackTracePlatformIcon} from '../../utils';
  13. import {StacktraceFilenameQuery} from '../exception/useSourceMapDebug';
  14. import StacktracePlatformIcon from './platformIcon';
  15. type Props = {
  16. data: StacktraceType;
  17. event: Event;
  18. platform: PlatformType;
  19. className?: string;
  20. debugFrames?: StacktraceFilenameQuery[];
  21. expandFirstFrame?: boolean;
  22. groupingCurrentLevel?: Group['metadata']['current_level'];
  23. hideIcon?: boolean;
  24. includeSystemFrames?: boolean;
  25. isHoverPreviewed?: boolean;
  26. maxDepth?: number;
  27. mechanism?: StackTraceMechanism | null;
  28. meta?: Record<any, any>;
  29. newestFirst?: boolean;
  30. };
  31. export function HierarchicalGroupingContent({
  32. data,
  33. debugFrames,
  34. platform,
  35. event,
  36. newestFirst,
  37. className,
  38. isHoverPreviewed,
  39. groupingCurrentLevel,
  40. maxDepth,
  41. meta,
  42. hideIcon,
  43. includeSystemFrames = true,
  44. expandFirstFrame = true,
  45. mechanism,
  46. }: Props) {
  47. const [showingAbsoluteAddresses, setShowingAbsoluteAddresses] = useState(false);
  48. const [showCompleteFunctionName, setShowCompleteFunctionName] = useState(false);
  49. const {frames = [], framesOmitted, registers} = data;
  50. function findImageForAddress(
  51. address: Frame['instructionAddr'],
  52. addrMode: Frame['addrMode']
  53. ) {
  54. const images = event.entries.find(entry => entry.type === 'debugmeta')?.data?.images;
  55. return images && address
  56. ? images.find((img, idx) => {
  57. if (!addrMode || addrMode === 'abs') {
  58. const [startAddress, endAddress] = getImageRange(img);
  59. return address >= (startAddress as any) && address < (endAddress as any);
  60. }
  61. return addrMode === `rel:${idx}`;
  62. })
  63. : null;
  64. }
  65. function getClassName() {
  66. if (includeSystemFrames) {
  67. return `${className} traceback full-traceback`;
  68. }
  69. return `${className} traceback in-app-traceback`;
  70. }
  71. function isFrameUsedForGrouping(frame: Frame) {
  72. const {minGroupingLevel} = frame;
  73. if (groupingCurrentLevel === undefined || minGroupingLevel === undefined) {
  74. return false;
  75. }
  76. return minGroupingLevel <= groupingCurrentLevel;
  77. }
  78. function handleToggleAddresses(mouseEvent: React.MouseEvent<SVGElement>) {
  79. mouseEvent.stopPropagation(); // to prevent collapsing if collapsible
  80. setShowingAbsoluteAddresses(!showingAbsoluteAddresses);
  81. }
  82. function handleToggleFunctionName(mouseEvent: React.MouseEvent<SVGElement>) {
  83. mouseEvent.stopPropagation(); // to prevent collapsing if collapsible
  84. setShowCompleteFunctionName(!showCompleteFunctionName);
  85. }
  86. function getLastFrameIndex() {
  87. const inAppFrameIndexes = frames
  88. .map((frame, frameIndex) => {
  89. if (frame.inApp) {
  90. return frameIndex;
  91. }
  92. return undefined;
  93. })
  94. .filter(frame => frame !== undefined);
  95. return !inAppFrameIndexes.length
  96. ? frames.length - 1
  97. : inAppFrameIndexes[inAppFrameIndexes.length - 1];
  98. }
  99. function renderOmittedFrames(firstFrameOmitted: any, lastFrameOmitted: any) {
  100. return (
  101. <ListItem className="frame frames-omitted">
  102. {t(
  103. 'Frames %d until %d were omitted and not available.',
  104. firstFrameOmitted,
  105. lastFrameOmitted
  106. )}
  107. </ListItem>
  108. );
  109. }
  110. function renderConvertedFrames() {
  111. const firstFrameOmitted = framesOmitted?.[0] ?? null;
  112. const lastFrameOmitted = framesOmitted?.[1] ?? null;
  113. const lastFrameIndex = getLastFrameIndex();
  114. let nRepeats = 0;
  115. const maxLengthOfAllRelativeAddresses = frames.reduce(
  116. (maxLengthUntilThisPoint, frame) => {
  117. const correspondingImage = findImageForAddress(
  118. frame.instructionAddr,
  119. frame.addrMode
  120. );
  121. try {
  122. const relativeAddress = (
  123. parseAddress(frame.instructionAddr) -
  124. parseAddress(correspondingImage.image_addr)
  125. ).toString(16);
  126. return maxLengthUntilThisPoint > relativeAddress.length
  127. ? maxLengthUntilThisPoint
  128. : relativeAddress.length;
  129. } catch {
  130. return maxLengthUntilThisPoint;
  131. }
  132. },
  133. 0
  134. );
  135. let convertedFrames = frames
  136. .map((frame, frameIndex) => {
  137. const prevFrame = frames[frameIndex - 1];
  138. const nextFrame = frames[frameIndex + 1];
  139. const repeatedFrame =
  140. nextFrame &&
  141. frame.lineNo === nextFrame.lineNo &&
  142. frame.instructionAddr === nextFrame.instructionAddr &&
  143. frame.package === nextFrame.package &&
  144. frame.module === nextFrame.module &&
  145. frame.function === nextFrame.function;
  146. if (repeatedFrame) {
  147. nRepeats++;
  148. }
  149. const isUsedForGrouping = isFrameUsedForGrouping(frame);
  150. const isVisible =
  151. includeSystemFrames ||
  152. frame.inApp ||
  153. (nextFrame && nextFrame.inApp) ||
  154. // the last non-app frame
  155. (!frame.inApp && !nextFrame) ||
  156. isUsedForGrouping;
  157. if (isVisible && !repeatedFrame) {
  158. const lineProps = {
  159. event,
  160. frame,
  161. prevFrame,
  162. nextFrame,
  163. isExpanded: expandFirstFrame && lastFrameIndex === frameIndex,
  164. emptySourceNotation: lastFrameIndex === frameIndex && frameIndex === 0,
  165. platform,
  166. timesRepeated: nRepeats,
  167. showingAbsoluteAddress: showingAbsoluteAddresses,
  168. onAddressToggle: handleToggleAddresses,
  169. image: findImageForAddress(frame.instructionAddr, frame.addrMode),
  170. maxLengthOfRelativeAddress: maxLengthOfAllRelativeAddresses,
  171. registers: {},
  172. includeSystemFrames,
  173. onFunctionNameToggle: handleToggleFunctionName,
  174. showCompleteFunctionName,
  175. isHoverPreviewed,
  176. isUsedForGrouping,
  177. frameMeta: meta?.frames?.[frameIndex],
  178. registersMeta: meta?.registers,
  179. debugFrames,
  180. mechanism,
  181. isNewestFrame: frameIndex === lastFrameIndex,
  182. };
  183. nRepeats = 0;
  184. if (frameIndex === firstFrameOmitted) {
  185. return (
  186. <Fragment key={frameIndex}>
  187. <Line {...lineProps} />
  188. {renderOmittedFrames(firstFrameOmitted, lastFrameOmitted)}
  189. </Fragment>
  190. );
  191. }
  192. return <Line key={frameIndex} {...lineProps} />;
  193. }
  194. if (!repeatedFrame) {
  195. nRepeats = 0;
  196. }
  197. if (frameIndex !== firstFrameOmitted) {
  198. return null;
  199. }
  200. return renderOmittedFrames(firstFrameOmitted, lastFrameOmitted);
  201. })
  202. .filter(frame => !!frame) as React.ReactElement[];
  203. if (convertedFrames.length > 0 && registers) {
  204. const lastFrame = convertedFrames.length - 1;
  205. convertedFrames[lastFrame] = cloneElement(convertedFrames[lastFrame], {
  206. registers,
  207. });
  208. }
  209. if (defined(maxDepth)) {
  210. convertedFrames = convertedFrames.slice(-maxDepth);
  211. }
  212. if (!newestFirst) {
  213. return convertedFrames;
  214. }
  215. return [...convertedFrames].reverse();
  216. }
  217. const platformIcon = stackTracePlatformIcon(platform, frames);
  218. return (
  219. <Wrapper className={getClassName()} data-test-id="stack-trace-content-v2">
  220. {!hideIcon && <StacktracePlatformIcon platform={platformIcon} />}
  221. <StyledList>{renderConvertedFrames()}</StyledList>
  222. </Wrapper>
  223. );
  224. }
  225. const Wrapper = styled(Panel)`
  226. position: relative;
  227. border-top-left-radius: 0;
  228. `;
  229. const StyledList = styled(List)`
  230. gap: 0;
  231. `;