Path: blob/master/lib/metasploit/framework/spec/threads/logger.rb
21558 views
#1# Standard Library2#34require 'securerandom'56#7# Project8#910require 'metasploit/framework/spec/threads/suite'1112original_thread_new = Thread.method(:new)1314# Patches `Thread.new` so that if logs `caller` so thread leaks can be traced15Thread.define_singleton_method(:new) { |*args, &block|16uuid = SecureRandom.uuid17# tag caller with uuid so that only leaked threads caller needs to be printed18lines = ["BEGIN Thread.new caller (#{uuid})"]1920caller.each do |frame|21lines << " #{frame}"22end2324lines << 'END Thread.new caller'2526Metasploit::Framework::Spec::Threads::Suite::LOG_PATHNAME.parent.mkpath2728Metasploit::Framework::Spec::Threads::Suite::LOG_PATHNAME.open('a') { |f|29# single puts so threads can't write in between each other.30f.puts lines.join("\n")31}3233options = {original_args: args, uuid: uuid}3435original_thread_new.call(options) {36# record uuid for thread-leak detection can used uuid to correlate log with this thread.37Thread.current[Metasploit::Framework::Spec::Threads::Suite::UUID_THREAD_LOCAL_VARIABLE] = options.fetch(:uuid)38block.call(*options.fetch(:original_args))39}40}4142