mobile-insight / mobileinsight-mobile

Mobile Network Intelligence Made Easy -- Android version of MobileInsight app
http://mobileinsight.net
Other
88 stars 55 forks source link

Enhancements to diag_revealer. #35

Closed lrh2000 closed 3 years ago

lrh2000 commented 3 years ago

Hello, several days ago I'm trying to use MobileInsight on Android 10. Unfortunately, I suffered from some confusing random bugs. So I checked the code and tried to find how to fix them.

It's easy to see that those bugs are mainly related to diag_revealer. And this pull request should fix them. I'll explain below what I have done in every commit in detail. (Actually, I have wrote detailed comments in the source code. But I will also explain them here.)

Commit 51a21212bc59d407751b6df6977d7a2bbe4a00e6 (diag_revealer: Format the code.)

The commit only uniformed the code style, such as the indentation character (tabs or spaces) and so on. It also removed a few obviously useless commented code, and moved some comments to a correct place. In a word, this commit does not modify any code essentially (excluding comments, code styles, e.t.c.).

Commit 0d7af7f96e61fe45c11ce1b7fc556978de5ba234 (diag_revealer: Explicitly probe DIAG_IOCTL_SWITCH_LOGGING's argument length.)

It optimized the way to enable logging mode (that is done by a DIAG_IOCTL_SWITCH_LOGGING ioctl system call).

Look at the comments:

    /*
     * Enable logging mode:
     *
     * DIAG_IOCTL_SWITCH_LOGGING has multiple versions. They require different arguments (which have
     * different fields and whose lengths are also different). However, it seems there is no way to
     * directly determine the version of DIAG_IOCTL_SWITCH_LOGGING. So some tricks can not be avoided
     * here.
     *
     * A traditional way is to try one by one. But it can cause undefined behaviour. Specially, when
     * a new verison of DIAG_IOCTL_SWITCH_LOGGING is introduced, it may not report an error. But some
     * new fields will be out of bounds. Consequently, it may cause random bugs, which is confusing.
     *
     * So a more elegant way is to explicitly probe the length of DIAG_IOCTL_SWITCH_LOGGING's argument.
     * And the version can be deduced from the length. It is not very precise, but it is enough at least
     * for now.
     */

Commit 988c40bcb19053d0c3ecd2f1571c2756bf2afc89 (diag_revealer: Fix the method of using libdiag.so to switch logging.)

Although diag_revealer has provided a method of using libdiag.so to switch logging, it can work in no devices that I have tested on. The reason is that a variable (diag_fd or fd in libdiag.so) is not properly set and functions in libdiag.so will do nothing and fail directly. I fixed this.

Meanwhile, libdiag.so will create useless threads and they can prevent us from calling dlclose. A fake pthread_create is added to solve the problem. It works fine at least in all devices that I have tested on (listed below in comments).

/*
 * Calling functions into libdiag.so will create several threads. For example,
 * in diag_switch_logging, three threads (disk_write_hdl, qsr4_db_parser_thread_hdl,
 * db_write_thread_hdl) will be created. But actually we don't need them at all.
 * Meanwhile we cannot call dlclose when these useless threads are still alive.
 * So the following fake pthread_create is used to prevent them from being created.
 *
 * Note this fake pthread_create may cause some unexpected side effects on another
 * untested version of libdiag.so. If so, futher modification is needed.
 *
 * Tested devices:
 *   Xiaomi Mi 5S            Android 7.1.2
 *   Huawei Nexus 6P         Android 8.0.0
 *   Xiaomi Redmi Note 8     Android 10.0.0
 *   Samsung Galaxy A90 5G   Android 10.0.0
 */

Commit 8be5e516501d776683280af25360d35c965e5eb8 (diag_revealer: Android 10 support, and other several fixes.)

It introduced the support of Android 10 DIAG_IOCTL_SWITCH_LOGGING ioctl. The code will simply behave the same as libdiag.so. Other several fixes include fixing Motorola Nexus 6 DIAG_IOCTL_OPTIMIZED_LOGGING ioctl, dealing with problems about linking private library libdiag.so in non-root environment, e.t.c.

Thanks for your review. And looking forward to any feedback (tests, suggestions and so on).