| 1 |
# frozen_string_literal: false |
| 2 |
require 'test/unit' |
| 3 |
require '-test-/debug' |
| 4 |
|
| 5 |
class SampleClassForTestProfileFrames |
| 6 |
class << self |
| 7 |
attr_accessor :sample4 |
| 8 |
end |
| 9 |
|
| 10 |
self.sample4 = Module.new do |
| 11 |
def self.corge(block) |
| 12 |
Sample2.new.baz(block) |
| 13 |
end |
| 14 |
end |
| 15 |
|
| 16 |
class Sample2 |
| 17 |
EVAL_LINE = __LINE__ + 3 |
| 18 |
|
| 19 |
def baz(block) |
| 20 |
instance_eval "def zab(block) block.call end" |
| 21 |
[self, zab(block)] |
| 22 |
end |
| 23 |
end |
| 24 |
|
| 25 |
module Sample3 |
| 26 |
class << self |
| 27 |
def qux(block) |
| 28 |
SampleClassForTestProfileFrames.sample4.corge(block) |
| 29 |
end |
| 30 |
end |
| 31 |
end |
| 32 |
|
| 33 |
def self.bar(block) |
| 34 |
Sample3.qux(block) |
| 35 |
end |
| 36 |
|
| 37 |
def foo(block) |
| 38 |
self.class.bar(block) |
| 39 |
end |
| 40 |
end |
| 41 |
|
| 42 |
class SampleClassForTestProfileThreadFrames |
| 43 |
def initialize(mutex) |
| 44 |
@mutex = mutex |
| 45 |
end |
| 46 |
|
| 47 |
def foo(block) |
| 48 |
bar(block) |
| 49 |
end |
| 50 |
|
| 51 |
def bar(block) |
| 52 |
block.call |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
class TestProfileFrames < Test::Unit::TestCase |
| 57 |
def test_profile_frames |
| 58 |
obj, frames = Fiber.new{ |
| 59 |
Fiber.yield SampleClassForTestProfileFrames.new.foo(lambda{ Bug::Debug.profile_frames(0, 10) }) |
| 60 |
}.resume |
| 61 |
|
| 62 |
labels = [ |
| 63 |
nil, |
| 64 |
"test_profile_frames", |
| 65 |
"zab", |
| 66 |
"baz", |
| 67 |
"corge", |
| 68 |
"qux", |
| 69 |
"bar", |
| 70 |
"foo", |
| 71 |
"test_profile_frames", |
| 72 |
] |
| 73 |
base_labels = [ |
| 74 |
nil, |
| 75 |
"test_profile_frames", |
| 76 |
"zab", |
| 77 |
"baz", |
| 78 |
"corge", |
| 79 |
"qux", |
| 80 |
"bar", |
| 81 |
"foo", |
| 82 |
"test_profile_frames", |
| 83 |
] |
| 84 |
full_labels = [ |
| 85 |
"Bug::Debug.profile_frames", |
| 86 |
"TestProfileFrames#test_profile_frames", |
| 87 |
"#{obj.inspect}.zab", |
| 88 |
"SampleClassForTestProfileFrames::Sample2#baz", |
| 89 |
"#{SampleClassForTestProfileFrames.sample4.inspect}.corge", |
| 90 |
"SampleClassForTestProfileFrames::Sample3.qux", |
| 91 |
"SampleClassForTestProfileFrames.bar", |
| 92 |
"SampleClassForTestProfileFrames#foo", |
| 93 |
"TestProfileFrames#test_profile_frames", |
| 94 |
] |
| 95 |
classes = [ |
| 96 |
Bug::Debug, |
| 97 |
TestProfileFrames, |
| 98 |
obj, |
| 99 |
SampleClassForTestProfileFrames::Sample2, |
| 100 |
SampleClassForTestProfileFrames.sample4, |
| 101 |
SampleClassForTestProfileFrames::Sample3, |
| 102 |
SampleClassForTestProfileFrames, # singleton method |
| 103 |
SampleClassForTestProfileFrames, |
| 104 |
TestProfileFrames, |
| 105 |
] |
| 106 |
singleton_method_p = [ |
| 107 |
true, false, true, false, true, true, true, false, false, false, |
| 108 |
] |
| 109 |
method_names = [ |
| 110 |
"profile_frames", |
| 111 |
"test_profile_frames", |
| 112 |
"zab", |
| 113 |
"baz", |
| 114 |
"corge", |
| 115 |
"qux", |
| 116 |
"bar", |
| 117 |
"foo", |
| 118 |
"test_profile_frames", |
| 119 |
] |
| 120 |
qualified_method_names = [ |
| 121 |
"Bug::Debug.profile_frames", |
| 122 |
"TestProfileFrames#test_profile_frames", |
| 123 |
"#{obj.inspect}.zab", |
| 124 |
"SampleClassForTestProfileFrames::Sample2#baz", |
| 125 |
"#{SampleClassForTestProfileFrames.sample4.inspect}.corge", |
| 126 |
"SampleClassForTestProfileFrames::Sample3.qux", |
| 127 |
"SampleClassForTestProfileFrames.bar", |
| 128 |
"SampleClassForTestProfileFrames#foo", |
| 129 |
"TestProfileFrames#test_profile_frames", |
| 130 |
] |
| 131 |
paths = [ nil, file=__FILE__, "(eval at #{__FILE__}:#{SampleClassForTestProfileFrames::Sample2::EVAL_LINE})", file, file, file, file, file, file, nil ] |
| 132 |
absolute_paths = [ "<cfunc>", file, nil, file, file, file, file, file, file, nil ] |
| 133 |
|
| 134 |
assert_equal(labels.size, frames.size) |
| 135 |
|
| 136 |
frames.each.with_index{|(path, absolute_path, label, base_label, full_label, first_lineno, |
| 137 |
classpath, singleton_p, method_name, qualified_method_name), i| |
| 138 |
err_msg = "#{i}th frame" |
| 139 |
assert_equal(paths[i], path, err_msg) |
| 140 |
assert_equal(absolute_paths[i], absolute_path, err_msg) |
| 141 |
assert_equal(labels[i], label, err_msg) |
| 142 |
assert_equal(base_labels[i], base_label, err_msg) |
| 143 |
assert_equal(singleton_method_p[i], singleton_p, err_msg) |
| 144 |
assert_equal(method_names[i], method_name, err_msg) |
| 145 |
assert_equal(qualified_method_names[i], qualified_method_name, err_msg) |
| 146 |
assert_equal(full_labels[i], full_label, err_msg) |
| 147 |
assert_match(classes[i].inspect, classpath, err_msg) |
| 148 |
if label == method_name |
| 149 |
c = classes[i] |
| 150 |
m = singleton_p ? c.method(method_name) : c.instance_method(method_name) |
| 151 |
assert_equal(m.source_location[1], first_lineno, err_msg) |
| 152 |
end |
| 153 |
} |
| 154 |
end |
| 155 |
|
| 156 |
def test_profile_thread_frames |
| 157 |
mutex = Mutex.new |
| 158 |
th = Thread.new do |
| 159 |
mutex.lock |
| 160 |
Thread.stop |
| 161 |
SampleClassForTestProfileThreadFrames.new(mutex).foo(lambda { mutex.unlock; loop { sleep(1) } } ) |
| 162 |
end |
| 163 |
|
| 164 |
# ensure execution has reached SampleClassForTestProfileThreadFrames#bar before running profile_thread_frames |
| 165 |
loop { break if th.status == "sleep"; sleep 0.1 } |
| 166 |
th.run |
| 167 |
mutex.lock # wait until SampleClassForTestProfileThreadFrames#bar has been called |
| 168 |
|
| 169 |
frames = Bug::Debug.profile_thread_frames(th, 0, 10) |
| 170 |
|
| 171 |
full_labels = [ |
| 172 |
"Kernel#sleep", |
| 173 |
"TestProfileFrames#test_profile_thread_frames", |
| 174 |
"Kernel#loop", |
| 175 |
"TestProfileFrames#test_profile_thread_frames", |
| 176 |
"SampleClassForTestProfileThreadFrames#bar", |
| 177 |
"SampleClassForTestProfileThreadFrames#foo", |
| 178 |
"TestProfileFrames#test_profile_thread_frames", |
| 179 |
] |
| 180 |
|
| 181 |
frames.each.with_index do |frame, i| |
| 182 |
assert_equal(full_labels[i], frame) |
| 183 |
end |
| 184 |
|
| 185 |
ensure |
| 186 |
th.kill |
| 187 |
th.join |
| 188 |
end |
| 189 |
|
| 190 |
|
| 191 |
def test_matches_backtrace_locations_main_thread |
| 192 |
assert_equal(Thread.current, Thread.main) |
| 193 |
|
| 194 |
# Keep these in the same line, so the backtraces match exactly |
| 195 |
backtrace_locations, profile_frames = [Thread.current.backtrace_locations, Bug::Debug.profile_frames(0, 100)] |
| 196 |
|
| 197 |
errmsg = "backtrace_locations:\n " + backtrace_locations.map.with_index{|loc, i| "#{i} #{loc}"}.join("\n ") |
| 198 |
errmsg += "\n\nprofile_frames:\n " + profile_frames.map.with_index{|(path, absolute_path, _, base_label, _, _, _, _, _, full_label, lineno), i| |
| 199 |
if lineno |
| 200 |
"#{i} #{absolute_path}:#{lineno} // #{full_label}" |
| 201 |
else |
| 202 |
"#{i} #{absolute_path} #{full_label}" |
| 203 |
end |
| 204 |
}.join("\n ") |
| 205 |
assert_equal(backtrace_locations.size, profile_frames.size, errmsg) |
| 206 |
|
| 207 |
# The first entries are not going to match, since one is #backtrace_locations and the other #profile_frames |
| 208 |
backtrace_locations.shift |
| 209 |
profile_frames.shift |
| 210 |
|
| 211 |
# The rest of the stack is expected to look the same... |
| 212 |
backtrace_locations.zip(profile_frames).each.with_index do |(location, (path, absolute_path, _, base_label, _, _, _, _, _, _, lineno)), i| |
| 213 |
next if absolute_path == "<cfunc>" # ...except for cfunc frames |
| 214 |
|
| 215 |
err_msg = "#{i}th frame" |
| 216 |
assert_equal(location.absolute_path, absolute_path, err_msg) |
| 217 |
assert_equal(location.base_label, base_label, err_msg) |
| 218 |
assert_equal(location.lineno, lineno, err_msg) |
| 219 |
assert_equal(location.path, path, err_msg) |
| 220 |
end |
| 221 |
end |
| 222 |
|
| 223 |
def test_ifunc_frame |
| 224 |
bug11851 = '[ruby-core:72409] [Bug #11851]' |
| 225 |
assert_ruby_status([], <<~'end;', bug11851) # do |
| 226 |
require '-test-/debug' |
| 227 |
class A |
| 228 |
include Bug::Debug |
| 229 |
def x |
| 230 |
profile_frames(0, 10) |
| 231 |
end |
| 232 |
end |
| 233 |
def a |
| 234 |
[A.new].each(&:x) |
| 235 |
end |
| 236 |
a |
| 237 |
end; |
| 238 |
end |
| 239 |
end |