For future reference when debugging inside JS modules (where no dump function is available)

Tags: , , , , , , , — Jeff @ 19:59
function dump(msg)
  var mod = {};
  Components.utils.import("resource://gre/modules/ctypes.jsm", mod);
  var ctypes = mod.ctypes;
  var libc = ctypes.open("libc.so.6");
  var puts =
    libc.declare("puts", ctypes.default_abi, ctypes.int, ctypes.char.ptr);
  puts("!!!!!!!!!!!!!!!!!!!\n" +
       "!!! " + msg + "\n" +


  1. It has been pointed out to me that Components.utils.reportError is a simpler alternative. I was aware of it, but it had (unfortunately, given my inexperience with ctypes) slipped my mind. I needed this for xpcshell test debugging, however, so reportError wouldn’t have sufficed — and even if I had been debugging in-browser, it would still have been valuable as a pedagogical exercise. 🙂

    Comment by Jeff — 26.08.10 @ 20:59

  2. So modules in xpcshell tests don’t get access to either dump or print? That sucks…

    Comment by Neil — 27.08.10 @ 02:27

  3. What I like doing is to import Services.jsm and do
    which gives me messages in the error console and
    for errors in the error console.

    Of course, during xpcshell tests, I guess both are not what you want 🙁

    Comment by Robert Kaiser — 27.08.10 @ 09:29

  4. Just a bit! It’s entirely justifiable and logical; it’s just not at all helpful when it’s the only communication channel at your disposal.

    Comment by Jeff — 27.08.10 @ 11:31

  5. I’m confused. I’m pretty sure I’ve used dump() in js code modules in the past, and I just doublechecked it using a trivial module loaded from xpcshell:

    function fun() {
    dump(‘hi world how is it going\n’);

    EXPORTED_SYMBOLS = [‘fun’];

    and ran:

    xpcshell-1.9.2 -e ‘Components.utils.import(“file:///tmp/blah.js”); fun();’

    which printed the string on stderr as expected. Is there a way to load js modules and not have dump() I’m overlooking?

    Comment by Marien Zwart — 28.08.10 @ 09:04

  6. Dunno: whatever the reason it’s working for you, for me, in the situation where I was trying to use it, it wasn’t doing anything.

    Comment by Jeff — 28.08.10 @ 20:15

RSS feed for comments on this post. TrackBack URI

Leave a comment

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>