CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/metasploit/framework/spec/threads/logger.rb
Views: 11789
1
#
2
# Standard Library
3
#
4
5
require 'securerandom'
6
7
#
8
# Project
9
#
10
11
require 'metasploit/framework/spec/threads/suite'
12
13
original_thread_new = Thread.method(:new)
14
15
# Patches `Thread.new` so that if logs `caller` so thread leaks can be traced
16
Thread.define_singleton_method(:new) { |*args, &block|
17
uuid = SecureRandom.uuid
18
# tag caller with uuid so that only leaked threads caller needs to be printed
19
lines = ["BEGIN Thread.new caller (#{uuid})"]
20
21
caller.each do |frame|
22
lines << " #{frame}"
23
end
24
25
lines << 'END Thread.new caller'
26
27
Metasploit::Framework::Spec::Threads::Suite::LOG_PATHNAME.parent.mkpath
28
29
Metasploit::Framework::Spec::Threads::Suite::LOG_PATHNAME.open('a') { |f|
30
# single puts so threads can't write in between each other.
31
f.puts lines.join("\n")
32
}
33
34
options = {original_args: args, uuid: uuid}
35
36
original_thread_new.call(options) {
37
# record uuid for thread-leak detection can used uuid to correlate log with this thread.
38
Thread.current[Metasploit::Framework::Spec::Threads::Suite::UUID_THREAD_LOCAL_VARIABLE] = options.fetch(:uuid)
39
block.call(*options.fetch(:original_args))
40
}
41
}
42