2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
2016-05-14 00:15:03 +00:00
|
|
|
#include "JSCTracing.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-07-25 11:45:08 +00:00
|
|
|
#if defined(WITH_FBSYSTRACE) && (defined(WITH_JSC_EXTRA_TRACING) || DEBUG)
|
|
|
|
#define USE_JSCTRACING 1
|
|
|
|
#else
|
|
|
|
#define USE_JSCTRACING 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_JSCTRACING
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <fbsystrace.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2017-07-25 11:45:08 +00:00
|
|
|
|
2016-11-22 14:05:36 +00:00
|
|
|
#include <jschelpers/JavaScriptCore.h>
|
2016-11-01 18:38:43 +00:00
|
|
|
#include <jschelpers/JSCHelpers.h>
|
2016-11-18 14:25:24 +00:00
|
|
|
#include <jschelpers/Value.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
using std::min;
|
2016-11-18 14:25:24 +00:00
|
|
|
using namespace facebook::react;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-07-25 11:45:08 +00:00
|
|
|
static int64_t int64FromJSValue(JSContextRef ctx, JSValueRef value, JSValueRef* exception) {
|
|
|
|
return static_cast<int64_t>(JSC_JSValueToNumber(ctx, value, exception));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t copyTruncatedAsciiChars(
|
|
|
|
char* buf,
|
|
|
|
size_t bufLen,
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSValueRef value,
|
|
|
|
size_t maxLen) {
|
2016-11-22 14:05:38 +00:00
|
|
|
JSStringRef jsString = JSC_JSValueToStringCopy(ctx, value, NULL);
|
|
|
|
size_t stringLen = JSC_JSStringGetLength(ctx, jsString);
|
2016-05-04 02:29:58 +00:00
|
|
|
// Unlike the Java version, we truncate from the end of the string,
|
|
|
|
// rather than the beginning.
|
|
|
|
size_t toWrite = min(stringLen, min(bufLen, maxLen));
|
|
|
|
|
|
|
|
const char *startBuf = buf;
|
2016-11-22 14:05:38 +00:00
|
|
|
const JSChar* chars = JSC_JSStringGetCharactersPtr(ctx, jsString);
|
2016-05-04 02:29:58 +00:00
|
|
|
while (toWrite-- > 0) {
|
|
|
|
*(buf++) = (char)*(chars++);
|
|
|
|
}
|
|
|
|
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSStringRelease(ctx, jsString);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
// Return the number of bytes written
|
|
|
|
return buf - startBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t copyArgsToBuffer(
|
|
|
|
char* buf,
|
|
|
|
size_t bufLen,
|
|
|
|
size_t pos,
|
|
|
|
JSContextRef ctx,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[]) {
|
|
|
|
char separator = '|';
|
|
|
|
for (
|
|
|
|
size_t idx = 0;
|
|
|
|
idx + 1 < argumentCount; // Make sure key and value are present.
|
|
|
|
idx += 2) {
|
|
|
|
JSValueRef key = arguments[idx];
|
|
|
|
JSValueRef value = arguments[idx+1];
|
|
|
|
|
|
|
|
buf[pos++] = separator;
|
|
|
|
separator = ';';
|
|
|
|
if (FBSYSTRACE_UNLIKELY(pos >= bufLen)) { break; }
|
|
|
|
pos += copyTruncatedAsciiChars(
|
|
|
|
buf + pos, bufLen - pos, ctx, key, FBSYSTRACE_MAX_MESSAGE_LENGTH);
|
|
|
|
if (FBSYSTRACE_UNLIKELY(pos >= bufLen)) { break; }
|
|
|
|
buf[pos++] = '=';
|
|
|
|
if (FBSYSTRACE_UNLIKELY(pos >= bufLen)) { break; }
|
|
|
|
pos += copyTruncatedAsciiChars(
|
|
|
|
buf + pos, bufLen - pos, ctx, value, FBSYSTRACE_MAX_MESSAGE_LENGTH);
|
|
|
|
if (FBSYSTRACE_UNLIKELY(pos >= bufLen)) { break; }
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef nativeTraceBeginSection(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
|
|
|
if (FBSYSTRACE_UNLIKELY(argumentCount < 2)) {
|
|
|
|
if (exception) {
|
2016-11-18 14:25:24 +00:00
|
|
|
*exception = Value::makeError(
|
2016-05-04 02:29:58 +00:00
|
|
|
ctx,
|
|
|
|
"nativeTraceBeginSection: requires at least 2 arguments");
|
|
|
|
}
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 11:45:08 +00:00
|
|
|
uint64_t tag = int64FromJSValue(ctx, arguments[0], exception);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!fbsystrace_is_tracing(tag)) {
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char buf[FBSYSTRACE_MAX_MESSAGE_LENGTH];
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
pos += snprintf(buf + pos, sizeof(buf) - pos, "B|%d|", getpid());
|
|
|
|
// Skip the overflow check here because the int will be small.
|
|
|
|
pos += copyTruncatedAsciiChars(buf + pos, sizeof(buf) - pos, ctx, arguments[1], FBSYSTRACE_MAX_SECTION_NAME_LENGTH);
|
|
|
|
// Skip the overflow check here because the section name will be small-ish.
|
|
|
|
|
|
|
|
pos = copyArgsToBuffer(buf, sizeof(buf), pos, ctx, argumentCount - 2, arguments + 2);
|
|
|
|
if (FBSYSTRACE_UNLIKELY(pos >= sizeof(buf))) {
|
|
|
|
goto flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush:
|
|
|
|
fbsystrace_trace_raw(buf, min(pos, sizeof(buf)-1));
|
|
|
|
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef nativeTraceEndSection(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
|
|
|
if (FBSYSTRACE_UNLIKELY(argumentCount < 1)) {
|
|
|
|
if (exception) {
|
2016-11-18 14:25:24 +00:00
|
|
|
*exception = Value::makeError(
|
2016-05-04 02:29:58 +00:00
|
|
|
ctx,
|
|
|
|
"nativeTraceEndSection: requires at least 1 argument");
|
|
|
|
}
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 11:45:08 +00:00
|
|
|
uint64_t tag = int64FromJSValue(ctx, arguments[0], exception);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!fbsystrace_is_tracing(tag)) {
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FBSYSTRACE_LIKELY(argumentCount == 1)) {
|
|
|
|
fbsystrace_end_section(tag);
|
|
|
|
} else {
|
|
|
|
char buf[FBSYSTRACE_MAX_MESSAGE_LENGTH];
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
buf[pos++] = 'E';
|
|
|
|
buf[pos++] = '|';
|
|
|
|
buf[pos++] = '|';
|
|
|
|
pos = copyArgsToBuffer(buf, sizeof(buf), pos, ctx, argumentCount - 1, arguments + 1);
|
|
|
|
if (FBSYSTRACE_UNLIKELY(pos >= sizeof(buf))) {
|
|
|
|
goto flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush:
|
|
|
|
fbsystrace_trace_raw(buf, min(pos, sizeof(buf)-1));
|
|
|
|
}
|
|
|
|
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef beginOrEndAsync(
|
|
|
|
bool isEnd,
|
|
|
|
bool isFlow,
|
|
|
|
JSContextRef ctx,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
|
|
|
if (FBSYSTRACE_UNLIKELY(argumentCount < 3)) {
|
|
|
|
if (exception) {
|
2016-11-18 14:25:24 +00:00
|
|
|
*exception = Value::makeError(
|
2016-05-04 02:29:58 +00:00
|
|
|
ctx,
|
|
|
|
"beginOrEndAsync: requires at least 3 arguments");
|
|
|
|
}
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 11:45:08 +00:00
|
|
|
uint64_t tag = int64FromJSValue(ctx, arguments[0], exception);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!fbsystrace_is_tracing(tag)) {
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char buf[FBSYSTRACE_MAX_MESSAGE_LENGTH];
|
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
// This uses an if-then-else instruction in ARMv7, which should be cheaper
|
|
|
|
// than a full branch.
|
|
|
|
buf[pos++] = ((isFlow) ? (isEnd ? 'f' : 's') : (isEnd ? 'F' : 'S'));
|
|
|
|
pos += snprintf(buf + pos, sizeof(buf) - pos, "|%d|", getpid());
|
|
|
|
// Skip the overflow check here because the int will be small.
|
|
|
|
pos += copyTruncatedAsciiChars(buf + pos, sizeof(buf) - pos, ctx, arguments[1], FBSYSTRACE_MAX_SECTION_NAME_LENGTH);
|
|
|
|
// Skip the overflow check here because the section name will be small-ish.
|
|
|
|
|
|
|
|
// I tried some trickery to avoid a branch here, but gcc did not cooperate.
|
|
|
|
// We could consider changing the implementation to be lest branchy in the
|
|
|
|
// future.
|
|
|
|
// This is not required for flow use an or to avoid introducing another branch
|
|
|
|
if (!(isEnd | isFlow)) {
|
|
|
|
buf[pos++] = '<';
|
|
|
|
buf[pos++] = '0';
|
|
|
|
buf[pos++] = '>';
|
|
|
|
}
|
|
|
|
buf[pos++] = '|';
|
|
|
|
|
|
|
|
// Append the cookie. It should be an integer, but copyTruncatedAsciiChars
|
|
|
|
// will automatically convert it to a string. We might be able to get more
|
|
|
|
// performance by just getting the number and doing to string
|
|
|
|
// conversion ourselves. We truncate to FBSYSTRACE_MAX_SECTION_NAME_LENGTH
|
|
|
|
// just to make sure we can avoid the overflow check even if the caller
|
|
|
|
// passes in something bad.
|
|
|
|
pos += copyTruncatedAsciiChars(buf + pos, sizeof(buf) - pos, ctx, arguments[2], FBSYSTRACE_MAX_SECTION_NAME_LENGTH);
|
|
|
|
|
|
|
|
pos = copyArgsToBuffer(buf, sizeof(buf), pos, ctx, argumentCount - 3, arguments + 3);
|
|
|
|
if (FBSYSTRACE_UNLIKELY(pos >= sizeof(buf))) {
|
|
|
|
goto flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush:
|
|
|
|
fbsystrace_trace_raw(buf, min(pos, sizeof(buf)-1));
|
|
|
|
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef nativeTraceBeginAsyncSection(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
2017-07-25 11:45:08 +00:00
|
|
|
return beginOrEndAsync(false /* isEnd */, false /* isFlow */,
|
|
|
|
ctx, argumentCount, arguments, exception);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef nativeTraceEndAsyncSection(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
2017-07-25 11:45:08 +00:00
|
|
|
return beginOrEndAsync(true /* isEnd */, false /* isFlow */,
|
|
|
|
ctx, argumentCount, arguments, exception);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef nativeTraceBeginAsyncFlow(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
2017-07-25 11:45:08 +00:00
|
|
|
return beginOrEndAsync(false /* isEnd */, true /* isFlow */,
|
|
|
|
ctx, argumentCount, arguments, exception);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef nativeTraceEndAsyncFlow(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
2017-07-25 11:45:08 +00:00
|
|
|
return beginOrEndAsync(true /* isEnd */, true /* isFlow */,
|
|
|
|
ctx, argumentCount, arguments, exception);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef nativeTraceCounter(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef* exception) {
|
|
|
|
if (FBSYSTRACE_UNLIKELY(argumentCount < 3)) {
|
|
|
|
if (exception) {
|
2016-11-18 14:25:24 +00:00
|
|
|
*exception = Value::makeError(
|
2016-05-04 02:29:58 +00:00
|
|
|
ctx,
|
|
|
|
"nativeTraceCounter: requires at least 3 arguments");
|
|
|
|
}
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 11:45:08 +00:00
|
|
|
uint64_t tag = int64FromJSValue(ctx, arguments[0], exception);
|
2016-05-04 02:29:58 +00:00
|
|
|
if (!fbsystrace_is_tracing(tag)) {
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char buf[FBSYSTRACE_MAX_MESSAGE_LENGTH];
|
|
|
|
size_t len = copyTruncatedAsciiChars(buf, sizeof(buf), ctx,
|
|
|
|
arguments[1], FBSYSTRACE_MAX_SECTION_NAME_LENGTH);
|
|
|
|
buf[min(len,(FBSYSTRACE_MAX_MESSAGE_LENGTH-1))] = 0;
|
|
|
|
int64_t value = int64FromJSValue(ctx, arguments[2], exception);
|
|
|
|
|
|
|
|
fbsystrace_counter(tag, buf, value);
|
|
|
|
|
2016-11-18 14:25:24 +00:00
|
|
|
return Value::makeUndefined(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 11:45:08 +00:00
|
|
|
#endif
|
|
|
|
|
2016-05-14 00:15:03 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
void addNativeTracingHooks(JSGlobalContextRef ctx) {
|
2017-07-25 11:45:08 +00:00
|
|
|
#if USE_JSCTRACING
|
2016-05-04 02:29:58 +00:00
|
|
|
installGlobalFunction(ctx, "nativeTraceBeginSection", nativeTraceBeginSection);
|
|
|
|
installGlobalFunction(ctx, "nativeTraceEndSection", nativeTraceEndSection);
|
|
|
|
installGlobalFunction(ctx, "nativeTraceBeginAsyncSection", nativeTraceBeginAsyncSection);
|
|
|
|
installGlobalFunction(ctx, "nativeTraceEndAsyncSection", nativeTraceEndAsyncSection);
|
|
|
|
installGlobalFunction(ctx, "nativeTraceBeginAsyncFlow", nativeTraceBeginAsyncFlow);
|
|
|
|
installGlobalFunction(ctx, "nativeTraceEndAsyncFlow", nativeTraceEndAsyncFlow);
|
|
|
|
installGlobalFunction(ctx, "nativeTraceCounter", nativeTraceCounter);
|
2017-07-25 11:45:08 +00:00
|
|
|
#endif
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} }
|