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

Fix exceptions not being inspectable when running binary from PATH #8807

Merged
merged 1 commit into from
Feb 18, 2020

Conversation

willhbr
Copy link
Contributor

@willhbr willhbr commented Feb 15, 2020

If the binary is added to $PATH, PROGRAM_NAME is the name of the file,
but not the path to read debug information from. This causes a file open
error, which is incredibly hard to debug. Using Process.executable_path will
instead always be the current executable.

A simple demonstration of this issue is:

def bar
  raise "Oh no it didn't work"
rescue e
  puts e.inspect_with_backtrace
end

bar

Build and move this binary somewhere in your $PATH:

$ crystal build exceptions.cr
$ mv exceptions ~/.local/bin
$ exceptions
Unhandled exception: Error opening file 'exceptions' with mode 'r': No such file or directory (Errno)             
Error while trying to dump the backtrace: Error opening file 'exceptions' with mode 'r': No such file or directory (Errno)

With this patch, the exception is printed as expected:

Oh no it didn't work (Exception)
  from exceptions.cr:2:3 in 'bar'
  from exceptions.cr:11:1 in '__crystal_main'
  from /home/will/projects/gh/crystal-lang/crystal/src/crystal/main.cr:106:5 in 'main_user_code'
  from /home/will/projects/gh/crystal-lang/crystal/src/crystal/main.cr:92:7 in 'main'
  from /home/will/projects/gh/crystal-lang/crystal/src/crystal/main.cr:115:3 in 'main'
  from __libc_start_main
  from _start
  from ???

@willhbr
Copy link
Contributor Author

willhbr commented Feb 15, 2020

Ok I thought alpine was failing because it didn't support /proc/self/exe, but I've changed it to use Process.executable_path and it's still failing. Any advice would be appreciated here!

@rdp
Copy link
Contributor

rdp commented Feb 15, 2020

LGTM, almost wish we'd fix PROGRAM_NAME to always be absolute instead but... :)

Here's what I get on my box without it:

Unhandled exception: Error opening file 'bad4' with mode 'r': No such file or directory (Errno)
Failed to raise an exception: END_OF_STACK
[0x563b1e098ef6] *CallStack::print_backtrace:Int32 +118
[0x563b1e07e786] __crystal_raise +86
[0x563b1e07ecae] ???
[0x563b1e0b6f43] *Crystal::System::File::open<String, String, File::Permissions>:Int32 +163
[0x563b1e0b35e3] *File::new<String, String, File::Permissions, Nil, Nil>:File +67
[0x563b1e0900bd] *CallStack::read_dwarf_sections:(Array(Tuple(UInt64, UInt64, String)) | Nil) +109
[0x563b1e08fe6d] *CallStack::decode_line_number<UInt64>:Tuple(String, Int32, Int32) +45
[0x563b1e08f67e] *CallStack#decode_backtrace:Array(String) +302
[0x563b1e08f532] *CallStack#printable_backtrace:Array(String) +50
[0x563b1e0e5ffd] *Exception+ +77
[0x563b1e0e5e48] *Exception+ +120
[0x563b1e0e3dd9] *AtExitHandlers::run<Int32>:Int32 +441
[0x563b1e0f049b] *Crystal::main<Int32, Pointer(Pointer(UInt8))>:Int32 +139
[0x563b1e089196] main +6
[0x7efeda25ab6b] __libc_start_main +235
[0x563b1e07d6fa] _start +42

If you search for read_dwarf_sections there are a few reports out there :)

@bcardiff
Copy link
Member

It’s #8738 do not worry about alpine

@straight-shoota
Copy link
Member

I'm not sure this should use PROGRAM_NAME at all. It's a compile time value and not necessarily represents the binary name at runtime.

I'd also like to make this behaviour more resilient in case the file can't be read at all. For example because the executable doesn't exist anymore. We should make sure to still print a meaningful account of the original error.

Copy link
Contributor

@ysbaddaden ysbaddaden left a comment

Choose a reason for hiding this comment

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

I agree with @straight-shoota, and we already do it for Darwin/Mach-O targets. Let's follow up for ELF based targets!

src/callstack.cr Outdated
@@ -367,7 +367,7 @@ struct CallStack
@@base_address : UInt64|UInt32|Nil

protected def self.read_dwarf_sections
Debug::ELF.open(PROGRAM_NAME) do |elf|
Debug::ELF.open(Process.executable_path || PROGRAM_NAME) do |elf|
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Debug::ELF.open(Process.executable_path || PROGRAM_NAME) do |elf|
return unless path = Process.executable_path
Debug::ELF.open(path) do |elf|

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Member

Choose a reason for hiding this comment

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

What if exception_path is present but File.open fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then we're back in the same position, but I think making that more robust should be done in another PR - it'll have to be done for the other executable types as well.

src/callstack.cr Outdated Show resolved Hide resolved
@willhbr
Copy link
Contributor Author

willhbr commented Feb 16, 2020

I've added a check for if the file exists (like the MacOS version), unsure of what to do to indicate that there was a problem reading the backtrace, but I think that can be addressed in another PR.

src/callstack.cr Outdated Show resolved Hide resolved
src/callstack.cr Outdated Show resolved Hide resolved
If the binary is added to $PATH, PROGRAM_NAME is the name of the file,
but not the path to read debug information from. This causes a file open
error, which is incredibly hard to debug.
Copy link
Contributor

@RX14 RX14 left a comment

Choose a reason for hiding this comment

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

This is fine, though adding Debug::ELF.open? which returned nil without running the block if the open call failed would be neater.

@RX14 RX14 merged commit ed0be70 into crystal-lang:master Feb 18, 2020
@RX14 RX14 added this to the 0.34.0 milestone Feb 18, 2020
@RX14 RX14 added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:runtime labels Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants