Skip to content

Debugger unixsockets #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions src/jni/JsDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "V8GlobalHelpers.h"
#include "JniLocalRef.h"
#include <assert.h>
#include <android/log.h>

using namespace std;
using namespace tns;
Expand All @@ -10,26 +11,33 @@ JsDebugger::JsDebugger()
{
}

void JsDebugger::Init(v8::Isolate *isolate, const string& packageName)
void JsDebugger::Init(v8::Isolate *isolate, const string& packageName, jobject jsDebugger)
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::Init called");

s_isolate = isolate;
s_packageName = packageName;

JEnv env;
s_jsDebugger = env.NewGlobalRef(jsDebugger);

s_JsDebuggerClass = env.FindClass("com/tns/JsDebugger");
assert(s_JsDebuggerClass != nullptr);

//what is enqueue message used for
s_EnqueueMessage = env.GetStaticMethodID(s_JsDebuggerClass, "enqueueMessage", "(Ljava/lang/String;)V");
s_EnqueueMessage = env.GetMethodID(s_JsDebuggerClass, "enqueueMessage", "(Ljava/lang/String;)V");
assert(s_EnqueueMessage != nullptr);

//enableAgent.start(.stop) what is it used for ?
s_EnableAgent = env.GetStaticMethodID(s_JsDebuggerClass, "enableAgent", "(Ljava/lang/String;IZ)V");
assert(s_EnqueueMessage != nullptr);
s_EnableAgent = env.GetMethodID(s_JsDebuggerClass, "enableAgent", "()V");
assert(s_EnableAgent != nullptr);


}

string JsDebugger::GetPackageName()
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::GetPackageName");
return s_packageName;
}

Expand All @@ -39,20 +47,29 @@ string JsDebugger::GetPackageName()
*/
void JsDebugger::MyMessageHandler(const v8::Debug::Message& message)
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::MyMessageHandler");

if (s_jsDebugger == nullptr)
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger s_jsDebugger is null...returning....");
return;
}

auto json = message.GetJSON();
auto str = ConvertToString(json);

JEnv env;
JniLocalRef s(env.NewStringUTF(str.c_str()));

env.CallStaticVoidMethod(s_JsDebuggerClass, s_EnqueueMessage, (jstring)s);
env.CallVoidMethod(s_jsDebugger, s_EnqueueMessage, (jstring)s);
}

/* *
* sets who will handle the messages when they start comming from v8
*/
void JsDebugger::Enable()
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::Enable called");
auto isolate = s_isolate;
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handleScope(isolate);
Expand All @@ -65,6 +82,7 @@ void JsDebugger::Enable()
*/
void JsDebugger::Disable()
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::Disable called");
auto isolate = s_isolate;
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handleScope(isolate);
Expand All @@ -78,6 +96,7 @@ void JsDebugger::Disable()
*/
void JsDebugger::DebugBreak()
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::DebugBreak called");
auto isolate = s_isolate;
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handleScope(isolate);
Expand All @@ -87,6 +106,7 @@ void JsDebugger::DebugBreak()

void JsDebugger::ProcessDebugMessages()
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::ProcessDebugMessages called");
auto isolate = s_isolate;
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handleScope(isolate);
Expand All @@ -96,30 +116,32 @@ void JsDebugger::ProcessDebugMessages()

void JsDebugger::SendCommand(uint16_t *cmd, int length)
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::SendCommand called");
auto isolate = s_isolate;

v8::Debug::SendCommand(isolate, cmd, length, nullptr);
}

void JsDebugger::DebugBreakCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
{
JEnv env;
JniLocalRef packageName(env.NewStringUTF(s_packageName.c_str()));

jint port = 8181;
if ((args.Length() > 0) && args[0]->IsInt32())
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger::DebugBreakCallback called");

if (s_jsDebugger == nullptr)
{
port = args[0]->Int32Value();
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "JsDebugger s_jsDebugger is null...returning");
return;
}
jboolean jniFalse = JNI_FALSE;

env.CallStaticVoidMethod(s_JsDebuggerClass, s_EnableAgent, (jstring)packageName, port, jniFalse);
JEnv env;
env.CallVoidMethod(s_jsDebugger, s_EnableAgent);

DebugBreak();
}


