Bug 889956 - Dump the length of the string when we do a GC log. r=njn
authorJustin Lebar <justin.lebar@gmail.com>
Wed, 10 Jul 2013 07:01:53 -0400
changeset 145541 8aeb99497de3bc2147776efcbe598ff732d6271f
parent 145540 0d65b1c05b2f2dcacf88bbac2038516b4c6525dd
child 145542 812075ed2f63fc323dd5df8c47706ac5831d0740
push idunknown
push userunknown
push dateunknown
reviewersnjn
bugs889956
milestone25.0a1
Bug 889956 - Dump the length of the string when we do a GC log. r=njn
js/src/jsapi.cpp
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -2406,16 +2406,28 @@ JS_TraceRuntime(JSTracer *trc)
 }
 
 JS_PUBLIC_API(void)
 JS_TraceChildren(JSTracer *trc, void *thing, JSGCTraceKind kind)
 {
     js::TraceChildren(trc, thing, kind);
 }
 
+static size_t
+CountDecimalDigits(size_t num)
+{
+    size_t numDigits = 0;
+    do {
+        num /= 10;
+        numDigits++;
+    } while (num > 0);
+
+    return numDigits;
+}
+
 JS_PUBLIC_API(void)
 JS_GetTraceThingInfo(char *buf, size_t bufsize, JSTracer *trc, void *thing,
                      JSGCTraceKind kind, JSBool details)
 {
     const char *name = NULL; /* silence uninitialized warning */
     size_t n;
 
     if (bufsize == 0)
@@ -2487,18 +2499,29 @@ JS_GetTraceThingInfo(char *buf, size_t b
             break;
           }
 
           case JSTRACE_STRING:
           {
             *buf++ = ' ';
             bufsize--;
             JSString *str = (JSString *)thing;
-            if (str->isLinear())
+
+            if (str->isLinear()) {
+                bool willFit = str->length() + strlen("<length > ") +
+                               CountDecimalDigits(str->length()) < bufsize;
+
+                n = JS_snprintf(buf, bufsize, "<length %d%s> ",
+                                (int)str->length(),
+                                willFit ? "" : " (truncated)");
+                buf += n;
+                bufsize -= n;
+
                 PutEscapedString(buf, bufsize, &str->asLinear(), 0);
+            }
             else
                 JS_snprintf(buf, bufsize, "<rope: length %d>", (int)str->length());
             break;
           }
 
           case JSTRACE_SCRIPT:
           {
             JSScript *script = static_cast<JSScript *>(thing);