Debugging

lsof - List open files.

List information about files opened by processes on a UNIX box.

  $> lsof -p <pid1,pid2>

Include details about pid1 and pid2.

  $> lsof -p <pid1,pid2,^pid3>

Exclude pid3

  $> lsof -c ruby

Files opened by all processes executing a command starting with ruby.

  $> lsof /path/to/file

List processes that have the /path/to/file file open.

lsof /dev/sda1

What files are keeping /dev/sda1 busy.

  $> lsof -i:50-5000

All networking related to ports 50-5000

  $> lsof -i -a -c ruby

Inspect Internet connections for Ruby processes. -a is used to AND selections.

  $> lsof -r -c ruby

Puts lsof in repeat mode. There lsof lists open files as selected by other options, delays t seconds (default fifteen), then repeats the listing, delaying and listing repetitively until stopped by a condition defined by the prefix to the option.

If the prefix is a -, repeat mode is endless and lsof must be terminated with an interrupt or quit signal.

If the prefix is +, repeat mode will end the first cycle no open files are listed and of course lsof is stopped with an interrupt or quit signal. When repeat mode ends because no files are listed, the process exit code will be zero if any open files were ever listed; one, if none were ever listed.

  $> lsof -t -i:3000 | xargs -n 1 kill -9

Specifies that lsof should produce terse output with process identifiers only and no header. The output is piped to kill.

strace

Trace system calls and signals.

  $> irb
  $> strace -o strace.log -p <irb_pid>
  irb> require 'nokogiri'

See where a library was loaded and what its associated directory lookup order is.

Stare at the code until it makes sense.