Title: [91627] trunk/Source/_javascript_Core
Revision
91627
Author
commit-qu...@webkit.org
Date
2011-07-22 17:19:05 -0700 (Fri, 22 Jul 2011)

Log Message

Patch by Sommer Panage <pan...@apple.com> on 2011-07-22
Reviewed by Oliver Hunt.

export JSContextCreateBacktrace as SPI in JSContextRefPrivate.h
https://bugs.webkit.org/show_bug.cgi?id=64981

UIAutomation for iOS would like to support a _javascript_ backtrace in our error logs.
Currently, the C API does not provide the tools to do this. However, the private API
does expose the necessary functionality to get a backtrace
(via Interpreter::retrieveLastCaller). We recognize this information may result in
failure in the cases of programs run by 'eval', stack frames beneath host function
call frames, and in programs run from other programs. Thus, we propose exporting our
JSContextCreateBacktrace in JSContextRefPrivate.h. This will provide us with the tools
we need while not advertising an API that isn't really ready for full use.

* API/JSContextRef.cpp:
* API/JSContextRefPrivate.h:
* _javascript_Core.exp:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/API/JSContextRef.cpp (91626 => 91627)


--- trunk/Source/_javascript_Core/API/JSContextRef.cpp	2011-07-23 00:18:34 UTC (rev 91626)
+++ trunk/Source/_javascript_Core/API/JSContextRef.cpp	2011-07-23 00:19:05 UTC (rev 91627)
@@ -29,12 +29,16 @@
 
 #include "APICast.h"
 #include "InitializeThreading.h"
+#include <interpreter/CallFrame.h>
+#include <interpreter/Interpreter.h>
 #include "JSCallbackObject.h"
 #include "JSClassRef.h"
 #include "JSGlobalObject.h"
 #include "JSObject.h"
+#include "UStringBuilder.h"
 #include <wtf/text/StringHash.h>
 
+
 #if OS(DARWIN)
 #include <mach-o/dyld.h>
 
@@ -177,3 +181,60 @@
 
     return toGlobalRef(exec->lexicalGlobalObject()->globalExec());
 }
+    
+JSStringRef JSContextCreateBacktrace(JSContextRef ctx, unsigned maxStackSize)
+{
+    ExecState* exec = toJS(ctx);
+    JSLock lock(exec);
+
+    unsigned count = 0;
+    UStringBuilder builder;
+    CallFrame* callFrame = exec;
+    UString functionName;
+    if (exec->callee()) {
+        if (asObject(exec->callee())->inherits(&InternalFunction::s_info)) {
+            functionName = asInternalFunction(exec->callee())->name(exec);
+            builder.append("#0 ");
+            builder.append(functionName);
+            builder.append("() ");
+            count++;
+        }
+    }
+    while (true) {
+        ASSERT(callFrame);
+        int signedLineNumber;
+        intptr_t sourceID;
+        UString urlString;
+        JSValue function;
+        
+        UString levelStr = UString::number(count);
+        
+        exec->interpreter()->retrieveLastCaller(callFrame, signedLineNumber, sourceID, urlString, function);
+
+        if (function)
+            functionName = asFunction(function)->name(exec);
+        else {
+            // Caller is unknown, but if frame is empty we should still add the frame, because
+            // something called us, and gave us arguments.
+            if (count)
+                break;
+        }
+        unsigned lineNumber = signedLineNumber >= 0 ? signedLineNumber : 0;
+        if (!builder.isEmpty())
+            builder.append("\n");
+        builder.append("#");
+        builder.append(levelStr);
+        builder.append(" ");
+        builder.append(functionName);
+        builder.append("() at ");
+        builder.append(urlString);
+        builder.append(":");
+        builder.append(UString::number(lineNumber));
+        if (!function || ++count == maxStackSize)
+            break;
+        callFrame = callFrame->callerFrame();
+    }
+    return OpaqueJSString::create(builder.toUString()).leakRef();
+}
+
+

Modified: trunk/Source/_javascript_Core/API/JSContextRefPrivate.h (91626 => 91627)


--- trunk/Source/_javascript_Core/API/JSContextRefPrivate.h	2011-07-23 00:18:34 UTC (rev 91626)
+++ trunk/Source/_javascript_Core/API/JSContextRefPrivate.h	2011-07-23 00:19:05 UTC (rev 91627)
@@ -46,6 +46,15 @@
 */
 JS_EXPORT JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx);
 
+    
+/*!
+@function
+@abstract Gets a Backtrace for the existing context
+@param ctx The JSContext whose backtrace you want to get
+@result A string containing the backtrace
+*/
+JS_EXPORT JSStringRef JSContextCreateBacktrace(JSContextRef ctx, unsigned maxStackSize) AVAILABLE_IN_WEBKIT_VERSION_4_0;
+    
 #ifdef __cplusplus
 }
 #endif

Modified: trunk/Source/_javascript_Core/ChangeLog (91626 => 91627)


--- trunk/Source/_javascript_Core/ChangeLog	2011-07-23 00:18:34 UTC (rev 91626)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-07-23 00:19:05 UTC (rev 91627)
@@ -1,3 +1,24 @@
+2011-07-22  Sommer Panage  <pan...@apple.com>
+
+        Reviewed by Oliver Hunt.
+
+        export JSContextCreateBacktrace as SPI in JSContextRefPrivate.h
+        https://bugs.webkit.org/show_bug.cgi?id=64981
+
+        UIAutomation for iOS would like to support a _javascript_ backtrace in our error logs.
+        Currently, the C API does not provide the tools to do this. However, the private API
+        does expose the necessary functionality to get a backtrace
+        (via Interpreter::retrieveLastCaller). We recognize this information may result in
+        failure in the cases of programs run by 'eval', stack frames beneath host function
+        call frames, and in programs run from other programs. Thus, we propose exporting our
+        JSContextCreateBacktrace in JSContextRefPrivate.h. This will provide us with the tools
+        we need while not advertising an API that isn't really ready for full use.
+
+        * API/JSContextRef.cpp:
+        * API/JSContextRefPrivate.h:
+        * _javascript_Core.exp:
+
+
 2011-07-22  Gavin Barraclough  <barraclo...@apple.com>
 
         https://bugs.webkit.org/show_bug.cgi?id=65051

Modified: trunk/Source/_javascript_Core/_javascript_Core.exp (91626 => 91627)


--- trunk/Source/_javascript_Core/_javascript_Core.exp	2011-07-23 00:18:34 UTC (rev 91626)
+++ trunk/Source/_javascript_Core/_javascript_Core.exp	2011-07-23 00:19:05 UTC (rev 91627)
@@ -8,6 +8,7 @@
 _JSContextGroupCreate
 _JSContextGroupRelease
 _JSContextGroupRetain
+_JSContextCreateBacktrace
 _JSEndProfiling
 _JSEvaluateScript
 _JSGarbageCollect
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to