当前位置: 首页 > article >正文

Android framework服务命令行工具框架 - Android13

Android framework服务命令行工具框架 - Android13

  • 1、framework服务命令行工具简介
  • 2、cmd 执行程序
    • 2.1 目录和Android.bp
    • 2.2 cmdMain 执行入口
    • 2.3 cmd命令
  • 3、am命令工具,实质脚本执行cmd activity
    • 3.1 sh脚本
    • 3.2 activity服务注册
    • 3.3 onShellCommand执行
  • 4、简易时序图

1、framework服务命令行工具简介

这里强调 “framework服务” ,主要就是bin命令模拟 framework服务 相关的查询和功能,如am\pm\input等;其实质就是 Android 提供了大多数常见的 Unix 命令行工具,说白了就是bin执行程序 。而 framework服务 命令行工具现在一般就是cmdbin执行程序Binder获取对应服务,通过IBinder::shellCommand调用对应服务的onShellCommand

在这里插入图片描述 在这里插入图片描述

2、cmd 执行程序

2.1 目录和Android.bp

frameworks/native/cmds/cmd/Android.bp
frameworks/native/cmds/cmd/cmd.cpp
在这里插入图片描述

2.2 cmdMain 执行入口

  • DEBUG:默认关闭#define DEBUG 0
  • serviceName:SM中注册的binder服务对应的名称,如ACTIVITY_SERVICE = "activity"(ActivityManagerService)
  • IBinder::shellCommand(service, in, out, err, args, cb, result):执行到对应服务onShellCommand

frameworks/native/cmds/cmd/cmd.cpp

int cmdMain(const std::vector<std::string_view>& argv, TextOutput& outputLog, TextOutput& errorLog,
            int in, int out, int err, RunMode runMode) {
    sp<ProcessState> proc = ProcessState::self();
    proc->startThreadPool();

#if DEBUG
    ALOGD("cmd: starting");
#endif
    sp<IServiceManager> sm = defaultServiceManager();
    if (runMode == RunMode::kStandalone) {
        fflush(stdout);
    }
    if (sm == nullptr) {
        ALOGW("Unable to get default service manager!");
        errorLog << "cmd: Unable to get default service manager!" << endl;
        return 20;
    }

    int argc = argv.size();

    if (argc == 0) {
        errorLog << "cmd: No service specified; use -l to list all running services. Use -w to start and wait for a service." << endl;
        return 20;
    }

    if ((argc == 1) && (argv[0] == "-l")) {
        Vector<String16> services = sm->listServices();
        services.sort(sort_func);
        outputLog << "Currently running services:" << endl;

        for (size_t i=0; i<services.size(); i++) {
            sp<IBinder> service = sm->checkService(services[i]);
            if (service != nullptr) {
                outputLog << "  " << services[i] << endl;
            }
        }
        return 0;
    }

    bool waitForService = ((argc > 1) && (argv[0] == "-w"));
    int serviceIdx = (waitForService) ? 1 : 0;
    const auto cmd = argv[serviceIdx];

    Vector<String16> args;
    String16 serviceName = String16(cmd.data(), cmd.size());
    for (int i = serviceIdx + 1; i < argc; i++) {
        args.add(String16(argv[i].data(), argv[i].size()));
    }
    sp<IBinder> service;
    if(waitForService) {
        service = sm->waitForService(serviceName);
    } else {
        service = sm->checkService(serviceName);
    }

    if (service == nullptr) {
        if (runMode == RunMode::kStandalone) {
            ALOGW("Can't find service %.*s", static_cast<int>(cmd.size()), cmd.data());
        }
        errorLog << "cmd: Can't find service: " << cmd << endl;
        return 20;
    }

    sp<MyShellCallback> cb = new MyShellCallback(errorLog);
    sp<MyResultReceiver> result = new MyResultReceiver();

#if DEBUG
    ALOGD("cmd: Invoking %.*s in=%d, out=%d, err=%d",
          static_cast<int>(cmd.size()), cmd.data(), in, out, err);
#endif

    // TODO: block until a result is returned to MyResultReceiver.
    status_t error = IBinder::shellCommand(service, in, out, err, args, cb, result);
    if (error < 0) {
        const char* errstr;
        switch (error) {
            case BAD_TYPE: errstr = "Bad type"; break;
            case FAILED_TRANSACTION: errstr = "Failed transaction"; break;
            case FDS_NOT_ALLOWED: errstr = "File descriptors not allowed"; break;
            case UNEXPECTED_NULL: errstr = "Unexpected null"; break;
            default: errstr = strerror(-error); break;
        }
        if (runMode == RunMode::kStandalone) {
            ALOGW("Failure calling service %.*s: %s (%d)", static_cast<int>(cmd.size()), cmd.data(),
                  errstr, -error);
        }
        outputLog << "cmd: Failure calling service " << cmd << ": " << errstr << " (" << (-error)
                  << ")" << endl;
        return error;
    }

    cb->mActive = false;
    status_t res = result->waitForResult();
#if DEBUG
    ALOGD("result=%d", (int)res);
#endif
    return res;
}