v8::Isolate* JsDebugger::s_isolate = nullptr;
jobject JsDebugger::s_jsDebugger = nullptr;
string JsDebugger::s_packageName = "";
jclass JsDebugger::s_JsDebuggerClass = nullptr;
jmethodID JsDebugger::s_EnqueueMessage = nullptr;
Expand Down
3 changes: 2 additions & 1 deletion src/jni/JsDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace tns
class JsDebugger
{
public:
static void Init(v8::Isolate *isolate, const std::string& packageName);
static void Init(v8::Isolate *isolate, const std::string& packageName, jobject jsDebugger);

static void ProcessDebugMessages();

Expand All @@ -34,6 +34,7 @@ namespace tns

static std::string s_packageName;
static jclass s_JsDebuggerClass;
static jobject s_jsDebugger;
static jmethodID s_EnqueueMessage;
static jmethodID s_EnableAgent;
static v8::Isolate *s_isolate;
Expand Down
5 changes: 5 additions & 0 deletions src/jni/com_tns_JsDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "JsDebugger.h"
#include "ArgConverter.h"
#include <string>
#include <android/log.h>

extern "C" void Java_com_tns_JsDebugger_processDebugMessages(JNIEnv *env, jobject obj)
{
Expand All @@ -10,22 +11,26 @@ extern "C" void Java_com_tns_JsDebugger_processDebugMessages(JNIEnv *env, jobjec

extern "C" void Java_com_tns_JsDebugger_enable(JNIEnv *env, jobject obj)
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "Java_com_tns_JsDebugger_enable called");
tns::JsDebugger::Enable();
}

extern "C" void Java_com_tns_JsDebugger_disable(JNIEnv *env, jobject obj)
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "Java_com_tns_JsDebugger_disable called");
tns::JsDebugger::Disable();
}

extern "C" void Java_com_tns_JsDebugger_debugBreak(JNIEnv *env, jobject obj)
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "Java_com_tns_JsDebugger_debugBreak called");
tns::JsDebugger::DebugBreak();
}


extern "C" void Java_com_tns_JsDebugger_sendCommand(JNIEnv *_env, jobject obj, jbyteArray command, jint length)
{
//__android_log_print(ANDROID_LOG_INFO, "TNS.JsDebugger", "Java_com_tns_JsDebugger_sendCommand called");
tns::JEnv env(_env);
auto buf = new jbyte[length];

Expand Down
8 changes: 4 additions & 4 deletions src/jni/com_tns_Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void PrepareExtendFunction(Isolate *isolate, jstring filesPath)
DEBUG_WRITE("Executed prepareExtend.js script");
}

void PrepareV8Runtime(Isolate *isolate, JEnv& env, jstring filesPath, jstring packageName, jstring jsoptions)
void PrepareV8Runtime(Isolate *isolate, JEnv& env, jstring filesPath, jstring packageName, jstring jsoptions, jobject jsDebugger)
{
string v8flags = ArgConverter::jstringToString(jsoptions);
V8::SetFlagsFromString(v8flags.c_str(), v8flags.size());
Expand Down Expand Up @@ -154,7 +154,7 @@ void PrepareV8Runtime(Isolate *isolate, JEnv& env, jstring filesPath, jstring pa

string pckName = ArgConverter::jstringToString(packageName);
Profiler::Init(pckName);
JsDebugger::Init(isolate, pckName);
JsDebugger::Init(isolate, pckName, jsDebugger);

//PrepareExtendFunction(isolate, filesPath);

Expand All @@ -163,7 +163,7 @@ void PrepareV8Runtime(Isolate *isolate, JEnv& env, jstring filesPath, jstring pa
NativeScriptRuntime::CreateTopLevelNamespaces(global);
}

extern "C" void Java_com_tns_Platform_initNativeScript(JNIEnv *_env, jobject obj, jstring filesPath, jint appJavaObjectId, jboolean verboseLoggingEnabled, jstring packageName, jstring jsoptions)
extern "C" void Java_com_tns_Platform_initNativeScript(JNIEnv *_env, jobject obj, jstring filesPath, jint appJavaObjectId, jboolean verboseLoggingEnabled, jstring packageName, jstring jsoptions, jobject jsDebugger)
{
AppJavaObjectID = appJavaObjectId;
tns::LogEnabled = verboseLoggingEnabled;
Expand All @@ -184,7 +184,7 @@ extern "C" void Java_com_tns_Platform_initNativeScript(JNIEnv *_env, jobject obj
ExceptionUtil::GetInstance()->Init(g_jvm, g_objectManager);

JEnv env(_env);
PrepareV8Runtime(isolate, env, filesPath, packageName, jsoptions);
PrepareV8Runtime(isolate, env, filesPath, packageName, jsoptions, jsDebugger);

NativeScriptRuntime::APP_FILES_DIR = ArgConverter::jstringToString(filesPath);
Constants::APP_ROOT_FOLDER_PATH = NativeScriptRuntime::APP_FILES_DIR + "/app/";
Expand Down
Loading