B2G/gecko/toolkit/crashreporter/test/unit/head_crashreporter.js
/*
 * Run an xpcshell subprocess and crash it.
 *
 * @param setup
 *        A string of JavaScript code to execute in the subprocess
 *        before crashing. If this is a function and not a string,
 *        it will have .toSource() called on it, and turned into
 *        a call to itself. (for programmer convenience)
 *        This code will be evaluted between crasher_subprocess_head.js
 *        and crasher_subprocess_tail.js, so it will have access
 *        to everything defined in crasher_subprocess_head.js,
 *        which includes "crashReporter", a variable holding
 *        the crash reporter service.
 *
 * @param callback
 *        A JavaScript function to be called after the subprocess
 *        crashes. It will be passed (minidump, extra), where
 *         minidump is an nsILocalFile of the minidump file produced,
 *         and extra is an object containing the key,value pairs from
 *         the .extra file.
 *
 * @param canReturnZero
 *       If true, the subprocess may return with a zero exit code.
 *       Certain types of crashes may not cause the process to
 *       exit with an error.
 */
Run an xpcshell subprocess and crash it.
@param setup
A string of JavaScript code to execute in the subprocess
before crashing. If this is a function and not a string,
it will have .toSource() called on it, and turned into
a call to itself. (for programmer convenience)
This code will be evaluted between crasher_subprocess_head.js
and crasher_subprocess_tail.js, so it will have access
to everything defined in crasher_subprocess_head.js,
which includes "crashReporter", a variable holding
the crash reporter service.

@param callback
A JavaScript function to be called after the subprocess
crashes. It will be passed (minidump, extra), where
minidump is an nsILocalFile of the minidump file produced,
and extra is an object containing the key,value pairs from
the .extra file.

@param canReturnZero
If true, the subprocess may return with a zero exit code.
Certain types of crashes may not cause the process to
exit with an error.
Run an xpcshell subprocess and crash it . @param setup A string of JavaScript code to execute in the subprocess before crashing . If this is a function and not a string it will have . toSource () called on it and turned into a call to itself . ( for programmer convenience ) This code will be evaluted between crasher_subprocess_head . js and crasher_subprocess_tail . js so it will have access to everything defined in crasher_subprocess_head . js which includes " crashReporter " a variable holding the crash reporter service . @param callback A JavaScript function to be called after the subprocess crashes . It will be passed ( minidump extra ) where minidump is an nsILocalFile of the minidump file produced and extra is an object containing the key value pairs from the . extra file . @param canReturnZero If true the subprocess may return with a zero exit code . Certain types of crashes may not cause the process to exit with an error .
function do_crash(setup, callback, canReturnZero)
{
  let ds = Components.classes["@mozilla.org/file/directory_service;1"]
    .getService(Components.interfaces.nsIProperties);
  let bin = ds.get("CurProcD", Components.interfaces.nsILocalFile);
  bin.append("xpcshell");
  if (!bin.exists()) {
    bin.leafName = "xpcshell.exe";
    do_check_true(bin.exists());
    if (!bin.exists())
      do_throw("Can't find xpcshell binary!");
  }
  let greD = ds.get("GreD", Components.interfaces.nsILocalFile);
  let headfile = do_get_file("crasher_subprocess_head.js");
  let tailfile = do_get_file("crasher_subprocess_tail.js");
  let process = Components.classes["@mozilla.org/process/util;1"]
                  .createInstance(Components.interfaces.nsIProcess);
  process.init(bin);
  let args = ['-g', greD.path,
              '-f', headfile.path];
  if (setup) {
    if (typeof(setup) == "function")
      setup = "("+setup.toSource()+")();";
    args.push('-e', setup);
  }
  args.push('-f', tailfile.path);
  try {
      process.run(true, args, args.length);
  }
  catch(ex) {} 
  if (!canReturnZero) {
    do_check_neq(process.exitValue, 0);
  }
  handleMinidump(callback);
}
function do_crash ( setup , callback , canReturnZero ) { let ds = Components . classes [ "@mozilla.org/file/directory_service;1" ] . getService ( Components . interfaces . nsIProperties ) ; let bin = ds . get ( "CurProcD" , Components . interfaces . nsILocalFile ) ; bin . append ( "xpcshell" ) ; if ( ! bin . exists ( ) ) { bin . leafName = "xpcshell.exe" ; do_check_true ( bin . exists ( ) ) ; if ( ! bin . exists ( ) ) do_throw ( "Can't find xpcshell binary!" ) ; } let greD = ds . get ( "GreD" , Components . interfaces . nsILocalFile ) ; let headfile = do_get_file ( "crasher_subprocess_head.js" ) ; let tailfile = do_get_file ( "crasher_subprocess_tail.js" ) ; let process = Components . classes [ "@mozilla.org/process/util;1" ] . createInstance ( Components . interfaces . nsIProcess ) ; process . init ( bin ) ; let args = [ '-g' , greD . path , '-f' , headfile . path ] ; if ( setup ) { if ( typeof ( setup ) == "function" ) setup = "(" + setup . toSource ( ) + ")();" ; args . push ( '-e' , setup ) ; } args . push ( '-f' , tailfile . path ) ; try { process . run ( true , args , args . length ) ; } catch ( ex ) { } if ( ! canReturnZero ) { do_check_neq ( process . exitValue , 0 ) ; } handleMinidump ( callback ) ; }
Run an xpcshell subprocess and crash it.
Run an xpcshell subprocess and crash it .
// get current process filename (xpcshell) // weird, can't find xpcshell binary? // get Gre dir (GreD) // run xpcshell -g GreD -f head -e "some setup code" -f tail // funky, but convenient // on Windows we exit with a -1 status when crashing. // should exit with an error (should have crashed)