Android Logcat very strange behavior when getting empty string

前端 未结 1 1942
我寻月下人不归
我寻月下人不归 2021-01-22 16:51

So I came across something strange that made me loose some time. I have been trying to print the content of an ArrayList containing string elements, sometimes, an element might

相关标签:
1条回答
  • 2021-01-22 17:41

    That is an interesting question. I just tried this in LogRabbit and am able to see the same result.

    I took a quick browse through the android source and see that Log.W(...) ends up in native code and getting handled in logd_write.c

    This basically writes the data to /dev/log/main (or one of the other logs)

    You can get those logs like this:

    adb pull /dev/log/events .
    adb pull /dev/log/main .
    adb pull /dev/log/radio .
    adb pull /dev/log/system .
    

    You will need to press cntl-C otherwise the copy will happen forever.

    Looking in the raw log in /dev/log/main I see the message does get logged:

     <8b>F×U^_<8c>^Y^U^Emfl_MessageList^@Before Empty^@^R^@^@^@!z^@^@!z^@^@
     <8b>F×U^_<8c>^Y^U^Emfl_MessageList^@^@^]^@^@^@!z^@^@!z^@^@
     <8b>F×U^_ <8c>^Y^U^Emfl_MessageList^@After Empty^@7^@^@^@^@^E^@^@^Z^E^@^@
    

    That gets decoded by struct found in logger.h So I think this is a problem in adb. pull the source code from here: (looks like quite a few of undocumented commands there)

    This is the primary function

    static int logcat(TransportType transport, const char* serial, int argc, const char** argv) {
        char* log_tags = getenv("ANDROID_LOG_TAGS");
        std::string quoted = escape_arg(log_tags == nullptr ? "" : log_tags);
    
        std::string cmd = "shell:export ANDROID_LOG_TAGS=\"" + quoted + "\"; exec logcat";
    
        if (!strcmp(argv[0], "longcat")) {
            cmd += " -v long";
        }
    
        --argc;
        ++argv;
        while (argc-- > 0) {
            cmd += " " + escape_arg(*argv++);
        }
    
        return send_shell_command(transport, serial, cmd);
    }
    

    Looking in there I see that all logcat does is basically this:

    adb shell
    > exec logcat
    

    So I think the root of the problem is in logcat itself. Logcat.cpp calls into log_read.c

    Based on my quick read through things what I think is happening is the message is not terminated properly. The empty message does not show up until another message is appended and the first message overruns and shows the second message because it has the appropriate termination.

    0 讨论(0)
提交回复
热议问题