Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
*[Android] Fix js process crash report error (#1438)
Browse files Browse the repository at this point in the history
  • Loading branch information
alwaysfight authored and YorkShen committed Aug 16, 2018
1 parent 87eeb6d commit 642dbf0
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 16 deletions.
Binary file added android/sdk/libs/armeabi-v7a/libJavaScriptCore.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi-v7a/libweexjsb.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi-v7a/libweexjss.so
Binary file not shown.
Binary file added android/sdk/libs/armeabi/libJavaScriptCore.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi/libweexjsb.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi/libweexjss.so
Binary file not shown.
14 changes: 14 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/WXEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,20 @@ public static String getFilesDir(Context context) {
return path;
}

public static String getCrashFilePath(Context context) {
if (context == null) {
return "";
}

File dir = context.getDir("crash", Context.MODE_PRIVATE);
if (dir == null)
return "";

String crashDir = dir.getAbsolutePath();

return crashDir;
}

public static String getGlobalFontFamilyName() {
return sGlobalFontFamily;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.taobao.weex.appfram.navigator;
import android.app.Activity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public int callRefreshFinish(String instanceId, byte[] tasks, String callback) {

@Override
public void reportServerCrash(String instanceId, String crashFile) {
WXLogUtils.e(TAG, "reportServerCrash instanceId:" + instanceId);
WXLogUtils.e(TAG, "reportServerCrash instanceId:" + instanceId + " crashFile: " + crashFile);
int errorCode = IWXBridge.INSTANCE_RENDERING;
try {
errorCode = WXBridgeManager.getInstance().callReportCrashReloadPage(instanceId, crashFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import android.util.Log;

import static com.taobao.weex.bridge.WXModuleManager.createDomModule;

Expand Down Expand Up @@ -678,8 +679,8 @@ public int callReportCrashReloadPage(String instanceId, String crashFile) {
}
try {
if (WXEnvironment.getApplication() != null) {
crashFile = WXEnvironment.getApplication().getApplicationContext().getCacheDir().getPath() + crashFile;
// Log.e("jsengine", "callReportCrashReloadPage crashFile:" + crashFile);
crashFile = mInitParams.getCrashFilePath() + crashFile;
Log.d("jsengine", "callReportCrashReloadPage crashFile:" + crashFile);
}
} catch (Throwable e) {
e.printStackTrace();
Expand Down Expand Up @@ -1153,6 +1154,7 @@ public void commitJscCrashAlarmMonitor(final String type, final WXErrorCode erro
return;
}

Log.d("ReportCrash", " commitJscCrashAlarmMonitor errMsg " + errMsg);
String method = "callReportCrash";
String exception = "weex core process crash and restart exception";
Map<String, String> extParams = new HashMap<String, String>();
Expand Down Expand Up @@ -1778,6 +1780,7 @@ private WXParams assembleDefaultOptions() {
wxParams.setShouldInfoCollect(config.get("infoCollect"));
wxParams.setLogLevel(config.get(WXConfig.logLevel));
wxParams.setUseSingleProcess(isUseSingleProcess ? "true" : "false");
wxParams.setCrashFilePath(WXEnvironment.getCrashFilePath(WXEnvironment.getApplication().getApplicationContext()));
wxParams.setLibJssPath(WXEnvironment.getLibJssRealPath());
String appName = config.get(WXConfig.appName);
if (!TextUtils.isEmpty(appName)) {
Expand Down
14 changes: 13 additions & 1 deletion android/sdk/src/main/java/com/taobao/weex/bridge/WXParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

import com.taobao.weex.utils.WXLogUtils;

import java.io.Serializable;
import java.util.Map;

public class WXParams {
public class WXParams implements Serializable {

private String platform;
private String osVersion;
Expand All @@ -37,6 +38,7 @@ public class WXParams {
private String needInitV8;
private String cacheDir;
private String useSingleProcess;
private String crashFilePath;
private String libJssPath;

private Map<String, String> options;
Expand Down Expand Up @@ -171,6 +173,16 @@ public void setNeedInitV8(boolean need) {
}
}

public void setCrashFilePath(String crashFilePath) {
WXLogUtils.e("WXParams","setCrashFilePath: " + crashFilePath);
this.crashFilePath = crashFilePath;
}

public String getCrashFilePath() {
WXLogUtils.e("WXParams", "getCrashFilePath:" + crashFilePath);
return this.crashFilePath;
}

public String getLibJssPath() {
WXLogUtils.e("getLibJssPath is running " + libJssPath);
return libJssPath;
Expand Down
93 changes: 82 additions & 11 deletions weex_core/Source/android/jsengine/multiprocess/WeexJSConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

#include "ashmem.h"
#include "WeexProxy.h"
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mman.h>
Expand All @@ -38,8 +40,11 @@
extern const char *s_cacheDir;
extern const char *g_jssSoPath;
extern const char *g_jssSoName;
extern const char *g_crashFilePath;
extern bool s_start_pie;
static bool s_in_find_icu = false;
static std::string g_crashFileName;

static void doExec(int fd, bool traceEnable, bool startupPie = true);

static int copyFile(const char *SourceFile, const char *NewFile);
Expand All @@ -48,6 +53,54 @@ static void closeAllButThis(int fd);

static void printLogOnFile(const char *log);

static bool checkOrCreateCrashFile(const char* file) {
if (file == nullptr) {
LOGE("checkOrCreateCrashFile Pass error file name!");
return false;
}

int flags = 0;
int mode = 0666;
int ret = ::access(file,F_OK);
if (ret < 0)
flags |= O_CREAT;
flags |= O_RDWR;
int fd = ::open(file, flags, mode);
if (fd < 0) {
LOGE(" checkOrCreateCrashFile failed, can not create or use crash file! ");
return false;
}
return true;
}

static bool checkDirOrFileIsLink(const char* path) {
if (path == nullptr)
return false;
struct stat fileStat;
int st = stat(path, &fileStat);
if (st < 0) {
LOGE(" checkDirOrFileIsLink file error: %d\n", errno);
return false;
}
if (!S_ISLNK(fileStat.st_mode))
return false;
return true;
}

static bool getDirOrFileLink(const char* path, char* buf, size_t length) {
if(path == nullptr || buf == nullptr) {
return false;
}

int ret = readlink(path, buf, length);
if (ret < 0 ) {
return false;
LOGE(" checkDirOrFileIsLink check link error: %d\n", errno);
}

return true;
}

#if PRINT_LOG_CACHEFILE
static std::string logFilePath = "/data/data/com.taobao.taobao/cache";
#endif
Expand All @@ -60,8 +113,25 @@ struct WeexJSConnection::WeexJSConnectionImpl {

WeexJSConnection::WeexJSConnection()
: m_impl(new WeexJSConnectionImpl) {
if (checkDirOrFileIsLink(g_crashFilePath)) {
std::string tmp = g_crashFilePath;
size_t length = tmp.length();
char buf[length];
memset(buf, 0, length);
if (!getDirOrFileLink(g_crashFilePath, buf, length)) {
LOGE("getDirOrFileLink filePath(%s) error\n", g_crashFilePath);
g_crashFileName = g_crashFilePath;
} else {
g_crashFileName = buf;
}
} else {
g_crashFileName = g_crashFilePath;
}
g_crashFileName += "/crash_dump.log";
LOGE("WeexJSConnection g_crashFileName: %s\n", g_crashFileName.c_str());
}


WeexJSConnection::~WeexJSConnection() {
end();
}
Expand All @@ -83,6 +153,12 @@ IPCSender *WeexJSConnection::start(IPCHandler *handler, bool reinit) {
std::unique_ptr<IPCSender> sender(createIPCSender(futexPageQueue.get(), handler));
m_impl->serverSender = std::move(sender);
m_impl->futexPageQueue = std::move(futexPageQueue);

//before process boot up, we prapare a crash file for child process
bool success = checkOrCreateCrashFile(g_crashFileName.c_str());
if (!success) {
LOGE("Create crash for child process failed, if child process crashed, we can not get a crash file now");
}
#if PRINT_LOG_CACHEFILE
if (s_cacheDir) {
logFilePath = s_cacheDir;
Expand Down Expand Up @@ -121,6 +197,7 @@ IPCSender *WeexJSConnection::start(IPCHandler *handler, bool reinit) {
close(fd);
throw IPCException("failed to fork: %s", strerror(myerrno));
} else if (child == 0) {
LOGE("weexcore fork child success\n");
// the child
closeAllButThis(fd);
// implements close all but handles[1]
Expand Down Expand Up @@ -237,6 +314,7 @@ std::unique_ptr<const char *[]> EnvPBuilder::build() {
}

void doExec(int fd, bool traceEnable, bool startupPie) {
LOGE("weexcore doExec start");
std::string executablePath;
std::string icuDataPath;
s_in_find_icu = true;
Expand Down Expand Up @@ -282,30 +360,22 @@ void doExec(int fd, bool traceEnable, bool startupPie) {
}
std::string ldLibraryPathEnv("LD_LIBRARY_PATH=");
std::string icuDataPathEnv("ICU_DATA_PATH=");
std::string crashFilePathEnv("CRASH_FILE_PATH=");
ldLibraryPathEnv.append(executablePath);
icuDataPathEnv.append(icuDataPath);
#if PRINT_LOG_CACHEFILE
mcfile << "jsengine ldLibraryPathEnv:" << ldLibraryPathEnv << " icuDataPathEnv:" << icuDataPathEnv
<< std::endl;
#endif
if (!s_cacheDir) {
crashFilePathEnv.append("/data/data/com.taobao.taobao/cache");
} else {
crashFilePathEnv.append(s_cacheDir);
}
crashFilePathEnv.append("/jsserver_crash");
char fdStr[16];
snprintf(fdStr, 16, "%d", fd);
EnvPBuilder envpBuilder;
envpBuilder.addNew(ldLibraryPathEnv.c_str());
envpBuilder.addNew(icuDataPathEnv.c_str());
envpBuilder.addNew(crashFilePathEnv.c_str());
auto envp = envpBuilder.build();
{
std::string executableName = executablePath + '/' + "libweexjsb64.so";
chmod(executableName.c_str(), 0755);
const char *argv[] = {executableName.c_str(), fdStr, traceEnable ? "1" : "0", nullptr};
const char *argv[] = {executableName.c_str(), fdStr, traceEnable ? "1" : "0", g_crashFileName.c_str(), nullptr};
if (-1 == execve(argv[0], const_cast<char *const *>(&argv[0]),
const_cast<char *const *>(envp.get()))) {
}
Expand Down Expand Up @@ -343,7 +413,7 @@ void doExec(int fd, bool traceEnable, bool startupPie) {
mcfile << "jsengine WeexJSConnection::doExec start path on sdcard, start execve so name:"
<< executableName << std::endl;
#endif
const char *argv[] = {executableName.c_str(), fdStr, traceEnable ? "1" : "0", nullptr};
const char *argv[] = {executableName.c_str(), fdStr, traceEnable ? "1" : "0", g_crashFileName.c_str(), nullptr};
if (-1 == execve(argv[0], const_cast<char *const *>(&argv[0]),
const_cast<char *const *>(envp.get()))) {
#if PRINT_LOG_CACHEFILE
Expand All @@ -357,7 +427,8 @@ void doExec(int fd, bool traceEnable, bool startupPie) {
mcfile << "jsengine WeexJSConnection::doExec start execve so name:" << executableName
<< std::endl;
#endif
const char *argv[] = {executableName.c_str(), fdStr, traceEnable ? "1" : "0", nullptr};
const char *argv[] = {executableName.c_str(), fdStr, traceEnable ? "1" : "0", g_crashFileName.c_str()
, nullptr};
if (-1 == execve(argv[0], const_cast<char *const *>(&argv[0]),
const_cast<char *const *>(envp.get()))) {
#if PRINT_LOG_CACHEFILE
Expand Down
14 changes: 13 additions & 1 deletion weex_core/Source/android/jsengine/multiprocess/WeexProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

const char *s_cacheDir;
const char *g_jssSoPath = nullptr;
const char *g_crashFilePath = nullptr;
const char *g_jssSoName = "libweexjss.so";
bool s_start_pie = true;

Expand Down Expand Up @@ -715,6 +716,17 @@ namespace WeexCore {
}
}

jmethodID m_get_crash_file_path = env->GetMethodID(c_params, "getCrashFilePath", "()Ljava/lang/String;");
if (m_get_jss_so_path != nullptr) {
jobject j_get_crash_file_path = env->CallObjectMethod(params, m_get_crash_file_path);
if (j_get_crash_file_path != nullptr) {
g_crashFilePath = env->GetStringUTFChars(
(jstring) (j_get_crash_file_path),
nullptr);
LOGE("g_crashFilePath is %s ", g_crashFilePath);
env->DeleteLocalRef(j_get_crash_file_path);
}
}

jmethodID m_osVersion = env->GetMethodID(c_params, "getOsVersion", "()Ljava/lang/String;");
if (m_osVersion == nullptr) {
Expand Down Expand Up @@ -932,7 +944,7 @@ namespace WeexCore {
if (!reportMethodId)
goto no_method;

crashFileStr.assign("/jsserver_crash/jsserver_crash_info.log");
crashFileStr.assign("/crash_dump.log");

crashFile = env->NewStringUTF(crashFileStr.c_str());
env->CallVoidMethod(jThis, reportMethodId, jinstanceid, crashFile);
Expand Down

0 comments on commit 642dbf0

Please sign in to comment.