runtime_analysis 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +115 -0
  3. data/LICENSE.txt +96 -0
  4. data/README.md +164 -0
  5. data/editor/nvim/README.md +278 -0
  6. data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
  7. data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
  8. data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
  9. data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
  10. data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
  11. data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
  12. data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
  13. data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
  14. data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
  15. data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
  16. data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
  17. data/editor/nvim/plugin/runtime_analysis.lua +71 -0
  18. data/examples/trackers/PROMPT.md +86 -0
  19. data/examples/trackers/honeybadger.rb +19 -0
  20. data/examples/trackers/sentry.rb +16 -0
  21. data/ext/runtime_analysis/extconf.rb +68 -0
  22. data/ext/runtime_analysis/fast.c +775 -0
  23. data/ext/runtime_analysis/fast.h +59 -0
  24. data/ext/runtime_analysis/hold.c +560 -0
  25. data/ext/runtime_analysis/init.c +36 -0
  26. data/ext/runtime_analysis/names.c +78 -0
  27. data/ext/runtime_analysis/names.h +28 -0
  28. data/ext/runtime_analysis/novelty.c +139 -0
  29. data/ext/runtime_analysis/novelty.h +22 -0
  30. data/ext/runtime_analysis/os.c +73 -0
  31. data/ext/runtime_analysis/os.h +32 -0
  32. data/ext/runtime_analysis/ring.c +445 -0
  33. data/ext/runtime_analysis/rows.c +304 -0
  34. data/ext/runtime_analysis/sink.c +485 -0
  35. data/ext/runtime_analysis/sink.h +240 -0
  36. data/ext/runtime_analysis/state.c +254 -0
  37. data/ext/runtime_analysis/state.h +57 -0
  38. data/ext/runtime_analysis/wrapper.c +278 -0
  39. data/ext/runtime_analysis/wrapper.h +24 -0
  40. data/lib/runtime_analysis/errors.rb +34 -0
  41. data/lib/runtime_analysis/failsafe.rb +123 -0
  42. data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
  43. data/lib/runtime_analysis/host/declared.rb +152 -0
  44. data/lib/runtime_analysis/host/inject.rb +90 -0
  45. data/lib/runtime_analysis/host/job_runners.rb +27 -0
  46. data/lib/runtime_analysis/host/middleware.rb +23 -0
  47. data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
  48. data/lib/runtime_analysis/host/railtie.rb +42 -0
  49. data/lib/runtime_analysis/host/reports.rb +54 -0
  50. data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
  51. data/lib/runtime_analysis/host/web.rb +22 -0
  52. data/lib/runtime_analysis/host.rb +34 -0
  53. data/lib/runtime_analysis/inject.rb +5 -0
  54. data/lib/runtime_analysis/minitest.rb +9 -0
  55. data/lib/runtime_analysis/native/build.rb +17 -0
  56. data/lib/runtime_analysis/native/byte_size.rb +27 -0
  57. data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
  58. data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
  59. data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
  60. data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
  61. data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
  62. data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
  63. data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
  64. data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
  65. data/lib/runtime_analysis/native/sink.rb +12 -0
  66. data/lib/runtime_analysis/native.rb +74 -0
  67. data/lib/runtime_analysis/platform.rb +30 -0
  68. data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
  69. data/lib/runtime_analysis/query/index/graph.rb +107 -0
  70. data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
  71. data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
  72. data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
  73. data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
  74. data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
  75. data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
  76. data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
  77. data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
  78. data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
  79. data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
  80. data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
  81. data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
  82. data/lib/runtime_analysis/query/index/reading.rb +80 -0
  83. data/lib/runtime_analysis/query/index/shapes.rb +142 -0
  84. data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
  85. data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
  86. data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
  87. data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
  88. data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
  89. data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
  90. data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
  91. data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
  92. data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
  93. data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
  94. data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
  95. data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
  96. data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
  97. data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
  98. data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
  99. data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
  100. data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
  101. data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
  102. data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
  103. data/lib/runtime_analysis/query/index/signatures.rb +93 -0
  104. data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
  105. data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
  106. data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
  107. data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
  108. data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
  109. data/lib/runtime_analysis/query/index/static/method.rb +19 -0
  110. data/lib/runtime_analysis/query/index/static/params.rb +52 -0
  111. data/lib/runtime_analysis/query/index/static/source.rb +69 -0
  112. data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
  113. data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
  114. data/lib/runtime_analysis/query/index/static/value.rb +17 -0
  115. data/lib/runtime_analysis/query/index/static/values.rb +71 -0
  116. data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
  117. data/lib/runtime_analysis/query/index/static.rb +85 -0
  118. data/lib/runtime_analysis/query/index.rb +32 -0
  119. data/lib/runtime_analysis/query/nodes.rb +60 -0
  120. data/lib/runtime_analysis/query/project/config.rb +108 -0
  121. data/lib/runtime_analysis/query/project.rb +40 -0
  122. data/lib/runtime_analysis/query/report/document.rb +84 -0
  123. data/lib/runtime_analysis/query/report/finding.rb +15 -0
  124. data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
  125. data/lib/runtime_analysis/query/report/list.rb +15 -0
  126. data/lib/runtime_analysis/query/report/location.rb +14 -0
  127. data/lib/runtime_analysis/query/report/one_row.rb +14 -0
  128. data/lib/runtime_analysis/query/report/outline.rb +15 -0
  129. data/lib/runtime_analysis/query/report/paint.rb +34 -0
  130. data/lib/runtime_analysis/query/report/related.rb +11 -0
  131. data/lib/runtime_analysis/query/report/table.rb +15 -0
  132. data/lib/runtime_analysis/query/report/text.rb +13 -0
  133. data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
  134. data/lib/runtime_analysis/query/report.rb +35 -0
  135. data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
  136. data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
  137. data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
  138. data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
  139. data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
  140. data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
  141. data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
  142. data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
  143. data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
  144. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
  145. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
  146. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
  147. data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
  148. data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
  149. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
  150. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
  151. data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
  152. data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
  153. data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
  154. data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
  155. data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
  156. data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
  157. data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
  158. data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
  159. data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
  160. data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
  161. data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
  162. data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
  163. data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
  164. data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
  165. data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
  166. data/lib/runtime_analysis/query/surfaces.rb +14 -0
  167. data/lib/runtime_analysis/query/tools/base.rb +86 -0
  168. data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
  169. data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
  170. data/lib/runtime_analysis/query/tools/origins.rb +47 -0
  171. data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
  172. data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
  173. data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
  174. data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
  175. data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
  176. data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
  177. data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
  178. data/lib/runtime_analysis/query/tools.rb +34 -0
  179. data/lib/runtime_analysis/query.rb +33 -0
  180. data/lib/runtime_analysis/recording/blob.rb +13 -0
  181. data/lib/runtime_analysis/recording/format.rb +32 -0
  182. data/lib/runtime_analysis/recording/opcodes.rb +30 -0
  183. data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
  184. data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
  185. data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
  186. data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
  187. data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
  188. data/lib/runtime_analysis/recording/seen.rb +63 -0
  189. data/lib/runtime_analysis/recording/sink.rb +19 -0
  190. data/lib/runtime_analysis/recording/source_blob.rb +20 -0
  191. data/lib/runtime_analysis/recording.rb +52 -0
  192. data/lib/runtime_analysis/rspec.rb +8 -0
  193. data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
  194. data/lib/runtime_analysis/ruby_parsing.rb +98 -0
  195. data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
  196. data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
  197. data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
  198. data/lib/runtime_analysis/trace/configuration.rb +115 -0
  199. data/lib/runtime_analysis/trace/entries.rb +47 -0
  200. data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
  201. data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
  202. data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
  203. data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
  204. data/lib/runtime_analysis/trace/hold.rb +142 -0
  205. data/lib/runtime_analysis/trace/inclusion.rb +46 -0
  206. data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
  207. data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
  208. data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
  209. data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
  210. data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
  211. data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
  212. data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
  213. data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
  214. data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
  215. data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
  216. data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
  217. data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
  218. data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
  219. data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
  220. data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
  221. data/lib/runtime_analysis/trace/installer.rb +149 -0
  222. data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
  223. data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
  224. data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
  225. data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
  226. data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
  227. data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
  228. data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
  229. data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
  230. data/lib/runtime_analysis/trace/mechanism.rb +28 -0
  231. data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
  232. data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
  233. data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
  234. data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
  235. data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
  236. data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
  237. data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
  238. data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
  239. data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
  240. data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
  241. data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
  242. data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
  243. data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
  244. data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
  245. data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
  246. data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
  247. data/lib/runtime_analysis/trace/novelty.rb +17 -0
  248. data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
  249. data/lib/runtime_analysis/trace/pure/state.rb +94 -0
  250. data/lib/runtime_analysis/trace/pure.rb +15 -0
  251. data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
  252. data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
  253. data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
  254. data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
  255. data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
  256. data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
  257. data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
  258. data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
  259. data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
  260. data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
  261. data/lib/runtime_analysis/trace/recorder.rb +149 -0
  262. data/lib/runtime_analysis/trace/state.rb +16 -0
  263. data/lib/runtime_analysis/trace.rb +142 -0
  264. data/lib/runtime_analysis/version.rb +6 -0
  265. data/lib/runtime_analysis/web.rb +6 -0
  266. data/lib/runtime_analysis.rb +125 -0
  267. metadata +330 -0
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through configuration_spec.rb, which reads a run's label and probe from a configuration.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Configuration
8
+ module PerRun
9
+ LABEL = "RUNTIME_ANALYSIS_LABEL"
10
+ PROBE = "RUNTIME_ANALYSIS_PROBE"
11
+
12
+ attr_writer :label, :probe
13
+
14
+ def label
15
+ @label || ENV.fetch(LABEL, nil)
16
+ end
17
+
18
+ def probe
19
+ @probe || named(ENV.fetch(PROBE, ""))
20
+ end
21
+
22
+ private
23
+
24
+ def named(list)
25
+ methods = list.split(",").map(&:strip).reject(&:empty?)
26
+ methods unless methods.empty?
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Trace
5
+ class Configuration
6
+ module Tier
7
+ NAMES = { calls: 1, classes: 2, objects: 3, raises: 4 }.freeze
8
+ NUMBERS = NAMES.invert.freeze
9
+
10
+ module_function
11
+
12
+ def number(value)
13
+ key = value.to_s.strip.downcase
14
+ NAMES[key.to_sym] || (Integer(key) if key.match?(/\A[1-4]\z/)) || raise(ArgumentError, unknown(value))
15
+ end
16
+
17
+ def name_of(number)
18
+ NUMBERS.fetch(number) { raise ArgumentError, unknown(number) }
19
+ end
20
+
21
+ def unknown(value)
22
+ "tier must be one of #{NAMES.map { |name, n| "#{name.inspect} (#{n})" }.join(", ")}, not #{value.inspect}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/native"
4
+ require_relative "inclusion"
5
+ require_relative "installer"
6
+ require_relative "configuration/tier"
7
+ require_relative "configuration/capturing"
8
+ require_relative "configuration/per_run"
9
+
10
+ module RuntimeAnalysis
11
+ module Trace
12
+ class Configuration
13
+ DEFAULT_RECORDING_DIR = "./tmp/runtime_analysis"
14
+
15
+ attr_accessor :included, :excluded, :excluded_files, :recording_dir, :retain, :adaptive, :errors_only,
16
+ :environment, :nil_index, :revision, :record_ivars
17
+ attr_writer :rotate, :error_signal, :mechanism, :class_builders
18
+
19
+ include Capturing
20
+ include PerRun
21
+
22
+ def self.tier(name) = Tier.number(name)
23
+
24
+ def rotate
25
+ @rotate || {}
26
+ end
27
+
28
+ def tier
29
+ @tier || (errors_only ? ERRORS_ONLY_TIER : 1)
30
+ end
31
+
32
+ def tier=(value)
33
+ @tier = Tier.number(value)
34
+ end
35
+
36
+ def error_signal
37
+ @error_signal || :raise
38
+ end
39
+
40
+ def ring
41
+ @ring || RING
42
+ end
43
+
44
+ def ring=(size)
45
+ bytes = Native.bytes(size)
46
+ raise ArgumentError, format(RING_REFUSAL, size.inspect) unless bytes.nil? || RING_SIZES.cover?(bytes)
47
+
48
+ @ring = bytes
49
+ end
50
+
51
+ RING = 8 * 1024 * 1024
52
+ RING_SIZES = ((64 * 1024)..(1024**3))
53
+ RING_REFUSAL = "ring must be between 64KB and 1GB for each recording thread, not %s"
54
+
55
+ ERRORS_ONLY_TIER = Tier.number(:raises)
56
+ ERRORS_ONLY_REFUSALS = {
57
+ pure: "errors_only holds a request in the native sink, and mechanism :pure records without it",
58
+ tier: "errors_only keeps a failed request to say what it raised and where its nil came from, " \
59
+ "which takes tier :raises",
60
+ adaptive: "errors_only cannot be on with adaptive yet: adaptive mode skips the calls a request " \
61
+ "repeats, and a failed request would be written without them"
62
+ }.freeze
63
+
64
+ def check!
65
+ refusal = errors_only_refusal
66
+ raise ArgumentError, ERRORS_ONLY_REFUSALS.fetch(refusal) if refusal
67
+ end
68
+
69
+ def mechanism
70
+ @mechanism || (RUBY_ENGINE == "ruby" ? :native : :pure)
71
+ end
72
+
73
+ def initialize
74
+ @included = Inclusion.default_directories
75
+ @excluded = []
76
+ @excluded_files = []
77
+ @recording_dir = DEFAULT_RECORDING_DIR
78
+ end
79
+
80
+ def include_gems(*names, depth: 1)
81
+ names.each { |name| gems[name.to_s] = depth }
82
+ end
83
+
84
+ def gems
85
+ @gems ||= {}
86
+ end
87
+
88
+ def class_builders
89
+ @class_builders ||= Installer.default_builders
90
+ end
91
+
92
+ def inclusion
93
+ Inclusion.new(included: included, excluded: excluded, excluded_files: excluded_files,
94
+ gems: gem_directories)
95
+ end
96
+
97
+ private
98
+
99
+ def errors_only_refusal
100
+ return unless errors_only
101
+ return :pure if mechanism == :pure
102
+ return :tier if tier < ERRORS_ONLY_TIER
103
+
104
+ :adaptive if adaptive
105
+ end
106
+
107
+ def gem_directories
108
+ gems.to_h do |name, depth|
109
+ spec = Gem.loaded_specs[name] or raise ArgumentError, "include_gems: #{name} is not in the bundle"
110
+ [spec.full_gem_path, depth]
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through runtime_analysis_spec.rb and trace_spec.rb, which name trees through
4
+ # these, and failsafe_recording_spec.rb, which makes each step of naming a request fail.
5
+
6
+ module RuntimeAnalysis
7
+ module Trace
8
+ module Entries
9
+ def entry_point
10
+ State.current.entry_point
11
+ end
12
+
13
+ def entry_point=(identity)
14
+ State.current.entry_point = identity
15
+ end
16
+
17
+ def with_entry_point(identity)
18
+ previous = entry_point
19
+ self.entry_point = identity
20
+ yield
21
+ ensure
22
+ self.entry_point = previous
23
+ end
24
+
25
+ def inner_entry
26
+ State.current.inner_entry
27
+ end
28
+
29
+ def with_inner_entry(identity)
30
+ replaced = Failsafe.attempt { replace_inner_entry(identity) }
31
+ Failsafe.attempt { @recorder.stamp_inner_entry } if enabled?
32
+ yield
33
+ ensure
34
+ Failsafe.attempt { replaced.first.inner_entry = replaced.last } if replaced
35
+ end
36
+
37
+ private
38
+
39
+ def replace_inner_entry(identity)
40
+ previous = inner_entry
41
+ state = State.current
42
+ state.inner_entry = identity
43
+ [state, previous]
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Trace
5
+ class Hold
6
+ class Raises
7
+ NIL = "NilClass"
8
+
9
+ def initialize(request)
10
+ @request = request
11
+ @point = TracePoint.new(:raise) { |raised| Failsafe.attempt { heard(raised) } }
12
+ end
13
+
14
+ def listen = @point.enable(target_thread: Thread.current)
15
+
16
+ def stop = @point.disable
17
+
18
+ def heard(raised)
19
+ error = raised.raised_exception
20
+ return unless Recorder.missing(error)&.last == NIL
21
+
22
+ frame = raised.binding
23
+ frame.local_variables.each { |name| @request.keep(frame.local_variable_get(name), error) }
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/hold_spec.rb, which records requests through these.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Hold
8
+ module Requests
9
+ def request(identity = nil, &)
10
+ return held_request(identity, &) unless Failsafe.attempt { identity && entry_point }
11
+
12
+ with_inner_entry(identity) { held_request(&) }
13
+ end
14
+
15
+ def keep_request
16
+ Failsafe.attempt { hold&.failed }
17
+ end
18
+
19
+ def reported(kept_already)
20
+ Failsafe.attempt { hold&.reported(kept_already) }
21
+ end
22
+
23
+ def nil_origin(error, root: Dir.pwd)
24
+ Failsafe.attempt { @recorder.nil_origin(error, root) if hold&.open? }
25
+ end
26
+
27
+ private
28
+
29
+ def held_request(identity = nil, &)
30
+ named = Failsafe.attempt { name_request(identity) }
31
+ held = Failsafe.attempt { hold }
32
+ held ? held.request(&) : yield
33
+ ensure
34
+ Failsafe.attempt { self.entry_point = nil } if named
35
+ end
36
+
37
+ def name_request(identity)
38
+ self.entry_point = identity unless entry_point
39
+ end
40
+
41
+ def hold
42
+ @recorder.hold if enabled?
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/failsafe"
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Hold
8
+ class Telling
9
+ UNTOLD = "runtime_analysis: error_signal is :tracker, and an exception left a request before anything " \
10
+ "called RuntimeAnalysis.reported. If your tracker ignored that error, nothing is wrong. If it " \
11
+ "reported it, no failed request is being kept: " \
12
+ "https://www.runtimeanalyz.ing/configuration#your-error-tracker"
13
+ OUTSIDE = "runtime_analysis: RuntimeAnalysis.reported was called with no request open, so nothing was " \
14
+ "kept and no nil could be traced. Either the error was reported outside any request, or a job " \
15
+ "runner needs RuntimeAnalysis.job around each job, for which Sidekiq has the " \
16
+ "runtime_analysis-sidekiq gem: " \
17
+ "https://www.runtimeanalyz.ing/configuration#errors-only-recording. Later ones are counted in " \
18
+ "RuntimeAnalysis.stats[:reported_outside_request] and not reported."
19
+
20
+ attr_reader :unplaced
21
+
22
+ def initialize(warns)
23
+ @warns = warns
24
+ @unplaced = 0
25
+ end
26
+
27
+ def told
28
+ @told = true
29
+ end
30
+
31
+ def untold
32
+ return if @told || @said || !@warns
33
+
34
+ @said = true
35
+ Failsafe.report(UNTOLD)
36
+ end
37
+
38
+ def outside
39
+ @unplaced += 1
40
+ Failsafe.report(OUTSIDE) if @warns && @unplaced == 1
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace/hold_spec.rb, which keeps objects in a request and asks for them.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Hold
8
+ class Unwound
9
+ NEAREST_THE_RAISE = 32
10
+ CAUSE = ::Exception.instance_method(:cause)
11
+
12
+ attr_reader :objects
13
+
14
+ def add(object, error)
15
+ @objects = [] unless raised_because_of_the_last?(error)
16
+ @error = error
17
+ @objects << object unless @objects.size == NEAREST_THE_RAISE || @objects.last.equal?(object)
18
+ end
19
+
20
+ private
21
+
22
+ def raised_because_of_the_last?(error)
23
+ return false unless error
24
+
25
+ error.equal?(@error) || raised_because_of_the_last?(CAUSE.bind_call(error))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "runtime_analysis/failsafe"
4
+ require_relative "hold/telling"
5
+ require_relative "hold/unwound"
6
+ require_relative "hold/raises"
7
+ require_relative "recorder"
8
+ require_relative "hold/requests"
9
+
10
+ module RuntimeAnalysis
11
+ module Trace
12
+ class Hold
13
+ SIGNALS = %i[raise tracker].freeze
14
+ SCOPE = :scope
15
+ KEY = :_ra_hold
16
+ Request = Struct.new(:hold, :owner, :open, :failed, :raised, :unwound, :raises)
17
+
18
+ class Request
19
+ def keep(object, error)
20
+ (self.unwound ||= Unwound.new).add(object, error) if owner
21
+ end
22
+ end
23
+
24
+ attr_reader :kept
25
+
26
+ def self.requests = Requests
27
+
28
+ def initialize(logger:, signal: :raise, warn_untold: false)
29
+ @logger = logger
30
+ @kept = 0
31
+ @telling = Telling.new(warn_untold)
32
+ self.signal = signal
33
+ end
34
+
35
+ def signal=(signal)
36
+ raise ArgumentError, unknown(signal) unless SIGNALS.include?(signal)
37
+
38
+ @raises = signal == :raise
39
+ end
40
+
41
+ def request(&)
42
+ counted = Failsafe.attempt { enter_scope }
43
+ watched(&)
44
+ ensure
45
+ Failsafe.attempt { leave_scope } if counted
46
+ end
47
+
48
+ def tree_began(state)
49
+ request = current
50
+ opened(request, state) unless request.owner
51
+ end
52
+
53
+ def tree_ended(state, error)
54
+ request = current
55
+ request.raised = true if error
56
+ finish(request) if request.owner.equal?(state)
57
+ end
58
+
59
+ def failed
60
+ request = current
61
+ request.failed = true if request.owner
62
+ end
63
+
64
+ def reported(kept_already)
65
+ @telling.told
66
+ failed
67
+ @telling.outside unless kept_already || open?
68
+ end
69
+
70
+ def unplaced = @telling.unplaced
71
+
72
+ def keep_alive(object, error) = current.keep(object, error)
73
+
74
+ def alive = current.unwound&.objects.to_a
75
+
76
+ def open?
77
+ !current.owner.nil?
78
+ end
79
+
80
+ private
81
+
82
+ def unknown(signal)
83
+ "error_signal must be one of #{SIGNALS.map(&:inspect).join(", ")}, not #{signal.inspect}"
84
+ end
85
+
86
+ def watched
87
+ # rubocop:disable Style/SpecialGlobalVars -- host program: $ERROR_INFO needs English, which is not loaded into it
88
+ handled = $!
89
+ # rubocop:enable Style/SpecialGlobalVars
90
+ yield
91
+ ensure
92
+ Failsafe.attempt { raised } if Recorder.raised_since(handled)
93
+ end
94
+
95
+ def raised
96
+ current.raised = true
97
+ end
98
+
99
+ def current
100
+ request = Thread.current.thread_variable_get(KEY)
101
+ return request if request && request.hold.equal?(self)
102
+
103
+ Thread.current.thread_variable_set(KEY, Request.new(self, nil, 0))
104
+ end
105
+
106
+ def enter_scope
107
+ request = current
108
+ opened(request, SCOPE) unless request.owner
109
+ request.open += 1 if request.owner.equal?(SCOPE)
110
+ end
111
+
112
+ def opened(request, owner)
113
+ (request.raises ||= Raises.new(request)).listen
114
+ request.owner = owner
115
+ end
116
+
117
+ def leave_scope
118
+ request = current
119
+ request.open -= 1
120
+ finish(request) if request.open.zero?
121
+ end
122
+
123
+ def finish(request)
124
+ failed = failed?(request)
125
+ request.raises.stop
126
+ request.owner = request.unwound = nil
127
+ request.failed = request.raised = false
128
+ failed ? keep : @logger.discard_held
129
+ end
130
+
131
+ def failed?(request)
132
+ failed = request.failed || (@raises && request.raised)
133
+ @telling.untold if request.raised && !failed
134
+ failed
135
+ end
136
+
137
+ def keep
138
+ @kept += 1 if @logger.keep_held
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuntimeAnalysis
4
+ module Trace
5
+ class Inclusion
6
+ DEFAULT_DIRECTORIES = %w[app lib].freeze
7
+
8
+ def self.default_directories = DEFAULT_DIRECTORIES
9
+
10
+ def initialize(included: DEFAULT_DIRECTORIES, excluded: nil, excluded_files: nil, gems: {})
11
+ @included = expand(included)
12
+ @excluded = expand(excluded)
13
+ @excluded_files = expand(excluded_files)
14
+ @gems = gems.transform_keys { |directory| File.expand_path(directory) }
15
+ end
16
+
17
+ def gems?
18
+ !@gems.empty?
19
+ end
20
+
21
+ def gem_depth(file)
22
+ return nil unless file
23
+
24
+ path = File.expand_path(file)
25
+ @gems.find { |directory, _depth| path.start_with?("#{directory}/") }&.last
26
+ end
27
+
28
+ def include?(file)
29
+ return false unless file
30
+
31
+ path = File.expand_path(file)
32
+ under_any?(@included, path) && !under_any?(@excluded, path) && !@excluded_files.include?(path)
33
+ end
34
+
35
+ private
36
+
37
+ def expand(paths)
38
+ Array(paths).map { |path| File.expand_path(path) }
39
+ end
40
+
41
+ def under_any?(dirs, path)
42
+ dirs.any? { |dir| path == dir || path.start_with?("#{dir}/") }
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace_spec.rb, whose examples alias wrapped methods, to themselves among others.
4
+
5
+ module RuntimeAnalysis
6
+ module Trace
7
+ class Installer
8
+ module AliasRepair
9
+ private
10
+
11
+ def restore_displaced(target, name)
12
+ source = displaced_original(target, name)
13
+ redefine(target, name, source, visibility_of(target, name)) if source
14
+ end
15
+
16
+ def displaced_original(target, name)
17
+ own = own_entry(target, name)
18
+ holder = wrapper_holder(own, target)
19
+ @originals.fetch([holder, own.original_name]) if holder
20
+ rescue NameError
21
+ nil
22
+ end
23
+
24
+ def own_entry(target, name)
25
+ method = raw_method(target, name)
26
+ own = @wrapper.own?(method.owner) ? method.super_method : method
27
+ own unless @wrapper.own?(own.owner)
28
+ end
29
+
30
+ def wrapper_holder(own, target)
31
+ return target if alias_of_wrapper?(own, target)
32
+
33
+ attached = attached_module(target)
34
+ attached if attached && function_copy?(own, attached)
35
+ end
36
+
37
+ def alias_of_wrapper?(own, target)
38
+ wrapper = raw_method(target, own.original_name)
39
+ @wrapper.own?(wrapper.owner) && own.inspect.include?(wrapper.owner.inspect)
40
+ end
41
+
42
+ def function_copy?(own, attached)
43
+ wrapper = raw_method(attached, own.original_name)
44
+ @wrapper.own?(wrapper.owner) && body_location(own) == body_location(wrapper)
45
+ end
46
+
47
+ def body_location(method)
48
+ Reflection.source_location(method)
49
+ end
50
+
51
+ def attached_module(target)
52
+ target.attached_object if target.singleton_class?
53
+ end
54
+
55
+ def redefine(target, name, source, visibility)
56
+ target.define_method(name, source)
57
+ target.__send__(visibility, name)
58
+ end
59
+
60
+ def visibility_of(target, name)
61
+ return :private if target.private_method_defined?(name)
62
+
63
+ target.protected_method_defined?(name) ? :protected : :public
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through installer_spec.rb, which asks the installer for each of these.
4
+
5
+ require_relative "reflection"
6
+ require_relative "class_builders"
7
+ require_relative "registry"
8
+ require_relative "body_sync"
9
+
10
+ module RuntimeAnalysis
11
+ module Trace
12
+ class Installer
13
+ module Answers
14
+ def unpatched_method(mod, name) = Reflection.raw_instance_method(mod, name)
15
+ def source_location(method) = Reflection.source_location(method)
16
+ def default_builders = ClassBuilders.default.dup
17
+ def registry_after(previous) = Registry.after(previous)
18
+ def called_names(directories) = CalledNames.in(directories)
19
+ def watch_builders(builders, listener) = ClassBuilders.watch(builders, listener)
20
+ def body_sync = BodySync
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # no-unit-test: asserted through trace_spec.rb, whose examples declare visibility after a def and
4
+ # define singleton methods, and installer_spec.rb for the sweep itself.
5
+
6
+ module RuntimeAnalysis
7
+ module Trace
8
+ class Installer
9
+ module BodySync
10
+ private
11
+
12
+ def visibility_sync
13
+ @visibility_sync ||= TracePoint.new(:end) { |tp| @installer.body_closed(tp.self) }
14
+ rescue ArgumentError
15
+ @visibility_sync = NoTracePoint
16
+ end
17
+
18
+ module NoTracePoint
19
+ def self.enable; end
20
+ def self.disable; end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end