OSX “strace” equivalent: dtruss – seeing inside applications what they do and why they hang

I am running OSX desktops coming from Linux background. We cross-develop software both on OSX and Linux. I am familiar of debugging system issues on Linux using strace command and now I had to try something similar on OSX. strace prints out system calls a running application makes so you can see “inside” the application and see where it crashes, hangs, etc.

OSX doesn’t have strace, but has its equivalent dtrace which originates from Solaris and I believe it has replaced OSX ktrace, truss etc. commands since OSX 10.5 Leopard.

Here are brief instructions how I tracked down a non-working Subversion svn up command. svn was correctly updating some repositories, but not others and just hung which means it probably hung on some system call.

Turns out dtrace itself is a very low level framework which its own programming language. I just want to dump the system calls. So there exist an utility called dtruss for this purpose.

But the first attempt failed:

dtruss svn up
dtrace: failed to initialize dtrace: DTrace requires additional privileges

You could solve this by running dtruss as root, but we really don’t want to do that. You can enable normal user execution for dtruss / dtrace with the following command:

sudo chmod u+s /usr/sbin/dtrace

Now we dtruss get output:

dtruss svn up
...
write(0x5, "( set-path ( 0: 94 false ( ) infinity ) ) ( link-path ( 10:thirdparty 31:svn+ssh://repo-svn/thirdparty 107 false ( ) infinity ) ) ( finish-report ( ) ) \0", 0x9A)         = 154 0
read(0x6, "( success ( ( ) 0: ) ) ( failure ( ( 170000 82:'svn+ssh://repo-svn/thirdparty' is not the same repository as 'svn+ssh://indweb' 63:/build/buildd/subversion-1.6.12dfsg/subversion/svnserve/serve.c 310 ) ) ) \0", 0x1000)         = 207 0
fcntl(0x5, 0x3, 0x0)         = 1 0
fcntl(0x5, 0x4, 0x5)         = 0 0
write(0x5, "( failure ( ( 210001 25:Unknown command 'failure' 34:subversion/libsvn_ra_svn/editorp.c 887 ) ) ) \0", 0x62)         = 98 0
fcntl(0x5, 0x3, 0x0)         = 5 0
fcntl(0x5, 0x4, 0x1)         = 0 0
read(0x6, "( failure ( ( 210001 25:Unknown command 'failure' 70:/build/buildd/subversion-1.6.12dfsg/subversion/libsvn_ra_svn/marshal.c 931 ) ) ) \0", 0x1000)         = 134 0
__disable_threadsignal(0x1, 0x0, 0x0)         = 0 0

… and there it hangs. Unknown command “failure”. In the end this all was not very helpful and I decided just selfupdate Macports and take clean checkout.

1. More info

 

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

Calculate aspect ratio conserving resize for images in Javascript

This is one way to do it. As a bonus no ifs needed.

   
     /**
      * Resize arbitary width x height region to fit inside another region.
      *
      * Conserve aspect ratio of the orignal region. Useful when shrinking/enlarging
      * images to fit into a certain area.
      *
      * @param {Number} srcWidth Source area width
      *
      * @param {Number} srcHeight Source area height
      *
      * @param {Number} maxWidth Fittable area maximum available width
      *
      * @param {Number} srcWidth Fittable area maximum available height
      *
      * @return {Object} { width, heigth }
      *
      */
     calculateAspectRatioFit : function(srcWidth, srcHeight, maxWidth, maxHeight) {

        var ratio = [maxWidth / srcWidth, maxHeight / srcHeight ];
        ratio = Math.min(ratio[0], ratio[1]);

        return { width:srcWidth*ratio, height:srcHeight*ratio };
     },

\"\" Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+