# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true module Contrast module Utils # this module extends HeadDumpUtil module HeadDumpExtend def log_enabled_warning control = Contrast::Utils::HeapDumpUtil.control dir = control[:path] window = control[:window] count = control[:count] delay = control[:delay] clean = control[:clean] logger.info(<<~WARNING) ***************************************************** ******** HEAP DUMP HAS BEEN ENABLED ******** *** APPLICATION PROCESS WILL EXIT UPON COMPLETION *** ***************************************************** Heap dump is a debugging tool that snapshots the entire state of the Ruby VM. It is an exceptionally expensive process, and should only be used to debug especially pernicious errors. It will write multiple memory snaphots, which are liable to be multiple gigabytes in size. They will be named "[unix timestamp]-heap.dump", e.g.: 1020304050-heap.dump It will then call Ruby `exit()`. If this is not your specific intent, you can (and should) disable this option in your Contrast config file. HEAP DUMP PARAMETERS: \t[write files to this directory] dir: #{ dir } \t[wait this many seconds in between dumps] window: #{ window } \t[heap dump this many times] count: #{ count } \t[wait this many seconds into app lifetime] delay: #{ delay } \t[perform gc pass before dump] clean: #{ clean } ***************************************************** ******** YOU HAVE BEEN WARNED ******** ***************************************************** WARNING end def capture_heap_dump control = Contrast::Utils::HeapDumpUtil.control dir = control[:path] window = control[:window] count = control[:count] clean = control[:clean] logger.info('HEAP DUMP MAIN LOOP') ObjectSpace.trace_object_allocations_start count.times do |i| logger.info('STARTING HEAP DUMP PASS', current_pass: i, max: count) snapshot_heap(dir, clean) logger.info('FINISHING HEAP DUMP PASS', current_pass: i, max: count) sleep(window) end ensure ObjectSpace.trace_object_allocations_stop logger.info('*****************************************************') logger.info('******** HEAP DUMP HAS CONCLUDED ********') logger.info('*** APPLICATION PROCESS WILL EXIT SHORTLY ***') logger.info('*****************************************************') exit # rubocop:disable Rails/Exit We weren't kidding! end end end end