Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add helper to easily enable core dumps #2010

Merged
merged 6 commits into from
May 13, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/datadog/enable_core_dumps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This helper is used to enable core dumps for the current Ruby app. This is useful when debugging native-level
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a significant side effect, so it needs # typed: ignore otherwise Sorbet will evaluate the file!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 14afd4d

# crashes.
#
# It can be enabled simply by adding `require 'datadog/enable_core_dumps'` to start of the app.

lambda do
lloeki marked this conversation as resolved.
Show resolved Hide resolved
current_size, maximum_size = Process.getrlimit(:CORE)
core_pattern =
begin
File.read('/proc/sys/kernel/core_pattern').strip
rescue
'(Could not open /proc/sys/kernel/core_pattern)'
end

if maximum_size <= 0
Kernel.warn("[DDTRACE] Could not enable core dumps on crash, maximum size is #{maximum_size} (disabled).")
return
elsif maximum_size == current_size
Kernel.warn('[DDTRACE] Core dumps already enabled, nothing to do!')
return
end

begin
Process.setrlimit(:CORE, maximum_size)
rescue => e
Kernel.warn(
"[DDTRACE] Failed to enable core dumps. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
)
return
end

if current_size == 0
Kernel.warn("[DDTRACE] Enabled core dumps. Maximum size: #{maximum_size} Output pattern: '#{core_pattern}'")
else
Kernel.warn(
"[DDTRACE] Raised core dump limit. Old size: #{current_size} " \
"Maximum size: #{maximum_size} Output pattern: '#{core_pattern}'"
)
end
end.call