changeset 73:b87b6ebb6e86

Memory profiler server is now much better at reporting errors.
author Atul Varma <varmaa@toolness.com>
date Thu, 25 Jun 2009 18:38:01 -0700
parents eca8642eed4a
children ed2cf86a7c9d
files memory_profiler.cpp memory_profiler_server.js spidermonkey-playground.cpp tcb.cpp tcb.h
diffstat 5 files changed, 66 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/memory_profiler.cpp	Thu Jun 25 18:16:41 2009 -0700
+++ b/memory_profiler.cpp	Thu Jun 25 18:38:01 2009 -0700
@@ -363,7 +363,8 @@
   if (!JS_EvaluateScript(serverCx, serverGlobal, source,
                          fileSize, SERVER_FILENAME, 1,
                          &serverRval)) {
-    JS_ReportError(cx, "Server failed.");
+    TCB_handleError(serverCx, serverGlobal);
+    JS_ReportError(cx, "Running server script failed.");
     return JS_FALSE;
   }
 
--- a/memory_profiler_server.js	Thu Jun 25 18:16:41 2009 -0700
+++ b/memory_profiler_server.js	Thu Jun 25 18:38:01 2009 -0700
@@ -1,3 +1,28 @@
+// This function is called by the platform whenever an uncaught exception
+// occurs.
+
+function handleError() {
+  printTraceback(lastExceptionTraceback);
+  print(lastException);
+}
+
+// This function uses the Python-inspired traceback functionality of the
+// playground to print a stack trace that looks much like Python's.
+
+function printTraceback(frame) {
+  print("Traceback (most recent call last):");
+  if (frame === undefined)
+    frame = stack();
+  var lines = [];
+  while (frame) {
+    var line = ('  File "' + frame.filename + '", line ' +
+                frame.lineNo + ', in ' + frame.functionName);
+    lines.splice(0, 0, line);
+    frame = frame.caller;
+  }
+  print(lines.join('\n'));
+}
+
 // This is just a test to exercise the code a bit.
 JSON.stringify(getObjectInfo(getGCRoots()[0]));
 
@@ -58,7 +83,7 @@
 
     if (path == "/")
       toSend = HELP;
-    
+
     if (path == "/gc-roots")
       toSend = JSON.stringify(getGCRoots());
 
@@ -67,6 +92,7 @@
 
     var objNum = path.match(/^\/objects\/(\d+)$/);
     if (objNum) {
+      throw new Error('wut');
       objNum = objNum[1];
       var objInfo = getObjectInfo(objNum);
       if (objInfo) {
@@ -94,7 +120,7 @@
     conn.close();
     //print("response sent.");
   } catch (e) {
-    print(e.toString());
+    handleError();
     try { conn.close(); } catch (e) {}
   }
   if (path == "/stop")
@@ -102,5 +128,14 @@
   return true;
 }
 
-while (processRequest(socket)) {}
+var keepGoing = true;
+
+while (keepGoing) {
+  try {
+    keepGoing = processRequest(socket);
+  } catch (e) {
+    handleError();
+  }
+}
+
 socket.close();
--- a/spidermonkey-playground.cpp	Thu Jun 25 18:16:41 2009 -0700
+++ b/spidermonkey-playground.cpp	Thu Jun 25 18:38:01 2009 -0700
@@ -213,12 +213,7 @@
   if (!JS_EvaluateScript(tcb_cx, tcb_global, source,
                          fileSize, TCB_FILENAME, 1,
                          &rval)) {
-    jsval handleError;
-    if (JS_GetProperty(tcb_cx, tcb_global, "handleError", &handleError) &&
-        JSVAL_IS_OBJECT(handleError) &&
-        JS_ObjectIsFunction(tcb_cx, JSVAL_TO_OBJECT(handleError))) {
-      JS_CallFunctionValue(tcb_cx, tcb_global, handleError, 0, NULL, &rval);
-    }
+    TCB_handleError(tcb_cx, tcb_global);
     return 1;
   }
 
--- a/tcb.cpp	Thu Jun 25 18:16:41 2009 -0700
+++ b/tcb.cpp	Thu Jun 25 18:38:01 2009 -0700
@@ -278,6 +278,29 @@
   return JSTRAP_CONTINUE;
 }
 
+void TCB_handleError(JSContext *cx, JSObject *global)
+{
+  jsval handleError;
+  jsval rval;
+
+  if (!JS_GetProperty(cx, global, "handleError", &handleError)) {
+    printf("Getting handleError property of global failed.\n");
+    return;
+  }
+
+  if (JSVAL_IS_OBJECT(handleError) &&
+      JS_ObjectIsFunction(cx, JSVAL_TO_OBJECT(handleError))) {
+    if (!JS_CallFunctionValue(cx, global,
+                              handleError, 0, NULL, &rval)) {
+      printf("An error occurred, but calling handleError() failed.\n");
+      return;
+    } else
+      return;
+  }
+
+  printf("An error occurred, but no handleError() is defined.\n");
+}
+
 JSBool TCB_init(JSContext *cx, jsval *rval)
 {
 #ifdef DEBUG
--- a/tcb.h	Thu Jun 25 18:16:41 2009 -0700
+++ b/tcb.h	Thu Jun 25 18:38:01 2009 -0700
@@ -61,3 +61,5 @@
                                   jsbytecode *pc, jsval *rval, void *closure);
 
 extern JSBool TCB_init(JSContext *cx, jsval *rval);
+
+extern void TCB_handleError(JSContext *cx, JSObject *global);