143

This is useful for debugging (hence programming related). On linux, we can use the command

strace -feopen python myfile.py

to figure out which python modules and shared objects are loaded. Is there an equivalent one-liner on macOS X?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Setjmp
  • 27,279
  • 27
  • 74
  • 92

1 Answers1

193

I suppose you meant strace -fetrace=open?

dtruss -f -t open python myfile.py
ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 2
    Hi ephemient, I just tested your version of strace (with -fetrace=open) and it gave exactly the same outuput as my example. I look forward to testing your dtruss command shortly. Thanks for the quick response! – Setjmp Dec 18 '09 at 06:43
  • 3
    Somehow, today, sudo dtruss doesn't work for me. It's as if Python is not executed at all! – Dima Tisnek Jul 18 '13 at 14:27
  • 3
    @qarma sudo-ing back again might solve your problem: http://serverfault.com/questions/215510/enable-dtrace-without-sudo-on-mac-os-x/215531#215531 – ErikR Oct 26 '13 at 02:51
  • btw., some `open` calls are reported as `open_nocancel` on osx. same wrt. `read` and `read_nocancel`. – Dima Tisnek Nov 24 '13 at 22:13
  • btw., some `open` calls are reported as `open_nocancel` on osx. same wrt. `read` `recv` `close` and perhaps a bunch/all others, they seems to have `*_nocancel` counterparts. – Dima Tisnek Nov 24 '13 at 22:15
  • 33
    `dtruss` requires root privileges, but you might not want to run the command you want traced as root. Consider `sudo dtruss -f -t open sudo -u $USER python myfile.py` – a paid nerd Jul 27 '14 at 17:48
  • The link to `dtruss` in your answer is broken. – michaelbahr May 02 '17 at 10:28
  • @michaelbahr Apparently Apple moved their manpages to `/legacy/` now. – ephemient May 02 '17 at 15:48
  • 13
    Getting an error with `$ sudo dtruss -f -t open sudo -u $USER python`: ` dtrace: system integrity protection is on, some features will not be available` and then `dtrace: failed to execute sudo: dtrace cannot control executables signed with restricted entitlements` – blueyed Feb 05 '18 at 16:41
  • The only way to run `dtruss` as an `strace`/`ltrace` replacement on macOS (since roughly 2018) is to disable SIP through `csrutil disable` in recovery mode. It's unfortunate, especially if one only needs it for a quick session to see which files are opened by process or similar query tasks. – ikaerom Jan 02 '23 at 10:35