Skip to content
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

Support detail runtime info dump #318

Merged
merged 4 commits into from
Aug 29, 2017
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ interface IPluginClient {
* @param Intent 广播的 Intent 数据
*/
void onReceive(String plugin, String receiver, in Intent intent);
}

/**
* dump通过插件化框架启动起来的Service信息
*/
String dumpServices();

/**
* dump插件化框架中存储的详细Activity坑位映射表
*/
String dumpActivities();
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ interface IPluginHost {
* 通过PID来获取进程名
*/
String getProcessNameByPid(int pid);
}

/**
* dump详细的运行时信息
*/
String dump();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ interface IPluginServiceServer {

int bindService(in Intent intent, in IServiceConnection conn, int flags, in Messenger client);
boolean unbindService(in IServiceConnection conn);
}

String dump();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2005-2017 Qihoo 360 Inc.
*
* Licensed 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.qihoo360.loader2;

import android.os.IBinder;
import android.util.Log;

import com.qihoo360.replugin.RePluginInternal;

import java.io.FileDescriptor;
import java.io.PrintWriter;

/**
* 运行时 dump 工具类
*
* @author RePlugin Team
*/
public class DumpUtils {

private static final String TAG = RePluginInternal.FOR_DEV ? DumpUtils.class.getSimpleName() : "DumpUtils";

/**
* dump RePlugin框架运行时的详细信息,包括:Activity 坑位映射表,正在运行的 Service,以及详细的插件信息
*
* @param fd
* @param writer
* @param args
*/
public static void dump(FileDescriptor fd, PrintWriter writer, String[] args) {

IBinder binder = PluginProviderStub.proxyFetchHostBinder(RePluginInternal.getAppContext());

if (binder == null) {
return;
}

IPluginHost pluginHost = IPluginHost.Stub.asInterface(binder);

try {
String dumpInfo = pluginHost.dump();

if (RePluginInternal.FOR_DEV) {
Log.d(TAG, "dumpInfo:" + dumpInfo);
}

if (writer != null) {
writer.println(dumpInfo);
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
import com.qihoo360.replugin.base.IPC;
import com.qihoo360.replugin.component.process.PluginProcessHost;
import com.qihoo360.replugin.helper.HostConfigHelper;
import com.qihoo360.replugin.helper.JSONHelper;
import com.qihoo360.replugin.helper.LogDebug;
import com.qihoo360.replugin.helper.LogRelease;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -41,6 +45,7 @@
import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_INSTANCE;
import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TASK;
import static android.content.pm.ActivityInfo.LAUNCH_SINGLE_TOP;
import static com.qihoo360.loader2.PluginContainers.ActivityState.toName;
import static com.qihoo360.replugin.helper.LogDebug.LOG;
import static com.qihoo360.replugin.helper.LogDebug.PLUGIN_TAG;
import static com.qihoo360.replugin.helper.LogRelease.LOGR;
Expand Down Expand Up @@ -771,4 +776,28 @@ final ActivityState lookupByContainer(String container) {

return null;
}
}

final String dump() {

JSONArray activityArr = new JSONArray();
JSONObject activityObj;

for (Map.Entry<String, ActivityState> entry : mStates.entrySet()) {
String container = entry.getKey();
ActivityState state = entry.getValue();

if (!TextUtils.isEmpty(state.plugin) && !TextUtils.isEmpty(state.activity)) {
activityObj = new JSONObject();
JSONHelper.putNoThrows(activityObj, "process", IPC.getCurrentProcessName());
JSONHelper.putNoThrows(activityObj, "className", container);
JSONHelper.putNoThrows(activityObj, "plugin", state.plugin);
JSONHelper.putNoThrows(activityObj, "realClassName", state.activity);
JSONHelper.putNoThrows(activityObj, "state", toName(state.state));
JSONHelper.putNoThrows(activityObj, "refs", state.refs != null ? state.refs.size() : 0);
activityArr.put(activityObj);
}
}

return activityArr.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
import com.qihoo360.replugin.packages.PluginManagerProxy;
import com.qihoo360.replugin.packages.PluginManagerServer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.HashMap;
Expand Down Expand Up @@ -229,6 +233,10 @@ public String toString() {
}
return super.toString();
}

public IPluginClient getClient() {
return client;
}
}

static final void reportStatus() {
Expand All @@ -242,6 +250,69 @@ static final void reportStatus() {
}
}

static final String dump() {

// 1.dump Activity映射表, service列表
JSONArray activityArr = new JSONArray();
JSONArray serviceArr = new JSONArray();

for (ProcessClientRecord clientRecord : ALL.values()) {
try {
IPluginClient pluginClient = clientRecord.getClient();
if (pluginClient == null) {
continue;
}

String activityDumpInfo = pluginClient.dumpActivities();
JSONArray activityList = new JSONArray(activityDumpInfo);
int activityCount = activityList.length();
if (activityCount > 0) {
for (int i = 0; i < activityCount; i++) {
activityArr.put(activityList.getJSONObject(i));
}
}

String serviceDumpInfo = pluginClient.dumpServices();
JSONArray serviceList = new JSONArray(serviceDumpInfo);
int serviceCount = serviceList.length();
if (serviceCount > 0) {
for (int i = 0; i < serviceCount; i++) {
serviceArr.put(serviceList.getJSONObject(i));
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}

// 2.dump 插件信息表
JSONArray pluginArr = new JSONArray();
List<PluginInfo> pluginList = MP.getPlugins(false);
if (pluginList != null) {
JSONObject pluginObj;
for (PluginInfo pluginInfo : pluginList) {
try {
pluginObj = new JSONObject();
pluginObj.put(pluginInfo.getName(), pluginInfo.toString());
pluginArr.put(pluginObj);
} catch (JSONException e) {
e.printStackTrace();
}
}
}

JSONObject detailObj = new JSONObject();
try {
detailObj.put("activity", activityArr);
detailObj.put("service", serviceArr);
detailObj.put("plugin", pluginArr);
} catch (JSONException e) {
e.printStackTrace();
}

return detailObj.toString();
}

static final void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
if (LogDebug.DUMP_ENABLED) {
writer.println("--- ALL.length = " + ALL.size() + " ---");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,29 @@ final String bindActivity(String plugin, int process, String activity, Intent in
public void onReceive(String plugin, final String receiver, final Intent intent) {
PluginReceiverHelper.onPluginReceiverReceived(plugin, receiver, mReceivers, intent);
}
}

@Override
public String dumpServices() {
try {
IPluginServiceServer pss = fetchServiceServer();
if (pss != null) {
try {
return pss.dump();
} catch (Throwable e) {
if (LOGR) {
LogRelease.e(PLUGIN_TAG, "psc.sts: pss e", e);
}
}
}
} catch (RemoteException e) {
e.printStackTrace();
}

return null;
}

@Override
public String dumpActivities() {
return mACM.dump();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,9 @@ public int getPidByProcessName(String processName) throws RemoteException {
public String getProcessNameByPid(int pid) throws RemoteException {
return PluginProcessMain.getProcessNameByPid(pid);
}
}

@Override
public String dump() {
return PluginProcessMain.dump();
}
}
Loading