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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +115 -0
- data/LICENSE.txt +96 -0
- data/README.md +164 -0
- data/editor/nvim/README.md +278 -0
- data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
- data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
- data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
- data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
- data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
- data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
- data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
- data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
- data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
- data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
- data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
- data/editor/nvim/plugin/runtime_analysis.lua +71 -0
- data/examples/trackers/PROMPT.md +86 -0
- data/examples/trackers/honeybadger.rb +19 -0
- data/examples/trackers/sentry.rb +16 -0
- data/ext/runtime_analysis/extconf.rb +68 -0
- data/ext/runtime_analysis/fast.c +775 -0
- data/ext/runtime_analysis/fast.h +59 -0
- data/ext/runtime_analysis/hold.c +560 -0
- data/ext/runtime_analysis/init.c +36 -0
- data/ext/runtime_analysis/names.c +78 -0
- data/ext/runtime_analysis/names.h +28 -0
- data/ext/runtime_analysis/novelty.c +139 -0
- data/ext/runtime_analysis/novelty.h +22 -0
- data/ext/runtime_analysis/os.c +73 -0
- data/ext/runtime_analysis/os.h +32 -0
- data/ext/runtime_analysis/ring.c +445 -0
- data/ext/runtime_analysis/rows.c +304 -0
- data/ext/runtime_analysis/sink.c +485 -0
- data/ext/runtime_analysis/sink.h +240 -0
- data/ext/runtime_analysis/state.c +254 -0
- data/ext/runtime_analysis/state.h +57 -0
- data/ext/runtime_analysis/wrapper.c +278 -0
- data/ext/runtime_analysis/wrapper.h +24 -0
- data/lib/runtime_analysis/errors.rb +34 -0
- data/lib/runtime_analysis/failsafe.rb +123 -0
- data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
- data/lib/runtime_analysis/host/declared.rb +152 -0
- data/lib/runtime_analysis/host/inject.rb +90 -0
- data/lib/runtime_analysis/host/job_runners.rb +27 -0
- data/lib/runtime_analysis/host/middleware.rb +23 -0
- data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
- data/lib/runtime_analysis/host/railtie.rb +42 -0
- data/lib/runtime_analysis/host/reports.rb +54 -0
- data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
- data/lib/runtime_analysis/host/web.rb +22 -0
- data/lib/runtime_analysis/host.rb +34 -0
- data/lib/runtime_analysis/inject.rb +5 -0
- data/lib/runtime_analysis/minitest.rb +9 -0
- data/lib/runtime_analysis/native/build.rb +17 -0
- data/lib/runtime_analysis/native/byte_size.rb +27 -0
- data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
- data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
- data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
- data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
- data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
- data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
- data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
- data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
- data/lib/runtime_analysis/native/sink.rb +12 -0
- data/lib/runtime_analysis/native.rb +74 -0
- data/lib/runtime_analysis/platform.rb +30 -0
- data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
- data/lib/runtime_analysis/query/index/graph.rb +107 -0
- data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
- data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
- data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
- data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
- data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
- data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
- data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
- data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
- data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
- data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
- data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
- data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
- data/lib/runtime_analysis/query/index/reading.rb +80 -0
- data/lib/runtime_analysis/query/index/shapes.rb +142 -0
- data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
- data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
- data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
- data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
- data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
- data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
- data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
- data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
- data/lib/runtime_analysis/query/index/signatures.rb +93 -0
- data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
- data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
- data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
- data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
- data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
- data/lib/runtime_analysis/query/index/static/method.rb +19 -0
- data/lib/runtime_analysis/query/index/static/params.rb +52 -0
- data/lib/runtime_analysis/query/index/static/source.rb +69 -0
- data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
- data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
- data/lib/runtime_analysis/query/index/static/value.rb +17 -0
- data/lib/runtime_analysis/query/index/static/values.rb +71 -0
- data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
- data/lib/runtime_analysis/query/index/static.rb +85 -0
- data/lib/runtime_analysis/query/index.rb +32 -0
- data/lib/runtime_analysis/query/nodes.rb +60 -0
- data/lib/runtime_analysis/query/project/config.rb +108 -0
- data/lib/runtime_analysis/query/project.rb +40 -0
- data/lib/runtime_analysis/query/report/document.rb +84 -0
- data/lib/runtime_analysis/query/report/finding.rb +15 -0
- data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
- data/lib/runtime_analysis/query/report/list.rb +15 -0
- data/lib/runtime_analysis/query/report/location.rb +14 -0
- data/lib/runtime_analysis/query/report/one_row.rb +14 -0
- data/lib/runtime_analysis/query/report/outline.rb +15 -0
- data/lib/runtime_analysis/query/report/paint.rb +34 -0
- data/lib/runtime_analysis/query/report/related.rb +11 -0
- data/lib/runtime_analysis/query/report/table.rb +15 -0
- data/lib/runtime_analysis/query/report/text.rb +13 -0
- data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
- data/lib/runtime_analysis/query/report.rb +35 -0
- data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
- data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
- data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
- data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
- data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
- data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
- data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
- data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
- data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
- data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
- data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
- data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
- data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
- data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
- data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
- data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
- data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
- data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
- data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
- data/lib/runtime_analysis/query/surfaces.rb +14 -0
- data/lib/runtime_analysis/query/tools/base.rb +86 -0
- data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
- data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
- data/lib/runtime_analysis/query/tools/origins.rb +47 -0
- data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
- data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
- data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
- data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
- data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
- data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
- data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
- data/lib/runtime_analysis/query/tools.rb +34 -0
- data/lib/runtime_analysis/query.rb +33 -0
- data/lib/runtime_analysis/recording/blob.rb +13 -0
- data/lib/runtime_analysis/recording/format.rb +32 -0
- data/lib/runtime_analysis/recording/opcodes.rb +30 -0
- data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
- data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
- data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
- data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
- data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
- data/lib/runtime_analysis/recording/seen.rb +63 -0
- data/lib/runtime_analysis/recording/sink.rb +19 -0
- data/lib/runtime_analysis/recording/source_blob.rb +20 -0
- data/lib/runtime_analysis/recording.rb +52 -0
- data/lib/runtime_analysis/rspec.rb +8 -0
- data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
- data/lib/runtime_analysis/ruby_parsing.rb +98 -0
- data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
- data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
- data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
- data/lib/runtime_analysis/trace/configuration.rb +115 -0
- data/lib/runtime_analysis/trace/entries.rb +47 -0
- data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
- data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
- data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
- data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
- data/lib/runtime_analysis/trace/hold.rb +142 -0
- data/lib/runtime_analysis/trace/inclusion.rb +46 -0
- data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
- data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
- data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
- data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
- data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
- data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
- data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
- data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
- data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
- data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
- data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
- data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
- data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
- data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
- data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
- data/lib/runtime_analysis/trace/installer.rb +149 -0
- data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
- data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
- data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
- data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
- data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
- data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
- data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
- data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
- data/lib/runtime_analysis/trace/mechanism.rb +28 -0
- data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
- data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
- data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
- data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
- data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
- data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
- data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
- data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
- data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
- data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
- data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
- data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
- data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
- data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
- data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
- data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
- data/lib/runtime_analysis/trace/novelty.rb +17 -0
- data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
- data/lib/runtime_analysis/trace/pure/state.rb +94 -0
- data/lib/runtime_analysis/trace/pure.rb +15 -0
- data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
- data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
- data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
- data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
- data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
- data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
- data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
- data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
- data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
- data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
- data/lib/runtime_analysis/trace/recorder.rb +149 -0
- data/lib/runtime_analysis/trace/state.rb +16 -0
- data/lib/runtime_analysis/trace.rb +142 -0
- data/lib/runtime_analysis/version.rb +6 -0
- data/lib/runtime_analysis/web.rb +6 -0
- data/lib/runtime_analysis.rb +125 -0
- metadata +330 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
#include "wrapper.h"
|
|
2
|
+
#include "fast.h"
|
|
3
|
+
#include <errno.h>
|
|
4
|
+
#include <poll.h>
|
|
5
|
+
#include <sched.h>
|
|
6
|
+
#include <stddef.h>
|
|
7
|
+
#include <stdlib.h>
|
|
8
|
+
#include <string.h>
|
|
9
|
+
#include <time.h>
|
|
10
|
+
#include <unistd.h>
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* The wrapper in front of each recorded method. Ruby's installer prepends an empty module onto a
|
|
14
|
+
* class and, for each method to record, calls Wrapper.install(mod, name, id). That keeps the
|
|
15
|
+
* method's registry id on the module, in an ivar keyed by the method's name, and defines the one C
|
|
16
|
+
* function ra_wrapper as that method, taking (argc, argv, self).
|
|
17
|
+
*
|
|
18
|
+
* On a call, ra_wrapper finds its id from the frame: rb_frame_method_id_and_class gives the name of
|
|
19
|
+
* the method running and the module that defines it, which is the prepended one. A call it has
|
|
20
|
+
* recorded before is handed to ra_fast_call, which writes the rows and calls super. Any other call
|
|
21
|
+
* is given to the Ruby recorder, which returns a token, and the wrapper calls super itself and
|
|
22
|
+
* hands the token back when the call ends, whether it returns or raises.
|
|
23
|
+
*
|
|
24
|
+
* Super is called with argv as it arrived, so no argument is copied and the block is carried
|
|
25
|
+
* through. Whenever something is missing, an id or the recorder or a state, the wrapper calls
|
|
26
|
+
* super and does nothing else. It never raises into the program it records.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
int ra_enabled = 0;
|
|
30
|
+
int ra_tier = 1;
|
|
31
|
+
int ra_hold = 0;
|
|
32
|
+
VALUE ra_trace = Qnil;
|
|
33
|
+
static ID id_ra_ids; /* ivar: method-name symbol -> registry id, per wrapper module */
|
|
34
|
+
static ID id_before; /* Trace.record_before_native */
|
|
35
|
+
static ID id_unwrapped; /* Trace.unwrapped */
|
|
36
|
+
static ID id_visibility_changed;
|
|
37
|
+
static ID id_finish; /* Trace.record_finish */
|
|
38
|
+
static ID id_method_missing; /* its first argument is the name asked for, read at every tier */
|
|
39
|
+
static ID id_exit; /* Trace.record_exit */
|
|
40
|
+
static ID id_raised_in; /* Trace.raised_in */
|
|
41
|
+
static ID id_displaced; /* Trace.call_displaced */
|
|
42
|
+
static long ra_displaced = 0; /* wrappers dropped with their old definition noted, see call_displaced */
|
|
43
|
+
|
|
44
|
+
/* Split argv into the positional array and the trailing keyword hash exactly as the Ruby wrapper's
|
|
45
|
+
* `*args, **kwargs` did, so the recorder reads the same tier-2/3 arg classes and kwargs row. Only done
|
|
46
|
+
* from tier 2 up; tier 1 reads no arguments, so it allocates nothing here. */
|
|
47
|
+
static void split_args(int argc, VALUE *argv, VALUE *args, VALUE *kwargs) {
|
|
48
|
+
int posc = argc;
|
|
49
|
+
*kwargs = Qnil;
|
|
50
|
+
if (argc > 0 && rb_keyword_given_p()) {
|
|
51
|
+
*kwargs = argv[argc - 1];
|
|
52
|
+
posc = argc - 1;
|
|
53
|
+
}
|
|
54
|
+
*args = rb_ary_new_from_values(posc, argv);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
struct super_ctx { int argc; VALUE *argv; VALUE token; int finished; };
|
|
58
|
+
|
|
59
|
+
static VALUE forward_super(VALUE p) {
|
|
60
|
+
struct super_ctx *c = (struct super_ctx *)p;
|
|
61
|
+
VALUE result = rb_call_super_kw(c->argc, c->argv, RB_PASS_CALLED_KEYWORDS);
|
|
62
|
+
c->finished = 1;
|
|
63
|
+
rb_funcall(ra_trace, id_finish, 3, c->token, Qnil, result);
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
void ra_carry_on(int state, void (*closing)(void *ctx, VALUE err), void *ctx) {
|
|
68
|
+
if (state == RA_TAG_RAISE) {
|
|
69
|
+
VALUE err = rb_errinfo();
|
|
70
|
+
/* Errors-only: the object the raise is unwinding through is kept until the request ends, so
|
|
71
|
+
* that a record only it holds is still there when the tracker asks where a nil came from. Only
|
|
72
|
+
* a raise comes here; a call that returns does not. */
|
|
73
|
+
if (ra_hold) rb_funcall(ra_trace, id_raised_in, 2, rb_current_receiver(), err);
|
|
74
|
+
closing(ctx, err);
|
|
75
|
+
rb_set_errinfo(err);
|
|
76
|
+
}
|
|
77
|
+
rb_jump_tag(state);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static void finish_raised(void *p, VALUE err) {
|
|
81
|
+
struct super_ctx *c = (struct super_ctx *)p;
|
|
82
|
+
if (c->finished) return;
|
|
83
|
+
c->finished = 1;
|
|
84
|
+
rb_funcall(ra_trace, id_finish, 3, c->token, err, Qnil);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static VALUE guarded_super(VALUE p) {
|
|
88
|
+
int state = 0;
|
|
89
|
+
VALUE result = rb_protect(forward_super, p, &state);
|
|
90
|
+
if (state) ra_carry_on(state, finish_raised, (void *)p);
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* Only a raise is finished above. A block's return, a break or a throw unwinds through the wrapper
|
|
95
|
+
* too, and would leave this call open on the tree with every later call nested under it. */
|
|
96
|
+
static VALUE close_abandoned(VALUE p) {
|
|
97
|
+
struct super_ctx *c = (struct super_ctx *)p;
|
|
98
|
+
if (!c->finished) rb_funcall(ra_trace, id_exit, 0);
|
|
99
|
+
return Qnil;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static VALUE just_super(int argc, VALUE *argv) {
|
|
103
|
+
return rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static VALUE trace_module(void) {
|
|
107
|
+
if (NIL_P(ra_trace)) {
|
|
108
|
+
ra_trace = rb_const_get(rb_const_get(rb_cObject, rb_intern("RuntimeAnalysis")), rb_intern("Trace"));
|
|
109
|
+
}
|
|
110
|
+
return ra_trace;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/* A wrapper that has been removed from its module can still be called, through a Method taken while
|
|
114
|
+
* it was in place. rspec-mocks takes one before it stubs a method, and and_call_original calls it
|
|
115
|
+
* from inside the stub. The wrapper's super would then reach the stub, which calls the handle again,
|
|
116
|
+
* until the stack runs out. So a dropped wrapper is told the definition it was dropped from
|
|
117
|
+
* (wrapper_displace, below) and the Ruby side runs that, recorded as a call to it. */
|
|
118
|
+
static VALUE call_displaced(VALUE displaced, int argc, VALUE *argv, VALUE self) {
|
|
119
|
+
VALUE *forwarded = ALLOCA_N(VALUE, argc + 2);
|
|
120
|
+
forwarded[0] = displaced;
|
|
121
|
+
forwarded[1] = self;
|
|
122
|
+
if (argc > 0) MEMCPY(forwarded + 2, argv, VALUE, argc);
|
|
123
|
+
|
|
124
|
+
VALUE block = rb_block_given_p() ? rb_block_proc() : Qnil;
|
|
125
|
+
return rb_funcall_with_block_kw(trace_module(), id_displaced, argc + 2, forwarded, block,
|
|
126
|
+
RB_PASS_CALLED_KEYWORDS);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
static VALUE ra_wrapper(int argc, VALUE *argv, VALUE self) {
|
|
130
|
+
/* With tracing off and nothing displaced, which is nearly always, this is the whole cost. */
|
|
131
|
+
if (!ra_enabled && ra_displaced == 0) return just_super(argc, argv);
|
|
132
|
+
|
|
133
|
+
ID mid;
|
|
134
|
+
VALUE defklass;
|
|
135
|
+
if (!rb_frame_method_id_and_class(&mid, &defklass)) return just_super(argc, argv);
|
|
136
|
+
|
|
137
|
+
VALUE ids = rb_ivar_get(defklass, id_ra_ids);
|
|
138
|
+
VALUE id = NIL_P(ids) ? Qnil : rb_hash_aref(ids, ID2SYM(mid));
|
|
139
|
+
if (NIL_P(id)) return just_super(argc, argv);
|
|
140
|
+
if (!FIXNUM_P(id)) return call_displaced(id, argc, argv, self);
|
|
141
|
+
if (!ra_enabled) return just_super(argc, argv);
|
|
142
|
+
|
|
143
|
+
ra_state *st = NULL;
|
|
144
|
+
ra_site from = { NULL, 0, 0, 0 };
|
|
145
|
+
long method = FIX2LONG(id);
|
|
146
|
+
if (ra_fast_method(method) && (st = ra_state_current())) {
|
|
147
|
+
VALUE result = ra_fast_call(st, method, &from, argc, argv, self);
|
|
148
|
+
if (result != Qundef) return result;
|
|
149
|
+
st->last_path_id = 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
VALUE args = Qnil, kwargs = Qnil;
|
|
153
|
+
if (ra_tier >= 2 || mid == id_method_missing || ra_probed_method(method)) split_args(argc, argv, &args, &kwargs);
|
|
154
|
+
|
|
155
|
+
ra_slow_calls++;
|
|
156
|
+
VALUE token = rb_funcall(ra_trace, id_before, 4, id, self, args, kwargs);
|
|
157
|
+
if (st) st->kept = 0;
|
|
158
|
+
if (NIL_P(token)) return just_super(argc, argv);
|
|
159
|
+
if (st && ra_site_known(&from)) ra_fast_remember(st, from.file, from.line);
|
|
160
|
+
|
|
161
|
+
struct super_ctx ctx = { argc, argv, token, 0 };
|
|
162
|
+
return rb_ensure(guarded_super, (VALUE)&ctx, close_abandoned, (VALUE)&ctx);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/* Install ra_wrapper as `name` on `mod`, remembering that name's registry id on the module. Called by
|
|
166
|
+
* the Ruby wrapper once per target method; visibility is set by the Ruby side afterwards. */
|
|
167
|
+
static VALUE wrapper_install(VALUE self, VALUE mod, VALUE name, VALUE id) {
|
|
168
|
+
ID mid = rb_to_id(name);
|
|
169
|
+
VALUE ids = rb_ivar_get(mod, id_ra_ids);
|
|
170
|
+
if (NIL_P(ids)) {
|
|
171
|
+
ids = rb_hash_new();
|
|
172
|
+
rb_ivar_set(mod, id_ra_ids, ids);
|
|
173
|
+
}
|
|
174
|
+
VALUE was = rb_hash_aref(ids, ID2SYM(mid));
|
|
175
|
+
if (!NIL_P(was) && !FIXNUM_P(was)) ra_displaced--;
|
|
176
|
+
rb_hash_aset(ids, ID2SYM(mid), id);
|
|
177
|
+
rb_define_method_id(mod, mid, ra_wrapper, -1);
|
|
178
|
+
return Qnil;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/* The Ruby wrapper is about to remove `name` from `mod`. In place of the registry id the name now
|
|
182
|
+
* holds [id, original], which ra_wrapper hands to call_displaced. wrapper_install puts the id back. */
|
|
183
|
+
static VALUE wrapper_displace(VALUE self, VALUE mod, VALUE name, VALUE original) {
|
|
184
|
+
VALUE ids = rb_ivar_get(mod, id_ra_ids);
|
|
185
|
+
if (NIL_P(ids)) return Qnil;
|
|
186
|
+
|
|
187
|
+
VALUE key = ID2SYM(rb_to_id(name));
|
|
188
|
+
VALUE id = rb_hash_aref(ids, key);
|
|
189
|
+
if (!FIXNUM_P(id)) return Qnil;
|
|
190
|
+
|
|
191
|
+
rb_hash_aset(ids, key, rb_assoc_new(id, original));
|
|
192
|
+
ra_displaced++;
|
|
193
|
+
return Qnil;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/* start(tier, gate = nil, fast = true, hold = false): the wrappers record from now on. `gate` is
|
|
197
|
+
* adaptive mode's, or nil when every call is kept. `fast` is false for a gate that only Ruby can
|
|
198
|
+
* ask, and every call then goes through Ruby. `hold` is true in errors-only recording, where Ruby
|
|
199
|
+
* is told of each tree that ends. */
|
|
200
|
+
static VALUE wrapper_start(int argc, VALUE *argv, VALUE self) {
|
|
201
|
+
VALUE tier, gate, fast, hold;
|
|
202
|
+
rb_scan_args(argc, argv, "13", &tier, &gate, &fast, &hold);
|
|
203
|
+
|
|
204
|
+
ra_tier = NUM2INT(tier);
|
|
205
|
+
ra_hold = RTEST(hold);
|
|
206
|
+
ra_fast_start(gate, argc < 3 || RTEST(fast));
|
|
207
|
+
trace_module();
|
|
208
|
+
ra_enabled = 1;
|
|
209
|
+
return Qnil;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/* stop: every wrapper passes its call straight through. */
|
|
213
|
+
static VALUE wrapper_stop(VALUE self) {
|
|
214
|
+
ra_enabled = 0;
|
|
215
|
+
return Qnil;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/* Module#define_method for every module, with any wrapper of ours among the bodies swapped for the
|
|
219
|
+
* method it wraps, so that `define_method(:html, Nokogiri.method(:HTML4))` copies the real method.
|
|
220
|
+
*
|
|
221
|
+
* It is written here because define_method reads the frame that called it, to see whether `private`
|
|
222
|
+
* is in force there. A Ruby method in between is a frame of its own, with nothing in force, and
|
|
223
|
+
* every method defined under `private` came out public. A C frame is passed over. */
|
|
224
|
+
static VALUE lookup_define_method(int argc, VALUE *argv, VALUE self) {
|
|
225
|
+
if (argc < 2) return just_super(argc, argv);
|
|
226
|
+
|
|
227
|
+
VALUE swapped = rb_funcall(trace_module(), id_unwrapped, 1, rb_ary_new_from_values(argc - 1, argv + 1));
|
|
228
|
+
Check_Type(swapped, T_ARRAY); /* anything else read as an Array is a crash in the host's define_method */
|
|
229
|
+
VALUE *forwarded = ALLOCA_N(VALUE, argc);
|
|
230
|
+
forwarded[0] = argv[0];
|
|
231
|
+
for (int i = 1; i < argc; i++) forwarded[i] = rb_ary_entry(swapped, i - 1);
|
|
232
|
+
|
|
233
|
+
VALUE defined = just_super(argc, forwarded);
|
|
234
|
+
RB_GC_GUARD(swapped);
|
|
235
|
+
return defined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/* Module#public, #private and #protected, and the two for class methods. A wrapper is given its
|
|
239
|
+
* method's visibility when the class is defined. A change made later by name, after the class body
|
|
240
|
+
* has closed, has to reach the wrapper too, or `Issue.send(:public, :hidden)` leaves the method as
|
|
241
|
+
* private as its wrapper still is. With no names these set the visibility of what follows in the
|
|
242
|
+
* caller's frame, which a Ruby method here would take for itself. A C frame is passed over. */
|
|
243
|
+
static VALUE lookup_visibility(int argc, VALUE *argv, VALUE self) {
|
|
244
|
+
VALUE result = just_super(argc, argv);
|
|
245
|
+
if (argc > 0 && ra_enabled) rb_funcall(trace_module(), id_visibility_changed, 1, self);
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/* lookups_on(mod): defines on `mod` the six methods above. Each is given the visibility Ruby gives
|
|
250
|
+
* its own: public, private and protected are private methods of Module. */
|
|
251
|
+
static VALUE wrapper_lookups_on(VALUE self, VALUE mod) {
|
|
252
|
+
rb_define_method(mod, "define_method", lookup_define_method, -1);
|
|
253
|
+
rb_define_private_method(mod, "public", lookup_visibility, -1);
|
|
254
|
+
rb_define_private_method(mod, "private", lookup_visibility, -1);
|
|
255
|
+
rb_define_private_method(mod, "protected", lookup_visibility, -1);
|
|
256
|
+
rb_define_method(mod, "public_class_method", lookup_visibility, -1);
|
|
257
|
+
rb_define_method(mod, "private_class_method", lookup_visibility, -1);
|
|
258
|
+
return Qnil;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
void Init_ra_wrapper(VALUE mWrapper) {
|
|
262
|
+
id_ra_ids = rb_intern("@__ra_ids");
|
|
263
|
+
id_before = rb_intern("record_before_native");
|
|
264
|
+
id_finish = rb_intern("record_finish");
|
|
265
|
+
id_displaced = rb_intern("call_displaced");
|
|
266
|
+
id_method_missing = rb_intern("method_missing");
|
|
267
|
+
id_exit = rb_intern("record_exit");
|
|
268
|
+
id_raised_in = rb_intern("raised_in");
|
|
269
|
+
id_unwrapped = rb_intern("unwrapped");
|
|
270
|
+
id_visibility_changed = rb_intern("visibility_changed");
|
|
271
|
+
rb_global_variable(&ra_trace); /* keep the cached module marked and updated across GC compaction */
|
|
272
|
+
|
|
273
|
+
rb_define_singleton_method(mWrapper, "install", wrapper_install, 3);
|
|
274
|
+
rb_define_singleton_method(mWrapper, "displace", wrapper_displace, 3);
|
|
275
|
+
rb_define_singleton_method(mWrapper, "lookups_on", wrapper_lookups_on, 1);
|
|
276
|
+
rb_define_singleton_method(mWrapper, "start", wrapper_start, -1);
|
|
277
|
+
rb_define_singleton_method(mWrapper, "stop", wrapper_stop, 0);
|
|
278
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#ifndef RA_WRAPPER_H
|
|
2
|
+
#define RA_WRAPPER_H
|
|
3
|
+
|
|
4
|
+
#include <ruby.h>
|
|
5
|
+
|
|
6
|
+
/* What the Ruby side has told the wrapper, which the code that records a call for it reads too. */
|
|
7
|
+
extern int ra_enabled; /* mirrors Trace.enabled?; toggled by Trace.start/stop */
|
|
8
|
+
extern int ra_tier; /* mirrors the recorder's tier; args are read only from tier 2 up */
|
|
9
|
+
extern int ra_hold; /* errors-only recording: Ruby is told of each call tree that ends */
|
|
10
|
+
extern VALUE ra_trace; /* RuntimeAnalysis::Trace, resolved once the Ruby side is loaded */
|
|
11
|
+
|
|
12
|
+
/* What rb_protect reports for an exception, CRuby's RUBY_TAG_RAISE. It is not in the public headers
|
|
13
|
+
* and has been 6 since 1.9. A wrapper needs it to tell a raise from a throw, and to carry an
|
|
14
|
+
* exception on with rb_jump_tag: raising it again would send it `backtrace`, which a program that
|
|
15
|
+
* overrides or mocks that would hear, and would fire a second :raise event. */
|
|
16
|
+
#define RA_TAG_RAISE 6
|
|
17
|
+
|
|
18
|
+
/* Carries on the unwind that rb_protect stopped. For a raise, `closing` is first handed the
|
|
19
|
+
* exception, which is still the one in flight when it is done. Does not return. */
|
|
20
|
+
NORETURN(void ra_carry_on(int state, void (*closing)(void *ctx, VALUE err), void *ctx));
|
|
21
|
+
|
|
22
|
+
void Init_ra_wrapper(VALUE mWrapper);
|
|
23
|
+
|
|
24
|
+
#endif
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuntimeAnalysis
|
|
4
|
+
module Errors
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
def missing_file = Errno::ENOENT
|
|
8
|
+
|
|
9
|
+
def reader_gone = Errno::EPIPE
|
|
10
|
+
|
|
11
|
+
def disk_full = Errno::ENOSPC
|
|
12
|
+
|
|
13
|
+
def out_of_memory = Errno::ENOMEM
|
|
14
|
+
|
|
15
|
+
def no_child_left = Errno::ECHILD
|
|
16
|
+
|
|
17
|
+
def not_permitted = Errno::EACCES
|
|
18
|
+
|
|
19
|
+
def unreadable_json
|
|
20
|
+
require "json"
|
|
21
|
+
JSON::ParserError
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def timed_out
|
|
25
|
+
require "timeout"
|
|
26
|
+
Timeout::Error
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def unusable_options
|
|
30
|
+
require "optparse"
|
|
31
|
+
OptionParser::ParseError
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuntimeAnalysis
|
|
4
|
+
module Failsafe
|
|
5
|
+
LIMIT = 10
|
|
6
|
+
FAILED = Object.new.freeze
|
|
7
|
+
|
|
8
|
+
INTERNAL = [StandardError, ScriptError, SystemStackError].freeze
|
|
9
|
+
|
|
10
|
+
RAISES_FROM_OTHER_THREADS_DEFERRED = { Object => :never }.freeze
|
|
11
|
+
|
|
12
|
+
FIRST = "runtime_analysis: an error inside the recorder was caught and the program carries on: %s. " \
|
|
13
|
+
"Later ones are counted in RuntimeAnalysis.stats[:errors] and not reported."
|
|
14
|
+
OFF = "runtime_analysis: %d errors inside the recorder, the last %s. Recording is off for process %d."
|
|
15
|
+
HOST = "runtime_analysis: the block given to RuntimeAnalysis.reported raised and its error was caught: %s. " \
|
|
16
|
+
"Later ones are counted in RuntimeAnalysis.stats[:hook_errors] and not reported."
|
|
17
|
+
|
|
18
|
+
@errors = 0
|
|
19
|
+
@host_errors = 0
|
|
20
|
+
@off = false
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
attr_accessor :strict
|
|
24
|
+
attr_reader :errors, :last
|
|
25
|
+
|
|
26
|
+
def guard
|
|
27
|
+
Thread.handle_interrupt(RAISES_FROM_OTHER_THREADS_DEFERRED) do
|
|
28
|
+
yield
|
|
29
|
+
rescue *INTERNAL => e
|
|
30
|
+
caught(e)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def masked(&)
|
|
35
|
+
Thread.handle_interrupt(RAISES_FROM_OTHER_THREADS_DEFERRED, &)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def rescued
|
|
39
|
+
yield
|
|
40
|
+
rescue *INTERNAL => e
|
|
41
|
+
caught(e)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def attempt(fallback = nil, &)
|
|
45
|
+
result = guard(&)
|
|
46
|
+
FAILED.equal?(result) ? fallback : result
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def host(&)
|
|
50
|
+
Thread.handle_interrupt(RAISES_FROM_OTHER_THREADS_DEFERRED) { hosted(&) }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def off? = @off
|
|
54
|
+
|
|
55
|
+
def failed = FAILED
|
|
56
|
+
|
|
57
|
+
def frames = FRAMES
|
|
58
|
+
|
|
59
|
+
def limit = LIMIT
|
|
60
|
+
|
|
61
|
+
def reset
|
|
62
|
+
@errors = 0
|
|
63
|
+
@host_errors = 0
|
|
64
|
+
@off = false
|
|
65
|
+
@last = nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def stats
|
|
69
|
+
{ errors: @errors, switched_off: @off, last_error: (describe(@last) if @last), hook_errors: @host_errors }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def report(line)
|
|
73
|
+
io = $stderr
|
|
74
|
+
io.respond_to?(:write_nonblock) ? io.write_nonblock("#{line}\n") : io.write("#{line}\n")
|
|
75
|
+
rescue *INTERNAL
|
|
76
|
+
nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def hosted
|
|
82
|
+
yield
|
|
83
|
+
rescue *INTERNAL => e
|
|
84
|
+
@host_errors += 1
|
|
85
|
+
report(format(HOST, describe(e))) if @host_errors == 1
|
|
86
|
+
FAILED
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def caught(error)
|
|
90
|
+
raise if strict
|
|
91
|
+
|
|
92
|
+
count(error)
|
|
93
|
+
FAILED
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def count(error)
|
|
97
|
+
@errors += 1
|
|
98
|
+
@last = error
|
|
99
|
+
report(format(FIRST, describe(error))) if @errors == 1
|
|
100
|
+
switch_off if @errors == LIMIT
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def switch_off
|
|
104
|
+
@off = true
|
|
105
|
+
stop_recording
|
|
106
|
+
report(format(OFF, @errors, describe(@last), Process.pid))
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def stop_recording
|
|
110
|
+
Trace.stop
|
|
111
|
+
rescue *INTERNAL
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def describe(error)
|
|
116
|
+
place = error.backtrace&.first
|
|
117
|
+
place ? "#{error.class} at #{place}" : error.class.to_s
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
FRAMES = masked { caller_locations(0).size } - caller_locations(0).size
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted by railtie_spec.rb, which runs a controller action through the Railtie's hook.
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Host
|
|
7
|
+
module ControllerEntryPoint
|
|
8
|
+
def self.included(base)
|
|
9
|
+
base.around_action { |controller, action| controller.runtime_analysis_entry_point(&action) }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def runtime_analysis_entry_point(&)
|
|
13
|
+
identity = "#{request.method} #{request.path} (#{self.class.name}##{action_name})"
|
|
14
|
+
RuntimeAnalysis.with_entry_point_unless_set(identity, &)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Host
|
|
7
|
+
module Declared
|
|
8
|
+
FRAMEWORKS = { jobs: %w[ActiveJob::Base Sidekiq::Job Sidekiq::Worker].freeze,
|
|
9
|
+
mailers: %w[ActionMailer::Base].freeze,
|
|
10
|
+
channels: %w[ActionCable::Channel::Base].freeze }.freeze
|
|
11
|
+
LOADED_WHEN_NEEDED = %w[app/jobs app/mailers app/channels].freeze
|
|
12
|
+
UNLISTABLE = [StandardError, ScriptError].freeze
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def write(config, app, pid: Process.pid)
|
|
17
|
+
dir = config.recording_dir
|
|
18
|
+
return unless File.exist?(File.join(dir, format("run.%d.json", pid)))
|
|
19
|
+
|
|
20
|
+
File.write(File.join(dir, format("declared.%d.json", pid)), JSON.generate(of(app, config.inclusion)))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def write_at_exit(app)
|
|
24
|
+
at_exit { Failsafe.attempt { write(RuntimeAnalysis.configuration, app) } }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def of(app, inclusion)
|
|
28
|
+
listed { load_classes(app) }
|
|
29
|
+
present = kinds(app, Own.new(inclusion)).reject { |kind, _list| absent?(kind) }
|
|
30
|
+
present.transform_values { |list| listed(&list) }.compact
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def absent?(kind)
|
|
34
|
+
FRAMEWORKS.key?(kind) && FRAMEWORKS.fetch(kind).none? { |name| const_defined?(name) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def kinds(app, own)
|
|
38
|
+
{ routes: -> { routes(app) }, jobs: -> { jobs(own) }, mailers: -> { actions(:mailers, own) },
|
|
39
|
+
channels: -> { actions(:channels, own) }, hooks: -> { Hooks.new(own).to_a } }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def listed
|
|
43
|
+
yield
|
|
44
|
+
rescue *UNLISTABLE
|
|
45
|
+
nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def load_classes(app)
|
|
49
|
+
return if app.config.eager_load
|
|
50
|
+
|
|
51
|
+
loader = app.autoloaders.main
|
|
52
|
+
loaded_when_needed(app.root).each { |dir| loader.eager_load_dir(dir.to_s) }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def loaded_when_needed(root)
|
|
56
|
+
LOADED_WHEN_NEEDED.map { |dir| root.join(dir) }.select(&:directory?)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def routes(app)
|
|
60
|
+
app.reload_routes_unless_loaded if app.respond_to?(:reload_routes_unless_loaded)
|
|
61
|
+
app.routes.routes.filter_map { |route| reached(route) unless route.internal }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def reached(route)
|
|
65
|
+
controller, action = route.defaults.values_at(:controller, :action)
|
|
66
|
+
return unless controller && action
|
|
67
|
+
|
|
68
|
+
[route.verb, route.path.spec.to_s.delete_suffix("(.:format)"), "#{controller}##{action}"]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def jobs(own)
|
|
72
|
+
entry_points(:jobs, own) { |job| "#{job.name}#perform" if defines?(job, :perform) }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def actions(kind, own)
|
|
76
|
+
entry_points(kind, own) { |klass| klass.action_methods.map { |action| "#{klass.name}##{action}" } }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def entry_points(kind, own, &)
|
|
80
|
+
own.under(FRAMEWORKS.fetch(kind)).flat_map(&).compact.sort
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def defines?(mod, name)
|
|
84
|
+
mod.method_defined?(name, false) || mod.private_method_defined?(name, false)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
class Own
|
|
88
|
+
def initialize(inclusion)
|
|
89
|
+
@inclusion = inclusion
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def include?(mod)
|
|
93
|
+
not_since_reloaded?(mod) && @inclusion.include?(Object.const_source_location(mod.name)&.first)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def classes
|
|
97
|
+
@classes ||= below(Object).select { |klass| include?(klass) }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def under(names)
|
|
101
|
+
bases = defined_among(names)
|
|
102
|
+
classes.select { |klass| bases.any? { |base| klass < base } }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def defined_among(names)
|
|
108
|
+
names.select { |name| Object.const_defined?(name) }.map { |name| Object.const_get(name) }
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def below(klass)
|
|
112
|
+
klass.subclasses.flat_map { |subclass| [subclass, *below(subclass)] }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def not_since_reloaded?(mod)
|
|
116
|
+
Object.const_get(String(mod.name)).equal?(mod)
|
|
117
|
+
rescue NameError
|
|
118
|
+
false
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
class Hooks
|
|
123
|
+
def initialize(own)
|
|
124
|
+
@own = own
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def to_a
|
|
128
|
+
@own.classes.flat_map { |klass| of(klass) }.sort
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
private
|
|
132
|
+
|
|
133
|
+
def of(klass)
|
|
134
|
+
own = klass.instance_methods(false) + klass.private_instance_methods(false) - [:initialize]
|
|
135
|
+
own.filter_map { |name| hook(klass, name, overridden(klass, name)) }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def hook(klass, name, owner)
|
|
139
|
+
["#{klass.name}##{name}", "#{owner.name}##{name}"] if owner
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def overridden(klass, name)
|
|
143
|
+
klass.ancestors.find { |ancestor| outside?(ancestor) && Declared.defines?(ancestor, name) }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def outside?(mod)
|
|
147
|
+
mod.name && !@own.include?(mod)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|