crash_event_android.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import time
  2. from collections.abc import Mapping, MutableMapping, Sequence
  3. def get_frames(
  4. sdk_frame_module: str, system_frame_module: str
  5. ) -> Sequence[MutableMapping[str, str]]:
  6. frames = [
  7. {
  8. "function": "main",
  9. "module": "android.app.ActivityThread",
  10. "filename": "ActivityThread.java",
  11. "abs_path": "ActivityThread.java",
  12. },
  13. {
  14. "function": "handleCallback",
  15. "module": "android.os.Handler",
  16. "filename": "Handler.java",
  17. "abs_path": "Handler.java",
  18. },
  19. {
  20. "function": "performClickInternal",
  21. "module": "android.view.View",
  22. "filename": "View.java",
  23. "abs_path": "View.java",
  24. },
  25. {
  26. "function": "onClick",
  27. "module": "com.some.samples.android.MainActivity$$ExternalSyntheticLambda8",
  28. },
  29. {
  30. "function": "captureMessage",
  31. "module": sdk_frame_module,
  32. "filename": "Hub.java",
  33. "abs_path": "Hub.java",
  34. },
  35. {
  36. "function": "invoke",
  37. "module": system_frame_module,
  38. "filename": "Method.java",
  39. },
  40. ]
  41. return frames
  42. def get_crash_event(
  43. sdk_frame_module="io.sentry.Hub",
  44. system_frame_module="java.lang.reflect.Method",
  45. **kwargs,
  46. ) -> dict[str, object]:
  47. return get_crash_event_with_frames(
  48. get_frames(sdk_frame_module, system_frame_module),
  49. **kwargs,
  50. )
  51. def get_apex_frames(
  52. apex_frame_function: str,
  53. apex_frame_package: str,
  54. system_frame_package: str,
  55. ) -> Sequence[MutableMapping[str, str]]:
  56. frames = [
  57. {
  58. "function": "__pthread_start",
  59. "raw_function": "__pthread_start(void*)",
  60. "symbol": "_ZL15__pthread_startPv",
  61. "package": "/apex/com.android.runtime/lib/bionic/libc.so",
  62. },
  63. {
  64. "function": "__start_thread",
  65. "symbol": "__start_thread",
  66. "package": "/apex/com.android.art/lib64/bionic/libc.so",
  67. },
  68. {
  69. "function": apex_frame_function,
  70. "symbol": apex_frame_function,
  71. "package": apex_frame_package,
  72. },
  73. {
  74. "function": "invoke",
  75. "package": system_frame_package,
  76. },
  77. ]
  78. return frames
  79. def get_apex_crash_event(
  80. apex_frame_function="__start_thread",
  81. apex_frame_package="/apex/com.android.art/lib64/bionic/libc.so",
  82. system_frame_package="/apex/com.android.art/lib64/libart.so",
  83. **kwargs,
  84. ) -> dict[str, object]:
  85. return get_crash_event_with_frames(
  86. get_apex_frames(apex_frame_function, apex_frame_package, system_frame_package),
  87. **kwargs,
  88. )
  89. def get_exception(
  90. frames: Sequence[Mapping[str, str]],
  91. mechanism=None,
  92. ) -> dict[str, object]:
  93. if mechanism is None:
  94. # linter complains about mutable arguments otherwise
  95. mechanism = {"type": "onerror", "handled": False}
  96. return {
  97. "type": "IllegalArgumentException",
  98. "value": "SDK Crash",
  99. "module": "java.lang",
  100. "stacktrace": {"frames": frames},
  101. "mechanism": mechanism,
  102. }
  103. def get_crash_event_with_frames(frames: Sequence[Mapping[str, str]], **kwargs) -> dict[str, object]:
  104. result = {
  105. "event_id": "0a52a8331d3b45089ebd74f8118d4fa1",
  106. "release": "io.sentry.samples.android@7.4.0+2",
  107. "dist": "2",
  108. "platform": "java",
  109. "environment": "debug",
  110. "exception": {"values": [get_exception(frames)]},
  111. "key_id": "1336851",
  112. "level": "fatal",
  113. "contexts": {
  114. "device": {
  115. "name": "sdk_gphone64_arm64",
  116. "family": "sdk_gphone64_arm64",
  117. "model": "sdk_gphone64_arm64",
  118. "simulator": True,
  119. },
  120. "os": {
  121. "name": "Android",
  122. "version": "13",
  123. "build": "sdk_gphone64_arm64-userdebug UpsideDownCake UPB2.230407.019 10170211 dev-keys",
  124. "kernel_version": "6.1.21-android14-3-01811-g9e35a21ec03f-ab9850788",
  125. "rooted": False,
  126. "type": "os",
  127. },
  128. },
  129. "sdk": {"name": "sentry.java.android", "version": "7.4.0"},
  130. "timestamp": time.time(),
  131. "type": "error",
  132. }
  133. result.update(kwargs)
  134. return result