2.3 cmd命令

  1. cmd
    在这里插入图片描述

  2. cmd -l 列出SM中注册的服务
    在这里插入图片描述

  3. cmd activity
    在这里插入图片描述

3、am命令工具,实质脚本执行cmd activity

3.1 sh脚本

这里am工具为例。Android 提供了大多数常见的 Unix 命令行工具,查看可用工具的列表:adb shell ls /system/bin

frameworks/base/cmds/am/am

#!/system/bin/sh

if [ "$1" != "instrument" ] ; then
    cmd activity "$@"
else
    base=/system
    export CLASSPATH=$base/framework/am.jar
    exec app_process $base/bin com.android.commands.am.Am "$@"
fi

3.2 activity服务注册

activity服务就是ActivityManagerService注册的Context.ACTIVITY_SERVICE

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

public void setSystemProcess() {
    try {
        ServiceManager.addService(Context.ACTIVITY_SERVICE, this, /* allowIsolated= */ true,
                DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL | DUMP_FLAG_PROTO);
        ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
        ServiceManager.addService("meminfo", new MemBinder(this), /* allowIsolated= */ false,
                DUMP_FLAG_PRIORITY_HIGH);
        ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
        ServiceManager.addService("dbinfo", new DbBinder(this));
        mAppProfiler.setCpuInfoService();
        ServiceManager.addService("permission", new PermissionController(this));
        ServiceManager.addService("processinfo", new ProcessInfoService(this));
        ServiceManager.addService("cacheinfo", new CacheBinder(this));
   //... ... ... ...
}

3.3 onShellCommand执行

ActivityManagerShellCommand专门处理am相关命令,这里具体功能不展开细说。

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

@Override
public void onShellCommand(FileDescriptor in, FileDescriptor out,
        FileDescriptor err, String[] args, ShellCallback callback,
        ResultReceiver resultReceiver) {
    (new ActivityManagerShellCommand(this, false)).exec(
            this, in, out, err, args, callback, resultReceiver);
}

frameworks/base/services/core/java/com/android/server/am/ActivityManagerShellCommand.java

@Override
public int onCommand(String cmd) {
    if (cmd == null) {
        return handleDefaultCommands(cmd);
    }
    final PrintWriter pw = getOutPrintWriter();
    try {
        switch (cmd) {
            case "start":
            case "start-activity":
                return runStartActivity(pw);
            case "startservice":
            case "start-service":
                return runStartService(pw, false);
            case "startforegroundservice":
            case "startfgservice":
            case "start-foreground-service":
            case "start-fg-service":
                return runStartService(pw, true);
            case "stopservice":
            case "stop-service":
                return runStopService(pw);
            case "broadcast":
                return runSendBroadcast(pw);
            case "compact":
                return runCompact(pw);
            case "instrument":
                getOutPrintWriter().println("Error: must be invoked through 'am instrument'.");
                return -1;
            case "trace-ipc":
                return runTraceIpc(pw);
            case "profile":
                return runProfile(pw);
            case "dumpheap":
                return runDumpHeap(pw);
            case "set-debug-app":
                return runSetDebugApp(pw);
            case "set-agent-app":
                return runSetAgentApp(pw);
            case "clear-debug-app":
                return runClearDebugApp(pw);
            case "set-watch-heap":
                return runSetWatchHeap(pw);
            case "clear-watch-heap":
                return runClearWatchHeap(pw);
            case "clear-exit-info":
                return runClearExitInfo(pw);
            case "bug-report":
                return runBugReport(pw);
            case "force-stop":
                return runForceStop(pw);
            case "stop-app":
                return runStopApp(pw);
            case "fgs-notification-rate-limit":
                return runFgsNotificationRateLimit(pw);
            case "crash":
                return runCrash(pw);
            case "kill":
                return runKill(pw);
            case "kill-all":
                return runKillAll(pw);
            case "make-uid-idle":
                return runMakeIdle(pw);
            case "monitor":
                return runMonitor(pw);
            case "watch-uids":
                return runWatchUids(pw);
            case "hang":
                return runHang(pw);
            case "restart":
                return runRestart(pw);
            case "idle-maintenance":
                return runIdleMaintenance(pw);
            case "screen-compat":
                return runScreenCompat(pw);
            case "package-importance":
                return runPackageImportance(pw);
            case "to-uri":
                return runToUri(pw, 0);
            case "to-intent-uri":
                return runToUri(pw, Intent.URI_INTENT_SCHEME);
            case "to-app-uri":
                return runToUri(pw, Intent.URI_ANDROID_APP_SCHEME);
            case "switch-user":
                return runSwitchUser(pw);
            case "get-current-user":
                return runGetCurrentUser(pw);
            case "start-user":
                return runStartUser(pw);
            case "unlock-user":
                return runUnlockUser(pw);
            case "stop-user":
                return runStopUser(pw);
            case "is-user-stopped":
                return runIsUserStopped(pw);
            case "get-started-user-state":
                return runGetStartedUserState(pw);
            case "track-associations":
                return runTrackAssociations(pw);
            case "untrack-associations":
                return runUntrackAssociations(pw);
            case "get-uid-state":
                return getUidState(pw);
            case "get-config":
                return runGetConfig(pw);
            case "suppress-resize-config-changes":
                return runSuppressResizeConfigChanges(pw);
            case "set-inactive":
                return runSetInactive(pw);
            case "get-inactive":
                return runGetInactive(pw);
            case "set-standby-bucket":
                return runSetStandbyBucket(pw);
            case "get-standby-bucket":
                return runGetStandbyBucket(pw);
            case "send-trim-memory":
                return runSendTrimMemory(pw);
            case "display":
                return runDisplay(pw);
            case "stack":
                return runStack(pw);
            case "task":
                return runTask(pw);
            case "write":
                return runWrite(pw);
            case "attach-agent":
                return runAttachAgent(pw);
            case "supports-multiwindow":
                return runSupportsMultiwindow(pw);
            case "supports-split-screen-multi-window":
                return runSupportsSplitScreenMultiwindow(pw);
            case "update-appinfo":
                return runUpdateApplicationInfo(pw);
            case "no-home-screen":
                return runNoHomeScreen(pw);
            case "wait-for-broadcast-idle":
                return runWaitForBroadcastIdle(pw);
            case "compat":
                return runCompat(pw);
            case "refresh-settings-cache":
                return runRefreshSettingsCache();
            case "memory-factor":
                return runMemoryFactor(pw);
            case "service-restart-backoff":
                return runServiceRestartBackoff(pw);
            case "get-isolated-pids":
                return runGetIsolatedProcesses(pw);
            case "set-stop-user-on-switch":
                return runSetStopUserOnSwitch(pw);
            case "set-bg-abusive-uids":
                return runSetBgAbusiveUids(pw);
            case "list-bg-exemptions-config":
                return runListBgExemptionsConfig(pw);
            default:
                return handleDefaultCommands(cmd);
        }
    } catch (RemoteException e) {
        pw.println("Remote exception: " + e);
    }
    return -1;
}

4、简易时序图

在这里插入图片描述


http://www.kler.cn/a/107072.html

相关文章:

  • 软件测试面试2024最新热点问题
  • 词嵌入方法(Word Embedding)
  • const限定符-C语言中指针的“可变与不可变”法则
  • Android 下内联汇编,Android Studio 汇编开发
  • 【大数据技术基础 | 实验十】Hive实验:部署Hive
  • Android OpenGL ES详解——纹理:纹理过滤GL_NEAREST和GL_LINEAR的区别
  • 高速下载b站视频的解决方案
  • UI自动化测试是什么?什么项目适合做UI自动化测试
  • 24年FRM备考知识点以及一级公式表
  • 【机器学习】KNN算法-模型选择与调优
  • 力扣每日一题73:矩阵置零
  • 2023CSP-J题解
  • python 字典dict和列表list的读取速度问题, range合并
  • 笔记-《RabbitMQ实战指南》
  • Oracle 数据库的锁排查方法
  • Linux 系统调用IO口,利用光标偏移实现文件复制
  • Kotlin 使用@BindingAdapter编译出错
  • 【微服务开篇-RestTemplate服务调用、Eureka注册中心、Nacos注册中心】
  • VPS是什么?详解亚马逊云科技Amazon Lightsail(VPS)虚拟专用服务器
  • C++模拟实现-----日期计算器(超详细解析,小白一看就会!)
  • Java架构师内功计算机网络
  • LVS集群-DR模式【部署高可用LVS-DR集群】
  • Java SE 学习笔记(十四)—— IO流(3)
  • Java 反射机制详解
  • SQL server中:常见问题汇总(如:修改表时不允许修改表结构、将截断字符串或二进制数据等)
  • 【计算机网络】HTTPS 的加密流程